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