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

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

Allow more instances of the same FS to be used.
(Thanks to Maurizio Lombardi.)

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