Rev 4306 | Rev 4370 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2645 | jermar | 1 | /* |
| 2685 | jermar | 2 | * Copyright (c) 2008 Jakub Jermar |
| 2645 | jermar | 3 | * All rights reserved. |
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 13 | * documentation and/or other materials provided with the distribution. |
||
| 14 | * - The name of the author may not be used to endorse or promote products |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 29 | /** @addtogroup fs |
||
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @file tmpfs_ops.c |
||
| 35 | * @brief Implementation of VFS operations for the TMPFS file system |
||
| 36 | * server. |
||
| 37 | */ |
||
| 38 | |||
| 39 | #include "tmpfs.h" |
||
| 40 | #include "../../vfs/vfs.h" |
||
| 41 | #include <ipc/ipc.h> |
||
| 42 | #include <async.h> |
||
| 43 | #include <errno.h> |
||
| 2655 | jermar | 44 | #include <atomic.h> |
| 45 | #include <stdlib.h> |
||
| 46 | #include <string.h> |
||
| 47 | #include <stdio.h> |
||
| 2699 | jermar | 48 | #include <assert.h> |
| 2658 | jermar | 49 | #include <sys/types.h> |
| 50 | #include <libadt/hash_table.h> |
||
| 51 | #include <as.h> |
||
| 2747 | jermar | 52 | #include <libfs.h> |
| 2645 | jermar | 53 | |
| 2658 | jermar | 54 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 55 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
||
| 56 | |||
| 57 | #define DENTRIES_BUCKETS 256 |
||
| 58 | |||
| 2760 | jermar | 59 | #define NAMES_BUCKETS 4 |
| 60 | |||
| 4306 | jermar | 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; |
||
| 2730 | jermar | 65 | |
| 2747 | jermar | 66 | /* |
| 67 | * Implementation of the libfs interface. |
||
| 68 | */ |
||
| 69 | |||
| 2742 | jermar | 70 | /* Forward declarations of static functions. */ |
| 4357 | jermar | 71 | static fs_node_t *tmpfs_match(fs_node_t *, const char *); |
| 72 | static fs_node_t *tmpfs_node_get(dev_handle_t, fs_index_t); |
||
| 73 | static void tmpfs_node_put(fs_node_t *); |
||
| 74 | static fs_node_t *tmpfs_create_node(dev_handle_t, int); |
||
| 75 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *); |
||
| 76 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *); |
||
| 77 | static int tmpfs_destroy_node(fs_node_t *); |
||
| 2742 | jermar | 78 | |
| 2747 | jermar | 79 | /* Implementation of helper functions. */ |
| 4357 | jermar | 80 | static fs_index_t tmpfs_index_get(fs_node_t *fn) |
| 2747 | jermar | 81 | { |
| 4357 | jermar | 82 | return TMPFS_NODE(fn)->index; |
| 2747 | jermar | 83 | } |
| 84 | |||
| 4357 | jermar | 85 | static size_t tmpfs_size_get(fs_node_t *fn) |
| 2747 | jermar | 86 | { |
| 4357 | jermar | 87 | return TMPFS_NODE(fn)->size; |
| 2747 | jermar | 88 | } |
| 89 | |||
| 4357 | jermar | 90 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn) |
| 2747 | jermar | 91 | { |
| 4357 | jermar | 92 | return TMPFS_NODE(fn)->lnkcnt; |
| 2747 | jermar | 93 | } |
| 94 | |||
| 4357 | jermar | 95 | static bool tmpfs_has_children(fs_node_t *fn) |
| 2747 | jermar | 96 | { |
| 4357 | jermar | 97 | return TMPFS_NODE(fn)->child != NULL; |
| 2747 | jermar | 98 | } |
| 99 | |||
| 4357 | jermar | 100 | static fs_node_t *tmpfs_root_get(dev_handle_t dev_handle) |
| 2747 | jermar | 101 | { |
| 4306 | jermar | 102 | return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT); |
| 2747 | jermar | 103 | } |
| 104 | |||
| 105 | static char tmpfs_plb_get_char(unsigned pos) |
||
| 106 | { |
||
| 107 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
||
| 108 | } |
||
| 109 | |||
| 4357 | jermar | 110 | static bool tmpfs_is_directory(fs_node_t *fn) |
| 2747 | jermar | 111 | { |
| 4357 | jermar | 112 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY; |
| 2747 | jermar | 113 | } |
| 114 | |||
| 4357 | jermar | 115 | static bool tmpfs_is_file(fs_node_t *fn) |
| 2747 | jermar | 116 | { |
| 4357 | jermar | 117 | return TMPFS_NODE(fn)->type == TMPFS_FILE; |
| 2747 | jermar | 118 | } |
| 119 | |||
| 120 | /** libfs operations */ |
||
| 121 | libfs_ops_t tmpfs_libfs_ops = { |
||
| 122 | .match = tmpfs_match, |
||
| 2763 | jermar | 123 | .node_get = tmpfs_node_get, |
| 2852 | jermar | 124 | .node_put = tmpfs_node_put, |
| 2747 | jermar | 125 | .create = tmpfs_create_node, |
| 126 | .destroy = tmpfs_destroy_node, |
||
| 127 | .link = tmpfs_link_node, |
||
| 128 | .unlink = tmpfs_unlink_node, |
||
| 129 | .index_get = tmpfs_index_get, |
||
| 130 | .size_get = tmpfs_size_get, |
||
| 131 | .lnkcnt_get = tmpfs_lnkcnt_get, |
||
| 2791 | jermar | 132 | .has_children = tmpfs_has_children, |
| 2747 | jermar | 133 | .root_get = tmpfs_root_get, |
| 134 | .plb_get_char = tmpfs_plb_get_char, |
||
| 135 | .is_directory = tmpfs_is_directory, |
||
| 136 | .is_file = tmpfs_is_file |
||
| 137 | }; |
||
| 138 | |||
| 2742 | jermar | 139 | /** Hash table of all directory entries. */ |
| 2658 | jermar | 140 | hash_table_t dentries; |
| 141 | |||
| 4306 | jermar | 142 | #define DENTRIES_KEY_INDEX 0 |
| 143 | #define DENTRIES_KEY_DEV 1 |
||
| 144 | |||
| 2760 | jermar | 145 | /* Implementation of hash table interface for the dentries hash table. */ |
| 4306 | jermar | 146 | static hash_index_t dentries_hash(unsigned long key[]) |
| 2658 | jermar | 147 | { |
| 4306 | jermar | 148 | return key[DENTRIES_KEY_INDEX] % DENTRIES_BUCKETS; |
| 2658 | jermar | 149 | } |
| 150 | |||
| 4306 | jermar | 151 | static int dentries_compare(unsigned long key[], hash_count_t keys, |
| 2658 | jermar | 152 | link_t *item) |
| 153 | { |
||
| 154 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
||
| 155 | dh_link); |
||
| 4306 | jermar | 156 | return (dentry->index == key[DENTRIES_KEY_INDEX] && |
| 157 | dentry->dev_handle == key[DENTRIES_KEY_DEV]); |
||
| 2658 | jermar | 158 | } |
| 159 | |||
| 160 | static void dentries_remove_callback(link_t *item) |
||
| 161 | { |
||
| 162 | } |
||
| 163 | |||
| 164 | /** TMPFS dentries hash table operations. */ |
||
| 165 | hash_table_operations_t dentries_ops = { |
||
| 166 | .hash = dentries_hash, |
||
| 167 | .compare = dentries_compare, |
||
| 168 | .remove_callback = dentries_remove_callback |
||
| 169 | }; |
||
| 170 | |||
| 2760 | jermar | 171 | typedef struct { |
| 172 | char *name; |
||
| 173 | tmpfs_dentry_t *parent; |
||
| 174 | link_t link; |
||
| 175 | } tmpfs_name_t; |
||
| 176 | |||
| 177 | /* Implementation of hash table interface for the names hash table. */ |
||
| 178 | static hash_index_t names_hash(unsigned long *key) |
||
| 2655 | jermar | 179 | { |
| 2760 | jermar | 180 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
| 181 | return dentry->index % NAMES_BUCKETS; |
||
| 182 | } |
||
| 183 | |||
| 184 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
||
| 185 | { |
||
| 186 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
||
| 187 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
||
| 188 | link); |
||
| 189 | return dentry == namep->parent; |
||
| 190 | } |
||
| 191 | |||
| 192 | static void names_remove_callback(link_t *item) |
||
| 193 | { |
||
| 194 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
||
| 195 | link); |
||
| 196 | free(namep->name); |
||
| 197 | free(namep); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** TMPFS node names hash table operations. */ |
||
| 201 | static hash_table_operations_t names_ops = { |
||
| 202 | .hash = names_hash, |
||
| 203 | .compare = names_compare, |
||
| 204 | .remove_callback = names_remove_callback |
||
| 205 | }; |
||
| 206 | |||
| 207 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
||
| 208 | { |
||
| 209 | namep->name = NULL; |
||
| 210 | namep->parent = NULL; |
||
| 211 | link_initialize(&namep->link); |
||
| 212 | } |
||
| 213 | |||
| 214 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
||
| 215 | { |
||
| 4357 | jermar | 216 | dentry->bp = NULL; |
| 2655 | jermar | 217 | dentry->index = 0; |
| 4306 | jermar | 218 | dentry->dev_handle = 0; |
| 2655 | jermar | 219 | dentry->sibling = NULL; |
| 220 | dentry->child = NULL; |
||
| 221 | dentry->type = TMPFS_NONE; |
||
| 2756 | jermar | 222 | dentry->lnkcnt = 0; |
| 2655 | jermar | 223 | dentry->size = 0; |
| 224 | dentry->data = NULL; |
||
| 2658 | jermar | 225 | link_initialize(&dentry->dh_link); |
| 2760 | jermar | 226 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
| 227 | &names_ops); |
||
| 2655 | jermar | 228 | } |
| 229 | |||
| 4306 | jermar | 230 | bool tmpfs_init(void) |
| 2655 | jermar | 231 | { |
| 4306 | jermar | 232 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 2, &dentries_ops)) |
| 2658 | jermar | 233 | return false; |
| 4306 | jermar | 234 | |
| 235 | return true; |
||
| 236 | } |
||
| 237 | |||
| 238 | static bool tmpfs_instance_init(dev_handle_t dev_handle) |
||
| 239 | { |
||
| 4357 | jermar | 240 | fs_node_t *rfn; |
| 4306 | jermar | 241 | |
| 4357 | jermar | 242 | rfn = tmpfs_create_node(dev_handle, L_DIRECTORY); |
| 243 | if (!rfn) |
||
| 2760 | jermar | 244 | return false; |
| 4357 | jermar | 245 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */ |
| 2760 | jermar | 246 | return true; |
| 2655 | jermar | 247 | } |
| 248 | |||
| 2645 | jermar | 249 | /** Compare one component of path to a directory entry. |
| 250 | * |
||
| 2791 | jermar | 251 | * @param parentp Pointer to node from which we descended. |
| 252 | * @param childp Pointer to node to compare the path component with. |
||
| 2705 | jermar | 253 | * @param component Array of characters holding component name. |
| 2645 | jermar | 254 | * |
| 2705 | jermar | 255 | * @return True on match, false otherwise. |
| 2645 | jermar | 256 | */ |
| 2791 | jermar | 257 | static bool |
| 258 | tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
||
| 259 | const char *component) |
||
| 2645 | jermar | 260 | { |
| 2760 | jermar | 261 | unsigned long key = (unsigned long) parentp; |
| 262 | link_t *hlp = hash_table_find(&childp->names, &key); |
||
| 263 | assert(hlp); |
||
| 264 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
||
| 4264 | svoboda | 265 | return !str_cmp(namep->name, component); |
| 2705 | jermar | 266 | } |
| 2655 | jermar | 267 | |
| 4357 | jermar | 268 | fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component) |
| 2791 | jermar | 269 | { |
| 4357 | jermar | 270 | tmpfs_dentry_t *parentp = TMPFS_NODE(pfn); |
| 2791 | jermar | 271 | tmpfs_dentry_t *childp = parentp->child; |
| 272 | |||
| 273 | while (childp && !tmpfs_match_one(parentp, childp, component)) |
||
| 274 | childp = childp->sibling; |
||
| 275 | |||
| 4357 | jermar | 276 | return FS_NODE(childp); |
| 2791 | jermar | 277 | } |
| 278 | |||
| 4357 | jermar | 279 | fs_node_t *tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
| 2763 | jermar | 280 | { |
| 4306 | jermar | 281 | unsigned long key[] = { |
| 282 | [DENTRIES_KEY_INDEX] = index, |
||
| 283 | [DENTRIES_KEY_DEV] = dev_handle |
||
| 284 | }; |
||
| 285 | link_t *lnk = hash_table_find(&dentries, key); |
||
| 2763 | jermar | 286 | if (!lnk) |
| 287 | return NULL; |
||
| 4357 | jermar | 288 | return FS_NODE(hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link)); |
| 2763 | jermar | 289 | } |
| 290 | |||
| 4357 | jermar | 291 | void tmpfs_node_put(fs_node_t *fn) |
| 2852 | jermar | 292 | { |
| 293 | /* nothing to do */ |
||
| 294 | } |
||
| 295 | |||
| 4357 | jermar | 296 | fs_node_t *tmpfs_create_node(dev_handle_t dev_handle, int lflag) |
| 2705 | jermar | 297 | { |
| 2707 | jermar | 298 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
| 299 | |||
| 4357 | jermar | 300 | fs_node_t *fn = malloc(sizeof(fs_node_t)); |
| 301 | if (!fn) |
||
| 302 | return NULL; |
||
| 303 | |||
| 2707 | jermar | 304 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
| 4357 | jermar | 305 | if (!node) { |
| 306 | free(fn); |
||
| 2730 | jermar | 307 | return NULL; |
| 4357 | jermar | 308 | } |
| 2760 | jermar | 309 | if (!tmpfs_dentry_initialize(node)) { |
| 4357 | jermar | 310 | free(fn); |
| 2760 | jermar | 311 | free(node); |
| 312 | return NULL; |
||
| 313 | } |
||
| 4357 | jermar | 314 | fn->data = node; |
| 315 | node->bp = fn; /* establish the back pointer */ |
||
| 4306 | jermar | 316 | if (!tmpfs_root_get(dev_handle)) |
| 317 | node->index = TMPFS_SOME_ROOT; |
||
| 318 | else |
||
| 319 | node->index = tmpfs_next_index++; |
||
| 320 | node->dev_handle = dev_handle; |
||
| 2707 | jermar | 321 | if (lflag & L_DIRECTORY) |
| 322 | node->type = TMPFS_DIRECTORY; |
||
| 323 | else |
||
| 324 | node->type = TMPFS_FILE; |
||
| 325 | |||
| 2742 | jermar | 326 | /* Insert the new node into the dentry hash table. */ |
| 4306 | jermar | 327 | unsigned long key[] = { |
| 328 | [DENTRIES_KEY_INDEX] = node->index, |
||
| 329 | [DENTRIES_KEY_DEV] = node->dev_handle |
||
| 330 | }; |
||
| 331 | hash_table_insert(&dentries, key, &node->dh_link); |
||
| 4357 | jermar | 332 | return fn; |
| 2742 | jermar | 333 | } |
| 334 | |||
| 4357 | jermar | 335 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
| 2742 | jermar | 336 | { |
| 4357 | jermar | 337 | tmpfs_dentry_t *parentp = TMPFS_NODE(pfn); |
| 338 | tmpfs_dentry_t *childp = TMPFS_NODE(cfn); |
||
| 2742 | jermar | 339 | |
| 340 | assert(parentp->type == TMPFS_DIRECTORY); |
||
| 341 | |||
| 2760 | jermar | 342 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
| 343 | if (!namep) |
||
| 3625 | jermar | 344 | return ENOMEM; |
| 2760 | jermar | 345 | tmpfs_name_initialize(namep); |
| 4264 | svoboda | 346 | size_t size = str_size(nm); |
| 347 | namep->name = malloc(size + 1); |
||
| 2760 | jermar | 348 | if (!namep->name) { |
| 349 | free(namep); |
||
| 3625 | jermar | 350 | return ENOMEM; |
| 2760 | jermar | 351 | } |
| 4268 | svoboda | 352 | str_cpy(namep->name, size + 1, nm); |
| 2760 | jermar | 353 | namep->parent = parentp; |
| 2756 | jermar | 354 | |
| 355 | childp->lnkcnt++; |
||
| 2742 | jermar | 356 | |
| 2760 | jermar | 357 | unsigned long key = (unsigned long) parentp; |
| 358 | hash_table_insert(&childp->names, &key, &namep->link); |
||
| 359 | |||
| 2707 | jermar | 360 | /* Insert the new node into the namespace. */ |
| 2742 | jermar | 361 | if (parentp->child) { |
| 362 | tmpfs_dentry_t *tmp = parentp->child; |
||
| 2707 | jermar | 363 | while (tmp->sibling) |
| 364 | tmp = tmp->sibling; |
||
| 2742 | jermar | 365 | tmp->sibling = childp; |
| 2707 | jermar | 366 | } else { |
| 2742 | jermar | 367 | parentp->child = childp; |
| 2707 | jermar | 368 | } |
| 369 | |||
| 3625 | jermar | 370 | return EOK; |
| 2705 | jermar | 371 | } |
| 2655 | jermar | 372 | |
| 4357 | jermar | 373 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn) |
| 2705 | jermar | 374 | { |
| 4357 | jermar | 375 | tmpfs_dentry_t *parentp = TMPFS_NODE(pfn); |
| 376 | tmpfs_dentry_t *childp = TMPFS_NODE(cfn); |
||
| 2733 | jermar | 377 | |
| 2758 | jermar | 378 | if (!parentp) |
| 379 | return EBUSY; |
||
| 380 | |||
| 381 | if (childp->child) |
||
| 2733 | jermar | 382 | return ENOTEMPTY; |
| 383 | |||
| 2758 | jermar | 384 | if (parentp->child == childp) { |
| 385 | parentp->child = childp->sibling; |
||
| 2733 | jermar | 386 | } else { |
| 387 | /* TODO: consider doubly linked list for organizing siblings. */ |
||
| 2758 | jermar | 388 | tmpfs_dentry_t *tmp = parentp->child; |
| 389 | while (tmp->sibling != childp) |
||
| 2733 | jermar | 390 | tmp = tmp->sibling; |
| 2758 | jermar | 391 | tmp->sibling = childp->sibling; |
| 2733 | jermar | 392 | } |
| 2758 | jermar | 393 | childp->sibling = NULL; |
| 2733 | jermar | 394 | |
| 2760 | jermar | 395 | unsigned long key = (unsigned long) parentp; |
| 396 | hash_table_remove(&childp->names, &key, 1); |
||
| 2742 | jermar | 397 | |
| 2758 | jermar | 398 | childp->lnkcnt--; |
| 2756 | jermar | 399 | |
| 2733 | jermar | 400 | return EOK; |
| 2645 | jermar | 401 | } |
| 402 | |||
| 4357 | jermar | 403 | int tmpfs_destroy_node(fs_node_t *fn) |
| 2742 | jermar | 404 | { |
| 4357 | jermar | 405 | tmpfs_dentry_t *dentry = TMPFS_NODE(fn); |
| 2742 | jermar | 406 | |
| 2756 | jermar | 407 | assert(!dentry->lnkcnt); |
| 2742 | jermar | 408 | assert(!dentry->child); |
| 409 | assert(!dentry->sibling); |
||
| 410 | |||
| 4306 | jermar | 411 | unsigned long key[] = { |
| 412 | [DENTRIES_KEY_INDEX] = dentry->index, |
||
| 413 | [DENTRIES_KEY_DEV] = dentry->dev_handle |
||
| 414 | }; |
||
| 415 | hash_table_remove(&dentries, key, 2); |
||
| 2742 | jermar | 416 | |
| 2760 | jermar | 417 | hash_table_destroy(&dentry->names); |
| 418 | |||
| 2742 | jermar | 419 | if (dentry->type == TMPFS_FILE) |
| 420 | free(dentry->data); |
||
| 4357 | jermar | 421 | free(dentry->bp); |
| 2742 | jermar | 422 | free(dentry); |
| 2858 | jermar | 423 | return EOK; |
| 2742 | jermar | 424 | } |
| 425 | |||
| 3109 | jermar | 426 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
| 427 | { |
||
| 428 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
| 429 | |||
| 4305 | jermar | 430 | /* accept the mount options */ |
| 431 | ipc_callid_t callid; |
||
| 432 | size_t size; |
||
| 433 | if (!ipc_data_write_receive(&callid, &size)) { |
||
| 434 | ipc_answer_0(callid, EINVAL); |
||
| 435 | ipc_answer_0(rid, EINVAL); |
||
| 436 | return; |
||
| 437 | } |
||
| 438 | char *opts = malloc(size + 1); |
||
| 439 | if (!opts) { |
||
| 440 | ipc_answer_0(callid, ENOMEM); |
||
| 441 | ipc_answer_0(rid, ENOMEM); |
||
| 442 | return; |
||
| 443 | } |
||
| 444 | ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
||
| 445 | if (retval != EOK) { |
||
| 446 | ipc_answer_0(rid, retval); |
||
| 447 | free(opts); |
||
| 448 | return; |
||
| 449 | } |
||
| 450 | opts[size] = '\0'; |
||
| 451 | |||
| 4306 | jermar | 452 | /* Initialize TMPFS instance. */ |
| 453 | if (!tmpfs_instance_init(dev_handle)) { |
||
| 3109 | jermar | 454 | ipc_answer_0(rid, ENOMEM); |
| 455 | return; |
||
| 456 | } |
||
| 457 | |||
| 4357 | jermar | 458 | tmpfs_dentry_t *root = TMPFS_NODE(tmpfs_root_get(dev_handle)); |
| 4305 | jermar | 459 | if (str_cmp(opts, "restore") == 0) { |
| 3109 | jermar | 460 | if (tmpfs_restore(dev_handle)) |
| 3352 | jermar | 461 | ipc_answer_3(rid, EOK, root->index, root->size, |
| 462 | root->lnkcnt); |
||
| 3109 | jermar | 463 | else |
| 464 | ipc_answer_0(rid, ELIMIT); |
||
| 465 | } else { |
||
| 3352 | jermar | 466 | ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt); |
| 3109 | jermar | 467 | } |
| 468 | } |
||
| 469 | |||
| 2958 | jermar | 470 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
| 471 | { |
||
| 3109 | jermar | 472 | dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
| 473 | fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request); |
||
| 474 | fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
||
| 475 | dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
||
| 3087 | decky | 476 | |
| 3109 | jermar | 477 | ipc_answer_0(rid, ENOTSUP); |
| 2958 | jermar | 478 | } |
| 479 | |||
| 2645 | jermar | 480 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
| 481 | { |
||
| 2747 | jermar | 482 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
| 2645 | jermar | 483 | } |
| 484 | |||
| 2658 | jermar | 485 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
| 486 | { |
||
| 2770 | jermar | 487 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 488 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
| 489 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
| 2658 | jermar | 490 | |
| 491 | /* |
||
| 492 | * Lookup the respective dentry. |
||
| 493 | */ |
||
| 494 | link_t *hlp; |
||
| 4306 | jermar | 495 | unsigned long key[] = { |
| 496 | [DENTRIES_KEY_INDEX] = index, |
||
| 497 | [DENTRIES_KEY_DEV] = dev_handle, |
||
| 498 | }; |
||
| 499 | hlp = hash_table_find(&dentries, key); |
||
| 2658 | jermar | 500 | if (!hlp) { |
| 501 | ipc_answer_0(rid, ENOENT); |
||
| 502 | return; |
||
| 503 | } |
||
| 504 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
| 505 | dh_link); |
||
| 506 | |||
| 507 | /* |
||
| 2663 | jermar | 508 | * Receive the read request. |
| 2658 | jermar | 509 | */ |
| 510 | ipc_callid_t callid; |
||
| 4264 | svoboda | 511 | size_t size; |
| 512 | if (!ipc_data_read_receive(&callid, &size)) { |
||
| 2658 | jermar | 513 | ipc_answer_0(callid, EINVAL); |
| 514 | ipc_answer_0(rid, EINVAL); |
||
| 515 | return; |
||
| 516 | } |
||
| 517 | |||
| 2699 | jermar | 518 | size_t bytes; |
| 519 | if (dentry->type == TMPFS_FILE) { |
||
| 4264 | svoboda | 520 | bytes = max(0, min(dentry->size - pos, size)); |
| 2699 | jermar | 521 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
| 522 | bytes); |
||
| 523 | } else { |
||
| 524 | int i; |
||
| 2739 | jermar | 525 | tmpfs_dentry_t *cur; |
| 2699 | jermar | 526 | |
| 527 | assert(dentry->type == TMPFS_DIRECTORY); |
||
| 528 | |||
| 529 | /* |
||
| 530 | * Yes, we really use O(n) algorithm here. |
||
| 531 | * If it bothers someone, it could be fixed by introducing a |
||
| 532 | * hash table. |
||
| 533 | */ |
||
| 534 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
||
| 535 | cur = cur->sibling) |
||
| 536 | ; |
||
| 2664 | jermar | 537 | |
| 2699 | jermar | 538 | if (!cur) { |
| 539 | ipc_answer_0(callid, ENOENT); |
||
| 540 | ipc_answer_1(rid, ENOENT, 0); |
||
| 541 | return; |
||
| 542 | } |
||
| 543 | |||
| 2760 | jermar | 544 | unsigned long key = (unsigned long) dentry; |
| 545 | link_t *hlp = hash_table_find(&cur->names, &key); |
||
| 546 | assert(hlp); |
||
| 547 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
||
| 548 | link); |
||
| 549 | |||
| 550 | (void) ipc_data_read_finalize(callid, namep->name, |
||
| 4264 | svoboda | 551 | str_size(namep->name) + 1); |
| 2699 | jermar | 552 | bytes = 1; |
| 553 | } |
||
| 554 | |||
| 2664 | jermar | 555 | /* |
| 556 | * Answer the VFS_READ call. |
||
| 557 | */ |
||
| 558 | ipc_answer_1(rid, EOK, bytes); |
||
| 2658 | jermar | 559 | } |
| 560 | |||
| 2666 | jermar | 561 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
| 562 | { |
||
| 2770 | jermar | 563 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 564 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
| 565 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
| 2666 | jermar | 566 | |
| 567 | /* |
||
| 568 | * Lookup the respective dentry. |
||
| 569 | */ |
||
| 570 | link_t *hlp; |
||
| 4306 | jermar | 571 | unsigned long key[] = { |
| 572 | [DENTRIES_KEY_INDEX] = index, |
||
| 573 | [DENTRIES_KEY_DEV] = dev_handle |
||
| 574 | }; |
||
| 575 | hlp = hash_table_find(&dentries, key); |
||
| 2666 | jermar | 576 | if (!hlp) { |
| 577 | ipc_answer_0(rid, ENOENT); |
||
| 578 | return; |
||
| 579 | } |
||
| 580 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
| 581 | dh_link); |
||
| 582 | |||
| 583 | /* |
||
| 584 | * Receive the write request. |
||
| 585 | */ |
||
| 586 | ipc_callid_t callid; |
||
| 4264 | svoboda | 587 | size_t size; |
| 588 | if (!ipc_data_write_receive(&callid, &size)) { |
||
| 2666 | jermar | 589 | ipc_answer_0(callid, EINVAL); |
| 590 | ipc_answer_0(rid, EINVAL); |
||
| 591 | return; |
||
| 592 | } |
||
| 593 | |||
| 594 | /* |
||
| 2667 | jermar | 595 | * Check whether the file needs to grow. |
| 596 | */ |
||
| 4264 | svoboda | 597 | if (pos + size <= dentry->size) { |
| 2667 | jermar | 598 | /* The file size is not changing. */ |
| 4264 | svoboda | 599 | (void) ipc_data_write_finalize(callid, dentry->data + pos, size); |
| 600 | ipc_answer_2(rid, EOK, size, dentry->size); |
||
| 2667 | jermar | 601 | return; |
| 602 | } |
||
| 4264 | svoboda | 603 | size_t delta = (pos + size) - dentry->size; |
| 2667 | jermar | 604 | /* |
| 2666 | jermar | 605 | * At this point, we are deliberately extremely straightforward and |
| 2667 | jermar | 606 | * simply realloc the contents of the file on every write that grows the |
| 607 | * file. In the end, the situation might not be as bad as it may look: |
||
| 608 | * our heap allocator can save us and just grow the block whenever |
||
| 609 | * possible. |
||
| 2666 | jermar | 610 | */ |
| 2667 | jermar | 611 | void *newdata = realloc(dentry->data, dentry->size + delta); |
| 2666 | jermar | 612 | if (!newdata) { |
| 613 | ipc_answer_0(callid, ENOMEM); |
||
| 2710 | jermar | 614 | ipc_answer_2(rid, EOK, 0, dentry->size); |
| 2666 | jermar | 615 | return; |
| 616 | } |
||
| 2693 | jermar | 617 | /* Clear any newly allocated memory in order to emulate gaps. */ |
| 2686 | jermar | 618 | memset(newdata + dentry->size, 0, delta); |
| 2667 | jermar | 619 | dentry->size += delta; |
| 2666 | jermar | 620 | dentry->data = newdata; |
| 4264 | svoboda | 621 | (void) ipc_data_write_finalize(callid, dentry->data + pos, size); |
| 622 | ipc_answer_2(rid, EOK, size, dentry->size); |
||
| 2666 | jermar | 623 | } |
| 624 | |||
| 2693 | jermar | 625 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
| 626 | { |
||
| 2770 | jermar | 627 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 628 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
| 629 | size_t size = (off_t)IPC_GET_ARG3(*request); |
||
| 2693 | jermar | 630 | |
| 631 | /* |
||
| 632 | * Lookup the respective dentry. |
||
| 633 | */ |
||
| 634 | link_t *hlp; |
||
| 4306 | jermar | 635 | unsigned long key[] = { |
| 636 | [DENTRIES_KEY_INDEX] = index, |
||
| 637 | [DENTRIES_KEY_DEV] = dev_handle |
||
| 638 | }; |
||
| 639 | hlp = hash_table_find(&dentries, key); |
||
| 2693 | jermar | 640 | if (!hlp) { |
| 641 | ipc_answer_0(rid, ENOENT); |
||
| 642 | return; |
||
| 643 | } |
||
| 644 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
| 645 | dh_link); |
||
| 646 | |||
| 647 | if (size == dentry->size) { |
||
| 648 | ipc_answer_0(rid, EOK); |
||
| 649 | return; |
||
| 650 | } |
||
| 651 | |||
| 652 | void *newdata = realloc(dentry->data, size); |
||
| 653 | if (!newdata) { |
||
| 654 | ipc_answer_0(rid, ENOMEM); |
||
| 655 | return; |
||
| 656 | } |
||
| 657 | if (size > dentry->size) { |
||
| 658 | size_t delta = size - dentry->size; |
||
| 659 | memset(newdata + dentry->size, 0, delta); |
||
| 660 | } |
||
| 661 | dentry->size = size; |
||
| 662 | dentry->data = newdata; |
||
| 663 | ipc_answer_0(rid, EOK); |
||
| 664 | } |
||
| 665 | |||
| 2742 | jermar | 666 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
| 2731 | jermar | 667 | { |
| 2770 | jermar | 668 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
| 669 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
| 2858 | jermar | 670 | int rc; |
| 2731 | jermar | 671 | |
| 672 | link_t *hlp; |
||
| 4306 | jermar | 673 | unsigned long key[] = { |
| 674 | [DENTRIES_KEY_INDEX] = index, |
||
| 675 | [DENTRIES_KEY_DEV] = dev_handle |
||
| 676 | }; |
||
| 677 | hlp = hash_table_find(&dentries, key); |
||
| 2731 | jermar | 678 | if (!hlp) { |
| 679 | ipc_answer_0(rid, ENOENT); |
||
| 680 | return; |
||
| 681 | } |
||
| 682 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
| 683 | dh_link); |
||
| 4357 | jermar | 684 | rc = tmpfs_destroy_node(FS_NODE(dentry)); |
| 2858 | jermar | 685 | ipc_answer_0(rid, rc); |
| 2731 | jermar | 686 | } |
| 687 | |||
| 2645 | jermar | 688 | /** |
| 689 | * @} |
||
| 690 | */ |