blob: d10a5dfe7e288be574abfe63133c6573b1ea32df [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 cluster.c (03.09.09)
3 exFAT file system implementation library.
4
bigbiff bigbiffca829c42013-01-28 08:14:25 -05005 Copyright (C) 2010-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05006
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.
11
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.
16
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/>.
19*/
20
21#include "exfat.h"
22#include <errno.h>
23#include <string.h>
24
25/*
26 * Sector to absolute offset.
27 */
28static off64_t s2o(const struct exfat* ef, off64_t sector)
29{
30 return sector << ef->sb->sector_bits;
31}
32
33/*
34 * Cluster to sector.
35 */
36static off64_t c2s(const struct exfat* ef, cluster_t cluster)
37{
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 ((off64_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
42}
43
44/*
45 * Cluster to absolute offset.
46 */
47off64_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
48{
49 return s2o(ef, c2s(ef, cluster));
50}
51
52/*
53 * Sector to cluster.
54 */
55static cluster_t s2c(const struct exfat* ef, off64_t sector)
56{
57 return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
58 ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
59}
60
61/*
62 * Size in bytes to size in clusters (rounded upwards).
63 */
64static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
65{
66 uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
67 return (bytes + cluster_size - 1) / cluster_size;
68}
69
70cluster_t exfat_next_cluster(const struct exfat* ef,
71 const struct exfat_node* node, cluster_t cluster)
72{
73 le32_t next;
74 off64_t fat_offset;
75
76 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
77 exfat_bug("bad cluster 0x%x", cluster);
78
79 if (IS_CONTIGUOUS(*node))
80 return cluster + 1;
81 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
82 + cluster * sizeof(cluster_t);
83 exfat_pread(ef->dev, &next, sizeof(next), fat_offset);
84 return le32_to_cpu(next);
85}
86
87cluster_t exfat_advance_cluster(const struct exfat* ef,
88 struct exfat_node* node, uint32_t count)
89{
90 uint32_t i;
91
92 if (node->fptr_index > count)
93 {
94 node->fptr_index = 0;
95 node->fptr_cluster = node->start_cluster;
96 }
97
98 for (i = node->fptr_index; i < count; i++)
99 {
100 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
101 if (CLUSTER_INVALID(node->fptr_cluster))
102 break; /* the caller should handle this and print appropriate
103 error message */
104 }
105 node->fptr_index = count;
106 return node->fptr_cluster;
107}
108
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500109static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500110{
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500111 const size_t start_index = start / 8;
112 const size_t end_index = DIV_ROUND_UP(end, 8);
113 size_t i;
114 size_t c;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500115
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500116 for (i = start_index; i < end_index; i++)
117 {
118 if (bitmap[i] == 0xff)
119 continue;
120 for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
121 if (BMAP_GET(bitmap, c) == 0)
122 {
123 BMAP_SET(bitmap, c);
124 return c + EXFAT_FIRST_DATA_CLUSTER;
125 }
126 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500127 return EXFAT_CLUSTER_END;
128}
129
130void exfat_flush_cmap(struct exfat* ef)
131{
132 exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
133 exfat_c2o(ef, ef->cmap.start_cluster));
134 ef->cmap.dirty = false;
135}
136
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500137static void set_next_cluster(const struct exfat* ef, bool contiguous,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500138 cluster_t current, cluster_t next)
139{
140 off64_t fat_offset;
141 le32_t next_le32;
142
143 if (contiguous)
144 return;
145 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
146 + current * sizeof(cluster_t);
147 next_le32 = cpu_to_le32(next);
148 exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset);
149}
150
151static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
152{
153 cluster_t cluster;
154
155 hint -= EXFAT_FIRST_DATA_CLUSTER;
156 if (hint >= ef->cmap.chunk_size)
157 hint = 0;
158
159 cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
160 if (cluster == EXFAT_CLUSTER_END)
161 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
162 if (cluster == EXFAT_CLUSTER_END)
163 {
164 exfat_error("no free space left");
165 return EXFAT_CLUSTER_END;
166 }
167
168 ef->cmap.dirty = true;
169 return cluster;
170}
171
172static void free_cluster(struct exfat* ef, cluster_t cluster)
173{
174 if (CLUSTER_INVALID(cluster))
175 exfat_bug("freeing invalid cluster 0x%x", cluster);
176 if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
177 exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
178 ef->cmap.size);
179
180 BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
181 ef->cmap.dirty = true;
182}
183
184static void make_noncontiguous(const struct exfat* ef, cluster_t first,
185 cluster_t last)
186{
187 cluster_t c;
188
189 for (c = first; c < last; c++)
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500190 set_next_cluster(ef, false, c, c + 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500191}
192
193static int shrink_file(struct exfat* ef, struct exfat_node* node,
194 uint32_t current, uint32_t difference);
195
196static int grow_file(struct exfat* ef, struct exfat_node* node,
197 uint32_t current, uint32_t difference)
198{
199 cluster_t previous;
200 cluster_t next;
201 uint32_t allocated = 0;
202
203 if (difference == 0)
204 exfat_bug("zero clusters count passed");
205
206 if (node->start_cluster != EXFAT_CLUSTER_FREE)
207 {
208 /* get the last cluster of the file */
209 previous = exfat_advance_cluster(ef, node, current - 1);
210 if (CLUSTER_INVALID(previous))
211 {
212 exfat_error("invalid cluster 0x%x while growing", previous);
213 return -EIO;
214 }
215 }
216 else
217 {
218 if (node->fptr_index != 0)
219 exfat_bug("non-zero pointer index (%u)", node->fptr_index);
220 /* file does not have clusters (i.e. is empty), allocate
221 the first one for it */
222 previous = allocate_cluster(ef, 0);
223 if (CLUSTER_INVALID(previous))
224 return -ENOSPC;
225 node->fptr_cluster = node->start_cluster = previous;
226 allocated = 1;
227 /* file consists of only one cluster, so it's contiguous */
228 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
229 }
230
231 while (allocated < difference)
232 {
233 next = allocate_cluster(ef, previous + 1);
234 if (CLUSTER_INVALID(next))
235 {
236 if (allocated != 0)
237 shrink_file(ef, node, current + allocated, allocated);
238 return -ENOSPC;
239 }
240 if (next != previous - 1 && IS_CONTIGUOUS(*node))
241 {
242 /* it's a pity, but we are not able to keep the file contiguous
243 anymore */
244 make_noncontiguous(ef, node->start_cluster, previous);
245 node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
246 node->flags |= EXFAT_ATTRIB_DIRTY;
247 }
248 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
249 previous = next;
250 allocated++;
251 }
252
253 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
254 return 0;
255}
256
257static int shrink_file(struct exfat* ef, struct exfat_node* node,
258 uint32_t current, uint32_t difference)
259{
260 cluster_t previous;
261 cluster_t next;
262
263 if (difference == 0)
264 exfat_bug("zero difference passed");
265 if (node->start_cluster == EXFAT_CLUSTER_FREE)
266 exfat_bug("unable to shrink empty file (%u clusters)", current);
267 if (current < difference)
268 exfat_bug("file underflow (%u < %u)", current, difference);
269
270 /* crop the file */
271 if (current > difference)
272 {
273 cluster_t last = exfat_advance_cluster(ef, node,
274 current - difference - 1);
275 if (CLUSTER_INVALID(last))
276 {
277 exfat_error("invalid cluster 0x%x while shrinking", last);
278 return -EIO;
279 }
280 previous = exfat_next_cluster(ef, node, last);
281 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
282 }
283 else
284 {
285 previous = node->start_cluster;
286 node->start_cluster = EXFAT_CLUSTER_FREE;
287 }
288 node->fptr_index = 0;
289 node->fptr_cluster = node->start_cluster;
290
291 /* free remaining clusters */
292 while (difference--)
293 {
294 if (CLUSTER_INVALID(previous))
295 {
296 exfat_error("invalid cluster 0x%x while freeing after shrink",
297 previous);
298 return -EIO;
299 }
300 next = exfat_next_cluster(ef, node, previous);
301 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
302 EXFAT_CLUSTER_FREE);
303 free_cluster(ef, previous);
304 previous = next;
305 }
306 return 0;
307}
308
309static void erase_raw(struct exfat* ef, size_t size, off64_t offset)
310{
311 exfat_pwrite(ef->dev, ef->zero_cluster, size, offset);
312}
313
314static int erase_range(struct exfat* ef, struct exfat_node* node,
315 uint64_t begin, uint64_t end)
316{
317 uint64_t cluster_boundary;
318 cluster_t cluster;
319
320 if (begin >= end)
321 return 0;
322
323 cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
324 cluster = exfat_advance_cluster(ef, node,
325 begin / CLUSTER_SIZE(*ef->sb));
326 if (CLUSTER_INVALID(cluster))
327 {
328 exfat_error("invalid cluster 0x%x while erasing", cluster);
329 return -EIO;
330 }
331 /* erase from the beginning to the closest cluster boundary */
332 erase_raw(ef, MIN(cluster_boundary, end) - begin,
333 exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb));
334 /* erase whole clusters */
335 while (cluster_boundary < end)
336 {
337 cluster = exfat_next_cluster(ef, node, cluster);
338 /* the cluster cannot be invalid because we have just allocated it */
339 if (CLUSTER_INVALID(cluster))
340 exfat_bug("invalid cluster 0x%x after allocation", cluster);
341 erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
342 cluster_boundary += CLUSTER_SIZE(*ef->sb);
343 }
344 return 0;
345}
346
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500347int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
348 bool erase)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500349{
350 uint32_t c1 = bytes2clusters(ef, node->size);
351 uint32_t c2 = bytes2clusters(ef, size);
352 int rc = 0;
353
354 if (node->references == 0 && node->parent)
355 exfat_bug("no references, node changes can be lost");
356
357 if (node->size == size)
358 return 0;
359
360 if (c1 < c2)
361 rc = grow_file(ef, node, c1, c2 - c1);
362 else if (c1 > c2)
363 rc = shrink_file(ef, node, c1, c1 - c2);
364
365 if (rc != 0)
366 return rc;
367
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500368 if (erase)
369 {
370 rc = erase_range(ef, node, node->size, size);
371 if (rc != 0)
372 return rc;
373 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500374
375 exfat_update_mtime(node);
376 node->size = size;
377 node->flags |= EXFAT_ATTRIB_DIRTY;
378 return 0;
379}
380
381uint32_t exfat_count_free_clusters(const struct exfat* ef)
382{
383 uint32_t free_clusters = 0;
384 uint32_t i;
385
386 for (i = 0; i < ef->cmap.size; i++)
387 if (BMAP_GET(ef->cmap.chunk, i) == 0)
388 free_clusters++;
389 return free_clusters;
390}
391
392static int find_used_clusters(const struct exfat* ef,
393 cluster_t* a, cluster_t* b)
394{
395 const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
396
397 /* find first used cluster */
398 for (*a = *b + 1; *a < end; (*a)++)
399 if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
400 break;
401 if (*a >= end)
402 return 1;
403
404 /* find last contiguous used cluster */
405 for (*b = *a; *b < end; (*b)++)
406 if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
407 {
408 (*b)--;
409 break;
410 }
411
412 return 0;
413}
414
415int exfat_find_used_sectors(const struct exfat* ef, off64_t* a, off64_t* b)
416{
417 cluster_t ca, cb;
418
419 if (*a == 0 && *b == 0)
420 ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
421 else
422 {
423 ca = s2c(ef, *a);
424 cb = s2c(ef, *b);
425 }
426 if (find_used_clusters(ef, &ca, &cb) != 0)
427 return 1;
428 if (*a != 0 || *b != 0)
429 *a = c2s(ef, ca);
430 *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);
431 return 0;
432}