source: mainline/uspace/srv/vfs/vfs_ops.c@ 8d6a41c

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

Remove the mtab_list_ent structure.

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