Subversion Repositories HelenOS

Rev

Rev 2687 | Rev 2699 | 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>
2658 jermar 48
#include <sys/types.h>
49
#include <libadt/hash_table.h>
50
#include <as.h>
2645 jermar 51
 
2658 jermar 52
#define min(a, b)		((a) < (b) ? (a) : (b))
53
#define max(a, b)		((a) > (b) ? (a) : (b))
54
 
2645 jermar 55
#define PLB_GET_CHAR(i)		(tmpfs_reg.plb_ro[(i) % PLB_SIZE])
56
 
2658 jermar 57
#define DENTRIES_BUCKETS	256
58
 
59
/*
60
 * Hash table of all directory entries.
61
 */
62
hash_table_t dentries;
63
 
64
static hash_index_t dentries_hash(unsigned long *key)
65
{
66
	return *key % DENTRIES_BUCKETS;
67
}
68
 
69
static int dentries_compare(unsigned long *key, hash_count_t keys,
70
    link_t *item)
71
{
72
	tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
73
	    dh_link);
74
	return dentry->index == *key;
75
}
76
 
77
static void dentries_remove_callback(link_t *item)
78
{
79
}
80
 
81
/** TMPFS dentries hash table operations. */
82
hash_table_operations_t dentries_ops = {
83
	.hash = dentries_hash,
84
	.compare = dentries_compare,
85
	.remove_callback = dentries_remove_callback
86
};
87
 
2655 jermar 88
unsigned tmpfs_next_index = 1;
89
 
90
static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
91
{
92
	dentry->index = 0;
93
	dentry->parent = NULL;
94
	dentry->sibling = NULL;
95
	dentry->child = NULL;
96
	dentry->name = NULL;
97
	dentry->type = TMPFS_NONE;
98
	dentry->size = 0;
99
	dentry->data = NULL;
2658 jermar 100
	link_initialize(&dentry->dh_link);
2655 jermar 101
}
102
 
103
/*
104
 * For now, we don't distinguish between different dev_handles/instances. All
105
 * requests resolve to the only instance, rooted in the following variable.
106
 */
107
static tmpfs_dentry_t *root;
108
 
109
static bool tmpfs_init(void)
110
{
2658 jermar 111
	if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
112
		return false;
113
 
2655 jermar 114
	root = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
2658 jermar 115
	if (!root)
2655 jermar 116
		return false;
117
	tmpfs_dentry_initialize(root);
118
	root->index = tmpfs_next_index++;
119
	root->name = "";
120
	root->type = TMPFS_DIRECTORY;
2658 jermar 121
	hash_table_insert(&dentries, &root->index, &root->dh_link);
2655 jermar 122
 
123
	/*
124
	 * This is only for debugging. Once we can create files and directories
125
	 * using VFS, we can get rid of this.
126
	 */
127
	tmpfs_dentry_t *d;
128
	d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
129
	if (!d) {
130
		free(root);
131
		root = NULL;
132
		return false;
133
	}
134
	tmpfs_dentry_initialize(d);
135
	d->index = tmpfs_next_index++;
136
	root->child = d;
137
	d->parent = root;
138
	d->type = TMPFS_DIRECTORY;
139
	d->name = "dir1";
2658 jermar 140
	hash_table_insert(&dentries, &d->index, &d->dh_link);
2655 jermar 141
 
142
	d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
143
	if (!d) {
144
		free(root->child);
145
		free(root);
146
		root = NULL;
147
		return false;
148
	}
149
	tmpfs_dentry_initialize(d);
150
	d->index = tmpfs_next_index++;
151
	root->child->sibling = d;
152
	d->parent = root;
153
	d->type = TMPFS_DIRECTORY;
154
	d->name = "dir2";
2658 jermar 155
	hash_table_insert(&dentries, &d->index, &d->dh_link);
2655 jermar 156
 
157
	d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
158
	if (!d) {
159
		free(root->child->sibling);
160
		free(root->child);
161
		free(root);
162
		root = NULL;
163
		return false;
164
	}
165
	tmpfs_dentry_initialize(d);
166
	d->index = tmpfs_next_index++;
167
	root->child->child = d;
168
	d->parent = root->child;
169
	d->type = TMPFS_FILE;
170
	d->name = "file1";
171
	d->data = "This is the contents of /dir1/file1.\n";
172
	d->size = strlen(d->data);
2658 jermar 173
	hash_table_insert(&dentries, &d->index, &d->dh_link);
2655 jermar 174
 
175
	d = (tmpfs_dentry_t *) malloc(sizeof(tmpfs_dentry_t));
176
	if (!d) {
177
		free(root->child->sibling);
178
		free(root->child->child);
179
		free(root->child);
180
		free(root);
181
		root = NULL;
182
		return false;
183
	}
184
	tmpfs_dentry_initialize(d);
185
	d->index = tmpfs_next_index++;
186
	root->child->sibling->child = d;
187
	d->parent = root->child->sibling;
188
	d->type = TMPFS_FILE;
189
	d->name = "file2";
190
	d->data = "This is the contents of /dir2/file2.\n";
191
	d->size = strlen(d->data);
2658 jermar 192
	hash_table_insert(&dentries, &d->index, &d->dh_link);
2655 jermar 193
 
194
	return true;
195
}
196
 
