source: mainline/uspace/srv/vfs/vfs_ops.c@ 35b7d86e

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

Remove VFS_IN_MTAB_GET

The mountpoints in VFS don't know their path anymore and it does not
make much sense to maintain this global mount table when tasks can have
different roots.

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