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

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

Update tmpfs implementation of libfs operations wrt. the preceding API change.

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