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
RevLine 
[4c3c4a5]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
[b1834a01]30/** @addtogroup exfat
[4c3c4a5]31 * @{
32 */
33
34/**
35 * @file exfat_fat.c
[5d5863c]36 * @brief Functions that manipulate the File Allocation Table.
[4c3c4a5]37 */
38
39#include "exfat_fat.h"
[a9b756e]40#include "exfat_bitmap.h"
[4c3c4a5]41#include "exfat.h"
42#include "../../vfs/vfs.h"
43#include <libfs.h>
[f73b291]44#include <block.h>
[4c3c4a5]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>
[38d150e]51#include <stdlib.h>
[1776895]52#include <str.h>
[4c3c4a5]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 */
[f3d4cd35]59static FIBRIL_MUTEX_INITIALIZE(exfat_alloc_lock);
[4c3c4a5]60
[c223ee2]61/** Walk the cluster chain.
62 *
63 * @param bs Buffer holding the boot sector for the file.
[375ab5e]64 * @param service_id Service ID of the device with the file.
[c223ee2]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 *
[cde999a]72 * @return EOK on success or an error code.
[c223ee2]73 */
[b7fd2a0]74errno_t
[1b20da0]75exfat_cluster_walk(exfat_bs_t *bs, service_id_t service_id,
[c223ee2]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;
[b7fd2a0]81 errno_t rc;
[c223ee2]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
[f3d4cd35]92 while (clst != EXFAT_CLST_EOF && clusters < max_clusters) {
[c223ee2]93 assert(clst >= EXFAT_CLST_FIRST);
94 if (lastc)
95 *lastc = clst; /* remember the last cluster number */
96
[375ab5e]97 rc = exfat_get_cluster(bs, service_id, clst, &clst);
[c223ee2]98 if (rc != EOK)
99 return rc;
100
101 assert(clst != EXFAT_CLST_BAD);
102 clusters++;
103 }
104
[f3d4cd35]105 if (lastc && clst != EXFAT_CLST_EOF)
[c223ee2]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 *
[cde999a]121 * @return EOK on success or an error code.
[c223ee2]122 */
[b7fd2a0]123errno_t
[c223ee2]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;
[aaa77ba0]128 exfat_cluster_t currc = 0;
[c223ee2]129 aoff64_t relbn = bn;
[b7fd2a0]130 errno_t rc;
[c223ee2]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)) &&
[0dbe5ac]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 */
[1b20da0]142 return block_get(block, nodep->idx->service_id, DATA_FS(bs) +
[18b6a88]143 (nodep->lastc_cached_value - EXFAT_CLST_FIRST) * SPC(bs) +
[c223ee2]144 (bn % SPC(bs)), flags);
145 }
146
147 if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
148 /*
[ae7d03c]149 * We can start with the cluster cached by the previous call to
150 * fat_block_get().
151 */
[c223ee2]152 firstc = nodep->currc_cached_value;
153 relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
154 }
155 }
156
[375ab5e]157 rc = exfat_block_get_by_clst(block, bs, nodep->idx->service_id,
[c223ee2]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
[5d5863c]172/** Read block from file located on a exFAT file system.
[c223ee2]173 *
174 * @param block Pointer to a block pointer for storing result.
175 * @param bs Buffer holding the boot sector of the file system.
[375ab5e]176 * @param service_id Service ID of the file system.
[c223ee2]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 *
[cde999a]185 * @return EOK on success or an error code.
[c223ee2]186 */
[b7fd2a0]187errno_t
[1b20da0]188exfat_block_get_by_clst(block_t **block, exfat_bs_t *bs,
[375ab5e]189 service_id_t service_id, bool fragmented, exfat_cluster_t fcl,
[c223ee2]190 exfat_cluster_t *clp, aoff64_t bn, int flags)
191{
192 uint32_t clusters;
193 uint32_t max_clusters;
[db8626d]194 exfat_cluster_t c = EXFAT_CLST_FIRST;
[b7fd2a0]195 errno_t rc;
[c223ee2]196
[0dbe5ac]197 if (fcl < EXFAT_CLST_FIRST || fcl > DATA_CNT(bs) + 2)
[c223ee2]198 return ELIMIT;
199
200 if (!fragmented) {
[1b20da0]201 rc = block_get(block, service_id, DATA_FS(bs) +
[18b6a88]202 (fcl - EXFAT_CLST_FIRST) * SPC(bs) + bn, flags);
[c223ee2]203 } else {
204 max_clusters = bn / SPC(bs);
[375ab5e]205 rc = exfat_cluster_walk(bs, service_id, fcl, &c, &clusters, max_clusters);
[c223ee2]206 if (rc != EOK)
207 return rc;
208 assert(clusters == max_clusters);
209
[1b20da0]210 rc = block_get(block, service_id, DATA_FS(bs) +
[0dbe5ac]211 (c - EXFAT_CLST_FIRST) * SPC(bs) + (bn % SPC(bs)), flags);
[c223ee2]212
213 if (clp)
214 *clp = c;
215 }
216
217 return rc;
218}
219
[9f98578]220/** Get cluster from the FAT.
221 *
222 * @param bs Buffer holding the boot sector for the file system.
[375ab5e]223 * @param service_id Service ID for the file system.
[9f98578]224 * @param clst Cluster which to get.
225 * @param value Output argument holding the value of the cluster.
226 *
[cde999a]227 * @return EOK or an error code.
[9f98578]228 */
[b7fd2a0]229errno_t
[375ab5e]230exfat_get_cluster(exfat_bs_t *bs, service_id_t service_id,
[9f98578]231 exfat_cluster_t clst, exfat_cluster_t *value)
232{
233 block_t *b;
234 aoff64_t offset;
[b7fd2a0]235 errno_t rc;
[9f98578]236
237 offset = clst * sizeof(exfat_cluster_t);
238
[375ab5e]239 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
[9f98578]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.
[375ab5e]253 * @param service_id Service ID for the file system.
[9f98578]254 * @param clst Cluster which is to be set.
255 * @param value Value to set the cluster with.
256 *
[cde999a]257 * @return EOK on success or an error code.
[9f98578]258 */
[b7fd2a0]259errno_t
[375ab5e]260exfat_set_cluster(exfat_bs_t *bs, service_id_t service_id,
[9f98578]261 exfat_cluster_t clst, exfat_cluster_t value)
262{
263 block_t *b;
264 aoff64_t offset;
[b7fd2a0]265 errno_t rc;
[9f98578]266
267 offset = clst * sizeof(exfat_cluster_t);
268
[375ab5e]269 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
[9f98578]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}
[4c3c4a5]279
[f3d4cd35]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.
[375ab5e]288 * @param service_id Service ID of the file system.
[f3d4cd35]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 *
[cde999a]295 * @return EOK on success, an error code otherwise.
[f3d4cd35]296 */
[b7fd2a0]297errno_t
[375ab5e]298exfat_alloc_clusters(exfat_bs_t *bs, service_id_t service_id, unsigned nclsts,
[f3d4cd35]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 */
[a9b756e]303 exfat_cluster_t clst;
[b7fd2a0]304 errno_t rc = EOK;
[f3d4cd35]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);
[0dbe5ac]311 for (clst = EXFAT_CLST_FIRST; clst < DATA_CNT(bs) + 2 && found < nclsts;
312 clst++) {
[a9b756e]313 /* Need to rewrite because of multiple exfat_bitmap_get calls */
[e738d56]314 if (exfat_bitmap_is_free(bs, service_id, clst) == EOK) {
[0dbe5ac]315 /*
316 * The cluster is free. Put it into our stack
317 * of found clusters and mark it as non-free.
318 */
[f3d4cd35]319 lifo[found] = clst;
[375ab5e]320 rc = exfat_set_cluster(bs, service_id, clst,
[0dbe5ac]321 (found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]);
[f3d4cd35]322 if (rc != EOK)
[071ff5fa]323 goto exit_error;
[f3d4cd35]324 found++;
[e738d56]325 rc = exfat_bitmap_set_cluster(bs, service_id, clst);
[a9b756e]326 if (rc != EOK)
[071ff5fa]327 goto exit_error;
[a9b756e]328
[f3d4cd35]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
[071ff5fa]340 rc = ENOSPC;
341
342exit_error:
343
[f3d4cd35]344 /* If something wrong - free the clusters */
[0dbe5ac]345 while (found--) {
[e738d56]346 (void) exfat_bitmap_clear_cluster(bs, service_id, lifo[found]);
[0dbe5ac]347 (void) exfat_set_cluster(bs, service_id, lifo[found], 0);
[f3d4cd35]348 }
349
350 free(lifo);
351 fibril_mutex_unlock(&exfat_alloc_lock);
[071ff5fa]352 return rc;
[f3d4cd35]353}
354
355/** Free clusters forming a cluster chain in FAT.
356 *
357 * @param bs Buffer hodling the boot sector of the file system.
[375ab5e]358 * @param service_id Service ID of the file system.
[f3d4cd35]359 * @param firstc First cluster in the chain which is to be freed.
360 *
[cde999a]361 * @return EOK on success or an error code.
[f3d4cd35]362 */
[b7fd2a0]363errno_t
[375ab5e]364exfat_free_clusters(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t firstc)
[f3d4cd35]365{
366 exfat_cluster_t nextc;
[b7fd2a0]367 errno_t rc;
[f3d4cd35]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);
[375ab5e]372 rc = exfat_get_cluster(bs, service_id, firstc, &nextc);
[f3d4cd35]373 if (rc != EOK)
374 return rc;
[375ab5e]375 rc = exfat_set_cluster(bs, service_id, firstc, 0);
[ae560da]376 if (rc != EOK)
377 return rc;
[e738d56]378 rc = exfat_bitmap_clear_cluster(bs, service_id, firstc);
[f3d4cd35]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 *
[cde999a]394 * @return EOK on success or an error code.
[f3d4cd35]395 */
[b7fd2a0]396errno_t
[f3d4cd35]397exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
398 exfat_cluster_t lcl)
399{
[375ab5e]400 service_id_t service_id = nodep->idx->service_id;
[10092c9a]401 exfat_cluster_t lastc = 0;
[b7fd2a0]402 errno_t rc;
[f3d4cd35]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 {
[375ab5e]413 rc = exfat_cluster_walk(bs, service_id, nodep->firstc,
[f3d4cd35]414 &lastc, NULL, (uint16_t) -1);
415 if (rc != EOK)
416 return rc;
417 }
418
[375ab5e]419 rc = exfat_set_cluster(bs, nodep->idx->service_id, lastc, mcl);
[f3d4cd35]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 *
[cde999a]438 * @return EOK on success or an error code.
[f3d4cd35]439 */
[b7fd2a0]440errno_t exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
[f3d4cd35]441{
[b7fd2a0]442 errno_t rc;
[375ab5e]443 service_id_t service_id = nodep->idx->service_id;
[f3d4cd35]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. */
[375ab5e]454 rc = exfat_free_clusters(bs, service_id, nodep->firstc);
[f3d4cd35]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
[375ab5e]462 rc = exfat_get_cluster(bs, service_id, lcl, &nextc);
[f3d4cd35]463 if (rc != EOK)
464 return rc;
465
466 /* Terminate the cluster chain */
[375ab5e]467 rc = exfat_set_cluster(bs, service_id, lcl, EXFAT_CLST_EOF);
[f3d4cd35]468 if (rc != EOK)
469 return rc;
470
471 /* Free all following clusters. */
[375ab5e]472 rc = exfat_free_clusters(bs, service_id, nextc);
[f3d4cd35]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
[b7fd2a0]486errno_t
[375ab5e]487exfat_zero_cluster(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t c)
[8a06c1b]488{
489 size_t i;
490 block_t *b;
[b7fd2a0]491 errno_t rc;
[8a06c1b]492
493 for (i = 0; i < SPC(bs); i++) {
[0dbe5ac]494 rc = exfat_block_get_by_clst(&b, bs, service_id, false, c, NULL,
495 i, BLOCK_FLAGS_NOREAD);
[8a06c1b]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
[b7fd2a0]508errno_t
[e8976b59]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;
[b7fd2a0]513 errno_t rc;
[18b6a88]514 blocks = ROUND_UP(nodep->size, BPS(bs)) / BPS(bs);
[e8976b59]515 count = BPS(bs);
[a35b458]516
[e8976b59]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;
[0dbe5ac]521 if (i == blocks - 1)
522 count = nodep->size - i * BPS(bs);
[e8976b59]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
[4c3c4a5]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 */
[b7fd2a0]539errno_t exfat_sanity_check(exfat_bs_t *bs)
[4c3c4a5]540{
[1776895]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 }
[4c3c4a5]555 return EOK;
556}
557
558/**
559 * @}
560 */
Note: See TracBrowser for help on using the repository browser.