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

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

add minimal data size check into async_data_receive
introduce more convenience wrappers for common IPC patterns:

async_data_read_forward_fast
async_data_void
async_data_forward_fast

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