source: mainline/uspace/srv/vfs/vfs_ops.c@ 6b8e5b74

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6b8e5b74 was 6b8e5b74, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

vfs_get_mtab(): service_id is more useful than flags and fs_handle

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