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