Subversion Repositories HelenOS

Rev

Rev 2758 | Rev 2763 | 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
 
2747 jermar 67
/*
68
 * Implementation of the libfs interface.
69
 */
70
 
2742 jermar 71
/* Forward declarations of static functions. */
2760 jermar 72
static bool tmpfs_match(void *, void *, const char *);
2747 jermar 73
static void *tmpfs_create_node(int);
74
static bool tmpfs_link_node(void *, void *, const char *);
2758 jermar 75
static int tmpfs_unlink_node(void *, void *);
2747 jermar 76
static void tmpfs_destroy_node(void *);
2742 jermar 77
 
2747 jermar 78
/* Implementation of helper functions. */
79
static unsigned long tmpfs_index_get(void *nodep)
80
{
81
	return ((tmpfs_dentry_t *) nodep)->index;
82
}
83
 
84
static unsigned long tmpfs_size_get(void *nodep)
85
{
86
	return ((tmpfs_dentry_t *) nodep)->size;
87
}
88
 
89
static unsigned tmpfs_lnkcnt_get(void *nodep)
90
{
2756 jermar 91
	return ((tmpfs_dentry_t *) nodep)->lnkcnt;
2747 jermar 92
}
93
 
94
static void *tmpfs_child_get(void *nodep)
95
{
96
	return ((tmpfs_dentry_t *) nodep)->child;
97
}
98
 
99
static void *tmpfs_sibling_get(void *nodep)
100
{
101
	return ((tmpfs_dentry_t *) nodep)->sibling;
102
}
103
 
104
static void *tmpfs_root_get(void)
105
{
106
	return root; 
107
}
108
 
109
static char tmpfs_plb_get_char(unsigned pos)
110
{
111
	return tmpfs_reg.plb_ro[pos % PLB_SIZE];
112
}
113
 
114
static bool tmpfs_is_directory(void *nodep)
115
{
116
	return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
117
}
118
 
119
static bool tmpfs_is_file(void *nodep)
120
{
121
	return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
122
}
123
 
124
/** libfs operations */
125
libfs_ops_t tmpfs_libfs_ops = {
126
	.match = tmpfs_match,
127
	.create = tmpfs_create_node,
128
	.destroy = tmpfs_destroy_node,
129
	.link = tmpfs_link_node,
130
	.unlink = tmpfs_unlink_node,
131
	.index_get = tmpfs_index_get,
132
	.size_get = tmpfs_size_get,
133
	.lnkcnt_get = tmpfs_lnkcnt_get,
134
	.child_get = tmpfs_child_get,
135
	.sibling_get = tmpfs_sibling_get,
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
 
2655 jermar 170
unsigned tmpfs_next_index = 1;
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;
2747 jermar 233
	root = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
2760 jermar 234
	if (!root) {
235
		hash_table_destroy(&dentries);
236
		return false;
237
	}
2757 jermar 238
	root->lnkcnt = 1;
2760 jermar 239
	return true;
2655 jermar 240
}
241
 
2645 jermar 242
/** Compare one component of path to a directory entry.
243
 *
2760 jermar 244
 * @param prnt		Node from which we descended.
245
 * @param chld		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
 */
2760 jermar 250
bool tmpfs_match(void *prnt, void *chld, const char *component)
2645 jermar 251
{
2760 jermar 252
	tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
253
	tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
2730 jermar 254
 
2760 jermar 255
	unsigned long key = (unsigned long) parentp;
256
	link_t *hlp = hash_table_find(&childp->names, &key);
257
	assert(hlp);
258
	tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
259
 
260
	return !strcmp(namep->name, component);
2705 jermar 261
}
2655 jermar 262
 
2747 jermar 263
void *tmpfs_create_node(int lflag)
2705 jermar 264
{
2707 jermar 265
	assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
266
 
267
	tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
268
	if (!node)
2730 jermar 269
		return NULL;
2707 jermar 270
 
2760 jermar 271
	if (!tmpfs_dentry_initialize(node)) {
272
		free(node);
273
		return NULL;
274
	}
2707 jermar 275
	node->index = tmpfs_next_index++;
276
	if (lflag & L_DIRECTORY) 
277
		node->type = TMPFS_DIRECTORY;
278
	else 
279
		node->type = TMPFS_FILE;
280
 
2742 jermar 281
	/* Insert the new node into the dentry hash table. */
282
	hash_table_insert(&dentries, &node->index, &node->dh_link);
283
	return (void *) node;
284
}
285
 
