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

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

do not intermix low-level IPC methods with async framework methods

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