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