2747 jermar 286
bool tmpfs_link_node(void *prnt, void *chld, const char *nm)
2742 jermar 287
{
288
	tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
289
	tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
290
 
291
	assert(parentp->type == TMPFS_DIRECTORY);
292
 
2760 jermar 293
	tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
294
	if (!namep)
295
		return false;
296
	tmpfs_name_initialize(namep);
2742 jermar 297
	size_t len = strlen(nm);
2760 jermar 298
	namep->name = malloc(len + 1);
299
	if (!namep->name) {
300
		free(namep);
2742 jermar 301
		return false;
2760 jermar 302
	}
303
	strcpy(namep->name, nm);
304
	namep->parent = parentp;
2756 jermar 305
 
306
	childp->lnkcnt++;
2742 jermar 307
 
2760 jermar 308
	unsigned long key = (unsigned long) parentp;
309
	hash_table_insert(&childp->names, &key, &namep->link);
310
 
2707 jermar 311
	/* Insert the new node into the namespace. */
2742 jermar 312
	if (parentp->child) {
313
		tmpfs_dentry_t *tmp = parentp->child;
2707 jermar 314
		while (tmp->sibling)
315
			tmp = tmp->sibling;
2742 jermar 316
		tmp->sibling = childp;
2707 jermar 317
	} else {
2742 jermar 318
		parentp->child = childp;
2707 jermar 319
	}
320
 
2742 jermar 321
	return true;
2705 jermar 322
}
2655 jermar 323
 
2758 jermar 324
int tmpfs_unlink_node(void *prnt, void *chld)
2705 jermar 325
{
2758 jermar 326
	tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
327
	tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
2733 jermar 328
 
2758 jermar 329
	if (!parentp)
330
		return EBUSY;
331
 
332
	if (childp->child)
2733 jermar 333
		return ENOTEMPTY;
334
 
2758 jermar 335
	if (parentp->child == childp) {
336
		parentp->child = childp->sibling;
2733 jermar 337
	} else {
338
		/* TODO: consider doubly linked list for organizing siblings. */
2758 jermar 339
		tmpfs_dentry_t *tmp = parentp->child;
340
		while (tmp->sibling != childp)
2733 jermar 341
			tmp = tmp->sibling;
2758 jermar 342
		tmp->sibling = childp->sibling;
2733 jermar 343
	}
2758 jermar 344
	childp->sibling = NULL;
2733 jermar 345
 
2760 jermar 346
	unsigned long key = (unsigned long) parentp;
347
	hash_table_remove(&childp->names, &key, 1);
2742 jermar 348
 
2758 jermar 349
	childp->lnkcnt--;
2756 jermar 350
 
2733 jermar 351
	return EOK;
2645 jermar 352
}
353
 
2747 jermar 354
void tmpfs_destroy_node(void *nodep)
2742 jermar 355
{
356
	tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
357
 
2756 jermar 358
	assert(!dentry->lnkcnt);
2742 jermar 359
	assert(!dentry->child);
360
	assert(!dentry->sibling);
361
 
362
	unsigned long index = dentry->index;
363
	hash_table_remove(&dentries, &index, 1);
364
 
2760 jermar 365
	hash_table_destroy(&dentry->names);
366
 
2742 jermar 367
	if (dentry->type == TMPFS_FILE)
368
		free(dentry->data);
369
	free(dentry);
370
}
371
 
2645 jermar 372
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
373
{
2747 jermar 374
	/* Initialize TMPFS. */
2655 jermar 375
	if (!root && !tmpfs_init()) {
376
		ipc_answer_0(rid, ENOMEM);
377
		return;
378
	}
2747 jermar 379
	libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
2645 jermar 380
}
381
 
2658 jermar 382
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
383
{
384
	int dev_handle = IPC_GET_ARG1(*request);
385
	unsigned long index = IPC_GET_ARG2(*request);
386
	off_t pos = IPC_GET_ARG3(*request);
387
 
388
	/*
389
	 * Lookup the respective dentry.
390
	 */
391
	link_t *hlp;
392
	hlp = hash_table_find(&dentries, &index);
393
	if (!hlp) {
394
		ipc_answer_0(rid, ENOENT);
395
		return;
396
	}
397
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
398
	    dh_link);
399
 
400
	/*
2663 jermar 401
	 * Receive the read request.
2658 jermar 402
	 */
403
	ipc_callid_t callid;
2673 jermar 404
	size_t len;
405
	if (!ipc_data_read_receive(&callid, &len)) {
2658 jermar 406
		ipc_answer_0(callid, EINVAL);	
407
		ipc_answer_0(rid, EINVAL);
408
		return;
409
	}
410
 
2699 jermar 411
	size_t bytes;
412
	if (dentry->type == TMPFS_FILE) {
413
		bytes = max(0, min(dentry->size - pos, len));
414
		(void) ipc_data_read_finalize(callid, dentry->data + pos,
415
		    bytes);
416
	} else {
417
		int i;
2739 jermar 418
		tmpfs_dentry_t *cur;
2699 jermar 419
 
420
		assert(dentry->type == TMPFS_DIRECTORY);
421
 
422
		/*
423
		 * Yes, we really use O(n) algorithm here.
424
		 * If it bothers someone, it could be fixed by introducing a
425
		 * hash table.
426
		 */
427
		for (i = 0, cur = dentry->child; i < pos && cur; i++,
428
		    cur = cur->sibling)
429
			;
2664 jermar 430
 
2699 jermar 431
		if (!cur) {
432
			ipc_answer_0(callid, ENOENT);
433
			ipc_answer_1(rid, ENOENT, 0);
434
			return;
435
		}
436
 
2760 jermar 437
		unsigned long key = (unsigned long) dentry;
438
		link_t *hlp = hash_table_find(&cur->names, &key);
439
		assert(hlp);
440
		tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
441
		    link);
