[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>
|
---|
[d9c8c81] | 50 | #include <adt/hash_table.h>
|
---|
[a4eb8a60] | 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 |
|
---|
[cf95bc0] | 57 | #define NODES_BUCKETS 256
|
---|
[3298ddc] | 58 |
|
---|
[8d049ee0] | 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;
|
---|
[adb5fe3] | 63 |
|
---|
[2c448fb] | 64 | /*
|
---|
| 65 | * Implementation of the libfs interface.
|
---|
| 66 | */
|
---|
[b5553a2] | 67 |
|
---|
[fdb7795] | 68 | /* Forward declarations of static functions. */
|
---|
[54e4479] | 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_put(fs_node_t *);
|
---|
| 72 | static int tmpfs_create_node(fs_node_t **, dev_handle_t, int);
|
---|
| 73 | static int tmpfs_destroy_node(fs_node_t *);
|
---|
[b6035ba] | 74 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
|
---|
[cf95bc0] | 75 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *);
|
---|
[2c448fb] | 76 |
|
---|
| 77 | /* Implementation of helper functions. */
|
---|
[54e4479] | 78 | static int tmpfs_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
|
---|
[2c448fb] | 79 | {
|
---|
[54e4479] | 80 | return tmpfs_node_get(rfn, dev_handle, TMPFS_SOME_ROOT);
|
---|
[2c448fb] | 81 | }
|
---|
| 82 |
|
---|
[54e4479] | 83 | static int tmpfs_has_children(bool *has_children, fs_node_t *fn)
|
---|
[2c448fb] | 84 | {
|
---|
[54e4479] | 85 | *has_children = !list_empty(&TMPFS_NODE(fn)->cs_head);
|
---|
| 86 | return EOK;
|
---|
[2c448fb] | 87 | }
|
---|
| 88 |
|
---|
[54e4479] | 89 | static fs_index_t tmpfs_index_get(fs_node_t *fn)
|
---|
[2c448fb] | 90 | {
|
---|
[54e4479] | 91 | return TMPFS_NODE(fn)->index;
|
---|
[2c448fb] | 92 | }
|
---|
| 93 |
|
---|
[54e4479] | 94 | static size_t tmpfs_size_get(fs_node_t *fn)
|
---|
[2c448fb] | 95 | {
|
---|
[54e4479] | 96 | return TMPFS_NODE(fn)->size;
|
---|
[2c448fb] | 97 | }
|
---|
| 98 |
|
---|
[54e4479] | 99 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn)
|
---|
[2c448fb] | 100 | {
|
---|
[54e4479] | 101 | return TMPFS_NODE(fn)->lnkcnt;
|
---|
[2c448fb] | 102 | }
|
---|
| 103 |
|
---|
| 104 | static char tmpfs_plb_get_char(unsigned pos)
|
---|
| 105 | {
|
---|
| 106 | return tmpfs_reg.plb_ro[pos % PLB_SIZE];
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[b6035ba] | 109 | static bool tmpfs_is_directory(fs_node_t *fn)
|
---|
[2c448fb] | 110 | {
|
---|
[b6035ba] | 111 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
|
---|
[2c448fb] | 112 | }
|
---|
| 113 |
|
---|
[b6035ba] | 114 | static bool tmpfs_is_file(fs_node_t *fn)
|
---|
[2c448fb] | 115 | {
|
---|
[b6035ba] | 116 | return TMPFS_NODE(fn)->type == TMPFS_FILE;
|
---|
[2c448fb] | 117 | }
|
---|
| 118 |
|
---|
| 119 | /** libfs operations */
|
---|
| 120 | libfs_ops_t tmpfs_libfs_ops = {
|
---|
[54e4479] | 121 | .root_get = tmpfs_root_get,
|
---|
[2c448fb] | 122 | .match = tmpfs_match,
|
---|
[a8e9ab8d] | 123 | .node_get = tmpfs_node_get,
|
---|
[06901c6b] | 124 | .node_put = tmpfs_node_put,
|
---|
[2c448fb] | 125 | .create = tmpfs_create_node,
|
---|
| 126 | .destroy = tmpfs_destroy_node,
|
---|
| 127 | .link = tmpfs_link_node,
|
---|
| 128 | .unlink = tmpfs_unlink_node,
|
---|
[54e4479] | 129 | .has_children = tmpfs_has_children,
|
---|
[2c448fb] | 130 | .index_get = tmpfs_index_get,
|
---|
| 131 | .size_get = tmpfs_size_get,
|
---|
| 132 | .lnkcnt_get = tmpfs_lnkcnt_get,
|
---|
| 133 | .plb_get_char = tmpfs_plb_get_char,
|
---|
| 134 | .is_directory = tmpfs_is_directory,
|
---|
| 135 | .is_file = tmpfs_is_file
|
---|
| 136 | };
|
---|
[fdb7795] | 137 |
|
---|
[cf95bc0] | 138 | /** Hash table of all TMPFS nodes. */
|
---|
| 139 | hash_table_t nodes;
|
---|
[a4eb8a60] | 140 |
|
---|
[cf95bc0] | 141 | #define NODES_KEY_INDEX 0
|
---|
| 142 | #define NODES_KEY_DEV 1
|
---|
[8d049ee0] | 143 |
|
---|
[cf95bc0] | 144 | /* Implementation of hash table interface for the nodes hash table. */
|
---|
| 145 | static hash_index_t nodes_hash(unsigned long key[])
|
---|
[a4eb8a60] | 146 | {
|
---|
[cf95bc0] | 147 | return key[NODES_KEY_INDEX] % NODES_BUCKETS;
|
---|
[a4eb8a60] | 148 | }
|
---|
| 149 |
|
---|
[cf95bc0] | 150 | static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[a4eb8a60] | 151 | {
|
---|
[cf95bc0] | 152 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
---|
| 153 | nh_link);
|
---|
| 154 | return (nodep->index == key[NODES_KEY_INDEX] &&
|
---|
| 155 | nodep->dev_handle == key[NODES_KEY_DEV]);
|
---|
[a4eb8a60] | 156 | }
|
---|
| 157 |
|
---|
[cf95bc0] | 158 | static void nodes_remove_callback(link_t *item)
|
---|
[a4eb8a60] | 159 | {
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[cf95bc0] | 162 | /** TMPFS nodes hash table operations. */
|
---|
| 163 | hash_table_operations_t nodes_ops = {
|
---|
| 164 | .hash = nodes_hash,
|
---|
| 165 | .compare = nodes_compare,
|
---|
| 166 | .remove_callback = nodes_remove_callback
|
---|
[a4eb8a60] | 167 | };
|
---|
| 168 |
|
---|
[cf95bc0] | 169 | static void tmpfs_node_initialize(tmpfs_node_t *nodep)
|
---|
[3298ddc] | 170 | {
|
---|
[cf95bc0] | 171 | nodep->bp = NULL;
|
---|
| 172 | nodep->index = 0;
|
---|
| 173 | nodep->dev_handle = 0;
|
---|
| 174 | nodep->type = TMPFS_NONE;
|
---|
| 175 | nodep->lnkcnt = 0;
|
---|
| 176 | nodep->size = 0;
|
---|
| 177 | nodep->data = NULL;
|
---|
| 178 | link_initialize(&nodep->nh_link);
|
---|
| 179 | list_initialize(&nodep->cs_head);
|
---|
[3298ddc] | 180 | }
|
---|
| 181 |
|
---|
[cf95bc0] | 182 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
|
---|
[3298ddc] | 183 | {
|
---|
[cf95bc0] | 184 | link_initialize(&dentryp->link);
|
---|
| 185 | dentryp->name = NULL;
|
---|
| 186 | dentryp->node = NULL;
|
---|
[4b11571] | 187 | }
|
---|
| 188 |
|
---|
[8d049ee0] | 189 | bool tmpfs_init(void)
|
---|
[4b11571] | 190 | {
|
---|
[cf95bc0] | 191 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
|
---|
[a4eb8a60] | 192 | return false;
|
---|
[8d049ee0] | 193 |
|
---|
| 194 | return true;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | static bool tmpfs_instance_init(dev_handle_t dev_handle)
|
---|
| 198 | {
|
---|
[b6035ba] | 199 | fs_node_t *rfn;
|
---|
[54e4479] | 200 | int rc;
|
---|
[8d049ee0] | 201 |
|
---|
[54e4479] | 202 | rc = tmpfs_create_node(&rfn, dev_handle, L_DIRECTORY);
|
---|
| 203 | if (rc != EOK || !rfn)
|
---|
[3298ddc] | 204 | return false;
|
---|
[b6035ba] | 205 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
---|
[3298ddc] | 206 | return true;
|
---|
[4b11571] | 207 | }
|
---|
| 208 |
|
---|
[54e4479] | 209 | int tmpfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
[736c164] | 210 | {
|
---|
[cf95bc0] | 211 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
| 212 | link_t *lnk;
|
---|
[736c164] | 213 |
|
---|
[cf95bc0] | 214 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
| 215 | lnk = lnk->next) {
|
---|
[54e4479] | 216 | tmpfs_dentry_t *dentryp;
|
---|
| 217 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
| 218 | if (!str_cmp(dentryp->name, component)) {
|
---|
| 219 | *rfn = FS_NODE(dentryp->node);
|
---|
| 220 | return EOK;
|
---|
| 221 | }
|
---|
[cf95bc0] | 222 | }
|
---|
[736c164] | 223 |
|
---|
[54e4479] | 224 | *rfn = NULL;
|
---|
| 225 | return EOK;
|
---|
[736c164] | 226 | }
|
---|
| 227 |
|
---|
[54e4479] | 228 | int tmpfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
|
---|
[a8e9ab8d] | 229 | {
|
---|
[8d049ee0] | 230 | unsigned long key[] = {
|
---|
[cf95bc0] | 231 | [NODES_KEY_INDEX] = index,
|
---|
| 232 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 233 | };
|
---|
[cf95bc0] | 234 | link_t *lnk = hash_table_find(&nodes, key);
|
---|
[54e4479] | 235 | if (lnk) {
|
---|
| 236 | tmpfs_node_t *nodep;
|
---|
| 237 | nodep = hash_table_get_instance(lnk, tmpfs_node_t, nh_link);
|
---|
| 238 | *rfn = FS_NODE(nodep);
|
---|
| 239 | } else {
|
---|
| 240 | *rfn = NULL;
|
---|
| 241 | }
|
---|
| 242 | return EOK;
|
---|
[a8e9ab8d] | 243 | }
|
---|
| 244 |
|
---|
[54e4479] | 245 | int tmpfs_node_put(fs_node_t *fn)
|
---|
[06901c6b] | 246 | {
|
---|
| 247 | /* nothing to do */
|
---|
[54e4479] | 248 | return EOK;
|
---|
[06901c6b] | 249 | }
|
---|
| 250 |
|
---|
[54e4479] | 251 | int tmpfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
|
---|
[b8b23c8] | 252 | {
|
---|
[54e4479] | 253 | fs_node_t *rootfn;
|
---|
| 254 | int rc;
|
---|
| 255 |
|
---|
[72bde81] | 256 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
| 257 |
|
---|
[cf95bc0] | 258 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
|
---|
| 259 | if (!nodep)
|
---|
[54e4479] | 260 | return ENOMEM;
|
---|
[cf95bc0] | 261 | tmpfs_node_initialize(nodep);
|
---|
| 262 | nodep->bp = malloc(sizeof(fs_node_t));
|
---|
| 263 | if (!nodep->bp) {
|
---|
| 264 | free(nodep);
|
---|
[54e4479] | 265 | return ENOMEM;
|
---|
[3298ddc] | 266 | }
|
---|
[83937ccd] | 267 | fs_node_initialize(nodep->bp);
|
---|
[cf95bc0] | 268 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
|
---|
[54e4479] | 269 |
|
---|
| 270 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
---|
| 271 | assert(rc == EOK);
|
---|
| 272 | if (!rootfn)
|
---|
[cf95bc0] | 273 | nodep->index = TMPFS_SOME_ROOT;
|
---|
[8d049ee0] | 274 | else
|
---|
[cf95bc0] | 275 | nodep->index = tmpfs_next_index++;
|
---|
| 276 | nodep->dev_handle = dev_handle;
|
---|
[72bde81] | 277 | if (lflag & L_DIRECTORY)
|
---|
[cf95bc0] | 278 | nodep->type = TMPFS_DIRECTORY;
|
---|
[72bde81] | 279 | else
|
---|
[cf95bc0] | 280 | nodep->type = TMPFS_FILE;
|
---|
[72bde81] | 281 |
|
---|
[cf95bc0] | 282 | /* Insert the new node into the nodes hash table. */
|
---|
[8d049ee0] | 283 | unsigned long key[] = {
|
---|
[cf95bc0] | 284 | [NODES_KEY_INDEX] = nodep->index,
|
---|
| 285 | [NODES_KEY_DEV] = nodep->dev_handle
|
---|
[8d049ee0] | 286 | };
|
---|
[cf95bc0] | 287 | hash_table_insert(&nodes, key, &nodep->nh_link);
|
---|
[54e4479] | 288 | *rfn = FS_NODE(nodep);
|
---|
| 289 | return EOK;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | int tmpfs_destroy_node(fs_node_t *fn)
|
---|
| 293 | {
|
---|
| 294 | tmpfs_node_t *nodep = TMPFS_NODE(fn);
|
---|
| 295 |
|
---|
| 296 | assert(!nodep->lnkcnt);
|
---|
| 297 | assert(list_empty(&nodep->cs_head));
|
---|
| 298 |
|
---|
| 299 | unsigned long key[] = {
|
---|
| 300 | [NODES_KEY_INDEX] = nodep->index,
|
---|
| 301 | [NODES_KEY_DEV] = nodep->dev_handle
|
---|
| 302 | };
|
---|
| 303 | hash_table_remove(&nodes, key, 2);
|
---|
| 304 |
|
---|
| 305 | if (nodep->type == TMPFS_FILE)
|
---|
| 306 | free(nodep->data);
|
---|
| 307 | free(nodep->bp);
|
---|
| 308 | free(nodep);
|
---|
| 309 | return EOK;
|
---|
[fdb7795] | 310 | }
|
---|
| 311 |
|
---|
[b6035ba] | 312 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
[fdb7795] | 313 | {
|
---|
[cf95bc0] | 314 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
| 315 | tmpfs_node_t *childp = TMPFS_NODE(cfn);
|
---|
| 316 | tmpfs_dentry_t *dentryp;
|
---|
| 317 | link_t *lnk;
|
---|
[fdb7795] | 318 |
|
---|
| 319 | assert(parentp->type == TMPFS_DIRECTORY);
|
---|
| 320 |
|
---|
[cf95bc0] | 321 | /* Check for duplicit entries. */
|
---|
| 322 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
| 323 | lnk = lnk->next) {
|
---|
| 324 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
| 325 | if (!str_cmp(dentryp->name, nm))
|
---|
| 326 | return EEXIST;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /* Allocate and initialize the dentry. */
|
---|
| 330 | dentryp = malloc(sizeof(tmpfs_dentry_t));
|
---|
| 331 | if (!dentryp)
|
---|
[0013b9ce] | 332 | return ENOMEM;
|
---|
[cf95bc0] | 333 | tmpfs_dentry_initialize(dentryp);
|
---|
| 334 |
|
---|
| 335 | /* Populate and link the new dentry. */
|
---|
[92fd52d7] | 336 | size_t size = str_size(nm);
|
---|
[cf95bc0] | 337 | dentryp->name = malloc(size + 1);
|
---|
| 338 | if (!dentryp->name) {
|
---|
| 339 | free(dentryp);
|
---|
[0013b9ce] | 340 | return ENOMEM;
|
---|
[3298ddc] | 341 | }
|
---|
[cf95bc0] | 342 | str_cpy(dentryp->name, size + 1, nm);
|
---|
| 343 | dentryp->node = childp;
|
---|
[adc8a63] | 344 | childp->lnkcnt++;
|
---|
[cf95bc0] | 345 | list_append(&dentryp->link, &parentp->cs_head);
|
---|
[72bde81] | 346 |
|
---|
[0013b9ce] | 347 | return EOK;
|
---|
[b8b23c8] | 348 | }
|
---|
[4b11571] | 349 |
|
---|
[cf95bc0] | 350 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
[b8b23c8] | 351 | {
|
---|
[cf95bc0] | 352 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
| 353 | tmpfs_node_t *childp = NULL;
|
---|
| 354 | tmpfs_dentry_t *dentryp;
|
---|
| 355 | link_t *lnk;
|
---|
[16105cba] | 356 |
|
---|
[7b6d98b] | 357 | if (!parentp)
|
---|
[16105cba] | 358 | return EBUSY;
|
---|
[cf95bc0] | 359 |
|
---|
| 360 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
| 361 | lnk = lnk->next) {
|
---|
| 362 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
| 363 | if (!str_cmp(dentryp->name, nm)) {
|
---|
| 364 | childp = dentryp->node;
|
---|
| 365 | assert(FS_NODE(childp) == cfn);
|
---|
| 366 | break;
|
---|
| 367 | }
|
---|
[16105cba] | 368 | }
|
---|
| 369 |
|
---|
[cf95bc0] | 370 | if (!childp)
|
---|
| 371 | return ENOENT;
|
---|
| 372 |
|
---|
| 373 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
|
---|
| 374 | return ENOTEMPTY;
|
---|
[fdb7795] | 375 |
|
---|
[cf95bc0] | 376 | list_remove(&dentryp->link);
|
---|
| 377 | free(dentryp);
|
---|
[7b6d98b] | 378 | childp->lnkcnt--;
|
---|
[adc8a63] | 379 |
|
---|
[16105cba] | 380 | return EOK;
|
---|
[d5cdffe] | 381 | }
|
---|
| 382 |
|
---|
[f49b0ea] | 383 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
[64b67c3] | 384 | {
|
---|
[f49b0ea] | 385 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
[54e4479] | 386 | int rc;
|
---|
[64b67c3] | 387 |
|
---|
[594303b] | 388 | /* accept the mount options */
|
---|
| 389 | ipc_callid_t callid;
|
---|
| 390 | size_t size;
|
---|
| 391 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 392 | ipc_answer_0(callid, EINVAL);
|
---|
| 393 | ipc_answer_0(rid, EINVAL);
|
---|
| 394 | return;
|
---|
| 395 | }
|
---|
| 396 | char *opts = malloc(size + 1);
|
---|
| 397 | if (!opts) {
|
---|
| 398 | ipc_answer_0(callid, ENOMEM);
|
---|
| 399 | ipc_answer_0(rid, ENOMEM);
|
---|
| 400 | return;
|
---|
| 401 | }
|
---|
| 402 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
|
---|
| 403 | if (retval != EOK) {
|
---|
| 404 | ipc_answer_0(rid, retval);
|
---|
| 405 | free(opts);
|
---|
| 406 | return;
|
---|
| 407 | }
|
---|
| 408 | opts[size] = '\0';
|
---|
| 409 |
|
---|
[8d049ee0] | 410 | /* Initialize TMPFS instance. */
|
---|
| 411 | if (!tmpfs_instance_init(dev_handle)) {
|
---|
[4b11571] | 412 | ipc_answer_0(rid, ENOMEM);
|
---|
| 413 | return;
|
---|
| 414 | }
|
---|
[f49b0ea] | 415 |
|
---|
[54e4479] | 416 | fs_node_t *rootfn;
|
---|
| 417 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
---|
| 418 | assert(rc == EOK);
|
---|
| 419 | tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
|
---|
[594303b] | 420 | if (str_cmp(opts, "restore") == 0) {
|
---|
[f49b0ea] | 421 | if (tmpfs_restore(dev_handle))
|
---|
[cf95bc0] | 422 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
| 423 | rootp->lnkcnt);
|
---|
[f49b0ea] | 424 | else
|
---|
| 425 | ipc_answer_0(rid, ELIMIT);
|
---|
| 426 | } else {
|
---|
[cf95bc0] | 427 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
| 428 | rootp->lnkcnt);
|
---|
[f49b0ea] | 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 433 | {
|
---|
[16d17ca] | 434 | libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
[f49b0ea] | 435 | }
|
---|
| 436 |
|
---|
| 437 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 438 | {
|
---|
[2c448fb] | 439 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
[d5cdffe] | 440 | }
|
---|
| 441 |
|
---|
[a4eb8a60] | 442 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 443 | {
|
---|
[f2ec8c8] | 444 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 445 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 446 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
[a4eb8a60] | 447 |
|
---|
| 448 | /*
|
---|
[cf95bc0] | 449 | * Lookup the respective TMPFS node.
|
---|
[a4eb8a60] | 450 | */
|
---|
| 451 | link_t *hlp;
|
---|
[8d049ee0] | 452 | unsigned long key[] = {
|
---|
[cf95bc0] | 453 | [NODES_KEY_INDEX] = index,
|
---|
| 454 | [NODES_KEY_DEV] = dev_handle,
|
---|
[8d049ee0] | 455 | };
|
---|
[cf95bc0] | 456 | hlp = hash_table_find(&nodes, key);
|
---|
[a4eb8a60] | 457 | if (!hlp) {
|
---|
| 458 | ipc_answer_0(rid, ENOENT);
|
---|
| 459 | return;
|
---|
| 460 | }
|
---|
[cf95bc0] | 461 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 462 | nh_link);
|
---|
[a4eb8a60] | 463 |
|
---|
| 464 | /*
|
---|
[a92da0a] | 465 | * Receive the read request.
|
---|
[a4eb8a60] | 466 | */
|
---|
| 467 | ipc_callid_t callid;
|
---|
[92fd52d7] | 468 | size_t size;
|
---|
| 469 | if (!ipc_data_read_receive(&callid, &size)) {
|
---|
[a4eb8a60] | 470 | ipc_answer_0(callid, EINVAL);
|
---|
| 471 | ipc_answer_0(rid, EINVAL);
|
---|
| 472 | return;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[5973fd0] | 475 | size_t bytes;
|
---|
[cf95bc0] | 476 | if (nodep->type == TMPFS_FILE) {
|
---|
| 477 | bytes = max(0, min(nodep->size - pos, size));
|
---|
| 478 | (void) ipc_data_read_finalize(callid, nodep->data + pos,
|
---|
[5973fd0] | 479 | bytes);
|
---|
| 480 | } else {
|
---|
[cf95bc0] | 481 | tmpfs_dentry_t *dentryp;
|
---|
| 482 | link_t *lnk;
|
---|
[5973fd0] | 483 | int i;
|
---|
| 484 |
|
---|
[cf95bc0] | 485 | assert(nodep->type == TMPFS_DIRECTORY);
|
---|
[5973fd0] | 486 |
|
---|
| 487 | /*
|
---|
| 488 | * Yes, we really use O(n) algorithm here.
|
---|
| 489 | * If it bothers someone, it could be fixed by introducing a
|
---|
| 490 | * hash table.
|
---|
| 491 | */
|
---|
[cf95bc0] | 492 | for (i = 0, lnk = nodep->cs_head.next;
|
---|
| 493 | i < pos && lnk != &nodep->cs_head;
|
---|
| 494 | i++, lnk = lnk->next)
|
---|
[5973fd0] | 495 | ;
|
---|
| 496 |
|
---|
[cf95bc0] | 497 | if (lnk == &nodep->cs_head) {
|
---|
[5973fd0] | 498 | ipc_answer_0(callid, ENOENT);
|
---|
| 499 | ipc_answer_1(rid, ENOENT, 0);
|
---|
| 500 | return;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[cf95bc0] | 503 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
[3298ddc] | 504 |
|
---|
[cf95bc0] | 505 | (void) ipc_data_read_finalize(callid, dentryp->name,
|
---|
| 506 | str_size(dentryp->name) + 1);
|
---|
[5973fd0] | 507 | bytes = 1;
|
---|
| 508 | }
|
---|
[7dab6b8] | 509 |
|
---|
| 510 | /*
|
---|
| 511 | * Answer the VFS_READ call.
|
---|
| 512 | */
|
---|
| 513 | ipc_answer_1(rid, EOK, bytes);
|
---|
[a4eb8a60] | 514 | }
|
---|
| 515 |
|
---|
[ee1b8ca] | 516 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 517 | {
|
---|
[f2ec8c8] | 518 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 519 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 520 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
[ee1b8ca] | 521 |
|
---|
| 522 | /*
|
---|
[cf95bc0] | 523 | * Lookup the respective TMPFS node.
|
---|
[ee1b8ca] | 524 | */
|
---|
| 525 | link_t *hlp;
|
---|
[8d049ee0] | 526 | unsigned long key[] = {
|
---|
[cf95bc0] | 527 | [NODES_KEY_INDEX] = index,
|
---|
| 528 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 529 | };
|
---|
[cf95bc0] | 530 | hlp = hash_table_find(&nodes, key);
|
---|
[ee1b8ca] | 531 | if (!hlp) {
|
---|
| 532 | ipc_answer_0(rid, ENOENT);
|
---|
| 533 | return;
|
---|
| 534 | }
|
---|
[cf95bc0] | 535 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 536 | nh_link);
|
---|
[ee1b8ca] | 537 |
|
---|
| 538 | /*
|
---|
| 539 | * Receive the write request.
|
---|
| 540 | */
|
---|
| 541 | ipc_callid_t callid;
|
---|
[92fd52d7] | 542 | size_t size;
|
---|
| 543 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
[ee1b8ca] | 544 | ipc_answer_0(callid, EINVAL);
|
---|
| 545 | ipc_answer_0(rid, EINVAL);
|
---|
| 546 | return;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
[c1bf5cb] | 549 | /*
|
---|
| 550 | * Check whether the file needs to grow.
|
---|
| 551 | */
|
---|
[cf95bc0] | 552 | if (pos + size <= nodep->size) {
|
---|
[c1bf5cb] | 553 | /* The file size is not changing. */
|
---|
[cf95bc0] | 554 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size);
|
---|
| 555 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
[c1bf5cb] | 556 | return;
|
---|
| 557 | }
|
---|
[cf95bc0] | 558 | size_t delta = (pos + size) - nodep->size;
|
---|
[ee1b8ca] | 559 | /*
|
---|
| 560 | * At this point, we are deliberately extremely straightforward and
|
---|
[c1bf5cb] | 561 | * simply realloc the contents of the file on every write that grows the
|
---|
| 562 | * file. In the end, the situation might not be as bad as it may look:
|
---|
| 563 | * our heap allocator can save us and just grow the block whenever
|
---|
| 564 | * possible.
|
---|
[ee1b8ca] | 565 | */
|
---|
[cf95bc0] | 566 | void *newdata = realloc(nodep->data, nodep->size + delta);
|
---|
[ee1b8ca] | 567 | if (!newdata) {
|
---|
| 568 | ipc_answer_0(callid, ENOMEM);
|
---|
[cf95bc0] | 569 | ipc_answer_2(rid, EOK, 0, nodep->size);
|
---|
[ee1b8ca] | 570 | return;
|
---|
| 571 | }
|
---|
[0ee4322] | 572 | /* Clear any newly allocated memory in order to emulate gaps. */
|
---|
[cf95bc0] | 573 | memset(newdata + nodep->size, 0, delta);
|
---|
| 574 | nodep->size += delta;
|
---|
| 575 | nodep->data = newdata;
|
---|
| 576 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size);
|
---|
| 577 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
[ee1b8ca] | 578 | }
|
---|
| 579 |
|
---|
[0ee4322] | 580 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 581 | {
|
---|
[f2ec8c8] | 582 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 583 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 584 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
---|
[0ee4322] | 585 |
|
---|
| 586 | /*
|
---|
[cf95bc0] | 587 | * Lookup the respective TMPFS node.
|
---|
[0ee4322] | 588 | */
|
---|
| 589 | link_t *hlp;
|
---|
[8d049ee0] | 590 | unsigned long key[] = {
|
---|
[cf95bc0] | 591 | [NODES_KEY_INDEX] = index,
|
---|
| 592 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 593 | };
|
---|
[cf95bc0] | 594 | hlp = hash_table_find(&nodes, key);
|
---|
[0ee4322] | 595 | if (!hlp) {
|
---|
| 596 | ipc_answer_0(rid, ENOENT);
|
---|
| 597 | return;
|
---|
| 598 | }
|
---|
[cf95bc0] | 599 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 600 | nh_link);
|
---|
[0ee4322] | 601 |
|
---|
[cf95bc0] | 602 | if (size == nodep->size) {
|
---|
[0ee4322] | 603 | ipc_answer_0(rid, EOK);
|
---|
| 604 | return;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[cf95bc0] | 607 | void *newdata = realloc(nodep->data, size);
|
---|
[0ee4322] | 608 | if (!newdata) {
|
---|
| 609 | ipc_answer_0(rid, ENOMEM);
|
---|
| 610 | return;
|
---|
| 611 | }
|
---|
[cf95bc0] | 612 | if (size > nodep->size) {
|
---|
| 613 | size_t delta = size - nodep->size;
|
---|
| 614 | memset(newdata + nodep->size, 0, delta);
|
---|
[0ee4322] | 615 | }
|
---|
[cf95bc0] | 616 | nodep->size = size;
|
---|
| 617 | nodep->data = newdata;
|
---|
[0ee4322] | 618 | ipc_answer_0(rid, EOK);
|
---|
| 619 | }
|
---|
| 620 |
|
---|
[c20aa06] | 621 | void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 622 | {
|
---|
| 623 | ipc_answer_0(rid, EOK);
|
---|
| 624 | }
|
---|
| 625 |
|
---|
[fdb7795] | 626 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
[f17667a] | 627 | {
|
---|
[f2ec8c8] | 628 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 629 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
[45f244b] | 630 | int rc;
|
---|
[f17667a] | 631 |
|
---|
| 632 | link_t *hlp;
|
---|
[8d049ee0] | 633 | unsigned long key[] = {
|
---|
[cf95bc0] | 634 | [NODES_KEY_INDEX] = index,
|
---|
| 635 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 636 | };
|
---|
[cf95bc0] | 637 | hlp = hash_table_find(&nodes, key);
|
---|
[f17667a] | 638 | if (!hlp) {
|
---|
| 639 | ipc_answer_0(rid, ENOENT);
|
---|
| 640 | return;
|
---|
| 641 | }
|
---|
[cf95bc0] | 642 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 643 | nh_link);
|
---|
| 644 | rc = tmpfs_destroy_node(FS_NODE(nodep));
|
---|
[45f244b] | 645 | ipc_answer_0(rid, rc);
|
---|
[f17667a] | 646 | }
|
---|
| 647 |
|
---|
[c20aa06] | 648 | void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 649 | {
|
---|
| 650 | libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
| 651 | }
|
---|
| 652 |
|
---|
[852b801] | 653 | void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
[c20aa06] | 654 | {
|
---|
[75160a6] | 655 | libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
[c20aa06] | 656 | }
|
---|
| 657 |
|
---|
| 658 | void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 659 | {
|
---|
| 660 | /* Dummy implementation */
|
---|
| 661 | ipc_answer_0(rid, EOK);
|
---|
| 662 | }
|
---|
| 663 |
|
---|
[d5cdffe] | 664 | /**
|
---|
| 665 | * @}
|
---|
[c20aa06] | 666 | */
|
---|