| 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 pipefs_ops.c
|
|---|
| 35 | * @brief Implementation of VFS operations for the PIPEFS file system
|
|---|
| 36 | * server.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include "pipefs.h"
|
|---|
| 40 | #include "../../vfs/vfs.h"
|
|---|
| 41 | #include <macros.h>
|
|---|
| 42 | #include <stdint.h>
|
|---|
| 43 | #include <async.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <atomic.h>
|
|---|
| 46 | #include <stdlib.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <stdio.h>
|
|---|
| 49 | #include <assert.h>
|
|---|
| 50 | #include <sys/types.h>
|
|---|
| 51 | #include <adt/hash_table.h>
|
|---|
| 52 | #include <as.h>
|
|---|
| 53 | #include <libfs.h>
|
|---|
| 54 |
|
|---|
| 55 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
|---|
| 56 | #define max(a, b) ((a) > (b) ? (a) : (b))
|
|---|
| 57 |
|
|---|
| 58 | #define NODES_BUCKETS 256
|
|---|
| 59 |
|
|---|
| 60 | /** All root nodes have index 0. */
|
|---|
| 61 | #define PIPEFS_SOME_ROOT 0
|
|---|
| 62 | /** Global counter for assigning node indices. Shared by all instances. */
|
|---|
| 63 | fs_index_t pipefs_next_index = 1;
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * Implementation of the libfs interface.
|
|---|
| 67 | */
|
|---|
| 68 |
|
|---|
| 69 | /* Forward declarations of static functions. */
|
|---|
| 70 | static int pipefs_match(fs_node_t **, fs_node_t *, const char *);
|
|---|
| 71 | static int pipefs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
|
|---|
| 72 | static int pipefs_node_open(fs_node_t *);
|
|---|
| 73 | static int pipefs_node_put(fs_node_t *);
|
|---|
| 74 | static int pipefs_create_node(fs_node_t **, devmap_handle_t, int);
|
|---|
| 75 | static int pipefs_destroy_node(fs_node_t *);
|
|---|
| 76 | static int pipefs_link_node(fs_node_t *, fs_node_t *, const char *);
|
|---|
| 77 | static int pipefs_unlink_node(fs_node_t *, fs_node_t *, const char *);
|
|---|
| 78 |
|
|---|
| 79 | /* Implementation of helper functions. */
|
|---|
| 80 | static int pipefs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
|---|
| 81 | {
|
|---|
| 82 | return pipefs_node_get(rfn, devmap_handle, PIPEFS_SOME_ROOT);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | static int pipefs_has_children(bool *has_children, fs_node_t *fn)
|
|---|
| 86 | {
|
|---|
| 87 | *has_children = !list_empty(&PIPEFS_NODE(fn)->cs_head);
|
|---|
| 88 | return EOK;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static fs_index_t pipefs_index_get(fs_node_t *fn)
|
|---|
| 92 | {
|
|---|
| 93 | return PIPEFS_NODE(fn)->index;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | static aoff64_t pipefs_size_get(fs_node_t *fn)
|
|---|
| 97 | {
|
|---|
| 98 | return 0;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static unsigned pipefs_lnkcnt_get(fs_node_t *fn)
|
|---|
| 102 | {
|
|---|
| 103 | return PIPEFS_NODE(fn)->lnkcnt;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static char pipefs_plb_get_char(unsigned pos)
|
|---|
| 107 | {
|
|---|
| 108 | return pipefs_reg.plb_ro[pos % PLB_SIZE];
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | static bool pipefs_is_directory(fs_node_t *fn)
|
|---|
| 112 | {
|
|---|
| 113 | return PIPEFS_NODE(fn)->type == PIPEFS_DIRECTORY;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static bool pipefs_is_file(fs_node_t *fn)
|
|---|
| 117 | {
|
|---|
| 118 | return PIPEFS_NODE(fn)->type == PIPEFS_FILE;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | static devmap_handle_t pipefs_device_get(fs_node_t *fn)
|
|---|
| 122 | {
|
|---|
| 123 | return 0;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** libfs operations */
|
|---|
| 127 | libfs_ops_t pipefs_libfs_ops = {
|
|---|
| 128 | .root_get = pipefs_root_get,
|
|---|
| 129 | .match = pipefs_match,
|
|---|
| 130 | .node_get = pipefs_node_get,
|
|---|
| 131 | .node_open = pipefs_node_open,
|
|---|
| 132 | .node_put = pipefs_node_put,
|
|---|
| 133 | .create = pipefs_create_node,
|
|---|
| 134 | .destroy = pipefs_destroy_node,
|
|---|
| 135 | .link = pipefs_link_node,
|
|---|
| 136 | .unlink = pipefs_unlink_node,
|
|---|
| 137 | .has_children = pipefs_has_children,
|
|---|
| 138 | .index_get = pipefs_index_get,
|
|---|
| 139 | .size_get = pipefs_size_get,
|
|---|
| 140 | .lnkcnt_get = pipefs_lnkcnt_get,
|
|---|
| 141 | .plb_get_char = pipefs_plb_get_char,
|
|---|
| 142 | .is_directory = pipefs_is_directory,
|
|---|
| 143 | .is_file = pipefs_is_file,
|
|---|
| 144 | .device_get = pipefs_device_get
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 | /** Hash table of all PIPEFS nodes. */
|
|---|
| 148 | hash_table_t nodes;
|
|---|
| 149 |
|
|---|
| 150 | #define NODES_KEY_DEV 0
|
|---|
| 151 | #define NODES_KEY_INDEX 1
|
|---|
| 152 |
|
|---|
| 153 | /* Implementation of hash table interface for the nodes hash table. */
|
|---|
| 154 | static hash_index_t nodes_hash(unsigned long key[])
|
|---|
| 155 | {
|
|---|
| 156 | return key[NODES_KEY_INDEX] % NODES_BUCKETS;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 160 | {
|
|---|
| 161 | pipefs_node_t *nodep = hash_table_get_instance(item, pipefs_node_t,
|
|---|
| 162 | nh_link);
|
|---|
| 163 |
|
|---|
| 164 | switch (keys) {
|
|---|
| 165 | case 1:
|
|---|
| 166 | return (nodep->devmap_handle == key[NODES_KEY_DEV]);
|
|---|
| 167 | case 2:
|
|---|
| 168 | return ((nodep->devmap_handle == key[NODES_KEY_DEV]) &&
|
|---|
| 169 | (nodep->index == key[NODES_KEY_INDEX]));
|
|---|
| 170 | default:
|
|---|
| 171 | assert((keys == 1) || (keys == 2));
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | return 0;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | static void nodes_remove_callback(link_t *item)
|
|---|
| 178 | {
|
|---|
| 179 | pipefs_node_t *nodep = hash_table_get_instance(item, pipefs_node_t,
|
|---|
| 180 | nh_link);
|
|---|
| 181 |
|
|---|
| 182 | while (!list_empty(&nodep->cs_head)) {
|
|---|
| 183 | pipefs_dentry_t *dentryp = list_get_instance(nodep->cs_head.next,
|
|---|
| 184 | pipefs_dentry_t, link);
|
|---|
| 185 |
|
|---|
| 186 | assert(nodep->type == PIPEFS_DIRECTORY);
|
|---|
| 187 | list_remove(&dentryp->link);
|
|---|
| 188 | free(dentryp);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | free(nodep->bp);
|
|---|
| 192 | free(nodep);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** PIPEFS nodes hash table operations. */
|
|---|
| 196 | hash_table_operations_t nodes_ops = {
|
|---|
| 197 | .hash = nodes_hash,
|
|---|
| 198 | .compare = nodes_compare,
|
|---|
| 199 | .remove_callback = nodes_remove_callback
|
|---|
| 200 | };
|
|---|
| 201 |
|
|---|
| 202 | static void pipefs_node_initialize(pipefs_node_t *nodep)
|
|---|
| 203 | {
|
|---|
| 204 | nodep->bp = NULL;
|
|---|
| 205 | nodep->index = 0;
|
|---|
| 206 | nodep->devmap_handle = 0;
|
|---|
| 207 | nodep->type = PIPEFS_NONE;
|
|---|
| 208 | nodep->lnkcnt = 0;
|
|---|
| 209 | nodep->start = 0;
|
|---|
| 210 | nodep->data = NULL;
|
|---|
| 211 | nodep->data_size = 0;
|
|---|
| 212 | fibril_mutex_initialize(&nodep->data_lock);
|
|---|
| 213 | fibril_condvar_initialize(&nodep->data_available);
|
|---|
| 214 | fibril_condvar_initialize(&nodep->data_consumed);
|
|---|
| 215 | link_initialize(&nodep->nh_link);
|
|---|
| 216 | list_initialize(&nodep->cs_head);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | static void pipefs_dentry_initialize(pipefs_dentry_t *dentryp)
|
|---|
| 220 | {
|
|---|
| 221 | link_initialize(&dentryp->link);
|
|---|
| 222 | dentryp->name = NULL;
|
|---|
| 223 | dentryp->node = NULL;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | bool pipefs_init(void)
|
|---|
| 227 | {
|
|---|
| 228 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
|
|---|
| 229 | return false;
|
|---|
| 230 |
|
|---|
| 231 | return true;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | static bool pipefs_instance_init(devmap_handle_t devmap_handle)
|
|---|
| 235 | {
|
|---|
| 236 | fs_node_t *rfn;
|
|---|
| 237 | int rc;
|
|---|
| 238 |
|
|---|
| 239 | rc = pipefs_create_node(&rfn, devmap_handle, L_DIRECTORY);
|
|---|
| 240 | if (rc != EOK || !rfn)
|
|---|
| 241 | return false;
|
|---|
| 242 | PIPEFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
|---|
| 243 | return true;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | static void pipefs_instance_done(devmap_handle_t devmap_handle)
|
|---|
| 247 | {
|
|---|
| 248 | unsigned long key[] = {
|
|---|
| 249 | [NODES_KEY_DEV] = devmap_handle
|
|---|
| 250 | };
|
|---|
| 251 | /*
|
|---|
| 252 | * Here we are making use of one special feature of our hash table
|
|---|
| 253 | * implementation, which allows to remove more items based on a partial
|
|---|
| 254 | * key match. In the following, we are going to remove all nodes
|
|---|
| 255 | * matching our device handle. The nodes_remove_callback() function will
|
|---|
| 256 | * take care of resource deallocation.
|
|---|
| 257 | */
|
|---|
| 258 | hash_table_remove(&nodes, key, 1);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | int pipefs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
|---|
| 262 | {
|
|---|
| 263 | pipefs_node_t *parentp = PIPEFS_NODE(pfn);
|
|---|
| 264 | link_t *lnk;
|
|---|
| 265 |
|
|---|
| 266 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 267 | lnk = lnk->next) {
|
|---|
| 268 | pipefs_dentry_t *dentryp;
|
|---|
| 269 | dentryp = list_get_instance(lnk, pipefs_dentry_t, link);
|
|---|
| 270 | if (!str_cmp(dentryp->name, component)) {
|
|---|
| 271 | *rfn = FS_NODE(dentryp->node);
|
|---|
| 272 | return EOK;
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | *rfn = NULL;
|
|---|
| 277 | return EOK;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | int pipefs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
|
|---|
| 281 | {
|
|---|
| 282 | unsigned long key[] = {
|
|---|
| 283 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 284 | [NODES_KEY_INDEX] = index
|
|---|
| 285 | };
|
|---|
| 286 | link_t *lnk = hash_table_find(&nodes, key);
|
|---|
| 287 | if (lnk) {
|
|---|
| 288 | pipefs_node_t *nodep;
|
|---|
| 289 | nodep = hash_table_get_instance(lnk, pipefs_node_t, nh_link);
|
|---|
| 290 | *rfn = FS_NODE(nodep);
|
|---|
| 291 | } else {
|
|---|
| 292 | *rfn = NULL;
|
|---|
| 293 | }
|
|---|
| 294 | return EOK;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | int pipefs_node_open(fs_node_t *fn)
|
|---|
| 298 | {
|
|---|
| 299 | /* nothing to do */
|
|---|
| 300 | return EOK;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | int pipefs_node_put(fs_node_t *fn)
|
|---|
| 304 | {
|
|---|
| 305 | /* nothing to do */
|
|---|
| 306 | return EOK;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | int pipefs_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int lflag)
|
|---|
| 310 | {
|
|---|
| 311 | fs_node_t *rootfn;
|
|---|
| 312 | int rc;
|
|---|
| 313 |
|
|---|
| 314 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
|---|
| 315 |
|
|---|
| 316 | pipefs_node_t *nodep = malloc(sizeof(pipefs_node_t));
|
|---|
| 317 | if (!nodep)
|
|---|
| 318 | return ENOMEM;
|
|---|
| 319 | pipefs_node_initialize(nodep);
|
|---|
| 320 | nodep->bp = malloc(sizeof(fs_node_t));
|
|---|
| 321 | if (!nodep->bp) {
|
|---|
| 322 | free(nodep);
|
|---|
| 323 | return ENOMEM;
|
|---|
| 324 | }
|
|---|
| 325 | fs_node_initialize(nodep->bp);
|
|---|
| 326 | nodep->bp->data = nodep; /* link the FS and PIPEFS nodes */
|
|---|
| 327 |
|
|---|
| 328 | rc = pipefs_root_get(&rootfn, devmap_handle);
|
|---|
| 329 | assert(rc == EOK);
|
|---|
| 330 | if (!rootfn)
|
|---|
| 331 | nodep->index = PIPEFS_SOME_ROOT;
|
|---|
| 332 | else
|
|---|
| 333 | nodep->index = pipefs_next_index++;
|
|---|
| 334 | nodep->devmap_handle = devmap_handle;
|
|---|
| 335 | if (lflag & L_DIRECTORY)
|
|---|
| 336 | nodep->type = PIPEFS_DIRECTORY;
|
|---|
| 337 | else
|
|---|
| 338 | nodep->type = PIPEFS_FILE;
|
|---|
| 339 |
|
|---|
| 340 | /* Insert the new node into the nodes hash table. */
|
|---|
| 341 | unsigned long key[] = {
|
|---|
| 342 | [NODES_KEY_DEV] = nodep->devmap_handle,
|
|---|
| 343 | [NODES_KEY_INDEX] = nodep->index
|
|---|
| 344 | };
|
|---|
| 345 | hash_table_insert(&nodes, key, &nodep->nh_link);
|
|---|
| 346 | *rfn = FS_NODE(nodep);
|
|---|
| 347 | return EOK;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | int pipefs_destroy_node(fs_node_t *fn)
|
|---|
| 351 | {
|
|---|
| 352 | pipefs_node_t *nodep = PIPEFS_NODE(fn);
|
|---|
| 353 |
|
|---|
| 354 | assert(!nodep->lnkcnt);
|
|---|
| 355 | assert(list_empty(&nodep->cs_head));
|
|---|
| 356 |
|
|---|
| 357 | unsigned long key[] = {
|
|---|
| 358 | [NODES_KEY_DEV] = nodep->devmap_handle,
|
|---|
| 359 | [NODES_KEY_INDEX] = nodep->index
|
|---|
| 360 | };
|
|---|
| 361 | hash_table_remove(&nodes, key, 2);
|
|---|
| 362 |
|
|---|
| 363 | /*
|
|---|
| 364 | * The nodes_remove_callback() function takes care of the actual
|
|---|
| 365 | * resource deallocation.
|
|---|
| 366 | */
|
|---|
| 367 | return EOK;
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | int pipefs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 371 | {
|
|---|
| 372 | pipefs_node_t *parentp = PIPEFS_NODE(pfn);
|
|---|
| 373 | pipefs_node_t *childp = PIPEFS_NODE(cfn);
|
|---|
| 374 | pipefs_dentry_t *dentryp;
|
|---|
| 375 | link_t *lnk;
|
|---|
| 376 |
|
|---|
| 377 | assert(parentp->type == PIPEFS_DIRECTORY);
|
|---|
| 378 |
|
|---|
| 379 | /* Check for duplicit entries. */
|
|---|
| 380 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 381 | lnk = lnk->next) {
|
|---|
| 382 | dentryp = list_get_instance(lnk, pipefs_dentry_t, link);
|
|---|
| 383 | if (!str_cmp(dentryp->name, nm))
|
|---|
| 384 | return EEXIST;
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | /* Allocate and initialize the dentry. */
|
|---|
| 388 | dentryp = malloc(sizeof(pipefs_dentry_t));
|
|---|
| 389 | if (!dentryp)
|
|---|
| 390 | return ENOMEM;
|
|---|
| 391 | pipefs_dentry_initialize(dentryp);
|
|---|
| 392 |
|
|---|
| 393 | /* Populate and link the new dentry. */
|
|---|
| 394 | size_t size = str_size(nm);
|
|---|
| 395 | dentryp->name = malloc(size + 1);
|
|---|
| 396 | if (!dentryp->name) {
|
|---|
| 397 | free(dentryp);
|
|---|
| 398 | return ENOMEM;
|
|---|
| 399 | }
|
|---|
| 400 | str_cpy(dentryp->name, size + 1, nm);
|
|---|
| 401 | dentryp->node = childp;
|
|---|
| 402 | childp->lnkcnt++;
|
|---|
| 403 | list_append(&dentryp->link, &parentp->cs_head);
|
|---|
| 404 |
|
|---|
| 405 | return EOK;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | int pipefs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 409 | {
|
|---|
| 410 | pipefs_node_t *parentp = PIPEFS_NODE(pfn);
|
|---|
| 411 | pipefs_node_t *childp = NULL;
|
|---|
| 412 | pipefs_dentry_t *dentryp;
|
|---|
| 413 | link_t *lnk;
|
|---|
| 414 |
|
|---|
| 415 | if (!parentp)
|
|---|
| 416 | return EBUSY;
|
|---|
| 417 |
|
|---|
| 418 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 419 | lnk = lnk->next) {
|
|---|
| 420 | dentryp = list_get_instance(lnk, pipefs_dentry_t, link);
|
|---|
| 421 | if (!str_cmp(dentryp->name, nm)) {
|
|---|
| 422 | childp = dentryp->node;
|
|---|
| 423 | assert(FS_NODE(childp) == cfn);
|
|---|
| 424 | break;
|
|---|
| 425 | }
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | if (!childp)
|
|---|
| 429 | return ENOENT;
|
|---|
| 430 |
|
|---|
| 431 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
|
|---|
| 432 | return ENOTEMPTY;
|
|---|
| 433 |
|
|---|
| 434 | list_remove(&dentryp->link);
|
|---|
| 435 | free(dentryp);
|
|---|
| 436 | childp->lnkcnt--;
|
|---|
| 437 |
|
|---|
| 438 | return EOK;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | void pipefs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 442 | {
|
|---|
| 443 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 444 | fs_node_t *rootfn;
|
|---|
| 445 | int rc;
|
|---|
| 446 |
|
|---|
| 447 | /* Accept the mount options. */
|
|---|
| 448 | char *opts;
|
|---|
| 449 | rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
|---|
| 450 | if (rc != EOK) {
|
|---|
| 451 | async_answer_0(rid, rc);
|
|---|
| 452 | return;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | /* Check if this device is not already mounted. */
|
|---|
| 456 | rc = pipefs_root_get(&rootfn, devmap_handle);
|
|---|
| 457 | if ((rc == EOK) && (rootfn)) {
|
|---|
| 458 | (void) pipefs_node_put(rootfn);
|
|---|
| 459 | free(opts);
|
|---|
| 460 | async_answer_0(rid, EEXIST);
|
|---|
| 461 | return;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /* Initialize PIPEFS instance. */
|
|---|
| 465 | if (!pipefs_instance_init(devmap_handle)) {
|
|---|
| 466 | free(opts);
|
|---|
| 467 | async_answer_0(rid, ENOMEM);
|
|---|
| 468 | return;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | rc = pipefs_root_get(&rootfn, devmap_handle);
|
|---|
| 472 | assert(rc == EOK);
|
|---|
| 473 | pipefs_node_t *rootp = PIPEFS_NODE(rootfn);
|
|---|
| 474 | async_answer_3(rid, EOK, rootp->index, 0, rootp->lnkcnt);
|
|---|
| 475 | free(opts);
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | void pipefs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 479 | {
|
|---|
| 480 | libfs_mount(&pipefs_libfs_ops, pipefs_reg.fs_handle, rid, request);
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | void pipefs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 484 | {
|
|---|
| 485 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 486 |
|
|---|
| 487 | pipefs_instance_done(devmap_handle);
|
|---|
| 488 | async_answer_0(rid, EOK);
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | void pipefs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 492 | {
|
|---|
| 493 | libfs_unmount(&pipefs_libfs_ops, rid, request);
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | void pipefs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 497 | {
|
|---|
| 498 | libfs_lookup(&pipefs_libfs_ops, pipefs_reg.fs_handle, rid, request);
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | void pipefs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 502 | {
|
|---|
| 503 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 504 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 505 | aoff64_t pos =
|
|---|
| 506 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
|---|
| 507 |
|
|---|
| 508 | /*
|
|---|
| 509 | * Lookup the respective PIPEFS node.
|
|---|
| 510 | */
|
|---|
| 511 | link_t *hlp;
|
|---|
| 512 | unsigned long key[] = {
|
|---|
| 513 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 514 | [NODES_KEY_INDEX] = index
|
|---|
| 515 | };
|
|---|
| 516 | hlp = hash_table_find(&nodes, key);
|
|---|
| 517 | if (!hlp) {
|
|---|
| 518 | async_answer_0(rid, ENOENT);
|
|---|
| 519 | return;
|
|---|
| 520 | }
|
|---|
| 521 | pipefs_node_t *nodep = hash_table_get_instance(hlp, pipefs_node_t,
|
|---|
| 522 | nh_link);
|
|---|
| 523 |
|
|---|
| 524 | /*
|
|---|
| 525 | * Receive the read request.
|
|---|
| 526 | */
|
|---|
| 527 | ipc_callid_t callid;
|
|---|
| 528 | size_t size;
|
|---|
| 529 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 530 | async_answer_0(callid, EINVAL);
|
|---|
| 531 | async_answer_0(rid, EINVAL);
|
|---|
| 532 | return;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | size_t bytes;
|
|---|
| 536 | if (nodep->type == PIPEFS_FILE) {
|
|---|
| 537 | fibril_mutex_lock(&nodep->data_lock);
|
|---|
| 538 |
|
|---|
| 539 | /*
|
|---|
| 540 | * Check if the client didn't seek somewhere else
|
|---|
| 541 | */
|
|---|
| 542 | if (pos != nodep->start) {
|
|---|
| 543 | async_answer_0(callid, ENOTSUP);
|
|---|
| 544 | async_answer_0(rid, ENOTSUP);
|
|---|
| 545 | fibril_mutex_unlock(&nodep->data_lock);
|
|---|
| 546 | return;
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | if (nodep->data == NULL || nodep->data_size > 0) {
|
|---|
| 550 | // Wait for the data
|
|---|
| 551 | fibril_condvar_wait(&nodep->data_available, &nodep->data_lock);
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | assert(nodep->data != NULL);
|
|---|
| 555 | assert(nodep->data_size > 0);
|
|---|
| 556 |
|
|---|
| 557 | bytes = min(size, nodep->data_size);
|
|---|
| 558 |
|
|---|
| 559 | (void) async_data_read_finalize(callid, nodep->data, bytes);
|
|---|
| 560 |
|
|---|
| 561 | nodep->data += bytes;
|
|---|
| 562 | nodep->data_size -= bytes;
|
|---|
| 563 | nodep->start += bytes;
|
|---|
| 564 |
|
|---|
| 565 | if (nodep->data_size == 0) {
|
|---|
| 566 | nodep->data = NULL;
|
|---|
| 567 | fibril_condvar_broadcast(&nodep->data_consumed);
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | fibril_mutex_unlock(&nodep->data_lock);
|
|---|
| 571 | } else {
|
|---|
| 572 | pipefs_dentry_t *dentryp;
|
|---|
| 573 | link_t *lnk;
|
|---|
| 574 | aoff64_t i;
|
|---|
| 575 |
|
|---|
| 576 | assert(nodep->type == PIPEFS_DIRECTORY);
|
|---|
| 577 |
|
|---|
| 578 | /*
|
|---|
| 579 | * Yes, we really use O(n) algorithm here.
|
|---|
| 580 | * If it bothers someone, it could be fixed by introducing a
|
|---|
| 581 | * hash table.
|
|---|
| 582 | */
|
|---|
| 583 | for (i = 0, lnk = nodep->cs_head.next;
|
|---|
| 584 | (i < pos) && (lnk != &nodep->cs_head);
|
|---|
| 585 | i++, lnk = lnk->next)
|
|---|
| 586 | ;
|
|---|
| 587 |
|
|---|
| 588 | if (lnk == &nodep->cs_head) {
|
|---|
| 589 | async_answer_0(callid, ENOENT);
|
|---|
| 590 | async_answer_1(rid, ENOENT, 0);
|
|---|
| 591 | return;
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | dentryp = list_get_instance(lnk, pipefs_dentry_t, link);
|
|---|
| 595 |
|
|---|
| 596 | (void) async_data_read_finalize(callid, dentryp->name,
|
|---|
| 597 | str_size(dentryp->name) + 1);
|
|---|
| 598 | bytes = 1;
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /*
|
|---|
| 602 | * Answer the VFS_READ call.
|
|---|
| 603 | */
|
|---|
| 604 | async_answer_1(rid, EOK, bytes);
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | void pipefs_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 608 | {
|
|---|
| 609 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 610 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 611 | aoff64_t pos =
|
|---|
| 612 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
|---|
| 613 |
|
|---|
| 614 | /*
|
|---|
| 615 | * Lookup the respective PIPEFS node.
|
|---|
| 616 | */
|
|---|
| 617 | link_t *hlp;
|
|---|
| 618 | unsigned long key[] = {
|
|---|
| 619 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 620 | [NODES_KEY_INDEX] = index
|
|---|
| 621 | };
|
|---|
| 622 | hlp = hash_table_find(&nodes, key);
|
|---|
| 623 | if (!hlp) {
|
|---|
| 624 | async_answer_0(rid, ENOENT);
|
|---|
| 625 | return;
|
|---|
| 626 | }
|
|---|
| 627 | pipefs_node_t *nodep = hash_table_get_instance(hlp, pipefs_node_t,
|
|---|
| 628 | nh_link);
|
|---|
| 629 |
|
|---|
| 630 | /*
|
|---|
| 631 | * Receive the write request.
|
|---|
| 632 | */
|
|---|
| 633 | ipc_callid_t callid;
|
|---|
| 634 | size_t size;
|
|---|
| 635 | if (!async_data_write_receive(&callid, &size)) {
|
|---|
| 636 | async_answer_0(callid, EINVAL);
|
|---|
| 637 | async_answer_0(rid, EINVAL);
|
|---|
| 638 | return;
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | if (size == 0) {
|
|---|
| 642 | async_data_write_finalize(callid, NULL, 0);
|
|---|
| 643 | async_answer_2(rid, EOK, 0, 0);
|
|---|
| 644 | return;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | fibril_mutex_lock(&nodep->data_lock);
|
|---|
| 648 |
|
|---|
| 649 | /*
|
|---|
| 650 | * Check whether we are writing to the end
|
|---|
| 651 | */
|
|---|
| 652 | if (pos != nodep->start+nodep->data_size) {
|
|---|
| 653 | fibril_mutex_unlock(&nodep->data_lock);
|
|---|
| 654 | async_answer_0(callid, ENOTSUP);
|
|---|
| 655 | async_answer_0(rid, ENOTSUP);
|
|---|
| 656 | return;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | /*
|
|---|
| 660 | * Wait until there is no data buffer
|
|---|
| 661 | */
|
|---|
| 662 | if (nodep->data != NULL) {
|
|---|
| 663 | fibril_condvar_wait(&nodep->data_consumed, &nodep->data_lock);
|
|---|
| 664 | }
|
|---|
| 665 |
|
|---|
| 666 | assert(nodep->data == NULL);
|
|---|
| 667 |
|
|---|
| 668 | /*
|
|---|
| 669 | * Allocate a buffer for the new data.
|
|---|
| 670 | * Currently we accept any size
|
|---|
| 671 | */
|
|---|
| 672 | void *newdata = malloc(size);
|
|---|
| 673 | if (!newdata) {
|
|---|
| 674 | fibril_mutex_unlock(&nodep->data_lock);
|
|---|
| 675 | async_answer_0(callid, ENOMEM);
|
|---|
| 676 | async_answer_0(rid, ENOMEM);
|
|---|
| 677 | return;
|
|---|
| 678 | }
|
|---|
| 679 |
|
|---|
| 680 | (void) async_data_write_finalize(callid, newdata, size);
|
|---|
| 681 |
|
|---|
| 682 | nodep->data = newdata;
|
|---|
| 683 | nodep->data_size = size;
|
|---|
| 684 |
|
|---|
| 685 | fibril_mutex_unlock(&nodep->data_lock);
|
|---|
| 686 |
|
|---|
| 687 | // Signal that the data is ready
|
|---|
| 688 | fibril_condvar_signal(&nodep->data_available);
|
|---|
| 689 |
|
|---|
| 690 | fibril_mutex_lock(&nodep->data_lock);
|
|---|
| 691 |
|
|---|
| 692 | // Wait until all data is consumed
|
|---|
| 693 | fibril_condvar_wait(&nodep->data_consumed, &nodep->data_lock);
|
|---|
| 694 |
|
|---|
| 695 | assert(nodep->data == NULL);
|
|---|
| 696 |
|
|---|
| 697 | fibril_mutex_unlock(&nodep->data_lock);
|
|---|
| 698 | free(newdata);
|
|---|
| 699 |
|
|---|
| 700 | async_answer_2(rid, EOK, size, 0);
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | void pipefs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 704 | {
|
|---|
| 705 | /*
|
|---|
| 706 | * PIPEFS does not support resizing of files
|
|---|
| 707 | */
|
|---|
| 708 | async_answer_0(rid, ENOTSUP);
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | void pipefs_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 712 | {
|
|---|
| 713 | async_answer_0(rid, EOK);
|
|---|
| 714 | }
|
|---|
| 715 |
|
|---|
| 716 | void pipefs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 717 | {
|
|---|
| 718 | devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 719 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 720 | int rc;
|
|---|
| 721 |
|
|---|
| 722 | link_t *hlp;
|
|---|
| 723 | unsigned long key[] = {
|
|---|
| 724 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 725 | [NODES_KEY_INDEX] = index
|
|---|
| 726 | };
|
|---|
| 727 | hlp = hash_table_find(&nodes, key);
|
|---|
| 728 | if (!hlp) {
|
|---|
| 729 | async_answer_0(rid, ENOENT);
|
|---|
| 730 | return;
|
|---|
| 731 | }
|
|---|
| 732 | pipefs_node_t *nodep = hash_table_get_instance(hlp, pipefs_node_t,
|
|---|
| 733 | nh_link);
|
|---|
| 734 | rc = pipefs_destroy_node(FS_NODE(nodep));
|
|---|
| 735 | async_answer_0(rid, rc);
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | void pipefs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 739 | {
|
|---|
| 740 | libfs_open_node(&pipefs_libfs_ops, pipefs_reg.fs_handle, rid, request);
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | void pipefs_stat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 744 | {
|
|---|
| 745 | libfs_stat(&pipefs_libfs_ops, pipefs_reg.fs_handle, rid, request);
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | void pipefs_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 749 | {
|
|---|
| 750 | /*
|
|---|
| 751 | * PIPEFS keeps its data structures always consistent,
|
|---|
| 752 | * thus the sync operation is a no-op.
|
|---|
| 753 | */
|
|---|
| 754 | async_answer_0(rid, EOK);
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | /**
|
|---|
| 758 | * @}
|
|---|
| 759 | */
|
|---|