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

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

Handle each VFS request using a dedicated connection.

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