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