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