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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b33870b was b33870b, checked in by Martin Decky <martin@…>, 14 years ago

rename device_get() method to service_get() to better reflect changes in the location service

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