Subversion Repositories HelenOS

Rev

Rev 4357 | Rev 4359 | 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
 
4357 jermar 58
#define FAT_NODE(node)	((node) ? (fat_node_t *) (node)->data : NULL)
59
#define FS_NODE(node)	((node) ? (node)->bp : NULL)
60
 
2951 jermar 61
/** Futex protecting the list of cached free FAT nodes. */
62
static futex_t ffn_futex = FUTEX_INITIALIZER;
2843 jermar 63
 
2951 jermar 64
/** List of cached free FAT nodes. */
65
static LIST_INITIALIZE(ffn_head);
66
 
2831 jermar 67
static void fat_node_initialize(fat_node_t *node)
2793 jermar 68
{
2951 jermar 69
	futex_initialize(&node->lock, 1);
4357 jermar 70
	node->bp = NULL;
2864 jermar 71
	node->idx = NULL;
2831 jermar 72
	node->type = 0;
73
	link_initialize(&node->ffn_link);
74
	node->size = 0;
75
	node->lnkcnt = 0;
76
	node->refcnt = 0;
77
	node->dirty = false;
2793 jermar 78
}
79
 
2893 jermar 80
static void fat_node_sync(fat_node_t *node)
2831 jermar 81
{
3530 jermar 82
	block_t *b;
83
	fat_bs_t *bs;
3519 jermar 84
	fat_dentry_t *d;
85
	uint16_t bps;
86
	unsigned dps;
87
 
88
	assert(node->dirty);
89
 
3530 jermar 90
	bs = block_bb_get(node->idx->dev_handle);
91
	bps = uint16_t_le2host(bs->bps);
3519 jermar 92
	dps = bps / sizeof(fat_dentry_t);
93
 
94
	/* Read the block that contains the dentry of interest. */
3530 jermar 95
	b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc,
3595 jermar 96
	    (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
3519 jermar 97
 
98
	d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
99
 
100
	d->firstc = host2uint16_t_le(node->firstc);
3629 jermar 101
	if (node->type == FAT_FILE) {
3519 jermar 102
		d->size = host2uint32_t_le(node->size);
3629 jermar 103
	} else if (node->type == FAT_DIRECTORY) {
104
		d->attr = FAT_ATTR_SUBDIR;
105
	}
3519 jermar 106
 
3629 jermar 107
	/* TODO: update other fields? (e.g time fields) */
108
 
3519 jermar 109
	b->dirty = true;		/* need to sync block */
110
	block_put(b);
2831 jermar 111
}
112
 
3574 jermar 113
static fat_node_t *fat_node_get_new(void)
114
{
4357 jermar 115
	fs_node_t *fn;
3574 jermar 116
	fat_node_t *nodep;
117
 
118
	futex_down(&ffn_futex);
119
	if (!list_empty(&ffn_head)) {
120
		/* Try to use a cached free node structure. */
121
		fat_idx_t *idxp_tmp;
122
		nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
123
		if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
124
			goto skip_cache;
125
		idxp_tmp = nodep->idx;
126
		if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
127
			futex_up(&nodep->lock);
128
			goto skip_cache;
129
		}
130
		list_remove(&nodep->ffn_link);
131
		futex_up(&ffn_futex);
132
		if (nodep->dirty)
133
			fat_node_sync(nodep);
134
		idxp_tmp->nodep = NULL;
135
		futex_up(&nodep->lock);
136
		futex_up(&idxp_tmp->lock);
4357 jermar 137
		fn = FS_NODE(nodep);
3574 jermar 138
	} else {
139
skip_cache:
140
		/* Try to allocate a new node structure. */
141
		futex_up(&ffn_futex);
4357 jermar 142
		fn = (fs_node_t *)malloc(sizeof(fs_node_t));
143
		if (!fn)
144
			return NULL;
3574 jermar 145
		nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
4357 jermar 146
		if (!nodep) {
147
			free(fn);
3574 jermar 148
			return NULL;
4357 jermar 149
		}
3574 jermar 150
	}
151
	fat_node_initialize(nodep);
4357 jermar 152
	fn->data = nodep;
153
	nodep->bp = fn;
3574 jermar 154
 
155
	return nodep;
156
}
157
 
2951 jermar 158
/** Internal version of fat_node_get().
159
 *
160
 * @param idxp		Locked index structure.
161
 */
4357 jermar 162
static fat_node_t *fat_node_get_core(fat_idx_t *idxp)
2831 jermar 163
{
3530 jermar 164
	block_t *b;
165
	fat_bs_t *bs;
2891 jermar 166
	fat_dentry_t *d;
3312 jermar 167
	fat_node_t *nodep = NULL;
2891 jermar 168
	unsigned bps;
3550 jermar 169
	unsigned spc;
2891 jermar 170
	unsigned dps;
171
 
2951 jermar 172
	if (idxp->nodep) {
2891 jermar 173
		/*
174
		 * We are lucky.
175
		 * The node is already instantiated in memory.
176
		 */
2951 jermar 177
		futex_down(&idxp->nodep->lock);
178
		if (!idxp->nodep->refcnt++)
3312 jermar 179
			list_remove(&idxp->nodep->ffn_link);
2951 jermar 180
		futex_up(&idxp->nodep->lock);
181
		return idxp->nodep;
2891 jermar 182
	}
183
 
184
	/*
185
	 * We must instantiate the node from the file system.
186
	 */
187
 
2951 jermar 188
	assert(idxp->pfc);
2891 jermar 189
 
3574 jermar 190
	nodep = fat_node_get_new();
191
	if (!nodep)
192
		return NULL;
2891 jermar 193
 
3530 jermar 194
	bs = block_bb_get(idxp->dev_handle);
195
	bps = uint16_t_le2host(bs->bps);
3550 jermar 196
	spc = bs->spc;
2891 jermar 197
	dps = bps / sizeof(fat_dentry_t);
198
 
2893 jermar 199
	/* Read the block that contains the dentry of interest. */
3530 jermar 200
	b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc,
3595 jermar 201
	    (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
2891 jermar 202
	assert(b);
203
 
2951 jermar 204
	d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
2893 jermar 205
	if (d->attr & FAT_ATTR_SUBDIR) {
206
		/* 
207
		 * The only directory which does not have this bit set is the
208
		 * root directory itself. The root directory node is handled
209
		 * and initialized elsewhere.
210
		 */
211
		nodep->type = FAT_DIRECTORY;
3282 jermar 212
		/*
3325 jermar 213
		 * Unfortunately, the 'size' field of the FAT dentry is not
214
		 * defined for the directory entry type. We must determine the
215
		 * size of the directory by walking the FAT.
3282 jermar 216
		 */
3550 jermar 217
		nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle,
218
		    uint16_t_le2host(d->firstc));
2893 jermar 219
	} else {
220
		nodep->type = FAT_FILE;
3282 jermar 221
		nodep->size = uint32_t_le2host(d->size);
2893 jermar 222
	}
223
	nodep->firstc = uint16_t_le2host(d->firstc);
224
	nodep->lnkcnt = 1;
225
	nodep->refcnt = 1;
226
 
227
	block_put(b);
228
 
229
	/* Link the idx structure with the node structure. */
2951 jermar 230
	nodep->idx = idxp;
231
	idxp->nodep = nodep;
2893 jermar 232
 
233
	return nodep;
2831 jermar 234
}
235
 
