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