[d5cdffe] | 1 | /*
|
---|
[41a0d27] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[d5cdffe] | 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>
|
---|
[4b11571] | 44 | #include <atomic.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 | #include <string.h>
|
---|
| 47 | #include <stdio.h>
|
---|
[5973fd0] | 48 | #include <assert.h>
|
---|
[a4eb8a60] | 49 | #include <sys/types.h>
|
---|
| 50 | #include <libadt/hash_table.h>
|
---|
| 51 | #include <as.h>
|
---|
[2c448fb] | 52 | #include <libfs.h>
|
---|
[a4eb8a60] | 53 |
|
---|
| 54 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 55 | #define max(a, b) ((a) > (b) ? (a) : (b))
|
---|
[d5cdffe] | 56 |
|
---|
[a4eb8a60] | 57 | #define DENTRIES_BUCKETS 256
|
---|
| 58 |
|
---|
[3298ddc] | 59 | #define NAMES_BUCKETS 4
|
---|
| 60 |
|
---|
[2c448fb] | 61 | /*
|
---|
| 62 | * For now, we don't distinguish between different dev_handles/instances. All
|
---|
| 63 | * requests resolve to the only instance, rooted in the following variable.
|
---|
| 64 | */
|
---|
| 65 | static tmpfs_dentry_t *root;
|
---|
| 66 |
|
---|
[adb5fe3] | 67 | #define TMPFS_DEV 0 /**< Dummy device handle for TMPFS */
|
---|
| 68 |
|
---|
[2c448fb] | 69 | /*
|
---|
| 70 | * Implementation of the libfs interface.
|
---|
| 71 | */
|
---|
[b5553a2] | 72 |
|
---|
[fdb7795] | 73 | /* Forward declarations of static functions. */
|
---|
[736c164] | 74 | static void *tmpfs_match(void *, const char *);
|
---|
[34f62f8] | 75 | static void *tmpfs_node_get(dev_handle_t, fs_index_t);
|
---|
[06901c6b] | 76 | static void tmpfs_node_put(void *);
|
---|
[adb5fe3] | 77 | static void *tmpfs_create_node(dev_handle_t, int);
|
---|
[0013b9ce] | 78 | static int tmpfs_link_node(void *, void *, const char *);
|
---|
[7b6d98b] | 79 | static int tmpfs_unlink_node(void *, void *);
|
---|
[45f244b] | 80 | static int tmpfs_destroy_node(void *);
|
---|
[2c448fb] | 81 |
|
---|
| 82 | /* Implementation of helper functions. */
|
---|
[f2ec8c8] | 83 | static fs_index_t tmpfs_index_get(void *nodep)
|
---|
[2c448fb] | 84 | {
|
---|
| 85 | return ((tmpfs_dentry_t *) nodep)->index;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[f2ec8c8] | 88 | static size_t tmpfs_size_get(void *nodep)
|
---|
[2c448fb] | 89 | {
|
---|
| 90 | return ((tmpfs_dentry_t *) nodep)->size;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | static unsigned tmpfs_lnkcnt_get(void *nodep)
|
---|
| 94 | {
|
---|
[adc8a63] | 95 | return ((tmpfs_dentry_t *) nodep)->lnkcnt;
|
---|
[2c448fb] | 96 | }
|
---|
| 97 |
|
---|
[736c164] | 98 | static bool tmpfs_has_children(void *nodep)
|
---|
[2c448fb] | 99 | {
|
---|
[736c164] | 100 | return ((tmpfs_dentry_t *) nodep)->child != NULL;
|
---|
[2c448fb] | 101 | }
|
---|
| 102 |
|
---|
[74ea3c6] | 103 | static void *tmpfs_root_get(dev_handle_t dev_handle)
|
---|
[2c448fb] | 104 | {
|
---|
| 105 | return root;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | static char tmpfs_plb_get_char(unsigned pos)
|
---|
| 109 | {
|
---|
| 110 | return tmpfs_reg.plb_ro[pos % PLB_SIZE];
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static bool tmpfs_is_directory(void *nodep)
|
---|
| 114 | {
|
---|
| 115 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | static bool tmpfs_is_file(void *nodep)
|
---|
| 119 | {
|
---|
| 120 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /** libfs operations */
|
---|
| 124 | libfs_ops_t tmpfs_libfs_ops = {
|
---|
| 125 | .match = tmpfs_match,
|
---|
[a8e9ab8d] | 126 | .node_get = tmpfs_node_get,
|
---|
[06901c6b] | 127 | .node_put = tmpfs_node_put,
|
---|
[2c448fb] | 128 | .create = tmpfs_create_node,
|
---|
| 129 | .destroy = tmpfs_destroy_node,
|
---|
| 130 | .link = tmpfs_link_node,
|
---|
| 131 | .unlink = tmpfs_unlink_node,
|
---|
| 132 | .index_get = tmpfs_index_get,
|
---|
| 133 | .size_get = tmpfs_size_get,
|
---|
| 134 | .lnkcnt_get = tmpfs_lnkcnt_get,
|
---|
[736c164] | 135 | .has_children = tmpfs_has_children,
|
---|
[2c448fb] | 136 | .root_get = tmpfs_root_get,
|
---|
| 137 | .plb_get_char = tmpfs_plb_get_char,
|
---|
| 138 | .is_directory = tmpfs_is_directory,
|
---|
| 139 | .is_file = tmpfs_is_file
|
---|
| 140 | };
|
---|
[fdb7795] | 141 |
|
---|
| 142 | /** Hash table of all directory entries. */
|
---|
[a4eb8a60] | 143 | hash_table_t dentries;
|
---|
| 144 |
|
---|
[3298ddc] | 145 | /* Implementation of hash table interface for the dentries hash table. */
|
---|
[a4eb8a60] | 146 | static hash_index_t dentries_hash(unsigned long *key)
|
---|
| 147 | {
|
---|
| 148 | return *key % DENTRIES_BUCKETS;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | static int dentries_compare(unsigned long *key, hash_count_t keys,
|
---|
| 152 | link_t *item)
|
---|
| 153 | {
|
---|
| 154 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
|
---|
| 155 | dh_link);
|
---|
| 156 | return dentry->index == *key;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | static void dentries_remove_callback(link_t *item)
|
---|
| 160 | {
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /** TMPFS dentries hash table operations. */
|
---|
| 164 | hash_table_operations_t dentries_ops = {
|
---|
| 165 | .hash = dentries_hash,
|
---|
| 166 | .compare = dentries_compare,
|
---|
| 167 | .remove_callback = dentries_remove_callback
|
---|
| 168 | };
|
---|
| 169 |
|
---|
[f2ec8c8] | 170 | fs_index_t tmpfs_next_index = 1;
|
---|
[4b11571] | 171 |
|
---|
[3298ddc] | 172 | typedef struct {
|
---|
| 173 | char *name;
|
---|
| 174 | tmpfs_dentry_t *parent;
|
---|
| 175 | link_t link;
|
---|
| 176 | } tmpfs_name_t;
|
---|
| 177 |
|
---|
| 178 | /* Implementation of hash table interface for the names hash table. */
|
---|
| 179 | static hash_index_t names_hash(unsigned long *key)
|
---|
| 180 | {
|
---|
| 181 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
|
---|
| 182 | return dentry->index % NAMES_BUCKETS;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
|
---|
| 186 | {
|
---|
| 187 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
|
---|
| 188 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
|
---|
| 189 | link);
|
---|
| 190 | return dentry == namep->parent;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | static void names_remove_callback(link_t *item)
|
---|
| 194 | {
|
---|
| 195 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
|
---|
| 196 | link);
|
---|
| 197 | free(namep->name);
|
---|
| 198 | free(namep);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** TMPFS node names hash table operations. */
|
---|
| 202 | static hash_table_operations_t names_ops = {
|
---|
| 203 | .hash = names_hash,
|
---|
| 204 | .compare = names_compare,
|
---|
| 205 | .remove_callback = names_remove_callback
|
---|
| 206 | };
|
---|
| 207 |
|
---|
| 208 | static void tmpfs_name_initialize(tmpfs_name_t *namep)
|
---|
| 209 | {
|
---|
| 210 | namep->name = NULL;
|
---|
| 211 | namep->parent = NULL;
|
---|
| 212 | link_initialize(&namep->link);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
|
---|
[4b11571] | 216 | {
|
---|
| 217 | dentry->index = 0;
|
---|
| 218 | dentry->sibling = NULL;
|
---|
| 219 | dentry->child = NULL;
|
---|
| 220 | dentry->type = TMPFS_NONE;
|
---|
[adc8a63] | 221 | dentry->lnkcnt = 0;
|
---|
[4b11571] | 222 | dentry->size = 0;
|
---|
| 223 | dentry->data = NULL;
|
---|
[a4eb8a60] | 224 | link_initialize(&dentry->dh_link);
|
---|
[3298ddc] | 225 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
|
---|
| 226 | &names_ops);
|
---|
[4b11571] | 227 | }
|
---|
| 228 |
|
---|
| 229 | static bool tmpfs_init(void)
|
---|
| 230 | {
|
---|
[a4eb8a60] | 231 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
|
---|
| 232 | return false;
|
---|
[adb5fe3] | 233 | root = (tmpfs_dentry_t *) tmpfs_create_node(TMPFS_DEV, L_DIRECTORY);
|
---|
[3298ddc] | 234 | if (!root) {
|
---|
| 235 | hash_table_destroy(&dentries);
|
---|
| 236 | return false;
|
---|
| 237 | }
|
---|
[5ab597d] | 238 | root->lnkcnt = 0; /* FS root is not linked */
|
---|
[3298ddc] | 239 | return true;
|
---|
[4b11571] | 240 | }
|
---|
| 241 |
|
---|
[d5cdffe] | 242 | /** Compare one component of path to a directory entry.
|
---|
| 243 | *
|
---|
[736c164] | 244 | * @param parentp Pointer to node from which we descended.
|
---|
| 245 | * @param childp Pointer to node to compare the path component with.
|
---|
[b8b23c8] | 246 | * @param component Array of characters holding component name.
|
---|
[d5cdffe] | 247 | *
|
---|
[b8b23c8] | 248 | * @return True on match, false otherwise.
|
---|
[d5cdffe] | 249 | */
|
---|
[736c164] | 250 | static bool
|
---|
| 251 | tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
|
---|
| 252 | const char *component)
|
---|
[d5cdffe] | 253 | {
|
---|
[3298ddc] | 254 | unsigned long key = (unsigned long) parentp;
|
---|
| 255 | link_t *hlp = hash_table_find(&childp->names, &key);
|
---|
| 256 | assert(hlp);
|
---|
| 257 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
|
---|
| 258 | return !strcmp(namep->name, component);
|
---|
[b8b23c8] | 259 | }
|
---|
[4b11571] | 260 |
|
---|
[736c164] | 261 | void *tmpfs_match(void *prnt, const char *component)
|
---|
| 262 | {
|
---|
| 263 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
|
---|
| 264 | tmpfs_dentry_t *childp = parentp->child;
|
---|
| 265 |
|
---|
| 266 | while (childp && !tmpfs_match_one(parentp, childp, component))
|
---|
| 267 | childp = childp->sibling;
|
---|
| 268 |
|
---|
| 269 | return (void *) childp;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[f2ec8c8] | 272 | void *
|
---|
[34f62f8] | 273 | tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
|
---|
[a8e9ab8d] | 274 | {
|
---|
[f2ec8c8] | 275 | unsigned long key = index;
|
---|
| 276 | link_t *lnk = hash_table_find(&dentries, &key);
|
---|
[a8e9ab8d] | 277 | if (!lnk)
|
---|
| 278 | return NULL;
|
---|
| 279 | return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[06901c6b] | 282 | void tmpfs_node_put(void *node)
|
---|
| 283 | {
|
---|
| 284 | /* nothing to do */
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[adb5fe3] | 287 | void *tmpfs_create_node(dev_handle_t dev_handle, int lflag)
|
---|
[b8b23c8] | 288 | {
|
---|
[72bde81] | 289 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
| 290 |
|
---|
| 291 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
|
---|
| 292 | if (!node)
|
---|
[b5553a2] | 293 | return NULL;
|
---|
[72bde81] | 294 |
|
---|
[3298ddc] | 295 | if (!tmpfs_dentry_initialize(node)) {
|
---|
| 296 | free(node);
|
---|
| 297 | return NULL;
|
---|
| 298 | }
|
---|
[72bde81] | 299 | node->index = tmpfs_next_index++;
|
---|
| 300 | if (lflag & L_DIRECTORY)
|
---|
| 301 | node->type = TMPFS_DIRECTORY;
|
---|
| 302 | else
|
---|
| 303 | node->type = TMPFS_FILE;
|
---|
| 304 |
|
---|
[fdb7795] | 305 | /* Insert the new node into the dentry hash table. */
|
---|
[f2ec8c8] | 306 | unsigned long key = node->index;
|
---|
| 307 | hash_table_insert(&dentries, &key, &node->dh_link);
|
---|
[fdb7795] | 308 | return (void *) node;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[0013b9ce] | 311 | int tmpfs_link_node(void *prnt, void *chld, const char *nm)
|
---|
[fdb7795] | 312 | {
|
---|
| 313 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
|
---|
| 314 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
|
---|
| 315 |
|
---|
| 316 | assert(parentp->type == TMPFS_DIRECTORY);
|
---|
| 317 |
|
---|
[3298ddc] | 318 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
|
---|
| 319 | if (!namep)
|
---|
[0013b9ce] | 320 | return ENOMEM;
|
---|
[3298ddc] | 321 | tmpfs_name_initialize(namep);
|
---|
[fdb7795] | 322 | size_t len = strlen(nm);
|
---|
[3298ddc] | 323 | namep->name = malloc(len + 1);
|
---|
| 324 | if (!namep->name) {
|
---|
| 325 | free(namep);
|
---|
[0013b9ce] | 326 | return ENOMEM;
|
---|
[3298ddc] | 327 | }
|
---|
| 328 | strcpy(namep->name, nm);
|
---|
| 329 | namep->parent = parentp;
|
---|
[adc8a63] | 330 |
|
---|
| 331 | childp->lnkcnt++;
|
---|
[3298ddc] | 332 |
|
---|
| 333 | unsigned long key = (unsigned long) parentp;
|
---|
| 334 | hash_table_insert(&childp->names, &key, &namep->link);
|
---|
[fdb7795] | 335 |
|
---|
[72bde81] | 336 | /* Insert the new node into the namespace. */
|
---|
[fdb7795] | 337 | if (parentp->child) {
|
---|
| 338 | tmpfs_dentry_t *tmp = parentp->child;
|
---|
[72bde81] | 339 | while (tmp->sibling)
|
---|
| 340 | tmp = tmp->sibling;
|
---|
[fdb7795] | 341 | tmp->sibling = childp;
|
---|
[72bde81] | 342 | } else {
|
---|
[fdb7795] | 343 | parentp->child = childp;
|
---|
[72bde81] | 344 | }
|
---|
| 345 |
|
---|
[0013b9ce] | 346 | return EOK;
|
---|
[b8b23c8] | 347 | }
|
---|
[4b11571] | 348 |
|
---|
[7b6d98b] | 349 | int tmpfs_unlink_node(void *prnt, void *chld)
|
---|
[b8b23c8] | 350 | {
|
---|
[7b6d98b] | 351 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
|
---|
| 352 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
|
---|
[16105cba] | 353 |
|
---|
[7b6d98b] | 354 | if (!parentp)
|
---|
[16105cba] | 355 | return EBUSY;
|
---|
| 356 |
|
---|
[7b6d98b] | 357 | if (childp->child)
|
---|
| 358 | return ENOTEMPTY;
|
---|
| 359 |
|
---|
| 360 | if (parentp->child == childp) {
|
---|
| 361 | parentp->child = childp->sibling;
|
---|
[16105cba] | 362 | } else {
|
---|
| 363 | /* TODO: consider doubly linked list for organizing siblings. */
|
---|
[7b6d98b] | 364 | tmpfs_dentry_t *tmp = parentp->child;
|
---|
| 365 | while (tmp->sibling != childp)
|
---|
[16105cba] | 366 | tmp = tmp->sibling;
|
---|
[7b6d98b] | 367 | tmp->sibling = childp->sibling;
|
---|
[16105cba] | 368 | }
|
---|
[7b6d98b] | 369 | childp->sibling = NULL;
|
---|
[16105cba] | 370 |
|
---|
[3298ddc] | 371 | unsigned long key = (unsigned long) parentp;
|
---|
| 372 | hash_table_remove(&childp->names, &key, 1);
|
---|
[fdb7795] | 373 |
|
---|
[7b6d98b] | 374 | childp->lnkcnt--;
|
---|
[adc8a63] | 375 |
|
---|
[16105cba] | 376 | return EOK;
|
---|
[d5cdffe] | 377 | }
|
---|
| 378 |
|
---|
[45f244b] | 379 | int tmpfs_destroy_node(void *nodep)
|
---|
[fdb7795] | 380 | {
|
---|
| 381 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
|
---|
| 382 |
|
---|
[adc8a63] | 383 | assert(!dentry->lnkcnt);
|
---|
[fdb7795] | 384 | assert(!dentry->child);
|
---|
| 385 | assert(!dentry->sibling);
|
---|
| 386 |
|
---|
[f2ec8c8] | 387 | unsigned long key = dentry->index;
|
---|
| 388 | hash_table_remove(&dentries, &key, 1);
|
---|
[fdb7795] | 389 |
|
---|
[3298ddc] | 390 | hash_table_destroy(&dentry->names);
|
---|
| 391 |
|
---|
[fdb7795] | 392 | if (dentry->type == TMPFS_FILE)
|
---|
| 393 | free(dentry->data);
|
---|
| 394 | free(dentry);
|
---|
[45f244b] | 395 | return EOK;
|
---|
[fdb7795] | 396 | }
|
---|
| 397 |
|
---|
[f49b0ea] | 398 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
[64b67c3] | 399 | {
|
---|
[f49b0ea] | 400 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
[64b67c3] | 401 |
|
---|
[2c448fb] | 402 | /* Initialize TMPFS. */
|
---|
[4b11571] | 403 | if (!root && !tmpfs_init()) {
|
---|
| 404 | ipc_answer_0(rid, ENOMEM);
|
---|
| 405 | return;
|
---|
| 406 | }
|
---|
[f49b0ea] | 407 |
|
---|
| 408 | if (dev_handle >= 0) {
|
---|
| 409 | if (tmpfs_restore(dev_handle))
|
---|
[5ab597d] | 410 | ipc_answer_3(rid, EOK, root->index, root->size,
|
---|
| 411 | root->lnkcnt);
|
---|
[f49b0ea] | 412 | else
|
---|
| 413 | ipc_answer_0(rid, ELIMIT);
|
---|
| 414 | } else {
|
---|
[5ab597d] | 415 | ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt);
|
---|
[f49b0ea] | 416 | }
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 420 | {
|
---|
| 421 | dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
| 422 | fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request);
|
---|
| 423 | fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
|
---|
| 424 | dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
|
---|
| 425 |
|
---|
| 426 | ipc_answer_0(rid, ENOTSUP);
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 430 | {
|
---|
[2c448fb] | 431 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
[d5cdffe] | 432 | }
|
---|
| 433 |
|
---|
[a4eb8a60] | 434 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 435 | {
|
---|
[f2ec8c8] | 436 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 437 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 438 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
[a4eb8a60] | 439 |
|
---|
| 440 | /*
|
---|
| 441 | * Lookup the respective dentry.
|
---|
| 442 | */
|
---|
| 443 | link_t *hlp;
|
---|
[f2ec8c8] | 444 | unsigned long key = index;
|
---|
| 445 | hlp = hash_table_find(&dentries, &key);
|
---|
[a4eb8a60] | 446 | if (!hlp) {
|
---|
| 447 | ipc_answer_0(rid, ENOENT);
|
---|
| 448 | return;
|
---|
| 449 | }
|
---|
| 450 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 451 | dh_link);
|
---|
| 452 |
|
---|
| 453 | /*
|
---|
[a92da0a] | 454 | * Receive the read request.
|
---|
[a4eb8a60] | 455 | */
|
---|
| 456 | ipc_callid_t callid;
|
---|
[92688eb] | 457 | size_t len;
|
---|
| 458 | if (!ipc_data_read_receive(&callid, &len)) {
|
---|
[a4eb8a60] | 459 | ipc_answer_0(callid, EINVAL);
|
---|
| 460 | ipc_answer_0(rid, EINVAL);
|
---|
| 461 | return;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
[5973fd0] | 464 | size_t bytes;
|
---|
| 465 | if (dentry->type == TMPFS_FILE) {
|
---|
| 466 | bytes = max(0, min(dentry->size - pos, len));
|
---|
| 467 | (void) ipc_data_read_finalize(callid, dentry->data + pos,
|
---|
| 468 | bytes);
|
---|
| 469 | } else {
|
---|
| 470 | int i;
|
---|
[75c426b4] | 471 | tmpfs_dentry_t *cur;
|
---|
[5973fd0] | 472 |
|
---|
| 473 | assert(dentry->type == TMPFS_DIRECTORY);
|
---|
| 474 |
|
---|
| 475 | /*
|
---|
| 476 | * Yes, we really use O(n) algorithm here.
|
---|
| 477 | * If it bothers someone, it could be fixed by introducing a
|
---|
| 478 | * hash table.
|
---|
| 479 | */
|
---|
| 480 | for (i = 0, cur = dentry->child; i < pos && cur; i++,
|
---|
| 481 | cur = cur->sibling)
|
---|
| 482 | ;
|
---|
| 483 |
|
---|
| 484 | if (!cur) {
|
---|
| 485 | ipc_answer_0(callid, ENOENT);
|
---|
| 486 | ipc_answer_1(rid, ENOENT, 0);
|
---|
| 487 | return;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[3298ddc] | 490 | unsigned long key = (unsigned long) dentry;
|
---|
| 491 | link_t *hlp = hash_table_find(&cur->names, &key);
|
---|
| 492 | assert(hlp);
|
---|
| 493 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
|
---|
| 494 | link);
|
---|
| 495 |
|
---|
| 496 | (void) ipc_data_read_finalize(callid, namep->name,
|
---|
| 497 | strlen(namep->name) + 1);
|
---|
[5973fd0] | 498 | bytes = 1;
|
---|
| 499 | }
|
---|
[7dab6b8] | 500 |
|
---|
| 501 | /*
|
---|
| 502 | * Answer the VFS_READ call.
|
---|
| 503 | */
|
---|
| 504 | ipc_answer_1(rid, EOK, bytes);
|
---|
[a4eb8a60] | 505 | }
|
---|
| 506 |
|
---|
[ee1b8ca] | 507 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 508 | {
|
---|
[f2ec8c8] | 509 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 510 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 511 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
[ee1b8ca] | 512 |
|
---|
| 513 | /*
|
---|
| 514 | * Lookup the respective dentry.
|
---|
| 515 | */
|
---|
| 516 | link_t *hlp;
|
---|
[f2ec8c8] | 517 | unsigned long key = index;
|
---|
| 518 | hlp = hash_table_find(&dentries, &key);
|
---|
[ee1b8ca] | 519 | if (!hlp) {
|
---|
| 520 | ipc_answer_0(rid, ENOENT);
|
---|
| 521 | return;
|
---|
| 522 | }
|
---|
| 523 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 524 | dh_link);
|
---|
| 525 |
|
---|
| 526 | /*
|
---|
| 527 | * Receive the write request.
|
---|
| 528 | */
|
---|
| 529 | ipc_callid_t callid;
|
---|
[92688eb] | 530 | size_t len;
|
---|
[3115355] | 531 | if (!ipc_data_write_receive(&callid, &len)) {
|
---|
[ee1b8ca] | 532 | ipc_answer_0(callid, EINVAL);
|
---|
| 533 | ipc_answer_0(rid, EINVAL);
|
---|
| 534 | return;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[c1bf5cb] | 537 | /*
|
---|
| 538 | * Check whether the file needs to grow.
|
---|
| 539 | */
|
---|
[92688eb] | 540 | if (pos + len <= dentry->size) {
|
---|
[c1bf5cb] | 541 | /* The file size is not changing. */
|
---|
[215e375] | 542 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
|
---|
[f7017572] | 543 | ipc_answer_2(rid, EOK, len, dentry->size);
|
---|
[c1bf5cb] | 544 | return;
|
---|
| 545 | }
|
---|
[92688eb] | 546 | size_t delta = (pos + len) - dentry->size;
|
---|
[ee1b8ca] | 547 | /*
|
---|
| 548 | * At this point, we are deliberately extremely straightforward and
|
---|
[c1bf5cb] | 549 | * simply realloc the contents of the file on every write that grows the
|
---|
| 550 | * file. In the end, the situation might not be as bad as it may look:
|
---|
| 551 | * our heap allocator can save us and just grow the block whenever
|
---|
| 552 | * possible.
|
---|
[ee1b8ca] | 553 | */
|
---|
[c1bf5cb] | 554 | void *newdata = realloc(dentry->data, dentry->size + delta);
|
---|
[ee1b8ca] | 555 | if (!newdata) {
|
---|
| 556 | ipc_answer_0(callid, ENOMEM);
|
---|
[f7017572] | 557 | ipc_answer_2(rid, EOK, 0, dentry->size);
|
---|
[ee1b8ca] | 558 | return;
|
---|
| 559 | }
|
---|
[0ee4322] | 560 | /* Clear any newly allocated memory in order to emulate gaps. */
|
---|
[752ccee] | 561 | memset(newdata + dentry->size, 0, delta);
|
---|
[c1bf5cb] | 562 | dentry->size += delta;
|
---|
[ee1b8ca] | 563 | dentry->data = newdata;
|
---|
[215e375] | 564 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
|
---|
[7fff5eab] | 565 | ipc_answer_2(rid, EOK, len, dentry->size);
|
---|
[ee1b8ca] | 566 | }
|
---|
| 567 |
|
---|
[0ee4322] | 568 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 569 | {
|
---|
[f2ec8c8] | 570 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 571 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 572 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
---|
[0ee4322] | 573 |
|
---|
| 574 | /*
|
---|
| 575 | * Lookup the respective dentry.
|
---|
| 576 | */
|
---|
| 577 | link_t *hlp;
|
---|
[f2ec8c8] | 578 | unsigned long key = index;
|
---|
| 579 | hlp = hash_table_find(&dentries, &key);
|
---|
[0ee4322] | 580 | if (!hlp) {
|
---|
| 581 | ipc_answer_0(rid, ENOENT);
|
---|
| 582 | return;
|
---|
| 583 | }
|
---|
| 584 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 585 | dh_link);
|
---|
| 586 |
|
---|
| 587 | if (size == dentry->size) {
|
---|
| 588 | ipc_answer_0(rid, EOK);
|
---|
| 589 | return;
|
---|
| 590 | }
|
---|
| 591 |
|
---|
| 592 | void *newdata = realloc(dentry->data, size);
|
---|
| 593 | if (!newdata) {
|
---|
| 594 | ipc_answer_0(rid, ENOMEM);
|
---|
| 595 | return;
|
---|
| 596 | }
|
---|
| 597 | if (size > dentry->size) {
|
---|
| 598 | size_t delta = size - dentry->size;
|
---|
| 599 | memset(newdata + dentry->size, 0, delta);
|
---|
| 600 | }
|
---|
| 601 | dentry->size = size;
|
---|
| 602 | dentry->data = newdata;
|
---|
| 603 | ipc_answer_0(rid, EOK);
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[fdb7795] | 606 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
[f17667a] | 607 | {
|
---|
[f2ec8c8] | 608 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 609 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
[45f244b] | 610 | int rc;
|
---|
[f17667a] | 611 |
|
---|
| 612 | link_t *hlp;
|
---|
[f2ec8c8] | 613 | unsigned long key = index;
|
---|
| 614 | hlp = hash_table_find(&dentries, &key);
|
---|
[f17667a] | 615 | if (!hlp) {
|
---|
| 616 | ipc_answer_0(rid, ENOENT);
|
---|
| 617 | return;
|
---|
| 618 | }
|
---|
| 619 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
|
---|
| 620 | dh_link);
|
---|
[45f244b] | 621 | rc = tmpfs_destroy_node(dentry);
|
---|
| 622 | ipc_answer_0(rid, rc);
|
---|
[f17667a] | 623 | }
|
---|
| 624 |
|
---|
[d5cdffe] | 625 | /**
|
---|
| 626 | * @}
|
---|
| 627 | */
|
---|