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

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

Relativize and simplify lookup().

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