1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file fat_fat.c
|
---|
35 | * @brief Functions that manipulate the File Allocation Tables.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "fat_fat.h"
|
---|
39 | #include "fat_dentry.h"
|
---|
40 | #include "fat.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 <mem.h>
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * The fat_alloc_lock mutex protects all copies of the File Allocation Table
|
---|
53 | * during allocation of clusters. The lock does not have to be held durring
|
---|
54 | * deallocation of clusters.
|
---|
55 | */
|
---|
56 | static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
|
---|
57 |
|
---|
58 | /** Walk the cluster chain.
|
---|
59 | *
|
---|
60 | * @param bs Buffer holding the boot sector for the file.
|
---|
61 | * @param dev_handle Device handle of the device with the file.
|
---|
62 | * @param firstc First cluster to start the walk with.
|
---|
63 | * @param lastc If non-NULL, output argument hodling the last cluster
|
---|
64 | * number visited.
|
---|
65 | * @param numc If non-NULL, output argument holding the number of
|
---|
66 | * clusters seen during the walk.
|
---|
67 | * @param max_clusters Maximum number of clusters to visit.
|
---|
68 | *
|
---|
69 | * @return EOK on success or a negative error code.
|
---|
70 | */
|
---|
71 | int
|
---|
72 | fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
|
---|
73 | fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
|
---|
74 | {
|
---|
75 | block_t *b;
|
---|
76 | unsigned bps;
|
---|
77 | unsigned rscnt; /* block address of the first FAT */
|
---|
78 | uint16_t clusters = 0;
|
---|
79 | fat_cluster_t clst = firstc;
|
---|
80 | int rc;
|
---|
81 |
|
---|
82 | bps = uint16_t_le2host(bs->bps);
|
---|
83 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
84 |
|
---|
85 | if (firstc == FAT_CLST_RES0) {
|
---|
86 | /* No space allocated to the file. */
|
---|
87 | if (lastc)
|
---|
88 | *lastc = firstc;
|
---|
89 | if (numc)
|
---|
90 | *numc = 0;
|
---|
91 | return EOK;
|
---|
92 | }
|
---|
93 |
|
---|
94 | while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
|
---|
95 | aoff64_t fsec; /* sector offset relative to FAT1 */
|
---|
96 | unsigned fidx; /* FAT1 entry index */
|
---|
97 |
|
---|
98 | assert(clst >= FAT_CLST_FIRST);
|
---|
99 | if (lastc)
|
---|
100 | *lastc = clst; /* remember the last cluster number */
|
---|
101 | fsec = (clst * sizeof(fat_cluster_t)) / bps;
|
---|
102 | fidx = clst % (bps / sizeof(fat_cluster_t));
|
---|
103 | /* read FAT1 */
|
---|
104 | rc = block_get(&b, dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
|
---|
105 | if (rc != EOK)
|
---|
106 | return rc;
|
---|
107 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
|
---|
108 | assert(clst != FAT_CLST_BAD);
|
---|
109 | rc = block_put(b);
|
---|
110 | if (rc != EOK)
|
---|
111 | return rc;
|
---|
112 | clusters++;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (lastc && clst < FAT_CLST_LAST1)
|
---|
116 | *lastc = clst;
|
---|
117 | if (numc)
|
---|
118 | *numc = clusters;
|
---|
119 |
|
---|
120 | return EOK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** Read block from file located on a FAT file system.
|
---|
124 | *
|
---|
125 | * @param block Pointer to a block pointer for storing result.
|
---|
126 | * @param bs Buffer holding the boot sector of the file system.
|
---|
127 | * @param dev_handle Device handle of the file system.
|
---|
128 | * @param firstc First cluster used by the file. Can be zero if the file
|
---|
129 | * is empty.
|
---|
130 | * @param bn Block number.
|
---|
131 | * @param flags Flags passed to libblock.
|
---|
132 | *
|
---|
133 | * @return EOK on success or a negative error code.
|
---|
134 | */
|
---|
135 | int
|
---|
136 | _fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle,
|
---|
137 | fat_cluster_t firstc, aoff64_t bn, int flags)
|
---|
138 | {
|
---|
139 | unsigned bps;
|
---|
140 | unsigned rscnt; /* block address of the first FAT */
|
---|
141 | unsigned rde;
|
---|
142 | unsigned rds; /* root directory size */
|
---|
143 | unsigned sf;
|
---|
144 | unsigned ssa; /* size of the system area */
|
---|
145 | uint16_t clusters;
|
---|
146 | unsigned max_clusters;
|
---|
147 | fat_cluster_t lastc;
|
---|
148 | int rc;
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * This function can only operate on non-zero length files.
|
---|
152 | */
|
---|
153 | if (firstc == FAT_CLST_RES0)
|
---|
154 | return ELIMIT;
|
---|
155 |
|
---|
156 | bps = uint16_t_le2host(bs->bps);
|
---|
157 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
158 | rde = uint16_t_le2host(bs->root_ent_max);
|
---|
159 | sf = uint16_t_le2host(bs->sec_per_fat);
|
---|
160 |
|
---|
161 | rds = (sizeof(fat_dentry_t) * rde) / bps;
|
---|
162 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
|
---|
163 | ssa = rscnt + bs->fatcnt * sf + rds;
|
---|
164 |
|
---|
165 | if (firstc == FAT_CLST_ROOT) {
|
---|
166 | /* root directory special case */
|
---|
167 | assert(bn < rds);
|
---|
168 | rc = block_get(block, dev_handle, rscnt + bs->fatcnt * sf + bn,
|
---|
169 | flags);
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 | max_clusters = bn / bs->spc;
|
---|
174 | rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters,
|
---|
175 | max_clusters);
|
---|
176 | if (rc != EOK)
|
---|
177 | return rc;
|
---|
178 | assert(clusters == max_clusters);
|
---|
179 |
|
---|
180 | rc = block_get(block, dev_handle,
|
---|
181 | ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags);
|
---|
182 |
|
---|
183 | return rc;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** Fill the gap between EOF and a new file position.
|
---|
187 | *
|
---|
188 | * @param bs Buffer holding the boot sector for nodep.
|
---|
189 | * @param nodep FAT node with the gap.
|
---|
190 | * @param mcl First cluster in an independent cluster chain that will
|
---|
191 | * be later appended to the end of the node's own cluster
|
---|
192 | * chain. If pos is still in the last allocated cluster,
|
---|
193 | * this argument is ignored.
|
---|
194 | * @param pos Position in the last node block.
|
---|
195 | *
|
---|
196 | * @return EOK on success or a negative error code.
|
---|
197 | */
|
---|
198 | int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
|
---|
199 | {
|
---|
200 | uint16_t bps;
|
---|
201 | unsigned spc;
|
---|
202 | block_t *b;
|
---|
203 | aoff64_t o, boundary;
|
---|
204 | int rc;
|
---|
205 |
|
---|
206 | bps = uint16_t_le2host(bs->bps);
|
---|
207 | spc = bs->spc;
|
---|
208 |
|
---|
209 | boundary = ROUND_UP(nodep->size, bps * spc);
|
---|
210 |
|
---|
211 | /* zero out already allocated space */
|
---|
212 | for (o = nodep->size; o < pos && o < boundary;
|
---|
213 | o = ALIGN_DOWN(o + bps, bps)) {
|
---|
214 | int flags = (o % bps == 0) ?
|
---|
215 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
|
---|
216 | rc = fat_block_get(&b, bs, nodep, o / bps, flags);
|
---|
217 | if (rc != EOK)
|
---|
218 | return rc;
|
---|
219 | memset(b->data + o % bps, 0, bps - o % bps);
|
---|
220 | b->dirty = true; /* need to sync node */
|
---|
221 | rc = block_put(b);
|
---|
222 | if (rc != EOK)
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (o >= pos)
|
---|
227 | return EOK;
|
---|
228 |
|
---|
229 | /* zero out the initial part of the new cluster chain */
|
---|
230 | for (o = boundary; o < pos; o += bps) {
|
---|
231 | rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
|
---|
232 | (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
|
---|
233 | if (rc != EOK)
|
---|
234 | return rc;
|
---|
235 | memset(b->data, 0, min(bps, pos - o));
|
---|
236 | b->dirty = true; /* need to sync node */
|
---|
237 | rc = block_put(b);
|
---|
238 | if (rc != EOK)
|
---|
239 | return rc;
|
---|
240 | }
|
---|
241 |
|
---|
242 | return EOK;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /** Get cluster from the first FAT.
|
---|
246 | *
|
---|
247 | * @param bs Buffer holding the boot sector for the file system.
|
---|
248 | * @param dev_handle Device handle for the file system.
|
---|
249 | * @param clst Cluster which to get.
|
---|
250 | * @param value Output argument holding the value of the cluster.
|
---|
251 | *
|
---|
252 | * @return EOK or a negative error code.
|
---|
253 | */
|
---|
254 | int
|
---|
255 | fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
|
---|
256 | fat_cluster_t clst, fat_cluster_t *value)
|
---|
257 | {
|
---|
258 | block_t *b;
|
---|
259 | uint16_t bps;
|
---|
260 | uint16_t rscnt;
|
---|
261 | uint16_t sf;
|
---|
262 | fat_cluster_t *cp;
|
---|
263 | int rc;
|
---|
264 |
|
---|
265 | bps = uint16_t_le2host(bs->bps);
|
---|
266 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
267 | sf = uint16_t_le2host(bs->sec_per_fat);
|
---|
268 |
|
---|
269 | rc = block_get(&b, dev_handle, rscnt + sf * fatno +
|
---|
270 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
---|
271 | if (rc != EOK)
|
---|
272 | return rc;
|
---|
273 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
---|
274 | *value = uint16_t_le2host(*cp);
|
---|
275 | rc = block_put(b);
|
---|
276 |
|
---|
277 | return rc;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /** Set cluster in one instance of FAT.
|
---|
281 | *
|
---|
282 | * @param bs Buffer holding the boot sector for the file system.
|
---|
283 | * @param dev_handle Device handle for the file system.
|
---|
284 | * @param fatno Number of the FAT instance where to make the change.
|
---|
285 | * @param clst Cluster which is to be set.
|
---|
286 | * @param value Value to set the cluster with.
|
---|
287 | *
|
---|
288 | * @return EOK on success or a negative error code.
|
---|
289 | */
|
---|
290 | int
|
---|
291 | fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
|
---|
292 | fat_cluster_t clst, fat_cluster_t value)
|
---|
293 | {
|
---|
294 | block_t *b;
|
---|
295 | uint16_t bps;
|
---|
296 | uint16_t rscnt;
|
---|
297 | uint16_t sf;
|
---|
298 | fat_cluster_t *cp;
|
---|
299 | int rc;
|
---|
300 |
|
---|
301 | bps = uint16_t_le2host(bs->bps);
|
---|
302 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
303 | sf = uint16_t_le2host(bs->sec_per_fat);
|
---|
304 |
|
---|
305 | assert(fatno < bs->fatcnt);
|
---|
306 | rc = block_get(&b, dev_handle, rscnt + sf * fatno +
|
---|
307 | (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
|
---|
308 | if (rc != EOK)
|
---|
309 | return rc;
|
---|
310 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
|
---|
311 | *cp = host2uint16_t_le(value);
|
---|
312 | b->dirty = true; /* need to sync block */
|
---|
313 | rc = block_put(b);
|
---|
314 | return rc;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /** Replay the allocatoin of clusters in all shadow instances of FAT.
|
---|
318 | *
|
---|
319 | * @param bs Buffer holding the boot sector of the file system.
|
---|
320 | * @param dev_handle Device handle of the file system.
|
---|
321 | * @param lifo Chain of allocated clusters.
|
---|
322 | * @param nclsts Number of clusters in the lifo chain.
|
---|
323 | *
|
---|
324 | * @return EOK on success or a negative error code.
|
---|
325 | */
|
---|
326 | int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
|
---|
327 | fat_cluster_t *lifo, unsigned nclsts)
|
---|
328 | {
|
---|
329 | uint8_t fatno;
|
---|
330 | unsigned c;
|
---|
331 | int rc;
|
---|
332 |
|
---|
333 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
|
---|
334 | for (c = 0; c < nclsts; c++) {
|
---|
335 | rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
|
---|
336 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
|
---|
337 | if (rc != EOK)
|
---|
338 | return rc;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | return EOK;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /** Allocate clusters in all copies of FAT.
|
---|
346 | *
|
---|
347 | * This function will attempt to allocate the requested number of clusters in
|
---|
348 | * all instances of the FAT. The FAT will be altered so that the allocated
|
---|
349 | * clusters form an independent chain (i.e. a chain which does not belong to any
|
---|
350 | * file yet).
|
---|
351 | *
|
---|
352 | * @param bs Buffer holding the boot sector of the file system.
|
---|
353 | * @param dev_handle Device handle of the file system.
|
---|
354 | * @param nclsts Number of clusters to allocate.
|
---|
355 | * @param mcl Output parameter where the first cluster in the chain
|
---|
356 | * will be returned.
|
---|
357 | * @param lcl Output parameter where the last cluster in the chain
|
---|
358 | * will be returned.
|
---|
359 | *
|
---|
360 | * @return EOK on success, a negative error code otherwise.
|
---|
361 | */
|
---|
362 | int
|
---|
363 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
|
---|
364 | fat_cluster_t *mcl, fat_cluster_t *lcl)
|
---|
365 | {
|
---|
366 | uint16_t bps;
|
---|
367 | uint16_t rscnt;
|
---|
368 | uint16_t sf;
|
---|
369 | uint32_t ts;
|
---|
370 | unsigned rde;
|
---|
371 | unsigned rds;
|
---|
372 | unsigned ssa;
|
---|
373 | block_t *blk;
|
---|
374 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */
|
---|
375 | unsigned found = 0; /* top of the free cluster number stack */
|
---|
376 | unsigned b, c, cl;
|
---|
377 | int rc;
|
---|
378 |
|
---|
379 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
|
---|
380 | if (!lifo)
|
---|
381 | return ENOMEM;
|
---|
382 |
|
---|
383 | bps = uint16_t_le2host(bs->bps);
|
---|
384 | rscnt = uint16_t_le2host(bs->rscnt);
|
---|
385 | sf = uint16_t_le2host(bs->sec_per_fat);
|
---|
386 | rde = uint16_t_le2host(bs->root_ent_max);
|
---|
387 | ts = (uint32_t) uint16_t_le2host(bs->totsec16);
|
---|
388 | if (ts == 0)
|
---|
389 | ts = uint32_t_le2host(bs->totsec32);
|
---|
390 |
|
---|
391 | rds = (sizeof(fat_dentry_t) * rde) / bps;
|
---|
392 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
|
---|
393 | ssa = rscnt + bs->fatcnt * sf + rds;
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Search FAT1 for unused clusters.
|
---|
397 | */
|
---|
398 | fibril_mutex_lock(&fat_alloc_lock);
|
---|
399 | for (b = 0, cl = 0; b < sf; b++) {
|
---|
400 | rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
|
---|
401 | if (rc != EOK)
|
---|
402 | goto error;
|
---|
403 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
|
---|
404 | /*
|
---|
405 | * Check if the cluster is physically there. This check
|
---|
406 | * becomes necessary when the file system is created
|
---|
407 | * with fewer total sectors than how many is inferred
|
---|
408 | * from the size of the file allocation table.
|
---|
409 | */
|
---|
410 | if ((cl >= 2) && ((cl - 2) * bs->spc + ssa >= ts)) {
|
---|
411 | rc = block_put(blk);
|
---|
412 | if (rc != EOK)
|
---|
413 | goto error;
|
---|
414 | goto out;
|
---|
415 | }
|
---|
416 |
|
---|
417 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
|
---|
418 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
|
---|
419 | /*
|
---|
420 | * The cluster is free. Put it into our stack
|
---|
421 | * of found clusters and mark it as non-free.
|
---|
422 | */
|
---|
423 | lifo[found] = cl;
|
---|
424 | *clst = (found == 0) ?
|
---|
425 | host2uint16_t_le(FAT_CLST_LAST1) :
|
---|
426 | host2uint16_t_le(lifo[found - 1]);
|
---|
427 | blk->dirty = true; /* need to sync block */
|
---|
428 | if (++found == nclsts) {
|
---|
429 | /* we are almost done */
|
---|
430 | rc = block_put(blk);
|
---|
431 | if (rc != EOK)
|
---|
432 | goto error;
|
---|
433 | /* update the shadow copies of FAT */
|
---|
434 | rc = fat_alloc_shadow_clusters(bs,
|
---|
435 | dev_handle, lifo, nclsts);
|
---|
436 | if (rc != EOK)
|
---|
437 | goto error;
|
---|
438 | *mcl = lifo[found - 1];
|
---|
439 | *lcl = lifo[0];
|
---|
440 | free(lifo);
|
---|
441 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
442 | return EOK;
|
---|
443 | }
|
---|
444 | }
|
---|
445 | }
|
---|
446 | rc = block_put(blk);
|
---|
447 | if (rc != EOK) {
|
---|
448 | error:
|
---|
449 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
450 | free(lifo);
|
---|
451 | return rc;
|
---|
452 | }
|
---|
453 | }
|
---|
454 | out:
|
---|
455 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
456 |
|
---|
457 | /*
|
---|
458 | * We could not find enough clusters. Now we need to free the clusters
|
---|
459 | * we have allocated so far.
|
---|
460 | */
|
---|
461 | while (found--) {
|
---|
462 | rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
|
---|
463 | FAT_CLST_RES0);
|
---|
464 | if (rc != EOK) {
|
---|
465 | free(lifo);
|
---|
466 | return rc;
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | free(lifo);
|
---|
471 | return ENOSPC;
|
---|
472 | }
|
---|
473 |
|
---|
474 | /** Free clusters forming a cluster chain in all copies of FAT.
|
---|
475 | *
|
---|
476 | * @param bs Buffer hodling the boot sector of the file system.
|
---|
477 | * @param dev_handle Device handle of the file system.
|
---|
478 | * @param firstc First cluster in the chain which is to be freed.
|
---|
479 | *
|
---|
480 | * @return EOK on success or a negative return code.
|
---|
481 | */
|
---|
482 | int
|
---|
483 | fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
|
---|
484 | {
|
---|
485 | unsigned fatno;
|
---|
486 | fat_cluster_t nextc;
|
---|
487 | int rc;
|
---|
488 |
|
---|
489 | /* Mark all clusters in the chain as free in all copies of FAT. */
|
---|
490 | while (firstc < FAT_CLST_LAST1) {
|
---|
491 | assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
|
---|
492 | rc = fat_get_cluster(bs, dev_handle, FAT1, firstc, &nextc);
|
---|
493 | if (rc != EOK)
|
---|
494 | return rc;
|
---|
495 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
---|
496 | rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
|
---|
497 | FAT_CLST_RES0);
|
---|
498 | if (rc != EOK)
|
---|
499 | return rc;
|
---|
500 | }
|
---|
501 |
|
---|
502 | firstc = nextc;
|
---|
503 | }
|
---|
504 |
|
---|
505 | return EOK;
|
---|
506 | }
|
---|
507 |
|
---|
508 | /** Append a cluster chain to the last file cluster in all FATs.
|
---|
509 | *
|
---|
510 | * @param bs Buffer holding the boot sector of the file system.
|
---|
511 | * @param nodep Node representing the file.
|
---|
512 | * @param mcl First cluster of the cluster chain to append.
|
---|
513 | * @param lcl Last cluster of the cluster chain to append.
|
---|
514 | *
|
---|
515 | * @return EOK on success or a negative error code.
|
---|
516 | */
|
---|
517 | int
|
---|
518 | fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
|
---|
519 | fat_cluster_t lcl)
|
---|
520 | {
|
---|
521 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
---|
522 | fat_cluster_t lastc;
|
---|
523 | uint16_t numc;
|
---|
524 | uint8_t fatno;
|
---|
525 | int rc;
|
---|
526 |
|
---|
527 | if (nodep->lastc_cached_valid) {
|
---|
528 | lastc = nodep->lastc_cached_value;
|
---|
529 | nodep->lastc_cached_valid = false;
|
---|
530 | } else {
|
---|
531 | rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lastc,
|
---|
532 | &numc, (uint16_t) -1);
|
---|
533 | if (rc != EOK)
|
---|
534 | return rc;
|
---|
535 |
|
---|
536 | if (numc == 0) {
|
---|
537 | /* No clusters allocated to the node yet. */
|
---|
538 | nodep->firstc = mcl;
|
---|
539 | nodep->dirty = true; /* need to sync node */
|
---|
540 | return EOK;
|
---|
541 | }
|
---|
542 | }
|
---|
543 |
|
---|
544 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
---|
545 | rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lastc,
|
---|
546 | mcl);
|
---|
547 | if (rc != EOK)
|
---|
548 | return rc;
|
---|
549 | }
|
---|
550 |
|
---|
551 | nodep->lastc_cached_valid = true;
|
---|
552 | nodep->lastc_cached_value = lcl;
|
---|
553 |
|
---|
554 | return EOK;
|
---|
555 | }
|
---|
556 |
|
---|
557 | /** Chop off node clusters in all copies of FAT.
|
---|
558 | *
|
---|
559 | * @param bs Buffer holding the boot sector of the file system.
|
---|
560 | * @param nodep FAT node where the chopping will take place.
|
---|
561 | * @param lcl Last cluster which will remain in the node. If this
|
---|
562 | * argument is FAT_CLST_RES0, then all clusters will
|
---|
563 | * be chopped off.
|
---|
564 | *
|
---|
565 | * @return EOK on success or a negative return code.
|
---|
566 | */
|
---|
567 | int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
|
---|
568 | {
|
---|
569 | int rc;
|
---|
570 | dev_handle_t dev_handle = nodep->idx->dev_handle;
|
---|
571 |
|
---|
572 | nodep->lastc_cached_valid = false;
|
---|
573 | if (lcl == FAT_CLST_RES0) {
|
---|
574 | /* The node will have zero size and no clusters allocated. */
|
---|
575 | rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
|
---|
576 | if (rc != EOK)
|
---|
577 | return rc;
|
---|
578 | nodep->firstc = FAT_CLST_RES0;
|
---|
579 | nodep->dirty = true; /* need to sync node */
|
---|
580 | } else {
|
---|
581 | fat_cluster_t nextc;
|
---|
582 | unsigned fatno;
|
---|
583 |
|
---|
584 | rc = fat_get_cluster(bs, dev_handle, FAT1, lcl, &nextc);
|
---|
585 | if (rc != EOK)
|
---|
586 | return rc;
|
---|
587 |
|
---|
588 | /* Terminate the cluster chain in all copies of FAT. */
|
---|
589 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
|
---|
590 | rc = fat_set_cluster(bs, dev_handle, fatno, lcl,
|
---|
591 | FAT_CLST_LAST1);
|
---|
592 | if (rc != EOK)
|
---|
593 | return rc;
|
---|
594 | }
|
---|
595 |
|
---|
596 | /* Free all following clusters. */
|
---|
597 | rc = fat_free_clusters(bs, dev_handle, nextc);
|
---|
598 | if (rc != EOK)
|
---|
599 | return rc;
|
---|
600 | }
|
---|
601 |
|
---|
602 | nodep->lastc_cached_valid = true;
|
---|
603 | nodep->lastc_cached_value = lcl;
|
---|
604 |
|
---|
605 | return EOK;
|
---|
606 | }
|
---|
607 |
|
---|
608 | int
|
---|
609 | fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
|
---|
610 | {
|
---|
611 | int i;
|
---|
612 | block_t *b;
|
---|
613 | unsigned bps;
|
---|
614 | int rc;
|
---|
615 |
|
---|
616 | bps = uint16_t_le2host(bs->bps);
|
---|
617 |
|
---|
618 | for (i = 0; i < bs->spc; i++) {
|
---|
619 | rc = _fat_block_get(&b, bs, dev_handle, c, i,
|
---|
620 | BLOCK_FLAGS_NOREAD);
|
---|
621 | if (rc != EOK)
|
---|
622 | return rc;
|
---|
623 | memset(b->data, 0, bps);
|
---|
624 | b->dirty = true;
|
---|
625 | rc = block_put(b);
|
---|
626 | if (rc != EOK)
|
---|
627 | return rc;
|
---|
628 | }
|
---|
629 |
|
---|
630 | return EOK;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /** Perform basic sanity checks on the file system.
|
---|
634 | *
|
---|
635 | * Verify if values of boot sector fields are sane. Also verify media
|
---|
636 | * descriptor. This is used to rule out cases when a device obviously
|
---|
637 | * does not contain a fat file system.
|
---|
638 | */
|
---|
639 | int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
|
---|
640 | {
|
---|
641 | fat_cluster_t e0, e1;
|
---|
642 | unsigned fat_no;
|
---|
643 | int rc;
|
---|
644 |
|
---|
645 | /* Check number of FATs. */
|
---|
646 | if (bs->fatcnt == 0)
|
---|
647 | return ENOTSUP;
|
---|
648 |
|
---|
649 | /* Check total number of sectors. */
|
---|
650 |
|
---|
651 | if (bs->totsec16 == 0 && bs->totsec32 == 0)
|
---|
652 | return ENOTSUP;
|
---|
653 |
|
---|
654 | if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
|
---|
655 | bs->totsec16 != bs->totsec32)
|
---|
656 | return ENOTSUP;
|
---|
657 |
|
---|
658 | /* Check media descriptor. Must be between 0xf0 and 0xff. */
|
---|
659 | if ((bs->mdesc & 0xf0) != 0xf0)
|
---|
660 | return ENOTSUP;
|
---|
661 |
|
---|
662 | /* Check number of sectors per FAT. */
|
---|
663 | if (bs->sec_per_fat == 0)
|
---|
664 | return ENOTSUP;
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * Check that the root directory entries take up whole blocks.
|
---|
668 | * This check is rather strict, but it allows us to treat the root
|
---|
669 | * directory and non-root directories uniformly in some places.
|
---|
670 | * It can be removed provided that functions such as fat_read() are
|
---|
671 | * sanitized to support file systems with this property.
|
---|
672 | */
|
---|
673 | if ((uint16_t_le2host(bs->root_ent_max) * sizeof(fat_dentry_t)) %
|
---|
674 | uint16_t_le2host(bs->bps) != 0)
|
---|
675 | return ENOTSUP;
|
---|
676 |
|
---|
677 | /* Check signature of each FAT. */
|
---|
678 |
|
---|
679 | for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
|
---|
680 | rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
|
---|
681 | if (rc != EOK)
|
---|
682 | return EIO;
|
---|
683 |
|
---|
684 | rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
|
---|
685 | if (rc != EOK)
|
---|
686 | return EIO;
|
---|
687 |
|
---|
688 | /* Check that first byte of FAT contains the media descriptor. */
|
---|
689 | if ((e0 & 0xff) != bs->mdesc)
|
---|
690 | return ENOTSUP;
|
---|
691 |
|
---|
692 | /*
|
---|
693 | * Check that remaining bits of the first two entries are
|
---|
694 | * set to one.
|
---|
695 | */
|
---|
696 | if ((e0 >> 8) != 0xff || e1 != 0xffff)
|
---|
697 | return ENOTSUP;
|
---|
698 | }
|
---|
699 |
|
---|
700 | return EOK;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * @}
|
---|
705 | */
|
---|