2645 jermar 197
/** Compare one component of path to a directory entry.
198
 *
199
 * @param dentry	Directory entry to compare the path component with.
200
 * @param start		Index into PLB where the path component starts.
201
 * @param last		Index of the last character of the path in PLB.
202
 *
203
 * @return		Zero on failure or delta such that (index + delta) %
2655 jermar 204
 *			PLB_SIZE points	to the first unprocessed character in
205
 *			PLB which comprises the path.
2645 jermar 206
 */
207
static unsigned match_path_component(tmpfs_dentry_t *dentry, unsigned start,
208
    unsigned last)
209
{
2655 jermar 210
	int i, j;
211
	size_t namelen;
212
 
213
	namelen = strlen(dentry->name);
214
 
215
	if (last < start)
216
		last += PLB_SIZE;
217
 
218
	for (i = 0, j = start; i < namelen && j <= last; i++, j++) {
219
		if (dentry->name[i] != PLB_GET_CHAR(j))
220
			return 0;
221
	}
222
 
223
	if (i != namelen)
224
		return 0;
225
	if (j < last && PLB_GET_CHAR(j) != '/')
226
		return 0;
227
	if (j == last)
228
	    	return 0;
229
 
230
	return (j - start);
2645 jermar 231
}
232
 
233
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
234
{
2655 jermar 235
	unsigned next = IPC_GET_ARG1(*request);
236
	unsigned last = IPC_GET_ARG2(*request);
2645 jermar 237
	int dev_handle = IPC_GET_ARG3(*request);
238
 
2655 jermar 239
	if (last < next)
240
		last += PLB_SIZE;
241
 
242
	if (!root && !tmpfs_init()) {
243
		ipc_answer_0(rid, ENOMEM);
244
		return;
245
	}
246
 
247
	tmpfs_dentry_t *dtmp = root->child; 
248
	tmpfs_dentry_t *dcur = root;
249
 
250
	bool hit = true;
2645 jermar 251
 
2655 jermar 252
	if (PLB_GET_CHAR(next) == '/')
253
		next++;		/* eat slash */
254
 
255
	while (next <= last) {
256
		unsigned delta;
257
		hit = false;
258
		do {
259
			delta = match_path_component(dtmp, next, last);
260
			if (!delta) {
261
				dtmp = dtmp->sibling;
262
			} else {
263
				hit = true;
264
				next += delta;
265
				next++;		/* eat slash */
266
				dcur = dtmp;
267
				dtmp = dtmp->child;
268
			}	
269
		} while (delta == 0 && dtmp);
270
		if (!hit) {
271
			ipc_answer_3(rid, ENOENT, tmpfs_reg.fs_handle,
272
			    dev_handle, dcur->index);
273
			return;
274
		}
275
	}
276
 
2687 jermar 277
	ipc_answer_4(rid, EOK, tmpfs_reg.fs_handle, dev_handle, dcur->index,
278
	    dcur->size);
2645 jermar 279
}
280
 
2658 jermar 281
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
282
{
283
	int dev_handle = IPC_GET_ARG1(*request);
284
	unsigned long index = IPC_GET_ARG2(*request);
285
	off_t pos = IPC_GET_ARG3(*request);
286
 
287
	/*
288
	 * Lookup the respective dentry.
289
	 */
290
	link_t *hlp;
291
	hlp = hash_table_find(&dentries, &index);
292
	if (!hlp) {
293
		ipc_answer_0(rid, ENOENT);
294
		return;
295
	}
296
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
297
	    dh_link);
298
 
299
	/*
2663 jermar 300
	 * Receive the read request.
2658 jermar 301
	 */
302
	ipc_callid_t callid;
