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
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 uint16_t byte1, byte2;
302 aoff64_t offset;
303 int rc;
304
305 offset = (clst + clst/2);
306 if (offset / BPS(bs) >= SF(bs))
307 return ERANGE;
308
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
314 byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
315 /* This cluster access spans a sector boundary. Check only for FAT12 */
316 if ((offset % BPS(bs)) + 1 == BPS(bs)) {
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;
325 }
326 /*
327 * Combining value with last byte of current sector and
328 * first byte of next sector
329 */
330 byte2 = ((uint8_t*) b1->data)[0];
331
332 rc = block_put(b1);
333 if (rc != EOK) {
334 block_put(b);
335 return rc;
336 }
337 }
338 else {
339 /* Yes. It is last sector of FAT */
340 block_put(b);
341 return ERANGE;
342 }
343 }
344 else
345 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
346
347#ifdef __BE__
348 *value = byte2 | (byte1 << 8);
349#else
350 *value = byte1 | (byte2 << 8);
351#endif
352
353 *value = uint16_t_le2host(*value);
354 if (IS_ODD(clst))
355 *value = (*value) >> 4;
356 else
357 *value = (*value) & FAT12_MASK;
358
359 rc = block_put(b);
360 return rc;
361}
362
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
418 *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) & FAT32_MASK;
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
443 if (FAT_IS_FAT12(bs)) {
444 rc = fat_get_cluster_fat12(bs, devmap_handle, fatno, clst, value);
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 }
452
453 return rc;
454}
455
456/** Set cluster in one instance of FAT. FAT12 version.
457 *
458 * @param bs Buffer holding the boot sector for the file system.
459 * @param devmap_handle Device handle for the file system.
460 * @param fatno Number of the FAT instance where to make the change.
461 * @param clst Cluster which is to be set.
462 * @param value Value to set the cluster with.
463 *
464 * @return EOK on success or a negative error code.
465 */
466int
467fat_set_cluster_fat12(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
468 fat_cluster_t clst, fat_cluster_t value)
469{
470 block_t *b, *b1=NULL;
471 aoff64_t offset;
472 uint16_t byte1, byte2;
473 int rc;
474
475 offset = (clst + clst/2);
476 if (offset / BPS(bs) >= SF(bs))
477 return ERANGE;
478
479 rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
480 offset / BPS(bs), BLOCK_FLAGS_NONE);
481 if (rc != EOK)
482 return rc;
483
484 byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
485 bool border = false;
486 /* This cluster access spans a sector boundary. Check only for FAT12 */
487 if ((offset % BPS(bs))+1 == BPS(bs)) {
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) {
494 block_put(b);
495 return rc;
496 }
497 /*
498 * Combining value with last byte of current sector and
499 * first byte of next sector
500 */
501 byte2 = ((uint8_t*) b1->data)[0];
502 border = true;
503 }
504 else {
505 /* Yes. It is last sector of fat */
506 block_put(b);
507 return ERANGE;
508 }
509 }
510 else
511 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
512
513 if (IS_ODD(clst)) {
514 byte1 &= 0x0f;
515 byte2 = 0;
516 value = (value << 4);
517 } else {
518 byte1 = 0;
519 byte2 &= 0xf0;
520 value &= FAT12_MASK;
521 }
522
523 byte1 = byte1 | (value & 0xff);
524 byte2 = byte2 | (value >> 8);
525
526 ((uint8_t*) b->data)[(offset % BPS(bs))] = byte1;
527 if (border) {
528 ((uint8_t*) b1->data)[0] = byte2;
529
530 b1->dirty = true;
531 rc = block_put(b1);
532 if (rc != EOK) {
533 block_put(b);
534 return rc;
535 }
536 } else
537 ((uint8_t*) b->data)[(offset % BPS(bs))+1] = byte2;
538
539 b->dirty = true; /* need to sync block */
540 rc = block_put(b);
541 return rc;
542}
543
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
569 *(uint16_t *)(b->data + offset % BPS(bs)) = host2uint16_t_le(value);
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;
593 fat_cluster_t temp;
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
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);
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
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.
643 * @param devmap_handle Device handle of the file system.
644 * @param lifo Chain of allocated clusters.
645 * @param nclsts Number of clusters in the lifo chain.
646 *
647 * @return EOK on success or a negative error code.
648 */
649int fat_alloc_shadow_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle,
650 fat_cluster_t *lifo, unsigned nclsts)
651{
652 uint8_t fatno;
653 unsigned c;
654 fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
655 int rc;
656
657 for (fatno = FAT1 + 1; fatno < FATCNT(bs); fatno++) {
658 for (c = 0; c < nclsts; c++) {
659 rc = fat_set_cluster(bs, devmap_handle, fatno, lifo[c],
660 c == 0 ? clst_last1 : lifo[c - 1]);
661 if (rc != EOK)
662 return rc;
663 }
664 }
665
666 return EOK;
667}
668
669/** Allocate clusters in all copies of FAT.
670 *
671 * This function will attempt to allocate the requested number of clusters in
672 * all instances of the FAT. The FAT will be altered so that the allocated
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.
677 * @param devmap_handle Device handle of the file system.
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 */
686int
687fat_alloc_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned nclsts,
688 fat_cluster_t *mcl, fat_cluster_t *lcl)
689{
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;
744}
745
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.
749 * @param devmap_handle Device handle of the file system.
750 * @param firstc First cluster in the chain which is to be freed.
751 *
752 * @return EOK on success or a negative return code.
753 */
754int
755fat_free_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc)
756{
757 unsigned fatno;
758 fat_cluster_t nextc, clst_bad = FAT_CLST_BAD(bs);
759 int rc;
760
761 /* Mark all clusters in the chain as free in all copies of FAT. */
762 while (firstc < FAT_CLST_LAST1(bs)) {
763 assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad);
764 rc = fat_get_cluster(bs, devmap_handle, FAT1, firstc, &nextc);
765 if (rc != EOK)
766 return rc;
767 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
768 rc = fat_set_cluster(bs, devmap_handle, fatno, firstc,
769 FAT_CLST_RES0);
770 if (rc != EOK)
771 return rc;
772 }
773
774 firstc = nextc;
775 }
776
777 return EOK;
778}
779
780/** Append a cluster chain to the last file cluster in all FATs.
781 *
782 * @param bs Buffer holding the boot sector of the file system.
783 * @param nodep Node representing the file.
784 * @param mcl First cluster of the cluster chain to append.
785 * @param lcl Last cluster of the cluster chain to append.
786 *
787 * @return EOK on success or a negative error code.
788 */
789int
790fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
791 fat_cluster_t lcl)
792{
793 devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
794 fat_cluster_t lastc;
795 uint8_t fatno;
796 int rc;
797
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 */
802 } else {
803 if (nodep->lastc_cached_valid) {
804 lastc = nodep->lastc_cached_value;
805 nodep->lastc_cached_valid = false;
806 } else {
807 rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
808 &lastc, NULL, (uint16_t) -1);
809 if (rc != EOK)
810 return rc;
811 }
812
813 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
814 rc = fat_set_cluster(bs, nodep->idx->devmap_handle,
815 fatno, lastc, mcl);
816 if (rc != EOK)
817 return rc;
818 }
819 }
820
821 nodep->lastc_cached_valid = true;
822 nodep->lastc_cached_value = lcl;
823
824 return EOK;
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.
831 * @param lcl Last cluster which will remain in the node. If this
832 * argument is FAT_CLST_RES0, then all clusters will
833 * be chopped off.
834 *
835 * @return EOK on success or a negative return code.
836 */
837int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
838{
839 fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
840 int rc;
841 devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
842
843 /*
844 * Invalidate cached cluster numbers.
845 */
846 nodep->lastc_cached_valid = false;
847 if (nodep->currc_cached_value != lcl)
848 nodep->currc_cached_valid = false;
849
850 if (lcl == FAT_CLST_RES0) {
851 /* The node will have zero size and no clusters allocated. */
852 rc = fat_free_clusters(bs, devmap_handle, nodep->firstc);
853 if (rc != EOK)
854 return rc;
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
861 rc = fat_get_cluster(bs, devmap_handle, FAT1, lcl, &nextc);
862 if (rc != EOK)
863 return rc;
864
865 /* Terminate the cluster chain in all copies of FAT. */
866 for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
867 rc = fat_set_cluster(bs, devmap_handle, fatno, lcl,
868 clst_last1);
869 if (rc != EOK)
870 return rc;
871 }
872
873 /* Free all following clusters. */
874 rc = fat_free_clusters(bs, devmap_handle, nextc);
875 if (rc != EOK)
876 return rc;
877 }
878
879 /*
880 * Update and re-enable the last cluster cache.
881 */
882 nodep->lastc_cached_valid = true;
883 nodep->lastc_cached_value = lcl;
884
885 return EOK;
886}
887
888int
889fat_zero_cluster(struct fat_bs *bs, devmap_handle_t devmap_handle, fat_cluster_t c)
890{
891 int i;
892 block_t *b;
893 int rc;
894
895 for (i = 0; i < SPC(bs); i++) {
896 rc = _fat_block_get(&b, bs, devmap_handle, c, NULL, i,
897 BLOCK_FLAGS_NOREAD);
898 if (rc != EOK)
899 return rc;
900 memset(b->data, 0, BPS(bs));
901 b->dirty = true;
902 rc = block_put(b);
903 if (rc != EOK)
904 return rc;
905 }
906
907 return EOK;
908}
909
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 */
916int fat_sanity_check(fat_bs_t *bs, devmap_handle_t devmap_handle)
917{
918 fat_cluster_t e0, e1;
919 unsigned fat_no;
920 int rc;
921
922 /* Check number of FATs. */
923 if (FATCNT(bs) == 0)
924 return ENOTSUP;
925
926 /* Check total number of sectors. */
927 if (TS(bs) == 0)
928 return ENOTSUP;
929
930 if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
931 bs->totsec16 != bs->totsec32)
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. */
939 if (SF(bs) == 0)
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 */
949 if (!FAT_IS_FAT32(bs) && (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0)
950 return ENOTSUP;
951
952 /* Check signature of each FAT. */
953 for (fat_no = 0; fat_no < FATCNT(bs); fat_no++) {
954 rc = fat_get_cluster(bs, devmap_handle, fat_no, 0, &e0);
955 if (rc != EOK)
956 return EIO;
957
958 rc = fat_get_cluster(bs, devmap_handle, fat_no, 1, &e1);
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 */
970 if (!FAT_IS_FAT12(bs) &&
971 ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs)))
972 return ENOTSUP;
973 }
974
975 return EOK;
976}
977
978/**
979 * @}
980 */
Note: See TracBrowser for help on using the repository browser.