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