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