source: mainline/uspace/srv/vfs/vfs_ops.c@ 7b47fa2

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

Do not use the "pending" fibril mechanism in VFS

  • Property mode set to 100644
File size: 30.8 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>
[230260ac]46#include <fibril_sync.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.
111 * It will be dropped upon the corresponding VFS_UNMOUNT.
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);
[594303b]124 msg = async_send_1(phone, VFS_MOUNTED,
125 (ipcarg_t) dev_handle, &answer);
126 /* send the mount options */
127 rc = ipc_data_write_start(phone, (void *)opts,
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);
[594303b]186 msg = async_send_4(phone, VFS_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 */
209 rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
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;
270 if (!ipc_data_write_receive(&callid, &size)) {
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. */
292 ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
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. */
301 if (!ipc_data_write_receive(&callid, &size)) {
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. */
309 if (size < 0 || size > MAX_MNTOPTS_LEN) {
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. */
326 retval = ipc_data_write_finalize(callid, opts, size);
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 */
339 if (!ipc_data_write_receive(&callid, &size)) {
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. */
372 retval = ipc_data_write_finalize(callid, fs_name, size);
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
[861e7d1]431void vfs_open(ipc_callid_t rid, ipc_call_t *request)
432{
433 if (!vfs_files_init()) {
434 ipc_answer_0(rid, ENOMEM);
435 return;
436 }
[05b9912]437
[861e7d1]438 /*
[ae78b530]439 * The POSIX interface is open(path, oflag, mode).
440 * We can receive oflags and mode along with the VFS_OPEN call; the path
[861e7d1]441 * will need to arrive in another call.
[ae78b530]442 *
443 * We also receive one private, non-POSIX set of flags called lflag
444 * used to pass information to vfs_lookup_internal().
[861e7d1]445 */
[ae78b530]446 int lflag = IPC_GET_ARG1(*request);
447 int oflag = IPC_GET_ARG2(*request);
448 int mode = IPC_GET_ARG3(*request);
[861e7d1]449 size_t len;
[05b9912]450
[b17186d]451 /*
452 * Make sure that we are called with exactly one of L_FILE and
[05b9912]453 * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
[b17186d]454 */
[230260ac]455 if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
456 ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
457 ((lflag & L_OPEN) != 0)) {
[b17186d]458 ipc_answer_0(rid, EINVAL);
459 return;
460 }
[05b9912]461
[2db4ac8]462 if (oflag & O_CREAT)
463 lflag |= L_CREATE;
464 if (oflag & O_EXCL)
465 lflag |= L_EXCLUSIVE;
[05b9912]466
[861e7d1]467 ipc_callid_t callid;
468 if (!ipc_data_write_receive(&callid, &len)) {
469 ipc_answer_0(callid, EINVAL);
470 ipc_answer_0(rid, EINVAL);
471 return;
472 }
[05b9912]473
[d6084ef]474 char *path = malloc(len + 1);
[861e7d1]475 if (!path) {
476 ipc_answer_0(callid, ENOMEM);
477 ipc_answer_0(rid, ENOMEM);
478 return;
479 }
[05b9912]480
[861e7d1]481 int rc;
482 if ((rc = ipc_data_write_finalize(callid, path, len))) {
483 ipc_answer_0(rid, rc);
484 free(path);
485 return;
486 }
[d6084ef]487 path[len] = '\0';
[861e7d1]488
489 /*
490 * Avoid the race condition in which the file can be deleted before we
491 * find/create-and-lock the VFS node corresponding to the looked-up
492 * triplet.
493 */
[2db4ac8]494 if (lflag & L_CREATE)
[230260ac]495 fibril_rwlock_write_lock(&namespace_rwlock);
[2db4ac8]496 else
[230260ac]497 fibril_rwlock_read_lock(&namespace_rwlock);
[05b9912]498
[72bde81]499 /* The path is now populated and we can call vfs_lookup_internal(). */
[eb27ce5a]500 vfs_lookup_res_t lr;
[05b9912]501 rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
502 if (rc != EOK) {
[2db4ac8]503 if (lflag & L_CREATE)
[230260ac]504 fibril_rwlock_write_unlock(&namespace_rwlock);
[2db4ac8]505 else
[230260ac]506 fibril_rwlock_read_unlock(&namespace_rwlock);
[861e7d1]507 ipc_answer_0(rid, rc);
508 free(path);
509 return;
510 }
[05b9912]511
[7fe1f75]512 /* Path is no longer needed. */
[861e7d1]513 free(path);
[05b9912]514
[eb27ce5a]515 vfs_node_t *node = vfs_node_get(&lr);
[2db4ac8]516 if (lflag & L_CREATE)
[230260ac]517 fibril_rwlock_write_unlock(&namespace_rwlock);
[2db4ac8]518 else
[230260ac]519 fibril_rwlock_read_unlock(&namespace_rwlock);
[05b9912]520
[7fe1f75]521 /* Truncate the file if requested and if necessary. */
522 if (oflag & O_TRUNC) {
[230260ac]523 fibril_rwlock_write_lock(&node->contents_rwlock);
[7fe1f75]524 if (node->size) {
525 rc = vfs_truncate_internal(node->fs_handle,
526 node->dev_handle, node->index, 0);
527 if (rc) {
[230260ac]528 fibril_rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]529 vfs_node_put(node);
530 ipc_answer_0(rid, rc);
531 return;
532 }
533 node->size = 0;
534 }
[230260ac]535 fibril_rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]536 }
[05b9912]537
[861e7d1]538 /*
539 * Get ourselves a file descriptor and the corresponding vfs_file_t
540 * structure.
541 */
542 int fd = vfs_fd_alloc();
543 if (fd < 0) {
544 vfs_node_put(node);
545 ipc_answer_0(rid, fd);
546 return;
547 }
548 vfs_file_t *file = vfs_file_get(fd);
549 file->node = node;
[05b9912]550 if (oflag & O_APPEND)
[15b9970]551 file->append = true;
[05b9912]552
[861e7d1]553 /*
554 * The following increase in reference count is for the fact that the
555 * file is being opened and that a file structure is pointing to it.
556 * It is necessary so that the file will not disappear when
557 * vfs_node_put() is called. The reference will be dropped by the
558 * respective VFS_CLOSE.
559 */
560 vfs_node_addref(node);
561 vfs_node_put(node);
[05b9912]562
563 /* Success! Return the new file descriptor to the client. */
564 ipc_answer_1(rid, EOK, fd);
565}
[861e7d1]566
[05b9912]567void vfs_open_node(ipc_callid_t rid, ipc_call_t *request)
568{
569 // FIXME: check for sanity of the supplied fs, dev and index
570
571 if (!vfs_files_init()) {
572 ipc_answer_0(rid, ENOMEM);
573 return;
574 }
575
576 /*
577 * The interface is open_node(fs, dev, index, oflag).
578 */
579 vfs_lookup_res_t lr;
580
581 lr.triplet.fs_handle = IPC_GET_ARG1(*request);
582 lr.triplet.dev_handle = IPC_GET_ARG2(*request);
583 lr.triplet.index = IPC_GET_ARG3(*request);
584 int oflag = IPC_GET_ARG4(*request);
585
[230260ac]586 fibril_rwlock_read_lock(&namespace_rwlock);
[05b9912]587
588 int rc = vfs_open_node_internal(&lr);
589 if (rc != EOK) {
[230260ac]590 fibril_rwlock_read_unlock(&namespace_rwlock);
[05b9912]591 ipc_answer_0(rid, rc);
592 return;
593 }
594
595 vfs_node_t *node = vfs_node_get(&lr);
[230260ac]596 fibril_rwlock_read_unlock(&namespace_rwlock);
[05b9912]597
598 /* Truncate the file if requested and if necessary. */
599 if (oflag & O_TRUNC) {
[230260ac]600 fibril_rwlock_write_lock(&node->contents_rwlock);
[05b9912]601 if (node->size) {
602 rc = vfs_truncate_internal(node->fs_handle,
603 node->dev_handle, node->index, 0);
604 if (rc) {
[230260ac]605 fibril_rwlock_write_unlock(&node->contents_rwlock);
[05b9912]606 vfs_node_put(node);
607 ipc_answer_0(rid, rc);
608 return;
609 }
610 node->size = 0;
611 }
[230260ac]612 fibril_rwlock_write_unlock(&node->contents_rwlock);
[05b9912]613 }
614
615 /*
616 * Get ourselves a file descriptor and the corresponding vfs_file_t
617 * structure.
618 */
619 int fd = vfs_fd_alloc();
620 if (fd < 0) {
621 vfs_node_put(node);
622 ipc_answer_0(rid, fd);
623 return;
624 }
625 vfs_file_t *file = vfs_file_get(fd);
626 file->node = node;
627 if (oflag & O_APPEND)
628 file->append = true;
629
630 /*
631 * The following increase in reference count is for the fact that the
632 * file is being opened and that a file structure is pointing to it.
633 * It is necessary so that the file will not disappear when
634 * vfs_node_put() is called. The reference will be dropped by the
635 * respective VFS_CLOSE.
636 */
637 vfs_node_addref(node);
638 vfs_node_put(node);
639
[72bde81]640 /* Success! Return the new file descriptor to the client. */
[861e7d1]641 ipc_answer_1(rid, EOK, fd);
642}
643
[05b9912]644void vfs_node(ipc_callid_t rid, ipc_call_t *request)
[e704503]645{
646 int fd = IPC_GET_ARG1(*request);
[05b9912]647
648 /* Lookup the file structure corresponding to the file descriptor. */
649 vfs_file_t *file = vfs_file_get(fd);
650 if (!file) {
651 ipc_answer_0(rid, ENOENT);
652 return;
653 }
654
[230260ac]655 ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle,
656 file->node->index);
[05b9912]657}
658
659void vfs_device(ipc_callid_t rid, ipc_call_t *request)
660{
661 int fd = IPC_GET_ARG1(*request);
662
663 /* Lookup the file structure corresponding to the file descriptor. */
664 vfs_file_t *file = vfs_file_get(fd);
665 if (!file) {
666 ipc_answer_0(rid, ENOENT);
667 return;
668 }
669
670 /*
671 * Lock the open file structure so that no other thread can manipulate
672 * the same open file at a time.
673 */
[230260ac]674 fibril_mutex_lock(&file->lock);
[05b9912]675 int fs_phone = vfs_grab_phone(file->node->fs_handle);
676
677 /* Make a VFS_DEVICE request at the destination FS server. */
678 aid_t msg;
679 ipc_call_t answer;
680 msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
681 file->node->dev_handle, file->node->index, &answer);
[230260ac]682
[05b9912]683 /* Wait for reply from the FS server. */
684 ipcarg_t rc;
685 async_wait_for(msg, &rc);
[34ca870]686
687 vfs_release_phone(fs_phone);
[230260ac]688 fibril_mutex_unlock(&file->lock);
[05b9912]689
690 ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer));
691}
692
693void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
694{
695 int fd = IPC_GET_ARG1(*request);
696
697 /* Lookup the file structure corresponding to the file descriptor. */
698 vfs_file_t *file = vfs_file_get(fd);
699 if (!file) {
700 ipc_answer_0(rid, ENOENT);
701 return;
702 }
703
704 /*
705 * Lock the open file structure so that no other thread can manipulate
706 * the same open file at a time.
707 */
[230260ac]708 fibril_mutex_lock(&file->lock);
[05b9912]709 int fs_phone = vfs_grab_phone(file->node->fs_handle);
710
711 /* Make a VFS_SYMC request at the destination FS server. */
712 aid_t msg;
713 ipc_call_t answer;
714 msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
715 file->node->dev_handle, file->node->index, &answer);
[230260ac]716
[05b9912]717 /* Wait for reply from the FS server. */
718 ipcarg_t rc;
719 async_wait_for(msg, &rc);
720
[34ca870]721 vfs_release_phone(fs_phone);
[230260ac]722 fibril_mutex_unlock(&file->lock);
[05b9912]723
[b7f9087]724 ipc_answer_0(rid, rc);
[e704503]725}
726
[05b9912]727void vfs_close(ipc_callid_t rid, ipc_call_t *request)
728{
729 int fd = IPC_GET_ARG1(*request);
730
731 /* Lookup the file structure corresponding to the file descriptor. */
732 vfs_file_t *file = vfs_file_get(fd);
733 if (!file) {
734 ipc_answer_0(rid, ENOENT);
735 return;
736 }
737
738 /*
739 * Lock the open file structure so that no other thread can manipulate
740 * the same open file at a time.
741 */
[230260ac]742 fibril_mutex_lock(&file->lock);
[05b9912]743 int fs_phone = vfs_grab_phone(file->node->fs_handle);
744
745 /* Make a VFS_CLOSE request at the destination FS server. */
746 aid_t msg;
747 ipc_call_t answer;
748 msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
749 file->node->dev_handle, file->node->index, &answer);
[230260ac]750
[05b9912]751 /* Wait for reply from the FS server. */
752 ipcarg_t rc;
753 async_wait_for(msg, &rc);
[34ca870]754
755 vfs_release_phone(fs_phone);
[230260ac]756 fibril_mutex_unlock(&file->lock);
[05b9912]757
758 int retval = IPC_GET_ARG1(answer);
759 if (retval != EOK)
760 ipc_answer_0(rid, retval);
761
762 retval = vfs_fd_free(fd);
763 ipc_answer_0(rid, retval);
764}
765
[861e7d1]766static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
767{
768
769 /*
770 * The following code strongly depends on the fact that the files data
771 * structure can be only accessed by a single fibril and all file
772 * operations are serialized (i.e. the reads and writes cannot
773 * interleave and a file cannot be closed while it is being read).
774 *
775 * Additional synchronization needs to be added once the table of
776 * open files supports parallel access!
777 */
778
779 int fd = IPC_GET_ARG1(*request);
[6c89f20]780
[72bde81]781 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]782 vfs_file_t *file = vfs_file_get(fd);
783 if (!file) {
784 ipc_answer_0(rid, ENOENT);
785 return;
786 }
[6c89f20]787
[861e7d1]788 /*
789 * Now we need to receive a call with client's
790 * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
791 */
792 ipc_callid_t callid;
793 int res;
794 if (read)
795 res = ipc_data_read_receive(&callid, NULL);
796 else
797 res = ipc_data_write_receive(&callid, NULL);
798 if (!res) {
799 ipc_answer_0(callid, EINVAL);
800 ipc_answer_0(rid, EINVAL);
801 return;
802 }
[6c89f20]803
[861e7d1]804 /*
805 * Lock the open file structure so that no other thread can manipulate
806 * the same open file at a time.
807 */
[230260ac]808 fibril_mutex_lock(&file->lock);
[b17186d]809
[861e7d1]810 /*
811 * Lock the file's node so that no other client can read/write to it at
812 * the same time.
813 */
814 if (read)
[230260ac]815 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[861e7d1]816 else
[230260ac]817 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[b17186d]818
819 if (file->node->type == VFS_NODE_DIRECTORY) {
820 /*
821 * Make sure that no one is modifying the namespace
822 * while we are in readdir().
823 */
824 assert(read);
[230260ac]825 fibril_rwlock_read_lock(&namespace_rwlock);
[b17186d]826 }
[6c89f20]827
[861e7d1]828 int fs_phone = vfs_grab_phone(file->node->fs_handle);
829
[72bde81]830 /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
[861e7d1]831 aid_t msg;
832 ipc_call_t answer;
[15b9970]833 if (!read && file->append)
834 file->pos = file->node->size;
[861e7d1]835 msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
836 file->node->dev_handle, file->node->index, file->pos, &answer);
837
838 /*
839 * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
840 * destination FS server. The call will be routed as if sent by
841 * ourselves. Note that call arguments are immutable in this case so we
842 * don't have to bother.
843 */
844 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
[230260ac]845
[72bde81]846 /* Wait for reply from the FS server. */
[861e7d1]847 ipcarg_t rc;
848 async_wait_for(msg, &rc);
[05b9912]849
[34ca870]850 vfs_release_phone(fs_phone);
851
[861e7d1]852 size_t bytes = IPC_GET_ARG1(answer);
[b17186d]853
854 if (file->node->type == VFS_NODE_DIRECTORY)
[230260ac]855 fibril_rwlock_read_unlock(&namespace_rwlock);
[6c89f20]856
[72bde81]857 /* Unlock the VFS node. */
[861e7d1]858 if (read)
[230260ac]859 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[861e7d1]860 else {
861 /* Update the cached version of node's size. */
[f7017572]862 if (rc == EOK)
863 file->node->size = IPC_GET_ARG2(answer);
[230260ac]864 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[861e7d1]865 }
[6c89f20]866
[72bde81]867 /* Update the position pointer and unlock the open file. */
[f7017572]868 if (rc == EOK)
869 file->pos += bytes;
[230260ac]870 fibril_mutex_unlock(&file->lock);
[6c89f20]871
[861e7d1]872 /*
873 * FS server's reply is the final result of the whole operation we
874 * return to the client.
875 */
876 ipc_answer_1(rid, rc, bytes);
877}
878
879void vfs_read(ipc_callid_t rid, ipc_call_t *request)
880{
881 vfs_rdwr(rid, request, true);
882}
883
884void vfs_write(ipc_callid_t rid, ipc_call_t *request)
885{
886 vfs_rdwr(rid, request, false);
887}
888
889void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
890{
891 int fd = (int) IPC_GET_ARG1(*request);
892 off_t off = (off_t) IPC_GET_ARG2(*request);
893 int whence = (int) IPC_GET_ARG3(*request);
894
895
[72bde81]896 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]897 vfs_file_t *file = vfs_file_get(fd);
898 if (!file) {
899 ipc_answer_0(rid, ENOENT);
900 return;
901 }
902
903 off_t newpos;
[230260ac]904 fibril_mutex_lock(&file->lock);
[861e7d1]905 if (whence == SEEK_SET) {
906 file->pos = off;
[230260ac]907 fibril_mutex_unlock(&file->lock);
[861e7d1]908 ipc_answer_1(rid, EOK, off);
909 return;
910 }
911 if (whence == SEEK_CUR) {
912 if (file->pos + off < file->pos) {
[230260ac]913 fibril_mutex_unlock(&file->lock);
[861e7d1]914 ipc_answer_0(rid, EOVERFLOW);
915 return;
916 }
917 file->pos += off;
918 newpos = file->pos;
[230260ac]919 fibril_mutex_unlock(&file->lock);
[861e7d1]920 ipc_answer_1(rid, EOK, newpos);
921 return;
922 }
923 if (whence == SEEK_END) {
[230260ac]924 fibril_rwlock_read_lock(&file->node->contents_rwlock);
[861e7d1]925 size_t size = file->node->size;
[230260ac]926 fibril_rwlock_read_unlock(&file->node->contents_rwlock);
[861e7d1]927 if (size + off < size) {
[230260ac]928 fibril_mutex_unlock(&file->lock);
[861e7d1]929 ipc_answer_0(rid, EOVERFLOW);
930 return;
931 }
932 newpos = size + off;
[230260ac]933 fibril_mutex_unlock(&file->lock);
[861e7d1]934 ipc_answer_1(rid, EOK, newpos);
935 return;
936 }
[230260ac]937 fibril_mutex_unlock(&file->lock);
[861e7d1]938 ipc_answer_0(rid, EINVAL);
939}
940
[f2ec8c8]941int
942vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
943 fs_index_t index, size_t size)
[7fe1f75]944{
945 ipcarg_t rc;
946 int fs_phone;
947
948 fs_phone = vfs_grab_phone(fs_handle);
949 rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
950 (ipcarg_t)index, (ipcarg_t)size);
951 vfs_release_phone(fs_phone);
952 return (int)rc;
953}
954
[0ee4322]955void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
956{
957 int fd = IPC_GET_ARG1(*request);
958 size_t size = IPC_GET_ARG2(*request);
[7fe1f75]959 int rc;
[0ee4322]960
961 vfs_file_t *file = vfs_file_get(fd);
962 if (!file) {
963 ipc_answer_0(rid, ENOENT);
964 return;
965 }
[230260ac]966 fibril_mutex_lock(&file->lock);
[0ee4322]967
[230260ac]968 fibril_rwlock_write_lock(&file->node->contents_rwlock);
[7fe1f75]969 rc = vfs_truncate_internal(file->node->fs_handle,
970 file->node->dev_handle, file->node->index, size);
[0ee4322]971 if (rc == EOK)
972 file->node->size = size;
[230260ac]973 fibril_rwlock_write_unlock(&file->node->contents_rwlock);
[0ee4322]974
[230260ac]975 fibril_mutex_unlock(&file->lock);
[7fe1f75]976 ipc_answer_0(rid, (ipcarg_t)rc);
[861e7d1]977}
978
[72bde81]979void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
980{
981 int mode = IPC_GET_ARG1(*request);
982
[f15cf1a6]983 size_t len;
[72bde81]984 ipc_callid_t callid;
985
986 if (!ipc_data_write_receive(&callid, &len)) {
987 ipc_answer_0(callid, EINVAL);
988 ipc_answer_0(rid, EINVAL);
989 return;
990 }
[d6084ef]991 char *path = malloc(len + 1);
[72bde81]992 if (!path) {
993 ipc_answer_0(callid, ENOMEM);
994 ipc_answer_0(rid, ENOMEM);
995 return;
996 }
997 int rc;
998 if ((rc = ipc_data_write_finalize(callid, path, len))) {
999 ipc_answer_0(rid, rc);
1000 free(path);
1001 return;
1002 }
[d6084ef]1003 path[len] = '\0';
[72bde81]1004
[230260ac]1005 fibril_rwlock_write_lock(&namespace_rwlock);
[72bde81]1006 int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
[d6084ef]1007 rc = vfs_lookup_internal(path, lflag, NULL, NULL);
[230260ac]1008 fibril_rwlock_write_unlock(&namespace_rwlock);
[72bde81]1009 free(path);
1010 ipc_answer_0(rid, rc);
1011}
1012
[f15cf1a6]1013void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
1014{
1015 int lflag = IPC_GET_ARG1(*request);
1016
1017 size_t len;
1018 ipc_callid_t callid;
1019
1020 if (!ipc_data_write_receive(&callid, &len)) {
1021 ipc_answer_0(callid, EINVAL);
1022 ipc_answer_0(rid, EINVAL);
1023 return;
1024 }
[d6084ef]1025 char *path = malloc(len + 1);
[f15cf1a6]1026 if (!path) {
1027 ipc_answer_0(callid, ENOMEM);
1028 ipc_answer_0(rid, ENOMEM);
1029 return;
1030 }
1031 int rc;
1032 if ((rc = ipc_data_write_finalize(callid, path, len))) {
1033 ipc_answer_0(rid, rc);
1034 free(path);
1035 return;
1036 }
[d6084ef]1037 path[len] = '\0';
[f15cf1a6]1038
[230260ac]1039 fibril_rwlock_write_lock(&namespace_rwlock);
[f15cf1a6]1040 lflag &= L_DIRECTORY; /* sanitize lflag */
1041 vfs_lookup_res_t lr;
[a8e9ab8d]1042 rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
[f15cf1a6]1043 free(path);
1044 if (rc != EOK) {
[230260ac]1045 fibril_rwlock_write_unlock(&namespace_rwlock);
[f15cf1a6]1046 ipc_answer_0(rid, rc);
1047 return;
1048 }
1049
1050 /*
1051 * The name has already been unlinked by vfs_lookup_internal().
1052 * We have to get and put the VFS node to ensure that it is
[fdb7795]1053 * VFS_DESTROY'ed after the last reference to it is dropped.
[f15cf1a6]1054 */
1055 vfs_node_t *node = vfs_node_get(&lr);
[553492be]1056 fibril_mutex_lock(&nodes_mutex);
[f15cf1a6]1057 node->lnkcnt--;
[553492be]1058 fibril_mutex_unlock(&nodes_mutex);
[230260ac]1059 fibril_rwlock_write_unlock(&namespace_rwlock);
[f15cf1a6]1060 vfs_node_put(node);
1061 ipc_answer_0(rid, EOK);
1062}
1063
[a8e9ab8d]1064void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
1065{
[732bb0c]1066 size_t olen, nlen;
[a8e9ab8d]1067 ipc_callid_t callid;
1068 int rc;
1069
1070 /* Retrieve the old path. */
[732bb0c]1071 if (!ipc_data_write_receive(&callid, &olen)) {
[a8e9ab8d]1072 ipc_answer_0(callid, EINVAL);
1073 ipc_answer_0(rid, EINVAL);
1074 return;
1075 }
[732bb0c]1076 char *old = malloc(olen + 1);
[a8e9ab8d]1077 if (!old) {
1078 ipc_answer_0(callid, ENOMEM);
1079 ipc_answer_0(rid, ENOMEM);
1080 return;
1081 }
[732bb0c]1082 if ((rc = ipc_data_write_finalize(callid, old, olen))) {
[a8e9ab8d]1083 ipc_answer_0(rid, rc);
1084 free(old);
1085 return;
1086 }
[732bb0c]1087 old[olen] = '\0';
[a8e9ab8d]1088
1089 /* Retrieve the new path. */
[732bb0c]1090 if (!ipc_data_write_receive(&callid, &nlen)) {
[a8e9ab8d]1091 ipc_answer_0(callid, EINVAL);
1092 ipc_answer_0(rid, EINVAL);
1093 free(old);
1094 return;
1095 }
[732bb0c]1096 char *new = malloc(nlen + 1);
[a8e9ab8d]1097 if (!new) {
1098 ipc_answer_0(callid, ENOMEM);
1099 ipc_answer_0(rid, ENOMEM);
1100 free(old);
1101 return;
1102 }
[732bb0c]1103 if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
[a8e9ab8d]1104 ipc_answer_0(rid, rc);
1105 free(old);
1106 free(new);
1107 return;
1108 }
[732bb0c]1109 new[nlen] = '\0';
[a8e9ab8d]1110
[732bb0c]1111 char *oldc = canonify(old, &olen);
1112 char *newc = canonify(new, &nlen);
[a8e9ab8d]1113 if (!oldc || !newc) {
1114 ipc_answer_0(rid, EINVAL);
1115 free(old);
1116 free(new);
1117 return;
1118 }
[732bb0c]1119 oldc[olen] = '\0';
1120 newc[nlen] = '\0';
[14040e5]1121 if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
1122 ((newc[str_length(oldc)] == '/') ||
1123 (str_length(oldc) == 1) ||
1124 (str_length(oldc) == str_length(newc)))) {
1125 /*
1126 * oldc is a prefix of newc and either
1127 * - newc continues with a / where oldc ends, or
1128 * - oldc was / itself, or
1129 * - oldc and newc are equal.
1130 */
[a8e9ab8d]1131 ipc_answer_0(rid, EINVAL);
1132 free(old);
1133 free(new);
1134 return;
1135 }
1136
1137 vfs_lookup_res_t old_lr;
1138 vfs_lookup_res_t new_lr;
1139 vfs_lookup_res_t new_par_lr;
[230260ac]1140 fibril_rwlock_write_lock(&namespace_rwlock);
[a8e9ab8d]1141 /* Lookup the node belonging to the old file name. */
1142 rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
1143 if (rc != EOK) {
[230260ac]1144 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1145 ipc_answer_0(rid, rc);
1146 free(old);
1147 free(new);
1148 return;
1149 }
1150 vfs_node_t *old_node = vfs_node_get(&old_lr);
1151 if (!old_node) {
[230260ac]1152 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1153 ipc_answer_0(rid, ENOMEM);
1154 free(old);
1155 free(new);
1156 return;
1157 }
[4f46695e]1158 /* Determine the path to the parent of the node with the new name. */
1159 char *parentc = str_dup(newc);
1160 if (!parentc) {
[230260ac]1161 fibril_rwlock_write_unlock(&namespace_rwlock);
[4f46695e]1162 ipc_answer_0(rid, rc);
1163 free(old);
1164 free(new);
1165 return;
1166 }
[ae55ee8]1167 char *lastsl = str_rchr(parentc + 1, '/');
[4f46695e]1168 if (lastsl)
1169 *lastsl = '\0';
1170 else
1171 parentc[1] = '\0';
[a8e9ab8d]1172 /* Lookup parent of the new file name. */
[4f46695e]1173 rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
1174 free(parentc); /* not needed anymore */
[a8e9ab8d]1175 if (rc != EOK) {
[230260ac]1176 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1177 ipc_answer_0(rid, rc);
1178 free(old);
1179 free(new);
1180 return;
1181 }
1182 /* Check whether linking to the same file system instance. */
1183 if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
1184 (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
[230260ac]1185 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1186 ipc_answer_0(rid, EXDEV); /* different file systems */
1187 free(old);
1188 free(new);
1189 return;
1190 }
1191 /* Destroy the old link for the new name. */
1192 vfs_node_t *new_node = NULL;
1193 rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
1194 switch (rc) {
1195 case ENOENT:
1196 /* simply not in our way */
1197 break;
1198 case EOK:
1199 new_node = vfs_node_get(&new_lr);
1200 if (!new_node) {
[230260ac]1201 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1202 ipc_answer_0(rid, ENOMEM);
1203 free(old);
1204 free(new);
1205 return;
1206 }
[553492be]1207 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1208 new_node->lnkcnt--;
[553492be]1209 fibril_mutex_unlock(&nodes_mutex);
[a8e9ab8d]1210 break;
1211 default:
[230260ac]1212 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1213 ipc_answer_0(rid, ENOTEMPTY);
1214 free(old);
1215 free(new);
1216 return;
1217 }
1218 /* Create the new link for the new name. */
1219 rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
1220 if (rc != EOK) {
[230260ac]1221 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1222 if (new_node)
1223 vfs_node_put(new_node);
1224 ipc_answer_0(rid, rc);
1225 free(old);
1226 free(new);
1227 return;
1228 }
[553492be]1229 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1230 old_node->lnkcnt++;
[553492be]1231 fibril_mutex_unlock(&nodes_mutex);
[a8e9ab8d]1232 /* Destroy the link for the old name. */
1233 rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
1234 if (rc != EOK) {
[230260ac]1235 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1236 vfs_node_put(old_node);
1237 if (new_node)
1238 vfs_node_put(new_node);
1239 ipc_answer_0(rid, rc);
1240 free(old);
1241 free(new);
1242 return;
1243 }
[553492be]1244 fibril_mutex_lock(&nodes_mutex);
[a8e9ab8d]1245 old_node->lnkcnt--;
[553492be]1246 fibril_mutex_unlock(&nodes_mutex);
[230260ac]1247 fibril_rwlock_write_unlock(&namespace_rwlock);
[a8e9ab8d]1248 vfs_node_put(old_node);
1249 if (new_node)
1250 vfs_node_put(new_node);
1251 free(old);
1252 free(new);
1253 ipc_answer_0(rid, EOK);
1254}
1255
[861e7d1]1256/**
1257 * @}
[05b9912]1258 */
Note: See TracBrowser for help on using the repository browser.