[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>
|
---|
| 46 | #include <futex.h>
|
---|
| 47 | #include <rwlock.h>
|
---|
| 48 | #include <libadt/list.h>
|
---|
| 49 | #include <unistd.h>
|
---|
| 50 | #include <ctype.h>
|
---|
[2db4ac8] | 51 | #include <fcntl.h>
|
---|
[861e7d1] | 52 | #include <assert.h>
|
---|
[a8e9ab8d] | 53 | #include <vfs/canonify.h>
|
---|
[861e7d1] | 54 |
|
---|
[7fe1f75] | 55 | /* Forward declarations of static functions. */
|
---|
[f2ec8c8] | 56 | static int vfs_truncate_internal(fs_handle_t, dev_handle_t, fs_index_t, size_t);
|
---|
[7fe1f75] | 57 |
|
---|
[8dc72b64] | 58 | /** Pending mount structure. */
|
---|
| 59 | typedef struct {
|
---|
| 60 | link_t link;
|
---|
| 61 | char *fs_name; /**< File system name */
|
---|
| 62 | char *mp; /**< Mount point */
|
---|
[594303b] | 63 | char *opts; /**< Mount options. */
|
---|
[8dc72b64] | 64 | ipc_callid_t callid; /**< Call ID waiting for the mount */
|
---|
| 65 | ipc_callid_t rid; /**< Request ID */
|
---|
| 66 | dev_handle_t dev_handle; /**< Device handle */
|
---|
| 67 | } pending_req_t;
|
---|
| 68 |
|
---|
| 69 | LIST_INITIALIZE(pending_req);
|
---|
| 70 |
|
---|
[861e7d1] | 71 | /**
|
---|
| 72 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
| 73 | * concurrent VFS operation which modifies the file system namespace.
|
---|
| 74 | */
|
---|
| 75 | RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
| 76 |
|
---|
[f49b0ea] | 77 | vfs_pair_t rootfs = {
|
---|
[861e7d1] | 78 | .fs_handle = 0,
|
---|
[f49b0ea] | 79 | .dev_handle = 0
|
---|
[861e7d1] | 80 | };
|
---|
| 81 |
|
---|
[8dc72b64] | 82 | static void vfs_mount_internal(ipc_callid_t rid, dev_handle_t dev_handle,
|
---|
[594303b] | 83 | fs_handle_t fs_handle, char *mp, char *opts)
|
---|
[861e7d1] | 84 | {
|
---|
[8dc72b64] | 85 | vfs_lookup_res_t mp_res;
|
---|
[83937ccd] | 86 | vfs_lookup_res_t mr_res;
|
---|
[861e7d1] | 87 | vfs_node_t *mp_node = NULL;
|
---|
[83937ccd] | 88 | vfs_node_t *mr_node;
|
---|
| 89 | fs_index_t rindex;
|
---|
| 90 | size_t rsize;
|
---|
| 91 | unsigned rlnkcnt;
|
---|
[594303b] | 92 | ipcarg_t rc;
|
---|
[64b67c3] | 93 | int phone;
|
---|
[594303b] | 94 | aid_t msg;
|
---|
| 95 | ipc_call_t answer;
|
---|
[83937ccd] | 96 |
|
---|
[594303b] | 97 | /* Resolve the path to the mountpoint. */
|
---|
[493853ec] | 98 | rwlock_write_lock(&namespace_rwlock);
|
---|
[861e7d1] | 99 | if (rootfs.fs_handle) {
|
---|
[72bde81] | 100 | /* We already have the root FS. */
|
---|
[92fd52d7] | 101 | if (str_cmp(mp, "/") == 0) {
|
---|
[2f60a529] | 102 | /* Trying to mount root FS over root FS */
|
---|
| 103 | ipc_answer_0(rid, EBUSY);
|
---|
[493853ec] | 104 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[2f60a529] | 105 | return;
|
---|
| 106 | }
|
---|
[8dc72b64] | 107 |
|
---|
| 108 | rc = vfs_lookup_internal(mp, L_DIRECTORY, &mp_res, NULL);
|
---|
[861e7d1] | 109 | if (rc != EOK) {
|
---|
[72bde81] | 110 | /* The lookup failed for some reason. */
|
---|
[861e7d1] | 111 | ipc_answer_0(rid, rc);
|
---|
[493853ec] | 112 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 113 | return;
|
---|
| 114 | }
|
---|
[8dc72b64] | 115 |
|
---|
[eb27ce5a] | 116 | mp_node = vfs_node_get(&mp_res);
|
---|
[861e7d1] | 117 | if (!mp_node) {
|
---|
| 118 | ipc_answer_0(rid, ENOMEM);
|
---|
[493853ec] | 119 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 120 | return;
|
---|
| 121 | }
|
---|
[8dc72b64] | 122 |
|
---|
[861e7d1] | 123 | /*
|
---|
| 124 | * Now we hold a reference to mp_node.
|
---|
| 125 | * It will be dropped upon the corresponding VFS_UNMOUNT.
|
---|
| 126 | * This prevents the mount point from being deleted.
|
---|
| 127 | */
|
---|
| 128 | } else {
|
---|
[72bde81] | 129 | /* We still don't have the root file system mounted. */
|
---|
[92fd52d7] | 130 | if (str_cmp(mp, "/") == 0) {
|
---|
[64b67c3] | 131 | /*
|
---|
| 132 | * For this simple, but important case,
|
---|
| 133 | * we are almost done.
|
---|
| 134 | */
|
---|
| 135 |
|
---|
[f49b0ea] | 136 | /* Tell the mountee that it is being mounted. */
|
---|
| 137 | phone = vfs_grab_phone(fs_handle);
|
---|
[594303b] | 138 | msg = async_send_1(phone, VFS_MOUNTED,
|
---|
| 139 | (ipcarg_t) dev_handle, &answer);
|
---|
| 140 | /* send the mount options */
|
---|
| 141 | rc = ipc_data_write_start(phone, (void *)opts,
|
---|
| 142 | str_size(opts));
|
---|
| 143 | if (rc != EOK) {
|
---|
| 144 | async_wait_for(msg, NULL);
|
---|
| 145 | vfs_release_phone(phone);
|
---|
| 146 | ipc_answer_0(rid, rc);
|
---|
[493853ec] | 147 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[594303b] | 148 | return;
|
---|
| 149 | }
|
---|
| 150 | async_wait_for(msg, &rc);
|
---|
[64b67c3] | 151 | vfs_release_phone(phone);
|
---|
[5ab597d] | 152 |
|
---|
| 153 | if (rc != EOK) {
|
---|
| 154 | ipc_answer_0(rid, rc);
|
---|
[493853ec] | 155 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[5ab597d] | 156 | return;
|
---|
[f49b0ea] | 157 | }
|
---|
[594303b] | 158 |
|
---|
| 159 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
| 160 | rsize = (size_t) IPC_GET_ARG2(answer);
|
---|
| 161 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
|
---|
[8dc72b64] | 162 |
|
---|
[5ab597d] | 163 | mr_res.triplet.fs_handle = fs_handle;
|
---|
| 164 | mr_res.triplet.dev_handle = dev_handle;
|
---|
[594303b] | 165 | mr_res.triplet.index = rindex;
|
---|
| 166 | mr_res.size = rsize;
|
---|
| 167 | mr_res.lnkcnt = rlnkcnt;
|
---|
[b17186d] | 168 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
[8dc72b64] | 169 |
|
---|
[5ab597d] | 170 | rootfs.fs_handle = fs_handle;
|
---|
| 171 | rootfs.dev_handle = dev_handle;
|
---|
[8dc72b64] | 172 |
|
---|
[5ab597d] | 173 | /* Add reference to the mounted root. */
|
---|
| 174 | mr_node = vfs_node_get(&mr_res);
|
---|
| 175 | assert(mr_node);
|
---|
[8dc72b64] | 176 |
|
---|
[64b67c3] | 177 | ipc_answer_0(rid, rc);
|
---|
[493853ec] | 178 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 179 | return;
|
---|
| 180 | } else {
|
---|
| 181 | /*
|
---|
| 182 | * We can't resolve this without the root filesystem
|
---|
| 183 | * being mounted first.
|
---|
| 184 | */
|
---|
| 185 | ipc_answer_0(rid, ENOENT);
|
---|
[493853ec] | 186 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 187 | return;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /*
|
---|
| 192 | * At this point, we have all necessary pieces: file system and device
|
---|
[f49b0ea] | 193 | * handles, and we know the mount point VFS node.
|
---|
[861e7d1] | 194 | */
|
---|
[8dc72b64] | 195 |
|
---|
[83937ccd] | 196 | int mountee_phone = vfs_grab_phone(fs_handle);
|
---|
| 197 | assert(mountee_phone >= 0);
|
---|
| 198 | vfs_release_phone(mountee_phone);
|
---|
| 199 |
|
---|
[64b67c3] | 200 | phone = vfs_grab_phone(mp_res.triplet.fs_handle);
|
---|
[594303b] | 201 | msg = async_send_4(phone, VFS_MOUNT,
|
---|
[eb27ce5a] | 202 | (ipcarg_t) mp_res.triplet.dev_handle,
|
---|
[ce7311fc] | 203 | (ipcarg_t) mp_res.triplet.index,
|
---|
[f49b0ea] | 204 | (ipcarg_t) fs_handle,
|
---|
[594303b] | 205 | (ipcarg_t) dev_handle, &answer);
|
---|
[83937ccd] | 206 |
|
---|
| 207 | /* send connection */
|
---|
| 208 | rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
|
---|
| 209 | if (rc != EOK) {
|
---|
| 210 | async_wait_for(msg, NULL);
|
---|
| 211 | vfs_release_phone(phone);
|
---|
| 212 | /* Mount failed, drop reference to mp_node. */
|
---|
| 213 | if (mp_node)
|
---|
| 214 | vfs_node_put(mp_node);
|
---|
| 215 | ipc_answer_0(rid, rc);
|
---|
[493853ec] | 216 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[83937ccd] | 217 | return;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[594303b] | 220 | /* send the mount options */
|
---|
| 221 | rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
|
---|
| 222 | if (rc != EOK) {
|
---|
| 223 | async_wait_for(msg, NULL);
|
---|
| 224 | vfs_release_phone(phone);
|
---|
| 225 | /* Mount failed, drop reference to mp_node. */
|
---|
| 226 | if (mp_node)
|
---|
| 227 | vfs_node_put(mp_node);
|
---|
| 228 | ipc_answer_0(rid, rc);
|
---|
[493853ec] | 229 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[594303b] | 230 | return;
|
---|
| 231 | }
|
---|
| 232 | async_wait_for(msg, &rc);
|
---|
[861e7d1] | 233 | vfs_release_phone(phone);
|
---|
[8dc72b64] | 234 |
|
---|
[493853ec] | 235 | if (rc == EOK) {
|
---|
| 236 | rindex = (fs_index_t) IPC_GET_ARG1(answer);
|
---|
| 237 | rsize = (size_t) IPC_GET_ARG2(answer);
|
---|
| 238 | rlnkcnt = (unsigned) IPC_GET_ARG3(answer);
|
---|
| 239 |
|
---|
| 240 | mr_res.triplet.fs_handle = fs_handle;
|
---|
| 241 | mr_res.triplet.dev_handle = dev_handle;
|
---|
| 242 | mr_res.triplet.index = rindex;
|
---|
| 243 | mr_res.size = rsize;
|
---|
| 244 | mr_res.lnkcnt = rlnkcnt;
|
---|
| 245 | mr_res.type = VFS_NODE_DIRECTORY;
|
---|
| 246 |
|
---|
| 247 | /* Add reference to the mounted root. */
|
---|
| 248 | mr_node = vfs_node_get(&mr_res);
|
---|
| 249 | assert(mr_node);
|
---|
| 250 | } else {
|
---|
[f49b0ea] | 251 | /* Mount failed, drop reference to mp_node. */
|
---|
[861e7d1] | 252 | if (mp_node)
|
---|
| 253 | vfs_node_put(mp_node);
|
---|
| 254 | }
|
---|
[83937ccd] | 255 |
|
---|
[ce7311fc] | 256 | ipc_answer_0(rid, rc);
|
---|
[493853ec] | 257 | rwlock_write_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 258 | }
|
---|
| 259 |
|
---|
[8dc72b64] | 260 | /** Process pending mount requests */
|
---|
[0c0b800] | 261 | void vfs_process_pending_mount(void)
|
---|
[8dc72b64] | 262 | {
|
---|
| 263 | link_t *cur;
|
---|
| 264 |
|
---|
| 265 | loop:
|
---|
| 266 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
|
---|
| 267 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
|
---|
| 268 |
|
---|
| 269 | fs_handle_t fs_handle = fs_name_to_handle(pr->fs_name, true);
|
---|
| 270 | if (!fs_handle)
|
---|
| 271 | continue;
|
---|
| 272 |
|
---|
| 273 | /* Acknowledge that we know fs_name. */
|
---|
| 274 | ipc_answer_0(pr->callid, EOK);
|
---|
| 275 |
|
---|
| 276 | /* Do the mount */
|
---|
[594303b] | 277 | vfs_mount_internal(pr->rid, pr->dev_handle, fs_handle, pr->mp,
|
---|
| 278 | pr->opts);
|
---|
[8dc72b64] | 279 |
|
---|
| 280 | free(pr->fs_name);
|
---|
| 281 | free(pr->mp);
|
---|
[594303b] | 282 | free(pr->opts);
|
---|
[8dc72b64] | 283 | list_remove(cur);
|
---|
| 284 | free(pr);
|
---|
| 285 | goto loop;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 290 | {
|
---|
| 291 | /*
|
---|
| 292 | * We expect the library to do the device-name to device-handle
|
---|
| 293 | * translation for us, thus the device handle will arrive as ARG1
|
---|
| 294 | * in the request.
|
---|
| 295 | */
|
---|
| 296 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
| 297 |
|
---|
| 298 | /*
|
---|
| 299 | * Mount flags are passed as ARG2.
|
---|
| 300 | */
|
---|
| 301 | unsigned int flags = (unsigned int) IPC_GET_ARG2(*request);
|
---|
| 302 |
|
---|
| 303 | /*
|
---|
| 304 | * For now, don't make use of ARG3, but it can be used to
|
---|
| 305 | * carry mount options in the future.
|
---|
| 306 | */
|
---|
| 307 |
|
---|
| 308 | /* We want the client to send us the mount point. */
|
---|
| 309 | ipc_callid_t callid;
|
---|
| 310 | size_t size;
|
---|
| 311 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 312 | ipc_answer_0(callid, EINVAL);
|
---|
| 313 | ipc_answer_0(rid, EINVAL);
|
---|
| 314 | return;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /* Check whether size is reasonable wrt. the mount point. */
|
---|
| 318 | if ((size < 1) || (size > MAX_PATH_LEN)) {
|
---|
| 319 | ipc_answer_0(callid, EINVAL);
|
---|
| 320 | ipc_answer_0(rid, EINVAL);
|
---|
| 321 | return;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | /* Allocate buffer for the mount point data being received. */
|
---|
| 325 | char *mp = malloc(size + 1);
|
---|
| 326 | if (!mp) {
|
---|
| 327 | ipc_answer_0(callid, ENOMEM);
|
---|
| 328 | ipc_answer_0(rid, ENOMEM);
|
---|
| 329 | return;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | /* Deliver the mount point. */
|
---|
| 333 | ipcarg_t retval = ipc_data_write_finalize(callid, mp, size);
|
---|
| 334 | if (retval != EOK) {
|
---|
[58d7280] | 335 | ipc_answer_0(rid, retval);
|
---|
[8dc72b64] | 336 | free(mp);
|
---|
| 337 | return;
|
---|
| 338 | }
|
---|
| 339 | mp[size] = '\0';
|
---|
| 340 |
|
---|
[594303b] | 341 | /* Now we expect to receive the mount options. */
|
---|
| 342 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 343 | ipc_answer_0(callid, EINVAL);
|
---|
| 344 | ipc_answer_0(rid, EINVAL);
|
---|
| 345 | free(mp);
|
---|
| 346 | return;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /* Check the offered options size. */
|
---|
| 350 | if (size < 0 || size > MAX_MNTOPTS_LEN) {
|
---|
| 351 | ipc_answer_0(callid, EINVAL);
|
---|
| 352 | ipc_answer_0(rid, EINVAL);
|
---|
| 353 | free(mp);
|
---|
| 354 | return;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | /* Allocate buffer for the mount options. */
|
---|
| 358 | char *opts = (char *) malloc(size + 1);
|
---|
| 359 | if (!opts) {
|
---|
| 360 | ipc_answer_0(callid, ENOMEM);
|
---|
| 361 | ipc_answer_0(rid, ENOMEM);
|
---|
| 362 | free(mp);
|
---|
| 363 | return;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /* Deliver the mount options. */
|
---|
| 367 | retval = ipc_data_write_finalize(callid, opts, size);
|
---|
| 368 | if (retval != EOK) {
|
---|
| 369 | ipc_answer_0(rid, retval);
|
---|
| 370 | free(mp);
|
---|
| 371 | free(opts);
|
---|
| 372 | return;
|
---|
| 373 | }
|
---|
| 374 | opts[size] = '\0';
|
---|
| 375 |
|
---|
[8dc72b64] | 376 | /*
|
---|
| 377 | * Now, we expect the client to send us data with the name of the file
|
---|
| 378 | * system.
|
---|
| 379 | */
|
---|
| 380 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 381 | ipc_answer_0(callid, EINVAL);
|
---|
| 382 | ipc_answer_0(rid, EINVAL);
|
---|
| 383 | free(mp);
|
---|
[594303b] | 384 | free(opts);
|
---|
[8dc72b64] | 385 | return;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /*
|
---|
| 389 | * Don't receive more than is necessary for storing a full file system
|
---|
| 390 | * name.
|
---|
| 391 | */
|
---|
| 392 | if ((size < 1) || (size > FS_NAME_MAXLEN)) {
|
---|
| 393 | ipc_answer_0(callid, EINVAL);
|
---|
| 394 | ipc_answer_0(rid, EINVAL);
|
---|
| 395 | free(mp);
|
---|
[594303b] | 396 | free(opts);
|
---|
[8dc72b64] | 397 | return;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | /*
|
---|
| 401 | * Allocate buffer for file system name.
|
---|
| 402 | */
|
---|
| 403 | char *fs_name = (char *) malloc(size + 1);
|
---|
| 404 | if (fs_name == NULL) {
|
---|
| 405 | ipc_answer_0(callid, ENOMEM);
|
---|
[58d7280] | 406 | ipc_answer_0(rid, ENOMEM);
|
---|
[8dc72b64] | 407 | free(mp);
|
---|
[594303b] | 408 | free(opts);
|
---|
[8dc72b64] | 409 | return;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /* Deliver the file system name. */
|
---|
| 413 | retval = ipc_data_write_finalize(callid, fs_name, size);
|
---|
| 414 | if (retval != EOK) {
|
---|
[58d7280] | 415 | ipc_answer_0(rid, retval);
|
---|
[8dc72b64] | 416 | free(mp);
|
---|
[594303b] | 417 | free(opts);
|
---|
[8dc72b64] | 418 | free(fs_name);
|
---|
| 419 | return;
|
---|
| 420 | }
|
---|
| 421 | fs_name[size] = '\0';
|
---|
[c08c355] | 422 |
|
---|
| 423 | /*
|
---|
| 424 | * Wait for IPC_M_PING so that we can return an error if we don't know
|
---|
| 425 | * fs_name.
|
---|
| 426 | */
|
---|
| 427 | ipc_call_t data;
|
---|
| 428 | callid = async_get_call(&data);
|
---|
| 429 | if (IPC_GET_METHOD(data) != IPC_M_PING) {
|
---|
| 430 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 431 | ipc_answer_0(rid, ENOTSUP);
|
---|
| 432 | free(mp);
|
---|
[594303b] | 433 | free(opts);
|
---|
[c08c355] | 434 | free(fs_name);
|
---|
| 435 | return;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[8dc72b64] | 438 | /*
|
---|
| 439 | * Check if we know a file system with the same name as is in fs_name.
|
---|
| 440 | * This will also give us its file system handle.
|
---|
| 441 | */
|
---|
| 442 | fs_handle_t fs_handle = fs_name_to_handle(fs_name, true);
|
---|
| 443 | if (!fs_handle) {
|
---|
| 444 | if (flags & IPC_FLAG_BLOCKING) {
|
---|
[c08c355] | 445 | pending_req_t *pr;
|
---|
| 446 |
|
---|
[8dc72b64] | 447 | /* Blocking mount, add to pending list */
|
---|
[c08c355] | 448 | pr = (pending_req_t *) malloc(sizeof(pending_req_t));
|
---|
[8dc72b64] | 449 | if (!pr) {
|
---|
| 450 | ipc_answer_0(callid, ENOMEM);
|
---|
| 451 | ipc_answer_0(rid, ENOMEM);
|
---|
| 452 | free(mp);
|
---|
| 453 | free(fs_name);
|
---|
[594303b] | 454 | free(opts);
|
---|
[8dc72b64] | 455 | return;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | pr->fs_name = fs_name;
|
---|
| 459 | pr->mp = mp;
|
---|
[594303b] | 460 | pr->opts = opts;
|
---|
[8dc72b64] | 461 | pr->callid = callid;
|
---|
| 462 | pr->rid = rid;
|
---|
| 463 | pr->dev_handle = dev_handle;
|
---|
[db90860] | 464 | link_initialize(&pr->link);
|
---|
[8dc72b64] | 465 | list_append(&pr->link, &pending_req);
|
---|
| 466 | return;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | ipc_answer_0(callid, ENOENT);
|
---|
| 470 | ipc_answer_0(rid, ENOENT);
|
---|
| 471 | free(mp);
|
---|
| 472 | free(fs_name);
|
---|
[594303b] | 473 | free(opts);
|
---|
[8dc72b64] | 474 | return;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | /* Acknowledge that we know fs_name. */
|
---|
| 478 | ipc_answer_0(callid, EOK);
|
---|
| 479 |
|
---|
| 480 | /* Do the mount */
|
---|
[594303b] | 481 | vfs_mount_internal(rid, dev_handle, fs_handle, mp, opts);
|
---|
[8dc72b64] | 482 | free(mp);
|
---|
| 483 | free(fs_name);
|
---|
[594303b] | 484 | free(opts);
|
---|
[8dc72b64] | 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 | }
|
---|
| 493 |
|
---|
| 494 | /*
|
---|
[ae78b530] | 495 | * The POSIX interface is open(path, oflag, mode).
|
---|
| 496 | * We can receive oflags and mode along with the VFS_OPEN call; the path
|
---|
[861e7d1] | 497 | * 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;
|
---|
| 506 |
|
---|
[b17186d] | 507 | /*
|
---|
| 508 | * Make sure that we are called with exactly one of L_FILE and
|
---|
| 509 | * L_DIRECTORY.
|
---|
| 510 | */
|
---|
| 511 | if ((lflag & (L_FILE | L_DIRECTORY)) == 0 ||
|
---|
| 512 | (lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) {
|
---|
| 513 | ipc_answer_0(rid, EINVAL);
|
---|
| 514 | return;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
[2db4ac8] | 517 | if (oflag & O_CREAT)
|
---|
| 518 | lflag |= L_CREATE;
|
---|
| 519 | if (oflag & O_EXCL)
|
---|
| 520 | lflag |= L_EXCLUSIVE;
|
---|
| 521 |
|
---|
[861e7d1] | 522 | ipc_callid_t callid;
|
---|
| 523 |
|
---|
| 524 | if (!ipc_data_write_receive(&callid, &len)) {
|
---|
| 525 | ipc_answer_0(callid, EINVAL);
|
---|
| 526 | ipc_answer_0(rid, EINVAL);
|
---|
| 527 | return;
|
---|
| 528 | }
|
---|
[d6084ef] | 529 | char *path = malloc(len + 1);
|
---|
[861e7d1] | 530 | if (!path) {
|
---|
| 531 | ipc_answer_0(callid, ENOMEM);
|
---|
| 532 | ipc_answer_0(rid, ENOMEM);
|
---|
| 533 | return;
|
---|
| 534 | }
|
---|
| 535 | int rc;
|
---|
| 536 | if ((rc = ipc_data_write_finalize(callid, path, len))) {
|
---|
| 537 | ipc_answer_0(rid, rc);
|
---|
| 538 | free(path);
|
---|
| 539 | return;
|
---|
| 540 | }
|
---|
[d6084ef] | 541 | path[len] = '\0';
|
---|
[861e7d1] | 542 |
|
---|
| 543 | /*
|
---|
| 544 | * Avoid the race condition in which the file can be deleted before we
|
---|
| 545 | * find/create-and-lock the VFS node corresponding to the looked-up
|
---|
| 546 | * triplet.
|
---|
| 547 | */
|
---|
[2db4ac8] | 548 | if (lflag & L_CREATE)
|
---|
| 549 | rwlock_write_lock(&namespace_rwlock);
|
---|
| 550 | else
|
---|
| 551 | rwlock_read_lock(&namespace_rwlock);
|
---|
[861e7d1] | 552 |
|
---|
[72bde81] | 553 | /* The path is now populated and we can call vfs_lookup_internal(). */
|
---|
[eb27ce5a] | 554 | vfs_lookup_res_t lr;
|
---|
[d6084ef] | 555 | rc = vfs_lookup_internal(path, lflag, &lr, NULL);
|
---|
[861e7d1] | 556 | if (rc) {
|
---|
[2db4ac8] | 557 | if (lflag & L_CREATE)
|
---|
| 558 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 559 | else
|
---|
| 560 | rwlock_read_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 561 | ipc_answer_0(rid, rc);
|
---|
| 562 | free(path);
|
---|
| 563 | return;
|
---|
| 564 | }
|
---|
| 565 |
|
---|
[7fe1f75] | 566 | /* Path is no longer needed. */
|
---|
[861e7d1] | 567 | free(path);
|
---|
| 568 |
|
---|
[eb27ce5a] | 569 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[2db4ac8] | 570 | if (lflag & L_CREATE)
|
---|
| 571 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 572 | else
|
---|
| 573 | rwlock_read_unlock(&namespace_rwlock);
|
---|
[861e7d1] | 574 |
|
---|
[7fe1f75] | 575 | /* Truncate the file if requested and if necessary. */
|
---|
| 576 | if (oflag & O_TRUNC) {
|
---|
[ee68e4bc] | 577 | rwlock_write_lock(&node->contents_rwlock);
|
---|
[7fe1f75] | 578 | if (node->size) {
|
---|
| 579 | rc = vfs_truncate_internal(node->fs_handle,
|
---|
| 580 | node->dev_handle, node->index, 0);
|
---|
| 581 | if (rc) {
|
---|
[ee68e4bc] | 582 | rwlock_write_unlock(&node->contents_rwlock);
|
---|
[7fe1f75] | 583 | vfs_node_put(node);
|
---|
| 584 | ipc_answer_0(rid, rc);
|
---|
| 585 | return;
|
---|
| 586 | }
|
---|
| 587 | node->size = 0;
|
---|
| 588 | }
|
---|
[ee68e4bc] | 589 | rwlock_write_unlock(&node->contents_rwlock);
|
---|
[7fe1f75] | 590 | }
|
---|
| 591 |
|
---|
[861e7d1] | 592 | /*
|
---|
| 593 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
| 594 | * structure.
|
---|
| 595 | */
|
---|
| 596 | int fd = vfs_fd_alloc();
|
---|
| 597 | if (fd < 0) {
|
---|
| 598 | vfs_node_put(node);
|
---|
| 599 | ipc_answer_0(rid, fd);
|
---|
| 600 | return;
|
---|
| 601 | }
|
---|
| 602 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 603 | file->node = node;
|
---|
[f7017572] | 604 | if (oflag & O_APPEND)
|
---|
[15b9970] | 605 | file->append = true;
|
---|
[861e7d1] | 606 |
|
---|
| 607 | /*
|
---|
| 608 | * The following increase in reference count is for the fact that the
|
---|
| 609 | * file is being opened and that a file structure is pointing to it.
|
---|
| 610 | * It is necessary so that the file will not disappear when
|
---|
| 611 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
| 612 | * respective VFS_CLOSE.
|
---|
| 613 | */
|
---|
| 614 | vfs_node_addref(node);
|
---|
| 615 | vfs_node_put(node);
|
---|
| 616 |
|
---|
[72bde81] | 617 | /* Success! Return the new file descriptor to the client. */
|
---|
[861e7d1] | 618 | ipc_answer_1(rid, EOK, fd);
|
---|
| 619 | }
|
---|
| 620 |
|
---|
[e704503] | 621 | void vfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 622 | {
|
---|
| 623 | int fd = IPC_GET_ARG1(*request);
|
---|
[b7f9087] | 624 | int rc = vfs_fd_free(fd);
|
---|
| 625 | ipc_answer_0(rid, rc);
|
---|
[e704503] | 626 | }
|
---|
| 627 |
|
---|
[861e7d1] | 628 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
| 629 | {
|
---|
| 630 |
|
---|
| 631 | /*
|
---|
| 632 | * The following code strongly depends on the fact that the files data
|
---|
| 633 | * structure can be only accessed by a single fibril and all file
|
---|
| 634 | * operations are serialized (i.e. the reads and writes cannot
|
---|
| 635 | * interleave and a file cannot be closed while it is being read).
|
---|
| 636 | *
|
---|
| 637 | * Additional synchronization needs to be added once the table of
|
---|
| 638 | * open files supports parallel access!
|
---|
| 639 | */
|
---|
| 640 |
|
---|
| 641 | int fd = IPC_GET_ARG1(*request);
|
---|
[6c89f20] | 642 |
|
---|
[72bde81] | 643 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
[861e7d1] | 644 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 645 | if (!file) {
|
---|
| 646 | ipc_answer_0(rid, ENOENT);
|
---|
| 647 | return;
|
---|
| 648 | }
|
---|
[6c89f20] | 649 |
|
---|
[861e7d1] | 650 | /*
|
---|
| 651 | * Now we need to receive a call with client's
|
---|
| 652 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
|
---|
| 653 | */
|
---|
| 654 | ipc_callid_t callid;
|
---|
| 655 | int res;
|
---|
| 656 | if (read)
|
---|
| 657 | res = ipc_data_read_receive(&callid, NULL);
|
---|
| 658 | else
|
---|
| 659 | res = ipc_data_write_receive(&callid, NULL);
|
---|
| 660 | if (!res) {
|
---|
| 661 | ipc_answer_0(callid, EINVAL);
|
---|
| 662 | ipc_answer_0(rid, EINVAL);
|
---|
| 663 | return;
|
---|
| 664 | }
|
---|
[6c89f20] | 665 |
|
---|
[861e7d1] | 666 | /*
|
---|
| 667 | * Lock the open file structure so that no other thread can manipulate
|
---|
| 668 | * the same open file at a time.
|
---|
| 669 | */
|
---|
| 670 | futex_down(&file->lock);
|
---|
[b17186d] | 671 |
|
---|
[861e7d1] | 672 | /*
|
---|
| 673 | * Lock the file's node so that no other client can read/write to it at
|
---|
| 674 | * the same time.
|
---|
| 675 | */
|
---|
| 676 | if (read)
|
---|
| 677 | rwlock_read_lock(&file->node->contents_rwlock);
|
---|
| 678 | else
|
---|
| 679 | rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[b17186d] | 680 |
|
---|
| 681 | if (file->node->type == VFS_NODE_DIRECTORY) {
|
---|
| 682 | /*
|
---|
| 683 | * Make sure that no one is modifying the namespace
|
---|
| 684 | * while we are in readdir().
|
---|
| 685 | */
|
---|
| 686 | assert(read);
|
---|
| 687 | rwlock_read_lock(&namespace_rwlock);
|
---|
| 688 | }
|
---|
[6c89f20] | 689 |
|
---|
[861e7d1] | 690 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
| 691 |
|
---|
[72bde81] | 692 | /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
|
---|
[861e7d1] | 693 | aid_t msg;
|
---|
| 694 | ipc_call_t answer;
|
---|
[15b9970] | 695 | if (!read && file->append)
|
---|
| 696 | file->pos = file->node->size;
|
---|
[861e7d1] | 697 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
|
---|
| 698 | file->node->dev_handle, file->node->index, file->pos, &answer);
|
---|
| 699 |
|
---|
| 700 | /*
|
---|
| 701 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
| 702 | * destination FS server. The call will be routed as if sent by
|
---|
| 703 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
| 704 | * don't have to bother.
|
---|
| 705 | */
|
---|
| 706 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
[6c89f20] | 707 |
|
---|
[861e7d1] | 708 | vfs_release_phone(fs_phone);
|
---|
[6c89f20] | 709 |
|
---|
[72bde81] | 710 | /* Wait for reply from the FS server. */
|
---|
[861e7d1] | 711 | ipcarg_t rc;
|
---|
| 712 | async_wait_for(msg, &rc);
|
---|
| 713 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
[b17186d] | 714 |
|
---|
| 715 | if (file->node->type == VFS_NODE_DIRECTORY)
|
---|
| 716 | rwlock_read_unlock(&namespace_rwlock);
|
---|
[6c89f20] | 717 |
|
---|
[72bde81] | 718 | /* Unlock the VFS node. */
|
---|
[861e7d1] | 719 | if (read)
|
---|
| 720 | rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
| 721 | else {
|
---|
| 722 | /* Update the cached version of node's size. */
|
---|
[f7017572] | 723 | if (rc == EOK)
|
---|
| 724 | file->node->size = IPC_GET_ARG2(answer);
|
---|
[861e7d1] | 725 | rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
| 726 | }
|
---|
[6c89f20] | 727 |
|
---|
[72bde81] | 728 | /* Update the position pointer and unlock the open file. */
|
---|
[f7017572] | 729 | if (rc == EOK)
|
---|
| 730 | file->pos += bytes;
|
---|
[861e7d1] | 731 | futex_up(&file->lock);
|
---|
[6c89f20] | 732 |
|
---|
[861e7d1] | 733 | /*
|
---|
| 734 | * FS server's reply is the final result of the whole operation we
|
---|
| 735 | * return to the client.
|
---|
| 736 | */
|
---|
| 737 | ipc_answer_1(rid, rc, bytes);
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 741 | {
|
---|
| 742 | vfs_rdwr(rid, request, true);
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 746 | {
|
---|
| 747 | vfs_rdwr(rid, request, false);
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 751 | {
|
---|
| 752 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
| 753 | off_t off = (off_t) IPC_GET_ARG2(*request);
|
---|
| 754 | int whence = (int) IPC_GET_ARG3(*request);
|
---|
| 755 |
|
---|
| 756 |
|
---|
[72bde81] | 757 | /* Lookup the file structure corresponding to the file descriptor. */
|
---|
[861e7d1] | 758 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 759 | if (!file) {
|
---|
| 760 | ipc_answer_0(rid, ENOENT);
|
---|
| 761 | return;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 | off_t newpos;
|
---|
| 765 | futex_down(&file->lock);
|
---|
| 766 | if (whence == SEEK_SET) {
|
---|
| 767 | file->pos = off;
|
---|
| 768 | futex_up(&file->lock);
|
---|
| 769 | ipc_answer_1(rid, EOK, off);
|
---|
| 770 | return;
|
---|
| 771 | }
|
---|
| 772 | if (whence == SEEK_CUR) {
|
---|
| 773 | if (file->pos + off < file->pos) {
|
---|
| 774 | futex_up(&file->lock);
|
---|
| 775 | ipc_answer_0(rid, EOVERFLOW);
|
---|
| 776 | return;
|
---|
| 777 | }
|
---|
| 778 | file->pos += off;
|
---|
| 779 | newpos = file->pos;
|
---|
| 780 | futex_up(&file->lock);
|
---|
| 781 | ipc_answer_1(rid, EOK, newpos);
|
---|
| 782 | return;
|
---|
| 783 | }
|
---|
| 784 | if (whence == SEEK_END) {
|
---|
| 785 | rwlock_read_lock(&file->node->contents_rwlock);
|
---|
| 786 | size_t size = file->node->size;
|
---|
| 787 | rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
| 788 | if (size + off < size) {
|
---|
| 789 | futex_up(&file->lock);
|
---|
| 790 | ipc_answer_0(rid, EOVERFLOW);
|
---|
| 791 | return;
|
---|
| 792 | }
|
---|
| 793 | newpos = size + off;
|
---|
| 794 | futex_up(&file->lock);
|
---|
| 795 | ipc_answer_1(rid, EOK, newpos);
|
---|
| 796 | return;
|
---|
| 797 | }
|
---|
| 798 | futex_up(&file->lock);
|
---|
| 799 | ipc_answer_0(rid, EINVAL);
|
---|
| 800 | }
|
---|
| 801 |
|
---|
[f2ec8c8] | 802 | int
|
---|
| 803 | vfs_truncate_internal(fs_handle_t fs_handle, dev_handle_t dev_handle,
|
---|
| 804 | fs_index_t index, size_t size)
|
---|
[7fe1f75] | 805 | {
|
---|
| 806 | ipcarg_t rc;
|
---|
| 807 | int fs_phone;
|
---|
| 808 |
|
---|
| 809 | fs_phone = vfs_grab_phone(fs_handle);
|
---|
| 810 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)dev_handle,
|
---|
| 811 | (ipcarg_t)index, (ipcarg_t)size);
|
---|
| 812 | vfs_release_phone(fs_phone);
|
---|
| 813 | return (int)rc;
|
---|
| 814 | }
|
---|
| 815 |
|
---|
[0ee4322] | 816 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 817 | {
|
---|
| 818 | int fd = IPC_GET_ARG1(*request);
|
---|
| 819 | size_t size = IPC_GET_ARG2(*request);
|
---|
[7fe1f75] | 820 | int rc;
|
---|
[0ee4322] | 821 |
|
---|
| 822 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 823 | if (!file) {
|
---|
| 824 | ipc_answer_0(rid, ENOENT);
|
---|
| 825 | return;
|
---|
| 826 | }
|
---|
| 827 | futex_down(&file->lock);
|
---|
| 828 |
|
---|
| 829 | rwlock_write_lock(&file->node->contents_rwlock);
|
---|
[7fe1f75] | 830 | rc = vfs_truncate_internal(file->node->fs_handle,
|
---|
| 831 | file->node->dev_handle, file->node->index, size);
|
---|
[0ee4322] | 832 | if (rc == EOK)
|
---|
| 833 | file->node->size = size;
|
---|
| 834 | rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
| 835 |
|
---|
| 836 | futex_up(&file->lock);
|
---|
[7fe1f75] | 837 | ipc_answer_0(rid, (ipcarg_t)rc);
|
---|
[861e7d1] | 838 | }
|
---|
| 839 |
|
---|
[72bde81] | 840 | void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 841 | {
|
---|
| 842 | int mode = IPC_GET_ARG1(*request);
|
---|
| 843 |
|
---|
[f15cf1a6] | 844 | size_t len;
|
---|
[72bde81] | 845 | ipc_callid_t callid;
|
---|
| 846 |
|
---|
| 847 | if (!ipc_data_write_receive(&callid, &len)) {
|
---|
| 848 | ipc_answer_0(callid, EINVAL);
|
---|
| 849 | ipc_answer_0(rid, EINVAL);
|
---|
| 850 | return;
|
---|
| 851 | }
|
---|
[d6084ef] | 852 | char *path = malloc(len + 1);
|
---|
[72bde81] | 853 | if (!path) {
|
---|
| 854 | ipc_answer_0(callid, ENOMEM);
|
---|
| 855 | ipc_answer_0(rid, ENOMEM);
|
---|
| 856 | return;
|
---|
| 857 | }
|
---|
| 858 | int rc;
|
---|
| 859 | if ((rc = ipc_data_write_finalize(callid, path, len))) {
|
---|
| 860 | ipc_answer_0(rid, rc);
|
---|
| 861 | free(path);
|
---|
| 862 | return;
|
---|
| 863 | }
|
---|
[d6084ef] | 864 | path[len] = '\0';
|
---|
[72bde81] | 865 |
|
---|
| 866 | rwlock_write_lock(&namespace_rwlock);
|
---|
| 867 | int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
|
---|
[d6084ef] | 868 | rc = vfs_lookup_internal(path, lflag, NULL, NULL);
|
---|
[72bde81] | 869 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 870 | free(path);
|
---|
| 871 | ipc_answer_0(rid, rc);
|
---|
| 872 | }
|
---|
| 873 |
|
---|
[f15cf1a6] | 874 | void vfs_unlink(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 875 | {
|
---|
| 876 | int lflag = IPC_GET_ARG1(*request);
|
---|
| 877 |
|
---|
| 878 | size_t len;
|
---|
| 879 | ipc_callid_t callid;
|
---|
| 880 |
|
---|
| 881 | if (!ipc_data_write_receive(&callid, &len)) {
|
---|
| 882 | ipc_answer_0(callid, EINVAL);
|
---|
| 883 | ipc_answer_0(rid, EINVAL);
|
---|
| 884 | return;
|
---|
| 885 | }
|
---|
[d6084ef] | 886 | char *path = malloc(len + 1);
|
---|
[f15cf1a6] | 887 | if (!path) {
|
---|
| 888 | ipc_answer_0(callid, ENOMEM);
|
---|
| 889 | ipc_answer_0(rid, ENOMEM);
|
---|
| 890 | return;
|
---|
| 891 | }
|
---|
| 892 | int rc;
|
---|
| 893 | if ((rc = ipc_data_write_finalize(callid, path, len))) {
|
---|
| 894 | ipc_answer_0(rid, rc);
|
---|
| 895 | free(path);
|
---|
| 896 | return;
|
---|
| 897 | }
|
---|
[d6084ef] | 898 | path[len] = '\0';
|
---|
[f15cf1a6] | 899 |
|
---|
| 900 | rwlock_write_lock(&namespace_rwlock);
|
---|
| 901 | lflag &= L_DIRECTORY; /* sanitize lflag */
|
---|
| 902 | vfs_lookup_res_t lr;
|
---|
[a8e9ab8d] | 903 | rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
|
---|
[f15cf1a6] | 904 | free(path);
|
---|
| 905 | if (rc != EOK) {
|
---|
| 906 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 907 | ipc_answer_0(rid, rc);
|
---|
| 908 | return;
|
---|
| 909 | }
|
---|
| 910 |
|
---|
| 911 | /*
|
---|
| 912 | * The name has already been unlinked by vfs_lookup_internal().
|
---|
| 913 | * We have to get and put the VFS node to ensure that it is
|
---|
[fdb7795] | 914 | * VFS_DESTROY'ed after the last reference to it is dropped.
|
---|
[f15cf1a6] | 915 | */
|
---|
| 916 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[c31d773] | 917 | futex_down(&nodes_futex);
|
---|
[f15cf1a6] | 918 | node->lnkcnt--;
|
---|
[c31d773] | 919 | futex_up(&nodes_futex);
|
---|
[f15cf1a6] | 920 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 921 | vfs_node_put(node);
|
---|
| 922 | ipc_answer_0(rid, EOK);
|
---|
| 923 | }
|
---|
| 924 |
|
---|
[a8e9ab8d] | 925 | void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 926 | {
|
---|
[732bb0c] | 927 | size_t olen, nlen;
|
---|
[a8e9ab8d] | 928 | ipc_callid_t callid;
|
---|
| 929 | int rc;
|
---|
| 930 |
|
---|
| 931 | /* Retrieve the old path. */
|
---|
[732bb0c] | 932 | if (!ipc_data_write_receive(&callid, &olen)) {
|
---|
[a8e9ab8d] | 933 | ipc_answer_0(callid, EINVAL);
|
---|
| 934 | ipc_answer_0(rid, EINVAL);
|
---|
| 935 | return;
|
---|
| 936 | }
|
---|
[732bb0c] | 937 | char *old = malloc(olen + 1);
|
---|
[a8e9ab8d] | 938 | if (!old) {
|
---|
| 939 | ipc_answer_0(callid, ENOMEM);
|
---|
| 940 | ipc_answer_0(rid, ENOMEM);
|
---|
| 941 | return;
|
---|
| 942 | }
|
---|
[732bb0c] | 943 | if ((rc = ipc_data_write_finalize(callid, old, olen))) {
|
---|
[a8e9ab8d] | 944 | ipc_answer_0(rid, rc);
|
---|
| 945 | free(old);
|
---|
| 946 | return;
|
---|
| 947 | }
|
---|
[732bb0c] | 948 | old[olen] = '\0';
|
---|
[a8e9ab8d] | 949 |
|
---|
| 950 | /* Retrieve the new path. */
|
---|
[732bb0c] | 951 | if (!ipc_data_write_receive(&callid, &nlen)) {
|
---|
[a8e9ab8d] | 952 | ipc_answer_0(callid, EINVAL);
|
---|
| 953 | ipc_answer_0(rid, EINVAL);
|
---|
| 954 | free(old);
|
---|
| 955 | return;
|
---|
| 956 | }
|
---|
[732bb0c] | 957 | char *new = malloc(nlen + 1);
|
---|
[a8e9ab8d] | 958 | if (!new) {
|
---|
| 959 | ipc_answer_0(callid, ENOMEM);
|
---|
| 960 | ipc_answer_0(rid, ENOMEM);
|
---|
| 961 | free(old);
|
---|
| 962 | return;
|
---|
| 963 | }
|
---|
[732bb0c] | 964 | if ((rc = ipc_data_write_finalize(callid, new, nlen))) {
|
---|
[a8e9ab8d] | 965 | ipc_answer_0(rid, rc);
|
---|
| 966 | free(old);
|
---|
| 967 | free(new);
|
---|
| 968 | return;
|
---|
| 969 | }
|
---|
[732bb0c] | 970 | new[nlen] = '\0';
|
---|
[a8e9ab8d] | 971 |
|
---|
[732bb0c] | 972 | char *oldc = canonify(old, &olen);
|
---|
| 973 | char *newc = canonify(new, &nlen);
|
---|
[a8e9ab8d] | 974 | if (!oldc || !newc) {
|
---|
| 975 | ipc_answer_0(rid, EINVAL);
|
---|
| 976 | free(old);
|
---|
| 977 | free(new);
|
---|
| 978 | return;
|
---|
| 979 | }
|
---|
[732bb0c] | 980 | oldc[olen] = '\0';
|
---|
| 981 | newc[nlen] = '\0';
|
---|
[14040e5] | 982 | if ((!str_lcmp(newc, oldc, str_length(oldc))) &&
|
---|
| 983 | ((newc[str_length(oldc)] == '/') ||
|
---|
| 984 | (str_length(oldc) == 1) ||
|
---|
| 985 | (str_length(oldc) == str_length(newc)))) {
|
---|
| 986 | /*
|
---|
| 987 | * oldc is a prefix of newc and either
|
---|
| 988 | * - newc continues with a / where oldc ends, or
|
---|
| 989 | * - oldc was / itself, or
|
---|
| 990 | * - oldc and newc are equal.
|
---|
| 991 | */
|
---|
[a8e9ab8d] | 992 | ipc_answer_0(rid, EINVAL);
|
---|
| 993 | free(old);
|
---|
| 994 | free(new);
|
---|
| 995 | return;
|
---|
| 996 | }
|
---|
| 997 |
|
---|
| 998 | vfs_lookup_res_t old_lr;
|
---|
| 999 | vfs_lookup_res_t new_lr;
|
---|
| 1000 | vfs_lookup_res_t new_par_lr;
|
---|
| 1001 | rwlock_write_lock(&namespace_rwlock);
|
---|
| 1002 | /* Lookup the node belonging to the old file name. */
|
---|
| 1003 | rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
|
---|
| 1004 | if (rc != EOK) {
|
---|
| 1005 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1006 | ipc_answer_0(rid, rc);
|
---|
| 1007 | free(old);
|
---|
| 1008 | free(new);
|
---|
| 1009 | return;
|
---|
| 1010 | }
|
---|
| 1011 | vfs_node_t *old_node = vfs_node_get(&old_lr);
|
---|
| 1012 | if (!old_node) {
|
---|
| 1013 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1014 | ipc_answer_0(rid, ENOMEM);
|
---|
| 1015 | free(old);
|
---|
| 1016 | free(new);
|
---|
| 1017 | return;
|
---|
| 1018 | }
|
---|
[4f46695e] | 1019 | /* Determine the path to the parent of the node with the new name. */
|
---|
| 1020 | char *parentc = str_dup(newc);
|
---|
| 1021 | if (!parentc) {
|
---|
| 1022 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1023 | ipc_answer_0(rid, rc);
|
---|
| 1024 | free(old);
|
---|
| 1025 | free(new);
|
---|
| 1026 | return;
|
---|
| 1027 | }
|
---|
[ae55ee8] | 1028 | char *lastsl = str_rchr(parentc + 1, '/');
|
---|
[4f46695e] | 1029 | if (lastsl)
|
---|
| 1030 | *lastsl = '\0';
|
---|
| 1031 | else
|
---|
| 1032 | parentc[1] = '\0';
|
---|
[a8e9ab8d] | 1033 | /* Lookup parent of the new file name. */
|
---|
[4f46695e] | 1034 | rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
|
---|
| 1035 | free(parentc); /* not needed anymore */
|
---|
[a8e9ab8d] | 1036 | if (rc != EOK) {
|
---|
| 1037 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1038 | ipc_answer_0(rid, rc);
|
---|
| 1039 | free(old);
|
---|
| 1040 | free(new);
|
---|
| 1041 | return;
|
---|
| 1042 | }
|
---|
| 1043 | /* Check whether linking to the same file system instance. */
|
---|
| 1044 | if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
|
---|
| 1045 | (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
|
---|
| 1046 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1047 | ipc_answer_0(rid, EXDEV); /* different file systems */
|
---|
| 1048 | free(old);
|
---|
| 1049 | free(new);
|
---|
| 1050 | return;
|
---|
| 1051 | }
|
---|
| 1052 | /* Destroy the old link for the new name. */
|
---|
| 1053 | vfs_node_t *new_node = NULL;
|
---|
| 1054 | rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
|
---|
| 1055 | switch (rc) {
|
---|
| 1056 | case ENOENT:
|
---|
| 1057 | /* simply not in our way */
|
---|
| 1058 | break;
|
---|
| 1059 | case EOK:
|
---|
| 1060 | new_node = vfs_node_get(&new_lr);
|
---|
| 1061 | if (!new_node) {
|
---|
| 1062 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1063 | ipc_answer_0(rid, ENOMEM);
|
---|
| 1064 | free(old);
|
---|
| 1065 | free(new);
|
---|
| 1066 | return;
|
---|
| 1067 | }
|
---|
[c31d773] | 1068 | futex_down(&nodes_futex);
|
---|
[a8e9ab8d] | 1069 | new_node->lnkcnt--;
|
---|
[c31d773] | 1070 | futex_up(&nodes_futex);
|
---|
[a8e9ab8d] | 1071 | break;
|
---|
| 1072 | default:
|
---|
| 1073 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1074 | ipc_answer_0(rid, ENOTEMPTY);
|
---|
| 1075 | free(old);
|
---|
| 1076 | free(new);
|
---|
| 1077 | return;
|
---|
| 1078 | }
|
---|
| 1079 | /* Create the new link for the new name. */
|
---|
| 1080 | rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
|
---|
| 1081 | if (rc != EOK) {
|
---|
| 1082 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1083 | if (new_node)
|
---|
| 1084 | vfs_node_put(new_node);
|
---|
| 1085 | ipc_answer_0(rid, rc);
|
---|
| 1086 | free(old);
|
---|
| 1087 | free(new);
|
---|
| 1088 | return;
|
---|
| 1089 | }
|
---|
[c31d773] | 1090 | futex_down(&nodes_futex);
|
---|
[a8e9ab8d] | 1091 | old_node->lnkcnt++;
|
---|
[c31d773] | 1092 | futex_up(&nodes_futex);
|
---|
[a8e9ab8d] | 1093 | /* Destroy the link for the old name. */
|
---|
| 1094 | rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
|
---|
| 1095 | if (rc != EOK) {
|
---|
| 1096 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1097 | vfs_node_put(old_node);
|
---|
| 1098 | if (new_node)
|
---|
| 1099 | vfs_node_put(new_node);
|
---|
| 1100 | ipc_answer_0(rid, rc);
|
---|
| 1101 | free(old);
|
---|
| 1102 | free(new);
|
---|
| 1103 | return;
|
---|
| 1104 | }
|
---|
[c31d773] | 1105 | futex_down(&nodes_futex);
|
---|
[a8e9ab8d] | 1106 | old_node->lnkcnt--;
|
---|
[c31d773] | 1107 | futex_up(&nodes_futex);
|
---|
[a8e9ab8d] | 1108 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 1109 | vfs_node_put(old_node);
|
---|
| 1110 | if (new_node)
|
---|
| 1111 | vfs_node_put(new_node);
|
---|
| 1112 | free(old);
|
---|
| 1113 | free(new);
|
---|
| 1114 | ipc_answer_0(rid, EOK);
|
---|
| 1115 | }
|
---|
| 1116 |
|
---|
[861e7d1] | 1117 | /**
|
---|
| 1118 | * @}
|
---|
| 1119 | */
|
---|