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_DEV 0
|
---|
150 | #define NODES_KEY_INDEX 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 |
|
---|
163 | switch (keys) {
|
---|
164 | case 1:
|
---|
165 | return (nodep->dev_handle == key[NODES_KEY_DEV]);
|
---|
166 | case 2:
|
---|
167 | return ((nodep->dev_handle == key[NODES_KEY_DEV]) &&
|
---|
168 | (nodep->index == key[NODES_KEY_INDEX]));
|
---|
169 | default:
|
---|
170 | assert((keys == 1) || (keys == 2));
|
---|
171 | }
|
---|
172 |
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | static void nodes_remove_callback(link_t *item)
|
---|
177 | {
|
---|
178 | tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t,
|
---|
179 | nh_link);
|
---|
180 |
|
---|
181 | while (!list_empty(&nodep->cs_head)) {
|
---|
182 | tmpfs_dentry_t *dentryp = list_get_instance(nodep->cs_head.next,
|
---|
183 | tmpfs_dentry_t, link);
|
---|
184 |
|
---|
185 | assert(nodep->type == TMPFS_DIRECTORY);
|
---|
186 | list_remove(&dentryp->link);
|
---|
187 | free(dentryp);
|
---|
188 | }
|
---|
189 |
|
---|
190 | if (nodep->data) {
|
---|
191 | assert(nodep->type == TMPFS_FILE);
|
---|
192 | free(nodep->data);
|
---|
193 | }
|
---|
194 | free(nodep->bp);
|
---|
195 | free(nodep);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** TMPFS nodes hash table operations. */
|
---|
199 | hash_table_operations_t nodes_ops = {
|
---|
200 | .hash = nodes_hash,
|
---|
201 | .compare = nodes_compare,
|
---|
202 | .remove_callback = nodes_remove_callback
|
---|
203 | };
|
---|
204 |
|
---|
205 | static void tmpfs_node_initialize(tmpfs_node_t *nodep)
|
---|
206 | {
|
---|
207 | nodep->bp = NULL;
|
---|
208 | nodep->index = 0;
|
---|
209 | nodep->dev_handle = 0;
|
---|
210 | nodep->type = TMPFS_NONE;
|
---|
211 | nodep->lnkcnt = 0;
|
---|
212 | nodep->size = 0;
|
---|
213 | nodep->data = NULL;
|
---|
214 | link_initialize(&nodep->nh_link);
|
---|
215 | list_initialize(&nodep->cs_head);
|
---|
216 | }
|
---|
217 |
|
---|
218 | static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
|
---|
219 | {
|
---|
220 | link_initialize(&dentryp->link);
|
---|
221 | dentryp->name = NULL;
|
---|
222 | dentryp->node = NULL;
|
---|
223 | }
|
---|
224 |
|
---|
225 | bool tmpfs_init(void)
|
---|
226 | {
|
---|
227 | if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
|
---|
228 | return false;
|
---|
229 |
|
---|
230 | return true;
|
---|
231 | }
|
---|
232 |
|
---|
233 | static bool tmpfs_instance_init(dev_handle_t dev_handle)
|
---|
234 | {
|
---|
235 | fs_node_t *rfn;
|
---|
236 | int rc;
|
---|
237 |
|
---|
238 | rc = tmpfs_create_node(&rfn, dev_handle, L_DIRECTORY);
|
---|
239 | if (rc != EOK || !rfn)
|
---|
240 | return false;
|
---|
241 | TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
|
---|
242 | return true;
|
---|
243 | }
|
---|
244 |
|
---|
245 | static void tmpfs_instance_done(dev_handle_t dev_handle)
|
---|
246 | {
|
---|
247 | unsigned long key[] = {
|
---|
248 | [NODES_KEY_DEV] = dev_handle
|
---|
249 | };
|
---|
250 | /*
|
---|
251 | * Here we are making use of one special feature of our hash table
|
---|
252 | * implementation, which allows to remove more items based on a partial
|
---|
253 | * key match. In the following, we are going to remove all nodes
|
---|
254 | * matching our device handle. The nodes_remove_callback() function will
|
---|
255 | * take care of resource deallocation.
|
---|
256 | */
|
---|
257 | hash_table_remove(&nodes, key, 1);
|
---|
258 | }
|
---|
259 |
|
---|
260 | int tmpfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
---|
261 | {
|
---|
262 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
263 | link_t *lnk;
|
---|
264 |
|
---|
265 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
266 | lnk = lnk->next) {
|
---|
267 | tmpfs_dentry_t *dentryp;
|
---|
268 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
269 | if (!str_cmp(dentryp->name, component)) {
|
---|
270 | *rfn = FS_NODE(dentryp->node);
|
---|
271 | return EOK;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | *rfn = NULL;
|
---|
276 | return EOK;
|
---|
277 | }
|
---|
278 |
|
---|
279 | int tmpfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
|
---|
280 | {
|
---|
281 | unsigned long key[] = {
|
---|
282 | [NODES_KEY_DEV] = dev_handle,
|
---|
283 | [NODES_KEY_INDEX] = index
|
---|
284 | };
|
---|
285 | link_t *lnk = hash_table_find(&nodes, key);
|
---|
286 | if (lnk) {
|
---|
287 | tmpfs_node_t *nodep;
|
---|
288 | nodep = hash_table_get_instance(lnk, tmpfs_node_t, nh_link);
|
---|
289 | *rfn = FS_NODE(nodep);
|
---|
290 | } else {
|
---|
291 | *rfn = NULL;
|
---|
292 | }
|
---|
293 | return EOK;
|
---|
294 | }
|
---|
295 |
|
---|
296 | int tmpfs_node_open(fs_node_t *fn)
|
---|
297 | {
|
---|
298 | /* nothing to do */
|
---|
299 | return EOK;
|
---|
300 | }
|
---|
301 |
|
---|
302 | int tmpfs_node_put(fs_node_t *fn)
|
---|
303 | {
|
---|
304 | /* nothing to do */
|
---|
305 | return EOK;
|
---|
306 | }
|
---|
307 |
|
---|
308 | int tmpfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
|
---|
309 | {
|
---|
310 | fs_node_t *rootfn;
|
---|
311 | int rc;
|
---|
312 |
|
---|
313 | assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
|
---|
314 |
|
---|
315 | tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
|
---|
316 | if (!nodep)
|
---|
317 | return ENOMEM;
|
---|
318 | tmpfs_node_initialize(nodep);
|
---|
319 | nodep->bp = malloc(sizeof(fs_node_t));
|
---|
320 | if (!nodep->bp) {
|
---|
321 | free(nodep);
|
---|
322 | return ENOMEM;
|
---|
323 | }
|
---|
324 | fs_node_initialize(nodep->bp);
|
---|
325 | nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
|
---|
326 |
|
---|
327 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
---|
328 | assert(rc == EOK);
|
---|
329 | if (!rootfn)
|
---|
330 | nodep->index = TMPFS_SOME_ROOT;
|
---|
331 | else
|
---|
332 | nodep->index = tmpfs_next_index++;
|
---|
333 | nodep->dev_handle = dev_handle;
|
---|
334 | if (lflag & L_DIRECTORY)
|
---|
335 | nodep->type = TMPFS_DIRECTORY;
|
---|
336 | else
|
---|
337 | nodep->type = TMPFS_FILE;
|
---|
338 |
|
---|
339 | /* Insert the new node into the nodes hash table. */
|
---|
340 | unsigned long key[] = {
|
---|
341 | [NODES_KEY_DEV] = nodep->dev_handle,
|
---|
342 | [NODES_KEY_INDEX] = nodep->index
|
---|
343 | };
|
---|
344 | hash_table_insert(&nodes, key, &nodep->nh_link);
|
---|
345 | *rfn = FS_NODE(nodep);
|
---|
346 | return EOK;
|
---|
347 | }
|
---|
348 |
|
---|
349 | int tmpfs_destroy_node(fs_node_t *fn)
|
---|
350 | {
|
---|
351 | tmpfs_node_t *nodep = TMPFS_NODE(fn);
|
---|
352 |
|
---|
353 | assert(!nodep->lnkcnt);
|
---|
354 | assert(list_empty(&nodep->cs_head));
|
---|
355 |
|
---|
356 | unsigned long key[] = {
|
---|
357 | [NODES_KEY_DEV] = nodep->dev_handle,
|
---|
358 | [NODES_KEY_INDEX] = nodep->index
|
---|
359 | };
|
---|
360 | hash_table_remove(&nodes, key, 2);
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * The nodes_remove_callback() function takes care of the actual
|
---|
364 | * resource deallocation.
|
---|
365 | */
|
---|
366 | return EOK;
|
---|
367 | }
|
---|
368 |
|
---|
369 | int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
370 | {
|
---|
371 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
372 | tmpfs_node_t *childp = TMPFS_NODE(cfn);
|
---|
373 | tmpfs_dentry_t *dentryp;
|
---|
374 | link_t *lnk;
|
---|
375 |
|
---|
376 | assert(parentp->type == TMPFS_DIRECTORY);
|
---|
377 |
|
---|
378 | /* Check for duplicit entries. */
|
---|
379 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
380 | lnk = lnk->next) {
|
---|
381 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
382 | if (!str_cmp(dentryp->name, nm))
|
---|
383 | return EEXIST;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* Allocate and initialize the dentry. */
|
---|
387 | dentryp = malloc(sizeof(tmpfs_dentry_t));
|
---|
388 | if (!dentryp)
|
---|
389 | return ENOMEM;
|
---|
390 | tmpfs_dentry_initialize(dentryp);
|
---|
391 |
|
---|
392 | /* Populate and link the new dentry. */
|
---|
393 | size_t size = str_size(nm);
|
---|
394 | dentryp->name = malloc(size + 1);
|
---|
395 | if (!dentryp->name) {
|
---|
396 | free(dentryp);
|
---|
397 | return ENOMEM;
|
---|
398 | }
|
---|
399 | str_cpy(dentryp->name, size + 1, nm);
|
---|
400 | dentryp->node = childp;
|
---|
401 | childp->lnkcnt++;
|
---|
402 | list_append(&dentryp->link, &parentp->cs_head);
|
---|
403 |
|
---|
404 | return EOK;
|
---|
405 | }
|
---|
406 |
|
---|
407 | int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
|
---|
408 | {
|
---|
409 | tmpfs_node_t *parentp = TMPFS_NODE(pfn);
|
---|
410 | tmpfs_node_t *childp = NULL;
|
---|
411 | tmpfs_dentry_t *dentryp;
|
---|
412 | link_t *lnk;
|
---|
413 |
|
---|
414 | if (!parentp)
|
---|
415 | return EBUSY;
|
---|
416 |
|
---|
417 | for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head;
|
---|
418 | lnk = lnk->next) {
|
---|
419 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
420 | if (!str_cmp(dentryp->name, nm)) {
|
---|
421 | childp = dentryp->node;
|
---|
422 | assert(FS_NODE(childp) == cfn);
|
---|
423 | break;
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | if (!childp)
|
---|
428 | return ENOENT;
|
---|
429 |
|
---|
430 | if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
|
---|
431 | return ENOTEMPTY;
|
---|
432 |
|
---|
433 | list_remove(&dentryp->link);
|
---|
434 | free(dentryp);
|
---|
435 | childp->lnkcnt--;
|
---|
436 |
|
---|
437 | return EOK;
|
---|
438 | }
|
---|
439 |
|
---|
440 | void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
441 | {
|
---|
442 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
443 | fs_node_t *rootfn;
|
---|
444 | int rc;
|
---|
445 |
|
---|
446 | /* Accept the mount options. */
|
---|
447 | char *opts;
|
---|
448 | rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
---|
449 | if (rc != EOK) {
|
---|
450 | ipc_answer_0(rid, rc);
|
---|
451 | return;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /* Check if this device is not already mounted. */
|
---|
455 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
---|
456 | if ((rc == EOK) && (rootfn)) {
|
---|
457 | (void) tmpfs_node_put(rootfn);
|
---|
458 | free(opts);
|
---|
459 | ipc_answer_0(rid, EEXIST);
|
---|
460 | return;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /* Initialize TMPFS instance. */
|
---|
464 | if (!tmpfs_instance_init(dev_handle)) {
|
---|
465 | free(opts);
|
---|
466 | ipc_answer_0(rid, ENOMEM);
|
---|
467 | return;
|
---|
468 | }
|
---|
469 |
|
---|
470 | rc = tmpfs_root_get(&rootfn, dev_handle);
|
---|
471 | assert(rc == EOK);
|
---|
472 | tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
|
---|
473 | if (str_cmp(opts, "restore") == 0) {
|
---|
474 | if (tmpfs_restore(dev_handle))
|
---|
475 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
476 | rootp->lnkcnt);
|
---|
477 | else
|
---|
478 | ipc_answer_0(rid, ELIMIT);
|
---|
479 | } else {
|
---|
480 | ipc_answer_3(rid, EOK, rootp->index, rootp->size,
|
---|
481 | rootp->lnkcnt);
|
---|
482 | }
|
---|
483 | free(opts);
|
---|
484 | }
|
---|
485 |
|
---|
486 | void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
---|
487 | {
|
---|
488 | libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
489 | }
|
---|
490 |
|
---|
491 | void tmpfs_unmounted(ipc_callid_t rid, ipc_call_t *request)
|
---|
492 | {
|
---|
493 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
|
---|
494 |
|
---|
495 | tmpfs_instance_done(dev_handle);
|
---|
496 | ipc_answer_0(rid, EOK);
|
---|
497 | }
|
---|
498 |
|
---|
499 | void tmpfs_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
500 | {
|
---|
501 | libfs_unmount(&tmpfs_libfs_ops, rid, request);
|
---|
502 | }
|
---|
503 |
|
---|
504 | void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
---|
505 | {
|
---|
506 | libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
507 | }
|
---|
508 |
|
---|
509 | void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
510 | {
|
---|
511 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
512 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
513 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
514 |
|
---|
515 | /*
|
---|
516 | * Lookup the respective TMPFS node.
|
---|
517 | */
|
---|
518 | link_t *hlp;
|
---|
519 | unsigned long key[] = {
|
---|
520 | [NODES_KEY_DEV] = dev_handle,
|
---|
521 | [NODES_KEY_INDEX] = index
|
---|
522 | };
|
---|
523 | hlp = hash_table_find(&nodes, key);
|
---|
524 | if (!hlp) {
|
---|
525 | ipc_answer_0(rid, ENOENT);
|
---|
526 | return;
|
---|
527 | }
|
---|
528 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
529 | nh_link);
|
---|
530 |
|
---|
531 | /*
|
---|
532 | * Receive the read request.
|
---|
533 | */
|
---|
534 | ipc_callid_t callid;
|
---|
535 | size_t size;
|
---|
536 | if (!async_data_read_receive(&callid, &size)) {
|
---|
537 | ipc_answer_0(callid, EINVAL);
|
---|
538 | ipc_answer_0(rid, EINVAL);
|
---|
539 | return;
|
---|
540 | }
|
---|
541 |
|
---|
542 | size_t bytes;
|
---|
543 | if (nodep->type == TMPFS_FILE) {
|
---|
544 | bytes = max(0, min(nodep->size - pos, size));
|
---|
545 | (void) async_data_read_finalize(callid, nodep->data + pos,
|
---|
546 | bytes);
|
---|
547 | } else {
|
---|
548 | tmpfs_dentry_t *dentryp;
|
---|
549 | link_t *lnk;
|
---|
550 | int i;
|
---|
551 |
|
---|
552 | assert(nodep->type == TMPFS_DIRECTORY);
|
---|
553 |
|
---|
554 | /*
|
---|
555 | * Yes, we really use O(n) algorithm here.
|
---|
556 | * If it bothers someone, it could be fixed by introducing a
|
---|
557 | * hash table.
|
---|
558 | */
|
---|
559 | for (i = 0, lnk = nodep->cs_head.next;
|
---|
560 | i < pos && lnk != &nodep->cs_head;
|
---|
561 | i++, lnk = lnk->next)
|
---|
562 | ;
|
---|
563 |
|
---|
564 | if (lnk == &nodep->cs_head) {
|
---|
565 | ipc_answer_0(callid, ENOENT);
|
---|
566 | ipc_answer_1(rid, ENOENT, 0);
|
---|
567 | return;
|
---|
568 | }
|
---|
569 |
|
---|
570 | dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
|
---|
571 |
|
---|
572 | (void) async_data_read_finalize(callid, dentryp->name,
|
---|
573 | str_size(dentryp->name) + 1);
|
---|
574 | bytes = 1;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /*
|
---|
578 | * Answer the VFS_READ call.
|
---|
579 | */
|
---|
580 | ipc_answer_1(rid, EOK, bytes);
|
---|
581 | }
|
---|
582 |
|
---|
583 | void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
584 | {
|
---|
585 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
586 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
587 | off_t pos = (off_t)IPC_GET_ARG3(*request);
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Lookup the respective TMPFS node.
|
---|
591 | */
|
---|
592 | link_t *hlp;
|
---|
593 | unsigned long key[] = {
|
---|
594 | [NODES_KEY_DEV] = dev_handle,
|
---|
595 | [NODES_KEY_INDEX] = index
|
---|
596 | };
|
---|
597 | hlp = hash_table_find(&nodes, key);
|
---|
598 | if (!hlp) {
|
---|
599 | ipc_answer_0(rid, ENOENT);
|
---|
600 | return;
|
---|
601 | }
|
---|
602 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
603 | nh_link);
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Receive the write request.
|
---|
607 | */
|
---|
608 | ipc_callid_t callid;
|
---|
609 | size_t size;
|
---|
610 | if (!async_data_write_receive(&callid, &size)) {
|
---|
611 | ipc_answer_0(callid, EINVAL);
|
---|
612 | ipc_answer_0(rid, EINVAL);
|
---|
613 | return;
|
---|
614 | }
|
---|
615 |
|
---|
616 | /*
|
---|
617 | * Check whether the file needs to grow.
|
---|
618 | */
|
---|
619 | if (pos + size <= nodep->size) {
|
---|
620 | /* The file size is not changing. */
|
---|
621 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
---|
622 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
623 | return;
|
---|
624 | }
|
---|
625 | size_t delta = (pos + size) - nodep->size;
|
---|
626 | /*
|
---|
627 | * At this point, we are deliberately extremely straightforward and
|
---|
628 | * simply realloc the contents of the file on every write that grows the
|
---|
629 | * file. In the end, the situation might not be as bad as it may look:
|
---|
630 | * our heap allocator can save us and just grow the block whenever
|
---|
631 | * possible.
|
---|
632 | */
|
---|
633 | void *newdata = realloc(nodep->data, nodep->size + delta);
|
---|
634 | if (!newdata) {
|
---|
635 | ipc_answer_0(callid, ENOMEM);
|
---|
636 | ipc_answer_2(rid, EOK, 0, nodep->size);
|
---|
637 | return;
|
---|
638 | }
|
---|
639 | /* Clear any newly allocated memory in order to emulate gaps. */
|
---|
640 | memset(newdata + nodep->size, 0, delta);
|
---|
641 | nodep->size += delta;
|
---|
642 | nodep->data = newdata;
|
---|
643 | (void) async_data_write_finalize(callid, nodep->data + pos, size);
|
---|
644 | ipc_answer_2(rid, EOK, size, nodep->size);
|
---|
645 | }
|
---|
646 |
|
---|
647 | void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
648 | {
|
---|
649 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
650 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
651 | size_t size = (off_t)IPC_GET_ARG3(*request);
|
---|
652 |
|
---|
653 | /*
|
---|
654 | * Lookup the respective TMPFS node.
|
---|
655 | */
|
---|
656 | link_t *hlp;
|
---|
657 | unsigned long key[] = {
|
---|
658 | [NODES_KEY_DEV] = dev_handle,
|
---|
659 | [NODES_KEY_INDEX] = index
|
---|
660 | };
|
---|
661 | hlp = hash_table_find(&nodes, key);
|
---|
662 | if (!hlp) {
|
---|
663 | ipc_answer_0(rid, ENOENT);
|
---|
664 | return;
|
---|
665 | }
|
---|
666 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
667 | nh_link);
|
---|
668 |
|
---|
669 | if (size == nodep->size) {
|
---|
670 | ipc_answer_0(rid, EOK);
|
---|
671 | return;
|
---|
672 | }
|
---|
673 |
|
---|
674 | void *newdata = realloc(nodep->data, size);
|
---|
675 | if (!newdata) {
|
---|
676 | ipc_answer_0(rid, ENOMEM);
|
---|
677 | return;
|
---|
678 | }
|
---|
679 | if (size > nodep->size) {
|
---|
680 | size_t delta = size - nodep->size;
|
---|
681 | memset(newdata + nodep->size, 0, delta);
|
---|
682 | }
|
---|
683 | nodep->size = size;
|
---|
684 | nodep->data = newdata;
|
---|
685 | ipc_answer_0(rid, EOK);
|
---|
686 | }
|
---|
687 |
|
---|
688 | void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
|
---|
689 | {
|
---|
690 | ipc_answer_0(rid, EOK);
|
---|
691 | }
|
---|
692 |
|
---|
693 | void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
|
---|
694 | {
|
---|
695 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
|
---|
696 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
|
---|
697 | int rc;
|
---|
698 |
|
---|
699 | link_t *hlp;
|
---|
700 | unsigned long key[] = {
|
---|
701 | [NODES_KEY_DEV] = dev_handle,
|
---|
702 | [NODES_KEY_INDEX] = index
|
---|
703 | };
|
---|
704 | hlp = hash_table_find(&nodes, key);
|
---|
705 | if (!hlp) {
|
---|
706 | ipc_answer_0(rid, ENOENT);
|
---|
707 | return;
|
---|
708 | }
|
---|
709 | tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
|
---|
710 | nh_link);
|
---|
711 | rc = tmpfs_destroy_node(FS_NODE(nodep));
|
---|
712 | ipc_answer_0(rid, rc);
|
---|
713 | }
|
---|
714 |
|
---|
715 | void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
---|
716 | {
|
---|
717 | libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
718 | }
|
---|
719 |
|
---|
720 | void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
---|
721 | {
|
---|
722 | libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
|
---|
723 | }
|
---|
724 |
|
---|
725 | void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
726 | {
|
---|
727 | /* Dummy implementation */
|
---|
728 | ipc_answer_0(rid, EOK);
|
---|
729 | }
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * @}
|
---|
733 | */
|
---|