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

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

Forbid mounting the same TMPFS instance multiple times.

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