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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f644472 was 58898d1d, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove VFS_IN_SEEK from VFS

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