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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 071ff5fa was 071ff5fa, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

exfat: exfat_alloc_clusters(), return the correct error code in case of error

  • Property mode set to 100644
File size: 14.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_bitmap.h"
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 */
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 a negative error code.
73 */
74int
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 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
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 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)) &&
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 a negative error code.
186 */
187int
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;
195 int 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
221/** Get cluster from the FAT.
222 *
223 * @param bs Buffer holding the boot sector for the file system.
224 * @param service_id Service ID for the file system.
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
231exfat_get_cluster(exfat_bs_t *bs, service_id_t service_id,
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
240 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
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.
254 * @param service_id Service ID for the file system.
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
261exfat_set_cluster(exfat_bs_t *bs, service_id_t service_id,
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
270 rc = block_get(&b, service_id, FAT_FS(bs) + offset / BPS(bs), BLOCK_FLAGS_NONE);
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}
280
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.
289 * @param service_id Service ID of the file system.
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
299exfat_alloc_clusters(exfat_bs_t *bs, service_id_t service_id, unsigned nclsts,
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 */
304 exfat_cluster_t clst;
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);
312 for (clst = EXFAT_CLST_FIRST; clst < DATA_CNT(bs) + 2 && found < nclsts;
313 clst++) {
314 /* Need to rewrite because of multiple exfat_bitmap_get calls */
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 */
320 lifo[found] = clst;
321 rc = exfat_set_cluster(bs, service_id, clst,
322 (found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]);
323 if (rc != EOK)
324 goto exit_error;
325 found++;
326 rc = bitmap_set_cluster(bs, service_id, clst);
327 if (rc != EOK)
328 goto exit_error;
329
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 rc = ENOSPC;
342
343exit_error:
344
345 /* If something wrong - free the clusters */
346 while (found--) {
347 (void) bitmap_clear_cluster(bs, service_id, lifo[found]);
348 (void) exfat_set_cluster(bs, service_id, lifo[found], 0);
349 }
350
351 free(lifo);
352 fibril_mutex_unlock(&exfat_alloc_lock);
353 return rc;
354}
355
356/** Free clusters forming a cluster chain in FAT.
357 *
358 * @param bs Buffer hodling the boot sector of the file system.
359 * @param service_id Service ID of the file system.
360 * @param firstc First cluster in the chain which is to be freed.
361 *
362 * @return EOK on success or a negative return code.
363 */
364int
365exfat_free_clusters(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t firstc)
366{
367 exfat_cluster_t nextc;
368 int rc;
369
370 /* Mark all clusters in the chain as free */
371 while (firstc != EXFAT_CLST_EOF) {
372 assert(firstc >= EXFAT_CLST_FIRST && firstc < EXFAT_CLST_BAD);
373 rc = exfat_get_cluster(bs, service_id, firstc, &nextc);
374 if (rc != EOK)
375 return rc;
376 rc = exfat_set_cluster(bs, service_id, firstc, 0);
377 if (rc != EOK)
378 return rc;
379 rc = bitmap_clear_cluster(bs, service_id, firstc);
380 if (rc != EOK)
381 return rc;
382 firstc = nextc;
383 }
384
385 return EOK;
386}
387
388/** Append a cluster chain to the last file cluster in FAT.
389 *
390 * @param bs Buffer holding the boot sector of the file system.
391 * @param nodep Node representing the file.
392 * @param mcl First cluster of the cluster chain to append.
393 * @param lcl Last cluster of the cluster chain to append.
394 *
395 * @return EOK on success or a negative error code.
396 */
397int
398exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
399 exfat_cluster_t lcl)
400{
401 service_id_t service_id = nodep->idx->service_id;
402 exfat_cluster_t lastc;
403 int rc;
404
405 if (nodep->firstc == 0) {
406 /* No clusters allocated to the node yet. */
407 nodep->firstc = mcl;
408 nodep->dirty = true; /* need to sync node */
409 } else {
410 if (nodep->lastc_cached_valid) {
411 lastc = nodep->lastc_cached_value;
412 nodep->lastc_cached_valid = false;
413 } else {
414 rc = exfat_cluster_walk(bs, service_id, nodep->firstc,
415 &lastc, NULL, (uint16_t) -1);
416 if (rc != EOK)
417 return rc;
418 }
419
420 rc = exfat_set_cluster(bs, nodep->idx->service_id, lastc, mcl);
421 if (rc != EOK)
422 return rc;
423 }
424
425 nodep->lastc_cached_valid = true;
426 nodep->lastc_cached_value = lcl;
427
428 return EOK;
429}
430
431/** Chop off node clusters in FAT.
432 *
433 * @param bs Buffer holding the boot sector of the file system.
434 * @param nodep FAT node where the chopping will take place.
435 * @param lcl Last cluster which will remain in the node. If this
436 * argument is FAT_CLST_RES0, then all clusters will
437 * be chopped off.
438 *
439 * @return EOK on success or a negative return code.
440 */
441int exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
442{
443 int rc;
444 service_id_t service_id = nodep->idx->service_id;
445
446 /*
447 * Invalidate cached cluster numbers.
448 */
449 nodep->lastc_cached_valid = false;
450 if (nodep->currc_cached_value != lcl)
451 nodep->currc_cached_valid = false;
452
453 if (lcl == 0) {
454 /* The node will have zero size and no clusters allocated. */
455 rc = exfat_free_clusters(bs, service_id, nodep->firstc);
456 if (rc != EOK)
457 return rc;
458 nodep->firstc = 0;
459 nodep->dirty = true; /* need to sync node */
460 } else {
461 exfat_cluster_t nextc;
462
463 rc = exfat_get_cluster(bs, service_id, lcl, &nextc);
464 if (rc != EOK)
465 return rc;
466
467 /* Terminate the cluster chain */
468 rc = exfat_set_cluster(bs, service_id, lcl, EXFAT_CLST_EOF);
469 if (rc != EOK)
470 return rc;
471
472 /* Free all following clusters. */
473 rc = exfat_free_clusters(bs, service_id, nextc);
474 if (rc != EOK)
475 return rc;
476 }
477
478 /*
479 * Update and re-enable the last cluster cache.
480 */
481 nodep->lastc_cached_valid = true;
482 nodep->lastc_cached_value = lcl;
483
484 return EOK;
485}
486
487int
488exfat_zero_cluster(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t c)
489{
490 size_t i;
491 block_t *b;
492 int rc;
493
494 for (i = 0; i < SPC(bs); i++) {
495 rc = exfat_block_get_by_clst(&b, bs, service_id, false, c, NULL,
496 i, BLOCK_FLAGS_NOREAD);
497 if (rc != EOK)
498 return rc;
499 memset(b->data, 0, BPS(bs));
500 b->dirty = true;
501 rc = block_put(b);
502 if (rc != EOK)
503 return rc;
504 }
505
506 return EOK;
507}
508
509int
510exfat_read_uctable(exfat_bs_t *bs, exfat_node_t *nodep, uint8_t *uctable)
511{
512 size_t i, blocks, count;
513 block_t *b;
514 int rc;
515 blocks = ROUND_UP(nodep->size, BPS(bs))/BPS(bs);
516 count = BPS(bs);
517
518 for (i = 0; i < blocks; i++) {
519 rc = exfat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NOREAD);
520 if (rc != EOK)
521 return rc;
522 if (i == blocks - 1)
523 count = nodep->size - i * BPS(bs);
524 memcpy(uctable, b->data, count);
525 uctable += count;
526 rc = block_put(b);
527 if (rc != EOK)
528 return rc;
529 }
530
531 return EOK;
532}
533
534
535/** Perform basic sanity checks on the file system.
536 *
537 * Verify if values of boot sector fields are sane. Also verify media
538 * descriptor. This is used to rule out cases when a device obviously
539 * does not contain a exfat file system.
540 */
541int exfat_sanity_check(exfat_bs_t *bs, service_id_t service_id)
542{
543 /* TODO */
544 return EOK;
545}
546
547/**
548 * @}
549 */
Note: See TracBrowser for help on using the repository browser.