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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2664838b was 3298ddc, checked in by Jakub Jermar <jakub@…>, 17 years ago

Support for multiple TMPFS node names.

  • Property mode set to 100644
File size: 13.7 KB
RevLine 
[d5cdffe]1/*
[41a0d27]2 * Copyright (c) 2008 Jakub Jermar
[d5cdffe]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>
[4b11571]44#include <atomic.h>
45#include <stdlib.h>
46#include <string.h>
47#include <stdio.h>
[5973fd0]48#include <assert.h>
[a4eb8a60]49#include <sys/types.h>
50#include <libadt/hash_table.h>
51#include <as.h>
[2c448fb]52#include <libfs.h>
[a4eb8a60]53
54#define min(a, b) ((a) < (b) ? (a) : (b))
55#define max(a, b) ((a) > (b) ? (a) : (b))
[d5cdffe]56
[a4eb8a60]57#define DENTRIES_BUCKETS 256
58
[3298ddc]59#define NAMES_BUCKETS 4
60
[2c448fb]61/*
62 * For now, we don't distinguish between different dev_handles/instances. All
63 * requests resolve to the only instance, rooted in the following variable.
64 */
65static tmpfs_dentry_t *root;
66
67/*
68 * Implementation of the libfs interface.
69 */
[b5553a2]70
[fdb7795]71/* Forward declarations of static functions. */
[3298ddc]72static bool tmpfs_match(void *, void *, const char *);
[2c448fb]73static void *tmpfs_create_node(int);
74static bool tmpfs_link_node(void *, void *, const char *);
[7b6d98b]75static int tmpfs_unlink_node(void *, void *);
[2c448fb]76static void tmpfs_destroy_node(void *);
77
78/* Implementation of helper functions. */
79static unsigned long tmpfs_index_get(void *nodep)
80{
81 return ((tmpfs_dentry_t *) nodep)->index;
82}
83
84static unsigned long tmpfs_size_get(void *nodep)
85{
86 return ((tmpfs_dentry_t *) nodep)->size;
87}
88
89static unsigned tmpfs_lnkcnt_get(void *nodep)
90{
[adc8a63]91 return ((tmpfs_dentry_t *) nodep)->lnkcnt;
[2c448fb]92}
93
94static void *tmpfs_child_get(void *nodep)
95{
96 return ((tmpfs_dentry_t *) nodep)->child;
97}
98
99static void *tmpfs_sibling_get(void *nodep)
100{
101 return ((tmpfs_dentry_t *) nodep)->sibling;
102}
103
104static void *tmpfs_root_get(void)
105{
106 return root;
107}
108
109static char tmpfs_plb_get_char(unsigned pos)
110{
111 return tmpfs_reg.plb_ro[pos % PLB_SIZE];
112}
113
114static bool tmpfs_is_directory(void *nodep)
115{
116 return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
117}
118
119static bool tmpfs_is_file(void *nodep)
120{
121 return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
122}
123
124/** libfs operations */
125libfs_ops_t tmpfs_libfs_ops = {
126 .match = tmpfs_match,
127 .create = tmpfs_create_node,
128 .destroy = tmpfs_destroy_node,
129 .link = tmpfs_link_node,
130 .unlink = tmpfs_unlink_node,
131 .index_get = tmpfs_index_get,
132 .size_get = tmpfs_size_get,
133 .lnkcnt_get = tmpfs_lnkcnt_get,
134 .child_get = tmpfs_child_get,
135 .sibling_get = tmpfs_sibling_get,
136 .root_get = tmpfs_root_get,
137 .plb_get_char = tmpfs_plb_get_char,
138 .is_directory = tmpfs_is_directory,
139 .is_file = tmpfs_is_file
140};
[fdb7795]141
142/** Hash table of all directory entries. */
[a4eb8a60]143hash_table_t dentries;
144
[3298ddc]145/* Implementation of hash table interface for the dentries hash table. */
[a4eb8a60]146static hash_index_t dentries_hash(unsigned long *key)
147{
148 return *key % DENTRIES_BUCKETS;
149}
150
151static int dentries_compare(unsigned long *key, hash_count_t keys,
152 link_t *item)
153{
154 tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
155 dh_link);
156 return dentry->index == *key;
157}
158
159static void dentries_remove_callback(link_t *item)
160{
161}
162
163/** TMPFS dentries hash table operations. */
164hash_table_operations_t dentries_ops = {
165 .hash = dentries_hash,
166 .compare = dentries_compare,
167 .remove_callback = dentries_remove_callback
168};
169
[4b11571]170unsigned tmpfs_next_index = 1;
171
[3298ddc]172typedef struct {
173 char *name;
174 tmpfs_dentry_t *parent;
175 link_t link;
176} tmpfs_name_t;
177
178/* Implementation of hash table interface for the names hash table. */
179static hash_index_t names_hash(unsigned long *key)
180{
181 tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
182 return dentry->index % NAMES_BUCKETS;
183}
184
185static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
186{
187 tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
188 tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
189 link);
190 return dentry == namep->parent;
191}
192
193static void names_remove_callback(link_t *item)
194{
195 tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
196 link);
197 free(namep->name);
198 free(namep);
199}
200
201/** TMPFS node names hash table operations. */
202static hash_table_operations_t names_ops = {
203 .hash = names_hash,
204 .compare = names_compare,
205 .remove_callback = names_remove_callback
206};
207
208static void tmpfs_name_initialize(tmpfs_name_t *namep)
209{
210 namep->name = NULL;
211 namep->parent = NULL;
212 link_initialize(&namep->link);
213}
214
215static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
[4b11571]216{
217 dentry->index = 0;
218 dentry->sibling = NULL;
219 dentry->child = NULL;
220 dentry->type = TMPFS_NONE;
[adc8a63]221 dentry->lnkcnt = 0;
[4b11571]222 dentry->size = 0;
223 dentry->data = NULL;
[a4eb8a60]224 link_initialize(&dentry->dh_link);
[3298ddc]225 return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
226 &names_ops);
[4b11571]227}
228
229static bool tmpfs_init(void)
230{
[a4eb8a60]231 if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
232 return false;
[2c448fb]233 root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
[3298ddc]234 if (!root) {
235 hash_table_destroy(&dentries);
236 return false;
237 }
[3ca7059]238 root->lnkcnt = 1;
[3298ddc]239 return true;
[4b11571]240}
241
[d5cdffe]242/** Compare one component of path to a directory entry.
243 *
[3298ddc]244 * @param prnt Node from which we descended.
245 * @param chld Node to compare the path component with.
[b8b23c8]246 * @param component Array of characters holding component name.
[d5cdffe]247 *
[b8b23c8]248 * @return True on match, false otherwise.
[d5cdffe]249 */
[3298ddc]250bool tmpfs_match(void *prnt, void *chld, const char *component)
[d5cdffe]251{
[3298ddc]252 tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
253 tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
[b5553a2]254
[3298ddc]255 unsigned long key = (unsigned long) parentp;
256 link_t *hlp = hash_table_find(&childp->names, &key);
257 assert(hlp);
258 tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
259
260 return !strcmp(namep->name, component);
[b8b23c8]261}
[4b11571]262
[2c448fb]263void *tmpfs_create_node(int lflag)
[b8b23c8]264{
[72bde81]265 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
266
267 tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
268 if (!node)
[b5553a2]269 return NULL;
[72bde81]270
[3298ddc]271 if (!tmpfs_dentry_initialize(node)) {
272 free(node);
273 return NULL;
274 }
[72bde81]275 node->index = tmpfs_next_index++;
276 if (lflag & L_DIRECTORY)
277 node->type = TMPFS_DIRECTORY;
278 else
279 node->type = TMPFS_FILE;
280
[fdb7795]281 /* Insert the new node into the dentry hash table. */
282 hash_table_insert(&dentries, &node->index, &node->dh_link);
283 return (void *) node;
284}
285
[2c448fb]286bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
[fdb7795]287{
288 tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
289 tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
290
291 assert(parentp->type == TMPFS_DIRECTORY);
292
[3298ddc]293 tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
294 if (!namep)
295 return false;
296 tmpfs_name_initialize(namep);
[fdb7795]297 size_t len = strlen(nm);
[3298ddc]298 namep->name = malloc(len + 1);
299 if (!namep->name) {
300 free(namep);
[fdb7795]301 return false;
[3298ddc]302 }
303 strcpy(namep->name, nm);
304 namep->parent = parentp;
[adc8a63]305
306 childp->lnkcnt++;
[3298ddc]307
308 unsigned long key = (unsigned long) parentp;
309 hash_table_insert(&childp->names, &key, &namep->link);
[fdb7795]310
[72bde81]311 /* Insert the new node into the namespace. */
[fdb7795]312 if (parentp->child) {
313 tmpfs_dentry_t *tmp = parentp->child;
[72bde81]314 while (tmp->sibling)
315 tmp = tmp->sibling;
[fdb7795]316 tmp->sibling = childp;
[72bde81]317 } else {
[fdb7795]318 parentp->child = childp;
[72bde81]319 }
320
[fdb7795]321 return true;
[b8b23c8]322}
[4b11571]323
[7b6d98b]324int tmpfs_unlink_node(void *prnt, void *chld)
[b8b23c8]325{
[7b6d98b]326 tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
327 tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
[16105cba]328
[7b6d98b]329 if (!parentp)
[16105cba]330 return EBUSY;
331
[7b6d98b]332 if (childp->child)
333 return ENOTEMPTY;
334
335 if (parentp->child == childp) {
336 parentp->child = childp->sibling;
[16105cba]337 } else {
338 /* TODO: consider doubly linked list for organizing siblings. */
[7b6d98b]339 tmpfs_dentry_t *tmp = parentp->child;
340 while (tmp->sibling != childp)
[16105cba]341 tmp = tmp->sibling;
[7b6d98b]342 tmp->sibling = childp->sibling;
[16105cba]343 }
[7b6d98b]344 childp->sibling = NULL;
[16105cba]345
[3298ddc]346 unsigned long key = (unsigned long) parentp;
347 hash_table_remove(&childp->names, &key, 1);
[fdb7795]348
[7b6d98b]349 childp->lnkcnt--;
[adc8a63]350
[16105cba]351 return EOK;
[d5cdffe]352}
353
[2c448fb]354void tmpfs_destroy_node(void *nodep)
[fdb7795]355{
356 tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
357
[adc8a63]358 assert(!dentry->lnkcnt);
[fdb7795]359 assert(!dentry->child);
360 assert(!dentry->sibling);
361
362 unsigned long index = dentry->index;
363 hash_table_remove(&dentries, &index, 1);
364
[3298ddc]365 hash_table_destroy(&dentry->names);
366
[fdb7795]367 if (dentry->type == TMPFS_FILE)
368 free(dentry->data);
369 free(dentry);
370}
371
[d5cdffe]372void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
373{
[2c448fb]374 /* Initialize TMPFS. */
[4b11571]375 if (!root && !tmpfs_init()) {
376 ipc_answer_0(rid, ENOMEM);
377 return;
378 }
[2c448fb]379 libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
[d5cdffe]380}
381
[a4eb8a60]382void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
383{
384 int dev_handle = IPC_GET_ARG1(*request);
385 unsigned long index = IPC_GET_ARG2(*request);
386 off_t pos = IPC_GET_ARG3(*request);
387
388 /*
389 * Lookup the respective dentry.
390 */
391 link_t *hlp;
392 hlp = hash_table_find(&dentries, &index);
393 if (!hlp) {
394 ipc_answer_0(rid, ENOENT);
395 return;
396 }
397 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
398 dh_link);
399
400 /*
[a92da0a]401 * Receive the read request.
[a4eb8a60]402 */
403 ipc_callid_t callid;
[92688eb]404 size_t len;
405 if (!ipc_data_read_receive(&callid, &len)) {
[a4eb8a60]406 ipc_answer_0(callid, EINVAL);
407 ipc_answer_0(rid, EINVAL);
408 return;
409 }
410
[5973fd0]411 size_t bytes;
412 if (dentry->type == TMPFS_FILE) {
413 bytes = max(0, min(dentry->size - pos, len));
414 (void) ipc_data_read_finalize(callid, dentry->data + pos,
415 bytes);
416 } else {
417 int i;
[75c426b4]418 tmpfs_dentry_t *cur;
[5973fd0]419
420 assert(dentry->type == TMPFS_DIRECTORY);
421
422 /*
423 * Yes, we really use O(n) algorithm here.
424 * If it bothers someone, it could be fixed by introducing a
425 * hash table.
426 */
427 for (i = 0, cur = dentry->child; i < pos && cur; i++,
428 cur = cur->sibling)
429 ;
430
431 if (!cur) {
432 ipc_answer_0(callid, ENOENT);
433 ipc_answer_1(rid, ENOENT, 0);
434 return;
435 }
436
[3298ddc]437 unsigned long key = (unsigned long) dentry;
438 link_t *hlp = hash_table_find(&cur->names, &key);
439 assert(hlp);
440 tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
441 link);
442
443 (void) ipc_data_read_finalize(callid, namep->name,
444 strlen(namep->name) + 1);
[5973fd0]445 bytes = 1;
446 }
[7dab6b8]447
448 /*
449 * Answer the VFS_READ call.
450 */
451 ipc_answer_1(rid, EOK, bytes);
[a4eb8a60]452}
453
[ee1b8ca]454void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
455{
456 int dev_handle = IPC_GET_ARG1(*request);
457 unsigned long index = IPC_GET_ARG2(*request);
458 off_t pos = IPC_GET_ARG3(*request);
459
460 /*
461 * Lookup the respective dentry.
462 */
463 link_t *hlp;
464 hlp = hash_table_find(&dentries, &index);
465 if (!hlp) {
466 ipc_answer_0(rid, ENOENT);
467 return;
468 }
469 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
470 dh_link);
471
472 /*
473 * Receive the write request.
474 */
475 ipc_callid_t callid;
[92688eb]476 size_t len;
[3115355]477 if (!ipc_data_write_receive(&callid, &len)) {
[ee1b8ca]478 ipc_answer_0(callid, EINVAL);
479 ipc_answer_0(rid, EINVAL);
480 return;
481 }
482
[c1bf5cb]483 /*
484 * Check whether the file needs to grow.
485 */
[92688eb]486 if (pos + len <= dentry->size) {
[c1bf5cb]487 /* The file size is not changing. */
[215e375]488 (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
[f7017572]489 ipc_answer_2(rid, EOK, len, dentry->size);
[c1bf5cb]490 return;
491 }
[92688eb]492 size_t delta = (pos + len) - dentry->size;
[ee1b8ca]493 /*
494 * At this point, we are deliberately extremely straightforward and
[c1bf5cb]495 * simply realloc the contents of the file on every write that grows the
496 * file. In the end, the situation might not be as bad as it may look:
497 * our heap allocator can save us and just grow the block whenever
498 * possible.
[ee1b8ca]499 */
[c1bf5cb]500 void *newdata = realloc(dentry->data, dentry->size + delta);
[ee1b8ca]501 if (!newdata) {
502 ipc_answer_0(callid, ENOMEM);
[f7017572]503 ipc_answer_2(rid, EOK, 0, dentry->size);
[ee1b8ca]504 return;
505 }
[0ee4322]506 /* Clear any newly allocated memory in order to emulate gaps. */
[752ccee]507 memset(newdata + dentry->size, 0, delta);
[c1bf5cb]508 dentry->size += delta;
[ee1b8ca]509 dentry->data = newdata;
[215e375]510 (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
[7fff5eab]511 ipc_answer_2(rid, EOK, len, dentry->size);
[ee1b8ca]512}
513
[0ee4322]514void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
515{
516 int dev_handle = IPC_GET_ARG1(*request);
517 unsigned long index = IPC_GET_ARG2(*request);
518 size_t size = IPC_GET_ARG3(*request);
519
520 /*
521 * Lookup the respective dentry.
522 */
523 link_t *hlp;
524 hlp = hash_table_find(&dentries, &index);
525 if (!hlp) {
526 ipc_answer_0(rid, ENOENT);
527 return;
528 }
529 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
530 dh_link);
531
532 if (size == dentry->size) {
533 ipc_answer_0(rid, EOK);
534 return;
535 }
536
537 void *newdata = realloc(dentry->data, size);
538 if (!newdata) {
539 ipc_answer_0(rid, ENOMEM);
540 return;
541 }
542 if (size > dentry->size) {
543 size_t delta = size - dentry->size;
544 memset(newdata + dentry->size, 0, delta);
545 }
546 dentry->size = size;
547 dentry->data = newdata;
548 ipc_answer_0(rid, EOK);
549}
550
[fdb7795]551void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
[f17667a]552{
553 int dev_handle = IPC_GET_ARG1(*request);
554 unsigned long index = IPC_GET_ARG2(*request);
555
556 link_t *hlp;
557 hlp = hash_table_find(&dentries, &index);
558 if (!hlp) {
559 ipc_answer_0(rid, ENOENT);
560 return;
561 }
562 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
563 dh_link);
[2c448fb]564 tmpfs_destroy_node(dentry);
[f17667a]565 ipc_answer_0(rid, EOK);
566}
567
[d5cdffe]568/**
569 * @}
570 */
Note: See TracBrowser for help on using the repository browser.