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

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

Support multiple TMPFS instances.

  • Property mode set to 100644
File size: 16.7 KB
RevLine 
[d5cdffe]1/*
[41a0d27]2 * Copyright (c) 2008 Jakub Jermar
[d5cdffe]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33/**
34 * @file tmpfs_ops.c
35 * @brief Implementation of VFS operations for the TMPFS file system
36 * server.
37 */
38
39#include "tmpfs.h"
40#include "../../vfs/vfs.h"
41#include <ipc/ipc.h>
42#include <async.h>
43#include <errno.h>
[4b11571]44#include <atomic.h>
45#include <stdlib.h>
46#include <string.h>
47#include <stdio.h>
[5973fd0]48#include <assert.h>
[a4eb8a60]49#include <sys/types.h>
50#include <libadt/hash_table.h>
51#include <as.h>
[2c448fb]52#include <libfs.h>
[a4eb8a60]53
54#define min(a, b) ((a) < (b) ? (a) : (b))
55#define max(a, b) ((a) > (b) ? (a) : (b))
[d5cdffe]56
[a4eb8a60]57#define DENTRIES_BUCKETS 256
58
[3298ddc]59#define NAMES_BUCKETS 4
60
[8d049ee0]61/** All root nodes have index 0. */
62#define TMPFS_SOME_ROOT 0
63/** Global counter for assigning node indices. Shared by all instances. */
64fs_index_t tmpfs_next_index = 1;
[adb5fe3]65
[2c448fb]66/*
67 * Implementation of the libfs interface.
68 */
[b5553a2]69
[fdb7795]70/* Forward declarations of static functions. */
[736c164]71static void *tmpfs_match(void *, const char *);
[34f62f8]72static void *tmpfs_node_get(dev_handle_t, fs_index_t);
[06901c6b]73static void tmpfs_node_put(void *);
[adb5fe3]74static void *tmpfs_create_node(dev_handle_t, int);
[0013b9ce]75static int tmpfs_link_node(void *, void *, const char *);
[7b6d98b]76static int tmpfs_unlink_node(void *, void *);
[45f244b]77static int tmpfs_destroy_node(void *);
[2c448fb]78
79/* Implementation of helper functions. */
[f2ec8c8]80static fs_index_t tmpfs_index_get(void *nodep)
[2c448fb]81{
82 return ((tmpfs_dentry_t *) nodep)->index;
83}
84
[f2ec8c8]85static size_t tmpfs_size_get(void *nodep)
[2c448fb]86{
87 return ((tmpfs_dentry_t *) nodep)->size;
88}
89
90static unsigned tmpfs_lnkcnt_get(void *nodep)
91{
[adc8a63]92 return ((tmpfs_dentry_t *) nodep)->lnkcnt;
[2c448fb]93}
94
[736c164]95static bool tmpfs_has_children(void *nodep)
[2c448fb]96{
[736c164]97 return ((tmpfs_dentry_t *) nodep)->child != NULL;
[2c448fb]98}
99
[74ea3c6]100static void *tmpfs_root_get(dev_handle_t dev_handle)
[2c448fb]101{
[8d049ee0]102 return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT);
[2c448fb]103}
104
105static char tmpfs_plb_get_char(unsigned pos)
106{
107 return tmpfs_reg.plb_ro[pos % PLB_SIZE];
108}
109
110static bool tmpfs_is_directory(void *nodep)
111{
112 return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
113}
114
115static bool tmpfs_is_file(void *nodep)
116{
117 return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
118}
119
120/** libfs operations */
121libfs_ops_t tmpfs_libfs_ops = {
122 .match = tmpfs_match,
[a8e9ab8d]123 .node_get = tmpfs_node_get,
[06901c6b]124 .node_put = tmpfs_node_put,
[2c448fb]125 .create = tmpfs_create_node,
126 .destroy = tmpfs_destroy_node,
127 .link = tmpfs_link_node,
128 .unlink = tmpfs_unlink_node,
129 .index_get = tmpfs_index_get,
130 .size_get = tmpfs_size_get,
131 .lnkcnt_get = tmpfs_lnkcnt_get,
[736c164]132 .has_children = tmpfs_has_children,
[2c448fb]133 .root_get = tmpfs_root_get,
134 .plb_get_char = tmpfs_plb_get_char,
135 .is_directory = tmpfs_is_directory,
136 .is_file = tmpfs_is_file
137};
[fdb7795]138
139/** Hash table of all directory entries. */
[a4eb8a60]140hash_table_t dentries;
141
[8d049ee0]142#define DENTRIES_KEY_INDEX 0
143#define DENTRIES_KEY_DEV 1
144
[3298ddc]145/* Implementation of hash table interface for the dentries hash table. */
[8d049ee0]146static hash_index_t dentries_hash(unsigned long key[])
[a4eb8a60]147{
[8d049ee0]148 return key[DENTRIES_KEY_INDEX] % DENTRIES_BUCKETS;
[a4eb8a60]149}
150
[8d049ee0]151static int dentries_compare(unsigned long key[], hash_count_t keys,
[a4eb8a60]152 link_t *item)
153{
154 tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
155 dh_link);
[8d049ee0]156 return (dentry->index == key[DENTRIES_KEY_INDEX] &&
157 dentry->dev_handle == key[DENTRIES_KEY_DEV]);
[a4eb8a60]158}
159
160static void dentries_remove_callback(link_t *item)
161{
162}
163
164/** TMPFS dentries hash table operations. */
165hash_table_operations_t dentries_ops = {
166 .hash = dentries_hash,
167 .compare = dentries_compare,
168 .remove_callback = dentries_remove_callback
169};
170
[3298ddc]171typedef struct {
172 char *name;
173 tmpfs_dentry_t *parent;
174 link_t link;
175} tmpfs_name_t;
176
177/* Implementation of hash table interface for the names hash table. */
178static hash_index_t names_hash(unsigned long *key)
179{
180 tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
181 return dentry->index % NAMES_BUCKETS;
182}
183
184static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
185{
186 tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
187 tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
188 link);
189 return dentry == namep->parent;
190}
191
192static void names_remove_callback(link_t *item)
193{
194 tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
195 link);
196 free(namep->name);
197 free(namep);
198}
199
200/** TMPFS node names hash table operations. */
201static hash_table_operations_t names_ops = {
202 .hash = names_hash,
203 .compare = names_compare,
204 .remove_callback = names_remove_callback
205};
206
207static void tmpfs_name_initialize(tmpfs_name_t *namep)
208{
209 namep->name = NULL;
210 namep->parent = NULL;
211 link_initialize(&namep->link);
212}
213
214static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
[4b11571]215{
216 dentry->index = 0;
[8d049ee0]217 dentry->dev_handle = 0;
[4b11571]218 dentry->sibling = NULL;
219 dentry->child = NULL;
220 dentry->type = TMPFS_NONE;
[adc8a63]221 dentry->lnkcnt = 0;
[4b11571]222 dentry->size = 0;
223 dentry->data = NULL;
[a4eb8a60]224 link_initialize(&dentry->dh_link);
[3298ddc]225 return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
226 &names_ops);
[4b11571]227}
228
[8d049ee0]229bool tmpfs_init(void)
[4b11571]230{
[8d049ee0]231 if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 2, &dentries_ops))
[a4eb8a60]232 return false;
[8d049ee0]233
234 return true;
235}
236
237static bool tmpfs_instance_init(dev_handle_t dev_handle)
238{
239 tmpfs_dentry_t *root;
240
241 root = (tmpfs_dentry_t *) tmpfs_create_node(dev_handle, L_DIRECTORY);
242 if (!root)
[3298ddc]243 return false;
[5ab597d]244 root->lnkcnt = 0; /* FS root is not linked */
[3298ddc]245 return true;
[4b11571]246}
247
[d5cdffe]248/** Compare one component of path to a directory entry.
249 *
[736c164]250 * @param parentp Pointer to node from which we descended.
251 * @param childp Pointer to node to compare the path component with.
[b8b23c8]252 * @param component Array of characters holding component name.
[d5cdffe]253 *
[b8b23c8]254 * @return True on match, false otherwise.
[d5cdffe]255 */
[736c164]256static bool
257tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
258 const char *component)
[d5cdffe]259{
[3298ddc]260 unsigned long key = (unsigned long) parentp;
261 link_t *hlp = hash_table_find(&childp->names, &key);
262 assert(hlp);
263 tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
[92fd52d7]264 return !str_cmp(namep->name, component);
[b8b23c8]265}
[4b11571]266
[736c164]267void *tmpfs_match(void *prnt, const char *component)
268{
269 tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
270 tmpfs_dentry_t *childp = parentp->child;
271
272 while (childp && !tmpfs_match_one(parentp, childp, component))
273 childp = childp->sibling;
274
275 return (void *) childp;
276}
277
[f2ec8c8]278void *
[34f62f8]279tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
[a8e9ab8d]280{
[8d049ee0]281 unsigned long key[] = {
282 [DENTRIES_KEY_INDEX] = index,
283 [DENTRIES_KEY_DEV] = dev_handle
284 };
285 link_t *lnk = hash_table_find(&dentries, key);
[a8e9ab8d]286 if (!lnk)
287 return NULL;
288 return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
289}
290
[06901c6b]291void tmpfs_node_put(void *node)
292{
293 /* nothing to do */
294}
295
[adb5fe3]296void *tmpfs_create_node(dev_handle_t dev_handle, int lflag)
[b8b23c8]297{
[72bde81]298 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
299
300 tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
301 if (!node)
[b5553a2]302 return NULL;
[72bde81]303
[3298ddc]304 if (!tmpfs_dentry_initialize(node)) {
305 free(node);
306 return NULL;
307 }
[8d049ee0]308 if (!tmpfs_root_get(dev_handle))
309 node->index = TMPFS_SOME_ROOT;
310 else
311 node->index = tmpfs_next_index++;
312 node->dev_handle = dev_handle;
[72bde81]313 if (lflag & L_DIRECTORY)
314 node->type = TMPFS_DIRECTORY;
315 else
316 node->type = TMPFS_FILE;
317
[fdb7795]318 /* Insert the new node into the dentry hash table. */
[8d049ee0]319 unsigned long key[] = {
320 [DENTRIES_KEY_INDEX] = node->index,
321 [DENTRIES_KEY_DEV] = node->dev_handle
322 };
323 hash_table_insert(&dentries, key, &node->dh_link);
[fdb7795]324 return (void *) node;
325}
326
[0013b9ce]327int tmpfs_link_node(void *prnt, void *chld, const char *nm)
[fdb7795]328{
329 tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
330 tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
331
332 assert(parentp->type == TMPFS_DIRECTORY);
333
[3298ddc]334 tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
335 if (!namep)
[0013b9ce]336 return ENOMEM;
[3298ddc]337 tmpfs_name_initialize(namep);
[92fd52d7]338 size_t size = str_size(nm);
339 namep->name = malloc(size + 1);
[3298ddc]340 if (!namep->name) {
341 free(namep);
[0013b9ce]342 return ENOMEM;
[3298ddc]343 }
[6eb2e96]344 str_cpy(namep->name, size + 1, nm);
[3298ddc]345 namep->parent = parentp;
[adc8a63]346
347 childp->lnkcnt++;
[3298ddc]348
349 unsigned long key = (unsigned long) parentp;
350 hash_table_insert(&childp->names, &key, &namep->link);
[fdb7795]351
[72bde81]352 /* Insert the new node into the namespace. */
[fdb7795]353 if (parentp->child) {
354 tmpfs_dentry_t *tmp = parentp->child;
[72bde81]355 while (tmp->sibling)
356 tmp = tmp->sibling;
[fdb7795]357 tmp->sibling = childp;
[72bde81]358 } else {
[fdb7795]359 parentp->child = childp;
[72bde81]360 }
361
[0013b9ce]362 return EOK;
[b8b23c8]363}
[4b11571]364
[7b6d98b]365int tmpfs_unlink_node(void *prnt, void *chld)
[b8b23c8]366{
[7b6d98b]367 tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
368 tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
[16105cba]369
[7b6d98b]370 if (!parentp)
[16105cba]371 return EBUSY;
372
[7b6d98b]373 if (childp->child)
374 return ENOTEMPTY;
375
376 if (parentp->child == childp) {
377 parentp->child = childp->sibling;
[16105cba]378 } else {
379 /* TODO: consider doubly linked list for organizing siblings. */
[7b6d98b]380 tmpfs_dentry_t *tmp = parentp->child;
381 while (tmp->sibling != childp)
[16105cba]382 tmp = tmp->sibling;
[7b6d98b]383 tmp->sibling = childp->sibling;
[16105cba]384 }
[7b6d98b]385 childp->sibling = NULL;
[16105cba]386
[3298ddc]387 unsigned long key = (unsigned long) parentp;
388 hash_table_remove(&childp->names, &key, 1);
[fdb7795]389
[7b6d98b]390 childp->lnkcnt--;
[adc8a63]391
[16105cba]392 return EOK;
[d5cdffe]393}
394
[45f244b]395int tmpfs_destroy_node(void *nodep)
[fdb7795]396{
397 tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
398
[adc8a63]399 assert(!dentry->lnkcnt);
[fdb7795]400 assert(!dentry->child);
401 assert(!dentry->sibling);
402
[8d049ee0]403 unsigned long key[] = {
404 [DENTRIES_KEY_INDEX] = dentry->index,
405 [DENTRIES_KEY_DEV] = dentry->dev_handle
406 };
407 hash_table_remove(&dentries, key, 2);
[fdb7795]408
[3298ddc]409 hash_table_destroy(&dentry->names);
410
[fdb7795]411 if (dentry->type == TMPFS_FILE)
412 free(dentry->data);
413 free(dentry);
[45f244b]414 return EOK;
[fdb7795]415}
416
[f49b0ea]417void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
[64b67c3]418{
[f49b0ea]419 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
[64b67c3]420
[594303b]421 /* accept the mount options */
422 ipc_callid_t callid;
423 size_t size;
424 if (!ipc_data_write_receive(&callid, &size)) {
425 ipc_answer_0(callid, EINVAL);
426 ipc_answer_0(rid, EINVAL);
427 return;
428 }
429 char *opts = malloc(size + 1);
430 if (!opts) {
431 ipc_answer_0(callid, ENOMEM);
432 ipc_answer_0(rid, ENOMEM);
433 return;
434 }
435 ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
436 if (retval != EOK) {
437 ipc_answer_0(rid, retval);
438 free(opts);
439 return;
440 }
441 opts[size] = '\0';
442
[8d049ee0]443 /* Initialize TMPFS instance. */
444 if (!tmpfs_instance_init(dev_handle)) {
[4b11571]445 ipc_answer_0(rid, ENOMEM);
446 return;
447 }
[f49b0ea]448
[8d049ee0]449 tmpfs_dentry_t *root = tmpfs_root_get(dev_handle);
[594303b]450 if (str_cmp(opts, "restore") == 0) {
[f49b0ea]451 if (tmpfs_restore(dev_handle))
[5ab597d]452 ipc_answer_3(rid, EOK, root->index, root->size,
453 root->lnkcnt);
[f49b0ea]454 else
455 ipc_answer_0(rid, ELIMIT);
456 } else {
[5ab597d]457 ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt);
[f49b0ea]458 }
459}
460
461void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
462{
463 dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
464 fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request);
465 fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
466 dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
467
468 ipc_answer_0(rid, ENOTSUP);
469}
470
471void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
472{
[2c448fb]473 libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
[d5cdffe]474}
475
[a4eb8a60]476void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
477{
[f2ec8c8]478 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
479 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
480 off_t pos = (off_t)IPC_GET_ARG3(*request);
[a4eb8a60]481
482 /*
483 * Lookup the respective dentry.
484 */
485 link_t *hlp;
[8d049ee0]486 unsigned long key[] = {
487 [DENTRIES_KEY_INDEX] = index,
488 [DENTRIES_KEY_DEV] = dev_handle,
489 };
490 hlp = hash_table_find(&dentries, key);
[a4eb8a60]491 if (!hlp) {
492 ipc_answer_0(rid, ENOENT);
493 return;
494 }
495 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
496 dh_link);
497
498 /*
[a92da0a]499 * Receive the read request.
[a4eb8a60]500 */
501 ipc_callid_t callid;
[92fd52d7]502 size_t size;
503 if (!ipc_data_read_receive(&callid, &size)) {
[a4eb8a60]504 ipc_answer_0(callid, EINVAL);
505 ipc_answer_0(rid, EINVAL);
506 return;
507 }
508
[5973fd0]509 size_t bytes;
510 if (dentry->type == TMPFS_FILE) {
[92fd52d7]511 bytes = max(0, min(dentry->size - pos, size));
[5973fd0]512 (void) ipc_data_read_finalize(callid, dentry->data + pos,
513 bytes);
514 } else {
515 int i;
[75c426b4]516 tmpfs_dentry_t *cur;
[5973fd0]517
518 assert(dentry->type == TMPFS_DIRECTORY);
519
520 /*
521 * Yes, we really use O(n) algorithm here.
522 * If it bothers someone, it could be fixed by introducing a
523 * hash table.
524 */
525 for (i = 0, cur = dentry->child; i < pos && cur; i++,
526 cur = cur->sibling)
527 ;
528
529 if (!cur) {
530 ipc_answer_0(callid, ENOENT);
531 ipc_answer_1(rid, ENOENT, 0);
532 return;
533 }
534
[3298ddc]535 unsigned long key = (unsigned long) dentry;
536 link_t *hlp = hash_table_find(&cur->names, &key);
537 assert(hlp);
538 tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
539 link);
540
541 (void) ipc_data_read_finalize(callid, namep->name,
[92fd52d7]542 str_size(namep->name) + 1);
[5973fd0]543 bytes = 1;
544 }
[7dab6b8]545
546 /*
547 * Answer the VFS_READ call.
548 */
549 ipc_answer_1(rid, EOK, bytes);
[a4eb8a60]550}
551
[ee1b8ca]552void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
553{
[f2ec8c8]554 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
555 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
556 off_t pos = (off_t)IPC_GET_ARG3(*request);
[ee1b8ca]557
558 /*
559 * Lookup the respective dentry.
560 */
561 link_t *hlp;
[8d049ee0]562 unsigned long key[] = {
563 [DENTRIES_KEY_INDEX] = index,
564 [DENTRIES_KEY_DEV] = dev_handle
565 };
566 hlp = hash_table_find(&dentries, key);
[ee1b8ca]567 if (!hlp) {
568 ipc_answer_0(rid, ENOENT);
569 return;
570 }
571 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
572 dh_link);
573
574 /*
575 * Receive the write request.
576 */
577 ipc_callid_t callid;
[92fd52d7]578 size_t size;
579 if (!ipc_data_write_receive(&callid, &size)) {
[ee1b8ca]580 ipc_answer_0(callid, EINVAL);
581 ipc_answer_0(rid, EINVAL);
582 return;
583 }
584
[c1bf5cb]585 /*
586 * Check whether the file needs to grow.
587 */
[92fd52d7]588 if (pos + size <= dentry->size) {
[c1bf5cb]589 /* The file size is not changing. */
[92fd52d7]590 (void) ipc_data_write_finalize(callid, dentry->data + pos, size);
591 ipc_answer_2(rid, EOK, size, dentry->size);
[c1bf5cb]592 return;
593 }
[92fd52d7]594 size_t delta = (pos + size) - dentry->size;
[ee1b8ca]595 /*
596 * At this point, we are deliberately extremely straightforward and
[c1bf5cb]597 * simply realloc the contents of the file on every write that grows the
598 * file. In the end, the situation might not be as bad as it may look:
599 * our heap allocator can save us and just grow the block whenever
600 * possible.
[ee1b8ca]601 */
[c1bf5cb]602 void *newdata = realloc(dentry->data, dentry->size + delta);
[ee1b8ca]603 if (!newdata) {
604 ipc_answer_0(callid, ENOMEM);
[f7017572]605 ipc_answer_2(rid, EOK, 0, dentry->size);
[ee1b8ca]606 return;
607 }
[0ee4322]608 /* Clear any newly allocated memory in order to emulate gaps. */
[752ccee]609 memset(newdata + dentry->size, 0, delta);
[c1bf5cb]610 dentry->size += delta;
[ee1b8ca]611 dentry->data = newdata;
[92fd52d7]612 (void) ipc_data_write_finalize(callid, dentry->data + pos, size);
613 ipc_answer_2(rid, EOK, size, dentry->size);
[ee1b8ca]614}
615
[0ee4322]616void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
617{
[f2ec8c8]618 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
619 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
620 size_t size = (off_t)IPC_GET_ARG3(*request);
[0ee4322]621
622 /*
623 * Lookup the respective dentry.
624 */
625 link_t *hlp;
[8d049ee0]626 unsigned long key[] = {
627 [DENTRIES_KEY_INDEX] = index,
628 [DENTRIES_KEY_DEV] = dev_handle
629 };
630 hlp = hash_table_find(&dentries, key);
[0ee4322]631 if (!hlp) {
632 ipc_answer_0(rid, ENOENT);
633 return;
634 }
635 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
636 dh_link);
637
638 if (size == dentry->size) {
639 ipc_answer_0(rid, EOK);
640 return;
641 }
642
643 void *newdata = realloc(dentry->data, size);
644 if (!newdata) {
645 ipc_answer_0(rid, ENOMEM);
646 return;
647 }
648 if (size > dentry->size) {
649 size_t delta = size - dentry->size;
650 memset(newdata + dentry->size, 0, delta);
651 }
652 dentry->size = size;
653 dentry->data = newdata;
654 ipc_answer_0(rid, EOK);
655}
656
[fdb7795]657void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
[f17667a]658{
[f2ec8c8]659 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
660 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
[45f244b]661 int rc;
[f17667a]662
663 link_t *hlp;
[8d049ee0]664 unsigned long key[] = {
665 [DENTRIES_KEY_INDEX] = index,
666 [DENTRIES_KEY_DEV] = dev_handle
667 };
668 hlp = hash_table_find(&dentries, key);
[f17667a]669 if (!hlp) {
670 ipc_answer_0(rid, ENOENT);
671 return;
672 }
673 tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
674 dh_link);
[45f244b]675 rc = tmpfs_destroy_node(dentry);
676 ipc_answer_0(rid, rc);
[f17667a]677}
678
[d5cdffe]679/**
680 * @}
681 */
Note: See TracBrowser for help on using the repository browser.