source: mainline/uspace/srv/vfs/vfs_ops.c@ 2e4bd1f

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

Fix the VFS protocol so that the client can determine that VFS_MOUNT failed due
to a request to mount an unregistered file system.

  • Property mode set to 100644
File size: 20.5 KB
RevLine 
[861e7d1]1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33/**
34 * @file vfs_ops.c
35 * @brief Operations that VFS offers to its clients.
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>
47#include <rwlock.h>
48#include <libadt/list.h>
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
[861e7d1]58/**
59 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
60 * concurrent VFS operation which modifies the file system namespace.
61 */
62RWLOCK_INITIALIZE(namespace_rwlock);
63
[c31d773]64futex_t rootfs_futex = FUTEX_INITIALIZER;
[f49b0ea]65vfs_pair_t rootfs = {
[861e7d1]66 .fs_handle = 0,
[f49b0ea]67 .dev_handle = 0
[861e7d1]68};
69
70void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
71{
[f2ec8c8]72 dev_handle_t dev_handle;
[861e7d1]73 vfs_node_t *mp_node = NULL;
[12fc042]74 ipc_callid_t callid;
75 ipc_call_t data;
[ce7311fc]76 int rc;
[64b67c3]77 int phone;
[12fc042]78 size_t size;
[861e7d1]79
80 /*
81 * We expect the library to do the device-name to device-handle
82 * translation for us, thus the device handle will arrive as ARG1
83 * in the request.
84 */
[f2ec8c8]85 dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
[861e7d1]86
87 /*
88 * For now, don't make use of ARG2 and ARG3, but they can be used to
89 * carry mount options in the future.
90 */
91
92 /*
93 * Now, we expect the client to send us data with the name of the file
94 * system.
95 */
96 if (!ipc_data_write_receive(&callid, &size)) {
97 ipc_answer_0(callid, EINVAL);
98 ipc_answer_0(rid, EINVAL);
99 return;
100 }
101
102 /*
103 * Don't receive more than is necessary for storing a full file system
104 * name.
105 */
106 if (size < 1 || size > FS_NAME_MAXLEN) {
107 ipc_answer_0(callid, EINVAL);
108 ipc_answer_0(rid, EINVAL);
109 return;
110 }
111
[72bde81]112 /* Deliver the file system name. */
[861e7d1]113 char fs_name[FS_NAME_MAXLEN + 1];
114 (void) ipc_data_write_finalize(callid, fs_name, size);
115 fs_name[size] = '\0';
116
[12fc042]117 /*
118 * Wait for IPC_M_PING so that we can return an error if we don't know
119 * fs_name.
120 */
121 callid = async_get_call(&data);
122 if (IPC_GET_METHOD(data) != IPC_M_PING) {
123 ipc_answer_0(callid, ENOTSUP);
124 ipc_answer_0(rid, ENOTSUP);
125 return;
126 }
127
[861e7d1]128 /*
129 * Check if we know a file system with the same name as is in fs_name.
130 * This will also give us its file system handle.
131 */
[f2ec8c8]132 fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
[861e7d1]133 if (!fs_handle) {
[12fc042]134 ipc_answer_0(callid, ENOENT);
[861e7d1]135 ipc_answer_0(rid, ENOENT);
136 return;
137 }
138
[12fc042]139 /* Acknowledge that we know fs_name. */
140 ipc_answer_0(callid, EOK);
141
[72bde81]142 /* Now, we want the client to send us the mount point. */
[861e7d1]143 if (!ipc_data_write_receive(&callid, &size)) {
144 ipc_answer_0(callid, EINVAL);
145 ipc_answer_0(rid, EINVAL);
146 return;
147 }
148
[72bde81]149 /* Check whether size is reasonable wrt. the mount point. */
[861e7d1]150 if (size < 1 || size > MAX_PATH_LEN) {
151 ipc_answer_0(callid, EINVAL);
152 ipc_answer_0(rid, EINVAL);
153 return;
154 }
[72bde81]155 /* Allocate buffer for the mount point data being received. */
[861e7d1]156 uint8_t *buf;
[d6084ef]157 buf = malloc(size + 1);
[861e7d1]158 if (!buf) {
159 ipc_answer_0(callid, ENOMEM);
160 ipc_answer_0(rid, ENOMEM);
161 return;
162 }
163
[72bde81]164 /* Deliver the mount point. */
[861e7d1]165 (void) ipc_data_write_finalize(callid, buf, size);
[d6084ef]166 buf[size] = '\0';
[861e7d1]167
[f49b0ea]168 /* Resolve the path to the mountpoint. */
[eb27ce5a]169 vfs_lookup_res_t mp_res;
[861e7d1]170 futex_down(&rootfs_futex);
171 if (rootfs.fs_handle) {
[72bde81]172 /* We already have the root FS. */
[861e7d1]173 rwlock_write_lock(&namespace_rwlock);
[2f60a529]174 if ((size == 1) && (buf[0] == '/')) {
175 /* Trying to mount root FS over root FS */
176 rwlock_write_unlock(&namespace_rwlock);
177 futex_up(&rootfs_futex);
178 free(buf);
179 ipc_answer_0(rid, EBUSY);
180 return;
181 }
[d6084ef]182 rc = vfs_lookup_internal(buf, L_DIRECTORY, &mp_res, NULL);
[861e7d1]183 if (rc != EOK) {
[72bde81]184 /* The lookup failed for some reason. */
[861e7d1]185 rwlock_write_unlock(&namespace_rwlock);
186 futex_up(&rootfs_futex);
187 free(buf);
188 ipc_answer_0(rid, rc);
189 return;
190 }
[eb27ce5a]191 mp_node = vfs_node_get(&mp_res);
[861e7d1]192 if (!mp_node) {
193 rwlock_write_unlock(&namespace_rwlock);
194 futex_up(&rootfs_futex);
195 free(buf);
196 ipc_answer_0(rid, ENOMEM);
197 return;
198 }
199 /*
200 * Now we hold a reference to mp_node.
201 * It will be dropped upon the corresponding VFS_UNMOUNT.
202 * This prevents the mount point from being deleted.
203 */
204 rwlock_write_unlock(&namespace_rwlock);
205 } else {
[72bde81]206 /* We still don't have the root file system mounted. */
[861e7d1]207 if ((size == 1) && (buf[0] == '/')) {
[64b67c3]208 /*
209 * For this simple, but important case,
210 * we are almost done.
211 */
[861e7d1]212 free(buf);
[64b67c3]213
[f49b0ea]214 /* Tell the mountee that it is being mounted. */
215 phone = vfs_grab_phone(fs_handle);
216 rc = async_req_1_0(phone, VFS_MOUNTED,
217 (ipcarg_t) dev_handle);
[64b67c3]218 vfs_release_phone(phone);
219
[f49b0ea]220 if (rc == EOK) {
221 rootfs.fs_handle = fs_handle;
222 rootfs.dev_handle = dev_handle;
223 }
[64b67c3]224
225 futex_up(&rootfs_futex);
226 ipc_answer_0(rid, rc);
[861e7d1]227 return;
228 } else {
229 /*
230 * We can't resolve this without the root filesystem
231 * being mounted first.
232 */
233 futex_up(&rootfs_futex);
234 free(buf);
235 ipc_answer_0(rid, ENOENT);
236 return;
237 }
238 }
239 futex_up(&rootfs_futex);
240
241 free(buf); /* The buffer is not needed anymore. */
242
243 /*
244 * At this point, we have all necessary pieces: file system and device
[f49b0ea]245 * handles, and we know the mount point VFS node.
[861e7d1]246 */
247
[64b67c3]248 phone = vfs_grab_phone(mp_res.triplet.fs_handle);
[f49b0ea]249 rc = async_req_4_0(phone, VFS_MOUNT,
[eb27ce5a]250 (ipcarg_t) mp_res.triplet.dev_handle,
[ce7311fc]251 (ipcarg_t) mp_res.triplet.index,
[f49b0ea]252 (ipcarg_t) fs_handle,
253 (ipcarg_t) dev_handle);
[861e7d1]254 vfs_release_phone(phone);
255
[ce7311fc]256 if (rc != EOK) {
[f49b0ea]257 /* Mount failed, drop reference to mp_node. */
[861e7d1]258 if (mp_node)
259 vfs_node_put(mp_node);
260 }
261
[ce7311fc]262 ipc_answer_0(rid, rc);
[861e7d1]263}
264
265void vfs_open(ipc_callid_t rid, ipc_call_t *request)
266{
267 if (!vfs_files_init()) {
268 ipc_answer_0(rid, ENOMEM);
269 return;
270 }
271
272 /*
[ae78b530]273 * The POSIX interface is open(path, oflag, mode).
274 * We can receive oflags and mode along with the VFS_OPEN call; the path
[861e7d1]275 * will need to arrive in another call.
[ae78b530]276 *
277 * We also receive one private, non-POSIX set of flags called lflag
278 * used to pass information to vfs_lookup_internal().
[861e7d1]279 */
[ae78b530]280 int lflag = IPC_GET_ARG1(*request);
281 int oflag = IPC_GET_ARG2(*request);
282 int mode = IPC_GET_ARG3(*request);
[861e7d1]283 size_t len;
284
[2db4ac8]285 if (oflag & O_CREAT)
286 lflag |= L_CREATE;
287 if (oflag & O_EXCL)
288 lflag |= L_EXCLUSIVE;
289
[861e7d1]290 ipc_callid_t callid;
291
292 if (!ipc_data_write_receive(&callid, &len)) {
293 ipc_answer_0(callid, EINVAL);
294 ipc_answer_0(rid, EINVAL);
295 return;
296 }
[d6084ef]297 char *path = malloc(len + 1);
[861e7d1]298 if (!path) {
299 ipc_answer_0(callid, ENOMEM);
300 ipc_answer_0(rid, ENOMEM);
301 return;
302 }
303 int rc;
304 if ((rc = ipc_data_write_finalize(callid, path, len))) {
305 ipc_answer_0(rid, rc);
306 free(path);
307 return;
308 }
[d6084ef]309 path[len] = '\0';
[861e7d1]310
311 /*
312 * Avoid the race condition in which the file can be deleted before we
313 * find/create-and-lock the VFS node corresponding to the looked-up
314 * triplet.
315 */
[2db4ac8]316 if (lflag & L_CREATE)
317 rwlock_write_lock(&namespace_rwlock);
318 else
319 rwlock_read_lock(&namespace_rwlock);
[861e7d1]320
[72bde81]321 /* The path is now populated and we can call vfs_lookup_internal(). */
[eb27ce5a]322 vfs_lookup_res_t lr;
[d6084ef]323 rc = vfs_lookup_internal(path, lflag, &lr, NULL);
[861e7d1]324 if (rc) {
[2db4ac8]325 if (lflag & L_CREATE)
326 rwlock_write_unlock(&namespace_rwlock);
327 else
328 rwlock_read_unlock(&namespace_rwlock);
[861e7d1]329 ipc_answer_0(rid, rc);
330 free(path);
331 return;
332 }
333
[7fe1f75]334 /* Path is no longer needed. */
[861e7d1]335 free(path);
336
[eb27ce5a]337 vfs_node_t *node = vfs_node_get(&lr);
[2db4ac8]338 if (lflag & L_CREATE)
339 rwlock_write_unlock(&namespace_rwlock);
340 else
341 rwlock_read_unlock(&namespace_rwlock);
[861e7d1]342
[7fe1f75]343 /* Truncate the file if requested and if necessary. */
344 if (oflag & O_TRUNC) {
[ee68e4bc]345 rwlock_write_lock(&node->contents_rwlock);
[7fe1f75]346 if (node->size) {
347 rc = vfs_truncate_internal(node->fs_handle,
348 node->dev_handle, node->index, 0);
349 if (rc) {
[ee68e4bc]350 rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]351 vfs_node_put(node);
352 ipc_answer_0(rid, rc);
353 return;
354 }
355 node->size = 0;
356 }
[ee68e4bc]357 rwlock_write_unlock(&node->contents_rwlock);
[7fe1f75]358 }
359
[861e7d1]360 /*
361 * Get ourselves a file descriptor and the corresponding vfs_file_t
362 * structure.
363 */
364 int fd = vfs_fd_alloc();
365 if (fd < 0) {
366 vfs_node_put(node);
367 ipc_answer_0(rid, fd);
368 return;
369 }
370 vfs_file_t *file = vfs_file_get(fd);
371 file->node = node;
[f7017572]372 if (oflag & O_APPEND)
[15b9970]373 file->append = true;
[861e7d1]374
375 /*
376 * The following increase in reference count is for the fact that the
377 * file is being opened and that a file structure is pointing to it.
378 * It is necessary so that the file will not disappear when
379 * vfs_node_put() is called. The reference will be dropped by the
380 * respective VFS_CLOSE.
381 */
382 vfs_node_addref(node);
383 vfs_node_put(node);
384
[72bde81]385 /* Success! Return the new file descriptor to the client. */
[861e7d1]386 ipc_answer_1(rid, EOK, fd);
387}
388
[e704503]389void vfs_close(ipc_callid_t rid, ipc_call_t *request)
390{
391 int fd = IPC_GET_ARG1(*request);
[b7f9087]392 int rc = vfs_fd_free(fd);
393 ipc_answer_0(rid, rc);
[e704503]394}
395
[861e7d1]396static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
397{
398
399 /*
400 * The following code strongly depends on the fact that the files data
401 * structure can be only accessed by a single fibril and all file
402 * operations are serialized (i.e. the reads and writes cannot
403 * interleave and a file cannot be closed while it is being read).
404 *
405 * Additional synchronization needs to be added once the table of
406 * open files supports parallel access!
407 */
408
409 int fd = IPC_GET_ARG1(*request);
[6c89f20]410
[72bde81]411 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]412 vfs_file_t *file = vfs_file_get(fd);
413 if (!file) {
414 ipc_answer_0(rid, ENOENT);
415 return;
416 }
[6c89f20]417
[861e7d1]418 /*
419 * Now we need to receive a call with client's
420 * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
421 */
422 ipc_callid_t callid;
423 int res;
424 if (read)
425 res = ipc_data_read_receive(&callid, NULL);
426 else
427 res = ipc_data_write_receive(&callid, NULL);
428 if (!res) {
429 ipc_answer_0(callid, EINVAL);
430 ipc_answer_0(rid, EINVAL);
431 return;
432 }
[6c89f20]433
[861e7d1]434 /*
435 * Lock the open file structure so that no other thread can manipulate
436 * the same open file at a time.
437 */
438 futex_down(&file->lock);
[6c89f20]439
[861e7d1]440 /*
441 * Lock the file's node so that no other client can read/write to it at
442 * the same time.
443 */
444 if (read)
445 rwlock_read_lock(&file->node->contents_rwlock);
446 else
447 rwlock_write_lock(&file->node->contents_rwlock);
[6c89f20]448
[861e7d1]449 int fs_phone = vfs_grab_phone(file->node->fs_handle);
450
[72bde81]451 /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
[861e7d1]452 aid_t msg;
453 ipc_call_t answer;
[15b9970]454 if (!read && file->append)
455 file->pos = file->node->size;
[861e7d1]456 msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
457 file->node->dev_handle, file->node->index, file->pos, &answer);
458
459 /*
460 * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
461 * destination FS server. The call will be routed as if sent by
462 * ourselves. Note that call arguments are immutable in this case so we
463 * don't have to bother.
464 */
465 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
[6c89f20]466
[861e7d1]467 vfs_release_phone(fs_phone);
[6c89f20]468
[72bde81]469 /* Wait for reply from the FS server. */
[861e7d1]470 ipcarg_t rc;
471 async_wait_for(msg, &rc);
472 size_t bytes = IPC_GET_ARG1(answer);
[6c89f20]473
[72bde81]474 /* Unlock the VFS node. */
[861e7d1]475 if (read)
476 rwlock_read_unlock(&file->node->contents_rwlock);
477 else {
478 /* Update the cached version of node's size. */
[f7017572]479 if (rc == EOK)
480 file->node->size = IPC_GET_ARG2(answer);
[861e7d1]481 rwlock_write_unlock(&file->node->contents_rwlock);
482 }
[6c89f20]483
[72bde81]484 /* Update the position pointer and unlock the open file. */
[f7017572]485 if (rc == EOK)
486 file->pos += bytes;
[861e7d1]487 futex_up(&file->lock);
[6c89f20]488
[861e7d1]489 /*
490 * FS server's reply is the final result of the whole operation we
491 * return to the client.
492 */
493 ipc_answer_1(rid, rc, bytes);
494}
495
496void vfs_read(ipc_callid_t rid, ipc_call_t *request)
497{
498 vfs_rdwr(rid, request, true);
499}
500
501void vfs_write(ipc_callid_t rid, ipc_call_t *request)
502{
503 vfs_rdwr(rid, request, false);
504}
505
506void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
507{
508 int fd = (int) IPC_GET_ARG1(*request);
509 off_t off = (off_t) IPC_GET_ARG2(*request);
510 int whence = (int) IPC_GET_ARG3(*request);
511
512
[72bde81]513 /* Lookup the file structure corresponding to the file descriptor. */
[861e7d1]514 vfs_file_t *file = vfs_file_get(fd);
515 if (!file) {
516 ipc_answer_0(rid, ENOENT);
517 return;
518 }
519
520 off_t newpos;
521 futex_down(&file->lock);
522 if (whence == SEEK_SET) {
523 file->pos = off;
524 futex_up(&file->lock);
525 ipc_answer_1(rid, EOK, off);
526 return;
527 }
528 if (whence == SEEK_CUR) {
529 if (file->pos + off < file->pos) {
530 futex_up(&file->lock);
531 ipc_answer_0(rid, EOVERFLOW);
532 return;
533 }
534 file->pos += off;
535 newpos = file->pos;
536 futex_up(&file->lock);
537 ipc_answer_1(rid, EOK, newpos);
538 return;
539 }
540 if (whence == SEEK_END) {
541 rwlock_read_lock(&file->node->contents_rwlock);
542 size_t size = file->node->size;
543 rwlock_read_unlock(&file->node->contents_rwlock);
544 if (size + off < size) {
545 futex_up(&file->lock);
546 ipc_answer_0(rid, EOVERFLOW);
547 return;
548 }
549 newpos = size + off;
550 futex_up(&file->lock);
551 ipc_answer_1(rid, EOK, newpos);
552 return;
553 }
554 futex_up(&file->lock);
555 ipc_answer_0(rid, EINVAL);
556}
557
[f2ec8c8]558int
559vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
560 fs_index_t index, size_t size)
[7fe1f75]561{
562 ipcarg_t rc;
563 int fs_phone;
564
565 fs_phone = vfs_grab_phone(fs_handle);
566 rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
567 (ipcarg_t)index, (ipcarg_t)size);
568 vfs_release_phone(fs_phone);
569 return (int)rc;
570}
571
[0ee4322]572void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
573{
574 int fd = IPC_GET_ARG1(*request);
575 size_t size = IPC_GET_ARG2(*request);
[7fe1f75]576 int rc;
[0ee4322]577
578 vfs_file_t *file = vfs_file_get(fd);
579 if (!file) {
580 ipc_answer_0(rid, ENOENT);
581 return;
582 }
583 futex_down(&file->lock);
584
585 rwlock_write_lock(&file->node->contents_rwlock);
[7fe1f75]586 rc = vfs_truncate_internal(file->node->fs_handle,
587 file->node->dev_handle, file->node->index, size);
[0ee4322]588 if (rc == EOK)
589 file->node->size = size;
590 rwlock_write_unlock(&file->node->contents_rwlock);
591
592 futex_up(&file->lock);
[7fe1f75]593 ipc_answer_0(rid, (ipcarg_t)rc);
[861e7d1]594}
595
[72bde81]596void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
597{
598 int mode = IPC_GET_ARG1(*request);
599
[f15cf1a6]600 size_t len;
[72bde81]601 ipc_callid_t callid;
602
603 if (!ipc_data_write_receive(&callid, &len)) {
604 ipc_answer_0(callid, EINVAL);
605 ipc_answer_0(rid, EINVAL);
606 return;
607 }
[d6084ef]608 char *path = malloc(len + 1);
[72bde81]609 if (!path) {
610 ipc_answer_0(callid, ENOMEM);
611 ipc_answer_0(rid, ENOMEM);
612 return;
613 }
614 int rc;
615 if ((rc = ipc_data_write_finalize(callid, path, len))) {
616 ipc_answer_0(rid, rc);
617 free(path);
618 return;
619 }
[d6084ef]620 path[len] = '\0';
[72bde81]621
622 rwlock_write_lock(&namespace_rwlock);
623 int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
[d6084ef]624 rc = vfs_lookup_internal(path, lflag, NULL, NULL);
[72bde81]625 rwlock_write_unlock(&namespace_rwlock);
626 free(path);
627 ipc_answer_0(rid, rc);
628}
629
[f15cf1a6]630void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
631{
632 int lflag = IPC_GET_ARG1(*request);
633
634 size_t len;
635 ipc_callid_t callid;
636
637 if (!ipc_data_write_receive(&callid, &len)) {
638 ipc_answer_0(callid, EINVAL);
639 ipc_answer_0(rid, EINVAL);
640 return;
641 }
[d6084ef]642 char *path = malloc(len + 1);
[f15cf1a6]643 if (!path) {
644 ipc_answer_0(callid, ENOMEM);
645 ipc_answer_0(rid, ENOMEM);
646 return;
647 }
648 int rc;
649 if ((rc = ipc_data_write_finalize(callid, path, len))) {
650 ipc_answer_0(rid, rc);
651 free(path);
652 return;
653 }
[d6084ef]654 path[len] = '\0';
[f15cf1a6]655
656 rwlock_write_lock(&namespace_rwlock);
657 lflag &= L_DIRECTORY; /* sanitize lflag */
658 vfs_lookup_res_t lr;
[a8e9ab8d]659 rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
[f15cf1a6]660 free(path);
661 if (rc != EOK) {
662 rwlock_write_unlock(&namespace_rwlock);
663 ipc_answer_0(rid, rc);
664 return;
665 }
666
667 /*
668 * The name has already been unlinked by vfs_lookup_internal().
669 * We have to get and put the VFS node to ensure that it is
[fdb7795]670 * VFS_DESTROY'ed after the last reference to it is dropped.
[f15cf1a6]671 */
672 vfs_node_t *node = vfs_node_get(&lr);
[c31d773]673 futex_down(&nodes_futex);
[f15cf1a6]674 node->lnkcnt--;
[c31d773]675 futex_up(&nodes_futex);
[f15cf1a6]676 rwlock_write_unlock(&namespace_rwlock);
677 vfs_node_put(node);
678 ipc_answer_0(rid, EOK);
679}
680
[a8e9ab8d]681void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
682{
683 size_t len;
684 ipc_callid_t callid;
685 int rc;
686
687 /* Retrieve the old path. */
688 if (!ipc_data_write_receive(&callid, &len)) {
689 ipc_answer_0(callid, EINVAL);
690 ipc_answer_0(rid, EINVAL);
691 return;
692 }
693 char *old = malloc(len + 1);
694 if (!old) {
695 ipc_answer_0(callid, ENOMEM);
696 ipc_answer_0(rid, ENOMEM);
697 return;
698 }
699 if ((rc = ipc_data_write_finalize(callid, old, len))) {
700 ipc_answer_0(rid, rc);
701 free(old);
702 return;
703 }
704 old[len] = '\0';
705
706 /* Retrieve the new path. */
707 if (!ipc_data_write_receive(&callid, &len)) {
708 ipc_answer_0(callid, EINVAL);
709 ipc_answer_0(rid, EINVAL);
710 free(old);
711 return;
712 }
713 char *new = malloc(len + 1);
714 if (!new) {
715 ipc_answer_0(callid, ENOMEM);
716 ipc_answer_0(rid, ENOMEM);
717 free(old);
718 return;
719 }
720 if ((rc = ipc_data_write_finalize(callid, new, len))) {
721 ipc_answer_0(rid, rc);
722 free(old);
723 free(new);
724 return;
725 }
726 new[len] = '\0';
727
728 char *oldc = canonify(old, &len);
729 char *newc = canonify(new, NULL);
730 if (!oldc || !newc) {
731 ipc_answer_0(rid, EINVAL);
732 free(old);
733 free(new);
734 return;
735 }
736 if (!strncmp(newc, oldc, len)) {
737 /* oldc is a prefix of newc */
738 ipc_answer_0(rid, EINVAL);
739 free(old);
740 free(new);
741 return;
742 }
743
744 vfs_lookup_res_t old_lr;
745 vfs_lookup_res_t new_lr;
746 vfs_lookup_res_t new_par_lr;
747 rwlock_write_lock(&namespace_rwlock);
748 /* Lookup the node belonging to the old file name. */
749 rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
750 if (rc != EOK) {
751 rwlock_write_unlock(&namespace_rwlock);
752 ipc_answer_0(rid, rc);
753 free(old);
754 free(new);
755 return;
756 }
757 vfs_node_t *old_node = vfs_node_get(&old_lr);
758 if (!old_node) {
759 rwlock_write_unlock(&namespace_rwlock);
760 ipc_answer_0(rid, ENOMEM);
761 free(old);
762 free(new);
763 return;
764 }
765 /* Lookup parent of the new file name. */
766 rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
767 if (rc != EOK) {
768 rwlock_write_unlock(&namespace_rwlock);
769 ipc_answer_0(rid, rc);
770 free(old);
771 free(new);
772 return;
773 }
774 /* Check whether linking to the same file system instance. */
775 if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
776 (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
777 rwlock_write_unlock(&namespace_rwlock);
778 ipc_answer_0(rid, EXDEV); /* different file systems */
779 free(old);
780 free(new);
781 return;
782 }
783 /* Destroy the old link for the new name. */
784 vfs_node_t *new_node = NULL;
785 rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
786 switch (rc) {
787 case ENOENT:
788 /* simply not in our way */
789 break;
790 case EOK:
791 new_node = vfs_node_get(&new_lr);
792 if (!new_node) {
793 rwlock_write_unlock(&namespace_rwlock);
794 ipc_answer_0(rid, ENOMEM);
795 free(old);
796 free(new);
797 return;
798 }
[c31d773]799 futex_down(&nodes_futex);
[a8e9ab8d]800 new_node->lnkcnt--;
[c31d773]801 futex_up(&nodes_futex);
[a8e9ab8d]802 break;
803 default:
804 rwlock_write_unlock(&namespace_rwlock);
805 ipc_answer_0(rid, ENOTEMPTY);
806 free(old);
807 free(new);
808 return;
809 }
810 /* Create the new link for the new name. */
811 rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
812 if (rc != EOK) {
813 rwlock_write_unlock(&namespace_rwlock);
814 if (new_node)
815 vfs_node_put(new_node);
816 ipc_answer_0(rid, rc);
817 free(old);
818 free(new);
819 return;
820 }
[c31d773]821 futex_down(&nodes_futex);
[a8e9ab8d]822 old_node->lnkcnt++;
[c31d773]823 futex_up(&nodes_futex);
[a8e9ab8d]824 /* Destroy the link for the old name. */
825 rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
826 if (rc != EOK) {
827 rwlock_write_unlock(&namespace_rwlock);
828 vfs_node_put(old_node);
829 if (new_node)
830 vfs_node_put(new_node);
831 ipc_answer_0(rid, rc);
832 free(old);
833 free(new);
834 return;
835 }
[c31d773]836 futex_down(&nodes_futex);
[a8e9ab8d]837 old_node->lnkcnt--;
[c31d773]838 futex_up(&nodes_futex);
[a8e9ab8d]839 rwlock_write_unlock(&namespace_rwlock);
840 vfs_node_put(old_node);
841 if (new_node)
842 vfs_node_put(new_node);
843 free(old);
844 free(new);
845 ipc_answer_0(rid, EOK);
846}
847
[861e7d1]848/**
849 * @}
850 */
Note: See TracBrowser for help on using the repository browser.