Subversion Repositories HelenOS

Rev

Rev 4268 | Rev 4306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2645 jermar 1
/*
2685 jermar 2
 * Copyright (c) 2008 Jakub Jermar
2645 jermar 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>
2655 jermar 44
#include <atomic.h>
45
#include <stdlib.h>
46
#include <string.h>
47
#include <stdio.h>
2699 jermar 48
#include <assert.h>
2658 jermar 49
#include <sys/types.h>
50
#include <libadt/hash_table.h>
51
#include <as.h>
2747 jermar 52
#include <libfs.h>
2645 jermar 53
 
2658 jermar 54
#define min(a, b)		((a) < (b) ? (a) : (b))
55
#define max(a, b)		((a) > (b) ? (a) : (b))
56
 
57
#define DENTRIES_BUCKETS	256
58
 
2760 jermar 59
#define NAMES_BUCKETS		4
60
 
2747 jermar 61
/*
62
 * For now, we don't distinguish between different dev_handles/instances. All
63
 * requests resolve to the only instance, rooted in the following variable.
64
 */
65
static tmpfs_dentry_t *root;
2730 jermar 66
 
3575 jermar 67
#define TMPFS_DEV		0	/**< Dummy device handle for TMPFS */
68
 
2747 jermar 69
/*
70
 * Implementation of the libfs interface.
71
 */
72
 
2742 jermar 73
/* Forward declarations of static functions. */
2791 jermar 74
static void *tmpfs_match(void *, const char *);
2863 jermar 75
static void *tmpfs_node_get(dev_handle_t, fs_index_t);
2852 jermar 76
static void tmpfs_node_put(void *);
3575 jermar 77
static void *tmpfs_create_node(dev_handle_t, int);
3625 jermar 78
static int tmpfs_link_node(void *, void *, const char *);
2758 jermar 79
static int tmpfs_unlink_node(void *, void *);
2858 jermar 80
static int tmpfs_destroy_node(void *);
2742 jermar 81
 
2747 jermar 82
/* Implementation of helper functions. */
2770 jermar 83
static fs_index_t tmpfs_index_get(void *nodep)
2747 jermar 84
{
85
	return ((tmpfs_dentry_t *) nodep)->index;
86
}
87
 
2770 jermar 88
static size_t tmpfs_size_get(void *nodep)
2747 jermar 89
{
90
	return ((tmpfs_dentry_t *) nodep)->size;
91
}
92
 
93
static unsigned tmpfs_lnkcnt_get(void *nodep)
94
{
2756 jermar 95
	return ((tmpfs_dentry_t *) nodep)->lnkcnt;
2747 jermar 96
}
97
 
2791 jermar 98
static bool tmpfs_has_children(void *nodep)
2747 jermar 99
{
2791 jermar 100
	return ((tmpfs_dentry_t *) nodep)->child != NULL;
2747 jermar 101
}
102
 
2844 jermar 103
static void *tmpfs_root_get(dev_handle_t dev_handle)
2747 jermar 104
{
105
	return root; 
106
}
107
 
108
static char tmpfs_plb_get_char(unsigned pos)
109
{
110
	return tmpfs_reg.plb_ro[pos % PLB_SIZE];
111
}
112
 
113
static bool tmpfs_is_directory(void *nodep)
114
{
115
	return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
116
}
117
 
118
static bool tmpfs_is_file(void *nodep)
119
{
120
	return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
121
}
122
 
123
/** libfs operations */
124
libfs_ops_t tmpfs_libfs_ops = {
125
	.match = tmpfs_match,
2763 jermar 126
	.node_get = tmpfs_node_get,
2852 jermar 127
	.node_put = tmpfs_node_put,
2747 jermar 128
	.create = tmpfs_create_node,
129
	.destroy = tmpfs_destroy_node,
130
	.link = tmpfs_link_node,
131
	.unlink = tmpfs_unlink_node,
132
	.index_get = tmpfs_index_get,
133
	.size_get = tmpfs_size_get,
134
	.lnkcnt_get = tmpfs_lnkcnt_get,
2791 jermar 135
	.has_children = tmpfs_has_children,
2747 jermar 136
	.root_get = tmpfs_root_get,
137
	.plb_get_char = tmpfs_plb_get_char,
138
	.is_directory = tmpfs_is_directory,
139
	.is_file = tmpfs_is_file
140
};
141
 
2742 jermar 142
/** Hash table of all directory entries. */
2658 jermar 143
hash_table_t dentries;
144
 
