source: mainline/uspace/srv/fs/exfat/exfat_fat.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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