3621 jermar 236
/*
237
 * Forward declarations of FAT libfs operations.
238
 */
4357 jermar 239
static fs_node_t *fat_node_get(dev_handle_t, fs_index_t);
240
static void fat_node_put(fs_node_t *);
241
static fs_node_t *fat_create_node(dev_handle_t, int);
242
static int fat_destroy_node(fs_node_t *);
243
static int fat_link(fs_node_t *, fs_node_t *, const char *);
244
static int fat_unlink(fs_node_t *, fs_node_t *);
245
static fs_node_t *fat_match(fs_node_t *, const char *);
246
static fs_index_t fat_index_get(fs_node_t *);
247
static size_t fat_size_get(fs_node_t *);
248
static unsigned fat_lnkcnt_get(fs_node_t *);
249
static bool fat_has_children(fs_node_t *);
250
static fs_node_t *fat_root_get(dev_handle_t);
3621 jermar 251
static char fat_plb_get_char(unsigned);
4357 jermar 252
static bool fat_is_directory(fs_node_t *);
253
static bool fat_is_file(fs_node_t *node);
3621 jermar 254
 
255
/*
256
 * FAT libfs operations.
257
 */
258
 
2951 jermar 259
/** Instantiate a FAT in-core node. */
4357 jermar 260
fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
2951 jermar 261
{
4357 jermar 262
	fat_node_t *nodep;
2951 jermar 263
	fat_idx_t *idxp;
264
 
265
	idxp = fat_idx_get_by_index(dev_handle, index);
266
	if (!idxp)
267
		return NULL;
268
	/* idxp->lock held */
4357 jermar 269
	nodep = fat_node_get_core(idxp);
2951 jermar 270
	futex_up(&idxp->lock);
4357 jermar 271
	return FS_NODE(nodep);
2951 jermar 272
}
273
 
4357 jermar 274
void fat_node_put(fs_node_t *fn)
2852 jermar 275
{
4357 jermar 276
	fat_node_t *nodep = FAT_NODE(fn);
3609 jermar 277
	bool destroy = false;
2910 jermar 278
 
2951 jermar 279
	futex_down(&nodep->lock);
2910 jermar 280
	if (!--nodep->refcnt) {
3609 jermar 281
		if (nodep->idx) {
282
			futex_down(&ffn_futex);
283
			list_append(&nodep->ffn_link, &ffn_head);
284
			futex_up(&ffn_futex);
285
		} else {
286
			/*
287
			 * The node does not have any index structure associated
288
			 * with itself. This can only mean that we are releasing
289
			 * the node after a failed attempt to allocate the index
290
			 * structure for it.
291
			 */
292
			destroy = true;
293
		}
2910 jermar 294
	}
2951 jermar 295
	futex_up(&nodep->lock);
4357 jermar 296
	if (destroy) {
297
		free(nodep->bp);
298
		free(nodep);
299
	}
2852 jermar 300
}
301
 
4357 jermar 302
fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags)
2857 jermar 303
{
3609 jermar 304
	fat_idx_t *idxp;
305
	fat_node_t *nodep;
3633 jermar 306
	fat_bs_t *bs;
307
	fat_cluster_t mcl, lcl;
308
	uint16_t bps;
309
	int rc;
3609 jermar 310
 
3633 jermar 311
	bs = block_bb_get(dev_handle);
312
	bps = uint16_t_le2host(bs->bps);
313
	if (flags & L_DIRECTORY) {
314
		/* allocate a cluster */
315
		rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
316
		if (rc != EOK) 
317
			return NULL;
318
	}
319
 
3609 jermar 320
	nodep = fat_node_get_new();
3633 jermar 321
	if (!nodep) {
322
		fat_free_clusters(bs, dev_handle, mcl);	
3609 jermar 323
		return NULL;
3633 jermar 324
	}
3609 jermar 325
	idxp = fat_idx_get_new(dev_handle);
326
	if (!idxp) {
3633 jermar 327
		fat_free_clusters(bs, dev_handle, mcl);	
4357 jermar 328
		fat_node_put(FS_NODE(nodep));
3609 jermar 329
		return NULL;
330
	}
331
	/* idxp->lock held */
332
	if (flags & L_DIRECTORY) {
3633 jermar 333
		int i;
334
		block_t *b;
335
 
336
		/*
337
		 * Populate the new cluster with unused dentries.
338
		 */
339
		for (i = 0; i < bs->spc; i++) {
340
			b = _fat_block_get(bs, dev_handle, mcl, i,
341
			    BLOCK_FLAGS_NOREAD);
342
			/* mark all dentries as never-used */
343
			memset(b->data, 0, bps);
344
			b->dirty = false;
345
			block_put(b);
346
		}
3609 jermar 347
		nodep->type = FAT_DIRECTORY;
3633 jermar 348
		nodep->firstc = mcl;
349
		nodep->size = bps * bs->spc;
3609 jermar 350
	} else {
351
		nodep->type = FAT_FILE;
3633 jermar 352
		nodep->firstc = FAT_CLST_RES0;
353
		nodep->size = 0;
3609 jermar 354
	}
355
	nodep->lnkcnt = 0;	/* not linked anywhere */
356
	nodep->refcnt = 1;
3633 jermar 357
	nodep->dirty = true;
3609 jermar 358
 
359
	nodep->idx = idxp;
360
	idxp->nodep = nodep;
361
 
362
	futex_up(&idxp->lock);
4357 jermar 363
	return FS_NODE(nodep);
2857 jermar 364
}
365
 
