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