source: mainline/uspace/srv/fs/tmpfs/tmpfs_ops.c@ d0a1e9b6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d0a1e9b6 was 4e00f87, checked in by Jakub Jermar <jakub@…>, 13 years ago

Use NULL instead of 0 as a hash_table_ops_t member initializer.

  • Property mode set to 100644
File size: 15.2 KB
Line 
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. */
62fs_index_t tmpfs_next_index = 1;
63
64/*
65 * Implementation of the libfs interface.
66 */
67
68/* Forward declarations of static functions. */
69static int tmpfs_match(fs_node_t **, fs_node_t *, const char *);
70static int tmpfs_node_get(fs_node_t **, service_id_t, fs_index_t);
71static int tmpfs_node_open(fs_node_t *);
72static int tmpfs_node_put(fs_node_t *);
73static int tmpfs_create_node(fs_node_t **, service_id_t, int);
74static int tmpfs_destroy_node(fs_node_t *);
75static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *);
76static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *);
77
78/* Implementation of helper functions. */
79static 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
84static 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
90static fs_index_t tmpfs_index_get(fs_node_t *fn)
91{
92 return TMPFS_NODE(fn)->index;
93}
94
95static aoff64_t tmpfs_size_get(fs_node_t *fn)
96{
97 return TMPFS_NODE(fn)->size;
98}
99
100static unsigned tmpfs_lnkcnt_get(fs_node_t *fn)
101{
102 return TMPFS_NODE(fn)->lnkcnt;
103}
104
105static bool tmpfs_is_directory(fs_node_t *fn)
106{
107 return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
108}
109
110static bool tmpfs_is_file(fs_node_t *fn)
111{
112 return TMPFS_NODE(fn)->type == TMPFS_FILE;
113}
114
115static service_id_t tmpfs_service_get(fs_node_t *fn)
116{
117 return 0;
118}
119
120/** libfs operations */
121libfs_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. */
141hash_table_t nodes;
142
143/*
144 * Implementation of hash table interface for the nodes hash table.
145 */
146
147typedef struct {
148 service_id_t service_id;
149 fs_index_t index;
150} node_key_t;
151
152static 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
158static 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
164static 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
172static 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. */
194hash_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
202static 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
214static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
215{
216 link_initialize(&dentryp->link);
217 dentryp->name = NULL;
218 dentryp->node = NULL;
219}
220
221bool tmpfs_init(void)
222{
223 if (!hash_table_create(&nodes, 0, 0, &nodes_ops))
224 return false;
225
226 return true;
227}
228
229static 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
241static 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
252static void tmpfs_instance_done(service_id_t service_id)
253{
254 hash_table_apply(&nodes, rm_service_id_nodes, &service_id);
255}
256
257int 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, lnk) {
262 tmpfs_dentry_t *dentryp;
263 dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
264 if (!str_cmp(dentryp->name, component)) {
265 *rfn = FS_NODE(dentryp->node);
266 return EOK;
267 }
268 }
269
270 *rfn = NULL;
271 return EOK;
272}
273
274int tmpfs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
275{
276 node_key_t key = {
277 .service_id = service_id,
278 .index = index
279 };
280
281 ht_link_t *lnk = hash_table_find(&nodes, &key);
282
283 if (lnk) {
284 tmpfs_node_t *nodep;
285 nodep = hash_table_get_inst(lnk, tmpfs_node_t, nh_link);
286 *rfn = FS_NODE(nodep);
287 } else {
288 *rfn = NULL;
289 }
290 return EOK;
291}
292
293int tmpfs_node_open(fs_node_t *fn)
294{
295 /* nothing to do */
296 return EOK;
297}
298
299int tmpfs_node_put(fs_node_t *fn)
300{
301 /* nothing to do */
302 return EOK;
303}
304
305int tmpfs_create_node(fs_node_t **rfn, service_id_t service_id, int lflag)
306{
307 fs_node_t *rootfn;
308 int rc;
309
310 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
311
312 tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
313 if (!nodep)
314 return ENOMEM;
315 tmpfs_node_initialize(nodep);
316 nodep->bp = malloc(sizeof(fs_node_t));
317 if (!nodep->bp) {
318 free(nodep);
319 return ENOMEM;
320 }
321 fs_node_initialize(nodep->bp);
322 nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
323
324 rc = tmpfs_root_get(&rootfn, service_id);
325 assert(rc == EOK);
326 if (!rootfn)
327 nodep->index = TMPFS_SOME_ROOT;
328 else
329 nodep->index = tmpfs_next_index++;
330 nodep->service_id = service_id;
331 if (lflag & L_DIRECTORY)
332 nodep->type = TMPFS_DIRECTORY;
333 else
334 nodep->type = TMPFS_FILE;
335
336 /* Insert the new node into the nodes hash table. */
337 hash_table_insert(&nodes, &nodep->nh_link);
338 *rfn = FS_NODE(nodep);
339 return EOK;
340}
341
342int tmpfs_destroy_node(fs_node_t *fn)
343{
344 tmpfs_node_t *nodep = TMPFS_NODE(fn);
345
346 assert(!nodep->lnkcnt);
347 assert(list_empty(&nodep->cs_list));
348
349 hash_table_remove_item(&nodes, &nodep->nh_link);
350
351 /*
352 * The nodes_remove_callback() function takes care of the actual
353 * resource deallocation.
354 */
355 return EOK;
356}
357
358int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
359{
360 tmpfs_node_t *parentp = TMPFS_NODE(pfn);
361 tmpfs_node_t *childp = TMPFS_NODE(cfn);
362 tmpfs_dentry_t *dentryp;
363
364 assert(parentp->type == TMPFS_DIRECTORY);
365
366 /* Check for duplicit entries. */
367 list_foreach(parentp->cs_list, lnk) {
368 dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
369 if (!str_cmp(dentryp->name, nm))
370 return EEXIST;
371 }
372
373 /* Allocate and initialize the dentry. */
374 dentryp = malloc(sizeof(tmpfs_dentry_t));
375 if (!dentryp)
376 return ENOMEM;
377 tmpfs_dentry_initialize(dentryp);
378
379 /* Populate and link the new dentry. */
380 size_t size = str_size(nm);
381 dentryp->name = malloc(size + 1);
382 if (!dentryp->name) {
383 free(dentryp);
384 return ENOMEM;
385 }
386 str_cpy(dentryp->name, size + 1, nm);
387 dentryp->node = childp;
388 childp->lnkcnt++;
389 list_append(&dentryp->link, &parentp->cs_list);
390
391 return EOK;
392}
393
394int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
395{
396 tmpfs_node_t *parentp = TMPFS_NODE(pfn);
397 tmpfs_node_t *childp = NULL;
398 tmpfs_dentry_t *dentryp;
399
400 if (!parentp)
401 return EBUSY;
402
403 list_foreach(parentp->cs_list, lnk) {
404 dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
405 if (!str_cmp(dentryp->name, nm)) {
406 childp = dentryp->node;
407 assert(FS_NODE(childp) == cfn);
408 break;
409 }
410 }
411
412 if (!childp)
413 return ENOENT;
414
415 if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_list))
416 return ENOTEMPTY;
417
418 list_remove(&dentryp->link);
419 free(dentryp);
420 childp->lnkcnt--;
421
422 return EOK;
423}
424
425/*
426 * Implementation of the VFS_OUT interface.
427 */
428
429static int
430tmpfs_mounted(service_id_t service_id, const char *opts,
431 fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
432{
433 fs_node_t *rootfn;
434 int rc;
435
436 /* Check if this device is not already mounted. */
437 rc = tmpfs_root_get(&rootfn, service_id);
438 if ((rc == EOK) && (rootfn)) {
439 (void) tmpfs_node_put(rootfn);
440 return EEXIST;
441 }
442
443 /* Initialize TMPFS instance. */
444 if (!tmpfs_instance_init(service_id))
445 return ENOMEM;
446
447 rc = tmpfs_root_get(&rootfn, service_id);
448 assert(rc == EOK);
449 tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
450 if (str_cmp(opts, "restore") == 0) {
451 if (!tmpfs_restore(service_id))
452 return ELIMIT;
453 }
454
455 *index = rootp->index;
456 *size = rootp->size;
457 *lnkcnt = rootp->lnkcnt;
458
459 return EOK;
460}
461
462static int tmpfs_unmounted(service_id_t service_id)
463{
464 tmpfs_instance_done(service_id);
465 return EOK;
466}
467
468static int tmpfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
469 size_t *rbytes)
470{
471 /*
472 * Lookup the respective TMPFS node.
473 */
474 node_key_t key = {
475 .service_id = service_id,
476 .index = index
477 };
478
479 ht_link_t *hlp = hash_table_find(&nodes, &key);
480 if (!hlp)
481 return ENOENT;
482
483 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
484
485 /*
486 * Receive the read request.
487 */
488 ipc_callid_t callid;
489 size_t size;
490 if (!async_data_read_receive(&callid, &size)) {
491 async_answer_0(callid, EINVAL);
492 return EINVAL;
493 }
494
495 size_t bytes;
496 if (nodep->type == TMPFS_FILE) {
497 bytes = min(nodep->size - pos, size);
498 (void) async_data_read_finalize(callid, nodep->data + pos,
499 bytes);
500 } else {
501 tmpfs_dentry_t *dentryp;
502 link_t *lnk;
503
504 assert(nodep->type == TMPFS_DIRECTORY);
505
506 /*
507 * Yes, we really use O(n) algorithm here.
508 * If it bothers someone, it could be fixed by introducing a
509 * hash table.
510 */
511 lnk = list_nth(&nodep->cs_list, pos);
512
513 if (lnk == NULL) {
514 async_answer_0(callid, ENOENT);
515 return ENOENT;
516 }
517
518 dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
519
520 (void) async_data_read_finalize(callid, dentryp->name,
521 str_size(dentryp->name) + 1);
522 bytes = 1;
523 }
524
525 *rbytes = bytes;
526 return EOK;
527}
528
529static int
530tmpfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
531 size_t *wbytes, aoff64_t *nsize)
532{
533 /*
534 * Lookup the respective TMPFS node.
535 */
536 node_key_t key = {
537 .service_id = service_id,
538 .index = index
539 };
540
541 ht_link_t *hlp = hash_table_find(&nodes, &key);
542
543 if (!hlp)
544 return ENOENT;
545
546 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
547
548 /*
549 * Receive the write request.
550 */
551 ipc_callid_t callid;
552 size_t size;
553 if (!async_data_write_receive(&callid, &size)) {
554 async_answer_0(callid, EINVAL);
555 return EINVAL;
556 }
557
558 /*
559 * Check whether the file needs to grow.
560 */
561 if (pos + size <= nodep->size) {
562 /* The file size is not changing. */
563 (void) async_data_write_finalize(callid, nodep->data + pos,
564 size);
565 goto out;
566 }
567 size_t delta = (pos + size) - nodep->size;
568 /*
569 * At this point, we are deliberately extremely straightforward and
570 * simply realloc the contents of the file on every write that grows the
571 * file. In the end, the situation might not be as bad as it may look:
572 * our heap allocator can save us and just grow the block whenever
573 * possible.
574 */
575 void *newdata = realloc(nodep->data, nodep->size + delta);
576 if (!newdata) {
577 async_answer_0(callid, ENOMEM);
578 size = 0;
579 goto out;
580 }
581 /* Clear any newly allocated memory in order to emulate gaps. */
582 memset(newdata + nodep->size, 0, delta);
583 nodep->size += delta;
584 nodep->data = newdata;
585 (void) async_data_write_finalize(callid, nodep->data + pos, size);
586
587out:
588 *wbytes = size;
589 *nsize = nodep->size;
590 return EOK;
591}
592
593static int tmpfs_truncate(service_id_t service_id, fs_index_t index,
594 aoff64_t size)
595{
596 /*
597 * Lookup the respective TMPFS node.
598 */
599 node_key_t key = {
600 .service_id = service_id,
601 .index = index
602 };
603
604 ht_link_t *hlp = hash_table_find(&nodes, &key);
605
606 if (!hlp)
607 return ENOENT;
608 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
609
610 if (size == nodep->size)
611 return EOK;
612
613 if (size > SIZE_MAX)
614 return ENOMEM;
615
616 void *newdata = realloc(nodep->data, size);
617 if (!newdata)
618 return ENOMEM;
619
620 if (size > nodep->size) {
621 size_t delta = size - nodep->size;
622 memset(newdata + nodep->size, 0, delta);
623 }
624
625 nodep->size = size;
626 nodep->data = newdata;
627 return EOK;
628}
629
630static int tmpfs_close(service_id_t service_id, fs_index_t index)
631{
632 return EOK;
633}
634
635static int tmpfs_destroy(service_id_t service_id, fs_index_t index)
636{
637 node_key_t key = {
638 .service_id = service_id,
639 .index = index
640 };
641
642 ht_link_t *hlp = hash_table_find(&nodes, &key);
643 if (!hlp)
644 return ENOENT;
645 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t,
646 nh_link);
647 return tmpfs_destroy_node(FS_NODE(nodep));
648}
649
650static int tmpfs_sync(service_id_t service_id, fs_index_t index)
651{
652 /*
653 * TMPFS keeps its data structures always consistent,
654 * thus the sync operation is a no-op.
655 */
656 return EOK;
657}
658
659vfs_out_ops_t tmpfs_ops = {
660 .mounted = tmpfs_mounted,
661 .unmounted = tmpfs_unmounted,
662 .read = tmpfs_read,
663 .write = tmpfs_write,
664 .truncate = tmpfs_truncate,
665 .close = tmpfs_close,
666 .destroy = tmpfs_destroy,
667 .sync = tmpfs_sync,
668};
669
670/**
671 * @}
672 */
673
Note: See TracBrowser for help on using the repository browser.