source: mainline/uspace/srv/vfs/vfs_ops.c@ 703d19c

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

Fix a regression introduced by changeset martin@….

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