source: mainline/uspace/srv/vfs/vfs_ops.c@ 553492be

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

Finish converting VFS to fibril synchronization.

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