source: mainline/uspace/srv/vfs/vfs_ops.c@ 582a0b8

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

Remove unistd.h

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