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