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