| 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 | return (nodep->index == key[NODES_KEY_INDEX] &&
|
|---|
| 163 | nodep->dev_handle == key[NODES_KEY_DEV]);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | static void nodes_remove_callback(link_t *item)
|
|---|
| 167 | {
|
|---|
| 168 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
|---|
| 169 | nh_link);
|
|---|
| 170 |
|
|---|
| 171 | while (!list_empty(&nodep->cs_head)) {
|
|---|
| 172 | tmpfs_dentry_t *dentryp = list_get_instance(nodep->cs_head.next,
|
|---|
| 173 | tmpfs_dentry_t, link);
|
|---|
| 174 |
|
|---|
| 175 | assert(nodep->type == TMPFS_DIRECTORY);
|
|---|
| 176 | list_remove(&dentryp->link);
|
|---|
| 177 | free(dentryp);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | if (nodep->data) {
|
|---|
| 181 | assert(nodep->type == TMPFS_FILE);
|
|---|
| 182 | free(nodep->data);
|
|---|
| 183 | }
|
|---|
| 184 | free(nodep->bp);
|
|---|
| 185 | free(nodep);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /** TMPFS nodes hash table operations. */
|
|---|
| 189 | hash_table_operations_t nodes_ops = {
|
|---|
| 190 | .hash = nodes_hash,
|
|---|
| 191 | .compare = nodes_compare,
|
|---|
| 192 | .remove_callback = nodes_remove_callback
|
|---|
| 193 | };
|
|---|
| 194 |
|
|---|
| 195 | static void tmpfs_node_initialize(tmpfs_node_t *nodep)
|
|---|
| 196 | {
|
|---|
| 197 | nodep->bp = NULL;
|
|---|
| 198 | nodep->index = 0;
|
|---|
| 199 | nodep->dev_handle = 0;
|
|---|
| 200 | nodep->type = TMPFS_NONE;
|
|---|
| 201 | nodep->lnkcnt = 0;
|
|---|
| 202 | nodep->size = 0;
|
|---|
| 203 | nodep->data = NULL;
|
|---|
| 204 | link_initialize(&nodep->nh_link);
|
|---|
| 205 | list_initialize(&nodep->cs_head);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
|
|---|
| 209 | {
|
|---|
| 210 | link_initialize(&dentryp->link);
|
|---|
| 211 | dentryp->name = NULL;
|
|---|
| 212 | dentryp->node = NULL;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | bool tmpfs_init(void)
|
|---|
| 216 | {
|
|---|
| 217 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
|
|---|
| 218 | return false;
|
|---|
| 219 |
|
|---|
| 220 | return true;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | static bool tmpfs_instance_init(dev_handle_t dev_handle)
|
|---|
| 224 | {
|
|---|
| 225 | fs_node_t *rfn;
|
|---|
| 226 | int rc;
|
|---|
| 227 |
|
|---|
| 228 | rc = tmpfs_create_node(&rfn, dev_handle, L_DIRECTORY);
|
|---|
| 229 | if (rc != EOK || !rfn)
|
|---|
| 230 | return false;
|
|---|
| 231 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
|---|
| 232 | return true;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | static void tmpfs_instance_done(dev_handle_t dev_handle)
|
|---|
| 236 | {
|
|---|
| 237 | unsigned long key[] = {
|
|---|
| 238 | [NODES_KEY_DEV] = dev_handle
|
|---|
| 239 | };
|
|---|
| 240 | /*
|
|---|
| 241 | * Here we are making use of one special feature of our hash table
|
|---|
| 242 | * implementation, which allows to remove more items based on a partial
|
|---|
| 243 | * key match. In the following, we are going to remove all nodes
|
|---|
| 244 | * matching our device handle. The nodes_remove_callback() function will
|
|---|
| 245 | * take care of resource deallocation.
|
|---|
| 246 | */
|
|---|
| 247 | hash_table_remove(&nodes, key, 1);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | int tmpfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
|---|
| 251 | {
|
|---|
| 252 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 253 | link_t *lnk;
|
|---|
| 254 |
|
|---|
| 255 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 256 | lnk = lnk->next) {
|
|---|
| 257 | tmpfs_dentry_t *dentryp;
|
|---|
| 258 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 259 | if (!str_cmp(dentryp->name, component)) {
|
|---|
| 260 | *rfn = FS_NODE(dentryp->node);
|
|---|
| 261 | return EOK;
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | *rfn = NULL;
|
|---|
| 266 | return EOK;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | int tmpfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
|
|---|
| 270 | {
|
|---|
| 271 | unsigned long key[] = {
|
|---|
| 272 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 273 | [NODES_KEY_INDEX] = index
|
|---|
| 274 | };
|
|---|
| 275 | link_t *lnk = hash_table_find(&nodes, key);
|
|---|
| 276 | if (lnk) {
|
|---|
| 277 | tmpfs_node_t *nodep;
|
|---|
| 278 | nodep = hash_table_get_instance(lnk, tmpfs_node_t, nh_link);
|
|---|
| 279 | *rfn = FS_NODE(nodep);
|
|---|
| 280 | } else {
|
|---|
| 281 | *rfn = NULL;
|
|---|
| 282 | }
|
|---|
| 283 | return EOK;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | int tmpfs_node_open(fs_node_t *fn)
|
|---|
| 287 | {
|
|---|
| 288 | /* nothing to do */
|
|---|
| 289 | return EOK;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | int tmpfs_node_put(fs_node_t *fn)
|
|---|
| 293 | {
|
|---|
| 294 | /* nothing to do */
|
|---|
| 295 | return EOK;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | int tmpfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
|
|---|
| 299 | {
|
|---|
| 300 | fs_node_t *rootfn;
|
|---|
| 301 | int rc;
|
|---|
| 302 |
|
|---|
| 303 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
|---|
| 304 |
|
|---|
| 305 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
|
|---|
| 306 | if (!nodep)
|
|---|
| 307 | return ENOMEM;
|
|---|
| 308 | tmpfs_node_initialize(nodep);
|
|---|
| 309 | nodep->bp = malloc(sizeof(fs_node_t));
|
|---|
| 310 | if (!nodep->bp) {
|
|---|
| 311 | free(nodep);
|
|---|
| 312 | return ENOMEM;
|
|---|
| 313 | }
|
|---|
| 314 | fs_node_initialize(nodep->bp);
|
|---|
| 315 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
|
|---|
| 316 |
|
|---|
| 317 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
|---|
| 318 | assert(rc == EOK);
|
|---|
| 319 | if (!rootfn)
|
|---|
| 320 | nodep->index = TMPFS_SOME_ROOT;
|
|---|
| 321 | else
|
|---|
| 322 | nodep->index = tmpfs_next_index++;
|
|---|
| 323 | nodep->dev_handle = dev_handle;
|
|---|
| 324 | if (lflag & L_DIRECTORY)
|
|---|
| 325 | nodep->type = TMPFS_DIRECTORY;
|
|---|
| 326 | else
|
|---|
| 327 | nodep->type = TMPFS_FILE;
|
|---|
| 328 |
|
|---|
| 329 | /* Insert the new node into the nodes hash table. */
|
|---|
| 330 | unsigned long key[] = {
|
|---|
| 331 | [NODES_KEY_DEV] = nodep->dev_handle,
|
|---|
| 332 | [NODES_KEY_INDEX] = nodep->index
|
|---|
| 333 | };
|
|---|
| 334 | hash_table_insert(&nodes, key, &nodep->nh_link);
|
|---|
| 335 | *rfn = FS_NODE(nodep);
|
|---|
| 336 | return EOK;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | int tmpfs_destroy_node(fs_node_t *fn)
|
|---|
| 340 | {
|
|---|
| 341 | tmpfs_node_t *nodep = TMPFS_NODE(fn);
|
|---|
| 342 |
|
|---|
| 343 | assert(!nodep->lnkcnt);
|
|---|
| 344 | assert(list_empty(&nodep->cs_head));
|
|---|
| 345 |
|
|---|
| 346 | unsigned long key[] = {
|
|---|
| 347 | [NODES_KEY_DEV] = nodep->dev_handle,
|
|---|
| 348 | [NODES_KEY_INDEX] = nodep->index
|
|---|
| 349 | };
|
|---|
| 350 | hash_table_remove(&nodes, key, 2);
|
|---|
| 351 |
|
|---|
| 352 | /*
|
|---|
| 353 | * The nodes_remove_callback() function takes care of the actual
|
|---|
| 354 | * resource deallocation.
|
|---|
| 355 | */
|
|---|
| 356 | return EOK;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 360 | {
|
|---|
| 361 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 362 | tmpfs_node_t *childp = TMPFS_NODE(cfn);
|
|---|
| 363 | tmpfs_dentry_t *dentryp;
|
|---|
| 364 | link_t *lnk;
|
|---|
| 365 |
|
|---|
| 366 | assert(parentp->type == TMPFS_DIRECTORY);
|
|---|
| 367 |
|
|---|
| 368 | /* Check for duplicit entries. */
|
|---|
| 369 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 370 | lnk = lnk->next) {
|
|---|
| 371 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 372 | if (!str_cmp(dentryp->name, nm))
|
|---|
| 373 | return EEXIST;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /* Allocate and initialize the dentry. */
|
|---|
| 377 | dentryp = malloc(sizeof(tmpfs_dentry_t));
|
|---|
| 378 | if (!dentryp)
|
|---|
| 379 | return ENOMEM;
|
|---|
| 380 | tmpfs_dentry_initialize(dentryp);
|
|---|
| 381 |
|
|---|
| 382 | /* Populate and link the new dentry. */
|
|---|
| 383 | size_t size = str_size(nm);
|
|---|
| 384 | dentryp->name = malloc(size + 1);
|
|---|
| 385 | if (!dentryp->name) {
|
|---|
| 386 | free(dentryp);
|
|---|
| 387 | return ENOMEM;
|
|---|
| 388 | }
|
|---|
| 389 | str_cpy(dentryp->name, size + 1, nm);
|
|---|
| 390 | dentryp->node = childp;
|
|---|
| 391 | childp->lnkcnt++;
|
|---|
| 392 | list_append(&dentryp->link, &parentp->cs_head);
|
|---|
| 393 |
|
|---|
| 394 | return EOK;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
|---|
| 398 | {
|
|---|
| 399 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
|---|
| 400 | tmpfs_node_t *childp = NULL;
|
|---|
| 401 | tmpfs_dentry_t *dentryp;
|
|---|
| 402 | link_t *lnk;
|
|---|
| 403 |
|
|---|
| 404 | if (!parentp)
|
|---|
| 405 | return EBUSY;
|
|---|
| 406 |
|
|---|
| 407 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
|---|
| 408 | lnk = lnk->next) {
|
|---|
| 409 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 410 | if (!str_cmp(dentryp->name, nm)) {
|
|---|
| 411 | childp = dentryp->node;
|
|---|
| 412 | assert(FS_NODE(childp) == cfn);
|
|---|
| 413 | break;
|
|---|
| 414 | }
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | if (!childp)
|
|---|
| 418 | return ENOENT;
|
|---|
| 419 |
|
|---|
| 420 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
|
|---|
| 421 | return ENOTEMPTY;
|
|---|
| 422 |
|
|---|
| 423 | list_remove(&dentryp->link);
|
|---|
| 424 | free(dentryp);
|
|---|
| 425 | childp->lnkcnt--;
|
|---|
| 426 |
|
|---|
| 427 | return EOK;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 431 | {
|
|---|
| 432 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 433 | int rc;
|
|---|
| 434 |
|
|---|
| 435 | /* accept the mount options */
|
|---|
| 436 | ipc_callid_t callid;
|
|---|
| 437 | size_t size;
|
|---|
| 438 | if (!async_data_write_receive(&callid, &size)) {
|
|---|
| 439 | ipc_answer_0(callid, EINVAL);
|
|---|
| 440 | ipc_answer_0(rid, EINVAL);
|
|---|
| 441 | return;
|
|---|
| 442 | }
|
|---|
| 443 | char *opts = malloc(size + 1);
|
|---|
| 444 | if (!opts) {
|
|---|
| 445 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 446 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 447 | return;
|
|---|
| 448 | }
|
|---|
| 449 | ipcarg_t retval = async_data_write_finalize(callid, opts, size);
|
|---|
| 450 | if (retval != EOK) {
|
|---|
| 451 | ipc_answer_0(rid, retval);
|
|---|
| 452 | free(opts);
|
|---|
| 453 | return;
|
|---|
| 454 | }
|
|---|
| 455 | opts[size] = '\0';
|
|---|
| 456 |
|
|---|
| 457 | /* Initialize TMPFS instance. */
|
|---|
| 458 | if (!tmpfs_instance_init(dev_handle)) {
|
|---|
| 459 | free(opts);
|
|---|
| 460 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 461 | return;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | fs_node_t *rootfn;
|
|---|
| 465 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
|---|
| 466 | assert(rc == EOK);
|
|---|
| 467 | tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
|
|---|
| 468 | if (str_cmp(opts, "restore") == 0) {
|
|---|
| 469 | if (tmpfs_restore(dev_handle))
|
|---|
| 470 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
|---|
| 471 | rootp->lnkcnt);
|
|---|
| 472 | else
|
|---|
| 473 | ipc_answer_0(rid, ELIMIT);
|
|---|
| 474 | } else {
|
|---|
| 475 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
|---|
| 476 | rootp->lnkcnt);
|
|---|
| 477 | }
|
|---|
| 478 | free(opts);
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 482 | {
|
|---|
| 483 | libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | void tmpfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 487 | {
|
|---|
| 488 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 489 |
|
|---|
| 490 | tmpfs_instance_done(dev_handle);
|
|---|
| 491 | ipc_answer_0(rid, EOK);
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | void tmpfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 495 | {
|
|---|
| 496 | libfs_unmount(&tmpfs_libfs_ops, rid, request);
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 500 | {
|
|---|
| 501 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 505 | {
|
|---|
| 506 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 507 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 508 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
|---|
| 509 |
|
|---|
| 510 | /*
|
|---|
| 511 | * Lookup the respective TMPFS node.
|
|---|
| 512 | */
|
|---|
| 513 | link_t *hlp;
|
|---|
| 514 | unsigned long key[] = {
|
|---|
| 515 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 516 | [NODES_KEY_INDEX] = index
|
|---|
| 517 | };
|
|---|
| 518 | hlp = hash_table_find(&nodes, key);
|
|---|
| 519 | if (!hlp) {
|
|---|
| 520 | ipc_answer_0(rid, ENOENT);
|
|---|
| 521 | return;
|
|---|
| 522 | }
|
|---|
| 523 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 524 | nh_link);
|
|---|
| 525 |
|
|---|
| 526 | /*
|
|---|
| 527 | * Receive the read request.
|
|---|
| 528 | */
|
|---|
| 529 | ipc_callid_t callid;
|
|---|
| 530 | size_t size;
|
|---|
| 531 | if (!async_data_read_receive(&callid, &size)) {
|
|---|
| 532 | ipc_answer_0(callid, EINVAL);
|
|---|
| 533 | ipc_answer_0(rid, EINVAL);
|
|---|
| 534 | return;
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | size_t bytes;
|
|---|
| 538 | if (nodep->type == TMPFS_FILE) {
|
|---|
| 539 | bytes = max(0, min(nodep->size - pos, size));
|
|---|
| 540 | (void) async_data_read_finalize(callid, nodep->data + pos,
|
|---|
| 541 | bytes);
|
|---|
| 542 | } else {
|
|---|
| 543 | tmpfs_dentry_t *dentryp;
|
|---|
| 544 | link_t *lnk;
|
|---|
| 545 | int i;
|
|---|
| 546 |
|
|---|
| 547 | assert(nodep->type == TMPFS_DIRECTORY);
|
|---|
| 548 |
|
|---|
| 549 | /*
|
|---|
| 550 | * Yes, we really use O(n) algorithm here.
|
|---|
| 551 | * If it bothers someone, it could be fixed by introducing a
|
|---|
| 552 | * hash table.
|
|---|
| 553 | */
|
|---|
| 554 | for (i = 0, lnk = nodep->cs_head.next;
|
|---|
| 555 | i < pos && lnk != &nodep->cs_head;
|
|---|
| 556 | i++, lnk = lnk->next)
|
|---|
| 557 | ;
|
|---|
| 558 |
|
|---|
| 559 | if (lnk == &nodep->cs_head) {
|
|---|
| 560 | ipc_answer_0(callid, ENOENT);
|
|---|
| 561 | ipc_answer_1(rid, ENOENT, 0);
|
|---|
| 562 | return;
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
|---|
| 566 |
|
|---|
| 567 | (void) async_data_read_finalize(callid, dentryp->name,
|
|---|
| 568 | str_size(dentryp->name) + 1);
|
|---|
| 569 | bytes = 1;
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | /*
|
|---|
| 573 | * Answer the VFS_READ call.
|
|---|
| 574 | */
|
|---|
| 575 | ipc_answer_1(rid, EOK, bytes);
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 579 | {
|
|---|
| 580 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 581 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 582 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
|---|
| 583 |
|
|---|
| 584 | /*
|
|---|
| 585 | * Lookup the respective TMPFS node.
|
|---|
| 586 | */
|
|---|
| 587 | link_t *hlp;
|
|---|
| 588 | unsigned long key[] = {
|
|---|
| 589 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 590 | [NODES_KEY_INDEX] = index
|
|---|
| 591 | };
|
|---|
| 592 | hlp = hash_table_find(&nodes, key);
|
|---|
| 593 | if (!hlp) {
|
|---|
| 594 | ipc_answer_0(rid, ENOENT);
|
|---|
| 595 | return;
|
|---|
| 596 | }
|
|---|
| 597 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 598 | nh_link);
|
|---|
| 599 |
|
|---|
| 600 | /*
|
|---|
| 601 | * Receive the write request.
|
|---|
| 602 | */
|
|---|
| 603 | ipc_callid_t callid;
|
|---|
| 604 | size_t size;
|
|---|
| 605 | if (!async_data_write_receive(&callid, &size)) {
|
|---|
| 606 | ipc_answer_0(callid, EINVAL);
|
|---|
| 607 | ipc_answer_0(rid, EINVAL);
|
|---|
| 608 | return;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | /*
|
|---|
| 612 | * Check whether the file needs to grow.
|
|---|
| 613 | */
|
|---|
| 614 | if (pos + size <= nodep->size) {
|
|---|
| 615 | /* The file size is not changing. */
|
|---|
| 616 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
|---|
| 617 | ipc_answer_2(rid, EOK, size, nodep->size);
|
|---|
| 618 | return;
|
|---|
| 619 | }
|
|---|
| 620 | size_t delta = (pos + size) - nodep->size;
|
|---|
| 621 | /*
|
|---|
| 622 | * At this point, we are deliberately extremely straightforward and
|
|---|
| 623 | * simply realloc the contents of the file on every write that grows the
|
|---|
| 624 | * file. In the end, the situation might not be as bad as it may look:
|
|---|
| 625 | * our heap allocator can save us and just grow the block whenever
|
|---|
| 626 | * possible.
|
|---|
| 627 | */
|
|---|
| 628 | void *newdata = realloc(nodep->data, nodep->size + delta);
|
|---|
| 629 | if (!newdata) {
|
|---|
| 630 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 631 | ipc_answer_2(rid, EOK, 0, nodep->size);
|
|---|
| 632 | return;
|
|---|
| 633 | }
|
|---|
| 634 | /* Clear any newly allocated memory in order to emulate gaps. */
|
|---|
| 635 | memset(newdata + nodep->size, 0, delta);
|
|---|
| 636 | nodep->size += delta;
|
|---|
| 637 | nodep->data = newdata;
|
|---|
| 638 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
|---|
| 639 | ipc_answer_2(rid, EOK, size, nodep->size);
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 643 | {
|
|---|
| 644 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 645 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 646 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
|---|
| 647 |
|
|---|
| 648 | /*
|
|---|
| 649 | * Lookup the respective TMPFS node.
|
|---|
| 650 | */
|
|---|
| 651 | link_t *hlp;
|
|---|
| 652 | unsigned long key[] = {
|
|---|
| 653 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 654 | [NODES_KEY_INDEX] = index
|
|---|
| 655 | };
|
|---|
| 656 | hlp = hash_table_find(&nodes, key);
|
|---|
| 657 | if (!hlp) {
|
|---|
| 658 | ipc_answer_0(rid, ENOENT);
|
|---|
| 659 | return;
|
|---|
| 660 | }
|
|---|
| 661 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 662 | nh_link);
|
|---|
| 663 |
|
|---|
| 664 | if (size == nodep->size) {
|
|---|
| 665 | ipc_answer_0(rid, EOK);
|
|---|
| 666 | return;
|
|---|
| 667 | }
|
|---|
| 668 |
|
|---|
| 669 | void *newdata = realloc(nodep->data, size);
|
|---|
| 670 | if (!newdata) {
|
|---|
| 671 | ipc_answer_0(rid, ENOMEM);
|
|---|
| 672 | return;
|
|---|
| 673 | }
|
|---|
| 674 | if (size > nodep->size) {
|
|---|
| 675 | size_t delta = size - nodep->size;
|
|---|
| 676 | memset(newdata + nodep->size, 0, delta);
|
|---|
| 677 | }
|
|---|
| 678 | nodep->size = size;
|
|---|
| 679 | nodep->data = newdata;
|
|---|
| 680 | ipc_answer_0(rid, EOK);
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 684 | {
|
|---|
| 685 | ipc_answer_0(rid, EOK);
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 689 | {
|
|---|
| 690 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
|---|
| 691 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
|---|
| 692 | int rc;
|
|---|
| 693 |
|
|---|
| 694 | link_t *hlp;
|
|---|
| 695 | unsigned long key[] = {
|
|---|
| 696 | [NODES_KEY_DEV] = dev_handle,
|
|---|
| 697 | [NODES_KEY_INDEX] = index
|
|---|
| 698 | };
|
|---|
| 699 | hlp = hash_table_find(&nodes, key);
|
|---|
| 700 | if (!hlp) {
|
|---|
| 701 | ipc_answer_0(rid, ENOENT);
|
|---|
| 702 | return;
|
|---|
| 703 | }
|
|---|
| 704 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
|---|
| 705 | nh_link);
|
|---|
| 706 | rc = tmpfs_destroy_node(FS_NODE(nodep));
|
|---|
| 707 | ipc_answer_0(rid, rc);
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 711 | {
|
|---|
| 712 | libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 716 | {
|
|---|
| 717 | libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 721 | {
|
|---|
| 722 | /* Dummy implementation */
|
|---|
| 723 | ipc_answer_0(rid, EOK);
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | /**
|
|---|
| 727 | * @}
|
|---|
| 728 | */
|
|---|