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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9246016 was f77c1c9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Return VFS handles separately from error codes.

  • Property mode set to 100644
File size: 21.1 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 <ctype.h>
50#include <assert.h>
51#include <vfs/canonify.h>
52
53/* Forward declarations of static functions. */
54static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
55 aoff64_t);
56
57/**
58 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
59 * concurrent VFS operation which modifies the file system namespace.
60 */
61FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
62
63static size_t shared_path(char *a, char *b)
64{
65 size_t res = 0;
66
67 while (a[res] == b[res] && a[res] != 0)
68 res++;
69
70 if (a[res] == b[res])
71 return res;
72
73 res--;
74 while (a[res] != '/')
75 res--;
76 return res;
77}
78
79/* This call destroys the file if and only if there are no hard links left. */
80static void out_destroy(vfs_triplet_t *file)
81{
82 async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
83 async_msg_2(exch, VFS_OUT_DESTROY, (sysarg_t) file->service_id,
84 (sysarg_t) file->index);
85 vfs_exchange_release(exch);
86}
87
88int vfs_op_clone(int oldfd, int newfd, bool desc, int *out_fd)
89{
90 int rc;
91
92 /* If the file descriptors are the same, do nothing. */
93 if (oldfd == newfd)
94 return EOK;
95
96 /* Lookup the file structure corresponding to fd. */
97 vfs_file_t *oldfile = vfs_file_get(oldfd);
98 if (oldfile == NULL)
99 return EBADF;
100
101 assert(oldfile->node != NULL);
102
103 if (newfd != -1) {
104 /* Assign the old file to newfd. */
105 rc = vfs_fd_assign(oldfile, newfd);
106 } else {
107 vfs_file_t *newfile;
108 int newfd = vfs_fd_alloc(&newfile, desc);
109 if (newfd >= 0) {
110 newfile->node = oldfile->node;
111 newfile->permissions = oldfile->permissions;
112 vfs_node_addref(newfile->node);
113
114 vfs_file_put(newfile);
115 }
116 rc = newfd;
117 }
118 vfs_file_put(oldfile);
119
120 if (rc < 0) {
121 return rc;
122 }
123
124 *out_fd = rc;
125 return EOK;
126}
127
128int vfs_op_put(int fd)
129{
130 return vfs_fd_free(fd);
131}
132
133static int vfs_connect_internal(service_id_t service_id, unsigned flags,
134 unsigned instance, const char *options, const char *fsname,
135 vfs_node_t **root)
136{
137 fs_handle_t fs_handle = 0;
138
139 fibril_mutex_lock(&fs_list_lock);
140 while (true) {
141 fs_handle = fs_name_to_handle(instance, fsname, false);
142
143 if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING))
144 break;
145
146 fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
147 }
148 fibril_mutex_unlock(&fs_list_lock);
149
150 if (fs_handle == 0)
151 return ENOFS;
152
153 /* Tell the mountee that it is being mounted. */
154 ipc_call_t answer;
155 async_exch_t *exch = vfs_exchange_grab(fs_handle);
156 aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
157 &answer);
158 /* Send the mount options */
159 sysarg_t rc = async_data_write_start(exch, options, str_size(options));
160 if (rc != EOK) {
161 async_forget(msg);
162 vfs_exchange_release(exch);
163 return rc;
164 }
165
166 async_wait_for(msg, &rc);
167 if (rc != EOK) {
168 vfs_exchange_release(exch);
169 return rc;
170 }
171
172 vfs_lookup_res_t res;
173 res.triplet.fs_handle = fs_handle;
174 res.triplet.service_id = service_id;
175 res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
176 res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
177 IPC_GET_ARG3(answer));
178 res.type = VFS_NODE_DIRECTORY;
179
180 /* Add reference to the mounted root. */
181 *root = vfs_node_get(&res);
182 if (!*root) {
183 aid_t msg = async_send_1(exch, VFS_OUT_UNMOUNTED,
184 (sysarg_t) service_id, NULL);
185 async_forget(msg);
186 vfs_exchange_release(exch);
187 return ENOMEM;
188 }
189
190 vfs_exchange_release(exch);
191
192 return EOK;
193}
194
195int vfs_op_fsprobe(const char *fs_name, service_id_t sid,
196 vfs_fs_probe_info_t *info)
197{
198 fs_handle_t fs_handle = 0;
199 sysarg_t rc;
200 int retval;
201
202 fibril_mutex_lock(&fs_list_lock);
203 fs_handle = fs_name_to_handle(0, fs_name, false);
204 fibril_mutex_unlock(&fs_list_lock);
205
206 if (fs_handle == 0)
207 return ENOFS;
208
209 /* Send probe request to the file system server */
210 ipc_call_t answer;
211 async_exch_t *exch = vfs_exchange_grab(fs_handle);
212 aid_t msg = async_send_1(exch, VFS_OUT_FSPROBE, (sysarg_t) sid,
213 &answer);
214 if (msg == 0)
215 return EINVAL;
216
217 /* Read probe information */
218 retval = async_data_read_start(exch, info, sizeof(*info));
219 if (retval != EOK) {
220 async_forget(msg);
221 return retval;
222 }
223
224 async_wait_for(msg, &rc);
225 vfs_exchange_release(exch);
226 return rc;
227}
228
229int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
230 unsigned instance, const char *opts, const char *fs_name, int *outfd)
231{
232 int rc;
233 vfs_file_t *mp = NULL;
234 vfs_file_t *file = NULL;
235 int fd = -1;
236
237 if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
238 mp = vfs_file_get(mpfd);
239 if (mp == NULL) {
240 rc = EBADF;
241 goto out;
242 }
243
244 if (mp->node->mount != NULL) {
245 rc = EBUSY;
246 goto out;
247 }
248
249 if (mp->node->type != VFS_NODE_DIRECTORY) {
250 rc = ENOTDIR;
251 goto out;
252 }
253
254 if (vfs_node_has_children(mp->node)) {
255 rc = ENOTEMPTY;
256 goto out;
257 }
258 }
259
260 if (!(flags & VFS_MOUNT_NO_REF)) {
261 fd = vfs_fd_alloc(&file, false);
262 if (fd < 0) {
263 rc = fd;
264 goto out;
265 }
266 }
267
268 vfs_node_t *root = NULL;
269
270 fibril_rwlock_write_lock(&namespace_rwlock);
271
272 rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
273 &root);
274 if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
275 vfs_node_addref(mp->node);
276 vfs_node_addref(root);
277 mp->node->mount = root;
278 }
279
280 fibril_rwlock_write_unlock(&namespace_rwlock);
281
282 if (rc != EOK)
283 goto out;
284
285 if (flags & VFS_MOUNT_NO_REF) {
286 vfs_node_delref(root);
287 } else {
288 assert(file != NULL);
289
290 file->node = root;
291 file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
292 file->open_read = false;
293 file->open_write = false;
294 }
295
296out:
297 if (mp)
298 vfs_file_put(mp);
299 if (file)
300 vfs_file_put(file);
301
302 if (rc != EOK && fd >= 0) {
303 vfs_fd_free(fd);
304 fd = 0;
305 }
306
307 *outfd = fd;
308 return rc;
309}
310
311int vfs_op_open(int fd, int mode)
312{
313 if (mode == 0)
314 return EINVAL;
315
316 vfs_file_t *file = vfs_file_get(fd);
317 if (!file)
318 return EBADF;
319
320 if ((mode & ~file->permissions) != 0) {
321 vfs_file_put(file);
322 return EPERM;
323 }
324
325 if (file->open_read || file->open_write) {
326 vfs_file_put(file);
327 return EBUSY;
328 }
329
330 file->open_read = (mode & MODE_READ) != 0;
331 file->open_write = (mode & (MODE_WRITE | MODE_APPEND)) != 0;
332 file->append = (mode & MODE_APPEND) != 0;
333
334 if (!file->open_read && !file->open_write) {
335 vfs_file_put(file);
336 return EINVAL;
337 }
338
339 if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
340 file->open_read = file->open_write = false;
341 vfs_file_put(file);
342 return EINVAL;
343 }
344
345 int rc = vfs_open_node_remote(file->node);
346 if (rc != EOK) {
347 file->open_read = file->open_write = false;
348 vfs_file_put(file);
349 return rc;
350 }
351
352 vfs_file_put(file);
353 return EOK;
354}
355
356typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, aoff64_t,
357 ipc_call_t *, bool, void *);
358
359static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
360 ipc_call_t *answer, bool read, void *data)
361{
362 size_t *bytes = (size_t *) data;
363 int rc;
364
365 /*
366 * Make a VFS_READ/VFS_WRITE request at the destination FS server
367 * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
368 * destination FS server. The call will be routed as if sent by
369 * ourselves. Note that call arguments are immutable in this case so we
370 * don't have to bother.
371 */
372
373 if (read) {
374 rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
375 file->node->service_id, file->node->index,
376 LOWER32(pos), UPPER32(pos), answer);
377 } else {
378 rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
379 file->node->service_id, file->node->index,
380 LOWER32(pos), UPPER32(pos), answer);
381 }
382
383 *bytes = IPC_GET_ARG1(*answer);
384 return rc;
385}
386
387static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
388 ipc_call_t *answer, bool read, void *data)
389{
390 rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
391
392 if (exch == NULL)
393 return ENOENT;
394
395 aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
396 file->node->service_id, file->node->index, LOWER32(pos),
397 UPPER32(pos), answer);
398 if (msg == 0)
399 return EINVAL;
400
401 int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
402 if (retval != EOK) {
403 async_forget(msg);
404 return retval;
405 }
406
407 sysarg_t rc;
408 async_wait_for(msg, &rc);
409
410 chunk->size = IPC_GET_ARG1(*answer);
411
412 return (int) rc;
413}
414
415static int vfs_rdwr(int fd, aoff64_t pos, bool read, rdwr_ipc_cb_t ipc_cb,
416 void *ipc_cb_data)
417{
418 /*
419 * The following code strongly depends on the fact that the files data
420 * structure can be only accessed by a single fibril and all file
421 * operations are serialized (i.e. the reads and writes cannot
422 * interleave and a file cannot be closed while it is being read).
423 *
424 * Additional synchronization needs to be added once the table of
425 * open files supports parallel access!
426 */
427
428 /* Lookup the file structure corresponding to the file descriptor. */
429 vfs_file_t *file = vfs_file_get(fd);
430 if (!file)
431 return EBADF;
432
433 if ((read && !file->open_read) || (!read && !file->open_write)) {
434 vfs_file_put(file);
435 return EINVAL;
436 }
437
438 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
439 assert(fs_info);
440
441 bool rlock = read ||
442 (fs_info->concurrent_read_write && fs_info->write_retains_size);
443
444 /*
445 * Lock the file's node so that no other client can read/write to it at
446 * the same time unless the FS supports concurrent reads/writes and its
447 * write implementation does not modify the file size.
448 */
449 if (rlock)
450 fibril_rwlock_read_lock(&file->node->contents_rwlock);
451 else
452 fibril_rwlock_write_lock(&file->node->contents_rwlock);
453
454 if (file->node->type == VFS_NODE_DIRECTORY) {
455 /*
456 * Make sure that no one is modifying the namespace
457 * while we are in readdir().
458 */
459
460 if (!read) {
461 if (rlock) {
462 fibril_rwlock_read_unlock(
463 &file->node->contents_rwlock);
464 } else {
465 fibril_rwlock_write_unlock(
466 &file->node->contents_rwlock);
467 }
468 vfs_file_put(file);
469 return EINVAL;
470 }
471
472 fibril_rwlock_read_lock(&namespace_rwlock);
473 }
474
475 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
476
477 if (!read && file->append)
478 pos = file->node->size;
479
480 /*
481 * Handle communication with the endpoint FS.
482 */
483 ipc_call_t answer;
484 int rc = ipc_cb(fs_exch, file, pos, &answer, read, ipc_cb_data);
485
486 vfs_exchange_release(fs_exch);
487
488 if (file->node->type == VFS_NODE_DIRECTORY)
489 fibril_rwlock_read_unlock(&namespace_rwlock);
490
491 /* Unlock the VFS node. */
492 if (rlock) {
493 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
494 } else {
495 /* Update the cached version of node's size. */
496 if (rc == EOK) {
497 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
498 IPC_GET_ARG3(answer));
499 }
500 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
501 }
502
503 vfs_file_put(file);
504
505 return rc;
506}
507
508int vfs_rdwr_internal(int fd, aoff64_t pos, bool read, rdwr_io_chunk_t *chunk)
509{
510 return vfs_rdwr(fd, pos, read, rdwr_ipc_internal, chunk);
511}
512
513int vfs_op_read(int fd, aoff64_t pos, size_t *out_bytes)
514{
515 return vfs_rdwr(fd, pos, true, rdwr_ipc_client, out_bytes);
516}
517
518int vfs_op_rename(int basefd, char *old, char *new)
519{
520 vfs_file_t *base_file = vfs_file_get(basefd);
521 if (!base_file)
522 return EBADF;
523
524 vfs_node_t *base = base_file->node;
525 vfs_node_addref(base);
526 vfs_file_put(base_file);
527
528 vfs_lookup_res_t base_lr;
529 vfs_lookup_res_t old_lr;
530 vfs_lookup_res_t new_lr_orig;
531 bool orig_unlinked = false;
532
533 int rc;
534
535 size_t shared = shared_path(old, new);
536
537 /* Do not allow one path to be a prefix of the other. */
538 if (old[shared] == 0 || new[shared] == 0) {
539 vfs_node_put(base);
540 return EINVAL;
541 }
542 assert(old[shared] == '/');
543 assert(new[shared] == '/');
544
545 fibril_rwlock_write_lock(&namespace_rwlock);
546
547 /* Resolve the shared portion of the path first. */
548 if (shared != 0) {
549 old[shared] = 0;
550 rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
551 if (rc != EOK) {
552 vfs_node_put(base);
553 fibril_rwlock_write_unlock(&namespace_rwlock);
554 return rc;
555 }
556
557 vfs_node_put(base);
558 base = vfs_node_get(&base_lr);
559 if (!base) {
560 fibril_rwlock_write_unlock(&namespace_rwlock);
561 return ENOMEM;
562 }
563 old[shared] = '/';
564 old += shared;
565 new += shared;
566 }
567
568 rc = vfs_lookup_internal(base, old, L_DISABLE_MOUNTS, &old_lr);
569 if (rc != EOK) {
570 vfs_node_put(base);
571 fibril_rwlock_write_unlock(&namespace_rwlock);
572 return rc;
573 }
574
575 rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
576 &new_lr_orig);
577 if (rc == EOK) {
578 orig_unlinked = true;
579 } else if (rc != ENOENT) {
580 vfs_node_put(base);
581 fibril_rwlock_write_unlock(&namespace_rwlock);
582 return rc;
583 }
584
585 rc = vfs_link_internal(base, new, &old_lr.triplet);
586 if (rc != EOK) {
587 vfs_link_internal(base, old, &old_lr.triplet);
588 if (orig_unlinked)
589 vfs_link_internal(base, new, &new_lr_orig.triplet);
590 vfs_node_put(base);
591 fibril_rwlock_write_unlock(&namespace_rwlock);
592 return rc;
593 }
594
595 rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
596 &old_lr);
597 if (rc != EOK) {
598 if (orig_unlinked)
599 vfs_link_internal(base, new, &new_lr_orig.triplet);
600 vfs_node_put(base);
601 fibril_rwlock_write_unlock(&namespace_rwlock);
602 return rc;
603 }
604
605 /* If the node is not held by anyone, try to destroy it. */
606 if (orig_unlinked) {
607 vfs_node_t *node = vfs_node_peek(&new_lr_orig);
608 if (!node)
609 out_destroy(&new_lr_orig.triplet);
610 else
611 vfs_node_put(node);
612 }
613
614 vfs_node_put(base);
615 fibril_rwlock_write_unlock(&namespace_rwlock);
616 return EOK;
617}
618
619int vfs_op_resize(int fd, int64_t size)
620{
621 vfs_file_t *file = vfs_file_get(fd);
622 if (!file)
623 return EBADF;
624
625 fibril_rwlock_write_lock(&file->node->contents_rwlock);
626
627 int rc = vfs_truncate_internal(file->node->fs_handle,
628 file->node->service_id, file->node->index, size);
629 if (rc == EOK)
630 file->node->size = size;
631
632 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
633 vfs_file_put(file);
634 return rc;
635}
636
637int vfs_op_stat(int fd)
638{
639 vfs_file_t *file = vfs_file_get(fd);
640 if (!file)
641 return EBADF;
642
643 vfs_node_t *node = file->node;
644
645 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
646 int rc = async_data_read_forward_fast(exch, VFS_OUT_STAT,
647 node->service_id, node->index, true, 0, NULL);
648 vfs_exchange_release(exch);
649
650 vfs_file_put(file);
651 return rc;
652}
653
654int vfs_op_statfs(int fd)
655{
656 vfs_file_t *file = vfs_file_get(fd);
657 if (!file)
658 return EBADF;
659
660 vfs_node_t *node = file->node;
661
662 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
663 int rc = async_data_read_forward_fast(exch, VFS_OUT_STATFS,
664 node->service_id, node->index, false, 0, NULL);
665 vfs_exchange_release(exch);
666
667 vfs_file_put(file);
668 return rc;
669}
670
671int vfs_op_sync(int fd)
672{
673 vfs_file_t *file = vfs_file_get(fd);
674 if (!file)
675 return EBADF;
676
677 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
678
679 aid_t msg;
680 ipc_call_t answer;
681 msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
682 file->node->index, &answer);
683
684 vfs_exchange_release(fs_exch);
685
686 sysarg_t rc;
687 async_wait_for(msg, &rc);
688
689 vfs_file_put(file);
690 return rc;
691
692}
693
694static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
695 fs_index_t index, aoff64_t size)
696{
697 async_exch_t *exch = vfs_exchange_grab(fs_handle);
698 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
699 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
700 UPPER32(size));
701 vfs_exchange_release(exch);
702
703 return (int) rc;
704}
705
706int vfs_op_unlink(int parentfd, int expectfd, char *path)
707{
708 int rc = EOK;
709 vfs_file_t *parent = NULL;
710 vfs_file_t *expect = NULL;
711
712 if (parentfd == expectfd)
713 return EINVAL;
714
715 fibril_rwlock_write_lock(&namespace_rwlock);
716
717 /*
718 * Files are retrieved in order of file descriptors, to prevent
719 * deadlock.
720 */
721 if (parentfd < expectfd) {
722 parent = vfs_file_get(parentfd);
723 if (!parent) {
724 rc = EBADF;
725 goto exit;
726 }
727 }
728
729 if (expectfd >= 0) {
730 expect = vfs_file_get(expectfd);
731 if (!expect) {
732 rc = EBADF;
733 goto exit;
734 }
735 }
736
737 if (parentfd > expectfd) {
738 parent = vfs_file_get(parentfd);
739 if (!parent) {
740 rc = EBADF;
741 goto exit;
742 }
743 }
744
745 assert(parent != NULL);
746
747 if (expectfd >= 0) {
748 vfs_lookup_res_t lr;
749 rc = vfs_lookup_internal(parent->node, path, 0, &lr);
750 if (rc != EOK)
751 goto exit;
752
753 vfs_node_t *found_node = vfs_node_peek(&lr);
754 vfs_node_put(found_node);
755 if (expect->node != found_node) {
756 rc = ENOENT;
757 goto exit;
758 }
759
760 vfs_file_put(expect);
761 expect = NULL;
762 }
763
764 vfs_lookup_res_t lr;
765 rc = vfs_lookup_internal(parent->node, path, L_UNLINK, &lr);
766 if (rc != EOK)
767 goto exit;
768
769 /* If the node is not held by anyone, try to destroy it. */
770 vfs_node_t *node = vfs_node_peek(&lr);
771 if (!node)
772 out_destroy(&lr.triplet);
773 else
774 vfs_node_put(node);
775
776exit:
777 if (path)
778 free(path);
779 if (parent)
780 vfs_file_put(parent);
781 if (expect)
782 vfs_file_put(expect);
783 fibril_rwlock_write_unlock(&namespace_rwlock);
784 return rc;
785}
786
787int vfs_op_unmount(int mpfd)
788{
789 vfs_file_t *mp = vfs_file_get(mpfd);
790 if (mp == NULL)
791 return EBADF;
792
793 if (mp->node->mount == NULL) {
794 vfs_file_put(mp);
795 return ENOENT;
796 }
797
798 fibril_rwlock_write_lock(&namespace_rwlock);
799
800 /*
801 * Count the total number of references for the mounted file system. We
802 * are expecting at least one, which is held by the mount point.
803 * If we find more, it means that
804 * the file system cannot be gracefully unmounted at the moment because
805 * someone is working with it.
806 */
807 if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
808 mp->node->mount->service_id) != 1) {
809 vfs_file_put(mp);
810 fibril_rwlock_write_unlock(&namespace_rwlock);
811 return EBUSY;
812 }
813
814 async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
815 int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
816 mp->node->mount->service_id);
817 vfs_exchange_release(exch);
818
819 if (rc != EOK) {
820 vfs_file_put(mp);
821 fibril_rwlock_write_unlock(&namespace_rwlock);
822 return rc;
823 }
824
825 vfs_node_forget(mp->node->mount);
826 vfs_node_put(mp->node);
827 mp->node->mount = NULL;
828
829 fibril_rwlock_write_unlock(&namespace_rwlock);
830
831 vfs_file_put(mp);
832 return EOK;
833}
834
835int vfs_op_wait_handle(bool high_fd)
836{
837 return vfs_wait_handle_internal(high_fd);
838}
839
840static inline bool walk_flags_valid(int flags)
841{
842 if ((flags & ~WALK_ALL_FLAGS) != 0)
843 return false;
844 if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE))
845 return false;
846 if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY))
847 return false;
848 if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
849 if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR))
850 return false;
851 }
852 return true;
853}
854
855static inline int walk_lookup_flags(int flags)
856{
857 int lflags = 0;
858 if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE))
859 lflags |= L_CREATE;
860 if (flags & WALK_MUST_CREATE)
861 lflags |= L_EXCLUSIVE;
862 if (flags & WALK_REGULAR)
863 lflags |= L_FILE;
864 if (flags & WALK_DIRECTORY)
865 lflags |= L_DIRECTORY;
866 if (flags & WALK_MOUNT_POINT)
867 lflags |= L_MP;
868 return lflags;
869}
870
871int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
872{
873 if (!walk_flags_valid(flags))
874 return EINVAL;
875
876 vfs_file_t *parent = vfs_file_get(parentfd);
877 if (!parent)
878 return EBADF;
879
880 fibril_rwlock_read_lock(&namespace_rwlock);
881
882 vfs_lookup_res_t lr;
883 int rc = vfs_lookup_internal(parent->node, path,
884 walk_lookup_flags(flags), &lr);
885 if (rc != EOK) {
886 fibril_rwlock_read_unlock(&namespace_rwlock);
887 vfs_file_put(parent);
888 return rc;
889 }
890
891 vfs_node_t *node = vfs_node_get(&lr);
892 if (!node) {
893 fibril_rwlock_read_unlock(&namespace_rwlock);
894 vfs_file_put(parent);
895 return ENOMEM;
896 }
897
898 vfs_file_t *file;
899 int fd = vfs_fd_alloc(&file, false);
900 if (fd < 0) {
901 vfs_node_put(node);
902 vfs_file_put(parent);
903 return fd;
904 }
905 assert(file != NULL);
906
907 file->node = node;
908 file->permissions = parent->permissions;
909 file->open_read = false;
910 file->open_write = false;
911
912 vfs_file_put(file);
913 vfs_file_put(parent);
914
915 fibril_rwlock_read_unlock(&namespace_rwlock);
916
917 *out_fd = fd;
918 return EOK;
919}
920
921int vfs_op_write(int fd, aoff64_t pos, size_t *out_bytes)
922{
923 return vfs_rdwr(fd, pos, false, rdwr_ipc_client, out_bytes);
924}
925
926/**
927 * @}
928 */
Note: See TracBrowser for help on using the repository browser.