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