source: mainline/uspace/srv/vfs/vfs_ops.c@ 60ab6c3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 60ab6c3 was ed903174, checked in by Martin Decky <martin@…>, 15 years ago

implement support for 64bit file offsets

  • the libc API is a small deviation from standard, but we have no reason to keep a strict backward compatibility with ancient code so far
    • the basic signed 64bit offset type is called off64_t
      • lseek() and fseek() take off64_t arguments (since the argument represents a relative offset)
      • ftell() returns off64_t values (since it is a wrapper of lseek())
      • vfs_seek() implementation supports negative offsets when SEEK_CUR and SEEK_END is used
    • aoff64_t is used for internal unsigned representation of sizes (in bytes, blocks, etc.) and absolute offsets
      • mmap() and ftruncate() take aoff64_t arguments (since the full range of the absolute file offset should be used here)
      • struct stat stores the file size as aoff64_t
    • in both cases the explicit range of the types shown in the names is helpful for proper filesystem and IPC interaction
    • note: size_t should be used only for representing in-memory sizes and offsets, not device and file-related information, and vice versa
      • the code base still needs a thorough revision with respect to this rule
    • PRIdOFF64 and PRIuOFF64 can be used for printing the offsets
  • VFS_OUT_LOOKUP returns the 64bit file size in two IPC arguments
    • since all 5 IPC arguments have already been taken, the fs_handle is returned as the return value (fs_handle has only 16 bits, thus the return value can be used for both indicating errors as negative values and returning positive handles)
  • VFS_OUT_READ and VFS_OUT_WRITE use aoff64_t absolute offsets split into two IPC arguments

replace bn_t with aoff64_t as a generic 64bit bytes/block counter type

note: filesystem drivers need to be revised with respect to make sure that all out-of-range checks are correct (especially w.r.t. file and block offsets)

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