4357 jermar 366
int fat_destroy_node(fs_node_t *fn)
2857 jermar 367
{
4357 jermar 368
	fat_node_t *nodep = FAT_NODE(fn);
3621 jermar 369
	fat_bs_t *bs;
370
 
371
	/*
372
	 * The node is not reachable from the file system. This means that the
373
	 * link count should be zero and that the index structure cannot be
374
	 * found in the position hash. Obviously, we don't need to lock the node
375
	 * nor its index structure.
376
	 */
377
	assert(nodep->lnkcnt == 0);
378
 
379
	/*
380
	 * The node may not have any children.
381
	 */
4357 jermar 382
	assert(fat_has_children(fn) == false);
3621 jermar 383
 
384
	bs = block_bb_get(nodep->idx->dev_handle);
385
	if (nodep->firstc != FAT_CLST_RES0) {
386
		assert(nodep->size);
387
		/* Free all clusters allocated to the node. */
388
		fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
389
	}
390
 
391
	fat_idx_destroy(nodep->idx);
4357 jermar 392
	free(nodep->bp);
3621 jermar 393
	free(nodep);
394
	return EOK;
2857 jermar 395
}
396
 
4357 jermar 397
int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
2857 jermar 398
{
4357 jermar 399
	fat_node_t *parentp = FAT_NODE(pfn);
400
	fat_node_t *childp = FAT_NODE(cfn);
3628 jermar 401
	fat_dentry_t *d;
402
	fat_bs_t *bs;
403
	block_t *b;
404
	int i, j;
405
	uint16_t bps;
406
	unsigned dps;
407
	unsigned blocks;
3700 jermar 408
	fat_cluster_t mcl, lcl;
409
	int rc;
3628 jermar 410
 
411
	futex_down(&childp->lock);
412
	if (childp->lnkcnt == 1) {
413
		/*
414
		 * On FAT, we don't support multiple hard links.
415
		 */
416
		futex_up(&childp->lock);
417
		return EMLINK;
418
	}
419
	assert(childp->lnkcnt == 0);
420
	futex_up(&childp->lock);
421
 
422
	if (!fat_dentry_name_verify(name)) {
423
		/*
424
		 * Attempt to create unsupported name.
425
		 */
426
		return ENOTSUP;
427
	}
428
 
429
	/*
430
	 * Get us an unused parent node's dentry or grow the parent and allocate
431
	 * a new one.
432
	 */
433
 
434
	futex_down(&parentp->idx->lock);
435
	bs = block_bb_get(parentp->idx->dev_handle);
436
	bps = uint16_t_le2host(bs->bps);
437
	dps = bps / sizeof(fat_dentry_t);
438
 
439
	blocks = parentp->size / bps;
440
 
441
	for (i = 0; i < blocks; i++) {
442
		b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
443
		for (j = 0; j < dps; j++) {
444
			d = ((fat_dentry_t *)b->data) + j;
445
			switch (fat_classify_dentry(d)) {
446
			case FAT_DENTRY_SKIP:
447
			case FAT_DENTRY_VALID:
448
				/* skipping used and meta entries */
449
				continue;
450
			case FAT_DENTRY_FREE:
451
			case FAT_DENTRY_LAST:
452
				/* found an empty slot */
453
				goto hit;
454
			}
455
		}
456
		block_put(b);
457
	}
3701 jermar 458
	j = 0;
3628 jermar 459
 
460
	/*
461
	 * We need to grow the parent in order to create a new unused dentry.
462
	 */
3700 jermar 463
	if (parentp->idx->pfc == FAT_CLST_ROOT) {
464
		/* Can't grow the root directory. */
465
		futex_up(&parentp->idx->lock);
466
		return ENOSPC;
467
	}
468
	rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
469
	if (rc != EOK) {
470
		futex_up(&parentp->idx->lock);
471
		return rc;
472
	}
473
	fat_append_clusters(bs, parentp, mcl);
474
	b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NOREAD);
475
	d = (fat_dentry_t *)b->data;
476
	/*
477
	 * Clear all dentries in the block except for the first one (the first
478
	 * dentry will be cleared in the next step).
479
	 */
480
	memset(d + 1, 0, bps - sizeof(fat_dentry_t));
3628 jermar 481
 
482
hit:
483
	/*
484
	 * At this point we only establish the link between the parent and the
485
	 * child.  The dentry, except of the name and the extension, will remain
3700 jermar 486
	 * uninitialized until the corresponding node is synced. Thus the valid
487
	 * dentry data is kept in the child node structure.
3628 jermar 488
	 */
489
	memset(d, 0, sizeof(fat_dentry_t));
490
	fat_dentry_name_set(d, name);
491
	b->dirty = true;		/* need to sync block */
