Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
2627 jermar 1
/*
2793 jermar 2
 * Copyright (c) 2008 Jakub Jermar
2627 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	fat_ops.c
35
 * @brief	Implementation of VFS operations for the FAT file system server.
36
 */
37
 
38
#include "fat.h"
2638 jermar 39
#include "../../vfs/vfs.h"
2793 jermar 40
#include <libfs.h>
2627 jermar 41
#include <ipc/ipc.h>
42
#include <async.h>
43
#include <errno.h>
2793 jermar 44
#include <string.h>
2798 jermar 45
#include <byteorder.h>
2831 jermar 46
#include <libadt/hash_table.h>
47
#include <libadt/list.h>
48
#include <assert.h>
2856 jermar 49
#include <futex.h>
2627 jermar 50
 
2843 jermar 51
#define BS_BLOCK		0
52
 
53
#define FIN_KEY_DEV_HANDLE	0
54
#define FIN_KEY_INDEX		1
55
 
2856 jermar 56
/** Futex protecting both fin_hash and ffn_head. */
57
futex_t fin_futex = FUTEX_INITIALIZER;
58
 
2831 jermar 59
/** Hash table of FAT in-core nodes. */
60
hash_table_t fin_hash;
61
 
62
/** List of free FAT in-core nodes. */
63
link_t ffn_head;
64
 
2638 jermar 65
#define FAT_NAME_LEN		8
66
#define FAT_EXT_LEN		3
67
 
68
#define FAT_PAD			' ' 
69
 
70
#define FAT_DENTRY_UNUSED	0x00
71
#define FAT_DENTRY_E5_ESC	0x05
72
#define FAT_DENTRY_DOT		0x2e
73
#define FAT_DENTRY_ERASED	0xe5
74
 
2793 jermar 75
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
2638 jermar 76
{
2793 jermar 77
	int i;
2639 jermar 78
 
2793 jermar 79
	for (i = 0; i < FAT_NAME_LEN; i++) {
80
		if (d->name[i] == FAT_PAD) {
81
			buf++;
82
			break;
2639 jermar 83
		}
2793 jermar 84
		if (d->name[i] == FAT_DENTRY_E5_ESC)
85
			*buf++ = 0xe5;
86
		else
87
			*buf++ = d->name[i];
88
	}
89
	if (d->ext[0] != FAT_PAD)
90
		*buf++ = '.';
91
	for (i = 0; i < FAT_EXT_LEN; i++) {
92
		if (d->ext[i] == FAT_PAD) {
93
			*buf = '\0';
94
			return;
95
		}
96
		if (d->ext[i] == FAT_DENTRY_E5_ESC)
97
			*buf++ = 0xe5;
98
		else
99
			*buf++ = d->ext[i];
100
	}
101
}
102
 
2822 jermar 103
/* TODO and also move somewhere else */
104
typedef struct {
105
	void *data;
106
} block_t;
107
 
108
static block_t *block_get(dev_handle_t dev_handle, off_t offset)
2793 jermar 109
{
110
	return NULL;	/* TODO */
111
}
112
 
2831 jermar 113
static block_t *fat_block_get(dev_handle_t dev_handle, fs_index_t index,
114
    off_t offset) {
2822 jermar 115
	return NULL;	/* TODO */
116
}
117
 
118
static void block_put(block_t *block)
2793 jermar 119
{
120
	/* TODO */
121
}
122
 
2831 jermar 123
static void fat_node_initialize(fat_node_t *node)
2793 jermar 124
{
2856 jermar 125
	futex_initialize(&node->lock, 1);
2831 jermar 126
	node->type = 0;
127
	node->index = 0;
128
	node->pindex = 0;
129
	node->dev_handle = 0;
130
	link_initialize(&node->fin_link);
131
	link_initialize(&node->ffn_link);
132
	node->size = 0;
133
	node->lnkcnt = 0;
134
	node->refcnt = 0;
135
	node->dirty = false;
2793 jermar 136
}
137
 
2843 jermar 138
static uint16_t fat_bps_get(dev_handle_t dev_handle)
139
{
140
	block_t *bb;
141
	uint16_t bps;
142
 
143
	bb = block_get(dev_handle, BS_BLOCK);
144
	assert(bb != NULL);
145
	bps = uint16_t_le2host(((fat_bs_t *)bb->data)->bps);
146
	block_put(bb);
147
 
148
	return bps;
149
}
150
 
