[861e7d1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
[8dc72b64] | 31 | */
|
---|
[861e7d1] | 32 |
|
---|
| 33 | /**
|
---|
[8dc72b64] | 34 | * @file vfs_ops.c
|
---|
| 35 | * @brief Operations that VFS offers to its clients.
|
---|
[861e7d1] | 36 | */
|
---|
| 37 |
|
---|
[a8e9ab8d] | 38 | #include "vfs.h"
|
---|
[ed903174] | 39 | #include <macros.h>
|
---|
[9539be6] | 40 | #include <stdint.h>
|
---|
[861e7d1] | 41 | #include <async.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
[19f857a] | 45 | #include <str.h>
|
---|
[3e6a98c5] | 46 | #include <stdbool.h>
|
---|
[1e4cada] | 47 | #include <fibril_synch.h>
|
---|
[d9c8c81] | 48 | #include <adt/list.h>
|
---|
[861e7d1] | 49 | #include <ctype.h>
|
---|
| 50 | #include <assert.h>
|
---|
[a8e9ab8d] | 51 | #include <vfs/canonify.h>
|
---|
[861e7d1] | 52 |
|
---|
[7fe1f75] | 53 | /* Forward declarations of static functions. */
|
---|
[15f3c3f] | 54 | static int vfs_truncate_internal(fs_handle_t, service_id_t, fs_index_t,
|
---|
[8df8415] | 55 | aoff64_t);
|
---|
[7fe1f75] | 56 |
|
---|
[861e7d1] | 57 | /**
|
---|
| 58 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
| 59 | * concurrent VFS operation which modifies the file system namespace.
|
---|
| 60 | */
|
---|
[230260ac] | 61 | FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
[861e7d1] | 62 |
|
---|
[0d35511] | 63 | static size_t shared_path(char *a, char *b)
|
---|
| 64 | {
|
---|
| 65 | size_t res = 0;
|
---|
| 66 |
|
---|
[61042de] | 67 | while (a[res] == b[res] && a[res] != 0)
|
---|
[0d35511] | 68 | res++;
|
---|
| 69 |
|
---|
[61042de] | 70 | if (a[res] == b[res])
|
---|
[0d35511] | 71 | return res;
|
---|
| 72 |
|
---|
| 73 | res--;
|
---|
[61042de] | 74 | while (a[res] != '/')
|
---|
[0d35511] | 75 | res--;
|
---|
| 76 | return res;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /* This call destroys the file if and only if there are no hard links left. */
|
---|
| 80 | static void out_destroy(vfs_triplet_t *file)
|
---|
| 81 | {
|
---|
| 82 | async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
|
---|
[61042de] | 83 | async_msg_2(exch, VFS_OUT_DESTROY, (sysarg_t) file->service_id,
|
---|
| 84 | (sysarg_t) file->index);
|
---|
[0d35511] | 85 | vfs_exchange_release(exch);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[f77c1c9] | 88 | int vfs_op_clone(int oldfd, int newfd, bool desc, int *out_fd)
|
---|
[0d35511] | 89 | {
|
---|
[fcab7ef] | 90 | int rc;
|
---|
| 91 |
|
---|
| 92 | /* If the file descriptors are the same, do nothing. */
|
---|
| 93 | if (oldfd == newfd)
|
---|
| 94 | return EOK;
|
---|
| 95 |
|
---|
[0d35511] | 96 | /* Lookup the file structure corresponding to fd. */
|
---|
| 97 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
[61042de] | 98 | if (oldfile == NULL)
|
---|
[0d35511] | 99 | return EBADF;
|
---|
[61042de] | 100 |
|
---|
[0d35511] | 101 | assert(oldfile->node != NULL);
|
---|
[fcab7ef] | 102 |
|
---|
| 103 | if (newfd != -1) {
|
---|
| 104 | /* Assign the old file to newfd. */
|
---|
| 105 | rc = vfs_fd_assign(oldfile, newfd);
|
---|
| 106 | } else {
|
---|
| 107 | vfs_file_t *newfile;
|
---|
| 108 | int newfd = vfs_fd_alloc(&newfile, desc);
|
---|
| 109 | if (newfd >= 0) {
|
---|
| 110 | newfile->node = oldfile->node;
|
---|
| 111 | newfile->permissions = oldfile->permissions;
|
---|
| 112 | vfs_node_addref(newfile->node);
|
---|
[0d35511] | 113 |
|
---|
[fcab7ef] | 114 | vfs_file_put(newfile);
|
---|
| 115 | }
|
---|
| 116 | rc = newfd;
|
---|
[0d35511] | 117 | }
|
---|
| 118 | vfs_file_put(oldfile);
|
---|
| 119 |
|
---|
[f77c1c9] | 120 | if (rc < 0) {
|
---|
| 121 | return rc;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | *out_fd = rc;
|
---|
| 125 | return EOK;
|
---|
[0d35511] | 126 | }
|
---|
| 127 |
|
---|
[9c4cf0d] | 128 | int vfs_op_put(int fd)
|
---|
[0d35511] | 129 | {
|
---|
| 130 | return vfs_fd_free(fd);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | static int vfs_connect_internal(service_id_t service_id, unsigned flags,
|
---|
| 134 | unsigned instance, const char *options, const char *fsname,
|
---|
| 135 | vfs_node_t **root)
|
---|
[861e7d1] | 136 | {
|
---|
[4636a60] | 137 | fs_handle_t fs_handle = 0;
|
---|
[05b9912] | 138 |
|
---|
[4636a60] | 139 | fibril_mutex_lock(&fs_list_lock);
|
---|
[61042de] | 140 | while (true) {
|
---|
[4636a60] | 141 | fs_handle = fs_name_to_handle(instance, fsname, false);
|
---|
[8dc72b64] | 142 |
|
---|
[61042de] | 143 | if (fs_handle != 0 || !(flags & VFS_MOUNT_BLOCKING))
|
---|
[4636a60] | 144 | break;
|
---|
[8dc72b64] | 145 |
|
---|
[4636a60] | 146 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
---|
| 147 | }
|
---|
| 148 | fibril_mutex_unlock(&fs_list_lock);
|
---|
| 149 |
|
---|
[61042de] | 150 | if (fs_handle == 0)
|
---|
[b14d9f9] | 151 | return ENOFS;
|
---|
[5bcd5b7] | 152 |
|
---|
[4636a60] | 153 | /* Tell the mountee that it is being mounted. */
|
---|
| 154 | ipc_call_t answer;
|
---|
| 155 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
[0d35511] | 156 | aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
|
---|
| 157 | &answer);
|
---|
[4636a60] | 158 | /* Send the mount options */
|
---|
| 159 | sysarg_t rc = async_data_write_start(exch, options, str_size(options));
|
---|
| 160 | if (rc != EOK) {
|
---|
| 161 | async_forget(msg);
|
---|
| 162 | vfs_exchange_release(exch);
|
---|
| 163 | return rc;
|
---|
[5bcd5b7] | 164 | }
|
---|
[c990ee6] | 165 |
|
---|
[4636a60] | 166 | async_wait_for(msg, &rc);
|
---|
[c990ee6] | 167 | if (rc != EOK) {
|
---|
| 168 | vfs_exchange_release(exch);
|
---|
[4636a60] | 169 | return rc;
|
---|
[c990ee6] | 170 | }
|
---|
[5bcd5b7] | 171 |
|
---|
[4636a60] | 172 | vfs_lookup_res_t res;
|
---|
| 173 | res.triplet.fs_handle = fs_handle;
|
---|
| 174 | res.triplet.service_id = service_id;
|
---|
| 175 | res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
[0d35511] | 176 | res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
| 177 | IPC_GET_ARG3(answer));
|
---|
[4636a60] | 178 | res.type = VFS_NODE_DIRECTORY;
|
---|
[5bcd5b7] | 179 |
|
---|
[4636a60] | 180 | /* Add reference to the mounted root. */
|
---|
| 181 | *root = vfs_node_get(&res);
|
---|
[c990ee6] | 182 | if (!*root) {
|
---|
| 183 | aid_t msg = async_send_1(exch, VFS_OUT_UNMOUNTED,
|
---|
| 184 | (sysarg_t) service_id, NULL);
|
---|
| 185 | async_forget(msg);
|
---|
| 186 | vfs_exchange_release(exch);
|
---|
| 187 | return ENOMEM;
|
---|
| 188 | }
|
---|
[8dc72b64] | 189 |
|
---|
[c990ee6] | 190 | vfs_exchange_release(exch);
|
---|
| 191 |
|
---|
[4636a60] | 192 | return EOK;
|
---|
[5bcd5b7] | 193 | }
|
---|
| 194 |
|
---|
[d2c8533] | 195 | int vfs_op_fsprobe(const char *fs_name, service_id_t sid,
|
---|
| 196 | vfs_fs_probe_info_t *info)
|
---|
| 197 | {
|
---|
| 198 | fs_handle_t fs_handle = 0;
|
---|
| 199 | sysarg_t rc;
|
---|
| 200 | int retval;
|
---|
| 201 |
|
---|
| 202 | fibril_mutex_lock(&fs_list_lock);
|
---|
| 203 | fs_handle = fs_name_to_handle(0, fs_name, false);
|
---|
| 204 | fibril_mutex_unlock(&fs_list_lock);
|
---|
| 205 |
|
---|
| 206 | if (fs_handle == 0)
|
---|
| 207 | return ENOFS;
|
---|
| 208 |
|
---|
| 209 | /* Send probe request to the file system server */
|
---|
| 210 | ipc_call_t answer;
|
---|
| 211 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
| 212 | aid_t msg = async_send_1(exch, VFS_OUT_FSPROBE, (sysarg_t) sid,
|
---|
| 213 | &answer);
|
---|
| 214 | if (msg == 0)
|
---|
| 215 | return EINVAL;
|
---|
| 216 |
|
---|
| 217 | /* Read probe information */
|
---|
| 218 | retval = async_data_read_start(exch, info, sizeof(*info));
|
---|
| 219 | if (retval != EOK) {
|
---|
| 220 | async_forget(msg);
|
---|
| 221 | return retval;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | async_wait_for(msg, &rc);
|
---|
| 225 | vfs_exchange_release(exch);
|
---|
| 226 | return rc;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[0d35511] | 229 | int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
|
---|
| 230 | unsigned instance, const char *opts, const char *fs_name, int *outfd)
|
---|
[8dc72b64] | 231 | {
|
---|
[0d35511] | 232 | int rc;
|
---|
[5126f80] | 233 | vfs_file_t *mp = NULL;
|
---|
| 234 | vfs_file_t *file = NULL;
|
---|
| 235 | int fd = -1;
|
---|
| 236 |
|
---|
| 237 | if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
---|
| 238 | mp = vfs_file_get(mpfd);
|
---|
| 239 | if (mp == NULL) {
|
---|
| 240 | rc = EBADF;
|
---|
| 241 | goto out;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | if (mp->node->mount != NULL) {
|
---|
| 245 | rc = EBUSY;
|
---|
| 246 | goto out;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if (mp->node->type != VFS_NODE_DIRECTORY) {
|
---|
| 250 | rc = ENOTDIR;
|
---|
| 251 | goto out;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | if (vfs_node_has_children(mp->node)) {
|
---|
| 255 | rc = ENOTEMPTY;
|
---|
| 256 | goto out;
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | if (!(flags & VFS_MOUNT_NO_REF)) {
|
---|
| 261 | fd = vfs_fd_alloc(&file, false);
|
---|
| 262 | if (fd < 0) {
|
---|
| 263 | rc = fd;
|
---|
| 264 | goto out;
|
---|
| 265 | }
|
---|
[c08c355] | 266 | }
|
---|
[4636a60] | 267 |
|
---|
[5126f80] | 268 | vfs_node_t *root = NULL;
|
---|
| 269 |
|
---|
[4636a60] | 270 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[6f9ef87a] | 271 |
|
---|
[0d35511] | 272 | rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
|
---|
| 273 | &root);
|
---|
[5126f80] | 274 | if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
|
---|
| 275 | vfs_node_addref(mp->node);
|
---|
| 276 | vfs_node_addref(root);
|
---|
| 277 | mp->node->mount = root;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 281 |
|
---|
[61042de] | 282 | if (rc != EOK)
|
---|
[5126f80] | 283 | goto out;
|
---|
| 284 |
|
---|
| 285 | if (flags & VFS_MOUNT_NO_REF) {
|
---|
| 286 | vfs_node_delref(root);
|
---|
| 287 | } else {
|
---|
| 288 | assert(file != NULL);
|
---|
| 289 |
|
---|
| 290 | file->node = root;
|
---|
| 291 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
---|
| 292 | file->open_read = false;
|
---|
| 293 | file->open_write = false;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | out:
|
---|
[61042de] | 297 | if (mp)
|
---|
[5126f80] | 298 | vfs_file_put(mp);
|
---|
[61042de] | 299 | if (file)
|
---|
[5126f80] | 300 | vfs_file_put(file);
|
---|
[61042de] | 301 |
|
---|
[5126f80] | 302 | if (rc != EOK && fd >= 0) {
|
---|
| 303 | vfs_fd_free(fd);
|
---|
[0d35511] | 304 | fd = 0;
|
---|
[5126f80] | 305 | }
|
---|
[0d35511] | 306 |
|
---|
| 307 | *outfd = fd;
|
---|
| 308 | return rc;
|
---|
[8dc72b64] | 309 | }
|
---|
| 310 |
|
---|
[b19e892] | 311 | int vfs_op_open(int fd, int mode)
|
---|
[861e7d1] | 312 | {
|
---|
[b19e892] | 313 | if (mode == 0)
|
---|
[0d35511] | 314 | return EINVAL;
|
---|
| 315 |
|
---|
| 316 | vfs_file_t *file = vfs_file_get(fd);
|
---|
[61042de] | 317 | if (!file)
|
---|
[0d35511] | 318 | return EBADF;
|
---|
| 319 |
|
---|
[b19e892] | 320 | if ((mode & ~file->permissions) != 0) {
|
---|
[0d35511] | 321 | vfs_file_put(file);
|
---|
| 322 | return EPERM;
|
---|
[0b18364] | 323 | }
|
---|
[0d35511] | 324 |
|
---|
| 325 | if (file->open_read || file->open_write) {
|
---|
| 326 | vfs_file_put(file);
|
---|
| 327 | return EBUSY;
|
---|
[cb65bbe] | 328 | }
|
---|
| 329 |
|
---|
[b19e892] | 330 | file->open_read = (mode & MODE_READ) != 0;
|
---|
| 331 | file->open_write = (mode & (MODE_WRITE | MODE_APPEND)) != 0;
|
---|
| 332 | file->append = (mode & MODE_APPEND) != 0;
|
---|
[05b9912] | 333 |
|
---|
[cb65bbe] | 334 | if (!file->open_read && !file->open_write) {
|
---|
| 335 | vfs_file_put(file);
|
---|
[0d35511] | 336 | return EINVAL;
|
---|
[6f2c1ff] | 337 | }
|
---|
[05b9912] | 338 |
|
---|
[cb65bbe] | 339 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
---|
| 340 | file->open_read = file->open_write = false;
|
---|
| 341 | vfs_file_put(file);
|
---|
[0d35511] | 342 | return EINVAL;
|
---|
[7fe1f75] | 343 | }
|
---|
[05b9912] | 344 |
|
---|
[cb65bbe] | 345 | int rc = vfs_open_node_remote(file->node);
|
---|
| 346 | if (rc != EOK) {
|
---|
| 347 | file->open_read = file->open_write = false;
|
---|
| 348 | vfs_file_put(file);
|
---|
[0d35511] | 349 | return rc;
|
---|
[05b9912] | 350 | }
|
---|
| 351 |
|
---|
[4fe94c66] | 352 | vfs_file_put(file);
|
---|
[0d35511] | 353 | return EOK;
|
---|
[05b9912] | 354 | }
|
---|
| 355 |
|
---|
[58898d1d] | 356 | typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, aoff64_t,
|
---|
| 357 | ipc_call_t *, bool, void *);
|
---|
[42d08592] | 358 |
|
---|
[58898d1d] | 359 | static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
---|
[42d08592] | 360 | ipc_call_t *answer, bool read, void *data)
|
---|
| 361 | {
|
---|
[e503517a] | 362 | size_t *bytes = (size_t *) data;
|
---|
| 363 | int rc;
|
---|
| 364 |
|
---|
[42d08592] | 365 | /*
|
---|
| 366 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
| 367 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
| 368 | * destination FS server. The call will be routed as if sent by
|
---|
| 369 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
| 370 | * don't have to bother.
|
---|
| 371 | */
|
---|
| 372 |
|
---|
| 373 | if (read) {
|
---|
[e503517a] | 374 | rc = async_data_read_forward_4_1(exch, VFS_OUT_READ,
|
---|
[42d08592] | 375 | file->node->service_id, file->node->index,
|
---|
[58898d1d] | 376 | LOWER32(pos), UPPER32(pos), answer);
|
---|
[42d08592] | 377 | } else {
|
---|
[e503517a] | 378 | rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE,
|
---|
[42d08592] | 379 | file->node->service_id, file->node->index,
|
---|
[58898d1d] | 380 | LOWER32(pos), UPPER32(pos), answer);
|
---|
[e503517a] | 381 | }
|
---|
| 382 |
|
---|
| 383 | *bytes = IPC_GET_ARG1(*answer);
|
---|
| 384 | return rc;
|
---|
[42d08592] | 385 | }
|
---|
[e503517a] | 386 |
|
---|
[58898d1d] | 387 | static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file, aoff64_t pos,
|
---|
[e503517a] | 388 | ipc_call_t *answer, bool read, void *data)
|
---|
| 389 | {
|
---|
| 390 | rdwr_io_chunk_t *chunk = (rdwr_io_chunk_t *) data;
|
---|
| 391 |
|
---|
| 392 | if (exch == NULL)
|
---|
| 393 | return ENOENT;
|
---|
[42d08592] | 394 |
|
---|
[e503517a] | 395 | aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE,
|
---|
[58898d1d] | 396 | file->node->service_id, file->node->index, LOWER32(pos),
|
---|
| 397 | UPPER32(pos), answer);
|
---|
[e503517a] | 398 | if (msg == 0)
|
---|
| 399 | return EINVAL;
|
---|
| 400 |
|
---|
| 401 | int retval = async_data_read_start(exch, chunk->buffer, chunk->size);
|
---|
| 402 | if (retval != EOK) {
|
---|
| 403 | async_forget(msg);
|
---|
| 404 | return retval;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | sysarg_t rc;
|
---|
| 408 | async_wait_for(msg, &rc);
|
---|
| 409 |
|
---|
| 410 | chunk->size = IPC_GET_ARG1(*answer);
|
---|
| 411 |
|
---|
| 412 | return (int) rc;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[58898d1d] | 415 | static int vfs_rdwr(int fd, aoff64_t pos, bool read, rdwr_ipc_cb_t ipc_cb,
|
---|
| 416 | void *ipc_cb_data)
|
---|
[861e7d1] | 417 | {
|
---|
| 418 | /*
|
---|
| 419 | * The following code strongly depends on the fact that the files data
|
---|
| 420 | * structure can be only accessed by a single fibril and all file
|
---|
| 421 | * operations are serialized (i.e. the reads and writes cannot
|
---|
| 422 | * interleave and a file cannot be closed while it is being read).
|
---|
| 423 | *
|
---|
| 424 | * Additional synchronization needs to be added once the table of
|
---|
| 425 | * open files supports parallel access!
|
---|
| 426 | */
|
---|
[79ae36dd] | 427 |
|
---|
[72bde81] | 428 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
[861e7d1] | 429 | vfs_file_t *file = vfs_file_get(fd);
|
---|
[e503517a] | 430 | if (!file)
|
---|
[354b642] | 431 | return EBADF;
|
---|
[6c89f20] | 432 |
|
---|
[cb65bbe] | 433 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
---|
[c577a9a] | 434 | vfs_file_put(file);
|
---|
[1dff985] | 435 | return EINVAL;
|
---|
[cb65bbe] | 436 | }
|
---|
| 437 |
|
---|
[79ae36dd] | 438 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
| 439 | assert(fs_info);
|
---|
| 440 |
|
---|
[0d35511] | 441 | bool rlock = read ||
|
---|
| 442 | (fs_info->concurrent_read_write && fs_info->write_retains_size);
|
---|
[354b642] | 443 |
|
---|
[861e7d1] | 444 | /*
|
---|
| 445 | * Lock the file's node so that no other client can read/write to it at
|
---|
[c2f4b6b] | 446 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
| 447 | * write implementation does not modify the file size.
|
---|
[861e7d1] | 448 | */
|
---|
[61042de] | 449 | if (rlock)
|
---|
[230260ac] | 450 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
[61042de] | 451 | else
|
---|
[230260ac] | 452 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[79ae36dd] | 453 |
|
---|
[b17186d] | 454 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
| 455 | /*
|
---|
| 456 | * Make sure that no one is modifying the namespace
|
---|
| 457 | * while we are in readdir().
|
---|
| 458 | */
|
---|
[354b642] | 459 |
|
---|
| 460 | if (!read) {
|
---|
| 461 | if (rlock) {
|
---|
[0d35511] | 462 | fibril_rwlock_read_unlock(
|
---|
| 463 | &file->node->contents_rwlock);
|
---|
[354b642] | 464 | } else {
|
---|
[0d35511] | 465 | fibril_rwlock_write_unlock(
|
---|
| 466 | &file->node->contents_rwlock);
|
---|
[354b642] | 467 | }
|
---|
| 468 | vfs_file_put(file);
|
---|
| 469 | return EINVAL;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
[230260ac] | 472 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[b17186d] | 473 | }
|
---|
[6c89f20] | 474 |
|
---|
[79ae36dd] | 475 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
[861e7d1] | 476 |
|
---|
[51774cd] | 477 | if (!read && file->append)
|
---|
[58898d1d] | 478 | pos = file->node->size;
|
---|
[42d08592] | 479 |
|
---|
[861e7d1] | 480 | /*
|
---|
[42d08592] | 481 | * Handle communication with the endpoint FS.
|
---|
[861e7d1] | 482 | */
|
---|
[b4cbef1] | 483 | ipc_call_t answer;
|
---|
[58898d1d] | 484 | int rc = ipc_cb(fs_exch, file, pos, &answer, read, ipc_cb_data);
|
---|
[05b9912] | 485 |
|
---|
[79ae36dd] | 486 | vfs_exchange_release(fs_exch);
|
---|
[34ca870] | 487 |
|
---|
[61042de] | 488 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
[230260ac] | 489 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[6c89f20] | 490 |
|
---|
[72bde81] | 491 | /* Unlock the VFS node. */
|
---|
[354b642] | 492 | if (rlock) {
|
---|
[230260ac] | 493 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
[354b642] | 494 | } else {
|
---|
[861e7d1] | 495 | /* Update the cached version of node's size. */
|
---|
[354b642] | 496 | if (rc == EOK) {
|
---|
[5bb9907] | 497 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
| 498 | IPC_GET_ARG3(answer));
|
---|
[354b642] | 499 | }
|
---|
[230260ac] | 500 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
[861e7d1] | 501 | }
|
---|
[6c89f20] | 502 |
|
---|
[4fe94c66] | 503 | vfs_file_put(file);
|
---|
| 504 |
|
---|
[e503517a] | 505 | return rc;
|
---|
| 506 | }
|
---|
[861e7d1] | 507 |
|
---|
[58898d1d] | 508 | int vfs_rdwr_internal(int fd, aoff64_t pos, bool read, rdwr_io_chunk_t *chunk)
|
---|
[e503517a] | 509 | {
|
---|
[58898d1d] | 510 | return vfs_rdwr(fd, pos, read, rdwr_ipc_internal, chunk);
|
---|
[e503517a] | 511 | }
|
---|
| 512 |
|
---|
[58898d1d] | 513 | int vfs_op_read(int fd, aoff64_t pos, size_t *out_bytes)
|
---|
[861e7d1] | 514 | {
|
---|
[58898d1d] | 515 | return vfs_rdwr(fd, pos, true, rdwr_ipc_client, out_bytes);
|
---|
[861e7d1] | 516 | }
|
---|
| 517 |
|
---|
[0d35511] | 518 | int vfs_op_rename(int basefd, char *old, char *new)
|
---|
[861e7d1] | 519 | {
|
---|
[0d35511] | 520 | vfs_file_t *base_file = vfs_file_get(basefd);
|
---|
[61042de] | 521 | if (!base_file)
|
---|
[0d35511] | 522 | return EBADF;
|
---|
[61042de] | 523 |
|
---|
[0d35511] | 524 | vfs_node_t *base = base_file->node;
|
---|
| 525 | vfs_node_addref(base);
|
---|
| 526 | vfs_file_put(base_file);
|
---|
| 527 |
|
---|
| 528 | vfs_lookup_res_t base_lr;
|
---|
| 529 | vfs_lookup_res_t old_lr;
|
---|
| 530 | vfs_lookup_res_t new_lr_orig;
|
---|
| 531 | bool orig_unlinked = false;
|
---|
| 532 |
|
---|
| 533 | int rc;
|
---|
| 534 |
|
---|
| 535 | size_t shared = shared_path(old, new);
|
---|
| 536 |
|
---|
| 537 | /* Do not allow one path to be a prefix of the other. */
|
---|
| 538 | if (old[shared] == 0 || new[shared] == 0) {
|
---|
[7f59d6c] | 539 | vfs_node_put(base);
|
---|
[0d35511] | 540 | return EINVAL;
|
---|
| 541 | }
|
---|
| 542 | assert(old[shared] == '/');
|
---|
| 543 | assert(new[shared] == '/');
|
---|
| 544 |
|
---|
| 545 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
| 546 |
|
---|
| 547 | /* Resolve the shared portion of the path first. */
|
---|
| 548 | if (shared != 0) {
|
---|
| 549 | old[shared] = 0;
|
---|
| 550 | rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
|
---|
| 551 | if (rc != EOK) {
|
---|
[7f59d6c] | 552 | vfs_node_put(base);
|
---|
[0d35511] | 553 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 554 | return rc;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | vfs_node_put(base);
|
---|
| 558 | base = vfs_node_get(&base_lr);
|
---|
[c990ee6] | 559 | if (!base) {
|
---|
| 560 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 561 | return ENOMEM;
|
---|
| 562 | }
|
---|
[0d35511] | 563 | old[shared] = '/';
|
---|
| 564 | old += shared;
|
---|
| 565 | new += shared;
|
---|
| 566 | }
|
---|
[7f59d6c] | 567 |
|
---|
[61042de] | 568 | rc = vfs_lookup_internal(base, old, L_DISABLE_MOUNTS, &old_lr);
|
---|
[7f59d6c] | 569 | if (rc != EOK) {
|
---|
| 570 | vfs_node_put(base);
|
---|
| 571 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 572 | return rc;
|
---|
| 573 | }
|
---|
[0d35511] | 574 |
|
---|
| 575 | rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
| 576 | &new_lr_orig);
|
---|
| 577 | if (rc == EOK) {
|
---|
| 578 | orig_unlinked = true;
|
---|
| 579 | } else if (rc != ENOENT) {
|
---|
| 580 | vfs_node_put(base);
|
---|
| 581 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 582 | return rc;
|
---|
| 583 | }
|
---|
[7f59d6c] | 584 |
|
---|
| 585 | rc = vfs_link_internal(base, new, &old_lr.triplet);
|
---|
[0d35511] | 586 | if (rc != EOK) {
|
---|
[7f59d6c] | 587 | vfs_link_internal(base, old, &old_lr.triplet);
|
---|
[61042de] | 588 | if (orig_unlinked)
|
---|
[0d35511] | 589 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
| 590 | vfs_node_put(base);
|
---|
| 591 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 592 | return rc;
|
---|
| 593 | }
|
---|
[7f59d6c] | 594 |
|
---|
| 595 | rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
|
---|
| 596 | &old_lr);
|
---|
[0d35511] | 597 | if (rc != EOK) {
|
---|
[61042de] | 598 | if (orig_unlinked)
|
---|
[0d35511] | 599 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
---|
| 600 | vfs_node_put(base);
|
---|
| 601 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 602 | return rc;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | /* If the node is not held by anyone, try to destroy it. */
|
---|
[4f9ab1e] | 606 | if (orig_unlinked) {
|
---|
| 607 | vfs_node_t *node = vfs_node_peek(&new_lr_orig);
|
---|
| 608 | if (!node)
|
---|
| 609 | out_destroy(&new_lr_orig.triplet);
|
---|
| 610 | else
|
---|
| 611 | vfs_node_put(node);
|
---|
[0d35511] | 612 | }
|
---|
| 613 |
|
---|
| 614 | vfs_node_put(base);
|
---|
| 615 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 616 | return EOK;
|
---|
[861e7d1] | 617 | }
|
---|
| 618 |
|
---|
[67e881c] | 619 | int vfs_op_resize(int fd, int64_t size)
|
---|
| 620 | {
|
---|
| 621 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 622 | if (!file)
|
---|
| 623 | return EBADF;
|
---|
| 624 |
|
---|
| 625 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
| 626 |
|
---|
| 627 | int rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
| 628 | file->node->service_id, file->node->index, size);
|
---|
| 629 | if (rc == EOK)
|
---|
| 630 | file->node->size = size;
|
---|
| 631 |
|
---|
| 632 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
| 633 | vfs_file_put(file);
|
---|
| 634 | return rc;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
[fe91f66] | 637 | int vfs_op_stat(int fd)
|
---|
| 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);
|
---|
| 646 | int rc = async_data_read_forward_fast(exch, VFS_OUT_STAT,
|
---|
| 647 | node->service_id, node->index, true, 0, NULL);
|
---|
| 648 | vfs_exchange_release(exch);
|
---|
| 649 |
|
---|
| 650 | vfs_file_put(file);
|
---|
| 651 | return rc;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
[0d35511] | 654 | int 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);
|
---|
[35e81e2] | 663 | int rc = async_data_read_forward_fast(exch, VFS_OUT_STATFS,
|
---|
| 664 | node->service_id, node->index, false, 0, NULL);
|
---|
[0d35511] | 665 | vfs_exchange_release(exch);
|
---|
| 666 |
|
---|
[4fe94c66] | 667 | vfs_file_put(file);
|
---|
[35e81e2] | 668 | return rc;
|
---|
[861e7d1] | 669 | }
|
---|
| 670 |
|
---|
[0d35511] | 671 | int vfs_op_sync(int fd)
|
---|
[852b801] | 672 | {
|
---|
| 673 | vfs_file_t *file = vfs_file_get(fd);
|
---|
[61042de] | 674 | if (!file)
|
---|
[0d35511] | 675 | return EBADF;
|
---|
| 676 |
|
---|
| 677 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
[852b801] | 678 |
|
---|
| 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);
|
---|
[79ae36dd] | 683 |
|
---|
[0d35511] | 684 | vfs_exchange_release(fs_exch);
|
---|
[79ae36dd] | 685 |
|
---|
[0d35511] | 686 | sysarg_t rc;
|
---|
[852b801] | 687 | async_wait_for(msg, &rc);
|
---|
[79ae36dd] | 688 |
|
---|
[4fe94c66] | 689 | vfs_file_put(file);
|
---|
[0d35511] | 690 | return rc;
|
---|
| 691 |
|
---|
[852b801] | 692 | }
|
---|
| 693 |
|
---|
[0d35511] | 694 | static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
| 695 | fs_index_t index, aoff64_t size)
|
---|
[5bcd5b7] | 696 | {
|
---|
[0d35511] | 697 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
| 698 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
| 699 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
| 700 | UPPER32(size));
|
---|
| 701 | vfs_exchange_release(exch);
|
---|
| 702 |
|
---|
| 703 | return (int) rc;
|
---|
[5bcd5b7] | 704 | }
|
---|
| 705 |
|
---|
[79ea5af] | 706 | int vfs_op_unlink(int parentfd, int expectfd, char *path)
|
---|
[0d35511] | 707 | {
|
---|
| 708 | int rc = EOK;
|
---|
[20c071d] | 709 | vfs_file_t *parent = NULL;
|
---|
| 710 | vfs_file_t *expect = NULL;
|
---|
| 711 |
|
---|
[61042de] | 712 | if (parentfd == expectfd)
|
---|
[0d35511] | 713 | return EINVAL;
|
---|
[472c09d] | 714 |
|
---|
[20c071d] | 715 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
| 716 |
|
---|
[0d35511] | 717 | /*
|
---|
| 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 | }
|
---|
| 728 |
|
---|
| 729 | if (expectfd >= 0) {
|
---|
| 730 | expect = vfs_file_get(expectfd);
|
---|
| 731 | if (!expect) {
|
---|
[0d35511] | 732 | rc = EBADF;
|
---|
[20c071d] | 733 | goto exit;
|
---|
| 734 | }
|
---|
[c577a9a] | 735 | }
|
---|
| 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 | }
|
---|
| 744 |
|
---|
[5126f80] | 745 | assert(parent != NULL);
|
---|
[c577a9a] | 746 |
|
---|
| 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;
|
---|
| 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 | }
|
---|
| 759 |
|
---|
| 760 | vfs_file_put(expect);
|
---|
| 761 | expect = NULL;
|
---|
| 762 | }
|
---|
| 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 |
|
---|
[0d35511] | 787 | int 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;
|
---|
[472c09d] | 792 |
|
---|
[0d35511] | 793 | if (mp->node->mount == NULL) {
|
---|
| 794 | vfs_file_put(mp);
|
---|
| 795 | return ENOENT;
|
---|
[a8e9ab8d] | 796 | }
|
---|
[72bde81] | 797 |
|
---|
[230260ac] | 798 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[472c09d] | 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 | }
|
---|
[472c09d] | 813 |
|
---|
[0d35511] | 814 | async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
|
---|
| 815 | int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
| 816 | mp->node->mount->service_id);
|
---|
| 817 | vfs_exchange_release(exch);
|
---|
[f15cf1a6] | 818 |
|
---|
| 819 | if (rc != EOK) {
|
---|
[0d35511] | 820 | vfs_file_put(mp);
|
---|
[230260ac] | 821 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[778d26d] | 822 | return rc;
|
---|
[f15cf1a6] | 823 | }
|
---|
[472c09d] | 824 |
|
---|
[0d35511] | 825 | vfs_node_forget(mp->node->mount);
|
---|
| 826 | vfs_node_put(mp->node);
|
---|
| 827 | mp->node->mount = NULL;
|
---|
[472c09d] | 828 |
|
---|
[230260ac] | 829 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[0d35511] | 830 |
|
---|
| 831 | vfs_file_put(mp);
|
---|
[778d26d] | 832 | return EOK;
|
---|
[f15cf1a6] | 833 | }
|
---|
| 834 |
|
---|
[0d35511] | 835 | int vfs_op_wait_handle(bool high_fd)
|
---|
[a8e9ab8d] | 836 | {
|
---|
[0d35511] | 837 | return vfs_wait_handle_internal(high_fd);
|
---|
| 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 |
|
---|
[0d35511] | 871 | int 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;
|
---|
[4fe94c66] | 875 |
|
---|
[0d35511] | 876 | vfs_file_t *parent = vfs_file_get(parentfd);
|
---|
[61042de] | 877 | if (!parent)
|
---|
[0d35511] | 878 | return EBADF;
|
---|
[2b88074b] | 879 |
|
---|
[0d35511] | 880 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[2b88074b] | 881 |
|
---|
[0d35511] | 882 | vfs_lookup_res_t lr;
|
---|
| 883 | int rc = vfs_lookup_internal(parent->node, path,
|
---|
| 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 | }
|
---|
[9dc6083] | 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 | }
|
---|
[9dc6083] | 897 |
|
---|
[0d35511] | 898 | vfs_file_t *file;
|
---|
| 899 | int fd = vfs_fd_alloc(&file, false);
|
---|
| 900 | if (fd < 0) {
|
---|
| 901 | vfs_node_put(node);
|
---|
| 902 | vfs_file_put(parent);
|
---|
| 903 | return fd;
|
---|
| 904 | }
|
---|
| 905 | assert(file != NULL);
|
---|
[9dc6083] | 906 |
|
---|
[0d35511] | 907 | file->node = node;
|
---|
| 908 | file->permissions = parent->permissions;
|
---|
| 909 | file->open_read = false;
|
---|
| 910 | file->open_write = false;
|
---|
[9dc6083] | 911 |
|
---|
[a737667e] | 912 | vfs_file_put(file);
|
---|
[0d35511] | 913 | vfs_file_put(parent);
|
---|
| 914 |
|
---|
| 915 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[9dc6083] | 916 |
|
---|
[0d35511] | 917 | *out_fd = fd;
|
---|
| 918 | return EOK;
|
---|
[66366470] | 919 | }
|
---|
| 920 |
|
---|
[58898d1d] | 921 | int 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 | */
|
---|