source: mainline/uspace/srv/vfs/vfs_ops.c@ 27b76ca

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

Synchronize VFS handle acceptor with VFS.

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