| 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 <ipc/ipc.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <atomic.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include <string.h>
|
|---|
| 47 | #include <stdio.h>
|
|---|
| 48 | #include <assert.h>
|
|---|
| 49 | #include <sys/types.h>
|
|---|
| 50 | #include <adt/hash_table.h>
|
|---|
| 51 | #include <as.h>
|
|---|
| 52 | #include <libfs.h>
|
|---|
| 53 |
|
|---|
| 54 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
|---|
| 55 | #define max(a, b) ((a) > (b) ? (a) : (b))
|
|---|
| 56 |
|
|---|
| 57 | #define NODES_BUCKETS 256
|
|---|
| 58 |
|
|---|
| 59 | /** All root nodes have index 0. */
|
|---|
| 60 | #define TMPFS_SOME_ROOT 0
|
|---|
| 61 | /** Global counter for assigning node indices. Shared by all instances. */
|
|---|
| 62 | fs_index_t tmpfs_next_index = 1;
|
|---|
| 63 |
|
|---|
| 64 | /*
|
|---|
| 65 | * Implementation of the libfs interface.
|
|---|
| 66 | */
|
|---|
| 67 |
|
|---|
| 68 | /* Forward declarations of static functions. */
|
|---|
| 69 | static int tmpfs_match(fs_node_t **, fs_node_t *, const char *);
|
|---|
| 70 | static int tmpfs_node_get(fs_node_t **, dev_handle_t, fs_index_t);
|
|---|
| 71 | static int tmpfs_node_open(fs_node_t *);
|
|---|
| 72 | static int tmpfs_node_put(fs_node_t *);
|
|---|
| 73 | static int tmpfs_create_node(fs_node_t **, dev_handle_t, int);
|
|---|
| 74 | static int tmpfs_destroy_node(fs_node_t *);
|
|---|
| 75 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
|
|---|
| 76 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *);
|
|---|
| 77 |
|
|---|
| 78 | /* Implementation of helper functions. */
|
|---|
| 79 | static int tmpfs_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
|
|---|
| 80 | {
|
|---|
| 81 | return tmpfs_node_get(rfn, dev_handle, TMPFS_SOME_ROOT);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | static int tmpfs_has_children(bool *has_children, fs_node_t *fn)
|
|---|
| 85 | {
|
|---|
| 86 | *has_children = !list_empty(&TMPFS_NODE(fn)->cs_head);
|
|---|
| 87 | return EOK;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | static fs_index_t tmpfs_index_get(fs_node_t *fn)
|
|---|
| 91 | {
|
|---|
| 92 | return TMPFS_NODE(fn)->index;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | static size_t tmpfs_size_get(fs_node_t *fn)
|
|---|
| 96 | {
|
|---|
| 97 | return TMPFS_NODE(fn)->size;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn)
|
|---|
| 101 | {
|
|---|
| 102 | return TMPFS_NODE(fn)->lnkcnt;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | static char tmpfs_plb_get_char(unsigned pos)
|
|---|
| 106 | {
|
|---|
| 107 | return tmpfs_reg.plb_ro[pos % PLB_SIZE];
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | static bool tmpfs_is_directory(fs_node_t *fn)
|
|---|
| 111 | {
|
|---|
| 112 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static bool tmpfs_is_file(fs_node_t *fn)
|
|---|
| 116 | {
|
|---|
| 117 | return TMPFS_NODE(fn)->type == TMPFS_FILE;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | static dev_handle_t tmpfs_device_get(fs_node_t *fn)
|
|---|
| 121 | {
|
|---|
| 122 | return 0;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** libfs operations */
|
|---|
| 126 | libfs_ops_t tmpfs_libfs_ops = {
|
|---|
| 127 | .root_get = tmpfs_root_get,
|
|---|
| 128 | .match = tmpfs_match,
|
|---|
| 129 | .node_get = tmpfs_node_get,
|
|---|
| 130 | .node_open = tmpfs_node_open,
|
|---|
| 131 | .node_put = tmpfs_node_put,
|
|---|
| 132 | .create = tmpfs_create_node,
|
|---|
| 133 | .destroy = tmpfs_destroy_node,
|
|---|
| 134 | .link = tmpfs_link_node,
|
|---|
| 135 | .unlink = tmpfs_unlink_node,
|
|---|
| 136 | .has_children = tmpfs_has_children,
|
|---|
| 137 | .index_get = tmpfs_index_get,
|
|---|
| 138 | .size_get = tmpfs_size_get,
|
|---|
| 139 | .lnkcnt_get = tmpfs_lnkcnt_get,
|
|---|
| 140 | .plb_get_char = tmpfs_plb_get_char,
|
|---|
| 141 | .is_directory = tmpfs_is_directory,
|
|---|
| 142 | .is_file = tmpfs_is_file,
|
|---|
| 143 | .device_get = tmpfs_device_get
|
|---|
| 144 | };
|
|---|
| 145 |
|
|---|
| 146 | /** Hash table of all TMPFS nodes. */
|
|---|
| 147 | hash_table_t nodes;
|
|---|
| 148 |
|
|---|
| 149 | #define NODES_KEY_DEV 0
|
|---|
| 150 | #define NODES_KEY_INDEX 1
|
|---|
| 151 |
|
|---|
| 152 | /* Implementation of hash table interface for the nodes hash table. */
|
|---|
| 153 | static hash_index_t nodes_hash(unsigned long key[])
|
|---|
| 154 | {
|
|---|
| 155 | return key[NODES_KEY_INDEX] % NODES_BUCKETS;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 159 | {
|
|---|
| 160 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
|---|
| 161 | nh_link);
|
|---|
| 162 |
|
|---|
| 163 | switch (keys) {
|
|---|
| 164 | case 1:
|
|---|
| 165 | return (nodep->dev_handle == key[NODES_KEY_DEV]);
|
|---|
| 166 | case 2:
|
|---|
| 167 | return ((nodep->dev_handle == key[NODES_KEY_DEV]) &&
|
|---|
| 168 | (nodep->index == key[NODES_KEY_INDEX]));
|
|---|
| 169 | default:
|
|---|
| 170 | abort();
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | static void nodes_remove_callback(link_t *item)
|
|---|
| 175 | {
|
|---|
| 176 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
|---|
| 177 | nh_link);
|
|---|
| 178 |
|
|---|
| 179 | while (!list_empty(&nodep->cs_head)) {
|
|---|
| 180 | tmpfs_dentry_t *dentryp = list_get_instance(nodep->cs_head.next,
|
|---|
| 181 | tmpfs_dentry_t, link);
|
|---|
| 182 |
|
|---|
| 183 | assert(nodep->type == TMPFS_DIRECTORY);
|
|---|
| 184 | list_remove(&dentryp->link);
|
|---|
| 185 | free(dentryp);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | if (nodep->data) {
|
|---|
| 189 | assert(nodep->type == TMPFS_FILE);
|
|---|
| 190 | free(nodep->data);
|
|---|
| 191 | }
|
|---|
| 192 | free(nodep->bp);
|
|---|
| 193 | free(nodep);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** TMPFS nodes hash table operations. */
|
|---|
| 197 | hash_table_operations_t nodes_ops = {
|
|---|
| 198 | .hash = nodes_hash,
|
|---|
| 199 | .compare = nodes_compare,
|
|---|
| 200 | .remove_callback = nodes_remove_callback
|
|---|
| 201 | };
|
|---|
| 202 |
|
|---|
| 203 | static void tmpfs_node_initialize(tmpfs_node_t *nodep)
|
|---|
| 204 | {
|
|---|
| 205 | nodep->bp = NULL;
|
|---|
| 206 | nodep->index = 0;
|
|---|
| 207 | nodep->dev_handle = 0;
|
|---|
| 208 | nodep->type = TMPFS_NONE;
|
|---|
| 209 | nodep->lnkcnt = 0;
|
|---|
| 210 | nodep->size = 0;
|
|---|
| 211 | nodep->data = NULL;
|
|---|
| 212 | link_initialize(&nodep->nh_link);
|
|---|
| 213 | list_initialize(&nodep->cs_head);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
|
|---|
| 217 | {
|
|---|
| 218 | link_initialize(&dentryp->link);
|
|---|
| 219 | dentryp->name = NULL;
|
|---|
| 220 | dentryp->node = NULL;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | bool tmpfs_init(void)
|
|---|
| 224 | {
|
|---|
| 225 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
|
|---|
| 226 | return false;
|
|---|
| 227 |
|
|---|
| 228 | return true;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | static bool tmpfs_instance_init(dev_handle_t dev_handle)
|
|---|
| 232 | {
|
|---|
| 233 | fs_node_t *rfn;
|
|---|
| 234 | int rc;
|
|---|
| 235 |
|
|---|
| 236 | rc = tmpfs_create_node(&rfn, dev_handle, L_DIRECTORY);
|
|---|
| 237 | if (rc != EOK || !rfn)
|
|---|
| 238 | return false;
|
|---|
| 239 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
|---|
| 240 | return true;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | static void tmpfs_instance_done(dev_handle_t dev_handle)
|
|---|
| 244 | {
|
|---|
| 245 | unsigned long key[] = {
|
|---|
| 246 | [NODES_KEY_DEV] = dev_handle
|
|---|
| 247 | };
|
|---|
| 248 | /*
|
|---|
| 249 | * Here we are making use of one special feature of our hash table
|
|---|
| 250 | * implementation, which allows to remove more items based on a partial
|
|---|
| 251 | * key match. In the following, we are going to remove all nodes
|
|---|
| 252 | * matching our device handle. The nodes_remove_callback() function will
|
|---|
| 253 | * take care of resource deallocation.
|
|---|
| 254 | */
|
|---|
| 255 | hash_table_remove(&nodes, key, 1);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | int tmpfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
|---|
| 259 | {
|
|---|
| 260 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 261 | link_t *lnk;
|
|---|
| 262 |
|
|---|
| 263 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 264 | lnk = lnk->next) {
|
|---|
| 265 | tmpfs_dentry_t *dentryp;
|
|---|
| 266 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 267 | if (!str_cmp(dentryp->name, component)) {
|
|---|
| 268 | *rfn = FS_NODE(dentryp->node);
|
|---|
| 269 | return EOK;
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | *rfn = NULL;
|
|---|
| 274 | return EOK;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | int tmpfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
|
|---|
| 278 | {
|
|---|
| 279 | unsigned long key[] = {
|
|---|
| 280 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 281 | [NODES_KEY_INDEX] = index
|
|---|
| 282 | };
|
|---|
| 283 | link_t *lnk = hash_table_find(&nodes, key);
|
|---|
| 284 | if (lnk) {
|
|---|
| 285 | tmpfs_node_t *nodep;
|
|---|
| 286 | nodep = hash_table_get_instance(lnk, tmpfs_node_t, nh_link);
|
|---|
| 287 | *rfn = FS_NODE(nodep);
|
|---|
| 288 | } else {
|
|---|
| 289 | *rfn = NULL;
|
|---|
| 290 | }
|
|---|
| 291 | return EOK;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | int tmpfs_node_open(fs_node_t *fn)
|
|---|
| 295 | {
|
|---|
| 296 | /* nothing to do */
|
|---|
| 297 | return EOK;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | int tmpfs_node_put(fs_node_t *fn)
|
|---|
| 301 | {
|
|---|
| 302 | /* nothing to do */
|
|---|
| 303 | return EOK;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | int tmpfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
|
|---|
| 307 | {
|
|---|
| 308 | fs_node_t *rootfn;
|
|---|
| 309 | int rc;
|
|---|
| 310 |
|
|---|
| 311 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
|---|
| 312 |
|
|---|
| 313 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
|
|---|
| 314 | if (!nodep)
|
|---|
| 315 | return ENOMEM;
|
|---|
| 316 | tmpfs_node_initialize(nodep);
|
|---|
| 317 | nodep->bp = malloc(sizeof(fs_node_t));
|
|---|
| 318 | if (!nodep->bp) {
|
|---|
| 319 | free(nodep);
|
|---|
| 320 | return ENOMEM;
|
|---|
| 321 | }
|
|---|
| 322 | fs_node_initialize(nodep->bp);
|
|---|
| 323 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
|
|---|
| 324 |
|
|---|
| 325 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
|---|
| 326 | assert(rc == EOK);
|
|---|
| 327 | if (!rootfn)
|
|---|
| 328 | nodep->index = TMPFS_SOME_ROOT;
|
|---|
| 329 | else
|
|---|
| 330 | nodep->index = tmpfs_next_index++;
|
|---|
| 331 | nodep->dev_handle = dev_handle;
|
|---|
| 332 | if (lflag & L_DIRECTORY)
|
|---|
| 333 | nodep->type = TMPFS_DIRECTORY;
|
|---|
| 334 | else
|
|---|
| 335 | nodep->type = TMPFS_FILE;
|
|---|
| 336 |
|
|---|
| 337 | /* Insert the new node into the nodes hash table. */
|
|---|
| 338 | unsigned long key[] = {
|
|---|
| 339 | [NODES_KEY_DEV] = nodep->dev_handle,
|
|---|
| 340 | [NODES_KEY_INDEX] = nodep->index
|
|---|
| 341 | };
|
|---|
| 342 | hash_table_insert(&nodes, key, &nodep->nh_link);
|
|---|
| 343 | *rfn = FS_NODE(nodep);
|
|---|
| 344 | return EOK;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | int tmpfs_destroy_node(fs_node_t *fn)
|
|---|
| 348 | {
|
|---|
| 349 | tmpfs_node_t *nodep = TMPFS_NODE(fn);
|
|---|
| 350 |
|
|---|
| 351 | assert(!nodep->lnkcnt);
|
|---|
| 352 | assert(list_empty(&nodep->cs_head));
|
|---|
| 353 |
|
|---|
| 354 | unsigned long key[] = {
|
|---|
| 355 | [NODES_KEY_DEV] = nodep->dev_handle,
|
|---|
| 356 | [NODES_KEY_INDEX] = nodep->index
|
|---|
| 357 | };
|
|---|
| 358 | hash_table_remove(&nodes, key, 2);
|
|---|
| 359 |
|
|---|
| 360 | /*
|
|---|
| 361 | * The nodes_remove_callback() function takes care of the actual
|
|---|
| 362 | * resource deallocation.
|
|---|
| 363 | */
|
|---|
| 364 | return EOK;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 368 | {
|
|---|
| 369 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 370 | tmpfs_node_t *childp = TMPFS_NODE(cfn);
|
|---|
| 371 | tmpfs_dentry_t *dentryp;
|
|---|
| 372 | link_t *lnk;
|
|---|
| 373 |
|
|---|
| 374 | assert(parentp->type == TMPFS_DIRECTORY);
|
|---|
| 375 |
|
|---|
| 376 | /* Check for duplicit entries. */
|
|---|
| 377 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 378 | lnk = lnk->next) {
|
|---|
| 379 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 380 | if (!str_cmp(dentryp->name, nm))
|
|---|
| 381 | return EEXIST;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /* Allocate and initialize the dentry. */
|
|---|
| 385 | dentryp = malloc(sizeof(tmpfs_dentry_t));
|
|---|
| 386 | if (!dentryp)
|
|---|
| 387 | return ENOMEM;
|
|---|
| 388 | tmpfs_dentry_initialize(dentryp);
|
|---|
| 389 |
|
|---|
| 390 | /* Populate and link the new dentry. */
|
|---|
| 391 | size_t size = str_size(nm);
|
|---|
| 392 | dentryp->name = malloc(size + 1);
|
|---|
| 393 | if (!dentryp->name) {
|
|---|
| 394 | free(dentryp);
|
|---|
| 395 | return ENOMEM;
|
|---|
| 396 | }
|
|---|
| 397 | str_cpy(dentryp->name, size + 1, nm);
|
|---|
| 398 | dentryp->node = childp;
|
|---|
| 399 | childp->lnkcnt++;
|
|---|
| 400 | list_append(&dentryp->link, &parentp->cs_head);
|
|---|
| 401 |
|
|---|
| 402 | return EOK;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 406 | {
|
|---|
| 407 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 408 | tmpfs_node_t *childp = NULL;
|
|---|
| 409 | tmpfs_dentry_t *dentryp;
|
|---|
| 410 | link_t *lnk;
|
|---|
| 411 |
|
|---|
| 412 | if (!parentp)
|
|---|
| 413 | return EBUSY;
|
|---|
| 414 |
|
|---|
| 415 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 416 | lnk = lnk->next) {
|
|---|
| 417 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 418 | if (!str_cmp(dentryp->name, nm)) {
|
|---|
| 419 | childp = dentryp->node;
|
|---|
| 420 | assert(FS_NODE(childp) == cfn);
|
|---|
| 421 | break;
|
|---|
| 422 | }
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | if (!childp)
|
|---|
| 426 | return ENOENT;
|
|---|
| 427 |
|
|---|
| 428 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
|
|---|
| 429 | return ENOTEMPTY;
|
|---|
| 430 |
|
|---|
| 431 | list_remove(&dentryp->link);
|
|---|
| 432 | free(dentryp);
|
|---|
| 433 | childp->lnkcnt--;
|
|---|
| 434 |
|
|---|
| 435 | return EOK;
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 439 | {
|
|---|
| 440 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 441 | int rc;
|
|---|
| 442 |
|
|---|
| 443 | /* accept the mount options */
|
|---|
| 444 | ipc_callid_t callid;
|
|---|
| 445 | size_t size;
|
|---|
| 446 | if (!async_data_write_receive(&callid, &size)) {
|
|---|
| 447 | ipc_answer_0(callid, EINVAL);
|
|---|
| 448 | ipc_answer_0(rid, EINVAL);
|
|---|
| 449 | return;
|
|---|
| 450 | }
|
|---|
| 451 | char *opts = malloc(size + 1);
|
|---|
| 452 | if (!opts) {
|
|---|
| 453 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 454 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 455 | return;
|
|---|
| 456 | }
|
|---|
| 457 | ipcarg_t retval = async_data_write_finalize(callid, opts, size);
|
|---|
| 458 | if (retval != EOK) {
|
|---|
| 459 | ipc_answer_0(rid, retval);
|
|---|
| 460 | free(opts);
|
|---|
| 461 | return;
|
|---|
| 462 | }
|
|---|
| 463 | opts[size] = '\0';
|
|---|
| 464 |
|
|---|
| 465 | /* Initialize TMPFS instance. */
|
|---|
| 466 | if (!tmpfs_instance_init(dev_handle)) {
|
|---|
| 467 | free(opts);
|
|---|
| 468 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 469 | return;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | fs_node_t *rootfn;
|
|---|
| 473 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
|---|
| 474 | assert(rc == EOK);
|
|---|
| 475 | tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
|
|---|
| 476 | if (str_cmp(opts, "restore") == 0) {
|
|---|
| 477 | if (tmpfs_restore(dev_handle))
|
|---|
| 478 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
|---|
| 479 | rootp->lnkcnt);
|
|---|
| 480 | else
|
|---|
| 481 | ipc_answer_0(rid, ELIMIT);
|
|---|
| 482 | } else {
|
|---|
| 483 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
|---|
| 484 | rootp->lnkcnt);
|
|---|
| 485 | }
|
|---|
| 486 | free(opts);
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 490 | {
|
|---|
| 491 | libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | void tmpfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 495 | {
|
|---|
| 496 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 497 |
|
|---|
| 498 | tmpfs_instance_done(dev_handle);
|
|---|
| 499 | ipc_answer_0(rid, EOK);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | void tmpfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 503 | {
|
|---|
| 504 | libfs_unmount(&tmpfs_libfs_ops, rid, request);
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 508 | {
|
|---|
| 509 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 513 | {
|
|---|
| 514 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 515 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 516 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
|---|
| 517 |
|
|---|
| 518 | /*
|
|---|
| 519 | * Lookup the respective TMPFS node.
|
|---|
| 520 | */
|
|---|
| 521 | link_t *hlp;
|
|---|
| 522 | unsigned long key[] = {
|
|---|
| 523 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 524 | [NODES_KEY_INDEX] = index
|
|---|
| 525 | };
|
|---|
| 526 | hlp = hash_table_find(&nodes, key);
|
|---|
| 527 | if (!hlp) {
|
|---|
| 528 | ipc_answer_0(rid, ENOENT);
|
|---|
| 529 | return;
|
|---|
| 530 | }
|
|---|
| 531 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 532 | nh_link);
|
|---|
| 533 |
|
|---|
| 534 | /*
|
|---|
| 535 | * Receive the read request.
|
|---|
| 536 | */
|
|---|
| 537 | ipc_callid_t callid;
|
|---|
| 538 | size_t size;
|
|---|
| 539 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 540 | ipc_answer_0(callid, EINVAL);
|
|---|
| 541 | ipc_answer_0(rid, EINVAL);
|
|---|
| 542 | return;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | size_t bytes;
|
|---|
| 546 | if (nodep->type == TMPFS_FILE) {
|
|---|
| 547 | bytes = max(0, min(nodep->size - pos, size));
|
|---|
| 548 | (void) async_data_read_finalize(callid, nodep->data + pos,
|
|---|
| 549 | bytes);
|
|---|
| 550 | } else {
|
|---|
| 551 | tmpfs_dentry_t *dentryp;
|
|---|
| 552 | link_t *lnk;
|
|---|
| 553 | int i;
|
|---|
| 554 |
|
|---|
| 555 | assert(nodep->type == TMPFS_DIRECTORY);
|
|---|
| 556 |
|
|---|
| 557 | /*
|
|---|
| 558 | * Yes, we really use O(n) algorithm here.
|
|---|
| 559 | * If it bothers someone, it could be fixed by introducing a
|
|---|
| 560 | * hash table.
|
|---|
| 561 | */
|
|---|
| 562 | for (i = 0, lnk = nodep->cs_head.next;
|
|---|
| 563 | i < pos && lnk != &nodep->cs_head;
|
|---|
| 564 | i++, lnk = lnk->next)
|
|---|
| 565 | ;
|
|---|
| 566 |
|
|---|
| 567 | if (lnk == &nodep->cs_head) {
|
|---|
| 568 | ipc_answer_0(callid, ENOENT);
|
|---|
| 569 | ipc_answer_1(rid, ENOENT, 0);
|
|---|
| 570 | return;
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 574 |
|
|---|
| 575 | (void) async_data_read_finalize(callid, dentryp->name,
|
|---|
| 576 | str_size(dentryp->name) + 1);
|
|---|
| 577 | bytes = 1;
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | /*
|
|---|
| 581 | * Answer the VFS_READ call.
|
|---|
| 582 | */
|
|---|
| 583 | ipc_answer_1(rid, EOK, bytes);
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 587 | {
|
|---|
| 588 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 589 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 590 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
|---|
| 591 |
|
|---|
| 592 | /*
|
|---|
| 593 | * Lookup the respective TMPFS node.
|
|---|
| 594 | */
|
|---|
| 595 | link_t *hlp;
|
|---|
| 596 | unsigned long key[] = {
|
|---|
| 597 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 598 | [NODES_KEY_INDEX] = index
|
|---|
| 599 | };
|
|---|
| 600 | hlp = hash_table_find(&nodes, key);
|
|---|
| 601 | if (!hlp) {
|
|---|
| 602 | ipc_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 | ipc_answer_0(callid, EINVAL);
|
|---|
| 615 | ipc_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 | ipc_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 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 639 | ipc_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 | ipc_answer_2(rid, EOK, size, nodep->size);
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 651 | {
|
|---|
| 652 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 653 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 654 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
|---|
| 655 |
|
|---|
| 656 | /*
|
|---|
| 657 | * Lookup the respective TMPFS node.
|
|---|
| 658 | */
|
|---|
| 659 | link_t *hlp;
|
|---|
| 660 | unsigned long key[] = {
|
|---|
| 661 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 662 | [NODES_KEY_INDEX] = index
|
|---|
| 663 | };
|
|---|
| 664 | hlp = hash_table_find(&nodes, key);
|
|---|
| 665 | if (!hlp) {
|
|---|
| 666 | ipc_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 | ipc_answer_0(rid, EOK);
|
|---|
| 674 | return;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | void *newdata = realloc(nodep->data, size);
|
|---|
| 678 | if (!newdata) {
|
|---|
| 679 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 680 | return;
|
|---|
| 681 | }
|
|---|
| 682 | if (size > nodep->size) {
|
|---|
| 683 | size_t delta = size - nodep->size;
|
|---|
| 684 | memset(newdata + nodep->size, 0, delta);
|
|---|
| 685 | }
|
|---|
| 686 | nodep->size = size;
|
|---|
| 687 | nodep->data = newdata;
|
|---|
| 688 | ipc_answer_0(rid, EOK);
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 692 | {
|
|---|
| 693 | ipc_answer_0(rid, EOK);
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 697 | {
|
|---|
| 698 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 699 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 700 | int rc;
|
|---|
| 701 |
|
|---|
| 702 | link_t *hlp;
|
|---|
| 703 | unsigned long key[] = {
|
|---|
| 704 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 705 | [NODES_KEY_INDEX] = index
|
|---|
| 706 | };
|
|---|
| 707 | hlp = hash_table_find(&nodes, key);
|
|---|
| 708 | if (!hlp) {
|
|---|
| 709 | ipc_answer_0(rid, ENOENT);
|
|---|
| 710 | return;
|
|---|
| 711 | }
|
|---|
| 712 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 713 | nh_link);
|
|---|
| 714 | rc = tmpfs_destroy_node(FS_NODE(nodep));
|
|---|
| 715 | ipc_answer_0(rid, rc);
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 719 | {
|
|---|
| 720 | libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 724 | {
|
|---|
| 725 | libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 729 | {
|
|---|
| 730 | /* Dummy implementation */
|
|---|
| 731 | ipc_answer_0(rid, EOK);
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | /**
|
|---|
| 735 | * @}
|
|---|
| 736 | */
|
|---|