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