1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub 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>
|
---|
44 | #include <atomic.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <string.h>
|
---|
47 | #include <stdio.h>
|
---|
48 | #include <assert.h>
|
---|
49 | #include <sys/types.h>
|
---|
50 | #include <adt/hash_table.h>
|
---|
51 | #include <as.h>
|
---|
52 | #include <libfs.h>
|
---|
53 |
|
---|
54 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
---|
55 | #define max(a, b) ((a) > (b) ? (a) : (b))
|
---|
56 |
|
---|
57 | #define NODES_BUCKETS 256
|
---|
58 |
|
---|
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;
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Implementation of the libfs interface.
|
---|
66 | */
|
---|
67 |
|
---|
68 | /* Forward declarations of static functions. */
|
---|
69 | static int tmpfs_match(fs_node_t **, fs_node_t *, const char *);
|
---|
70 | static int tmpfs_node_get(fs_node_t **, dev_handle_t, fs_index_t);
|
---|
71 | static int tmpfs_node_open(fs_node_t *);
|
---|
72 | static int tmpfs_node_put(fs_node_t *);
|
---|
73 | static int tmpfs_create_node(fs_node_t **, dev_handle_t, int);
|
---|
74 | static int tmpfs_destroy_node(fs_node_t *);
|
---|
75 | static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
|
---|
76 | static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *);
|
---|
77 |
|
---|
78 | /* Implementation of helper functions. */
|
---|
79 | static int tmpfs_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
|
---|
80 | {
|
---|
81 | return tmpfs_node_get(rfn, dev_handle, TMPFS_SOME_ROOT);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static int tmpfs_has_children(bool *has_children, fs_node_t *fn)
|
---|
85 | {
|
---|
86 | *has_children = !list_empty(&TMPFS_NODE(fn)->cs_head);
|
---|
87 | return EOK;
|
---|
88 | }
|
---|
89 |
|
---|
90 | static fs_index_t tmpfs_index_get(fs_node_t *fn)
|
---|
91 | {
|
---|
92 | return TMPFS_NODE(fn)->index;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static size_t tmpfs_size_get(fs_node_t *fn)
|
---|
96 | {
|
---|
97 | return TMPFS_NODE(fn)->size;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static unsigned tmpfs_lnkcnt_get(fs_node_t *fn)
|
---|
101 | {
|
---|
102 | return TMPFS_NODE(fn)->lnkcnt;
|
---|
103 | }
|
---|
104 |
|
---|
105 | static char tmpfs_plb_get_char(unsigned pos)
|
---|
106 | {
|
---|
107 | return tmpfs_reg.plb_ro[pos % PLB_SIZE];
|
---|
108 | }
|
---|
109 |
|
---|
110 | static bool tmpfs_is_directory(fs_node_t *fn)
|
---|
111 | {
|
---|
112 | return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static bool tmpfs_is_file(fs_node_t *fn)
|
---|
116 | {
|
---|
117 | return TMPFS_NODE(fn)->type == TMPFS_FILE;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static dev_handle_t tmpfs_device_get(fs_node_t *fn)
|
---|
121 | {
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** libfs operations */
|
---|
126 | libfs_ops_t tmpfs_libfs_ops = {
|
---|
127 | .root_get = tmpfs_root_get,
|
---|
128 | .match = tmpfs_match,
|
---|
129 | .node_get = tmpfs_node_get,
|
---|
130 | .node_open = tmpfs_node_open,
|
---|
131 | .node_put = tmpfs_node_put,
|
---|
132 | .create = tmpfs_create_node,
|
---|
133 | .destroy = tmpfs_destroy_node,
|
---|
134 | .link = tmpfs_link_node,
|
---|
135 | .unlink = tmpfs_unlink_node,
|
---|
136 | .has_children = tmpfs_has_children,
|
---|
137 | .index_get = tmpfs_index_get,
|
---|
138 | .size_get = tmpfs_size_get,
|
---|
139 | .lnkcnt_get = tmpfs_lnkcnt_get,
|
---|
140 | .plb_get_char = tmpfs_plb_get_char,
|
---|
141 | .is_directory = tmpfs_is_directory,
|
---|
142 | .is_file = tmpfs_is_file,
|
---|
143 | .device_get = tmpfs_device_get
|
---|
144 | };
|
---|
145 |
|
---|
146 | /** Hash table of all TMPFS nodes. */
|
---|
147 | hash_table_t nodes;
|
---|
148 |
|
---|
149 | #define NODES_KEY_INDEX 0
|
---|
150 | #define NODES_KEY_DEV 1
|
---|
151 |
|
---|
152 | /* Implementation of hash table interface for the nodes hash table. */
|
---|
153 | static hash_index_t nodes_hash(unsigned long key[])
|
---|
154 | {
|
---|
155 | return key[NODES_KEY_INDEX] % NODES_BUCKETS;
|
---|
156 | }
|
---|
157 |
|
---|
158 | static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
159 | {
|
---|
160 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
---|
161 | nh_link);
|
---|
162 | return (nodep->index == key[NODES_KEY_INDEX] &&
|
---|
163 | nodep->dev_handle == key[NODES_KEY_DEV]);
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void nodes_remove_callback(link_t *item)
|
---|
167 | {
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** TMPFS nodes hash table operations. */
|
---|
171 | hash_table_operations_t nodes_ops = {
|
---|
172 | .hash = nodes_hash,
|
---|
173 | .compare = nodes_compare,
|
---|
174 | .remove_callback = nodes_remove_callback
|
---|
175 | };
|
---|
176 |
|
---|
177 | static void tmpfs_node_initialize(tmpfs_node_t *nodep)
|
---|
178 | {
|
---|
179 | nodep->bp = NULL;
|
---|
180 | nodep->index = 0;
|
---|
181 | nodep->dev_handle = 0;
|
---|
182 | nodep->type = TMPFS_NONE;
|
---|
183 | nodep->lnkcnt = 0;
|
---|
184 | nodep->size = 0;
|
---|
185 | nodep->data = NULL;
|
---|
186 | link_initialize(&nodep->nh_link);
|
---|
187 | list_initialize(&nodep->cs_head);
|
---|
188 | }
|
---|
189 |
|
---|
190 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
|
---|
191 | {
|
---|
192 | link_initialize(&dentryp->link);
|
---|
193 | dentryp->name = NULL;
|
---|
194 | dentryp->node = NULL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | bool tmpfs_init(void)
|
---|
198 | {
|
---|
199 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
|
---|
200 | return false;
|
---|
201 |
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | static bool tmpfs_instance_init(dev_handle_t dev_handle)
|
---|
206 | {
|
---|
207 | fs_node_t *rfn;
|
---|
208 | int rc;
|
---|
209 |
|
---|
210 | rc = tmpfs_create_node(&rfn, dev_handle, L_DIRECTORY);
|
---|
211 | if (rc != EOK || !rfn)
|
---|
212 | return false;
|
---|
213 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 |
|
---|
217 | int tmpfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
218 | {
|
---|
219 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
220 | link_t *lnk;
|
---|
221 |
|
---|
222 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
223 | lnk = lnk->next) {
|
---|
224 | tmpfs_dentry_t *dentryp;
|
---|
225 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
226 | if (!str_cmp(dentryp->name, component)) {
|
---|
227 | *rfn = FS_NODE(dentryp->node);
|
---|
228 | return EOK;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | *rfn = NULL;
|
---|
233 | return EOK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | int tmpfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
|
---|
237 | {
|
---|
238 | unsigned long key[] = {
|
---|
239 | [NODES_KEY_INDEX] = index,
|
---|
240 | [NODES_KEY_DEV] = dev_handle
|
---|
241 | };
|
---|
242 | link_t *lnk = hash_table_find(&nodes, key);
|
---|
243 | if (lnk) {
|
---|
244 | tmpfs_node_t *nodep;
|
---|
245 | nodep = hash_table_get_instance(lnk, tmpfs_node_t, nh_link);
|
---|
246 | *rfn = FS_NODE(nodep);
|
---|
247 | } else {
|
---|
248 | *rfn = NULL;
|
---|
249 | }
|
---|
250 | return EOK;
|
---|
251 | }
|
---|
252 |
|
---|
253 | int tmpfs_node_open(fs_node_t *fn)
|
---|
254 | {
|
---|
255 | /* nothing to do */
|
---|
256 | return EOK;
|
---|
257 | }
|
---|
258 |
|
---|
259 | int tmpfs_node_put(fs_node_t *fn)
|
---|
260 | {
|
---|
261 | /* nothing to do */
|
---|
262 | return EOK;
|
---|
263 | }
|
---|
264 |
|
---|
265 | int tmpfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
|
---|
266 | {
|
---|
267 | fs_node_t *rootfn;
|
---|
268 | int rc;
|
---|
269 |
|
---|
270 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
271 |
|
---|
272 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
|
---|
273 | if (!nodep)
|
---|
274 | return ENOMEM;
|
---|
275 | tmpfs_node_initialize(nodep);
|
---|
276 | nodep->bp = malloc(sizeof(fs_node_t));
|
---|
277 | if (!nodep->bp) {
|
---|
278 | free(nodep);
|
---|
279 | return ENOMEM;
|
---|
280 | }
|
---|
281 | fs_node_initialize(nodep->bp);
|
---|
282 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
|
---|
283 |
|
---|
284 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
---|
285 | assert(rc == EOK);
|
---|
286 | if (!rootfn)
|
---|
287 | nodep->index = TMPFS_SOME_ROOT;
|
---|
288 | else
|
---|
289 | nodep->index = tmpfs_next_index++;
|
---|
290 | nodep->dev_handle = dev_handle;
|
---|
291 | if (lflag & L_DIRECTORY)
|
---|
292 | nodep->type = TMPFS_DIRECTORY;
|
---|
293 | else
|
---|
294 | nodep->type = TMPFS_FILE;
|
---|
295 |
|
---|
296 | /* Insert the new node into the nodes hash table. */
|
---|
297 | unsigned long key[] = {
|
---|
298 | [NODES_KEY_INDEX] = nodep->index,
|
---|
299 | [NODES_KEY_DEV] = nodep->dev_handle
|
---|
300 | };
|
---|
301 | hash_table_insert(&nodes, key, &nodep->nh_link);
|
---|
302 | *rfn = FS_NODE(nodep);
|
---|
303 | return EOK;
|
---|
304 | }
|
---|
305 |
|
---|
306 | int tmpfs_destroy_node(fs_node_t *fn)
|
---|
307 | {
|
---|
308 | tmpfs_node_t *nodep = TMPFS_NODE(fn);
|
---|
309 |
|
---|
310 | assert(!nodep->lnkcnt);
|
---|
311 | assert(list_empty(&nodep->cs_head));
|
---|
312 |
|
---|
313 | unsigned long key[] = {
|
---|
314 | [NODES_KEY_INDEX] = nodep->index,
|
---|
315 | [NODES_KEY_DEV] = nodep->dev_handle
|
---|
316 | };
|
---|
317 | hash_table_remove(&nodes, key, 2);
|
---|
318 |
|
---|
319 | if (nodep->type == TMPFS_FILE)
|
---|
320 | free(nodep->data);
|
---|
321 | free(nodep->bp);
|
---|
322 | free(nodep);
|
---|
323 | return EOK;
|
---|
324 | }
|
---|
325 |
|
---|
326 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
327 | {
|
---|
328 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
329 | tmpfs_node_t *childp = TMPFS_NODE(cfn);
|
---|
330 | tmpfs_dentry_t *dentryp;
|
---|
331 | link_t *lnk;
|
---|
332 |
|
---|
333 | assert(parentp->type == TMPFS_DIRECTORY);
|
---|
334 |
|
---|
335 | /* Check for duplicit entries. */
|
---|
336 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
337 | lnk = lnk->next) {
|
---|
338 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
339 | if (!str_cmp(dentryp->name, nm))
|
---|
340 | return EEXIST;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /* Allocate and initialize the dentry. */
|
---|
344 | dentryp = malloc(sizeof(tmpfs_dentry_t));
|
---|
345 | if (!dentryp)
|
---|
346 | return ENOMEM;
|
---|
347 | tmpfs_dentry_initialize(dentryp);
|
---|
348 |
|
---|
349 | /* Populate and link the new dentry. */
|
---|
350 | size_t size = str_size(nm);
|
---|
351 | dentryp->name = malloc(size + 1);
|
---|
352 | if (!dentryp->name) {
|
---|
353 | free(dentryp);
|
---|
354 | return ENOMEM;
|
---|
355 | }
|
---|
356 | str_cpy(dentryp->name, size + 1, nm);
|
---|
357 | dentryp->node = childp;
|
---|
358 | childp->lnkcnt++;
|
---|
359 | list_append(&dentryp->link, &parentp->cs_head);
|
---|
360 |
|
---|
361 | return EOK;
|
---|
362 | }
|
---|
363 |
|
---|
364 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
365 | {
|
---|
366 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
367 | tmpfs_node_t *childp = NULL;
|
---|
368 | tmpfs_dentry_t *dentryp;
|
---|
369 | link_t *lnk;
|
---|
370 |
|
---|
371 | if (!parentp)
|
---|
372 | return EBUSY;
|
---|
373 |
|
---|
374 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
375 | lnk = lnk->next) {
|
---|
376 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
377 | if (!str_cmp(dentryp->name, nm)) {
|
---|
378 | childp = dentryp->node;
|
---|
379 | assert(FS_NODE(childp) == cfn);
|
---|
380 | break;
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | if (!childp)
|
---|
385 | return ENOENT;
|
---|
386 |
|
---|
387 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
|
---|
388 | return ENOTEMPTY;
|
---|
389 |
|
---|
390 | list_remove(&dentryp->link);
|
---|
391 | free(dentryp);
|
---|
392 | childp->lnkcnt--;
|
---|
393 |
|
---|
394 | return EOK;
|
---|
395 | }
|
---|
396 |
|
---|
397 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
398 | {
|
---|
399 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
400 | int rc;
|
---|
401 |
|
---|
402 | /* accept the mount options */
|
---|
403 | ipc_callid_t callid;
|
---|
404 | size_t size;
|
---|
405 | if (!async_data_write_receive(&callid, &size)) {
|
---|
406 | ipc_answer_0(callid, EINVAL);
|
---|
407 | ipc_answer_0(rid, EINVAL);
|
---|
408 | return;
|
---|
409 | }
|
---|
410 | char *opts = malloc(size + 1);
|
---|
411 | if (!opts) {
|
---|
412 | ipc_answer_0(callid, ENOMEM);
|
---|
413 | ipc_answer_0(rid, ENOMEM);
|
---|
414 | return;
|
---|
415 | }
|
---|
416 | ipcarg_t retval = async_data_write_finalize(callid, opts, size);
|
---|
417 | if (retval != EOK) {
|
---|
418 | ipc_answer_0(rid, retval);
|
---|
419 | free(opts);
|
---|
420 | return;
|
---|
421 | }
|
---|
422 | opts[size] = '\0';
|
---|
423 |
|
---|
424 | /* Initialize TMPFS instance. */
|
---|
425 | if (!tmpfs_instance_init(dev_handle)) {
|
---|
426 | free(opts);
|
---|
427 | ipc_answer_0(rid, ENOMEM);
|
---|
428 | return;
|
---|
429 | }
|
---|
430 |
|
---|
431 | fs_node_t *rootfn;
|
---|
432 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
---|
433 | assert(rc == EOK);
|
---|
434 | tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
|
---|
435 | if (str_cmp(opts, "restore") == 0) {
|
---|
436 | if (tmpfs_restore(dev_handle))
|
---|
437 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
438 | rootp->lnkcnt);
|
---|
439 | else
|
---|
440 | ipc_answer_0(rid, ELIMIT);
|
---|
441 | } else {
|
---|
442 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
443 | rootp->lnkcnt);
|
---|
444 | }
|
---|
445 | free(opts);
|
---|
446 | }
|
---|
447 |
|
---|
448 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
449 | {
|
---|
450 | libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
451 | }
|
---|
452 |
|
---|
453 | void tmpfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
454 | {
|
---|
455 | ipc_answer_0(rid, ENOTSUP);
|
---|
456 | }
|
---|
457 |
|
---|
458 | void tmpfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
459 | {
|
---|
460 | libfs_unmount(&tmpfs_libfs_ops, rid, request);
|
---|
461 | }
|
---|
462 |
|
---|
463 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
464 | {
|
---|
465 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
466 | }
|
---|
467 |
|
---|
468 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
469 | {
|
---|
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);
|
---|
473 |
|
---|
474 | /*
|
---|
475 | * Lookup the respective TMPFS node.
|
---|
476 | */
|
---|
477 | link_t *hlp;
|
---|
478 | unsigned long key[] = {
|
---|
479 | [NODES_KEY_INDEX] = index,
|
---|
480 | [NODES_KEY_DEV] = dev_handle,
|
---|
481 | };
|
---|
482 | hlp = hash_table_find(&nodes, key);
|
---|
483 | if (!hlp) {
|
---|
484 | ipc_answer_0(rid, ENOENT);
|
---|
485 | return;
|
---|
486 | }
|
---|
487 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
488 | nh_link);
|
---|
489 |
|
---|
490 | /*
|
---|
491 | * Receive the read request.
|
---|
492 | */
|
---|
493 | ipc_callid_t callid;
|
---|
494 | size_t size;
|
---|
495 | if (!async_data_read_receive(&callid, &size)) {
|
---|
496 | ipc_answer_0(callid, EINVAL);
|
---|
497 | ipc_answer_0(rid, EINVAL);
|
---|
498 | return;
|
---|
499 | }
|
---|
500 |
|
---|
501 | size_t bytes;
|
---|
502 | if (nodep->type == TMPFS_FILE) {
|
---|
503 | bytes = max(0, min(nodep->size - pos, size));
|
---|
504 | (void) async_data_read_finalize(callid, nodep->data + pos,
|
---|
505 | bytes);
|
---|
506 | } else {
|
---|
507 | tmpfs_dentry_t *dentryp;
|
---|
508 | link_t *lnk;
|
---|
509 | int i;
|
---|
510 |
|
---|
511 | assert(nodep->type == TMPFS_DIRECTORY);
|
---|
512 |
|
---|
513 | /*
|
---|
514 | * Yes, we really use O(n) algorithm here.
|
---|
515 | * If it bothers someone, it could be fixed by introducing a
|
---|
516 | * hash table.
|
---|
517 | */
|
---|
518 | for (i = 0, lnk = nodep->cs_head.next;
|
---|
519 | i < pos && lnk != &nodep->cs_head;
|
---|
520 | i++, lnk = lnk->next)
|
---|
521 | ;
|
---|
522 |
|
---|
523 | if (lnk == &nodep->cs_head) {
|
---|
524 | ipc_answer_0(callid, ENOENT);
|
---|
525 | ipc_answer_1(rid, ENOENT, 0);
|
---|
526 | return;
|
---|
527 | }
|
---|
528 |
|
---|
529 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
530 |
|
---|
531 | (void) async_data_read_finalize(callid, dentryp->name,
|
---|
532 | str_size(dentryp->name) + 1);
|
---|
533 | bytes = 1;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /*
|
---|
537 | * Answer the VFS_READ call.
|
---|
538 | */
|
---|
539 | ipc_answer_1(rid, EOK, bytes);
|
---|
540 | }
|
---|
541 |
|
---|
542 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
543 | {
|
---|
544 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
545 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
546 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * Lookup the respective TMPFS node.
|
---|
550 | */
|
---|
551 | link_t *hlp;
|
---|
552 | unsigned long key[] = {
|
---|
553 | [NODES_KEY_INDEX] = index,
|
---|
554 | [NODES_KEY_DEV] = dev_handle
|
---|
555 | };
|
---|
556 | hlp = hash_table_find(&nodes, key);
|
---|
557 | if (!hlp) {
|
---|
558 | ipc_answer_0(rid, ENOENT);
|
---|
559 | return;
|
---|
560 | }
|
---|
561 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
562 | nh_link);
|
---|
563 |
|
---|
564 | /*
|
---|
565 | * Receive the write request.
|
---|
566 | */
|
---|
567 | ipc_callid_t callid;
|
---|
568 | size_t size;
|
---|
569 | if (!async_data_write_receive(&callid, &size)) {
|
---|
570 | ipc_answer_0(callid, EINVAL);
|
---|
571 | ipc_answer_0(rid, EINVAL);
|
---|
572 | return;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * Check whether the file needs to grow.
|
---|
577 | */
|
---|
578 | if (pos + size <= nodep->size) {
|
---|
579 | /* The file size is not changing. */
|
---|
580 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
---|
581 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
582 | return;
|
---|
583 | }
|
---|
584 | size_t delta = (pos + size) - nodep->size;
|
---|
585 | /*
|
---|
586 | * At this point, we are deliberately extremely straightforward and
|
---|
587 | * simply realloc the contents of the file on every write that grows the
|
---|
588 | * file. In the end, the situation might not be as bad as it may look:
|
---|
589 | * our heap allocator can save us and just grow the block whenever
|
---|
590 | * possible.
|
---|
591 | */
|
---|
592 | void *newdata = realloc(nodep->data, nodep->size + delta);
|
---|
593 | if (!newdata) {
|
---|
594 | ipc_answer_0(callid, ENOMEM);
|
---|
595 | ipc_answer_2(rid, EOK, 0, nodep->size);
|
---|
596 | return;
|
---|
597 | }
|
---|
598 | /* Clear any newly allocated memory in order to emulate gaps. */
|
---|
599 | memset(newdata + nodep->size, 0, delta);
|
---|
600 | nodep->size += delta;
|
---|
601 | nodep->data = newdata;
|
---|
602 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
---|
603 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
604 | }
|
---|
605 |
|
---|
606 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
607 | {
|
---|
608 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
609 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
610 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
---|
611 |
|
---|
612 | /*
|
---|
613 | * Lookup the respective TMPFS node.
|
---|
614 | */
|
---|
615 | link_t *hlp;
|
---|
616 | unsigned long key[] = {
|
---|
617 | [NODES_KEY_INDEX] = index,
|
---|
618 | [NODES_KEY_DEV] = dev_handle
|
---|
619 | };
|
---|
620 | hlp = hash_table_find(&nodes, key);
|
---|
621 | if (!hlp) {
|
---|
622 | ipc_answer_0(rid, ENOENT);
|
---|
623 | return;
|
---|
624 | }
|
---|
625 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
626 | nh_link);
|
---|
627 |
|
---|
628 | if (size == nodep->size) {
|
---|
629 | ipc_answer_0(rid, EOK);
|
---|
630 | return;
|
---|
631 | }
|
---|
632 |
|
---|
633 | void *newdata = realloc(nodep->data, size);
|
---|
634 | if (!newdata) {
|
---|
635 | ipc_answer_0(rid, ENOMEM);
|
---|
636 | return;
|
---|
637 | }
|
---|
638 | if (size > nodep->size) {
|
---|
639 | size_t delta = size - nodep->size;
|
---|
640 | memset(newdata + nodep->size, 0, delta);
|
---|
641 | }
|
---|
642 | nodep->size = size;
|
---|
643 | nodep->data = newdata;
|
---|
644 | ipc_answer_0(rid, EOK);
|
---|
645 | }
|
---|
646 |
|
---|
647 | void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
648 | {
|
---|
649 | ipc_answer_0(rid, EOK);
|
---|
650 | }
|
---|
651 |
|
---|
652 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
653 | {
|
---|
654 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
655 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
656 | int rc;
|
---|
657 |
|
---|
658 | link_t *hlp;
|
---|
659 | unsigned long key[] = {
|
---|
660 | [NODES_KEY_INDEX] = index,
|
---|
661 | [NODES_KEY_DEV] = dev_handle
|
---|
662 | };
|
---|
663 | hlp = hash_table_find(&nodes, key);
|
---|
664 | if (!hlp) {
|
---|
665 | ipc_answer_0(rid, ENOENT);
|
---|
666 | return;
|
---|
667 | }
|
---|
668 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
669 | nh_link);
|
---|
670 | rc = tmpfs_destroy_node(FS_NODE(nodep));
|
---|
671 | ipc_answer_0(rid, rc);
|
---|
672 | }
|
---|
673 |
|
---|
674 | void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
675 | {
|
---|
676 | libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
677 | }
|
---|
678 |
|
---|
679 | void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
680 | {
|
---|
681 | libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
682 | }
|
---|
683 |
|
---|
684 | void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
685 | {
|
---|
686 | /* Dummy implementation */
|
---|
687 | ipc_answer_0(rid, EOK);
|
---|
688 | }
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * @}
|
---|
692 | */
|
---|