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

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

Introduce vfs_rdwr_internal()

This function can be used by the VFS itself to do read/write I/O on
client's file descriptor.

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