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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17fa0280 was 17fa0280, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago

Fix fat_set_cluster_fat32. Now it is possible to do write on FAT32 fs
on both litle and big endian archs.

  • Property mode set to 100644
File size: 25.9 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 * @{
[97bc3ee]31 */
[6ebaff9]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 CLBN2PBN(bs, cl, bn) \
57 (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
58
[979c313a]59#define IS_ODD(number) (number & 0x1)
60
[0ec862d]61/**
[6ebe721]62 * The fat_alloc_lock mutex protects all copies of the File Allocation Table
[0ec862d]63 * during allocation of clusters. The lock does not have to be held durring
64 * deallocation of clusters.
[97bc3ee]65 */
[6ebe721]66static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
[0f57d0e]67
[4f1c0b4]68/** Walk the cluster chain.
[9f95a80]69 *
[4f1c0b4]70 * @param bs Buffer holding the boot sector for the file.
[991f645]71 * @param devmap_handle Device handle of the device with the file.
[4f1c0b4]72 * @param firstc First cluster to start the walk with.
[e402382]73 * @param lastc If non-NULL, output argument hodling the last cluster
74 * number visited.
75 * @param numc If non-NULL, output argument holding the number of
76 * clusters seen during the walk.
[97bc3ee]77 * @param max_clusters Maximum number of clusters to visit.
[9f95a80]78 *
[e402382]79 * @return EOK on success or a negative error code.
[9f95a80]80 */
[97bc3ee]81int
[991f645]82fat_cluster_walk(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc,
[e402382]83 fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
[0f57d0e]84{
[4f1c0b4]85 uint16_t clusters = 0;
[d260a95]86 fat_cluster_t clst = firstc, clst_last1 = FAT_CLST_LAST1(bs);
87 fat_cluster_t clst_bad = FAT_CLST_BAD(bs);
[c91f2d1b]88 int rc;
[0f57d0e]89
[4f1c0b4]90 if (firstc == FAT_CLST_RES0) {
91 /* No space allocated to the file. */
[6c8d267]92 if (lastc)
93 *lastc = firstc;
[e402382]94 if (numc)
95 *numc = 0;
96 return EOK;
[0f57d0e]97 }
98
[fc35e98]99 while (clst < clst_last1 && clusters < max_clusters) {
[4f1c0b4]100 assert(clst >= FAT_CLST_FIRST);
[6c8d267]101 if (lastc)
102 *lastc = clst; /* remember the last cluster number */
[fc35e98]103
[d260a95]104 /* read FAT1 */
105 rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &clst);
106 if (rc != EOK)
107 return rc;
[fc35e98]108
[d260a95]109 assert(clst != clst_bad);
110 clusters++;
[0f57d0e]111 }
112
[fc35e98]113 if (lastc && clst < clst_last1)
[6c8d267]114 *lastc = clst;
[e402382]115 if (numc)
116 *numc = clusters;
[0f57d0e]117
[e402382]118 return EOK;
[0f57d0e]119}
120
[746f623]121/** Read block from file located on a FAT file system.
122 *
123 * @param block Pointer to a block pointer for storing result.
124 * @param bs Buffer holding the boot sector of the file system.
125 * @param nodep FAT node.
126 * @param bn Block number.
127 * @param flags Flags passed to libblock.
128 *
129 * @return EOK on success or a negative error code.
130 */
131int
132fat_block_get(block_t **block, struct fat_bs *bs, fat_node_t *nodep,
133 aoff64_t bn, int flags)
134{
[dba4a23]135 fat_cluster_t firstc = nodep->firstc;
136 fat_cluster_t currc;
137 aoff64_t relbn = bn;
138 int rc;
139
[746f623]140 if (!nodep->size)
141 return ELIMIT;
142
[b5db2ae]143 if (!FAT_IS_FAT32(bs) && nodep->firstc == FAT_CLST_ROOT)
[ed6cf34b]144 goto fall_through;
145
146 if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
147 nodep->lastc_cached_valid) {
[746f623]148 /*
149 * This is a request to read a block within the last cluster
150 * when fortunately we have the last cluster number cached.
151 */
[991f645]152 return block_get(block, nodep->idx->devmap_handle,
[ed6cf34b]153 CLBN2PBN(bs, nodep->lastc_cached_value, bn), flags);
[746f623]154 }
[ed6cf34b]155
[dba4a23]156 if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
157 /*
158 * We can start with the cluster cached by the previous call to
159 * fat_block_get().
160 */
161 firstc = nodep->currc_cached_value;
162 relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
163 }
164
[ed6cf34b]165fall_through:
[991f645]166 rc = _fat_block_get(block, bs, nodep->idx->devmap_handle, firstc,
[dba4a23]167 &currc, relbn, flags);
168 if (rc != EOK)
169 return rc;
[97bc3ee]170
[dba4a23]171 /*
172 * Update the "current" cluster cache.
173 */
174 nodep->currc_cached_valid = true;
175 nodep->currc_cached_bn = bn;
176 nodep->currc_cached_value = currc;
177
178 return rc;
[746f623]179}
180
[4f1c0b4]181/** Read block from file located on a FAT file system.
[0f57d0e]182 *
[684b655]183 * @param block Pointer to a block pointer for storing result.
[4f1c0b4]184 * @param bs Buffer holding the boot sector of the file system.
[991f645]185 * @param devmap_handle Device handle of the file system.
[6da81e0]186 * @param fcl First cluster used by the file. Can be zero if the file
[4f1c0b4]187 * is empty.
[6da81e0]188 * @param clp If not NULL, address where the cluster containing bn
189 * will be stored.
[97bc3ee]190 * stored
[6c8d267]191 * @param bn Block number.
[1d8cdb1]192 * @param flags Flags passed to libblock.
[0f57d0e]193 *
[684b655]194 * @return EOK on success or a negative error code.
[0f57d0e]195 */
[684b655]196int
[991f645]197_fat_block_get(block_t **block, fat_bs_t *bs, devmap_handle_t devmap_handle,
[6da81e0]198 fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags)
[0f57d0e]199{
[e402382]200 uint16_t clusters;
201 unsigned max_clusters;
[6da81e0]202 fat_cluster_t c;
[c91f2d1b]203 int rc;
[0f57d0e]204
[5178ee2]205 /*
206 * This function can only operate on non-zero length files.
207 */
[6da81e0]208 if (fcl == FAT_CLST_RES0)
[5178ee2]209 return ELIMIT;
210
[b5db2ae]211 if (!FAT_IS_FAT32(bs) && fcl == FAT_CLST_ROOT) {
[4f1c0b4]212 /* root directory special case */
[ed6cf34b]213 assert(bn < RDS(bs));
[991f645]214 rc = block_get(block, devmap_handle,
[ed6cf34b]215 RSCNT(bs) + FATCNT(bs) * SF(bs) + bn, flags);
[684b655]216 return rc;
[0f57d0e]217 }
218
[ed6cf34b]219 max_clusters = bn / SPC(bs);
[991f645]220 rc = fat_cluster_walk(bs, devmap_handle, fcl, &c, &clusters, max_clusters);
[e402382]221 if (rc != EOK)
222 return rc;
[4f1c0b4]223 assert(clusters == max_clusters);
[0f57d0e]224
[991f645]225 rc = block_get(block, devmap_handle, CLBN2PBN(bs, c, bn), flags);
[6da81e0]226
227 if (clp)
228 *clp = c;
[0f57d0e]229
[684b655]230 return rc;
[0f57d0e]231}
232
233/** Fill the gap between EOF and a new file position.
234 *
[cb682eb]235 * @param bs Buffer holding the boot sector for nodep.
[0f57d0e]236 * @param nodep FAT node with the gap.
237 * @param mcl First cluster in an independent cluster chain that will
238 * be later appended to the end of the node's own cluster
239 * chain. If pos is still in the last allocated cluster,
240 * this argument is ignored.
241 * @param pos Position in the last node block.
[cca29e3c]242 *
243 * @return EOK on success or a negative error code.
[0f57d0e]244 */
[ed903174]245int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
[0f57d0e]246{
[cb682eb]247 block_t *b;
[ed903174]248 aoff64_t o, boundary;
[c91f2d1b]249 int rc;
[0f57d0e]250
[ed6cf34b]251 boundary = ROUND_UP(nodep->size, BPS(bs) * SPC(bs));
[0f57d0e]252
253 /* zero out already allocated space */
[abd36f7]254 for (o = nodep->size; o < pos && o < boundary;
[ed6cf34b]255 o = ALIGN_DOWN(o + BPS(bs), BPS(bs))) {
256 int flags = (o % BPS(bs) == 0) ?
[1d8cdb1]257 BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
[ed6cf34b]258 rc = fat_block_get(&b, bs, nodep, o / BPS(bs), flags);
[cca29e3c]259 if (rc != EOK)
260 return rc;
[ed6cf34b]261 memset(b->data + o % BPS(bs), 0, BPS(bs) - o % BPS(bs));
[0f57d0e]262 b->dirty = true; /* need to sync node */
[c91f2d1b]263 rc = block_put(b);
[cca29e3c]264 if (rc != EOK)
265 return rc;
[0f57d0e]266 }
[97bc3ee]267
[0f57d0e]268 if (o >= pos)
[cca29e3c]269 return EOK;
[97bc3ee]270
[0f57d0e]271 /* zero out the initial part of the new cluster chain */
[ed6cf34b]272 for (o = boundary; o < pos; o += BPS(bs)) {
[991f645]273 rc = _fat_block_get(&b, bs, nodep->idx->devmap_handle, mcl,
[6da81e0]274 NULL, (o - boundary) / BPS(bs), BLOCK_FLAGS_NOREAD);
[cca29e3c]275 if (rc != EOK)
276 return rc;
[ed6cf34b]277 memset(b->data, 0, min(BPS(bs), pos - o));
[0f57d0e]278 b->dirty = true; /* need to sync node */
[c91f2d1b]279 rc = block_put(b);
[cca29e3c]280 if (rc != EOK)
281 return rc;
[0f57d0e]282 }
[cca29e3c]283
284 return EOK;
[0f57d0e]285}
286
[88a27f1]287/** Get cluster from the first FAT. FAT12 version
[913a821c]288 *
289 * @param bs Buffer holding the boot sector for the file system.
[991f645]290 * @param devmap_handle Device handle for the file system.
[913a821c]291 * @param clst Cluster which to get.
[dc87ad11]292 * @param value Output argument holding the value of the cluster.
[913a821c]293 *
[dc87ad11]294 * @return EOK or a negative error code.
[913a821c]295 */
[dc87ad11]296int
[88a27f1]297fat_get_cluster_fat12(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
[711e1f32]298 fat_cluster_t clst, fat_cluster_t *value)
[913a821c]299{
[97bc3ee]300 block_t *b, *b1;
[0182e5cc]301 uint16_t byte1, byte2;
[d260a95]302 aoff64_t offset;
[c91f2d1b]303 int rc;
[913a821c]304
[88a27f1]305 offset = (clst + clst/2);
[b9060a83]306 if (offset / BPS(bs) >= SF(bs))
307 return ERANGE;
308
[d260a95]309 rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
310 offset / BPS(bs), BLOCK_FLAGS_NONE);
311 if (rc != EOK)
312 return rc;
313
[b9060a83]314 byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
[88a27f1]315 /* This cluster access spans a sector boundary. Check only for FAT12 */
[b9060a83]316 if ((offset % BPS(bs)) + 1 == BPS(bs)) {
[88a27f1]317 /* Is it last sector of FAT? */
318 if (offset / BPS(bs) < SF(bs)) {
319 /* No. Reading next sector */
320 rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
321 SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
322 if (rc != EOK) {
323 block_put(b);
324 return rc;
[d260a95]325 }
[88a27f1]326 /*
327 * Combining value with last byte of current sector and
328 * first byte of next sector
329 */
[0182e5cc]330 byte2 = ((uint8_t*) b1->data)[0];
[88a27f1]331
332 rc = block_put(b1);
333 if (rc != EOK) {
[d260a95]334 block_put(b);
[88a27f1]335 return rc;
[d260a95]336 }
337 }
[88a27f1]338 else {
339 /* Yes. It is last sector of FAT */
340 block_put(b);
341 return ERANGE;
342 }
[d260a95]343 }
[88a27f1]344 else
[b9060a83]345 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
[0182e5cc]346
347#ifdef __BE__
348 *value = byte2 | (byte1 << 8);
349#else
350 *value = byte1 | (byte2 << 8);
351#endif
[d260a95]352
[0182e5cc]353 *value = uint16_t_le2host(*value);
[88a27f1]354 if (IS_ODD(clst))
[b9060a83]355 *value = (*value) >> 4;
[88a27f1]356 else
[b9060a83]357 *value = (*value) & FAT12_MASK;
358
[c91f2d1b]359 rc = block_put(b);
[dc87ad11]360 return rc;
[913a821c]361}
362
[88a27f1]363/** Get cluster from the first FAT. FAT16 version
364 *
365 * @param bs Buffer holding the boot sector for the file system.
366 * @param devmap_handle Device handle for the file system.
367 * @param clst Cluster which to get.
368 * @param value Output argument holding the value of the cluster.
369 *
370 * @return EOK or a negative error code.
371 */
372int
373fat_get_cluster_fat16(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
374 fat_cluster_t clst, fat_cluster_t *value)
375{
376 block_t *b;
377 aoff64_t offset;
378 int rc;
379
380 offset = (clst * FAT16_CLST_SIZE);
381
382 rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
383 offset / BPS(bs), BLOCK_FLAGS_NONE);
384 if (rc != EOK)
385 return rc;
386
387 *value = uint16_t_le2host(*(uint16_t *)(b->data + offset % BPS(bs)));
388
389 rc = block_put(b);
390
391 return rc;
392}
393
394/** Get cluster from the first FAT. FAT32 version
395 *
396 * @param bs Buffer holding the boot sector for the file system.
397 * @param devmap_handle Device handle for the file system.
398 * @param clst Cluster which to get.
399 * @param value Output argument holding the value of the cluster.
400 *
401 * @return EOK or a negative error code.
402 */
403int
404fat_get_cluster_fat32(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
405 fat_cluster_t clst, fat_cluster_t *value)
406{
407 block_t *b;
408 aoff64_t offset;
409 int rc;
410
411 offset = (clst * FAT32_CLST_SIZE);
412
413 rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
414 offset / BPS(bs), BLOCK_FLAGS_NONE);
415 if (rc != EOK)
416 return rc;
417
[0182e5cc]418 *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) & FAT32_MASK;
[88a27f1]419
420 rc = block_put(b);
421
422 return rc;
423}
424
425
426/** Get cluster from the first FAT.
427 *
428 * @param bs Buffer holding the boot sector for the file system.
429 * @param devmap_handle Device handle for the file system.
430 * @param clst Cluster which to get.
431 * @param value Output argument holding the value of the cluster.
432 *
433 * @return EOK or a negative error code.
434 */
435int
436fat_get_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
437 fat_cluster_t clst, fat_cluster_t *value)
438{
439 int rc;
440
441 assert(fatno < FATCNT(bs));
442
[0182e5cc]443 if (FAT_IS_FAT12(bs)) {
[88a27f1]444 rc = fat_get_cluster_fat12(bs, devmap_handle, fatno, clst, value);
[0182e5cc]445 }
446 else {
447 if (FAT_IS_FAT32(bs))
448 rc = fat_get_cluster_fat32(bs, devmap_handle, fatno, clst, value);
449 else
450 rc = fat_get_cluster_fat16(bs, devmap_handle, fatno, clst, value);
451 }
[88a27f1]452
453 return rc;
454}
455
456/** Set cluster in one instance of FAT. FAT12 version.
[9f95a80]457 *
458 * @param bs Buffer holding the boot sector for the file system.
[991f645]459 * @param devmap_handle Device handle for the file system.
[9f95a80]460 * @param fatno Number of the FAT instance where to make the change.
[913a821c]461 * @param clst Cluster which is to be set.
462 * @param value Value to set the cluster with.
[cca29e3c]463 *
464 * @return EOK on success or a negative error code.
[9f95a80]465 */
[cca29e3c]466int
[88a27f1]467fat_set_cluster_fat12(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
[cb682eb]468 fat_cluster_t clst, fat_cluster_t value)
[0f57d0e]469{
[a8c14aa]470 block_t *b, *b1=NULL;
[d260a95]471 aoff64_t offset;
[0182e5cc]472 uint16_t byte1, byte2;
[c91f2d1b]473 int rc;
[fc35e98]474
[88a27f1]475 offset = (clst + clst/2);
[b9060a83]476 if (offset / BPS(bs) >= SF(bs))
477 return ERANGE;
478
[d260a95]479 rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
480 offset / BPS(bs), BLOCK_FLAGS_NONE);
[cca29e3c]481 if (rc != EOK)
482 return rc;
[fc35e98]483
[b9060a83]484 byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
[88a27f1]485 bool border = false;
486 /* This cluster access spans a sector boundary. Check only for FAT12 */
[b9060a83]487 if ((offset % BPS(bs))+1 == BPS(bs)) {
[88a27f1]488 /* Is it last sector of FAT? */
489 if (offset / BPS(bs) < SF(bs)) {
490 /* No. Reading next sector */
491 rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
492 SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
493 if (rc != EOK) {
[d260a95]494 block_put(b);
[88a27f1]495 return rc;
[d260a95]496 }
[88a27f1]497 /*
[b9060a83]498 * Combining value with last byte of current sector and
499 * first byte of next sector
500 */
[0182e5cc]501 byte2 = ((uint8_t*) b1->data)[0];
[88a27f1]502 border = true;
[d260a95]503 }
504 else {
[88a27f1]505 /* Yes. It is last sector of fat */
506 block_put(b);
507 return ERANGE;
[d260a95]508 }
[88a27f1]509 }
510 else
[b9060a83]511 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
[d260a95]512
[88a27f1]513 if (IS_ODD(clst)) {
[0182e5cc]514 byte1 &= 0x0f;
515 byte2 = 0;
516 value = (value << 4);
517 } else {
518 byte1 = 0;
519 byte2 &= 0xf0;
520 value &= FAT12_MASK;
[a8c14aa]521 }
522
[0182e5cc]523 byte1 = byte1 | (value & 0xff);
524 byte2 = byte2 | (value >> 8);
525
[b9060a83]526 ((uint8_t*) b->data)[(offset % BPS(bs))] = byte1;
[88a27f1]527 if (border) {
[0182e5cc]528 ((uint8_t*) b1->data)[0] = byte2;
[88a27f1]529
[0182e5cc]530 b1->dirty = true;
[a8c14aa]531 rc = block_put(b1);
532 if (rc != EOK) {
533 block_put(b);
534 return rc;
[d260a95]535 }
[0182e5cc]536 } else
[b9060a83]537 ((uint8_t*) b->data)[(offset % BPS(bs))+1] = byte2;
[d260a95]538
539 b->dirty = true; /* need to sync block */
[c91f2d1b]540 rc = block_put(b);
[cca29e3c]541 return rc;
[0f57d0e]542}
543
[88a27f1]544/** Set cluster in one instance of FAT. FAT16 version.
545 *
546 * @param bs Buffer holding the boot sector for the file system.
547 * @param devmap_handle Device handle for the file system.
548 * @param fatno Number of the FAT instance where to make the change.
549 * @param clst Cluster which is to be set.
550 * @param value Value to set the cluster with.
551 *
552 * @return EOK on success or a negative error code.
553 */
554int
555fat_set_cluster_fat16(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
556 fat_cluster_t clst, fat_cluster_t value)
557{
558 block_t *b;
559 aoff64_t offset;
560 int rc;
561
562 offset = (clst * FAT16_CLST_SIZE);
563
564 rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
565 offset / BPS(bs), BLOCK_FLAGS_NONE);
566 if (rc != EOK)
567 return rc;
568
[0182e5cc]569 *(uint16_t *)(b->data + offset % BPS(bs)) = host2uint16_t_le(value);
[88a27f1]570
571 b->dirty = true; /* need to sync block */
572 rc = block_put(b);
573 return rc;
574}
575
576/** Set cluster in one instance of FAT. FAT32 version.
577 *
578 * @param bs Buffer holding the boot sector for the file system.
579 * @param devmap_handle Device handle for the file system.
580 * @param fatno Number of the FAT instance where to make the change.
581 * @param clst Cluster which is to be set.
582 * @param value Value to set the cluster with.
583 *
584 * @return EOK on success or a negative error code.
585 */
586int
587fat_set_cluster_fat32(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
588 fat_cluster_t clst, fat_cluster_t value)
589{
590 block_t *b;
591 aoff64_t offset;
592 int rc;
[17fa0280]593 fat_cluster_t temp;
[88a27f1]594
595 offset = (clst * FAT32_CLST_SIZE);
596
597 rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
598 offset / BPS(bs), BLOCK_FLAGS_NONE);
599 if (rc != EOK)
600 return rc;
601
[17fa0280]602 temp = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs)));
603 temp &= 0xf0000000;
604 temp |= (value & FAT32_MASK);
605 *(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(temp);
[88a27f1]606
607 b->dirty = true; /* need to sync block */
608 rc = block_put(b);
609 return rc;
610}
611
612/** Set cluster in one instance of FAT.
613 *
614 * @param bs Buffer holding the boot sector for the file system.
615 * @param devmap_handle Device handle for the file system.
616 * @param fatno Number of the FAT instance where to make the change.
617 * @param clst Cluster which is to be set.
618 * @param value Value to set the cluster with.
619 *
620 * @return EOK on success or a negative error code.
621 */
622int
623fat_set_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
624 fat_cluster_t clst, fat_cluster_t value)
625{
626 int rc;
627
628 assert(fatno < FATCNT(bs));
629
630 if (FAT_IS_FAT12(bs))
631 rc = fat_set_cluster_fat12(bs, devmap_handle, fatno, clst, value);
632 else if (FAT_IS_FAT32(bs))
633 rc = fat_set_cluster_fat32(bs, devmap_handle, fatno, clst, value);
634 else
635 rc = fat_set_cluster_fat16(bs, devmap_handle, fatno, clst, value);
636
637 return rc;
638}
639
[9f95a80]640/** Replay the allocatoin of clusters in all shadow instances of FAT.
641 *
642 * @param bs Buffer holding the boot sector of the file system.
[991f645]643 * @param devmap_handle Device handle of the file system.
[9f95a80]644 * @param lifo Chain of allocated clusters.
645 * @param nclsts Number of clusters in the lifo chain.
[cca29e3c]646 *
647 * @return EOK on success or a negative error code.
[9f95a80]648 */
[991f645]649int fat_alloc_shadow_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle,
[cb682eb]650 fat_cluster_t *lifo, unsigned nclsts)
[0f57d0e]651{
[b1178d0]652 uint8_t fatno;
653 unsigned c;
[d260a95]654 fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
[cca29e3c]655 int rc;
[b1178d0]656
[0a51029f]657 for (fatno = FAT1 + 1; fatno < FATCNT(bs); fatno++) {
[b1178d0]658 for (c = 0; c < nclsts; c++) {
[991f645]659 rc = fat_set_cluster(bs, devmap_handle, fatno, lifo[c],
[fc35e98]660 c == 0 ? clst_last1 : lifo[c - 1]);
[cca29e3c]661 if (rc != EOK)
662 return rc;
[b1178d0]663 }
664 }
[cca29e3c]665
666 return EOK;
[0f57d0e]667}
668
[e478b2a4]669/** Allocate clusters in all copies of FAT.
[9f95a80]670 *
671 * This function will attempt to allocate the requested number of clusters in
[e478b2a4]672 * all instances of the FAT. The FAT will be altered so that the allocated
[9f95a80]673 * clusters form an independent chain (i.e. a chain which does not belong to any
674 * file yet).
675 *
676 * @param bs Buffer holding the boot sector of the file system.
[991f645]677 * @param devmap_handle Device handle of the file system.
[9f95a80]678 * @param nclsts Number of clusters to allocate.
679 * @param mcl Output parameter where the first cluster in the chain
680 * will be returned.
681 * @param lcl Output parameter where the last cluster in the chain
682 * will be returned.
683 *
684 * @return EOK on success, a negative error code otherwise.
685 */
[0f57d0e]686int
[991f645]687fat_alloc_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned nclsts,
[cb682eb]688 fat_cluster_t *mcl, fat_cluster_t *lcl)
[0f57d0e]689{
[d260a95]690 fat_cluster_t *lifo; /* stack for storing free cluster numbers */
691 unsigned found = 0; /* top of the free cluster number stack */
692 fat_cluster_t clst, value, clst_last1 = FAT_CLST_LAST1(bs);
693 int rc = EOK;
694
695 lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
696 if (!lifo)
697 return ENOMEM;
698 /*
699 * Search FAT1 for unused clusters.
700 */
701 fibril_mutex_lock(&fat_alloc_lock);
702 for (clst=FAT_CLST_FIRST; clst < CC(bs)+2 && found < nclsts; clst++) {
703 rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &value);
704 if (rc != EOK)
705 break;
706
707 if (value == FAT_CLST_RES0) {
708 /*
709 * The cluster is free. Put it into our stack
710 * of found clusters and mark it as non-free.
711 */
712 lifo[found] = clst;
713 rc = fat_set_cluster(bs, devmap_handle, FAT1, clst,
714 (found == 0) ? clst_last1 : lifo[found - 1]);
715 if (rc != EOK)
716 break;
717
718 found++;
719 }
720 }
721
722 if (rc == EOK && found == nclsts) {
723 rc = fat_alloc_shadow_clusters(bs, devmap_handle, lifo, nclsts);
724 if (rc == EOK) {
725 *mcl = lifo[found - 1];
726 *lcl = lifo[0];
727 free(lifo);
728 fibril_mutex_unlock(&fat_alloc_lock);
729 return EOK;
730 }
731 }
732
733 /* If something wrong - free the clusters */
734 if (found > 0) {
735 while (found--) {
736 rc = fat_set_cluster(bs, devmap_handle, FAT1, lifo[found],
737 FAT_CLST_RES0);
738 }
739 }
740
741 free(lifo);
742 fibril_mutex_unlock(&fat_alloc_lock);
743 return ENOSPC;
[0f57d0e]744}
745
[913a821c]746/** Free clusters forming a cluster chain in all copies of FAT.
747 *
748 * @param bs Buffer hodling the boot sector of the file system.
[991f645]749 * @param devmap_handle Device handle of the file system.
[913a821c]750 * @param firstc First cluster in the chain which is to be freed.
[cca29e3c]751 *
752 * @return EOK on success or a negative return code.
[913a821c]753 */
[cca29e3c]754int
[991f645]755fat_free_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc)
[913a821c]756{
757 unsigned fatno;
[d260a95]758 fat_cluster_t nextc, clst_bad = FAT_CLST_BAD(bs);
[dc87ad11]759 int rc;
[913a821c]760
761 /* Mark all clusters in the chain as free in all copies of FAT. */
[d260a95]762 while (firstc < FAT_CLST_LAST1(bs)) {
[fc35e98]763 assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad);
[991f645]764 rc = fat_get_cluster(bs, devmap_handle, FAT1, firstc, &nextc);
[cca29e3c]765 if (rc != EOK)
766 return rc;
[0a51029f]767 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
[991f645]768 rc = fat_set_cluster(bs, devmap_handle, fatno, firstc,
[913a821c]769 FAT_CLST_RES0);
[cca29e3c]770 if (rc != EOK)
771 return rc;
772 }
773
[913a821c]774 firstc = nextc;
775 }
[cca29e3c]776
777 return EOK;
[913a821c]778}
779
[9f95a80]780/** Append a cluster chain to the last file cluster in all FATs.
781 *
[913a821c]782 * @param bs Buffer holding the boot sector of the file system.
[9f95a80]783 * @param nodep Node representing the file.
784 * @param mcl First cluster of the cluster chain to append.
[377cce8]785 * @param lcl Last cluster of the cluster chain to append.
[cca29e3c]786 *
787 * @return EOK on success or a negative error code.
[9f95a80]788 */
[377cce8]789int
790fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
791 fat_cluster_t lcl)
[0f57d0e]792{
[991f645]793 devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
[377cce8]794 fat_cluster_t lastc;
[cb682eb]795 uint8_t fatno;
[e402382]796 int rc;
797
[db4ec8d]798 if (nodep->firstc == FAT_CLST_RES0) {
799 /* No clusters allocated to the node yet. */
800 nodep->firstc = mcl;
801 nodep->dirty = true; /* need to sync node */
[377cce8]802 } else {
[db4ec8d]803 if (nodep->lastc_cached_valid) {
804 lastc = nodep->lastc_cached_value;
805 nodep->lastc_cached_valid = false;
806 } else {
[991f645]807 rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
[db4ec8d]808 &lastc, NULL, (uint16_t) -1);
809 if (rc != EOK)
810 return rc;
[377cce8]811 }
[cca29e3c]812
[0a51029f]813 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
[d260a95]814 rc = fat_set_cluster(bs, nodep->idx->devmap_handle,
815 fatno, lastc, mcl);
[db4ec8d]816 if (rc != EOK)
817 return rc;
818 }
[e17d986]819 }
820
[377cce8]821 nodep->lastc_cached_valid = true;
822 nodep->lastc_cached_value = lcl;
823
[cca29e3c]824 return EOK;
[913a821c]825}
826
827/** Chop off node clusters in all copies of FAT.
828 *
829 * @param bs Buffer holding the boot sector of the file system.
830 * @param nodep FAT node where the chopping will take place.
[377cce8]831 * @param lcl Last cluster which will remain in the node. If this
[913a821c]832 * argument is FAT_CLST_RES0, then all clusters will
[ac49f5d1]833 * be chopped off.
[cca29e3c]834 *
835 * @return EOK on success or a negative return code.
[913a821c]836 */
[377cce8]837int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
[913a821c]838{
[d260a95]839 fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
[dc87ad11]840 int rc;
[991f645]841 devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
[377cce8]842
[dba4a23]843 /*
844 * Invalidate cached cluster numbers.
845 */
[377cce8]846 nodep->lastc_cached_valid = false;
[dba4a23]847 if (nodep->currc_cached_value != lcl)
848 nodep->currc_cached_valid = false;
849
[377cce8]850 if (lcl == FAT_CLST_RES0) {
[913a821c]851 /* The node will have zero size and no clusters allocated. */
[991f645]852 rc = fat_free_clusters(bs, devmap_handle, nodep->firstc);
[cca29e3c]853 if (rc != EOK)
854 return rc;
[913a821c]855 nodep->firstc = FAT_CLST_RES0;
856 nodep->dirty = true; /* need to sync node */
857 } else {
858 fat_cluster_t nextc;
859 unsigned fatno;
860
[991f645]861 rc = fat_get_cluster(bs, devmap_handle, FAT1, lcl, &nextc);
[cca29e3c]862 if (rc != EOK)
863 return rc;
[913a821c]864
865 /* Terminate the cluster chain in all copies of FAT. */
[0a51029f]866 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
[991f645]867 rc = fat_set_cluster(bs, devmap_handle, fatno, lcl,
[fc35e98]868 clst_last1);
[cca29e3c]869 if (rc != EOK)
870 return rc;
871 }
[913a821c]872
873 /* Free all following clusters. */
[991f645]874 rc = fat_free_clusters(bs, devmap_handle, nextc);
[cca29e3c]875 if (rc != EOK)
876 return rc;
[913a821c]877 }
[cca29e3c]878
[dba4a23]879 /*
880 * Update and re-enable the last cluster cache.
881 */
[377cce8]882 nodep->lastc_cached_valid = true;
883 nodep->lastc_cached_value = lcl;
884
[cca29e3c]885 return EOK;
[0f57d0e]886}
[6ebaff9]887
[cca29e3c]888int
[991f645]889fat_zero_cluster(struct fat_bs *bs, devmap_handle_t devmap_handle, fat_cluster_t c)
[5f116e7]890{
891 int i;
892 block_t *b;
[c91f2d1b]893 int rc;
[5f116e7]894
[ed6cf34b]895 for (i = 0; i < SPC(bs); i++) {
[991f645]896 rc = _fat_block_get(&b, bs, devmap_handle, c, NULL, i,
[684b655]897 BLOCK_FLAGS_NOREAD);
[cca29e3c]898 if (rc != EOK)
899 return rc;
[ed6cf34b]900 memset(b->data, 0, BPS(bs));
[5f116e7]901 b->dirty = true;
[c91f2d1b]902 rc = block_put(b);
[cca29e3c]903 if (rc != EOK)
904 return rc;
[5f116e7]905 }
[cca29e3c]906
907 return EOK;
[5f116e7]908}
909
[7efc517]910/** Perform basic sanity checks on the file system.
911 *
912 * Verify if values of boot sector fields are sane. Also verify media
913 * descriptor. This is used to rule out cases when a device obviously
914 * does not contain a fat file system.
915 */
[991f645]916int fat_sanity_check(fat_bs_t *bs, devmap_handle_t devmap_handle)
[7efc517]917{
918 fat_cluster_t e0, e1;
919 unsigned fat_no;
920 int rc;
921
922 /* Check number of FATs. */
[875edff6]923 if (FATCNT(bs) == 0)
[7efc517]924 return ENOTSUP;
925
926 /* Check total number of sectors. */
[875edff6]927 if (TS(bs) == 0)
[7efc517]928 return ENOTSUP;
929
930 if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
[97bc3ee]931 bs->totsec16 != bs->totsec32)
[7efc517]932 return ENOTSUP;
933
934 /* Check media descriptor. Must be between 0xf0 and 0xff. */
935 if ((bs->mdesc & 0xf0) != 0xf0)
936 return ENOTSUP;
937
938 /* Check number of sectors per FAT. */
[875edff6]939 if (SF(bs) == 0)
[7efc517]940 return ENOTSUP;
941
942 /*
943 * Check that the root directory entries take up whole blocks.
944 * This check is rather strict, but it allows us to treat the root
945 * directory and non-root directories uniformly in some places.
946 * It can be removed provided that functions such as fat_read() are
947 * sanitized to support file systems with this property.
948 */
[875edff6]949 if (!FAT_IS_FAT32(bs) && (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0)
[7efc517]950 return ENOTSUP;
951
952 /* Check signature of each FAT. */
[875edff6]953 for (fat_no = 0; fat_no < FATCNT(bs); fat_no++) {
[991f645]954 rc = fat_get_cluster(bs, devmap_handle, fat_no, 0, &e0);
[7efc517]955 if (rc != EOK)
956 return EIO;
957
[991f645]958 rc = fat_get_cluster(bs, devmap_handle, fat_no, 1, &e1);
[7efc517]959 if (rc != EOK)
960 return EIO;
961
962 /* Check that first byte of FAT contains the media descriptor. */
963 if ((e0 & 0xff) != bs->mdesc)
964 return ENOTSUP;
965
966 /*
967 * Check that remaining bits of the first two entries are
968 * set to one.
969 */
[aa2ea13]970 if (!FAT_IS_FAT12(bs) &&
971 ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs)))
[7efc517]972 return ENOTSUP;
973 }
974
975 return EOK;
976}
977
[6ebaff9]978/**
979 * @}
[97bc3ee]980 */
Note: See TracBrowser for help on using the repository browser.