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

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

flag, instance, and fs_handle fields can be sent together in a single IPC message

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