492
	block_put(b);
493
	futex_up(&parentp->idx->lock);
494
 
495
	futex_down(&childp->idx->lock);
3660 jermar 496
 
497
	/*
498
	 * If possible, create the Sub-directory Identifier Entry and the
499
	 * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
500
	 * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
501
	 * not use them anyway, so this is rather a sign of our good will.
502
	 */
503
	b = fat_block_get(bs, childp, 0, BLOCK_FLAGS_NONE);
504
	d = (fat_dentry_t *)b->data;
505
	if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
4264 svoboda 506
	    str_cmp(d->name, FAT_NAME_DOT) == 0) {
3660 jermar 507
	   	memset(d, 0, sizeof(fat_dentry_t));
4268 svoboda 508
	   	str_cpy(d->name, 8, FAT_NAME_DOT);
509
		str_cpy(d->ext, 3, FAT_EXT_PAD);
3660 jermar 510
		d->attr = FAT_ATTR_SUBDIR;
511
		d->firstc = host2uint16_t_le(childp->firstc);
512
		/* TODO: initialize also the date/time members. */
513
	}
514
	d++;
515
	if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
4264 svoboda 516
	    str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
3660 jermar 517
		memset(d, 0, sizeof(fat_dentry_t));
4268 svoboda 518
		str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
519
		str_cpy(d->ext, 3, FAT_EXT_PAD);
3660 jermar 520
		d->attr = FAT_ATTR_SUBDIR;
521
		d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
522
		    host2uint16_t_le(FAT_CLST_RES0) :
523
		    host2uint16_t_le(parentp->firstc);
524
		/* TODO: initialize also the date/time members. */
525
	}
526
	b->dirty = true;		/* need to sync block */
527
	block_put(b);
528
 
3628 jermar 529
	childp->idx->pfc = parentp->firstc;
530
	childp->idx->pdi = i * dps + j;
531
	futex_up(&childp->idx->lock);
532
 
533
	futex_down(&childp->lock);
534
	childp->lnkcnt = 1;
535
	childp->dirty = true;		/* need to sync node */
536
	futex_up(&childp->lock);
537
 
538
	/*
539
	 * Hash in the index structure into the position hash.
540
	 */
541
	fat_idx_hashin(childp->idx);
542
 
543
	return EOK;
2857 jermar 544
}
545
 
