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