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