2760 jermar 145
/* Implementation of hash table interface for the dentries hash table. */
2658 jermar 146
static hash_index_t dentries_hash(unsigned long *key)
147
{
148
	return *key % DENTRIES_BUCKETS;
149
}
150
 
151
static int dentries_compare(unsigned long *key, hash_count_t keys,
152
    link_t *item)
153
{
154
	tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
155
	    dh_link);
156
	return dentry->index == *key;
157
}
158
 
159
static void dentries_remove_callback(link_t *item)
160
{
161
}
162
 
163
/** TMPFS dentries hash table operations. */
164
hash_table_operations_t dentries_ops = {
165
	.hash = dentries_hash,
166
	.compare = dentries_compare,
167
	.remove_callback = dentries_remove_callback
168
};
169
 
2770 jermar 170
fs_index_t tmpfs_next_index = 1;
2655 jermar 171
 
2760 jermar 172
typedef struct {
173
	char *name;
174
	tmpfs_dentry_t *parent;
175
	link_t link;
176
} tmpfs_name_t;
177
 
178
/* Implementation of hash table interface for the names hash table. */
179
static hash_index_t names_hash(unsigned long *key)
2655 jermar 180
{
2760 jermar 181
	tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
182
	return dentry->index % NAMES_BUCKETS;
183
}
184
 
185
static int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
186
{
187
	tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
188
	tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
189
	    link);
190
	return dentry == namep->parent;
191
}
192
 
193
static void names_remove_callback(link_t *item)
194
{
195
	tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
196
	    link);
197
	free(namep->name);
198
	free(namep);
199
}
200
 
201
/** TMPFS node names hash table operations. */
202
static hash_table_operations_t names_ops = {
203
	.hash = names_hash,
204
	.compare = names_compare,
205
	.remove_callback = names_remove_callback
206
};
207
 
208
static void tmpfs_name_initialize(tmpfs_name_t *namep)
209
{
210
	namep->name = NULL;
211
	namep->parent = NULL;
212
	link_initialize(&namep->link);
213
}
214
 
215
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
216
{
2655 jermar 217
	dentry->index = 0;
218
	dentry->sibling = NULL;
219
	dentry->child = NULL;
220
	dentry->type = TMPFS_NONE;
2756 jermar 221
	dentry->lnkcnt = 0;
2655 jermar 222
	dentry->size = 0;
223
	dentry->data = NULL;
2658 jermar 224
	link_initialize(&dentry->dh_link);
2760 jermar 225
	return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
226
	    &names_ops);
2655 jermar 227
}
228
 
229
static bool tmpfs_init(void)
230
{
2658 jermar 231
	if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
232
		return false;
3575 jermar 233
	root = (tmpfs_dentry_t *) tmpfs_create_node(TMPFS_DEV, L_DIRECTORY);
2760 jermar 234
	if (!root) {
235
		hash_table_destroy(&dentries);
236
		return false;
237
	}
3352 jermar 238
	root->lnkcnt = 0;	/* FS root is not linked */
2760 jermar 239
	return true;
2655 jermar 240
}
241
 
2645 jermar 242
/** Compare one component of path to a directory entry.
243
 *
2791 jermar 244
 * @param parentp	Pointer to node from which we descended.
245
 * @param childp	Pointer to node to compare the path component with.
2705 jermar 246
 * @param component	Array of characters holding component name.
2645 jermar 247
 *
2705 jermar 248
 * @return		True on match, false otherwise.
2645 jermar 249
 */
2791 jermar 250
static bool
251
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
252
    const char *component)
2645 jermar 253
{
2760 jermar 254
	unsigned long key = (unsigned long) parentp;
255
	link_t *hlp = hash_table_find(&childp->names, &key);
256
	assert(hlp);
257
	tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
4264 svoboda 258
	return !str_cmp(namep->name, component);
2705 jermar 259
}
2655 jermar 260
 
2791 jermar 261
void *tmpfs_match(void *prnt, const char *component)
262
{
263
	tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
264
	tmpfs_dentry_t *childp = parentp->child;
265
 
266
	while (childp && !tmpfs_match_one(parentp, childp, component))
267
		childp = childp->sibling;
268
 
269
	return (void *) childp;
270
}
271
 
2770 jermar 272
void *
2863 jermar 273
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
2763 jermar 274
{
2770 jermar 275
	unsigned long key = index;
276
	link_t *lnk = hash_table_find(&dentries, &key);
2763 jermar 277
	if (!lnk)
278
		return NULL;
279
	return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link); 
