3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2016 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 * Sector to absolute offset.
31 static off_t s2o(const struct exfat* ef, off_t sector)
33 return sector << ef->sb->sector_bits;
39 static off_t c2s(const struct exfat* ef, cluster_t cluster)
41 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
42 exfat_bug("invalid cluster number %u", cluster);
43 return le32_to_cpu(ef->sb->cluster_sector_start) +
44 ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
48 * Cluster to absolute offset.
50 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
52 return s2o(ef, c2s(ef, cluster));
58 static cluster_t s2c(const struct exfat* ef, off_t sector)
60 return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
61 ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
65 * Size in bytes to size in clusters (rounded upwards).
67 static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
69 uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
70 return (bytes + cluster_size - 1) / cluster_size;
73 cluster_t exfat_next_cluster(const struct exfat* ef,
74 const struct exfat_node* node, cluster_t cluster)
79 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
80 exfat_bug("bad cluster 0x%x", cluster);
82 if (IS_CONTIGUOUS(*node))
84 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
85 + cluster * sizeof(cluster_t);
86 if (exfat_pread(ef->dev, &next, sizeof(next), fat_offset) < 0)
87 return EXFAT_CLUSTER_BAD; /* the caller should handle this and print
88 appropriate error message */
89 return le32_to_cpu(next);
92 cluster_t exfat_advance_cluster(const struct exfat* ef,
93 struct exfat_node* node, uint32_t count)
97 if (node->fptr_index > count)
100 node->fptr_cluster = node->start_cluster;
103 for (i = node->fptr_index; i < count; i++)
105 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
106 if (CLUSTER_INVALID(node->fptr_cluster))
107 break; /* the caller should handle this and print appropriate
110 node->fptr_index = count;
111 return node->fptr_cluster;
114 static cluster_t find_bit_and_set(bitmap_t* bitmap, size_t start, size_t end)
116 const size_t start_index = start / sizeof(bitmap_t) / 8;
117 const size_t end_index = DIV_ROUND_UP(end, sizeof(bitmap_t) * 8);
119 size_t start_bitindex;
123 for (i = start_index; i < end_index; i++)
125 if (bitmap[i] == ~((bitmap_t) 0))
127 start_bitindex = MAX(i * sizeof(bitmap_t) * 8, start);
128 end_bitindex = MIN((i + 1) * sizeof(bitmap_t) * 8, end);
129 for (c = start_bitindex; c < end_bitindex; c++)
130 if (BMAP_GET(bitmap, c) == 0)
133 return c + EXFAT_FIRST_DATA_CLUSTER;
136 return EXFAT_CLUSTER_END;
139 static int flush_nodes(struct exfat* ef, struct exfat_node* node)
141 struct exfat_node* p;
143 for (p = node->child; p != NULL; p = p->next)
145 int rc = flush_nodes(ef, p);
149 return exfat_flush_node(ef, node);
152 int exfat_flush_nodes(struct exfat* ef)
154 return flush_nodes(ef, ef->root);
157 int exfat_flush(struct exfat* ef)
161 if (exfat_pwrite(ef->dev, ef->cmap.chunk,
162 BMAP_SIZE(ef->cmap.chunk_size),
163 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
165 exfat_error("failed to write clusters bitmap");
168 ef->cmap.dirty = false;
174 static bool set_next_cluster(const struct exfat* ef, bool contiguous,
175 cluster_t current, cluster_t next)
182 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
183 + current * sizeof(cluster_t);
184 next_le32 = cpu_to_le32(next);
185 if (exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset) < 0)
187 exfat_error("failed to write the next cluster %#x after %#x", next,
194 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
198 hint -= EXFAT_FIRST_DATA_CLUSTER;
199 if (hint >= ef->cmap.chunk_size)
202 cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
203 if (cluster == EXFAT_CLUSTER_END)
204 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
205 if (cluster == EXFAT_CLUSTER_END)
207 exfat_error("no free space left");
208 return EXFAT_CLUSTER_END;
211 ef->cmap.dirty = true;
215 static void free_cluster(struct exfat* ef, cluster_t cluster)
217 if (CLUSTER_INVALID(cluster))
218 exfat_bug("freeing invalid cluster 0x%x", cluster);
219 if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
220 exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
223 BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
224 ef->cmap.dirty = true;
227 static bool make_noncontiguous(const struct exfat* ef, cluster_t first,
232 for (c = first; c < last; c++)
233 if (!set_next_cluster(ef, false, c, c + 1))
238 static int shrink_file(struct exfat* ef, struct exfat_node* node,
239 uint32_t current, uint32_t difference);
241 static int grow_file(struct exfat* ef, struct exfat_node* node,
242 uint32_t current, uint32_t difference)
246 uint32_t allocated = 0;
249 exfat_bug("zero clusters count passed");
251 if (node->start_cluster != EXFAT_CLUSTER_FREE)
253 /* get the last cluster of the file */
254 previous = exfat_advance_cluster(ef, node, current - 1);
255 if (CLUSTER_INVALID(previous))
257 exfat_error("invalid cluster 0x%x while growing", previous);
263 if (node->fptr_index != 0)
264 exfat_bug("non-zero pointer index (%u)", node->fptr_index);
265 /* file does not have clusters (i.e. is empty), allocate
266 the first one for it */
267 previous = allocate_cluster(ef, 0);
268 if (CLUSTER_INVALID(previous))
270 node->fptr_cluster = node->start_cluster = previous;
272 /* file consists of only one cluster, so it's contiguous */
273 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
276 while (allocated < difference)
278 next = allocate_cluster(ef, previous + 1);
279 if (CLUSTER_INVALID(next))
282 shrink_file(ef, node, current + allocated, allocated);
285 if (next != previous - 1 && IS_CONTIGUOUS(*node))
287 /* it's a pity, but we are not able to keep the file contiguous
289 if (!make_noncontiguous(ef, node->start_cluster, previous))
291 node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
292 node->flags |= EXFAT_ATTRIB_DIRTY;
294 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next))
300 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
306 static int shrink_file(struct exfat* ef, struct exfat_node* node,
307 uint32_t current, uint32_t difference)
313 exfat_bug("zero difference passed");
314 if (node->start_cluster == EXFAT_CLUSTER_FREE)
315 exfat_bug("unable to shrink empty file (%u clusters)", current);
316 if (current < difference)
317 exfat_bug("file underflow (%u < %u)", current, difference);
320 if (current > difference)
322 cluster_t last = exfat_advance_cluster(ef, node,
323 current - difference - 1);
324 if (CLUSTER_INVALID(last))
326 exfat_error("invalid cluster 0x%x while shrinking", last);
329 previous = exfat_next_cluster(ef, node, last);
330 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), last,
336 previous = node->start_cluster;
337 node->start_cluster = EXFAT_CLUSTER_FREE;
338 node->flags |= EXFAT_ATTRIB_DIRTY;
340 node->fptr_index = 0;
341 node->fptr_cluster = node->start_cluster;
343 /* free remaining clusters */
346 if (CLUSTER_INVALID(previous))
348 exfat_error("invalid cluster 0x%x while freeing after shrink",
352 next = exfat_next_cluster(ef, node, previous);
353 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
356 free_cluster(ef, previous);
362 static bool erase_raw(struct exfat* ef, size_t size, off_t offset)
364 if (exfat_pwrite(ef->dev, ef->zero_cluster, size, offset) < 0)
366 exfat_error("failed to erase %zu bytes at %"PRId64, size, offset);
372 static int erase_range(struct exfat* ef, struct exfat_node* node,
373 uint64_t begin, uint64_t end)
375 uint64_t cluster_boundary;
381 cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
382 cluster = exfat_advance_cluster(ef, node,
383 begin / CLUSTER_SIZE(*ef->sb));
384 if (CLUSTER_INVALID(cluster))
386 exfat_error("invalid cluster 0x%x while erasing", cluster);
389 /* erase from the beginning to the closest cluster boundary */
390 if (!erase_raw(ef, MIN(cluster_boundary, end) - begin,
391 exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb)))
393 /* erase whole clusters */
394 while (cluster_boundary < end)
396 cluster = exfat_next_cluster(ef, node, cluster);
397 /* the cluster cannot be invalid because we have just allocated it */
398 if (CLUSTER_INVALID(cluster))
399 exfat_bug("invalid cluster 0x%x after allocation", cluster);
400 if (!erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster)))
402 cluster_boundary += CLUSTER_SIZE(*ef->sb);
407 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
410 uint32_t c1 = bytes2clusters(ef, node->size);
411 uint32_t c2 = bytes2clusters(ef, size);
414 if (node->references == 0 && node->parent)
415 exfat_bug("no references, node changes can be lost");
417 if (node->size == size)
421 rc = grow_file(ef, node, c1, c2 - c1);
423 rc = shrink_file(ef, node, c1, c1 - c2);
430 rc = erase_range(ef, node, node->size, size);
435 exfat_update_mtime(node);
437 node->flags |= EXFAT_ATTRIB_DIRTY;
441 uint32_t exfat_count_free_clusters(const struct exfat* ef)
443 uint32_t free_clusters = 0;
446 for (i = 0; i < ef->cmap.size; i++)
447 if (BMAP_GET(ef->cmap.chunk, i) == 0)
449 return free_clusters;
452 static int find_used_clusters(const struct exfat* ef,
453 cluster_t* a, cluster_t* b)
455 const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
457 /* find first used cluster */
458 for (*a = *b + 1; *a < end; (*a)++)
459 if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
464 /* find last contiguous used cluster */
465 for (*b = *a; *b < end; (*b)++)
466 if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
475 int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
479 if (*a == 0 && *b == 0)
480 ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
486 if (find_used_clusters(ef, &ca, &cb) != 0)
488 if (*a != 0 || *b != 0)
490 *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);