]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/cluster.c
0418932ec802f66590fcf29139a2d075aa64e335
[sven/exfat-utils.git] / libexfat / cluster.c
1 /*
2         cluster.c (03.09.09)
3         exFAT file system implementation library.
4
5         Copyright (C) 2010-2012  Andrew Nayenko
6
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  */
28 static off_t s2o(const struct exfat* ef, off_t sector)
29 {
30         return sector << ef->sb->sector_bits;
31 }
32
33 /*
34  * Cluster to sector.
35  */
36 static off_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                 ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
42 }
43
44 /*
45  * Cluster to absolute offset.
46  */
47 off_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  */
55 static cluster_t s2c(const struct exfat* ef, off_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  */
64 static 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
70 cluster_t exfat_next_cluster(const struct exfat* ef,
71                 const struct exfat_node* node, cluster_t cluster)
72 {
73         le32_t next;
74         off_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
87 cluster_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;
103         }
104         node->fptr_index = count;
105         return node->fptr_cluster;
106 }
107
108 static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
109                 cluster_t end)
110 {
111         const cluster_t mid_start = (start + 7) / 8 * 8;
112         const cluster_t mid_end = end / 8 * 8;
113         cluster_t c;
114         cluster_t byte;
115
116         for (c = start; c < mid_start; c++)
117                 if (BMAP_GET(bitmap, c) == 0)
118                 {
119                         BMAP_SET(bitmap, c);
120                         return c + EXFAT_FIRST_DATA_CLUSTER;
121                 }
122
123         for (byte = mid_start / 8; byte < mid_end / 8; byte++)
124                 if (bitmap[byte] != 0xff)
125                 {
126                         cluster_t bit;
127
128                         for (bit = 0; bit < 8; bit++)
129                                 if (!(bitmap[byte] & (1u << bit)))
130                                 {
131                                         bitmap[byte] |= (1u << bit);
132                                         return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
133                                 }
134                 }
135
136         for (c = mid_end; c < end; c++)
137                 if (BMAP_GET(bitmap, c) == 0)
138                 {
139                         BMAP_SET(bitmap, c);
140                         return c + EXFAT_FIRST_DATA_CLUSTER;
141                 }
142
143         return EXFAT_CLUSTER_END;
144 }
145
146 void exfat_flush_cmap(struct exfat* ef)
147 {
148         exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
149                         exfat_c2o(ef, ef->cmap.start_cluster));
150         ef->cmap.dirty = 0;
151 }
152
153 static void set_next_cluster(const struct exfat* ef, int contiguous,
154                 cluster_t current, cluster_t next)
155 {
156         off_t fat_offset;
157         le32_t next_le32;
158
159         if (contiguous)
160                 return;
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_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset);
165 }
166
167 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
168 {
169         cluster_t cluster;
170
171         hint -= EXFAT_FIRST_DATA_CLUSTER;
172         if (hint >= ef->cmap.chunk_size)
173                 hint = 0;
174
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)
179         {
180                 exfat_error("no free space left");
181                 return EXFAT_CLUSTER_END;
182         }
183
184         ef->cmap.dirty = 1;
185         return cluster;
186 }
187
188 static void free_cluster(struct exfat* ef, cluster_t cluster)
189 {
190         if (CLUSTER_INVALID(cluster))
191                 exfat_bug("attempting to free invalid cluster");
192         if (cluster < EXFAT_FIRST_DATA_CLUSTER ||
193                 cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
194                 exfat_bug("bad cluster 0x%x (0x%x)", cluster, ef->cmap.size);
195
196         BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
197         ef->cmap.dirty = 1;
198 }
199
200 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
201                 cluster_t last)
202 {
203         cluster_t c;
204
205         for (c = first; c < last; c++)
206                 set_next_cluster(ef, 0, c, c + 1);
207 }
208
209 static int shrink_file(struct exfat* ef, struct exfat_node* node,
210                 uint32_t current, uint32_t difference);
211
212 static int grow_file(struct exfat* ef, struct exfat_node* node,
213                 uint32_t current, uint32_t difference)
214 {
215         cluster_t previous;
216         cluster_t next;
217         uint32_t allocated = 0;
218
219         if (difference == 0)
220                 exfat_bug("zero clusters count passed");
221
222         if (node->start_cluster != EXFAT_CLUSTER_FREE)
223         {
224                 /* get the last cluster of the file */
225                 previous = exfat_advance_cluster(ef, node, current - 1);
226                 if (CLUSTER_INVALID(previous))
227                 {
228                         exfat_error("invalid cluster in file");
229                         return -EIO;
230                 }
231         }
232         else
233         {
234                 if (node->fptr_index != 0)
235                         exfat_bug("non-zero pointer index (%u)", node->fptr_index);
236                 /* file does not have clusters (i.e. is empty), allocate
237                    the first one for it */
238                 previous = allocate_cluster(ef, 0);
239                 if (CLUSTER_INVALID(previous))
240                         return -ENOSPC;
241                 node->fptr_cluster = node->start_cluster = previous;
242                 allocated = 1;
243                 /* file consists of only one cluster, so it's contiguous */
244                 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
245         }
246
247         while (allocated < difference)
248         {
249                 next = allocate_cluster(ef, previous + 1);
250                 if (CLUSTER_INVALID(next))
251                 {
252                         if (allocated != 0)
253                                 shrink_file(ef, node, current + allocated, allocated);
254                         return -ENOSPC;
255                 }
256                 if (next != previous - 1 && IS_CONTIGUOUS(*node))
257                 {
258                         /* it's a pity, but we are not able to keep the file contiguous
259                            anymore */
260                         make_noncontiguous(ef, node->start_cluster, previous);
261                         node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
262                         node->flags |= EXFAT_ATTRIB_DIRTY;
263                 }
264                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
265                 previous = next;
266                 allocated++;
267         }
268
269         set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
270         return 0;
271 }
272
273 static int shrink_file(struct exfat* ef, struct exfat_node* node,
274                 uint32_t current, uint32_t difference)
275 {
276         cluster_t previous;
277         cluster_t next;
278
279         if (difference == 0)
280                 exfat_bug("zero difference passed");
281         if (node->start_cluster == EXFAT_CLUSTER_FREE)
282                 exfat_bug("unable to shrink empty file (%u clusters)", current);
283         if (current < difference)
284                 exfat_bug("file underflow (%u < %u)", current, difference);
285
286         /* crop the file */
287         if (current > difference)
288         {
289                 cluster_t last = exfat_advance_cluster(ef, node,
290                                 current - difference - 1);
291                 if (CLUSTER_INVALID(last))
292                 {
293                         exfat_error("invalid cluster in file");
294                         return -EIO;
295                 }
296                 previous = exfat_next_cluster(ef, node, last);
297                 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
298         }
299         else
300         {
301                 previous = node->start_cluster;
302                 node->start_cluster = EXFAT_CLUSTER_FREE;
303         }
304         node->fptr_index = 0;
305         node->fptr_cluster = node->start_cluster;
306
307         /* free remaining clusters */
308         while (difference--)
309         {
310                 if (CLUSTER_INVALID(previous))
311                 {
312                         exfat_error("invalid cluster in file");
313                         return -EIO;
314                 }
315                 next = exfat_next_cluster(ef, node, previous);
316                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
317                                 EXFAT_CLUSTER_FREE);
318                 free_cluster(ef, previous);
319                 previous = next;
320         }
321         return 0;
322 }
323
324 static void erase_raw(struct exfat* ef, size_t size, off_t offset)
325 {
326         exfat_pwrite(ef->dev, ef->zero_cluster, size, offset);
327 }
328
329 static int erase_range(struct exfat* ef, struct exfat_node* node,
330                 uint64_t begin, uint64_t end)
331 {
332         uint64_t cluster_boundary;
333         cluster_t cluster;
334
335         if (begin >= end)
336                 return 0;
337
338         cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
339         cluster = exfat_advance_cluster(ef, node,
340                         begin / CLUSTER_SIZE(*ef->sb));
341         if (CLUSTER_INVALID(cluster))
342         {
343                 exfat_error("invalid cluster in file");
344                 return -EIO;
345         }
346         /* erase from the beginning to the closest cluster boundary */
347         erase_raw(ef, MIN(cluster_boundary, end) - begin,
348                         exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb));
349         /* erase whole clusters */
350         while (cluster_boundary < end)
351         {
352                 cluster = exfat_next_cluster(ef, node, cluster);
353                 /* the cluster cannot be invalid because we have just allocated it */
354                 if (CLUSTER_INVALID(cluster))
355                         exfat_bug("invalid cluster in file");
356                 erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
357                 cluster_boundary += CLUSTER_SIZE(*ef->sb);
358         }
359         return 0;
360 }
361
362 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
363 {
364         uint32_t c1 = bytes2clusters(ef, node->size);
365         uint32_t c2 = bytes2clusters(ef, size);
366         int rc = 0;
367
368         if (node->references == 0 && node->parent)
369                 exfat_bug("no references, node changes can be lost");
370
371         if (node->size == size)
372                 return 0;
373
374         if (c1 < c2)
375                 rc = grow_file(ef, node, c1, c2 - c1);
376         else if (c1 > c2)
377                 rc = shrink_file(ef, node, c1, c1 - c2);
378
379         if (rc != 0)
380                 return rc;
381
382         rc = erase_range(ef, node, node->size, size);
383         if (rc != 0)
384                 return rc;
385
386         exfat_update_mtime(node);
387         node->size = size;
388         node->flags |= EXFAT_ATTRIB_DIRTY;
389         return 0;
390 }
391
392 uint32_t exfat_count_free_clusters(const struct exfat* ef)
393 {
394         uint32_t free_clusters = 0;
395         uint32_t i;
396
397         for (i = 0; i < ef->cmap.size; i++)
398                 if (BMAP_GET(ef->cmap.chunk, i) == 0)
399                         free_clusters++;
400         return free_clusters;
401 }
402
403 static int find_used_clusters(const struct exfat* ef,
404                 cluster_t* a, cluster_t* b)
405 {
406         const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
407
408         /* find first used cluster */
409         for (*a = *b + 1; *a < end; (*a)++)
410                 if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
411                         break;
412         if (*a >= end)
413                 return 1;
414
415         /* find last contiguous used cluster */
416         for (*b = *a; *b < end; (*b)++)
417                 if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
418                 {
419                         (*b)--;
420                         break;
421                 }
422
423         return 0;
424 }
425
426 int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
427 {
428         cluster_t ca, cb;
429
430         if (*a == 0 && *b == 0)
431                 ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
432         else
433         {
434                 ca = s2c(ef, *a);
435                 cb = s2c(ef, *b);
436         }
437         if (find_used_clusters(ef, &ca, &cb) != 0)
438                 return 1;
439         if (*a != 0 || *b != 0)
440                 *a = c2s(ef, ca);
441         *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);
442         return 0;
443 }