2845 jermar 151
typedef enum {
152
	FAT_DENTRY_SKIP,
153
	FAT_DENTRY_LAST,
154
	FAT_DENTRY_VALID
155
} fat_dentry_clsf_t;
156
 
157
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
158
{
159
	if (d->attr & FAT_ATTR_VOLLABEL) {
160
		/* volume label entry */
161
		return FAT_DENTRY_SKIP;
162
	}
163
	if (d->name[0] == FAT_DENTRY_ERASED) {
164
		/* not-currently-used entry */
165
		return FAT_DENTRY_SKIP;
166
	}
167
	if (d->name[0] == FAT_DENTRY_UNUSED) {
168
		/* never used entry */
169
		return FAT_DENTRY_LAST;
170
	}
171
	if (d->name[0] == FAT_DENTRY_DOT) {
172
		/*
173
		 * Most likely '.' or '..'.
174
		 * It cannot occur in a regular file name.
175
		 */
176
		return FAT_DENTRY_SKIP;
177
	}
178
	return FAT_DENTRY_VALID;
179
}
180
 
2831 jermar 181
static void fat_sync_node(fat_node_t *node)
182
{
183
	/* TODO */
184
}
185
 
2843 jermar 186
/** Instantiate a FAT in-core node.
187
 *
188
 * FAT stores the info necessary for instantiation of a node in the parent of
189
 * that node.  This design necessitated the addition of the parent node index
190
 * parameter to this otherwise generic libfs API.
191
 */
2831 jermar 192
static void *
193
fat_node_get(dev_handle_t dev_handle, fs_index_t index, fs_index_t pindex)
194
{
195
	link_t *lnk;
196
	fat_node_t *node = NULL;
197
	block_t *b;
2843 jermar 198
	unsigned bps;
199
	unsigned dps;
2831 jermar 200
	fat_dentry_t *d;
2843 jermar 201
	unsigned i, j;
2831 jermar 202
 
203
	unsigned long key[] = {
2843 jermar 204
		[FIN_KEY_DEV_HANDLE] = dev_handle,
205
		[FIN_KEY_INDEX] = index
2831 jermar 206
	};
207
 
2856 jermar 208
	futex_down(&fin_futex);
2831 jermar 209
	lnk = hash_table_find(&fin_hash, key);
210
	if (lnk) {
211
		/*
212
		 * The in-core node was found in the hash table.
213
		 */
214
		node = hash_table_get_instance(lnk, fat_node_t, fin_link);
215
		if (!node->refcnt++)
216
			list_remove(&node->ffn_link);
2856 jermar 217
		futex_up(&fin_futex);
218
 
219
		/* Make sure that the node is fully instantiated. */
220
		futex_down(&node->lock);
221
		futex_up(&node->lock);
222
 
2831 jermar 223
		return (void *) node;	
224
	}
225
 
2843 jermar 226
	bps = fat_bps_get(dev_handle);
227
	dps = bps / sizeof(fat_dentry_t);
228
 
2831 jermar 229
	if (!list_empty(&ffn_head)) {
230
		/*
231
		 * We are going to reuse a node from the free list.
232
		 */
233
		lnk = ffn_head.next; 
234
		list_remove(lnk);
235
		node = list_get_instance(lnk, fat_node_t, ffn_link);
236
		assert(!node->refcnt);
237
		if (node->dirty)
238
			fat_sync_node(node);
2843 jermar 239
		key[FIN_KEY_DEV_HANDLE] = node->dev_handle;
240
		key[FIN_KEY_INDEX] = node->index;
241
		hash_table_remove(&fin_hash, key, sizeof(key)/sizeof(*key));
2831 jermar 242
	} else {
243
		/*
244
		 * We need to allocate a new node.
245
		 */
246
		node = malloc(sizeof(fat_node_t));
247
		if (!node)
248
			return NULL;
249
	}
250
	fat_node_initialize(node);
2843 jermar 251
	node->refcnt++;
252
	node->lnkcnt++;
253
	node->dev_handle = dev_handle;
254
	node->index = index;
255
	node->pindex = pindex;
2856 jermar 256
	key[FIN_KEY_DEV_HANDLE] = node->dev_handle;
257
	key[FIN_KEY_INDEX] = node->index;
258
	hash_table_insert(&fin_hash, key, &node->fin_link);
2831 jermar 259
 
2843 jermar 260
	/*
2856 jermar 261
	 * We have already put the node back to fin_hash.
262
	 * The node is not yet fully instantiated so we lock it prior to
263
	 * unlocking fin_hash.
264
	 */
265
	futex_down(&node->lock);
266
	futex_up(&fin_futex);
267
 
268
	/*
2843 jermar 269
	 * Because of the design of the FAT file system, we have no clue about
270
	 * how big (i.e. how many directory entries it contains) is the parent
271
	 * of the node we are trying to instantiate.  However, we know that it
272
	 * must contain a directory entry for our node of interest.  We simply
273
	 * scan the parent until we find it.
274
	 */
275
	for (i = 0; ; i++) {
276
		b = fat_block_get(node->dev_handle, node->pindex, i);
277
		for (j = 0; j < dps; j++) {
278
			d = ((fat_dentry_t *)b->data) + j;
279
			if (d->firstc == node->index)
280
				goto found;
281
		}
282
		block_put(b);
2831 jermar 283
	}
2843 jermar 284
 
285
found:
286
	if (!(d->attr & (FAT_ATTR_SUBDIR | FAT_ATTR_VOLLABEL)))
287
		node->type = FAT_FILE;
2844 jermar 288
	if ((d->attr & FAT_ATTR_SUBDIR) || !index)
2843 jermar 289
		node->type = FAT_DIRECTORY;
290
	assert((node->type == FAT_FILE) || (node->type == FAT_DIRECTORY));
291
 
292
	node->size = uint32_t_le2host(d->size);
293
	block_put(b);
2831 jermar 294
 
2856 jermar 295
	futex_up(&node->lock);
2843 jermar 296
	return node;
2831 jermar 297
}
298
 
