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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1a86c50 was c7bbf029, checked in by Martin Decky <martin@…>, 14 years ago

improve stack traces and assertions
reduce header files pollution

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