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

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

Change the order of TMPFS node keys.

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