[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 |
|
---|
[ae75e2e3] | 466 | free(mp);
|
---|
[79ae36dd] | 467 |
|
---|
| 468 | exch = vfs_exchange_grab(mr_node->fs_handle);
|
---|
| 469 | rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
|
---|
[15f3c3f] | 470 | mr_node->service_id);
|
---|
[79ae36dd] | 471 | vfs_exchange_release(exch);
|
---|
| 472 |
|
---|
[ae75e2e3] | 473 | if (rc != EOK) {
|
---|
| 474 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 475 | vfs_node_put(mr_node);
|
---|
[ffa2c8ef] | 476 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 477 | return;
|
---|
| 478 | }
|
---|
[79ae36dd] | 479 |
|
---|
[ae75e2e3] | 480 | rootfs.fs_handle = 0;
|
---|
[15f3c3f] | 481 | rootfs.service_id = 0;
|
---|
[ae75e2e3] | 482 | } else {
|
---|
[79ae36dd] | 483 |
|
---|
[ae75e2e3] | 484 | /*
|
---|
| 485 | * Unmounting a non-root file system.
|
---|
| 486 | *
|
---|
| 487 | * We have a regular mount point node representing the parent
|
---|
| 488 | * file system, so we delegate the operation to it.
|
---|
| 489 | */
|
---|
[79ae36dd] | 490 |
|
---|
[f7376cbf] | 491 | rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
|
---|
[ae75e2e3] | 492 | free(mp);
|
---|
| 493 | if (rc != EOK) {
|
---|
| 494 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 495 | vfs_node_put(mr_node);
|
---|
[ffa2c8ef] | 496 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 497 | return;
|
---|
| 498 | }
|
---|
[79ae36dd] | 499 |
|
---|
[ae75e2e3] | 500 | vfs_node_t *mp_node = vfs_node_get(&mp_res);
|
---|
| 501 | if (!mp_node) {
|
---|
| 502 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
| 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);
|
---|
| 515 | vfs_node_put(mp_node);
|
---|
| 516 | vfs_node_put(mr_node);
|
---|
[ffa2c8ef] | 517 | async_answer_0(rid, rc);
|
---|
[ae75e2e3] | 518 | return;
|
---|
| 519 | }
|
---|
[79ae36dd] | 520 |
|
---|
[ae75e2e3] | 521 | /* Drop the reference we got above. */
|
---|
| 522 | vfs_node_put(mp_node);
|
---|
| 523 | /* Drop the reference from when the file system was mounted. */
|
---|
| 524 | vfs_node_put(mp_node);
|
---|
| 525 | }
|
---|
[79ae36dd] | 526 |
|
---|
[ae75e2e3] | 527 | /*
|
---|
| 528 | * All went well, the mounted file system was successfully unmounted.
|
---|
| 529 | * The only thing left is to forget the unmounted root VFS node.
|
---|
| 530 | */
|
---|
| 531 | vfs_node_forget(mr_node);
|
---|
[79ae36dd] | 532 |
|
---|
[ae75e2e3] | 533 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 534 | async_answer_0(rid, EOK);
|
---|
[7f5e070] | 535 | }
|
---|
| 536 |
|
---|
[861e7d1] | 537 | void vfs_open(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 538 | {
|
---|
| 539 | /*
|
---|
[ae78b530] | 540 | * The POSIX interface is open(path, oflag, mode).
|
---|
[4198f9c3] | 541 | * We can receive oflags and mode along with the VFS_IN_OPEN call;
|
---|
| 542 | * the path will need to arrive in another call.
|
---|
[ae78b530] | 543 | *
|
---|
| 544 | * We also receive one private, non-POSIX set of flags called lflag
|
---|
| 545 | * used to pass information to vfs_lookup_internal().
|
---|
[861e7d1] | 546 | */
|
---|
[ae78b530] | 547 | int lflag = IPC_GET_ARG1(*request);
|
---|
| 548 | int oflag = IPC_GET_ARG2(*request);
|
---|
| 549 | int mode = IPC_GET_ARG3(*request);
|
---|
[dd2cfa7] | 550 |
|
---|
| 551 | /* Ignore mode for now. */
|
---|
| 552 | (void) mode;
|
---|
[05b9912] | 553 |
|
---|
[b17186d] | 554 | /*
|
---|
| 555 | * Make sure that we are called with exactly one of L_FILE and
|
---|
[f7376cbf] | 556 | * L_DIRECTORY. Make sure that the user does not pass L_OPEN,
|
---|
| 557 | * L_ROOT or L_MP.
|
---|
[b17186d] | 558 | */
|
---|
[230260ac] | 559 | if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
|
---|
| 560 | ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
|
---|
[f7376cbf] | 561 | (lflag & (L_OPEN | L_ROOT | L_MP))) {
|
---|
[ffa2c8ef] | 562 | async_answer_0(rid, EINVAL);
|
---|
[b17186d] | 563 | return;
|
---|
| 564 | }
|
---|
[05b9912] | 565 |
|
---|
[2db4ac8] | 566 | if (oflag & O_CREAT)
|
---|
| 567 | lflag |= L_CREATE;
|
---|
| 568 | if (oflag & O_EXCL)
|
---|
| 569 | lflag |= L_EXCLUSIVE;
|
---|
[05b9912] | 570 |
|
---|
[472c09d] | 571 | char *path;
|
---|
[4cac2d69] | 572 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 573 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 574 | async_answer_0(rid, rc);
|
---|
[861e7d1] | 575 | return;
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | /*
|
---|
| 579 | * Avoid the race condition in which the file can be deleted before we
|
---|
| 580 | * find/create-and-lock the VFS node corresponding to the looked-up
|
---|
| 581 | * triplet.
|
---|
| 582 | */
|
---|
[2db4ac8] | 583 | if (lflag & L_CREATE)
|
---|
[230260ac] | 584 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[2db4ac8] | 585 | else
|
---|
[230260ac] | 586 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[05b9912] | 587 |
|
---|
[72bde81] | 588 | /* The path is now populated and we can call vfs_lookup_internal(). */
|
---|
[eb27ce5a] | 589 | vfs_lookup_res_t lr;
|
---|
[05b9912] | 590 | rc = vfs_lookup_internal(path, lflag | L_OPEN, &lr, NULL);
|
---|
| 591 | if (rc != EOK) {
|
---|
[2db4ac8] | 592 | if (lflag & L_CREATE)
|
---|
[230260ac] | 593 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[2db4ac8] | 594 | else
|
---|
[230260ac] | 595 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 596 | async_answer_0(rid, rc);
|
---|
[861e7d1] | 597 | free(path);
|
---|
| 598 | return;
|
---|
| 599 | }
|
---|
[05b9912] | 600 |
|
---|
[7fe1f75] | 601 | /* Path is no longer needed. */
|
---|
[861e7d1] | 602 | free(path);
|
---|
[05b9912] | 603 |
|
---|
[eb27ce5a] | 604 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[2db4ac8] | 605 | if (lflag & L_CREATE)
|
---|
[230260ac] | 606 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[2db4ac8] | 607 | else
|
---|
[230260ac] | 608 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[05b9912] | 609 |
|
---|
[7fe1f75] | 610 | /* Truncate the file if requested and if necessary. */
|
---|
| 611 | if (oflag & O_TRUNC) {
|
---|
[230260ac] | 612 | fibril_rwlock_write_lock(&node->contents_rwlock);
|
---|
[7fe1f75] | 613 | if (node->size) {
|
---|
| 614 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
[15f3c3f] | 615 | node->service_id, node->index, 0);
|
---|
[7fe1f75] | 616 | if (rc) {
|
---|
[230260ac] | 617 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
[7fe1f75] | 618 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 619 | async_answer_0(rid, rc);
|
---|
[7fe1f75] | 620 | return;
|
---|
| 621 | }
|
---|
| 622 | node->size = 0;
|
---|
| 623 | }
|
---|
[230260ac] | 624 | fibril_rwlock_write_unlock(&node->contents_rwlock);
|
---|
[7fe1f75] | 625 | }
|
---|
[05b9912] | 626 |
|
---|
[861e7d1] | 627 | /*
|
---|
| 628 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
| 629 | * structure.
|
---|
| 630 | */
|
---|
[2b88074b] | 631 | int fd = vfs_fd_alloc((oflag & O_DESC) != 0);
|
---|
[861e7d1] | 632 | if (fd < 0) {
|
---|
| 633 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 634 | async_answer_0(rid, fd);
|
---|
[861e7d1] | 635 | return;
|
---|
| 636 | }
|
---|
| 637 | vfs_file_t *file = vfs_file_get(fd);
|
---|
[179d052] | 638 | assert(file);
|
---|
[861e7d1] | 639 | file->node = node;
|
---|
[05b9912] | 640 | if (oflag & O_APPEND)
|
---|
[15b9970] | 641 | file->append = true;
|
---|
[05b9912] | 642 |
|
---|
[861e7d1] | 643 | /*
|
---|
| 644 | * The following increase in reference count is for the fact that the
|
---|
| 645 | * file is being opened and that a file structure is pointing to it.
|
---|
| 646 | * It is necessary so that the file will not disappear when
|
---|
| 647 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
[4198f9c3] | 648 | * respective VFS_IN_CLOSE.
|
---|
[861e7d1] | 649 | */
|
---|
| 650 | vfs_node_addref(node);
|
---|
| 651 | vfs_node_put(node);
|
---|
[4fe94c66] | 652 | vfs_file_put(file);
|
---|
[05b9912] | 653 |
|
---|
| 654 | /* Success! Return the new file descriptor to the client. */
|
---|
[ffa2c8ef] | 655 | async_answer_1(rid, EOK, fd);
|
---|
[05b9912] | 656 | }
|
---|
[861e7d1] | 657 |
|
---|
[05b9912] | 658 | void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 659 | {
|
---|
| 660 | int fd = IPC_GET_ARG1(*request);
|
---|
| 661 |
|
---|
| 662 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
| 663 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 664 | if (!file) {
|
---|
[ffa2c8ef] | 665 | async_answer_0(rid, ENOENT);
|
---|
[05b9912] | 666 | return;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | /*
|
---|
| 670 | * Lock the open file structure so that no other thread can manipulate
|
---|
| 671 | * the same open file at a time.
|
---|
| 672 | */
|
---|
[230260ac] | 673 | fibril_mutex_lock(&file->lock);
|
---|
[79ae36dd] | 674 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
[05b9912] | 675 |
|
---|
[4198f9c3] | 676 | /* Make a VFS_OUT_SYMC request at the destination FS server. */
|
---|
[05b9912] | 677 | aid_t msg;
|
---|
| 678 | ipc_call_t answer;
|
---|
[15f3c3f] | 679 | msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
|
---|
[4198f9c3] | 680 | file->node->index, &answer);
|
---|
[79ae36dd] | 681 |
|
---|
| 682 | vfs_exchange_release(fs_exch);
|
---|
| 683 |
|
---|
[05b9912] | 684 | /* Wait for reply from the FS server. */
|
---|
[96b02eb9] | 685 | sysarg_t rc;
|
---|
[05b9912] | 686 | async_wait_for(msg, &rc);
|
---|
| 687 |
|
---|
[230260ac] | 688 | fibril_mutex_unlock(&file->lock);
|
---|
[79ae36dd] | 689 |
|
---|
[4fe94c66] | 690 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 691 | async_answer_0(rid, rc);
|
---|
[e704503] | 692 | }
|
---|
| 693 |
|
---|
[05b9912] | 694 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 695 | {
|
---|
| 696 | int fd = IPC_GET_ARG1(*request);
|
---|
[79ae36dd] | 697 | int ret = vfs_fd_free(fd);
|
---|
[ffa2c8ef] | 698 | async_answer_0(rid, ret);
|
---|
[05b9912] | 699 | }
|
---|
| 700 |
|
---|
[861e7d1] | 701 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
| 702 | {
|
---|
| 703 | /*
|
---|
| 704 | * The following code strongly depends on the fact that the files data
|
---|
| 705 | * structure can be only accessed by a single fibril and all file
|
---|
| 706 | * operations are serialized (i.e. the reads and writes cannot
|
---|
| 707 | * interleave and a file cannot be closed while it is being read).
|
---|
| 708 | *
|
---|
| 709 | * Additional synchronization needs to be added once the table of
|
---|
| 710 | * open files supports parallel access!
|
---|
| 711 | */
|
---|
[79ae36dd] | 712 |
|
---|
[861e7d1] | 713 | int fd = IPC_GET_ARG1(*request);
|
---|
[6c89f20] | 714 |
|
---|
[72bde81] | 715 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
[861e7d1] | 716 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 717 | if (!file) {
|
---|
[ffa2c8ef] | 718 | async_answer_0(rid, ENOENT);
|
---|
[861e7d1] | 719 | return;
|
---|
| 720 | }
|
---|
[6c89f20] | 721 |
|
---|
[861e7d1] | 722 | /*
|
---|
| 723 | * Lock the open file structure so that no other thread can manipulate
|
---|
| 724 | * the same open file at a time.
|
---|
| 725 | */
|
---|
[230260ac] | 726 | fibril_mutex_lock(&file->lock);
|
---|
[79ae36dd] | 727 |
|
---|
| 728 | vfs_info_t *fs_info = fs_handle_to_info(file->node->fs_handle);
|
---|
| 729 | assert(fs_info);
|
---|
| 730 |
|
---|
[861e7d1] | 731 | /*
|
---|
| 732 | * Lock the file's node so that no other client can read/write to it at
|
---|
[c2f4b6b] | 733 | * the same time unless the FS supports concurrent reads/writes and its
|
---|
| 734 | * write implementation does not modify the file size.
|
---|
[861e7d1] | 735 | */
|
---|
[79ae36dd] | 736 | if ((read) ||
|
---|
| 737 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
---|
[230260ac] | 738 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
[861e7d1] | 739 | else
|
---|
[230260ac] | 740 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[79ae36dd] | 741 |
|
---|
[b17186d] | 742 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
| 743 | /*
|
---|
| 744 | * Make sure that no one is modifying the namespace
|
---|
| 745 | * while we are in readdir().
|
---|
| 746 | */
|
---|
| 747 | assert(read);
|
---|
[230260ac] | 748 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
[b17186d] | 749 | }
|
---|
[6c89f20] | 750 |
|
---|
[79ae36dd] | 751 | async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
[861e7d1] | 752 |
|
---|
| 753 | /*
|
---|
[b4cbef1] | 754 | * Make a VFS_READ/VFS_WRITE request at the destination FS server
|
---|
| 755 | * and forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
[861e7d1] | 756 | * destination FS server. The call will be routed as if sent by
|
---|
| 757 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
| 758 | * don't have to bother.
|
---|
| 759 | */
|
---|
[96b02eb9] | 760 | sysarg_t rc;
|
---|
[b4cbef1] | 761 | ipc_call_t answer;
|
---|
| 762 | if (read) {
|
---|
[5bb9907] | 763 | rc = async_data_read_forward_4_1(fs_exch, VFS_OUT_READ,
|
---|
[86ffa27f] | 764 | file->node->service_id, file->node->index,
|
---|
[5bb9907] | 765 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
---|
[b4cbef1] | 766 | } else {
|
---|
[3a4b3ba] | 767 | if (file->append)
|
---|
| 768 | file->pos = file->node->size;
|
---|
| 769 |
|
---|
[5bb9907] | 770 | rc = async_data_write_forward_4_1(fs_exch, VFS_OUT_WRITE,
|
---|
[86ffa27f] | 771 | file->node->service_id, file->node->index,
|
---|
[5bb9907] | 772 | LOWER32(file->pos), UPPER32(file->pos), &answer);
|
---|
[b4cbef1] | 773 | }
|
---|
[05b9912] | 774 |
|
---|
[79ae36dd] | 775 | vfs_exchange_release(fs_exch);
|
---|
[34ca870] | 776 |
|
---|
[861e7d1] | 777 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
[b4cbef1] | 778 |
|
---|
[b17186d] | 779 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
[230260ac] | 780 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[6c89f20] | 781 |
|
---|
[72bde81] | 782 | /* Unlock the VFS node. */
|
---|
[79ae36dd] | 783 | if ((read) ||
|
---|
| 784 | ((fs_info->concurrent_read_write) && (fs_info->write_retains_size)))
|
---|
[230260ac] | 785 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
[861e7d1] | 786 | else {
|
---|
| 787 | /* Update the cached version of node's size. */
|
---|
[f7017572] | 788 | if (rc == EOK)
|
---|
[5bb9907] | 789 | file->node->size = MERGE_LOUP32(IPC_GET_ARG2(answer),
|
---|
| 790 | IPC_GET_ARG3(answer));
|
---|
[230260ac] | 791 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
[861e7d1] | 792 | }
|
---|
[6c89f20] | 793 |
|
---|
[72bde81] | 794 | /* Update the position pointer and unlock the open file. */
|
---|
[f7017572] | 795 | if (rc == EOK)
|
---|
| 796 | file->pos += bytes;
|
---|
[230260ac] | 797 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 798 | vfs_file_put(file);
|
---|
| 799 |
|
---|
[861e7d1] | 800 | /*
|
---|
| 801 | * FS server's reply is the final result of the whole operation we
|
---|
| 802 | * return to the client.
|
---|
| 803 | */
|
---|
[ffa2c8ef] | 804 | async_answer_1(rid, rc, bytes);
|
---|
[861e7d1] | 805 | }
|
---|
| 806 |
|
---|
| 807 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 808 | {
|
---|
| 809 | vfs_rdwr(rid, request, true);
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 813 | {
|
---|
| 814 | vfs_rdwr(rid, request, false);
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 818 | {
|
---|
| 819 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
[8df8415] | 820 | off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
| 821 | IPC_GET_ARG3(*request));
|
---|
[ed903174] | 822 | int whence = (int) IPC_GET_ARG4(*request);
|
---|
| 823 |
|
---|
[72bde81] | 824 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
[861e7d1] | 825 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 826 | if (!file) {
|
---|
[ffa2c8ef] | 827 | async_answer_0(rid, ENOENT);
|
---|
[861e7d1] | 828 | return;
|
---|
| 829 | }
|
---|
[ed903174] | 830 |
|
---|
[230260ac] | 831 | fibril_mutex_lock(&file->lock);
|
---|
[ed903174] | 832 |
|
---|
| 833 | off64_t newoff;
|
---|
| 834 | switch (whence) {
|
---|
[8df8415] | 835 | case SEEK_SET:
|
---|
| 836 | if (off >= 0) {
|
---|
| 837 | file->pos = (aoff64_t) off;
|
---|
[230260ac] | 838 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 839 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 840 | async_answer_1(rid, EOK, off);
|
---|
[861e7d1] | 841 | return;
|
---|
[8df8415] | 842 | }
|
---|
| 843 | break;
|
---|
| 844 | case SEEK_CUR:
|
---|
| 845 | if ((off >= 0) && (file->pos + off < file->pos)) {
|
---|
| 846 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 847 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 848 | async_answer_0(rid, EOVERFLOW);
|
---|
[8df8415] | 849 | return;
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | if ((off < 0) && (file->pos < (aoff64_t) -off)) {
|
---|
| 853 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 854 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 855 | async_answer_0(rid, EOVERFLOW);
|
---|
[8df8415] | 856 | return;
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 | file->pos += off;
|
---|
| 860 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
| 861 |
|
---|
| 862 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 863 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 864 | async_answer_2(rid, EOK, LOWER32(newoff),
|
---|
[8df8415] | 865 | UPPER32(newoff));
|
---|
| 866 | return;
|
---|
| 867 | case SEEK_END:
|
---|
| 868 | fibril_rwlock_read_lock(&file->node->contents_rwlock);
|
---|
| 869 | aoff64_t size = file->node->size;
|
---|
| 870 |
|
---|
| 871 | if ((off >= 0) && (size + off < size)) {
|
---|
[ed903174] | 872 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
[230260ac] | 873 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 874 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 875 | async_answer_0(rid, EOVERFLOW);
|
---|
[861e7d1] | 876 | return;
|
---|
[8df8415] | 877 | }
|
---|
| 878 |
|
---|
| 879 | if ((off < 0) && (size < (aoff64_t) -off)) {
|
---|
| 880 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
| 881 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 882 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 883 | async_answer_0(rid, EOVERFLOW);
|
---|
[8df8415] | 884 | return;
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | file->pos = size + off;
|
---|
| 888 | newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
|
---|
| 889 |
|
---|
| 890 | fibril_rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
| 891 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 892 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 893 | async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
|
---|
[8df8415] | 894 | return;
|
---|
[861e7d1] | 895 | }
|
---|
[ed903174] | 896 |
|
---|
[230260ac] | 897 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 898 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 899 | async_answer_0(rid, EINVAL);
|
---|
[861e7d1] | 900 | }
|
---|
| 901 |
|
---|
[15f3c3f] | 902 | int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
|
---|
[ed903174] | 903 | fs_index_t index, aoff64_t size)
|
---|
[7fe1f75] | 904 | {
|
---|
[79ae36dd] | 905 | async_exch_t *exch = vfs_exchange_grab(fs_handle);
|
---|
| 906 | sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
|
---|
[15f3c3f] | 907 | (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
|
---|
[79ae36dd] | 908 | UPPER32(size));
|
---|
| 909 | vfs_exchange_release(exch);
|
---|
[7fe1f75] | 910 |
|
---|
[79ae36dd] | 911 | return (int) rc;
|
---|
[7fe1f75] | 912 | }
|
---|
| 913 |
|
---|
[0ee4322] | 914 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 915 | {
|
---|
| 916 | int fd = IPC_GET_ARG1(*request);
|
---|
[8df8415] | 917 | aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
| 918 | IPC_GET_ARG3(*request));
|
---|
[7fe1f75] | 919 | int rc;
|
---|
[0ee4322] | 920 |
|
---|
| 921 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 922 | if (!file) {
|
---|
[ffa2c8ef] | 923 | async_answer_0(rid, ENOENT);
|
---|
[0ee4322] | 924 | return;
|
---|
| 925 | }
|
---|
[230260ac] | 926 | fibril_mutex_lock(&file->lock);
|
---|
[0ee4322] | 927 |
|
---|
[230260ac] | 928 | fibril_rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[7fe1f75] | 929 | rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
[15f3c3f] | 930 | file->node->service_id, file->node->index, size);
|
---|
[0ee4322] | 931 | if (rc == EOK)
|
---|
| 932 | file->node->size = size;
|
---|
[230260ac] | 933 | fibril_rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
[0ee4322] | 934 |
|
---|
[230260ac] | 935 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 936 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 937 | async_answer_0(rid, (sysarg_t)rc);
|
---|
[861e7d1] | 938 | }
|
---|
| 939 |
|
---|
[852b801] | 940 | void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 941 | {
|
---|
| 942 | int fd = IPC_GET_ARG1(*request);
|
---|
[96b02eb9] | 943 | sysarg_t rc;
|
---|
[852b801] | 944 |
|
---|
| 945 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 946 | if (!file) {
|
---|
[ffa2c8ef] | 947 | async_answer_0(rid, ENOENT);
|
---|
[852b801] | 948 | return;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | ipc_callid_t callid;
|
---|
[0da4e41] | 952 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
[4fe94c66] | 953 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 954 | async_answer_0(callid, EINVAL);
|
---|
| 955 | async_answer_0(rid, EINVAL);
|
---|
[852b801] | 956 | return;
|
---|
| 957 | }
|
---|
| 958 |
|
---|
| 959 | fibril_mutex_lock(&file->lock);
|
---|
| 960 |
|
---|
[79ae36dd] | 961 | async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
|
---|
[852b801] | 962 |
|
---|
| 963 | aid_t msg;
|
---|
[15f3c3f] | 964 | msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
|
---|
[852b801] | 965 | file->node->index, true, NULL);
|
---|
[79ae36dd] | 966 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
| 967 |
|
---|
| 968 | vfs_exchange_release(exch);
|
---|
| 969 |
|
---|
[852b801] | 970 | async_wait_for(msg, &rc);
|
---|
[79ae36dd] | 971 |
|
---|
[852b801] | 972 | fibril_mutex_unlock(&file->lock);
|
---|
[4fe94c66] | 973 | vfs_file_put(file);
|
---|
[ffa2c8ef] | 974 | async_answer_0(rid, rc);
|
---|
[852b801] | 975 | }
|
---|
| 976 |
|
---|
| 977 | void vfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 978 | {
|
---|
[472c09d] | 979 | char *path;
|
---|
[4cac2d69] | 980 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 981 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 982 | async_answer_0(rid, rc);
|
---|
[415c7e0d] | 983 | return;
|
---|
| 984 | }
|
---|
[472c09d] | 985 |
|
---|
| 986 | ipc_callid_t callid;
|
---|
[0da4e41] | 987 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
[415c7e0d] | 988 | free(path);
|
---|
[ffa2c8ef] | 989 | async_answer_0(callid, EINVAL);
|
---|
| 990 | async_answer_0(rid, EINVAL);
|
---|
[415c7e0d] | 991 | return;
|
---|
| 992 | }
|
---|
| 993 |
|
---|
| 994 | vfs_lookup_res_t lr;
|
---|
| 995 | fibril_rwlock_read_lock(&namespace_rwlock);
|
---|
| 996 | rc = vfs_lookup_internal(path, L_NONE, &lr, NULL);
|
---|
| 997 | free(path);
|
---|
| 998 | if (rc != EOK) {
|
---|
| 999 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1000 | async_answer_0(callid, rc);
|
---|
| 1001 | async_answer_0(rid, rc);
|
---|
[415c7e0d] | 1002 | return;
|
---|
| 1003 | }
|
---|
| 1004 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
| 1005 | if (!node) {
|
---|
| 1006 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1007 | async_answer_0(callid, ENOMEM);
|
---|
| 1008 | async_answer_0(rid, ENOMEM);
|
---|
[415c7e0d] | 1009 | return;
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | fibril_rwlock_read_unlock(&namespace_rwlock);
|
---|
| 1013 |
|
---|
[79ae36dd] | 1014 | async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
|
---|
| 1015 |
|
---|
[415c7e0d] | 1016 | aid_t msg;
|
---|
[15f3c3f] | 1017 | msg = async_send_3(exch, VFS_OUT_STAT, node->service_id,
|
---|
[415c7e0d] | 1018 | node->index, false, NULL);
|
---|
[79ae36dd] | 1019 | async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
| 1020 |
|
---|
| 1021 | vfs_exchange_release(exch);
|
---|
[057760d3] | 1022 |
|
---|
[96b02eb9] | 1023 | sysarg_t rv;
|
---|
[057760d3] | 1024 | async_wait_for(msg, &rv);
|
---|
[415c7e0d] | 1025 |
|
---|
[ffa2c8ef] | 1026 | async_answer_0(rid, rv);
|
---|
[415c7e0d] | 1027 |
|
---|
| 1028 | vfs_node_put(node);
|
---|
[852b801] | 1029 | }
|
---|
| 1030 |
|
---|
[72bde81] | 1031 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1032 | {
|
---|
| 1033 | int mode = IPC_GET_ARG1(*request);
|
---|
[472c09d] | 1034 |
|
---|
| 1035 | char *path;
|
---|
[4cac2d69] | 1036 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1037 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 1038 | async_answer_0(rid, rc);
|
---|
[72bde81] | 1039 | return;
|
---|
| 1040 | }
|
---|
[472c09d] | 1041 |
|
---|
[dd2cfa7] | 1042 | /* Ignore mode for now. */
|
---|
| 1043 | (void) mode;
|
---|
[72bde81] | 1044 |
|
---|
[230260ac] | 1045 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[72bde81] | 1046 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
|
---|
[d6084ef] | 1047 | rc = vfs_lookup_internal(path, lflag, NULL, NULL);
|
---|
[230260ac] | 1048 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[72bde81] | 1049 | free(path);
|
---|
[ffa2c8ef] | 1050 | async_answer_0(rid, rc);
|
---|
[72bde81] | 1051 | }
|
---|
| 1052 |
|
---|
[f15cf1a6] | 1053 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1054 | {
|
---|
| 1055 | int lflag = IPC_GET_ARG1(*request);
|
---|
[472c09d] | 1056 |
|
---|
| 1057 | char *path;
|
---|
[4cac2d69] | 1058 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1059 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 1060 | async_answer_0(rid, rc);
|
---|
[f15cf1a6] | 1061 | return;
|
---|
| 1062 | }
|
---|
| 1063 |
|
---|
[230260ac] | 1064 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[f15cf1a6] | 1065 | lflag &= L_DIRECTORY; /* sanitize lflag */
|
---|
| 1066 | vfs_lookup_res_t lr;
|
---|
[a8e9ab8d] | 1067 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
|
---|
[f15cf1a6] | 1068 | free(path);
|
---|
| 1069 | if (rc != EOK) {
|
---|
[230260ac] | 1070 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1071 | async_answer_0(rid, rc);
|
---|
[f15cf1a6] | 1072 | return;
|
---|
| 1073 | }
|
---|
| 1074 |
|
---|
| 1075 | /*
|
---|
| 1076 | * The name has already been unlinked by vfs_lookup_internal().
|
---|
| 1077 | * We have to get and put the VFS node to ensure that it is
|
---|
[4198f9c3] | 1078 | * VFS_OUT_DESTROY'ed after the last reference to it is dropped.
|
---|
[f15cf1a6] | 1079 | */
|
---|
| 1080 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[553492be] | 1081 | fibril_mutex_lock(&nodes_mutex);
|
---|
[f15cf1a6] | 1082 | node->lnkcnt--;
|
---|
[553492be] | 1083 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[230260ac] | 1084 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[f15cf1a6] | 1085 | vfs_node_put(node);
|
---|
[ffa2c8ef] | 1086 | async_answer_0(rid, EOK);
|
---|
[f15cf1a6] | 1087 | }
|
---|
| 1088 |
|
---|
[a8e9ab8d] | 1089 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1090 | {
|
---|
| 1091 | /* Retrieve the old path. */
|
---|
[472c09d] | 1092 | char *old;
|
---|
[4cac2d69] | 1093 | int rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1094 | if (rc != EOK) {
|
---|
[ffa2c8ef] | 1095 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1096 | return;
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 | /* Retrieve the new path. */
|
---|
[472c09d] | 1100 | char *new;
|
---|
[4cac2d69] | 1101 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
---|
[472c09d] | 1102 | if (rc != EOK) {
|
---|
[a8e9ab8d] | 1103 | free(old);
|
---|
[ffa2c8ef] | 1104 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1105 | return;
|
---|
| 1106 | }
|
---|
[472c09d] | 1107 |
|
---|
| 1108 | size_t olen;
|
---|
| 1109 | size_t nlen;
|
---|
[732bb0c] | 1110 | char *oldc = canonify(old, &olen);
|
---|
| 1111 | char *newc = canonify(new, &nlen);
|
---|
[472c09d] | 1112 |
|
---|
| 1113 | if ((!oldc) || (!newc)) {
|
---|
[ffa2c8ef] | 1114 | async_answer_0(rid, EINVAL);
|
---|
[a8e9ab8d] | 1115 | free(old);
|
---|
| 1116 | free(new);
|
---|
| 1117 | return;
|
---|
| 1118 | }
|
---|
[472c09d] | 1119 |
|
---|
[732bb0c] | 1120 | oldc[olen] = '\0';
|
---|
| 1121 | newc[nlen] = '\0';
|
---|
[472c09d] | 1122 |
|
---|
[14040e5] | 1123 | if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
|
---|
| 1124 | ((newc[str_length(oldc)] == '/') ||
|
---|
| 1125 | (str_length(oldc) == 1) ||
|
---|
| 1126 | (str_length(oldc) == str_length(newc)))) {
|
---|
| 1127 | /*
|
---|
| 1128 | * oldc is a prefix of newc and either
|
---|
| 1129 | * - newc continues with a / where oldc ends, or
|
---|
| 1130 | * - oldc was / itself, or
|
---|
| 1131 | * - oldc and newc are equal.
|
---|
| 1132 | */
|
---|
[ffa2c8ef] | 1133 | async_answer_0(rid, EINVAL);
|
---|
[a8e9ab8d] | 1134 | free(old);
|
---|
| 1135 | free(new);
|
---|
| 1136 | return;
|
---|
| 1137 | }
|
---|
| 1138 |
|
---|
| 1139 | vfs_lookup_res_t old_lr;
|
---|
| 1140 | vfs_lookup_res_t new_lr;
|
---|
| 1141 | vfs_lookup_res_t new_par_lr;
|
---|
[230260ac] | 1142 | fibril_rwlock_write_lock(&namespace_rwlock);
|
---|
[472c09d] | 1143 |
|
---|
[a8e9ab8d] | 1144 | /* Lookup the node belonging to the old file name. */
|
---|
| 1145 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
|
---|
| 1146 | if (rc != EOK) {
|
---|
[230260ac] | 1147 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1148 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1149 | free(old);
|
---|
| 1150 | free(new);
|
---|
| 1151 | return;
|
---|
| 1152 | }
|
---|
[472c09d] | 1153 |
|
---|
[a8e9ab8d] | 1154 | vfs_node_t *old_node = vfs_node_get(&old_lr);
|
---|
| 1155 | if (!old_node) {
|
---|
[230260ac] | 1156 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[ffa2c8ef] | 1157 | async_answer_0(rid, ENOMEM);
|
---|
[a8e9ab8d] | 1158 | free(old);
|
---|
| 1159 | free(new);
|
---|
| 1160 | return;
|
---|
| 1161 | }
|
---|
[472c09d] | 1162 |
|
---|
[4f46695e] | 1163 | /* Determine the path to the parent of the node with the new name. */
|
---|
| 1164 | char *parentc = str_dup(newc);
|
---|
| 1165 | if (!parentc) {
|
---|
[230260ac] | 1166 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[71af5a4] | 1167 | vfs_node_put(old_node);
|
---|
[ffa2c8ef] | 1168 | async_answer_0(rid, rc);
|
---|
[4f46695e] | 1169 | free(old);
|
---|
| 1170 | free(new);
|
---|
| 1171 | return;
|
---|
| 1172 | }
|
---|
[472c09d] | 1173 |
|
---|
[ae55ee8] | 1174 | char *lastsl = str_rchr(parentc + 1, '/');
|
---|
[4f46695e] | 1175 | if (lastsl)
|
---|
| 1176 | *lastsl = '\0';
|
---|
| 1177 | else
|
---|
| 1178 | parentc[1] = '\0';
|
---|
[472c09d] | 1179 |
|
---|
[a8e9ab8d] | 1180 | /* Lookup parent of the new file name. */
|
---|
[4f46695e] | 1181 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
|
---|
| 1182 | free(parentc); /* not needed anymore */
|
---|
[a8e9ab8d] | 1183 | if (rc != EOK) {
|
---|
[230260ac] | 1184 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[71af5a4] | 1185 | vfs_node_put(old_node);
|
---|
[ffa2c8ef] | 1186 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1187 | free(old);
|
---|
| 1188 | free(new);
|
---|
| 1189 | return;
|
---|
| 1190 | }
|
---|
[472c09d] | 1191 |
|
---|
[a8e9ab8d] | 1192 | /* Check whether linking to the same file system instance. */
|
---|
| 1193 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
|
---|
[15f3c3f] | 1194 | (old_node->service_id != new_par_lr.triplet.service_id)) {
|
---|
[230260ac] | 1195 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[71af5a4] | 1196 | vfs_node_put(old_node);
|
---|
[ffa2c8ef] | 1197 | async_answer_0(rid, EXDEV); /* different file systems */
|
---|
[a8e9ab8d] | 1198 | free(old);
|
---|
| 1199 | free(new);
|
---|
| 1200 | return;
|
---|
| 1201 | }
|
---|
[472c09d] | 1202 |
|
---|
[a8e9ab8d] | 1203 | /* Destroy the old link for the new name. */
|
---|
| 1204 | vfs_node_t *new_node = NULL;
|
---|
| 1205 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
|
---|
[472c09d] | 1206 |
|
---|
[a8e9ab8d] | 1207 | switch (rc) {
|
---|
| 1208 | case ENOENT:
|
---|
| 1209 | /* simply not in our way */
|
---|
| 1210 | break;
|
---|
| 1211 | case EOK:
|
---|
| 1212 | new_node = vfs_node_get(&new_lr);
|
---|
| 1213 | if (!new_node) {
|
---|
[230260ac] | 1214 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[71af5a4] | 1215 | vfs_node_put(old_node);
|
---|
[ffa2c8ef] | 1216 | async_answer_0(rid, ENOMEM);
|
---|
[a8e9ab8d] | 1217 | free(old);
|
---|
| 1218 | free(new);
|
---|
| 1219 | return;
|
---|
| 1220 | }
|
---|
[553492be] | 1221 | fibril_mutex_lock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1222 | new_node->lnkcnt--;
|
---|
[553492be] | 1223 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1224 | break;
|
---|
| 1225 | default:
|
---|
[230260ac] | 1226 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[71af5a4] | 1227 | vfs_node_put(old_node);
|
---|
[ffa2c8ef] | 1228 | async_answer_0(rid, ENOTEMPTY);
|
---|
[a8e9ab8d] | 1229 | free(old);
|
---|
| 1230 | free(new);
|
---|
| 1231 | return;
|
---|
| 1232 | }
|
---|
[472c09d] | 1233 |
|
---|
[a8e9ab8d] | 1234 | /* Create the new link for the new name. */
|
---|
| 1235 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
|
---|
| 1236 | if (rc != EOK) {
|
---|
[230260ac] | 1237 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[71af5a4] | 1238 | vfs_node_put(old_node);
|
---|
[a8e9ab8d] | 1239 | if (new_node)
|
---|
| 1240 | vfs_node_put(new_node);
|
---|
[ffa2c8ef] | 1241 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1242 | free(old);
|
---|
| 1243 | free(new);
|
---|
| 1244 | return;
|
---|
| 1245 | }
|
---|
[472c09d] | 1246 |
|
---|
[553492be] | 1247 | fibril_mutex_lock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1248 | old_node->lnkcnt++;
|
---|
[553492be] | 1249 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[472c09d] | 1250 |
|
---|
[a8e9ab8d] | 1251 | /* Destroy the link for the old name. */
|
---|
| 1252 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
|
---|
| 1253 | if (rc != EOK) {
|
---|
[230260ac] | 1254 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[a8e9ab8d] | 1255 | vfs_node_put(old_node);
|
---|
| 1256 | if (new_node)
|
---|
| 1257 | vfs_node_put(new_node);
|
---|
[ffa2c8ef] | 1258 | async_answer_0(rid, rc);
|
---|
[a8e9ab8d] | 1259 | free(old);
|
---|
| 1260 | free(new);
|
---|
| 1261 | return;
|
---|
| 1262 | }
|
---|
[472c09d] | 1263 |
|
---|
[553492be] | 1264 | fibril_mutex_lock(&nodes_mutex);
|
---|
[a8e9ab8d] | 1265 | old_node->lnkcnt--;
|
---|
[553492be] | 1266 | fibril_mutex_unlock(&nodes_mutex);
|
---|
[230260ac] | 1267 | fibril_rwlock_write_unlock(&namespace_rwlock);
|
---|
[a8e9ab8d] | 1268 | vfs_node_put(old_node);
|
---|
[472c09d] | 1269 |
|
---|
[a8e9ab8d] | 1270 | if (new_node)
|
---|
| 1271 | vfs_node_put(new_node);
|
---|
[472c09d] | 1272 |
|
---|
[a8e9ab8d] | 1273 | free(old);
|
---|
| 1274 | free(new);
|
---|
[ffa2c8ef] | 1275 | async_answer_0(rid, EOK);
|
---|
[a8e9ab8d] | 1276 | }
|
---|
| 1277 |
|
---|
[2b88074b] | 1278 | void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1279 | {
|
---|
| 1280 | int oldfd = IPC_GET_ARG1(*request);
|
---|
| 1281 | int newfd = IPC_GET_ARG2(*request);
|
---|
| 1282 |
|
---|
[4fe94c66] | 1283 | /* If the file descriptors are the same, do nothing. */
|
---|
| 1284 | if (oldfd == newfd) {
|
---|
[ffa2c8ef] | 1285 | async_answer_1(rid, EOK, newfd);
|
---|
[4fe94c66] | 1286 | return;
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
[2b88074b] | 1289 | /* Lookup the file structure corresponding to oldfd. */
|
---|
| 1290 | vfs_file_t *oldfile = vfs_file_get(oldfd);
|
---|
| 1291 | if (!oldfile) {
|
---|
[ffa2c8ef] | 1292 | async_answer_0(rid, EBADF);
|
---|
[2b88074b] | 1293 | return;
|
---|
| 1294 | }
|
---|
| 1295 |
|
---|
| 1296 | /*
|
---|
| 1297 | * Lock the open file structure so that no other thread can manipulate
|
---|
| 1298 | * the same open file at a time.
|
---|
| 1299 | */
|
---|
| 1300 | fibril_mutex_lock(&oldfile->lock);
|
---|
| 1301 |
|
---|
[25bef0ff] | 1302 | /* Make sure newfd is closed. */
|
---|
| 1303 | (void) vfs_fd_free(newfd);
|
---|
[2b88074b] | 1304 |
|
---|
| 1305 | /* Assign the old file to newfd. */
|
---|
| 1306 | int ret = vfs_fd_assign(oldfile, newfd);
|
---|
| 1307 | fibril_mutex_unlock(&oldfile->lock);
|
---|
[4fe94c66] | 1308 | vfs_file_put(oldfile);
|
---|
[2b88074b] | 1309 |
|
---|
| 1310 | if (ret != EOK)
|
---|
[ffa2c8ef] | 1311 | async_answer_0(rid, ret);
|
---|
[2b88074b] | 1312 | else
|
---|
[ffa2c8ef] | 1313 | async_answer_1(rid, EOK, newfd);
|
---|
[2b88074b] | 1314 | }
|
---|
| 1315 |
|
---|
[27b76ca] | 1316 | void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 1317 | {
|
---|
| 1318 | int fd = vfs_wait_handle_internal();
|
---|
| 1319 | async_answer_1(rid, EOK, fd);
|
---|
| 1320 | }
|
---|
| 1321 |
|
---|
[861e7d1] | 1322 | /**
|
---|
| 1323 | * @}
|
---|
[05b9912] | 1324 | */
|
---|