2852 jermar 299
static void fat_node_put(void *node)
300
{
2855 jermar 301
	fat_node_t *nodep = (fat_node_t *)node;
302
 
2856 jermar 303
	futex_down(&fin_futex);
304
	if (!--nodep->refcnt)
2855 jermar 305
		list_append(&nodep->ffn_link, &ffn_head);
2856 jermar 306
	futex_up(&fin_futex);
2852 jermar 307
}
308
 
2793 jermar 309
static void *fat_match(void *prnt, const char *component)
310
{
311
	fat_node_t *parentp = (fat_node_t *)prnt;
312
	char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
2822 jermar 313
	unsigned i, j;
2828 jermar 314
	unsigned bps;		/* bytes per sector */
2822 jermar 315
	unsigned dps;		/* dentries per sector */
316
	unsigned blocks;
2793 jermar 317
	fat_dentry_t *d;
2822 jermar 318
	block_t *b;
2793 jermar 319
 
2843 jermar 320
	bps = fat_bps_get(parentp->dev_handle);
2828 jermar 321
	dps = bps / sizeof(fat_dentry_t);
322
	blocks = parentp->size / bps + (parentp->size % bps != 0);
2822 jermar 323
	for (i = 0; i < blocks; i++) {
324
		unsigned dentries;
2793 jermar 325
 
2831 jermar 326
		b = fat_block_get(parentp->dev_handle, parentp->index, i);
2822 jermar 327
		dentries = (i == blocks - 1) ?
328
		    parentp->size % sizeof(fat_dentry_t) :
329
		    dps;
330
		for (j = 0; j < dentries; j++) { 
331
			d = ((fat_dentry_t *)b->data) + j;
2845 jermar 332
			switch (fat_classify_dentry(d)) {
333
			case FAT_DENTRY_SKIP:
2822 jermar 334
				continue;
2845 jermar 335
			case FAT_DENTRY_LAST:
2822 jermar 336
				block_put(b);
337
				return NULL;
2845 jermar 338
			default:
339
			case FAT_DENTRY_VALID:
340
				dentry_name_canonify(d, name);
341
				break;
2822 jermar 342
			}
343
			if (strcmp(name, component) == 0) {
344
				/* hit */
345
				void *node = fat_node_get(parentp->dev_handle,
2831 jermar 346
				    (fs_index_t)uint16_t_le2host(d->firstc),
347
				    parentp->index);
2822 jermar 348
				block_put(b);
349
				return node;
350
			}
2793 jermar 351
		}
2822 jermar 352
		block_put(b);
2639 jermar 353
	}
2793 jermar 354
 
355
	return NULL;
2638 jermar 356
}
357
 