4357 jermar 546
int fat_unlink(fs_node_t *pfn, fs_node_t *cfn)
2857 jermar 547
{
4357 jermar 548
	fat_node_t *parentp = FAT_NODE(pfn);
549
	fat_node_t *childp = FAT_NODE(cfn);
3638 jermar 550
	fat_bs_t *bs;
551
	fat_dentry_t *d;
552
	uint16_t bps;
553
	block_t *b;
554
 
4358 jermar 555
	if (!parentp)
556
		return EBUSY;
557
 
3638 jermar 558
	futex_down(&parentp->lock);
559
	futex_down(&childp->lock);
560
	assert(childp->lnkcnt == 1);
561
	futex_down(&childp->idx->lock);
562
	bs = block_bb_get(childp->idx->dev_handle);
563
	bps = uint16_t_le2host(bs->bps);
564
 
565
	b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc,
566
	    (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
567
	    BLOCK_FLAGS_NONE);
568
	d = (fat_dentry_t *)b->data +
569
	    (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
570
	/* mark the dentry as not-currently-used */
571
	d->name[0] = FAT_DENTRY_ERASED;
572
	b->dirty = true;		/* need to sync block */
573
	block_put(b);
574
 
575
	/* remove the index structure from the position hash */
576
	fat_idx_hashout(childp->idx);
577
	/* clear position information */
578
	childp->idx->pfc = FAT_CLST_RES0;
579
	childp->idx->pdi = 0;
580
	futex_up(&childp->idx->lock);
581
	childp->lnkcnt = 0;
582
	childp->dirty = true;
583
	futex_up(&childp->lock);
584
	futex_up(&parentp->lock);
585
 
586
	return EOK;
2857 jermar 587
}
588
 
4357 jermar 589
fs_node_t *fat_match(fs_node_t *pfn, const char *component)
2793 jermar 590
{
3530 jermar 591
	fat_bs_t *bs;
4357 jermar 592
	fat_node_t *parentp = FAT_NODE(pfn);
2793 jermar 593
	char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
2822 jermar 594
	unsigned i, j;
2828 jermar 595
	unsigned bps;		/* bytes per sector */
2822 jermar 596
	unsigned dps;		/* dentries per sector */
597
	unsigned blocks;
2793 jermar 598
	fat_dentry_t *d;
3530 jermar 599
	block_t *b;
2793 jermar 600
 
2953 jermar 601
	futex_down(&parentp->idx->lock);
3530 jermar 602
	bs = block_bb_get(parentp->idx->dev_handle);
603
	bps = uint16_t_le2host(bs->bps);
2828 jermar 604
	dps = bps / sizeof(fat_dentry_t);
3526 jermar 605
	blocks = parentp->size / bps;
2822 jermar 606
	for (i = 0; i < blocks; i++) {
3595 jermar 607
		b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
3526 jermar 608
		for (j = 0; j < dps; j++) { 
2822 jermar 609
			d = ((fat_dentry_t *)b->data) + j;
2845 jermar 610
			switch (fat_classify_dentry(d)) {
611
			case FAT_DENTRY_SKIP:
3628 jermar 612
			case FAT_DENTRY_FREE:
2822 jermar 613
				continue;
2845 jermar 614
			case FAT_DENTRY_LAST:
2822 jermar 615
				block_put(b);
2953 jermar 616
				futex_up(&parentp->idx->lock);
2822 jermar 617
				return NULL;
2845 jermar 618
			default:
619
			case FAT_DENTRY_VALID:
3628 jermar 620
				fat_dentry_name_get(d, name);
2845 jermar 621
				break;
2822 jermar 622
			}
3634 jermar 623
			if (fat_dentry_namecmp(name, component) == 0) {
2822 jermar 624
				/* hit */
4357 jermar 625
				fat_node_t *nodep;
2953 jermar 626
				/*
627
				 * Assume tree hierarchy for locking.  We
628
				 * already have the parent and now we are going
629
				 * to lock the child.  Never lock in the oposite
630
				 * order.
631
				 */
2890 jermar 632
				fat_idx_t *idx = fat_idx_get_by_pos(
2881 jermar 633
				    parentp->idx->dev_handle, parentp->firstc,
2864 jermar 634
				    i * dps + j);
2953 jermar 635
				futex_up(&parentp->idx->lock);
2890 jermar 636
				if (!idx) {
637
					/*
638
					 * Can happen if memory is low or if we
639
					 * run out of 32-bit indices.
640
					 */
641
					block_put(b);
642
					return NULL;
643
				}
4357 jermar 644
				nodep = fat_node_get_core(idx);
2951 jermar 645
				futex_up(&idx->lock);
2822 jermar 646
				block_put(b);
4357 jermar 647
				return FS_NODE(nodep);
2822 jermar 648
			}
2793 jermar 649
		}
2822 jermar 650
		block_put(b);
2639 jermar 651
	}
3516 jermar 652
 
2953 jermar 653
	futex_up(&parentp->idx->lock);
2793 jermar 654
	return NULL;
2638 jermar 655
}
656
 
4357 jermar 657
fs_index_t fat_index_get(fs_node_t *fn)
2831 jermar 658
{
4357 jermar 659
	return FAT_NODE(fn)->idx->index;
2831 jermar 660
}
661
 
4357 jermar 662
size_t fat_size_get(fs_node_t *fn)
2831 jermar 663
{
4357 jermar 664
	return FAT_NODE(fn)->size;
2831 jermar 665
}
666
 
4357 jermar 667
unsigned fat_lnkcnt_get(fs_node_t *fn)
2831 jermar 668
{
4357 jermar 669
	return FAT_NODE(fn)->lnkcnt;
2831 jermar 670
}
671
 
4357 jermar 672
bool fat_has_children(fs_node_t *fn)
2845 jermar 673
{
3530 jermar 674
	fat_bs_t *bs;
4357 jermar 675
	fat_node_t *nodep = FAT_NODE(fn);
2845 jermar 676
	unsigned bps;
677
	unsigned dps;
678
	unsigned blocks;
3530 jermar 679
	block_t *b;
2845 jermar 680
	unsigned i, j;
681
 
682
	if (nodep->type != FAT_DIRECTORY)
683
		return false;
3526 jermar 684
 
2951 jermar 685
	futex_down(&nodep->idx->lock);
3530 jermar 686
	bs = block_bb_get(nodep->idx->dev_handle);
687
	bps = uint16_t_le2host(bs->bps);
2845 jermar 688
	dps = bps / sizeof(fat_dentry_t);
689
 
3526 jermar 690
	blocks = nodep->size / bps;
2845 jermar 691
 
692
	for (i = 0; i < blocks; i++) {
693
		fat_dentry_t *d;
694
 
3595 jermar 695
		b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
3526 jermar 696
		for (j = 0; j < dps; j++) {
2845 jermar 697
			d = ((fat_dentry_t *)b->data) + j;
698
			switch (fat_classify_dentry(d)) {
699
			case FAT_DENTRY_SKIP:
3628 jermar 700
			case FAT_DENTRY_FREE:
2845 jermar 701
				continue;
702
			case FAT_DENTRY_LAST:
703
				block_put(b);
2951 jermar 704
				futex_up(&nodep->idx->lock);
2845 jermar 705
				return false;
706
			default:
707
			case FAT_DENTRY_VALID:
708
				block_put(b);
2951 jermar 709
				futex_up(&nodep->idx->lock);
2845 jermar 710
				return true;
711
			}
712
			block_put(b);
2951 jermar 713
			futex_up(&nodep->idx->lock);
2845 jermar 714
			return true;
715
		}
716
		block_put(b);
717
	}
718
 
2951 jermar 719
	futex_up(&nodep->idx->lock);
2845 jermar 720
	return false;
721
}
722
 
4357 jermar 723
fs_node_t *fat_root_get(dev_handle_t dev_handle)
2844 jermar 724
{
3119 jermar 725
	return fat_node_get(dev_handle, 0);
2844 jermar 726
}
727
 
3621 jermar 728
char fat_plb_get_char(unsigned pos)
2844 jermar 729
{
730
	return fat_reg.plb_ro[pos % PLB_SIZE];
731
}
732
 
4357 jermar 733
bool fat_is_directory(fs_node_t *fn)
2831 jermar 734
{
4357 jermar 735
	return FAT_NODE(fn)->type == FAT_DIRECTORY;
2831 jermar 736
}
737
 
4357 jermar 738
bool fat_is_file(fs_node_t *fn)
2831 jermar 739
{
4357 jermar 740
	return FAT_NODE(fn)->type == FAT_FILE;
2831 jermar 741
}
742
 
2793 jermar 743
/** libfs operations */
744
libfs_ops_t fat_libfs_ops = {
745
	.match = fat_match,
746
	.node_get = fat_node_get,
2852 jermar 747
	.node_put = fat_node_put,
3609 jermar 748
	.create = fat_create_node,
749
	.destroy = fat_destroy_node,
2857 jermar 750
	.link = fat_link,
751
	.unlink = fat_unlink,
2831 jermar 752
	.index_get = fat_index_get,
753
	.size_get = fat_size_get,
754
	.lnkcnt_get = fat_lnkcnt_get,
2845 jermar 755
	.has_children = fat_has_children,
2844 jermar 756
	.root_get = fat_root_get,
757
	.plb_get_char =	fat_plb_get_char,
2831 jermar 758
	.is_directory = fat_is_directory,
759
	.is_file = fat_is_file
2793 jermar 760
};
761
 
3625 jermar 762
/*
763
 * VFS operations.
764
 */
765
 
3110 jermar 766
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
767
{
768
	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
3530 jermar 769
	fat_bs_t *bs;
3257 jermar 770
	uint16_t bps;
3119 jermar 771
	uint16_t rde;
3110 jermar 772
	int rc;
773
 
4305 jermar 774
	/* accept the mount options */
775
	ipc_callid_t callid;
776
	size_t size;
777
	if (!ipc_data_write_receive(&callid, &size)) {
778
		ipc_answer_0(callid, EINVAL);
779
		ipc_answer_0(rid, EINVAL);
780
		return;
781
	}
782
	char *opts = malloc(size + 1);
783
	if (!opts) {
784
		ipc_answer_0(callid, ENOMEM);
785
		ipc_answer_0(rid, ENOMEM);
786
		return;
787
	}
788
	ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
789
	if (retval != EOK) {
790
		ipc_answer_0(rid, retval);
791
		free(opts);
792
		return;
793
	}
794
	opts[size] = '\0';
795
 
3530 jermar 796
	/* initialize libblock */
3537 jermar 797
	rc = block_init(dev_handle, BS_SIZE);
3257 jermar 798
	if (rc != EOK) {
3537 jermar 799
		ipc_answer_0(rid, rc);
3257 jermar 800
		return;
801
	}
802
 
3537 jermar 803
	/* prepare the boot block */
804
	rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
805
	if (rc != EOK) {
806
		block_fini(dev_handle);
807
		ipc_answer_0(rid, rc);
808
		return;
809
	}
810
 
3530 jermar 811
	/* get the buffer with the boot sector */
812
	bs = block_bb_get(dev_handle);
813
 
3119 jermar 814
	/* Read the number of root directory entries. */
3530 jermar 815
	bps = uint16_t_le2host(bs->bps);
816
	rde = uint16_t_le2host(bs->root_ent_max);
3119 jermar 817
 
3257 jermar 818
	if (bps != BS_SIZE) {
3530 jermar 819
		block_fini(dev_handle);
3257 jermar 820
		ipc_answer_0(rid, ENOTSUP);
821
		return;
822
	}
823
 
3539 jermar 824
	/* Initialize the block cache */
825
	rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
826
	if (rc != EOK) {
827
		block_fini(dev_handle);
828
		ipc_answer_0(rid, rc);
829
		return;
830
	}
831
 
3110 jermar 832
	rc = fat_idx_init_by_dev_handle(dev_handle);
833
	if (rc != EOK) {
3530 jermar 834
		block_fini(dev_handle);
3110 jermar 835
		ipc_answer_0(rid, rc);
836
		return;
837
	}
838
 
3119 jermar 839
	/* Initialize the root node. */
4357 jermar 840
	fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
841
	if (!rfn) {
842
		block_fini(dev_handle);
843
		fat_idx_fini_by_dev_handle(dev_handle);
844
		ipc_answer_0(rid, ENOMEM);
845
		return;
846
	}
3119 jermar 847
	fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
848
	if (!rootp) {
4357 jermar 849
		free(rfn);
3530 jermar 850
		block_fini(dev_handle);
3119 jermar 851
		fat_idx_fini_by_dev_handle(dev_handle);
852
		ipc_answer_0(rid, ENOMEM);
853
		return;
854
	}
855
	fat_node_initialize(rootp);
856
 
857
	fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
858
	if (!ridxp) {
4357 jermar 859
		free(rfn);
860
		free(rootp);
3530 jermar 861
		block_fini(dev_handle);
3119 jermar 862
		fat_idx_fini_by_dev_handle(dev_handle);
863
		ipc_answer_0(rid, ENOMEM);
864
		return;
865
	}
866
	assert(ridxp->index == 0);
867
	/* ridxp->lock held */
868
 
869
	rootp->type = FAT_DIRECTORY;
870
	rootp->firstc = FAT_CLST_ROOT;
871
	rootp->refcnt = 1;
3352 jermar 872
	rootp->lnkcnt = 0;	/* FS root is not linked */
3119 jermar 873
	rootp->size = rde * sizeof(fat_dentry_t);
874
	rootp->idx = ridxp;
875
	ridxp->nodep = rootp;
4357 jermar 876
	rootp->bp = rfn;
877
	rfn->data = rootp;
3119 jermar 878
 
879
	futex_up(&ridxp->lock);
880
 
3352 jermar 881
	ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
3110 jermar 882
}
883
 
884
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
885
{
886
	ipc_answer_0(rid, ENOTSUP);
887
}
888
 
2627 jermar 889
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
890
{
2793 jermar 891
	libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
2627 jermar 892
}
893
 
3307 jermar 894
void fat_read(ipc_callid_t rid, ipc_call_t *request)
895
{
896
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
897
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
898
	off_t pos = (off_t)IPC_GET_ARG3(*request);
4357 jermar 899
	fs_node_t *fn = fat_node_get(dev_handle, index);
900
	fat_node_t *nodep;
3530 jermar 901
	fat_bs_t *bs;
3516 jermar 902
	uint16_t bps;
3308 jermar 903
	size_t bytes;
3530 jermar 904
	block_t *b;
3308 jermar 905
 
4357 jermar 906
	if (!fn) {
3307 jermar 907
		ipc_answer_0(rid, ENOENT);
908
		return;
909
	}
4357 jermar 910
	nodep = FAT_NODE(fn);
3307 jermar 911
 
912
	ipc_callid_t callid;
913
	size_t len;
3314 jermar 914
	if (!ipc_data_read_receive(&callid, &len)) {
4357 jermar 915
		fat_node_put(fn);
3307 jermar 916
		ipc_answer_0(callid, EINVAL);
917
		ipc_answer_0(rid, EINVAL);
918
		return;
919
	}
920
 
3530 jermar 921
	bs = block_bb_get(dev_handle);
922
	bps = uint16_t_le2host(bs->bps);
3516 jermar 923
 
3307 jermar 924
	if (nodep->type == FAT_FILE) {
3335 jermar 925
		/*
926
		 * Our strategy for regular file reads is to read one block at
927
		 * most and make use of the possibility to return less data than
928
		 * requested. This keeps the code very simple.
929
		 */
3532 jermar 930
		if (pos >= nodep->size) {
3533 jermar 931
			/* reading beyond the EOF */
932
			bytes = 0;
3532 jermar 933
			(void) ipc_data_read_finalize(callid, NULL, 0);
934
		} else {
935
			bytes = min(len, bps - pos % bps);
936
			bytes = min(bytes, nodep->size - pos);
3595 jermar 937
			b = fat_block_get(bs, nodep, pos / bps,
938
			    BLOCK_FLAGS_NONE);
3532 jermar 939
			(void) ipc_data_read_finalize(callid, b->data + pos % bps,
940
			    bytes);
941
			block_put(b);
942
		}
3307 jermar 943
	} else {
3335 jermar 944
		unsigned bnum;
945
		off_t spos = pos;
946
		char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
947
		fat_dentry_t *d;
948
 
3307 jermar 949
		assert(nodep->type == FAT_DIRECTORY);
3335 jermar 950
		assert(nodep->size % bps == 0);
951
		assert(bps % sizeof(fat_dentry_t) == 0);
952
 
953
		/*
954
		 * Our strategy for readdir() is to use the position pointer as
955
		 * an index into the array of all dentries. On entry, it points
956
		 * to the first unread dentry. If we skip any dentries, we bump
957
		 * the position pointer accordingly.
958
		 */
959
		bnum = (pos * sizeof(fat_dentry_t)) / bps;
960
		while (bnum < nodep->size / bps) {
961
			off_t o;
962
 
3595 jermar 963
			b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE);
3335 jermar 964
			for (o = pos % (bps / sizeof(fat_dentry_t));
965
			    o < bps / sizeof(fat_dentry_t);
966
			    o++, pos++) {
967
				d = ((fat_dentry_t *)b->data) + o;
968
				switch (fat_classify_dentry(d)) {
969
				case FAT_DENTRY_SKIP:
3628 jermar 970
				case FAT_DENTRY_FREE:
3335 jermar 971
					continue;
972
				case FAT_DENTRY_LAST:
973
					block_put(b);
974
					goto miss;
975
				default:
976
				case FAT_DENTRY_VALID:
3628 jermar 977
					fat_dentry_name_get(d, name);
3335 jermar 978
					block_put(b);
979
					goto hit;
980
				}
981
			}
982
			block_put(b);
983
			bnum++;
984
		}
985
miss:
4357 jermar 986
		fat_node_put(fn);
3335 jermar 987
		ipc_answer_0(callid, ENOENT);
988
		ipc_answer_1(rid, ENOENT, 0);
3307 jermar 989
		return;
3335 jermar 990
hit:
4264 svoboda 991
		(void) ipc_data_read_finalize(callid, name, str_size(name) + 1);
3335 jermar 992
		bytes = (pos - spos) + 1;
3307 jermar 993
	}
994
 
4357 jermar 995
	fat_node_put(fn);
3308 jermar 996
	ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
3307 jermar 997
}
998
 
