| [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>
|
|---|
| [3e6a98c5] | 46 | #include <stdbool.h>
|
|---|
| [1e4cada] | 47 | #include <fibril_synch.h>
|
|---|
| [d9c8c81] | 48 | #include <adt/list.h>
|
|---|
| [861e7d1] | 49 | #include <ctype.h>
|
|---|
| 50 | #include <assert.h>
|
|---|
| [a8e9ab8d] | 51 | #include <vfs/canonify.h>
|
|---|
| [861e7d1] | 52 |
|
|---|
| [7fe1f75] | 53 | /* Forward declarations of static functions. */
|
|---|
| [15f3c3f] | 54 | static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
|
|---|
| [8df8415] | 55 | aoff64_t);
|
|---|
| [7fe1f75] | 56 |
|
|---|
| [861e7d1] | 57 | /**
|
|---|
| 58 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
|---|
| 59 | * concurrent VFS operation which modifies the file system namespace.
|
|---|
| 60 | */
|
|---|
| [230260ac] | 61 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
|
|---|
| [861e7d1] | 62 |
|
|---|
| [0d35511] | 63 | static size_t shared_path(char *a, char *b)
|
|---|
| 64 | {
|
|---|
| 65 | size_t res = 0;
|
|---|
| 66 |
|
|---|
| [61042de] | 67 | while (a[res] == b[res] && a[res] != 0)
|
|---|
| [0d35511] | 68 | res++;
|
|---|
| 69 |
|
|---|
| [61042de] | 70 | if (a[res] == b[res])
|
|---|
| [0d35511] | 71 | return res;
|
|---|
| 72 |
|
|---|
| 73 | res--;
|
|---|
| [61042de] | 74 | while (a[res] != '/')
|
|---|
| [0d35511] | 75 | res--;
|
|---|
| 76 | return res;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /* This call destroys the file if and only if there are no hard links left. */
|
|---|
| 80 | static void out_destroy(vfs_triplet_t *file)
|
|---|
| 81 | {
|
|---|
| 82 | async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
|
|---|
| [61042de] | 83 | async_msg_2(exch, VFS_OUT_DESTROY, (sysarg_t) file->service_id,
|
|---|
| 84 | (sysarg_t) file->index);
|
|---|
| [0d35511] | 85 | vfs_exchange_release(exch);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| [fcab7ef] | 88 | int vfs_op_clone(int oldfd, int newfd, bool desc)
|
|---|
| [0d35511] | 89 | {
|
|---|
| [fcab7ef] | 90 | int rc;
|
|---|
| 91 |
|
|---|
| 92 | /* If the file descriptors are the same, do nothing. */
|
|---|
| 93 | if (oldfd == newfd)
|
|---|
| 94 | return EOK;
|
|---|
| 95 |
|
|---|
| [0d35511] | 96 | /* Lookup the file structure corresponding to fd. */
|
|---|
| 97 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
|---|
| [61042de] | 98 | if (oldfile == NULL)
|
|---|
| [0d35511] | 99 | return EBADF;
|
|---|
| [61042de] | 100 |
|
|---|
| [0d35511] | 101 | assert(oldfile->node != NULL);
|
|---|
| [fcab7ef] | 102 |
|
|---|
| 103 | if (newfd != -1) {
|
|---|
| 104 | /* Make sure newfd is closed. */
|
|---|
| 105 | (void) vfs_fd_free(newfd);
|
|---|
| 106 | /* Assign the old file to newfd. */
|
|---|
| 107 | rc = vfs_fd_assign(oldfile, newfd);
|
|---|
| 108 | } else {
|
|---|
| 109 | vfs_file_t *newfile;
|
|---|
| 110 | int newfd = vfs_fd_alloc(&newfile, desc);
|
|---|
| 111 | if (newfd >= 0) {
|
|---|
| 112 | newfile->node = oldfile->node;
|
|---|
| 113 | newfile->permissions = oldfile->permissions;
|
|---|
| 114 | vfs_node_addref(newfile->node);
|
|---|
| [0d35511] | 115 |
|
|---|
| [fcab7ef] | 116 | vfs_file_put(newfile);
|
|---|
| 117 | }
|
|---|
| 118 | rc = newfd;
|
|---|
| [0d35511] | 119 | }
|
|---|
| 120 | vfs_file_put(oldfile);
|
|---|
| 121 |
|
|---|
| [fcab7ef] | 122 | return rc;
|
|---|
| [0d35511] | 123 | }
|
|---|
| 124 |
|
|---|
| [9c4cf0d] | 125 | int vfs_op_put(int fd)
|
|---|
| [0d35511] | 126 | {
|
|---|
| 127 | return vfs_fd_free(fd);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static int vfs_connect_internal(service_id_t service_id, unsigned flags,
|
|---|
| 131 | unsigned instance, const char *options, const char *fsname,
|
|---|
| 132 | vfs_node_t **root)
|
|---|
| [861e7d1] | 133 | {
|
|---|
| [4636a60] | 134 | fs_handle_t fs_handle = 0;
|
|---|
| [05b9912] | 135 |
|
|---|
| [4636a60] | 136 | fibril_mutex_lock(&fs_list_lock);
|
|---|
| [61042de] | 137 | while (true) {
|
|---|
| [4636a60] | 138 | fs_handle = fs_name_to_handle(instance, fsname, false);
|
|---|
| [8dc72b64] | 139 |
|
|---|
| [61042de] | 140 | if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING))
|
|---|
| [4636a60] | 141 | break;
|
|---|
| [8dc72b64] | 142 |
|
|---|
| [4636a60] | 143 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
|---|
| 144 | }
|
|---|
| 145 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| 146 |
|
|---|
| [61042de] | 147 | if (fs_handle == 0)
|
|---|
| [b14d9f9] | 148 | return ENOFS;
|
|---|
| [5bcd5b7] | 149 |
|
|---|
| [4636a60] | 150 | /* Tell the mountee that it is being mounted. */
|
|---|
| 151 | ipc_call_t answer;
|
|---|
| 152 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
|---|
| [0d35511] | 153 | aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
|
|---|
| 154 | &answer);
|
|---|
| [4636a60] | 155 | /* Send the mount options */
|
|---|
| 156 | sysarg_t rc = async_data_write_start(exch, options, str_size(options));
|
|---|
| 157 | if (rc != EOK) {
|
|---|
| 158 | async_forget(msg);
|
|---|
| 159 | vfs_exchange_release(exch);
|
|---|
| 160 | return rc;
|
|---|
| [5bcd5b7] | 161 | }
|
|---|
| [c990ee6] | 162 |
|
|---|
| [4636a60] | 163 | async_wait_for(msg, &rc);
|
|---|
| [c990ee6] | 164 | if (rc != EOK) {
|
|---|
| 165 | vfs_exchange_release(exch);
|
|---|
| [4636a60] | 166 | return rc;
|
|---|
| [c990ee6] | 167 | }
|
|---|
| [5bcd5b7] | 168 |
|
|---|
| [4636a60] | 169 | vfs_lookup_res_t res;
|
|---|
| 170 | res.triplet.fs_handle = fs_handle;
|
|---|
| 171 | res.triplet.service_id = service_id;
|
|---|
| 172 | res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
|
|---|
| [0d35511] | 173 | res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
|---|
| 174 | IPC_GET_ARG3(answer));
|
|---|
| [4636a60] | 175 | res.type = VFS_NODE_DIRECTORY;
|
|---|
| [5bcd5b7] | 176 |
|
|---|
| [4636a60] | 177 | /* Add reference to the mounted root. */
|
|---|
| 178 | *root = vfs_node_get(&res);
|
|---|
| [c990ee6] | 179 | if (!*root) {
|
|---|
| 180 | aid_t msg = async_send_1(exch, VFS_OUT_UNMOUNTED,
|
|---|
| 181 | (sysarg_t) service_id, NULL);
|
|---|
| 182 | async_forget(msg);
|
|---|
| 183 | vfs_exchange_release(exch);
|
|---|
| 184 | return ENOMEM;
|
|---|
| 185 | }
|
|---|
| [8dc72b64] | 186 |
|
|---|
| [c990ee6] | 187 | vfs_exchange_release(exch);
|
|---|
| 188 |
|
|---|
| [4636a60] | 189 | return EOK;
|
|---|
| [5bcd5b7] | 190 | }
|
|---|
| 191 |
|
|---|
| [d2c8533] | 192 | int vfs_op_fsprobe(const char *fs_name, service_id_t sid,
|
|---|
| 193 | vfs_fs_probe_info_t *info)
|
|---|
| 194 | {
|
|---|
| 195 | fs_handle_t fs_handle = 0;
|
|---|
| 196 | sysarg_t rc;
|
|---|
| 197 | int retval;
|
|---|
| 198 |
|
|---|
| 199 | fibril_mutex_lock(&fs_list_lock);
|
|---|
| 200 | fs_handle = fs_name_to_handle(0, fs_name, false);
|
|---|
| 201 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| 202 |
|
|---|
| 203 | if (fs_handle == 0)
|
|---|
| 204 | return ENOFS;
|
|---|
| 205 |
|
|---|
| 206 | /* Send probe request to the file system server */
|
|---|
| 207 | ipc_call_t answer;
|
|---|
| 208 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
|---|
| 209 | aid_t msg = async_send_1(exch, VFS_OUT_FSPROBE, (sysarg_t) sid,
|
|---|
| 210 | &answer);
|
|---|
| 211 | if (msg == 0)
|
|---|
| 212 | return EINVAL;
|
|---|
| 213 |
|
|---|
| 214 | /* Read probe information */
|
|---|
| 215 | retval = async_data_read_start(exch, info, sizeof(*info));
|
|---|
| 216 | if (retval != EOK) {
|
|---|
| 217 | async_forget(msg);
|
|---|
| 218 | return retval;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | async_wait_for(msg, &rc);
|
|---|
| 222 | vfs_exchange_release(exch);
|
|---|
| 223 | return rc;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| [0d35511] | 226 | int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
|
|---|
| 227 | unsigned instance, const char *opts, const char *fs_name, int *outfd)
|
|---|
| [8dc72b64] | 228 | {
|
|---|
| [0d35511] | 229 | int rc;
|
|---|
| [5126f80] | 230 | vfs_file_t *mp = NULL;
|
|---|
| 231 | vfs_file_t *file = NULL;
|
|---|
| 232 | int fd = -1;
|
|---|
| 233 |
|
|---|
| 234 | if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
|---|
| 235 | mp = vfs_file_get(mpfd);
|
|---|
| 236 | if (mp == NULL) {
|
|---|
| 237 | rc = EBADF;
|
|---|
| 238 | goto out;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | if (mp->node->mount != NULL) {
|
|---|
| 242 | rc = EBUSY;
|
|---|
| 243 | goto out;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | if (mp->node->type != VFS_NODE_DIRECTORY) {
|
|---|
| 247 | rc = ENOTDIR;
|
|---|
| 248 | goto out;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | if (vfs_node_has_children(mp->node)) {
|
|---|
| 252 | rc = ENOTEMPTY;
|
|---|
| 253 | goto out;
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if (!(flags & VFS_MOUNT_NO_REF)) {
|
|---|
| 258 | fd = vfs_fd_alloc(&file, false);
|
|---|
| 259 | if (fd < 0) {
|
|---|
| 260 | rc = fd;
|
|---|
| 261 | goto out;
|
|---|
| 262 | }
|
|---|
| [c08c355] | 263 | }
|
|---|
| [4636a60] | 264 |
|
|---|
| [5126f80] | 265 | vfs_node_t *root = NULL;
|
|---|
| 266 |
|
|---|
| [4636a60] | 267 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| [6f9ef87a] | 268 |
|
|---|
| [0d35511] | 269 | rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
|
|---|
| 270 | &root);
|
|---|
| [5126f80] | 271 | if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
|---|
| 272 | vfs_node_addref(mp->node);
|
|---|
| 273 | vfs_node_addref(root);
|
|---|
| 274 | mp->node->mount = root;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 278 |
|
|---|
| [61042de] | 279 | if (rc != EOK)
|
|---|
| [5126f80] | 280 | goto out;
|
|---|
| 281 |
|
|---|
| 282 | if (flags & VFS_MOUNT_NO_REF) {
|
|---|
| 283 | vfs_node_delref(root);
|
|---|
| 284 | } else {
|
|---|
| 285 | assert(file != NULL);
|
|---|
| 286 |
|
|---|
| 287 | file->node = root;
|
|---|
| 288 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
|---|
| 289 | file->open_read = false;
|
|---|
| 290 | file->open_write = false;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | out:
|
|---|
| [61042de] | 294 | if (mp)
|
|---|
| [5126f80] | 295 | vfs_file_put(mp);
|
|---|
| [61042de] | 296 | if (file)
|
|---|
| [5126f80] | 297 | vfs_file_put(file);
|
|---|
| [61042de] | 298 |
|
|---|
| [5126f80] | 299 | if (rc != EOK && fd >= 0) {
|
|---|
| 300 | vfs_fd_free(fd);
|
|---|
| [0d35511] | 301 | fd = 0;
|
|---|
| [5126f80] | 302 | }
|
|---|
| [0d35511] | 303 |
|
|---|
| 304 | *outfd = fd;
|
|---|
| 305 | return rc;
|
|---|
| [8dc72b64] | 306 | }
|
|---|
| 307 |
|
|---|
| [b19e892] | 308 | int vfs_op_open(int fd, int mode)
|
|---|
| [861e7d1] | 309 | {
|
|---|
| [b19e892] | 310 | if (mode == 0)
|
|---|
| [0d35511] | 311 | return EINVAL;
|
|---|
| 312 |
|
|---|
| 313 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| [61042de] | 314 | if (!file)
|
|---|
| [0d35511] | 315 | return EBADF;
|
|---|
| 316 |
|
|---|
| [b19e892] | 317 | if ((mode & ~file->permissions) != 0) {
|
|---|
| [0d35511] | 318 | vfs_file_put(file);
|
|---|
| 319 | return EPERM;
|
|---|
| [0b18364] | 320 | }
|
|---|
| [0d35511] | 321 |
|
|---|
| 322 | if (file->open_read || file->open_write) {
|
|---|
| 323 | vfs_file_put(file);
|
|---|
| 324 | return EBUSY;
|
|---|
| [cb65bbe] | 325 | }
|
|---|
| 326 |
|
|---|
| [b19e892] | 327 | file->open_read = (mode & MODE_READ) != 0;
|
|---|
| 328 | file->open_write = (mode & (MODE_WRITE | MODE_APPEND)) != 0;
|
|---|
| 329 | file->append = (mode & MODE_APPEND) != 0;
|
|---|
| [05b9912] | 330 |
|
|---|
| [cb65bbe] | 331 | if (!file->open_read && !file->open_write) {
|
|---|
| 332 | vfs_file_put(file);
|
|---|
| [0d35511] | 333 | return EINVAL;
|
|---|
| [6f2c1ff] | 334 | }
|
|---|
| [05b9912] | 335 |
|
|---|
| [cb65bbe] | 336 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
|---|
| 337 | file->open_read = file->open_write = false;
|
|---|
| 338 | vfs_file_put(file);
|
|---|
| [0d35511] | 339 | return EINVAL;
|
|---|
| [7fe1f75] | 340 | }
|
|---|
| [05b9912] | 341 |
|
|---|
| [cb65bbe] | 342 | int rc = vfs_open_node_remote(file->node);
|
|---|
| 343 | if (rc != EOK) {
|
|---|
| 344 | file->open_read = file->open_write = false;
|
|---|
| 345 | vfs_file_put(file);
|
|---|
| [0d35511] | 346 | return rc;
|
|---|
| [05b9912] | 347 | }
|
|---|
| 348 |
|
|---|
| [4fe94c66] | 349 | vfs_file_put(file);
|
|---|
| [0d35511] | 350 | return EOK;
|
|---|
| [05b9912] | 351 | }
|
|---|
| 352 |
|
|---|
| [58898d1d] | 353 | typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, aoff64_t,
|
|---|
| 354 | ipc_call_t *, bool, void *);
|
|---|
| [42d08592] | 355 |
|
|---|
| [58898d1d] | 356 | static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
|---|
| [42d08592] | 357 | ipc_call_t *answer, bool read, void *data)
|
|---|
| 358 | {
|
|---|
| [e503517a] | 359 | size_t *bytes = (size_t *) data;
|
|---|
| 360 | int rc;
|
|---|
| 361 |
|
|---|
| [42d08592] | 362 | /*
|
|---|
| 363 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
|---|
| 364 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
|---|
| 365 | * destination FS server. The call will be routed as if sent by
|
|---|
| 366 | * ourselves. Note that call arguments are immutable in this case so we
|
|---|
| 367 | * don't have to bother.
|
|---|
| 368 | */
|
|---|
| 369 |
|
|---|
| 370 | if (read) {
|
|---|
| [e503517a] | 371 | rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
|
|---|
| [42d08592] | 372 | file->node->service_id, file->node->index,
|
|---|
| [58898d1d] | 373 | LOWER32(pos), UPPER32(pos), answer);
|
|---|
| [42d08592] | 374 | } else {
|
|---|
| [e503517a] | 375 | rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
|
|---|
| [42d08592] | 376 | file->node->service_id, file->node->index,
|
|---|
| [58898d1d] | 377 | LOWER32(pos), UPPER32(pos), answer);
|
|---|
| [e503517a] | 378 | }
|
|---|
| 379 |
|
|---|
| 380 | *bytes = IPC_GET_ARG1(*answer);
|
|---|
| 381 | return rc;
|
|---|
| [42d08592] | 382 | }
|
|---|
| [e503517a] | 383 |
|
|---|
| [58898d1d] | 384 | static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
|---|
| [e503517a] | 385 | ipc_call_t *answer, bool read, void *data)
|
|---|
| 386 | {
|
|---|
| 387 | rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
|
|---|
| 388 |
|
|---|
| 389 | if (exch == NULL)
|
|---|
| 390 | return ENOENT;
|
|---|
| [42d08592] | 391 |
|
|---|
| [e503517a] | 392 | aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
|
|---|
| [58898d1d] | 393 | file->node->service_id, file->node->index, LOWER32(pos),
|
|---|
| 394 | UPPER32(pos), answer);
|
|---|
| [e503517a] | 395 | if (msg == 0)
|
|---|
| 396 | return EINVAL;
|
|---|
| 397 |
|
|---|
| 398 | int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
|
|---|
| 399 | if (retval != EOK) {
|
|---|
| 400 | async_forget(msg);
|
|---|
| 401 | return retval;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | sysarg_t rc;
|
|---|
| 405 | async_wait_for(msg, &rc);
|
|---|
| 406 |
|
|---|
| 407 | chunk->size = IPC_GET_ARG1(*answer);
|
|---|
| 408 |
|
|---|
| 409 | return (int) rc;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| [58898d1d] | 412 | static int vfs_rdwr(int fd, aoff64_t pos, bool read, rdwr_ipc_cb_t ipc_cb,
|
|---|
| 413 | void *ipc_cb_data)
|
|---|
| [861e7d1] | 414 | {
|
|---|
| 415 | /*
|
|---|
| 416 | * The following code strongly depends on the fact that the files data
|
|---|
| 417 | * structure can be only accessed by a single fibril and all file
|
|---|
| 418 | * operations are serialized (i.e. the reads and writes cannot
|
|---|
| 419 | * interleave and a file cannot be closed while it is being read).
|
|---|
| 420 | *
|
|---|
| 421 | * Additional synchronization needs to be added once the table of
|
|---|
| 422 | * open files supports parallel access!
|
|---|
| 423 | */
|
|---|
| [79ae36dd] | 424 |
|
|---|
| [72bde81] | 425 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| [861e7d1] | 426 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| [e503517a] | 427 | if (!file)
|
|---|
| [354b642] | 428 | return EBADF;
|
|---|
| [6c89f20d] | 429 |
|
|---|
| [cb65bbe] | 430 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
|---|
| [c577a9a] | 431 | vfs_file_put(file);
|
|---|
| [1dff985] | 432 | return EINVAL;
|
|---|
| [cb65bbe] | 433 | }
|
|---|
| 434 |
|
|---|
| [79ae36dd] | 435 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
|---|
| 436 | assert(fs_info);
|
|---|
| 437 |
|
|---|
| [0d35511] | 438 | bool rlock = read ||
|
|---|
| 439 | (fs_info->concurrent_read_write && fs_info->write_retains_size);
|
|---|
| [354b642] | 440 |
|
|---|
| [861e7d1] | 441 | /*
|
|---|
| 442 | * Lock the file's node so that no other client can read/write to it at
|
|---|
| [c2f4b6b] | 443 | * the same time unless the FS supports concurrent reads/writes and its
|
|---|
| 444 | * write implementation does not modify the file size.
|
|---|
| [861e7d1] | 445 | */
|
|---|
| [61042de] | 446 | if (rlock)
|
|---|
| [230260ac] | 447 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
|---|
| [61042de] | 448 | else
|
|---|
| [230260ac] | 449 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
|---|
| [79ae36dd] | 450 |
|
|---|
| [b17186d] | 451 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
|---|
| 452 | /*
|
|---|
| 453 | * Make sure that no one is modifying the namespace
|
|---|
| 454 | * while we are in readdir().
|
|---|
| 455 | */
|
|---|
| [354b642] | 456 |
|
|---|
| 457 | if (!read) {
|
|---|
| 458 | if (rlock) {
|
|---|
| [0d35511] | 459 | fibril_rwlock_read_unlock(
|
|---|
| 460 | &file->node->contents_rwlock);
|
|---|
| [354b642] | 461 | } else {
|
|---|
| [0d35511] | 462 | fibril_rwlock_write_unlock(
|
|---|
| 463 | &file->node->contents_rwlock);
|
|---|
| [354b642] | 464 | }
|
|---|
| 465 | vfs_file_put(file);
|
|---|
| 466 | return EINVAL;
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| [230260ac] | 469 | fibril_rwlock_read_lock(&namespace_rwlock);
|
|---|
| [b17186d] | 470 | }
|
|---|
| [6c89f20d] | 471 |
|
|---|
| [79ae36dd] | 472 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
|---|
| [861e7d1] | 473 |
|
|---|
| [51774cd] | 474 | if (!read && file->append)
|
|---|
| [58898d1d] | 475 | pos = file->node->size;
|
|---|
| [42d08592] | 476 |
|
|---|
| [861e7d1] | 477 | /*
|
|---|
| [42d08592] | 478 | * Handle communication with the endpoint FS.
|
|---|
| [861e7d1] | 479 | */
|
|---|
| [b4cbef1] | 480 | ipc_call_t answer;
|
|---|
| [58898d1d] | 481 | int rc = ipc_cb(fs_exch, file, pos, &answer, read, ipc_cb_data);
|
|---|
| [05b9912] | 482 |
|
|---|
| [79ae36dd] | 483 | vfs_exchange_release(fs_exch);
|
|---|
| [34ca870] | 484 |
|
|---|
| [61042de] | 485 | if (file->node->type == VFS_NODE_DIRECTORY)
|
|---|
| [230260ac] | 486 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
|---|
| [6c89f20d] | 487 |
|
|---|
| [72bde81] | 488 | /* Unlock the VFS node. */
|
|---|
| [354b642] | 489 | if (rlock) {
|
|---|
| [230260ac] | 490 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
|---|
| [354b642] | 491 | } else {
|
|---|
| [861e7d1] | 492 | /* Update the cached version of node's size. */
|
|---|
| [354b642] | 493 | if (rc == EOK) {
|
|---|
| [5bb9907] | 494 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
|---|
| 495 | IPC_GET_ARG3(answer));
|
|---|
| [354b642] | 496 | }
|
|---|
| [230260ac] | 497 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
|---|
| [861e7d1] | 498 | }
|
|---|
| [6c89f20d] | 499 |
|
|---|
| [4fe94c66] | 500 | vfs_file_put(file);
|
|---|
| 501 |
|
|---|
| [e503517a] | 502 | return rc;
|
|---|
| 503 | }
|
|---|
| [861e7d1] | 504 |
|
|---|
| [58898d1d] | 505 | int vfs_rdwr_internal(int fd, aoff64_t pos, bool read, rdwr_io_chunk_t *chunk)
|
|---|
| [e503517a] | 506 | {
|
|---|
| [58898d1d] | 507 | return vfs_rdwr(fd, pos, read, rdwr_ipc_internal, chunk);
|
|---|
| [e503517a] | 508 | }
|
|---|
| 509 |
|
|---|
| [58898d1d] | 510 | int vfs_op_read(int fd, aoff64_t pos, size_t *out_bytes)
|
|---|
| [861e7d1] | 511 | {
|
|---|
| [58898d1d] | 512 | return vfs_rdwr(fd, pos, true, rdwr_ipc_client, out_bytes);
|
|---|
| [861e7d1] | 513 | }
|
|---|
| 514 |
|
|---|
| [0d35511] | 515 | int vfs_op_rename(int basefd, char *old, char *new)
|
|---|
| [861e7d1] | 516 | {
|
|---|
| [0d35511] | 517 | vfs_file_t *base_file = vfs_file_get(basefd);
|
|---|
| [61042de] | 518 | if (!base_file)
|
|---|
| [0d35511] | 519 | return EBADF;
|
|---|
| [61042de] | 520 |
|
|---|
| [0d35511] | 521 | vfs_node_t *base = base_file->node;
|
|---|
| 522 | vfs_node_addref(base);
|
|---|
| 523 | vfs_file_put(base_file);
|
|---|
| 524 |
|
|---|
| 525 | vfs_lookup_res_t base_lr;
|
|---|
| 526 | vfs_lookup_res_t old_lr;
|
|---|
| 527 | vfs_lookup_res_t new_lr_orig;
|
|---|
| 528 | bool orig_unlinked = false;
|
|---|
| 529 |
|
|---|
| 530 | int rc;
|
|---|
| 531 |
|
|---|
| 532 | size_t shared = shared_path(old, new);
|
|---|
| 533 |
|
|---|
| 534 | /* Do not allow one path to be a prefix of the other. */
|
|---|
| 535 | if (old[shared] == 0 || new[shared] == 0) {
|
|---|
| [7f59d6c] | 536 | vfs_node_put(base);
|
|---|
| [0d35511] | 537 | return EINVAL;
|
|---|
| 538 | }
|
|---|
| 539 | assert(old[shared] == '/');
|
|---|
| 540 | assert(new[shared] == '/');
|
|---|
| 541 |
|
|---|
| 542 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| 543 |
|
|---|
| 544 | /* Resolve the shared portion of the path first. */
|
|---|
| 545 | if (shared != 0) {
|
|---|
| 546 | old[shared] = 0;
|
|---|
| 547 | rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
|
|---|
| 548 | if (rc != EOK) {
|
|---|
| [7f59d6c] | 549 | vfs_node_put(base);
|
|---|
| [0d35511] | 550 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 551 | return rc;
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | vfs_node_put(base);
|
|---|
| 555 | base = vfs_node_get(&base_lr);
|
|---|
| [c990ee6] | 556 | if (!base) {
|
|---|
| 557 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 558 | return ENOMEM;
|
|---|
| 559 | }
|
|---|
| [0d35511] | 560 | old[shared] = '/';
|
|---|
| 561 | old += shared;
|
|---|
| 562 | new += shared;
|
|---|
| 563 | }
|
|---|
| [7f59d6c] | 564 |
|
|---|
| [61042de] | 565 | rc = vfs_lookup_internal(base, old, L_DISABLE_MOUNTS, &old_lr);
|
|---|
| [7f59d6c] | 566 | if (rc != EOK) {
|
|---|
| 567 | vfs_node_put(base);
|
|---|
| 568 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 569 | return rc;
|
|---|
| 570 | }
|
|---|
| [0d35511] | 571 |
|
|---|
| 572 | rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
|
|---|
| 573 | &new_lr_orig);
|
|---|
| 574 | if (rc == EOK) {
|
|---|
| 575 | orig_unlinked = true;
|
|---|
| 576 | } else if (rc != ENOENT) {
|
|---|
| 577 | vfs_node_put(base);
|
|---|
| 578 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 579 | return rc;
|
|---|
| 580 | }
|
|---|
| [7f59d6c] | 581 |
|
|---|
| 582 | rc = vfs_link_internal(base, new, &old_lr.triplet);
|
|---|
| [0d35511] | 583 | if (rc != EOK) {
|
|---|
| [7f59d6c] | 584 | vfs_link_internal(base, old, &old_lr.triplet);
|
|---|
| [61042de] | 585 | if (orig_unlinked)
|
|---|
| [0d35511] | 586 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
|---|
| 587 | vfs_node_put(base);
|
|---|
| 588 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 589 | return rc;
|
|---|
| 590 | }
|
|---|
| [7f59d6c] | 591 |
|
|---|
| 592 | rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
|
|---|
| 593 | &old_lr);
|
|---|
| [0d35511] | 594 | if (rc != EOK) {
|
|---|
| [61042de] | 595 | if (orig_unlinked)
|
|---|
| [0d35511] | 596 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
|---|
| 597 | vfs_node_put(base);
|
|---|
| 598 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 599 | return rc;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | /* If the node is not held by anyone, try to destroy it. */
|
|---|
| [4f9ab1e] | 603 | if (orig_unlinked) {
|
|---|
| 604 | vfs_node_t *node = vfs_node_peek(&new_lr_orig);
|
|---|
| 605 | if (!node)
|
|---|
| 606 | out_destroy(&new_lr_orig.triplet);
|
|---|
| 607 | else
|
|---|
| 608 | vfs_node_put(node);
|
|---|
| [0d35511] | 609 | }
|
|---|
| 610 |
|
|---|
| 611 | vfs_node_put(base);
|
|---|
| 612 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 613 | return EOK;
|
|---|
| [861e7d1] | 614 | }
|
|---|
| 615 |
|
|---|
| [67e881c] | 616 | int vfs_op_resize(int fd, int64_t size)
|
|---|
| 617 | {
|
|---|
| 618 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 619 | if (!file)
|
|---|
| 620 | return EBADF;
|
|---|
| 621 |
|
|---|
| 622 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
|---|
| 623 |
|
|---|
| 624 | int rc = vfs_truncate_internal(file->node->fs_handle,
|
|---|
| 625 | file->node->service_id, file->node->index, size);
|
|---|
| 626 | if (rc == EOK)
|
|---|
| 627 | file->node->size = size;
|
|---|
| 628 |
|
|---|
| 629 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
|---|
| 630 | vfs_file_put(file);
|
|---|
| 631 | return rc;
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| [fe91f66] | 634 | int vfs_op_stat(int fd)
|
|---|
| 635 | {
|
|---|
| 636 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 637 | if (!file)
|
|---|
| 638 | return EBADF;
|
|---|
| 639 |
|
|---|
| 640 | vfs_node_t *node = file->node;
|
|---|
| 641 |
|
|---|
| 642 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
|---|
| 643 | int rc = async_data_read_forward_fast(exch, VFS_OUT_STAT,
|
|---|
| 644 | node->service_id, node->index, true, 0, NULL);
|
|---|
| 645 | vfs_exchange_release(exch);
|
|---|
| 646 |
|
|---|
| 647 | vfs_file_put(file);
|
|---|
| 648 | return rc;
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| [0d35511] | 651 | int vfs_op_statfs(int fd)
|
|---|
| [7fe1f75] | 652 | {
|
|---|
| [0ee4322] | 653 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| [35e81e2] | 654 | if (!file)
|
|---|
| [0d35511] | 655 | return EBADF;
|
|---|
| [0ee4322] | 656 |
|
|---|
| [0d35511] | 657 | vfs_node_t *node = file->node;
|
|---|
| 658 |
|
|---|
| 659 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
|---|
| [35e81e2] | 660 | int rc = async_data_read_forward_fast(exch, VFS_OUT_STATFS,
|
|---|
| 661 | node->service_id, node->index, false, 0, NULL);
|
|---|
| [0d35511] | 662 | vfs_exchange_release(exch);
|
|---|
| 663 |
|
|---|
| [4fe94c66] | 664 | vfs_file_put(file);
|
|---|
| [35e81e2] | 665 | return rc;
|
|---|
| [861e7d1] | 666 | }
|
|---|
| 667 |
|
|---|
| [0d35511] | 668 | int vfs_op_sync(int fd)
|
|---|
| [852b801] | 669 | {
|
|---|
| 670 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| [61042de] | 671 | if (!file)
|
|---|
| [0d35511] | 672 | return EBADF;
|
|---|
| 673 |
|
|---|
| 674 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
|---|
| [852b801] | 675 |
|
|---|
| 676 | aid_t msg;
|
|---|
| [0d35511] | 677 | ipc_call_t answer;
|
|---|
| 678 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
|---|
| [61042de] | 679 | file->node->index, &answer);
|
|---|
| [79ae36dd] | 680 |
|
|---|
| [0d35511] | 681 | vfs_exchange_release(fs_exch);
|
|---|
| [79ae36dd] | 682 |
|
|---|
| [0d35511] | 683 | sysarg_t rc;
|
|---|
| [852b801] | 684 | async_wait_for(msg, &rc);
|
|---|
| [79ae36dd] | 685 |
|
|---|
| [4fe94c66] | 686 | vfs_file_put(file);
|
|---|
| [0d35511] | 687 | return rc;
|
|---|
| 688 |
|
|---|
| [852b801] | 689 | }
|
|---|
| 690 |
|
|---|
| [0d35511] | 691 | static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
|---|
| 692 | fs_index_t index, aoff64_t size)
|
|---|
| [5bcd5b7] | 693 | {
|
|---|
| [0d35511] | 694 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
|---|
| 695 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
|---|
| 696 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
|---|
| 697 | UPPER32(size));
|
|---|
| 698 | vfs_exchange_release(exch);
|
|---|
| 699 |
|
|---|
| 700 | return (int) rc;
|
|---|
| [5bcd5b7] | 701 | }
|
|---|
| 702 |
|
|---|
| [79ea5af] | 703 | int vfs_op_unlink(int parentfd, int expectfd, char *path)
|
|---|
| [0d35511] | 704 | {
|
|---|
| 705 | int rc = EOK;
|
|---|
| [20c071d] | 706 | vfs_file_t *parent = NULL;
|
|---|
| 707 | vfs_file_t *expect = NULL;
|
|---|
| 708 |
|
|---|
| [61042de] | 709 | if (parentfd == expectfd)
|
|---|
| [0d35511] | 710 | return EINVAL;
|
|---|
| [472c09d] | 711 |
|
|---|
| [20c071d] | 712 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| 713 |
|
|---|
| [0d35511] | 714 | /*
|
|---|
| 715 | * Files are retrieved in order of file descriptors, to prevent
|
|---|
| 716 | * deadlock.
|
|---|
| 717 | */
|
|---|
| [5126f80] | 718 | if (parentfd < expectfd) {
|
|---|
| [20c071d] | 719 | parent = vfs_file_get(parentfd);
|
|---|
| 720 | if (!parent) {
|
|---|
| [5126f80] | 721 | rc = EBADF;
|
|---|
| [20c071d] | 722 | goto exit;
|
|---|
| 723 | }
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | if (expectfd >= 0) {
|
|---|
| 727 | expect = vfs_file_get(expectfd);
|
|---|
| 728 | if (!expect) {
|
|---|
| [0d35511] | 729 | rc = EBADF;
|
|---|
| [20c071d] | 730 | goto exit;
|
|---|
| 731 | }
|
|---|
| [c577a9a] | 732 | }
|
|---|
| 733 |
|
|---|
| [5126f80] | 734 | if (parentfd > expectfd) {
|
|---|
| [c577a9a] | 735 | parent = vfs_file_get(parentfd);
|
|---|
| 736 | if (!parent) {
|
|---|
| [5126f80] | 737 | rc = EBADF;
|
|---|
| [c577a9a] | 738 | goto exit;
|
|---|
| 739 | }
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| [5126f80] | 742 | assert(parent != NULL);
|
|---|
| [c577a9a] | 743 |
|
|---|
| 744 | if (expectfd >= 0) {
|
|---|
| [20c071d] | 745 | vfs_lookup_res_t lr;
|
|---|
| [79ea5af] | 746 | rc = vfs_lookup_internal(parent->node, path, 0, &lr);
|
|---|
| [61042de] | 747 | if (rc != EOK)
|
|---|
| [20c071d] | 748 | goto exit;
|
|---|
| 749 |
|
|---|
| [4f9ab1e] | 750 | vfs_node_t *found_node = vfs_node_peek(&lr);
|
|---|
| 751 | vfs_node_put(found_node);
|
|---|
| [a274a5f] | 752 | if (expect->node != found_node) {
|
|---|
| [20c071d] | 753 | rc = ENOENT;
|
|---|
| 754 | goto exit;
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | vfs_file_put(expect);
|
|---|
| 758 | expect = NULL;
|
|---|
| 759 | }
|
|---|
| 760 |
|
|---|
| [415c7e0d] | 761 | vfs_lookup_res_t lr;
|
|---|
| [79ea5af] | 762 | rc = vfs_lookup_internal(parent->node, path, L_UNLINK, &lr);
|
|---|
| [61042de] | 763 | if (rc != EOK)
|
|---|
| [20c071d] | 764 | goto exit;
|
|---|
| 765 |
|
|---|
| [5bcd5b7] | 766 | /* If the node is not held by anyone, try to destroy it. */
|
|---|
| [4f9ab1e] | 767 | vfs_node_t *node = vfs_node_peek(&lr);
|
|---|
| 768 | if (!node)
|
|---|
| [5bcd5b7] | 769 | out_destroy(&lr.triplet);
|
|---|
| [4f9ab1e] | 770 | else
|
|---|
| 771 | vfs_node_put(node);
|
|---|
| [415c7e0d] | 772 |
|
|---|
| [20c071d] | 773 | exit:
|
|---|
| [61042de] | 774 | if (path)
|
|---|
| [20c071d] | 775 | free(path);
|
|---|
| [61042de] | 776 | if (parent)
|
|---|
| [20c071d] | 777 | vfs_file_put(parent);
|
|---|
| [61042de] | 778 | if (expect)
|
|---|
| [20c071d] | 779 | vfs_file_put(expect);
|
|---|
| 780 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| [0d35511] | 781 | return rc;
|
|---|
| [20c071d] | 782 | }
|
|---|
| [415c7e0d] | 783 |
|
|---|
| [0d35511] | 784 | int vfs_op_unmount(int mpfd)
|
|---|
| [a8e9ab8d] | 785 | {
|
|---|
| [0d35511] | 786 | vfs_file_t *mp = vfs_file_get(mpfd);
|
|---|
| [61042de] | 787 | if (mp == NULL)
|
|---|
| [0d35511] | 788 | return EBADF;
|
|---|
| [472c09d] | 789 |
|
|---|
| [0d35511] | 790 | if (mp->node->mount == NULL) {
|
|---|
| 791 | vfs_file_put(mp);
|
|---|
| 792 | return ENOENT;
|
|---|
| [a8e9ab8d] | 793 | }
|
|---|
| [72bde81] | 794 |
|
|---|
| [230260ac] | 795 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| [472c09d] | 796 |
|
|---|
| [0d35511] | 797 | /*
|
|---|
| 798 | * Count the total number of references for the mounted file system. We
|
|---|
| 799 | * are expecting at least one, which is held by the mount point.
|
|---|
| 800 | * If we find more, it means that
|
|---|
| 801 | * the file system cannot be gracefully unmounted at the moment because
|
|---|
| 802 | * someone is working with it.
|
|---|
| 803 | */
|
|---|
| 804 | if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
|
|---|
| 805 | mp->node->mount->service_id) != 1) {
|
|---|
| 806 | vfs_file_put(mp);
|
|---|
| [230260ac] | 807 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| [0d35511] | 808 | return EBUSY;
|
|---|
| [a8e9ab8d] | 809 | }
|
|---|
| [472c09d] | 810 |
|
|---|
| [0d35511] | 811 | async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
|
|---|
| 812 | int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
|---|
| 813 | mp->node->mount->service_id);
|
|---|
| 814 | vfs_exchange_release(exch);
|
|---|
| [f15cf1a6] | 815 |
|
|---|
| 816 | if (rc != EOK) {
|
|---|
| [0d35511] | 817 | vfs_file_put(mp);
|
|---|
| [230260ac] | 818 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| [778d26d] | 819 | return rc;
|
|---|
| [f15cf1a6] | 820 | }
|
|---|
| [472c09d] | 821 |
|
|---|
| [0d35511] | 822 | vfs_node_forget(mp->node->mount);
|
|---|
| 823 | vfs_node_put(mp->node);
|
|---|
| 824 | mp->node->mount = NULL;
|
|---|
| [472c09d] | 825 |
|
|---|
| [230260ac] | 826 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| [0d35511] | 827 |
|
|---|
| 828 | vfs_file_put(mp);
|
|---|
| [778d26d] | 829 | return EOK;
|
|---|
| [f15cf1a6] | 830 | }
|
|---|
| 831 |
|
|---|
| [0d35511] | 832 | int vfs_op_wait_handle(bool high_fd)
|
|---|
| [a8e9ab8d] | 833 | {
|
|---|
| [0d35511] | 834 | return vfs_wait_handle_internal(high_fd);
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | static inline bool walk_flags_valid(int flags)
|
|---|
| 838 | {
|
|---|
| [61042de] | 839 | if ((flags & ~WALK_ALL_FLAGS) != 0)
|
|---|
| [0d35511] | 840 | return false;
|
|---|
| [61042de] | 841 | if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE))
|
|---|
| [0d35511] | 842 | return false;
|
|---|
| [61042de] | 843 | if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY))
|
|---|
| [0d35511] | 844 | return false;
|
|---|
| [4809715] | 845 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
|
|---|
| [61042de] | 846 | if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR))
|
|---|
| [0d35511] | 847 | return false;
|
|---|
| [a8e9ab8d] | 848 | }
|
|---|
| [0d35511] | 849 | return true;
|
|---|
| 850 | }
|
|---|
| [778d26d] | 851 |
|
|---|
| [0d35511] | 852 | static inline int walk_lookup_flags(int flags)
|
|---|
| 853 | {
|
|---|
| 854 | int lflags = 0;
|
|---|
| [61042de] | 855 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE))
|
|---|
| [0d35511] | 856 | lflags |= L_CREATE;
|
|---|
| [61042de] | 857 | if (flags & WALK_MUST_CREATE)
|
|---|
| [0d35511] | 858 | lflags |= L_EXCLUSIVE;
|
|---|
| [61042de] | 859 | if (flags & WALK_REGULAR)
|
|---|
| [0d35511] | 860 | lflags |= L_FILE;
|
|---|
| [61042de] | 861 | if (flags & WALK_DIRECTORY)
|
|---|
| [0d35511] | 862 | lflags |= L_DIRECTORY;
|
|---|
| [61042de] | 863 | if (flags & WALK_MOUNT_POINT)
|
|---|
| [0d35511] | 864 | lflags |= L_MP;
|
|---|
| 865 | return lflags;
|
|---|
| [a8e9ab8d] | 866 | }
|
|---|
| 867 |
|
|---|
| [0d35511] | 868 | int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
|
|---|
| [2b88074b] | 869 | {
|
|---|
| [61042de] | 870 | if (!walk_flags_valid(flags))
|
|---|
| [0d35511] | 871 | return EINVAL;
|
|---|
| [4fe94c66] | 872 |
|
|---|
| [0d35511] | 873 | vfs_file_t *parent = vfs_file_get(parentfd);
|
|---|
| [61042de] | 874 | if (!parent)
|
|---|
| [0d35511] | 875 | return EBADF;
|
|---|
| [2b88074b] | 876 |
|
|---|
| [0d35511] | 877 | fibril_rwlock_read_lock(&namespace_rwlock);
|
|---|
| [2b88074b] | 878 |
|
|---|
| [0d35511] | 879 | vfs_lookup_res_t lr;
|
|---|
| 880 | int rc = vfs_lookup_internal(parent->node, path,
|
|---|
| 881 | walk_lookup_flags(flags), &lr);
|
|---|
| 882 | if (rc != EOK) {
|
|---|
| 883 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 884 | vfs_file_put(parent);
|
|---|
| 885 | return rc;
|
|---|
| [76a67ce] | 886 | }
|
|---|
| [9dc6083] | 887 |
|
|---|
| [0d35511] | 888 | vfs_node_t *node = vfs_node_get(&lr);
|
|---|
| [c990ee6] | 889 | if (!node) {
|
|---|
| 890 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 891 | vfs_file_put(parent);
|
|---|
| 892 | return ENOMEM;
|
|---|
| 893 | }
|
|---|
| [9dc6083] | 894 |
|
|---|
| [0d35511] | 895 | vfs_file_t *file;
|
|---|
| 896 | int fd = vfs_fd_alloc(&file, false);
|
|---|
| 897 | if (fd < 0) {
|
|---|
| 898 | vfs_node_put(node);
|
|---|
| 899 | vfs_file_put(parent);
|
|---|
| 900 | return fd;
|
|---|
| 901 | }
|
|---|
| 902 | assert(file != NULL);
|
|---|
| [9dc6083] | 903 |
|
|---|
| [0d35511] | 904 | file->node = node;
|
|---|
| 905 | file->permissions = parent->permissions;
|
|---|
| 906 | file->open_read = false;
|
|---|
| 907 | file->open_write = false;
|
|---|
| [9dc6083] | 908 |
|
|---|
| [a737667e] | 909 | vfs_file_put(file);
|
|---|
| [0d35511] | 910 | vfs_file_put(parent);
|
|---|
| 911 |
|
|---|
| 912 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
|---|
| [9dc6083] | 913 |
|
|---|
| [0d35511] | 914 | *out_fd = fd;
|
|---|
| 915 | return EOK;
|
|---|
| [66366470] | 916 | }
|
|---|
| 917 |
|
|---|
| [58898d1d] | 918 | int vfs_op_write(int fd, aoff64_t pos, size_t *out_bytes)
|
|---|
| [354b642] | 919 | {
|
|---|
| [58898d1d] | 920 | return vfs_rdwr(fd, pos, false, rdwr_ipc_client, out_bytes);
|
|---|
| [354b642] | 921 | }
|
|---|
| 922 |
|
|---|
| [861e7d1] | 923 | /**
|
|---|
| 924 | * @}
|
|---|
| [05b9912] | 925 | */
|
|---|