442
 
443
		(void) ipc_data_read_finalize(callid, namep->name,
444
		    strlen(namep->name) + 1);
2699 jermar 445
		bytes = 1;
446
	}
447
 
2664 jermar 448
	/*
449
	 * Answer the VFS_READ call.
450
	 */
451
	ipc_answer_1(rid, EOK, bytes);
2658 jermar 452
}
453
 
2666 jermar 454
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
455
{
456
	int dev_handle = IPC_GET_ARG1(*request);
457
	unsigned long index = IPC_GET_ARG2(*request);
458
	off_t pos = IPC_GET_ARG3(*request);
459
 
460
	/*
461
	 * Lookup the respective dentry.
462
	 */
463
	link_t *hlp;
464
	hlp = hash_table_find(&dentries, &index);
465
	if (!hlp) {
466
		ipc_answer_0(rid, ENOENT);
467
		return;
468
	}
469
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
470
	    dh_link);
471
 
472
	/*
473
	 * Receive the write request.
474
	 */
475
	ipc_callid_t callid;
2673 jermar 476
	size_t len;
2676 jermar 477
	if (!ipc_data_write_receive(&callid, &len)) {
2666 jermar 478
		ipc_answer_0(callid, EINVAL);	
479
		ipc_answer_0(rid, EINVAL);
480
		return;
481
	}
482
 
483
	/*
2667 jermar 484
	 * Check whether the file needs to grow.
485
	 */
2673 jermar 486
	if (pos + len <= dentry->size) {
2667 jermar 487
		/* The file size is not changing. */
2678 jermar 488
		(void) ipc_data_write_finalize(callid, dentry->data + pos, len);
2710 jermar 489
		ipc_answer_2(rid, EOK, len, dentry->size);
2667 jermar 490
		return;
491
	}
2673 jermar 492
	size_t delta = (pos + len) - dentry->size;
2667 jermar 493
	/*
2666 jermar 494
	 * At this point, we are deliberately extremely straightforward and
2667 jermar 495
	 * simply realloc the contents of the file on every write that grows the
496
	 * file. In the end, the situation might not be as bad as it may look:
497
	 * our heap allocator can save us and just grow the block whenever
498
	 * possible.
2666 jermar 499
	 */
2667 jermar 500
	void *newdata = realloc(dentry->data, dentry->size + delta);
2666 jermar 501
	if (!newdata) {
502
		ipc_answer_0(callid, ENOMEM);
2710 jermar 503
		ipc_answer_2(rid, EOK, 0, dentry->size);
2666 jermar 504
		return;
505
	}
2693 jermar 506
	/* Clear any newly allocated memory in order to emulate gaps. */
2686 jermar 507
	memset(newdata + dentry->size, 0, delta);
2667 jermar 508
	dentry->size += delta;
2666 jermar 509
	dentry->data = newdata;
2678 jermar 510
	(void) ipc_data_write_finalize(callid, dentry->data + pos, len);
2687 jermar 511
	ipc_answer_2(rid, EOK, len, dentry->size);
2666 jermar 512
}
513
 
2693 jermar 514
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
515
{
516
	int dev_handle = IPC_GET_ARG1(*request);
517
	unsigned long index = IPC_GET_ARG2(*request);
518
	size_t size = IPC_GET_ARG3(*request);
519
 
520
	/*
521
	 * Lookup the respective dentry.
522
	 */
523
	link_t *hlp;
524
	hlp = hash_table_find(&dentries, &index);
525
	if (!hlp) {
526
		ipc_answer_0(rid, ENOENT);
527
		return;
528
	}
529
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
530
	    dh_link);
531
 
532
	if (size == dentry->size) {
533
		ipc_answer_0(rid, EOK);
534
		return;
535
	}
536
 
537
	void *newdata = realloc(dentry->data, size);
538
	if (!newdata) {
539
		ipc_answer_0(rid, ENOMEM);
540
		return;
541
	}
542
	if (size > dentry->size) {
543
		size_t delta = size - dentry->size;
544
		memset(newdata + dentry->size, 0, delta);
545
	}
546
	dentry->size = size;
547
	dentry->data = newdata;
548
	ipc_answer_0(rid, EOK);
549
}
550
 
2742 jermar 551
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request)
2731 jermar 552
{
553
	int dev_handle = IPC_GET_ARG1(*request);
554
	unsigned long index = IPC_GET_ARG2(*request);
555
 
556
	link_t *hlp;
557
	hlp = hash_table_find(&dentries, &index);
558
	if (!hlp) {
559
		ipc_answer_0(rid, ENOENT);
560
		return;
561
	}
562
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
563
	    dh_link);
2747 jermar 564
	tmpfs_destroy_node(dentry);
2731 jermar 565
	ipc_answer_0(rid, EOK);
566
}
567
 
2645 jermar 568
/**
569
 * @}
570
 */