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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25c60f4 was 5d5863c, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago

exFAT: move bitmap_* function to separate file exfat_bitmap.c

  • Property mode set to 100644
File size: 13.4 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.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/**
54 * The fat_alloc_lock mutex protects all copies of the File Allocation Table
55 * during allocation of clusters. The lock does not have to be held durring
56 * deallocation of clusters.
57 */
58static FIBRIL_MUTEX_INITIALIZE(exfat_alloc_lock);
59
60/** Walk the cluster chain.
61 *
62 * @param bs Buffer holding the boot sector for the file.
63 * @param devmap_handle Device handle of the device with the file.
64 * @param firstc First cluster to start the walk with.
65 * @param lastc If non-NULL, output argument hodling the last cluster
66 * number visited.
67 * @param numc If non-NULL, output argument holding the number of
68 * clusters seen during the walk.
69 * @param max_clusters Maximum number of clusters to visit.
70 *
71 * @return EOK on success or a negative error code.
72 */
73int
74exfat_cluster_walk(exfat_bs_t *bs, devmap_handle_t devmap_handle,
75 exfat_cluster_t firstc, exfat_cluster_t *lastc, uint32_t *numc,
76 uint32_t max_clusters)
77{
78 uint32_t clusters = 0;
79 exfat_cluster_t clst = firstc;
80 int rc;
81
82 if (firstc < EXFAT_CLST_FIRST) {
83 /* No space allocated to the file. */
84 if (lastc)
85 *lastc = firstc;
86 if (numc)
87 *numc = 0;
88 return EOK;
89 }
90
91 while (clst != EXFAT_CLST_EOF && clusters < max_clusters) {
92 assert(clst >= EXFAT_CLST_FIRST);
93 if (lastc)
94 *lastc = clst; /* remember the last cluster number */
95
96 rc = exfat_get_cluster(bs, devmap_handle, clst, &clst);
97 if (rc != EOK)
98 return rc;
99
100 assert(clst != EXFAT_CLST_BAD);
101 clusters++;
102 }
103
104 if (lastc && clst != EXFAT_CLST_EOF)
105 *lastc = clst;
106 if (numc)
107 *numc = clusters;
108
109 return EOK;
110}
111
112/** Read block from file located on a exFAT file system.
113 *
114 * @param block Pointer to a block pointer for storing result.
115 * @param bs Buffer holding the boot sector of the file system.
116 * @param nodep FAT node.
117 * @param bn Block number.
118 * @param flags Flags passed to libblock.
119 *
120 * @return EOK on success or a negative error code.
121 */
122int
123exfat_block_get(block_t **block, exfat_bs_t *bs, exfat_node_t *nodep,
124 aoff64_t bn, int flags)
125{
126 exfat_cluster_t firstc = nodep->firstc;
127 exfat_cluster_t currc;
128 aoff64_t relbn = bn;
129 int rc;
130
131 if (!nodep->size)
132 return ELIMIT;
133
134 if (nodep->fragmented) {
135 if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
136 nodep->lastc_cached_valid) {
137 /*
138 * This is a request to read a block within the last cluster
139 * when fortunately we have the last cluster number cached.
140 */
141 return block_get(block, nodep->idx->devmap_handle, DATA_FS(bs) +
142 (nodep->lastc_cached_value-EXFAT_CLST_FIRST)*SPC(bs) +
143 (bn % SPC(bs)), flags);
144 }
145
146 if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
147 /*
148 * We can start with the cluster cached by the previous call to
149 * fat_block_get().
150 */
151 firstc = nodep->currc_cached_value;
152 relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
153 }
154 }
155
156 rc = exfat_block_get_by_clst(block, bs, nodep->idx->devmap_handle,
157 nodep->fragmented, firstc, &currc, relbn, flags);
158 if (rc != EOK)
159 return rc;
160
161 /*
162 * Update the "current" cluster cache.
163 */
164 nodep->currc_cached_valid = true;
165 nodep->currc_cached_bn = bn;
166 nodep->currc_cached_value = currc;
167
168 return rc;
169}
170
171/** Read block from file located on a exFAT file system.
172 *
173 * @param block Pointer to a block pointer for storing result.
174 * @param bs Buffer holding the boot sector of the file system.
175 * @param devmap_handle Device handle of the file system.
176 * @param fcl First cluster used by the file. Can be zero if the file
177 * is empty.
178 * @param clp If not NULL, address where the cluster containing bn
179 * will be stored.
180 * stored
181 * @param bn Block number.
182 * @param flags Flags passed to libblock.
183 *
184 * @return EOK on success or a negative error code.
185 */
186int
187exfat_block_get_by_clst(block_t **block, exfat_bs_t *bs,
188 devmap_handle_t devmap_handle, bool fragmented, exfat_cluster_t fcl,
189 exfat_cluster_t *clp, aoff64_t bn, int flags)
190{
191 uint32_t clusters;
192 uint32_t max_clusters;
193 exfat_cluster_t c;
194 int rc;
195
196 if (fcl < EXFAT_CLST_FIRST)
197 return ELIMIT;
198
199 if (!fragmented) {
200 rc = block_get(block, devmap_handle, DATA_FS(bs) +
201 (fcl-EXFAT_CLST_FIRST)*SPC(bs) + bn, flags);
202 } else {
203 max_clusters = bn / SPC(bs);
204 rc = exfat_cluster_walk(bs, devmap_handle, fcl, &c, &clusters, max_clusters);
205 if (rc != EOK)
206 return rc;
207 assert(clusters == max_clusters);
208
209 rc = block_get(block, devmap_handle, DATA_FS(bs) +
210 (c-EXFAT_CLST_FIRST)*SPC(bs) + (bn % SPC(bs)), flags);
211
212 if (clp)
213 *clp = c;
214 }
215
216 return rc;
217}
218
219
220/** Get cluster from the FAT.
221 *
222 * @param bs Buffer holding the boot sector for the file system.
223 * @param devmap_handle Device handle 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 a negative error code.
228 */
229int
230exfat_get_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
231 exfat_cluster_t clst, exfat_cluster_t *value)
232{
233 block_t *b;
234 aoff64_t offset;
235 int rc;
236
237 offset = clst * sizeof(exfat_cluster_t);
238
239 rc = block_get(&b, devmap_handle, 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 devmap_handle Device handle 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 a negative error code.
258 */
259int
260exfat_set_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
261 exfat_cluster_t clst, exfat_cluster_t value)
262{
263 block_t *b;
264 aoff64_t offset;
265 int rc;
266
267 offset = clst * sizeof(exfat_cluster_t);
268
269 rc = block_get(&b, devmap_handle, 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 devmap_handle Device handle 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, a negative error code otherwise.
296 */
297int
298exfat_alloc_clusters(exfat_bs_t *bs, devmap_handle_t devmap_handle, 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, value;
304 int 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; clst++) {
312 rc = exfat_get_cluster(bs, devmap_handle, clst, &value);
313 if (rc != EOK)
314 break;
315
316 if (value == 0) {
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, devmap_handle, clst,
323 (found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]);
324 if (rc != EOK)
325 break;
326
327 found++;
328 }
329 }
330
331 if (rc == EOK && found == nclsts) {
332 *mcl = lifo[found - 1];
333 *lcl = lifo[0];
334 free(lifo);
335 fibril_mutex_unlock(&exfat_alloc_lock);
336 return EOK;
337 }
338
339 /* If something wrong - free the clusters */
340 if (found > 0) {
341 while (found--)
342 rc = exfat_set_cluster(bs, devmap_handle, lifo[found], 0);
343 }
344
345 free(lifo);
346 fibril_mutex_unlock(&exfat_alloc_lock);
347 return ENOSPC;
348}
349
350/** Free clusters forming a cluster chain in FAT.
351 *
352 * @param bs Buffer hodling the boot sector of the file system.
353 * @param devmap_handle Device handle of the file system.
354 * @param firstc First cluster in the chain which is to be freed.
355 *
356 * @return EOK on success or a negative return code.
357 */
358int
359exfat_free_clusters(exfat_bs_t *bs, devmap_handle_t devmap_handle, exfat_cluster_t firstc)
360{
361 exfat_cluster_t nextc;
362 int rc;
363
364 /* Mark all clusters in the chain as free */
365 while (firstc != EXFAT_CLST_EOF) {
366 assert(firstc >= EXFAT_CLST_FIRST && firstc < EXFAT_CLST_BAD);
367 rc = exfat_get_cluster(bs, devmap_handle, firstc, &nextc);
368 if (rc != EOK)
369 return rc;
370 rc = exfat_set_cluster(bs, devmap_handle, firstc, 0);
371 if (rc != EOK)
372 return rc;
373 firstc = nextc;
374 }
375
376 return EOK;
377}
378
379/** Append a cluster chain to the last file cluster in FAT.
380 *
381 * @param bs Buffer holding the boot sector of the file system.
382 * @param nodep Node representing the file.
383 * @param mcl First cluster of the cluster chain to append.
384 * @param lcl Last cluster of the cluster chain to append.
385 *
386 * @return EOK on success or a negative error code.
387 */
388int
389exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
390 exfat_cluster_t lcl)
391{
392 devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
393 exfat_cluster_t lastc;
394 int rc;
395
396 if (nodep->firstc == 0) {
397 /* No clusters allocated to the node yet. */
398 nodep->firstc = mcl;
399 nodep->dirty = true; /* need to sync node */
400 } else {
401 if (nodep->lastc_cached_valid) {
402 lastc = nodep->lastc_cached_value;
403 nodep->lastc_cached_valid = false;
404 } else {
405 rc = exfat_cluster_walk(bs, devmap_handle, nodep->firstc,
406 &lastc, NULL, (uint16_t) -1);
407 if (rc != EOK)
408 return rc;
409 }
410
411 rc = exfat_set_cluster(bs, nodep->idx->devmap_handle, lastc, mcl);
412 if (rc != EOK)
413 return rc;
414 }
415
416 nodep->lastc_cached_valid = true;
417 nodep->lastc_cached_value = lcl;
418
419 return EOK;
420}
421
422/** Chop off node clusters in FAT.
423 *
424 * @param bs Buffer holding the boot sector of the file system.
425 * @param nodep FAT node where the chopping will take place.
426 * @param lcl Last cluster which will remain in the node. If this
427 * argument is FAT_CLST_RES0, then all clusters will
428 * be chopped off.
429 *
430 * @return EOK on success or a negative return code.
431 */
432int exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
433{
434 int rc;
435 devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
436
437 /*
438 * Invalidate cached cluster numbers.
439 */
440 nodep->lastc_cached_valid = false;
441 if (nodep->currc_cached_value != lcl)
442 nodep->currc_cached_valid = false;
443
444 if (lcl == 0) {
445 /* The node will have zero size and no clusters allocated. */
446 rc = exfat_free_clusters(bs, devmap_handle, nodep->firstc);
447 if (rc != EOK)
448 return rc;
449 nodep->firstc = 0;
450 nodep->dirty = true; /* need to sync node */
451 } else {
452 exfat_cluster_t nextc;
453
454 rc = exfat_get_cluster(bs, devmap_handle, lcl, &nextc);
455 if (rc != EOK)
456 return rc;
457
458 /* Terminate the cluster chain */
459 rc = exfat_set_cluster(bs, devmap_handle, lcl, EXFAT_CLST_EOF);
460 if (rc != EOK)
461 return rc;
462
463 /* Free all following clusters. */
464 rc = exfat_free_clusters(bs, devmap_handle, nextc);
465 if (rc != EOK)
466 return rc;
467 }
468
469 /*
470 * Update and re-enable the last cluster cache.
471 */
472 nodep->lastc_cached_valid = true;
473 nodep->lastc_cached_value = lcl;
474
475 return EOK;
476}
477
478/** Perform basic sanity checks on the file system.
479 *
480 * Verify if values of boot sector fields are sane. Also verify media
481 * descriptor. This is used to rule out cases when a device obviously
482 * does not contain a exfat file system.
483 */
484int exfat_sanity_check(exfat_bs_t *bs, devmap_handle_t devmap_handle)
485{
486 /* TODO */
487 return EOK;
488}
489
490/**
491 * @}
492 */
Note: See TracBrowser for help on using the repository browser.