[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;
|
---|
[96b02eb9] | 80 | sysarg_t rc;
|
---|
[64b67c3] | 81 | int phone;
|
---|
[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. */
|
---|
| 125 | phone = vfs_grab_phone(fs_handle);
|
---|
[4198f9c3] | 126 | msg = async_send_1(phone, VFS_OUT_MOUNTED,
|
---|
[96b02eb9] | 127 | (sysarg_t) devmap_handle, &answer);
|
---|
[594303b] | 128 | /* send the mount options */
|
---|
[0da4e41] | 129 | rc = async_data_write_start(phone, (void *)opts,
|
---|
[594303b] | 130 | str_size(opts));
|
---|
| 131 | if (rc != EOK) {
|
---|
[230260ac] | 132 | async_wait_for(msg, NULL);
|
---|
[df908b3] | 133 | vfs_release_phone(fs_handle, phone);
|
---|
[230260ac] | 134 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 135 | async_answer_0(rid, rc);
|
---|
[594303b] | 136 | return;
|
---|
| 137 | }
|
---|
[230260ac] | 138 | async_wait_for(msg, &rc);
|
---|
[df908b3] | 139 | vfs_release_phone(fs_handle, phone);
|
---|
[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 |
|
---|
[83937ccd] | 184 | int mountee_phone = vfs_grab_phone(fs_handle);
|
---|
| 185 | assert(mountee_phone >= 0);
|
---|
| 186 |
|
---|
[64b67c3] | 187 | phone = vfs_grab_phone(mp_res.triplet.fs_handle);
|
---|
[4198f9c3] | 188 | msg = async_send_4(phone, 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 |
|
---|
| 194 | /* send connection */
|
---|
| 195 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
|
---|
| 196 | if (rc != EOK) {
|
---|
[230260ac] | 197 | async_wait_for(msg, NULL);
|
---|
[df908b3] | 198 | vfs_release_phone(fs_handle, mountee_phone);
|
---|
| 199 | vfs_release_phone(mp_res.triplet.fs_handle, phone);
|
---|
[83937ccd] | 200 | /* Mount failed, drop reference to mp_node. */
|
---|
| 201 | if (mp_node)
|
---|
| 202 | vfs_node_put(mp_node);
|
---|
[ffa2c8ef] | 203 | async_answer_0(rid, rc);
|
---|
[230260ac] | 204 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[83937ccd] | 205 | return;
|
---|
| 206 | }
|
---|
[34ca870] | 207 |
|
---|
[df908b3] | 208 | vfs_release_phone(fs_handle, mountee_phone);
|
---|
[83937ccd] | 209 |
|
---|
[594303b] | 210 | /* send the mount options */
|
---|
[0da4e41] | 211 | rc = async_data_write_start(phone, (void *)opts, str_size(opts));
|
---|
[594303b] | 212 | if (rc != EOK) {
|
---|
[230260ac] | 213 | async_wait_for(msg, NULL);
|
---|
[df908b3] | 214 | vfs_release_phone(mp_res.triplet.fs_handle, phone);
|
---|
[594303b] | 215 | /* Mount failed, drop reference to mp_node. */
|
---|
| 216 | if (mp_node)
|
---|
| 217 | vfs_node_put(mp_node);
|
---|
[230260ac] | 218 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 219 | async_answer_0(rid, rc);
|
---|
[594303b] | 220 | return;
|
---|
| 221 | }
|
---|
[230260ac] | 222 | async_wait_for(msg, &rc);
|
---|
[df908b3] | 223 | vfs_release_phone(mp_res.triplet.fs_handle, phone);
|
---|
[8dc72b64] | 224 |
|
---|
[493853ec] | 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;
|
---|
[991f645] | 231 | mr_res.triplet.devmap_handle = devmap_handle;
|
---|
[493853ec] | 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 {
|
---|
[f49b0ea] | 241 | /* Mount failed, drop reference to mp_node. */
|
---|
[861e7d1] | 242 | if (mp_node)
|
---|
| 243 | vfs_node_put(mp_node);
|
---|
| 244 | }
|
---|
[83937ccd] | 245 |
|
---|
[ffa2c8ef] | 246 | async_answer_0(rid, rc);
|
---|
[230260ac] | 247 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 248 | }
|
---|
| 249 |
|
---|
[8dc72b64] | 250 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 251 | {
|
---|
[8df8415] | 252 | devmap_handle_t devmap_handle;
|
---|
| 253 |
|
---|
[8dc72b64] | 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 | */
|
---|
[8df8415] | 259 | devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
---|
[8dc72b64] | 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. */
|
---|
[472c09d] | 272 | char *mp;
|
---|
[4cac2d69] | 273 | int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
[eda925a] | 274 | 0, NULL);
|
---|
[472c09d] | 275 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 276 | async_answer_0(rid, rc);
|
---|
[8dc72b64] | 277 | return;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[594303b] | 280 | /* Now we expect to receive the mount options. */
|
---|
[472c09d] | 281 | char *opts;
|
---|
[4cac2d69] | 282 | rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
|
---|
[eda925a] | 283 | 0, NULL);
|
---|
[472c09d] | 284 | if (rc != EOK) {
|
---|
[594303b] | 285 | free(mp);
|
---|
[ffa2c8ef] | 286 | async_answer_0(rid, rc);
|
---|
[594303b] | 287 | return;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[8dc72b64] | 290 | /*
|
---|
| 291 | * Now, we expect the client to send us data with the name of the file
|
---|
| 292 | * system.
|
---|
| 293 | */
|
---|
[472c09d] | 294 | char *fs_name;
|
---|
[8df8415] | 295 | rc = async_data_write_accept((void **) &fs_name, true, 0,
|
---|
| 296 | FS_NAME_MAXLEN, 0, NULL);
|
---|
[472c09d] | 297 | if (rc != EOK) {
|
---|
[8dc72b64] | 298 | free(mp);
|
---|
[594303b] | 299 | free(opts);
|
---|
[ffa2c8ef] | 300 | async_answer_0(rid, rc);
|
---|
[8dc72b64] | 301 | return;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[c08c355] | 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;
|
---|
[472c09d] | 309 | ipc_callid_t callid = async_get_call(&data);
|
---|
[228e490] | 310 | if (IPC_GET_IMETHOD(data) != IPC_M_PING) {
|
---|
[ffa2c8ef] | 311 | async_answer_0(callid, ENOTSUP);
|
---|
| 312 | async_answer_0(rid, ENOTSUP);
|
---|
[c08c355] | 313 | free(mp);
|
---|
[594303b] | 314 | free(opts);
|
---|
[c08c355] | 315 | free(fs_name);
|
---|
| 316 | return;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[8dc72b64] | 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 | */
|
---|
[af7383f3] | 323 | fibril_mutex_lock(&fs_head_lock);
|
---|
[7b47fa2] | 324 | fs_handle_t fs_handle;
|
---|
| 325 | recheck:
|
---|
| 326 | fs_handle = fs_name_to_handle(fs_name, false);
|
---|
[8dc72b64] | 327 | if (!fs_handle) {
|
---|
| 328 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
[7b47fa2] | 329 | fibril_condvar_wait(&fs_head_cv, &fs_head_lock);
|
---|
| 330 | goto recheck;
|
---|
[8dc72b64] | 331 | }
|
---|
| 332 |
|
---|
[af7383f3] | 333 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[ffa2c8ef] | 334 | async_answer_0(callid, ENOENT);
|
---|
| 335 | async_answer_0(rid, ENOENT);
|
---|
[8dc72b64] | 336 | free(mp);
|
---|
| 337 | free(fs_name);
|
---|
[594303b] | 338 | free(opts);
|
---|
[8dc72b64] | 339 | return;
|
---|
| 340 | }
|
---|
[af7383f3] | 341 | fibril_mutex_unlock(&fs_head_lock);
|
---|
[8dc72b64] | 342 |
|
---|
| 343 | /* Acknowledge that we know fs_name. */
|
---|
[ffa2c8ef] | 344 | async_answer_0(callid, EOK);
|
---|
[8dc72b64] | 345 |
|
---|
| 346 | /* Do the mount */
|
---|
[991f645] | 347 | vfs_mount_internal(rid, devmap_handle, fs_handle, mp, opts);
|
---|
[8dc72b64] | 348 | free(mp);
|
---|
| 349 | free(fs_name);
|
---|
[594303b] | 350 | free(opts);
|
---|
[8dc72b64] | 351 | }
|
---|
| 352 |
|
---|
[7f5e070] | 353 | void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 354 | {
|
---|
[ae75e2e3] | 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 | */
|
---|
[4cac2d69] | 365 | rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
---|
[eda925a] | 366 | 0, NULL);
|
---|
[ae75e2e3] | 367 | if (rc != EOK)
|
---|
[ffa2c8ef] | 368 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 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 | */
|
---|
[f7376cbf] | 383 | rc = vfs_lookup_internal(mp, L_ROOT, &mr_res, NULL);
|
---|
[ae75e2e3] | 384 | if (rc != EOK) {
|
---|
| 385 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 386 | free(mp);
|
---|
[ffa2c8ef] | 387 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 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);
|
---|
[ffa2c8ef] | 394 | async_answer_0(rid, ENOMEM);
|
---|
[ae75e2e3] | 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,
|
---|
[991f645] | 406 | mr_node->devmap_handle) != 2) {
|
---|
[ae75e2e3] | 407 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 408 | vfs_node_put(mr_node);
|
---|
| 409 | free(mp);
|
---|
[ffa2c8ef] | 410 | async_answer_0(rid, EBUSY);
|
---|
[ae75e2e3] | 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,
|
---|
[991f645] | 426 | mr_node->devmap_handle);
|
---|
[df908b3] | 427 | vfs_release_phone(mr_node->fs_handle, phone);
|
---|
[ae75e2e3] | 428 | if (rc != EOK) {
|
---|
| 429 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 430 | vfs_node_put(mr_node);
|
---|
[ffa2c8ef] | 431 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 432 | return;
|
---|
| 433 | }
|
---|
| 434 | rootfs.fs_handle = 0;
|
---|
[991f645] | 435 | rootfs.devmap_handle = 0;
|
---|
[ae75e2e3] | 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 |
|
---|
[f7376cbf] | 445 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
[ae75e2e3] | 446 | free(mp);
|
---|
| 447 | if (rc != EOK) {
|
---|
| 448 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 449 | vfs_node_put(mr_node);
|
---|
[ffa2c8ef] | 450 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 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);
|
---|
[ffa2c8ef] | 457 | async_answer_0(rid, ENOMEM);
|
---|
[ae75e2e3] | 458 | return;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | phone = vfs_grab_phone(mp_node->fs_handle);
|
---|
[8df8415] | 462 | rc = async_req_2_0(phone, VFS_OUT_UNMOUNT,
|
---|
| 463 | mp_node->devmap_handle, mp_node->index);
|
---|
[df908b3] | 464 | vfs_release_phone(mp_node->fs_handle, phone);
|
---|
[ae75e2e3] | 465 | if (rc != EOK) {
|
---|
| 466 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 467 | vfs_node_put(mp_node);
|
---|
| 468 | vfs_node_put(mr_node);
|
---|
[ffa2c8ef] | 469 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 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);
|
---|
[ffa2c8ef] | 487 | async_answer_0(rid, EOK);
|
---|
[7f5e070] | 488 | }
|
---|
| 489 |
|
---|
[861e7d1] | 490 | void vfs_open(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 491 | {
|
---|
| 492 | /*
|
---|
[ae78b530] | 493 | * The POSIX interface is open(path, oflag, mode).
|
---|
[4198f9c3] | 494 | * We can receive oflags and mode along with the VFS_IN_OPEN call;
|
---|
| 495 | * the path will need to arrive in another call.
|
---|
[ae78b530] | 496 | *
|
---|
| 497 | * We also receive one private, non-POSIX set of flags called lflag
|
---|
| 498 | * used to pass information to vfs_lookup_internal().
|
---|
[861e7d1] | 499 | */
|
---|
[ae78b530] | 500 | int lflag = IPC_GET_ARG1(*request);
|
---|
| 501 | int oflag = IPC_GET_ARG2(*request);
|
---|
| 502 | int mode = IPC_GET_ARG3(*request);
|
---|
[dd2cfa7] | 503 |
|
---|
| 504 | /* Ignore mode for now. */
|
---|
| 505 | (void) mode;
|
---|
[05b9912] | 506 |
|
---|
[b17186d] | 507 | /*
|
---|
| 508 | * Make sure that we are called with exactly one of L_FILE and
|
---|
[f7376cbf] | 509 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
|
---|
| 510 | * L_ROOT or L_MP.
|
---|
[b17186d] | 511 | */
|
---|
[230260ac] | 512 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
|
---|
| 513 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
|
---|
[f7376cbf] | 514 | (lflag & (L_OPEN | L_ROOT | L_MP))) {
|
---|
[ffa2c8ef] | 515 | async_answer_0(rid, EINVAL);
|
---|
[b17186d] | 516 | return;
|
---|
| 517 | }
|
---|
[05b9912] | 518 |
|
---|
[2db4ac8] | 519 | if (oflag & O_CREAT)
|
---|
| 520 | lflag |= L_CREATE;
|
---|
| 521 | if (oflag & O_EXCL)
|
---|
| 522 | lflag |= L_EXCLUSIVE;
|
---|
[05b9912] | 523 |
|
---|
[472c09d] | 524 | char *path;
|
---|
[4cac2d69] | 525 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 526 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 527 | async_answer_0(rid, rc);
|
---|
[861e7d1] | 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 | */
|
---|
[2db4ac8] | 536 | if (lflag & L_CREATE)
|
---|
[230260ac] | 537 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[2db4ac8] | 538 | else
|
---|
[230260ac] | 539 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[05b9912] | 540 |
|
---|
[72bde81] | 541 | /* The path is now populated and we can call vfs_lookup_internal(). */
|
---|
[eb27ce5a] | 542 | vfs_lookup_res_t lr;
|
---|
[05b9912] | 543 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
|
---|
| 544 | if (rc != EOK) {
|
---|
[2db4ac8] | 545 | if (lflag & L_CREATE)
|
---|
[230260ac] | 546 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[2db4ac8] | 547 | else
|
---|
[230260ac] | 548 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 549 | async_answer_0(rid, rc);
|
---|
[861e7d1] | 550 | free(path);
|
---|
| 551 | return;
|
---|
| 552 | }
|
---|
[05b9912] | 553 |
|
---|
[7fe1f75] | 554 | /* Path is no longer needed. */
|
---|
[861e7d1] | 555 | free(path);
|
---|
[05b9912] | 556 |
|
---|
[eb27ce5a] | 557 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[2db4ac8] | 558 | if (lflag & L_CREATE)
|
---|
[230260ac] | 559 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[2db4ac8] | 560 | else
|
---|
[230260ac] | 561 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[05b9912] | 562 |
|
---|
[7fe1f75] | 563 | /* Truncate the file if requested and if necessary. */
|
---|
| 564 | if (oflag & O_TRUNC) {
|
---|
[230260ac] | 565 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
[7fe1f75] | 566 | if (node->size) {
|
---|
| 567 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
[991f645] | 568 | node->devmap_handle, node->index, 0);
|
---|
[7fe1f75] | 569 | if (rc) {
|
---|
[230260ac] | 570 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
[7fe1f75] | 571 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 572 | async_answer_0(rid, rc);
|
---|
[7fe1f75] | 573 | return;
|
---|
| 574 | }
|
---|
| 575 | node->size = 0;
|
---|
| 576 | }
|
---|
[230260ac] | 577 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
[7fe1f75] | 578 | }
|
---|
[05b9912] | 579 |
|
---|
[861e7d1] | 580 | /*
|
---|
| 581 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
| 582 | * structure.
|
---|
| 583 | */
|
---|
[2b88074b] | 584 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
[861e7d1] | 585 | if (fd < 0) {
|
---|
| 586 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 587 | async_answer_0(rid, fd);
|
---|
[861e7d1] | 588 | return;
|
---|
| 589 | }
|
---|
| 590 | vfs_file_t *file = vfs_file_get(fd);
|
---|
[179d052] | 591 | assert(file);
|
---|
[861e7d1] | 592 | file->node = node;
|
---|
[05b9912] | 593 | if (oflag & O_APPEND)
|
---|
[15b9970] | 594 | file->append = true;
|
---|
[05b9912] | 595 |
|
---|
[861e7d1] | 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
|
---|
[4198f9c3] | 601 | * respective VFS_IN_CLOSE.
|
---|
[861e7d1] | 602 | */
|
---|
| 603 | vfs_node_addref(node);
|
---|
| 604 | vfs_node_put(node);
|
---|
[4fe94c66] | 605 | vfs_file_put(file);
|
---|
[05b9912] | 606 |
|
---|
| 607 | /* Success! Return the new file descriptor to the client. */
|
---|
[ffa2c8ef] | 608 | async_answer_1(rid, EOK, fd);
|
---|
[05b9912] | 609 | }
|
---|
[861e7d1] | 610 |
|
---|
[05b9912] | 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);
|
---|
[991f645] | 621 | lr.triplet.devmap_handle = IPC_GET_ARG2(*request);
|
---|
[05b9912] | 622 | lr.triplet.index = IPC_GET_ARG3(*request);
|
---|
| 623 | int oflag = IPC_GET_ARG4(*request);
|
---|
| 624 |
|
---|
[230260ac] | 625 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[05b9912] | 626 |
|
---|
| 627 | int rc = vfs_open_node_internal(&lr);
|
---|
| 628 | if (rc != EOK) {
|
---|
[230260ac] | 629 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 630 | async_answer_0(rid, rc);
|
---|
[05b9912] | 631 | return;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[230260ac] | 635 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[05b9912] | 636 |
|
---|
| 637 | /* Truncate the file if requested and if necessary. */
|
---|
| 638 | if (oflag & O_TRUNC) {
|
---|
[230260ac] | 639 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
[05b9912] | 640 | if (node->size) {
|
---|
| 641 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
[991f645] | 642 | node->devmap_handle, node->index, 0);
|
---|
[05b9912] | 643 | if (rc) {
|
---|
[230260ac] | 644 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
[05b9912] | 645 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 646 | async_answer_0(rid, rc);
|
---|
[05b9912] | 647 | return;
|
---|
| 648 | }
|
---|
| 649 | node->size = 0;
|
---|
| 650 | }
|
---|
[230260ac] | 651 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
[05b9912] | 652 | }
|
---|
| 653 |
|
---|
| 654 | /*
|
---|
| 655 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
| 656 | * structure.
|
---|
| 657 | */
|
---|
[2b88074b] | 658 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
[05b9912] | 659 | if (fd < 0) {
|
---|
| 660 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 661 | async_answer_0(rid, fd);
|
---|
[05b9912] | 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
|
---|
[4198f9c3] | 674 | * respective VFS_IN_CLOSE.
|
---|
[05b9912] | 675 | */
|
---|
| 676 | vfs_node_addref(node);
|
---|
| 677 | vfs_node_put(node);
|
---|
[4fe94c66] | 678 | vfs_file_put(file);
|
---|
[05b9912] | 679 |
|
---|
[72bde81] | 680 | /* Success! Return the new file descriptor to the client. */
|
---|
[ffa2c8ef] | 681 | async_answer_1(rid, EOK, fd);
|
---|
[861e7d1] | 682 | }
|
---|
| 683 |
|
---|
[05b9912] | 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) {
|
---|
[ffa2c8ef] | 691 | async_answer_0(rid, ENOENT);
|
---|
[05b9912] | 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 | */
|
---|
[230260ac] | 699 | fibril_mutex_lock(&file->lock);
|
---|
[05b9912] | 700 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
| 701 |
|
---|
[4198f9c3] | 702 | /* Make a VFS_OUT_SYMC request at the destination FS server. */
|
---|
[05b9912] | 703 | aid_t msg;
|
---|
| 704 | ipc_call_t answer;
|
---|
[991f645] | 705 | msg = async_send_2(fs_phone, VFS_OUT_SYNC, file->node->devmap_handle,
|
---|
[4198f9c3] | 706 | file->node->index, &answer);
|
---|
[230260ac] | 707 |
|
---|
[05b9912] | 708 | /* Wait for reply from the FS server. */
|
---|
[96b02eb9] | 709 | sysarg_t rc;
|
---|
[05b9912] | 710 | async_wait_for(msg, &rc);
|
---|
| 711 |
|
---|
[df908b3] | 712 | vfs_release_phone(file->node->fs_handle, fs_phone);
|
---|
[230260ac] | 713 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 714 |
|
---|
| 715 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 716 | async_answer_0(rid, rc);
|
---|
[e704503] | 717 | }
|
---|
| 718 |
|
---|
[f29a3a2] | 719 | int vfs_close_internal(vfs_file_t *file)
|
---|
[2b88074b] | 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;
|
---|
[8df8415] | 737 | msg = async_send_2(fs_phone, VFS_OUT_CLOSE,
|
---|
| 738 | file->node->devmap_handle, file->node->index, &answer);
|
---|
[2b88074b] | 739 |
|
---|
| 740 | /* Wait for reply from the FS server. */
|
---|
[96b02eb9] | 741 | sysarg_t rc;
|
---|
[2b88074b] | 742 | async_wait_for(msg, &rc);
|
---|
| 743 |
|
---|
[df908b3] | 744 | vfs_release_phone(file->node->fs_handle, fs_phone);
|
---|
[2b88074b] | 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 |
|
---|
[05b9912] | 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) {
|
---|
[ffa2c8ef] | 761 | async_answer_0(rid, ENOENT);
|
---|
[05b9912] | 762 | return;
|
---|
| 763 | }
|
---|
| 764 |
|
---|
[2b88074b] | 765 | int ret = vfs_close_internal(file);
|
---|
| 766 | if (ret != EOK)
|
---|
[ffa2c8ef] | 767 | async_answer_0(rid, ret);
|
---|
[05b9912] | 768 |
|
---|
[4fe94c66] | 769 | vfs_file_put(file);
|
---|
[2b88074b] | 770 | ret = vfs_fd_free(fd);
|
---|
[ffa2c8ef] | 771 | async_answer_0(rid, ret);
|
---|
[05b9912] | 772 | }
|
---|
| 773 |
|
---|
[861e7d1] | 774 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
| 775 | {
|
---|
[c2f4b6b] | 776 | vfs_info_t *vi;
|
---|
[861e7d1] | 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);
|
---|
[6c89f20] | 789 |
|
---|
[72bde81] | 790 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
[861e7d1] | 791 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 792 | if (!file) {
|
---|
[ffa2c8ef] | 793 | async_answer_0(rid, ENOENT);
|
---|
[861e7d1] | 794 | return;
|
---|
| 795 | }
|
---|
[6c89f20] | 796 |
|
---|
[861e7d1] | 797 | /*
|
---|
| 798 | * Lock the open file structure so that no other thread can manipulate
|
---|
| 799 | * the same open file at a time.
|
---|
| 800 | */
|
---|
[230260ac] | 801 | fibril_mutex_lock(&file->lock);
|
---|
[b17186d] | 802 |
|
---|
[c2f4b6b] | 803 | vi = fs_handle_to_info(file->node->fs_handle);
|
---|
| 804 | assert(vi);
|
---|
| 805 |
|
---|
[861e7d1] | 806 | /*
|
---|
| 807 | * Lock the file's node so that no other client can read/write to it at
|
---|
[c2f4b6b] | 808 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
| 809 | * write implementation does not modify the file size.
|
---|
[861e7d1] | 810 | */
|
---|
[c2f4b6b] | 811 | if (read || (vi->concurrent_read_write && vi->write_retains_size))
|
---|
[230260ac] | 812 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
[861e7d1] | 813 | else
|
---|
[230260ac] | 814 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[b17186d] | 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);
|
---|
[230260ac] | 822 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[b17186d] | 823 | }
|
---|
[6c89f20] | 824 |
|
---|
[b4cbef1] | 825 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
[861e7d1] | 826 |
|
---|
| 827 | /*
|
---|
[b4cbef1] | 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
|
---|
[861e7d1] | 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 | */
|
---|
[96b02eb9] | 834 | sysarg_t rc;
|
---|
[b4cbef1] | 835 | ipc_call_t answer;
|
---|
| 836 | if (read) {
|
---|
| 837 | rc = async_data_read_forward_3_1(fs_phone, VFS_OUT_READ,
|
---|
[991f645] | 838 | file->node->devmap_handle, file->node->index, file->pos,
|
---|
[b4cbef1] | 839 | &answer);
|
---|
| 840 | } else {
|
---|
[3a4b3ba] | 841 | if (file->append)
|
---|
| 842 | file->pos = file->node->size;
|
---|
| 843 |
|
---|
[eda925a] | 844 | rc = async_data_write_forward_3_1(fs_phone, VFS_OUT_WRITE,
|
---|
[991f645] | 845 | file->node->devmap_handle, file->node->index, file->pos,
|
---|
[b4cbef1] | 846 | &answer);
|
---|
| 847 | }
|
---|
[05b9912] | 848 |
|
---|
[df908b3] | 849 | vfs_release_phone(file->node->fs_handle, fs_phone);
|
---|
[34ca870] | 850 |
|
---|
[861e7d1] | 851 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
[b4cbef1] | 852 |
|
---|
[b17186d] | 853 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
[230260ac] | 854 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[6c89f20] | 855 |
|
---|
[72bde81] | 856 | /* Unlock the VFS node. */
|
---|
[c2f4b6b] | 857 | if (read || (vi->concurrent_read_write && vi->write_retains_size))
|
---|
[230260ac] | 858 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
[861e7d1] | 859 | else {
|
---|
| 860 | /* Update the cached version of node's size. */
|
---|
[f7017572] | 861 | if (rc == EOK)
|
---|
| 862 | file->node->size = IPC_GET_ARG2(answer);
|
---|
[230260ac] | 863 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
[861e7d1] | 864 | }
|
---|
[6c89f20] | 865 |
|
---|
[72bde81] | 866 | /* Update the position pointer and unlock the open file. */
|
---|
[f7017572] | 867 | if (rc == EOK)
|
---|
| 868 | file->pos += bytes;
|
---|
[230260ac] | 869 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 870 | vfs_file_put(file);
|
---|
| 871 |
|
---|
[861e7d1] | 872 | /*
|
---|
| 873 | * FS server's reply is the final result of the whole operation we
|
---|
| 874 | * return to the client.
|
---|
| 875 | */
|
---|
[ffa2c8ef] | 876 | async_answer_1(rid, rc, bytes);
|
---|
[861e7d1] | 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);
|
---|
[8df8415] | 892 | off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
| 893 | IPC_GET_ARG3(*request));
|
---|
[ed903174] | 894 | int whence = (int) IPC_GET_ARG4(*request);
|
---|
| 895 |
|
---|
[72bde81] | 896 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
[861e7d1] | 897 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 898 | if (!file) {
|
---|
[ffa2c8ef] | 899 | async_answer_0(rid, ENOENT);
|
---|
[861e7d1] | 900 | return;
|
---|
| 901 | }
|
---|
[ed903174] | 902 |
|
---|
[230260ac] | 903 | fibril_mutex_lock(&file->lock);
|
---|
[ed903174] | 904 |
|
---|
| 905 | off64_t newoff;
|
---|
| 906 | switch (whence) {
|
---|
[8df8415] | 907 | case SEEK_SET:
|
---|
| 908 | if (off >= 0) {
|
---|
| 909 | file->pos = (aoff64_t) off;
|
---|
[230260ac] | 910 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 911 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 912 | async_answer_1(rid, EOK, off);
|
---|
[861e7d1] | 913 | return;
|
---|
[8df8415] | 914 | }
|
---|
| 915 | break;
|
---|
| 916 | case SEEK_CUR:
|
---|
| 917 | if ((off >= 0) && (file->pos + off < file->pos)) {
|
---|
| 918 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 919 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 920 | async_answer_0(rid, EOVERFLOW);
|
---|
[8df8415] | 921 | return;
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | if ((off < 0) && (file->pos < (aoff64_t) -off)) {
|
---|
| 925 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 926 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 927 | async_answer_0(rid, EOVERFLOW);
|
---|
[8df8415] | 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);
|
---|
[4fe94c66] | 935 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 936 | async_answer_2(rid, EOK, LOWER32(newoff),
|
---|
[8df8415] | 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)) {
|
---|
[ed903174] | 944 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
[230260ac] | 945 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 946 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 947 | async_answer_0(rid, EOVERFLOW);
|
---|
[861e7d1] | 948 | return;
|
---|
[8df8415] | 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);
|
---|
[4fe94c66] | 954 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 955 | async_answer_0(rid, EOVERFLOW);
|
---|
[8df8415] | 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);
|
---|
[4fe94c66] | 964 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 965 | async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
|
---|
[8df8415] | 966 | return;
|
---|
[861e7d1] | 967 | }
|
---|
[ed903174] | 968 |
|
---|
[230260ac] | 969 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 970 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 971 | async_answer_0(rid, EINVAL);
|
---|
[861e7d1] | 972 | }
|
---|
| 973 |
|
---|
[991f645] | 974 | int vfs_truncate_internal(fs_handle_t fs_handle, devmap_handle_t devmap_handle,
|
---|
[ed903174] | 975 | fs_index_t index, aoff64_t size)
|
---|
[7fe1f75] | 976 | {
|
---|
[96b02eb9] | 977 | sysarg_t rc;
|
---|
[7fe1f75] | 978 | int fs_phone;
|
---|
| 979 |
|
---|
| 980 | fs_phone = vfs_grab_phone(fs_handle);
|
---|
[96b02eb9] | 981 | rc = async_req_4_0(fs_phone, VFS_OUT_TRUNCATE, (sysarg_t) devmap_handle,
|
---|
| 982 | (sysarg_t) index, LOWER32(size), UPPER32(size));
|
---|
[df908b3] | 983 | vfs_release_phone(fs_handle, fs_phone);
|
---|
[7fe1f75] | 984 | return (int)rc;
|
---|
| 985 | }
|
---|
| 986 |
|
---|
[0ee4322] | 987 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 988 | {
|
---|
| 989 | int fd = IPC_GET_ARG1(*request);
|
---|
[8df8415] | 990 | aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
| 991 | IPC_GET_ARG3(*request));
|
---|
[7fe1f75] | 992 | int rc;
|
---|
[0ee4322] | 993 |
|
---|
| 994 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 995 | if (!file) {
|
---|
[ffa2c8ef] | 996 | async_answer_0(rid, ENOENT);
|
---|
[0ee4322] | 997 | return;
|
---|
| 998 | }
|
---|
[230260ac] | 999 | fibril_mutex_lock(&file->lock);
|
---|
[0ee4322] | 1000 |
|
---|
[230260ac] | 1001 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[7fe1f75] | 1002 | rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
[991f645] | 1003 | file->node->devmap_handle, file->node->index, size);
|
---|
[0ee4322] | 1004 | if (rc == EOK)
|
---|
| 1005 | file->node->size = size;
|
---|
[230260ac] | 1006 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
[0ee4322] | 1007 |
|
---|
[230260ac] | 1008 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 1009 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 1010 | async_answer_0(rid, (sysarg_t)rc);
|
---|
[861e7d1] | 1011 | }
|
---|
| 1012 |
|
---|
[852b801] | 1013 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1014 | {
|
---|
| 1015 | int fd = IPC_GET_ARG1(*request);
|
---|
[96b02eb9] | 1016 | sysarg_t rc;
|
---|
[852b801] | 1017 |
|
---|
| 1018 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 1019 | if (!file) {
|
---|
[ffa2c8ef] | 1020 | async_answer_0(rid, ENOENT);
|
---|
[852b801] | 1021 | return;
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
| 1024 | ipc_callid_t callid;
|
---|
[0da4e41] | 1025 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
[4fe94c66] | 1026 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 1027 | async_answer_0(callid, EINVAL);
|
---|
| 1028 | async_answer_0(rid, EINVAL);
|
---|
[852b801] | 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;
|
---|
[991f645] | 1037 | msg = async_send_3(fs_phone, VFS_OUT_STAT, file->node->devmap_handle,
|
---|
[852b801] | 1038 | file->node->index, true, NULL);
|
---|
[ffa2c8ef] | 1039 | async_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
[852b801] | 1040 | async_wait_for(msg, &rc);
|
---|
[df908b3] | 1041 | vfs_release_phone(file->node->fs_handle, fs_phone);
|
---|
[852b801] | 1042 |
|
---|
| 1043 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 1044 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 1045 | async_answer_0(rid, rc);
|
---|
[852b801] | 1046 | }
|
---|
| 1047 |
|
---|
| 1048 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1049 | {
|
---|
[472c09d] | 1050 | char *path;
|
---|
[4cac2d69] | 1051 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1052 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 1053 | async_answer_0(rid, rc);
|
---|
[415c7e0d] | 1054 | return;
|
---|
| 1055 | }
|
---|
[472c09d] | 1056 |
|
---|
| 1057 | ipc_callid_t callid;
|
---|
[0da4e41] | 1058 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
[415c7e0d] | 1059 | free(path);
|
---|
[ffa2c8ef] | 1060 | async_answer_0(callid, EINVAL);
|
---|
| 1061 | async_answer_0(rid, EINVAL);
|
---|
[415c7e0d] | 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);
|
---|
[ffa2c8ef] | 1071 | async_answer_0(callid, rc);
|
---|
| 1072 | async_answer_0(rid, rc);
|
---|
[415c7e0d] | 1073 | return;
|
---|
| 1074 | }
|
---|
| 1075 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
| 1076 | if (!node) {
|
---|
| 1077 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1078 | async_answer_0(callid, ENOMEM);
|
---|
| 1079 | async_answer_0(rid, ENOMEM);
|
---|
[415c7e0d] | 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;
|
---|
[991f645] | 1087 | msg = async_send_3(fs_phone, VFS_OUT_STAT, node->devmap_handle,
|
---|
[415c7e0d] | 1088 | node->index, false, NULL);
|
---|
[ffa2c8ef] | 1089 | async_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
[057760d3] | 1090 |
|
---|
[96b02eb9] | 1091 | sysarg_t rv;
|
---|
[057760d3] | 1092 | async_wait_for(msg, &rv);
|
---|
[df908b3] | 1093 | vfs_release_phone(node->fs_handle, fs_phone);
|
---|
[415c7e0d] | 1094 |
|
---|
[ffa2c8ef] | 1095 | async_answer_0(rid, rv);
|
---|
[415c7e0d] | 1096 |
|
---|
| 1097 | vfs_node_put(node);
|
---|
[852b801] | 1098 | }
|
---|
| 1099 |
|
---|
[72bde81] | 1100 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1101 | {
|
---|
| 1102 | int mode = IPC_GET_ARG1(*request);
|
---|
[472c09d] | 1103 |
|
---|
| 1104 | char *path;
|
---|
[4cac2d69] | 1105 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1106 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 1107 | async_answer_0(rid, rc);
|
---|
[72bde81] | 1108 | return;
|
---|
| 1109 | }
|
---|
[472c09d] | 1110 |
|
---|
[dd2cfa7] | 1111 | /* Ignore mode for now. */
|
---|
| 1112 | (void) mode;
|
---|
[72bde81] | 1113 |
|
---|
[230260ac] | 1114 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[72bde81] | 1115 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
|
---|
[d6084ef] | 1116 | rc = vfs_lookup_internal(path, lflag, NULL, NULL);
|
---|
[230260ac] | 1117 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[72bde81] | 1118 | free(path);
|
---|
[ffa2c8ef] | 1119 | async_answer_0(rid, rc);
|
---|
[72bde81] | 1120 | }
|
---|
| 1121 |
|
---|
[f15cf1a6] | 1122 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1123 | {
|
---|
| 1124 | int lflag = IPC_GET_ARG1(*request);
|
---|
[472c09d] | 1125 |
|
---|
| 1126 | char *path;
|
---|
[4cac2d69] | 1127 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1128 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 1129 | async_answer_0(rid, rc);
|
---|
[f15cf1a6] | 1130 | return;
|
---|
| 1131 | }
|
---|
| 1132 |
|
---|
[230260ac] | 1133 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[f15cf1a6] | 1134 | lflag &= L_DIRECTORY; /* sanitize lflag */
|
---|
| 1135 | vfs_lookup_res_t lr;
|
---|
[a8e9ab8d] | 1136 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
|
---|
[f15cf1a6] | 1137 | free(path);
|
---|
| 1138 | if (rc != EOK) {
|
---|
[230260ac] | 1139 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1140 | async_answer_0(rid, rc);
|
---|
[f15cf1a6] | 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
|
---|
[4198f9c3] | 1147 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
|
---|
[f15cf1a6] | 1148 | */
|
---|
| 1149 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[553492be] | 1150 | fibril_mutex_lock(&nodes_mutex);
|
---|
[f15cf1a6] | 1151 | node->lnkcnt--;
|
---|
[553492be] | 1152 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[230260ac] | 1153 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[f15cf1a6] | 1154 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 1155 | async_answer_0(rid, EOK);
|
---|
[f15cf1a6] | 1156 | }
|
---|
| 1157 |
|
---|
[a8e9ab8d] | 1158 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1159 | {
|
---|
| 1160 | /* Retrieve the old path. */
|
---|
[472c09d] | 1161 | char *old;
|
---|
[4cac2d69] | 1162 | int rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1163 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 1164 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1165 | return;
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | /* Retrieve the new path. */
|
---|
[472c09d] | 1169 | char *new;
|
---|
[4cac2d69] | 1170 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1171 | if (rc != EOK) {
|
---|
[a8e9ab8d] | 1172 | free(old);
|
---|
[ffa2c8ef] | 1173 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1174 | return;
|
---|
| 1175 | }
|
---|
[472c09d] | 1176 |
|
---|
| 1177 | size_t olen;
|
---|
| 1178 | size_t nlen;
|
---|
[732bb0c] | 1179 | char *oldc = canonify(old, &olen);
|
---|
| 1180 | char *newc = canonify(new, &nlen);
|
---|
[472c09d] | 1181 |
|
---|
| 1182 | if ((!oldc) || (!newc)) {
|
---|
[ffa2c8ef] | 1183 | async_answer_0(rid, EINVAL);
|
---|
[a8e9ab8d] | 1184 | free(old);
|
---|
| 1185 | free(new);
|
---|
| 1186 | return;
|
---|
| 1187 | }
|
---|
[472c09d] | 1188 |
|
---|
[732bb0c] | 1189 | oldc[olen] = '\0';
|
---|
| 1190 | newc[nlen] = '\0';
|
---|
[472c09d] | 1191 |
|
---|
[14040e5] | 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 | */
|
---|
[ffa2c8ef] | 1202 | async_answer_0(rid, EINVAL);
|
---|
[a8e9ab8d] | 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;
|
---|
[230260ac] | 1211 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[472c09d] | 1212 |
|
---|
[a8e9ab8d] | 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) {
|
---|
[230260ac] | 1216 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1217 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1218 | free(old);
|
---|
| 1219 | free(new);
|
---|
| 1220 | return;
|
---|
| 1221 | }
|
---|
[472c09d] | 1222 |
|
---|
[a8e9ab8d] | 1223 | vfs_node_t *old_node = vfs_node_get(&old_lr);
|
---|
| 1224 | if (!old_node) {
|
---|
[230260ac] | 1225 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1226 | async_answer_0(rid, ENOMEM);
|
---|
[a8e9ab8d] | 1227 | free(old);
|
---|
| 1228 | free(new);
|
---|
| 1229 | return;
|
---|
| 1230 | }
|
---|
[472c09d] | 1231 |
|
---|
[4f46695e] | 1232 | /* Determine the path to the parent of the node with the new name. */
|
---|
| 1233 | char *parentc = str_dup(newc);
|
---|
| 1234 | if (!parentc) {
|
---|
[230260ac] | 1235 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1236 | async_answer_0(rid, rc);
|
---|
[4f46695e] | 1237 | free(old);
|
---|
| 1238 | free(new);
|
---|
| 1239 | return;
|
---|
| 1240 | }
|
---|
[472c09d] | 1241 |
|
---|
[ae55ee8] | 1242 | char *lastsl = str_rchr(parentc + 1, '/');
|
---|
[4f46695e] | 1243 | if (lastsl)
|
---|
| 1244 | *lastsl = '\0';
|
---|
| 1245 | else
|
---|
| 1246 | parentc[1] = '\0';
|
---|
[472c09d] | 1247 |
|
---|
[a8e9ab8d] | 1248 | /* Lookup parent of the new file name. */
|
---|
[4f46695e] | 1249 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
|
---|
| 1250 | free(parentc); /* not needed anymore */
|
---|
[a8e9ab8d] | 1251 | if (rc != EOK) {
|
---|
[230260ac] | 1252 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1253 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1254 | free(old);
|
---|
| 1255 | free(new);
|
---|
| 1256 | return;
|
---|
| 1257 | }
|
---|
[472c09d] | 1258 |
|
---|
[a8e9ab8d] | 1259 | /* Check whether linking to the same file system instance. */
|
---|
| 1260 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
|
---|
[991f645] | 1261 | (old_node->devmap_handle != new_par_lr.triplet.devmap_handle)) {
|
---|
[230260ac] | 1262 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1263 | async_answer_0(rid, EXDEV); /* different file systems */
|
---|
[a8e9ab8d] | 1264 | free(old);
|
---|
| 1265 | free(new);
|
---|
| 1266 | return;
|
---|
| 1267 | }
|
---|
[472c09d] | 1268 |
|
---|
[a8e9ab8d] | 1269 | /* Destroy the old link for the new name. */
|
---|
| 1270 | vfs_node_t *new_node = NULL;
|
---|
| 1271 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
|
---|
[472c09d] | 1272 |
|
---|
[a8e9ab8d] | 1273 | switch (rc) {
|
---|
| 1274 | case ENOENT:
|
---|
| 1275 | /* simply not in our way */
|
---|
| 1276 | break;
|
---|
| 1277 | case EOK:
|
---|
| 1278 | new_node = vfs_node_get(&new_lr);
|
---|
| 1279 | if (!new_node) {
|
---|
[230260ac] | 1280 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1281 | async_answer_0(rid, ENOMEM);
|
---|
[a8e9ab8d] | 1282 | free(old);
|
---|
| 1283 | free(new);
|
---|
| 1284 | return;
|
---|
| 1285 | }
|
---|
[553492be] | 1286 | fibril_mutex_lock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1287 | new_node->lnkcnt--;
|
---|
[553492be] | 1288 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1289 | break;
|
---|
| 1290 | default:
|
---|
[230260ac] | 1291 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1292 | async_answer_0(rid, ENOTEMPTY);
|
---|
[a8e9ab8d] | 1293 | free(old);
|
---|
| 1294 | free(new);
|
---|
| 1295 | return;
|
---|
| 1296 | }
|
---|
[472c09d] | 1297 |
|
---|
[a8e9ab8d] | 1298 | /* Create the new link for the new name. */
|
---|
| 1299 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
|
---|
| 1300 | if (rc != EOK) {
|
---|
[230260ac] | 1301 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[a8e9ab8d] | 1302 | if (new_node)
|
---|
| 1303 | vfs_node_put(new_node);
|
---|
[ffa2c8ef] | 1304 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1305 | free(old);
|
---|
| 1306 | free(new);
|
---|
| 1307 | return;
|
---|
| 1308 | }
|
---|
[472c09d] | 1309 |
|
---|
[553492be] | 1310 | fibril_mutex_lock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1311 | old_node->lnkcnt++;
|
---|
[553492be] | 1312 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[472c09d] | 1313 |
|
---|
[a8e9ab8d] | 1314 | /* Destroy the link for the old name. */
|
---|
| 1315 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
|
---|
| 1316 | if (rc != EOK) {
|
---|
[230260ac] | 1317 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[a8e9ab8d] | 1318 | vfs_node_put(old_node);
|
---|
| 1319 | if (new_node)
|
---|
| 1320 | vfs_node_put(new_node);
|
---|
[ffa2c8ef] | 1321 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1322 | free(old);
|
---|
| 1323 | free(new);
|
---|
| 1324 | return;
|
---|
| 1325 | }
|
---|
[472c09d] | 1326 |
|
---|
[553492be] | 1327 | fibril_mutex_lock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1328 | old_node->lnkcnt--;
|
---|
[553492be] | 1329 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[230260ac] | 1330 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[a8e9ab8d] | 1331 | vfs_node_put(old_node);
|
---|
[472c09d] | 1332 |
|
---|
[a8e9ab8d] | 1333 | if (new_node)
|
---|
| 1334 | vfs_node_put(new_node);
|
---|
[472c09d] | 1335 |
|
---|
[a8e9ab8d] | 1336 | free(old);
|
---|
| 1337 | free(new);
|
---|
[ffa2c8ef] | 1338 | async_answer_0(rid, EOK);
|
---|
[a8e9ab8d] | 1339 | }
|
---|
| 1340 |
|
---|
[2b88074b] | 1341 | void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1342 | {
|
---|
| 1343 | int oldfd = IPC_GET_ARG1(*request);
|
---|
| 1344 | int newfd = IPC_GET_ARG2(*request);
|
---|
| 1345 |
|
---|
[4fe94c66] | 1346 | /* If the file descriptors are the same, do nothing. */
|
---|
| 1347 | if (oldfd == newfd) {
|
---|
[ffa2c8ef] | 1348 | async_answer_1(rid, EOK, newfd);
|
---|
[4fe94c66] | 1349 | return;
|
---|
| 1350 | }
|
---|
| 1351 |
|
---|
[2b88074b] | 1352 | /* Lookup the file structure corresponding to oldfd. */
|
---|
| 1353 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
| 1354 | if (!oldfile) {
|
---|
[ffa2c8ef] | 1355 | async_answer_0(rid, EBADF);
|
---|
[2b88074b] | 1356 | return;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 | /*
|
---|
| 1360 | * Lock the open file structure so that no other thread can manipulate
|
---|
| 1361 | * the same open file at a time.
|
---|
| 1362 | */
|
---|
| 1363 | fibril_mutex_lock(&oldfile->lock);
|
---|
| 1364 |
|
---|
| 1365 | /* Lookup an open file structure possibly corresponding to newfd. */
|
---|
| 1366 | vfs_file_t *newfile = vfs_file_get(newfd);
|
---|
| 1367 | if (newfile) {
|
---|
| 1368 | /* Close the originally opened file. */
|
---|
| 1369 | int ret = vfs_close_internal(newfile);
|
---|
| 1370 | if (ret != EOK) {
|
---|
[1882525] | 1371 | fibril_mutex_unlock(&oldfile->lock);
|
---|
[4fe94c66] | 1372 | vfs_file_put(oldfile);
|
---|
| 1373 | vfs_file_put(newfile);
|
---|
[ffa2c8ef] | 1374 | async_answer_0(rid, ret);
|
---|
[2b88074b] | 1375 | return;
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
| 1378 | ret = vfs_fd_free(newfd);
|
---|
| 1379 | if (ret != EOK) {
|
---|
[1882525] | 1380 | fibril_mutex_unlock(&oldfile->lock);
|
---|
[4fe94c66] | 1381 | vfs_file_put(oldfile);
|
---|
| 1382 | vfs_file_put(newfile);
|
---|
[ffa2c8ef] | 1383 | async_answer_0(rid, ret);
|
---|
[2b88074b] | 1384 | return;
|
---|
| 1385 | }
|
---|
[4fe94c66] | 1386 | vfs_file_put(newfile);
|
---|
[2b88074b] | 1387 | }
|
---|
| 1388 |
|
---|
| 1389 | /* Assign the old file to newfd. */
|
---|
| 1390 | int ret = vfs_fd_assign(oldfile, newfd);
|
---|
| 1391 | fibril_mutex_unlock(&oldfile->lock);
|
---|
[4fe94c66] | 1392 | vfs_file_put(oldfile);
|
---|
[2b88074b] | 1393 |
|
---|
| 1394 | if (ret != EOK)
|
---|
[ffa2c8ef] | 1395 | async_answer_0(rid, ret);
|
---|
[2b88074b] | 1396 | else
|
---|
[ffa2c8ef] | 1397 | async_answer_1(rid, EOK, newfd);
|
---|
[2b88074b] | 1398 | }
|
---|
| 1399 |
|
---|
[861e7d1] | 1400 | /**
|
---|
| 1401 | * @}
|
---|
[05b9912] | 1402 | */
|
---|