Subversion Repositories HelenOS

Rev

Rev 3521 | Rev 3530 | 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"
3505 jermar 39
#include "fat_dentry.h"
40
#include "fat_fat.h"
2638 jermar 41
#include "../../vfs/vfs.h"
2793 jermar 42
#include <libfs.h>
3521 jermar 43
#include <libblock.h>
2627 jermar 44
#include <ipc/ipc.h>
3257 jermar 45
#include <ipc/services.h>
46
#include <ipc/devmap.h>
2627 jermar 47
#include <async.h>
48
#include <errno.h>
2793 jermar 49
#include <string.h>
2798 jermar 50
#include <byteorder.h>
2831 jermar 51
#include <libadt/hash_table.h>
52
#include <libadt/list.h>
53
#include <assert.h>
2856 jermar 54
#include <futex.h>
3257 jermar 55
#include <sys/mman.h>
3499 jermar 56
#include <align.h>
2627 jermar 57
 
2951 jermar 58
/** Futex protecting the list of cached free FAT nodes. */
59
static futex_t ffn_futex = FUTEX_INITIALIZER;
2843 jermar 60
 
2951 jermar 61
/** List of cached free FAT nodes. */
62
static LIST_INITIALIZE(ffn_head);
63
 
2831 jermar 64
static void fat_node_initialize(fat_node_t *node)
2793 jermar 65
{
2951 jermar 66
	futex_initialize(&node->lock, 1);
2864 jermar 67
	node->idx = NULL;
2831 jermar 68
	node->type = 0;
69
	link_initialize(&node->ffn_link);
70
	node->size = 0;
71
	node->lnkcnt = 0;
72
	node->refcnt = 0;
73
	node->dirty = false;
2793 jermar 74
}
75
 
2893 jermar 76
static void fat_node_sync(fat_node_t *node)
2831 jermar 77
{
3519 jermar 78
	block_t *bb, *b;
79
	fat_dentry_t *d;
80
	uint16_t bps;
81
	unsigned dps;
82
 
83
	assert(node->dirty);
84
 
85
	bb = block_get(node->idx->dev_handle, BS_BLOCK, BS_SIZE);
86
	bps = uint16_t_le2host(FAT_BS(bb)->bps);
87
	dps = bps / sizeof(fat_dentry_t);
88
 
89
	/* Read the block that contains the dentry of interest. */
90
	b = _fat_block_get(bb->data, node->idx->dev_handle, node->idx->pfc,
91
	    (node->idx->pdi * sizeof(fat_dentry_t)) / bps);
92
 
93
	d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
94
 
95
	d->firstc = host2uint16_t_le(node->firstc);
96
	if (node->type == FAT_FILE)
97
		d->size = host2uint32_t_le(node->size);
98
	/* TODO: update other fields? (e.g time fields, attr field) */
99
 
100
	b->dirty = true;		/* need to sync block */
101
	block_put(b);
102
	block_put(bb);
2831 jermar 103
}
104
 
2951 jermar 105
/** Internal version of fat_node_get().
106
 *
107
 * @param idxp		Locked index structure.
108
 */
