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