2831 jermar 358
static fs_index_t fat_index_get(void *node)
359
{
360
	fat_node_t *fnodep = (fat_node_t *)node;
361
	if (!fnodep)
362
		return 0;
363
	return fnodep->index;
364
}
365
 
366
static size_t fat_size_get(void *node)
367
{
368
	return ((fat_node_t *)node)->size;
369
}
370
 
371
static unsigned fat_lnkcnt_get(void *node)
372
{
373
	return ((fat_node_t *)node)->lnkcnt;
374
}
375
 
2845 jermar 376
static bool fat_has_children(void *node)
377
{
378
	fat_node_t *nodep = (fat_node_t *)node;
379
	unsigned bps;
380
	unsigned dps;
381
	unsigned blocks;
382
	block_t *b;
383
	unsigned i, j;
384
 
385
	if (nodep->type != FAT_DIRECTORY)
386
		return false;
387
 
388
	bps = fat_bps_get(nodep->dev_handle);
389
	dps = bps / sizeof(fat_dentry_t);
390
 
391
	blocks = nodep->size / bps + (nodep->size % bps != 0);
392
 
393
	for (i = 0; i < blocks; i++) {
394
		unsigned dentries;
395
		fat_dentry_t *d;
396
 
397
		b = fat_block_get(nodep->dev_handle, nodep->index, i);
398
		dentries = (i == blocks - 1) ?
399
		    nodep->size % sizeof(fat_dentry_t) :
400
		    dps;
401
		for (j = 0; j < dentries; j++) {
402
			d = ((fat_dentry_t *)b->data) + j;
403
			switch (fat_classify_dentry(d)) {
404
			case FAT_DENTRY_SKIP:
405
				continue;
406
			case FAT_DENTRY_LAST:
407
				block_put(b);
408
				return false;
409
			default:
410
			case FAT_DENTRY_VALID:
411
				block_put(b);
412
				return true;
413
			}
414
			block_put(b);
415
			return true;
416
		}
417
		block_put(b);
418
	}
419
 
420
	return false;
421
}
422
 
2844 jermar 423
static void *fat_root_get(dev_handle_t dev_handle)
424
{
425
	return fat_node_get(dev_handle, 0, 0);	
426
}
427
 
428
static char fat_plb_get_char(unsigned pos)
429
{
430
	return fat_reg.plb_ro[pos % PLB_SIZE];
431
}
432
 
2831 jermar 433
static bool fat_is_directory(void *node)
434
{
435
	return ((fat_node_t *)node)->type == FAT_DIRECTORY;
436
}
437
 
438
static bool fat_is_file(void *node)
439
{
440
	return ((fat_node_t *)node)->type == FAT_FILE;
441
}
442
 
2793 jermar 443
/** libfs operations */
444
libfs_ops_t fat_libfs_ops = {
445
	.match = fat_match,
446
	.node_get = fat_node_get,
2852 jermar 447
	.node_put = fat_node_put,
2793 jermar 448
	.create = NULL,
449
	.destroy = NULL,
450
	.link = NULL,
451
	.unlink = NULL,
2831 jermar 452
	.index_get = fat_index_get,
453
	.size_get = fat_size_get,
454
	.lnkcnt_get = fat_lnkcnt_get,
2845 jermar 455
	.has_children = fat_has_children,
2844 jermar 456
	.root_get = fat_root_get,
457
	.plb_get_char =	fat_plb_get_char,
2831 jermar 458
	.is_directory = fat_is_directory,
459
	.is_file = fat_is_file
2793 jermar 460
};
461
 
2627 jermar 462
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
463
{
2793 jermar 464
	libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
2627 jermar 465
}
466
 
467
/**
468
 * @}
469
 */