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 CLBN2PBN(bs, cl, bn) \
|
---|
57 | (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
|
---|
58 |
|
---|
59 | #define IS_ODD(number) (number & 0x1)
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * The fat_alloc_lock mutex protects all copies of the File Allocation Table
|
---|
63 | * during allocation of clusters. The lock does not have to be held durring
|
---|
64 | * deallocation of clusters.
|
---|
65 | */
|
---|
66 | static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
|
---|
67 |
|
---|
68 | /** Walk the cluster chain.
|
---|
69 | *
|
---|
70 | * @param bs Buffer holding the boot sector for the file.
|
---|
71 | * @param devmap_handle Device handle of the device with the file.
|
---|
72 | * @param firstc First cluster to start the walk with.
|
---|
73 | * @param lastc If non-NULL, output argument hodling the last cluster
|
---|
74 | * number visited.
|
---|
75 | * @param numc If non-NULL, output argument holding the number of
|
---|
76 | * clusters seen during the walk.
|
---|
77 | * @param max_clusters Maximum number of clusters to visit.
|
---|
78 | *
|
---|
79 | * @return EOK on success or a negative error code.
|
---|
80 | */
|
---|
81 | int
|
---|
82 | fat_cluster_walk(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc,
|
---|
83 | fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
|
---|
84 | {
|
---|
85 | uint16_t clusters = 0;
|
---|
86 | fat_cluster_t clst = firstc, clst_last1 = FAT_CLST_LAST1(bs);
|
---|
87 | fat_cluster_t clst_bad = FAT_CLST_BAD(bs);
|
---|
88 | int rc;
|
---|
89 |
|
---|
90 | if (firstc == FAT_CLST_RES0) {
|
---|
91 | /* No space allocated to the file. */
|
---|
92 | if (lastc)
|
---|
93 | *lastc = firstc;
|
---|
94 | if (numc)
|
---|
95 | *numc = 0;
|
---|
96 | return EOK;
|
---|
97 | }
|
---|
98 |
|
---|
99 | while (clst < clst_last1 && clusters < max_clusters) {
|
---|
100 | assert(clst >= FAT_CLST_FIRST);
|
---|
101 | if (lastc)
|
---|
102 | *lastc = clst; /* remember the last cluster number */
|
---|
103 |
|
---|
104 | /* read FAT1 */
|
---|
105 | rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &clst);
|
---|
106 | if (rc != EOK)
|
---|
107 | return rc;
|
---|
108 |
|
---|
109 | assert(clst != clst_bad);
|
---|
110 | clusters++;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (lastc && clst < clst_last1)
|
---|
114 | *lastc = clst;
|
---|
115 | if (numc)
|
---|
116 | *numc = clusters;
|
---|
117 |
|
---|
118 | return EOK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Read block from file located on a FAT file system.
|
---|
122 | *
|
---|
123 | * @param block Pointer to a block pointer for storing result.
|
---|
124 | * @param bs Buffer holding the boot sector of the file system.
|
---|
125 | * @param nodep FAT node.
|
---|
126 | * @param bn Block number.
|
---|
127 | * @param flags Flags passed to libblock.
|
---|
128 | *
|
---|
129 | * @return EOK on success or a negative error code.
|
---|
130 | */
|
---|
131 | int
|
---|
132 | fat_block_get(block_t **block, struct fat_bs *bs, fat_node_t *nodep,
|
---|
133 | aoff64_t bn, int flags)
|
---|
134 | {
|
---|
135 | fat_cluster_t firstc = nodep->firstc;
|
---|
136 | fat_cluster_t currc;
|
---|
137 | aoff64_t relbn = bn;
|
---|
138 | int rc;
|
---|
139 |
|
---|
140 | if (!nodep->size)
|
---|
141 | return ELIMIT;
|
---|
142 |
|
---|
143 | if (!FAT_IS_FAT32(bs) && nodep->firstc == FAT_CLST_ROOT)
|
---|
144 | goto fall_through;
|
---|
145 |
|
---|
146 | if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
|
---|
147 | nodep->lastc_cached_valid) {
|
---|
148 | /*
|
---|
149 | * This is a request to read a block within the last cluster
|
---|
150 | * when fortunately we have the last cluster number cached.
|
---|
151 | */
|
---|
152 | return block_get(block, nodep->idx->devmap_handle,
|
---|
153 | CLBN2PBN(bs, nodep->lastc_cached_value, bn), flags);
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
|
---|
157 | /*
|
---|
158 | * We can start with the cluster cached by the previous call to
|
---|
159 | * fat_block_get().
|
---|
160 | */
|
---|
161 | firstc = nodep->currc_cached_value;
|
---|
162 | relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
|
---|
163 | }
|
---|
164 |
|
---|
165 | fall_through:
|
---|
166 | rc = _fat_block_get(block, bs, nodep->idx->devmap_handle, firstc,
|
---|
167 | &currc, relbn, flags);
|
---|
168 | if (rc != EOK)
|
---|
169 | return rc;
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Update the "current" cluster cache.
|
---|
173 | */
|
---|
174 | nodep->currc_cached_valid = true;
|
---|
175 | nodep->currc_cached_bn = bn;
|
---|
176 | nodep->currc_cached_value = currc;
|
---|
177 |
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** Read block from file located on a FAT file system.
|
---|
182 | *
|
---|
183 | * @param block Pointer to a block pointer for storing result.
|
---|
184 | * @param bs Buffer holding the boot sector of the file system.
|
---|
185 | * @param devmap_handle Device handle of the file system.
|
---|
186 | * @param fcl First cluster used by the file. Can be zero if the file
|
---|
187 | * is empty.
|
---|
188 | * @param clp If not NULL, address where the cluster containing bn
|
---|
189 | * will be stored.
|
---|
190 | * stored
|
---|
191 | * @param bn Block number.
|
---|
192 | * @param flags Flags passed to libblock.
|
---|
193 | *
|
---|
194 | * @return EOK on success or a negative error code.
|
---|
195 | */
|
---|
196 | int
|
---|
197 | _fat_block_get(block_t **block, fat_bs_t *bs, devmap_handle_t devmap_handle,
|
---|
198 | fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags)
|
---|
199 | {
|
---|
200 | uint16_t clusters;
|
---|
201 | unsigned max_clusters;
|
---|
202 | fat_cluster_t c;
|
---|
203 | int rc;
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * This function can only operate on non-zero length files.
|
---|
207 | */
|
---|
208 | if (fcl == FAT_CLST_RES0)
|
---|
209 | return ELIMIT;
|
---|
210 |
|
---|
211 | if (!FAT_IS_FAT32(bs) && fcl == FAT_CLST_ROOT) {
|
---|
212 | /* root directory special case */
|
---|
213 | assert(bn < RDS(bs));
|
---|
214 | rc = block_get(block, devmap_handle,
|
---|
215 | RSCNT(bs) + FATCNT(bs) * SF(bs) + bn, flags);
|
---|
216 | return rc;
|
---|
217 | }
|
---|
218 |
|
---|
219 | max_clusters = bn / SPC(bs);
|
---|
220 | rc = fat_cluster_walk(bs, devmap_handle, fcl, &c, &clusters, max_clusters);
|
---|
221 | if (rc != EOK)
|
---|
222 | return rc;
|
---|
223 | assert(clusters == max_clusters);
|
---|
224 |
|
---|
225 | rc = block_get(block, devmap_handle, CLBN2PBN(bs, c, bn), flags);
|
---|
226 |
|
---|
227 | if (clp)
|
---|
228 | *clp = c;
|
---|
229 |
|
---|
230 | return rc;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** Fill the gap between EOF and a new file position.
|
---|
234 | *
|
---|
235 | * @param bs Buffer holding the boot sector for nodep.
|
---|
236 | * @param nodep FAT node with the gap.
|
---|
237 | * @param mcl First cluster in an independent cluster chain that will
|
---|
238 | * be later appended to the end of the node's own cluster
|
---|
239 | * chain. If pos is still in the last allocated cluster,
|
---|
240 | * this argument is ignored.
|
---|
241 | * @param pos Position in the last node block.
|
---|
242 | *
|
---|
243 | * @return EOK on success or a negative error code.
|
---|
244 | */
|
---|
245 | int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
|
---|
246 | {
|
---|
247 | block_t *b;
|
---|
248 | aoff64_t o, boundary;
|
---|
249 | int rc;
|
---|
250 |
|
---|
251 | boundary = ROUND_UP(nodep->size, BPS(bs) * SPC(bs));
|
---|
252 |
|
---|
253 | /* zero out already allocated space */
|
---|
254 | for (o = nodep->size; o < pos && o < boundary;
|
---|
255 | o = ALIGN_DOWN(o + BPS(bs), BPS(bs))) {
|
---|
256 | int flags = (o % BPS(bs) == 0) ?
|
---|
257 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
|
---|
258 | rc = fat_block_get(&b, bs, nodep, o / BPS(bs), flags);
|
---|
259 | if (rc != EOK)
|
---|
260 | return rc;
|
---|
261 | memset(b->data + o % BPS(bs), 0, BPS(bs) - o % BPS(bs));
|
---|
262 | b->dirty = true; /* need to sync node */
|
---|
263 | rc = block_put(b);
|
---|
264 | if (rc != EOK)
|
---|
265 | return rc;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (o >= pos)
|
---|
269 | return EOK;
|
---|
270 |
|
---|
271 | /* zero out the initial part of the new cluster chain */
|
---|
272 | for (o = boundary; o < pos; o += BPS(bs)) {
|
---|
273 | rc = _fat_block_get(&b, bs, nodep->idx->devmap_handle, mcl,
|
---|
274 | NULL, (o - boundary) / BPS(bs), BLOCK_FLAGS_NOREAD);
|
---|
275 | if (rc != EOK)
|
---|
276 | return rc;
|
---|
277 | memset(b->data, 0, min(BPS(bs), pos - o));
|
---|
278 | b->dirty = true; /* need to sync node */
|
---|
279 | rc = block_put(b);
|
---|
280 | if (rc != EOK)
|
---|
281 | return rc;
|
---|
282 | }
|
---|
283 |
|
---|
284 | return EOK;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /** Get cluster from the first FAT. FAT12 version
|
---|
288 | *
|
---|
289 | * @param bs Buffer holding the boot sector for the file system.
|
---|
290 | * @param devmap_handle Device handle for the file system.
|
---|
291 | * @param clst Cluster which to get.
|
---|
292 | * @param value Output argument holding the value of the cluster.
|
---|
293 | *
|
---|
294 | * @return EOK or a negative error code.
|
---|
295 | */
|
---|
296 | int
|
---|
297 | fat_get_cluster_fat12(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
298 | fat_cluster_t clst, fat_cluster_t *value)
|
---|
299 | {
|
---|
300 | block_t *b, *b1;
|
---|
301 | uint16_t byte1, byte2;
|
---|
302 | aoff64_t offset;
|
---|
303 | int rc;
|
---|
304 |
|
---|
305 | offset = (clst + clst/2);
|
---|
306 | if (offset / BPS(bs) >= SF(bs))
|
---|
307 | return ERANGE;
|
---|
308 |
|
---|
309 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
---|
310 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
311 | if (rc != EOK)
|
---|
312 | return rc;
|
---|
313 |
|
---|
314 | byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
|
---|
315 | /* This cluster access spans a sector boundary. Check only for FAT12 */
|
---|
316 | if ((offset % BPS(bs)) + 1 == BPS(bs)) {
|
---|
317 | /* Is it last sector of FAT? */
|
---|
318 | if (offset / BPS(bs) < SF(bs)) {
|
---|
319 | /* No. Reading next sector */
|
---|
320 | rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
|
---|
321 | SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
322 | if (rc != EOK) {
|
---|
323 | block_put(b);
|
---|
324 | return rc;
|
---|
325 | }
|
---|
326 | /*
|
---|
327 | * Combining value with last byte of current sector and
|
---|
328 | * first byte of next sector
|
---|
329 | */
|
---|
330 | byte2 = ((uint8_t*) b1->data)[0];
|
---|
331 |
|
---|
332 | rc = block_put(b1);
|
---|
333 | if (rc != EOK) {
|
---|
334 | block_put(b);
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | else {
|
---|
339 | /* Yes. It is last sector of FAT */
|
---|
340 | block_put(b);
|
---|
341 | return ERANGE;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | else
|
---|
345 | byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
|
---|
346 |
|
---|
347 | #ifdef __BE__
|
---|
348 | *value = byte2 | (byte1 << 8);
|
---|
349 | #else
|
---|
350 | *value = byte1 | (byte2 << 8);
|
---|
351 | #endif
|
---|
352 |
|
---|
353 | *value = uint16_t_le2host(*value);
|
---|
354 | if (IS_ODD(clst))
|
---|
355 | *value = (*value) >> 4;
|
---|
356 | else
|
---|
357 | *value = (*value) & FAT12_MASK;
|
---|
358 |
|
---|
359 | rc = block_put(b);
|
---|
360 | return rc;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Get cluster from the first FAT. FAT16 version
|
---|
364 | *
|
---|
365 | * @param bs Buffer holding the boot sector for the file system.
|
---|
366 | * @param devmap_handle Device handle for the file system.
|
---|
367 | * @param clst Cluster which to get.
|
---|
368 | * @param value Output argument holding the value of the cluster.
|
---|
369 | *
|
---|
370 | * @return EOK or a negative error code.
|
---|
371 | */
|
---|
372 | int
|
---|
373 | fat_get_cluster_fat16(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
374 | fat_cluster_t clst, fat_cluster_t *value)
|
---|
375 | {
|
---|
376 | block_t *b;
|
---|
377 | aoff64_t offset;
|
---|
378 | int rc;
|
---|
379 |
|
---|
380 | offset = (clst * FAT16_CLST_SIZE);
|
---|
381 |
|
---|
382 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
---|
383 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
384 | if (rc != EOK)
|
---|
385 | return rc;
|
---|
386 |
|
---|
387 | *value = uint16_t_le2host(*(uint16_t *)(b->data + offset % BPS(bs)));
|
---|
388 |
|
---|
389 | rc = block_put(b);
|
---|
390 |
|
---|
391 | return rc;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /** Get cluster from the first FAT. FAT32 version
|
---|
395 | *
|
---|
396 | * @param bs Buffer holding the boot sector for the file system.
|
---|
397 | * @param devmap_handle Device handle for the file system.
|
---|
398 | * @param clst Cluster which to get.
|
---|
399 | * @param value Output argument holding the value of the cluster.
|
---|
400 | *
|
---|
401 | * @return EOK or a negative error code.
|
---|
402 | */
|
---|
403 | int
|
---|
404 | fat_get_cluster_fat32(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
405 | fat_cluster_t clst, fat_cluster_t *value)
|
---|
406 | {
|
---|
407 | block_t *b;
|
---|
408 | aoff64_t offset;
|
---|
409 | int rc;
|
---|
410 |
|
---|
411 | offset = (clst * FAT32_CLST_SIZE);
|
---|
412 |
|
---|
413 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
---|
414 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
415 | if (rc != EOK)
|
---|
416 | return rc;
|
---|
417 |
|
---|
418 | *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) & FAT32_MASK;
|
---|
419 |
|
---|
420 | rc = block_put(b);
|
---|
421 |
|
---|
422 | return rc;
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | /** Get cluster from the first FAT.
|
---|
427 | *
|
---|
428 | * @param bs Buffer holding the boot sector for the file system.
|
---|
429 | * @param devmap_handle Device handle for the file system.
|
---|
430 | * @param clst Cluster which to get.
|
---|
431 | * @param value Output argument holding the value of the cluster.
|
---|
432 | *
|
---|
433 | * @return EOK or a negative error code.
|
---|
434 | */
|
---|
435 | int
|
---|
436 | fat_get_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
437 | fat_cluster_t clst, fat_cluster_t *value)
|
---|
438 | {
|
---|
439 | int rc;
|
---|
440 |
|
---|
441 | assert(fatno < FATCNT(bs));
|
---|
442 |
|
---|
443 | if (FAT_IS_FAT12(bs)) {
|
---|
444 | rc = fat_get_cluster_fat12(bs, devmap_handle, fatno, clst, value);
|
---|
445 | }
|
---|
446 | else {
|
---|
447 | if (FAT_IS_FAT32(bs))
|
---|
448 | rc = fat_get_cluster_fat32(bs, devmap_handle, fatno, clst, value);
|
---|
449 | else
|
---|
450 | rc = fat_get_cluster_fat16(bs, devmap_handle, fatno, clst, value);
|
---|
451 | }
|
---|
452 |
|
---|
453 | return rc;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /** Set cluster in one instance of FAT. FAT12 version.
|
---|
457 | *
|
---|
458 | * @param bs Buffer holding the boot sector for the file system.
|
---|
459 | * @param devmap_handle Device handle for the file system.
|
---|
460 | * @param fatno Number of the FAT instance where to make the change.
|
---|
461 | * @param clst Cluster which is to be set.
|
---|
462 | * @param value Value to set the cluster with.
|
---|
463 | *
|
---|
464 | * @return EOK on success or a negative error code.
|
---|
465 | */
|
---|
466 | int
|
---|
467 | fat_set_cluster_fat12(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
468 | fat_cluster_t clst, fat_cluster_t value)
|
---|
469 | {
|
---|
470 | block_t *b, *b1=NULL;
|
---|
471 | aoff64_t offset;
|
---|
472 | uint16_t byte1, byte2;
|
---|
473 | int rc;
|
---|
474 |
|
---|
475 | offset = (clst + clst/2);
|
---|
476 | if (offset / BPS(bs) >= SF(bs))
|
---|
477 | return ERANGE;
|
---|
478 |
|
---|
479 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
---|
480 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
481 | if (rc != EOK)
|
---|
482 | return rc;
|
---|
483 |
|
---|
484 | byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
|
---|
485 | bool border = false;
|
---|
486 | /* This cluster access spans a sector boundary. Check only for FAT12 */
|
---|
487 | if ((offset % BPS(bs))+1 == BPS(bs)) {
|
---|
488 | /* Is it last sector of FAT? */
|
---|
489 | if (offset / BPS(bs) < SF(bs)) {
|
---|
490 | /* No. Reading next sector */
|
---|
491 | rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
|
---|
492 | SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
493 | if (rc != EOK) {
|
---|
494 | block_put(b);
|
---|
495 | return rc;
|
---|
496 | }
|
---|
497 | /*
|
---|
498 | * Combining value with last byte of current sector and
|
---|
499 | * first byte of next sector
|
---|
500 | */
|
---|
501 | byte2 = ((uint8_t*) b1->data)[0];
|
---|
502 | border = true;
|
---|
503 | }
|
---|
504 | else {
|
---|
505 | /* Yes. It is last sector of fat */
|
---|
506 | block_put(b);
|
---|
507 | return ERANGE;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | else
|
---|
511 | byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
|
---|
512 |
|
---|
513 | if (IS_ODD(clst)) {
|
---|
514 | byte1 &= 0x0f;
|
---|
515 | byte2 = 0;
|
---|
516 | value = (value << 4);
|
---|
517 | } else {
|
---|
518 | byte1 = 0;
|
---|
519 | byte2 &= 0xf0;
|
---|
520 | value &= FAT12_MASK;
|
---|
521 | }
|
---|
522 |
|
---|
523 | byte1 = byte1 | (value & 0xff);
|
---|
524 | byte2 = byte2 | (value >> 8);
|
---|
525 |
|
---|
526 | ((uint8_t*) b->data)[(offset % BPS(bs))] = byte1;
|
---|
527 | if (border) {
|
---|
528 | ((uint8_t*) b1->data)[0] = byte2;
|
---|
529 |
|
---|
530 | b1->dirty = true;
|
---|
531 | rc = block_put(b1);
|
---|
532 | if (rc != EOK) {
|
---|
533 | block_put(b);
|
---|
534 | return rc;
|
---|
535 | }
|
---|
536 | } else
|
---|
537 | ((uint8_t*) b->data)[(offset % BPS(bs))+1] = byte2;
|
---|
538 |
|
---|
539 | b->dirty = true; /* need to sync block */
|
---|
540 | rc = block_put(b);
|
---|
541 | return rc;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /** Set cluster in one instance of FAT. FAT16 version.
|
---|
545 | *
|
---|
546 | * @param bs Buffer holding the boot sector for the file system.
|
---|
547 | * @param devmap_handle Device handle for the file system.
|
---|
548 | * @param fatno Number of the FAT instance where to make the change.
|
---|
549 | * @param clst Cluster which is to be set.
|
---|
550 | * @param value Value to set the cluster with.
|
---|
551 | *
|
---|
552 | * @return EOK on success or a negative error code.
|
---|
553 | */
|
---|
554 | int
|
---|
555 | fat_set_cluster_fat16(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
556 | fat_cluster_t clst, fat_cluster_t value)
|
---|
557 | {
|
---|
558 | block_t *b;
|
---|
559 | aoff64_t offset;
|
---|
560 | int rc;
|
---|
561 |
|
---|
562 | offset = (clst * FAT16_CLST_SIZE);
|
---|
563 |
|
---|
564 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
---|
565 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
566 | if (rc != EOK)
|
---|
567 | return rc;
|
---|
568 |
|
---|
569 | *(uint16_t *)(b->data + offset % BPS(bs)) = host2uint16_t_le(value);
|
---|
570 |
|
---|
571 | b->dirty = true; /* need to sync block */
|
---|
572 | rc = block_put(b);
|
---|
573 | return rc;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /** Set cluster in one instance of FAT. FAT32 version.
|
---|
577 | *
|
---|
578 | * @param bs Buffer holding the boot sector for the file system.
|
---|
579 | * @param devmap_handle Device handle for the file system.
|
---|
580 | * @param fatno Number of the FAT instance where to make the change.
|
---|
581 | * @param clst Cluster which is to be set.
|
---|
582 | * @param value Value to set the cluster with.
|
---|
583 | *
|
---|
584 | * @return EOK on success or a negative error code.
|
---|
585 | */
|
---|
586 | int
|
---|
587 | fat_set_cluster_fat32(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
588 | fat_cluster_t clst, fat_cluster_t value)
|
---|
589 | {
|
---|
590 | block_t *b;
|
---|
591 | aoff64_t offset;
|
---|
592 | int rc;
|
---|
593 |
|
---|
594 | offset = (clst * FAT32_CLST_SIZE);
|
---|
595 |
|
---|
596 | rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
|
---|
597 | offset / BPS(bs), BLOCK_FLAGS_NONE);
|
---|
598 | if (rc != EOK)
|
---|
599 | return rc;
|
---|
600 |
|
---|
601 | value = host2uint32_t_le(value);
|
---|
602 | *(uint32_t *)(b->data + offset % BPS(bs)) &= 0xf0000000;
|
---|
603 | *(uint32_t *)(b->data + offset % BPS(bs)) |= (value & FAT32_MASK);
|
---|
604 |
|
---|
605 | b->dirty = true; /* need to sync block */
|
---|
606 | rc = block_put(b);
|
---|
607 | return rc;
|
---|
608 | }
|
---|
609 |
|
---|
610 | /** Set cluster in one instance of FAT.
|
---|
611 | *
|
---|
612 | * @param bs Buffer holding the boot sector for the file system.
|
---|
613 | * @param devmap_handle Device handle for the file system.
|
---|
614 | * @param fatno Number of the FAT instance where to make the change.
|
---|
615 | * @param clst Cluster which is to be set.
|
---|
616 | * @param value Value to set the cluster with.
|
---|
617 | *
|
---|
618 | * @return EOK on success or a negative error code.
|
---|
619 | */
|
---|
620 | int
|
---|
621 | fat_set_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
|
---|
622 | fat_cluster_t clst, fat_cluster_t value)
|
---|
623 | {
|
---|
624 | int rc;
|
---|
625 |
|
---|
626 | assert(fatno < FATCNT(bs));
|
---|
627 |
|
---|
628 | if (FAT_IS_FAT12(bs))
|
---|
629 | rc = fat_set_cluster_fat12(bs, devmap_handle, fatno, clst, value);
|
---|
630 | else if (FAT_IS_FAT32(bs))
|
---|
631 | rc = fat_set_cluster_fat32(bs, devmap_handle, fatno, clst, value);
|
---|
632 | else
|
---|
633 | rc = fat_set_cluster_fat16(bs, devmap_handle, fatno, clst, value);
|
---|
634 |
|
---|
635 | return rc;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /** Replay the allocatoin of clusters in all shadow instances of FAT.
|
---|
639 | *
|
---|
640 | * @param bs Buffer holding the boot sector of the file system.
|
---|
641 | * @param devmap_handle Device handle of the file system.
|
---|
642 | * @param lifo Chain of allocated clusters.
|
---|
643 | * @param nclsts Number of clusters in the lifo chain.
|
---|
644 | *
|
---|
645 | * @return EOK on success or a negative error code.
|
---|
646 | */
|
---|
647 | int fat_alloc_shadow_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle,
|
---|
648 | fat_cluster_t *lifo, unsigned nclsts)
|
---|
649 | {
|
---|
650 | uint8_t fatno;
|
---|
651 | unsigned c;
|
---|
652 | fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
|
---|
653 | int rc;
|
---|
654 |
|
---|
655 | for (fatno = FAT1 + 1; fatno < FATCNT(bs); fatno++) {
|
---|
656 | for (c = 0; c < nclsts; c++) {
|
---|
657 | rc = fat_set_cluster(bs, devmap_handle, fatno, lifo[c],
|
---|
658 | c == 0 ? clst_last1 : lifo[c - 1]);
|
---|
659 | if (rc != EOK)
|
---|
660 | return rc;
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | return EOK;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /** Allocate clusters in all copies of FAT.
|
---|
668 | *
|
---|
669 | * This function will attempt to allocate the requested number of clusters in
|
---|
670 | * all instances of the FAT. The FAT will be altered so that the allocated
|
---|
671 | * clusters form an independent chain (i.e. a chain which does not belong to any
|
---|
672 | * file yet).
|
---|
673 | *
|
---|
674 | * @param bs Buffer holding the boot sector of the file system.
|
---|
675 | * @param devmap_handle Device handle of the file system.
|
---|
676 | * @param nclsts Number of clusters to allocate.
|
---|
677 | * @param mcl Output parameter where the first cluster in the chain
|
---|
678 | * will be returned.
|
---|
679 | * @param lcl Output parameter where the last cluster in the chain
|
---|
680 | * will be returned.
|
---|
681 | *
|
---|
682 | * @return EOK on success, a negative error code otherwise.
|
---|
683 | */
|
---|
684 | int
|
---|
685 | fat_alloc_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned nclsts,
|
---|
686 | fat_cluster_t *mcl, fat_cluster_t *lcl)
|
---|
687 | {
|
---|
688 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */
|
---|
689 | unsigned found = 0; /* top of the free cluster number stack */
|
---|
690 | fat_cluster_t clst, value, clst_last1 = FAT_CLST_LAST1(bs);
|
---|
691 | int rc = EOK;
|
---|
692 |
|
---|
693 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
|
---|
694 | if (!lifo)
|
---|
695 | return ENOMEM;
|
---|
696 | /*
|
---|
697 | * Search FAT1 for unused clusters.
|
---|
698 | */
|
---|
699 | fibril_mutex_lock(&fat_alloc_lock);
|
---|
700 | for (clst=FAT_CLST_FIRST; clst < CC(bs)+2 && found < nclsts; clst++) {
|
---|
701 | rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &value);
|
---|
702 | if (rc != EOK)
|
---|
703 | break;
|
---|
704 |
|
---|
705 | if (value == FAT_CLST_RES0) {
|
---|
706 | /*
|
---|
707 | * The cluster is free. Put it into our stack
|
---|
708 | * of found clusters and mark it as non-free.
|
---|
709 | */
|
---|
710 | lifo[found] = clst;
|
---|
711 | rc = fat_set_cluster(bs, devmap_handle, FAT1, clst,
|
---|
712 | (found == 0) ? clst_last1 : lifo[found - 1]);
|
---|
713 | if (rc != EOK)
|
---|
714 | break;
|
---|
715 |
|
---|
716 | found++;
|
---|
717 | }
|
---|
718 | }
|
---|
719 |
|
---|
720 | if (rc == EOK && found == nclsts) {
|
---|
721 | rc = fat_alloc_shadow_clusters(bs, devmap_handle, lifo, nclsts);
|
---|
722 | if (rc == EOK) {
|
---|
723 | *mcl = lifo[found - 1];
|
---|
724 | *lcl = lifo[0];
|
---|
725 | free(lifo);
|
---|
726 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
727 | return EOK;
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | /* If something wrong - free the clusters */
|
---|
732 | if (found > 0) {
|
---|
733 | while (found--) {
|
---|
734 | rc = fat_set_cluster(bs, devmap_handle, FAT1, lifo[found],
|
---|
735 | FAT_CLST_RES0);
|
---|
736 | }
|
---|
737 | }
|
---|
738 |
|
---|
739 | free(lifo);
|
---|
740 | fibril_mutex_unlock(&fat_alloc_lock);
|
---|
741 | return ENOSPC;
|
---|
742 | }
|
---|
743 |
|
---|
744 | /** Free clusters forming a cluster chain in all copies of FAT.
|
---|
745 | *
|
---|
746 | * @param bs Buffer hodling the boot sector of the file system.
|
---|
747 | * @param devmap_handle Device handle of the file system.
|
---|
748 | * @param firstc First cluster in the chain which is to be freed.
|
---|
749 | *
|
---|
750 | * @return EOK on success or a negative return code.
|
---|
751 | */
|
---|
752 | int
|
---|
753 | fat_free_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc)
|
---|
754 | {
|
---|
755 | unsigned fatno;
|
---|
756 | fat_cluster_t nextc, clst_bad = FAT_CLST_BAD(bs);
|
---|
757 | int rc;
|
---|
758 |
|
---|
759 | /* Mark all clusters in the chain as free in all copies of FAT. */
|
---|
760 | while (firstc < FAT_CLST_LAST1(bs)) {
|
---|
761 | assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad);
|
---|
762 | rc = fat_get_cluster(bs, devmap_handle, FAT1, firstc, &nextc);
|
---|
763 | if (rc != EOK)
|
---|
764 | return rc;
|
---|
765 | for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
|
---|
766 | rc = fat_set_cluster(bs, devmap_handle, fatno, firstc,
|
---|
767 | FAT_CLST_RES0);
|
---|
768 | if (rc != EOK)
|
---|
769 | return rc;
|
---|
770 | }
|
---|
771 |
|
---|
772 | firstc = nextc;
|
---|
773 | }
|
---|
774 |
|
---|
775 | return EOK;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /** Append a cluster chain to the last file cluster in all FATs.
|
---|
779 | *
|
---|
780 | * @param bs Buffer holding the boot sector of the file system.
|
---|
781 | * @param nodep Node representing the file.
|
---|
782 | * @param mcl First cluster of the cluster chain to append.
|
---|
783 | * @param lcl Last cluster of the cluster chain to append.
|
---|
784 | *
|
---|
785 | * @return EOK on success or a negative error code.
|
---|
786 | */
|
---|
787 | int
|
---|
788 | fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
|
---|
789 | fat_cluster_t lcl)
|
---|
790 | {
|
---|
791 | devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
|
---|
792 | fat_cluster_t lastc;
|
---|
793 | uint8_t fatno;
|
---|
794 | int rc;
|
---|
795 |
|
---|
796 | if (nodep->firstc == FAT_CLST_RES0) {
|
---|
797 | /* No clusters allocated to the node yet. */
|
---|
798 | nodep->firstc = mcl;
|
---|
799 | nodep->dirty = true; /* need to sync node */
|
---|
800 | } else {
|
---|
801 | if (nodep->lastc_cached_valid) {
|
---|
802 | lastc = nodep->lastc_cached_value;
|
---|
803 | nodep->lastc_cached_valid = false;
|
---|
804 | } else {
|
---|
805 | rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
|
---|
806 | &lastc, NULL, (uint16_t) -1);
|
---|
807 | if (rc != EOK)
|
---|
808 | return rc;
|
---|
809 | }
|
---|
810 |
|
---|
811 | for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
|
---|
812 | rc = fat_set_cluster(bs, nodep->idx->devmap_handle,
|
---|
813 | fatno, lastc, mcl);
|
---|
814 | if (rc != EOK)
|
---|
815 | return rc;
|
---|
816 | }
|
---|
817 | }
|
---|
818 |
|
---|
819 | nodep->lastc_cached_valid = true;
|
---|
820 | nodep->lastc_cached_value = lcl;
|
---|
821 |
|
---|
822 | return EOK;
|
---|
823 | }
|
---|
824 |
|
---|
825 | /** Chop off node clusters in all copies of FAT.
|
---|
826 | *
|
---|
827 | * @param bs Buffer holding the boot sector of the file system.
|
---|
828 | * @param nodep FAT node where the chopping will take place.
|
---|
829 | * @param lcl Last cluster which will remain in the node. If this
|
---|
830 | * argument is FAT_CLST_RES0, then all clusters will
|
---|
831 | * be chopped off.
|
---|
832 | *
|
---|
833 | * @return EOK on success or a negative return code.
|
---|
834 | */
|
---|
835 | int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
|
---|
836 | {
|
---|
837 | fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
|
---|
838 | int rc;
|
---|
839 | devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
|
---|
840 |
|
---|
841 | /*
|
---|
842 | * Invalidate cached cluster numbers.
|
---|
843 | */
|
---|
844 | nodep->lastc_cached_valid = false;
|
---|
845 | if (nodep->currc_cached_value != lcl)
|
---|
846 | nodep->currc_cached_valid = false;
|
---|
847 |
|
---|
848 | if (lcl == FAT_CLST_RES0) {
|
---|
849 | /* The node will have zero size and no clusters allocated. */
|
---|
850 | rc = fat_free_clusters(bs, devmap_handle, nodep->firstc);
|
---|
851 | if (rc != EOK)
|
---|
852 | return rc;
|
---|
853 | nodep->firstc = FAT_CLST_RES0;
|
---|
854 | nodep->dirty = true; /* need to sync node */
|
---|
855 | } else {
|
---|
856 | fat_cluster_t nextc;
|
---|
857 | unsigned fatno;
|
---|
858 |
|
---|
859 | rc = fat_get_cluster(bs, devmap_handle, FAT1, lcl, &nextc);
|
---|
860 | if (rc != EOK)
|
---|
861 | return rc;
|
---|
862 |
|
---|
863 | /* Terminate the cluster chain in all copies of FAT. */
|
---|
864 | for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
|
---|
865 | rc = fat_set_cluster(bs, devmap_handle, fatno, lcl,
|
---|
866 | clst_last1);
|
---|
867 | if (rc != EOK)
|
---|
868 | return rc;
|
---|
869 | }
|
---|
870 |
|
---|
871 | /* Free all following clusters. */
|
---|
872 | rc = fat_free_clusters(bs, devmap_handle, nextc);
|
---|
873 | if (rc != EOK)
|
---|
874 | return rc;
|
---|
875 | }
|
---|
876 |
|
---|
877 | /*
|
---|
878 | * Update and re-enable the last cluster cache.
|
---|
879 | */
|
---|
880 | nodep->lastc_cached_valid = true;
|
---|
881 | nodep->lastc_cached_value = lcl;
|
---|
882 |
|
---|
883 | return EOK;
|
---|
884 | }
|
---|
885 |
|
---|
886 | int
|
---|
887 | fat_zero_cluster(struct fat_bs *bs, devmap_handle_t devmap_handle, fat_cluster_t c)
|
---|
888 | {
|
---|
889 | int i;
|
---|
890 | block_t *b;
|
---|
891 | int rc;
|
---|
892 |
|
---|
893 | for (i = 0; i < SPC(bs); i++) {
|
---|
894 | rc = _fat_block_get(&b, bs, devmap_handle, c, NULL, i,
|
---|
895 | BLOCK_FLAGS_NOREAD);
|
---|
896 | if (rc != EOK)
|
---|
897 | return rc;
|
---|
898 | memset(b->data, 0, BPS(bs));
|
---|
899 | b->dirty = true;
|
---|
900 | rc = block_put(b);
|
---|
901 | if (rc != EOK)
|
---|
902 | return rc;
|
---|
903 | }
|
---|
904 |
|
---|
905 | return EOK;
|
---|
906 | }
|
---|
907 |
|
---|
908 | /** Perform basic sanity checks on the file system.
|
---|
909 | *
|
---|
910 | * Verify if values of boot sector fields are sane. Also verify media
|
---|
911 | * descriptor. This is used to rule out cases when a device obviously
|
---|
912 | * does not contain a fat file system.
|
---|
913 | */
|
---|
914 | int fat_sanity_check(fat_bs_t *bs, devmap_handle_t devmap_handle)
|
---|
915 | {
|
---|
916 | fat_cluster_t e0, e1;
|
---|
917 | unsigned fat_no;
|
---|
918 | int rc;
|
---|
919 |
|
---|
920 | /* Check number of FATs. */
|
---|
921 | if (FATCNT(bs) == 0)
|
---|
922 | return ENOTSUP;
|
---|
923 |
|
---|
924 | /* Check total number of sectors. */
|
---|
925 | if (TS(bs) == 0)
|
---|
926 | return ENOTSUP;
|
---|
927 |
|
---|
928 | if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
|
---|
929 | bs->totsec16 != bs->totsec32)
|
---|
930 | return ENOTSUP;
|
---|
931 |
|
---|
932 | /* Check media descriptor. Must be between 0xf0 and 0xff. */
|
---|
933 | if ((bs->mdesc & 0xf0) != 0xf0)
|
---|
934 | return ENOTSUP;
|
---|
935 |
|
---|
936 | /* Check number of sectors per FAT. */
|
---|
937 | if (SF(bs) == 0)
|
---|
938 | return ENOTSUP;
|
---|
939 |
|
---|
940 | /*
|
---|
941 | * Check that the root directory entries take up whole blocks.
|
---|
942 | * This check is rather strict, but it allows us to treat the root
|
---|
943 | * directory and non-root directories uniformly in some places.
|
---|
944 | * It can be removed provided that functions such as fat_read() are
|
---|
945 | * sanitized to support file systems with this property.
|
---|
946 | */
|
---|
947 | if (!FAT_IS_FAT32(bs) && (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0)
|
---|
948 | return ENOTSUP;
|
---|
949 |
|
---|
950 | /* Check signature of each FAT. */
|
---|
951 | for (fat_no = 0; fat_no < FATCNT(bs); fat_no++) {
|
---|
952 | rc = fat_get_cluster(bs, devmap_handle, fat_no, 0, &e0);
|
---|
953 | if (rc != EOK)
|
---|
954 | return EIO;
|
---|
955 |
|
---|
956 | rc = fat_get_cluster(bs, devmap_handle, fat_no, 1, &e1);
|
---|
957 | if (rc != EOK)
|
---|
958 | return EIO;
|
---|
959 |
|
---|
960 | /* Check that first byte of FAT contains the media descriptor. */
|
---|
961 | if ((e0 & 0xff) != bs->mdesc)
|
---|
962 | return ENOTSUP;
|
---|
963 |
|
---|
964 | /*
|
---|
965 | * Check that remaining bits of the first two entries are
|
---|
966 | * set to one.
|
---|
967 | */
|
---|
968 | if (!FAT_IS_FAT12(bs) &&
|
---|
969 | ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs)))
|
---|
970 | return ENOTSUP;
|
---|
971 | }
|
---|
972 |
|
---|
973 | return EOK;
|
---|
974 | }
|
---|
975 |
|
---|
976 | /**
|
---|
977 | * @}
|
---|
978 | */
|
---|