source: mainline/uspace/srv/vfs/vfs_ops.c@ 4b995b92

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

Introduce the L_NOCROSS_LAST_MP lookup flag and treat the last mount point node
according to it in libfs_lookup().

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