source: mainline/uspace/srv/vfs/vfs_ops.c@ b7c62a9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7c62a9 was b7c62a9, checked in by Jiri Zarevucky <zarevucky.jiri@…>, 12 years ago

Make the server oblivious to the link count. It is just another source of potential problems. Also clean up some confusion with file types and node refcount.

  • Property mode set to 100644
File size: 32.9 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33/**
34 * @file vfs_ops.c
35 * @brief Operations that VFS offers to its clients.
36 */
37
38#include "vfs.h"
39#include <macros.h>
40#include <stdint.h>
41#include <async.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <str.h>
46#include <stdbool.h>
47#include <fibril_synch.h>
48#include <adt/list.h>
49#include <unistd.h>
50#include <ctype.h>
51#include <fcntl.h>
52#include <assert.h>
53#include <vfs/canonify.h>
54#include <vfs/vfs_mtab.h>
55
56FIBRIL_MUTEX_INITIALIZE(mtab_list_lock);
57LIST_INITIALIZE(mtab_list);
58static size_t mtab_size = 0;
59
60/* Forward declarations of static functions. */
61static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
62 aoff64_t);
63
64/**
65 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
66 * concurrent VFS operation which modifies the file system namespace.
67 */
68FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
69
70vfs_node_t *root = NULL;
71
72static int vfs_mount_internal(ipc_callid_t rid, service_id_t service_id,
73 fs_handle_t fs_handle, char *mp, char *opts)
74{
75 vfs_lookup_res_t mp_res;
76 vfs_lookup_res_t mr_res;
77 vfs_node_t *mp_node = NULL;
78 vfs_node_t *mr_node;
79 fs_index_t rindex;
80 aoff64_t rsize;
81 async_exch_t *exch;
82 sysarg_t rc;
83 aid_t msg;
84 ipc_call_t answer;
85
86 /* Resolve the path to the mountpoint. */
87 fibril_rwlock_write_lock(&namespace_rwlock);
88 if (root == NULL) {
89 /* We still don't have the root file system mounted. */
90 if (str_cmp(mp, "/") != 0) {
91 /*
92 * We can't resolve this without the root filesystem
93 * being mounted first.
94 */
95 fibril_rwlock_write_unlock(&namespace_rwlock);
96 async_answer_0(rid, ENOENT);
97 return ENOENT;
98 }
99
100 /*
101 * For this simple, but important case,
102 * we are almost done.
103 */
104
105 /* Tell the mountee that it is being mounted. */
106 exch = vfs_exchange_grab(fs_handle);
107 msg = async_send_1(exch, VFS_OUT_MOUNTED,
108 (sysarg_t) service_id, &answer);
109 /* Send the mount options */
110 rc = async_data_write_start(exch, (void *)opts,
111 str_size(opts));
112 vfs_exchange_release(exch);
113
114 if (rc != EOK) {
115 async_forget(msg);
116 fibril_rwlock_write_unlock(&namespace_rwlock);
117 async_answer_0(rid, rc);
118 return rc;
119 }
120 async_wait_for(msg, &rc);
121
122 if (rc != EOK) {
123 fibril_rwlock_write_unlock(&namespace_rwlock);
124 async_answer_0(rid, rc);
125 return rc;
126 }
127
128 rindex = (fs_index_t) IPC_GET_ARG1(answer);
129 rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
130 IPC_GET_ARG3(answer));
131
132 mr_res.triplet.fs_handle = fs_handle;
133 mr_res.triplet.service_id = service_id;
134 mr_res.triplet.index = rindex;
135 mr_res.size = rsize;
136 mr_res.type = VFS_NODE_DIRECTORY;
137
138 /* Add reference to the mounted root. */
139 root = vfs_node_get(&mr_res);
140 assert(root);
141
142 fibril_rwlock_write_unlock(&namespace_rwlock);
143 async_answer_0(rid, rc);
144 return rc;
145 }
146
147 /* We already have the root FS. */
148 if (str_cmp(mp, "/") == 0) {
149 /* Trying to mount root FS over root FS */
150 fibril_rwlock_write_unlock(&namespace_rwlock);
151 async_answer_0(rid, EBUSY);
152 return EBUSY;
153 }
154
155 rc = vfs_lookup_internal((vfs_triplet_t *) root, mp, L_DIRECTORY, &mp_res);
156 if (rc != EOK) {
157 /* The lookup failed. */
158 fibril_rwlock_write_unlock(&namespace_rwlock);
159 async_answer_0(rid, rc);
160 return rc;
161 }
162
163 mp_node = vfs_node_get(&mp_res);
164 if (!mp_node) {
165 fibril_rwlock_write_unlock(&namespace_rwlock);
166 async_answer_0(rid, ENOMEM);
167 return ENOMEM;
168 }
169
170 /*
171 * Now we hold a reference to mp_node.
172 * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
173 * This prevents the mount point from being deleted.
174 */
175
176 /*
177 * At this point, we have all necessary pieces: file system handle
178 * and service ID, and we know the mount point VFS node.
179 */
180
181 async_exch_t *mountee_exch = vfs_exchange_grab(fs_handle);
182 assert(mountee_exch);
183
184 exch = vfs_exchange_grab(mp_res.triplet.fs_handle);
185 msg = async_send_4(exch, VFS_OUT_MOUNT,
186 (sysarg_t) mp_res.triplet.service_id,
187 (sysarg_t) mp_res.triplet.index,
188 (sysarg_t) fs_handle,
189 (sysarg_t) service_id, &answer);
190
191 /* Send connection */
192 rc = async_exchange_clone(exch, mountee_exch);
193 vfs_exchange_release(mountee_exch);
194
195 if (rc != EOK) {
196 vfs_exchange_release(exch);
197 async_forget(msg);
198
199 /* Mount failed, drop reference to mp_node. */
200 if (mp_node)
201 vfs_node_put(mp_node);
202
203 async_answer_0(rid, rc);
204 fibril_rwlock_write_unlock(&namespace_rwlock);
205 return rc;
206 }
207
208 /* send the mount options */
209 rc = async_data_write_start(exch, (void *) opts, str_size(opts));
210 if (rc != EOK) {
211 vfs_exchange_release(exch);
212 async_forget(msg);
213
214 /* Mount failed, drop reference to mp_node. */
215 if (mp_node)
216 vfs_node_put(mp_node);
217
218 fibril_rwlock_write_unlock(&namespace_rwlock);
219 async_answer_0(rid, rc);
220 return rc;
221 }
222
223 /*
224 * Wait for the answer before releasing the exchange to avoid deadlock
225 * in case the answer depends on further calls to the same file system.
226 * Think of a case when mounting a FS on a file_bd backed by a file on
227 * the same FS.
228 */
229 async_wait_for(msg, &rc);
230 vfs_exchange_release(exch);
231
232 if (rc == EOK) {
233 rindex = (fs_index_t) IPC_GET_ARG1(answer);
234 rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
235 IPC_GET_ARG3(answer));
236
237 mr_res.triplet.fs_handle = fs_handle;
238 mr_res.triplet.service_id = service_id;
239 mr_res.triplet.index = rindex;
240 mr_res.size = rsize;
241 mr_res.type = VFS_NODE_DIRECTORY;
242
243 /* Add reference to the mounted root. */
244 mr_node = vfs_node_get(&mr_res);
245 assert(mr_node);
246 } else {
247 /* Mount failed, drop reference to mp_node. */
248 if (mp_node)
249 vfs_node_put(mp_node);
250 }
251
252 async_answer_0(rid, rc);
253 fibril_rwlock_write_unlock(&namespace_rwlock);
254 return rc;
255}
256
257void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
258{
259 service_id_t service_id;
260
261 /*
262 * We expect the library to do the device-name to device-handle
263 * translation for us, thus the device handle will arrive as ARG1
264 * in the request.
265 */
266 service_id = (service_id_t) IPC_GET_ARG1(*request);
267
268 /*
269 * Mount flags are passed as ARG2.
270 */
271 unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
272
273 /*
274 * Instance number is passed as ARG3.
275 */
276 unsigned int instance = IPC_GET_ARG3(*request);
277
278 /* We want the client to send us the mount point. */
279 char *mp;
280 int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
281 0, NULL);
282 if (rc != EOK) {
283 async_answer_0(rid, rc);
284 return;
285 }
286
287 /* Now we expect to receive the mount options. */
288 char *opts;
289 rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
290 0, NULL);
291 if (rc != EOK) {
292 free(mp);
293 async_answer_0(rid, rc);
294 return;
295 }
296
297 /*
298 * Now, we expect the client to send us data with the name of the file
299 * system.
300 */
301 char *fs_name;
302 rc = async_data_write_accept((void **) &fs_name, true, 0,
303 FS_NAME_MAXLEN, 0, NULL);
304 if (rc != EOK) {
305 free(mp);
306 free(opts);
307 async_answer_0(rid, rc);
308 return;
309 }
310
311 /*
312 * Wait for VFS_IN_PING so that we can return an error if we don't know
313 * fs_name.
314 */
315 ipc_call_t data;
316 ipc_callid_t callid = async_get_call(&data);
317 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
318 async_answer_0(callid, ENOTSUP);
319 async_answer_0(rid, ENOTSUP);
320 free(mp);
321 free(opts);
322 free(fs_name);
323 return;
324 }
325
326 /*
327 * Check if we know a file system with the same name as is in fs_name.
328 * This will also give us its file system handle.
329 */
330 fibril_mutex_lock(&fs_list_lock);
331 fs_handle_t fs_handle;
332recheck:
333 fs_handle = fs_name_to_handle(instance, fs_name, false);
334 if (!fs_handle) {
335 if (flags & IPC_FLAG_BLOCKING) {
336 fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
337 goto recheck;
338 }
339
340 fibril_mutex_unlock(&fs_list_lock);
341 async_answer_0(callid, ENOENT);
342 async_answer_0(rid, ENOENT);
343 free(mp);
344 free(fs_name);
345 free(opts);
346 return;
347 }
348 fibril_mutex_unlock(&fs_list_lock);
349
350 /* Add the filesystem info to the list of mounted filesystems */
351 mtab_ent_t *mtab_ent = malloc(sizeof(mtab_ent_t));
352 if (!mtab_ent) {
353 async_answer_0(callid, ENOMEM);
354 async_answer_0(rid, ENOMEM);
355 free(mp);
356 free(fs_name);
357 free(opts);
358 return;
359 }
360
361 /* Do the mount */
362 rc = vfs_mount_internal(rid, service_id, fs_handle, mp, opts);
363 if (rc != EOK) {
364 async_answer_0(callid, ENOTSUP);
365 async_answer_0(rid, ENOTSUP);
366 free(mtab_ent);
367 free(mp);
368 free(opts);
369 free(fs_name);
370 return;
371 }
372
373 /* Add the filesystem info to the list of mounted filesystems */
374
375 str_cpy(mtab_ent->mp, MAX_PATH_LEN, mp);
376 str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
377 str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
378 mtab_ent->instance = instance;
379 mtab_ent->service_id = service_id;
380
381 link_initialize(&mtab_ent->link);
382
383 fibril_mutex_lock(&mtab_list_lock);
384 list_append(&mtab_ent->link, &mtab_list);
385 mtab_size++;
386 fibril_mutex_unlock(&mtab_list_lock);
387
388 free(mp);
389 free(fs_name);
390 free(opts);
391
392 /* Acknowledge that we know fs_name. */
393 async_answer_0(callid, EOK);
394}
395
396void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
397{
398 int rc;
399 char *mp;
400 vfs_lookup_res_t mp_res;
401 vfs_lookup_res_t mr_res;
402 vfs_node_t *mr_node;
403 async_exch_t *exch;
404
405 /*
406 * Receive the mount point path.
407 */
408 rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
409 0, NULL);
410 if (rc != EOK)
411 async_answer_0(rid, rc);
412
413 /*
414 * Taking the namespace lock will do two things for us. First, it will
415 * prevent races with other lookup operations. Second, it will stop new
416 * references to already existing VFS nodes and creation of new VFS
417 * nodes. This is because new references are added as a result of some
418 * lookup operation or at least of some operation which is protected by
419 * the namespace lock.
420 */
421 fibril_rwlock_write_lock(&namespace_rwlock);
422
423 /*
424 * Lookup the mounted root and instantiate it.
425 */
426 rc = vfs_lookup_internal((vfs_triplet_t *) root, mp, 0, &mr_res);
427 if (rc != EOK) {
428 fibril_rwlock_write_unlock(&namespace_rwlock);
429 free(mp);
430 async_answer_0(rid, rc);
431 return;
432 }
433 mr_node = vfs_node_get(&mr_res);
434 if (!mr_node) {
435 fibril_rwlock_write_unlock(&namespace_rwlock);
436 free(mp);
437 async_answer_0(rid, ENOMEM);
438 return;
439 }
440
441 /*
442 * Count the total number of references for the mounted file system. We
443 * are expecting at least two. One which we got above and one which we
444 * got when the file system was mounted. If we find more, it means that
445 * the file system cannot be gracefully unmounted at the moment because
446 * someone is working with it.
447 */
448 if (vfs_nodes_refcount_sum_get(mr_node->fs_handle,
449 mr_node->service_id) != 2) {
450 fibril_rwlock_write_unlock(&namespace_rwlock);
451 vfs_node_put(mr_node);
452 free(mp);
453 async_answer_0(rid, EBUSY);
454 return;
455 }
456
457 if (str_cmp(mp, "/") == 0) {
458
459 /*
460 * Unmounting the root file system.
461 *
462 * In this case, there is no mount point node and we send
463 * VFS_OUT_UNMOUNTED directly to the mounted file system.
464 */
465
466 exch = vfs_exchange_grab(mr_node->fs_handle);
467 rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
468 mr_node->service_id);
469 vfs_exchange_release(exch);
470
471 if (rc != EOK) {
472 fibril_rwlock_write_unlock(&namespace_rwlock);
473 free(mp);
474 vfs_node_put(mr_node);
475 async_answer_0(rid, rc);
476 return;
477 }
478
479 root = NULL;
480 } else {
481
482 /*
483 * Unmounting a non-root file system.
484 *
485 * We have a regular mount point node representing the parent
486 * file system, so we delegate the operation to it.
487 */
488
489 rc = vfs_lookup_internal((vfs_triplet_t *) root, mp, L_MP, &mp_res);
490 if (rc != EOK) {
491 fibril_rwlock_write_unlock(&namespace_rwlock);
492 free(mp);
493 vfs_node_put(mr_node);
494 async_answer_0(rid, rc);
495 return;
496 }
497
498 vfs_node_t *mp_node = vfs_node_get(&mp_res);
499 if (!mp_node) {
500 fibril_rwlock_write_unlock(&namespace_rwlock);
501 free(mp);
502 vfs_node_put(mr_node);
503 async_answer_0(rid, ENOMEM);
504 return;
505 }
506
507 exch = vfs_exchange_grab(mp_node->fs_handle);
508 rc = async_req_2_0(exch, VFS_OUT_UNMOUNT,
509 mp_node->service_id, mp_node->index);
510 vfs_exchange_release(exch);
511
512 if (rc != EOK) {
513 fibril_rwlock_write_unlock(&namespace_rwlock);
514 free(mp);
515 vfs_node_put(mp_node);
516 vfs_node_put(mr_node);
517 async_answer_0(rid, rc);
518 return;
519 }
520
521 /* Drop the reference we got above. */
522 vfs_node_put(mp_node);
523 /* Drop the reference from when the file system was mounted. */
524 vfs_node_put(mp_node);
525 }
526
527 /*
528 * All went well, the mounted file system was successfully unmounted.
529 * The only thing left is to forget the unmounted root VFS node.
530 */
531 vfs_node_forget(mr_node);
532 fibril_rwlock_write_unlock(&namespace_rwlock);
533
534 fibril_mutex_lock(&mtab_list_lock);
535
536 int found = 0;
537
538 list_foreach(mtab_list, cur) {
539 mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
540 link);
541
542 if (str_cmp(mtab_ent->mp, mp) == 0) {
543 list_remove(&mtab_ent->link);
544 mtab_size--;
545 free(mtab_ent);
546 found = 1;
547 break;
548 }
549 }
550 assert(found);
551 fibril_mutex_unlock(&mtab_list_lock);
552
553 free(mp);
554
555 async_answer_0(rid, EOK);
556}
557
558static inline bool walk_flags_valid(int flags)
559{
560 if ((flags&~WALK_ALL_FLAGS) != 0) {
561 return false;
562 }
563 if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
564 return false;
565 }
566 if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
567 return false;
568 }
569 if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
570 if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
571 return false;
572 }
573 }
574 return true;
575}
576
577static inline int walk_lookup_flags(int flags)
578{
579 int lflags = 0;
580 if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
581 lflags |= L_CREATE;
582 }
583 if (flags&WALK_MUST_CREATE) {
584 lflags |= L_EXCLUSIVE;
585 }
586 if (flags&WALK_REGULAR) {
587 lflags |= L_FILE;
588 }
589 if (flags&WALK_DIRECTORY) {
590 lflags |= L_DIRECTORY;
591 }
592 return lflags;
593}
594
595void vfs_walk(ipc_callid_t rid, ipc_call_t *request)
596{
597 /*
598 * Parent is our relative root for file lookup.
599 * For defined flags, see <ipc/vfs.h>.
600 */
601 int parentfd = IPC_GET_ARG1(*request);
602 int flags = IPC_GET_ARG2(*request);
603
604 if (!walk_flags_valid(flags)) {
605 async_answer_0(rid, EINVAL);
606 return;
607 }
608
609 char *path;
610 int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
611
612 /* Lookup the file structure corresponding to the file descriptor. */
613 vfs_file_t *parent = NULL;
614 vfs_node_t *parent_node = root;
615 // TODO: Client-side root.
616 if (parentfd != -1) {
617 parent = vfs_file_get(parentfd);
618 if (!parent) {
619 free(path);
620 async_answer_0(rid, EBADF);
621 return;
622 }
623 parent_node = parent->node;
624 }
625
626 fibril_rwlock_read_lock(&namespace_rwlock);
627
628 vfs_lookup_res_t lr;
629 rc = vfs_lookup_internal((vfs_triplet_t *) parent_node, path, walk_lookup_flags(flags), &lr);
630 free(path);
631
632 if (rc != EOK) {
633 fibril_rwlock_read_unlock(&namespace_rwlock);
634 if (parent) {
635 vfs_file_put(parent);
636 }
637 async_answer_0(rid, rc);
638 return;
639 }
640
641 vfs_node_t *node = vfs_node_get(&lr);
642
643 int fd = vfs_fd_alloc(false);
644 if (fd < 0) {
645 vfs_node_put(node);
646 if (parent) {
647 vfs_file_put(parent);
648 }
649 async_answer_0(rid, fd);
650 return;
651 }
652
653 vfs_file_t *file = vfs_file_get(fd);
654 assert(file != NULL);
655
656 file->node = node;
657 if (parent) {
658 file->permissions = parent->permissions;
659 } else {
660 file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
661 }
662 file->open_read = false;
663 file->open_write = false;
664
665 vfs_file_put(file);
666 if (parent) {
667 vfs_file_put(parent);
668 }
669
670 fibril_rwlock_read_unlock(&namespace_rwlock);
671
672 async_answer_1(rid, EOK, fd);
673}
674
675void vfs_open2(ipc_callid_t rid, ipc_call_t *request)
676{
677 int fd = IPC_GET_ARG1(*request);
678 int flags = IPC_GET_ARG2(*request);
679
680 if (flags == 0) {
681 async_answer_0(rid, EINVAL);
682 return;
683 }
684
685 vfs_file_t *file = vfs_file_get(fd);
686 if (!file) {
687 async_answer_0(rid, EBADF);
688 return;
689 }
690
691 if ((flags & ~file->permissions) != 0) {
692 vfs_file_put(file);
693 async_answer_0(rid, EPERM);
694 return;
695 }
696
697 file->open_read = (flags & MODE_READ) != 0;
698 file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
699 file->append = (flags & MODE_APPEND) != 0;
700
701 if (!file->open_read && !file->open_write) {
702 vfs_file_put(file);
703 async_answer_0(rid, EINVAL);
704 return;
705 }
706
707 if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
708 file->open_read = file->open_write = false;
709 vfs_file_put(file);
710 async_answer_0(rid, EINVAL);
711 return;
712 }
713
714 int rc = vfs_open_node_remote(file->node);
715 if (rc != EOK) {
716 file->open_read = file->open_write = false;
717 vfs_file_put(file);
718 async_answer_0(rid, rc);
719 return;
720 }
721
722 vfs_file_put(file);
723 async_answer_0(rid, EOK);
724}
725
726void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
727{
728 int fd = IPC_GET_ARG1(*request);
729
730 /* Lookup the file structure corresponding to the file descriptor. */
731 vfs_file_t *file = vfs_file_get(fd);
732 if (!file) {
733 async_answer_0(rid, ENOENT);
734 return;
735 }
736
737 /*
738 * Lock the open file structure so that no other thread can manipulate
739 * the same open file at a time.
740 */
741 fibril_mutex_lock(&file->lock);
742 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
743
744 /* Make a VFS_OUT_SYMC request at the destination FS server. */
745 aid_t msg;
746 ipc_call_t answer;
747 msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
748 file->node->index, &answer);
749
750 vfs_exchange_release(fs_exch);
751
752 /* Wait for reply from the FS server. */
753 sysarg_t rc;
754 async_wait_for(msg, &rc);
755
756 fibril_mutex_unlock(&file->lock);
757
758 vfs_file_put(file);
759 async_answer_0(rid, rc);
760}
761
762void vfs_close(ipc_callid_t rid, ipc_call_t *request)
763{
764 int fd = IPC_GET_ARG1(*request);
765 int ret = vfs_fd_free(fd);
766 async_answer_0(rid, ret);
767}
768
769static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
770{
771 /*
772 * The following code strongly depends on the fact that the files data
773 * structure can be only accessed by a single fibril and all file
774 * operations are serialized (i.e. the reads and writes cannot
775 * interleave and a file cannot be closed while it is being read).
776 *
777 * Additional synchronization needs to be added once the table of
778 * open files supports parallel access!
779 */
780
781 int fd = IPC_GET_ARG1(*request);
782
783 /* Lookup the file structure corresponding to the file descriptor. */
784 vfs_file_t *file = vfs_file_get(fd);
785 if (!file) {
786 async_answer_0(rid, ENOENT);
787 return;
788 }
789
790 /*
791 * Lock the open file structure so that no other thread can manipulate
792 * the same open file at a time.
793 */
794 fibril_mutex_lock(&file->lock);
795
796 if ((read && !file->open_read) || (!read && !file->open_write)) {
797 fibril_mutex_unlock(&file->lock);
798 async_answer_0(rid, EINVAL);
799 return;
800 }
801
802 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
803 assert(fs_info);
804
805 /*
806 * Lock the file's node so that no other client can read/write to it at
807 * the same time unless the FS supports concurrent reads/writes and its
808 * write implementation does not modify the file size.
809 */
810 if ((read) ||
811 ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
812 fibril_rwlock_read_lock(&file->node->contents_rwlock);
813 else
814 fibril_rwlock_write_lock(&file->node->contents_rwlock);
815
816 if (file->node->type == VFS_NODE_DIRECTORY) {
817 /*
818 * Make sure that no one is modifying the namespace
819 * while we are in readdir().
820 */
821 assert(read);
822 fibril_rwlock_read_lock(&namespace_rwlock);
823 }
824
825 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
826
827 /*
828 * Make a VFS_READ/VFS_WRITE request at the destination FS server
829 * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
830 * destination FS server. The call will be routed as if sent by
831 * ourselves. Note that call arguments are immutable in this case so we
832 * don't have to bother.
833 */
834 sysarg_t rc;
835 ipc_call_t answer;
836 if (read) {
837 rc = async_data_read_forward_4_1(fs_exch, VFS_OUT_READ,
838 file->node->service_id, file->node->index,
839 LOWER32(file->pos), UPPER32(file->pos), &answer);
840 } else {
841 if (file->append)
842 file->pos = file->node->size;
843
844 rc = async_data_write_forward_4_1(fs_exch, VFS_OUT_WRITE,
845 file->node->service_id, file->node->index,
846 LOWER32(file->pos), UPPER32(file->pos), &answer);
847 }
848
849 vfs_exchange_release(fs_exch);
850
851 size_t bytes = IPC_GET_ARG1(answer);
852
853 if (file->node->type == VFS_NODE_DIRECTORY) {
854 fibril_rwlock_read_unlock(&namespace_rwlock);
855 }
856
857 /* Unlock the VFS node. */
858 if ((read) ||
859 ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
860 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
861 else {
862 /* Update the cached version of node's size. */
863 if (rc == EOK)
864 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
865 IPC_GET_ARG3(answer));
866 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
867 }
868
869 /* Update the position pointer and unlock the open file. */
870 if (rc == EOK)
871 file->pos += bytes;
872 fibril_mutex_unlock(&file->lock);
873 vfs_file_put(file);
874
875 /*
876 * FS server's reply is the final result of the whole operation we
877 * return to the client.
878 */
879 async_answer_1(rid, rc, bytes);
880}
881
882void vfs_read(ipc_callid_t rid, ipc_call_t *request)
883{
884 vfs_rdwr(rid, request, true);
885}
886
887void vfs_write(ipc_callid_t rid, ipc_call_t *request)
888{
889 vfs_rdwr(rid, request, false);
890}
891
892void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
893{
894 int fd = (int) IPC_GET_ARG1(*request);
895 off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
896 IPC_GET_ARG3(*request));
897 int whence = (int) IPC_GET_ARG4(*request);
898
899 /* Lookup the file structure corresponding to the file descriptor. */
900 vfs_file_t *file = vfs_file_get(fd);
901 if (!file) {
902 async_answer_0(rid, ENOENT);
903 return;
904 }
905
906 fibril_mutex_lock(&file->lock);
907
908 off64_t newoff;
909 switch (whence) {
910 case SEEK_SET:
911 if (off >= 0) {
912 file->pos = (aoff64_t) off;
913 fibril_mutex_unlock(&file->lock);
914 vfs_file_put(file);
915 async_answer_1(rid, EOK, off);
916 return;
917 }
918 break;
919 case SEEK_CUR:
920 if ((off >= 0) && (file->pos + off < file->pos)) {
921 fibril_mutex_unlock(&file->lock);
922 vfs_file_put(file);
923 async_answer_0(rid, EOVERFLOW);
924 return;
925 }
926
927 if ((off < 0) && (file->pos < (aoff64_t) -off)) {
928 fibril_mutex_unlock(&file->lock);
929 vfs_file_put(file);
930 async_answer_0(rid, EOVERFLOW);
931 return;
932 }
933
934 file->pos += off;
935 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
936
937 fibril_mutex_unlock(&file->lock);
938 vfs_file_put(file);
939 async_answer_2(rid, EOK, LOWER32(newoff),
940 UPPER32(newoff));
941 return;
942 case SEEK_END:
943 fibril_rwlock_read_lock(&file->node->contents_rwlock);
944 aoff64_t size = file->node->size;
945
946 if ((off >= 0) && (size + off < size)) {
947 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
948 fibril_mutex_unlock(&file->lock);
949 vfs_file_put(file);
950 async_answer_0(rid, EOVERFLOW);
951 return;
952 }
953
954 if ((off < 0) && (size < (aoff64_t) -off)) {
955 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
956 fibril_mutex_unlock(&file->lock);
957 vfs_file_put(file);
958 async_answer_0(rid, EOVERFLOW);
959 return;
960 }
961
962 file->pos = size + off;
963 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
964
965 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
966 fibril_mutex_unlock(&file->lock);
967 vfs_file_put(file);
968 async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
969 return;
970 }
971
972 fibril_mutex_unlock(&file->lock);
973 vfs_file_put(file);
974 async_answer_0(rid, EINVAL);
975}
976
977int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
978 fs_index_t index, aoff64_t size)
979{
980 async_exch_t *exch = vfs_exchange_grab(fs_handle);
981 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
982 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
983 UPPER32(size));
984 vfs_exchange_release(exch);
985
986 return (int) rc;
987}
988
989void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
990{
991 int fd = IPC_GET_ARG1(*request);
992 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
993 IPC_GET_ARG3(*request));
994 int rc;
995
996 vfs_file_t *file = vfs_file_get(fd);
997 if (!file) {
998 async_answer_0(rid, ENOENT);
999 return;
1000 }
1001 fibril_mutex_lock(&file->lock);
1002
1003 fibril_rwlock_write_lock(&file->node->contents_rwlock);
1004 rc = vfs_truncate_internal(file->node->fs_handle,
1005 file->node->service_id, file->node->index, size);
1006 if (rc == EOK)
1007 file->node->size = size;
1008 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
1009
1010 fibril_mutex_unlock(&file->lock);
1011 vfs_file_put(file);
1012 async_answer_0(rid, (sysarg_t)rc);
1013}
1014
1015void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
1016{
1017 int fd = IPC_GET_ARG1(*request);
1018 sysarg_t rc;
1019
1020 vfs_file_t *file = vfs_file_get(fd);
1021 if (!file) {
1022 async_answer_0(rid, ENOENT);
1023 return;
1024 }
1025
1026 ipc_callid_t callid;
1027 if (!async_data_read_receive(&callid, NULL)) {
1028 vfs_file_put(file);
1029 async_answer_0(callid, EINVAL);
1030 async_answer_0(rid, EINVAL);
1031 return;
1032 }
1033
1034 fibril_mutex_lock(&file->lock);
1035
1036 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
1037
1038 aid_t msg;
1039 msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
1040 file->node->index, true, NULL);
1041 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1042
1043 vfs_exchange_release(exch);
1044
1045 async_wait_for(msg, &rc);
1046
1047 fibril_mutex_unlock(&file->lock);
1048 vfs_file_put(file);
1049 async_answer_0(rid, rc);
1050}
1051
1052void vfs_unlink2(ipc_callid_t rid, ipc_call_t *request)
1053{
1054 int rc;
1055 char *path;
1056 vfs_file_t *parent = NULL;
1057 vfs_file_t *expect = NULL;
1058 vfs_node_t *parent_node = root;
1059
1060 int parentfd = IPC_GET_ARG1(*request);
1061 int expectfd = IPC_GET_ARG2(*request);
1062 int wflag = IPC_GET_ARG3(*request);
1063
1064 rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
1065 if (rc != EOK) {
1066 async_answer_0(rid, rc);
1067 return;
1068 }
1069
1070 fibril_rwlock_write_lock(&namespace_rwlock);
1071
1072 int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
1073
1074 if (parentfd >= 0) {
1075 parent = vfs_file_get(parentfd);
1076 if (!parent) {
1077 rc = ENOENT;
1078 goto exit;
1079 }
1080 parent_node = parent->node;
1081 }
1082
1083 if (expectfd >= 0) {
1084 expect = vfs_file_get(expectfd);
1085 if (!expect) {
1086 rc = ENOENT;
1087 goto exit;
1088 }
1089
1090 vfs_lookup_res_t lr;
1091 rc = vfs_lookup_internal((vfs_triplet_t *) parent_node, path, lflag, &lr);
1092 if (rc != EOK) {
1093 goto exit;
1094 }
1095
1096 if (__builtin_memcmp(&lr.triplet, expect->node, sizeof(vfs_triplet_t)) != 0) {
1097 rc = ENOENT;
1098 goto exit;
1099 }
1100
1101 vfs_file_put(expect);
1102 expect = NULL;
1103 }
1104
1105 vfs_lookup_res_t lr;
1106 rc = vfs_lookup_internal((vfs_triplet_t *) parent_node, path, lflag | L_UNLINK, &lr);
1107 if (rc != EOK) {
1108 goto exit;
1109 }
1110
1111 /*
1112 * The name has already been unlinked by vfs_lookup_internal().
1113 * We have to get and put the VFS node to ensure that it is
1114 * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
1115 */
1116 vfs_node_put(vfs_node_get(&lr));
1117
1118exit:
1119 if (path) {
1120 free(path);
1121 }
1122 if (parent) {
1123 vfs_file_put(parent);
1124 }
1125 if (expect) {
1126 vfs_file_put(expect);
1127 }
1128 fibril_rwlock_write_unlock(&namespace_rwlock);
1129 async_answer_0(rid, rc);
1130}
1131
1132static size_t shared_path(char *a, char *b)
1133{
1134 size_t res = 0;
1135
1136 while (a[res] == b[res] && a[res] != 0) {
1137 res++;
1138 }
1139
1140 if (a[res] == b[res]) {
1141 return res;
1142 }
1143
1144 res--;
1145 while (a[res] != '/') {
1146 res--;
1147 }
1148 return res;
1149}
1150
1151static int vfs_rename_internal(vfs_triplet_t *base, char *old, char *new)
1152{
1153 assert(base != NULL);
1154 assert(old != NULL);
1155 assert(new != NULL);
1156
1157 vfs_lookup_res_t base_lr;
1158 vfs_lookup_res_t old_lr;
1159 vfs_lookup_res_t new_lr_orig;
1160 bool orig_unlinked = false;
1161
1162 int rc;
1163
1164 size_t shared = shared_path(old, new);
1165
1166 /* Do not allow one path to be a prefix of the other. */
1167 if (old[shared] == 0 || new[shared] == 0) {
1168 return EINVAL;
1169 }
1170 assert(old[shared] == '/');
1171 assert(new[shared] == '/');
1172
1173 fibril_rwlock_write_lock(&namespace_rwlock);
1174
1175 /* Resolve the shared portion of the path first. */
1176 if (shared != 0) {
1177 old[shared] = 0;
1178 rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
1179 if (rc != EOK) {
1180 fibril_rwlock_write_unlock(&namespace_rwlock);
1181 return rc;
1182 }
1183
1184 base = &base_lr.triplet;
1185 old[shared] = '/';
1186 old += shared;
1187 new += shared;
1188 }
1189
1190
1191 rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS, &new_lr_orig);
1192 if (rc == EOK) {
1193 orig_unlinked = true;
1194 } else if (rc != ENOENT) {
1195 fibril_rwlock_write_unlock(&namespace_rwlock);
1196 return rc;
1197 }
1198
1199 rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS, &old_lr);
1200 if (rc != EOK) {
1201 if (orig_unlinked) {
1202 vfs_link_internal(base, new, &new_lr_orig.triplet);
1203 }
1204 fibril_rwlock_write_unlock(&namespace_rwlock);
1205 return rc;
1206 }
1207
1208 rc = vfs_link_internal(base, new, &old_lr.triplet);
1209 if (rc != EOK) {
1210 vfs_link_internal(base, old, &old_lr.triplet);
1211 if (orig_unlinked) {
1212 vfs_link_internal(base, new, &new_lr_orig.triplet);
1213 }
1214 fibril_rwlock_write_unlock(&namespace_rwlock);
1215 return rc;
1216 }
1217
1218 if (orig_unlinked) {
1219 vfs_node_put(vfs_node_get(&new_lr_orig));
1220 }
1221
1222 fibril_rwlock_write_unlock(&namespace_rwlock);
1223 return EOK;
1224}
1225
1226void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1227{
1228 /* The common base directory. */
1229 int basefd;
1230 char *old = NULL;
1231 char *new = NULL;
1232 vfs_file_t *base = NULL;
1233 int rc;
1234
1235 basefd = IPC_GET_ARG1(*request);
1236
1237 /* Retrieve the old path. */
1238 rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
1239 if (rc != EOK) {
1240 goto out;
1241 }
1242
1243 /* Retrieve the new path. */
1244 rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
1245 if (rc != EOK) {
1246 goto out;
1247 }
1248
1249 size_t olen;
1250 size_t nlen;
1251 char *oldc = canonify(old, &olen);
1252 char *newc = canonify(new, &nlen);
1253
1254 if ((!oldc) || (!newc)) {
1255 rc = EINVAL;
1256 goto out;
1257 }
1258
1259 assert(oldc[olen] == '\0');
1260 assert(newc[nlen] == '\0');
1261
1262 /* Lookup the file structure corresponding to the file descriptor. */
1263 vfs_node_t *base_node = root;
1264 // TODO: Client-side root.
1265 if (basefd != -1) {
1266 base = vfs_file_get(basefd);
1267 if (!base) {
1268 rc = EBADF;
1269 goto out;
1270 }
1271 base_node = base->node;
1272 }
1273
1274 rc = vfs_rename_internal((vfs_triplet_t *) base_node, oldc, newc);
1275
1276out:
1277 async_answer_0(rid, rc);
1278
1279 if (old) {
1280 free(old);
1281 }
1282 if (new) {
1283 free(new);
1284 }
1285 if (base) {
1286 vfs_file_put(base);
1287 }
1288}
1289
1290void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1291{
1292 int oldfd = IPC_GET_ARG1(*request);
1293 int newfd = IPC_GET_ARG2(*request);
1294
1295 /* If the file descriptors are the same, do nothing. */
1296 if (oldfd == newfd) {
1297 async_answer_1(rid, EOK, newfd);
1298 return;
1299 }
1300
1301 /* Lookup the file structure corresponding to oldfd. */
1302 vfs_file_t *oldfile = vfs_file_get(oldfd);
1303 if (!oldfile) {
1304 async_answer_0(rid, EBADF);
1305 return;
1306 }
1307
1308 /*
1309 * Lock the open file structure so that no other thread can manipulate
1310 * the same open file at a time.
1311 */
1312 fibril_mutex_lock(&oldfile->lock);
1313
1314 /* Make sure newfd is closed. */
1315 (void) vfs_fd_free(newfd);
1316
1317 /* Assign the old file to newfd. */
1318 int ret = vfs_fd_assign(oldfile, newfd);
1319 fibril_mutex_unlock(&oldfile->lock);
1320 vfs_file_put(oldfile);
1321
1322 if (ret != EOK)
1323 async_answer_0(rid, ret);
1324 else
1325 async_answer_1(rid, EOK, newfd);
1326}
1327
1328void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
1329{
1330 int fd = vfs_wait_handle_internal();
1331 async_answer_1(rid, EOK, fd);
1332}
1333
1334void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
1335{
1336 ipc_callid_t callid;
1337 ipc_call_t data;
1338 sysarg_t rc = EOK;
1339 size_t len;
1340
1341 fibril_mutex_lock(&mtab_list_lock);
1342
1343 /* Send to the caller the number of mounted filesystems */
1344 callid = async_get_call(&data);
1345 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
1346 rc = ENOTSUP;
1347 async_answer_0(callid, rc);
1348 goto exit;
1349 }
1350 async_answer_1(callid, EOK, mtab_size);
1351
1352 list_foreach(mtab_list, cur) {
1353 mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
1354 link);
1355
1356 rc = ENOTSUP;
1357
1358 if (!async_data_read_receive(&callid, &len)) {
1359 async_answer_0(callid, rc);
1360 goto exit;
1361 }
1362
1363 (void) async_data_read_finalize(callid, mtab_ent->mp,
1364 str_size(mtab_ent->mp));
1365
1366 if (!async_data_read_receive(&callid, &len)) {
1367 async_answer_0(callid, rc);
1368 goto exit;
1369 }
1370
1371 (void) async_data_read_finalize(callid, mtab_ent->opts,
1372 str_size(mtab_ent->opts));
1373
1374 if (!async_data_read_receive(&callid, &len)) {
1375 async_answer_0(callid, rc);
1376 goto exit;
1377 }
1378
1379 (void) async_data_read_finalize(callid, mtab_ent->fs_name,
1380 str_size(mtab_ent->fs_name));
1381
1382 callid = async_get_call(&data);
1383
1384 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
1385 async_answer_0(callid, rc);
1386 goto exit;
1387 }
1388
1389 rc = EOK;
1390 async_answer_2(callid, rc, mtab_ent->instance,
1391 mtab_ent->service_id);
1392 }
1393
1394exit:
1395 fibril_mutex_unlock(&mtab_list_lock);
1396 async_answer_0(rid, rc);
1397}
1398
1399/**
1400 * @}
1401 */
Note: See TracBrowser for help on using the repository browser.