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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bf9dc4e2 was bf9dc4e2, checked in by Jiri Zarevucky <zarevucky.jiri@…>, 12 years ago

Relativize and simplify lookup().

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