source: mainline/uspace/srv/vfs/vfs_ops.c@ 9b48c06

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9b48c06 was 9a8c188, checked in by Ji?? Z?rev?cky <zarevucky.jiri@…>, 12 years ago

Fix a bug and work around applications that call open() incorrectly.

  • Property mode set to 100644
File size: 39.1 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
[8dc72b64]268void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
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
[7f5e070]407void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
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
550 list_foreach(mtab_list, cur) {
[8d6a41c]551 mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
[76a67ce]552 link);
553
554 if (str_cmp(mtab_ent->mp, mp) == 0) {
[8d6a41c]555 list_remove(&mtab_ent->link);
[76a67ce]556 mtab_size--;
[8d6a41c]557 free(mtab_ent);
[76a67ce]558 found = 1;
559 break;
560 }
561 }
562 assert(found);
[4af2f36]563 fibril_mutex_unlock(&mtab_list_lock);
[76a67ce]564
565 free(mp);
566
[ffa2c8ef]567 async_answer_0(rid, EOK);
[7f5e070]568}
569
[0b18364]570static inline bool walk_flags_valid(int flags)
571{
572 if ((flags&~WALK_ALL_FLAGS) != 0) {
573 return false;
574 }
575 if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
576 return false;
577 }
578 if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
579 return false;
580 }
581 if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
582 if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
583 return false;
584 }
585 }
586 return true;
587}
588
589static inline int walk_lookup_flags(int flags)
590{
591 int lflags = 0;
592 if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
593 lflags |= L_CREATE;
594 }
595 if (flags&WALK_MUST_CREATE) {
596 lflags |= L_EXCLUSIVE;
597 }
598 if (flags&WALK_REGULAR) {
599 lflags |= L_FILE;
600 }
601 if (flags&WALK_DIRECTORY) {
602 lflags |= L_DIRECTORY;
603 }
604 return lflags;
605}
606
[cb65bbe]607void vfs_walk(ipc_callid_t rid, ipc_call_t *request)
608{
609 /*
610 * Parent is our relative root for file lookup.
611 * For defined flags, see <ipc/vfs.h>.
612 */
613 int parentfd = IPC_GET_ARG1(*request);
614 int flags = IPC_GET_ARG2(*request);
615
[0b18364]616 if (!walk_flags_valid(flags)) {
[cb65bbe]617 async_answer_0(rid, EINVAL);
618 return;
619 }
620
621 char *path;
622 int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
623
624 /* Lookup the file structure corresponding to the file descriptor. */
625 vfs_file_t *parent = NULL;
626 vfs_pair_t *parent_node = NULL;
627 // TODO: Client-side root.
628 if (parentfd != -1) {
629 parent = vfs_file_get(parentfd);
630 if (!parent) {
631 free(path);
632 async_answer_0(rid, EBADF);
633 return;
634 }
635 parent_node = (vfs_pair_t *)parent->node;
636 }
637
638 fibril_rwlock_read_lock(&namespace_rwlock);
639
640 vfs_lookup_res_t lr;
[0b18364]641 rc = vfs_lookup_internal(path, walk_lookup_flags(flags), &lr, parent_node);
[cb65bbe]642 free(path);
643
644 if (rc != EOK) {
645 fibril_rwlock_read_unlock(&namespace_rwlock);
646 if (parent) {
647 vfs_file_put(parent);
648 }
649 async_answer_0(rid, rc);
650 return;
651 }
652
653 vfs_node_t *node = vfs_node_get(&lr);
654
655 int fd = vfs_fd_alloc(false);
656 if (fd < 0) {
657 vfs_node_put(node);
658 if (parent) {
659 vfs_file_put(parent);
660 }
661 async_answer_0(rid, fd);
662 return;
663 }
664
665 vfs_file_t *file = vfs_file_get(fd);
666 assert(file != NULL);
667
668 file->node = node;
669 if (parent) {
670 file->permissions = parent->permissions;
671 } else {
672 file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
673 }
674 file->open_read = false;
675 file->open_write = false;
676
677 vfs_node_addref(node);
678 vfs_node_put(node);
679 vfs_file_put(file);
680 if (parent) {
681 vfs_file_put(parent);
682 }
683
684 fibril_rwlock_read_unlock(&namespace_rwlock);
685
686 async_answer_1(rid, EOK, fd);
687}
688
689void vfs_open2(ipc_callid_t rid, ipc_call_t *request)
690{
691 int fd = IPC_GET_ARG1(*request);
692 int flags = IPC_GET_ARG2(*request);
693
694 if (flags == 0) {
695 async_answer_0(rid, EINVAL);
696 return;
697 }
698
699 vfs_file_t *file = vfs_file_get(fd);
700 if (!file) {
701 async_answer_0(rid, EBADF);
702 return;
703 }
704
705 if ((flags & ~file->permissions) != 0) {
706 vfs_file_put(file);
707 async_answer_0(rid, EPERM);
708 return;
709 }
710
711 file->open_read = (flags & MODE_READ) != 0;
712 file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
713 file->append = (flags & MODE_APPEND) != 0;
714
715 if (!file->open_read && !file->open_write) {
716 vfs_file_put(file);
717 async_answer_0(rid, EINVAL);
718 return;
719 }
720
721 if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
722 file->open_read = file->open_write = false;
723 vfs_file_put(file);
724 async_answer_0(rid, EINVAL);
725 return;
726 }
727
728 int rc = vfs_open_node_remote(file->node);
729 if (rc != EOK) {
730 file->open_read = file->open_write = false;
731 vfs_file_put(file);
732 async_answer_0(rid, rc);
733 return;
734 }
735
736 vfs_file_put(file);
737 async_answer_0(rid, EOK);
738}
739
[861e7d1]740void vfs_open(ipc_callid_t rid, ipc_call_t *request)
741{
742 /*
[ae78b530]743 * The POSIX interface is open(path, oflag, mode).
[4198f9c3]744 * We can receive oflags and mode along with the VFS_IN_OPEN call;
745 * the path will need to arrive in another call.
[ae78b530]746 *
747 * We also receive one private, non-POSIX set of flags called lflag
748 * used to pass information to vfs_lookup_internal().
[861e7d1]749 */
[ae78b530]750 int lflag = IPC_GET_ARG1(*request);
751 int oflag = IPC_GET_ARG2(*request);
752 int mode = IPC_GET_ARG3(*request);
[dd2cfa7]753
754 /* Ignore mode for now. */
755 (void) mode;
[05b9912]756
[b17186d]757 /*
758 * Make sure that we are called with exactly one of L_FILE and
[f7376cbf]759 * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
760 * L_ROOT or L_MP.
[b17186d]761 */
[230260ac]762 if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
763 ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
[f7376cbf]764 (lflag & (L_OPEN | L_ROOT | L_MP))) {
[ffa2c8ef]765 async_answer_0(rid, EINVAL);
[b17186d]766 return;
767 }
[05b9912]768
[9a8c188]769 if ((((oflag & O_RDONLY) != 0) + ((oflag & O_WRONLY) != 0) + ((oflag & O_RDWR) != 0)) != 1) {
770 async_answer_0(rid, EINVAL);
771 return;
772 }
773
[2db4ac8]774 if (oflag & O_CREAT)
775 lflag |= L_CREATE;
776 if (oflag & O_EXCL)
777 lflag |= L_EXCLUSIVE;
[05b9912]778
[472c09d]779 char *path;
[4cac2d69]780 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
[472c09d]781 if (rc != EOK) {
[ffa2c8ef]782 async_answer_0(rid, rc);
[861e7d1]783 return;
784 }
785
786 /*
787 * Avoid the race condition in which the file can be deleted before we
788 * find/create-and-lock the VFS node corresponding to the looked-up
789 * triplet.
790 */
[2db4ac8]791 if (lflag & L_CREATE)
[230260ac]792 fibril_rwlock_write_lock(&namespace_rwlock);
[2db4ac8]793 else
[230260ac]794 fibril_rwlock_read_lock(&namespace_rwlock);
[05b9912]795
[72bde81]796 /* The path is now populated and we can call vfs_lookup_internal(). */
[eb27ce5a]797 vfs_lookup_res_t lr;
[05b9912]798 rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
799 if (rc != EOK) {
[2db4ac8]800 if (lflag & L_CREATE)
[230260ac]801 fibril_rwlock_write_unlock(&namespace_rwlock);
[2db4ac8]802 else
[230260ac]803 fibril_rwlock_read_unlock(&namespace_rwlock);
[ffa2c8ef]804 async_answer_0(rid, rc);
[861e7d1]805 free(path);
806 return;
807 }
[05b9912]808
[7fe1f75]809 /* Path is no longer needed. */
[861e7d1]810 free(path);
[05b9912]811
[eb27ce5a]812 vfs_node_t *node = vfs_node_get(&lr);
[2db4ac8]813 if (lflag & L_CREATE)
[230260ac]814 fibril_rwlock_write_unlock(&namespace_rwlock);
[2db4ac8]815 else
[230260ac]816 fibril_rwlock_read_unlock(&namespace_rwlock);
[05b9912]817
[7fe1f75]818 /* Truncate the file if requested and if necessary. */
819 if (oflag & O_TRUNC) {
[230260ac]820 fibril_rwlock_write_lock(&node->contents_rwlock);
[7fe1f75]821 if (node->size) {
822 rc = vfs_truncate_internal(node->fs_handle,
[15f3c3f]823 node->service_id, node->index, 0);
[7fe1f75]824 if (rc) {
[230260ac]825 fibril_rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]826 vfs_node_put(node);
[ffa2c8ef]827 async_answer_0(rid, rc);
[7fe1f75]828 return;
829 }
830 node->size = 0;
831 }
[230260ac]832 fibril_rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]833 }
[05b9912]834
[861e7d1]835 /*
836 * Get ourselves a file descriptor and the corresponding vfs_file_t
837 * structure.
838 */
[2b88074b]839 int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
[861e7d1]840 if (fd < 0) {
841 vfs_node_put(node);
[ffa2c8ef]842 async_answer_0(rid, fd);
[861e7d1]843 return;
844 }
845 vfs_file_t *file = vfs_file_get(fd);
[cb65bbe]846
[9a8c188]847 /* FIXME: There is a potential race with another fibril of a malicious client. */
848 assert(file);
[cb65bbe]849
[861e7d1]850 file->node = node;
[cb65bbe]851 if (oflag & O_RDONLY)
852 file->open_read = true;
853 if (oflag & O_WRONLY)
854 file->open_write = true;
855 if (oflag & O_RDWR)
856 file->open_read = file->open_write = true;
[05b9912]857 if (oflag & O_APPEND)
[15b9970]858 file->append = true;
[9a8c188]859 assert(file->open_read || file->open_write);
[05b9912]860
[861e7d1]861 /*
862 * The following increase in reference count is for the fact that the
863 * file is being opened and that a file structure is pointing to it.
864 * It is necessary so that the file will not disappear when
865 * vfs_node_put() is called. The reference will be dropped by the
[4198f9c3]866 * respective VFS_IN_CLOSE.
[861e7d1]867 */
868 vfs_node_addref(node);
869 vfs_node_put(node);
[4fe94c66]870 vfs_file_put(file);
[05b9912]871
872 /* Success! Return the new file descriptor to the client. */
[ffa2c8ef]873 async_answer_1(rid, EOK, fd);
[05b9912]874}
[861e7d1]875
[05b9912]876void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
877{
878 int fd = IPC_GET_ARG1(*request);
879
880 /* Lookup the file structure corresponding to the file descriptor. */
881 vfs_file_t *file = vfs_file_get(fd);
882 if (!file) {
[ffa2c8ef]883 async_answer_0(rid, ENOENT);
[05b9912]884 return;
885 }
886
887 /*
888 * Lock the open file structure so that no other thread can manipulate
889 * the same open file at a time.
890 */
[230260ac]891 fibril_mutex_lock(&file->lock);
[79ae36dd]892 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[05b9912]893
[4198f9c3]894 /* Make a VFS_OUT_SYMC request at the destination FS server. */
[05b9912]895 aid_t msg;
896 ipc_call_t answer;
[15f3c3f]897 msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
[4198f9c3]898 file->node->index, &answer);
[79ae36dd]899
900 vfs_exchange_release(fs_exch);
901
[05b9912]902 /* Wait for reply from the FS server. */
[96b02eb9]903 sysarg_t rc;
[05b9912]904 async_wait_for(msg, &rc);
905
[230260ac]906 fibril_mutex_unlock(&file->lock);
[79ae36dd]907
[4fe94c66]908 vfs_file_put(file);
[ffa2c8ef]909 async_answer_0(rid, rc);
[e704503]910}
911
[05b9912]912void vfs_close(ipc_callid_t rid, ipc_call_t *request)
913{
914 int fd = IPC_GET_ARG1(*request);
[79ae36dd]915 int ret = vfs_fd_free(fd);
[ffa2c8ef]916 async_answer_0(rid, ret);
[05b9912]917}
918
[861e7d1]919static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
920{
921 /*
922 * The following code strongly depends on the fact that the files data
923 * structure can be only accessed by a single fibril and all file
924 * operations are serialized (i.e. the reads and writes cannot
925 * interleave and a file cannot be closed while it is being read).
926 *
927 * Additional synchronization needs to be added once the table of
928 * open files supports parallel access!
929 */
[79ae36dd]930
[861e7d1]931 int fd = IPC_GET_ARG1(*request);
[6c89f20]932
[72bde81]933 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]934 vfs_file_t *file = vfs_file_get(fd);
935 if (!file) {
[ffa2c8ef]936 async_answer_0(rid, ENOENT);
[861e7d1]937 return;
938 }
[6c89f20]939
[861e7d1]940 /*
941 * Lock the open file structure so that no other thread can manipulate
942 * the same open file at a time.
943 */
[230260ac]944 fibril_mutex_lock(&file->lock);
[79ae36dd]945
[cb65bbe]946 if ((read && !file->open_read) || (!read && !file->open_write)) {
947 fibril_mutex_unlock(&file->lock);
948 async_answer_0(rid, EINVAL);
949 return;
950 }
951
[79ae36dd]952 vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
953 assert(fs_info);
954
[861e7d1]955 /*
956 * Lock the file's node so that no other client can read/write to it at
[c2f4b6b]957 * the same time unless the FS supports concurrent reads/writes and its
958 * write implementation does not modify the file size.
[861e7d1]959 */
[79ae36dd]960 if ((read) ||
961 ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
[230260ac]962 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[861e7d1]963 else
[230260ac]964 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[79ae36dd]965
[b17186d]966 if (file->node->type == VFS_NODE_DIRECTORY) {
967 /*
968 * Make sure that no one is modifying the namespace
969 * while we are in readdir().
970 */
971 assert(read);
[230260ac]972 fibril_rwlock_read_lock(&namespace_rwlock);
[b17186d]973 }
[6c89f20]974
[79ae36dd]975 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
[861e7d1]976
977 /*
[b4cbef1]978 * Make a VFS_READ/VFS_WRITE request at the destination FS server
979 * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
[861e7d1]980 * destination FS server. The call will be routed as if sent by
981 * ourselves. Note that call arguments are immutable in this case so we
982 * don't have to bother.
983 */
[96b02eb9]984 sysarg_t rc;
[b4cbef1]985 ipc_call_t answer;
986 if (read) {
[5bb9907]987 rc = async_data_read_forward_4_1(fs_exch, VFS_OUT_READ,
[86ffa27f]988 file->node->service_id, file->node->index,
[5bb9907]989 LOWER32(file->pos), UPPER32(file->pos), &answer);
[b4cbef1]990 } else {
[3a4b3ba]991 if (file->append)
992 file->pos = file->node->size;
993
[5bb9907]994 rc = async_data_write_forward_4_1(fs_exch, VFS_OUT_WRITE,
[86ffa27f]995 file->node->service_id, file->node->index,
[5bb9907]996 LOWER32(file->pos), UPPER32(file->pos), &answer);
[b4cbef1]997 }
[05b9912]998
[79ae36dd]999 vfs_exchange_release(fs_exch);
[34ca870]1000
[861e7d1]1001 size_t bytes = IPC_GET_ARG1(answer);
[b4cbef1]1002
[b17186d]1003 if (file->node->type == VFS_NODE_DIRECTORY)
[230260ac]1004 fibril_rwlock_read_unlock(&namespace_rwlock);
[6c89f20]1005
[72bde81]1006 /* Unlock the VFS node. */
[79ae36dd]1007 if ((read) ||
1008 ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
[230260ac]1009 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[861e7d1]1010 else {
1011 /* Update the cached version of node's size. */
[f7017572]1012 if (rc == EOK)
[5bb9907]1013 file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
1014 IPC_GET_ARG3(answer));
[230260ac]1015 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[861e7d1]1016 }
[6c89f20]1017
[72bde81]1018 /* Update the position pointer and unlock the open file. */
[f7017572]1019 if (rc == EOK)
1020 file->pos += bytes;
[230260ac]1021 fibril_mutex_unlock(&file->lock);
[4fe94c66]1022 vfs_file_put(file);
1023
[861e7d1]1024 /*
1025 * FS server's reply is the final result of the whole operation we
1026 * return to the client.
1027 */
[ffa2c8ef]1028 async_answer_1(rid, rc, bytes);
[861e7d1]1029}
1030
1031void vfs_read(ipc_callid_t rid, ipc_call_t *request)
1032{
1033 vfs_rdwr(rid, request, true);
1034}
1035
1036void vfs_write(ipc_callid_t rid, ipc_call_t *request)
1037{
1038 vfs_rdwr(rid, request, false);
1039}
1040
1041void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
1042{
1043 int fd = (int) IPC_GET_ARG1(*request);
[8df8415]1044 off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
1045 IPC_GET_ARG3(*request));
[ed903174]1046 int whence = (int) IPC_GET_ARG4(*request);
1047
[72bde81]1048 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]1049 vfs_file_t *file = vfs_file_get(fd);
1050 if (!file) {
[ffa2c8ef]1051 async_answer_0(rid, ENOENT);
[861e7d1]1052 return;
1053 }
[ed903174]1054
[230260ac]1055 fibril_mutex_lock(&file->lock);
[ed903174]1056
1057 off64_t newoff;
1058 switch (whence) {
[8df8415]1059 case SEEK_SET:
1060 if (off >= 0) {
1061 file->pos = (aoff64_t) off;
[230260ac]1062 fibril_mutex_unlock(&file->lock);
[4fe94c66]1063 vfs_file_put(file);
[ffa2c8ef]1064 async_answer_1(rid, EOK, off);
[861e7d1]1065 return;
[8df8415]1066 }
1067 break;
1068 case SEEK_CUR:
1069 if ((off >= 0) && (file->pos + off < file->pos)) {
1070 fibril_mutex_unlock(&file->lock);
[4fe94c66]1071 vfs_file_put(file);
[ffa2c8ef]1072 async_answer_0(rid, EOVERFLOW);
[8df8415]1073 return;
1074 }
1075
1076 if ((off < 0) && (file->pos < (aoff64_t) -off)) {
1077 fibril_mutex_unlock(&file->lock);
[4fe94c66]1078 vfs_file_put(file);
[ffa2c8ef]1079 async_answer_0(rid, EOVERFLOW);
[8df8415]1080 return;
1081 }
1082
1083 file->pos += off;
1084 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
1085
1086 fibril_mutex_unlock(&file->lock);
[4fe94c66]1087 vfs_file_put(file);
[ffa2c8ef]1088 async_answer_2(rid, EOK, LOWER32(newoff),
[8df8415]1089 UPPER32(newoff));
1090 return;
1091 case SEEK_END:
1092 fibril_rwlock_read_lock(&file->node->contents_rwlock);
1093 aoff64_t size = file->node->size;
1094
1095 if ((off >= 0) && (size + off < size)) {
[ed903174]1096 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[230260ac]1097 fibril_mutex_unlock(&file->lock);
[4fe94c66]1098 vfs_file_put(file);
[ffa2c8ef]1099 async_answer_0(rid, EOVERFLOW);
[861e7d1]1100 return;
[8df8415]1101 }
1102
1103 if ((off < 0) && (size < (aoff64_t) -off)) {
1104 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
1105 fibril_mutex_unlock(&file->lock);
[4fe94c66]1106 vfs_file_put(file);
[ffa2c8ef]1107 async_answer_0(rid, EOVERFLOW);
[8df8415]1108 return;
1109 }
1110
1111 file->pos = size + off;
1112 newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
1113
1114 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
1115 fibril_mutex_unlock(&file->lock);
[4fe94c66]1116 vfs_file_put(file);
[ffa2c8ef]1117 async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
[8df8415]1118 return;
[861e7d1]1119 }
[ed903174]1120
[230260ac]1121 fibril_mutex_unlock(&file->lock);
[4fe94c66]1122 vfs_file_put(file);
[ffa2c8ef]1123 async_answer_0(rid, EINVAL);
[861e7d1]1124}
1125
[15f3c3f]1126int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
[ed903174]1127 fs_index_t index, aoff64_t size)
[7fe1f75]1128{
[79ae36dd]1129 async_exch_t *exch = vfs_exchange_grab(fs_handle);
1130 sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
[15f3c3f]1131 (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
[79ae36dd]1132 UPPER32(size));
1133 vfs_exchange_release(exch);
[7fe1f75]1134
[79ae36dd]1135 return (int) rc;
[7fe1f75]1136}
1137
[0ee4322]1138void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
1139{
1140 int fd = IPC_GET_ARG1(*request);
[8df8415]1141 aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
1142 IPC_GET_ARG3(*request));
[7fe1f75]1143 int rc;
[0ee4322]1144
1145 vfs_file_t *file = vfs_file_get(fd);
1146 if (!file) {
[ffa2c8ef]1147 async_answer_0(rid, ENOENT);
[0ee4322]1148 return;
1149 }
[230260ac]1150 fibril_mutex_lock(&file->lock);
[0ee4322]1151
[230260ac]1152 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[7fe1f75]1153 rc = vfs_truncate_internal(file->node->fs_handle,
[15f3c3f]1154 file->node->service_id, file->node->index, size);
[0ee4322]1155 if (rc == EOK)
1156 file->node->size = size;
[230260ac]1157 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[0ee4322]1158
[230260ac]1159 fibril_mutex_unlock(&file->lock);
[4fe94c66]1160 vfs_file_put(file);
[ffa2c8ef]1161 async_answer_0(rid, (sysarg_t)rc);
[861e7d1]1162}
1163
[852b801]1164void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
1165{
1166 int fd = IPC_GET_ARG1(*request);
[96b02eb9]1167 sysarg_t rc;
[852b801]1168
1169 vfs_file_t *file = vfs_file_get(fd);
1170 if (!file) {
[ffa2c8ef]1171 async_answer_0(rid, ENOENT);
[852b801]1172 return;
1173 }
1174
1175 ipc_callid_t callid;
[0da4e41]1176 if (!async_data_read_receive(&callid, NULL)) {
[4fe94c66]1177 vfs_file_put(file);
[ffa2c8ef]1178 async_answer_0(callid, EINVAL);
1179 async_answer_0(rid, EINVAL);
[852b801]1180 return;
1181 }
1182
1183 fibril_mutex_lock(&file->lock);
1184
[79ae36dd]1185 async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
[852b801]1186
1187 aid_t msg;
[15f3c3f]1188 msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
[852b801]1189 file->node->index, true, NULL);
[79ae36dd]1190 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1191
1192 vfs_exchange_release(exch);
1193
[852b801]1194 async_wait_for(msg, &rc);
[79ae36dd]1195
[852b801]1196 fibril_mutex_unlock(&file->lock);
[4fe94c66]1197 vfs_file_put(file);
[ffa2c8ef]1198 async_answer_0(rid, rc);
[852b801]1199}
1200
1201void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
1202{
[472c09d]1203 char *path;
[4cac2d69]1204 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
[472c09d]1205 if (rc != EOK) {
[ffa2c8ef]1206 async_answer_0(rid, rc);
[415c7e0d]1207 return;
1208 }
[472c09d]1209
1210 ipc_callid_t callid;
[0da4e41]1211 if (!async_data_read_receive(&callid, NULL)) {
[415c7e0d]1212 free(path);
[ffa2c8ef]1213 async_answer_0(callid, EINVAL);
1214 async_answer_0(rid, EINVAL);
[415c7e0d]1215 return;
1216 }
1217
1218 vfs_lookup_res_t lr;
1219 fibril_rwlock_read_lock(&namespace_rwlock);
1220 rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
1221 free(path);
1222 if (rc != EOK) {
1223 fibril_rwlock_read_unlock(&namespace_rwlock);
[ffa2c8ef]1224 async_answer_0(callid, rc);
1225 async_answer_0(rid, rc);
[415c7e0d]1226 return;
1227 }
1228 vfs_node_t *node = vfs_node_get(&lr);
1229 if (!node) {
1230 fibril_rwlock_read_unlock(&namespace_rwlock);
[ffa2c8ef]1231 async_answer_0(callid, ENOMEM);
1232 async_answer_0(rid, ENOMEM);
[415c7e0d]1233 return;
1234 }
1235
1236 fibril_rwlock_read_unlock(&namespace_rwlock);
1237
[79ae36dd]1238 async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
1239
[415c7e0d]1240 aid_t msg;
[15f3c3f]1241 msg = async_send_3(exch, VFS_OUT_STAT, node->service_id,
[415c7e0d]1242 node->index, false, NULL);
[79ae36dd]1243 async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
1244
1245 vfs_exchange_release(exch);
[057760d3]1246
[96b02eb9]1247 sysarg_t rv;
[057760d3]1248 async_wait_for(msg, &rv);
[415c7e0d]1249
[ffa2c8ef]1250 async_answer_0(rid, rv);
[415c7e0d]1251
1252 vfs_node_put(node);
[852b801]1253}
1254
[72bde81]1255void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
1256{
1257 int mode = IPC_GET_ARG1(*request);
[472c09d]1258
1259 char *path;
[4cac2d69]1260 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
[472c09d]1261 if (rc != EOK) {
[ffa2c8ef]1262 async_answer_0(rid, rc);
[72bde81]1263 return;
1264 }
[472c09d]1265
[dd2cfa7]1266 /* Ignore mode for now. */
1267 (void) mode;
[72bde81]1268
[230260ac]1269 fibril_rwlock_write_lock(&namespace_rwlock);
[72bde81]1270 int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
[d6084ef]1271 rc = vfs_lookup_internal(path, lflag, NULL, NULL);
[230260ac]1272 fibril_rwlock_write_unlock(&namespace_rwlock);
[72bde81]1273 free(path);
[ffa2c8ef]1274 async_answer_0(rid, rc);
[72bde81]1275}
1276
[f15cf1a6]1277void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1278{
1279 int lflag = IPC_GET_ARG1(*request);
[472c09d]1280
1281 char *path;
[4cac2d69]1282 int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
[472c09d]1283 if (rc != EOK) {
[ffa2c8ef]1284 async_answer_0(rid, rc);
[f15cf1a6]1285 return;
1286 }
1287
[230260ac]1288 fibril_rwlock_write_lock(&namespace_rwlock);
[f15cf1a6]1289 lflag &= L_DIRECTORY; /* sanitize lflag */
1290 vfs_lookup_res_t lr;
[a8e9ab8d]1291 rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
[f15cf1a6]1292 free(path);
1293 if (rc != EOK) {
[230260ac]1294 fibril_rwlock_write_unlock(&namespace_rwlock);
[ffa2c8ef]1295 async_answer_0(rid, rc);
[f15cf1a6]1296 return;
1297 }
1298
1299 /*
1300 * The name has already been unlinked by vfs_lookup_internal().
1301 * We have to get and put the VFS node to ensure that it is
[4198f9c3]1302 * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
[f15cf1a6]1303 */
1304 vfs_node_t *node = vfs_node_get(&lr);
[553492be]1305 fibril_mutex_lock(&nodes_mutex);
[f15cf1a6]1306 node->lnkcnt--;
[553492be]1307 fibril_mutex_unlock(&nodes_mutex);
[230260ac]1308 fibril_rwlock_write_unlock(&namespace_rwlock);
[f15cf1a6]1309 vfs_node_put(node);
[ffa2c8ef]1310 async_answer_0(rid, EOK);
[f15cf1a6]1311}
1312
[a8e9ab8d]1313void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1314{
1315 /* Retrieve the old path. */
[472c09d]1316 char *old;
[4cac2d69]1317 int rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
[472c09d]1318 if (rc != EOK) {
[ffa2c8ef]1319 async_answer_0(rid, rc);
[a8e9ab8d]1320 return;
1321 }
1322
1323 /* Retrieve the new path. */
[472c09d]1324 char *new;
[4cac2d69]1325 rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
[472c09d]1326 if (rc != EOK) {
[a8e9ab8d]1327 free(old);
[ffa2c8ef]1328 async_answer_0(rid, rc);
[a8e9ab8d]1329 return;
1330 }
[472c09d]1331
1332 size_t olen;
1333 size_t nlen;
[732bb0c]1334 char *oldc = canonify(old, &olen);
1335 char *newc = canonify(new, &nlen);
[472c09d]1336
1337 if ((!oldc) || (!newc)) {
[ffa2c8ef]1338 async_answer_0(rid, EINVAL);
[a8e9ab8d]1339 free(old);
1340 free(new);
1341 return;
1342 }
[472c09d]1343
[732bb0c]1344 oldc[olen] = '\0';
1345 newc[nlen] = '\0';
[472c09d]1346
[14040e5]1347 if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1348 ((newc[str_length(oldc)] == '/') ||
1349 (str_length(oldc) == 1) ||
1350 (str_length(oldc) == str_length(newc)))) {
1351 /*
1352 * oldc is a prefix of newc and either
1353 * - newc continues with a / where oldc ends, or
1354 * - oldc was / itself, or
1355 * - oldc and newc are equal.
1356 */
[ffa2c8ef]1357 async_answer_0(rid, EINVAL);
[a8e9ab8d]1358 free(old);
1359 free(new);
1360 return;
1361 }
1362
1363 vfs_lookup_res_t old_lr;
1364 vfs_lookup_res_t new_lr;
1365 vfs_lookup_res_t new_par_lr;
[230260ac]1366 fibril_rwlock_write_lock(&namespace_rwlock);
[472c09d]1367
[a8e9ab8d]1368 /* Lookup the node belonging to the old file name. */
1369 rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1370 if (rc != EOK) {
[230260ac]1371 fibril_rwlock_write_unlock(&namespace_rwlock);
[ffa2c8ef]1372 async_answer_0(rid, rc);
[a8e9ab8d]1373 free(old);
1374 free(new);
1375 return;
1376 }
[472c09d]1377
[a8e9ab8d]1378 vfs_node_t *old_node = vfs_node_get(&old_lr);
1379 if (!old_node) {
[230260ac]1380 fibril_rwlock_write_unlock(&namespace_rwlock);
[ffa2c8ef]1381 async_answer_0(rid, ENOMEM);
[a8e9ab8d]1382 free(old);
1383 free(new);
1384 return;
1385 }
[472c09d]1386
[4f46695e]1387 /* Determine the path to the parent of the node with the new name. */
1388 char *parentc = str_dup(newc);
1389 if (!parentc) {
[230260ac]1390 fibril_rwlock_write_unlock(&namespace_rwlock);
[71af5a4]1391 vfs_node_put(old_node);
[ffa2c8ef]1392 async_answer_0(rid, rc);
[4f46695e]1393 free(old);
1394 free(new);
1395 return;
1396 }
[472c09d]1397
[ae55ee8]1398 char *lastsl = str_rchr(parentc + 1, '/');
[4f46695e]1399 if (lastsl)
1400 *lastsl = '\0';
1401 else
1402 parentc[1] = '\0';
[472c09d]1403
[a8e9ab8d]1404 /* Lookup parent of the new file name. */
[4f46695e]1405 rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1406 free(parentc); /* not needed anymore */
[a8e9ab8d]1407 if (rc != EOK) {
[230260ac]1408 fibril_rwlock_write_unlock(&namespace_rwlock);
[71af5a4]1409 vfs_node_put(old_node);
[ffa2c8ef]1410 async_answer_0(rid, rc);
[a8e9ab8d]1411 free(old);
1412 free(new);
1413 return;
1414 }
[472c09d]1415
[a8e9ab8d]1416 /* Check whether linking to the same file system instance. */
1417 if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
[15f3c3f]1418 (old_node->service_id != new_par_lr.triplet.service_id)) {
[230260ac]1419 fibril_rwlock_write_unlock(&namespace_rwlock);
[71af5a4]1420 vfs_node_put(old_node);
[ffa2c8ef]1421 async_answer_0(rid, EXDEV); /* different file systems */
[a8e9ab8d]1422 free(old);
1423 free(new);
1424 return;
1425 }
[472c09d]1426
[a8e9ab8d]1427 /* Destroy the old link for the new name. */
1428 vfs_node_t *new_node = NULL;
1429 rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
[472c09d]1430
[a8e9ab8d]1431 switch (rc) {
1432 case ENOENT:
1433 /* simply not in our way */
1434 break;
1435 case EOK:
1436 new_node = vfs_node_get(&new_lr);
1437 if (!new_node) {
[230260ac]1438 fibril_rwlock_write_unlock(&namespace_rwlock);
[71af5a4]1439 vfs_node_put(old_node);
[ffa2c8ef]1440 async_answer_0(rid, ENOMEM);
[a8e9ab8d]1441 free(old);
1442 free(new);
1443 return;
1444 }
[553492be]1445 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1446 new_node->lnkcnt--;
[553492be]1447 fibril_mutex_unlock(&nodes_mutex);
[a8e9ab8d]1448 break;
1449 default:
[230260ac]1450 fibril_rwlock_write_unlock(&namespace_rwlock);
[71af5a4]1451 vfs_node_put(old_node);
[ffa2c8ef]1452 async_answer_0(rid, ENOTEMPTY);
[a8e9ab8d]1453 free(old);
1454 free(new);
1455 return;
1456 }
[472c09d]1457
[a8e9ab8d]1458 /* Create the new link for the new name. */
1459 rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1460 if (rc != EOK) {
[230260ac]1461 fibril_rwlock_write_unlock(&namespace_rwlock);
[71af5a4]1462 vfs_node_put(old_node);
[a8e9ab8d]1463 if (new_node)
1464 vfs_node_put(new_node);
[ffa2c8ef]1465 async_answer_0(rid, rc);
[a8e9ab8d]1466 free(old);
1467 free(new);
1468 return;
1469 }
[472c09d]1470
[553492be]1471 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1472 old_node->lnkcnt++;
[553492be]1473 fibril_mutex_unlock(&nodes_mutex);
[472c09d]1474
[a8e9ab8d]1475 /* Destroy the link for the old name. */
1476 rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1477 if (rc != EOK) {
[230260ac]1478 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1479 vfs_node_put(old_node);
1480 if (new_node)
1481 vfs_node_put(new_node);
[ffa2c8ef]1482 async_answer_0(rid, rc);
[a8e9ab8d]1483 free(old);
1484 free(new);
1485 return;
1486 }
[472c09d]1487
[553492be]1488 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1489 old_node->lnkcnt--;
[553492be]1490 fibril_mutex_unlock(&nodes_mutex);
[230260ac]1491 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1492 vfs_node_put(old_node);
[472c09d]1493
[a8e9ab8d]1494 if (new_node)
1495 vfs_node_put(new_node);
[472c09d]1496
[a8e9ab8d]1497 free(old);
1498 free(new);
[ffa2c8ef]1499 async_answer_0(rid, EOK);
[a8e9ab8d]1500}
1501
[2b88074b]1502void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
1503{
1504 int oldfd = IPC_GET_ARG1(*request);
1505 int newfd = IPC_GET_ARG2(*request);
1506
[4fe94c66]1507 /* If the file descriptors are the same, do nothing. */
1508 if (oldfd == newfd) {
[ffa2c8ef]1509 async_answer_1(rid, EOK, newfd);
[4fe94c66]1510 return;
1511 }
1512
[2b88074b]1513 /* Lookup the file structure corresponding to oldfd. */
1514 vfs_file_t *oldfile = vfs_file_get(oldfd);
1515 if (!oldfile) {
[ffa2c8ef]1516 async_answer_0(rid, EBADF);
[2b88074b]1517 return;
1518 }
1519
1520 /*
1521 * Lock the open file structure so that no other thread can manipulate
1522 * the same open file at a time.
1523 */
1524 fibril_mutex_lock(&oldfile->lock);
1525
[25bef0ff]1526 /* Make sure newfd is closed. */
1527 (void) vfs_fd_free(newfd);
[2b88074b]1528
1529 /* Assign the old file to newfd. */
1530 int ret = vfs_fd_assign(oldfile, newfd);
1531 fibril_mutex_unlock(&oldfile->lock);
[4fe94c66]1532 vfs_file_put(oldfile);
[2b88074b]1533
1534 if (ret != EOK)
[ffa2c8ef]1535 async_answer_0(rid, ret);
[2b88074b]1536 else
[ffa2c8ef]1537 async_answer_1(rid, EOK, newfd);
[2b88074b]1538}
1539
[27b76ca]1540void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
1541{
1542 int fd = vfs_wait_handle_internal();
1543 async_answer_1(rid, EOK, fd);
1544}
1545
[76a67ce]1546void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
1547{
1548 ipc_callid_t callid;
1549 ipc_call_t data;
1550 sysarg_t rc = EOK;
1551 size_t len;
1552
1553 fibril_mutex_lock(&mtab_list_lock);
1554
1555 /* Send to the caller the number of mounted filesystems */
1556 callid = async_get_call(&data);
1557 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
1558 rc = ENOTSUP;
[7e8403b]1559 async_answer_0(callid, rc);
[76a67ce]1560 goto exit;
1561 }
1562 async_answer_1(callid, EOK, mtab_size);
1563
1564 list_foreach(mtab_list, cur) {
[8d6a41c]1565 mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
[76a67ce]1566 link);
1567
1568 rc = ENOTSUP;
1569
[e6da6d5]1570 if (!async_data_read_receive(&callid, &len)) {
1571 async_answer_0(callid, rc);
[76a67ce]1572 goto exit;
[e6da6d5]1573 }
[76a67ce]1574
1575 (void) async_data_read_finalize(callid, mtab_ent->mp,
1576 str_size(mtab_ent->mp));
1577
[e6da6d5]1578 if (!async_data_read_receive(&callid, &len)) {
1579 async_answer_0(callid, rc);
[76a67ce]1580 goto exit;
[e6da6d5]1581 }
[76a67ce]1582
1583 (void) async_data_read_finalize(callid, mtab_ent->opts,
1584 str_size(mtab_ent->opts));
1585
[e6da6d5]1586 if (!async_data_read_receive(&callid, &len)) {
1587 async_answer_0(callid, rc);
[76a67ce]1588 goto exit;
[e6da6d5]1589 }
[76a67ce]1590
1591 (void) async_data_read_finalize(callid, mtab_ent->fs_name,
1592 str_size(mtab_ent->fs_name));
1593
[f8838b8]1594 callid = async_get_call(&data);
1595
1596 if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
[7e8403b]1597 async_answer_0(callid, rc);
[f8838b8]1598 goto exit;
[76a67ce]1599 }
1600
1601 rc = EOK;
[6b8e5b74]1602 async_answer_2(callid, rc, mtab_ent->instance,
1603 mtab_ent->service_id);
[76a67ce]1604 }
1605
1606exit:
1607 fibril_mutex_unlock(&mtab_list_lock);
1608 async_answer_0(rid, rc);
1609}
1610
[861e7d1]1611/**
1612 * @}
[05b9912]1613 */
Note: See TracBrowser for help on using the repository browser.