Rev 3087 | Rev 3103 | 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> |
3087 | decky | 53 | #include <ipc/services.h> |
54 | #include <ipc/devmap.h> |
||
55 | #include <sys/mman.h> |
||
56 | #include <byteorder.h> |
||
2645 | jermar | 57 | |
2658 | jermar | 58 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
59 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
||
60 | |||
61 | #define DENTRIES_BUCKETS 256 |
||
62 | |||
2760 | jermar | 63 | #define NAMES_BUCKETS 4 |
64 | |||
3087 | decky | 65 | #define BLOCK_SIZE 1024 // FIXME |
66 | #define RD_BASE 1024 // FIXME |
||
67 | #define RD_READ_BLOCK (RD_BASE + 1) |
||
68 | |||
2747 | jermar | 69 | /* |
70 | * For now, we don't distinguish between different dev_handles/instances. All |
||
71 | * requests resolve to the only instance, rooted in the following variable. |
||
72 | */ |
||
73 | static tmpfs_dentry_t *root; |
||
2730 | jermar | 74 | |
2747 | jermar | 75 | /* |
76 | * Implementation of the libfs interface. |
||
77 | */ |
||
78 | |||
2742 | jermar | 79 | /* Forward declarations of static functions. */ |
2791 | jermar | 80 | static void *tmpfs_match(void *, const char *); |
2863 | jermar | 81 | static void *tmpfs_node_get(dev_handle_t, fs_index_t); |
2852 | jermar | 82 | static void tmpfs_node_put(void *); |
2747 | jermar | 83 | static void *tmpfs_create_node(int); |
84 | static bool tmpfs_link_node(void *, void *, const char *); |
||
2758 | jermar | 85 | static int tmpfs_unlink_node(void *, void *); |
2858 | jermar | 86 | static int tmpfs_destroy_node(void *); |
2742 | jermar | 87 | |
2747 | jermar | 88 | /* Implementation of helper functions. */ |
2770 | jermar | 89 | static fs_index_t tmpfs_index_get(void *nodep) |
2747 | jermar | 90 | { |
91 | return ((tmpfs_dentry_t *) nodep)->index; |
||
92 | } |
||
93 | |||
2770 | jermar | 94 | static size_t tmpfs_size_get(void *nodep) |
2747 | jermar | 95 | { |
96 | return ((tmpfs_dentry_t *) nodep)->size; |
||
97 | } |
||
98 | |||
99 | static unsigned tmpfs_lnkcnt_get(void *nodep) |
||
100 | { |
||
2756 | jermar | 101 | return ((tmpfs_dentry_t *) nodep)->lnkcnt; |
2747 | jermar | 102 | } |
103 | |||
2791 | jermar | 104 | static bool tmpfs_has_children(void *nodep) |
2747 | jermar | 105 | { |
2791 | jermar | 106 | return ((tmpfs_dentry_t *) nodep)->child != NULL; |
2747 | jermar | 107 | } |
108 | |||
2844 | jermar | 109 | static void *tmpfs_root_get(dev_handle_t dev_handle) |
2747 | jermar | 110 | { |
111 | return root; |
||
112 | } |
||
113 | |||
114 | static char tmpfs_plb_get_char(unsigned pos) |
||
115 | { |
||
116 | return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
||
117 | } |
||
118 | |||
119 | static bool tmpfs_is_directory(void *nodep) |
||
120 | { |
||
121 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY; |
||
122 | } |
||
123 | |||
124 | static bool tmpfs_is_file(void *nodep) |
||
125 | { |
||
126 | return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE; |
||
127 | } |
||
128 | |||
129 | /** libfs operations */ |
||
130 | libfs_ops_t tmpfs_libfs_ops = { |
||
131 | .match = tmpfs_match, |
||
2763 | jermar | 132 | .node_get = tmpfs_node_get, |
2852 | jermar | 133 | .node_put = tmpfs_node_put, |
2747 | jermar | 134 | .create = tmpfs_create_node, |
135 | .destroy = tmpfs_destroy_node, |
||
136 | .link = tmpfs_link_node, |
||
137 | .unlink = tmpfs_unlink_node, |
||
138 | .index_get = tmpfs_index_get, |
||
139 | .size_get = tmpfs_size_get, |
||
140 | .lnkcnt_get = tmpfs_lnkcnt_get, |
||
2791 | jermar | 141 | .has_children = tmpfs_has_children, |
2747 | jermar | 142 | .root_get = tmpfs_root_get, |
143 | .plb_get_char = tmpfs_plb_get_char, |
||
144 | .is_directory = tmpfs_is_directory, |
||
145 | .is_file = tmpfs_is_file |
||
146 | }; |
||
147 | |||
2742 | jermar | 148 | /** Hash table of all directory entries. */ |
2658 | jermar | 149 | hash_table_t dentries; |
150 | |||
3087 | decky | 151 | struct rdentry { |
152 | uint8_t type; |
||
153 | uint32_t len; |
||
154 | } __attribute__((packed)); |
||
155 | |||
2760 | jermar | 156 | /* Implementation of hash table interface for the dentries hash table. */ |
2658 | jermar | 157 | static hash_index_t dentries_hash(unsigned long *key) |
158 | { |
||
159 | return *key % DENTRIES_BUCKETS; |
||
160 | } |
||
161 | |||
162 | static int dentries_compare(unsigned long *key, hash_count_t keys, |
||
163 | link_t *item) |
||
164 | { |
||
165 | tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t, |
||
166 | dh_link); |
||
167 | return dentry->index == *key; |
||
168 | } |
||
169 | |||
170 | static void dentries_remove_callback(link_t *item) |
||
171 | { |
||
172 | } |
||
173 | |||
174 | /** TMPFS dentries hash table operations. */ |
||
175 | hash_table_operations_t dentries_ops = { |
||
176 | .hash = dentries_hash, |
||
177 | .compare = dentries_compare, |
||
178 | .remove_callback = dentries_remove_callback |
||
179 | }; |
||
180 | |||
2770 | jermar | 181 | fs_index_t tmpfs_next_index = 1; |
2655 | jermar | 182 | |
2760 | jermar | 183 | typedef struct { |
184 | char *name; |
||
185 | tmpfs_dentry_t *parent; |
||
186 | link_t link; |
||
187 | } tmpfs_name_t; |
||
188 | |||
189 | /* Implementation of hash table interface for the names hash table. */ |
||
190 | static hash_index_t names_hash(unsigned long *key) |
||
2655 | jermar | 191 | { |
2760 | jermar | 192 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
193 | return dentry->index % NAMES_BUCKETS; |
||
194 | } |
||
195 | |||
196 | static int names_compare(unsigned long *key, hash_count_t keys, link_t *item) |
||
197 | { |
||
198 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key; |
||
199 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
||
200 | link); |
||
201 | return dentry == namep->parent; |
||
202 | } |
||
203 | |||
204 | static void names_remove_callback(link_t *item) |
||
205 | { |
||
206 | tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t, |
||
207 | link); |
||
208 | free(namep->name); |
||
209 | free(namep); |
||
210 | } |
||
211 | |||
212 | /** TMPFS node names hash table operations. */ |
||
213 | static hash_table_operations_t names_ops = { |
||
214 | .hash = names_hash, |
||
215 | .compare = names_compare, |
||
216 | .remove_callback = names_remove_callback |
||
217 | }; |
||
218 | |||
219 | static void tmpfs_name_initialize(tmpfs_name_t *namep) |
||
220 | { |
||
221 | namep->name = NULL; |
||
222 | namep->parent = NULL; |
||
223 | link_initialize(&namep->link); |
||
224 | } |
||
225 | |||
226 | static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry) |
||
227 | { |
||
2655 | jermar | 228 | dentry->index = 0; |
229 | dentry->sibling = NULL; |
||
230 | dentry->child = NULL; |
||
231 | dentry->type = TMPFS_NONE; |
||
2756 | jermar | 232 | dentry->lnkcnt = 0; |
2655 | jermar | 233 | dentry->size = 0; |
234 | dentry->data = NULL; |
||
2658 | jermar | 235 | link_initialize(&dentry->dh_link); |
2760 | jermar | 236 | return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1, |
237 | &names_ops); |
||
2655 | jermar | 238 | } |
239 | |||
240 | static bool tmpfs_init(void) |
||
241 | { |
||
2658 | jermar | 242 | if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops)) |
243 | return false; |
||
2747 | jermar | 244 | root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
2760 | jermar | 245 | if (!root) { |
246 | hash_table_destroy(&dentries); |
||
247 | return false; |
||
248 | } |
||
2757 | jermar | 249 | root->lnkcnt = 1; |
2760 | jermar | 250 | return true; |
2655 | jermar | 251 | } |
252 | |||
3087 | decky | 253 | static bool tmpfs_blockread(int phone, void *buffer, size_t *bufpos, size_t *buflen, size_t *pos, void *dst, size_t size) |
254 | { |
||
255 | size_t offset = 0; |
||
256 | size_t left = size; |
||
257 | |||
258 | while (left > 0) { |
||
259 | size_t rd; |
||
260 | |||
261 | if (*bufpos + left < *buflen) |
||
262 | rd = left; |
||
263 | else |
||
264 | rd = *buflen - *bufpos; |
||
265 | |||
266 | if (rd > 0) { |
||
267 | memcpy(dst + offset, buffer + *bufpos, rd); |
||
268 | offset += rd; |
||
269 | *bufpos += rd; |
||
270 | *pos += rd; |
||
271 | left -= rd; |
||
272 | } |
||
273 | |||
274 | if (*bufpos == *buflen) { |
||
3091 | jermar | 275 | ipcarg_t retval; |
276 | int rc = ipc_call_sync_2_1(phone, RD_READ_BLOCK, |
||
277 | *pos / BLOCK_SIZE, BLOCK_SIZE, |
||
278 | &retval); |
||
3087 | decky | 279 | if ((rc != EOK) || (retval != EOK)) |
280 | return false; |
||
281 | |||
282 | *bufpos = 0; |
||
283 | *buflen = BLOCK_SIZE; |
||
284 | } |
||
285 | } |
||
286 | |||
287 | return true; |
||
288 | } |
||
289 | |||
290 | static bool tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen, size_t *pos, tmpfs_dentry_t *parent) |
||
291 | { |
||
292 | struct rdentry entry; |
||
293 | |||
294 | do { |
||
295 | char *fname; |
||
296 | tmpfs_dentry_t *node; |
||
297 | uint32_t size; |
||
298 | |||
299 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &entry, sizeof(entry))) |
||
300 | return false; |
||
301 | |||
302 | entry.len = uint32_t_le2host(entry.len); |
||
303 | |||
304 | switch (entry.type) { |
||
305 | case 0: |
||
306 | break; |
||
307 | case 1: |
||
308 | fname = malloc(entry.len + 1); |
||
309 | if (fname == NULL) |
||
310 | return false; |
||
311 | |||
312 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_FILE); |
||
313 | if (node == NULL) { |
||
314 | free(fname); |
||
315 | return false; |
||
316 | } |
||
317 | |||
318 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
||
319 | tmpfs_destroy_node((void *) node); |
||
320 | free(fname); |
||
321 | return false; |
||
322 | } |
||
323 | fname[entry.len] = 0; |
||
324 | |||
325 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
||
326 | tmpfs_destroy_node((void *) node); |
||
327 | free(fname); |
||
328 | return false; |
||
329 | } |
||
330 | free(fname); |
||
331 | |||
332 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &size, sizeof(size))) |
||
333 | return false; |
||
334 | |||
335 | size = uint32_t_le2host(size); |
||
336 | |||
337 | node->data = malloc(size); |
||
338 | if (node->data == NULL) |
||
339 | return false; |
||
340 | |||
341 | node->size = size; |
||
342 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, node->data, size)) |
||
343 | return false; |
||
344 | |||
345 | break; |
||
346 | case 2: |
||
347 | fname = malloc(entry.len + 1); |
||
348 | if (fname == NULL) |
||
349 | return false; |
||
350 | |||
351 | node = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY); |
||
352 | if (node == NULL) { |
||
353 | free(fname); |
||
354 | return false; |
||
355 | } |
||
356 | |||
357 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) { |
||
358 | tmpfs_destroy_node((void *) node); |
||
359 | free(fname); |
||
360 | return false; |
||
361 | } |
||
362 | fname[entry.len] = 0; |
||
363 | |||
364 | if (!tmpfs_link_node((void *) parent, (void *) node, fname)) { |
||
365 | tmpfs_destroy_node((void *) node); |
||
366 | free(fname); |
||
367 | return false; |
||
368 | } |
||
369 | free(fname); |
||
370 | |||
371 | if (!tmpfs_restore_recursion(phone, block, bufpos, buflen, pos, node)) |
||
372 | return false; |
||
373 | |||
374 | break; |
||
375 | default: |
||
376 | return false; |
||
377 | } |
||
378 | } while (entry.type != 0); |
||
379 | |||
380 | return true; |
||
381 | } |
||
382 | |||
383 | static bool tmpfs_restore(dev_handle_t dev) |
||
384 | { |
||
385 | void *block = mmap(NULL, BLOCK_SIZE, |
||
386 | PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
||
387 | |||
388 | if (block == NULL) |
||
389 | return false; |
||
390 | |||
391 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, dev); |
||
392 | |||
393 | if (phone < 0) { |
||
394 | munmap(block, BLOCK_SIZE); |
||
395 | return false; |
||
396 | } |
||
397 | |||
398 | if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != EOK) |
||
399 | goto error; |
||
400 | |||
401 | size_t bufpos = 0; |
||
402 | size_t buflen = 0; |
||
403 | size_t pos = 0; |
||
404 | |||
405 | char tag[6]; |
||
406 | if (!tmpfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5)) |
||
407 | goto error; |
||
408 | |||
409 | tag[5] = 0; |
||
410 | if (strcmp(tag, "TMPFS") != 0) |
||
411 | goto error; |
||
412 | |||
413 | if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, root)) |
||
414 | goto error; |
||
415 | |||
416 | ipc_hangup(phone); |
||
417 | munmap(block, BLOCK_SIZE); |
||
418 | return true; |
||
419 | |||
420 | error: |
||
421 | ipc_hangup(phone); |
||
422 | munmap(block, BLOCK_SIZE); |
||
423 | return false; |
||
424 | } |
||
425 | |||
2645 | jermar | 426 | /** Compare one component of path to a directory entry. |
427 | * |
||
2791 | jermar | 428 | * @param parentp Pointer to node from which we descended. |
429 | * @param childp Pointer to node to compare the path component with. |
||
2705 | jermar | 430 | * @param component Array of characters holding component name. |
2645 | jermar | 431 | * |
2705 | jermar | 432 | * @return True on match, false otherwise. |
2645 | jermar | 433 | */ |
2791 | jermar | 434 | static bool |
435 | tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp, |
||
436 | const char *component) |
||
2645 | jermar | 437 | { |
2760 | jermar | 438 | unsigned long key = (unsigned long) parentp; |
439 | link_t *hlp = hash_table_find(&childp->names, &key); |
||
440 | assert(hlp); |
||
441 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link); |
||
442 | return !strcmp(namep->name, component); |
||
2705 | jermar | 443 | } |
2655 | jermar | 444 | |
2791 | jermar | 445 | void *tmpfs_match(void *prnt, const char *component) |
446 | { |
||
447 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
||
448 | tmpfs_dentry_t *childp = parentp->child; |
||
449 | |||
450 | while (childp && !tmpfs_match_one(parentp, childp, component)) |
||
451 | childp = childp->sibling; |
||
452 | |||
453 | return (void *) childp; |
||
454 | } |
||
455 | |||
2770 | jermar | 456 | void * |
2863 | jermar | 457 | tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
2763 | jermar | 458 | { |
2770 | jermar | 459 | unsigned long key = index; |
460 | link_t *lnk = hash_table_find(&dentries, &key); |
||
2763 | jermar | 461 | if (!lnk) |
462 | return NULL; |
||
463 | return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); |
||
464 | } |
||
465 | |||
2852 | jermar | 466 | void tmpfs_node_put(void *node) |
467 | { |
||
468 | /* nothing to do */ |
||
469 | } |
||
470 | |||
2747 | jermar | 471 | void *tmpfs_create_node(int lflag) |
2705 | jermar | 472 | { |
2707 | jermar | 473 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
474 | |||
475 | tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t)); |
||
476 | if (!node) |
||
2730 | jermar | 477 | return NULL; |
2707 | jermar | 478 | |
2760 | jermar | 479 | if (!tmpfs_dentry_initialize(node)) { |
480 | free(node); |
||
481 | return NULL; |
||
482 | } |
||
2707 | jermar | 483 | node->index = tmpfs_next_index++; |
484 | if (lflag & L_DIRECTORY) |
||
485 | node->type = TMPFS_DIRECTORY; |
||
486 | else |
||
487 | node->type = TMPFS_FILE; |
||
488 | |||
2742 | jermar | 489 | /* Insert the new node into the dentry hash table. */ |
2770 | jermar | 490 | unsigned long key = node->index; |
491 | hash_table_insert(&dentries, &key, &node->dh_link); |
||
2742 | jermar | 492 | return (void *) node; |
493 | } |
||
494 | |||
2747 | jermar | 495 | bool tmpfs_link_node(void *prnt, void *chld, const char *nm) |
2742 | jermar | 496 | { |
497 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt; |
||
498 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld; |
||
499 | |||
500 | assert(parentp->type == TMPFS_DIRECTORY); |
||
501 | |||
2760 | jermar | 502 | tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t)); |
503 | if (!namep) |
||
504 | return false; |
||
505 | tmpfs_name_initialize(namep); |
||
2742 | jermar | 506 | size_t len = strlen(nm); |
2760 | jermar | 507 | namep->name = malloc(len + 1); |
508 | if (!namep->name) { |
||
509 | free(namep); |
||
2742 | jermar | 510 | return false; |
2760 | jermar | 511 | } |
512 | strcpy(namep->name, nm); |
||
513 | namep->parent = parentp; |
||
2756 | jermar | 514 | |
515 | childp->lnkcnt++; |
||
2742 | jermar | 516 | |
2760 | jermar | 517 | unsigned long key = (unsigned long) parentp; |
518 | hash_table_insert(&childp->names, &key, &namep->link); |
||
519 | |||
2707 | jermar | 520 | /* Insert the new node into the namespace. */ |
2742 | jermar | 521 | if (parentp->child) { |
522 | tmpfs_dentry_t *tmp = parentp->child; |
||
2707 | jermar | 523 | while (tmp->sibling) |
524 | tmp = tmp->sibling; |
||
2742 | jermar | 525 | tmp->sibling = childp; |
2707 | jermar | 526 | } else { |
2742 | jermar | 527 | parentp->child = childp; |
2707 | jermar | 528 | } |
529 | |||
2742 | jermar | 530 | return true; |
2705 | jermar | 531 | } |
2655 | jermar | 532 | |
2758 | jermar | 533 | int tmpfs_unlink_node(void *prnt, void *chld) |
2705 | jermar | 534 | { |
2758 | jermar | 535 | tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt; |
536 | tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld; |
||
2733 | jermar | 537 | |
2758 | jermar | 538 | if (!parentp) |
539 | return EBUSY; |
||
540 | |||
541 | if (childp->child) |
||
2733 | jermar | 542 | return ENOTEMPTY; |
543 | |||
2758 | jermar | 544 | if (parentp->child == childp) { |
545 | parentp->child = childp->sibling; |
||
2733 | jermar | 546 | } else { |
547 | /* TODO: consider doubly linked list for organizing siblings. */ |
||
2758 | jermar | 548 | tmpfs_dentry_t *tmp = parentp->child; |
549 | while (tmp->sibling != childp) |
||
2733 | jermar | 550 | tmp = tmp->sibling; |
2758 | jermar | 551 | tmp->sibling = childp->sibling; |
2733 | jermar | 552 | } |
2758 | jermar | 553 | childp->sibling = NULL; |
2733 | jermar | 554 | |
2760 | jermar | 555 | unsigned long key = (unsigned long) parentp; |
556 | hash_table_remove(&childp->names, &key, 1); |
||
2742 | jermar | 557 | |
2758 | jermar | 558 | childp->lnkcnt--; |
2756 | jermar | 559 | |
2733 | jermar | 560 | return EOK; |
2645 | jermar | 561 | } |
562 | |||
2858 | jermar | 563 | int tmpfs_destroy_node(void *nodep) |
2742 | jermar | 564 | { |
565 | tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep; |
||
566 | |||
2756 | jermar | 567 | assert(!dentry->lnkcnt); |
2742 | jermar | 568 | assert(!dentry->child); |
569 | assert(!dentry->sibling); |
||
570 | |||
2770 | jermar | 571 | unsigned long key = dentry->index; |
572 | hash_table_remove(&dentries, &key, 1); |
||
2742 | jermar | 573 | |
2760 | jermar | 574 | hash_table_destroy(&dentry->names); |
575 | |||
2742 | jermar | 576 | if (dentry->type == TMPFS_FILE) |
577 | free(dentry->data); |
||
578 | free(dentry); |
||
2858 | jermar | 579 | return EOK; |
2742 | jermar | 580 | } |
581 | |||
2958 | jermar | 582 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
583 | { |
||
3087 | decky | 584 | dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
585 | fs_index_t mr_index = (fs_index_t) IPC_GET_ARG2(*request); |
||
586 | fs_handle_t mp_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request); |
||
587 | dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request); |
||
588 | fs_index_t mp_index = (fs_index_t) IPC_GET_ARG5(*request); |
||
589 | |||
2958 | jermar | 590 | if ((mr_index == root->index) && |
3087 | decky | 591 | (mp_fs_handle == tmpfs_reg.fs_handle) && |
592 | (mp_index == mr_index)) { |
||
593 | |||
594 | if (mr_dev_handle >= 0) { |
||
595 | if (tmpfs_restore(mr_dev_handle)) |
||
596 | ipc_answer_0(rid, EOK); |
||
597 | else |
||
598 | ipc_answer_0(rid, ELIMIT); |
||
599 | } else |
||
600 | ipc_answer_0(rid, EOK); |
||
601 | } else |
||
2958 | jermar | 602 | ipc_answer_0(rid, ENOTSUP); |
603 | } |
||
604 | |||
2645 | jermar | 605 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
606 | { |
||
2747 | jermar | 607 | /* Initialize TMPFS. */ |
2655 | jermar | 608 | if (!root && !tmpfs_init()) { |
609 | ipc_answer_0(rid, ENOMEM); |
||
610 | return; |
||
611 | } |
||
2747 | jermar | 612 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
2645 | jermar | 613 | } |
614 | |||
2658 | jermar | 615 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
616 | { |
||
2770 | jermar | 617 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
618 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
619 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
2658 | jermar | 620 | |
621 | /* |
||
622 | * Lookup the respective dentry. |
||
623 | */ |
||
624 | link_t *hlp; |
||
2770 | jermar | 625 | unsigned long key = index; |
626 | hlp = hash_table_find(&dentries, &key); |
||
2658 | jermar | 627 | if (!hlp) { |
628 | ipc_answer_0(rid, ENOENT); |
||
629 | return; |
||
630 | } |
||
631 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
632 | dh_link); |
||
633 | |||
634 | /* |
||
2663 | jermar | 635 | * Receive the read request. |
2658 | jermar | 636 | */ |
637 | ipc_callid_t callid; |
||
2673 | jermar | 638 | size_t len; |
639 | if (!ipc_data_read_receive(&callid, &len)) { |
||
2658 | jermar | 640 | ipc_answer_0(callid, EINVAL); |
641 | ipc_answer_0(rid, EINVAL); |
||
642 | return; |
||
643 | } |
||
644 | |||
2699 | jermar | 645 | size_t bytes; |
646 | if (dentry->type == TMPFS_FILE) { |
||
647 | bytes = max(0, min(dentry->size - pos, len)); |
||
648 | (void) ipc_data_read_finalize(callid, dentry->data + pos, |
||
649 | bytes); |
||
650 | } else { |
||
651 | int i; |
||
2739 | jermar | 652 | tmpfs_dentry_t *cur; |
2699 | jermar | 653 | |
654 | assert(dentry->type == TMPFS_DIRECTORY); |
||
655 | |||
656 | /* |
||
657 | * Yes, we really use O(n) algorithm here. |
||
658 | * If it bothers someone, it could be fixed by introducing a |
||
659 | * hash table. |
||
660 | */ |
||
661 | for (i = 0, cur = dentry->child; i < pos && cur; i++, |
||
662 | cur = cur->sibling) |
||
663 | ; |
||
2664 | jermar | 664 | |
2699 | jermar | 665 | if (!cur) { |
666 | ipc_answer_0(callid, ENOENT); |
||
667 | ipc_answer_1(rid, ENOENT, 0); |
||
668 | return; |
||
669 | } |
||
670 | |||
2760 | jermar | 671 | unsigned long key = (unsigned long) dentry; |
672 | link_t *hlp = hash_table_find(&cur->names, &key); |
||
673 | assert(hlp); |
||
674 | tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, |
||
675 | link); |
||
676 | |||
677 | (void) ipc_data_read_finalize(callid, namep->name, |
||
678 | strlen(namep->name) + 1); |
||
2699 | jermar | 679 | bytes = 1; |
680 | } |
||
681 | |||
2664 | jermar | 682 | /* |
683 | * Answer the VFS_READ call. |
||
684 | */ |
||
685 | ipc_answer_1(rid, EOK, bytes); |
||
2658 | jermar | 686 | } |
687 | |||
2666 | jermar | 688 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
689 | { |
||
2770 | jermar | 690 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
691 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
692 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
2666 | jermar | 693 | |
694 | /* |
||
695 | * Lookup the respective dentry. |
||
696 | */ |
||
697 | link_t *hlp; |
||
2770 | jermar | 698 | unsigned long key = index; |
699 | hlp = hash_table_find(&dentries, &key); |
||
2666 | jermar | 700 | if (!hlp) { |
701 | ipc_answer_0(rid, ENOENT); |
||
702 | return; |
||
703 | } |
||
704 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
705 | dh_link); |
||
706 | |||
707 | /* |
||
708 | * Receive the write request. |
||
709 | */ |
||
710 | ipc_callid_t callid; |
||
2673 | jermar | 711 | size_t len; |
2676 | jermar | 712 | if (!ipc_data_write_receive(&callid, &len)) { |
2666 | jermar | 713 | ipc_answer_0(callid, EINVAL); |
714 | ipc_answer_0(rid, EINVAL); |
||
715 | return; |
||
716 | } |
||
717 | |||
718 | /* |
||
2667 | jermar | 719 | * Check whether the file needs to grow. |
720 | */ |
||
2673 | jermar | 721 | if (pos + len <= dentry->size) { |
2667 | jermar | 722 | /* The file size is not changing. */ |
2678 | jermar | 723 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
2710 | jermar | 724 | ipc_answer_2(rid, EOK, len, dentry->size); |
2667 | jermar | 725 | return; |
726 | } |
||
2673 | jermar | 727 | size_t delta = (pos + len) - dentry->size; |
2667 | jermar | 728 | /* |
2666 | jermar | 729 | * At this point, we are deliberately extremely straightforward and |
2667 | jermar | 730 | * simply realloc the contents of the file on every write that grows the |
731 | * file. In the end, the situation might not be as bad as it may look: |
||
732 | * our heap allocator can save us and just grow the block whenever |
||
733 | * possible. |
||
2666 | jermar | 734 | */ |
2667 | jermar | 735 | void *newdata = realloc(dentry->data, dentry->size + delta); |
2666 | jermar | 736 | if (!newdata) { |
737 | ipc_answer_0(callid, ENOMEM); |
||
2710 | jermar | 738 | ipc_answer_2(rid, EOK, 0, dentry->size); |
2666 | jermar | 739 | return; |
740 | } |
||
2693 | jermar | 741 | /* Clear any newly allocated memory in order to emulate gaps. */ |
2686 | jermar | 742 | memset(newdata + dentry->size, 0, delta); |
2667 | jermar | 743 | dentry->size += delta; |
2666 | jermar | 744 | dentry->data = newdata; |
2678 | jermar | 745 | (void) ipc_data_write_finalize(callid, dentry->data + pos, len); |
2687 | jermar | 746 | ipc_answer_2(rid, EOK, len, dentry->size); |
2666 | jermar | 747 | } |
748 | |||
2693 | jermar | 749 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
750 | { |
||
2770 | jermar | 751 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
752 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
753 | size_t size = (off_t)IPC_GET_ARG3(*request); |
||
2693 | jermar | 754 | |
755 | /* |
||
756 | * Lookup the respective dentry. |
||
757 | */ |
||
758 | link_t *hlp; |
||
2770 | jermar | 759 | unsigned long key = index; |
760 | hlp = hash_table_find(&dentries, &key); |
||
2693 | jermar | 761 | if (!hlp) { |
762 | ipc_answer_0(rid, ENOENT); |
||
763 | return; |
||
764 | } |
||
765 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
766 | dh_link); |
||
767 | |||
768 | if (size == dentry->size) { |
||
769 | ipc_answer_0(rid, EOK); |
||
770 | return; |
||
771 | } |
||
772 | |||
773 | void *newdata = realloc(dentry->data, size); |
||
774 | if (!newdata) { |
||
775 | ipc_answer_0(rid, ENOMEM); |
||
776 | return; |
||
777 | } |
||
778 | if (size > dentry->size) { |
||
779 | size_t delta = size - dentry->size; |
||
780 | memset(newdata + dentry->size, 0, delta); |
||
781 | } |
||
782 | dentry->size = size; |
||
783 | dentry->data = newdata; |
||
784 | ipc_answer_0(rid, EOK); |
||
785 | } |
||
786 | |||
2742 | jermar | 787 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
2731 | jermar | 788 | { |
2770 | jermar | 789 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
790 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
2858 | jermar | 791 | int rc; |
2731 | jermar | 792 | |
793 | link_t *hlp; |
||
2770 | jermar | 794 | unsigned long key = index; |
795 | hlp = hash_table_find(&dentries, &key); |
||
2731 | jermar | 796 | if (!hlp) { |
797 | ipc_answer_0(rid, ENOENT); |
||
798 | return; |
||
799 | } |
||
800 | tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t, |
||
801 | dh_link); |
||
2858 | jermar | 802 | rc = tmpfs_destroy_node(dentry); |
803 | ipc_answer_0(rid, rc); |
||
2731 | jermar | 804 | } |
805 | |||
2645 | jermar | 806 | /** |
807 | * @} |
||
808 | */ |