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

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

introduce device namespaces

  • add support for explicit open in libfs (needed by devfs, but also possibly for other filesystems which need to track some stateful information)
  • extend libfs to be more generic, make proper adjustments to libc, tmpfs and fat
  • various updates to make use of the device namespaces
  • code cleanup
  • Property mode set to 100644
File size: 16.1 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 <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. */
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 **, dev_handle_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 **, dev_handle_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, dev_handle_t dev_handle)
80{
81 return tmpfs_node_get(rfn, dev_handle, 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_head);
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 size_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 char tmpfs_plb_get_char(unsigned pos)
106{
107 return tmpfs_reg.plb_ro[pos % PLB_SIZE];
108}
109
110static bool tmpfs_is_directory(fs_node_t *fn)
111{
112 return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
113}
114
115static bool tmpfs_is_file(fs_node_t *fn)
116{
117 return TMPFS_NODE(fn)->type == TMPFS_FILE;
118}
119
120static dev_handle_t tmpfs_device_get(fs_node_t *fn)
121{
122 return 0;
123}
124
125/** libfs operations */
126libfs_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. */
147hash_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. */
153static hash_index_t nodes_hash(unsigned long key[])
154{
155 return key[NODES_KEY_INDEX] % NODES_BUCKETS;
156}
157
158static 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
166static void nodes_remove_callback(link_t *item)
167{
168}
169
170/** TMPFS nodes hash table operations. */
171hash_table_operations_t nodes_ops = {
172 .hash = nodes_hash,
173 .compare = nodes_compare,
174 .remove_callback = nodes_remove_callback
175};
176
177static 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
190static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
191{
192 link_initialize(&dentryp->link);
193 dentryp->name = NULL;
194 dentryp->node = NULL;
195}
196
197bool tmpfs_init(void)
198{
199 if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
200 return false;
201
202 return true;
203}
204
205static 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
217int 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
236int 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
253int tmpfs_node_open(fs_node_t *fn)
254{
255 /* nothing to do */
256 return EOK;
257}
258
259int tmpfs_node_put(fs_node_t *fn)
260{
261 /* nothing to do */
262 return EOK;
263}
264
265int 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
306int 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
326int 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
364int 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
397void 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 ipc_answer_0(rid, ENOMEM);
427 return;
428 }
429
430 fs_node_t *rootfn;
431 rc = tmpfs_root_get(&rootfn, dev_handle);
432 assert(rc == EOK);
433 tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
434 if (str_cmp(opts, "restore") == 0) {
435 if (tmpfs_restore(dev_handle))
436 ipc_answer_3(rid, EOK, rootp->index, rootp->size,
437 rootp->lnkcnt);
438 else
439 ipc_answer_0(rid, ELIMIT);
440 } else {
441 ipc_answer_3(rid, EOK, rootp->index, rootp->size,
442 rootp->lnkcnt);
443 }
444}
445
446void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
447{
448 libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
449}
450
451void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
452{
453 libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
454}
455
456void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
457{
458 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
459 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
460 off_t pos = (off_t)IPC_GET_ARG3(*request);
461
462 /*
463 * Lookup the respective TMPFS node.
464 */
465 link_t *hlp;
466 unsigned long key[] = {
467 [NODES_KEY_INDEX] = index,
468 [NODES_KEY_DEV] = dev_handle,
469 };
470 hlp = hash_table_find(&nodes, key);
471 if (!hlp) {
472 ipc_answer_0(rid, ENOENT);
473 return;
474 }
475 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
476 nh_link);
477
478 /*
479 * Receive the read request.
480 */
481 ipc_callid_t callid;
482 size_t size;
483 if (!async_data_read_receive(&callid, &size)) {
484 ipc_answer_0(callid, EINVAL);
485 ipc_answer_0(rid, EINVAL);
486 return;
487 }
488
489 size_t bytes;
490 if (nodep->type == TMPFS_FILE) {
491 bytes = max(0, min(nodep->size - pos, size));
492 (void) async_data_read_finalize(callid, nodep->data + pos,
493 bytes);
494 } else {
495 tmpfs_dentry_t *dentryp;
496 link_t *lnk;
497 int i;
498
499 assert(nodep->type == TMPFS_DIRECTORY);
500
501 /*
502 * Yes, we really use O(n) algorithm here.
503 * If it bothers someone, it could be fixed by introducing a
504 * hash table.
505 */
506 for (i = 0, lnk = nodep->cs_head.next;
507 i < pos && lnk != &nodep->cs_head;
508 i++, lnk = lnk->next)
509 ;
510
511 if (lnk == &nodep->cs_head) {
512 ipc_answer_0(callid, ENOENT);
513 ipc_answer_1(rid, ENOENT, 0);
514 return;
515 }
516
517 dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
518
519 (void) async_data_read_finalize(callid, dentryp->name,
520 str_size(dentryp->name) + 1);
521 bytes = 1;
522 }
523
524 /*
525 * Answer the VFS_READ call.
526 */
527 ipc_answer_1(rid, EOK, bytes);
528}
529
530void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
531{
532 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
533 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
534 off_t pos = (off_t)IPC_GET_ARG3(*request);
535
536 /*
537 * Lookup the respective TMPFS node.
538 */
539 link_t *hlp;
540 unsigned long key[] = {
541 [NODES_KEY_INDEX] = index,
542 [NODES_KEY_DEV] = dev_handle
543 };
544 hlp = hash_table_find(&nodes, key);
545 if (!hlp) {
546 ipc_answer_0(rid, ENOENT);
547 return;
548 }
549 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
550 nh_link);
551
552 /*
553 * Receive the write request.
554 */
555 ipc_callid_t callid;
556 size_t size;
557 if (!async_data_write_receive(&callid, &size)) {
558 ipc_answer_0(callid, EINVAL);
559 ipc_answer_0(rid, EINVAL);
560 return;
561 }
562
563 /*
564 * Check whether the file needs to grow.
565 */
566 if (pos + size <= nodep->size) {
567 /* The file size is not changing. */
568 (void) async_data_write_finalize(callid, nodep->data + pos, size);
569 ipc_answer_2(rid, EOK, size, nodep->size);
570 return;
571 }
572 size_t delta = (pos + size) - nodep->size;
573 /*
574 * At this point, we are deliberately extremely straightforward and
575 * simply realloc the contents of the file on every write that grows the
576 * file. In the end, the situation might not be as bad as it may look:
577 * our heap allocator can save us and just grow the block whenever
578 * possible.
579 */
580 void *newdata = realloc(nodep->data, nodep->size + delta);
581 if (!newdata) {
582 ipc_answer_0(callid, ENOMEM);
583 ipc_answer_2(rid, EOK, 0, nodep->size);
584 return;
585 }
586 /* Clear any newly allocated memory in order to emulate gaps. */
587 memset(newdata + nodep->size, 0, delta);
588 nodep->size += delta;
589 nodep->data = newdata;
590 (void) async_data_write_finalize(callid, nodep->data + pos, size);
591 ipc_answer_2(rid, EOK, size, nodep->size);
592}
593
594void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
595{
596 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
597 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
598 size_t size = (off_t)IPC_GET_ARG3(*request);
599
600 /*
601 * Lookup the respective TMPFS node.
602 */
603 link_t *hlp;
604 unsigned long key[] = {
605 [NODES_KEY_INDEX] = index,
606 [NODES_KEY_DEV] = dev_handle
607 };
608 hlp = hash_table_find(&nodes, key);
609 if (!hlp) {
610 ipc_answer_0(rid, ENOENT);
611 return;
612 }
613 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
614 nh_link);
615
616 if (size == nodep->size) {
617 ipc_answer_0(rid, EOK);
618 return;
619 }
620
621 void *newdata = realloc(nodep->data, size);
622 if (!newdata) {
623 ipc_answer_0(rid, ENOMEM);
624 return;
625 }
626 if (size > nodep->size) {
627 size_t delta = size - nodep->size;
628 memset(newdata + nodep->size, 0, delta);
629 }
630 nodep->size = size;
631 nodep->data = newdata;
632 ipc_answer_0(rid, EOK);
633}
634
635void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
636{
637 ipc_answer_0(rid, EOK);
638}
639
640void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
641{
642 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
643 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
644 int rc;
645
646 link_t *hlp;
647 unsigned long key[] = {
648 [NODES_KEY_INDEX] = index,
649 [NODES_KEY_DEV] = dev_handle
650 };
651 hlp = hash_table_find(&nodes, key);
652 if (!hlp) {
653 ipc_answer_0(rid, ENOENT);
654 return;
655 }
656 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
657 nh_link);
658 rc = tmpfs_destroy_node(FS_NODE(nodep));
659 ipc_answer_0(rid, rc);
660}
661
662void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
663{
664 libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
665}
666
667void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
668{
669 libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
670}
671
672void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
673{
674 /* Dummy implementation */
675 ipc_answer_0(rid, EOK);
676}
677
678/**
679 * @}
680 */
Note: See TracBrowser for help on using the repository browser.