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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 196a1439 was 1313ee9, checked in by Martin Decky <martin@…>, 16 years ago

introduce device namespaces

  • add support for explicit open in libfs (needed by devfs, but also possibly for other filesystems which need to track some stateful information)
  • extend libfs to be more generic, make proper adjustments to libc, tmpfs and fat
  • various updates to make use of the device namespaces
  • code cleanup
  • Property mode set to 100644
File size: 16.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 * @{
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
[cf95bc0]149#define NODES_KEY_INDEX 0
150#define NODES_KEY_DEV 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[] = {
[cf95bc0]239 [NODES_KEY_INDEX] = index,
240 [NODES_KEY_DEV] = dev_handle
[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[] = {
[cf95bc0]298 [NODES_KEY_INDEX] = nodep->index,
299 [NODES_KEY_DEV] = nodep->dev_handle
[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[] = {
314 [NODES_KEY_INDEX] = nodep->index,
315 [NODES_KEY_DEV] = nodep->dev_handle
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)) {
[4b11571]426 ipc_answer_0(rid, ENOMEM);
427 return;
428 }
[f49b0ea]429
[54e4479]430 fs_node_t *rootfn;
431 rc = tmpfs_root_get(&rootfn, dev_handle);
432 assert(rc == EOK);
433 tmpfs_node_t *rootp = TMPFS_NODE(rootfn);
[594303b]434 if (str_cmp(opts, "restore") == 0) {
[f49b0ea]435 if (tmpfs_restore(dev_handle))
[cf95bc0]436 ipc_answer_3(rid, EOK, rootp->index, rootp->size,
437 rootp->lnkcnt);
[f49b0ea]438 else
439 ipc_answer_0(rid, ELIMIT);
440 } else {
[cf95bc0]441 ipc_answer_3(rid, EOK, rootp->index, rootp->size,
442 rootp->lnkcnt);
[f49b0ea]443 }
444}
445
446void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
447{
[16d17ca]448 libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
[f49b0ea]449}
450
451void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
452{
[2c448fb]453 libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
[d5cdffe]454}
455
[a4eb8a60]456void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
457{
[f2ec8c8]458 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
459 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
460 off_t pos = (off_t)IPC_GET_ARG3(*request);
[a4eb8a60]461
462 /*
[cf95bc0]463 * Lookup the respective TMPFS node.
[a4eb8a60]464 */
465 link_t *hlp;
[8d049ee0]466 unsigned long key[] = {
[cf95bc0]467 [NODES_KEY_INDEX] = index,
468 [NODES_KEY_DEV] = dev_handle,
[8d049ee0]469 };
[cf95bc0]470 hlp = hash_table_find(&nodes, key);
[a4eb8a60]471 if (!hlp) {
472 ipc_answer_0(rid, ENOENT);
473 return;
474 }
[cf95bc0]475 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
476 nh_link);
[a4eb8a60]477
478 /*
[a92da0a]479 * Receive the read request.
[a4eb8a60]480 */
481 ipc_callid_t callid;
[92fd52d7]482 size_t size;
[0da4e41]483 if (!async_data_read_receive(&callid, &size)) {
[a4eb8a60]484 ipc_answer_0(callid, EINVAL);
485 ipc_answer_0(rid, EINVAL);
486 return;
487 }
488
[5973fd0]489 size_t bytes;
[cf95bc0]490 if (nodep->type == TMPFS_FILE) {
491 bytes = max(0, min(nodep->size - pos, size));
[0da4e41]492 (void) async_data_read_finalize(callid, nodep->data + pos,
[5973fd0]493 bytes);
494 } else {
[cf95bc0]495 tmpfs_dentry_t *dentryp;
496 link_t *lnk;
[5973fd0]497 int i;
498
[cf95bc0]499 assert(nodep->type == TMPFS_DIRECTORY);
[5973fd0]500
501 /*
502 * Yes, we really use O(n) algorithm here.
503 * If it bothers someone, it could be fixed by introducing a
504 * hash table.
505 */
[cf95bc0]506 for (i = 0, lnk = nodep->cs_head.next;
507 i < pos && lnk != &nodep->cs_head;
508 i++, lnk = lnk->next)
[5973fd0]509 ;
510
[cf95bc0]511 if (lnk == &nodep->cs_head) {
[5973fd0]512 ipc_answer_0(callid, ENOENT);
513 ipc_answer_1(rid, ENOENT, 0);
514 return;
515 }
516
[cf95bc0]517 dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
[3298ddc]518
[0da4e41]519 (void) async_data_read_finalize(callid, dentryp->name,
[cf95bc0]520 str_size(dentryp->name) + 1);
[5973fd0]521 bytes = 1;
522 }
[7dab6b8]523
524 /*
525 * Answer the VFS_READ call.
526 */
527 ipc_answer_1(rid, EOK, bytes);
[a4eb8a60]528}
529
[ee1b8ca]530void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
531{
[f2ec8c8]532 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
533 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
534 off_t pos = (off_t)IPC_GET_ARG3(*request);
[ee1b8ca]535
536 /*
[cf95bc0]537 * Lookup the respective TMPFS node.
[ee1b8ca]538 */
539 link_t *hlp;
[8d049ee0]540 unsigned long key[] = {
[cf95bc0]541 [NODES_KEY_INDEX] = index,
542 [NODES_KEY_DEV] = dev_handle
[8d049ee0]543 };
[cf95bc0]544 hlp = hash_table_find(&nodes, key);
[ee1b8ca]545 if (!hlp) {
546 ipc_answer_0(rid, ENOENT);
547 return;
548 }
[cf95bc0]549 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
550 nh_link);
[ee1b8ca]551
552 /*
553 * Receive the write request.
554 */
555 ipc_callid_t callid;
[92fd52d7]556 size_t size;
[0da4e41]557 if (!async_data_write_receive(&callid, &size)) {
[ee1b8ca]558 ipc_answer_0(callid, EINVAL);
559 ipc_answer_0(rid, EINVAL);
560 return;
561 }
562
[c1bf5cb]563 /*
564 * Check whether the file needs to grow.
565 */
[cf95bc0]566 if (pos + size <= nodep->size) {
[c1bf5cb]567 /* The file size is not changing. */
[0da4e41]568 (void) async_data_write_finalize(callid, nodep->data + pos, size);
[cf95bc0]569 ipc_answer_2(rid, EOK, size, nodep->size);
[c1bf5cb]570 return;
571 }
[cf95bc0]572 size_t delta = (pos + size) - nodep->size;
[ee1b8ca]573 /*
574 * At this point, we are deliberately extremely straightforward and
[c1bf5cb]575 * simply realloc the contents of the file on every write that grows the
576 * file. In the end, the situation might not be as bad as it may look:
577 * our heap allocator can save us and just grow the block whenever
578 * possible.
[ee1b8ca]579 */
[cf95bc0]580 void *newdata = realloc(nodep->data, nodep->size + delta);
[ee1b8ca]581 if (!newdata) {
582 ipc_answer_0(callid, ENOMEM);
[cf95bc0]583 ipc_answer_2(rid, EOK, 0, nodep->size);
[ee1b8ca]584 return;
585 }
[0ee4322]586 /* Clear any newly allocated memory in order to emulate gaps. */
[cf95bc0]587 memset(newdata + nodep->size, 0, delta);
588 nodep->size += delta;
589 nodep->data = newdata;
[0da4e41]590 (void) async_data_write_finalize(callid, nodep->data + pos, size);
[cf95bc0]591 ipc_answer_2(rid, EOK, size, nodep->size);
[ee1b8ca]592}
593
[0ee4322]594void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
595{
[f2ec8c8]596 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
597 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
598 size_t size = (off_t)IPC_GET_ARG3(*request);
[0ee4322]599
600 /*
[cf95bc0]601 * Lookup the respective TMPFS node.
[0ee4322]602 */
603 link_t *hlp;
[8d049ee0]604 unsigned long key[] = {
[cf95bc0]605 [NODES_KEY_INDEX] = index,
606 [NODES_KEY_DEV] = dev_handle
[8d049ee0]607 };
[cf95bc0]608 hlp = hash_table_find(&nodes, key);
[0ee4322]609 if (!hlp) {
610 ipc_answer_0(rid, ENOENT);
611 return;
612 }
[cf95bc0]613 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
614 nh_link);
[0ee4322]615
[cf95bc0]616 if (size == nodep->size) {
[0ee4322]617 ipc_answer_0(rid, EOK);
618 return;
619 }
620
[cf95bc0]621 void *newdata = realloc(nodep->data, size);
[0ee4322]622 if (!newdata) {
623 ipc_answer_0(rid, ENOMEM);
624 return;
625 }
[cf95bc0]626 if (size > nodep->size) {
627 size_t delta = size - nodep->size;
628 memset(newdata + nodep->size, 0, delta);
[0ee4322]629 }
[cf95bc0]630 nodep->size = size;
631 nodep->data = newdata;
[0ee4322]632 ipc_answer_0(rid, EOK);
633}
634
[c20aa06]635void tmpfs_close(ipc_callid_t rid, ipc_call_t *request)
636{
637 ipc_answer_0(rid, EOK);
638}
639
[fdb7795]640void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
[f17667a]641{
[f2ec8c8]642 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
643 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
[45f244b]644 int rc;
[f17667a]645
646 link_t *hlp;
[8d049ee0]647 unsigned long key[] = {
[cf95bc0]648 [NODES_KEY_INDEX] = index,
649 [NODES_KEY_DEV] = dev_handle
[8d049ee0]650 };
[cf95bc0]651 hlp = hash_table_find(&nodes, key);
[f17667a]652 if (!hlp) {
653 ipc_answer_0(rid, ENOENT);
654 return;
655 }
[cf95bc0]656 tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
657 nh_link);
658 rc = tmpfs_destroy_node(FS_NODE(nodep));
[45f244b]659 ipc_answer_0(rid, rc);
[f17667a]660}
661
[c20aa06]662void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request)
663{
664 libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
665}
666
[852b801]667void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request)
[c20aa06]668{
[75160a6]669 libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
[c20aa06]670}
671
672void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
673{
674 /* Dummy implementation */
675 ipc_answer_0(rid, EOK);
676}
677
[d5cdffe]678/**
679 * @}
[c20aa06]680 */
Note: See TracBrowser for help on using the repository browser.