3 exFAT file system implementation library.
5 Copyright (C) 2009, 2010 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * Sector to absolute offset.
28 static off_t s2o(const struct exfat* ef, off_t sector)
30 return sector << ef->sb->sector_bits;
36 static off_t c2s(const struct exfat* ef, cluster_t cluster)
38 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
39 exfat_bug("invalid cluster number %u", cluster);
40 return le32_to_cpu(ef->sb->cluster_sector_start) +
41 ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
45 * Cluster to absolute offset.
47 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
49 return s2o(ef, c2s(ef, cluster));
55 static cluster_t s2c(const struct exfat* ef, off_t sector)
57 return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
58 ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
62 * Size in bytes to size in clusters (rounded upwards).
64 static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
66 uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
67 return (bytes + cluster_size - 1) / cluster_size;
70 cluster_t exfat_next_cluster(const struct exfat* ef,
71 const struct exfat_node* node, cluster_t cluster)
76 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
77 exfat_bug("bad cluster 0x%x", cluster);
79 if (IS_CONTIGUOUS(*node))
81 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
82 + cluster * sizeof(cluster_t);
83 exfat_read_raw(&next, sizeof(next), fat_offset, ef->fd);
84 return le32_to_cpu(next);
87 cluster_t exfat_advance_cluster(const struct exfat* ef,
88 struct exfat_node* node, uint32_t count)
92 if (node->fptr_index > count)
95 node->fptr_cluster = node->start_cluster;
98 for (i = node->fptr_index; i < count; i++)
100 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
101 if (CLUSTER_INVALID(node->fptr_cluster))
104 node->fptr_index = count;
105 return node->fptr_cluster;
108 static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
111 const cluster_t mid_start = (start + 7) / 8 * 8;
112 const cluster_t mid_end = end / 8 * 8;
116 for (c = start; c < mid_start; c++)
117 if (BMAP_GET(bitmap, c) == 0)
120 return c + EXFAT_FIRST_DATA_CLUSTER;
123 for (byte = mid_start / 8; byte < mid_end / 8; byte++)
124 if (bitmap[byte] != 0xff)
128 for (bit = 0; bit < 8; bit++)
129 if (!(bitmap[byte] & (1u << bit)))
131 bitmap[byte] |= (1u << bit);
132 return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
136 for (c = mid_end; c < end; c++)
137 if (BMAP_GET(bitmap, c) == 0)
140 return c + EXFAT_FIRST_DATA_CLUSTER;
143 return EXFAT_CLUSTER_END;
146 void exfat_flush_cmap(struct exfat* ef)
148 exfat_write_raw(ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
149 exfat_c2o(ef, ef->cmap.start_cluster), ef->fd);
153 static void set_next_cluster(const struct exfat* ef, int contiguous,
154 cluster_t current, cluster_t next)
161 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
162 + current * sizeof(cluster_t);
163 next_le32 = cpu_to_le32(next);
164 exfat_write_raw(&next_le32, sizeof(next_le32), fat_offset, ef->fd);
167 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
171 hint -= EXFAT_FIRST_DATA_CLUSTER;
172 if (hint >= ef->cmap.chunk_size)
175 cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
176 if (cluster == EXFAT_CLUSTER_END)
177 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
178 if (cluster == EXFAT_CLUSTER_END)
180 exfat_error("no free space left");
181 return EXFAT_CLUSTER_END;
185 /* FIXME update percentage of used space */
189 static void free_cluster(struct exfat* ef, cluster_t cluster)
191 if (CLUSTER_INVALID(cluster))
192 exfat_bug("attempting to free invalid cluster");
193 if (cluster < EXFAT_FIRST_DATA_CLUSTER ||
194 cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
195 exfat_bug("bad cluster 0x%x (0x%x)", cluster, ef->cmap.size);
197 BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
199 /* FIXME update percentage of used space */
202 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
207 for (c = first; c < last; c++)
208 set_next_cluster(ef, 0, c, c + 1);
211 static int shrink_file(struct exfat* ef, struct exfat_node* node,
212 uint32_t current, uint32_t difference);
214 static int grow_file(struct exfat* ef, struct exfat_node* node,
215 uint32_t current, uint32_t difference)
219 uint32_t allocated = 0;
222 exfat_bug("zero clusters count passed");
224 if (node->start_cluster != EXFAT_CLUSTER_FREE)
226 /* get the last cluster of the file */
227 previous = exfat_advance_cluster(ef, node, current - 1);
228 if (CLUSTER_INVALID(previous))
230 exfat_error("invalid cluster in file");
236 if (node->fptr_index != 0)
237 exfat_bug("non-zero pointer index (%u)", node->fptr_index);
238 /* file does not have clusters (i.e. is empty), allocate
239 the first one for it */
240 previous = allocate_cluster(ef, 0);
241 if (CLUSTER_INVALID(previous))
243 node->fptr_cluster = node->start_cluster = previous;
245 /* file consists of only one cluster, so it's contiguous */
246 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
249 while (allocated < difference)
251 next = allocate_cluster(ef, previous + 1);
252 if (CLUSTER_INVALID(next))
255 shrink_file(ef, node, current + allocated, allocated);
258 if (next != previous - 1 && IS_CONTIGUOUS(*node))
260 /* it's a pity, but we are not able to keep the file contiguous
262 make_noncontiguous(ef, node->start_cluster, previous);
263 node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
264 node->flags |= EXFAT_ATTRIB_DIRTY;
266 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
271 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
275 static int shrink_file(struct exfat* ef, struct exfat_node* node,
276 uint32_t current, uint32_t difference)
282 exfat_bug("zero difference passed");
283 if (node->start_cluster == EXFAT_CLUSTER_FREE)
284 exfat_bug("unable to shrink empty file (%u clusters)", current);
285 if (current < difference)
286 exfat_bug("file underflow (%u < %u)", current, difference);
289 if (current > difference)
291 cluster_t last = exfat_advance_cluster(ef, node,
292 current - difference - 1);
293 if (CLUSTER_INVALID(last))
295 exfat_error("invalid cluster in file");
298 previous = exfat_next_cluster(ef, node, last);
299 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
303 previous = node->start_cluster;
304 node->start_cluster = EXFAT_CLUSTER_FREE;
306 node->fptr_index = 0;
307 node->fptr_cluster = node->start_cluster;
309 /* free remaining clusters */
312 if (CLUSTER_INVALID(previous))
314 exfat_error("invalid cluster in file");
317 next = exfat_next_cluster(ef, node, previous);
318 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
320 free_cluster(ef, previous);
326 static void erase_raw(struct exfat* ef, size_t size, off_t offset)
328 exfat_write_raw(ef->zero_sector, size, offset, ef->fd);
331 static int erase_range(struct exfat* ef, struct exfat_node* node,
332 uint64_t begin, uint64_t end)
334 uint64_t sector_boundary;
340 sector_boundary = (node->size | (SECTOR_SIZE(*ef->sb) - 1)) + 1;
341 cluster = exfat_advance_cluster(ef, node,
342 node->size / CLUSTER_SIZE(*ef->sb));
343 if (CLUSTER_INVALID(cluster))
345 exfat_error("invalid cluster in file");
348 /* erase from the beginning to the closest sector boundary */
349 erase_raw(ef, MIN(sector_boundary, end) - node->size,
350 exfat_c2o(ef, cluster) + node->size % CLUSTER_SIZE(*ef->sb));
351 /* erase whole sectors */
352 while (sector_boundary < end)
354 if (sector_boundary % CLUSTER_SIZE(*ef->sb) == 0)
355 cluster = exfat_next_cluster(ef, node, cluster);
356 erase_raw(ef, SECTOR_SIZE(*ef->sb),
357 exfat_c2o(ef, cluster) + sector_boundary % CLUSTER_SIZE(*ef->sb));
358 sector_boundary += SECTOR_SIZE(*ef->sb);
363 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
365 uint32_t c1 = bytes2clusters(ef, node->size);
366 uint32_t c2 = bytes2clusters(ef, size);
369 if (node->references == 0 && node->parent)
370 exfat_bug("no references, node changes can be lost");
372 if (node->size == size)
376 rc = grow_file(ef, node, c1, c2 - c1);
378 rc = shrink_file(ef, node, c1, c1 - c2);
383 rc = erase_range(ef, node, node->size, size);
387 exfat_update_mtime(node);
389 node->flags |= EXFAT_ATTRIB_DIRTY;
393 uint32_t exfat_count_free_clusters(const struct exfat* ef)
395 uint32_t free_clusters = 0;
398 for (i = 0; i < ef->cmap.size; i++)
399 if (BMAP_GET(ef->cmap.chunk, i) == 0)
401 return free_clusters;
404 static int find_used_clusters(const struct exfat* ef,
405 cluster_t* a, cluster_t* b)
407 const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
409 /* find first used cluster */
410 for (*a = *b + 1; *a < end; (*a)++)
411 if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
416 /* find last contiguous used cluster */
417 for (*b = *a; *b < end; (*b)++)
418 if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
427 int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
431 if (*a == 0 && *b == 0)
432 ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
438 if (find_used_clusters(ef, &ca, &cb) != 0)
440 if (*a != 0 || *b != 0)
442 *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);