[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 |
|
---|
| 620 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[a35b458] | 621 |
|
---|
[b7fd2a0] | 622 | errno_t rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
[67e881c] | 623 | file->node->service_id, file->node->index, size);
|
---|
| 624 | if (rc == EOK)
|
---|
| 625 | file->node->size = size;
|
---|
[a35b458] | 626 |
|
---|
[67e881c] | 627 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
| 628 | vfs_file_put(file);
|
---|
| 629 | return rc;
|
---|
| 630 | }
|
---|
| 631 |
|
---|
[b7fd2a0] | 632 | errno_t vfs_op_stat(int fd)
|
---|
[fe91f66] | 633 | {
|
---|
| 634 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 635 | if (!file)
|
---|
| 636 | return EBADF;
|
---|
| 637 |
|
---|
| 638 | vfs_node_t *node = file->node;
|
---|
| 639 |
|
---|
| 640 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
[4f13e19] | 641 | errno_t rc = async_data_read_forward_3_0(exch, VFS_OUT_STAT,
|
---|
| 642 | node->service_id, node->index, true);
|
---|
[fe91f66] | 643 | vfs_exchange_release(exch);
|
---|
[a35b458] | 644 |
|
---|
[fe91f66] | 645 | vfs_file_put(file);
|
---|
| 646 | return rc;
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[b7fd2a0] | 649 | errno_t vfs_op_statfs(int fd)
|
---|
[7fe1f75] | 650 | {
|
---|
[0ee4322] | 651 | vfs_file_t *file = vfs_file_get(fd);
|
---|
[35e81e2] | 652 | if (!file)
|
---|
[0d35511] | 653 | return EBADF;
|
---|
[0ee4322] | 654 |
|
---|
[0d35511] | 655 | vfs_node_t *node = file->node;
|
---|
| 656 |
|
---|
| 657 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
[4f13e19] | 658 | errno_t rc = async_data_read_forward_3_0(exch, VFS_OUT_STATFS,
|
---|
| 659 | node->service_id, node->index, false);
|
---|
[0d35511] | 660 | vfs_exchange_release(exch);
|
---|
| 661 |
|
---|
[4fe94c66] | 662 | vfs_file_put(file);
|
---|
[35e81e2] | 663 | return rc;
|
---|
[861e7d1] | 664 | }
|
---|
| 665 |
|
---|
[b7fd2a0] | 666 | errno_t vfs_op_sync(int fd)
|
---|
[852b801] | 667 | {
|
---|
| 668 | vfs_file_t *file = vfs_file_get(fd);
|
---|
[61042de] | 669 | if (!file)
|
---|
[0d35511] | 670 | return EBADF;
|
---|
[a35b458] | 671 |
|
---|
[0d35511] | 672 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
[a35b458] | 673 |
|
---|
[852b801] | 674 | aid_t msg;
|
---|
[0d35511] | 675 | ipc_call_t answer;
|
---|
| 676 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
---|
[61042de] | 677 | file->node->index, &answer);
|
---|
[a35b458] | 678 |
|
---|
[0d35511] | 679 | vfs_exchange_release(fs_exch);
|
---|
[a35b458] | 680 |
|
---|
[b7fd2a0] | 681 | errno_t rc;
|
---|
[852b801] | 682 | async_wait_for(msg, &rc);
|
---|
[a35b458] | 683 |
|
---|
[4fe94c66] | 684 | vfs_file_put(file);
|
---|
[0d35511] | 685 | return rc;
|
---|
[a35b458] | 686 |
|
---|
[852b801] | 687 | }
|
---|
| 688 |
|
---|
[b7fd2a0] | 689 | static errno_t vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
[0d35511] | 690 | fs_index_t index, aoff64_t size)
|
---|
[5bcd5b7] | 691 | {
|
---|
[0d35511] | 692 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
[b7fd2a0] | 693 | errno_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
[0d35511] | 694 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
| 695 | UPPER32(size));
|
---|
| 696 | vfs_exchange_release(exch);
|
---|
[a35b458] | 697 |
|
---|
[b7fd2a0] | 698 | return (errno_t) rc;
|
---|
[5bcd5b7] | 699 | }
|
---|
| 700 |
|
---|
[b7fd2a0] | 701 | errno_t vfs_op_unlink(int parentfd, int expectfd, char *path)
|
---|
[0d35511] | 702 | {
|
---|
[b7fd2a0] | 703 | errno_t rc = EOK;
|
---|
[20c071d] | 704 | vfs_file_t *parent = NULL;
|
---|
| 705 | vfs_file_t *expect = NULL;
|
---|
[a35b458] | 706 |
|
---|
[61042de] | 707 | if (parentfd == expectfd)
|
---|
[0d35511] | 708 | return EINVAL;
|
---|
[a35b458] | 709 |
|
---|
[20c071d] | 710 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[a35b458] | 711 |
|
---|
[1b20da0] | 712 | /*
|
---|
[0d35511] | 713 | * Files are retrieved in order of file descriptors, to prevent
|
---|
| 714 | * deadlock.
|
---|
| 715 | */
|
---|
[5126f80] | 716 | if (parentfd < expectfd) {
|
---|
[20c071d] | 717 | parent = vfs_file_get(parentfd);
|
---|
| 718 | if (!parent) {
|
---|
[5126f80] | 719 | rc = EBADF;
|
---|
[20c071d] | 720 | goto exit;
|
---|
| 721 | }
|
---|
| 722 | }
|
---|
[a35b458] | 723 |
|
---|
[20c071d] | 724 | if (expectfd >= 0) {
|
---|
| 725 | expect = vfs_file_get(expectfd);
|
---|
| 726 | if (!expect) {
|
---|
[0d35511] | 727 | rc = EBADF;
|
---|
[20c071d] | 728 | goto exit;
|
---|
| 729 | }
|
---|
[c577a9a] | 730 | }
|
---|
[a35b458] | 731 |
|
---|
[5126f80] | 732 | if (parentfd > expectfd) {
|
---|
[c577a9a] | 733 | parent = vfs_file_get(parentfd);
|
---|
| 734 | if (!parent) {
|
---|
[5126f80] | 735 | rc = EBADF;
|
---|
[c577a9a] | 736 | goto exit;
|
---|
| 737 | }
|
---|
| 738 | }
|
---|
[a35b458] | 739 |
|
---|
[5126f80] | 740 | assert(parent != NULL);
|
---|
[a35b458] | 741 |
|
---|
[c577a9a] | 742 | if (expectfd >= 0) {
|
---|
[20c071d] | 743 | vfs_lookup_res_t lr;
|
---|
[79ea5af] | 744 | rc = vfs_lookup_internal(parent->node, path, 0, &lr);
|
---|
[61042de] | 745 | if (rc != EOK)
|
---|
[20c071d] | 746 | goto exit;
|
---|
[a35b458] | 747 |
|
---|
[4f9ab1e] | 748 | vfs_node_t *found_node = vfs_node_peek(&lr);
|
---|
| 749 | vfs_node_put(found_node);
|
---|
[a274a5f] | 750 | if (expect->node != found_node) {
|
---|
[20c071d] | 751 | rc = ENOENT;
|
---|
| 752 | goto exit;
|
---|
| 753 | }
|
---|
[a35b458] | 754 |
|
---|
[20c071d] | 755 | vfs_file_put(expect);
|
---|
| 756 | expect = NULL;
|
---|
| 757 | }
|
---|
[a35b458] | 758 |
|
---|
[415c7e0d] | 759 | vfs_lookup_res_t lr;
|
---|
[79ea5af] | 760 | rc = vfs_lookup_internal(parent->node, path, L_UNLINK, &lr);
|
---|
[61042de] | 761 | if (rc != EOK)
|
---|
[20c071d] | 762 | goto exit;
|
---|
| 763 |
|
---|
[5bcd5b7] | 764 | /* If the node is not held by anyone, try to destroy it. */
|
---|
[4f9ab1e] | 765 | vfs_node_t *node = vfs_node_peek(&lr);
|
---|
| 766 | if (!node)
|
---|
[5bcd5b7] | 767 | out_destroy(&lr.triplet);
|
---|
[4f9ab1e] | 768 | else
|
---|
| 769 | vfs_node_put(node);
|
---|
[415c7e0d] | 770 |
|
---|
[20c071d] | 771 | exit:
|
---|
[61042de] | 772 | if (path)
|
---|
[20c071d] | 773 | free(path);
|
---|
[61042de] | 774 | if (parent)
|
---|
[20c071d] | 775 | vfs_file_put(parent);
|
---|
[61042de] | 776 | if (expect)
|
---|
[20c071d] | 777 | vfs_file_put(expect);
|
---|
| 778 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[0d35511] | 779 | return rc;
|
---|
[20c071d] | 780 | }
|
---|
[415c7e0d] | 781 |
|
---|
[b7fd2a0] | 782 | errno_t vfs_op_unmount(int mpfd)
|
---|
[a8e9ab8d] | 783 | {
|
---|
[0d35511] | 784 | vfs_file_t *mp = vfs_file_get(mpfd);
|
---|
[61042de] | 785 | if (mp == NULL)
|
---|
[0d35511] | 786 | return EBADF;
|
---|
[a35b458] | 787 |
|
---|
[0d35511] | 788 | if (mp->node->mount == NULL) {
|
---|
| 789 | vfs_file_put(mp);
|
---|
| 790 | return ENOENT;
|
---|
[a8e9ab8d] | 791 | }
|
---|
[a35b458] | 792 |
|
---|
[230260ac] | 793 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[a35b458] | 794 |
|
---|
[0d35511] | 795 | /*
|
---|
| 796 | * Count the total number of references for the mounted file system. We
|
---|
| 797 | * are expecting at least one, which is held by the mount point.
|
---|
| 798 | * If we find more, it means that
|
---|
| 799 | * the file system cannot be gracefully unmounted at the moment because
|
---|
| 800 | * someone is working with it.
|
---|
| 801 | */
|
---|
| 802 | if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
|
---|
| 803 | mp->node->mount->service_id) != 1) {
|
---|
| 804 | vfs_file_put(mp);
|
---|
[230260ac] | 805 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[0d35511] | 806 | return EBUSY;
|
---|
[a8e9ab8d] | 807 | }
|
---|
[a35b458] | 808 |
|
---|
[0d35511] | 809 | async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
|
---|
[b7fd2a0] | 810 | errno_t rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
[0d35511] | 811 | mp->node->mount->service_id);
|
---|
| 812 | vfs_exchange_release(exch);
|
---|
[a35b458] | 813 |
|
---|
[f15cf1a6] | 814 | if (rc != EOK) {
|
---|
[0d35511] | 815 | vfs_file_put(mp);
|
---|
[230260ac] | 816 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[778d26d] | 817 | return rc;
|
---|
[f15cf1a6] | 818 | }
|
---|
[a35b458] | 819 |
|
---|
[0d35511] | 820 | vfs_node_forget(mp->node->mount);
|
---|
| 821 | vfs_node_put(mp->node);
|
---|
| 822 | mp->node->mount = NULL;
|
---|
[a35b458] | 823 |
|
---|
[230260ac] | 824 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[a35b458] | 825 |
|
---|
[0d35511] | 826 | vfs_file_put(mp);
|
---|
[778d26d] | 827 | return EOK;
|
---|
[f15cf1a6] | 828 | }
|
---|
| 829 |
|
---|
[b7fd2a0] | 830 | errno_t vfs_op_wait_handle(bool high_fd, int *out_fd)
|
---|
[a8e9ab8d] | 831 | {
|
---|
[6ad454f] | 832 | return vfs_wait_handle_internal(high_fd, out_fd);
|
---|
[0d35511] | 833 | }
|
---|
| 834 |
|
---|
| 835 | static inline bool walk_flags_valid(int flags)
|
---|
| 836 | {
|
---|
[61042de] | 837 | if ((flags & ~WALK_ALL_FLAGS) != 0)
|
---|
[0d35511] | 838 | return false;
|
---|
[61042de] | 839 | if ((flags & WALK_MAY_CREATE) && (flags & WALK_MUST_CREATE))
|
---|
[0d35511] | 840 | return false;
|
---|
[61042de] | 841 | if ((flags & WALK_REGULAR) && (flags & WALK_DIRECTORY))
|
---|
[0d35511] | 842 | return false;
|
---|
[4809715] | 843 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE)) {
|
---|
[61042de] | 844 | if (!(flags & WALK_DIRECTORY) && !(flags & WALK_REGULAR))
|
---|
[0d35511] | 845 | return false;
|
---|
[a8e9ab8d] | 846 | }
|
---|
[0d35511] | 847 | return true;
|
---|
| 848 | }
|
---|
[778d26d] | 849 |
|
---|
[0d35511] | 850 | static inline int walk_lookup_flags(int flags)
|
---|
| 851 | {
|
---|
| 852 | int lflags = 0;
|
---|
[61042de] | 853 | if ((flags & WALK_MAY_CREATE) || (flags & WALK_MUST_CREATE))
|
---|
[0d35511] | 854 | lflags |= L_CREATE;
|
---|
[61042de] | 855 | if (flags & WALK_MUST_CREATE)
|
---|
[0d35511] | 856 | lflags |= L_EXCLUSIVE;
|
---|
[61042de] | 857 | if (flags & WALK_REGULAR)
|
---|
[0d35511] | 858 | lflags |= L_FILE;
|
---|
[61042de] | 859 | if (flags & WALK_DIRECTORY)
|
---|
[0d35511] | 860 | lflags |= L_DIRECTORY;
|
---|
[61042de] | 861 | if (flags & WALK_MOUNT_POINT)
|
---|
[0d35511] | 862 | lflags |= L_MP;
|
---|
| 863 | return lflags;
|
---|
[a8e9ab8d] | 864 | }
|
---|
| 865 |
|
---|
[b7fd2a0] | 866 | errno_t vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
|
---|
[2b88074b] | 867 | {
|
---|
[61042de] | 868 | if (!walk_flags_valid(flags))
|
---|
[0d35511] | 869 | return EINVAL;
|
---|
[a35b458] | 870 |
|
---|
[0d35511] | 871 | vfs_file_t *parent = vfs_file_get(parentfd);
|
---|
[61042de] | 872 | if (!parent)
|
---|
[0d35511] | 873 | return EBADF;
|
---|
[a35b458] | 874 |
|
---|
[0d35511] | 875 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[a35b458] | 876 |
|
---|
[0d35511] | 877 | vfs_lookup_res_t lr;
|
---|
[b7fd2a0] | 878 | errno_t rc = vfs_lookup_internal(parent->node, path,
|
---|
[0d35511] | 879 | walk_lookup_flags(flags), &lr);
|
---|
| 880 | if (rc != EOK) {
|
---|
| 881 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
| 882 | vfs_file_put(parent);
|
---|
| 883 | return rc;
|
---|
[76a67ce] | 884 | }
|
---|
[a35b458] | 885 |
|
---|
[0d35511] | 886 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[c990ee6] | 887 | if (!node) {
|
---|
| 888 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
| 889 | vfs_file_put(parent);
|
---|
| 890 | return ENOMEM;
|
---|
| 891 | }
|
---|
[a35b458] | 892 |
|
---|
[0d35511] | 893 | vfs_file_t *file;
|
---|
[6ad454f] | 894 | rc = vfs_fd_alloc(&file, false, out_fd);
|
---|
| 895 | if (rc != EOK) {
|
---|
[6371eb47] | 896 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[0d35511] | 897 | vfs_node_put(node);
|
---|
| 898 | vfs_file_put(parent);
|
---|
[6ad454f] | 899 | return rc;
|
---|
[0d35511] | 900 | }
|
---|
| 901 | assert(file != NULL);
|
---|
[a35b458] | 902 |
|
---|
[0d35511] | 903 | file->node = node;
|
---|
| 904 | file->permissions = parent->permissions;
|
---|
| 905 | file->open_read = false;
|
---|
| 906 | file->open_write = false;
|
---|
[a35b458] | 907 |
|
---|
[a737667e] | 908 | vfs_file_put(file);
|
---|
[0d35511] | 909 | vfs_file_put(parent);
|
---|
[a35b458] | 910 |
|
---|
[0d35511] | 911 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[9dc6083] | 912 |
|
---|
[0d35511] | 913 | return EOK;
|
---|
[66366470] | 914 | }
|
---|
| 915 |
|
---|
[b7fd2a0] | 916 | errno_t vfs_op_write(int fd, aoff64_t pos, size_t *out_bytes)
|
---|
[354b642] | 917 | {
|
---|
[58898d1d] | 918 | return vfs_rdwr(fd, pos, false, rdwr_ipc_client, out_bytes);
|
---|
[354b642] | 919 | }
|
---|
| 920 |
|
---|
[861e7d1] | 921 | /**
|
---|
| 922 | * @}
|
---|
[05b9912] | 923 | */
|
---|