source: mainline/uspace/srv/vfs/vfs_ops.c@ 354b642

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

Merge from lp:~zarevucky-jiri/helenos/vfs-2.5/ revisions 1932-1936

Original commit messages:

1936: Jiri Zarevucky 2013-08-05 Modifications to vfs_rdwr.
1935: Jiri Zarevucky 2013-08-05 Fix a bug in read/write.
1934: Jiri Zarevucky 2013-08-05 Fix a hidden bug in handle passing.
1933: Jiri Zarevucky 2013-08-05 Add VFS_IN_CLONE.
1932: Jiri Zarevucky 2013-08-05 Add functions for passing handles around.

Modifications:

  • New vcl_* interfaces renamed to vfs_*
  • Server-side vfs_pass_handle() and vfs_clone() renamed to vfs_op_* to avoid name conflict with libc
  • Property mode set to 100644
File size: 32.4 KB
RevLine 
[861e7d1]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 * @{
[8dc72b64]31 */
[861e7d1]32
33/**
[8dc72b64]34 * @file vfs_ops.c
35 * @brief Operations that VFS offers to its clients.
[861e7d1]36 */
37
[a8e9ab8d]38#include "vfs.h"
[ed903174]39#include <macros.h>
[9539be6]40#include <stdint.h>
[861e7d1]41#include <async.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
[19f857a]45#include <str.h>
[3e6a98c5]46#include <stdbool.h>
[1e4cada]47#include <fibril_synch.h>
[d9c8c81]48#include <adt/list.h>
[861e7d1]49#include <unistd.h>
50#include <ctype.h>
[2db4ac8]51#include <fcntl.h>
[861e7d1]52#include <assert.h>
[a8e9ab8d]53#include <vfs/canonify.h>
[10e4cd7]54#include <vfs/vfs_mtab.h>
55
56FIBRIL_MUTEX_INITIALIZE(mtab_list_lock);
57LIST_INITIALIZE(mtab_list);
58static size_t mtab_size = 0;
[861e7d1]59
[7fe1f75]60/* Forward declarations of static functions. */
[15f3c3f]61static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
[8df8415]62 aoff64_t);
[7fe1f75]63
[861e7d1]64/**
65 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
66 * concurrent VFS operation which modifies the file system namespace.
67 */
[230260ac]68FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
[861e7d1]69
[bf9dc4e2]70vfs_node_t *root = NULL;
[861e7d1]71
[4636a60]72static int vfs_connect_internal(service_id_t service_id, unsigned flags, unsigned instance,
73 char *options, char *fsname, vfs_node_t **root)
[861e7d1]74{
[4636a60]75 fs_handle_t fs_handle = 0;
[05b9912]76
[4636a60]77 fibril_mutex_lock(&fs_list_lock);
78 while (1) {
79 fs_handle = fs_name_to_handle(instance, fsname, false);
[8dc72b64]80
[4636a60]81 if (fs_handle != 0 || !(flags & IPC_FLAG_BLOCKING)) {
82 break;
[861e7d1]83 }
[8dc72b64]84
[4636a60]85 fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
86 }
87 fibril_mutex_unlock(&fs_list_lock);
88
89 if (fs_handle == 0) {
90 return ENOENT;
[5bcd5b7]91 }
92
[4636a60]93 /* Tell the mountee that it is being mounted. */
94 ipc_call_t answer;
95 async_exch_t *exch = vfs_exchange_grab(fs_handle);
96 aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id, &answer);
97 /* Send the mount options */
98 sysarg_t rc = async_data_write_start(exch, options, str_size(options));
99 if (rc != EOK) {
100 async_forget(msg);
101 vfs_exchange_release(exch);
102 return rc;
[5bcd5b7]103 }
[4636a60]104 async_wait_for(msg, &rc);
105 vfs_exchange_release(exch);
[5bcd5b7]106
[4636a60]107 if (rc != EOK) {
108 return rc;
[5bcd5b7]109 }
110
[4636a60]111 vfs_lookup_res_t res;
112 res.triplet.fs_handle = fs_handle;
113 res.triplet.service_id = service_id;
114 res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
115 res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
116 res.type = VFS_NODE_DIRECTORY;
[5bcd5b7]117
[4636a60]118 /* Add reference to the mounted root. */
119 *root = vfs_node_get(&res);
120 assert(*root);
[8dc72b64]121
[4636a60]122 return EOK;
[5bcd5b7]123}
124
[4636a60]125static int vfs_mount_internal(service_id_t service_id, unsigned flags, unsigned instance,
126 char *opts, char *fs_name, char *mp)
[861e7d1]127{
[594303b]128 /* Resolve the path to the mountpoint. */
[4636a60]129
[bf9dc4e2]130 if (root == NULL) {
131 /* We still don't have the root file system mounted. */
132 if (str_cmp(mp, "/") != 0) {
[861e7d1]133 /*
134 * We can't resolve this without the root filesystem
135 * being mounted first.
136 */
[6f9ef87a]137 return ENOENT;
[861e7d1]138 }
[8dc72b64]139
[4636a60]140 return vfs_connect_internal(service_id, flags, instance, opts, fs_name, &root);
[861e7d1]141 }
142
[bf9dc4e2]143 /* We already have the root FS. */
144 if (str_cmp(mp, "/") == 0) {
145 /* Trying to mount root FS over root FS */
146 return EBUSY;
[861e7d1]147 }
[8dc72b64]148
[4636a60]149 vfs_lookup_res_t mp_res;
150 int rc = vfs_lookup_internal(root, mp, L_DIRECTORY, &mp_res);
[bf9dc4e2]151 if (rc != EOK) {
152 /* The lookup failed. */
153 return rc;
154 }
[79ae36dd]155
[4636a60]156 vfs_node_t *mp_node;
[bf9dc4e2]157 mp_node = vfs_node_get(&mp_res);
158 if (!mp_node) {
159 return ENOMEM;
160 }
[83937ccd]161
[4636a60]162 if (mp_node->mount != NULL) {
163 return EBUSY;
164 }
[79ae36dd]165
[4636a60]166 if (mp_node->type != VFS_NODE_DIRECTORY) {
[1dff985]167 printf("%s node not a directory, type=%d\n", mp, mp_node->type);
[4636a60]168 return ENOTDIR;
[83937ccd]169 }
170
[4636a60]171 if (vfs_node_has_children(mp_node)) {
172 return ENOTEMPTY;
[594303b]173 }
[79ae36dd]174
[4636a60]175 vfs_node_t *mountee;
[8dc72b64]176
[4636a60]177 rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name, &mountee);
178 if (rc != EOK) {
179 vfs_node_put(mp_node);
180 return ENOMEM;
[861e7d1]181 }
[79ae36dd]182
[4636a60]183 mp_node->mount = mountee;
184 /* The two references to nodes are held by the mount so that they cannot be freed.
185 * They are removed in detach_internal().
186 */
187 return EOK;
[861e7d1]188}
189
[6afc9d7]190void vfs_mount_srv(ipc_callid_t rid, ipc_call_t *request)
[8dc72b64]191{
192 /*
193 * We expect the library to do the device-name to device-handle
194 * translation for us, thus the device handle will arrive as ARG1
195 * in the request.
196 */
[4636a60]197 service_id_t service_id = (service_id_t) IPC_GET_ARG1(*request);
[8dc72b64]198
199 /*
200 * Mount flags are passed as ARG2.
201 */
202 unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
203
[4979403]204 /*
205 * Instance number is passed as ARG3.
206 */
207 unsigned int instance = IPC_GET_ARG3(*request);
208
[8dc72b64]209 /* We want the client to send us the mount point. */
[472c09d]210 char *mp;
[4cac2d69]211 int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
[eda925a]212 0, NULL);
[472c09d]213 if (rc != EOK) {
[ffa2c8ef]214 async_answer_0(rid, rc);
[8dc72b64]215 return;
216 }
217
[594303b]218 /* Now we expect to receive the mount options. */
[472c09d]219 char *opts;
[4cac2d69]220 rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
[eda925a]221 0, NULL);
[472c09d]222 if (rc != EOK) {
[ffa2c8ef]223 async_answer_0(rid, rc);
[4636a60]224 free(mp);
[594303b]225 return;
226 }
227
[8dc72b64]228 /*
229 * Now, we expect the client to send us data with the name of the file
230 * system.
231 */
[472c09d]232 char *fs_name;
[8df8415]233 rc = async_data_write_accept((void **) &fs_name, true, 0,
234 FS_NAME_MAXLEN, 0, NULL);
[472c09d]235 if (rc != EOK) {
[ffa2c8ef]236 async_answer_0(rid, rc);
[c08c355]237 free(mp);
[594303b]238 free(opts);
[c08c355]239 return;
240 }
[4636a60]241
[460514d]242 /* Add the filesystem info to the list of mounted filesystems */
243 mtab_ent_t *mtab_ent = malloc(sizeof(mtab_ent_t));
244 if (!mtab_ent) {
245 async_answer_0(rid, ENOMEM);
246 free(mp);
247 free(fs_name);
248 free(opts);
249 return;
250 }
[4636a60]251
252 /* Mount the filesystem. */
253 fibril_rwlock_write_lock(&namespace_rwlock);
254 rc = vfs_mount_internal(service_id, flags, instance, opts, fs_name, mp);
255 fibril_rwlock_write_unlock(&namespace_rwlock);
[6f9ef87a]256
[10e4cd7]257 /* Add the filesystem info to the list of mounted filesystems */
[4636a60]258 if (rc == EOK) {
259 str_cpy(mtab_ent->mp, MAX_PATH_LEN, mp);
260 str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
261 str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
262 mtab_ent->instance = instance;
263 mtab_ent->service_id = service_id;
[10e4cd7]264
[4636a60]265 link_initialize(&mtab_ent->link);
[10e4cd7]266
[4636a60]267 fibril_mutex_lock(&mtab_list_lock);
268 list_append(&mtab_ent->link, &mtab_list);
269 mtab_size++;
270 fibril_mutex_unlock(&mtab_list_lock);
271 }
272
273 async_answer_0(rid, rc);
[10e4cd7]274
[daf8ff2]275 free(mp);
276 free(fs_name);
277 free(opts);
[8dc72b64]278}
279
[6afc9d7]280void vfs_unmount_srv(ipc_callid_t rid, ipc_call_t *request)
[7f5e070]281{
[ae75e2e3]282 /*
283 * Receive the mount point path.
284 */
[4636a60]285 char *mp;
286 int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
[eda925a]287 0, NULL);
[ae75e2e3]288 if (rc != EOK)
[ffa2c8ef]289 async_answer_0(rid, rc);
[79ae36dd]290
[ae75e2e3]291 /*
292 * Taking the namespace lock will do two things for us. First, it will
293 * prevent races with other lookup operations. Second, it will stop new
294 * references to already existing VFS nodes and creation of new VFS
295 * nodes. This is because new references are added as a result of some
296 * lookup operation or at least of some operation which is protected by
297 * the namespace lock.
298 */
299 fibril_rwlock_write_lock(&namespace_rwlock);
[4636a60]300
301 if (str_cmp(mp, "/") == 0) {
302 free(mp);
303
304 /*
305 * Unmounting the root file system.
306 *
307 * In this case, there is no mount point node and we send
308 * VFS_OUT_UNMOUNTED directly to the mounted file system.
309 */
310
311 if (!root) {
312 fibril_rwlock_write_unlock(&namespace_rwlock);
313 async_answer_0(rid, ENOENT);
314 return;
315 }
316
317 /*
318 * Count the total number of references for the mounted file system. We
319 * are expecting at least one, which we got when the file system was mounted.
320 * If we find more, it means that
321 * the file system cannot be gracefully unmounted at the moment because
322 * someone is working with it.
323 */
324 if (vfs_nodes_refcount_sum_get(root->fs_handle, root->service_id) != 1) {
325 fibril_rwlock_write_unlock(&namespace_rwlock);
326 async_answer_0(rid, EBUSY);
327 return;
328 }
329
330 async_exch_t *exch = vfs_exchange_grab(root->fs_handle);
331 rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED, root->service_id);
332 vfs_exchange_release(exch);
333
334 fibril_rwlock_write_unlock(&namespace_rwlock);
335 if (rc == EOK) {
336 vfs_node_forget(root);
337 root = NULL;
338 }
339 async_answer_0(rid, rc);
340 return;
341 }
[ae75e2e3]342
343 /*
344 * Lookup the mounted root and instantiate it.
345 */
[4636a60]346 vfs_lookup_res_t mp_res;
347 rc = vfs_lookup_internal(root, mp, L_MP, &mp_res);
[ae75e2e3]348 if (rc != EOK) {
349 fibril_rwlock_write_unlock(&namespace_rwlock);
350 free(mp);
[ffa2c8ef]351 async_answer_0(rid, rc);
[ae75e2e3]352 return;
353 }
[4636a60]354 vfs_node_t *mp_node = vfs_node_get(&mp_res);
355 if (!mp_node) {
[ae75e2e3]356 fibril_rwlock_write_unlock(&namespace_rwlock);
357 free(mp);
[ffa2c8ef]358 async_answer_0(rid, ENOMEM);
[ae75e2e3]359 return;
360 }
[79ae36dd]361
[4636a60]362 if (mp_node->mount == NULL) {
363 fibril_rwlock_write_unlock(&namespace_rwlock);
364 vfs_node_put(mp_node);
365 free(mp);
366 async_answer_0(rid, ENOENT);
367 return;
368 }
369
[ae75e2e3]370 /*
371 * Count the total number of references for the mounted file system. We
[4636a60]372 * are expecting at least one, which we got when the file system was mounted.
373 * If we find more, it means that
[ae75e2e3]374 * the file system cannot be gracefully unmounted at the moment because
375 * someone is working with it.
376 */
[4636a60]377 if (vfs_nodes_refcount_sum_get(mp_node->mount->fs_handle, mp_node->mount->service_id) != 1) {
[ae75e2e3]378 fibril_rwlock_write_unlock(&namespace_rwlock);
[4636a60]379 vfs_node_put(mp_node);
[ae75e2e3]380 free(mp);
[ffa2c8ef]381 async_answer_0(rid, EBUSY);
[ae75e2e3]382 return;
383 }
[79ae36dd]384
[4636a60]385 /* Unmount the filesystem. */
386 async_exch_t *exch = vfs_exchange_grab(mp_node->mount->fs_handle);
387 rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED, mp_node->mount->service_id);
388 vfs_exchange_release(exch);
[79ae36dd]389
[4636a60]390 vfs_node_forget(mp_node->mount);
391 mp_node->mount = NULL;
392
393 vfs_node_put(mp_node);
[ae75e2e3]394 fibril_rwlock_write_unlock(&namespace_rwlock);
[4636a60]395
[76a67ce]396 fibril_mutex_lock(&mtab_list_lock);
397 int found = 0;
398
[feeac0d]399 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
[76a67ce]400 if (str_cmp(mtab_ent->mp, mp) == 0) {
[8d6a41c]401 list_remove(&mtab_ent->link);
[76a67ce]402 mtab_size--;
[8d6a41c]403 free(mtab_ent);
[76a67ce]404 found = 1;
405 break;
406 }
407 }
408 assert(found);
[4af2f36]409 fibril_mutex_unlock(&mtab_list_lock);
[76a67ce]410
[4636a60]411 free(mp);
412
[ffa2c8ef]413 async_answer_0(rid, EOK);
[4636a60]414 return;
[7f5e070]415}
416
[0b18364]417static inline bool walk_flags_valid(int flags)
[861e7d1]418{
[0b18364]419 if ((flags&~WALK_ALL_FLAGS) != 0) {
420 return false;
421 }
422 if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
423 return false;
424 }
425 if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
426 return false;
427 }
428 if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
429 if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
430 return false;
431 }
432 }
433 return true;
434}
[dd2cfa7]435
[0b18364]436static inline int walk_lookup_flags(int flags)
437{
438 int lflags = 0;
439 if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
440 lflags |= L_CREATE;
441 }
442 if (flags&WALK_MUST_CREATE) {
443 lflags |= L_EXCLUSIVE;
444 }
445 if (flags&WALK_REGULAR) {
446 lflags |= L_FILE;
447 }
448 if (flags&WALK_DIRECTORY) {
449 lflags |= L_DIRECTORY;
450 }
451 return lflags;
452}
453
[cb65bbe]454void vfs_walk(ipc_callid_t rid, ipc_call_t *request)
455{
[b17186d]456 /*
[cb65bbe]457 * Parent is our relative root for file lookup.
458 * For defined flags, see <ipc/vfs.h>.
[b17186d]459 */
[cb65bbe]460 int parentfd = IPC_GET_ARG1(*request);
461 int flags = IPC_GET_ARG2(*request);
462
[0b18364]463 if (!walk_flags_valid(flags)) {
[ffa2c8ef]464 async_answer_0(rid, EINVAL);
[b17186d]465 return;
466 }
[05b9912]467
[472c09d]468 char *path;
[cb65bbe]469 int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
470
471 /* Lookup the file structure corresponding to the file descriptor. */
472 vfs_file_t *parent = NULL;
[bf9dc4e2]473 vfs_node_t *parent_node = root;
[cb65bbe]474 // TODO: Client-side root.
475 if (parentfd != -1) {
476 parent = vfs_file_get(parentfd);
477 if (!parent) {
478 free(path);
479 async_answer_0(rid, EBADF);
480 return;
481 }
[bf9dc4e2]482 parent_node = parent->node;
[861e7d1]483 }
484
[cb65bbe]485 fibril_rwlock_read_lock(&namespace_rwlock);
[05b9912]486
[eb27ce5a]487 vfs_lookup_res_t lr;
[5bcd5b7]488 rc = vfs_lookup_internal(parent_node, path, walk_lookup_flags(flags), &lr);
[cb65bbe]489 free(path);
490
[05b9912]491 if (rc != EOK) {
[cb65bbe]492 fibril_rwlock_read_unlock(&namespace_rwlock);
493 if (parent) {
494 vfs_file_put(parent);
495 }
[ffa2c8ef]496 async_answer_0(rid, rc);
[861e7d1]497 return;
498 }
[05b9912]499
[cb65bbe]500 vfs_node_t *node = vfs_node_get(&lr);
501
[c577a9a]502 vfs_file_t *file;
503 int fd = vfs_fd_alloc(&file, false);
[cb65bbe]504 if (fd < 0) {
505 vfs_node_put(node);
506 if (parent) {
507 vfs_file_put(parent);
508 }
509 async_answer_0(rid, fd);
510 return;
511 }
512 assert(file != NULL);
513
514 file->node = node;
515 if (parent) {
516 file->permissions = parent->permissions;
517 } else {
518 file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
519 }
520 file->open_read = false;
521 file->open_write = false;
522
523 vfs_file_put(file);
524 if (parent) {
525 vfs_file_put(parent);
526 }
527
528 fibril_rwlock_read_unlock(&namespace_rwlock);
529
530 async_answer_1(rid, EOK, fd);
531}
532
533void vfs_open2(ipc_callid_t rid, ipc_call_t *request)
534{
535 int fd = IPC_GET_ARG1(*request);
536 int flags = IPC_GET_ARG2(*request);
537
538 if (flags == 0) {
539 async_answer_0(rid, EINVAL);
540 return;
541 }
542
543 vfs_file_t *file = vfs_file_get(fd);
544 if (!file) {
545 async_answer_0(rid, EBADF);
546 return;
547 }
548
549 if ((flags & ~file->permissions) != 0) {
550 vfs_file_put(file);
551 async_answer_0(rid, EPERM);
552 return;
553 }
554
555 file->open_read = (flags & MODE_READ) != 0;
556 file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
557 file->append = (flags & MODE_APPEND) != 0;
[05b9912]558
[cb65bbe]559 if (!file->open_read && !file->open_write) {
560 vfs_file_put(file);
561 async_answer_0(rid, EINVAL);
[6f2c1ff]562 return;
563 }
[05b9912]564
[cb65bbe]565 if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
566 file->open_read = file->open_write = false;
567 vfs_file_put(file);
568 async_answer_0(rid, EINVAL);
569 return;
[7fe1f75]570 }
[05b9912]571
[cb65bbe]572 int rc = vfs_open_node_remote(file->node);
573 if (rc != EOK) {
574 file->open_read = file->open_write = false;
575 vfs_file_put(file);
576 async_answer_0(rid, rc);
[861e7d1]577 return;
578 }
[05b9912]579
[4fe94c66]580 vfs_file_put(file);
[cb65bbe]581 async_answer_0(rid, EOK);
[05b9912]582}
[861e7d1]583
[05b9912]584void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
585{
586 int fd = IPC_GET_ARG1(*request);
587
588 /* Lookup the file structure corresponding to the file descriptor. */
589 vfs_file_t *file = vfs_file_get(fd);
590 if (!file) {
[ffa2c8ef]591 async_answer_0(rid, ENOENT);
[05b9912]592 return;
593 }
594
595 /*
596 * Lock the open file structure so that no other thread can manipulate
597 * the same open file at a time.
598 */
[79ae36dd]599 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[05b9912]600
[4198f9c3]601 /* Make a VFS_OUT_SYMC request at the destination FS server. */
[05b9912]602 aid_t msg;
603 ipc_call_t answer;
[15f3c3f]604 msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
[4198f9c3]605 file->node->index, &answer);
[79ae36dd]606
607 vfs_exchange_release(fs_exch);
608
[05b9912]609 /* Wait for reply from the FS server. */
[96b02eb9]610 sysarg_t rc;
[05b9912]611 async_wait_for(msg, &rc);
612
[4fe94c66]613 vfs_file_put(file);
[ffa2c8ef]614 async_answer_0(rid, rc);
[e704503]615}
616
[05b9912]617void vfs_close(ipc_callid_t rid, ipc_call_t *request)
618{
619 int fd = IPC_GET_ARG1(*request);
[79ae36dd]620 int ret = vfs_fd_free(fd);
[ffa2c8ef]621 async_answer_0(rid, ret);
[05b9912]622}
623
[e503517a]624typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, ipc_call_t *,
[42d08592]625 bool, void *);
626
[e503517a]627static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file,
[42d08592]628 ipc_call_t *answer, bool read, void *data)
629{
[e503517a]630 size_t *bytes = (size_t *) data;
631 int rc;
632
[42d08592]633 /*
634 * Make a VFS_READ/VFS_WRITE request at the destination FS server
635 * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
636 * destination FS server. The call will be routed as if sent by
637 * ourselves. Note that call arguments are immutable in this case so we
638 * don't have to bother.
639 */
640
641 if (read) {
[e503517a]642 rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
[42d08592]643 file->node->service_id, file->node->index,
644 LOWER32(file->pos), UPPER32(file->pos), answer);
645 } else {
[e503517a]646 rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
[42d08592]647 file->node->service_id, file->node->index,
648 LOWER32(file->pos), UPPER32(file->pos), answer);
[e503517a]649 }
650
651 *bytes = IPC_GET_ARG1(*answer);
652 return rc;
[42d08592]653}
[e503517a]654
655static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file,
656 ipc_call_t *answer, bool read, void *data)
657{
658 rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
659
660 if (exch == NULL)
661 return ENOENT;
[42d08592]662
[e503517a]663 aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
664 file->node->service_id, file->node->index, LOWER32(file->pos),
665 UPPER32(file->pos), answer);
666 if (msg == 0)
667 return EINVAL;
668
669 int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
670 if (retval != EOK) {
671 async_forget(msg);
672 return retval;
673 }
674
675 sysarg_t rc;
676 async_wait_for(msg, &rc);
677
678 chunk->size = IPC_GET_ARG1(*answer);
679
680 return (int) rc;
681}
682
683static int vfs_rdwr(int fd, bool read, rdwr_ipc_cb_t ipc_cb, void *ipc_cb_data)
[861e7d1]684{
685 /*
686 * The following code strongly depends on the fact that the files data
687 * structure can be only accessed by a single fibril and all file
688 * operations are serialized (i.e. the reads and writes cannot
689 * interleave and a file cannot be closed while it is being read).
690 *
691 * Additional synchronization needs to be added once the table of
692 * open files supports parallel access!
693 */
[79ae36dd]694
[72bde81]695 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]696 vfs_file_t *file = vfs_file_get(fd);
[e503517a]697 if (!file)
[354b642]698 return EBADF;
[6c89f20]699
[cb65bbe]700 if ((read && !file->open_read) || (!read && !file->open_write)) {
[c577a9a]701 vfs_file_put(file);
[1dff985]702 return EINVAL;
[cb65bbe]703 }
704
[79ae36dd]705 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
706 assert(fs_info);
707
[354b642]708 bool rlock = read || ((fs_info->concurrent_read_write) && (fs_info->write_retains_size));
709
[861e7d1]710 /*
711 * Lock the file's node so that no other client can read/write to it at
[c2f4b6b]712 * the same time unless the FS supports concurrent reads/writes and its
713 * write implementation does not modify the file size.
[861e7d1]714 */
[354b642]715 if (rlock) {
[230260ac]716 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[354b642]717 } else {
[230260ac]718 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[354b642]719 }
[79ae36dd]720
[b17186d]721 if (file->node->type == VFS_NODE_DIRECTORY) {
722 /*
723 * Make sure that no one is modifying the namespace
724 * while we are in readdir().
725 */
[354b642]726
727 if (!read) {
728 if (rlock) {
729 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
730 } else {
731 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
732 }
733 vfs_file_put(file);
734 return EINVAL;
735 }
736
[230260ac]737 fibril_rwlock_read_lock(&namespace_rwlock);
[b17186d]738 }
[6c89f20]739
[79ae36dd]740 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[861e7d1]741
[42d08592]742 if (!read && file->append)
743 file->pos = file->node->size;
744
[861e7d1]745 /*
[42d08592]746 * Handle communication with the endpoint FS.
[861e7d1]747 */
[b4cbef1]748 ipc_call_t answer;
[e503517a]749 int rc = ipc_cb(fs_exch, file, &answer, read, ipc_cb_data);
[05b9912]750
[79ae36dd]751 vfs_exchange_release(fs_exch);
[34ca870]752
[861e7d1]753 size_t bytes = IPC_GET_ARG1(answer);
[b4cbef1]754
[b7c62a9]755 if (file->node->type == VFS_NODE_DIRECTORY) {
[230260ac]756 fibril_rwlock_read_unlock(&namespace_rwlock);
[b7c62a9]757 }
[6c89f20]758
[72bde81]759 /* Unlock the VFS node. */
[354b642]760 if (rlock) {
[230260ac]761 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[354b642]762 } else {
[861e7d1]763 /* Update the cached version of node's size. */
[354b642]764 if (rc == EOK) {
[5bb9907]765 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
766 IPC_GET_ARG3(answer));
[354b642]767 }
[230260ac]768 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[861e7d1]769 }
[6c89f20]770
[72bde81]771 /* Update the position pointer and unlock the open file. */
[354b642]772 if (rc == EOK) {
[f7017572]773 file->pos += bytes;
[354b642]774 }
[4fe94c66]775 vfs_file_put(file);
776
[e503517a]777 return rc;
778}
779
780static void vfs_rdwr_client(ipc_callid_t rid, ipc_call_t *request, bool read)
781{
782 size_t bytes = 0;
783 int rc = vfs_rdwr(IPC_GET_ARG1(*request), read, rdwr_ipc_client,
784 &bytes);
[ffa2c8ef]785 async_answer_1(rid, rc, bytes);
[861e7d1]786}
787
[e503517a]788int vfs_rdwr_internal(int fd, bool read, rdwr_io_chunk_t *chunk)
789{
790 return vfs_rdwr(fd, read, rdwr_ipc_internal, chunk);
791}
792
[861e7d1]793void vfs_read(ipc_callid_t rid, ipc_call_t *request)
794{
[e503517a]795 vfs_rdwr_client(rid, request, true);
[861e7d1]796}
797
798void vfs_write(ipc_callid_t rid, ipc_call_t *request)
799{
[e503517a]800 vfs_rdwr_client(rid, request, false);
[861e7d1]801}
802
803void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
804{
805 int fd = (int) IPC_GET_ARG1(*request);
[8df8415]806 off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
807 IPC_GET_ARG3(*request));
[ed903174]808 int whence = (int) IPC_GET_ARG4(*request);
809
[72bde81]810 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]811 vfs_file_t *file = vfs_file_get(fd);
812 if (!file) {
[ffa2c8ef]813 async_answer_0(rid, ENOENT);
[861e7d1]814 return;
815 }
[ed903174]816
817 off64_t newoff;
818 switch (whence) {
[8df8415]819 case SEEK_SET:
820 if (off >= 0) {
821 file->pos = (aoff64_t) off;
[4fe94c66]822 vfs_file_put(file);
[ffa2c8ef]823 async_answer_1(rid, EOK, off);
[861e7d1]824 return;
[8df8415]825 }
826 break;
827 case SEEK_CUR:
828 if ((off >= 0) && (file->pos + off < file->pos)) {
[4fe94c66]829 vfs_file_put(file);
[ffa2c8ef]830 async_answer_0(rid, EOVERFLOW);
[8df8415]831 return;
832 }
833
834 if ((off < 0) && (file->pos < (aoff64_t) -off)) {
[4fe94c66]835 vfs_file_put(file);
[ffa2c8ef]836 async_answer_0(rid, EOVERFLOW);
[8df8415]837 return;
838 }
839
840 file->pos += off;
[b1956e3]841 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
[8df8415]842
[4fe94c66]843 vfs_file_put(file);
[ffa2c8ef]844 async_answer_2(rid, EOK, LOWER32(newoff),
[8df8415]845 UPPER32(newoff));
846 return;
847 case SEEK_END:
848 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[677745a]849 aoff64_t size = vfs_node_get_size(file->node);
[8df8415]850
851 if ((off >= 0) && (size + off < size)) {
[ed903174]852 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[4fe94c66]853 vfs_file_put(file);
[ffa2c8ef]854 async_answer_0(rid, EOVERFLOW);
[861e7d1]855 return;
[8df8415]856 }
857
858 if ((off < 0) && (size < (aoff64_t) -off)) {
859 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[4fe94c66]860 vfs_file_put(file);
[ffa2c8ef]861 async_answer_0(rid, EOVERFLOW);
[8df8415]862 return;
863 }
864
865 file->pos = size + off;
866 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
867
868 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[4fe94c66]869 vfs_file_put(file);
[ffa2c8ef]870 async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
[8df8415]871 return;
[861e7d1]872 }
[ed903174]873
[4fe94c66]874 vfs_file_put(file);
[ffa2c8ef]875 async_answer_0(rid, EINVAL);
[861e7d1]876}
877
[15f3c3f]878int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
[ed903174]879 fs_index_t index, aoff64_t size)
[7fe1f75]880{
[79ae36dd]881 async_exch_t *exch = vfs_exchange_grab(fs_handle);
882 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
[15f3c3f]883 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
[79ae36dd]884 UPPER32(size));
885 vfs_exchange_release(exch);
[7fe1f75]886
[79ae36dd]887 return (int) rc;
[7fe1f75]888}
889
[0ee4322]890void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
891{
892 int fd = IPC_GET_ARG1(*request);
[8df8415]893 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
894 IPC_GET_ARG3(*request));
[7fe1f75]895 int rc;
[0ee4322]896
897 vfs_file_t *file = vfs_file_get(fd);
898 if (!file) {
[ffa2c8ef]899 async_answer_0(rid, ENOENT);
[0ee4322]900 return;
901 }
902
[230260ac]903 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[7fe1f75]904 rc = vfs_truncate_internal(file->node->fs_handle,
[15f3c3f]905 file->node->service_id, file->node->index, size);
[0ee4322]906 if (rc == EOK)
907 file->node->size = size;
[230260ac]908 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[0ee4322]909
[4fe94c66]910 vfs_file_put(file);
[ffa2c8ef]911 async_answer_0(rid, (sysarg_t)rc);
[861e7d1]912}
913
[852b801]914void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
915{
916 int fd = IPC_GET_ARG1(*request);
[96b02eb9]917 sysarg_t rc;
[852b801]918
919 vfs_file_t *file = vfs_file_get(fd);
920 if (!file) {
[ffa2c8ef]921 async_answer_0(rid, ENOENT);
[852b801]922 return;
923 }
[c577a9a]924 assert(file->node);
[852b801]925
926 ipc_callid_t callid;
[0da4e41]927 if (!async_data_read_receive(&callid, NULL)) {
[4fe94c66]928 vfs_file_put(file);
[ffa2c8ef]929 async_answer_0(callid, EINVAL);
930 async_answer_0(rid, EINVAL);
[852b801]931 return;
932 }
933
[79ae36dd]934 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
[c577a9a]935 assert(exch);
[852b801]936
937 aid_t msg;
[15f3c3f]938 msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
[852b801]939 file->node->index, true, NULL);
[c577a9a]940 assert(msg);
[79ae36dd]941 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
942
943 vfs_exchange_release(exch);
944
[852b801]945 async_wait_for(msg, &rc);
[79ae36dd]946
[4fe94c66]947 vfs_file_put(file);
[ffa2c8ef]948 async_answer_0(rid, rc);
[852b801]949}
950
[5bcd5b7]951static void out_destroy(vfs_triplet_t *file)
952{
953 async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
954 async_msg_2(exch, VFS_OUT_DESTROY,
955 (sysarg_t) file->service_id, (sysarg_t) file->index);
956 vfs_exchange_release(exch);
957}
958
[20c071d]959void vfs_unlink2(ipc_callid_t rid, ipc_call_t *request)
[852b801]960{
[20c071d]961 int rc;
[472c09d]962 char *path;
[20c071d]963 vfs_file_t *parent = NULL;
964 vfs_file_t *expect = NULL;
[bf9dc4e2]965 vfs_node_t *parent_node = root;
[20c071d]966
967 int parentfd = IPC_GET_ARG1(*request);
968 int expectfd = IPC_GET_ARG2(*request);
969 int wflag = IPC_GET_ARG3(*request);
970
971 rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
[472c09d]972 if (rc != EOK) {
[ffa2c8ef]973 async_answer_0(rid, rc);
[415c7e0d]974 return;
975 }
[472c09d]976
[20c071d]977 fibril_rwlock_write_lock(&namespace_rwlock);
978
979 int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
[415c7e0d]980
[c577a9a]981 /* Files are retrieved in order of file descriptors, to prevent deadlock. */
982 if (parentfd >= 0 && parentfd < expectfd) {
[20c071d]983 parent = vfs_file_get(parentfd);
984 if (!parent) {
985 rc = ENOENT;
986 goto exit;
987 }
988 }
989
990 if (expectfd >= 0) {
991 expect = vfs_file_get(expectfd);
992 if (!expect) {
993 rc = ENOENT;
994 goto exit;
995 }
[c577a9a]996 }
997
998 if (parentfd >= 0 && parentfd >= expectfd) {
999 parent = vfs_file_get(parentfd);
1000 if (!parent) {
1001 rc = ENOENT;
1002 goto exit;
1003 }
1004 }
1005
1006 if (parent) {
1007 parent_node = parent->node;
1008 }
1009
1010 if (expectfd >= 0) {
[20c071d]1011 vfs_lookup_res_t lr;
[5bcd5b7]1012 rc = vfs_lookup_internal(parent_node, path, lflag, &lr);
[20c071d]1013 if (rc != EOK) {
1014 goto exit;
1015 }
1016
1017 if (__builtin_memcmp(&lr.triplet, expect->node, sizeof(vfs_triplet_t)) != 0) {
1018 rc = ENOENT;
1019 goto exit;
1020 }
1021
1022 vfs_file_put(expect);
1023 expect = NULL;
1024 }
1025
[415c7e0d]1026 vfs_lookup_res_t lr;
[5bcd5b7]1027 rc = vfs_lookup_internal(parent_node, path, lflag | L_UNLINK, &lr);
[415c7e0d]1028 if (rc != EOK) {
[20c071d]1029 goto exit;
[415c7e0d]1030 }
[20c071d]1031
[5bcd5b7]1032 /* If the node is not held by anyone, try to destroy it. */
1033 if (vfs_node_peek(&lr) == NULL) {
1034 out_destroy(&lr.triplet);
[415c7e0d]1035 }
1036
[20c071d]1037exit:
1038 if (path) {
1039 free(path);
1040 }
1041 if (parent) {
1042 vfs_file_put(parent);
1043 }
1044 if (expect) {
1045 vfs_file_put(expect);
1046 }
1047 fibril_rwlock_write_unlock(&namespace_rwlock);
1048 async_answer_0(rid, rc);
1049}
[415c7e0d]1050
[778d26d]1051static size_t shared_path(char *a, char *b)
[a8e9ab8d]1052{
[778d26d]1053 size_t res = 0;
[79ae36dd]1054
[778d26d]1055 while (a[res] == b[res] && a[res] != 0) {
1056 res++;
[a8e9ab8d]1057 }
[79ae36dd]1058
[778d26d]1059 if (a[res] == b[res]) {
1060 return res;
1061 }
[057760d3]1062
[778d26d]1063 res--;
1064 while (a[res] != '/') {
1065 res--;
[a8e9ab8d]1066 }
[778d26d]1067 return res;
[852b801]1068}
1069
[5bcd5b7]1070static int vfs_rename_internal(vfs_node_t *base, char *old, char *new)
[72bde81]1071{
[778d26d]1072 assert(base != NULL);
1073 assert(old != NULL);
1074 assert(new != NULL);
[472c09d]1075
[778d26d]1076 vfs_lookup_res_t base_lr;
1077 vfs_lookup_res_t old_lr;
1078 vfs_lookup_res_t new_lr_orig;
1079 bool orig_unlinked = false;
[472c09d]1080
[778d26d]1081 int rc;
1082
1083 size_t shared = shared_path(old, new);
[472c09d]1084
[778d26d]1085 /* Do not allow one path to be a prefix of the other. */
1086 if (old[shared] == 0 || new[shared] == 0) {
1087 return EINVAL;
[a8e9ab8d]1088 }
[778d26d]1089 assert(old[shared] == '/');
1090 assert(new[shared] == '/');
[72bde81]1091
[230260ac]1092 fibril_rwlock_write_lock(&namespace_rwlock);
[472c09d]1093
[778d26d]1094 /* Resolve the shared portion of the path first. */
1095 if (shared != 0) {
1096 old[shared] = 0;
1097 rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
1098 if (rc != EOK) {
1099 fibril_rwlock_write_unlock(&namespace_rwlock);
1100 return rc;
1101 }
1102
[5bcd5b7]1103 base = vfs_node_get(&base_lr);
[778d26d]1104 old[shared] = '/';
1105 old += shared;
1106 new += shared;
[5bcd5b7]1107 } else {
1108 vfs_node_addref(base);
[a8e9ab8d]1109 }
[472c09d]1110
[778d26d]1111
1112 rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS, &new_lr_orig);
1113 if (rc == EOK) {
1114 orig_unlinked = true;
1115 } else if (rc != ENOENT) {
[5bcd5b7]1116 vfs_node_put(base);
[230260ac]1117 fibril_rwlock_write_unlock(&namespace_rwlock);
[778d26d]1118 return rc;
[a8e9ab8d]1119 }
[472c09d]1120
[778d26d]1121 rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS, &old_lr);
[472c09d]1122 if (rc != EOK) {
[778d26d]1123 if (orig_unlinked) {
1124 vfs_link_internal(base, new, &new_lr_orig.triplet);
1125 }
[5bcd5b7]1126 vfs_node_put(base);
[230260ac]1127 fibril_rwlock_write_unlock(&namespace_rwlock);
[778d26d]1128 return rc;
[f15cf1a6]1129 }
1130
[778d26d]1131 rc = vfs_link_internal(base, new, &old_lr.triplet);
[f15cf1a6]1132 if (rc != EOK) {
[778d26d]1133 vfs_link_internal(base, old, &old_lr.triplet);
1134 if (orig_unlinked) {
1135 vfs_link_internal(base, new, &new_lr_orig.triplet);
1136 }
[5bcd5b7]1137 vfs_node_put(base);
[230260ac]1138 fibril_rwlock_write_unlock(&namespace_rwlock);
[778d26d]1139 return rc;
[f15cf1a6]1140 }
[472c09d]1141
[5bcd5b7]1142 /* If the node is not held by anyone, try to destroy it. */
1143 if (orig_unlinked && vfs_node_peek(&new_lr_orig) == NULL) {
1144 out_destroy(&new_lr_orig.triplet);
[a8e9ab8d]1145 }
[472c09d]1146
[5bcd5b7]1147 vfs_node_put(base);
[230260ac]1148 fibril_rwlock_write_unlock(&namespace_rwlock);
[778d26d]1149 return EOK;
[f15cf1a6]1150}
1151
[a8e9ab8d]1152void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1153{
[778d26d]1154 /* The common base directory. */
1155 int basefd;
1156 char *old = NULL;
1157 char *new = NULL;
1158 vfs_file_t *base = NULL;
1159 int rc;
[472c09d]1160
[778d26d]1161 basefd = IPC_GET_ARG1(*request);
1162
[a8e9ab8d]1163 /* Retrieve the old path. */
[778d26d]1164 rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
[472c09d]1165 if (rc != EOK) {
[778d26d]1166 goto out;
[a8e9ab8d]1167 }
1168
1169 /* Retrieve the new path. */
[4cac2d69]1170 rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
[472c09d]1171 if (rc != EOK) {
[778d26d]1172 goto out;
[a8e9ab8d]1173 }
[472c09d]1174
1175 size_t olen;
1176 size_t nlen;
[732bb0c]1177 char *oldc = canonify(old, &olen);
1178 char *newc = canonify(new, &nlen);
[472c09d]1179
1180 if ((!oldc) || (!newc)) {
[778d26d]1181 rc = EINVAL;
1182 goto out;
[a8e9ab8d]1183 }
[472c09d]1184
[778d26d]1185 assert(oldc[olen] == '\0');
1186 assert(newc[nlen] == '\0');
[472c09d]1187
[778d26d]1188 /* Lookup the file structure corresponding to the file descriptor. */
1189 vfs_node_t *base_node = root;
1190 // TODO: Client-side root.
1191 if (basefd != -1) {
1192 base = vfs_file_get(basefd);
1193 if (!base) {
1194 rc = EBADF;
1195 goto out;
[a8e9ab8d]1196 }
[778d26d]1197 base_node = base->node;
[a8e9ab8d]1198 }
[472c09d]1199
[5bcd5b7]1200 rc = vfs_rename_internal(base_node, oldc, newc);
[778d26d]1201
1202out:
1203 async_answer_0(rid, rc);
1204
1205 if (old) {
[a8e9ab8d]1206 free(old);
1207 }
[778d26d]1208 if (new) {
[a8e9ab8d]1209 free(new);
1210 }
[778d26d]1211 if (base) {
1212 vfs_file_put(base);
1213 }
[a8e9ab8d]1214}
1215
[2b88074b]1216void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1217{
1218 int oldfd = IPC_GET_ARG1(*request);
1219 int newfd = IPC_GET_ARG2(*request);
1220
[4fe94c66]1221 /* If the file descriptors are the same, do nothing. */
1222 if (oldfd == newfd) {
[ffa2c8ef]1223 async_answer_1(rid, EOK, newfd);
[4fe94c66]1224 return;
1225 }
1226
[2b88074b]1227 /* Lookup the file structure corresponding to oldfd. */
1228 vfs_file_t *oldfile = vfs_file_get(oldfd);
1229 if (!oldfile) {
[ffa2c8ef]1230 async_answer_0(rid, EBADF);
[2b88074b]1231 return;
1232 }
1233
[25bef0ff]1234 /* Make sure newfd is closed. */
1235 (void) vfs_fd_free(newfd);
[2b88074b]1236
1237 /* Assign the old file to newfd. */
1238 int ret = vfs_fd_assign(oldfile, newfd);
[4fe94c66]1239 vfs_file_put(oldfile);
[2b88074b]1240
1241 if (ret != EOK)
[ffa2c8ef]1242 async_answer_0(rid, ret);
[2b88074b]1243 else
[ffa2c8ef]1244 async_answer_1(rid, EOK, newfd);
[2b88074b]1245}
1246
[27b76ca]1247void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
1248{
1249 int fd = vfs_wait_handle_internal();
1250 async_answer_1(rid, EOK, fd);
1251}
1252
[76a67ce]1253void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
1254{
1255 ipc_callid_t callid;
1256 ipc_call_t data;
1257 sysarg_t rc = EOK;
1258 size_t len;
1259
1260 fibril_mutex_lock(&mtab_list_lock);
1261
1262 /* Send to the caller the number of mounted filesystems */
1263 callid = async_get_call(&data);
1264 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
1265 rc = ENOTSUP;
[7e8403b]1266 async_answer_0(callid, rc);
[76a67ce]1267 goto exit;
1268 }
1269 async_answer_1(callid, EOK, mtab_size);
1270
[feeac0d]1271 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
[76a67ce]1272 rc = ENOTSUP;
1273
[e6da6d5]1274 if (!async_data_read_receive(&callid, &len)) {
1275 async_answer_0(callid, rc);
[76a67ce]1276 goto exit;
[e6da6d5]1277 }
[76a67ce]1278
1279 (void) async_data_read_finalize(callid, mtab_ent->mp,
1280 str_size(mtab_ent->mp));
1281
[e6da6d5]1282 if (!async_data_read_receive(&callid, &len)) {
1283 async_answer_0(callid, rc);
[76a67ce]1284 goto exit;
[e6da6d5]1285 }
[76a67ce]1286
1287 (void) async_data_read_finalize(callid, mtab_ent->opts,
1288 str_size(mtab_ent->opts));
1289
[e6da6d5]1290 if (!async_data_read_receive(&callid, &len)) {
1291 async_answer_0(callid, rc);
[76a67ce]1292 goto exit;
[e6da6d5]1293 }
[76a67ce]1294
1295 (void) async_data_read_finalize(callid, mtab_ent->fs_name,
1296 str_size(mtab_ent->fs_name));
1297
[f8838b8]1298 callid = async_get_call(&data);
1299
1300 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
[7e8403b]1301 async_answer_0(callid, rc);
[f8838b8]1302 goto exit;
[76a67ce]1303 }
1304
1305 rc = EOK;
[6b8e5b74]1306 async_answer_2(callid, rc, mtab_ent->instance,
1307 mtab_ent->service_id);
[76a67ce]1308 }
1309
1310exit:
1311 fibril_mutex_unlock(&mtab_list_lock);
1312 async_answer_0(rid, rc);
1313}
1314
[66366470]1315void vfs_statfs(ipc_callid_t rid, ipc_call_t *request)
1316{
[9dc6083]1317 char *path;
1318 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
1319 if (rc != EOK) {
1320 async_answer_0(rid, rc);
1321 return;
1322 }
1323
1324 ipc_callid_t callid;
1325 if (!async_data_read_receive(&callid, NULL)) {
1326 free(path);
1327 async_answer_0(callid, EINVAL);
1328 async_answer_0(rid, EINVAL);
1329 return;
1330 }
1331
1332 vfs_lookup_res_t lr;
1333 fibril_rwlock_read_lock(&namespace_rwlock);
[1dff985]1334 rc = vfs_lookup_internal(root, path, L_NONE, &lr);
[9dc6083]1335 free(path);
1336 if (rc != EOK) {
1337 fibril_rwlock_read_unlock(&namespace_rwlock);
1338 async_answer_0(callid, rc);
1339 async_answer_0(rid, rc);
1340 return;
1341 }
1342 vfs_node_t *node = vfs_node_get(&lr);
1343 if (!node) {
1344 fibril_rwlock_read_unlock(&namespace_rwlock);
1345 async_answer_0(callid, ENOMEM);
1346 async_answer_0(rid, ENOMEM);
1347 return;
1348 }
1349
1350 fibril_rwlock_read_unlock(&namespace_rwlock);
1351
1352 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
1353
1354 aid_t msg;
1355 msg = async_send_3(exch, VFS_OUT_STATFS, node->service_id,
1356 node->index, false, NULL);
1357 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1358
1359 vfs_exchange_release(exch);
1360
1361 sysarg_t rv;
1362 async_wait_for(msg, &rv);
[66366470]1363
[9dc6083]1364 async_answer_0(rid, rv);
1365
1366 vfs_node_put(node);
[66366470]1367}
1368
[354b642]1369void vfs_op_clone(ipc_callid_t rid, ipc_call_t *request)
1370{
1371 int oldfd = IPC_GET_ARG1(*request);
1372 bool desc = IPC_GET_ARG2(*request);
1373
1374 /* Lookup the file structure corresponding to fd. */
1375 vfs_file_t *oldfile = vfs_file_get(oldfd);
1376 if (!oldfile) {
1377 async_answer_0(rid, EBADF);
1378 return;
1379 }
1380
1381 vfs_file_t *newfile;
1382 int newfd = vfs_fd_alloc(&newfile, desc);
1383 if (newfd < 0) {
1384 async_answer_0(rid, newfd);
1385 vfs_file_put(oldfile);
1386 return;
1387 }
1388
1389 assert(oldfile->node != NULL);
1390
1391 newfile->node = oldfile->node;
1392 newfile->permissions = oldfile->permissions;
1393
1394 vfs_file_put(oldfile);
1395 vfs_file_put(newfile);
1396
1397 async_answer_0(rid, newfd);
1398}
1399
[861e7d1]1400/**
1401 * @}
[05b9912]1402 */
Note: See TracBrowser for help on using the repository browser.