| 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 | vfs_node_t *root = NULL;
|
|---|
| 71 |
|
|---|
| 72 | #if 0
|
|---|
| 73 | static int vfs_attach_internal(vfs_node_t *mpoint, vfs_node_t *mountee)
|
|---|
| 74 | {
|
|---|
| 75 | assert(mpoint != NULL);
|
|---|
| 76 | assert(mountee != NULL);
|
|---|
| 77 |
|
|---|
| 78 | if (mpoint->mount != NULL) {
|
|---|
| 79 | return EBUSY;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if (mpoint->type != VFS_NODE_DIRECTORY) {
|
|---|
| 83 | return ENOTDIR;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | if (vfs_node_has_children(mpoint)) {
|
|---|
| 87 | return ENOTEMPTY;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | mpoint->mount = mountee;
|
|---|
| 91 | vfs_node_addref(mountee);
|
|---|
| 92 | /* Add reference to make sure the node is not freed. Removed in detach_internal(). */
|
|---|
| 93 | vfs_node_addref(mpoint);
|
|---|
| 94 | return EOK;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static int vfs_detach_internal(vfs_node_t *mpoint)
|
|---|
| 98 | {
|
|---|
| 99 | assert(mpoint != NULL);
|
|---|
| 100 |
|
|---|
| 101 | if (mpoint->mount == NULL) {
|
|---|
| 102 | return ENOENT;
|
|---|
| 103 | }
|
|---|
| 104 | vfs_node_put(mpoint->mount);
|
|---|
| 105 | mpoint->mount = NULL;
|
|---|
| 106 | vfs_node_put(mpoint);
|
|---|
| 107 | }
|
|---|
| 108 | #endif
|
|---|
| 109 |
|
|---|
| 110 | static int vfs_mount_internal(ipc_callid_t rid, service_id_t service_id,
|
|---|
| 111 | fs_handle_t fs_handle, char *mp, char *opts)
|
|---|
| 112 | {
|
|---|
| 113 | vfs_lookup_res_t mp_res;
|
|---|
| 114 | vfs_lookup_res_t mr_res;
|
|---|
| 115 | vfs_node_t *mp_node = NULL;
|
|---|
| 116 | vfs_node_t *mr_node;
|
|---|
| 117 | fs_index_t rindex;
|
|---|
| 118 | aoff64_t rsize;
|
|---|
| 119 | async_exch_t *exch;
|
|---|
| 120 | sysarg_t rc;
|
|---|
| 121 | aid_t msg;
|
|---|
| 122 | ipc_call_t answer;
|
|---|
| 123 |
|
|---|
| 124 | /* Resolve the path to the mountpoint. */
|
|---|
| 125 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| 126 | if (root == NULL) {
|
|---|
| 127 | /* We still don't have the root file system mounted. */
|
|---|
| 128 | if (str_cmp(mp, "/") != 0) {
|
|---|
| 129 | /*
|
|---|
| 130 | * We can't resolve this without the root filesystem
|
|---|
| 131 | * being mounted first.
|
|---|
| 132 | */
|
|---|
| 133 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 134 | async_answer_0(rid, ENOENT);
|
|---|
| 135 | return ENOENT;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /*
|
|---|
| 139 | * For this simple, but important case,
|
|---|
| 140 | * we are almost done.
|
|---|
| 141 | */
|
|---|
| 142 |
|
|---|
| 143 | /* Tell the mountee that it is being mounted. */
|
|---|
| 144 | exch = vfs_exchange_grab(fs_handle);
|
|---|
| 145 | msg = async_send_1(exch, VFS_OUT_MOUNTED,
|
|---|
| 146 | (sysarg_t) service_id, &answer);
|
|---|
| 147 | /* Send the mount options */
|
|---|
| 148 | rc = async_data_write_start(exch, (void *)opts,
|
|---|
| 149 | str_size(opts));
|
|---|
| 150 | vfs_exchange_release(exch);
|
|---|
| 151 |
|
|---|
| 152 | if (rc != EOK) {
|
|---|
| 153 | async_forget(msg);
|
|---|
| 154 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 155 | async_answer_0(rid, rc);
|
|---|
| 156 | return rc;
|
|---|
| 157 | }
|
|---|
| 158 | async_wait_for(msg, &rc);
|
|---|
| 159 |
|
|---|
| 160 | if (rc != EOK) {
|
|---|
| 161 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 162 | async_answer_0(rid, rc);
|
|---|
| 163 | return rc;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
|---|
| 167 | rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
|---|
| 168 | IPC_GET_ARG3(answer));
|
|---|
| 169 |
|
|---|
| 170 | mr_res.triplet.fs_handle = fs_handle;
|
|---|
| 171 | mr_res.triplet.service_id = service_id;
|
|---|
| 172 | mr_res.triplet.index = rindex;
|
|---|
| 173 | mr_res.size = rsize;
|
|---|
| 174 | mr_res.type = VFS_NODE_DIRECTORY;
|
|---|
| 175 |
|
|---|
| 176 | /* Add reference to the mounted root. */
|
|---|
| 177 | root = vfs_node_get(&mr_res);
|
|---|
| 178 | assert(root);
|
|---|
| 179 |
|
|---|
| 180 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 181 | async_answer_0(rid, rc);
|
|---|
| 182 | return rc;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /* We already have the root FS. */
|
|---|
| 186 | if (str_cmp(mp, "/") == 0) {
|
|---|
| 187 | /* Trying to mount root FS over root FS */
|
|---|
| 188 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 189 | async_answer_0(rid, EBUSY);
|
|---|
| 190 | return EBUSY;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | rc = vfs_lookup_internal(root, mp, L_DIRECTORY, &mp_res);
|
|---|
| 194 | if (rc != EOK) {
|
|---|
| 195 | /* The lookup failed. */
|
|---|
| 196 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 197 | async_answer_0(rid, rc);
|
|---|
| 198 | return rc;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | mp_node = vfs_node_get(&mp_res);
|
|---|
| 202 | if (!mp_node) {
|
|---|
| 203 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 204 | async_answer_0(rid, ENOMEM);
|
|---|
| 205 | return ENOMEM;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /*
|
|---|
| 209 | * Now we hold a reference to mp_node.
|
|---|
| 210 | * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
|
|---|
| 211 | * This prevents the mount point from being deleted.
|
|---|
| 212 | */
|
|---|
| 213 |
|
|---|
| 214 | /*
|
|---|
| 215 | * At this point, we have all necessary pieces: file system handle
|
|---|
| 216 | * and service ID, and we know the mount point VFS node.
|
|---|
| 217 | */
|
|---|
| 218 |
|
|---|
| 219 | async_exch_t *mountee_exch = vfs_exchange_grab(fs_handle);
|
|---|
| 220 | assert(mountee_exch);
|
|---|
| 221 |
|
|---|
| 222 | exch = vfs_exchange_grab(mp_res.triplet.fs_handle);
|
|---|
| 223 | msg = async_send_4(exch, VFS_OUT_MOUNT,
|
|---|
| 224 | (sysarg_t) mp_res.triplet.service_id,
|
|---|
| 225 | (sysarg_t) mp_res.triplet.index,
|
|---|
| 226 | (sysarg_t) fs_handle,
|
|---|
| 227 | (sysarg_t) service_id, &answer);
|
|---|
| 228 |
|
|---|
| 229 | /* Send connection */
|
|---|
| 230 | rc = async_exchange_clone(exch, mountee_exch);
|
|---|
| 231 | vfs_exchange_release(mountee_exch);
|
|---|
| 232 |
|
|---|
| 233 | if (rc != EOK) {
|
|---|
| 234 | vfs_exchange_release(exch);
|
|---|
| 235 | async_forget(msg);
|
|---|
| 236 |
|
|---|
| 237 | /* Mount failed, drop reference to mp_node. */
|
|---|
| 238 | if (mp_node)
|
|---|
| 239 | vfs_node_put(mp_node);
|
|---|
| 240 |
|
|---|
| 241 | async_answer_0(rid, rc);
|
|---|
| 242 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 243 | return rc;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /* send the mount options */
|
|---|
| 247 | rc = async_data_write_start(exch, (void *) opts, str_size(opts));
|
|---|
| 248 | if (rc != EOK) {
|
|---|
| 249 | vfs_exchange_release(exch);
|
|---|
| 250 | async_forget(msg);
|
|---|
| 251 |
|
|---|
| 252 | /* Mount failed, drop reference to mp_node. */
|
|---|
| 253 | if (mp_node)
|
|---|
| 254 | vfs_node_put(mp_node);
|
|---|
| 255 |
|
|---|
| 256 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 257 | async_answer_0(rid, rc);
|
|---|
| 258 | return rc;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /*
|
|---|
| 262 | * Wait for the answer before releasing the exchange to avoid deadlock
|
|---|
| 263 | * in case the answer depends on further calls to the same file system.
|
|---|
| 264 | * Think of a case when mounting a FS on a file_bd backed by a file on
|
|---|
| 265 | * the same FS.
|
|---|
| 266 | */
|
|---|
| 267 | async_wait_for(msg, &rc);
|
|---|
| 268 | vfs_exchange_release(exch);
|
|---|
| 269 |
|
|---|
| 270 | if (rc == EOK) {
|
|---|
| 271 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
|---|
| 272 | rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
|
|---|
| 273 | IPC_GET_ARG3(answer));
|
|---|
| 274 |
|
|---|
| 275 | mr_res.triplet.fs_handle = fs_handle;
|
|---|
| 276 | mr_res.triplet.service_id = service_id;
|
|---|
| 277 | mr_res.triplet.index = rindex;
|
|---|
| 278 | mr_res.size = rsize;
|
|---|
| 279 | mr_res.type = VFS_NODE_DIRECTORY;
|
|---|
| 280 |
|
|---|
| 281 | /* Add reference to the mounted root. */
|
|---|
| 282 | mr_node = vfs_node_get(&mr_res);
|
|---|
| 283 | assert(mr_node);
|
|---|
| 284 | } else {
|
|---|
| 285 | /* Mount failed, drop reference to mp_node. */
|
|---|
| 286 | if (mp_node)
|
|---|
| 287 | vfs_node_put(mp_node);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | async_answer_0(rid, rc);
|
|---|
| 291 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 292 | return rc;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 296 | {
|
|---|
| 297 | service_id_t service_id;
|
|---|
| 298 |
|
|---|
| 299 | /*
|
|---|
| 300 | * We expect the library to do the device-name to device-handle
|
|---|
| 301 | * translation for us, thus the device handle will arrive as ARG1
|
|---|
| 302 | * in the request.
|
|---|
| 303 | */
|
|---|
| 304 | service_id = (service_id_t) IPC_GET_ARG1(*request);
|
|---|
| 305 |
|
|---|
| 306 | /*
|
|---|
| 307 | * Mount flags are passed as ARG2.
|
|---|
| 308 | */
|
|---|
| 309 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
|
|---|
| 310 |
|
|---|
| 311 | /*
|
|---|
| 312 | * Instance number is passed as ARG3.
|
|---|
| 313 | */
|
|---|
| 314 | unsigned int instance = IPC_GET_ARG3(*request);
|
|---|
| 315 |
|
|---|
| 316 | /* We want the client to send us the mount point. */
|
|---|
| 317 | char *mp;
|
|---|
| 318 | int rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
|---|
| 319 | 0, NULL);
|
|---|
| 320 | if (rc != EOK) {
|
|---|
| 321 | async_answer_0(rid, rc);
|
|---|
| 322 | return;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | /* Now we expect to receive the mount options. */
|
|---|
| 326 | char *opts;
|
|---|
| 327 | rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
|
|---|
| 328 | 0, NULL);
|
|---|
| 329 | if (rc != EOK) {
|
|---|
| 330 | free(mp);
|
|---|
| 331 | async_answer_0(rid, rc);
|
|---|
| 332 | return;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /*
|
|---|
| 336 | * Now, we expect the client to send us data with the name of the file
|
|---|
| 337 | * system.
|
|---|
| 338 | */
|
|---|
| 339 | char *fs_name;
|
|---|
| 340 | rc = async_data_write_accept((void **) &fs_name, true, 0,
|
|---|
| 341 | FS_NAME_MAXLEN, 0, NULL);
|
|---|
| 342 | if (rc != EOK) {
|
|---|
| 343 | free(mp);
|
|---|
| 344 | free(opts);
|
|---|
| 345 | async_answer_0(rid, rc);
|
|---|
| 346 | return;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | /*
|
|---|
| 350 | * Wait for VFS_IN_PING so that we can return an error if we don't know
|
|---|
| 351 | * fs_name.
|
|---|
| 352 | */
|
|---|
| 353 | ipc_call_t data;
|
|---|
| 354 | ipc_callid_t callid = async_get_call(&data);
|
|---|
| 355 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
|---|
| 356 | async_answer_0(callid, ENOTSUP);
|
|---|
| 357 | async_answer_0(rid, ENOTSUP);
|
|---|
| 358 | free(mp);
|
|---|
| 359 | free(opts);
|
|---|
| 360 | free(fs_name);
|
|---|
| 361 | return;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /*
|
|---|
| 365 | * Check if we know a file system with the same name as is in fs_name.
|
|---|
| 366 | * This will also give us its file system handle.
|
|---|
| 367 | */
|
|---|
| 368 | fibril_mutex_lock(&fs_list_lock);
|
|---|
| 369 | fs_handle_t fs_handle;
|
|---|
| 370 | recheck:
|
|---|
| 371 | fs_handle = fs_name_to_handle(instance, fs_name, false);
|
|---|
| 372 | if (!fs_handle) {
|
|---|
| 373 | if (flags & IPC_FLAG_BLOCKING) {
|
|---|
| 374 | fibril_condvar_wait(&fs_list_cv, &fs_list_lock);
|
|---|
| 375 | goto recheck;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| 379 | async_answer_0(callid, ENOENT);
|
|---|
| 380 | async_answer_0(rid, ENOENT);
|
|---|
| 381 | free(mp);
|
|---|
| 382 | free(fs_name);
|
|---|
| 383 | free(opts);
|
|---|
| 384 | return;
|
|---|
| 385 | }
|
|---|
| 386 | fibril_mutex_unlock(&fs_list_lock);
|
|---|
| 387 |
|
|---|
| 388 | /* Add the filesystem info to the list of mounted filesystems */
|
|---|
| 389 | mtab_ent_t *mtab_ent = malloc(sizeof(mtab_ent_t));
|
|---|
| 390 | if (!mtab_ent) {
|
|---|
| 391 | async_answer_0(callid, ENOMEM);
|
|---|
| 392 | async_answer_0(rid, ENOMEM);
|
|---|
| 393 | free(mp);
|
|---|
| 394 | free(fs_name);
|
|---|
| 395 | free(opts);
|
|---|
| 396 | return;
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /* Do the mount */
|
|---|
| 400 | rc = vfs_mount_internal(rid, service_id, fs_handle, mp, opts);
|
|---|
| 401 | if (rc != EOK) {
|
|---|
| 402 | async_answer_0(callid, ENOTSUP);
|
|---|
| 403 | async_answer_0(rid, ENOTSUP);
|
|---|
| 404 | free(mtab_ent);
|
|---|
| 405 | free(mp);
|
|---|
| 406 | free(opts);
|
|---|
| 407 | free(fs_name);
|
|---|
| 408 | return;
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | /* Add the filesystem info to the list of mounted filesystems */
|
|---|
| 412 |
|
|---|
| 413 | str_cpy(mtab_ent->mp, MAX_PATH_LEN, mp);
|
|---|
| 414 | str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
|
|---|
| 415 | str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
|
|---|
| 416 | mtab_ent->instance = instance;
|
|---|
| 417 | mtab_ent->service_id = service_id;
|
|---|
| 418 |
|
|---|
| 419 | link_initialize(&mtab_ent->link);
|
|---|
| 420 |
|
|---|
| 421 | fibril_mutex_lock(&mtab_list_lock);
|
|---|
| 422 | list_append(&mtab_ent->link, &mtab_list);
|
|---|
| 423 | mtab_size++;
|
|---|
| 424 | fibril_mutex_unlock(&mtab_list_lock);
|
|---|
| 425 |
|
|---|
| 426 | free(mp);
|
|---|
| 427 | free(fs_name);
|
|---|
| 428 | free(opts);
|
|---|
| 429 |
|
|---|
| 430 | /* Acknowledge that we know fs_name. */
|
|---|
| 431 | async_answer_0(callid, EOK);
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | void vfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 435 | {
|
|---|
| 436 | int rc;
|
|---|
| 437 | char *mp;
|
|---|
| 438 | vfs_lookup_res_t mp_res;
|
|---|
| 439 | vfs_lookup_res_t mr_res;
|
|---|
| 440 | vfs_node_t *mr_node;
|
|---|
| 441 | async_exch_t *exch;
|
|---|
| 442 |
|
|---|
| 443 | /*
|
|---|
| 444 | * Receive the mount point path.
|
|---|
| 445 | */
|
|---|
| 446 | rc = async_data_write_accept((void **) &mp, true, 0, MAX_PATH_LEN,
|
|---|
| 447 | 0, NULL);
|
|---|
| 448 | if (rc != EOK)
|
|---|
| 449 | async_answer_0(rid, rc);
|
|---|
| 450 |
|
|---|
| 451 | /*
|
|---|
| 452 | * Taking the namespace lock will do two things for us. First, it will
|
|---|
| 453 | * prevent races with other lookup operations. Second, it will stop new
|
|---|
| 454 | * references to already existing VFS nodes and creation of new VFS
|
|---|
| 455 | * nodes. This is because new references are added as a result of some
|
|---|
| 456 | * lookup operation or at least of some operation which is protected by
|
|---|
| 457 | * the namespace lock.
|
|---|
| 458 | */
|
|---|
| 459 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| 460 |
|
|---|
| 461 | /*
|
|---|
| 462 | * Lookup the mounted root and instantiate it.
|
|---|
| 463 | */
|
|---|
| 464 | rc = vfs_lookup_internal(root, mp, 0, &mr_res);
|
|---|
| 465 | if (rc != EOK) {
|
|---|
| 466 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 467 | free(mp);
|
|---|
| 468 | async_answer_0(rid, rc);
|
|---|
| 469 | return;
|
|---|
| 470 | }
|
|---|
| 471 | mr_node = vfs_node_get(&mr_res);
|
|---|
| 472 | if (!mr_node) {
|
|---|
| 473 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 474 | free(mp);
|
|---|
| 475 | async_answer_0(rid, ENOMEM);
|
|---|
| 476 | return;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | /*
|
|---|
| 480 | * Count the total number of references for the mounted file system. We
|
|---|
| 481 | * are expecting at least two. One which we got above and one which we
|
|---|
| 482 | * got when the file system was mounted. If we find more, it means that
|
|---|
| 483 | * the file system cannot be gracefully unmounted at the moment because
|
|---|
| 484 | * someone is working with it.
|
|---|
| 485 | */
|
|---|
| 486 | if (vfs_nodes_refcount_sum_get(mr_node->fs_handle,
|
|---|
| 487 | mr_node->service_id) != 2) {
|
|---|
| 488 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 489 | vfs_node_put(mr_node);
|
|---|
| 490 | free(mp);
|
|---|
| 491 | async_answer_0(rid, EBUSY);
|
|---|
| 492 | return;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | if (str_cmp(mp, "/") == 0) {
|
|---|
| 496 |
|
|---|
| 497 | /*
|
|---|
| 498 | * Unmounting the root file system.
|
|---|
| 499 | *
|
|---|
| 500 | * In this case, there is no mount point node and we send
|
|---|
| 501 | * VFS_OUT_UNMOUNTED directly to the mounted file system.
|
|---|
| 502 | */
|
|---|
| 503 |
|
|---|
| 504 | exch = vfs_exchange_grab(mr_node->fs_handle);
|
|---|
| 505 | rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
|---|
| 506 | mr_node->service_id);
|
|---|
| 507 | vfs_exchange_release(exch);
|
|---|
| 508 |
|
|---|
| 509 | if (rc != EOK) {
|
|---|
| 510 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 511 | free(mp);
|
|---|
| 512 | vfs_node_put(mr_node);
|
|---|
| 513 | async_answer_0(rid, rc);
|
|---|
| 514 | return;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | root = NULL;
|
|---|
| 518 | } else {
|
|---|
| 519 |
|
|---|
| 520 | /*
|
|---|
| 521 | * Unmounting a non-root file system.
|
|---|
| 522 | *
|
|---|
| 523 | * We have a regular mount point node representing the parent
|
|---|
| 524 | * file system, so we delegate the operation to it.
|
|---|
| 525 | */
|
|---|
| 526 |
|
|---|
| 527 | rc = vfs_lookup_internal(root, mp, L_MP, &mp_res);
|
|---|
| 528 | if (rc != EOK) {
|
|---|
| 529 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 530 | free(mp);
|
|---|
| 531 | vfs_node_put(mr_node);
|
|---|
| 532 | async_answer_0(rid, rc);
|
|---|
| 533 | return;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | vfs_node_t *mp_node = vfs_node_get(&mp_res);
|
|---|
| 537 | if (!mp_node) {
|
|---|
| 538 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 539 | free(mp);
|
|---|
| 540 | vfs_node_put(mr_node);
|
|---|
| 541 | async_answer_0(rid, ENOMEM);
|
|---|
| 542 | return;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | exch = vfs_exchange_grab(mp_node->fs_handle);
|
|---|
| 546 | rc = async_req_2_0(exch, VFS_OUT_UNMOUNT,
|
|---|
| 547 | mp_node->service_id, mp_node->index);
|
|---|
| 548 | vfs_exchange_release(exch);
|
|---|
| 549 |
|
|---|
| 550 | if (rc != EOK) {
|
|---|
| 551 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 552 | free(mp);
|
|---|
| 553 | vfs_node_put(mp_node);
|
|---|
| 554 | vfs_node_put(mr_node);
|
|---|
| 555 | async_answer_0(rid, rc);
|
|---|
| 556 | return;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | /* Drop the reference we got above. */
|
|---|
| 560 | vfs_node_put(mp_node);
|
|---|
| 561 | /* Drop the reference from when the file system was mounted. */
|
|---|
| 562 | vfs_node_put(mp_node);
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | /*
|
|---|
| 566 | * All went well, the mounted file system was successfully unmounted.
|
|---|
| 567 | * The only thing left is to forget the unmounted root VFS node.
|
|---|
| 568 | */
|
|---|
| 569 | vfs_node_forget(mr_node);
|
|---|
| 570 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 571 |
|
|---|
| 572 | fibril_mutex_lock(&mtab_list_lock);
|
|---|
| 573 |
|
|---|
| 574 | int found = 0;
|
|---|
| 575 |
|
|---|
| 576 | list_foreach(mtab_list, cur) {
|
|---|
| 577 | mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
|
|---|
| 578 | link);
|
|---|
| 579 |
|
|---|
| 580 | if (str_cmp(mtab_ent->mp, mp) == 0) {
|
|---|
| 581 | list_remove(&mtab_ent->link);
|
|---|
| 582 | mtab_size--;
|
|---|
| 583 | free(mtab_ent);
|
|---|
| 584 | found = 1;
|
|---|
| 585 | break;
|
|---|
| 586 | }
|
|---|
| 587 | }
|
|---|
| 588 | assert(found);
|
|---|
| 589 | fibril_mutex_unlock(&mtab_list_lock);
|
|---|
| 590 |
|
|---|
| 591 | free(mp);
|
|---|
| 592 |
|
|---|
| 593 | async_answer_0(rid, EOK);
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | static inline bool walk_flags_valid(int flags)
|
|---|
| 597 | {
|
|---|
| 598 | if ((flags&~WALK_ALL_FLAGS) != 0) {
|
|---|
| 599 | return false;
|
|---|
| 600 | }
|
|---|
| 601 | if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
|
|---|
| 602 | return false;
|
|---|
| 603 | }
|
|---|
| 604 | if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
|
|---|
| 605 | return false;
|
|---|
| 606 | }
|
|---|
| 607 | if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
|
|---|
| 608 | if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
|
|---|
| 609 | return false;
|
|---|
| 610 | }
|
|---|
| 611 | }
|
|---|
| 612 | return true;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | static inline int walk_lookup_flags(int flags)
|
|---|
| 616 | {
|
|---|
| 617 | int lflags = 0;
|
|---|
| 618 | if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
|
|---|
| 619 | lflags |= L_CREATE;
|
|---|
| 620 | }
|
|---|
| 621 | if (flags&WALK_MUST_CREATE) {
|
|---|
| 622 | lflags |= L_EXCLUSIVE;
|
|---|
| 623 | }
|
|---|
| 624 | if (flags&WALK_REGULAR) {
|
|---|
| 625 | lflags |= L_FILE;
|
|---|
| 626 | }
|
|---|
| 627 | if (flags&WALK_DIRECTORY) {
|
|---|
| 628 | lflags |= L_DIRECTORY;
|
|---|
| 629 | }
|
|---|
| 630 | return lflags;
|
|---|
| 631 | }
|
|---|
| 632 |
|
|---|
| 633 | void vfs_walk(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 634 | {
|
|---|
| 635 | /*
|
|---|
| 636 | * Parent is our relative root for file lookup.
|
|---|
| 637 | * For defined flags, see <ipc/vfs.h>.
|
|---|
| 638 | */
|
|---|
| 639 | int parentfd = IPC_GET_ARG1(*request);
|
|---|
| 640 | int flags = IPC_GET_ARG2(*request);
|
|---|
| 641 |
|
|---|
| 642 | if (!walk_flags_valid(flags)) {
|
|---|
| 643 | async_answer_0(rid, EINVAL);
|
|---|
| 644 | return;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | char *path;
|
|---|
| 648 | int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
|
|---|
| 649 |
|
|---|
| 650 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| 651 | vfs_file_t *parent = NULL;
|
|---|
| 652 | vfs_node_t *parent_node = root;
|
|---|
| 653 | // TODO: Client-side root.
|
|---|
| 654 | if (parentfd != -1) {
|
|---|
| 655 | parent = vfs_file_get(parentfd);
|
|---|
| 656 | if (!parent) {
|
|---|
| 657 | free(path);
|
|---|
| 658 | async_answer_0(rid, EBADF);
|
|---|
| 659 | return;
|
|---|
| 660 | }
|
|---|
| 661 | parent_node = parent->node;
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | fibril_rwlock_read_lock(&namespace_rwlock);
|
|---|
| 665 |
|
|---|
| 666 | vfs_lookup_res_t lr;
|
|---|
| 667 | rc = vfs_lookup_internal(parent_node, path, walk_lookup_flags(flags), &lr);
|
|---|
| 668 | free(path);
|
|---|
| 669 |
|
|---|
| 670 | if (rc != EOK) {
|
|---|
| 671 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 672 | if (parent) {
|
|---|
| 673 | vfs_file_put(parent);
|
|---|
| 674 | }
|
|---|
| 675 | async_answer_0(rid, rc);
|
|---|
| 676 | return;
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | vfs_node_t *node = vfs_node_get(&lr);
|
|---|
| 680 |
|
|---|
| 681 | int fd = vfs_fd_alloc(false);
|
|---|
| 682 | if (fd < 0) {
|
|---|
| 683 | vfs_node_put(node);
|
|---|
| 684 | if (parent) {
|
|---|
| 685 | vfs_file_put(parent);
|
|---|
| 686 | }
|
|---|
| 687 | async_answer_0(rid, fd);
|
|---|
| 688 | return;
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 692 | assert(file != NULL);
|
|---|
| 693 |
|
|---|
| 694 | file->node = node;
|
|---|
| 695 | if (parent) {
|
|---|
| 696 | file->permissions = parent->permissions;
|
|---|
| 697 | } else {
|
|---|
| 698 | file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
|
|---|
| 699 | }
|
|---|
| 700 | file->open_read = false;
|
|---|
| 701 | file->open_write = false;
|
|---|
| 702 |
|
|---|
| 703 | vfs_file_put(file);
|
|---|
| 704 | if (parent) {
|
|---|
| 705 | vfs_file_put(parent);
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 709 |
|
|---|
| 710 | async_answer_1(rid, EOK, fd);
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | void vfs_open2(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 714 | {
|
|---|
| 715 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 716 | int flags = IPC_GET_ARG2(*request);
|
|---|
| 717 |
|
|---|
| 718 | if (flags == 0) {
|
|---|
| 719 | async_answer_0(rid, EINVAL);
|
|---|
| 720 | return;
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 724 | if (!file) {
|
|---|
| 725 | async_answer_0(rid, EBADF);
|
|---|
| 726 | return;
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | if ((flags & ~file->permissions) != 0) {
|
|---|
| 730 | vfs_file_put(file);
|
|---|
| 731 | async_answer_0(rid, EPERM);
|
|---|
| 732 | return;
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | file->open_read = (flags & MODE_READ) != 0;
|
|---|
| 736 | file->open_write = (flags & (MODE_WRITE | MODE_APPEND)) != 0;
|
|---|
| 737 | file->append = (flags & MODE_APPEND) != 0;
|
|---|
| 738 |
|
|---|
| 739 | if (!file->open_read && !file->open_write) {
|
|---|
| 740 | vfs_file_put(file);
|
|---|
| 741 | async_answer_0(rid, EINVAL);
|
|---|
| 742 | return;
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | if (file->node->type == VFS_NODE_DIRECTORY && file->open_write) {
|
|---|
| 746 | file->open_read = file->open_write = false;
|
|---|
| 747 | vfs_file_put(file);
|
|---|
| 748 | async_answer_0(rid, EINVAL);
|
|---|
| 749 | return;
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | int rc = vfs_open_node_remote(file->node);
|
|---|
| 753 | if (rc != EOK) {
|
|---|
| 754 | file->open_read = file->open_write = false;
|
|---|
| 755 | vfs_file_put(file);
|
|---|
| 756 | async_answer_0(rid, rc);
|
|---|
| 757 | return;
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | vfs_file_put(file);
|
|---|
| 761 | async_answer_0(rid, EOK);
|
|---|
| 762 | }
|
|---|
| 763 |
|
|---|
| 764 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 765 | {
|
|---|
| 766 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 767 |
|
|---|
| 768 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| 769 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 770 | if (!file) {
|
|---|
| 771 | async_answer_0(rid, ENOENT);
|
|---|
| 772 | return;
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | /*
|
|---|
| 776 | * Lock the open file structure so that no other thread can manipulate
|
|---|
| 777 | * the same open file at a time.
|
|---|
| 778 | */
|
|---|
| 779 | fibril_mutex_lock(&file->lock);
|
|---|
| 780 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
|---|
| 781 |
|
|---|
| 782 | /* Make a VFS_OUT_SYMC request at the destination FS server. */
|
|---|
| 783 | aid_t msg;
|
|---|
| 784 | ipc_call_t answer;
|
|---|
| 785 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
|---|
| 786 | file->node->index, &answer);
|
|---|
| 787 |
|
|---|
| 788 | vfs_exchange_release(fs_exch);
|
|---|
| 789 |
|
|---|
| 790 | /* Wait for reply from the FS server. */
|
|---|
| 791 | sysarg_t rc;
|
|---|
| 792 | async_wait_for(msg, &rc);
|
|---|
| 793 |
|
|---|
| 794 | fibril_mutex_unlock(&file->lock);
|
|---|
| 795 |
|
|---|
| 796 | vfs_file_put(file);
|
|---|
| 797 | async_answer_0(rid, rc);
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 801 | {
|
|---|
| 802 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 803 | int ret = vfs_fd_free(fd);
|
|---|
| 804 | async_answer_0(rid, ret);
|
|---|
| 805 | }
|
|---|
| 806 |
|
|---|
| 807 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
|---|
| 808 | {
|
|---|
| 809 | /*
|
|---|
| 810 | * The following code strongly depends on the fact that the files data
|
|---|
| 811 | * structure can be only accessed by a single fibril and all file
|
|---|
| 812 | * operations are serialized (i.e. the reads and writes cannot
|
|---|
| 813 | * interleave and a file cannot be closed while it is being read).
|
|---|
| 814 | *
|
|---|
| 815 | * Additional synchronization needs to be added once the table of
|
|---|
| 816 | * open files supports parallel access!
|
|---|
| 817 | */
|
|---|
| 818 |
|
|---|
| 819 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 820 |
|
|---|
| 821 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| 822 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 823 | if (!file) {
|
|---|
| 824 | async_answer_0(rid, ENOENT);
|
|---|
| 825 | return;
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | /*
|
|---|
| 829 | * Lock the open file structure so that no other thread can manipulate
|
|---|
| 830 | * the same open file at a time.
|
|---|
| 831 | */
|
|---|
| 832 | fibril_mutex_lock(&file->lock);
|
|---|
| 833 |
|
|---|
| 834 | if ((read && !file->open_read) || (!read && !file->open_write)) {
|
|---|
| 835 | fibril_mutex_unlock(&file->lock);
|
|---|
| 836 | async_answer_0(rid, EINVAL);
|
|---|
| 837 | return;
|
|---|
| 838 | }
|
|---|
| 839 |
|
|---|
| 840 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
|---|
| 841 | assert(fs_info);
|
|---|
| 842 |
|
|---|
| 843 | /*
|
|---|
| 844 | * Lock the file's node so that no other client can read/write to it at
|
|---|
| 845 | * the same time unless the FS supports concurrent reads/writes and its
|
|---|
| 846 | * write implementation does not modify the file size.
|
|---|
| 847 | */
|
|---|
| 848 | if ((read) ||
|
|---|
| 849 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
|---|
| 850 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
|---|
| 851 | else
|
|---|
| 852 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
|---|
| 853 |
|
|---|
| 854 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
|---|
| 855 | /*
|
|---|
| 856 | * Make sure that no one is modifying the namespace
|
|---|
| 857 | * while we are in readdir().
|
|---|
| 858 | */
|
|---|
| 859 | assert(read);
|
|---|
| 860 | fibril_rwlock_read_lock(&namespace_rwlock);
|
|---|
| 861 | }
|
|---|
| 862 |
|
|---|
| 863 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
|---|
| 864 |
|
|---|
| 865 | /*
|
|---|
| 866 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
|---|
| 867 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
|---|
| 868 | * destination FS server. The call will be routed as if sent by
|
|---|
| 869 | * ourselves. Note that call arguments are immutable in this case so we
|
|---|
| 870 | * don't have to bother.
|
|---|
| 871 | */
|
|---|
| 872 | sysarg_t rc;
|
|---|
| 873 | ipc_call_t answer;
|
|---|
| 874 | if (read) {
|
|---|
| 875 | rc = async_data_read_forward_4_1(fs_exch, VFS_OUT_READ,
|
|---|
| 876 | file->node->service_id, file->node->index,
|
|---|
| 877 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
|---|
| 878 | } else {
|
|---|
| 879 | if (file->append)
|
|---|
| 880 | file->pos = vfs_node_get_size(file->node);
|
|---|
| 881 |
|
|---|
| 882 | rc = async_data_write_forward_4_1(fs_exch, VFS_OUT_WRITE,
|
|---|
| 883 | file->node->service_id, file->node->index,
|
|---|
| 884 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | vfs_exchange_release(fs_exch);
|
|---|
| 888 |
|
|---|
| 889 | size_t bytes = IPC_GET_ARG1(answer);
|
|---|
| 890 |
|
|---|
| 891 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
|---|
| 892 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | /* Unlock the VFS node. */
|
|---|
| 896 | if ((read) ||
|
|---|
| 897 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
|---|
| 898 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
|---|
| 899 | else {
|
|---|
| 900 | /* Update the cached version of node's size. */
|
|---|
| 901 | if (rc == EOK)
|
|---|
| 902 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
|---|
| 903 | IPC_GET_ARG3(answer));
|
|---|
| 904 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
|---|
| 905 | }
|
|---|
| 906 |
|
|---|
| 907 | /* Update the position pointer and unlock the open file. */
|
|---|
| 908 | if (rc == EOK)
|
|---|
| 909 | file->pos += bytes;
|
|---|
| 910 | fibril_mutex_unlock(&file->lock);
|
|---|
| 911 | vfs_file_put(file);
|
|---|
| 912 |
|
|---|
| 913 | /*
|
|---|
| 914 | * FS server's reply is the final result of the whole operation we
|
|---|
| 915 | * return to the client.
|
|---|
| 916 | */
|
|---|
| 917 | async_answer_1(rid, rc, bytes);
|
|---|
| 918 | }
|
|---|
| 919 |
|
|---|
| 920 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 921 | {
|
|---|
| 922 | vfs_rdwr(rid, request, true);
|
|---|
| 923 | }
|
|---|
| 924 |
|
|---|
| 925 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 926 | {
|
|---|
| 927 | vfs_rdwr(rid, request, false);
|
|---|
| 928 | }
|
|---|
| 929 |
|
|---|
| 930 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 931 | {
|
|---|
| 932 | int fd = (int) IPC_GET_ARG1(*request);
|
|---|
| 933 | off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
|---|
| 934 | IPC_GET_ARG3(*request));
|
|---|
| 935 | int whence = (int) IPC_GET_ARG4(*request);
|
|---|
| 936 |
|
|---|
| 937 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| 938 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 939 | if (!file) {
|
|---|
| 940 | async_answer_0(rid, ENOENT);
|
|---|
| 941 | return;
|
|---|
| 942 | }
|
|---|
| 943 |
|
|---|
| 944 | fibril_mutex_lock(&file->lock);
|
|---|
| 945 |
|
|---|
| 946 | off64_t newoff;
|
|---|
| 947 | switch (whence) {
|
|---|
| 948 | case SEEK_SET:
|
|---|
| 949 | if (off >= 0) {
|
|---|
| 950 | file->pos = (aoff64_t) off;
|
|---|
| 951 | fibril_mutex_unlock(&file->lock);
|
|---|
| 952 | vfs_file_put(file);
|
|---|
| 953 | async_answer_1(rid, EOK, off);
|
|---|
| 954 | return;
|
|---|
| 955 | }
|
|---|
| 956 | break;
|
|---|
| 957 | case SEEK_CUR:
|
|---|
| 958 | if ((off >= 0) && (file->pos + off < file->pos)) {
|
|---|
| 959 | fibril_mutex_unlock(&file->lock);
|
|---|
| 960 | vfs_file_put(file);
|
|---|
| 961 | async_answer_0(rid, EOVERFLOW);
|
|---|
| 962 | return;
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | if ((off < 0) && (file->pos < (aoff64_t) -off)) {
|
|---|
| 966 | fibril_mutex_unlock(&file->lock);
|
|---|
| 967 | vfs_file_put(file);
|
|---|
| 968 | async_answer_0(rid, EOVERFLOW);
|
|---|
| 969 | return;
|
|---|
| 970 | }
|
|---|
| 971 |
|
|---|
| 972 | file->pos += off;
|
|---|
| 973 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
|---|
| 974 |
|
|---|
| 975 | fibril_mutex_unlock(&file->lock);
|
|---|
| 976 | vfs_file_put(file);
|
|---|
| 977 | async_answer_2(rid, EOK, LOWER32(newoff),
|
|---|
| 978 | UPPER32(newoff));
|
|---|
| 979 | return;
|
|---|
| 980 | case SEEK_END:
|
|---|
| 981 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
|---|
| 982 | aoff64_t size = vfs_node_get_size(file->node);
|
|---|
| 983 |
|
|---|
| 984 | if ((off >= 0) && (size + off < size)) {
|
|---|
| 985 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
|---|
| 986 | fibril_mutex_unlock(&file->lock);
|
|---|
| 987 | vfs_file_put(file);
|
|---|
| 988 | async_answer_0(rid, EOVERFLOW);
|
|---|
| 989 | return;
|
|---|
| 990 | }
|
|---|
| 991 |
|
|---|
| 992 | if ((off < 0) && (size < (aoff64_t) -off)) {
|
|---|
| 993 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
|---|
| 994 | fibril_mutex_unlock(&file->lock);
|
|---|
| 995 | vfs_file_put(file);
|
|---|
| 996 | async_answer_0(rid, EOVERFLOW);
|
|---|
| 997 | return;
|
|---|
| 998 | }
|
|---|
| 999 |
|
|---|
| 1000 | file->pos = size + off;
|
|---|
| 1001 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
|---|
| 1002 |
|
|---|
| 1003 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
|---|
| 1004 | fibril_mutex_unlock(&file->lock);
|
|---|
| 1005 | vfs_file_put(file);
|
|---|
| 1006 | async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
|
|---|
| 1007 | return;
|
|---|
| 1008 | }
|
|---|
| 1009 |
|
|---|
| 1010 | fibril_mutex_unlock(&file->lock);
|
|---|
| 1011 | vfs_file_put(file);
|
|---|
| 1012 | async_answer_0(rid, EINVAL);
|
|---|
| 1013 | }
|
|---|
| 1014 |
|
|---|
| 1015 | int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
|---|
| 1016 | fs_index_t index, aoff64_t size)
|
|---|
| 1017 | {
|
|---|
| 1018 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
|---|
| 1019 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
|---|
| 1020 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
|---|
| 1021 | UPPER32(size));
|
|---|
| 1022 | vfs_exchange_release(exch);
|
|---|
| 1023 |
|
|---|
| 1024 | return (int) rc;
|
|---|
| 1025 | }
|
|---|
| 1026 |
|
|---|
| 1027 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1028 | {
|
|---|
| 1029 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 1030 | aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
|---|
| 1031 | IPC_GET_ARG3(*request));
|
|---|
| 1032 | int rc;
|
|---|
| 1033 |
|
|---|
| 1034 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 1035 | if (!file) {
|
|---|
| 1036 | async_answer_0(rid, ENOENT);
|
|---|
| 1037 | return;
|
|---|
| 1038 | }
|
|---|
| 1039 | fibril_mutex_lock(&file->lock);
|
|---|
| 1040 |
|
|---|
| 1041 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
|---|
| 1042 | rc = vfs_truncate_internal(file->node->fs_handle,
|
|---|
| 1043 | file->node->service_id, file->node->index, size);
|
|---|
| 1044 | if (rc == EOK)
|
|---|
| 1045 | file->node->size = size;
|
|---|
| 1046 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
|---|
| 1047 |
|
|---|
| 1048 | fibril_mutex_unlock(&file->lock);
|
|---|
| 1049 | vfs_file_put(file);
|
|---|
| 1050 | async_answer_0(rid, (sysarg_t)rc);
|
|---|
| 1051 | }
|
|---|
| 1052 |
|
|---|
| 1053 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1054 | {
|
|---|
| 1055 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 1056 | sysarg_t rc;
|
|---|
| 1057 |
|
|---|
| 1058 | vfs_file_t *file = vfs_file_get(fd);
|
|---|
| 1059 | if (!file) {
|
|---|
| 1060 | async_answer_0(rid, ENOENT);
|
|---|
| 1061 | return;
|
|---|
| 1062 | }
|
|---|
| 1063 |
|
|---|
| 1064 | ipc_callid_t callid;
|
|---|
| 1065 | if (!async_data_read_receive(&callid, NULL)) {
|
|---|
| 1066 | vfs_file_put(file);
|
|---|
| 1067 | async_answer_0(callid, EINVAL);
|
|---|
| 1068 | async_answer_0(rid, EINVAL);
|
|---|
| 1069 | return;
|
|---|
| 1070 | }
|
|---|
| 1071 |
|
|---|
| 1072 | fibril_mutex_lock(&file->lock);
|
|---|
| 1073 |
|
|---|
| 1074 | async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
|
|---|
| 1075 |
|
|---|
| 1076 | aid_t msg;
|
|---|
| 1077 | msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
|
|---|
| 1078 | file->node->index, true, NULL);
|
|---|
| 1079 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
|---|
| 1080 |
|
|---|
| 1081 | vfs_exchange_release(exch);
|
|---|
| 1082 |
|
|---|
| 1083 | async_wait_for(msg, &rc);
|
|---|
| 1084 |
|
|---|
| 1085 | fibril_mutex_unlock(&file->lock);
|
|---|
| 1086 | vfs_file_put(file);
|
|---|
| 1087 | async_answer_0(rid, rc);
|
|---|
| 1088 | }
|
|---|
| 1089 |
|
|---|
| 1090 | static void out_destroy(vfs_triplet_t *file)
|
|---|
| 1091 | {
|
|---|
| 1092 | async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
|
|---|
| 1093 | async_msg_2(exch, VFS_OUT_DESTROY,
|
|---|
| 1094 | (sysarg_t) file->service_id, (sysarg_t) file->index);
|
|---|
| 1095 | vfs_exchange_release(exch);
|
|---|
| 1096 | }
|
|---|
| 1097 |
|
|---|
| 1098 | void vfs_unlink2(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1099 | {
|
|---|
| 1100 | int rc;
|
|---|
| 1101 | char *path;
|
|---|
| 1102 | vfs_file_t *parent = NULL;
|
|---|
| 1103 | vfs_file_t *expect = NULL;
|
|---|
| 1104 | vfs_node_t *parent_node = root;
|
|---|
| 1105 |
|
|---|
| 1106 | int parentfd = IPC_GET_ARG1(*request);
|
|---|
| 1107 | int expectfd = IPC_GET_ARG2(*request);
|
|---|
| 1108 | int wflag = IPC_GET_ARG3(*request);
|
|---|
| 1109 |
|
|---|
| 1110 | rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
|---|
| 1111 | if (rc != EOK) {
|
|---|
| 1112 | async_answer_0(rid, rc);
|
|---|
| 1113 | return;
|
|---|
| 1114 | }
|
|---|
| 1115 |
|
|---|
| 1116 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| 1117 |
|
|---|
| 1118 | int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
|
|---|
| 1119 |
|
|---|
| 1120 | if (parentfd >= 0) {
|
|---|
| 1121 | parent = vfs_file_get(parentfd);
|
|---|
| 1122 | if (!parent) {
|
|---|
| 1123 | rc = ENOENT;
|
|---|
| 1124 | goto exit;
|
|---|
| 1125 | }
|
|---|
| 1126 | parent_node = parent->node;
|
|---|
| 1127 | }
|
|---|
| 1128 |
|
|---|
| 1129 | if (expectfd >= 0) {
|
|---|
| 1130 | expect = vfs_file_get(expectfd);
|
|---|
| 1131 | if (!expect) {
|
|---|
| 1132 | rc = ENOENT;
|
|---|
| 1133 | goto exit;
|
|---|
| 1134 | }
|
|---|
| 1135 |
|
|---|
| 1136 | vfs_lookup_res_t lr;
|
|---|
| 1137 | rc = vfs_lookup_internal(parent_node, path, lflag, &lr);
|
|---|
| 1138 | if (rc != EOK) {
|
|---|
| 1139 | goto exit;
|
|---|
| 1140 | }
|
|---|
| 1141 |
|
|---|
| 1142 | if (__builtin_memcmp(&lr.triplet, expect->node, sizeof(vfs_triplet_t)) != 0) {
|
|---|
| 1143 | rc = ENOENT;
|
|---|
| 1144 | goto exit;
|
|---|
| 1145 | }
|
|---|
| 1146 |
|
|---|
| 1147 | vfs_file_put(expect);
|
|---|
| 1148 | expect = NULL;
|
|---|
| 1149 | }
|
|---|
| 1150 |
|
|---|
| 1151 | vfs_lookup_res_t lr;
|
|---|
| 1152 | rc = vfs_lookup_internal(parent_node, path, lflag | L_UNLINK, &lr);
|
|---|
| 1153 | if (rc != EOK) {
|
|---|
| 1154 | goto exit;
|
|---|
| 1155 | }
|
|---|
| 1156 |
|
|---|
| 1157 | /* If the node is not held by anyone, try to destroy it. */
|
|---|
| 1158 | if (vfs_node_peek(&lr) == NULL) {
|
|---|
| 1159 | out_destroy(&lr.triplet);
|
|---|
| 1160 | }
|
|---|
| 1161 |
|
|---|
| 1162 | exit:
|
|---|
| 1163 | if (path) {
|
|---|
| 1164 | free(path);
|
|---|
| 1165 | }
|
|---|
| 1166 | if (parent) {
|
|---|
| 1167 | vfs_file_put(parent);
|
|---|
| 1168 | }
|
|---|
| 1169 | if (expect) {
|
|---|
| 1170 | vfs_file_put(expect);
|
|---|
| 1171 | }
|
|---|
| 1172 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 1173 | async_answer_0(rid, rc);
|
|---|
| 1174 | }
|
|---|
| 1175 |
|
|---|
| 1176 | static size_t shared_path(char *a, char *b)
|
|---|
| 1177 | {
|
|---|
| 1178 | size_t res = 0;
|
|---|
| 1179 |
|
|---|
| 1180 | while (a[res] == b[res] && a[res] != 0) {
|
|---|
| 1181 | res++;
|
|---|
| 1182 | }
|
|---|
| 1183 |
|
|---|
| 1184 | if (a[res] == b[res]) {
|
|---|
| 1185 | return res;
|
|---|
| 1186 | }
|
|---|
| 1187 |
|
|---|
| 1188 | res--;
|
|---|
| 1189 | while (a[res] != '/') {
|
|---|
| 1190 | res--;
|
|---|
| 1191 | }
|
|---|
| 1192 | return res;
|
|---|
| 1193 | }
|
|---|
| 1194 |
|
|---|
| 1195 | static int vfs_rename_internal(vfs_node_t *base, char *old, char *new)
|
|---|
| 1196 | {
|
|---|
| 1197 | assert(base != NULL);
|
|---|
| 1198 | assert(old != NULL);
|
|---|
| 1199 | assert(new != NULL);
|
|---|
| 1200 |
|
|---|
| 1201 | vfs_lookup_res_t base_lr;
|
|---|
| 1202 | vfs_lookup_res_t old_lr;
|
|---|
| 1203 | vfs_lookup_res_t new_lr_orig;
|
|---|
| 1204 | bool orig_unlinked = false;
|
|---|
| 1205 |
|
|---|
| 1206 | int rc;
|
|---|
| 1207 |
|
|---|
| 1208 | size_t shared = shared_path(old, new);
|
|---|
| 1209 |
|
|---|
| 1210 | /* Do not allow one path to be a prefix of the other. */
|
|---|
| 1211 | if (old[shared] == 0 || new[shared] == 0) {
|
|---|
| 1212 | return EINVAL;
|
|---|
| 1213 | }
|
|---|
| 1214 | assert(old[shared] == '/');
|
|---|
| 1215 | assert(new[shared] == '/');
|
|---|
| 1216 |
|
|---|
| 1217 | fibril_rwlock_write_lock(&namespace_rwlock);
|
|---|
| 1218 |
|
|---|
| 1219 | /* Resolve the shared portion of the path first. */
|
|---|
| 1220 | if (shared != 0) {
|
|---|
| 1221 | old[shared] = 0;
|
|---|
| 1222 | rc = vfs_lookup_internal(base, old, L_DIRECTORY, &base_lr);
|
|---|
| 1223 | if (rc != EOK) {
|
|---|
| 1224 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 1225 | return rc;
|
|---|
| 1226 | }
|
|---|
| 1227 |
|
|---|
| 1228 | base = vfs_node_get(&base_lr);
|
|---|
| 1229 | old[shared] = '/';
|
|---|
| 1230 | old += shared;
|
|---|
| 1231 | new += shared;
|
|---|
| 1232 | } else {
|
|---|
| 1233 | vfs_node_addref(base);
|
|---|
| 1234 | }
|
|---|
| 1235 |
|
|---|
| 1236 |
|
|---|
| 1237 | rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS, &new_lr_orig);
|
|---|
| 1238 | if (rc == EOK) {
|
|---|
| 1239 | orig_unlinked = true;
|
|---|
| 1240 | } else if (rc != ENOENT) {
|
|---|
| 1241 | vfs_node_put(base);
|
|---|
| 1242 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 1243 | return rc;
|
|---|
| 1244 | }
|
|---|
| 1245 |
|
|---|
| 1246 | rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS, &old_lr);
|
|---|
| 1247 | if (rc != EOK) {
|
|---|
| 1248 | if (orig_unlinked) {
|
|---|
| 1249 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
|---|
| 1250 | }
|
|---|
| 1251 | vfs_node_put(base);
|
|---|
| 1252 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 1253 | return rc;
|
|---|
| 1254 | }
|
|---|
| 1255 |
|
|---|
| 1256 | rc = vfs_link_internal(base, new, &old_lr.triplet);
|
|---|
| 1257 | if (rc != EOK) {
|
|---|
| 1258 | vfs_link_internal(base, old, &old_lr.triplet);
|
|---|
| 1259 | if (orig_unlinked) {
|
|---|
| 1260 | vfs_link_internal(base, new, &new_lr_orig.triplet);
|
|---|
| 1261 | }
|
|---|
| 1262 | vfs_node_put(base);
|
|---|
| 1263 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 1264 | return rc;
|
|---|
| 1265 | }
|
|---|
| 1266 |
|
|---|
| 1267 | /* If the node is not held by anyone, try to destroy it. */
|
|---|
| 1268 | if (orig_unlinked && vfs_node_peek(&new_lr_orig) == NULL) {
|
|---|
| 1269 | out_destroy(&new_lr_orig.triplet);
|
|---|
| 1270 | }
|
|---|
| 1271 |
|
|---|
| 1272 | vfs_node_put(base);
|
|---|
| 1273 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
|---|
| 1274 | return EOK;
|
|---|
| 1275 | }
|
|---|
| 1276 |
|
|---|
| 1277 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1278 | {
|
|---|
| 1279 | /* The common base directory. */
|
|---|
| 1280 | int basefd;
|
|---|
| 1281 | char *old = NULL;
|
|---|
| 1282 | char *new = NULL;
|
|---|
| 1283 | vfs_file_t *base = NULL;
|
|---|
| 1284 | int rc;
|
|---|
| 1285 |
|
|---|
| 1286 | basefd = IPC_GET_ARG1(*request);
|
|---|
| 1287 |
|
|---|
| 1288 | /* Retrieve the old path. */
|
|---|
| 1289 | rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
|---|
| 1290 | if (rc != EOK) {
|
|---|
| 1291 | goto out;
|
|---|
| 1292 | }
|
|---|
| 1293 |
|
|---|
| 1294 | /* Retrieve the new path. */
|
|---|
| 1295 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
|---|
| 1296 | if (rc != EOK) {
|
|---|
| 1297 | goto out;
|
|---|
| 1298 | }
|
|---|
| 1299 |
|
|---|
| 1300 | size_t olen;
|
|---|
| 1301 | size_t nlen;
|
|---|
| 1302 | char *oldc = canonify(old, &olen);
|
|---|
| 1303 | char *newc = canonify(new, &nlen);
|
|---|
| 1304 |
|
|---|
| 1305 | if ((!oldc) || (!newc)) {
|
|---|
| 1306 | rc = EINVAL;
|
|---|
| 1307 | goto out;
|
|---|
| 1308 | }
|
|---|
| 1309 |
|
|---|
| 1310 | assert(oldc[olen] == '\0');
|
|---|
| 1311 | assert(newc[nlen] == '\0');
|
|---|
| 1312 |
|
|---|
| 1313 | /* Lookup the file structure corresponding to the file descriptor. */
|
|---|
| 1314 | vfs_node_t *base_node = root;
|
|---|
| 1315 | // TODO: Client-side root.
|
|---|
| 1316 | if (basefd != -1) {
|
|---|
| 1317 | base = vfs_file_get(basefd);
|
|---|
| 1318 | if (!base) {
|
|---|
| 1319 | rc = EBADF;
|
|---|
| 1320 | goto out;
|
|---|
| 1321 | }
|
|---|
| 1322 | base_node = base->node;
|
|---|
| 1323 | }
|
|---|
| 1324 |
|
|---|
| 1325 | rc = vfs_rename_internal(base_node, oldc, newc);
|
|---|
| 1326 |
|
|---|
| 1327 | out:
|
|---|
| 1328 | async_answer_0(rid, rc);
|
|---|
| 1329 |
|
|---|
| 1330 | if (old) {
|
|---|
| 1331 | free(old);
|
|---|
| 1332 | }
|
|---|
| 1333 | if (new) {
|
|---|
| 1334 | free(new);
|
|---|
| 1335 | }
|
|---|
| 1336 | if (base) {
|
|---|
| 1337 | vfs_file_put(base);
|
|---|
| 1338 | }
|
|---|
| 1339 | }
|
|---|
| 1340 |
|
|---|
| 1341 | void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1342 | {
|
|---|
| 1343 | int oldfd = IPC_GET_ARG1(*request);
|
|---|
| 1344 | int newfd = IPC_GET_ARG2(*request);
|
|---|
| 1345 |
|
|---|
| 1346 | /* If the file descriptors are the same, do nothing. */
|
|---|
| 1347 | if (oldfd == newfd) {
|
|---|
| 1348 | async_answer_1(rid, EOK, newfd);
|
|---|
| 1349 | return;
|
|---|
| 1350 | }
|
|---|
| 1351 |
|
|---|
| 1352 | /* Lookup the file structure corresponding to oldfd. */
|
|---|
| 1353 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
|---|
| 1354 | if (!oldfile) {
|
|---|
| 1355 | async_answer_0(rid, EBADF);
|
|---|
| 1356 | return;
|
|---|
| 1357 | }
|
|---|
| 1358 |
|
|---|
| 1359 | /*
|
|---|
| 1360 | * Lock the open file structure so that no other thread can manipulate
|
|---|
| 1361 | * the same open file at a time.
|
|---|
| 1362 | */
|
|---|
| 1363 | fibril_mutex_lock(&oldfile->lock);
|
|---|
| 1364 |
|
|---|
| 1365 | /* Make sure newfd is closed. */
|
|---|
| 1366 | (void) vfs_fd_free(newfd);
|
|---|
| 1367 |
|
|---|
| 1368 | /* Assign the old file to newfd. */
|
|---|
| 1369 | int ret = vfs_fd_assign(oldfile, newfd);
|
|---|
| 1370 | fibril_mutex_unlock(&oldfile->lock);
|
|---|
| 1371 | vfs_file_put(oldfile);
|
|---|
| 1372 |
|
|---|
| 1373 | if (ret != EOK)
|
|---|
| 1374 | async_answer_0(rid, ret);
|
|---|
| 1375 | else
|
|---|
| 1376 | async_answer_1(rid, EOK, newfd);
|
|---|
| 1377 | }
|
|---|
| 1378 |
|
|---|
| 1379 | void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1380 | {
|
|---|
| 1381 | int fd = vfs_wait_handle_internal();
|
|---|
| 1382 | async_answer_1(rid, EOK, fd);
|
|---|
| 1383 | }
|
|---|
| 1384 |
|
|---|
| 1385 | void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 1386 | {
|
|---|
| 1387 | ipc_callid_t callid;
|
|---|
| 1388 | ipc_call_t data;
|
|---|
| 1389 | sysarg_t rc = EOK;
|
|---|
| 1390 | size_t len;
|
|---|
| 1391 |
|
|---|
| 1392 | fibril_mutex_lock(&mtab_list_lock);
|
|---|
| 1393 |
|
|---|
| 1394 | /* Send to the caller the number of mounted filesystems */
|
|---|
| 1395 | callid = async_get_call(&data);
|
|---|
| 1396 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
|---|
| 1397 | rc = ENOTSUP;
|
|---|
| 1398 | async_answer_0(callid, rc);
|
|---|
| 1399 | goto exit;
|
|---|
| 1400 | }
|
|---|
| 1401 | async_answer_1(callid, EOK, mtab_size);
|
|---|
| 1402 |
|
|---|
| 1403 | list_foreach(mtab_list, cur) {
|
|---|
| 1404 | mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
|
|---|
| 1405 | link);
|
|---|
| 1406 |
|
|---|
| 1407 | rc = ENOTSUP;
|
|---|
| 1408 |
|
|---|
| 1409 | if (!async_data_read_receive(&callid, &len)) {
|
|---|
| 1410 | async_answer_0(callid, rc);
|
|---|
| 1411 | goto exit;
|
|---|
| 1412 | }
|
|---|
| 1413 |
|
|---|
| 1414 | (void) async_data_read_finalize(callid, mtab_ent->mp,
|
|---|
| 1415 | str_size(mtab_ent->mp));
|
|---|
| 1416 |
|
|---|
| 1417 | if (!async_data_read_receive(&callid, &len)) {
|
|---|
| 1418 | async_answer_0(callid, rc);
|
|---|
| 1419 | goto exit;
|
|---|
| 1420 | }
|
|---|
| 1421 |
|
|---|
| 1422 | (void) async_data_read_finalize(callid, mtab_ent->opts,
|
|---|
| 1423 | str_size(mtab_ent->opts));
|
|---|
| 1424 |
|
|---|
| 1425 | if (!async_data_read_receive(&callid, &len)) {
|
|---|
| 1426 | async_answer_0(callid, rc);
|
|---|
| 1427 | goto exit;
|
|---|
| 1428 | }
|
|---|
| 1429 |
|
|---|
| 1430 | (void) async_data_read_finalize(callid, mtab_ent->fs_name,
|
|---|
| 1431 | str_size(mtab_ent->fs_name));
|
|---|
| 1432 |
|
|---|
| 1433 | callid = async_get_call(&data);
|
|---|
| 1434 |
|
|---|
| 1435 | if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
|
|---|
| 1436 | async_answer_0(callid, rc);
|
|---|
| 1437 | goto exit;
|
|---|
| 1438 | }
|
|---|
| 1439 |
|
|---|
| 1440 | rc = EOK;
|
|---|
| 1441 | async_answer_2(callid, rc, mtab_ent->instance,
|
|---|
| 1442 | mtab_ent->service_id);
|
|---|
| 1443 | }
|
|---|
| 1444 |
|
|---|
| 1445 | exit:
|
|---|
| 1446 | fibril_mutex_unlock(&mtab_list_lock);
|
|---|
| 1447 | async_answer_0(rid, rc);
|
|---|
| 1448 | }
|
|---|
| 1449 |
|
|---|
| 1450 | /**
|
|---|
| 1451 | * @}
|
|---|
| 1452 | */
|
|---|