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