blob: 394ca4a22c9a5d89c7b49213b1eedee174d9f769 [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 bigbiff61cdc022013-08-08 08:35:06 -04005 Free exFAT implementation.
Matt Mower09ef1e42015-12-13 11:29:45 -06006 Copyright (C) 2010-2015 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05007
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04008 This program is free software; you can redistribute it and/or modify
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009 it under the terms of the GNU General Public License as published by
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040010 the Free Software Foundation, either version 2 of the License, or
bigbiff bigbiff9c754052013-01-09 09:09:08 -050011 (at your option) any later version.
12
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.
17
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040018 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.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021*/
22
23#include "exfat.h"
24#include <errno.h>
25#include <string.h>
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040026#include <inttypes.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027
28/*
29 * Sector to absolute offset.
30 */
Spegeliusd69ac2b2014-11-23 15:15:06 +020031static loff_t s2o(const struct exfat* ef, loff_t sector)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050032{
33 return sector << ef->sb->sector_bits;
34}
35
36/*
37 * Cluster to sector.
38 */
Spegeliusd69ac2b2014-11-23 15:15:06 +020039static loff_t c2s(const struct exfat* ef, cluster_t cluster)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050040{
41 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
42 exfat_bug("invalid cluster number %u", cluster);
43 return le32_to_cpu(ef->sb->cluster_sector_start) +
Spegeliusd69ac2b2014-11-23 15:15:06 +020044 ((loff_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045}
46
47/*
48 * Cluster to absolute offset.
49 */
Spegeliusd69ac2b2014-11-23 15:15:06 +020050loff_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050051{
52 return s2o(ef, c2s(ef, cluster));
53}
54
55/*
56 * Sector to cluster.
57 */
Spegeliusd69ac2b2014-11-23 15:15:06 +020058static cluster_t s2c(const struct exfat* ef, loff_t sector)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050059{
60 return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
61 ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
62}
63
64/*
65 * Size in bytes to size in clusters (rounded upwards).
66 */
67static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
68{
69 uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
70 return (bytes + cluster_size - 1) / cluster_size;
71}
72
73cluster_t exfat_next_cluster(const struct exfat* ef,
74 const struct exfat_node* node, cluster_t cluster)
75{
76 le32_t next;
Spegeliusd69ac2b2014-11-23 15:15:06 +020077 loff_t fat_offset;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050078
79 if (cluster < EXFAT_FIRST_DATA_CLUSTER)
80 exfat_bug("bad cluster 0x%x", cluster);
81
82 if (IS_CONTIGUOUS(*node))
83 return cluster + 1;
84 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
85 + cluster * sizeof(cluster_t);
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040086 if (exfat_pread(ef->dev, &next, sizeof(next), fat_offset) < 0)
Matt Mower09ef1e42015-12-13 11:29:45 -060087 return EXFAT_CLUSTER_BAD; /* the caller should handle this and print
88 appropriate error message */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050089 return le32_to_cpu(next);
90}
91
92cluster_t exfat_advance_cluster(const struct exfat* ef,
93 struct exfat_node* node, uint32_t count)
94{
95 uint32_t i;
96
97 if (node->fptr_index > count)
98 {
99 node->fptr_index = 0;
100 node->fptr_cluster = node->start_cluster;
101 }
102
103 for (i = node->fptr_index; i < count; i++)
104 {
105 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
106 if (CLUSTER_INVALID(node->fptr_cluster))
107 break; /* the caller should handle this and print appropriate
108 error message */
109 }
110 node->fptr_index = count;
111 return node->fptr_cluster;
112}
113
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400114static cluster_t find_bit_and_set(bitmap_t* bitmap, size_t start, size_t end)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500115{
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400116 const size_t start_index = start / sizeof(bitmap_t) / 8;
117 const size_t end_index = DIV_ROUND_UP(end, sizeof(bitmap_t) * 8);
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500118 size_t i;
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400119 size_t start_bitindex;
120 size_t end_bitindex;
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500121 size_t c;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500122
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500123 for (i = start_index; i < end_index; i++)
124 {
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400125 if (bitmap[i] == ~((bitmap_t) 0))
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500126 continue;
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400127 start_bitindex = MAX(i * sizeof(bitmap_t) * 8, start);
128 end_bitindex = MIN((i + 1) * sizeof(bitmap_t) * 8, end);
129 for (c = start_bitindex; c < end_bitindex; c++)
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500130 if (BMAP_GET(bitmap, c) == 0)
131 {
132 BMAP_SET(bitmap, c);
133 return c + EXFAT_FIRST_DATA_CLUSTER;
134 }
135 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500136 return EXFAT_CLUSTER_END;
137}
138
Matt Mower09ef1e42015-12-13 11:29:45 -0600139static int flush_nodes(struct exfat* ef, struct exfat_node* node)
140{
141 struct exfat_node* p;
142
143 for (p = node->child; p != NULL; p = p->next)
144 {
145 int rc = flush_nodes(ef, p);
146 if (rc != 0)
147 return rc;
148 }
149 return exfat_flush_node(ef, node);
150}
151
152int exfat_flush_nodes(struct exfat* ef)
153{
154 return flush_nodes(ef, ef->root);
155}
156
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400157int exfat_flush(struct exfat* ef)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500158{
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400159 if (ef->cmap.dirty)
160 {
161 if (exfat_pwrite(ef->dev, ef->cmap.chunk,
162 BMAP_SIZE(ef->cmap.chunk_size),
163 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
164 {
165 exfat_error("failed to write clusters bitmap");
166 return -EIO;
167 }
168 ef->cmap.dirty = false;
169 }
Matt Mower09ef1e42015-12-13 11:29:45 -0600170
bigbiffc40c1c52014-11-01 09:34:57 -0400171 return 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500172}
173
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400174static bool set_next_cluster(const struct exfat* ef, bool contiguous,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500175 cluster_t current, cluster_t next)
176{
Spegeliusd69ac2b2014-11-23 15:15:06 +0200177 loff_t fat_offset;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500178 le32_t next_le32;
179
180 if (contiguous)
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400181 return true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500182 fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
183 + current * sizeof(cluster_t);
184 next_le32 = cpu_to_le32(next);
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400185 if (exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset) < 0)
186 {
187 exfat_error("failed to write the next cluster %#x after %#x", next,
188 current);
189 return false;
190 }
191 return true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500192}
193
194static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
195{
196 cluster_t cluster;
197
198 hint -= EXFAT_FIRST_DATA_CLUSTER;
199 if (hint >= ef->cmap.chunk_size)
200 hint = 0;
201
202 cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
203 if (cluster == EXFAT_CLUSTER_END)
204 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
205 if (cluster == EXFAT_CLUSTER_END)
206 {
207 exfat_error("no free space left");
208 return EXFAT_CLUSTER_END;
209 }
210
211 ef->cmap.dirty = true;
212 return cluster;
213}
214
215static void free_cluster(struct exfat* ef, cluster_t cluster)
216{
217 if (CLUSTER_INVALID(cluster))
218 exfat_bug("freeing invalid cluster 0x%x", cluster);
219 if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
220 exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
221 ef->cmap.size);
222
223 BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
224 ef->cmap.dirty = true;
225}
226
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400227static bool make_noncontiguous(const struct exfat* ef, cluster_t first,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500228 cluster_t last)
229{
230 cluster_t c;
231
232 for (c = first; c < last; c++)
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400233 if (!set_next_cluster(ef, false, c, c + 1))
234 return false;
235 return true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500236}
237
238static int shrink_file(struct exfat* ef, struct exfat_node* node,
239 uint32_t current, uint32_t difference);
240
241static int grow_file(struct exfat* ef, struct exfat_node* node,
242 uint32_t current, uint32_t difference)
243{
244 cluster_t previous;
245 cluster_t next;
246 uint32_t allocated = 0;
247
248 if (difference == 0)
249 exfat_bug("zero clusters count passed");
250
251 if (node->start_cluster != EXFAT_CLUSTER_FREE)
252 {
253 /* get the last cluster of the file */
254 previous = exfat_advance_cluster(ef, node, current - 1);
255 if (CLUSTER_INVALID(previous))
256 {
257 exfat_error("invalid cluster 0x%x while growing", previous);
258 return -EIO;
259 }
260 }
261 else
262 {
263 if (node->fptr_index != 0)
264 exfat_bug("non-zero pointer index (%u)", node->fptr_index);
265 /* file does not have clusters (i.e. is empty), allocate
266 the first one for it */
267 previous = allocate_cluster(ef, 0);
268 if (CLUSTER_INVALID(previous))
269 return -ENOSPC;
270 node->fptr_cluster = node->start_cluster = previous;
271 allocated = 1;
272 /* file consists of only one cluster, so it's contiguous */
273 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
274 }
275
276 while (allocated < difference)
277 {
278 next = allocate_cluster(ef, previous + 1);
279 if (CLUSTER_INVALID(next))
280 {
281 if (allocated != 0)
282 shrink_file(ef, node, current + allocated, allocated);
283 return -ENOSPC;
284 }
285 if (next != previous - 1 && IS_CONTIGUOUS(*node))
286 {
287 /* it's a pity, but we are not able to keep the file contiguous
288 anymore */
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400289 if (!make_noncontiguous(ef, node->start_cluster, previous))
290 return -EIO;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500291 node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
292 node->flags |= EXFAT_ATTRIB_DIRTY;
293 }
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400294 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next))
295 return -EIO;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500296 previous = next;
297 allocated++;
298 }
299
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400300 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
301 EXFAT_CLUSTER_END))
302 return -EIO;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500303 return 0;
304}
305
306static int shrink_file(struct exfat* ef, struct exfat_node* node,
307 uint32_t current, uint32_t difference)
308{
309 cluster_t previous;
310 cluster_t next;
311
312 if (difference == 0)
313 exfat_bug("zero difference passed");
314 if (node->start_cluster == EXFAT_CLUSTER_FREE)
315 exfat_bug("unable to shrink empty file (%u clusters)", current);
316 if (current < difference)
317 exfat_bug("file underflow (%u < %u)", current, difference);
318
319 /* crop the file */
320 if (current > difference)
321 {
322 cluster_t last = exfat_advance_cluster(ef, node,
323 current - difference - 1);
324 if (CLUSTER_INVALID(last))
325 {
326 exfat_error("invalid cluster 0x%x while shrinking", last);
327 return -EIO;
328 }
329 previous = exfat_next_cluster(ef, node, last);
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400330 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), last,
331 EXFAT_CLUSTER_END))
332 return -EIO;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500333 }
334 else
335 {
336 previous = node->start_cluster;
337 node->start_cluster = EXFAT_CLUSTER_FREE;
Matt Mower09ef1e42015-12-13 11:29:45 -0600338 node->flags |= EXFAT_ATTRIB_DIRTY;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500339 }
340 node->fptr_index = 0;
341 node->fptr_cluster = node->start_cluster;
342
343 /* free remaining clusters */
344 while (difference--)
345 {
346 if (CLUSTER_INVALID(previous))
347 {
348 exfat_error("invalid cluster 0x%x while freeing after shrink",
349 previous);
350 return -EIO;
351 }
352 next = exfat_next_cluster(ef, node, previous);
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400353 if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
354 EXFAT_CLUSTER_FREE))
355 return -EIO;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500356 free_cluster(ef, previous);
357 previous = next;
358 }
359 return 0;
360}
361
Spegeliusd69ac2b2014-11-23 15:15:06 +0200362static bool erase_raw(struct exfat* ef, size_t size, loff_t offset)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500363{
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400364 if (exfat_pwrite(ef->dev, ef->zero_cluster, size, offset) < 0)
365 {
366 exfat_error("failed to erase %zu bytes at %"PRId64, size, offset);
367 return false;
368 }
369 return true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500370}
371
372static int erase_range(struct exfat* ef, struct exfat_node* node,
373 uint64_t begin, uint64_t end)
374{
375 uint64_t cluster_boundary;
376 cluster_t cluster;
377
378 if (begin >= end)
379 return 0;
380
381 cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
382 cluster = exfat_advance_cluster(ef, node,
383 begin / CLUSTER_SIZE(*ef->sb));
384 if (CLUSTER_INVALID(cluster))
385 {
386 exfat_error("invalid cluster 0x%x while erasing", cluster);
387 return -EIO;
388 }
389 /* erase from the beginning to the closest cluster boundary */
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400390 if (!erase_raw(ef, MIN(cluster_boundary, end) - begin,
391 exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb)))
392 return -EIO;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500393 /* erase whole clusters */
394 while (cluster_boundary < end)
395 {
396 cluster = exfat_next_cluster(ef, node, cluster);
397 /* the cluster cannot be invalid because we have just allocated it */
398 if (CLUSTER_INVALID(cluster))
399 exfat_bug("invalid cluster 0x%x after allocation", cluster);
bigbiff bigbiff61cdc022013-08-08 08:35:06 -0400400 if (!erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster)))
401 return -EIO;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500402 cluster_boundary += CLUSTER_SIZE(*ef->sb);
403 }
404 return 0;
405}
406
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500407int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
408 bool erase)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500409{
410 uint32_t c1 = bytes2clusters(ef, node->size);
411 uint32_t c2 = bytes2clusters(ef, size);
412 int rc = 0;
413
414 if (node->references == 0 && node->parent)
415 exfat_bug("no references, node changes can be lost");
416
417 if (node->size == size)
418 return 0;
419
420 if (c1 < c2)
421 rc = grow_file(ef, node, c1, c2 - c1);
422 else if (c1 > c2)
423 rc = shrink_file(ef, node, c1, c1 - c2);
424
425 if (rc != 0)
426 return rc;
427
bigbiff bigbiff998716f2013-03-07 09:59:37 -0500428 if (erase)
429 {
430 rc = erase_range(ef, node, node->size, size);
431 if (rc != 0)
432 return rc;
433 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500434
435 exfat_update_mtime(node);
436 node->size = size;
437 node->flags |= EXFAT_ATTRIB_DIRTY;
438 return 0;
439}
440
441uint32_t exfat_count_free_clusters(const struct exfat* ef)
442{
443 uint32_t free_clusters = 0;
444 uint32_t i;
445
446 for (i = 0; i < ef->cmap.size; i++)
447 if (BMAP_GET(ef->cmap.chunk, i) == 0)
448 free_clusters++;
449 return free_clusters;
450}
451
452static int find_used_clusters(const struct exfat* ef,
453 cluster_t* a, cluster_t* b)
454{
455 const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
456
457 /* find first used cluster */
458 for (*a = *b + 1; *a < end; (*a)++)
459 if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
460 break;
461 if (*a >= end)
462 return 1;
463
464 /* find last contiguous used cluster */
465 for (*b = *a; *b < end; (*b)++)
466 if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
467 {
468 (*b)--;
469 break;
470 }
471
472 return 0;
473}
474
Spegeliusd69ac2b2014-11-23 15:15:06 +0200475int exfat_find_used_sectors(const struct exfat* ef, loff_t* a, loff_t* b)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500476{
477 cluster_t ca, cb;
478
479 if (*a == 0 && *b == 0)
480 ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
481 else
482 {
483 ca = s2c(ef, *a);
484 cb = s2c(ef, *b);
485 }
486 if (find_used_clusters(ef, &ca, &cb) != 0)
487 return 1;
488 if (*a != 0 || *b != 0)
489 *a = c2s(ef, ca);
490 *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);
491 return 0;
492}