280
}
281
 
2852 jermar 282
void tmpfs_node_put(void *node)
283
{
284
	/* nothing to do */
285
}
286
 
3575 jermar 287
void *tmpfs_create_node(dev_handle_t dev_handle, int lflag)
2705 jermar 288
{
2707 jermar 289
	assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
290
 
291
	tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
292
	if (!node)
2730 jermar 293
		return NULL;
2707 jermar 294
 
2760 jermar 295
	if (!tmpfs_dentry_initialize(node)) {
296
		free(node);
297
		return NULL;
298
	}
2707 jermar 299
	node->index = tmpfs_next_index++;
300
	if (lflag & L_DIRECTORY) 
301
		node->type = TMPFS_DIRECTORY;
302
	else 
303
		node->type = TMPFS_FILE;
304
 
2742 jermar 305
	/* Insert the new node into the dentry hash table. */
2770 jermar 306
	unsigned long key = node->index;
307
	hash_table_insert(&dentries, &key, &node->dh_link);
2742 jermar 308
	return (void *) node;
309
}
310
 
3625 jermar 311
int tmpfs_link_node(void *prnt, void *chld, const char *nm)
2742 jermar 312
{
313
	tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
314
	tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
315
 
316
	assert(parentp->type == TMPFS_DIRECTORY);
317
 
2760 jermar 318
	tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
319
	if (!namep)
3625 jermar 320
		return ENOMEM;
2760 jermar 321
	tmpfs_name_initialize(namep);
4264 svoboda 322
	size_t size = str_size(nm);
323
	namep->name = malloc(size + 1);
2760 jermar 324
	if (!namep->name) {
325
		free(namep);
3625 jermar 326
		return ENOMEM;
2760 jermar 327
	}
4268 svoboda 328
	str_cpy(namep->name, size + 1, nm);
2760 jermar 329
	namep->parent = parentp;
2756 jermar 330
 
331
	childp->lnkcnt++;
2742 jermar 332
 
2760 jermar 333
	unsigned long key = (unsigned long) parentp;
334
	hash_table_insert(&childp->names, &key, &namep->link);
335
 
2707 jermar 336
	/* Insert the new node into the namespace. */
2742 jermar 337
	if (parentp->child) {
338
		tmpfs_dentry_t *tmp = parentp->child;
2707 jermar 339
		while (tmp->sibling)
340
			tmp = tmp->sibling;
2742 jermar 341
		tmp->sibling = childp;
2707 jermar 342
	} else {
2742 jermar 343
		parentp->child = childp;
2707 jermar 344
	}
345
 
3625 jermar 346
	return EOK;
2705 jermar 347
}
2655 jermar 348
 
2758 jermar 349
int tmpfs_unlink_node(void *prnt, void *chld)
2705 jermar 350
{
2758 jermar 351
	tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
352
	tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
2733 jermar 353
 
2758 jermar 354
	if (!parentp)
355
		return EBUSY;
356
 
357
	if (childp->child)
2733 jermar 358
		return ENOTEMPTY;
359
 
2758 jermar 360
	if (parentp->child == childp) {
361
		parentp->child = childp->sibling;
2733 jermar 362
	} else {
363
		/* TODO: consider doubly linked list for organizing siblings. */
2758 jermar 364
		tmpfs_dentry_t *tmp = parentp->child;
365
		while (tmp->sibling != childp)
2733 jermar 366
			tmp = tmp->sibling;
2758 jermar 367
		tmp->sibling = childp->sibling;
2733 jermar 368
	}
2758 jermar 369
	childp->sibling = NULL;
2733 jermar 370
 
2760 jermar 371
	unsigned long key = (unsigned long) parentp;
372
	hash_table_remove(&childp->names, &key, 1);
2742 jermar 373
 
2758 jermar 374
	childp->lnkcnt--;
2756 jermar 375
 
2733 jermar 376
	return EOK;
2645 jermar 377
}
378
 
2858 jermar 379
int tmpfs_destroy_node(void *nodep)
2742 jermar 380
{
381
	tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
382
 
2756 jermar 383
	assert(!dentry->lnkcnt);
2742 jermar 384
	assert(!dentry->child);
385
	assert(!dentry->sibling);
386
 
2770 jermar 387
	unsigned long key = dentry->index;
388
	hash_table_remove(&dentries, &key, 1);
2742 jermar 389
 
2760 jermar 390
	hash_table_destroy(&dentry->names);
391
 
2742 jermar 392
	if (dentry->type == TMPFS_FILE)
393
		free(dentry->data);
394
	free(dentry);
2858 jermar 395
	return EOK;
2742 jermar 396
}
397
 
