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