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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 88a27f1 was 88a27f1, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago
  1. Split fat_get_cluster into 3 methods: fat_get_cluster_fat[12,16,32]

and fat_set_cluster to fat_set_cluster_fat[12,16,32]

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