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