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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4982d87 was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 15.1 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>
[062d900]52#include <adt/hash.h>
[a4eb8a60]53#include <as.h>
[2c448fb]54#include <libfs.h>
[a4eb8a60]55
56#define min(a, b) ((a) < (b) ? (a) : (b))
57#define max(a, b) ((a) > (b) ? (a) : (b))
[d5cdffe]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 *);
[15f3c3f]70static int tmpfs_node_get(fs_node_t **, service_id_t, fs_index_t);
[1313ee9]71static int tmpfs_node_open(fs_node_t *);
[54e4479]72static int tmpfs_node_put(fs_node_t *);
[15f3c3f]73static int tmpfs_create_node(fs_node_t **, service_id_t, int);
[54e4479]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. */
[15f3c3f]79static int tmpfs_root_get(fs_node_t **rfn, service_id_t service_id)
[2c448fb]80{
[15f3c3f]81 return tmpfs_node_get(rfn, service_id, TMPFS_SOME_ROOT);
[2c448fb]82}
83
[54e4479]84static int tmpfs_has_children(bool *has_children, fs_node_t *fn)
[2c448fb]85{
[b72efe8]86 *has_children = !list_empty(&TMPFS_NODE(fn)->cs_list);
[54e4479]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
[ed903174]95static aoff64_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
[b6035ba]105static bool tmpfs_is_directory(fs_node_t *fn)
[2c448fb]106{
[b6035ba]107 return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
[2c448fb]108}
109
[b6035ba]110static bool tmpfs_is_file(fs_node_t *fn)
[2c448fb]111{
[b6035ba]112 return TMPFS_NODE(fn)->type == TMPFS_FILE;
[2c448fb]113}
114
[b33870b]115static service_id_t tmpfs_service_get(fs_node_t *fn)
[1313ee9]116{
117 return 0;
118}
119
[2c448fb]120/** libfs operations */
121libfs_ops_t tmpfs_libfs_ops = {
[54e4479]122 .root_get = tmpfs_root_get,
[2c448fb]123 .match = tmpfs_match,
[a8e9ab8d]124 .node_get = tmpfs_node_get,
[1313ee9]125 .node_open = tmpfs_node_open,
[06901c6b]126 .node_put = tmpfs_node_put,
[2c448fb]127 .create = tmpfs_create_node,
128 .destroy = tmpfs_destroy_node,
129 .link = tmpfs_link_node,
130 .unlink = tmpfs_unlink_node,
[54e4479]131 .has_children = tmpfs_has_children,
[2c448fb]132 .index_get = tmpfs_index_get,
133 .size_get = tmpfs_size_get,
134 .lnkcnt_get = tmpfs_lnkcnt_get,
135 .is_directory = tmpfs_is_directory,
[1313ee9]136 .is_file = tmpfs_is_file,
[b33870b]137 .service_get = tmpfs_service_get
[2c448fb]138};
[fdb7795]139
[cf95bc0]140/** Hash table of all TMPFS nodes. */
141hash_table_t nodes;
[a4eb8a60]142
[062d900]143/*
144 * Implementation of hash table interface for the nodes hash table.
145 */
146
147typedef struct {
148 service_id_t service_id;
149 fs_index_t index;
150} node_key_t;
[8d049ee0]151
[062d900]152static size_t nodes_key_hash(void *k)
[a4eb8a60]153{
[062d900]154 node_key_t *key = (node_key_t *)k;
155 return hash_combine(key->service_id, key->index);
[a4eb8a60]156}
157
[062d900]158static size_t nodes_hash(const ht_link_t *item)
[a4eb8a60]159{
[062d900]160 tmpfs_node_t *nodep = hash_table_get_inst(item, tmpfs_node_t, nh_link);
161 return hash_combine(nodep->service_id, nodep->index);
162}
[0055cfd]163
[062d900]164static bool nodes_key_equal(void *key_arg, const ht_link_t *item)
165{
166 tmpfs_node_t *node = hash_table_get_inst(item, tmpfs_node_t, nh_link);
167 node_key_t *key = (node_key_t *)key_arg;
168
169 return key->service_id == node->service_id && key->index == node->index;
[a4eb8a60]170}
171
[062d900]172static void nodes_remove_callback(ht_link_t *item)
[a4eb8a60]173{
[062d900]174 tmpfs_node_t *nodep = hash_table_get_inst(item, tmpfs_node_t, nh_link);
[9bddf37]175
[b72efe8]176 while (!list_empty(&nodep->cs_list)) {
177 tmpfs_dentry_t *dentryp = list_get_instance(
178 list_first(&nodep->cs_list), tmpfs_dentry_t, link);
[9bddf37]179
180 assert(nodep->type == TMPFS_DIRECTORY);
181 list_remove(&dentryp->link);
182 free(dentryp);
183 }
184
185 if (nodep->data) {
186 assert(nodep->type == TMPFS_FILE);
187 free(nodep->data);
188 }
189 free(nodep->bp);
190 free(nodep);
[a4eb8a60]191}
192
[cf95bc0]193/** TMPFS nodes hash table operations. */
[062d900]194hash_table_ops_t nodes_ops = {
[cf95bc0]195 .hash = nodes_hash,
[062d900]196 .key_hash = nodes_key_hash,
197 .key_equal = nodes_key_equal,
[4e00f87]198 .equal = NULL,
[cf95bc0]199 .remove_callback = nodes_remove_callback
[a4eb8a60]200};
201
[cf95bc0]202static void tmpfs_node_initialize(tmpfs_node_t *nodep)
[3298ddc]203{
[cf95bc0]204 nodep->bp = NULL;
205 nodep->index = 0;
[15f3c3f]206 nodep->service_id = 0;
[cf95bc0]207 nodep->type = TMPFS_NONE;
208 nodep->lnkcnt = 0;
209 nodep->size = 0;
210 nodep->data = NULL;
[b72efe8]211 list_initialize(&nodep->cs_list);
[3298ddc]212}
213
[cf95bc0]214static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
[3298ddc]215{
[cf95bc0]216 link_initialize(&dentryp->link);
217 dentryp->name = NULL;
218 dentryp->node = NULL;
[4b11571]219}
220
[8d049ee0]221bool tmpfs_init(void)
[4b11571]222{
[062d900]223 if (!hash_table_create(&nodes, 0, 0, &nodes_ops))
[a4eb8a60]224 return false;
[8d049ee0]225
226 return true;
227}
228
[15f3c3f]229static bool tmpfs_instance_init(service_id_t service_id)
[8d049ee0]230{
[b6035ba]231 fs_node_t *rfn;
[54e4479]232 int rc;
[8d049ee0]233
[15f3c3f]234 rc = tmpfs_create_node(&rfn, service_id, L_DIRECTORY);
[54e4479]235 if (rc != EOK || !rfn)
[3298ddc]236 return false;
[b6035ba]237 TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
[3298ddc]238 return true;
[4b11571]239}
240
[062d900]241static bool rm_service_id_nodes(ht_link_t *item, void *arg)
[e056e820]242{
[062d900]243 service_id_t sid = *(service_id_t*)arg;
244 tmpfs_node_t *node = hash_table_get_inst(item, tmpfs_node_t, nh_link);
245
246 if (node->service_id == sid) {
247 hash_table_remove_item(&nodes, &node->nh_link);
248 }
249 return true;
250}
251
252static void tmpfs_instance_done(service_id_t service_id)
253{
254 hash_table_apply(&nodes, rm_service_id_nodes, &service_id);
[e056e820]255}
256
[54e4479]257int tmpfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
[736c164]258{
[cf95bc0]259 tmpfs_node_t *parentp = TMPFS_NODE(pfn);
[736c164]260
[feeac0d]261 list_foreach(parentp->cs_list, link, tmpfs_dentry_t, dentryp) {
[54e4479]262 if (!str_cmp(dentryp->name, component)) {
263 *rfn = FS_NODE(dentryp->node);
264 return EOK;
265 }
[cf95bc0]266 }
[736c164]267
[54e4479]268 *rfn = NULL;
269 return EOK;
[736c164]270}
271
[15f3c3f]272int tmpfs_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
[a8e9ab8d]273{
[062d900]274 node_key_t key = {
275 .service_id = service_id,
276 .index = index
[8d049ee0]277 };
[062d900]278
279 ht_link_t *lnk = hash_table_find(&nodes, &key);
280
[54e4479]281 if (lnk) {
282 tmpfs_node_t *nodep;
[062d900]283 nodep = hash_table_get_inst(lnk, tmpfs_node_t, nh_link);
[54e4479]284 *rfn = FS_NODE(nodep);
285 } else {
286 *rfn = NULL;
287 }
288 return EOK;
[a8e9ab8d]289}
290
[1313ee9]291int tmpfs_node_open(fs_node_t *fn)
292{
293 /* nothing to do */
294 return EOK;
295}
296
[54e4479]297int tmpfs_node_put(fs_node_t *fn)
[06901c6b]298{
299 /* nothing to do */
[54e4479]300 return EOK;
[06901c6b]301}
302
[15f3c3f]303int tmpfs_create_node(fs_node_t **rfn, service_id_t service_id, int lflag)
[b8b23c8]304{
[54e4479]305 fs_node_t *rootfn;
306 int rc;
307
[72bde81]308 assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
309
[cf95bc0]310 tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
311 if (!nodep)
[54e4479]312 return ENOMEM;
[cf95bc0]313 tmpfs_node_initialize(nodep);
314 nodep->bp = malloc(sizeof(fs_node_t));
315 if (!nodep->bp) {
316 free(nodep);
[54e4479]317 return ENOMEM;
[3298ddc]318 }
[83937ccd]319 fs_node_initialize(nodep->bp);
[cf95bc0]320 nodep->bp->data = nodep; /* link the FS and TMPFS nodes */
[54e4479]321
[15f3c3f]322 rc = tmpfs_root_get(&rootfn, service_id);
[54e4479]323 assert(rc == EOK);
324 if (!rootfn)
[cf95bc0]325 nodep->index = TMPFS_SOME_ROOT;
[8d049ee0]326 else
[cf95bc0]327 nodep->index = tmpfs_next_index++;
[15f3c3f]328 nodep->service_id = service_id;
[72bde81]329 if (lflag & L_DIRECTORY)
[cf95bc0]330 nodep->type = TMPFS_DIRECTORY;
[72bde81]331 else
[cf95bc0]332 nodep->type = TMPFS_FILE;
[72bde81]333
[cf95bc0]334 /* Insert the new node into the nodes hash table. */
[062d900]335 hash_table_insert(&nodes, &nodep->nh_link);
[54e4479]336 *rfn = FS_NODE(nodep);
337 return EOK;
338}
339
340int tmpfs_destroy_node(fs_node_t *fn)
341{
342 tmpfs_node_t *nodep = TMPFS_NODE(fn);
343
344 assert(!nodep->lnkcnt);
[b72efe8]345 assert(list_empty(&nodep->cs_list));
[062d900]346
347 hash_table_remove_item(&nodes, &nodep->nh_link);
[54e4479]348
[9bddf37]349 /*
350 * The nodes_remove_callback() function takes care of the actual
351 * resource deallocation.
352 */
[54e4479]353 return EOK;
[fdb7795]354}
355
[b6035ba]356int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
[fdb7795]357{
[cf95bc0]358 tmpfs_node_t *parentp = TMPFS_NODE(pfn);
359 tmpfs_node_t *childp = TMPFS_NODE(cfn);
360 tmpfs_dentry_t *dentryp;
[fdb7795]361
362 assert(parentp->type == TMPFS_DIRECTORY);
363
[cf95bc0]364 /* Check for duplicit entries. */
[feeac0d]365 list_foreach(parentp->cs_list, link, tmpfs_dentry_t, dp) {
366 if (!str_cmp(dp->name, nm))
[cf95bc0]367 return EEXIST;
368 }
369
370 /* Allocate and initialize the dentry. */
371 dentryp = malloc(sizeof(tmpfs_dentry_t));
372 if (!dentryp)
[0013b9ce]373 return ENOMEM;
[cf95bc0]374 tmpfs_dentry_initialize(dentryp);
375
376 /* Populate and link the new dentry. */
[92fd52d7]377 size_t size = str_size(nm);
[cf95bc0]378 dentryp->name = malloc(size + 1);
379 if (!dentryp->name) {
380 free(dentryp);
[0013b9ce]381 return ENOMEM;
[3298ddc]382 }
[cf95bc0]383 str_cpy(dentryp->name, size + 1, nm);
384 dentryp->node = childp;
[adc8a63]385 childp->lnkcnt++;
[b72efe8]386 list_append(&dentryp->link, &parentp->cs_list);
[72bde81]387
[0013b9ce]388 return EOK;
[b8b23c8]389}
[4b11571]390
[cf95bc0]391int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
[b8b23c8]392{
[cf95bc0]393 tmpfs_node_t *parentp = TMPFS_NODE(pfn);
394 tmpfs_node_t *childp = NULL;
395 tmpfs_dentry_t *dentryp;
[16105cba]396
[7b6d98b]397 if (!parentp)
[16105cba]398 return EBUSY;
[feeac0d]399
400 list_foreach(parentp->cs_list, link, tmpfs_dentry_t, dp) {
401 if (!str_cmp(dp->name, nm)) {
402 dentryp = dp;
[cf95bc0]403 childp = dentryp->node;
404 assert(FS_NODE(childp) == cfn);
405 break;
[b72efe8]406 }
[16105cba]407 }
408
[cf95bc0]409 if (!childp)
410 return ENOENT;
411
[b72efe8]412 if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_list))
[cf95bc0]413 return ENOTEMPTY;
[fdb7795]414
[cf95bc0]415 list_remove(&dentryp->link);
416 free(dentryp);
[7b6d98b]417 childp->lnkcnt--;
[adc8a63]418
[16105cba]419 return EOK;
[d5cdffe]420}
421
[efcebe1]422/*
423 * Implementation of the VFS_OUT interface.
424 */
425
426static int
[86ffa27f]427tmpfs_mounted(service_id_t service_id, const char *opts,
[efcebe1]428 fs_index_t *index, aoff64_t *size, unsigned *lnkcnt)
[64b67c3]429{
[fc2e71e]430 fs_node_t *rootfn;
[54e4479]431 int rc;
[472c09d]432
[d42976c]433 /* Check if this device is not already mounted. */
[15f3c3f]434 rc = tmpfs_root_get(&rootfn, service_id);
[fc2e71e]435 if ((rc == EOK) && (rootfn)) {
[0055cfd]436 (void) tmpfs_node_put(rootfn);
[efcebe1]437 return EEXIST;
[fc2e71e]438 }
439
[8d049ee0]440 /* Initialize TMPFS instance. */
[86ffa27f]441 if (!tmpfs_instance_init(service_id))
[efcebe1]442 return ENOMEM;
[f49b0ea]443
[15f3c3f]444 rc = tmpfs_root_get(&rootfn, service_id);
[54e4479]445 assert(rc == EOK);
446 tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
[594303b]447 if (str_cmp(opts, "restore") == 0) {
[86ffa27f]448 if (!tmpfs_restore(service_id))
[efcebe1]449 return ELIMIT;
[f49b0ea]450 }
451
[efcebe1]452 *index = rootp->index;
453 *size = rootp->size;
454 *lnkcnt = rootp->lnkcnt;
[e056e820]455
[efcebe1]456 return EOK;
[3c11713]457}
458
[86ffa27f]459static int tmpfs_unmounted(service_id_t service_id)
[f49b0ea]460{
[86ffa27f]461 tmpfs_instance_done(service_id);
[efcebe1]462 return EOK;
[d5cdffe]463}
464
[86ffa27f]465static int tmpfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
[efcebe1]466 size_t *rbytes)
[a4eb8a60]467{
468 /*
[cf95bc0]469 * Lookup the respective TMPFS node.
[a4eb8a60]470 */
[062d900]471 node_key_t key = {
472 .service_id = service_id,
473 .index = index
[8d049ee0]474 };
[062d900]475
476 ht_link_t *hlp = hash_table_find(&nodes, &key);
[efcebe1]477 if (!hlp)
478 return ENOENT;
[062d900]479
480 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
[ed903174]481
[a4eb8a60]482 /*
[a92da0a]483 * Receive the read request.
[a4eb8a60]484 */
485 ipc_callid_t callid;
[92fd52d7]486 size_t size;
[0da4e41]487 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]488 async_answer_0(callid, EINVAL);
[efcebe1]489 return EINVAL;
[a4eb8a60]490 }
491
[5973fd0]492 size_t bytes;
[cf95bc0]493 if (nodep->type == TMPFS_FILE) {
[ed903174]494 bytes = min(nodep->size - pos, size);
[0da4e41]495 (void) async_data_read_finalize(callid, nodep->data + pos,
[5973fd0]496 bytes);
497 } else {
[cf95bc0]498 tmpfs_dentry_t *dentryp;
499 link_t *lnk;
[5973fd0]500
[cf95bc0]501 assert(nodep->type == TMPFS_DIRECTORY);
[5973fd0]502
503 /*
504 * Yes, we really use O(n) algorithm here.
505 * If it bothers someone, it could be fixed by introducing a
506 * hash table.
507 */
[b72efe8]508 lnk = list_nth(&nodep->cs_list, pos);
509
510 if (lnk == NULL) {
[ffa2c8ef]511 async_answer_0(callid, ENOENT);
[efcebe1]512 return ENOENT;
[5973fd0]513 }
514
[cf95bc0]515 dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
[3298ddc]516
[0da4e41]517 (void) async_data_read_finalize(callid, dentryp->name,
[cf95bc0]518 str_size(dentryp->name) + 1);
[5973fd0]519 bytes = 1;
520 }
[7dab6b8]521
[efcebe1]522 *rbytes = bytes;
523 return EOK;
[a4eb8a60]524}
525
[efcebe1]526static int
[86ffa27f]527tmpfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
[efcebe1]528 size_t *wbytes, aoff64_t *nsize)
[ee1b8ca]529{
530 /*
[cf95bc0]531 * Lookup the respective TMPFS node.
[ee1b8ca]532 */
[062d900]533 node_key_t key = {
534 .service_id = service_id,
535 .index = index
[8d049ee0]536 };
[062d900]537
538 ht_link_t *hlp = hash_table_find(&nodes, &key);
539
[efcebe1]540 if (!hlp)
541 return ENOENT;
[062d900]542
543 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
[ee1b8ca]544
545 /*
546 * Receive the write request.
547 */
548 ipc_callid_t callid;
[92fd52d7]549 size_t size;
[0da4e41]550 if (!async_data_write_receive(&callid, &size)) {
[ffa2c8ef]551 async_answer_0(callid, EINVAL);
[efcebe1]552 return EINVAL;
[ee1b8ca]553 }
554
[c1bf5cb]555 /*
556 * Check whether the file needs to grow.
557 */
[cf95bc0]558 if (pos + size <= nodep->size) {
[c1bf5cb]559 /* The file size is not changing. */
[efcebe1]560 (void) async_data_write_finalize(callid, nodep->data + pos,
561 size);
562 goto out;
[c1bf5cb]563 }
[cf95bc0]564 size_t delta = (pos + size) - nodep->size;
[ee1b8ca]565 /*
566 * At this point, we are deliberately extremely straightforward and
[c1bf5cb]567 * simply realloc the contents of the file on every write that grows the
568 * file. In the end, the situation might not be as bad as it may look:
569 * our heap allocator can save us and just grow the block whenever
570 * possible.
[ee1b8ca]571 */
[cf95bc0]572 void *newdata = realloc(nodep->data, nodep->size + delta);
[ee1b8ca]573 if (!newdata) {
[ffa2c8ef]574 async_answer_0(callid, ENOMEM);
[efcebe1]575 size = 0;
576 goto out;
[ee1b8ca]577 }
[0ee4322]578 /* Clear any newly allocated memory in order to emulate gaps. */
[cf95bc0]579 memset(newdata + nodep->size, 0, delta);
580 nodep->size += delta;
581 nodep->data = newdata;
[0da4e41]582 (void) async_data_write_finalize(callid, nodep->data + pos, size);
[efcebe1]583
584out:
585 *wbytes = size;
586 *nsize = nodep->size;
587 return EOK;
[ee1b8ca]588}
589
[86ffa27f]590static int tmpfs_truncate(service_id_t service_id, fs_index_t index,
[efcebe1]591 aoff64_t size)
[0ee4322]592{
593 /*
[cf95bc0]594 * Lookup the respective TMPFS node.
[0ee4322]595 */
[062d900]596 node_key_t key = {
597 .service_id = service_id,
598 .index = index
[8d049ee0]599 };
[062d900]600
601 ht_link_t *hlp = hash_table_find(&nodes, &key);
602
[efcebe1]603 if (!hlp)
604 return ENOENT;
[062d900]605 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t, nh_link);
[ed903174]606
[efcebe1]607 if (size == nodep->size)
608 return EOK;
[ed903174]609
[efcebe1]610 if (size > SIZE_MAX)
611 return ENOMEM;
[ed903174]612
[cf95bc0]613 void *newdata = realloc(nodep->data, size);
[efcebe1]614 if (!newdata)
615 return ENOMEM;
[ed903174]616
[cf95bc0]617 if (size > nodep->size) {
618 size_t delta = size - nodep->size;
619 memset(newdata + nodep->size, 0, delta);
[0ee4322]620 }
[ed903174]621
[cf95bc0]622 nodep->size = size;
623 nodep->data = newdata;
[efcebe1]624 return EOK;
[0ee4322]625}
626
[86ffa27f]627static int tmpfs_close(service_id_t service_id, fs_index_t index)
[c20aa06]628{
[efcebe1]629 return EOK;
[c20aa06]630}
631
[86ffa27f]632static int tmpfs_destroy(service_id_t service_id, fs_index_t index)
[f17667a]633{
[062d900]634 node_key_t key = {
635 .service_id = service_id,
636 .index = index
[8d049ee0]637 };
[062d900]638
639 ht_link_t *hlp = hash_table_find(&nodes, &key);
[efcebe1]640 if (!hlp)
641 return ENOENT;
[062d900]642 tmpfs_node_t *nodep = hash_table_get_inst(hlp, tmpfs_node_t,
[cf95bc0]643 nh_link);
[efcebe1]644 return tmpfs_destroy_node(FS_NODE(nodep));
[c20aa06]645}
646
[86ffa27f]647static int tmpfs_sync(service_id_t service_id, fs_index_t index)
[c20aa06]648{
[69a60c4]649 /*
650 * TMPFS keeps its data structures always consistent,
651 * thus the sync operation is a no-op.
652 */
[efcebe1]653 return EOK;
[c20aa06]654}
655
[efcebe1]656vfs_out_ops_t tmpfs_ops = {
657 .mounted = tmpfs_mounted,
658 .unmounted = tmpfs_unmounted,
659 .read = tmpfs_read,
660 .write = tmpfs_write,
661 .truncate = tmpfs_truncate,
662 .close = tmpfs_close,
663 .destroy = tmpfs_destroy,
664 .sync = tmpfs_sync,
665};
666
[d5cdffe]667/**
668 * @}
[c20aa06]669 */
[efcebe1]670
Note: See TracBrowser for help on using the repository browser.