109
static void *fat_node_get_core(fat_idx_t *idxp)
2831 jermar 110
{
3516 jermar 111
	block_t *bb, *b;
2891 jermar 112
	fat_dentry_t *d;
3312 jermar 113
	fat_node_t *nodep = NULL;
2891 jermar 114
	unsigned bps;
115
	unsigned dps;
116
 
2951 jermar 117
	if (idxp->nodep) {
2891 jermar 118
		/*
119
		 * We are lucky.
120
		 * The node is already instantiated in memory.
121
		 */
2951 jermar 122
		futex_down(&idxp->nodep->lock);
123
		if (!idxp->nodep->refcnt++)
3312 jermar 124
			list_remove(&idxp->nodep->ffn_link);
2951 jermar 125
		futex_up(&idxp->nodep->lock);
126
		return idxp->nodep;
2891 jermar 127
	}
128
 
129
	/*
130
	 * We must instantiate the node from the file system.
131
	 */
132
 
2951 jermar 133
	assert(idxp->pfc);
2891 jermar 134
 
2951 jermar 135
	futex_down(&ffn_futex);
2893 jermar 136
	if (!list_empty(&ffn_head)) {
2951 jermar 137
		/* Try to use a cached free node structure. */
138
		fat_idx_t *idxp_tmp;
2893 jermar 139
		nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
2951 jermar 140
		if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
141
			goto skip_cache;
142
		idxp_tmp = nodep->idx;
143
		if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
144
			futex_up(&nodep->lock);
145
			goto skip_cache;
146
		}
147
		list_remove(&nodep->ffn_link);
148
		futex_up(&ffn_futex);
2893 jermar 149
		if (nodep->dirty)
150
			fat_node_sync(nodep);
2951 jermar 151
		idxp_tmp->nodep = NULL;
152
		futex_up(&nodep->lock);
153
		futex_up(&idxp_tmp->lock);
2893 jermar 154
	} else {
2951 jermar 155
skip_cache:
2893 jermar 156
		/* Try to allocate a new node structure. */
2951 jermar 157
		futex_up(&ffn_futex);
2893 jermar 158
		nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
159
		if (!nodep)
160
			return NULL;
161
	}
2891 jermar 162
	fat_node_initialize(nodep);
163
 
3516 jermar 164
	bb = block_get(idxp->dev_handle, BS_BLOCK, BS_SIZE);
165
	bps = uint16_t_le2host(FAT_BS(bb)->bps);
2891 jermar 166
	dps = bps / sizeof(fat_dentry_t);
167
 
2893 jermar 168
	/* Read the block that contains the dentry of interest. */
3516 jermar 169
	b = _fat_block_get(bb->data, idxp->dev_handle, idxp->pfc,
2951 jermar 170
	    (idxp->pdi * sizeof(fat_dentry_t)) / bps);
2891 jermar 171
	assert(b);
172
 
2951 jermar 173
	d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
2893 jermar 174
	if (d->attr & FAT_ATTR_SUBDIR) {
175
		/* 
176
		 * The only directory which does not have this bit set is the
177
		 * root directory itself. The root directory node is handled
178
		 * and initialized elsewhere.
179
		 */
180
		nodep->type = FAT_DIRECTORY;
3282 jermar 181
		/*
3325 jermar 182
		 * Unfortunately, the 'size' field of the FAT dentry is not
183
		 * defined for the directory entry type. We must determine the
184
		 * size of the directory by walking the FAT.
3282 jermar 185
		 */
3516 jermar 186
		nodep->size = bps * _fat_blcks_get(bb->data, idxp->dev_handle,
3513 jermar 187
		    uint16_t_le2host(d->firstc), NULL);
2893 jermar 188
	} else {
189
		nodep->type = FAT_FILE;
3282 jermar 190
		nodep->size = uint32_t_le2host(d->size);
2893 jermar 191
	}
192
	nodep->firstc = uint16_t_le2host(d->firstc);
193
	nodep->lnkcnt = 1;
194
	nodep->refcnt = 1;
195
 
196
	block_put(b);
3516 jermar 197
	block_put(bb);
2893 jermar 198
 
199
	/* Link the idx structure with the node structure. */
2951 jermar 200
	nodep->idx = idxp;
201
	idxp->nodep = nodep;
2893 jermar 202
 
203
	return nodep;
2831 jermar 204
}
205
 
2951 jermar 206
/** Instantiate a FAT in-core node. */
207
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
208
{
209
	void *node;
210
	fat_idx_t *idxp;
211
 
212
	idxp = fat_idx_get_by_index(dev_handle, index);
213
	if (!idxp)
214
		return NULL;
215
	/* idxp->lock held */
216
	node = fat_node_get_core(idxp);
217
	futex_up(&idxp->lock);
218
	return node;
219
}
220
 
2852 jermar 221
static void fat_node_put(void *node)
222
{
2910 jermar 223
	fat_node_t *nodep = (fat_node_t *)node;
224
 
2951 jermar 225
	futex_down(&nodep->lock);
2910 jermar 226
	if (!--nodep->refcnt) {
2951 jermar 227
		futex_down(&ffn_futex);
2910 jermar 228
		list_append(&nodep->ffn_link, &ffn_head);
2951 jermar 229
		futex_up(&ffn_futex);
2910 jermar 230
	}
2951 jermar 231
	futex_up(&nodep->lock);
2852 jermar 232
}
233
 
