| 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 tmpfs_ops.c
|
|---|
| 35 | * @brief Implementation of VFS operations for the TMPFS file system
|
|---|
| 36 | * server.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include "tmpfs.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 TMPFS_SOME_ROOT 0
|
|---|
| 62 | /** Global counter for assigning node indices. Shared by all instances. */
|
|---|
| 63 | fs_index_t tmpfs_next_index = 1;
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * Implementation of the libfs interface.
|
|---|
| 67 | */
|
|---|
| 68 |
|
|---|
| 69 | /* Forward declarations of static functions. */
|
|---|
| 70 | static int tmpfs_match(fs_node_t **, fs_node_t *, const char *);
|
|---|
| 71 | static int tmpfs_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
|
|---|
| 72 | static int tmpfs_node_open(fs_node_t *);
|
|---|
| 73 | static int tmpfs_node_put(fs_node_t *);
|
|---|
| 74 | static int tmpfs_create_node(fs_node_t **, devmap_handle_t, int);
|
|---|
| 75 | static int tmpfs_destroy_node(fs_node_t *);
|
|---|
| 76 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
|
|---|
| 77 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *);
|
|---|
| 78 |
|
|---|
| 79 | /* Implementation of helper functions. */
|
|---|
| 80 | static int tmpfs_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
|
|---|
| 81 | {
|
|---|
| 82 | return tmpfs_node_get(rfn, devmap_handle, TMPFS_SOME_ROOT);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | static int tmpfs_has_children(bool *has_children, fs_node_t *fn)
|
|---|
| 86 | {
|
|---|
| 87 | *has_children = !list_empty(&TMPFS_NODE(fn)->cs_head);
|
|---|
| 88 | return EOK;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static fs_index_t tmpfs_index_get(fs_node_t *fn)
|
|---|
| 92 | {
|
|---|
| 93 | return TMPFS_NODE(fn)->index;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | static aoff64_t tmpfs_size_get(fs_node_t *fn)
|
|---|
| 97 | {
|
|---|
| 98 | return TMPFS_NODE(fn)->size;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn)
|
|---|
| 102 | {
|
|---|
| 103 | return TMPFS_NODE(fn)->lnkcnt;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static char tmpfs_plb_get_char(unsigned pos)
|
|---|
| 107 | {
|
|---|
| 108 | return tmpfs_reg.plb_ro[pos % PLB_SIZE];
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | static bool tmpfs_is_directory(fs_node_t *fn)
|
|---|
| 112 | {
|
|---|
| 113 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static bool tmpfs_is_file(fs_node_t *fn)
|
|---|
| 117 | {
|
|---|
| 118 | return TMPFS_NODE(fn)->type == TMPFS_FILE;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | static devmap_handle_t tmpfs_device_get(fs_node_t *fn)
|
|---|
| 122 | {
|
|---|
| 123 | return 0;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** libfs operations */
|
|---|
| 127 | libfs_ops_t tmpfs_libfs_ops = {
|
|---|
| 128 | .root_get = tmpfs_root_get,
|
|---|
| 129 | .match = tmpfs_match,
|
|---|
| 130 | .node_get = tmpfs_node_get,
|
|---|
| 131 | .node_open = tmpfs_node_open,
|
|---|
| 132 | .node_put = tmpfs_node_put,
|
|---|
| 133 | .create = tmpfs_create_node,
|
|---|
| 134 | .destroy = tmpfs_destroy_node,
|
|---|
| 135 | .link = tmpfs_link_node,
|
|---|
| 136 | .unlink = tmpfs_unlink_node,
|
|---|
| 137 | .has_children = tmpfs_has_children,
|
|---|
| 138 | .index_get = tmpfs_index_get,
|
|---|
| 139 | .size_get = tmpfs_size_get,
|
|---|
| 140 | .lnkcnt_get = tmpfs_lnkcnt_get,
|
|---|
| 141 | .plb_get_char = tmpfs_plb_get_char,
|
|---|
| 142 | .is_directory = tmpfs_is_directory,
|
|---|
| 143 | .is_file = tmpfs_is_file,
|
|---|
| 144 | .device_get = tmpfs_device_get
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 | /** Hash table of all TMPFS 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 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_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 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
|---|
| 180 | nh_link);
|
|---|
| 181 |
|
|---|
| 182 | while (!list_empty(&nodep->cs_head)) {
|
|---|
| 183 | tmpfs_dentry_t *dentryp = list_get_instance(nodep->cs_head.next,
|
|---|
| 184 | tmpfs_dentry_t, link);
|
|---|
| 185 |
|
|---|
| 186 | assert(nodep->type == TMPFS_DIRECTORY);
|
|---|
| 187 | list_remove(&dentryp->link);
|
|---|
| 188 | free(dentryp);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | if (nodep->data) {
|
|---|
| 192 | assert(nodep->type == TMPFS_FILE);
|
|---|
| 193 | free(nodep->data);
|
|---|
| 194 | }
|
|---|
| 195 | free(nodep->bp);
|
|---|
| 196 | free(nodep);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /** TMPFS nodes hash table operations. */
|
|---|
| 200 | hash_table_operations_t nodes_ops = {
|
|---|
| 201 | .hash = nodes_hash,
|
|---|
| 202 | .compare = nodes_compare,
|
|---|
| 203 | .remove_callback = nodes_remove_callback
|
|---|
| 204 | };
|
|---|
| 205 |
|
|---|
| 206 | static void tmpfs_node_initialize(tmpfs_node_t *nodep)
|
|---|
| 207 | {
|
|---|
| 208 | nodep->bp = NULL;
|
|---|
| 209 | nodep->index = 0;
|
|---|
| 210 | nodep->devmap_handle = 0;
|
|---|
| 211 | nodep->type = TMPFS_NONE;
|
|---|
| 212 | nodep->lnkcnt = 0;
|
|---|
| 213 | nodep->size = 0;
|
|---|
| 214 | nodep->data = NULL;
|
|---|
| 215 | link_initialize(&nodep->nh_link);
|
|---|
| 216 | list_initialize(&nodep->cs_head);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
|
|---|
| 220 | {
|
|---|
| 221 | link_initialize(&dentryp->link);
|
|---|
| 222 | dentryp->name = NULL;
|
|---|
| 223 | dentryp->node = NULL;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | bool tmpfs_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 tmpfs_instance_init(devmap_handle_t devmap_handle)
|
|---|
| 235 | {
|
|---|
| 236 | fs_node_t *rfn;
|
|---|
| 237 | int rc;
|
|---|
| 238 |
|
|---|
| 239 | rc = tmpfs_create_node(&rfn, devmap_handle, L_DIRECTORY);
|
|---|
| 240 | if (rc != EOK || !rfn)
|
|---|
| 241 | return false;
|
|---|
| 242 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
|---|
| 243 | return true;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | static void tmpfs_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 tmpfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
|---|
| 262 | {
|
|---|
| 263 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 264 | link_t *lnk;
|
|---|
| 265 |
|
|---|
| 266 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 267 | lnk = lnk->next) {
|
|---|
| 268 | tmpfs_dentry_t *dentryp;
|
|---|
| 269 | dentryp = list_get_instance(lnk, tmpfs_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 tmpfs_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 | tmpfs_node_t *nodep;
|
|---|
| 289 | nodep = hash_table_get_instance(lnk, tmpfs_node_t, nh_link);
|
|---|
| 290 | *rfn = FS_NODE(nodep);
|
|---|
| 291 | } else {
|
|---|
| 292 | *rfn = NULL;
|
|---|
| 293 | }
|
|---|
| 294 | return EOK;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | int tmpfs_node_open(fs_node_t *fn)
|
|---|
| 298 | {
|
|---|
| 299 | /* nothing to do */
|
|---|
| 300 | return EOK;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | int tmpfs_node_put(fs_node_t *fn)
|
|---|
| 304 | {
|
|---|
| 305 | /* nothing to do */
|
|---|
| 306 | return EOK;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | int tmpfs_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 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
|
|---|
| 317 | if (!nodep)
|
|---|
| 318 | return ENOMEM;
|
|---|
| 319 | tmpfs_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 TMPFS nodes */
|
|---|
| 327 |
|
|---|
| 328 | rc = tmpfs_root_get(&rootfn, devmap_handle);
|
|---|
| 329 | assert(rc == EOK);
|
|---|
| 330 | if (!rootfn)
|
|---|
| 331 | nodep->index = TMPFS_SOME_ROOT;
|
|---|
| 332 | else
|
|---|
| 333 | nodep->index = tmpfs_next_index++;
|
|---|
| 334 | nodep->devmap_handle = devmap_handle;
|
|---|
| 335 | if (lflag & L_DIRECTORY)
|
|---|
| 336 | nodep->type = TMPFS_DIRECTORY;
|
|---|
| 337 | else
|
|---|
| 338 | nodep->type = TMPFS_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 tmpfs_destroy_node(fs_node_t *fn)
|
|---|
| 351 | {
|
|---|
| 352 | tmpfs_node_t *nodep = TMPFS_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 tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 371 | {
|
|---|
| 372 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 373 | tmpfs_node_t *childp = TMPFS_NODE(cfn);
|
|---|
| 374 | tmpfs_dentry_t *dentryp;
|
|---|
| 375 | link_t *lnk;
|
|---|
| 376 |
|
|---|
| 377 | assert(parentp->type == TMPFS_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, tmpfs_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(tmpfs_dentry_t));
|
|---|
| 389 | if (!dentryp)
|
|---|
| 390 | return ENOMEM;
|
|---|
| 391 | tmpfs_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 tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 409 | {
|
|---|
| 410 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 411 | tmpfs_node_t *childp = NULL;
|
|---|
| 412 | tmpfs_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, tmpfs_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 tmpfs_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 = tmpfs_root_get(&rootfn, devmap_handle);
|
|---|
| 457 | if ((rc == EOK) && (rootfn)) {
|
|---|
| 458 | (void) tmpfs_node_put(rootfn);
|
|---|
| 459 | free(opts);
|
|---|
| 460 | async_answer_0(rid, EEXIST);
|
|---|
| 461 | return;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /* Initialize TMPFS instance. */
|
|---|
| 465 | if (!tmpfs_instance_init(devmap_handle)) {
|
|---|
| 466 | free(opts);
|
|---|
| 467 | async_answer_0(rid, ENOMEM);
|
|---|
| 468 | return;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | rc = tmpfs_root_get(&rootfn, devmap_handle);
|
|---|
| 472 | assert(rc == EOK);
|
|---|
| 473 | tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
|
|---|
| 474 | if (str_cmp(opts, "restore") == 0) {
|
|---|
| 475 | if (tmpfs_restore(devmap_handle))
|
|---|
| 476 | async_answer_3(rid, EOK, rootp->index, rootp->size,
|
|---|
| 477 | rootp->lnkcnt);
|
|---|
| 478 | else
|
|---|
| 479 | async_answer_0(rid, ELIMIT);
|
|---|
| 480 | } else {
|
|---|
| 481 | async_answer_3(rid, EOK, rootp->index, rootp->size,
|
|---|
| 482 | rootp->lnkcnt);
|
|---|
| 483 | }
|
|---|
| 484 | free(opts);
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 488 | {
|
|---|
| 489 | libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | void tmpfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 493 | {
|
|---|
| 494 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 495 |
|
|---|
| 496 | tmpfs_instance_done(devmap_handle);
|
|---|
| 497 | async_answer_0(rid, EOK);
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | void tmpfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 501 | {
|
|---|
| 502 | libfs_unmount(&tmpfs_libfs_ops, rid, request);
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 506 | {
|
|---|
| 507 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 511 | {
|
|---|
| 512 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 513 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 514 | aoff64_t pos =
|
|---|
| 515 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
|---|
| 516 |
|
|---|
| 517 | /*
|
|---|
| 518 | * Lookup the respective TMPFS node.
|
|---|
| 519 | */
|
|---|
| 520 | link_t *hlp;
|
|---|
| 521 | unsigned long key[] = {
|
|---|
| 522 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 523 | [NODES_KEY_INDEX] = index
|
|---|
| 524 | };
|
|---|
| 525 | hlp = hash_table_find(&nodes, key);
|
|---|
| 526 | if (!hlp) {
|
|---|
| 527 | async_answer_0(rid, ENOENT);
|
|---|
| 528 | return;
|
|---|
| 529 | }
|
|---|
| 530 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 531 | nh_link);
|
|---|
| 532 |
|
|---|
| 533 | /*
|
|---|
| 534 | * Receive the read request.
|
|---|
| 535 | */
|
|---|
| 536 | ipc_callid_t callid;
|
|---|
| 537 | size_t size;
|
|---|
| 538 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 539 | async_answer_0(callid, EINVAL);
|
|---|
| 540 | async_answer_0(rid, EINVAL);
|
|---|
| 541 | return;
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | size_t bytes;
|
|---|
| 545 | if (nodep->type == TMPFS_FILE) {
|
|---|
| 546 | bytes = min(nodep->size - pos, size);
|
|---|
| 547 | (void) async_data_read_finalize(callid, nodep->data + pos,
|
|---|
| 548 | bytes);
|
|---|
| 549 | } else {
|
|---|
| 550 | tmpfs_dentry_t *dentryp;
|
|---|
| 551 | link_t *lnk;
|
|---|
| 552 | aoff64_t i;
|
|---|
| 553 |
|
|---|
| 554 | assert(nodep->type == TMPFS_DIRECTORY);
|
|---|
| 555 |
|
|---|
| 556 | /*
|
|---|
| 557 | * Yes, we really use O(n) algorithm here.
|
|---|
| 558 | * If it bothers someone, it could be fixed by introducing a
|
|---|
| 559 | * hash table.
|
|---|
| 560 | */
|
|---|
| 561 | for (i = 0, lnk = nodep->cs_head.next;
|
|---|
| 562 | (i < pos) && (lnk != &nodep->cs_head);
|
|---|
| 563 | i++, lnk = lnk->next)
|
|---|
| 564 | ;
|
|---|
| 565 |
|
|---|
| 566 | if (lnk == &nodep->cs_head) {
|
|---|
| 567 | async_answer_0(callid, ENOENT);
|
|---|
| 568 | async_answer_1(rid, ENOENT, 0);
|
|---|
| 569 | return;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 573 |
|
|---|
| 574 | (void) async_data_read_finalize(callid, dentryp->name,
|
|---|
| 575 | str_size(dentryp->name) + 1);
|
|---|
| 576 | bytes = 1;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | /*
|
|---|
| 580 | * Answer the VFS_READ call.
|
|---|
| 581 | */
|
|---|
| 582 | async_answer_1(rid, EOK, bytes);
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 586 | {
|
|---|
| 587 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 588 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 589 | aoff64_t pos =
|
|---|
| 590 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
|---|
| 591 |
|
|---|
| 592 | /*
|
|---|
| 593 | * Lookup the respective TMPFS node.
|
|---|
| 594 | */
|
|---|
| 595 | link_t *hlp;
|
|---|
| 596 | unsigned long key[] = {
|
|---|
| 597 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 598 | [NODES_KEY_INDEX] = index
|
|---|
| 599 | };
|
|---|
| 600 | hlp = hash_table_find(&nodes, key);
|
|---|
| 601 | if (!hlp) {
|
|---|
| 602 | async_answer_0(rid, ENOENT);
|
|---|
| 603 | return;
|
|---|
| 604 | }
|
|---|
| 605 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 606 | nh_link);
|
|---|
| 607 |
|
|---|
| 608 | /*
|
|---|
| 609 | * Receive the write request.
|
|---|
| 610 | */
|
|---|
| 611 | ipc_callid_t callid;
|
|---|
| 612 | size_t size;
|
|---|
| 613 | if (!async_data_write_receive(&callid, &size)) {
|
|---|
| 614 | async_answer_0(callid, EINVAL);
|
|---|
| 615 | async_answer_0(rid, EINVAL);
|
|---|
| 616 | return;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | /*
|
|---|
| 620 | * Check whether the file needs to grow.
|
|---|
| 621 | */
|
|---|
| 622 | if (pos + size <= nodep->size) {
|
|---|
| 623 | /* The file size is not changing. */
|
|---|
| 624 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
|---|
| 625 | async_answer_2(rid, EOK, size, nodep->size);
|
|---|
| 626 | return;
|
|---|
| 627 | }
|
|---|
| 628 | size_t delta = (pos + size) - nodep->size;
|
|---|
| 629 | /*
|
|---|
| 630 | * At this point, we are deliberately extremely straightforward and
|
|---|
| 631 | * simply realloc the contents of the file on every write that grows the
|
|---|
| 632 | * file. In the end, the situation might not be as bad as it may look:
|
|---|
| 633 | * our heap allocator can save us and just grow the block whenever
|
|---|
| 634 | * possible.
|
|---|
| 635 | */
|
|---|
| 636 | void *newdata = realloc(nodep->data, nodep->size + delta);
|
|---|
| 637 | if (!newdata) {
|
|---|
| 638 | async_answer_0(callid, ENOMEM);
|
|---|
| 639 | async_answer_2(rid, EOK, 0, nodep->size);
|
|---|
| 640 | return;
|
|---|
| 641 | }
|
|---|
| 642 | /* Clear any newly allocated memory in order to emulate gaps. */
|
|---|
| 643 | memset(newdata + nodep->size, 0, delta);
|
|---|
| 644 | nodep->size += delta;
|
|---|
| 645 | nodep->data = newdata;
|
|---|
| 646 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
|---|
| 647 | async_answer_2(rid, EOK, size, nodep->size);
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 651 | {
|
|---|
| 652 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 653 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 654 | aoff64_t size =
|
|---|
| 655 | (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
|
|---|
| 656 |
|
|---|
| 657 | /*
|
|---|
| 658 | * Lookup the respective TMPFS node.
|
|---|
| 659 | */
|
|---|
| 660 | unsigned long key[] = {
|
|---|
| 661 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 662 | [NODES_KEY_INDEX] = index
|
|---|
| 663 | };
|
|---|
| 664 | link_t *hlp = hash_table_find(&nodes, key);
|
|---|
| 665 | if (!hlp) {
|
|---|
| 666 | async_answer_0(rid, ENOENT);
|
|---|
| 667 | return;
|
|---|
| 668 | }
|
|---|
| 669 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 670 | nh_link);
|
|---|
| 671 |
|
|---|
| 672 | if (size == nodep->size) {
|
|---|
| 673 | async_answer_0(rid, EOK);
|
|---|
| 674 | return;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | if (size > SIZE_MAX) {
|
|---|
| 678 | async_answer_0(rid, ENOMEM);
|
|---|
| 679 | return;
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | void *newdata = realloc(nodep->data, size);
|
|---|
| 683 | if (!newdata) {
|
|---|
| 684 | async_answer_0(rid, ENOMEM);
|
|---|
| 685 | return;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | if (size > nodep->size) {
|
|---|
| 689 | size_t delta = size - nodep->size;
|
|---|
| 690 | memset(newdata + nodep->size, 0, delta);
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | nodep->size = size;
|
|---|
| 694 | nodep->data = newdata;
|
|---|
| 695 | async_answer_0(rid, EOK);
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 | void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 699 | {
|
|---|
| 700 | async_answer_0(rid, EOK);
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 704 | {
|
|---|
| 705 | devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 706 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 707 | int rc;
|
|---|
| 708 |
|
|---|
| 709 | link_t *hlp;
|
|---|
| 710 | unsigned long key[] = {
|
|---|
| 711 | [NODES_KEY_DEV] = devmap_handle,
|
|---|
| 712 | [NODES_KEY_INDEX] = index
|
|---|
| 713 | };
|
|---|
| 714 | hlp = hash_table_find(&nodes, key);
|
|---|
| 715 | if (!hlp) {
|
|---|
| 716 | async_answer_0(rid, ENOENT);
|
|---|
| 717 | return;
|
|---|
| 718 | }
|
|---|
| 719 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 720 | nh_link);
|
|---|
| 721 | rc = tmpfs_destroy_node(FS_NODE(nodep));
|
|---|
| 722 | async_answer_0(rid, rc);
|
|---|
| 723 | }
|
|---|
| 724 |
|
|---|
| 725 | void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 726 | {
|
|---|
| 727 | libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 731 | {
|
|---|
| 732 | libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 736 | {
|
|---|
| 737 | /*
|
|---|
| 738 | * TMPFS keeps its data structures always consistent,
|
|---|
| 739 | * thus the sync operation is a no-op.
|
|---|
| 740 | */
|
|---|
| 741 | async_answer_0(rid, EOK);
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | /**
|
|---|
| 745 | * @}
|
|---|
| 746 | */
|
|---|