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

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

implement support for 64bit file offsets

  • the libc API is a small deviation from standard, but we have no reason to keep a strict backward compatibility with ancient code so far
    • the basic signed 64bit offset type is called off64_t
      • lseek() and fseek() take off64_t arguments (since the argument represents a relative offset)
      • ftell() returns off64_t values (since it is a wrapper of lseek())
      • vfs_seek() implementation supports negative offsets when SEEK_CUR and SEEK_END is used
    • aoff64_t is used for internal unsigned representation of sizes (in bytes, blocks, etc.) and absolute offsets
      • mmap() and ftruncate() take aoff64_t arguments (since the full range of the absolute file offset should be used here)
      • struct stat stores the file size as aoff64_t
    • in both cases the explicit range of the types shown in the names is helpful for proper filesystem and IPC interaction
    • note: size_t should be used only for representing in-memory sizes and offsets, not device and file-related information, and vice versa
      • the code base still needs a thorough revision with respect to this rule
    • PRIdOFF64 and PRIuOFF64 can be used for printing the offsets
  • VFS_OUT_LOOKUP returns the 64bit file size in two IPC arguments
    • since all 5 IPC arguments have already been taken, the fs_handle is returned as the return value (fs_handle has only 16 bits, thus the return value can be used for both indicating errors as negative values and returning positive handles)
  • VFS_OUT_READ and VFS_OUT_WRITE use aoff64_t absolute offsets split into two IPC arguments

replace bn_t with aoff64_t as a generic 64bit bytes/block counter type

note: filesystem drivers need to be revised with respect to make sure that all out-of-range checks are correct (especially w.r.t. file and block offsets)

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