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