[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 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file vfs_ops.c
|
---|
| 35 | * @brief Operations that VFS offers to its clients.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <ipc/ipc.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
| 40 | #include <async.h>
|
---|
| 41 | #include <fibril.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 | #include <string.h>
|
---|
| 46 | #include <bool.h>
|
---|
| 47 | #include <futex.h>
|
---|
| 48 | #include <rwlock.h>
|
---|
| 49 | #include <libadt/list.h>
|
---|
| 50 | #include <unistd.h>
|
---|
| 51 | #include <ctype.h>
|
---|
| 52 | #include <as.h>
|
---|
| 53 | #include <assert.h>
|
---|
| 54 | #include <atomic.h>
|
---|
| 55 | #include "vfs.h"
|
---|
| 56 |
|
---|
| 57 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
|
---|
| 61 | * concurrent VFS operation which modifies the file system namespace.
|
---|
| 62 | */
|
---|
| 63 | RWLOCK_INITIALIZE(namespace_rwlock);
|
---|
| 64 |
|
---|
| 65 | atomic_t plb_futex = FUTEX_INITIALIZER;
|
---|
| 66 | link_t plb_head; /**< PLB entry ring buffer. */
|
---|
| 67 | uint8_t *plb = NULL;
|
---|
| 68 |
|
---|
| 69 | /** Perform a path lookup.
|
---|
| 70 | *
|
---|
| 71 | * @param path Path to be resolved; it needn't be an ASCIIZ string.
|
---|
| 72 | * @param len Number of path characters pointed by path.
|
---|
[eb27ce5a] | 73 | * @param result Empty structure where the lookup result will be stored.
|
---|
[861e7d1] | 74 | * @param altroot If non-empty, will be used instead of rootfs as the root
|
---|
| 75 | * of the whole VFS tree.
|
---|
| 76 | *
|
---|
| 77 | * @return EOK on success or an error code from errno.h.
|
---|
| 78 | */
|
---|
[eb27ce5a] | 79 | int vfs_lookup_internal(char *path, size_t len, vfs_lookup_res_t *result,
|
---|
| 80 | vfs_pair_t *altroot)
|
---|
[861e7d1] | 81 | {
|
---|
| 82 | vfs_pair_t *root;
|
---|
| 83 |
|
---|
| 84 | if (!len)
|
---|
| 85 | return EINVAL;
|
---|
| 86 |
|
---|
| 87 | if (altroot)
|
---|
| 88 | root = altroot;
|
---|
| 89 | else
|
---|
| 90 | root = (vfs_pair_t *) &rootfs;
|
---|
| 91 |
|
---|
| 92 | if (!root->fs_handle)
|
---|
| 93 | return ENOENT;
|
---|
| 94 |
|
---|
| 95 | futex_down(&plb_futex);
|
---|
| 96 |
|
---|
| 97 | plb_entry_t entry;
|
---|
| 98 | link_initialize(&entry.plb_link);
|
---|
| 99 | entry.len = len;
|
---|
| 100 |
|
---|
| 101 | off_t first; /* the first free index */
|
---|
| 102 | off_t last; /* the last free index */
|
---|
| 103 |
|
---|
| 104 | if (list_empty(&plb_head)) {
|
---|
| 105 | first = 0;
|
---|
| 106 | last = PLB_SIZE - 1;
|
---|
| 107 | } else {
|
---|
| 108 | plb_entry_t *oldest = list_get_instance(plb_head.next,
|
---|
| 109 | plb_entry_t, plb_link);
|
---|
| 110 | plb_entry_t *newest = list_get_instance(plb_head.prev,
|
---|
| 111 | plb_entry_t, plb_link);
|
---|
| 112 |
|
---|
| 113 | first = (newest->index + newest->len) % PLB_SIZE;
|
---|
| 114 | last = (oldest->index - 1) % PLB_SIZE;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | if (first <= last) {
|
---|
| 118 | if ((last - first) + 1 < len) {
|
---|
| 119 | /*
|
---|
| 120 | * The buffer cannot absorb the path.
|
---|
| 121 | */
|
---|
| 122 | futex_up(&plb_futex);
|
---|
| 123 | return ELIMIT;
|
---|
| 124 | }
|
---|
| 125 | } else {
|
---|
| 126 | if (PLB_SIZE - ((first - last) + 1) < len) {
|
---|
| 127 | /*
|
---|
| 128 | * The buffer cannot absorb the path.
|
---|
| 129 | */
|
---|
| 130 | futex_up(&plb_futex);
|
---|
| 131 | return ELIMIT;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /*
|
---|
| 136 | * We know the first free index in PLB and we also know that there is
|
---|
| 137 | * enough space in the buffer to hold our path.
|
---|
| 138 | */
|
---|
| 139 |
|
---|
| 140 | entry.index = first;
|
---|
| 141 | entry.len = len;
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Claim PLB space by inserting the entry into the PLB entry ring
|
---|
| 145 | * buffer.
|
---|
| 146 | */
|
---|
| 147 | list_append(&entry.plb_link, &plb_head);
|
---|
| 148 |
|
---|
| 149 | futex_up(&plb_futex);
|
---|
| 150 |
|
---|
| 151 | /*
|
---|
| 152 | * Copy the path into PLB.
|
---|
| 153 | */
|
---|
| 154 | size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
|
---|
| 155 | size_t cnt2 = len - cnt1;
|
---|
| 156 |
|
---|
| 157 | memcpy(&plb[first], path, cnt1);
|
---|
| 158 | memcpy(plb, &path[cnt1], cnt2);
|
---|
| 159 |
|
---|
| 160 | ipc_call_t answer;
|
---|
| 161 | int phone = vfs_grab_phone(root->fs_handle);
|
---|
| 162 | aid_t req = async_send_3(phone, VFS_LOOKUP, (ipcarg_t) first,
|
---|
| 163 | (ipcarg_t) (first + len - 1) % PLB_SIZE,
|
---|
| 164 | (ipcarg_t) root->dev_handle, &answer);
|
---|
| 165 | vfs_release_phone(phone);
|
---|
| 166 |
|
---|
| 167 | ipcarg_t rc;
|
---|
| 168 | async_wait_for(req, &rc);
|
---|
| 169 |
|
---|
| 170 | futex_down(&plb_futex);
|
---|
| 171 | list_remove(&entry.plb_link);
|
---|
| 172 | /*
|
---|
| 173 | * Erasing the path from PLB will come handy for debugging purposes.
|
---|
| 174 | */
|
---|
| 175 | memset(&plb[first], 0, cnt1);
|
---|
| 176 | memset(plb, 0, cnt2);
|
---|
| 177 | futex_up(&plb_futex);
|
---|
| 178 |
|
---|
| 179 | if (rc == EOK) {
|
---|
[eb27ce5a] | 180 | result->triplet.fs_handle = (int) IPC_GET_ARG1(answer);
|
---|
| 181 | result->triplet.dev_handle = (int) IPC_GET_ARG2(answer);
|
---|
| 182 | result->triplet.index = (int) IPC_GET_ARG3(answer);
|
---|
| 183 | result->size = (size_t) IPC_GET_ARG4(answer);
|
---|
[861e7d1] | 184 | }
|
---|
| 185 |
|
---|
| 186 | return rc;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | atomic_t rootfs_futex = FUTEX_INITIALIZER;
|
---|
| 190 | vfs_triplet_t rootfs = {
|
---|
| 191 | .fs_handle = 0,
|
---|
| 192 | .dev_handle = 0,
|
---|
| 193 | .index = 0,
|
---|
| 194 | };
|
---|
| 195 |
|
---|
[eb27ce5a] | 196 | static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result)
|
---|
[861e7d1] | 197 | {
|
---|
| 198 | vfs_pair_t altroot = {
|
---|
| 199 | .fs_handle = fs_handle,
|
---|
| 200 | .dev_handle = dev_handle,
|
---|
| 201 | };
|
---|
| 202 |
|
---|
[eb27ce5a] | 203 | return vfs_lookup_internal("/", strlen("/"), result, &altroot);
|
---|
[861e7d1] | 204 | }
|
---|
| 205 |
|
---|
| 206 | void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 207 | {
|
---|
| 208 | int dev_handle;
|
---|
| 209 | vfs_node_t *mp_node = NULL;
|
---|
| 210 |
|
---|
| 211 | /*
|
---|
| 212 | * We expect the library to do the device-name to device-handle
|
---|
| 213 | * translation for us, thus the device handle will arrive as ARG1
|
---|
| 214 | * in the request.
|
---|
| 215 | */
|
---|
| 216 | dev_handle = IPC_GET_ARG1(*request);
|
---|
| 217 |
|
---|
| 218 | /*
|
---|
| 219 | * For now, don't make use of ARG2 and ARG3, but they can be used to
|
---|
| 220 | * carry mount options in the future.
|
---|
| 221 | */
|
---|
| 222 |
|
---|
| 223 | ipc_callid_t callid;
|
---|
| 224 | size_t size;
|
---|
| 225 |
|
---|
| 226 | /*
|
---|
| 227 | * Now, we expect the client to send us data with the name of the file
|
---|
| 228 | * system.
|
---|
| 229 | */
|
---|
| 230 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 231 | ipc_answer_0(callid, EINVAL);
|
---|
| 232 | ipc_answer_0(rid, EINVAL);
|
---|
| 233 | return;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /*
|
---|
| 237 | * Don't receive more than is necessary for storing a full file system
|
---|
| 238 | * name.
|
---|
| 239 | */
|
---|
| 240 | if (size < 1 || size > FS_NAME_MAXLEN) {
|
---|
| 241 | ipc_answer_0(callid, EINVAL);
|
---|
| 242 | ipc_answer_0(rid, EINVAL);
|
---|
| 243 | return;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /*
|
---|
| 247 | * Deliver the file system name.
|
---|
| 248 | */
|
---|
| 249 | char fs_name[FS_NAME_MAXLEN + 1];
|
---|
| 250 | (void) ipc_data_write_finalize(callid, fs_name, size);
|
---|
| 251 | fs_name[size] = '\0';
|
---|
| 252 |
|
---|
| 253 | /*
|
---|
| 254 | * Check if we know a file system with the same name as is in fs_name.
|
---|
| 255 | * This will also give us its file system handle.
|
---|
| 256 | */
|
---|
| 257 | int fs_handle = fs_name_to_handle(fs_name, true);
|
---|
| 258 | if (!fs_handle) {
|
---|
| 259 | ipc_answer_0(rid, ENOENT);
|
---|
| 260 | return;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /*
|
---|
| 264 | * Now, we want the client to send us the mount point.
|
---|
| 265 | */
|
---|
| 266 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 267 | ipc_answer_0(callid, EINVAL);
|
---|
| 268 | ipc_answer_0(rid, EINVAL);
|
---|
| 269 | return;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /*
|
---|
| 273 | * Check whether size is reasonable wrt. the mount point.
|
---|
| 274 | */
|
---|
| 275 | if (size < 1 || size > MAX_PATH_LEN) {
|
---|
| 276 | ipc_answer_0(callid, EINVAL);
|
---|
| 277 | ipc_answer_0(rid, EINVAL);
|
---|
| 278 | return;
|
---|
| 279 | }
|
---|
| 280 | /*
|
---|
| 281 | * Allocate buffer for the mount point data being received.
|
---|
| 282 | */
|
---|
| 283 | uint8_t *buf;
|
---|
| 284 | buf = malloc(size);
|
---|
| 285 | if (!buf) {
|
---|
| 286 | ipc_answer_0(callid, ENOMEM);
|
---|
| 287 | ipc_answer_0(rid, ENOMEM);
|
---|
| 288 | return;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /*
|
---|
| 292 | * Deliver the mount point.
|
---|
| 293 | */
|
---|
| 294 | (void) ipc_data_write_finalize(callid, buf, size);
|
---|
| 295 |
|
---|
| 296 | /*
|
---|
| 297 | * Lookup the root node of the filesystem being mounted.
|
---|
| 298 | * In this case, we don't need to take the namespace_futex as the root
|
---|
| 299 | * node cannot be removed. However, we do take a reference to it so
|
---|
| 300 | * that we can track how many times it has been mounted.
|
---|
| 301 | */
|
---|
| 302 | int rc;
|
---|
[eb27ce5a] | 303 | vfs_lookup_res_t mr_res;
|
---|
| 304 | rc = lookup_root(fs_handle, dev_handle, &mr_res);
|
---|
[861e7d1] | 305 | if (rc != EOK) {
|
---|
| 306 | free(buf);
|
---|
| 307 | ipc_answer_0(rid, rc);
|
---|
| 308 | return;
|
---|
| 309 | }
|
---|
[eb27ce5a] | 310 | vfs_node_t *mr_node = vfs_node_get(&mr_res);
|
---|
[861e7d1] | 311 | if (!mr_node) {
|
---|
| 312 | free(buf);
|
---|
| 313 | ipc_answer_0(rid, ENOMEM);
|
---|
| 314 | return;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /*
|
---|
| 318 | * Finally, we need to resolve the path to the mountpoint.
|
---|
| 319 | */
|
---|
[eb27ce5a] | 320 | vfs_lookup_res_t mp_res;
|
---|
[861e7d1] | 321 | futex_down(&rootfs_futex);
|
---|
| 322 | if (rootfs.fs_handle) {
|
---|
| 323 | /*
|
---|
| 324 | * We already have the root FS.
|
---|
| 325 | */
|
---|
| 326 | rwlock_write_lock(&namespace_rwlock);
|
---|
[eb27ce5a] | 327 | rc = vfs_lookup_internal(buf, size, &mp_res, NULL);
|
---|
[861e7d1] | 328 | if (rc != EOK) {
|
---|
| 329 | /*
|
---|
| 330 | * The lookup failed for some reason.
|
---|
| 331 | */
|
---|
| 332 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 333 | futex_up(&rootfs_futex);
|
---|
| 334 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
| 335 | free(buf);
|
---|
| 336 | ipc_answer_0(rid, rc);
|
---|
| 337 | return;
|
---|
| 338 | }
|
---|
[eb27ce5a] | 339 | mp_node = vfs_node_get(&mp_res);
|
---|
[861e7d1] | 340 | if (!mp_node) {
|
---|
| 341 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 342 | futex_up(&rootfs_futex);
|
---|
| 343 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
| 344 | free(buf);
|
---|
| 345 | ipc_answer_0(rid, ENOMEM);
|
---|
| 346 | return;
|
---|
| 347 | }
|
---|
| 348 | /*
|
---|
| 349 | * Now we hold a reference to mp_node.
|
---|
| 350 | * It will be dropped upon the corresponding VFS_UNMOUNT.
|
---|
| 351 | * This prevents the mount point from being deleted.
|
---|
| 352 | */
|
---|
| 353 | rwlock_write_unlock(&namespace_rwlock);
|
---|
| 354 | } else {
|
---|
| 355 | /*
|
---|
| 356 | * We still don't have the root file system mounted.
|
---|
| 357 | */
|
---|
| 358 | if ((size == 1) && (buf[0] == '/')) {
|
---|
| 359 | /*
|
---|
| 360 | * For this simple, but important case, we are done.
|
---|
| 361 | */
|
---|
[eb27ce5a] | 362 | rootfs = mr_res.triplet;
|
---|
[861e7d1] | 363 | futex_up(&rootfs_futex);
|
---|
| 364 | free(buf);
|
---|
| 365 | ipc_answer_0(rid, EOK);
|
---|
| 366 | return;
|
---|
| 367 | } else {
|
---|
| 368 | /*
|
---|
| 369 | * We can't resolve this without the root filesystem
|
---|
| 370 | * being mounted first.
|
---|
| 371 | */
|
---|
| 372 | futex_up(&rootfs_futex);
|
---|
| 373 | free(buf);
|
---|
| 374 | vfs_node_put(mr_node); /* failed -> drop reference */
|
---|
| 375 | ipc_answer_0(rid, ENOENT);
|
---|
| 376 | return;
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 | futex_up(&rootfs_futex);
|
---|
| 380 |
|
---|
| 381 | free(buf); /* The buffer is not needed anymore. */
|
---|
| 382 |
|
---|
| 383 | /*
|
---|
| 384 | * At this point, we have all necessary pieces: file system and device
|
---|
| 385 | * handles, and we know the mount point VFS node and also the root node
|
---|
| 386 | * of the file system being mounted.
|
---|
| 387 | */
|
---|
| 388 |
|
---|
[eb27ce5a] | 389 | int phone = vfs_grab_phone(mp_res.triplet.fs_handle);
|
---|
[861e7d1] | 390 | /* Later we can use ARG3 to pass mode/flags. */
|
---|
[eb27ce5a] | 391 | aid_t req1 = async_send_3(phone, VFS_MOUNT,
|
---|
| 392 | (ipcarg_t) mp_res.triplet.dev_handle,
|
---|
| 393 | (ipcarg_t) mp_res.triplet.index, 0, NULL);
|
---|
[861e7d1] | 394 | /* The second call uses the same method. */
|
---|
| 395 | aid_t req2 = async_send_3(phone, VFS_MOUNT,
|
---|
[eb27ce5a] | 396 | (ipcarg_t) mr_res.triplet.fs_handle,
|
---|
| 397 | (ipcarg_t) mr_res.triplet.dev_handle,
|
---|
| 398 | (ipcarg_t) mr_res.triplet.index, NULL);
|
---|
[861e7d1] | 399 | vfs_release_phone(phone);
|
---|
| 400 |
|
---|
| 401 | ipcarg_t rc1;
|
---|
| 402 | ipcarg_t rc2;
|
---|
| 403 | async_wait_for(req1, &rc1);
|
---|
| 404 | async_wait_for(req2, &rc2);
|
---|
| 405 |
|
---|
| 406 | if ((rc1 != EOK) || (rc2 != EOK)) {
|
---|
| 407 | /* Mount failed, drop references to mr_node and mp_node. */
|
---|
| 408 | vfs_node_put(mr_node);
|
---|
| 409 | if (mp_node)
|
---|
| 410 | vfs_node_put(mp_node);
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | if (rc2 == EOK)
|
---|
| 414 | ipc_answer_0(rid, rc1);
|
---|
| 415 | else if (rc1 == EOK)
|
---|
| 416 | ipc_answer_0(rid, rc2);
|
---|
| 417 | else
|
---|
| 418 | ipc_answer_0(rid, rc1);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | void vfs_open(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 422 | {
|
---|
| 423 | if (!vfs_files_init()) {
|
---|
| 424 | ipc_answer_0(rid, ENOMEM);
|
---|
| 425 | return;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /*
|
---|
| 429 | * The POSIX interface is open(path, flags, mode).
|
---|
| 430 | * We can receive flags and mode along with the VFS_OPEN call; the path
|
---|
| 431 | * will need to arrive in another call.
|
---|
| 432 | */
|
---|
| 433 | int flags = IPC_GET_ARG1(*request);
|
---|
| 434 | int mode = IPC_GET_ARG2(*request);
|
---|
| 435 | size_t len;
|
---|
| 436 |
|
---|
| 437 | ipc_callid_t callid;
|
---|
| 438 |
|
---|
| 439 | if (!ipc_data_write_receive(&callid, &len)) {
|
---|
| 440 | ipc_answer_0(callid, EINVAL);
|
---|
| 441 | ipc_answer_0(rid, EINVAL);
|
---|
| 442 | return;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | /*
|
---|
| 446 | * Now we are on the verge of accepting the path.
|
---|
| 447 | *
|
---|
| 448 | * There is one optimization we could do in the future: copy the path
|
---|
| 449 | * directly into the PLB using some kind of a callback.
|
---|
| 450 | */
|
---|
| 451 | char *path = malloc(len);
|
---|
| 452 |
|
---|
| 453 | if (!path) {
|
---|
| 454 | ipc_answer_0(callid, ENOMEM);
|
---|
| 455 | ipc_answer_0(rid, ENOMEM);
|
---|
| 456 | return;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | int rc;
|
---|
| 460 | if ((rc = ipc_data_write_finalize(callid, path, len))) {
|
---|
| 461 | ipc_answer_0(rid, rc);
|
---|
| 462 | free(path);
|
---|
| 463 | return;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | /*
|
---|
| 467 | * Avoid the race condition in which the file can be deleted before we
|
---|
| 468 | * find/create-and-lock the VFS node corresponding to the looked-up
|
---|
| 469 | * triplet.
|
---|
| 470 | */
|
---|
| 471 | rwlock_read_lock(&namespace_rwlock);
|
---|
| 472 |
|
---|
| 473 | /*
|
---|
| 474 | * The path is now populated and we can call vfs_lookup_internal().
|
---|
| 475 | */
|
---|
[eb27ce5a] | 476 | vfs_lookup_res_t lr;
|
---|
| 477 | rc = vfs_lookup_internal(path, len, &lr, NULL);
|
---|
[861e7d1] | 478 | if (rc) {
|
---|
| 479 | rwlock_read_unlock(&namespace_rwlock);
|
---|
| 480 | ipc_answer_0(rid, rc);
|
---|
| 481 | free(path);
|
---|
| 482 | return;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | /*
|
---|
| 486 | * Path is no longer needed.
|
---|
| 487 | */
|
---|
| 488 | free(path);
|
---|
| 489 |
|
---|
[eb27ce5a] | 490 | vfs_node_t *node = vfs_node_get(&lr);
|
---|
[861e7d1] | 491 | rwlock_read_unlock(&namespace_rwlock);
|
---|
| 492 |
|
---|
| 493 | /*
|
---|
| 494 | * Get ourselves a file descriptor and the corresponding vfs_file_t
|
---|
| 495 | * structure.
|
---|
| 496 | */
|
---|
| 497 | int fd = vfs_fd_alloc();
|
---|
| 498 | if (fd < 0) {
|
---|
| 499 | vfs_node_put(node);
|
---|
| 500 | ipc_answer_0(rid, fd);
|
---|
| 501 | return;
|
---|
| 502 | }
|
---|
| 503 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 504 | file->node = node;
|
---|
| 505 |
|
---|
| 506 | /*
|
---|
| 507 | * The following increase in reference count is for the fact that the
|
---|
| 508 | * file is being opened and that a file structure is pointing to it.
|
---|
| 509 | * It is necessary so that the file will not disappear when
|
---|
| 510 | * vfs_node_put() is called. The reference will be dropped by the
|
---|
| 511 | * respective VFS_CLOSE.
|
---|
| 512 | */
|
---|
| 513 | vfs_node_addref(node);
|
---|
| 514 | vfs_node_put(node);
|
---|
| 515 |
|
---|
| 516 | /*
|
---|
| 517 | * Success! Return the new file descriptor to the client.
|
---|
| 518 | */
|
---|
| 519 | ipc_answer_1(rid, EOK, fd);
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
| 523 | {
|
---|
| 524 |
|
---|
| 525 | /*
|
---|
| 526 | * The following code strongly depends on the fact that the files data
|
---|
| 527 | * structure can be only accessed by a single fibril and all file
|
---|
| 528 | * operations are serialized (i.e. the reads and writes cannot
|
---|
| 529 | * interleave and a file cannot be closed while it is being read).
|
---|
| 530 | *
|
---|
| 531 | * Additional synchronization needs to be added once the table of
|
---|
| 532 | * open files supports parallel access!
|
---|
| 533 | */
|
---|
| 534 |
|
---|
| 535 | int fd = IPC_GET_ARG1(*request);
|
---|
| 536 |
|
---|
| 537 | /*
|
---|
| 538 | * Lookup the file structure corresponding to the file descriptor.
|
---|
| 539 | */
|
---|
| 540 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 541 | if (!file) {
|
---|
| 542 | ipc_answer_0(rid, ENOENT);
|
---|
| 543 | return;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | /*
|
---|
| 547 | * Now we need to receive a call with client's
|
---|
| 548 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
|
---|
| 549 | */
|
---|
| 550 | ipc_callid_t callid;
|
---|
| 551 | int res;
|
---|
| 552 | if (read)
|
---|
| 553 | res = ipc_data_read_receive(&callid, NULL);
|
---|
| 554 | else
|
---|
| 555 | res = ipc_data_write_receive(&callid, NULL);
|
---|
| 556 | if (!res) {
|
---|
| 557 | ipc_answer_0(callid, EINVAL);
|
---|
| 558 | ipc_answer_0(rid, EINVAL);
|
---|
| 559 | return;
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | /*
|
---|
| 563 | * Lock the open file structure so that no other thread can manipulate
|
---|
| 564 | * the same open file at a time.
|
---|
| 565 | */
|
---|
| 566 | futex_down(&file->lock);
|
---|
| 567 |
|
---|
| 568 | /*
|
---|
| 569 | * Lock the file's node so that no other client can read/write to it at
|
---|
| 570 | * the same time.
|
---|
| 571 | */
|
---|
| 572 | if (read)
|
---|
| 573 | rwlock_read_lock(&file->node->contents_rwlock);
|
---|
| 574 | else
|
---|
| 575 | rwlock_write_lock(&file->node->contents_rwlock);
|
---|
| 576 |
|
---|
| 577 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
| 578 |
|
---|
| 579 | /*
|
---|
| 580 | * Make a VFS_READ/VFS_WRITE request at the destination FS server.
|
---|
| 581 | */
|
---|
| 582 | aid_t msg;
|
---|
| 583 | ipc_call_t answer;
|
---|
| 584 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
|
---|
| 585 | file->node->dev_handle, file->node->index, file->pos, &answer);
|
---|
| 586 |
|
---|
| 587 | /*
|
---|
| 588 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
| 589 | * destination FS server. The call will be routed as if sent by
|
---|
| 590 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
| 591 | * don't have to bother.
|
---|
| 592 | */
|
---|
| 593 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
| 594 |
|
---|
| 595 | vfs_release_phone(fs_phone);
|
---|
| 596 |
|
---|
| 597 | /*
|
---|
| 598 | * Wait for reply from the FS server.
|
---|
| 599 | */
|
---|
| 600 | ipcarg_t rc;
|
---|
| 601 | async_wait_for(msg, &rc);
|
---|
| 602 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
| 603 |
|
---|
| 604 | /*
|
---|
| 605 | * Unlock the VFS node.
|
---|
| 606 | */
|
---|
| 607 | if (read)
|
---|
| 608 | rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
| 609 | else {
|
---|
| 610 | /* Update the cached version of node's size. */
|
---|
| 611 | file->node->size = IPC_GET_ARG2(answer);
|
---|
| 612 | rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | /*
|
---|
| 616 | * Update the position pointer and unlock the open file.
|
---|
| 617 | */
|
---|
| 618 | file->pos += bytes;
|
---|
| 619 | futex_up(&file->lock);
|
---|
| 620 |
|
---|
| 621 | /*
|
---|
| 622 | * FS server's reply is the final result of the whole operation we
|
---|
| 623 | * return to the client.
|
---|
| 624 | */
|
---|
| 625 | ipc_answer_1(rid, rc, bytes);
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 629 | {
|
---|
| 630 | vfs_rdwr(rid, request, true);
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 634 | {
|
---|
| 635 | vfs_rdwr(rid, request, false);
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 639 | {
|
---|
| 640 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
| 641 | off_t off = (off_t) IPC_GET_ARG2(*request);
|
---|
| 642 | int whence = (int) IPC_GET_ARG3(*request);
|
---|
| 643 |
|
---|
| 644 |
|
---|
| 645 | /*
|
---|
| 646 | * Lookup the file structure corresponding to the file descriptor.
|
---|
| 647 | */
|
---|
| 648 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 649 | if (!file) {
|
---|
| 650 | ipc_answer_0(rid, ENOENT);
|
---|
| 651 | return;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | off_t newpos;
|
---|
| 655 | futex_down(&file->lock);
|
---|
| 656 | if (whence == SEEK_SET) {
|
---|
| 657 | file->pos = off;
|
---|
| 658 | futex_up(&file->lock);
|
---|
| 659 | ipc_answer_1(rid, EOK, off);
|
---|
| 660 | return;
|
---|
| 661 | }
|
---|
| 662 | if (whence == SEEK_CUR) {
|
---|
| 663 | if (file->pos + off < file->pos) {
|
---|
| 664 | futex_up(&file->lock);
|
---|
| 665 | ipc_answer_0(rid, EOVERFLOW);
|
---|
| 666 | return;
|
---|
| 667 | }
|
---|
| 668 | file->pos += off;
|
---|
| 669 | newpos = file->pos;
|
---|
| 670 | futex_up(&file->lock);
|
---|
| 671 | ipc_answer_1(rid, EOK, newpos);
|
---|
| 672 | return;
|
---|
| 673 | }
|
---|
| 674 | if (whence == SEEK_END) {
|
---|
| 675 | rwlock_read_lock(&file->node->contents_rwlock);
|
---|
| 676 | size_t size = file->node->size;
|
---|
| 677 | rwlock_read_unlock(&file->node->contents_rwlock);
|
---|
| 678 | if (size + off < size) {
|
---|
| 679 | futex_up(&file->lock);
|
---|
| 680 | ipc_answer_0(rid, EOVERFLOW);
|
---|
| 681 | return;
|
---|
| 682 | }
|
---|
| 683 | newpos = size + off;
|
---|
| 684 | futex_up(&file->lock);
|
---|
| 685 | ipc_answer_1(rid, EOK, newpos);
|
---|
| 686 | return;
|
---|
| 687 | }
|
---|
| 688 | futex_up(&file->lock);
|
---|
| 689 | ipc_answer_0(rid, EINVAL);
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[0ee4322] | 692 | void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 693 | {
|
---|
| 694 | int fd = IPC_GET_ARG1(*request);
|
---|
| 695 | size_t size = IPC_GET_ARG2(*request);
|
---|
| 696 | ipcarg_t rc;
|
---|
| 697 |
|
---|
| 698 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 699 | if (!file) {
|
---|
| 700 | ipc_answer_0(rid, ENOENT);
|
---|
| 701 | return;
|
---|
| 702 | }
|
---|
| 703 | futex_down(&file->lock);
|
---|
| 704 |
|
---|
| 705 | rwlock_write_lock(&file->node->contents_rwlock);
|
---|
| 706 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
| 707 | rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)file->node->dev_handle,
|
---|
| 708 | (ipcarg_t)file->node->index, (ipcarg_t)size);
|
---|
| 709 | vfs_release_phone(fs_phone);
|
---|
| 710 | if (rc == EOK)
|
---|
| 711 | file->node->size = size;
|
---|
| 712 | rwlock_write_unlock(&file->node->contents_rwlock);
|
---|
| 713 |
|
---|
| 714 | futex_up(&file->lock);
|
---|
| 715 |
|
---|
| 716 | return rc;
|
---|
| 717 | }
|
---|
| 718 |
|
---|
[861e7d1] | 719 | atomic_t fs_head_futex = FUTEX_INITIALIZER;
|
---|
| 720 | link_t fs_head;
|
---|
| 721 |
|
---|
| 722 | atomic_t fs_handle_next = {
|
---|
| 723 | .count = 1
|
---|
| 724 | };
|
---|
| 725 |
|
---|
| 726 | /** Verify the VFS info structure.
|
---|
| 727 | *
|
---|
| 728 | * @param info Info structure to be verified.
|
---|
| 729 | *
|
---|
| 730 | * @return Non-zero if the info structure is sane, zero otherwise.
|
---|
| 731 | */
|
---|
| 732 | static bool vfs_info_sane(vfs_info_t *info)
|
---|
| 733 | {
|
---|
| 734 | int i;
|
---|
| 735 |
|
---|
| 736 | /*
|
---|
| 737 | * Check if the name is non-empty and is composed solely of ASCII
|
---|
| 738 | * characters [a-z]+[a-z0-9_-]*.
|
---|
| 739 | */
|
---|
| 740 | if (!islower(info->name[0])) {
|
---|
| 741 | dprintf("The name doesn't start with a lowercase character.\n");
|
---|
| 742 | return false;
|
---|
| 743 | }
|
---|
| 744 | for (i = 1; i < FS_NAME_MAXLEN; i++) {
|
---|
| 745 | if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
|
---|
| 746 | (info->name[i] != '-') && (info->name[i] != '_')) {
|
---|
| 747 | if (info->name[i] == '\0') {
|
---|
| 748 | break;
|
---|
| 749 | } else {
|
---|
| 750 | dprintf("The name contains illegal "
|
---|
| 751 | "characters.\n");
|
---|
| 752 | return false;
|
---|
| 753 | }
|
---|
| 754 | }
|
---|
| 755 | }
|
---|
| 756 | /*
|
---|
| 757 | * This check is not redundant. It ensures that the name is
|
---|
| 758 | * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
|
---|
| 759 | */
|
---|
| 760 | if (info->name[i] != '\0') {
|
---|
| 761 | dprintf("The name is not properly NULL-terminated.\n");
|
---|
| 762 | return false;
|
---|
| 763 | }
|
---|
| 764 |
|
---|
| 765 |
|
---|
| 766 | /*
|
---|
| 767 | * Check if the FS implements mandatory VFS operations.
|
---|
| 768 | */
|
---|
| 769 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
|
---|
| 770 | dprintf("Operation VFS_LOOKUP not defined by the client.\n");
|
---|
| 771 | return false;
|
---|
| 772 | }
|
---|
| 773 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
|
---|
| 774 | dprintf("Operation VFS_OPEN not defined by the client.\n");
|
---|
| 775 | return false;
|
---|
| 776 | }
|
---|
| 777 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
|
---|
| 778 | dprintf("Operation VFS_CLOSE not defined by the client.\n");
|
---|
| 779 | return false;
|
---|
| 780 | }
|
---|
| 781 | if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
|
---|
| 782 | dprintf("Operation VFS_READ not defined by the client.\n");
|
---|
| 783 | return false;
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | /*
|
---|
| 787 | * Check if each operation is either not defined, defined or default.
|
---|
| 788 | */
|
---|
[cad9c72] | 789 | for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) {
|
---|
[861e7d1] | 790 | if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
|
---|
| 791 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
|
---|
| 792 | (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
|
---|
| 793 | dprintf("Operation info not understood.\n");
|
---|
| 794 | return false;
|
---|
| 795 | }
|
---|
| 796 | }
|
---|
| 797 | return true;
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | /** VFS_REGISTER protocol function.
|
---|
| 801 | *
|
---|
| 802 | * @param rid Hash of the call with the request.
|
---|
| 803 | * @param request Call structure with the request.
|
---|
| 804 | */
|
---|
| 805 | void vfs_register(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 806 | {
|
---|
| 807 | ipc_callid_t callid;
|
---|
| 808 | ipc_call_t call;
|
---|
| 809 | int rc;
|
---|
| 810 | size_t size;
|
---|
| 811 |
|
---|
| 812 | dprintf("Processing VFS_REGISTER request received from %p.\n",
|
---|
| 813 | request->in_phone_hash);
|
---|
| 814 |
|
---|
| 815 | /*
|
---|
| 816 | * The first call has to be IPC_M_DATA_SEND in which we receive the
|
---|
| 817 | * VFS info structure from the client FS.
|
---|
| 818 | */
|
---|
| 819 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 820 | /*
|
---|
| 821 | * The client doesn't obey the same protocol as we do.
|
---|
| 822 | */
|
---|
| 823 | dprintf("Receiving of VFS info failed.\n");
|
---|
| 824 | ipc_answer_0(callid, EINVAL);
|
---|
| 825 | ipc_answer_0(rid, EINVAL);
|
---|
| 826 | return;
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 | dprintf("VFS info received, size = %d\n", size);
|
---|
| 830 |
|
---|
| 831 | /*
|
---|
| 832 | * We know the size of the VFS info structure. See if the client
|
---|
| 833 | * understands this easy concept too.
|
---|
| 834 | */
|
---|
| 835 | if (size != sizeof(vfs_info_t)) {
|
---|
| 836 | /*
|
---|
| 837 | * The client is sending us something, which cannot be
|
---|
| 838 | * the info structure.
|
---|
| 839 | */
|
---|
| 840 | dprintf("Received VFS info has bad size.\n");
|
---|
| 841 | ipc_answer_0(callid, EINVAL);
|
---|
| 842 | ipc_answer_0(rid, EINVAL);
|
---|
| 843 | return;
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 | /*
|
---|
| 847 | * Allocate and initialize a buffer for the fs_info structure.
|
---|
| 848 | */
|
---|
| 849 | fs_info_t *fs_info;
|
---|
| 850 | fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
|
---|
| 851 | if (!fs_info) {
|
---|
| 852 | dprintf("Could not allocate memory for FS info.\n");
|
---|
| 853 | ipc_answer_0(callid, ENOMEM);
|
---|
| 854 | ipc_answer_0(rid, ENOMEM);
|
---|
| 855 | return;
|
---|
| 856 | }
|
---|
| 857 | link_initialize(&fs_info->fs_link);
|
---|
| 858 | futex_initialize(&fs_info->phone_futex, 1);
|
---|
| 859 |
|
---|
| 860 | rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
|
---|
| 861 | if (rc != EOK) {
|
---|
| 862 | dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
|
---|
| 863 | rc);
|
---|
| 864 | free(fs_info);
|
---|
| 865 | ipc_answer_0(callid, rc);
|
---|
| 866 | ipc_answer_0(rid, rc);
|
---|
| 867 | return;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | dprintf("VFS info delivered.\n");
|
---|
| 871 |
|
---|
| 872 | if (!vfs_info_sane(&fs_info->vfs_info)) {
|
---|
| 873 | free(fs_info);
|
---|
| 874 | ipc_answer_0(callid, EINVAL);
|
---|
| 875 | ipc_answer_0(rid, EINVAL);
|
---|
| 876 | return;
|
---|
| 877 | }
|
---|
| 878 |
|
---|
| 879 | futex_down(&fs_head_futex);
|
---|
| 880 |
|
---|
| 881 | /*
|
---|
| 882 | * Check for duplicit registrations.
|
---|
| 883 | */
|
---|
| 884 | if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
|
---|
| 885 | /*
|
---|
| 886 | * We already register a fs like this.
|
---|
| 887 | */
|
---|
| 888 | dprintf("FS is already registered.\n");
|
---|
| 889 | futex_up(&fs_head_futex);
|
---|
| 890 | free(fs_info);
|
---|
| 891 | ipc_answer_0(callid, EEXISTS);
|
---|
| 892 | ipc_answer_0(rid, EEXISTS);
|
---|
| 893 | return;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | /*
|
---|
| 897 | * Add fs_info to the list of registered FS's.
|
---|
| 898 | */
|
---|
| 899 | dprintf("Inserting FS into the list of registered file systems.\n");
|
---|
| 900 | list_append(&fs_info->fs_link, &fs_head);
|
---|
| 901 |
|
---|
| 902 | /*
|
---|
| 903 | * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
|
---|
| 904 | * that a callback connection is created and we have a phone through
|
---|
| 905 | * which to forward VFS requests to it.
|
---|
| 906 | */
|
---|
| 907 | callid = async_get_call(&call);
|
---|
| 908 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
|
---|
| 909 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
|
---|
| 910 | list_remove(&fs_info->fs_link);
|
---|
| 911 | futex_up(&fs_head_futex);
|
---|
| 912 | free(fs_info);
|
---|
| 913 | ipc_answer_0(callid, EINVAL);
|
---|
| 914 | ipc_answer_0(rid, EINVAL);
|
---|
| 915 | return;
|
---|
| 916 | }
|
---|
| 917 | fs_info->phone = IPC_GET_ARG5(call);
|
---|
| 918 | ipc_answer_0(callid, EOK);
|
---|
| 919 |
|
---|
| 920 | dprintf("Callback connection to FS created.\n");
|
---|
| 921 |
|
---|
| 922 | /*
|
---|
| 923 | * The client will want us to send him the address space area with PLB.
|
---|
| 924 | */
|
---|
| 925 |
|
---|
| 926 | if (!ipc_share_in_receive(&callid, &size)) {
|
---|
| 927 | dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
|
---|
| 928 | list_remove(&fs_info->fs_link);
|
---|
| 929 | futex_up(&fs_head_futex);
|
---|
| 930 | ipc_hangup(fs_info->phone);
|
---|
| 931 | free(fs_info);
|
---|
| 932 | ipc_answer_0(callid, EINVAL);
|
---|
| 933 | ipc_answer_0(rid, EINVAL);
|
---|
| 934 | return;
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | /*
|
---|
| 938 | * We can only send the client address space area PLB_SIZE bytes long.
|
---|
| 939 | */
|
---|
| 940 | if (size != PLB_SIZE) {
|
---|
| 941 | dprintf("Client suggests wrong size of PFB, size = %d\n", size);
|
---|
| 942 | list_remove(&fs_info->fs_link);
|
---|
| 943 | futex_up(&fs_head_futex);
|
---|
| 944 | ipc_hangup(fs_info->phone);
|
---|
| 945 | free(fs_info);
|
---|
| 946 | ipc_answer_0(callid, EINVAL);
|
---|
| 947 | ipc_answer_0(rid, EINVAL);
|
---|
| 948 | return;
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | /*
|
---|
| 952 | * Commit to read-only sharing the PLB with the client.
|
---|
| 953 | */
|
---|
| 954 | (void) ipc_share_in_finalize(callid, plb,
|
---|
| 955 | AS_AREA_READ | AS_AREA_CACHEABLE);
|
---|
| 956 |
|
---|
| 957 | dprintf("Sharing PLB.\n");
|
---|
| 958 |
|
---|
| 959 | /*
|
---|
| 960 | * That was it. The FS has been registered.
|
---|
| 961 | * In reply to the VFS_REGISTER request, we assign the client file
|
---|
| 962 | * system a global file system handle.
|
---|
| 963 | */
|
---|
| 964 | fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
|
---|
| 965 | ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
|
---|
| 966 |
|
---|
| 967 | futex_up(&fs_head_futex);
|
---|
| 968 |
|
---|
| 969 | dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
|
---|
| 970 | FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
|
---|
| 971 | }
|
---|
| 972 |
|
---|
| 973 | /** For a given file system handle, implement policy for allocating a phone.
|
---|
| 974 | *
|
---|
| 975 | * @param handle File system handle.
|
---|
| 976 | *
|
---|
| 977 | * @return Phone over which a multi-call request can be safely
|
---|
| 978 | * sent. Return 0 if no phone was found.
|
---|
| 979 | */
|
---|
| 980 | int vfs_grab_phone(int handle)
|
---|
| 981 | {
|
---|
| 982 | /*
|
---|
| 983 | * For now, we don't try to be very clever and very fast.
|
---|
| 984 | * We simply lookup the phone in the fs_head list. We currently don't
|
---|
| 985 | * open any additional phones (even though that itself would be pretty
|
---|
| 986 | * straightforward; housekeeping multiple open phones to a FS task would
|
---|
| 987 | * be more demanding). Instead, we simply take the respective
|
---|
| 988 | * phone_futex and keep it until vfs_release_phone().
|
---|
| 989 | */
|
---|
| 990 | futex_down(&fs_head_futex);
|
---|
| 991 | link_t *cur;
|
---|
| 992 | fs_info_t *fs;
|
---|
| 993 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
| 994 | fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
| 995 | if (fs->fs_handle == handle) {
|
---|
| 996 | futex_up(&fs_head_futex);
|
---|
| 997 | /*
|
---|
| 998 | * For now, take the futex unconditionally.
|
---|
| 999 | * Oh yeah, serialization rocks.
|
---|
| 1000 | * It will be up'ed in vfs_release_phone().
|
---|
| 1001 | */
|
---|
| 1002 | futex_down(&fs->phone_futex);
|
---|
| 1003 | /*
|
---|
| 1004 | * Avoid deadlock with other fibrils in the same thread
|
---|
| 1005 | * by disabling fibril preemption.
|
---|
| 1006 | */
|
---|
| 1007 | fibril_inc_sercount();
|
---|
| 1008 | return fs->phone;
|
---|
| 1009 | }
|
---|
| 1010 | }
|
---|
| 1011 | futex_up(&fs_head_futex);
|
---|
| 1012 | return 0;
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
| 1015 | /** Tell VFS that the phone is in use for any request.
|
---|
| 1016 | *
|
---|
| 1017 | * @param phone Phone to FS task.
|
---|
| 1018 | */
|
---|
| 1019 | void vfs_release_phone(int phone)
|
---|
| 1020 | {
|
---|
| 1021 | bool found = false;
|
---|
| 1022 |
|
---|
| 1023 | /*
|
---|
| 1024 | * Undo the fibril_inc_sercount() done in vfs_grab_phone().
|
---|
| 1025 | */
|
---|
| 1026 | fibril_dec_sercount();
|
---|
| 1027 |
|
---|
| 1028 | futex_down(&fs_head_futex);
|
---|
| 1029 | link_t *cur;
|
---|
| 1030 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
| 1031 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
| 1032 | if (fs->phone == phone) {
|
---|
| 1033 | found = true;
|
---|
| 1034 | futex_up(&fs_head_futex);
|
---|
| 1035 | futex_up(&fs->phone_futex);
|
---|
| 1036 | return;
|
---|
| 1037 | }
|
---|
| 1038 | }
|
---|
| 1039 | futex_up(&fs_head_futex);
|
---|
| 1040 |
|
---|
| 1041 | /*
|
---|
| 1042 | * Not good to get here.
|
---|
| 1043 | */
|
---|
| 1044 | assert(found == true);
|
---|
| 1045 | }
|
---|
| 1046 |
|
---|
| 1047 | /** Convert file system name to its handle.
|
---|
| 1048 | *
|
---|
| 1049 | * @param name File system name.
|
---|
| 1050 | * @param lock If true, the function will down and up the
|
---|
| 1051 | * fs_head_futex.
|
---|
| 1052 | *
|
---|
| 1053 | * @return File system handle or zero if file system not found.
|
---|
| 1054 | */
|
---|
| 1055 | int fs_name_to_handle(char *name, bool lock)
|
---|
| 1056 | {
|
---|
| 1057 | int handle = 0;
|
---|
| 1058 |
|
---|
| 1059 | if (lock)
|
---|
| 1060 | futex_down(&fs_head_futex);
|
---|
| 1061 | link_t *cur;
|
---|
| 1062 | for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
|
---|
| 1063 | fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
|
---|
| 1064 | if (strncmp(fs->vfs_info.name, name,
|
---|
| 1065 | sizeof(fs->vfs_info.name)) == 0) {
|
---|
| 1066 | handle = fs->fs_handle;
|
---|
| 1067 | break;
|
---|
| 1068 | }
|
---|
| 1069 | }
|
---|
| 1070 | if (lock)
|
---|
| 1071 | futex_up(&fs_head_futex);
|
---|
| 1072 | return handle;
|
---|
| 1073 | }
|
---|
| 1074 |
|
---|
| 1075 | /**
|
---|
| 1076 | * @}
|
---|
| 1077 | */
|
---|