source: mainline/uspace/srv/fs/fat/fat_fat.c@ dba4a23

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dba4a23 was dba4a23, checked in by Jakub Jermar <jakub@…>, 15 years ago

Speed up sequential I/O by caching the "current" cluster in fat_block_get().
This improves sequentil read of a 5m file (using the cat command) by 45 seconds
(73s → 28s).

  • Property mode set to 100644
File size: 19.7 KB
RevLine 
[6ebaff9]1/*
2 * Copyright (c) 2008 Jakub 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_fat.c
[0ec862d]35 * @brief Functions that manipulate the File Allocation Tables.
[6ebaff9]36 */
37
[0f57d0e]38#include "fat_fat.h"
39#include "fat_dentry.h"
40#include "fat.h"
41#include "../../vfs/vfs.h"
42#include <libfs.h>
[fc840d9]43#include <libblock.h>
[0f57d0e]44#include <errno.h>
45#include <byteorder.h>
46#include <align.h>
47#include <assert.h>
[1e4cada]48#include <fibril_synch.h>
[c20aa06]49#include <mem.h>
[0ec862d]50
[ed6cf34b]51/*
52 * Convenience macros for computing some frequently used values from the
53 * primitive boot sector members.
54 */
55#define RDS(bs) ((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \
56 (((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0)
57#define SSA(bs) (RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs))
58
59#define CLBN2PBN(bs, cl, bn) \
60 (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
61
[0ec862d]62/**
[6ebe721]63 * The fat_alloc_lock mutex protects all copies of the File Allocation Table
[0ec862d]64 * during allocation of clusters. The lock does not have to be held durring
65 * deallocation of clusters.
66 */
[6ebe721]67static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
[0f57d0e]68
[4f1c0b4]69/** Walk the cluster chain.
[9f95a80]70 *
[4f1c0b4]71 * @param bs Buffer holding the boot sector for the file.
72 * @param dev_handle Device handle of the device with the file.
73 * @param firstc First cluster to start the walk with.
[e402382]74 * @param lastc If non-NULL, output argument hodling the last cluster
75 * number visited.
76 * @param numc If non-NULL, output argument holding the number of
77 * clusters seen during the walk.
[4f1c0b4]78 * @param max_clusters Maximum number of clusters to visit.
[9f95a80]79 *
[e402382]80 * @return EOK on success or a negative error code.
[9f95a80]81 */
[e402382]82int
[4f1c0b4]83fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
[e402382]84 fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
[0f57d0e]85{
86 block_t *b;
[4f1c0b4]87 uint16_t clusters = 0;
[0f57d0e]88 fat_cluster_t clst = firstc;
[c91f2d1b]89 int rc;
[0f57d0e]90
[4f1c0b4]91 if (firstc == FAT_CLST_RES0) {
92 /* No space allocated to the file. */
[6c8d267]93 if (lastc)
94 *lastc = firstc;
[e402382]95 if (numc)
96 *numc = 0;
97 return EOK;
[0f57d0e]98 }
99
[4f1c0b4]100 while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
[ed903174]101 aoff64_t fsec; /* sector offset relative to FAT1 */
[0f57d0e]102 unsigned fidx; /* FAT1 entry index */
103
[4f1c0b4]104 assert(clst >= FAT_CLST_FIRST);
[6c8d267]105 if (lastc)
106 *lastc = clst; /* remember the last cluster number */
[ed6cf34b]107 fsec = (clst * sizeof(fat_cluster_t)) / BPS(bs);
108 fidx = clst % (BPS(bs) / sizeof(fat_cluster_t));
[0f57d0e]109 /* read FAT1 */
[ed6cf34b]110 rc = block_get(&b, dev_handle, RSCNT(bs) + fsec,
111 BLOCK_FLAGS_NONE);
[e402382]112 if (rc != EOK)
113 return rc;
[0f57d0e]114 clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
115 assert(clst != FAT_CLST_BAD);
[c91f2d1b]116 rc = block_put(b);
[e402382]117 if (rc != EOK)
118 return rc;
[4f1c0b4]119 clusters++;
[0f57d0e]120 }
121
[6c8d267]122 if (lastc && clst < FAT_CLST_LAST1)
123 *lastc = clst;
[e402382]124 if (numc)
125 *numc = clusters;
[0f57d0e]126
[e402382]127 return EOK;
[0f57d0e]128}
129
[746f623]130/** Read block from file located on a FAT file system.
131 *
132 * @param block Pointer to a block pointer for storing result.
133 * @param bs Buffer holding the boot sector of the file system.
134 * @param nodep FAT node.
135 * @param bn Block number.
136 * @param flags Flags passed to libblock.
137 *
138 * @return EOK on success or a negative error code.
139 */
140int
141fat_block_get(block_t **block, struct fat_bs *bs, fat_node_t *nodep,
142 aoff64_t bn, int flags)
143{
[dba4a23]144 fat_cluster_t firstc = nodep->firstc;
145 fat_cluster_t currc;
146 aoff64_t relbn = bn;
147 int rc;
148
[746f623]149 if (!nodep->size)
150 return ELIMIT;
151
[ed6cf34b]152 if (nodep->firstc == FAT_CLST_ROOT)
153 goto fall_through;
154
155 if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
156 nodep->lastc_cached_valid) {
[746f623]157 /*
158 * This is a request to read a block within the last cluster
159 * when fortunately we have the last cluster number cached.
160 */
161 return block_get(block, nodep->idx->dev_handle,
[ed6cf34b]162 CLBN2PBN(bs, nodep->lastc_cached_value, bn), flags);
[746f623]163 }
[ed6cf34b]164
[dba4a23]165 if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
166 /*
167 * We can start with the cluster cached by the previous call to
168 * fat_block_get().
169 */
170 firstc = nodep->currc_cached_value;
171 relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
172 }
173
[ed6cf34b]174fall_through:
[dba4a23]175 rc = _fat_block_get(block, bs, nodep->idx->dev_handle, firstc,
176 &currc, relbn, flags);
177 if (rc != EOK)
178 return rc;
179
180 /*
181 * Update the "current" cluster cache.
182 */
183 nodep->currc_cached_valid = true;
184 nodep->currc_cached_bn = bn;
185 nodep->currc_cached_value = currc;
186
187 return rc;
[746f623]188}
189
[4f1c0b4]190/** Read block from file located on a FAT file system.
[0f57d0e]191 *
[684b655]192 * @param block Pointer to a block pointer for storing result.
[4f1c0b4]193 * @param bs Buffer holding the boot sector of the file system.
194 * @param dev_handle Device handle of the file system.
[6da81e0]195 * @param fcl First cluster used by the file. Can be zero if the file
[4f1c0b4]196 * is empty.
[6da81e0]197 * @param clp If not NULL, address where the cluster containing bn
198 * will be stored.
199 * stored
[6c8d267]200 * @param bn Block number.
[1d8cdb1]201 * @param flags Flags passed to libblock.
[0f57d0e]202 *
[684b655]203 * @return EOK on success or a negative error code.
[0f57d0e]204 */
[684b655]205int
206_fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle,
[6da81e0]207 fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags)
[0f57d0e]208{
[e402382]209 uint16_t clusters;
210 unsigned max_clusters;
[6da81e0]211 fat_cluster_t c;
[c91f2d1b]212 int rc;
[0f57d0e]213
[5178ee2]214 /*
215 * This function can only operate on non-zero length files.
216 */
[6da81e0]217 if (fcl == FAT_CLST_RES0)
[5178ee2]218 return ELIMIT;
219
[6da81e0]220 if (fcl == FAT_CLST_ROOT) {
[4f1c0b4]221 /* root directory special case */
[ed6cf34b]222 assert(bn < RDS(bs));
223 rc = block_get(block, dev_handle,
224 RSCNT(bs) + FATCNT(bs) * SF(bs) + bn, flags);
[684b655]225 return rc;
[0f57d0e]226 }
227
[ed6cf34b]228 max_clusters = bn / SPC(bs);
[6da81e0]229 rc = fat_cluster_walk(bs, dev_handle, fcl, &c, &clusters, max_clusters);
[e402382]230 if (rc != EOK)
231 return rc;
[4f1c0b4]232 assert(clusters == max_clusters);
[0f57d0e]233
[6da81e0]234 rc = block_get(block, dev_handle, CLBN2PBN(bs, c, bn), flags);
235
236 if (clp)
237 *clp = c;
[0f57d0e]238
[684b655]239 return rc;
[0f57d0e]240}
241
242/** Fill the gap between EOF and a new file position.
243 *
[cb682eb]244 * @param bs Buffer holding the boot sector for nodep.
[0f57d0e]245 * @param nodep FAT node with the gap.
246 * @param mcl First cluster in an independent cluster chain that will
247 * be later appended to the end of the node's own cluster
248 * chain. If pos is still in the last allocated cluster,
249 * this argument is ignored.
250 * @param pos Position in the last node block.
[cca29e3c]251 *
252 * @return EOK on success or a negative error code.
[0f57d0e]253 */
[ed903174]254int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
[0f57d0e]255{
[cb682eb]256 block_t *b;
[ed903174]257 aoff64_t o, boundary;
[c91f2d1b]258 int rc;
[0f57d0e]259
[ed6cf34b]260 boundary = ROUND_UP(nodep->size, BPS(bs) * SPC(bs));
[0f57d0e]261
262 /* zero out already allocated space */
[abd36f7]263 for (o = nodep->size; o < pos && o < boundary;
[ed6cf34b]264 o = ALIGN_DOWN(o + BPS(bs), BPS(bs))) {
265 int flags = (o % BPS(bs) == 0) ?
[1d8cdb1]266 BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
[ed6cf34b]267 rc = fat_block_get(&b, bs, nodep, o / BPS(bs), flags);
[cca29e3c]268 if (rc != EOK)
269 return rc;
[ed6cf34b]270 memset(b->data + o % BPS(bs), 0, BPS(bs) - o % BPS(bs));
[0f57d0e]271 b->dirty = true; /* need to sync node */
[c91f2d1b]272 rc = block_put(b);
[cca29e3c]273 if (rc != EOK)
274 return rc;
[0f57d0e]275 }
276
277 if (o >= pos)
[cca29e3c]278 return EOK;
[0f57d0e]279
280 /* zero out the initial part of the new cluster chain */
[ed6cf34b]281 for (o = boundary; o < pos; o += BPS(bs)) {
[684b655]282 rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
[6da81e0]283 NULL, (o - boundary) / BPS(bs), BLOCK_FLAGS_NOREAD);
[cca29e3c]284 if (rc != EOK)
285 return rc;
[ed6cf34b]286 memset(b->data, 0, min(BPS(bs), pos - o));
[0f57d0e]287 b->dirty = true; /* need to sync node */
[c91f2d1b]288 rc = block_put(b);
[cca29e3c]289 if (rc != EOK)
290 return rc;
[0f57d0e]291 }
[cca29e3c]292
293 return EOK;
[0f57d0e]294}
295
[913a821c]296/** Get cluster from the first FAT.
297 *
298 * @param bs Buffer holding the boot sector for the file system.
299 * @param dev_handle Device handle for the file system.
300 * @param clst Cluster which to get.
[dc87ad11]301 * @param value Output argument holding the value of the cluster.
[913a821c]302 *
[dc87ad11]303 * @return EOK or a negative error code.
[913a821c]304 */
[dc87ad11]305int
[711e1f32]306fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
307 fat_cluster_t clst, fat_cluster_t *value)
[913a821c]308{
309 block_t *b;
[dc87ad11]310 fat_cluster_t *cp;
[c91f2d1b]311 int rc;
[913a821c]312
[ed6cf34b]313 rc = block_get(&b, dev_handle, RSCNT(bs) + SF(bs) * fatno +
314 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
[dc87ad11]315 if (rc != EOK)
316 return rc;
[ed6cf34b]317 cp = (fat_cluster_t *)b->data +
318 clst % (BPS(bs) / sizeof(fat_cluster_t));
[dc87ad11]319 *value = uint16_t_le2host(*cp);
[c91f2d1b]320 rc = block_put(b);
[913a821c]321
[dc87ad11]322 return rc;
[913a821c]323}
324
325/** Set cluster in one instance of FAT.
[9f95a80]326 *
327 * @param bs Buffer holding the boot sector for the file system.
328 * @param dev_handle Device handle for the file system.
329 * @param fatno Number of the FAT instance where to make the change.
[913a821c]330 * @param clst Cluster which is to be set.
331 * @param value Value to set the cluster with.
[cca29e3c]332 *
333 * @return EOK on success or a negative error code.
[9f95a80]334 */
[cca29e3c]335int
[913a821c]336fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
[cb682eb]337 fat_cluster_t clst, fat_cluster_t value)
[0f57d0e]338{
[cb682eb]339 block_t *b;
[23b56ca]340 fat_cluster_t *cp;
[c91f2d1b]341 int rc;
[23b56ca]342
[ed6cf34b]343 assert(fatno < FATCNT(bs));
344 rc = block_get(&b, dev_handle, RSCNT(bs) + SF(bs) * fatno +
345 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
[cca29e3c]346 if (rc != EOK)
347 return rc;
[ed6cf34b]348 cp = (fat_cluster_t *)b->data +
349 clst % (BPS(bs) / sizeof(fat_cluster_t));
[23b56ca]350 *cp = host2uint16_t_le(value);
[cb682eb]351 b->dirty = true; /* need to sync block */
[c91f2d1b]352 rc = block_put(b);
[cca29e3c]353 return rc;
[0f57d0e]354}
355
[9f95a80]356/** Replay the allocatoin of clusters in all shadow instances of FAT.
357 *
358 * @param bs Buffer holding the boot sector of the file system.
359 * @param dev_handle Device handle of the file system.
360 * @param lifo Chain of allocated clusters.
361 * @param nclsts Number of clusters in the lifo chain.
[cca29e3c]362 *
363 * @return EOK on success or a negative error code.
[9f95a80]364 */
[cca29e3c]365int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
[cb682eb]366 fat_cluster_t *lifo, unsigned nclsts)
[0f57d0e]367{
[b1178d0]368 uint8_t fatno;
369 unsigned c;
[cca29e3c]370 int rc;
[b1178d0]371
[cb682eb]372 for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
[b1178d0]373 for (c = 0; c < nclsts; c++) {
[cca29e3c]374 rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
[b1178d0]375 c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
[cca29e3c]376 if (rc != EOK)
377 return rc;
[b1178d0]378 }
379 }
[cca29e3c]380
381 return EOK;
[0f57d0e]382}
383
[e478b2a4]384/** Allocate clusters in all copies of FAT.
[9f95a80]385 *
386 * This function will attempt to allocate the requested number of clusters in
[e478b2a4]387 * all instances of the FAT. The FAT will be altered so that the allocated
[9f95a80]388 * clusters form an independent chain (i.e. a chain which does not belong to any
389 * file yet).
390 *
391 * @param bs Buffer holding the boot sector of the file system.
392 * @param dev_handle Device handle of the file system.
393 * @param nclsts Number of clusters to allocate.
394 * @param mcl Output parameter where the first cluster in the chain
395 * will be returned.
396 * @param lcl Output parameter where the last cluster in the chain
397 * will be returned.
398 *
399 * @return EOK on success, a negative error code otherwise.
400 */
[0f57d0e]401int
[cb682eb]402fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
403 fat_cluster_t *mcl, fat_cluster_t *lcl)
[0f57d0e]404{
[cb682eb]405 block_t *blk;
[0f57d0e]406 fat_cluster_t *lifo; /* stack for storing free cluster numbers */
407 unsigned found = 0; /* top of the free cluster number stack */
408 unsigned b, c, cl;
[c91f2d1b]409 int rc;
[0f57d0e]410
411 lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
[e478b2a4]412 if (!lifo)
[0f57d0e]413 return ENOMEM;
414
415 /*
416 * Search FAT1 for unused clusters.
417 */
[6ebe721]418 fibril_mutex_lock(&fat_alloc_lock);
[ed6cf34b]419 for (b = 0, cl = 0; b < SF(bs); b++) {
420 rc = block_get(&blk, dev_handle, RSCNT(bs) + b,
421 BLOCK_FLAGS_NONE);
[2f636b6]422 if (rc != EOK)
423 goto error;
[ed6cf34b]424 for (c = 0; c < BPS(bs) / sizeof(fat_cluster_t); c++, cl++) {
[3a8faba]425 /*
426 * Check if the cluster is physically there. This check
427 * becomes necessary when the file system is created
428 * with fewer total sectors than how many is inferred
429 * from the size of the file allocation table.
430 */
[ed6cf34b]431 if ((cl >= 2) &&
432 ((cl - 2) * SPC(bs) + SSA(bs) >= TS(bs))) {
[3a8faba]433 rc = block_put(blk);
434 if (rc != EOK)
435 goto error;
436 goto out;
437 }
438
[0f57d0e]439 fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
[a429bfb]440 if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
[0f57d0e]441 /*
442 * The cluster is free. Put it into our stack
443 * of found clusters and mark it as non-free.
444 */
445 lifo[found] = cl;
[a429bfb]446 *clst = (found == 0) ?
447 host2uint16_t_le(FAT_CLST_LAST1) :
448 host2uint16_t_le(lifo[found - 1]);
[0f57d0e]449 blk->dirty = true; /* need to sync block */
450 if (++found == nclsts) {
451 /* we are almost done */
[c91f2d1b]452 rc = block_put(blk);
[2f636b6]453 if (rc != EOK)
454 goto error;
[0f57d0e]455 /* update the shadow copies of FAT */
[cca29e3c]456 rc = fat_alloc_shadow_clusters(bs,
[cb682eb]457 dev_handle, lifo, nclsts);
[2f636b6]458 if (rc != EOK)
459 goto error;
[0f57d0e]460 *mcl = lifo[found - 1];
461 *lcl = lifo[0];
462 free(lifo);
[6ebe721]463 fibril_mutex_unlock(&fat_alloc_lock);
[0f57d0e]464 return EOK;
465 }
466 }
467 }
[c91f2d1b]468 rc = block_put(blk);
[2f636b6]469 if (rc != EOK) {
470error:
471 fibril_mutex_unlock(&fat_alloc_lock);
472 free(lifo);
473 return rc;
474 }
[0f57d0e]475 }
[3a8faba]476out:
[6ebe721]477 fibril_mutex_unlock(&fat_alloc_lock);
[0f57d0e]478
479 /*
480 * We could not find enough clusters. Now we need to free the clusters
481 * we have allocated so far.
482 */
[cb682eb]483 while (found--) {
[cca29e3c]484 rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
[cb682eb]485 FAT_CLST_RES0);
[cca29e3c]486 if (rc != EOK) {
487 free(lifo);
488 return rc;
489 }
[cb682eb]490 }
[0f57d0e]491
492 free(lifo);
493 return ENOSPC;
494}
495
[913a821c]496/** Free clusters forming a cluster chain in all copies of FAT.
497 *
498 * @param bs Buffer hodling the boot sector of the file system.
499 * @param dev_handle Device handle of the file system.
500 * @param firstc First cluster in the chain which is to be freed.
[cca29e3c]501 *
502 * @return EOK on success or a negative return code.
[913a821c]503 */
[cca29e3c]504int
[913a821c]505fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
506{
507 unsigned fatno;
508 fat_cluster_t nextc;
[dc87ad11]509 int rc;
[913a821c]510
511 /* Mark all clusters in the chain as free in all copies of FAT. */
512 while (firstc < FAT_CLST_LAST1) {
[d1b625b]513 assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
[711e1f32]514 rc = fat_get_cluster(bs, dev_handle, FAT1, firstc, &nextc);
[cca29e3c]515 if (rc != EOK)
516 return rc;
517 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
518 rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
[913a821c]519 FAT_CLST_RES0);
[cca29e3c]520 if (rc != EOK)
521 return rc;
522 }
523
[913a821c]524 firstc = nextc;
525 }
[cca29e3c]526
527 return EOK;
[913a821c]528}
529
[9f95a80]530/** Append a cluster chain to the last file cluster in all FATs.
531 *
[913a821c]532 * @param bs Buffer holding the boot sector of the file system.
[9f95a80]533 * @param nodep Node representing the file.
534 * @param mcl First cluster of the cluster chain to append.
[377cce8]535 * @param lcl Last cluster of the cluster chain to append.
[cca29e3c]536 *
537 * @return EOK on success or a negative error code.
[9f95a80]538 */
[377cce8]539int
540fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
541 fat_cluster_t lcl)
[0f57d0e]542{
[cb682eb]543 dev_handle_t dev_handle = nodep->idx->dev_handle;
[377cce8]544 fat_cluster_t lastc;
[e402382]545 uint16_t numc;
[cb682eb]546 uint8_t fatno;
[e402382]547 int rc;
548
[377cce8]549 if (nodep->lastc_cached_valid) {
550 lastc = nodep->lastc_cached_value;
551 nodep->lastc_cached_valid = false;
552 } else {
553 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lastc,
554 &numc, (uint16_t) -1);
555 if (rc != EOK)
556 return rc;
[e17d986]557
[377cce8]558 if (numc == 0) {
559 /* No clusters allocated to the node yet. */
560 nodep->firstc = mcl;
561 nodep->dirty = true; /* need to sync node */
562 return EOK;
563 }
[cca29e3c]564 }
565
566 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
[377cce8]567 rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lastc,
[cca29e3c]568 mcl);
569 if (rc != EOK)
570 return rc;
[e17d986]571 }
572
[377cce8]573 nodep->lastc_cached_valid = true;
574 nodep->lastc_cached_value = lcl;
575
[cca29e3c]576 return EOK;
[913a821c]577}
578
579/** Chop off node clusters in all copies of FAT.
580 *
581 * @param bs Buffer holding the boot sector of the file system.
582 * @param nodep FAT node where the chopping will take place.
[377cce8]583 * @param lcl Last cluster which will remain in the node. If this
[913a821c]584 * argument is FAT_CLST_RES0, then all clusters will
[ac49f5d1]585 * be chopped off.
[cca29e3c]586 *
587 * @return EOK on success or a negative return code.
[913a821c]588 */
[377cce8]589int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
[913a821c]590{
[dc87ad11]591 int rc;
[913a821c]592 dev_handle_t dev_handle = nodep->idx->dev_handle;
[377cce8]593
[dba4a23]594 /*
595 * Invalidate cached cluster numbers.
596 */
[377cce8]597 nodep->lastc_cached_valid = false;
[dba4a23]598 if (nodep->currc_cached_value != lcl)
599 nodep->currc_cached_valid = false;
600
[377cce8]601 if (lcl == FAT_CLST_RES0) {
[913a821c]602 /* The node will have zero size and no clusters allocated. */
[cca29e3c]603 rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
604 if (rc != EOK)
605 return rc;
[913a821c]606 nodep->firstc = FAT_CLST_RES0;
607 nodep->dirty = true; /* need to sync node */
608 } else {
609 fat_cluster_t nextc;
610 unsigned fatno;
611
[377cce8]612 rc = fat_get_cluster(bs, dev_handle, FAT1, lcl, &nextc);
[cca29e3c]613 if (rc != EOK)
614 return rc;
[913a821c]615
616 /* Terminate the cluster chain in all copies of FAT. */
[cca29e3c]617 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
[377cce8]618 rc = fat_set_cluster(bs, dev_handle, fatno, lcl,
[cca29e3c]619 FAT_CLST_LAST1);
620 if (rc != EOK)
621 return rc;
622 }
[913a821c]623
624 /* Free all following clusters. */
[cca29e3c]625 rc = fat_free_clusters(bs, dev_handle, nextc);
626 if (rc != EOK)
627 return rc;
[913a821c]628 }
[cca29e3c]629
[dba4a23]630 /*
631 * Update and re-enable the last cluster cache.
632 */
[377cce8]633 nodep->lastc_cached_valid = true;
634 nodep->lastc_cached_value = lcl;
635
[cca29e3c]636 return EOK;
[0f57d0e]637}
[6ebaff9]638
[cca29e3c]639int
[5f116e7]640fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
641{
642 int i;
643 block_t *b;
[c91f2d1b]644 int rc;
[5f116e7]645
[ed6cf34b]646 for (i = 0; i < SPC(bs); i++) {
[6da81e0]647 rc = _fat_block_get(&b, bs, dev_handle, c, NULL, i,
[684b655]648 BLOCK_FLAGS_NOREAD);
[cca29e3c]649 if (rc != EOK)
650 return rc;
[ed6cf34b]651 memset(b->data, 0, BPS(bs));
[5f116e7]652 b->dirty = true;
[c91f2d1b]653 rc = block_put(b);
[cca29e3c]654 if (rc != EOK)
655 return rc;
[5f116e7]656 }
[cca29e3c]657
658 return EOK;
[5f116e7]659}
660
[7efc517]661/** Perform basic sanity checks on the file system.
662 *
663 * Verify if values of boot sector fields are sane. Also verify media
664 * descriptor. This is used to rule out cases when a device obviously
665 * does not contain a fat file system.
666 */
667int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
668{
669 fat_cluster_t e0, e1;
670 unsigned fat_no;
671 int rc;
672
673 /* Check number of FATs. */
674 if (bs->fatcnt == 0)
675 return ENOTSUP;
676
677 /* Check total number of sectors. */
678
679 if (bs->totsec16 == 0 && bs->totsec32 == 0)
680 return ENOTSUP;
681
682 if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
683 bs->totsec16 != bs->totsec32)
684 return ENOTSUP;
685
686 /* Check media descriptor. Must be between 0xf0 and 0xff. */
687 if ((bs->mdesc & 0xf0) != 0xf0)
688 return ENOTSUP;
689
690 /* Check number of sectors per FAT. */
691 if (bs->sec_per_fat == 0)
692 return ENOTSUP;
693
694 /*
695 * Check that the root directory entries take up whole blocks.
696 * This check is rather strict, but it allows us to treat the root
697 * directory and non-root directories uniformly in some places.
698 * It can be removed provided that functions such as fat_read() are
699 * sanitized to support file systems with this property.
700 */
701 if ((uint16_t_le2host(bs->root_ent_max) * sizeof(fat_dentry_t)) %
702 uint16_t_le2host(bs->bps) != 0)
703 return ENOTSUP;
704
705 /* Check signature of each FAT. */
706
707 for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
708 rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
709 if (rc != EOK)
710 return EIO;
711
712 rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
713 if (rc != EOK)
714 return EIO;
715
716 /* Check that first byte of FAT contains the media descriptor. */
717 if ((e0 & 0xff) != bs->mdesc)
718 return ENOTSUP;
719
720 /*
721 * Check that remaining bits of the first two entries are
722 * set to one.
723 */
724 if ((e0 >> 8) != 0xff || e1 != 0xffff)
725 return ENOTSUP;
726 }
727
728 return EOK;
729}
730
[6ebaff9]731/**
732 * @}
733 */
Note: See TracBrowser for help on using the repository browser.