source: mainline/uspace/srv/vfs/vfs_ops.c@ 55a7fee

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

Streamline vfs_op_fstat()

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