OSDN Git Service

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