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

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

Finish implementation of tmpfs_unmounted().

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