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