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