3 * exFAT file system implementation library.
5 * Created by Andrew Nayenko on 03.09.09.
6 * This software is distributed under the GNU General Public License
7 * version 3 or any later.
14 #define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8)))
15 #define BMAP_SET(bitmap, index) (bitmap)[(index) / 8] |= (1u << ((index) % 8))
16 #define BMAP_CLR(bitmap, index) (bitmap)[(index) / 8] &= ~(1u << ((index) % 8))
21 static uint32_t c2b(const struct exfat* ef, cluster_t cluster)
23 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
24 exfat_bug("invalid cluster number %u", cluster);
25 return le32_to_cpu(ef->sb->cluster_block_start) +
26 ((cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->bpc_bits);
30 * Cluster to absolute offset.
32 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
34 return (off_t) c2b(ef, cluster) << ef->sb->block_bits;
38 * Block to absolute offset.
40 static off_t b2o(const struct exfat* ef, uint32_t block)
42 return (off_t) block << ef->sb->block_bits;
46 * Size in bytes to size in clusters (rounded upwards).
48 static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
50 uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
51 return (bytes + cluster_size - 1) / cluster_size;
54 cluster_t exfat_next_cluster(const struct exfat* ef,
55 const struct exfat_node* node, cluster_t cluster)
60 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
61 exfat_bug("bad cluster 0x%x", cluster);
63 if (IS_CONTIGUOUS(*node))
65 fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
66 + cluster * sizeof(cluster_t);
67 exfat_read_raw(&next, sizeof(next), fat_offset, ef->fd);
71 cluster_t exfat_advance_cluster(const struct exfat* ef,
72 const struct exfat_node* node, cluster_t cluster, uint32_t count)
76 for (i = 0; i < count; i++)
78 cluster = exfat_next_cluster(ef, node, cluster);
79 if (CLUSTER_INVALID(cluster))
85 static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
88 const cluster_t mid_start = (start + 7) / 8 * 8;
89 const cluster_t mid_end = end / 8 * 8;
93 for (c = start; c < mid_start; c++)
94 if (BMAP_GET(bitmap, c) == 0)
97 return c + EXFAT_FIRST_DATA_CLUSTER;
100 for (byte = mid_start / 8; byte < mid_end / 8; byte++)
101 if (bitmap[byte] != 0xff)
105 for (bit = 0; bit < 8; bit++)
106 if (!(bitmap[byte] & (1u << bit)))
108 bitmap[byte] |= (1u << bit);
109 return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
113 for (c = mid_end; c < end; c++)
114 if (BMAP_GET(bitmap, c) == 0)
117 return c + EXFAT_FIRST_DATA_CLUSTER;
120 return EXFAT_CLUSTER_END;
123 static void flush_cmap(struct exfat* ef)
125 exfat_write_raw(ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
126 exfat_c2o(ef, ef->cmap.start_cluster), ef->fd);
129 static void set_next_cluster(const struct exfat* ef, int contiguous,
130 cluster_t current, cluster_t next)
136 fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
137 + current * sizeof(cluster_t);
138 exfat_write_raw(&next, sizeof(next), fat_offset, ef->fd);
141 static void erase_cluster(struct exfat* ef, cluster_t cluster)
143 const int block_size = BLOCK_SIZE(*ef->sb);
144 const int blocks_in_cluster = CLUSTER_SIZE(*ef->sb) / block_size;
147 for (i = 0; i < blocks_in_cluster; i++)
148 exfat_write_raw(ef->zero_block, block_size,
149 exfat_c2o(ef, cluster) + i * block_size, ef->fd);
152 static cluster_t allocate_cluster(struct exfat* ef)
154 cluster_t cluster = find_bit_and_set(ef->cmap.chunk, 0,
155 ef->cmap.chunk_size);
157 if (cluster == EXFAT_CLUSTER_END)
159 exfat_error("no free space left");
160 return EXFAT_CLUSTER_END;
163 erase_cluster(ef, cluster);
164 /* FIXME no need to flush immediately */
166 /* FIXME update percentage of used space */
170 static void free_cluster(struct exfat* ef, cluster_t cluster)
172 if (CLUSTER_INVALID(cluster))
173 exfat_bug("attempting to free invalid cluster");
175 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
176 exfat_bug("bad cluster 0x%x", cluster);
177 BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
178 /* FIXME no need to flush immediately */
182 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
187 for (c = first; c < last; c++)
188 set_next_cluster(ef, 0, c, c + 1);
191 static int grow_file(struct exfat* ef, struct exfat_node* node,
198 exfat_bug("zero clusters count passed");
200 if (node->start_cluster != EXFAT_CLUSTER_FREE)
202 /* get the last cluster of the file */
203 previous = exfat_advance_cluster(ef, node, node->start_cluster,
204 bytes2clusters(ef, node->size) - 1);
205 if (CLUSTER_INVALID(previous))
207 exfat_error("invalid cluster in file");
213 /* file does not have clusters (i.e. is empty), allocate
214 the first one for it */
215 previous = allocate_cluster(ef);
216 if (CLUSTER_INVALID(previous))
218 node->start_cluster = previous;
220 /* file consists of only one cluster, so it's contiguous */
221 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
226 next = allocate_cluster(ef);
227 if (CLUSTER_INVALID(next))
229 if (next != previous - 1 && IS_CONTIGUOUS(*node))
231 /* it's a pity, but we are not able to keep the file contiguous
233 make_noncontiguous(ef, node->start_cluster, previous);
234 node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
236 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
240 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
244 static int shrink_file(struct exfat* ef, struct exfat_node* node,
247 uint32_t current = bytes2clusters(ef, node->size);
252 exfat_bug("zero difference passed");
253 if (node->start_cluster == EXFAT_CLUSTER_FREE)
254 exfat_bug("unable to shrink empty file (%u clusters)", current);
255 if (current < difference)
256 exfat_bug("file underflow (%u < %u)", current, difference);
259 if (current > difference)
261 cluster_t last = exfat_advance_cluster(ef, node, node->start_cluster,
262 current - difference - 1);
263 if (CLUSTER_INVALID(last))
265 exfat_error("invalid cluster in file");
268 previous = exfat_next_cluster(ef, node, last);
269 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
273 previous = node->start_cluster;
274 node->start_cluster = EXFAT_CLUSTER_FREE;
277 /* free remaining clusters */
280 if (CLUSTER_INVALID(previous))
282 exfat_error("invalid cluster in file");
285 next = exfat_next_cluster(ef, node, previous);
286 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
288 free_cluster(ef, previous);
294 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
296 uint32_t c1 = bytes2clusters(ef, node->size);
297 uint32_t c2 = bytes2clusters(ef, size);
301 rc = grow_file(ef, node, c2 - c1);
303 rc = shrink_file(ef, node, c1 - c2);
308 if (node->size != size)
311 /* FIXME no need to flush immediately */
312 exfat_flush_node(ef, node);