3497 jermar 999
void fat_write(ipc_callid_t rid, ipc_call_t *request)
1000
{
3499 jermar 1001
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1002
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1003
	off_t pos = (off_t)IPC_GET_ARG3(*request);
4357 jermar 1004
	fs_node_t *fn = fat_node_get(dev_handle, index);
1005
	fat_node_t *nodep;
3530 jermar 1006
	fat_bs_t *bs;
3499 jermar 1007
	size_t bytes;
3530 jermar 1008
	block_t *b;
3499 jermar 1009
	uint16_t bps;
1010
	unsigned spc;
3572 jermar 1011
	unsigned bpc;		/* bytes per cluster */
3501 jermar 1012
	off_t boundary;
3595 jermar 1013
	int flags = BLOCK_FLAGS_NONE;
3499 jermar 1014
 
4357 jermar 1015
	if (!fn) {
3499 jermar 1016
		ipc_answer_0(rid, ENOENT);
1017
		return;
1018
	}
4357 jermar 1019
	nodep = FAT_NODE(fn);
3499 jermar 1020
 
1021
	ipc_callid_t callid;
1022
	size_t len;
1023
	if (!ipc_data_write_receive(&callid, &len)) {
4357 jermar 1024
		fat_node_put(fn);
3499 jermar 1025
		ipc_answer_0(callid, EINVAL);
1026
		ipc_answer_0(rid, EINVAL);
1027
		return;
1028
	}
1029
 
3572 jermar 1030
	bs = block_bb_get(dev_handle);
1031
	bps = uint16_t_le2host(bs->bps);
1032
	spc = bs->spc;
1033
	bpc = bps * spc;
1034
 
3499 jermar 1035
	/*
1036
	 * In all scenarios, we will attempt to write out only one block worth
1037
	 * of data at maximum. There might be some more efficient approaches,
1038
	 * but this one greatly simplifies fat_write(). Note that we can afford
1039
	 * to do this because the client must be ready to handle the return
1040
	 * value signalizing a smaller number of bytes written. 
1041
	 */ 
1042
	bytes = min(len, bps - pos % bps);
3595 jermar 1043
	if (bytes == bps)
1044
		flags |= BLOCK_FLAGS_NOREAD;
3499 jermar 1045
 
3572 jermar 1046
	boundary = ROUND_UP(nodep->size, bpc);
3501 jermar 1047
	if (pos < boundary) {
3499 jermar 1048
		/*
1049
		 * This is the easier case - we are either overwriting already
1050
		 * existing contents or writing behind the EOF, but still within
1051
		 * the limits of the last cluster. The node size may grow to the
1052
		 * next block size boundary.
1053
		 */
3530 jermar 1054
		fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
3595 jermar 1055
		b = fat_block_get(bs, nodep, pos / bps, flags);
3499 jermar 1056
		(void) ipc_data_write_finalize(callid, b->data + pos % bps,
1057
		    bytes);
1058
		b->dirty = true;		/* need to sync block */
3500 jermar 1059
		block_put(b);
3499 jermar 1060
		if (pos + bytes > nodep->size) {
1061
			nodep->size = pos + bytes;
1062
			nodep->dirty = true;	/* need to sync node */
1063
		}
3573 jermar 1064
		ipc_answer_2(rid, EOK, bytes, nodep->size);	
4357 jermar 1065
		fat_node_put(fn);
3499 jermar 1066
		return;
1067
	} else {
1068
		/*
1069
		 * This is the more difficult case. We must allocate new
1070
		 * clusters for the node and zero them out.
1071
		 */
3500 jermar 1072
		int status;
3499 jermar 1073
		unsigned nclsts;
3548 jermar 1074
		fat_cluster_t mcl, lcl; 
1075
 
3572 jermar 1076
		nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
3500 jermar 1077
		/* create an independent chain of nclsts clusters in all FATs */
3572 jermar 1078
		status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
3500 jermar 1079
		if (status != EOK) {
1080
			/* could not allocate a chain of nclsts clusters */
4357 jermar 1081
			fat_node_put(fn);
3500 jermar 1082
			ipc_answer_0(callid, status);
1083
			ipc_answer_0(rid, status);
1084
			return;
1085
		}
1086
		/* zero fill any gaps */
3530 jermar 1087
		fat_fill_gap(bs, nodep, mcl, pos);
3595 jermar 1088
		b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc,
1089
		    flags);
3500 jermar 1090
		(void) ipc_data_write_finalize(callid, b->data + pos % bps,
1091
		    bytes);
3501 jermar 1092
		b->dirty = true;		/* need to sync block */
3500 jermar 1093
		block_put(b);
1094
		/*
1095
		 * Append the cluster chain starting in mcl to the end of the
1096
		 * node's cluster chain.
1097
		 */
3530 jermar 1098
		fat_append_clusters(bs, nodep, mcl);
3500 jermar 1099
		nodep->size = pos + bytes;
3501 jermar 1100
		nodep->dirty = true;		/* need to sync node */
3573 jermar 1101
		ipc_answer_2(rid, EOK, bytes, nodep->size);
4357 jermar 1102
		fat_node_put(fn);
3500 jermar 1103
		return;
3499 jermar 1104
	}