3109 jermar 398
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
399
{
400
	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
401
 
4305 jermar 402
	/* accept the mount options */
403
	ipc_callid_t callid;
404
	size_t size;
405
	if (!ipc_data_write_receive(&callid, &size)) {
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
	}
416
	ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
417
	if (retval != EOK) {
418
		ipc_answer_0(rid, retval);
419
		free(opts);
420
		return;
421
	}
422
	opts[size] = '\0';
423
 
3109 jermar 424
	/* Initialize TMPFS. */
425
	if (!root && !tmpfs_init()) {
426
		ipc_answer_0(rid, ENOMEM);
427
		return;
428
	}
429
 
4305 jermar 430
	if (str_cmp(opts, "restore") == 0) {
3109 jermar 431
		if (tmpfs_restore(dev_handle))
3352 jermar 432
			ipc_answer_3(rid, EOK, root->index, root->size,
433
			    root->lnkcnt);
3109 jermar 434
		else
435
			ipc_answer_0(rid, ELIMIT);
436
	} else {
3352 jermar 437
		ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt);
3109 jermar 438
	}
439
}
440
 
2958 jermar 441
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
442
{
3109 jermar 443
	dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
444
	fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request);
445
	fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
446
	dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
3087 decky 447
 
3109 jermar 448
	ipc_answer_0(rid, ENOTSUP);
2958 jermar 449
}
450
 
2645 jermar 451
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
452
{
2747 jermar 453
	libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
2645 jermar 454
}
455
 
2658 jermar 456
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
457
{
2770 jermar 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);
2658 jermar 461
 
462
	/*
463
	 * Lookup the respective dentry.
464
	 */
465
	link_t *hlp;
2770 jermar 466
	unsigned long key = index;
467
	hlp = hash_table_find(&dentries, &key);
2658 jermar 468
	if (!hlp) {
469
		ipc_answer_0(rid, ENOENT);
470
		return;
471
	}
472
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
473
	    dh_link);
474
 
475
	/*
2663 jermar 476
	 * Receive the read request.
2658 jermar 477
	 */
478
	ipc_callid_t callid;
4264 svoboda 479
	size_t size;
480
	if (!ipc_data_read_receive(&callid, &size)) {
2658 jermar 481
		ipc_answer_0(callid, EINVAL);	
482
		ipc_answer_0(rid, EINVAL);
483
		return;
484
	}
485
 
2699 jermar 486
	size_t bytes;
487
	if (dentry->type == TMPFS_FILE) {
4264 svoboda 488
		bytes = max(0, min(dentry->size - pos, size));
2699 jermar 489
		(void) ipc_data_read_finalize(callid, dentry->data + pos,
490
		    bytes);
491
	} else {
492
		int i;
2739 jermar 493
		tmpfs_dentry_t *cur;
2699 jermar 494
 
495
		assert(dentry->type == TMPFS_DIRECTORY);
496
 
497
		/*
498
		 * Yes, we really use O(n) algorithm here.
499
		 * If it bothers someone, it could be fixed by introducing a
500
		 * hash table.
501
		 */
502
		for (i = 0, cur = dentry->child; i < pos && cur; i++,
503
		    cur = cur->sibling)
504
			;
2664 jermar 505
 
2699 jermar 506
		if (!cur) {
507
			ipc_answer_0(callid, ENOENT);
508
			ipc_answer_1(rid, ENOENT, 0);
509
			return;
510
		}
511
 
2760 jermar 512
		unsigned long key = (unsigned long) dentry;
513
		link_t *hlp = hash_table_find(&cur->names, &key);
514
		assert(hlp);
515
		tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
516
		    link);
517
 
518
		(void) ipc_data_read_finalize(callid, namep->name,
4264 svoboda 519
		    str_size(namep->name) + 1);
2699 jermar 520
		bytes = 1;
521
	}
522
 
2664 jermar 523
	/*
524
	 * Answer the VFS_READ call.
525
	 */
526
	ipc_answer_1(rid, EOK, bytes);
2658 jermar 527
}
528
 
