[d5cdffe] | 1 | /*
|
---|
[41a0d27] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[d5cdffe] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file tmpfs_ops.c
|
---|
| 35 | * @brief Implementation of VFS operations for the TMPFS file system
|
---|
| 36 | * server.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include "tmpfs.h"
|
---|
| 40 | #include "../../vfs/vfs.h"
|
---|
| 41 | #include <ipc/ipc.h>
|
---|
| 42 | #include <async.h>
|
---|
| 43 | #include <errno.h>
|
---|
[4b11571] | 44 | #include <atomic.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 | #include <string.h>
|
---|
| 47 | #include <stdio.h>
|
---|
[5973fd0] | 48 | #include <assert.h>
|
---|
[a4eb8a60] | 49 | #include <sys/types.h>
|
---|
| 50 | #include <libadt/hash_table.h>
|
---|
| 51 | #include <as.h>
|
---|
[2c448fb] | 52 | #include <libfs.h>
|
---|
[a4eb8a60] | 53 |
|
---|
| 54 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 55 | #define max(a, b) ((a) > (b) ? (a) : (b))
|
---|
[d5cdffe] | 56 |
|
---|
[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. */
|
---|
[b6035ba] | 69 | static fs_node_t *tmpfs_match(fs_node_t *, const char *);
|
---|
| 70 | static fs_node_t *tmpfs_node_get(dev_handle_t, fs_index_t);
|
---|
| 71 | static void tmpfs_node_put(fs_node_t *);
|
---|
| 72 | static fs_node_t *tmpfs_create_node(dev_handle_t, int);
|
---|
| 73 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
|
---|
[cf95bc0] | 74 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *);
|
---|
[b6035ba] | 75 | static int tmpfs_destroy_node(fs_node_t *);
|
---|
[2c448fb] | 76 |
|
---|
| 77 | /* Implementation of helper functions. */
|
---|
[b6035ba] | 78 | static fs_index_t tmpfs_index_get(fs_node_t *fn)
|
---|
[2c448fb] | 79 | {
|
---|
[b6035ba] | 80 | return TMPFS_NODE(fn)->index;
|
---|
[2c448fb] | 81 | }
|
---|
| 82 |
|
---|
[b6035ba] | 83 | static size_t tmpfs_size_get(fs_node_t *fn)
|
---|
[2c448fb] | 84 | {
|
---|
[b6035ba] | 85 | return TMPFS_NODE(fn)->size;
|
---|
[2c448fb] | 86 | }
|
---|
| 87 |
|
---|
[b6035ba] | 88 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn)
|
---|
[2c448fb] | 89 | {
|
---|
[b6035ba] | 90 | return TMPFS_NODE(fn)->lnkcnt;
|
---|
[2c448fb] | 91 | }
|
---|
| 92 |
|
---|
[b6035ba] | 93 | static bool tmpfs_has_children(fs_node_t *fn)
|
---|
[2c448fb] | 94 | {
|
---|
[cf95bc0] | 95 | return !list_empty(&TMPFS_NODE(fn)->cs_head);
|
---|
[2c448fb] | 96 | }
|
---|
| 97 |
|
---|
[b6035ba] | 98 | static fs_node_t *tmpfs_root_get(dev_handle_t dev_handle)
|
---|
[2c448fb] | 99 | {
|
---|
[8d049ee0] | 100 | return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT);
|
---|
[2c448fb] | 101 | }
|
---|
| 102 |
|
---|
| 103 | static char tmpfs_plb_get_char(unsigned pos)
|
---|
| 104 | {
|
---|
| 105 | return tmpfs_reg.plb_ro[pos % PLB_SIZE];
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[b6035ba] | 108 | static bool tmpfs_is_directory(fs_node_t *fn)
|
---|
[2c448fb] | 109 | {
|
---|
[b6035ba] | 110 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
|
---|
[2c448fb] | 111 | }
|
---|
| 112 |
|
---|
[b6035ba] | 113 | static bool tmpfs_is_file(fs_node_t *fn)
|
---|
[2c448fb] | 114 | {
|
---|
[b6035ba] | 115 | return TMPFS_NODE(fn)->type == TMPFS_FILE;
|
---|
[2c448fb] | 116 | }
|
---|
| 117 |
|
---|
| 118 | /** libfs operations */
|
---|
| 119 | libfs_ops_t tmpfs_libfs_ops = {
|
---|
| 120 | .match = tmpfs_match,
|
---|
[a8e9ab8d] | 121 | .node_get = tmpfs_node_get,
|
---|
[06901c6b] | 122 | .node_put = tmpfs_node_put,
|
---|
[2c448fb] | 123 | .create = tmpfs_create_node,
|
---|
| 124 | .destroy = tmpfs_destroy_node,
|
---|
| 125 | .link = tmpfs_link_node,
|
---|
| 126 | .unlink = tmpfs_unlink_node,
|
---|
| 127 | .index_get = tmpfs_index_get,
|
---|
| 128 | .size_get = tmpfs_size_get,
|
---|
| 129 | .lnkcnt_get = tmpfs_lnkcnt_get,
|
---|
[736c164] | 130 | .has_children = tmpfs_has_children,
|
---|
[2c448fb] | 131 | .root_get = tmpfs_root_get,
|
---|
| 132 | .plb_get_char = tmpfs_plb_get_char,
|
---|
| 133 | .is_directory = tmpfs_is_directory,
|
---|
| 134 | .is_file = tmpfs_is_file
|
---|
| 135 | };
|
---|
[fdb7795] | 136 |
|
---|
[cf95bc0] | 137 | /** Hash table of all TMPFS nodes. */
|
---|
| 138 | hash_table_t nodes;
|
---|
[a4eb8a60] | 139 |
|
---|
[cf95bc0] | 140 | #define NODES_KEY_INDEX 0
|
---|
| 141 | #define NODES_KEY_DEV 1
|
---|
[8d049ee0] | 142 |
|
---|
[cf95bc0] | 143 | /* Implementation of hash table interface for the nodes hash table. */
|
---|
| 144 | static hash_index_t nodes_hash(unsigned long key[])
|
---|
[a4eb8a60] | 145 | {
|
---|
[cf95bc0] | 146 | return key[NODES_KEY_INDEX] % NODES_BUCKETS;
|
---|
[a4eb8a60] | 147 | }
|
---|
| 148 |
|
---|
[cf95bc0] | 149 | static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[a4eb8a60] | 150 | {
|
---|
[cf95bc0] | 151 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
---|
| 152 | nh_link);
|
---|
| 153 | return (nodep->index == key[NODES_KEY_INDEX] &&
|
---|
| 154 | nodep->dev_handle == key[NODES_KEY_DEV]);
|
---|
[a4eb8a60] | 155 | }
|
---|
| 156 |
|
---|
[cf95bc0] | 157 | static void nodes_remove_callback(link_t *item)
|
---|
[a4eb8a60] | 158 | {
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[cf95bc0] | 161 | /** TMPFS nodes hash table operations. */
|
---|
| 162 | hash_table_operations_t nodes_ops = {
|
---|
| 163 | .hash = nodes_hash,
|
---|
| 164 | .compare = nodes_compare,
|
---|
| 165 | .remove_callback = nodes_remove_callback
|
---|
[a4eb8a60] | 166 | };
|
---|
| 167 |
|
---|
[cf95bc0] | 168 | static void tmpfs_node_initialize(tmpfs_node_t *nodep)
|
---|
[3298ddc] | 169 | {
|
---|
[cf95bc0] | 170 | nodep->bp = NULL;
|
---|
| 171 | nodep->index = 0;
|
---|
| 172 | nodep->dev_handle = 0;
|
---|
| 173 | nodep->type = TMPFS_NONE;
|
---|
| 174 | nodep->lnkcnt = 0;
|
---|
| 175 | nodep->size = 0;
|
---|
| 176 | nodep->data = NULL;
|
---|
| 177 | link_initialize(&nodep->nh_link);
|
---|
| 178 | list_initialize(&nodep->cs_head);
|
---|
[3298ddc] | 179 | }
|
---|
| 180 |
|
---|
[cf95bc0] | 181 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
|
---|
[3298ddc] | 182 | {
|
---|
[cf95bc0] | 183 | link_initialize(&dentryp->link);
|
---|
| 184 | dentryp->name = NULL;
|
---|
| 185 | dentryp->node = NULL;
|
---|
[4b11571] | 186 | }
|
---|
| 187 |
|
---|
[8d049ee0] | 188 | bool tmpfs_init(void)
|
---|
[4b11571] | 189 | {
|
---|
[cf95bc0] | 190 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
|
---|
[a4eb8a60] | 191 | return false;
|
---|
[8d049ee0] | 192 |
|
---|
| 193 | return true;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | static bool tmpfs_instance_init(dev_handle_t dev_handle)
|
---|
| 197 | {
|
---|
[b6035ba] | 198 | fs_node_t *rfn;
|
---|
[8d049ee0] | 199 |
|
---|
[b6035ba] | 200 | rfn = tmpfs_create_node(dev_handle, L_DIRECTORY);
|
---|
| 201 | if (!rfn)
|
---|
[3298ddc] | 202 | return false;
|
---|
[b6035ba] | 203 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
---|
[3298ddc] | 204 | return true;
|
---|
[4b11571] | 205 | }
|
---|
| 206 |
|
---|
[b6035ba] | 207 | fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component)
|
---|
[736c164] | 208 | {
|
---|
[cf95bc0] | 209 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
| 210 | link_t *lnk;
|
---|
[736c164] | 211 |
|
---|
[cf95bc0] | 212 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
| 213 | lnk = lnk->next) {
|
---|
| 214 | tmpfs_dentry_t *dentryp = list_get_instance(lnk, tmpfs_dentry_t,
|
---|
| 215 | link);
|
---|
| 216 | if (!str_cmp(dentryp->name, component))
|
---|
| 217 | return FS_NODE(dentryp->node);
|
---|
| 218 | }
|
---|
[736c164] | 219 |
|
---|
[cf95bc0] | 220 | return NULL;
|
---|
[736c164] | 221 | }
|
---|
| 222 |
|
---|
[b6035ba] | 223 | fs_node_t *tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
|
---|
[a8e9ab8d] | 224 | {
|
---|
[8d049ee0] | 225 | unsigned long key[] = {
|
---|
[cf95bc0] | 226 | [NODES_KEY_INDEX] = index,
|
---|
| 227 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 228 | };
|
---|
[cf95bc0] | 229 | link_t *lnk = hash_table_find(&nodes, key);
|
---|
[a8e9ab8d] | 230 | if (!lnk)
|
---|
| 231 | return NULL;
|
---|
[cf95bc0] | 232 | return FS_NODE(hash_table_get_instance(lnk, tmpfs_node_t, nh_link));
|
---|
[a8e9ab8d] | 233 | }
|
---|
| 234 |
|
---|
[b6035ba] | 235 | void tmpfs_node_put(fs_node_t *fn)
|
---|
[06901c6b] | 236 | {
|
---|
| 237 | /* nothing to do */
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[b6035ba] | 240 | fs_node_t *tmpfs_create_node(dev_handle_t dev_handle, int lflag)
|
---|
[b8b23c8] | 241 | {
|
---|
[72bde81] | 242 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
| 243 |
|
---|
[cf95bc0] | 244 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
|
---|
| 245 | if (!nodep)
|
---|
[b6035ba] | 246 | return NULL;
|
---|
[cf95bc0] | 247 | tmpfs_node_initialize(nodep);
|
---|
| 248 | nodep->bp = malloc(sizeof(fs_node_t));
|
---|
| 249 | if (!nodep->bp) {
|
---|
| 250 | free(nodep);
|
---|
[3298ddc] | 251 | return NULL;
|
---|
| 252 | }
|
---|
[83937ccd] | 253 | fs_node_initialize(nodep->bp);
|
---|
[cf95bc0] | 254 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
|
---|
[8d049ee0] | 255 | if (!tmpfs_root_get(dev_handle))
|
---|
[cf95bc0] | 256 | nodep->index = TMPFS_SOME_ROOT;
|
---|
[8d049ee0] | 257 | else
|
---|
[cf95bc0] | 258 | nodep->index = tmpfs_next_index++;
|
---|
| 259 | nodep->dev_handle = dev_handle;
|
---|
[72bde81] | 260 | if (lflag & L_DIRECTORY)
|
---|
[cf95bc0] | 261 | nodep->type = TMPFS_DIRECTORY;
|
---|
[72bde81] | 262 | else
|
---|
[cf95bc0] | 263 | nodep->type = TMPFS_FILE;
|
---|
[72bde81] | 264 |
|
---|
[cf95bc0] | 265 | /* Insert the new node into the nodes hash table. */
|
---|
[8d049ee0] | 266 | unsigned long key[] = {
|
---|
[cf95bc0] | 267 | [NODES_KEY_INDEX] = nodep->index,
|
---|
| 268 | [NODES_KEY_DEV] = nodep->dev_handle
|
---|
[8d049ee0] | 269 | };
|
---|
[cf95bc0] | 270 | hash_table_insert(&nodes, key, &nodep->nh_link);
|
---|
| 271 | return FS_NODE(nodep);
|
---|
[fdb7795] | 272 | }
|
---|
| 273 |
|
---|
[b6035ba] | 274 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
[fdb7795] | 275 | {
|
---|
[cf95bc0] | 276 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
| 277 | tmpfs_node_t *childp = TMPFS_NODE(cfn);
|
---|
| 278 | tmpfs_dentry_t *dentryp;
|
---|
| 279 | link_t *lnk;
|
---|
[fdb7795] | 280 |
|
---|
| 281 | assert(parentp->type == TMPFS_DIRECTORY);
|
---|
| 282 |
|
---|
[cf95bc0] | 283 | /* Check for duplicit entries. */
|
---|
| 284 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
| 285 | lnk = lnk->next) {
|
---|
| 286 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
| 287 | if (!str_cmp(dentryp->name, nm))
|
---|
| 288 | return EEXIST;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /* Allocate and initialize the dentry. */
|
---|
| 292 | dentryp = malloc(sizeof(tmpfs_dentry_t));
|
---|
| 293 | if (!dentryp)
|
---|
[0013b9ce] | 294 | return ENOMEM;
|
---|
[cf95bc0] | 295 | tmpfs_dentry_initialize(dentryp);
|
---|
| 296 |
|
---|
| 297 | /* Populate and link the new dentry. */
|
---|
[92fd52d7] | 298 | size_t size = str_size(nm);
|
---|
[cf95bc0] | 299 | dentryp->name = malloc(size + 1);
|
---|
| 300 | if (!dentryp->name) {
|
---|
| 301 | free(dentryp);
|
---|
[0013b9ce] | 302 | return ENOMEM;
|
---|
[3298ddc] | 303 | }
|
---|
[cf95bc0] | 304 | str_cpy(dentryp->name, size + 1, nm);
|
---|
| 305 | dentryp->node = childp;
|
---|
[adc8a63] | 306 | childp->lnkcnt++;
|
---|
[cf95bc0] | 307 | list_append(&dentryp->link, &parentp->cs_head);
|
---|
[72bde81] | 308 |
|
---|
[0013b9ce] | 309 | return EOK;
|
---|
[b8b23c8] | 310 | }
|
---|
[4b11571] | 311 |
|
---|
[cf95bc0] | 312 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
[b8b23c8] | 313 | {
|
---|
[cf95bc0] | 314 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
| 315 | tmpfs_node_t *childp = NULL;
|
---|
| 316 | tmpfs_dentry_t *dentryp;
|
---|
| 317 | link_t *lnk;
|
---|
[16105cba] | 318 |
|
---|
[7b6d98b] | 319 | if (!parentp)
|
---|
[16105cba] | 320 | return EBUSY;
|
---|
[cf95bc0] | 321 |
|
---|
| 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 | childp = dentryp->node;
|
---|
| 327 | assert(FS_NODE(childp) == cfn);
|
---|
| 328 | break;
|
---|
| 329 | }
|
---|
[16105cba] | 330 | }
|
---|
| 331 |
|
---|
[cf95bc0] | 332 | if (!childp)
|
---|
| 333 | return ENOENT;
|
---|
| 334 |
|
---|
| 335 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
|
---|
| 336 | return ENOTEMPTY;
|
---|
[fdb7795] | 337 |
|
---|
[cf95bc0] | 338 | list_remove(&dentryp->link);
|
---|
| 339 | free(dentryp);
|
---|
[7b6d98b] | 340 | childp->lnkcnt--;
|
---|
[adc8a63] | 341 |
|
---|
[16105cba] | 342 | return EOK;
|
---|
[d5cdffe] | 343 | }
|
---|
| 344 |
|
---|
[b6035ba] | 345 | int tmpfs_destroy_node(fs_node_t *fn)
|
---|
[fdb7795] | 346 | {
|
---|
[cf95bc0] | 347 | tmpfs_node_t *nodep = TMPFS_NODE(fn);
|
---|
[fdb7795] | 348 |
|
---|
[cf95bc0] | 349 | assert(!nodep->lnkcnt);
|
---|
| 350 | assert(list_empty(&nodep->cs_head));
|
---|
[fdb7795] | 351 |
|
---|
[8d049ee0] | 352 | unsigned long key[] = {
|
---|
[cf95bc0] | 353 | [NODES_KEY_INDEX] = nodep->index,
|
---|
| 354 | [NODES_KEY_DEV] = nodep->dev_handle
|
---|
[8d049ee0] | 355 | };
|
---|
[cf95bc0] | 356 | hash_table_remove(&nodes, key, 2);
|
---|
[fdb7795] | 357 |
|
---|
[cf95bc0] | 358 | if (nodep->type == TMPFS_FILE)
|
---|
| 359 | free(nodep->data);
|
---|
| 360 | free(nodep->bp);
|
---|
| 361 | free(nodep);
|
---|
[45f244b] | 362 | return EOK;
|
---|
[fdb7795] | 363 | }
|
---|
| 364 |
|
---|
[f49b0ea] | 365 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
[64b67c3] | 366 | {
|
---|
[f49b0ea] | 367 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
[64b67c3] | 368 |
|
---|
[594303b] | 369 | /* accept the mount options */
|
---|
| 370 | ipc_callid_t callid;
|
---|
| 371 | size_t size;
|
---|
| 372 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
| 373 | ipc_answer_0(callid, EINVAL);
|
---|
| 374 | ipc_answer_0(rid, EINVAL);
|
---|
| 375 | return;
|
---|
| 376 | }
|
---|
| 377 | char *opts = malloc(size + 1);
|
---|
| 378 | if (!opts) {
|
---|
| 379 | ipc_answer_0(callid, ENOMEM);
|
---|
| 380 | ipc_answer_0(rid, ENOMEM);
|
---|
| 381 | return;
|
---|
| 382 | }
|
---|
| 383 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
|
---|
| 384 | if (retval != EOK) {
|
---|
| 385 | ipc_answer_0(rid, retval);
|
---|
| 386 | free(opts);
|
---|
| 387 | return;
|
---|
| 388 | }
|
---|
| 389 | opts[size] = '\0';
|
---|
| 390 |
|
---|
[8d049ee0] | 391 | /* Initialize TMPFS instance. */
|
---|
| 392 | if (!tmpfs_instance_init(dev_handle)) {
|
---|
[4b11571] | 393 | ipc_answer_0(rid, ENOMEM);
|
---|
| 394 | return;
|
---|
| 395 | }
|
---|
[f49b0ea] | 396 |
|
---|
[cf95bc0] | 397 | tmpfs_node_t *rootp = TMPFS_NODE(tmpfs_root_get(dev_handle));
|
---|
[594303b] | 398 | if (str_cmp(opts, "restore") == 0) {
|
---|
[f49b0ea] | 399 | if (tmpfs_restore(dev_handle))
|
---|
[cf95bc0] | 400 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
| 401 | rootp->lnkcnt);
|
---|
[f49b0ea] | 402 | else
|
---|
| 403 | ipc_answer_0(rid, ELIMIT);
|
---|
| 404 | } else {
|
---|
[cf95bc0] | 405 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
| 406 | rootp->lnkcnt);
|
---|
[f49b0ea] | 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 411 | {
|
---|
[16d17ca] | 412 | libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
[f49b0ea] | 413 | }
|
---|
| 414 |
|
---|
| 415 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 416 | {
|
---|
[2c448fb] | 417 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
[d5cdffe] | 418 | }
|
---|
| 419 |
|
---|
[a4eb8a60] | 420 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 421 | {
|
---|
[f2ec8c8] | 422 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 423 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 424 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
[a4eb8a60] | 425 |
|
---|
| 426 | /*
|
---|
[cf95bc0] | 427 | * Lookup the respective TMPFS node.
|
---|
[a4eb8a60] | 428 | */
|
---|
| 429 | link_t *hlp;
|
---|
[8d049ee0] | 430 | unsigned long key[] = {
|
---|
[cf95bc0] | 431 | [NODES_KEY_INDEX] = index,
|
---|
| 432 | [NODES_KEY_DEV] = dev_handle,
|
---|
[8d049ee0] | 433 | };
|
---|
[cf95bc0] | 434 | hlp = hash_table_find(&nodes, key);
|
---|
[a4eb8a60] | 435 | if (!hlp) {
|
---|
| 436 | ipc_answer_0(rid, ENOENT);
|
---|
| 437 | return;
|
---|
| 438 | }
|
---|
[cf95bc0] | 439 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 440 | nh_link);
|
---|
[a4eb8a60] | 441 |
|
---|
| 442 | /*
|
---|
[a92da0a] | 443 | * Receive the read request.
|
---|
[a4eb8a60] | 444 | */
|
---|
| 445 | ipc_callid_t callid;
|
---|
[92fd52d7] | 446 | size_t size;
|
---|
| 447 | if (!ipc_data_read_receive(&callid, &size)) {
|
---|
[a4eb8a60] | 448 | ipc_answer_0(callid, EINVAL);
|
---|
| 449 | ipc_answer_0(rid, EINVAL);
|
---|
| 450 | return;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
[5973fd0] | 453 | size_t bytes;
|
---|
[cf95bc0] | 454 | if (nodep->type == TMPFS_FILE) {
|
---|
| 455 | bytes = max(0, min(nodep->size - pos, size));
|
---|
| 456 | (void) ipc_data_read_finalize(callid, nodep->data + pos,
|
---|
[5973fd0] | 457 | bytes);
|
---|
| 458 | } else {
|
---|
[cf95bc0] | 459 | tmpfs_dentry_t *dentryp;
|
---|
| 460 | link_t *lnk;
|
---|
[5973fd0] | 461 | int i;
|
---|
| 462 |
|
---|
[cf95bc0] | 463 | assert(nodep->type == TMPFS_DIRECTORY);
|
---|
[5973fd0] | 464 |
|
---|
| 465 | /*
|
---|
| 466 | * Yes, we really use O(n) algorithm here.
|
---|
| 467 | * If it bothers someone, it could be fixed by introducing a
|
---|
| 468 | * hash table.
|
---|
| 469 | */
|
---|
[cf95bc0] | 470 | for (i = 0, lnk = nodep->cs_head.next;
|
---|
| 471 | i < pos && lnk != &nodep->cs_head;
|
---|
| 472 | i++, lnk = lnk->next)
|
---|
[5973fd0] | 473 | ;
|
---|
| 474 |
|
---|
[cf95bc0] | 475 | if (lnk == &nodep->cs_head) {
|
---|
[5973fd0] | 476 | ipc_answer_0(callid, ENOENT);
|
---|
| 477 | ipc_answer_1(rid, ENOENT, 0);
|
---|
| 478 | return;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[cf95bc0] | 481 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
[3298ddc] | 482 |
|
---|
[cf95bc0] | 483 | (void) ipc_data_read_finalize(callid, dentryp->name,
|
---|
| 484 | str_size(dentryp->name) + 1);
|
---|
[5973fd0] | 485 | bytes = 1;
|
---|
| 486 | }
|
---|
[7dab6b8] | 487 |
|
---|
| 488 | /*
|
---|
| 489 | * Answer the VFS_READ call.
|
---|
| 490 | */
|
---|
| 491 | ipc_answer_1(rid, EOK, bytes);
|
---|
[a4eb8a60] | 492 | }
|
---|
| 493 |
|
---|
[ee1b8ca] | 494 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 495 | {
|
---|
[f2ec8c8] | 496 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 497 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 498 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
[ee1b8ca] | 499 |
|
---|
| 500 | /*
|
---|
[cf95bc0] | 501 | * Lookup the respective TMPFS node.
|
---|
[ee1b8ca] | 502 | */
|
---|
| 503 | link_t *hlp;
|
---|
[8d049ee0] | 504 | unsigned long key[] = {
|
---|
[cf95bc0] | 505 | [NODES_KEY_INDEX] = index,
|
---|
| 506 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 507 | };
|
---|
[cf95bc0] | 508 | hlp = hash_table_find(&nodes, key);
|
---|
[ee1b8ca] | 509 | if (!hlp) {
|
---|
| 510 | ipc_answer_0(rid, ENOENT);
|
---|
| 511 | return;
|
---|
| 512 | }
|
---|
[cf95bc0] | 513 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 514 | nh_link);
|
---|
[ee1b8ca] | 515 |
|
---|
| 516 | /*
|
---|
| 517 | * Receive the write request.
|
---|
| 518 | */
|
---|
| 519 | ipc_callid_t callid;
|
---|
[92fd52d7] | 520 | size_t size;
|
---|
| 521 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
[ee1b8ca] | 522 | ipc_answer_0(callid, EINVAL);
|
---|
| 523 | ipc_answer_0(rid, EINVAL);
|
---|
| 524 | return;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
[c1bf5cb] | 527 | /*
|
---|
| 528 | * Check whether the file needs to grow.
|
---|
| 529 | */
|
---|
[cf95bc0] | 530 | if (pos + size <= nodep->size) {
|
---|
[c1bf5cb] | 531 | /* The file size is not changing. */
|
---|
[cf95bc0] | 532 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size);
|
---|
| 533 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
[c1bf5cb] | 534 | return;
|
---|
| 535 | }
|
---|
[cf95bc0] | 536 | size_t delta = (pos + size) - nodep->size;
|
---|
[ee1b8ca] | 537 | /*
|
---|
| 538 | * At this point, we are deliberately extremely straightforward and
|
---|
[c1bf5cb] | 539 | * simply realloc the contents of the file on every write that grows the
|
---|
| 540 | * file. In the end, the situation might not be as bad as it may look:
|
---|
| 541 | * our heap allocator can save us and just grow the block whenever
|
---|
| 542 | * possible.
|
---|
[ee1b8ca] | 543 | */
|
---|
[cf95bc0] | 544 | void *newdata = realloc(nodep->data, nodep->size + delta);
|
---|
[ee1b8ca] | 545 | if (!newdata) {
|
---|
| 546 | ipc_answer_0(callid, ENOMEM);
|
---|
[cf95bc0] | 547 | ipc_answer_2(rid, EOK, 0, nodep->size);
|
---|
[ee1b8ca] | 548 | return;
|
---|
| 549 | }
|
---|
[0ee4322] | 550 | /* Clear any newly allocated memory in order to emulate gaps. */
|
---|
[cf95bc0] | 551 | memset(newdata + nodep->size, 0, delta);
|
---|
| 552 | nodep->size += delta;
|
---|
| 553 | nodep->data = newdata;
|
---|
| 554 | (void) ipc_data_write_finalize(callid, nodep->data + pos, size);
|
---|
| 555 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
[ee1b8ca] | 556 | }
|
---|
| 557 |
|
---|
[0ee4322] | 558 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 559 | {
|
---|
[f2ec8c8] | 560 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 561 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
| 562 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
---|
[0ee4322] | 563 |
|
---|
| 564 | /*
|
---|
[cf95bc0] | 565 | * Lookup the respective TMPFS node.
|
---|
[0ee4322] | 566 | */
|
---|
| 567 | link_t *hlp;
|
---|
[8d049ee0] | 568 | unsigned long key[] = {
|
---|
[cf95bc0] | 569 | [NODES_KEY_INDEX] = index,
|
---|
| 570 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 571 | };
|
---|
[cf95bc0] | 572 | hlp = hash_table_find(&nodes, key);
|
---|
[0ee4322] | 573 | if (!hlp) {
|
---|
| 574 | ipc_answer_0(rid, ENOENT);
|
---|
| 575 | return;
|
---|
| 576 | }
|
---|
[cf95bc0] | 577 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 578 | nh_link);
|
---|
[0ee4322] | 579 |
|
---|
[cf95bc0] | 580 | if (size == nodep->size) {
|
---|
[0ee4322] | 581 | ipc_answer_0(rid, EOK);
|
---|
| 582 | return;
|
---|
| 583 | }
|
---|
| 584 |
|
---|
[cf95bc0] | 585 | void *newdata = realloc(nodep->data, size);
|
---|
[0ee4322] | 586 | if (!newdata) {
|
---|
| 587 | ipc_answer_0(rid, ENOMEM);
|
---|
| 588 | return;
|
---|
| 589 | }
|
---|
[cf95bc0] | 590 | if (size > nodep->size) {
|
---|
| 591 | size_t delta = size - nodep->size;
|
---|
| 592 | memset(newdata + nodep->size, 0, delta);
|
---|
[0ee4322] | 593 | }
|
---|
[cf95bc0] | 594 | nodep->size = size;
|
---|
| 595 | nodep->data = newdata;
|
---|
[0ee4322] | 596 | ipc_answer_0(rid, EOK);
|
---|
| 597 | }
|
---|
| 598 |
|
---|
[fdb7795] | 599 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
[f17667a] | 600 | {
|
---|
[f2ec8c8] | 601 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
| 602 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
[45f244b] | 603 | int rc;
|
---|
[f17667a] | 604 |
|
---|
| 605 | link_t *hlp;
|
---|
[8d049ee0] | 606 | unsigned long key[] = {
|
---|
[cf95bc0] | 607 | [NODES_KEY_INDEX] = index,
|
---|
| 608 | [NODES_KEY_DEV] = dev_handle
|
---|
[8d049ee0] | 609 | };
|
---|
[cf95bc0] | 610 | hlp = hash_table_find(&nodes, key);
|
---|
[f17667a] | 611 | if (!hlp) {
|
---|
| 612 | ipc_answer_0(rid, ENOENT);
|
---|
| 613 | return;
|
---|
| 614 | }
|
---|
[cf95bc0] | 615 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
| 616 | nh_link);
|
---|
| 617 | rc = tmpfs_destroy_node(FS_NODE(nodep));
|
---|
[45f244b] | 618 | ipc_answer_0(rid, rc);
|
---|
[f17667a] | 619 | }
|
---|
| 620 |
|
---|
[d5cdffe] | 621 | /**
|
---|
| 622 | * @}
|
---|
| 623 | */
|
---|