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

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

rename device_get() method to service_get() to better reflect changes in the location service

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