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/>.
25 #define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8)))
26 #define BMAP_SET(bitmap, index) (bitmap)[(index) / 8] |= (1u << ((index) % 8))
27 #define BMAP_CLR(bitmap, index) (bitmap)[(index) / 8] &= ~(1u << ((index) % 8))
32 static uint32_t c2b(const struct exfat* ef, cluster_t cluster)
34 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
35 exfat_bug("invalid cluster number %u", cluster);
36 return le32_to_cpu(ef->sb->cluster_block_start) +
37 ((cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->bpc_bits);
41 * Cluster to absolute offset.
43 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
45 return (off_t) c2b(ef, cluster) << ef->sb->block_bits;
49 * Block to absolute offset.
51 static off_t b2o(const struct exfat* ef, uint32_t block)
53 return (off_t) block << ef->sb->block_bits;
57 * Size in bytes to size in clusters (rounded upwards).
59 static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
61 uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
62 return (bytes + cluster_size - 1) / cluster_size;
65 cluster_t exfat_next_cluster(const struct exfat* ef,
66 const struct exfat_node* node, cluster_t cluster)
71 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
72 exfat_bug("bad cluster 0x%x", cluster);
74 if (IS_CONTIGUOUS(*node))
76 fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
77 + cluster * sizeof(cluster_t);
78 exfat_read_raw(&next, sizeof(next), fat_offset, ef->fd);
82 cluster_t exfat_advance_cluster(const struct exfat* ef,
83 struct exfat_node* node, uint32_t count)
87 if (node->fptr_index > count)
90 node->fptr_cluster = node->start_cluster;
93 for (i = node->fptr_index; i < count; i++)
95 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
96 if (CLUSTER_INVALID(node->fptr_cluster))
99 node->fptr_index = count;
100 return node->fptr_cluster;
103 static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
106 const cluster_t mid_start = (start + 7) / 8 * 8;
107 const cluster_t mid_end = end / 8 * 8;
111 for (c = start; c < mid_start; c++)
112 if (BMAP_GET(bitmap, c) == 0)
115 return c + EXFAT_FIRST_DATA_CLUSTER;
118 for (byte = mid_start / 8; byte < mid_end / 8; byte++)
119 if (bitmap[byte] != 0xff)
123 for (bit = 0; bit < 8; bit++)
124 if (!(bitmap[byte] & (1u << bit)))
126 bitmap[byte] |= (1u << bit);
127 return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
131 for (c = mid_end; c < end; c++)
132 if (BMAP_GET(bitmap, c) == 0)
135 return c + EXFAT_FIRST_DATA_CLUSTER;
138 return EXFAT_CLUSTER_END;
141 void exfat_flush_cmap(struct exfat* ef)
143 exfat_write_raw(ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
144 exfat_c2o(ef, ef->cmap.start_cluster), ef->fd);
148 static void set_next_cluster(const struct exfat* ef, int contiguous,
149 cluster_t current, cluster_t next)
155 fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
156 + current * sizeof(cluster_t);
157 exfat_write_raw(&next, sizeof(next), fat_offset, ef->fd);
160 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
164 hint -= EXFAT_FIRST_DATA_CLUSTER;
165 if (hint >= ef->cmap.chunk_size)
168 cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
169 if (cluster == EXFAT_CLUSTER_END)
170 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
171 if (cluster == EXFAT_CLUSTER_END)
173 exfat_error("no free space left");
174 return EXFAT_CLUSTER_END;
178 /* FIXME update percentage of used space */
182 static void free_cluster(struct exfat* ef, cluster_t cluster)
184 if (CLUSTER_INVALID(cluster))
185 exfat_bug("attempting to free invalid cluster");
186 if (cluster < EXFAT_FIRST_DATA_CLUSTER ||
187 cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
188 exfat_bug("bad cluster 0x%x (0x%x)", cluster, ef->cmap.size);
190 BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
192 /* FIXME update percentage of used space */
195 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
200 for (c = first; c < last; c++)
201 set_next_cluster(ef, 0, c, c + 1);
204 static int shrink_file(struct exfat* ef, struct exfat_node* node,
205 uint32_t current, uint32_t difference);
207 static int grow_file(struct exfat* ef, struct exfat_node* node,
208 uint32_t current, uint32_t difference)
212 uint32_t allocated = 0;
215 exfat_bug("zero clusters count passed");
217 if (node->start_cluster != EXFAT_CLUSTER_FREE)
219 /* get the last cluster of the file */
220 previous = exfat_advance_cluster(ef, node, current - 1);
221 if (CLUSTER_INVALID(previous))
223 exfat_error("invalid cluster in file");
229 if (node->fptr_index != 0)
230 exfat_bug("non-zero pointer index (%u)", node->fptr_index);
231 /* file does not have clusters (i.e. is empty), allocate
232 the first one for it */
233 previous = allocate_cluster(ef, 0);
234 if (CLUSTER_INVALID(previous))
236 node->fptr_cluster = node->start_cluster = previous;
238 /* file consists of only one cluster, so it's contiguous */
239 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
242 while (allocated < difference)
244 next = allocate_cluster(ef, previous + 1);
245 if (CLUSTER_INVALID(next))
248 shrink_file(ef, node, current + allocated, allocated);
251 if (next != previous - 1 && IS_CONTIGUOUS(*node))
253 /* it's a pity, but we are not able to keep the file contiguous
255 make_noncontiguous(ef, node->start_cluster, previous);
256 node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
257 node->flags |= EXFAT_ATTRIB_DIRTY;
259 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
264 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
268 static int shrink_file(struct exfat* ef, struct exfat_node* node,
269 uint32_t current, uint32_t difference)
275 exfat_bug("zero difference passed");
276 if (node->start_cluster == EXFAT_CLUSTER_FREE)
277 exfat_bug("unable to shrink empty file (%u clusters)", current);
278 if (current < difference)
279 exfat_bug("file underflow (%u < %u)", current, difference);
282 if (current > difference)
284 cluster_t last = exfat_advance_cluster(ef, node,
285 current - difference - 1);
286 if (CLUSTER_INVALID(last))
288 exfat_error("invalid cluster in file");
291 previous = exfat_next_cluster(ef, node, last);
292 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
296 previous = node->start_cluster;
297 node->start_cluster = EXFAT_CLUSTER_FREE;
299 node->fptr_index = 0;
300 node->fptr_cluster = node->start_cluster;
302 /* free remaining clusters */
305 if (CLUSTER_INVALID(previous))
307 exfat_error("invalid cluster in file");
310 next = exfat_next_cluster(ef, node, previous);
311 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
313 free_cluster(ef, previous);
319 static void erase_raw(struct exfat* ef, size_t size, off_t offset)
321 exfat_write_raw(ef->zero_block, size, offset, ef->fd);
324 static int erase_range(struct exfat* ef, struct exfat_node* node,
325 uint64_t begin, uint64_t end)
327 uint64_t block_boundary;
333 block_boundary = (node->size | (BLOCK_SIZE(*ef->sb) - 1)) + 1;
334 cluster = exfat_advance_cluster(ef, node,
335 node->size / CLUSTER_SIZE(*ef->sb));
336 if (CLUSTER_INVALID(cluster))
338 exfat_error("invalid cluster in file");
341 /* erase from the beginning to the closest block boundary */
342 erase_raw(ef, MIN(block_boundary, end) - node->size,
343 exfat_c2o(ef, cluster) + node->size % CLUSTER_SIZE(*ef->sb));
344 /* erase whole blocks */
345 while (block_boundary < end)
347 if (block_boundary % CLUSTER_SIZE(*ef->sb) == 0)
348 cluster = exfat_next_cluster(ef, node, cluster);
349 erase_raw(ef, BLOCK_SIZE(*ef->sb),
350 exfat_c2o(ef, cluster) + block_boundary % CLUSTER_SIZE(*ef->sb));
351 block_boundary += BLOCK_SIZE(*ef->sb);
356 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
358 uint32_t c1 = bytes2clusters(ef, node->size);
359 uint32_t c2 = bytes2clusters(ef, size);
362 if (node->size == size)
366 rc = grow_file(ef, node, c1, c2 - c1);
368 rc = shrink_file(ef, node, c1, c1 - c2);
373 rc = erase_range(ef, node, node->size, size);
377 exfat_update_mtime(node);
379 node->flags |= EXFAT_ATTRIB_DIRTY;