2666 jermar 529
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
530
{
2770 jermar 531
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
532
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
533
	off_t pos = (off_t)IPC_GET_ARG3(*request);
2666 jermar 534
 
535
	/*
536
	 * Lookup the respective dentry.
537
	 */
538
	link_t *hlp;
2770 jermar 539
	unsigned long key = index;
540
	hlp = hash_table_find(&dentries, &key);
2666 jermar 541
	if (!hlp) {
542
		ipc_answer_0(rid, ENOENT);
543
		return;
544
	}
545
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
546
	    dh_link);
547
 
548
	/*
549
	 * Receive the write request.
550
	 */
551
	ipc_callid_t callid;
4264 svoboda 552
	size_t size;
553
	if (!ipc_data_write_receive(&callid, &size)) {
2666 jermar 554
		ipc_answer_0(callid, EINVAL);	
555
		ipc_answer_0(rid, EINVAL);
556
		return;
557
	}
558
 
559
	/*
2667 jermar 560
	 * Check whether the file needs to grow.
561
	 */
4264 svoboda 562
	if (pos + size <= dentry->size) {
2667 jermar 563
		/* The file size is not changing. */
4264 svoboda 564
		(void) ipc_data_write_finalize(callid, dentry->data + pos, size);
565
		ipc_answer_2(rid, EOK, size, dentry->size);
2667 jermar 566
		return;
567
	}
4264 svoboda 568
	size_t delta = (pos + size) - dentry->size;
2667 jermar 569
	/*
2666 jermar 570
	 * At this point, we are deliberately extremely straightforward and
2667 jermar 571
	 * simply realloc the contents of the file on every write that grows the
572
	 * file. In the end, the situation might not be as bad as it may look:
573
	 * our heap allocator can save us and just grow the block whenever
574
	 * possible.
2666 jermar 575
	 */
2667 jermar 576
	void *newdata = realloc(dentry->data, dentry->size + delta);
2666 jermar 577
	if (!newdata) {
578
		ipc_answer_0(callid, ENOMEM);
2710 jermar 579
		ipc_answer_2(rid, EOK, 0, dentry->size);
2666 jermar 580
		return;
581
	}
2693 jermar 582
	/* Clear any newly allocated memory in order to emulate gaps. */
2686 jermar 583
	memset(newdata + dentry->size, 0, delta);
2667 jermar 584
	dentry->size += delta;
2666 jermar 585
	dentry->data = newdata;
4264 svoboda 586
	(void) ipc_data_write_finalize(callid, dentry->data + pos, size);
587
	ipc_answer_2(rid, EOK, size, dentry->size);
2666 jermar 588
}
589
 
2693 jermar 590
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
591
{
2770 jermar 592
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
593
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
594
	size_t size = (off_t)IPC_GET_ARG3(*request);
2693 jermar 595
 
596
	/*
597
	 * Lookup the respective dentry.
598
	 */
599
	link_t *hlp;
2770 jermar 600
	unsigned long key = index;
601
	hlp = hash_table_find(&dentries, &key);
2693 jermar 602
	if (!hlp) {
603
		ipc_answer_0(rid, ENOENT);
604
		return;
605
	}
606
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
607
	    dh_link);
608
 
609
	if (size == dentry->size) {
610
		ipc_answer_0(rid, EOK);
611
		return;
612
	}
613
 
614
	void *newdata = realloc(dentry->data, size);
615
	if (!newdata) {
616
		ipc_answer_0(rid, ENOMEM);
617
		return;
618
	}
619
	if (size > dentry->size) {
620
		size_t delta = size - dentry->size;
621
		memset(newdata + dentry->size, 0, delta);
622
	}
623
	dentry->size = size;
624
	dentry->data = newdata;
625
	ipc_answer_0(rid, EOK);
626
}
627
 
2742 jermar 628
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
2731 jermar 629
{
2770 jermar 630
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
631
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
2858 jermar 632
	int rc;
2731 jermar 633
 
634
	link_t *hlp;
2770 jermar 635
	unsigned long key = index;
636
	hlp = hash_table_find(&dentries, &key);
2731 jermar 637
	if (!hlp) {
638
		ipc_answer_0(rid, ENOENT);
639
		return;
640
	}
641
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
642
	    dh_link);
2858 jermar 643
	rc = tmpfs_destroy_node(dentry);
644
	ipc_answer_0(rid, rc);
2731 jermar 645
}
646
 
2645 jermar 647
/**
648
 * @}
649
 */