2857 jermar 234
static void *fat_create(int flags)
235
{
236
	return NULL;	/* not supported at the moment */
237
}
238
 
2858 jermar 239
static int fat_destroy(void *node)
2857 jermar 240
{
2858 jermar 241
	return ENOTSUP;	/* not supported at the moment */
2857 jermar 242
}
243
 
244
static bool fat_link(void *prnt, void *chld, const char *name)
245
{
246
	return false;	/* not supported at the moment */
247
}
248
 
249
static int fat_unlink(void *prnt, void *chld)
250
{
251
	return ENOTSUP;	/* not supported at the moment */
252
}
253
 
2793 jermar 254
static void *fat_match(void *prnt, const char *component)
255
{
256
	fat_node_t *parentp = (fat_node_t *)prnt;
257
	char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
2822 jermar 258
	unsigned i, j;
2828 jermar 259
	unsigned bps;		/* bytes per sector */
2822 jermar 260
	unsigned dps;		/* dentries per sector */
261
	unsigned blocks;
2793 jermar 262
	fat_dentry_t *d;
3516 jermar 263
	block_t *bb, *b;
2793 jermar 264
 
2953 jermar 265
	futex_down(&parentp->idx->lock);
3516 jermar 266
	bb = block_get(parentp->idx->dev_handle, BS_BLOCK, BS_SIZE);
267
	bps = uint16_t_le2host(FAT_BS(bb)->bps);
2828 jermar 268
	dps = bps / sizeof(fat_dentry_t);
3526 jermar 269
	blocks = parentp->size / bps;
2822 jermar 270
	for (i = 0; i < blocks; i++) {
3516 jermar 271
		b = fat_block_get(bb->data, parentp, i);
3526 jermar 272
		for (j = 0; j < dps; j++) { 
2822 jermar 273
			d = ((fat_dentry_t *)b->data) + j;
2845 jermar 274
			switch (fat_classify_dentry(d)) {
275
			case FAT_DENTRY_SKIP:
2822 jermar 276
				continue;
2845 jermar 277
			case FAT_DENTRY_LAST:
2822 jermar 278
				block_put(b);
3516 jermar 279
				block_put(bb);
2953 jermar 280
				futex_up(&parentp->idx->lock);
2822 jermar 281
				return NULL;
2845 jermar 282
			default:
283
			case FAT_DENTRY_VALID:
284
				dentry_name_canonify(d, name);
285
				break;
2822 jermar 286
			}
3272 jermar 287
			if (stricmp(name, component) == 0) {
2822 jermar 288
				/* hit */
2951 jermar 289
				void *node;
2953 jermar 290
				/*
291
				 * Assume tree hierarchy for locking.  We
292
				 * already have the parent and now we are going
293
				 * to lock the child.  Never lock in the oposite
294
				 * order.
295
				 */
2890 jermar 296
				fat_idx_t *idx = fat_idx_get_by_pos(
2881 jermar 297
				    parentp->idx->dev_handle, parentp->firstc,
2864 jermar 298
				    i * dps + j);
2953 jermar 299
				futex_up(&parentp->idx->lock);
2890 jermar 300
				if (!idx) {
301
					/*
302
					 * Can happen if memory is low or if we
303
					 * run out of 32-bit indices.
304
					 */
305
					block_put(b);
3516 jermar 306
					block_put(bb);
2890 jermar 307
					return NULL;
308
				}
2951 jermar 309
				node = fat_node_get_core(idx);
310
				futex_up(&idx->lock);
2822 jermar 311
				block_put(b);
3516 jermar 312
				block_put(bb);
2822 jermar 313
				return node;
314
			}
2793 jermar 315
		}
2822 jermar 316
		block_put(b);
2639 jermar 317
	}
3516 jermar 318
	block_put(bb);
319
 
2953 jermar 320
	futex_up(&parentp->idx->lock);
2793 jermar 321
	return NULL;
2638 jermar 322
}
323
 
