2
* Copyright (c) 2008 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
35
* @brief Functions that manipulate the File Allocation Tables.
39
#include "fat_dentry.h"
41
#include "../../vfs/vfs.h"
45
#include <byteorder.h>
48
#include <fibril_sync.h>
52
* The fat_alloc_lock mutex protects all copies of the File Allocation Table
53
* during allocation of clusters. The lock does not have to be held durring
54
* deallocation of clusters.
56
static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
58
/** Walk the cluster chain.
60
* @param bs Buffer holding the boot sector for the file.
61
* @param dev_handle Device handle of the device with the file.
62
* @param firstc First cluster to start the walk with.
63
* @param lastc If non-NULL, output argument hodling the last cluster number visited.
64
* @param max_clusters Maximum number of clusters to visit.
66
* @return Number of clusters seen during the walk.
69
fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
70
fat_cluster_t *lastc, uint16_t max_clusters)
74
unsigned rscnt; /* block address of the first FAT */
75
uint16_t clusters = 0;
76
fat_cluster_t clst = firstc;
78
bps = uint16_t_le2host(bs->bps);
79
rscnt = uint16_t_le2host(bs->rscnt);
81
if (firstc == FAT_CLST_RES0) {
82
/* No space allocated to the file. */
88
while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
89
bn_t fsec; /* sector offset relative to FAT1 */
90
unsigned fidx; /* FAT1 entry index */
92
assert(clst >= FAT_CLST_FIRST);
94
*lastc = clst; /* remember the last cluster number */
95
fsec = (clst * sizeof(fat_cluster_t)) / bps;
96
fidx = clst % (bps / sizeof(fat_cluster_t));
98
b = block_get(dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
99
clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
100
assert(clst != FAT_CLST_BAD);
105
if (lastc && clst < FAT_CLST_LAST1)
111
/** Read block from file located on a FAT file system.
113
* @param bs Buffer holding the boot sector of the file system.
114
* @param dev_handle Device handle of the file system.
115
* @param firstc First cluster used by the file. Can be zero if the file
117
* @param bn Block number.
118
* @param flags Flags passed to libblock.
120
* @return Block structure holding the requested block.
123
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
128
unsigned rscnt; /* block address of the first FAT */
130
unsigned rds; /* root directory size */
132
unsigned ssa; /* size of the system area */
133
unsigned clusters, max_clusters;
136
bps = uint16_t_le2host(bs->bps);
137
rscnt = uint16_t_le2host(bs->rscnt);
138
rde = uint16_t_le2host(bs->root_ent_max);
139
sf = uint16_t_le2host(bs->sec_per_fat);
141
rds = (sizeof(fat_dentry_t) * rde) / bps;
142
rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
143
ssa = rscnt + bs->fatcnt * sf + rds;
145
if (firstc == FAT_CLST_ROOT) {
146
/* root directory special case */
148
b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn, flags);
152
max_clusters = bn / bs->spc;
153
clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
155
assert(clusters == max_clusters);
157
b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
158
bn % bs->spc, flags);
163
/** Fill the gap between EOF and a new file position.
165
* @param bs Buffer holding the boot sector for nodep.
166
* @param nodep FAT node with the gap.
167
* @param mcl First cluster in an independent cluster chain that will
168
* be later appended to the end of the node's own cluster
169
* chain. If pos is still in the last allocated cluster,
170
* this argument is ignored.
171
* @param pos Position in the last node block.
173
void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
180
bps = uint16_t_le2host(bs->bps);
183
boundary = ROUND_UP(nodep->size, bps * spc);
185
/* zero out already allocated space */
186
for (o = nodep->size; o < pos && o < boundary;
187
o = ALIGN_DOWN(o + bps, bps)) {
188
int flags = (o % bps == 0) ?
189
BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
190
b = fat_block_get(bs, nodep, o / bps, flags);
191
memset(b->data + o % bps, 0, bps - o % bps);
192
b->dirty = true; /* need to sync node */
199
/* zero out the initial part of the new cluster chain */
200
for (o = boundary; o < pos; o += bps) {
201
b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
202
(o - boundary) / bps, BLOCK_FLAGS_NOREAD);
203
memset(b->data, 0, min(bps, pos - o));
204
b->dirty = true; /* need to sync node */
209
/** Get cluster from the first FAT.
211
* @param bs Buffer holding the boot sector for the file system.
212
* @param dev_handle Device handle for the file system.
213
* @param clst Cluster which to get.
215
* @return Value found in the cluster.
218
fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst)
223
fat_cluster_t *cp, value;
225
bps = uint16_t_le2host(bs->bps);
226
rscnt = uint16_t_le2host(bs->rscnt);
228
b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps,
230
cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
231
value = uint16_t_le2host(*cp);
237
/** Set cluster in one instance of FAT.
239
* @param bs Buffer holding the boot sector for the file system.
240
* @param dev_handle Device handle for the file system.
241
* @param fatno Number of the FAT instance where to make the change.
242
* @param clst Cluster which is to be set.
243
* @param value Value to set the cluster with.
246
fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
247
fat_cluster_t clst, fat_cluster_t value)
255
bps = uint16_t_le2host(bs->bps);
256
rscnt = uint16_t_le2host(bs->rscnt);
257
sf = uint16_t_le2host(bs->sec_per_fat);
259
assert(fatno < bs->fatcnt);
260
b = block_get(dev_handle, rscnt + sf * fatno +
261
(clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
262
cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
263
*cp = host2uint16_t_le(value);
264
b->dirty = true; /* need to sync block */
268
/** Replay the allocatoin of clusters in all shadow instances of FAT.
270
* @param bs Buffer holding the boot sector of the file system.
271
* @param dev_handle Device handle of the file system.
272
* @param lifo Chain of allocated clusters.
273
* @param nclsts Number of clusters in the lifo chain.
275
void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
276
fat_cluster_t *lifo, unsigned nclsts)
281
for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
282
for (c = 0; c < nclsts; c++) {
283
fat_set_cluster(bs, dev_handle, fatno, lifo[c],
284
c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
289
/** Allocate clusters in all copies of FAT.
291
* This function will attempt to allocate the requested number of clusters in
292
* all instances of the FAT. The FAT will be altered so that the allocated
293
* clusters form an independent chain (i.e. a chain which does not belong to any
296
* @param bs Buffer holding the boot sector of the file system.
297
* @param dev_handle Device handle of the file system.
298
* @param nclsts Number of clusters to allocate.
299
* @param mcl Output parameter where the first cluster in the chain
301
* @param lcl Output parameter where the last cluster in the chain
304
* @return EOK on success, a negative error code otherwise.
307
fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
308
fat_cluster_t *mcl, fat_cluster_t *lcl)
314
fat_cluster_t *lifo; /* stack for storing free cluster numbers */
315
unsigned found = 0; /* top of the free cluster number stack */
318
lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
322
bps = uint16_t_le2host(bs->bps);
323
rscnt = uint16_t_le2host(bs->rscnt);
324
sf = uint16_t_le2host(bs->sec_per_fat);
327
* Search FAT1 for unused clusters.
329
fibril_mutex_lock(&fat_alloc_lock);
330
for (b = 0, cl = 0; b < sf; b++) {
331
blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
332
for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
333
fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
334
if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
336
* The cluster is free. Put it into our stack
337
* of found clusters and mark it as non-free.
340
*clst = (found == 0) ?
341
host2uint16_t_le(FAT_CLST_LAST1) :
342
host2uint16_t_le(lifo[found - 1]);
343
blk->dirty = true; /* need to sync block */
344
if (++found == nclsts) {
345
/* we are almost done */
347
/* update the shadow copies of FAT */
348
fat_alloc_shadow_clusters(bs,
349
dev_handle, lifo, nclsts);
350
*mcl = lifo[found - 1];
353
fibril_mutex_unlock(&fat_alloc_lock);
360
fibril_mutex_unlock(&fat_alloc_lock);
363
* We could not find enough clusters. Now we need to free the clusters
364
* we have allocated so far.
367
fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
375
/** Free clusters forming a cluster chain in all copies of FAT.
377
* @param bs Buffer hodling the boot sector of the file system.
378
* @param dev_handle Device handle of the file system.
379
* @param firstc First cluster in the chain which is to be freed.
382
fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
387
/* Mark all clusters in the chain as free in all copies of FAT. */
388
while (firstc < FAT_CLST_LAST1) {
389
assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
390
nextc = fat_get_cluster(bs, dev_handle, firstc);
391
for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
392
fat_set_cluster(bs, dev_handle, fatno, firstc,
398
/** Append a cluster chain to the last file cluster in all FATs.
400
* @param bs Buffer holding the boot sector of the file system.
401
* @param nodep Node representing the file.
402
* @param mcl First cluster of the cluster chain to append.
404
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
406
dev_handle_t dev_handle = nodep->idx->dev_handle;
410
if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
411
(uint16_t) -1) == 0) {
412
/* No clusters allocated to the node yet. */
414
nodep->dirty = true; /* need to sync node */
418
for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
419
fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
422
/** Chop off node clusters in all copies of FAT.
424
* @param bs Buffer holding the boot sector of the file system.
425
* @param nodep FAT node where the chopping will take place.
426
* @param lastc Last cluster which will remain in the node. If this
427
* argument is FAT_CLST_RES0, then all clusters will
430
void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
432
dev_handle_t dev_handle = nodep->idx->dev_handle;
433
if (lastc == FAT_CLST_RES0) {
434
/* The node will have zero size and no clusters allocated. */
435
fat_free_clusters(bs, dev_handle, nodep->firstc);
436
nodep->firstc = FAT_CLST_RES0;
437
nodep->dirty = true; /* need to sync node */
442
nextc = fat_get_cluster(bs, dev_handle, lastc);
444
/* Terminate the cluster chain in all copies of FAT. */
445
for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
446
fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1);
448
/* Free all following clusters. */
449
fat_free_clusters(bs, dev_handle, nextc);