source: mainline/uspace/srv/fs/exfat/exfat_fat.c@ 1b20da0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1b20da0 was 1b20da0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on non-empty lines, in certain file types.

Command used: tools/srepl '\([^[:space:]]\)\s\+$' '\1' -- *.c *.h *.py *.sh *.s *.S *.ag

  • Property mode set to 100644
File size: 14.9 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * Copyright (c) 2011 Oleg Romanenko
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup fs
31 * @{
32 */
33
34/**
35 * @file exfat_fat.c
36 * @brief Functions that manipulate the File Allocation Table.
37 */
38
39#include "exfat_fat.h"
40#include "exfat_bitmap.h"
41#include "exfat.h"
42#include "../../vfs/vfs.h"
43#include <libfs.h>
44#include <block.h>
45#include <errno.h>
46#include <byteorder.h>
47#include <align.h>
48#include <assert.h>
49#include <fibril_synch.h>
50#include <mem.h>
51#include <stdlib.h>
52#include <str.h>
53
54
55/**
56 * The fat_alloc_lock mutex protects all copies of the File Allocation Table
57 * during allocation of clusters. The lock does not have to be held durring
58 * deallocation of clusters.
59 */
60static FIBRIL_MUTEX_INITIALIZE(exfat_alloc_lock);
61
62/** Walk the cluster chain.
63 *
64 * @param bs Buffer holding the boot sector for the file.
65 * @param service_id Service ID of the device with the file.
66 * @param firstc First cluster to start the walk with.
67 * @param lastc If non-NULL, output argument hodling the last cluster
68 * number visited.
69 * @param numc If non-NULL, output argument holding the number of
70 * clusters seen during the walk.
71 * @param max_clusters Maximum number of clusters to visit.
72 *
73 * @return EOK on success or an error code.
74 */
75errno_t
76exfat_cluster_walk(exfat_bs_t *bs, service_id_t service_id,
77 exfat_cluster_t firstc, exfat_cluster_t *lastc, uint32_t *numc,
78 uint32_t max_clusters)
79{
80 uint32_t clusters = 0;
81 exfat_cluster_t clst = firstc;
82 errno_t rc;
83
84 if (firstc < EXFAT_CLST_FIRST) {
85 /* No space allocated to the file. */
86 if (lastc)
87 *lastc = firstc;
88 if (numc)
89 *numc = 0;
90 return EOK;
91 }
92
93 while (clst != EXFAT_CLST_EOF && clusters < max_clusters) {
94 assert(clst >= EXFAT_CLST_FIRST);
95 if (lastc)
96 *lastc = clst; /* remember the last cluster number */
97
98 rc = exfat_get_cluster(bs, service_id, clst, &clst);
99 if (rc != EOK)
100 return rc;
101
102 assert(clst != EXFAT_CLST_BAD);
103 clusters++;
104 }
105
106 if (lastc && clst != EXFAT_CLST_EOF)
107 *lastc = clst;
108 if (numc)
109 *numc = clusters;
110
111 return EOK;
112}
113
114/** Read block from file located on a exFAT file system.
115 *
116 * @param block Pointer to a block pointer for storing result.
117 * @param bs Buffer holding the boot sector of the file system.
118 * @param nodep FAT node.
119 * @param bn Block number.
120 * @param flags Flags passed to libblock.
121 *
122 * @return EOK on success or an error code.
123 */
124errno_t
125exfat_block_get(block_t **block, exfat_bs_t *bs, exfat_node_t *nodep,
126 aoff64_t bn, int flags)
127{
128 exfat_cluster_t firstc = nodep->firstc;
129 exfat_cluster_t currc = 0;
130 aoff64_t relbn = bn;
131 errno_t rc;
132
133 if (!nodep->size)
134 return ELIMIT;
135
136 if (nodep->fragmented) {
137 if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
138 nodep->lastc_cached_valid) {
139 /*
140 * This is a request to read a block within the last cluster
141 * when fortunately we have the last cluster number cached.
142 */
143 return block_get(block, nodep->idx->service_id, DATA_FS(bs) +
144 (nodep->lastc_cached_value-EXFAT_CLST_FIRST)*SPC(bs) +
145 (bn % SPC(bs)), flags);
146 }
147
148 if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
149 /*
150 * We can start with the cluster cached by the previous call to
151 * fat_block_get().
152 */
153 firstc = nodep->currc_cached_value;
154 relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
155 }
156 }
157
158 rc = exfat_block_get_by_clst(block, bs, nodep->idx->service_id,
159 nodep->fragmented, firstc, &currc, relbn, flags);
160 if (rc != EOK)
161 return rc;
162
163 /*
164 * Update the "current" cluster cache.
165 */
166 nodep->currc_cached_valid = true;
167 nodep->currc_cached_bn = bn;
168 nodep->currc_cached_value = currc;
169
170 return rc;
171}
172
173/** Read block from file located on a exFAT file system.
174 *
175 * @param block Pointer to a block pointer for storing result.
176 * @param bs Buffer holding the boot sector of the file system.
177 * @param service_id Service ID of the file system.
178 * @param fcl First cluster used by the file. Can be zero if the file
179 * is empty.
180 * @param clp If not NULL, address where the cluster containing bn
181 * will be stored.
182 * stored
183 * @param bn Block number.
184 * @param flags Flags passed to libblock.
185 *
186 * @return EOK on success or an error code.
187 */
188errno_t
189exfat_block_get_by_clst(block_t **block, exfat_bs_t *bs,
190 service_id_t service_id, bool fragmented, exfat_cluster_t fcl,
191 exfat_cluster_t *clp, aoff64_t bn, int flags)
192{
193 uint32_t clusters;
194 uint32_t max_clusters;
195 exfat_cluster_t c = EXFAT_CLST_FIRST;
196 errno_t rc;
197
198 if (fcl < EXFAT_CLST_FIRST || fcl > DATA_CNT(bs) + 2)
199 return ELIMIT;
200
201 if (!fragmented) {
202 rc = block_get(block, service_id, DATA_FS(bs) +
203 (fcl - EXFAT_CLST_FIRST)*SPC(bs) + bn, flags);
204 } else {
205 max_clusters = bn / SPC(bs);
206 rc = exfat_cluster_walk(bs, service_id, fcl, &c, &clusters, max_clusters);
207 if (rc != EOK)
208 return rc;
209 assert(clusters == max_clusters);
210
211 rc = block_get(block, service_id, DATA_FS(bs) +
212 (c - EXFAT_CLST_FIRST) * SPC(bs) + (bn % SPC(bs)), flags);
213
214 if (clp)
215 *clp = c;
216 }
217
218 return rc;
219}
220
221
222/** Get cluster from the FAT.
223 *
224 * @param bs Buffer holding the boot sector for the file system.
225 * @param service_id Service ID for the file system.
226 * @param clst Cluster which to get.
227 * @param value Output argument holding the value of the cluster.
228 *
229 * @return EOK or an error code.
230 */
231errno_t
232exfat_get_cluster(exfat_bs_t *bs, service_id_t service_id,
233 exfat_cluster_t clst, exfat_cluster_t *value)
234{
235 block_t *b;
236 aoff64_t offset;
237 errno_t rc;
238
239 offset = clst * sizeof(exfat_cluster_t);
240
241 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
242 if (rc != EOK)
243 return rc;
244
245 *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs)));
246
247 rc = block_put(b);
248
249 return rc;
250}
251
252/** Set cluster in FAT.
253 *
254 * @param bs Buffer holding the boot sector for the file system.
255 * @param service_id Service ID for the file system.
256 * @param clst Cluster which is to be set.
257 * @param value Value to set the cluster with.
258 *
259 * @return EOK on success or an error code.
260 */
261errno_t
262exfat_set_cluster(exfat_bs_t *bs, service_id_t service_id,
263 exfat_cluster_t clst, exfat_cluster_t value)
264{
265 block_t *b;
266 aoff64_t offset;
267 errno_t rc;
268
269 offset = clst * sizeof(exfat_cluster_t);
270
271 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
272 if (rc != EOK)
273 return rc;
274
275 *(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(value);
276
277 b->dirty = true; /* need to sync block */
278 rc = block_put(b);
279 return rc;
280}
281
282/** Allocate clusters in FAT.
283 *
284 * This function will attempt to allocate the requested number of clusters in
285 * the FAT. The FAT will be altered so that the allocated
286 * clusters form an independent chain (i.e. a chain which does not belong to any
287 * file yet).
288 *
289 * @param bs Buffer holding the boot sector of the file system.
290 * @param service_id Service ID of the file system.
291 * @param nclsts Number of clusters to allocate.
292 * @param mcl Output parameter where the first cluster in the chain
293 * will be returned.
294 * @param lcl Output parameter where the last cluster in the chain
295 * will be returned.
296 *
297 * @return EOK on success, an error code otherwise.
298 */
299errno_t
300exfat_alloc_clusters(exfat_bs_t *bs, service_id_t service_id, unsigned nclsts,
301 exfat_cluster_t *mcl, exfat_cluster_t *lcl)
302{
303 exfat_cluster_t *lifo; /* stack for storing free cluster numbers */
304 unsigned found = 0; /* top of the free cluster number stack */
305 exfat_cluster_t clst;
306 errno_t rc = EOK;
307
308 lifo = (exfat_cluster_t *) malloc(nclsts * sizeof(exfat_cluster_t));
309 if (!lifo)
310 return ENOMEM;
311
312 fibril_mutex_lock(&exfat_alloc_lock);
313 for (clst = EXFAT_CLST_FIRST; clst < DATA_CNT(bs) + 2 && found < nclsts;
314 clst++) {
315 /* Need to rewrite because of multiple exfat_bitmap_get calls */
316 if (exfat_bitmap_is_free(bs, service_id, clst) == EOK) {
317 /*
318 * The cluster is free. Put it into our stack
319 * of found clusters and mark it as non-free.
320 */
321 lifo[found] = clst;
322 rc = exfat_set_cluster(bs, service_id, clst,
323 (found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]);
324 if (rc != EOK)
325 goto exit_error;
326 found++;
327 rc = exfat_bitmap_set_cluster(bs, service_id, clst);
328 if (rc != EOK)
329 goto exit_error;
330
331 }
332 }
333
334 if (rc == EOK && found == nclsts) {
335 *mcl = lifo[found - 1];
336 *lcl = lifo[0];
337 free(lifo);
338 fibril_mutex_unlock(&exfat_alloc_lock);
339 return EOK;
340 }
341
342 rc = ENOSPC;
343
344exit_error:
345
346 /* If something wrong - free the clusters */
347 while (found--) {
348 (void) exfat_bitmap_clear_cluster(bs, service_id, lifo[found]);
349 (void) exfat_set_cluster(bs, service_id, lifo[found], 0);
350 }
351
352 free(lifo);
353 fibril_mutex_unlock(&exfat_alloc_lock);
354 return rc;
355}
356
357/** Free clusters forming a cluster chain in FAT.
358 *
359 * @param bs Buffer hodling the boot sector of the file system.
360 * @param service_id Service ID of the file system.
361 * @param firstc First cluster in the chain which is to be freed.
362 *
363 * @return EOK on success or an error code.
364 */
365errno_t
366exfat_free_clusters(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t firstc)
367{
368 exfat_cluster_t nextc;
369 errno_t rc;
370
371 /* Mark all clusters in the chain as free */
372 while (firstc != EXFAT_CLST_EOF) {
373 assert(firstc >= EXFAT_CLST_FIRST && firstc < EXFAT_CLST_BAD);
374 rc = exfat_get_cluster(bs, service_id, firstc, &nextc);
375 if (rc != EOK)
376 return rc;
377 rc = exfat_set_cluster(bs, service_id, firstc, 0);
378 if (rc != EOK)
379 return rc;
380 rc = exfat_bitmap_clear_cluster(bs, service_id, firstc);
381 if (rc != EOK)
382 return rc;
383 firstc = nextc;
384 }
385
386 return EOK;
387}
388
389/** Append a cluster chain to the last file cluster in FAT.
390 *
391 * @param bs Buffer holding the boot sector of the file system.
392 * @param nodep Node representing the file.
393 * @param mcl First cluster of the cluster chain to append.
394 * @param lcl Last cluster of the cluster chain to append.
395 *
396 * @return EOK on success or an error code.
397 */
398errno_t
399exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
400 exfat_cluster_t lcl)
401{
402 service_id_t service_id = nodep->idx->service_id;
403 exfat_cluster_t lastc = 0;
404 errno_t rc;
405
406 if (nodep->firstc == 0) {
407 /* No clusters allocated to the node yet. */
408 nodep->firstc = mcl;
409 nodep->dirty = true; /* need to sync node */
410 } else {
411 if (nodep->lastc_cached_valid) {
412 lastc = nodep->lastc_cached_value;
413 nodep->lastc_cached_valid = false;
414 } else {
415 rc = exfat_cluster_walk(bs, service_id, nodep->firstc,
416 &lastc, NULL, (uint16_t) -1);
417 if (rc != EOK)
418 return rc;
419 }
420
421 rc = exfat_set_cluster(bs, nodep->idx->service_id, lastc, mcl);
422 if (rc != EOK)
423 return rc;
424 }
425
426 nodep->lastc_cached_valid = true;
427 nodep->lastc_cached_value = lcl;
428
429 return EOK;
430}
431
432/** Chop off node clusters in FAT.
433 *
434 * @param bs Buffer holding the boot sector of the file system.
435 * @param nodep FAT node where the chopping will take place.
436 * @param lcl Last cluster which will remain in the node. If this
437 * argument is FAT_CLST_RES0, then all clusters will
438 * be chopped off.
439 *
440 * @return EOK on success or an error code.
441 */
442errno_t exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
443{
444 errno_t rc;
445 service_id_t service_id = nodep->idx->service_id;
446
447 /*
448 * Invalidate cached cluster numbers.
449 */
450 nodep->lastc_cached_valid = false;
451 if (nodep->currc_cached_value != lcl)
452 nodep->currc_cached_valid = false;
453
454 if (lcl == 0) {
455 /* The node will have zero size and no clusters allocated. */
456 rc = exfat_free_clusters(bs, service_id, nodep->firstc);
457 if (rc != EOK)
458 return rc;
459 nodep->firstc = 0;
460 nodep->dirty = true; /* need to sync node */
461 } else {
462 exfat_cluster_t nextc;
463
464 rc = exfat_get_cluster(bs, service_id, lcl, &nextc);
465 if (rc != EOK)
466 return rc;
467
468 /* Terminate the cluster chain */
469 rc = exfat_set_cluster(bs, service_id, lcl, EXFAT_CLST_EOF);
470 if (rc != EOK)
471 return rc;
472
473 /* Free all following clusters. */
474 rc = exfat_free_clusters(bs, service_id, nextc);
475 if (rc != EOK)
476 return rc;
477 }
478
479 /*
480 * Update and re-enable the last cluster cache.
481 */
482 nodep->lastc_cached_valid = true;
483 nodep->lastc_cached_value = lcl;
484
485 return EOK;
486}
487
488errno_t
489exfat_zero_cluster(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t c)
490{
491 size_t i;
492 block_t *b;
493 errno_t rc;
494
495 for (i = 0; i < SPC(bs); i++) {
496 rc = exfat_block_get_by_clst(&b, bs, service_id, false, c, NULL,
497 i, BLOCK_FLAGS_NOREAD);
498 if (rc != EOK)
499 return rc;
500 memset(b->data, 0, BPS(bs));
501 b->dirty = true;
502 rc = block_put(b);
503 if (rc != EOK)
504 return rc;
505 }
506
507 return EOK;
508}
509
510errno_t
511exfat_read_uctable(exfat_bs_t *bs, exfat_node_t *nodep, uint8_t *uctable)
512{
513 size_t i, blocks, count;
514 block_t *b;
515 errno_t rc;
516 blocks = ROUND_UP(nodep->size, BPS(bs))/BPS(bs);
517 count = BPS(bs);
518
519 for (i = 0; i < blocks; i++) {
520 rc = exfat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NOREAD);
521 if (rc != EOK)
522 return rc;
523 if (i == blocks - 1)
524 count = nodep->size - i * BPS(bs);
525 memcpy(uctable, b->data, count);
526 uctable += count;
527 rc = block_put(b);
528 if (rc != EOK)
529 return rc;
530 }
531
532 return EOK;
533}
534
535
536/** Perform basic sanity checks on the file system.
537 *
538 * Verify if values of boot sector fields are sane. Also verify media
539 * descriptor. This is used to rule out cases when a device obviously
540 * does not contain a exfat file system.
541 */
542errno_t exfat_sanity_check(exfat_bs_t *bs)
543{
544 if (str_cmp((char const *)bs->oem_name, "EXFAT "))
545 return ENOTSUP;
546 else if (uint16_t_le2host(bs->signature) != 0xAA55)
547 return ENOTSUP;
548 else if (uint32_t_le2host(bs->fat_sector_count) == 0)
549 return ENOTSUP;
550 else if (uint32_t_le2host(bs->data_clusters) == 0)
551 return ENOTSUP;
552 else if (bs->fat_count != 1)
553 return ENOTSUP;
554 else if ((bs->bytes_per_sector + bs->sec_per_cluster) > 25) {
555 /* exFAT does not support cluster size > 32 Mb */
556 return ENOTSUP;
557 }
558 return EOK;
559}
560
561/**
562 * @}
563 */
Note: See TracBrowser for help on using the repository browser.