2831 jermar 324
static fs_index_t fat_index_get(void *node)
325
{
326
	fat_node_t *fnodep = (fat_node_t *)node;
327
	if (!fnodep)
328
		return 0;
2864 jermar 329
	return fnodep->idx->index;
2831 jermar 330
}
331
 
332
static size_t fat_size_get(void *node)
333
{
334
	return ((fat_node_t *)node)->size;
335
}
336
 
337
static unsigned fat_lnkcnt_get(void *node)
338
{
339
	return ((fat_node_t *)node)->lnkcnt;
340
}
341
 
2845 jermar 342
static bool fat_has_children(void *node)
343
{
344
	fat_node_t *nodep = (fat_node_t *)node;
345
	unsigned bps;
346
	unsigned dps;
347
	unsigned blocks;
3516 jermar 348
	block_t *bb, *b;
2845 jermar 349
	unsigned i, j;
350
 
351
	if (nodep->type != FAT_DIRECTORY)
352
		return false;
3526 jermar 353
 
2951 jermar 354
	futex_down(&nodep->idx->lock);
3516 jermar 355
	bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE);
356
	bps = uint16_t_le2host(FAT_BS(bb)->bps);
2845 jermar 357
	dps = bps / sizeof(fat_dentry_t);
358
 
3526 jermar 359
	blocks = nodep->size / bps;
2845 jermar 360
 
361
	for (i = 0; i < blocks; i++) {
362
		fat_dentry_t *d;
363
 
3516 jermar 364
		b = fat_block_get(bb->data, nodep, i);
3526 jermar 365
		for (j = 0; j < dps; j++) {
2845 jermar 366
			d = ((fat_dentry_t *)b->data) + j;
367
			switch (fat_classify_dentry(d)) {
368
			case FAT_DENTRY_SKIP:
369
				continue;
370
			case FAT_DENTRY_LAST:
371
				block_put(b);
3516 jermar 372
				block_put(bb);
2951 jermar 373
				futex_up(&nodep->idx->lock);
2845 jermar 374
				return false;
375
			default:
376
			case FAT_DENTRY_VALID:
377
				block_put(b);
3516 jermar 378
				block_put(bb);
2951 jermar 379
				futex_up(&nodep->idx->lock);
2845 jermar 380
				return true;
381
			}
382
			block_put(b);
3516 jermar 383
			block_put(bb);
2951 jermar 384
			futex_up(&nodep->idx->lock);
2845 jermar 385
			return true;
386
		}
387
		block_put(b);
388
	}
3516 jermar 389
	block_put(bb);
2845 jermar 390
 
2951 jermar 391
	futex_up(&nodep->idx->lock);
2845 jermar 392
	return false;
393
}
394
 
2844 jermar 395
static void *fat_root_get(dev_handle_t dev_handle)
396
{
3119 jermar 397
	return fat_node_get(dev_handle, 0);
2844 jermar 398
}
399
 
400
static char fat_plb_get_char(unsigned pos)
401
{
402
	return fat_reg.plb_ro[pos % PLB_SIZE];
403
}
404
 
2831 jermar 405
static bool fat_is_directory(void *node)
406
{
407
	return ((fat_node_t *)node)->type == FAT_DIRECTORY;
408
}
409
 
410
static bool fat_is_file(void *node)
411
{
412
	return ((fat_node_t *)node)->type == FAT_FILE;
413
}
414
 
2793 jermar 415
/** libfs operations */
416
libfs_ops_t fat_libfs_ops = {
417
	.match = fat_match,
418
	.node_get = fat_node_get,
2852 jermar 419
	.node_put = fat_node_put,
2857 jermar 420
	.create = fat_create,
421
	.destroy = fat_destroy,
422
	.link = fat_link,
423
	.unlink = fat_unlink,
2831 jermar 424
	.index_get = fat_index_get,
425
	.size_get = fat_size_get,
426
	.lnkcnt_get = fat_lnkcnt_get,
2845 jermar 427
	.has_children = fat_has_children,
2844 jermar 428
	.root_get = fat_root_get,
429
	.plb_get_char =	fat_plb_get_char,
2831 jermar 430
	.is_directory = fat_is_directory,
431
	.is_file = fat_is_file
2793 jermar 432
};
433
 
