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