| 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 |
|
|---|
| 38 | #include "vfs.h"
|
|---|
| 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>
|
|---|
| 51 | #include <fcntl.h>
|
|---|
| 52 | #include <assert.h>
|
|---|
| 53 | #include <vfs/canonify.h>
|
|---|
| 54 |
|
|---|
| 55 | /* Forward declarations of static functions. */
|
|---|
| 56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
|
|---|
| 57 |
|
|---|
| 58 | /** Pending mount structure. */
|
|---|
| 59 | typedef struct {
|
|---|
| 60 | link_t link;
|
|---|
| 61 | char *fs_name; /**< File system name */
|
|---|
| 62 | char *mp; /**< Mount point */
|
|---|
| 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 |
|
|---|
| 68 | LIST_INITIALIZE(pending_req);
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
|---|
| 72 | * concurrent VFS operation which modifies the file system namespace.
|
|---|
| 73 | */
|
|---|
| 74 | RWLOCK_INITIALIZE(namespace_rwlock);
|
|---|
| 75 |
|
|---|
| 76 | futex_t rootfs_futex = FUTEX_INITIALIZER;
|
|---|
| 77 | vfs_pair_t rootfs = {
|
|---|
| 78 | .fs_handle = 0,
|
|---|
| 79 | .dev_handle = 0
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
|
|---|
| 83 | fs_handle_t fs_handle, char *mp)
|
|---|
| 84 | {
|
|---|
| 85 | /* Resolve the path to the mountpoint. */
|
|---|
| 86 | vfs_lookup_res_t mp_res;
|
|---|
| 87 | vfs_node_t *mp_node = NULL;
|
|---|
| 88 | int rc;
|
|---|
| 89 | int phone;
|
|---|
| 90 | futex_down(&rootfs_futex);
|
|---|
| 91 | if (rootfs.fs_handle) {
|
|---|
| 92 | /* We already have the root FS. */
|
|---|
| 93 | rwlock_write_lock(&namespace_rwlock);
|
|---|
| 94 | if (str_cmp(mp, "/") == 0) {
|
|---|
| 95 | /* Trying to mount root FS over root FS */
|
|---|
| 96 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 97 | futex_up(&rootfs_futex);
|
|---|
| 98 | ipc_answer_0(rid, EBUSY);
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| 104 | /* The lookup failed for some reason. */
|
|---|
| 105 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 106 | futex_up(&rootfs_futex);
|
|---|
| 107 | ipc_answer_0(rid, rc);
|
|---|
| 108 | return;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | mp_node = vfs_node_get(&mp_res);
|
|---|
| 112 | if (!mp_node) {
|
|---|
| 113 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 114 | futex_up(&rootfs_futex);
|
|---|
| 115 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 116 | return;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /*
|
|---|
| 120 | * Now we hold a reference to mp_node.
|
|---|
| 121 | * It will be dropped upon the corresponding VFS_UNMOUNT.
|
|---|
| 122 | * This prevents the mount point from being deleted.
|
|---|
| 123 | */
|
|---|
| 124 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 125 | } else {
|
|---|
| 126 | /* We still don't have the root file system mounted. */
|
|---|
| 127 | if (str_cmp(mp, "/") == 0) {
|
|---|
| 128 | vfs_lookup_res_t mr_res;
|
|---|
| 129 | vfs_node_t *mr_node;
|
|---|
| 130 | ipcarg_t rindex;
|
|---|
| 131 | ipcarg_t rsize;
|
|---|
| 132 | ipcarg_t rlnkcnt;
|
|---|
| 133 |
|
|---|
| 134 | /*
|
|---|
| 135 | * For this simple, but important case,
|
|---|
| 136 | * we are almost done.
|
|---|
| 137 | */
|
|---|
| 138 |
|
|---|
| 139 | /* Tell the mountee that it is being mounted. */
|
|---|
| 140 | phone = vfs_grab_phone(fs_handle);
|
|---|
| 141 | rc = async_req_1_3(phone, VFS_MOUNTED,
|
|---|
| 142 | (ipcarg_t) dev_handle, &rindex, &rsize, &rlnkcnt);
|
|---|
| 143 | vfs_release_phone(phone);
|
|---|
| 144 |
|
|---|
| 145 | if (rc != EOK) {
|
|---|
| 146 | futex_up(&rootfs_futex);
|
|---|
| 147 | ipc_answer_0(rid, rc);
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | mr_res.triplet.fs_handle = fs_handle;
|
|---|
| 152 | mr_res.triplet.dev_handle = dev_handle;
|
|---|
| 153 | mr_res.triplet.index = (fs_index_t) rindex;
|
|---|
| 154 | mr_res.size = (size_t) rsize;
|
|---|
| 155 | mr_res.lnkcnt = (unsigned) rlnkcnt;
|
|---|
| 156 | mr_res.type = VFS_NODE_DIRECTORY;
|
|---|
| 157 |
|
|---|
| 158 | rootfs.fs_handle = fs_handle;
|
|---|
| 159 | rootfs.dev_handle = dev_handle;
|
|---|
| 160 | futex_up(&rootfs_futex);
|
|---|
| 161 |
|
|---|
| 162 | /* Add reference to the mounted root. */
|
|---|
| 163 | mr_node = vfs_node_get(&mr_res);
|
|---|
| 164 | assert(mr_node);
|
|---|
| 165 |
|
|---|
| 166 | ipc_answer_0(rid, rc);
|
|---|
| 167 | return;
|
|---|
| 168 | } else {
|
|---|
| 169 | /*
|
|---|
| 170 | * We can't resolve this without the root filesystem
|
|---|
| 171 | * being mounted first.
|
|---|
| 172 | */
|
|---|
| 173 | futex_up(&rootfs_futex);
|
|---|
| 174 | ipc_answer_0(rid, ENOENT);
|
|---|
| 175 | return;
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | futex_up(&rootfs_futex);
|
|---|
| 179 |
|
|---|
| 180 | /*
|
|---|
| 181 | * At this point, we have all necessary pieces: file system and device
|
|---|
| 182 | * handles, and we know the mount point VFS node.
|
|---|
| 183 | */
|
|---|
| 184 |
|
|---|
| 185 | phone = vfs_grab_phone(mp_res.triplet.fs_handle);
|
|---|
| 186 | rc = async_req_4_0(phone, VFS_MOUNT,
|
|---|
| 187 | (ipcarg_t) mp_res.triplet.dev_handle,
|
|---|
| 188 | (ipcarg_t) mp_res.triplet.index,
|
|---|
| 189 | (ipcarg_t) fs_handle,
|
|---|
| 190 | (ipcarg_t) dev_handle);
|
|---|
| 191 | vfs_release_phone(phone);
|
|---|
| 192 |
|
|---|
| 193 | if (rc != EOK) {
|
|---|
| 194 | /* Mount failed, drop reference to mp_node. */
|
|---|
| 195 | if (mp_node)
|
|---|
| 196 | vfs_node_put(mp_node);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | ipc_answer_0(rid, rc);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /** Process pending mount requests */
|
|---|
| 203 | void vfs_process_pending_mount()
|
|---|
| 204 | {
|
|---|
| 205 | link_t *cur;
|
|---|
| 206 |
|
|---|
| 207 | loop:
|
|---|
| 208 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
|
|---|
| 209 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
|
|---|
| 210 |
|
|---|
| 211 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
|
|---|
| 212 | if (!fs_handle)
|
|---|
| 213 | continue;
|
|---|
| 214 |
|
|---|
| 215 | /* Acknowledge that we know fs_name. */
|
|---|
| 216 | ipc_answer_0(pr->callid, EOK);
|
|---|
| 217 |
|
|---|
| 218 | /* Do the mount */
|
|---|
| 219 | vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp);
|
|---|
| 220 |
|
|---|
| 221 | free(pr->fs_name);
|
|---|
| 222 | free(pr->mp);
|
|---|
| 223 | list_remove(cur);
|
|---|
| 224 | free(pr);
|
|---|
| 225 | goto loop;
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 230 | {
|
|---|
| 231 | /*
|
|---|
| 232 | * We expect the library to do the device-name to device-handle
|
|---|
| 233 | * translation for us, thus the device handle will arrive as ARG1
|
|---|
| 234 | * in the request.
|
|---|
| 235 | */
|
|---|
| 236 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 237 |
|
|---|
| 238 | /*
|
|---|
| 239 | * Mount flags are passed as ARG2.
|
|---|
| 240 | */
|
|---|
| 241 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
|
|---|
| 242 |
|
|---|
| 243 | /*
|
|---|
| 244 | * For now, don't make use of ARG3, but it can be used to
|
|---|
| 245 | * carry mount options in the future.
|
|---|
| 246 | */
|
|---|
| 247 |
|
|---|
| 248 | /* We want the client to send us the mount point. */
|
|---|
| 249 | ipc_callid_t callid;
|
|---|
| 250 | size_t size;
|
|---|
| 251 | if (!ipc_data_write_receive(&callid, &size)) {
|
|---|
| 252 | ipc_answer_0(callid, EINVAL);
|
|---|
| 253 | ipc_answer_0(rid, EINVAL);
|
|---|
| 254 | return;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /* Check whether size is reasonable wrt. the mount point. */
|
|---|
| 258 | if ((size < 1) || (size > MAX_PATH_LEN)) {
|
|---|
| 259 | ipc_answer_0(callid, EINVAL);
|
|---|
| 260 | ipc_answer_0(rid, EINVAL);
|
|---|
| 261 | return;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /* Allocate buffer for the mount point data being received. */
|
|---|
| 265 | char *mp = malloc(size + 1);
|
|---|
| 266 | if (!mp) {
|
|---|
| 267 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 268 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 269 | return;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /* Deliver the mount point. */
|
|---|
| 273 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
|
|---|
| 274 | if (retval != EOK) {
|
|---|
| 275 | ipc_answer_0(rid, EREFUSED);
|
|---|
| 276 | free(mp);
|
|---|
| 277 | return;
|
|---|
| 278 | }
|
|---|
| 279 | mp[size] = '\0';
|
|---|
| 280 |
|
|---|
| 281 | /*
|
|---|
| 282 | * Now, we expect the client to send us data with the name of the file
|
|---|
| 283 | * system.
|
|---|
| 284 | */
|
|---|
| 285 | if (!ipc_data_write_receive(&callid, &size)) {
|
|---|
| 286 | ipc_answer_0(callid, EINVAL);
|
|---|
| 287 | ipc_answer_0(rid, EINVAL);
|
|---|
| 288 | free(mp);
|
|---|
| 289 | return;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /*
|
|---|
| 293 | * Don't receive more than is necessary for storing a full file system
|
|---|
| 294 | * name.
|
|---|
| 295 | */
|
|---|
| 296 | if ((size < 1) || (size > FS_NAME_MAXLEN)) {
|
|---|
| 297 | ipc_answer_0(callid, EINVAL);
|
|---|
| 298 | ipc_answer_0(rid, EINVAL);
|
|---|
| 299 | free(mp);
|
|---|
| 300 | return;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /*
|
|---|
| 304 | * Allocate buffer for file system name.
|
|---|
| 305 | */
|
|---|
| 306 | char *fs_name = (char *) malloc(size + 1);
|
|---|
| 307 | if (fs_name == NULL) {
|
|---|
| 308 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 309 | ipc_answer_0(rid, EREFUSED);
|
|---|
| 310 | free(mp);
|
|---|
| 311 | return;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /* Deliver the file system name. */
|
|---|
| 315 | retval = ipc_data_write_finalize(callid, fs_name, size);
|
|---|
| 316 | if (retval != EOK) {
|
|---|
| 317 | ipc_answer_0(rid, EREFUSED);
|
|---|
| 318 | free(mp);
|
|---|
| 319 | free(fs_name);
|
|---|
| 320 | return;
|
|---|
| 321 | }
|
|---|
| 322 | fs_name[size] = '\0';
|
|---|
| 323 |
|
|---|
| 324 | /*
|
|---|
| 325 | * Check if we know a file system with the same name as is in fs_name.
|
|---|
| 326 | * This will also give us its file system handle.
|
|---|
| 327 | */
|
|---|
| 328 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
|
|---|
| 329 | if (!fs_handle) {
|
|---|
| 330 | if (flags & IPC_FLAG_BLOCKING) {
|
|---|
| 331 | /* Blocking mount, add to pending list */
|
|---|
| 332 | pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
|
|---|
| 333 | if (!pr) {
|
|---|
| 334 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 335 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 336 | free(mp);
|
|---|
| 337 | free(fs_name);
|
|---|
| 338 | return;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | pr->fs_name = fs_name;
|
|---|
| 342 | pr->mp = mp;
|
|---|
| 343 | pr->callid = callid;
|
|---|
| 344 | pr->rid = rid;
|
|---|
| 345 | pr->dev_handle = dev_handle;
|
|---|
| 346 | link_initialize(&pr->link);
|
|---|
| 347 | list_append(&pr->link, &pending_req);
|
|---|
| 348 | return;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | ipc_answer_0(callid, ENOENT);
|
|---|
| 352 | ipc_answer_0(rid, ENOENT);
|
|---|
| 353 | free(mp);
|
|---|
| 354 | free(fs_name);
|
|---|
| 355 | return;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | /* Acknowledge that we know fs_name. */
|
|---|
| 359 | ipc_answer_0(callid, EOK);
|
|---|
| 360 |
|
|---|
| 361 | /* Do the mount */
|
|---|
| 362 | vfs_mount_internal(rid, dev_handle, fs_handle, mp);
|
|---|
| 363 | free(mp);
|
|---|
| 364 | free(fs_name);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | void vfs_open(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 368 | {
|
|---|
| 369 | if (!vfs_files_init()) {
|
|---|
| 370 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 371 | return;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | /*
|
|---|
| 375 | * The POSIX interface is open(path, oflag, mode).
|
|---|
| 376 | * We can receive oflags and mode along with the VFS_OPEN call; the path
|
|---|
| 377 | * will need to arrive in another call.
|
|---|
| 378 | *
|
|---|
| 379 | * We also receive one private, non-POSIX set of flags called lflag
|
|---|
| 380 | * used to pass information to vfs_lookup_internal().
|
|---|
| 381 | */
|
|---|
| 382 | int lflag = IPC_GET_ARG1(*request);
|
|---|
| 383 | int oflag = IPC_GET_ARG2(*request);
|
|---|
| 384 | int mode = IPC_GET_ARG3(*request);
|
|---|
| 385 | size_t len;
|
|---|
| 386 |
|
|---|
| 387 | /*
|
|---|
| 388 | * Make sure that we are called with exactly one of L_FILE and
|
|---|
| 389 | * L_DIRECTORY.
|
|---|
| 390 | */
|
|---|
| 391 | if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
|
|---|
| 392 | (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
|
|---|
| 393 | ipc_answer_0(rid, EINVAL);
|
|---|
| 394 | return;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | if (oflag & O_CREAT)
|
|---|
| 398 | lflag |= L_CREATE;
|
|---|
| 399 | if (oflag & O_EXCL)
|
|---|
| 400 | lflag |= L_EXCLUSIVE;
|
|---|
| 401 |
|
|---|
| 402 | ipc_callid_t callid;
|
|---|
| 403 |
|
|---|
| 404 | if (!ipc_data_write_receive(&callid, &len)) {
|
|---|
| 405 | ipc_answer_0(callid, EINVAL);
|
|---|
| 406 | ipc_answer_0(rid, EINVAL);
|
|---|
| 407 | return;
|
|---|
| 408 | }
|
|---|
| 409 | char *path = malloc(len + 1);
|
|---|
| 410 | if (!path) {
|
|---|
| 411 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 412 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 413 | return;
|
|---|
| 414 | }
|
|---|
| 415 | int rc;
|
|---|
| 416 | if ((rc = ipc_data_write_finalize(callid, path, len))) {
|
|---|
| 417 | ipc_answer_0(rid, rc);
|
|---|
| 418 | free(path);
|
|---|
| 419 | return;
|
|---|
| 420 | }
|
|---|
| 421 | path[len] = '\0';
|
|---|
| 422 |
|
|---|
| 423 | /*
|
|---|
| 424 | * Avoid the race condition in which the file can be deleted before we
|
|---|
| 425 | * find/create-and-lock the VFS node corresponding to the looked-up
|
|---|
| 426 | * triplet.
|
|---|
| 427 | */
|
|---|
| 428 | if (lflag & L_CREATE)
|
|---|
| 429 | rwlock_write_lock(&namespace_rwlock);
|
|---|
| 430 | else
|
|---|
| 431 | rwlock_read_lock(&namespace_rwlock);
|
|---|
| 432 |
|
|---|
| 433 | /* The path is now populated and we can call vfs_lookup_internal(). */
|
|---|
| 434 | vfs_lookup_res_t lr;
|
|---|
| 435 | rc = vfs_lookup_internal(path, lflag, &lr, NULL);
|
|---|
| 436 | if (rc) {
|
|---|
| 437 | if (lflag & L_CREATE)
|
|---|
| 438 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 439 | else
|
|---|
| 440 | rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 441 | ipc_answer_0(rid, rc);
|
|---|
| 442 | free(path);
|
|---|
| 443 | return;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | /* Path is no longer needed. */
|
|---|
| 447 | free(path);
|
|---|
| 448 |
|
|---|
| 449 | vfs_node_t *node = vfs_node_get(&lr);
|
|---|
| 450 | if (lflag & L_CREATE)
|
|---|
| 451 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 452 | else
|
|---|
| 453 | rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 454 |
|
|---|
| 455 | /* Truncate the file if requested and if necessary. */
|
|---|
| 456 | if (oflag & O_TRUNC) {
|
|---|
| 457 | rwlock_write_lock(&node->contents_rwlock);
|
|---|
| 458 | if (node->size) {
|
|---|
| 459 | rc = vfs_truncate_internal(node->fs_handle,
|
|---|
| 460 | node->dev_handle, node->index, 0);
|
|---|
| 461 | if (rc) {
|
|---|
| 462 | rwlock_write_unlock(&node->contents_rwlock);
|
|---|
| 463 | vfs_node_put(node);
|
|---|
| 464 | ipc_answer_0(rid, rc);
|
|---|
| 465 | return;
|
|---|
| 466 | }
|
|---|
| 467 | node->size = 0;
|
|---|
| 468 | }
|
|---|
| 469 | rwlock_write_unlock(&node->contents_rwlock);
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | /*
|
|---|
| 473 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
|---|
| 474 | * structure.
|
|---|
| 475 | */
|
|---|
| 476 | int fd = vfs_fd_alloc();
|
|---|
| 477 | if (fd < 0) {
|
|---|
| 478 | vfs_node_put(node);
|
|---|
| 479 | ipc_answer_0(rid, fd);
|
|---|
| 480 | return;
|
|---|
| 481 | }
|
|---|
| 482 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 483 | file->node = node;
|
|---|
| 484 | if (oflag & O_APPEND)
|
|---|
| 485 | file->append = true;
|
|---|
| 486 |
|
|---|
| 487 | /*
|
|---|
| 488 | * The following increase in reference count is for the fact that the
|
|---|
| 489 | * file is being opened and that a file structure is pointing to it.
|
|---|
| 490 | * It is necessary so that the file will not disappear when
|
|---|
| 491 | * vfs_node_put() is called. The reference will be dropped by the
|
|---|
| 492 | * respective VFS_CLOSE.
|
|---|
| 493 | */
|
|---|
| 494 | vfs_node_addref(node);
|
|---|
| 495 | vfs_node_put(node);
|
|---|
| 496 |
|
|---|
| 497 | /* Success! Return the new file descriptor to the client. */
|
|---|
| 498 | ipc_answer_1(rid, EOK, fd);
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 502 | {
|
|---|
| 503 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 504 | int rc = vfs_fd_free(fd);
|
|---|
| 505 | ipc_answer_0(rid, rc);
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
|---|
| 509 | {
|
|---|
| 510 |
|
|---|
| 511 | /*
|
|---|
| 512 | * The following code strongly depends on the fact that the files data
|
|---|
| 513 | * structure can be only accessed by a single fibril and all file
|
|---|
| 514 | * operations are serialized (i.e. the reads and writes cannot
|
|---|
| 515 | * interleave and a file cannot be closed while it is being read).
|
|---|
| 516 | *
|
|---|
| 517 | * Additional synchronization needs to be added once the table of
|
|---|
| 518 | * open files supports parallel access!
|
|---|
| 519 | */
|
|---|
| 520 |
|
|---|
| 521 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 522 |
|
|---|
| 523 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| 524 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 525 | if (!file) {
|
|---|
| 526 | ipc_answer_0(rid, ENOENT);
|
|---|
| 527 | return;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | /*
|
|---|
| 531 | * Now we need to receive a call with client's
|
|---|
| 532 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
|
|---|
| 533 | */
|
|---|
| 534 | ipc_callid_t callid;
|
|---|
| 535 | int res;
|
|---|
| 536 | if (read)
|
|---|
| 537 | res = ipc_data_read_receive(&callid, NULL);
|
|---|
| 538 | else
|
|---|
| 539 | res = ipc_data_write_receive(&callid, NULL);
|
|---|
| 540 | if (!res) {
|
|---|
| 541 | ipc_answer_0(callid, EINVAL);
|
|---|
| 542 | ipc_answer_0(rid, EINVAL);
|
|---|
| 543 | return;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | /*
|
|---|
| 547 | * Lock the open file structure so that no other thread can manipulate
|
|---|
| 548 | * the same open file at a time.
|
|---|
| 549 | */
|
|---|
| 550 | futex_down(&file->lock);
|
|---|
| 551 |
|
|---|
| 552 | /*
|
|---|
| 553 | * Lock the file's node so that no other client can read/write to it at
|
|---|
| 554 | * the same time.
|
|---|
| 555 | */
|
|---|
| 556 | if (read)
|
|---|
| 557 | rwlock_read_lock(&file->node->contents_rwlock);
|
|---|
| 558 | else
|
|---|
| 559 | rwlock_write_lock(&file->node->contents_rwlock);
|
|---|
| 560 |
|
|---|
| 561 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
|---|
| 562 | /*
|
|---|
| 563 | * Make sure that no one is modifying the namespace
|
|---|
| 564 | * while we are in readdir().
|
|---|
| 565 | */
|
|---|
| 566 | assert(read);
|
|---|
| 567 | rwlock_read_lock(&namespace_rwlock);
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
|---|
| 571 |
|
|---|
| 572 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
|
|---|
| 573 | aid_t msg;
|
|---|
| 574 | ipc_call_t answer;
|
|---|
| 575 | if (!read && file->append)
|
|---|
| 576 | file->pos = file->node->size;
|
|---|
| 577 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
|
|---|
| 578 | file->node->dev_handle, file->node->index, file->pos, &answer);
|
|---|
| 579 |
|
|---|
| 580 | /*
|
|---|
| 581 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
|---|
| 582 | * destination FS server. The call will be routed as if sent by
|
|---|
| 583 | * ourselves. Note that call arguments are immutable in this case so we
|
|---|
| 584 | * don't have to bother.
|
|---|
| 585 | */
|
|---|
| 586 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 587 |
|
|---|
| 588 | vfs_release_phone(fs_phone);
|
|---|
| 589 |
|
|---|
| 590 | /* Wait for reply from the FS server. */
|
|---|
| 591 | ipcarg_t rc;
|
|---|
| 592 | async_wait_for(msg, &rc);
|
|---|
| 593 | size_t bytes = IPC_GET_ARG1(answer);
|
|---|
| 594 |
|
|---|
| 595 | if (file->node->type == VFS_NODE_DIRECTORY)
|
|---|
| 596 | rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 597 |
|
|---|
| 598 | /* Unlock the VFS node. */
|
|---|
| 599 | if (read)
|
|---|
| 600 | rwlock_read_unlock(&file->node->contents_rwlock);
|
|---|
| 601 | else {
|
|---|
| 602 | /* Update the cached version of node's size. */
|
|---|
| 603 | if (rc == EOK)
|
|---|
| 604 | file->node->size = IPC_GET_ARG2(answer);
|
|---|
| 605 | rwlock_write_unlock(&file->node->contents_rwlock);
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | /* Update the position pointer and unlock the open file. */
|
|---|
| 609 | if (rc == EOK)
|
|---|
| 610 | file->pos += bytes;
|
|---|
| 611 | futex_up(&file->lock);
|
|---|
| 612 |
|
|---|
| 613 | /*
|
|---|
| 614 | * FS server's reply is the final result of the whole operation we
|
|---|
| 615 | * return to the client.
|
|---|
| 616 | */
|
|---|
| 617 | ipc_answer_1(rid, rc, bytes);
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 621 | {
|
|---|
| 622 | vfs_rdwr(rid, request, true);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 626 | {
|
|---|
| 627 | vfs_rdwr(rid, request, false);
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 631 | {
|
|---|
| 632 | int fd = (int) IPC_GET_ARG1(*request);
|
|---|
| 633 | off_t off = (off_t) IPC_GET_ARG2(*request);
|
|---|
| 634 | int whence = (int) IPC_GET_ARG3(*request);
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| 638 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 639 | if (!file) {
|
|---|
| 640 | ipc_answer_0(rid, ENOENT);
|
|---|
| 641 | return;
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | off_t newpos;
|
|---|
| 645 | futex_down(&file->lock);
|
|---|
| 646 | if (whence == SEEK_SET) {
|
|---|
| 647 | file->pos = off;
|
|---|
| 648 | futex_up(&file->lock);
|
|---|
| 649 | ipc_answer_1(rid, EOK, off);
|
|---|
| 650 | return;
|
|---|
| 651 | }
|
|---|
| 652 | if (whence == SEEK_CUR) {
|
|---|
| 653 | if (file->pos + off < file->pos) {
|
|---|
| 654 | futex_up(&file->lock);
|
|---|
| 655 | ipc_answer_0(rid, EOVERFLOW);
|
|---|
| 656 | return;
|
|---|
| 657 | }
|
|---|
| 658 | file->pos += off;
|
|---|
| 659 | newpos = file->pos;
|
|---|
| 660 | futex_up(&file->lock);
|
|---|
| 661 | ipc_answer_1(rid, EOK, newpos);
|
|---|
| 662 | return;
|
|---|
| 663 | }
|
|---|
| 664 | if (whence == SEEK_END) {
|
|---|
| 665 | rwlock_read_lock(&file->node->contents_rwlock);
|
|---|
| 666 | size_t size = file->node->size;
|
|---|
| 667 | rwlock_read_unlock(&file->node->contents_rwlock);
|
|---|
| 668 | if (size + off < size) {
|
|---|
| 669 | futex_up(&file->lock);
|
|---|
| 670 | ipc_answer_0(rid, EOVERFLOW);
|
|---|
| 671 | return;
|
|---|
| 672 | }
|
|---|
| 673 | newpos = size + off;
|
|---|
| 674 | futex_up(&file->lock);
|
|---|
| 675 | ipc_answer_1(rid, EOK, newpos);
|
|---|
| 676 | return;
|
|---|
| 677 | }
|
|---|
| 678 | futex_up(&file->lock);
|
|---|
| 679 | ipc_answer_0(rid, EINVAL);
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | int
|
|---|
| 683 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
|
|---|
| 684 | fs_index_t index, size_t size)
|
|---|
| 685 | {
|
|---|
| 686 | ipcarg_t rc;
|
|---|
| 687 | int fs_phone;
|
|---|
| 688 |
|
|---|
| 689 | fs_phone = vfs_grab_phone(fs_handle);
|
|---|
| 690 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
|
|---|
| 691 | (ipcarg_t)index, (ipcarg_t)size);
|
|---|
| 692 | vfs_release_phone(fs_phone);
|
|---|
| 693 | return (int)rc;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 697 | {
|
|---|
| 698 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 699 | size_t size = IPC_GET_ARG2(*request);
|
|---|
| 700 | int rc;
|
|---|
| 701 |
|
|---|
| 702 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 703 | if (!file) {
|
|---|
| 704 | ipc_answer_0(rid, ENOENT);
|
|---|
| 705 | return;
|
|---|
| 706 | }
|
|---|
| 707 | futex_down(&file->lock);
|
|---|
| 708 |
|
|---|
| 709 | rwlock_write_lock(&file->node->contents_rwlock);
|
|---|
| 710 | rc = vfs_truncate_internal(file->node->fs_handle,
|
|---|
| 711 | file->node->dev_handle, file->node->index, size);
|
|---|
| 712 | if (rc == EOK)
|
|---|
| 713 | file->node->size = size;
|
|---|
| 714 | rwlock_write_unlock(&file->node->contents_rwlock);
|
|---|
| 715 |
|
|---|
| 716 | futex_up(&file->lock);
|
|---|
| 717 | ipc_answer_0(rid, (ipcarg_t)rc);
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 721 | {
|
|---|
| 722 | int mode = IPC_GET_ARG1(*request);
|
|---|
| 723 |
|
|---|
| 724 | size_t len;
|
|---|
| 725 | ipc_callid_t callid;
|
|---|
| 726 |
|
|---|
| 727 | if (!ipc_data_write_receive(&callid, &len)) {
|
|---|
| 728 | ipc_answer_0(callid, EINVAL);
|
|---|
| 729 | ipc_answer_0(rid, EINVAL);
|
|---|
| 730 | return;
|
|---|
| 731 | }
|
|---|
| 732 | char *path = malloc(len + 1);
|
|---|
| 733 | if (!path) {
|
|---|
| 734 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 735 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 736 | return;
|
|---|
| 737 | }
|
|---|
| 738 | int rc;
|
|---|
| 739 | if ((rc = ipc_data_write_finalize(callid, path, len))) {
|
|---|
| 740 | ipc_answer_0(rid, rc);
|
|---|
| 741 | free(path);
|
|---|
| 742 | return;
|
|---|
| 743 | }
|
|---|
| 744 | path[len] = '\0';
|
|---|
| 745 |
|
|---|
| 746 | rwlock_write_lock(&namespace_rwlock);
|
|---|
| 747 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
|
|---|
| 748 | rc = vfs_lookup_internal(path, lflag, NULL, NULL);
|
|---|
| 749 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 750 | free(path);
|
|---|
| 751 | ipc_answer_0(rid, rc);
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 755 | {
|
|---|
| 756 | int lflag = IPC_GET_ARG1(*request);
|
|---|
| 757 |
|
|---|
| 758 | size_t len;
|
|---|
| 759 | ipc_callid_t callid;
|
|---|
| 760 |
|
|---|
| 761 | if (!ipc_data_write_receive(&callid, &len)) {
|
|---|
| 762 | ipc_answer_0(callid, EINVAL);
|
|---|
| 763 | ipc_answer_0(rid, EINVAL);
|
|---|
| 764 | return;
|
|---|
| 765 | }
|
|---|
| 766 | char *path = malloc(len + 1);
|
|---|
| 767 | if (!path) {
|
|---|
| 768 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 769 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 770 | return;
|
|---|
| 771 | }
|
|---|
| 772 | int rc;
|
|---|
| 773 | if ((rc = ipc_data_write_finalize(callid, path, len))) {
|
|---|
| 774 | ipc_answer_0(rid, rc);
|
|---|
| 775 | free(path);
|
|---|
| 776 | return;
|
|---|
| 777 | }
|
|---|
| 778 | path[len] = '\0';
|
|---|
| 779 |
|
|---|
| 780 | rwlock_write_lock(&namespace_rwlock);
|
|---|
| 781 | lflag &= L_DIRECTORY; /* sanitize lflag */
|
|---|
| 782 | vfs_lookup_res_t lr;
|
|---|
| 783 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
|
|---|
| 784 | free(path);
|
|---|
| 785 | if (rc != EOK) {
|
|---|
| 786 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 787 | ipc_answer_0(rid, rc);
|
|---|
| 788 | return;
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | /*
|
|---|
| 792 | * The name has already been unlinked by vfs_lookup_internal().
|
|---|
| 793 | * We have to get and put the VFS node to ensure that it is
|
|---|
| 794 | * VFS_DESTROY'ed after the last reference to it is dropped.
|
|---|
| 795 | */
|
|---|
| 796 | vfs_node_t *node = vfs_node_get(&lr);
|
|---|
| 797 | futex_down(&nodes_futex);
|
|---|
| 798 | node->lnkcnt--;
|
|---|
| 799 | futex_up(&nodes_futex);
|
|---|
| 800 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 801 | vfs_node_put(node);
|
|---|
| 802 | ipc_answer_0(rid, EOK);
|
|---|
| 803 | }
|
|---|
| 804 |
|
|---|
| 805 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 806 | {
|
|---|
| 807 | size_t olen, nlen;
|
|---|
| 808 | ipc_callid_t callid;
|
|---|
| 809 | int rc;
|
|---|
| 810 |
|
|---|
| 811 | /* Retrieve the old path. */
|
|---|
| 812 | if (!ipc_data_write_receive(&callid, &olen)) {
|
|---|
| 813 | ipc_answer_0(callid, EINVAL);
|
|---|
| 814 | ipc_answer_0(rid, EINVAL);
|
|---|
| 815 | return;
|
|---|
| 816 | }
|
|---|
| 817 | char *old = malloc(olen + 1);
|
|---|
| 818 | if (!old) {
|
|---|
| 819 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 820 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 821 | return;
|
|---|
| 822 | }
|
|---|
| 823 | if ((rc = ipc_data_write_finalize(callid, old, olen))) {
|
|---|
| 824 | ipc_answer_0(rid, rc);
|
|---|
| 825 | free(old);
|
|---|
| 826 | return;
|
|---|
| 827 | }
|
|---|
| 828 | old[olen] = '\0';
|
|---|
| 829 |
|
|---|
| 830 | /* Retrieve the new path. */
|
|---|
| 831 | if (!ipc_data_write_receive(&callid, &nlen)) {
|
|---|
| 832 | ipc_answer_0(callid, EINVAL);
|
|---|
| 833 | ipc_answer_0(rid, EINVAL);
|
|---|
| 834 | free(old);
|
|---|
| 835 | return;
|
|---|
| 836 | }
|
|---|
| 837 | char *new = malloc(nlen + 1);
|
|---|
| 838 | if (!new) {
|
|---|
| 839 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 840 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 841 | free(old);
|
|---|
| 842 | return;
|
|---|
| 843 | }
|
|---|
| 844 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
|
|---|
| 845 | ipc_answer_0(rid, rc);
|
|---|
| 846 | free(old);
|
|---|
| 847 | free(new);
|
|---|
| 848 | return;
|
|---|
| 849 | }
|
|---|
| 850 | new[nlen] = '\0';
|
|---|
| 851 |
|
|---|
| 852 | char *oldc = canonify(old, &olen);
|
|---|
| 853 | char *newc = canonify(new, &nlen);
|
|---|
| 854 | if (!oldc || !newc) {
|
|---|
| 855 | ipc_answer_0(rid, EINVAL);
|
|---|
| 856 | free(old);
|
|---|
| 857 | free(new);
|
|---|
| 858 | return;
|
|---|
| 859 | }
|
|---|
| 860 | oldc[olen] = '\0';
|
|---|
| 861 | newc[nlen] = '\0';
|
|---|
| 862 | if (!str_lcmp(newc, oldc, str_length(oldc))) {
|
|---|
| 863 | /* oldc is a prefix of newc */
|
|---|
| 864 | ipc_answer_0(rid, EINVAL);
|
|---|
| 865 | free(old);
|
|---|
| 866 | free(new);
|
|---|
| 867 | return;
|
|---|
| 868 | }
|
|---|
| 869 |
|
|---|
| 870 | vfs_lookup_res_t old_lr;
|
|---|
| 871 | vfs_lookup_res_t new_lr;
|
|---|
| 872 | vfs_lookup_res_t new_par_lr;
|
|---|
| 873 | rwlock_write_lock(&namespace_rwlock);
|
|---|
| 874 | /* Lookup the node belonging to the old file name. */
|
|---|
| 875 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
|
|---|
| 876 | if (rc != EOK) {
|
|---|
| 877 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 878 | ipc_answer_0(rid, rc);
|
|---|
| 879 | free(old);
|
|---|
| 880 | free(new);
|
|---|
| 881 | return;
|
|---|
| 882 | }
|
|---|
| 883 | vfs_node_t *old_node = vfs_node_get(&old_lr);
|
|---|
| 884 | if (!old_node) {
|
|---|
| 885 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 886 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 887 | free(old);
|
|---|
| 888 | free(new);
|
|---|
| 889 | return;
|
|---|
| 890 | }
|
|---|
| 891 | /* Lookup parent of the new file name. */
|
|---|
| 892 | rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
|
|---|
| 893 | if (rc != EOK) {
|
|---|
| 894 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 895 | ipc_answer_0(rid, rc);
|
|---|
| 896 | free(old);
|
|---|
| 897 | free(new);
|
|---|
| 898 | return;
|
|---|
| 899 | }
|
|---|
| 900 | /* Check whether linking to the same file system instance. */
|
|---|
| 901 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
|
|---|
| 902 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
|
|---|
| 903 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 904 | ipc_answer_0(rid, EXDEV); /* different file systems */
|
|---|
| 905 | free(old);
|
|---|
| 906 | free(new);
|
|---|
| 907 | return;
|
|---|
| 908 | }
|
|---|
| 909 | /* Destroy the old link for the new name. */
|
|---|
| 910 | vfs_node_t *new_node = NULL;
|
|---|
| 911 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
|
|---|
| 912 | switch (rc) {
|
|---|
| 913 | case ENOENT:
|
|---|
| 914 | /* simply not in our way */
|
|---|
| 915 | break;
|
|---|
| 916 | case EOK:
|
|---|
| 917 | new_node = vfs_node_get(&new_lr);
|
|---|
| 918 | if (!new_node) {
|
|---|
| 919 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 920 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 921 | free(old);
|
|---|
| 922 | free(new);
|
|---|
| 923 | return;
|
|---|
| 924 | }
|
|---|
| 925 | futex_down(&nodes_futex);
|
|---|
| 926 | new_node->lnkcnt--;
|
|---|
| 927 | futex_up(&nodes_futex);
|
|---|
| 928 | break;
|
|---|
| 929 | default:
|
|---|
| 930 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 931 | ipc_answer_0(rid, ENOTEMPTY);
|
|---|
| 932 | free(old);
|
|---|
| 933 | free(new);
|
|---|
| 934 | return;
|
|---|
| 935 | }
|
|---|
| 936 | /* Create the new link for the new name. */
|
|---|
| 937 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
|
|---|
| 938 | if (rc != EOK) {
|
|---|
| 939 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 940 | if (new_node)
|
|---|
| 941 | vfs_node_put(new_node);
|
|---|
| 942 | ipc_answer_0(rid, rc);
|
|---|
| 943 | free(old);
|
|---|
| 944 | free(new);
|
|---|
| 945 | return;
|
|---|
| 946 | }
|
|---|
| 947 | futex_down(&nodes_futex);
|
|---|
| 948 | old_node->lnkcnt++;
|
|---|
| 949 | futex_up(&nodes_futex);
|
|---|
| 950 | /* Destroy the link for the old name. */
|
|---|
| 951 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
|
|---|
| 952 | if (rc != EOK) {
|
|---|
| 953 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 954 | vfs_node_put(old_node);
|
|---|
| 955 | if (new_node)
|
|---|
| 956 | vfs_node_put(new_node);
|
|---|
| 957 | ipc_answer_0(rid, rc);
|
|---|
| 958 | free(old);
|
|---|
| 959 | free(new);
|
|---|
| 960 | return;
|
|---|
| 961 | }
|
|---|
| 962 | futex_down(&nodes_futex);
|
|---|
| 963 | old_node->lnkcnt--;
|
|---|
| 964 | futex_up(&nodes_futex);
|
|---|
| 965 | rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 966 | vfs_node_put(old_node);
|
|---|
| 967 | if (new_node)
|
|---|
| 968 | vfs_node_put(new_node);
|
|---|
| 969 | free(old);
|
|---|
| 970 | free(new);
|
|---|
| 971 | ipc_answer_0(rid, EOK);
|
|---|
| 972 | }
|
|---|
| 973 |
|
|---|
| 974 | /**
|
|---|
| 975 | * @}
|
|---|
| 976 | */
|
|---|