3497 jermar 1105
}
1106
 
3546 jermar 1107
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1108
{
3548 jermar 1109
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1110
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1111
	size_t size = (off_t)IPC_GET_ARG3(*request);
4357 jermar 1112
	fs_node_t *fn = fat_node_get(dev_handle, index);
1113
	fat_node_t *nodep;
3572 jermar 1114
	fat_bs_t *bs;
1115
	uint16_t bps;
1116
	uint8_t spc;
1117
	unsigned bpc;	/* bytes per cluster */
3548 jermar 1118
	int rc;
1119
 
4357 jermar 1120
	if (!fn) {
3548 jermar 1121
		ipc_answer_0(rid, ENOENT);
1122
		return;
1123
	}
4357 jermar 1124
	nodep = FAT_NODE(fn);
3548 jermar 1125
 
3572 jermar 1126
	bs = block_bb_get(dev_handle);
1127
	bps = uint16_t_le2host(bs->bps);
1128
	spc = bs->spc;
1129
	bpc = bps * spc;
1130
 
3548 jermar 1131
	if (nodep->size == size) {
1132
		rc = EOK;
1133
	} else if (nodep->size < size) {
1134
		/*
3572 jermar 1135
		 * The standard says we have the freedom to grow the node.
3548 jermar 1136
		 * For now, we simply return an error.
1137
		 */
1138
		rc = EINVAL;
3572 jermar 1139
	} else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
1140
		/*
1141
		 * The node will be shrunk, but no clusters will be deallocated.
1142
		 */
1143
		nodep->size = size;
1144
		nodep->dirty = true;		/* need to sync node */
1145
		rc = EOK;	
3548 jermar 1146
	} else {
1147
		/*
3572 jermar 1148
		 * The node will be shrunk, clusters will be deallocated.
3548 jermar 1149
		 */
3572 jermar 1150
		if (size == 0) {
1151
			fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1152
		} else {
1153
			fat_cluster_t lastc;
1154
			(void) fat_cluster_walk(bs, dev_handle, nodep->firstc,
1155
			    &lastc, (size - 1) / bpc);
1156
			fat_chop_clusters(bs, nodep, lastc);
1157
		}
1158
		nodep->size = size;
1159
		nodep->dirty = true;		/* need to sync node */
1160
		rc = EOK;	
3548 jermar 1161
	}
4357 jermar 1162
	fat_node_put(fn);
3548 jermar 1163
	ipc_answer_0(rid, rc);
1164
	return;
3546 jermar 1165
}
1166
 
3621 jermar 1167
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1168
{
1169
	dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1170
	fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1171
	int rc;
1172
 
4357 jermar 1173
	fs_node_t *fn = fat_node_get(dev_handle, index);
1174
	if (!fn) {
3621 jermar 1175
		ipc_answer_0(rid, ENOENT);
1176
		return;
1177
	}
1178
 
4357 jermar 1179
	rc = fat_destroy_node(fn);
3621 jermar 1180
	ipc_answer_0(rid, rc);
1181
}
1182
 
2627 jermar 1183
/**
1184
 * @}
1185
 */