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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a68a94e was 0dbe5ac, checked in by Jakub Jermar <jakub@…>, 14 years ago

Cstyle.

  • Property mode set to 100644
File size: 14.4 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
30/** @addtogroup fs
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>
44#include <libblock.h>
45#include <errno.h>
46#include <byteorder.h>
47#include <align.h>
48#include <assert.h>
49#include <fibril_synch.h>
50#include <malloc.h>
51#include <mem.h>
52
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 *
72 * @return EOK on success or a negative error code.
73 */
74int
[375ab5e]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;
81 int 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
[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 *
121 * @return EOK on success or a negative error code.
122 */
123int
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;
129 aoff64_t relbn = bn;
130 int 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)) &&
[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 */
[375ab5e]142 return block_get(block, nodep->idx->service_id, DATA_FS(bs) +
[c223ee2]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
[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 *
185 * @return EOK on success or a negative error code.
186 */
187int
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;
194 exfat_cluster_t c;
195 int rc;
196
[0dbe5ac]197 if (fcl < EXFAT_CLST_FIRST || fcl > DATA_CNT(bs) + 2)
[c223ee2]198 return ELIMIT;
199
200 if (!fragmented) {
[375ab5e]201 rc = block_get(block, service_id, DATA_FS(bs) +
[0dbe5ac]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
[375ab5e]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
[4c3c4a5]220
[9f98578]221/** Get cluster from the FAT.
222 *
223 * @param bs Buffer holding the boot sector for the file system.
[375ab5e]224 * @param service_id Service ID for the file system.
[9f98578]225 * @param clst Cluster which to get.
226 * @param value Output argument holding the value of the cluster.
227 *
228 * @return EOK or a negative error code.
229 */
230int
[375ab5e]231exfat_get_cluster(exfat_bs_t *bs, service_id_t service_id,
[9f98578]232 exfat_cluster_t clst, exfat_cluster_t *value)
233{
234 block_t *b;
235 aoff64_t offset;
236 int rc;
237
238 offset = clst * sizeof(exfat_cluster_t);
239
[375ab5e]240 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
[9f98578]241 if (rc != EOK)
242 return rc;
243
244 *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs)));
245
246 rc = block_put(b);
247
248 return rc;
249}
250
251/** Set cluster in FAT.
252 *
253 * @param bs Buffer holding the boot sector for the file system.
[375ab5e]254 * @param service_id Service ID for the file system.
[9f98578]255 * @param clst Cluster which is to be set.
256 * @param value Value to set the cluster with.
257 *
258 * @return EOK on success or a negative error code.
259 */
260int
[375ab5e]261exfat_set_cluster(exfat_bs_t *bs, service_id_t service_id,
[9f98578]262 exfat_cluster_t clst, exfat_cluster_t value)
263{
264 block_t *b;
265 aoff64_t offset;
266 int rc;
267
268 offset = clst * sizeof(exfat_cluster_t);
269
[375ab5e]270 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
[9f98578]271 if (rc != EOK)
272 return rc;
273
274 *(uint32_t *)(b->data + offset % BPS(bs)) = host2uint32_t_le(value);
275
276 b->dirty = true; /* need to sync block */
277 rc = block_put(b);
278 return rc;
279}
[4c3c4a5]280
[f3d4cd35]281/** Allocate clusters in FAT.
282 *
283 * This function will attempt to allocate the requested number of clusters in
284 * the FAT. The FAT will be altered so that the allocated
285 * clusters form an independent chain (i.e. a chain which does not belong to any
286 * file yet).
287 *
288 * @param bs Buffer holding the boot sector of the file system.
[375ab5e]289 * @param service_id Service ID of the file system.
[f3d4cd35]290 * @param nclsts Number of clusters to allocate.
291 * @param mcl Output parameter where the first cluster in the chain
292 * will be returned.
293 * @param lcl Output parameter where the last cluster in the chain
294 * will be returned.
295 *
296 * @return EOK on success, a negative error code otherwise.
297 */
298int
[375ab5e]299exfat_alloc_clusters(exfat_bs_t *bs, service_id_t service_id, unsigned nclsts,
[f3d4cd35]300 exfat_cluster_t *mcl, exfat_cluster_t *lcl)
301{
302 exfat_cluster_t *lifo; /* stack for storing free cluster numbers */
303 unsigned found = 0; /* top of the free cluster number stack */
[a9b756e]304 exfat_cluster_t clst;
[f3d4cd35]305 int rc = EOK;
306
307 lifo = (exfat_cluster_t *) malloc(nclsts * sizeof(exfat_cluster_t));
308 if (!lifo)
309 return ENOMEM;
310
311 fibril_mutex_lock(&exfat_alloc_lock);
[0dbe5ac]312 for (clst = EXFAT_CLST_FIRST; clst < DATA_CNT(bs) + 2 && found < nclsts;
313 clst++) {
[a9b756e]314 /* Need to rewrite because of multiple exfat_bitmap_get calls */
[0dbe5ac]315 if (bitmap_is_free(bs, service_id, clst) == EOK) {
316 /*
317 * The cluster is free. Put it into our stack
318 * of found clusters and mark it as non-free.
319 */
[f3d4cd35]320 lifo[found] = clst;
[375ab5e]321 rc = exfat_set_cluster(bs, service_id, clst,
[0dbe5ac]322 (found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]);
[f3d4cd35]323 if (rc != EOK)
324 break;
325 found++;
[375ab5e]326 rc = bitmap_set_cluster(bs, service_id, clst);
[a9b756e]327 if (rc != EOK)
328 break;
329
[f3d4cd35]330 }
331 }
332
333 if (rc == EOK && found == nclsts) {
334 *mcl = lifo[found - 1];
335 *lcl = lifo[0];
336 free(lifo);
337 fibril_mutex_unlock(&exfat_alloc_lock);
338 return EOK;
339 }
340
341 /* If something wrong - free the clusters */
[0dbe5ac]342 while (found--) {
343 (void) bitmap_clear_cluster(bs, service_id, lifo[found]);
344 (void) exfat_set_cluster(bs, service_id, lifo[found], 0);
[f3d4cd35]345 }
346
347 free(lifo);
348 fibril_mutex_unlock(&exfat_alloc_lock);
349 return ENOSPC;
350}
351
352/** Free clusters forming a cluster chain in FAT.
353 *
354 * @param bs Buffer hodling the boot sector of the file system.
[375ab5e]355 * @param service_id Service ID of the file system.
[f3d4cd35]356 * @param firstc First cluster in the chain which is to be freed.
357 *
358 * @return EOK on success or a negative return code.
359 */
360int
[375ab5e]361exfat_free_clusters(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t firstc)
[f3d4cd35]362{
363 exfat_cluster_t nextc;
364 int rc;
365
366 /* Mark all clusters in the chain as free */
367 while (firstc != EXFAT_CLST_EOF) {
368 assert(firstc >= EXFAT_CLST_FIRST && firstc < EXFAT_CLST_BAD);
[375ab5e]369 rc = exfat_get_cluster(bs, service_id, firstc, &nextc);
[f3d4cd35]370 if (rc != EOK)
371 return rc;
[375ab5e]372 rc = exfat_set_cluster(bs, service_id, firstc, 0);
[ae560da]373 if (rc != EOK)
374 return rc;
[375ab5e]375 rc = bitmap_clear_cluster(bs, service_id, firstc);
[f3d4cd35]376 if (rc != EOK)
377 return rc;
378 firstc = nextc;
379 }
380
381 return EOK;
382}
383
384/** Append a cluster chain to the last file cluster in FAT.
385 *
386 * @param bs Buffer holding the boot sector of the file system.
387 * @param nodep Node representing the file.
388 * @param mcl First cluster of the cluster chain to append.
389 * @param lcl Last cluster of the cluster chain to append.
390 *
391 * @return EOK on success or a negative error code.
392 */
393int
394exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
395 exfat_cluster_t lcl)
396{
[375ab5e]397 service_id_t service_id = nodep->idx->service_id;
[f3d4cd35]398 exfat_cluster_t lastc;
399 int rc;
400
401 if (nodep->firstc == 0) {
402 /* No clusters allocated to the node yet. */
403 nodep->firstc = mcl;
404 nodep->dirty = true; /* need to sync node */
405 } else {
406 if (nodep->lastc_cached_valid) {
407 lastc = nodep->lastc_cached_value;
408 nodep->lastc_cached_valid = false;
409 } else {
[375ab5e]410 rc = exfat_cluster_walk(bs, service_id, nodep->firstc,
[f3d4cd35]411 &lastc, NULL, (uint16_t) -1);
412 if (rc != EOK)
413 return rc;
414 }
415
[375ab5e]416 rc = exfat_set_cluster(bs, nodep->idx->service_id, lastc, mcl);
[f3d4cd35]417 if (rc != EOK)
418 return rc;
419 }
420
421 nodep->lastc_cached_valid = true;
422 nodep->lastc_cached_value = lcl;
423
424 return EOK;
425}
426
427/** Chop off node clusters in FAT.
428 *
429 * @param bs Buffer holding the boot sector of the file system.
430 * @param nodep FAT node where the chopping will take place.
431 * @param lcl Last cluster which will remain in the node. If this
432 * argument is FAT_CLST_RES0, then all clusters will
433 * be chopped off.
434 *
435 * @return EOK on success or a negative return code.
436 */
437int exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
438{
439 int rc;
[375ab5e]440 service_id_t service_id = nodep->idx->service_id;
[f3d4cd35]441
442 /*
443 * Invalidate cached cluster numbers.
444 */
445 nodep->lastc_cached_valid = false;
446 if (nodep->currc_cached_value != lcl)
447 nodep->currc_cached_valid = false;
448
449 if (lcl == 0) {
450 /* The node will have zero size and no clusters allocated. */
[375ab5e]451 rc = exfat_free_clusters(bs, service_id, nodep->firstc);
[f3d4cd35]452 if (rc != EOK)
453 return rc;
454 nodep->firstc = 0;
455 nodep->dirty = true; /* need to sync node */
456 } else {
457 exfat_cluster_t nextc;
458
[375ab5e]459 rc = exfat_get_cluster(bs, service_id, lcl, &nextc);
[f3d4cd35]460 if (rc != EOK)
461 return rc;
462
463 /* Terminate the cluster chain */
[375ab5e]464 rc = exfat_set_cluster(bs, service_id, lcl, EXFAT_CLST_EOF);
[f3d4cd35]465 if (rc != EOK)
466 return rc;
467
468 /* Free all following clusters. */
[375ab5e]469 rc = exfat_free_clusters(bs, service_id, nextc);
[f3d4cd35]470 if (rc != EOK)
471 return rc;
472 }
473
474 /*
475 * Update and re-enable the last cluster cache.
476 */
477 nodep->lastc_cached_valid = true;
478 nodep->lastc_cached_value = lcl;
479
480 return EOK;
481}
482
[8a06c1b]483int
[375ab5e]484exfat_zero_cluster(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t c)
[8a06c1b]485{
486 size_t i;
487 block_t *b;
488 int rc;
489
490 for (i = 0; i < SPC(bs); i++) {
[0dbe5ac]491 rc = exfat_block_get_by_clst(&b, bs, service_id, false, c, NULL,
492 i, BLOCK_FLAGS_NOREAD);
[8a06c1b]493 if (rc != EOK)
494 return rc;
495 memset(b->data, 0, BPS(bs));
496 b->dirty = true;
497 rc = block_put(b);
498 if (rc != EOK)
499 return rc;
500 }
501
502 return EOK;
503}
504
[e8976b59]505int
506exfat_read_uctable(exfat_bs_t *bs, exfat_node_t *nodep, uint8_t *uctable)
507{
508 size_t i, blocks, count;
509 block_t *b;
510 int rc;
511 blocks = ROUND_UP(nodep->size, BPS(bs))/BPS(bs);
512 count = BPS(bs);
513
514 for (i = 0; i < blocks; i++) {
515 rc = exfat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NOREAD);
516 if (rc != EOK)
517 return rc;
[0dbe5ac]518 if (i == blocks - 1)
519 count = nodep->size - i * BPS(bs);
[e8976b59]520 memcpy(uctable, b->data, count);
521 uctable += count;
522 rc = block_put(b);
523 if (rc != EOK)
524 return rc;
525 }
526
527 return EOK;
528}
529
530
[4c3c4a5]531/** Perform basic sanity checks on the file system.
532 *
533 * Verify if values of boot sector fields are sane. Also verify media
534 * descriptor. This is used to rule out cases when a device obviously
535 * does not contain a exfat file system.
536 */
[375ab5e]537int exfat_sanity_check(exfat_bs_t *bs, service_id_t service_id)
[4c3c4a5]538{
[5d5863c]539 /* TODO */
[4c3c4a5]540 return EOK;
541}
542
543/**
544 * @}
545 */
Note: See TracBrowser for help on using the repository browser.