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

Last change on this file since c1b2084 was 4224ef7, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

sysman: Add VFS FS server autostart

  • VFS autostart instrumentation removes explicit dependency on FS servers.
  • Compositor service properly named, it's now resolved as implicit dependency.

Conflicts:

boot/Makefile.common
uspace/lib/gui/window.c
uspace/srv/locsrv/locsrv.c
uspace/srv/vfs/vfs.c
uspace/srv/vfs/vfs_ops.c

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