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