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