3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2013 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.
28 * Sector to absolute offset.
30 static off_t s2o(const struct exfat* ef, off_t sector)
32 return sector << ef->sb->sector_bits;
38 static off_t c2s(const struct exfat* ef, cluster_t cluster)
40 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
41 exfat_bug("invalid cluster number %u", cluster);
42 return le32_to_cpu(ef->sb->cluster_sector_start) +
43 ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
47 * Cluster to absolute offset.
49 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
51 return s2o(ef, c2s(ef, cluster));
57 static cluster_t s2c(const struct exfat* ef, off_t sector)
59 return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
60 ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
64 * Size in bytes to size in clusters (rounded upwards).
66 static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
68 uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
69 return (bytes + cluster_size - 1) / cluster_size;
72 cluster_t exfat_next_cluster(const struct exfat* ef,
73 const struct exfat_node* node, cluster_t cluster)
78 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
79 exfat_bug("bad cluster 0x%x", cluster);
81 if (IS_CONTIGUOUS(*node))
83 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
84 + cluster * sizeof(cluster_t);
85 exfat_pread(ef->dev, &next, sizeof(next), fat_offset);
86 return le32_to_cpu(next);
89 cluster_t exfat_advance_cluster(const struct exfat* ef,
90 struct exfat_node* node, uint32_t count)
94 if (node->fptr_index > count)
97 node->fptr_cluster = node->start_cluster;
100 for (i = node->fptr_index; i < count; i++)
102 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
103 if (CLUSTER_INVALID(node->fptr_cluster))
104 break; /* the caller should handle this and print appropriate
107 node->fptr_index = count;
108 return node->fptr_cluster;
111 static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
113 const size_t start_index = start / 8;
114 const size_t end_index = DIV_ROUND_UP(end, 8);
118 for (i = start_index; i < end_index; i++)
120 if (bitmap[i] == 0xff)
122 for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
123 if (BMAP_GET(bitmap, c) == 0)
126 return c + EXFAT_FIRST_DATA_CLUSTER;
129 return EXFAT_CLUSTER_END;
132 void exfat_flush_cmap(struct exfat* ef)
134 exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
135 exfat_c2o(ef, ef->cmap.start_cluster));
136 ef->cmap.dirty = false;
139 static void set_next_cluster(const struct exfat* ef, bool contiguous,
140 cluster_t current, cluster_t next)
147 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
148 + current * sizeof(cluster_t);
149 next_le32 = cpu_to_le32(next);
150 exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset);
153 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
157 hint -= EXFAT_FIRST_DATA_CLUSTER;
158 if (hint >= ef->cmap.chunk_size)
161 cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
162 if (cluster == EXFAT_CLUSTER_END)
163 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
164 if (cluster == EXFAT_CLUSTER_END)
166 exfat_error("no free space left");
167 return EXFAT_CLUSTER_END;
170 ef->cmap.dirty = true;
174 static void free_cluster(struct exfat* ef, cluster_t cluster)
176 if (CLUSTER_INVALID(cluster))
177 exfat_bug("freeing invalid cluster 0x%x", cluster);
178 if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
179 exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
182 BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
183 ef->cmap.dirty = true;
186 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
191 for (c = first; c < last; c++)
192 set_next_cluster(ef, false, c, c + 1);
195 static int shrink_file(struct exfat* ef, struct exfat_node* node,
196 uint32_t current, uint32_t difference);
198 static int grow_file(struct exfat* ef, struct exfat_node* node,
199 uint32_t current, uint32_t difference)
203 uint32_t allocated = 0;
206 exfat_bug("zero clusters count passed");
208 if (node->start_cluster != EXFAT_CLUSTER_FREE)
210 /* get the last cluster of the file */
211 previous = exfat_advance_cluster(ef, node, current - 1);
212 if (CLUSTER_INVALID(previous))
214 exfat_error("invalid cluster 0x%x while growing", previous);
220 if (node->fptr_index != 0)
221 exfat_bug("non-zero pointer index (%u)", node->fptr_index);
222 /* file does not have clusters (i.e. is empty), allocate
223 the first one for it */
224 previous = allocate_cluster(ef, 0);
225 if (CLUSTER_INVALID(previous))
227 node->fptr_cluster = node->start_cluster = previous;
229 /* file consists of only one cluster, so it's contiguous */
230 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
233 while (allocated < difference)
235 next = allocate_cluster(ef, previous + 1);
236 if (CLUSTER_INVALID(next))
239 shrink_file(ef, node, current + allocated, allocated);
242 if (next != previous - 1 && IS_CONTIGUOUS(*node))
244 /* it's a pity, but we are not able to keep the file contiguous
246 make_noncontiguous(ef, node->start_cluster, previous);
247 node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
248 node->flags |= EXFAT_ATTRIB_DIRTY;
250 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
255 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
259 static int shrink_file(struct exfat* ef, struct exfat_node* node,
260 uint32_t current, uint32_t difference)
266 exfat_bug("zero difference passed");
267 if (node->start_cluster == EXFAT_CLUSTER_FREE)
268 exfat_bug("unable to shrink empty file (%u clusters)", current);
269 if (current < difference)
270 exfat_bug("file underflow (%u < %u)", current, difference);
273 if (current > difference)
275 cluster_t last = exfat_advance_cluster(ef, node,
276 current - difference - 1);
277 if (CLUSTER_INVALID(last))
279 exfat_error("invalid cluster 0x%x while shrinking", last);
282 previous = exfat_next_cluster(ef, node, last);
283 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
287 previous = node->start_cluster;
288 node->start_cluster = EXFAT_CLUSTER_FREE;
290 node->fptr_index = 0;
291 node->fptr_cluster = node->start_cluster;
293 /* free remaining clusters */
296 if (CLUSTER_INVALID(previous))
298 exfat_error("invalid cluster 0x%x while freeing after shrink",
302 next = exfat_next_cluster(ef, node, previous);
303 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
305 free_cluster(ef, previous);
311 static void erase_raw(struct exfat* ef, size_t size, off_t offset)
313 exfat_pwrite(ef->dev, ef->zero_cluster, size, offset);
316 static int erase_range(struct exfat* ef, struct exfat_node* node,
317 uint64_t begin, uint64_t end)
319 uint64_t cluster_boundary;
325 cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
326 cluster = exfat_advance_cluster(ef, node,
327 begin / CLUSTER_SIZE(*ef->sb));
328 if (CLUSTER_INVALID(cluster))
330 exfat_error("invalid cluster 0x%x while erasing", cluster);
333 /* erase from the beginning to the closest cluster boundary */
334 erase_raw(ef, MIN(cluster_boundary, end) - begin,
335 exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb));
336 /* erase whole clusters */
337 while (cluster_boundary < end)
339 cluster = exfat_next_cluster(ef, node, cluster);
340 /* the cluster cannot be invalid because we have just allocated it */
341 if (CLUSTER_INVALID(cluster))
342 exfat_bug("invalid cluster 0x%x after allocation", cluster);
343 erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
344 cluster_boundary += CLUSTER_SIZE(*ef->sb);
349 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
352 uint32_t c1 = bytes2clusters(ef, node->size);
353 uint32_t c2 = bytes2clusters(ef, size);
356 if (node->references == 0 && node->parent)
357 exfat_bug("no references, node changes can be lost");
359 if (node->size == size)
363 rc = grow_file(ef, node, c1, c2 - c1);
365 rc = shrink_file(ef, node, c1, c1 - c2);
372 rc = erase_range(ef, node, node->size, size);
377 exfat_update_mtime(node);
379 node->flags |= EXFAT_ATTRIB_DIRTY;
383 uint32_t exfat_count_free_clusters(const struct exfat* ef)
385 uint32_t free_clusters = 0;
388 for (i = 0; i < ef->cmap.size; i++)
389 if (BMAP_GET(ef->cmap.chunk, i) == 0)
391 return free_clusters;
394 static int find_used_clusters(const struct exfat* ef,
395 cluster_t* a, cluster_t* b)
397 const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
399 /* find first used cluster */
400 for (*a = *b + 1; *a < end; (*a)++)
401 if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
406 /* find last contiguous used cluster */
407 for (*b = *a; *b < end; (*b)++)
408 if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
417 int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
421 if (*a == 0 && *b == 0)
422 ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
428 if (find_used_clusters(ef, &ca, &cb) != 0)
430 if (*a != 0 || *b != 0)
432 *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);