2673 jermar 303
	size_t len;
304
	if (!ipc_data_read_receive(&callid, &len)) {
2658 jermar 305
		ipc_answer_0(callid, EINVAL);	
306
		ipc_answer_0(rid, EINVAL);
307
		return;
308
	}
309
 
2673 jermar 310
	size_t bytes = max(0, min(dentry->size - pos, len));
2678 jermar 311
	(void) ipc_data_read_finalize(callid, dentry->data + pos, bytes);
2664 jermar 312
 
313
	/*
314
	 * Answer the VFS_READ call.
315
	 */
316
	ipc_answer_1(rid, EOK, bytes);
2658 jermar 317
}
318
 
2666 jermar 319
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request)
320
{
321
	int dev_handle = IPC_GET_ARG1(*request);
322
	unsigned long index = IPC_GET_ARG2(*request);
323
	off_t pos = IPC_GET_ARG3(*request);
324
 
325
	/*
326
	 * Lookup the respective dentry.
327
	 */
328
	link_t *hlp;
329
	hlp = hash_table_find(&dentries, &index);
330
	if (!hlp) {
331
		ipc_answer_0(rid, ENOENT);
332
		return;
333
	}
334
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
335
	    dh_link);
336
 
337
	/*
338
	 * Receive the write request.
339
	 */
340
	ipc_callid_t callid;
2673 jermar 341
	size_t len;
2676 jermar 342
	if (!ipc_data_write_receive(&callid, &len)) {
2666 jermar 343
		ipc_answer_0(callid, EINVAL);	
344
		ipc_answer_0(rid, EINVAL);
345
		return;
346
	}
347
 
348
	/*
2667 jermar 349
	 * Check whether the file needs to grow.
350
	 */
2673 jermar 351
	if (pos + len <= dentry->size) {
2667 jermar 352
		/* The file size is not changing. */
2678 jermar 353
		(void) ipc_data_write_finalize(callid, dentry->data + pos, len);
2673 jermar 354
		ipc_answer_1(rid, EOK, len);
2667 jermar 355
		return;
356
	}
2673 jermar 357
	size_t delta = (pos + len) - dentry->size;
2667 jermar 358
	/*
2666 jermar 359
	 * At this point, we are deliberately extremely straightforward and
2667 jermar 360
	 * simply realloc the contents of the file on every write that grows the
361
	 * file. In the end, the situation might not be as bad as it may look:
362
	 * our heap allocator can save us and just grow the block whenever
363
	 * possible.
2666 jermar 364
	 */
2667 jermar 365
	void *newdata = realloc(dentry->data, dentry->size + delta);
2666 jermar 366
	if (!newdata) {
367
		ipc_answer_0(callid, ENOMEM);
368
		ipc_answer_1(rid, EOK, 0);
369
		return;
370
	}
2693 jermar 371
	/* Clear any newly allocated memory in order to emulate gaps. */
2686 jermar 372
	memset(newdata + dentry->size, 0, delta);
2667 jermar 373
	dentry->size += delta;
2666 jermar 374
	dentry->data = newdata;
2678 jermar 375
	(void) ipc_data_write_finalize(callid, dentry->data + pos, len);
2687 jermar 376
	ipc_answer_2(rid, EOK, len, dentry->size);
2666 jermar 377
}
378
 
2693 jermar 379
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
380
{
381
	int dev_handle = IPC_GET_ARG1(*request);
382
	unsigned long index = IPC_GET_ARG2(*request);
383
	size_t size = IPC_GET_ARG3(*request);
384
 
385
	/*
386
	 * Lookup the respective dentry.
387
	 */
388
	link_t *hlp;
389
	hlp = hash_table_find(&dentries, &index);
390
	if (!hlp) {
391
		ipc_answer_0(rid, ENOENT);
392
		return;
393
	}
394
	tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
395
	    dh_link);
396
 
397
	if (size == dentry->size) {
398
		ipc_answer_0(rid, EOK);
399
		return;
400
	}
401
 
402
	void *newdata = realloc(dentry->data, size);
403
	if (!newdata) {
404
		ipc_answer_0(rid, ENOMEM);
405
		return;
406
	}
407
	if (size > dentry->size) {
408
		size_t delta = size - dentry->size;
409
		memset(newdata + dentry->size, 0, delta);
410
	}
411
	dentry->size = size;
412
	dentry->data = newdata;
413
	ipc_answer_0(rid, EOK);
414
}
415
 
2645 jermar 416
/**
417
 * @}
418
 */