3110 jermar 434
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
435
{
436
	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
3119 jermar 437
	block_t *bb;
3257 jermar 438
	uint16_t bps;
3119 jermar 439
	uint16_t rde;
3110 jermar 440
	int rc;
441
 
3257 jermar 442
	/*
443
	 * For now, we don't bother to remember dev_handle, dev_phone or
444
	 * dev_buffer in some data structure. We use global variables because we
445
	 * know there will be at most one mount on this file system.
446
	 * Of course, this is a huge TODO item.
447
	 */
448
	dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
449
	    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
450
 
451
	if (!dev_buffer) {
452
		ipc_answer_0(rid, ENOMEM);
453
		return;
454
	}
455
 
456
	dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
457
	    DEVMAP_CONNECT_TO_DEVICE, dev_handle);
458
 
459
	if (dev_phone < 0) {
460
		munmap(dev_buffer, BS_SIZE);
461
		ipc_answer_0(rid, dev_phone);
462
		return;
463
	}
464
 
465
	rc = ipc_share_out_start(dev_phone, dev_buffer,
466
	    AS_AREA_READ | AS_AREA_WRITE);
467
	if (rc != EOK) {
468
	    	munmap(dev_buffer, BS_SIZE);
469
		ipc_answer_0(rid, rc);
470
		return;
471
	}
472
 
3119 jermar 473
	/* Read the number of root directory entries. */
3253 jermar 474
	bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
3257 jermar 475
	bps = uint16_t_le2host(FAT_BS(bb)->bps);
3119 jermar 476
	rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
477
	block_put(bb);
478
 
3257 jermar 479
	if (bps != BS_SIZE) {
480
		munmap(dev_buffer, BS_SIZE);
481
		ipc_answer_0(rid, ENOTSUP);
482
		return;
483
	}
484
 
3110 jermar 485
	rc = fat_idx_init_by_dev_handle(dev_handle);
486
	if (rc != EOK) {
3257 jermar 487
	    	munmap(dev_buffer, BS_SIZE);
3110 jermar 488
		ipc_answer_0(rid, rc);
489
		return;
490
	}
491
 
3119 jermar 492
	/* Initialize the root node. */
493
	fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
494
	if (!rootp) {
3257 jermar 495
	    	munmap(dev_buffer, BS_SIZE);
3119 jermar 496
		fat_idx_fini_by_dev_handle(dev_handle);
497
		ipc_answer_0(rid, ENOMEM);
498
		return;
499
	}
500
	fat_node_initialize(rootp);
501
 
502
	fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
503
	if (!ridxp) {
3257 jermar 504
	    	munmap(dev_buffer, BS_SIZE);
3119 jermar 505
		free(rootp);
506
		fat_idx_fini_by_dev_handle(dev_handle);
507
		ipc_answer_0(rid, ENOMEM);
508
		return;
509
	}
510
	assert(ridxp->index == 0);
511
	/* ridxp->lock held */
512
 
513
	rootp->type = FAT_DIRECTORY;
514
	rootp->firstc = FAT_CLST_ROOT;
515
	rootp->refcnt = 1;
3352 jermar 516
	rootp->lnkcnt = 0;	/* FS root is not linked */
3119 jermar 517
	rootp->size = rde * sizeof(fat_dentry_t);
518
	rootp->idx = ridxp;
519
	ridxp->nodep = rootp;
520
 
521
	futex_up(&ridxp->lock);
522
 
3352 jermar 523
	ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
3110 jermar 524
}
525
 
526
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
527
{
528
	ipc_answer_0(rid, ENOTSUP);
529
}
530
 
2627 jermar 531
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
532
{
2793 jermar 533
	libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
2627 jermar 534
}
535
 
