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