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