3307 jermar 536
void fat_read(ipc_callid_t rid, ipc_call_t *request)
537
{
538
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
539
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
540
	off_t pos = (off_t)IPC_GET_ARG3(*request);
541
	fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3516 jermar 542
	uint16_t bps;
3308 jermar 543
	size_t bytes;
3516 jermar 544
	block_t *bb, *b;
3308 jermar 545
 
3307 jermar 546
	if (!nodep) {
547
		ipc_answer_0(rid, ENOENT);
548
		return;
549
	}
550
 
551
	ipc_callid_t callid;
552
	size_t len;
3314 jermar 553
	if (!ipc_data_read_receive(&callid, &len)) {
3307 jermar 554
		fat_node_put(nodep);
555
		ipc_answer_0(callid, EINVAL);
556
		ipc_answer_0(rid, EINVAL);
557
		return;
558
	}
559
 
3516 jermar 560
	bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
561
	bps = uint16_t_le2host(FAT_BS(bb)->bps);
562
 
3307 jermar 563
	if (nodep->type == FAT_FILE) {
3335 jermar 564
		/*
565
		 * Our strategy for regular file reads is to read one block at
566
		 * most and make use of the possibility to return less data than
567
		 * requested. This keeps the code very simple.
568
		 */
3308 jermar 569
		bytes = min(len, bps - pos % bps);
3516 jermar 570
		b = fat_block_get(bb->data, nodep, pos / bps);
3308 jermar 571
		(void) ipc_data_read_finalize(callid, b->data + pos % bps,
572
		    bytes);
573
		block_put(b);
3307 jermar 574
	} else {
3335 jermar 575
		unsigned bnum;
576
		off_t spos = pos;
577
		char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
578
		fat_dentry_t *d;
579
 
3307 jermar 580
		assert(nodep->type == FAT_DIRECTORY);
3335 jermar 581
		assert(nodep->size % bps == 0);
582
		assert(bps % sizeof(fat_dentry_t) == 0);
583
 
584
		/*
585
		 * Our strategy for readdir() is to use the position pointer as
586
		 * an index into the array of all dentries. On entry, it points
587
		 * to the first unread dentry. If we skip any dentries, we bump
588
		 * the position pointer accordingly.
589
		 */
590
		bnum = (pos * sizeof(fat_dentry_t)) / bps;
591
		while (bnum < nodep->size / bps) {
592
			off_t o;
593
 
3516 jermar 594
			b = fat_block_get(bb->data, nodep, bnum);
3335 jermar 595
			for (o = pos % (bps / sizeof(fat_dentry_t));
596
			    o < bps / sizeof(fat_dentry_t);
597
			    o++, pos++) {
598
				d = ((fat_dentry_t *)b->data) + o;
599
				switch (fat_classify_dentry(d)) {
600
				case FAT_DENTRY_SKIP:
601
					continue;
602
				case FAT_DENTRY_LAST:
603
					block_put(b);
604
					goto miss;
605
				default:
606
				case FAT_DENTRY_VALID:
607
					dentry_name_canonify(d, name);
608
					block_put(b);
609
					goto hit;
610
				}
611
			}
612
			block_put(b);
613
			bnum++;
614
		}
615
miss:
3307 jermar 616
		fat_node_put(nodep);
3516 jermar 617
		block_put(bb);
3335 jermar 618
		ipc_answer_0(callid, ENOENT);
619
		ipc_answer_1(rid, ENOENT, 0);
3307 jermar 620
		return;
3335 jermar 621
hit:
622
		(void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
623
		bytes = (pos - spos) + 1;
3307 jermar 624
	}
625
 
626
	fat_node_put(nodep);
3516 jermar 627
	block_put(bb);
3308 jermar 628
	ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
3307 jermar 629
}
630
 
3497 jermar 631
void fat_write(ipc_callid_t rid, ipc_call_t *request)
632
{
3499 jermar 633
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
634
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
635
	off_t pos = (off_t)IPC_GET_ARG3(*request);
636
	fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
637
	size_t bytes;
638
	block_t *b, *bb;
639
	uint16_t bps;
640
	unsigned spc;
3501 jermar 641
	off_t boundary;
3499 jermar 642
 
643
	if (!nodep) {
644
		ipc_answer_0(rid, ENOENT);
645
		return;
646
	}
647
 
648
	/* XXX remove me when you are ready */
649
	{
650
		ipc_answer_0(rid, ENOTSUP);
651
		fat_node_put(nodep);
652
		return;
653
	}
654
 
655
	ipc_callid_t callid;
656
	size_t len;
657
	if (!ipc_data_write_receive(&callid, &len)) {
658
		fat_node_put(nodep);
659
		ipc_answer_0(callid, EINVAL);
660
		ipc_answer_0(rid, EINVAL);
661
		return;
662
	}
663
 
664
	/*
665
	 * In all scenarios, we will attempt to write out only one block worth
666
	 * of data at maximum. There might be some more efficient approaches,
667
	 * but this one greatly simplifies fat_write(). Note that we can afford
668
	 * to do this because the client must be ready to handle the return
669
	 * value signalizing a smaller number of bytes written. 
670
	 */ 
671
	bytes = min(len, bps - pos % bps);
672
 
673
	bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
674
	bps = uint16_t_le2host(FAT_BS(bb)->bps);
675
	spc = FAT_BS(bb)->spc;
676
 
3501 jermar 677
	boundary = ROUND_UP(nodep->size, bps * spc);
678
	if (pos < boundary) {
3499 jermar 679
		/*
680
		 * This is the easier case - we are either overwriting already
681
		 * existing contents or writing behind the EOF, but still within
682
		 * the limits of the last cluster. The node size may grow to the
683
		 * next block size boundary.
684
		 */
3516 jermar 685
		fat_fill_gap(bb->data, nodep, FAT_CLST_RES0, pos);
686
		b = fat_block_get(bb->data, nodep, pos / bps);
3499 jermar 687
		(void) ipc_data_write_finalize(callid, b->data + pos % bps,
688
		    bytes);
689
		b->dirty = true;		/* need to sync block */
3500 jermar 690
		block_put(b);
3499 jermar 691
		if (pos + bytes > nodep->size) {
692
			nodep->size = pos + bytes;
693
			nodep->dirty = true;	/* need to sync node */
694
		}
695
		fat_node_put(nodep);
3516 jermar 696
		block_put(bb);
3499 jermar 697
		ipc_answer_1(rid, EOK, bytes);	
698
		return;
699
	} else {
700
		/*
701
		 * This is the more difficult case. We must allocate new
702
		 * clusters for the node and zero them out.
703
		 */
3500 jermar 704
		int status;
3499 jermar 705
		unsigned nclsts;
3500 jermar 706
		fat_cluster_t mcl, lcl;
707
 
3501 jermar 708
		nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
3500 jermar 709
		    bps * spc;
710
		/* create an independent chain of nclsts clusters in all FATs */
3516 jermar 711
		status = fat_alloc_clusters(bb->data, dev_handle, nclsts, &mcl,
712
		    &lcl);
3500 jermar 713
		if (status != EOK) {
714
			/* could not allocate a chain of nclsts clusters */
715
			fat_node_put(nodep);
3516 jermar 716
			block_put(bb);
3500 jermar 717
			ipc_answer_0(callid, status);
718
			ipc_answer_0(rid, status);
719
			return;
720
		}
721
		/* zero fill any gaps */
3516 jermar 722
		fat_fill_gap(bb->data, nodep, mcl, pos);
723
		b = _fat_block_get(bb->data, dev_handle, lcl,
724
		    (pos / bps) % spc);
3500 jermar 725
		(void) ipc_data_write_finalize(callid, b->data + pos % bps,
726
		    bytes);
3501 jermar 727
		b->dirty = true;		/* need to sync block */
3500 jermar 728
		block_put(b);
729
		/*
730
		 * Append the cluster chain starting in mcl to the end of the
731
		 * node's cluster chain.
732
		 */
3516 jermar 733
		fat_append_clusters(bb->data, nodep, mcl);
3500 jermar 734
		nodep->size = pos + bytes;
3501 jermar 735
		nodep->dirty = true;		/* need to sync node */
3500 jermar 736
		fat_node_put(nodep);
3516 jermar 737
		block_put(bb);
3500 jermar 738
		ipc_answer_1(rid, EOK, bytes);
739
		return;
3499 jermar 740
	}
3497 jermar 741
}
742
 
2627 jermar 743
/**
744
 * @}
745
 */