OSDN Git Service

Handle I/O error in erase_raw().
[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 #include <inttypes.h>
27
28 /*
29  * Sector to absolute offset.
30  */
31 static off_t s2o(const struct exfat* ef, off_t sector)
32 {
33         return sector << ef->sb->sector_bits;
34 }
35
36 /*
37  * Cluster to sector.
38  */
39 static off_t c2s(const struct exfat* ef, cluster_t cluster)
40 {
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) +
44                 ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
45 }
46
47 /*
48  * Cluster to absolute offset.
49  */
50 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
51 {
52         return s2o(ef, c2s(ef, cluster));
53 }
54
55 /*
56  * Sector to cluster.
57  */
58 static cluster_t s2c(const struct exfat* ef, off_t sector)
59 {
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  */
67 static 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
73 cluster_t exfat_next_cluster(const struct exfat* ef,
74                 const struct exfat_node* node, cluster_t cluster)
75 {
76         le32_t next;
77         off_t fat_offset;
78
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);
86         /* FIXME handle I/O error */
87         if (exfat_pread(ef->dev, &next, sizeof(next), fat_offset) < 0)
88                 exfat_bug("failed to read the next cluster after %#x", cluster);
89         return le32_to_cpu(next);
90 }
91
92 cluster_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
114 static cluster_t find_bit_and_set(bitmap_t* bitmap, size_t start, size_t end)
115 {
116         const size_t start_index = start / sizeof(bitmap_t) / 8;
117         const size_t end_index = DIV_ROUND_UP(end, sizeof(bitmap_t) * 8);
118         size_t i;
119         size_t start_bitindex;
120         size_t end_bitindex;
121         size_t c;
122
123         for (i = start_index; i < end_index; i++)
124         {
125                 if (bitmap[i] == ~((bitmap_t) 0))
126                         continue;
127                 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++)
130                         if (BMAP_GET(bitmap, c) == 0)
131                         {
132                                 BMAP_SET(bitmap, c);
133                                 return c + EXFAT_FIRST_DATA_CLUSTER;
134                         }
135         }
136         return EXFAT_CLUSTER_END;
137 }
138
139 void exfat_flush(struct exfat* ef)
140 {
141         if (ef->cmap.dirty)
142         {
143                 /* FIXME handle I/O error */
144                 if (exfat_pwrite(ef->dev, ef->cmap.chunk,
145                                 BMAP_SIZE(ef->cmap.chunk_size),
146                                 exfat_c2o(ef, ef->cmap.start_cluster)) < 0)
147                         exfat_bug("failed to write clusters bitmap");
148                 ef->cmap.dirty = false;
149         }
150 }
151
152 static void set_next_cluster(const struct exfat* ef, bool contiguous,
153                 cluster_t current, cluster_t next)
154 {
155         off_t fat_offset;
156         le32_t next_le32;
157
158         if (contiguous)
159                 return;
160         fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
161                 + current * sizeof(cluster_t);
162         next_le32 = cpu_to_le32(next);
163         /* FIXME handle I/O error */
164         if (exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset) < 0)
165                 exfat_bug("failed to write the next cluster");
166 }
167
168 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
169 {
170         cluster_t cluster;
171
172         hint -= EXFAT_FIRST_DATA_CLUSTER;
173         if (hint >= ef->cmap.chunk_size)
174                 hint = 0;
175
176         cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
177         if (cluster == EXFAT_CLUSTER_END)
178                 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
179         if (cluster == EXFAT_CLUSTER_END)
180         {
181                 exfat_error("no free space left");
182                 return EXFAT_CLUSTER_END;
183         }
184
185         ef->cmap.dirty = true;
186         return cluster;
187 }
188
189 static void free_cluster(struct exfat* ef, cluster_t cluster)
190 {
191         if (CLUSTER_INVALID(cluster))
192                 exfat_bug("freeing invalid cluster 0x%x", cluster);
193         if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
194                 exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
195                                 ef->cmap.size);
196
197         BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
198         ef->cmap.dirty = true;
199 }
200
201 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
202                 cluster_t last)
203 {
204         cluster_t c;
205
206         for (c = first; c < last; c++)
207                 set_next_cluster(ef, false, c, c + 1);
208 }
209
210 static int shrink_file(struct exfat* ef, struct exfat_node* node,
211                 uint32_t current, uint32_t difference);
212
213 static int grow_file(struct exfat* ef, struct exfat_node* node,
214                 uint32_t current, uint32_t difference)
215 {
216         cluster_t previous;
217         cluster_t next;
218         uint32_t allocated = 0;
219
220         if (difference == 0)
221                 exfat_bug("zero clusters count passed");
222
223         if (node->start_cluster != EXFAT_CLUSTER_FREE)
224         {
225                 /* get the last cluster of the file */
226                 previous = exfat_advance_cluster(ef, node, current - 1);
227                 if (CLUSTER_INVALID(previous))
228                 {
229                         exfat_error("invalid cluster 0x%x while growing", previous);
230                         return -EIO;
231                 }
232         }
233         else
234         {
235                 if (node->fptr_index != 0)
236                         exfat_bug("non-zero pointer index (%u)", node->fptr_index);
237                 /* file does not have clusters (i.e. is empty), allocate
238                    the first one for it */
239                 previous = allocate_cluster(ef, 0);
240                 if (CLUSTER_INVALID(previous))
241                         return -ENOSPC;
242                 node->fptr_cluster = node->start_cluster = previous;
243                 allocated = 1;
244                 /* file consists of only one cluster, so it's contiguous */
245                 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
246         }
247
248         while (allocated < difference)
249         {
250                 next = allocate_cluster(ef, previous + 1);
251                 if (CLUSTER_INVALID(next))
252                 {
253                         if (allocated != 0)
254                                 shrink_file(ef, node, current + allocated, allocated);
255                         return -ENOSPC;
256                 }
257                 if (next != previous - 1 && IS_CONTIGUOUS(*node))
258                 {
259                         /* it's a pity, but we are not able to keep the file contiguous
260                            anymore */
261                         make_noncontiguous(ef, node->start_cluster, previous);
262                         node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
263                         node->flags |= EXFAT_ATTRIB_DIRTY;
264                 }
265                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
266                 previous = next;
267                 allocated++;
268         }
269
270         set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
271         return 0;
272 }
273
274 static int shrink_file(struct exfat* ef, struct exfat_node* node,
275                 uint32_t current, uint32_t difference)
276 {
277         cluster_t previous;
278         cluster_t next;
279
280         if (difference == 0)
281                 exfat_bug("zero difference passed");
282         if (node->start_cluster == EXFAT_CLUSTER_FREE)
283                 exfat_bug("unable to shrink empty file (%u clusters)", current);
284         if (current < difference)
285                 exfat_bug("file underflow (%u < %u)", current, difference);
286
287         /* crop the file */
288         if (current > difference)
289         {
290                 cluster_t last = exfat_advance_cluster(ef, node,
291                                 current - difference - 1);
292                 if (CLUSTER_INVALID(last))
293                 {
294                         exfat_error("invalid cluster 0x%x while shrinking", last);
295                         return -EIO;
296                 }
297                 previous = exfat_next_cluster(ef, node, last);
298                 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
299         }
300         else
301         {
302                 previous = node->start_cluster;
303                 node->start_cluster = EXFAT_CLUSTER_FREE;
304         }
305         node->fptr_index = 0;
306         node->fptr_cluster = node->start_cluster;
307
308         /* free remaining clusters */
309         while (difference--)
310         {
311                 if (CLUSTER_INVALID(previous))
312                 {
313                         exfat_error("invalid cluster 0x%x while freeing after shrink",
314                                         previous);
315                         return -EIO;
316                 }
317                 next = exfat_next_cluster(ef, node, previous);
318                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
319                                 EXFAT_CLUSTER_FREE);
320                 free_cluster(ef, previous);
321                 previous = next;
322         }
323         return 0;
324 }
325
326 static bool erase_raw(struct exfat* ef, size_t size, off_t offset)
327 {
328         if (exfat_pwrite(ef->dev, ef->zero_cluster, size, offset) < 0)
329         {
330                 exfat_error("failed to erase %zu bytes at %"PRId64, size, offset);
331                 return false;
332         }
333         return true;
334 }
335
336 static int erase_range(struct exfat* ef, struct exfat_node* node,
337                 uint64_t begin, uint64_t end)
338 {
339         uint64_t cluster_boundary;
340         cluster_t cluster;
341
342         if (begin >= end)
343                 return 0;
344
345         cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
346         cluster = exfat_advance_cluster(ef, node,
347                         begin / CLUSTER_SIZE(*ef->sb));
348         if (CLUSTER_INVALID(cluster))
349         {
350                 exfat_error("invalid cluster 0x%x while erasing", cluster);
351                 return -EIO;
352         }
353         /* erase from the beginning to the closest cluster boundary */
354         if (!erase_raw(ef, MIN(cluster_boundary, end) - begin,
355                         exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb)))
356                 return -EIO;
357         /* erase whole clusters */
358         while (cluster_boundary < end)
359         {
360                 cluster = exfat_next_cluster(ef, node, cluster);
361                 /* the cluster cannot be invalid because we have just allocated it */
362                 if (CLUSTER_INVALID(cluster))
363                         exfat_bug("invalid cluster 0x%x after allocation", cluster);
364                 if (!erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster)))
365                         return -EIO;
366                 cluster_boundary += CLUSTER_SIZE(*ef->sb);
367         }
368         return 0;
369 }
370
371 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
372                 bool erase)
373 {
374         uint32_t c1 = bytes2clusters(ef, node->size);
375         uint32_t c2 = bytes2clusters(ef, size);
376         int rc = 0;
377
378         if (node->references == 0 && node->parent)
379                 exfat_bug("no references, node changes can be lost");
380
381         if (node->size == size)
382                 return 0;
383
384         if (c1 < c2)
385                 rc = grow_file(ef, node, c1, c2 - c1);
386         else if (c1 > c2)
387                 rc = shrink_file(ef, node, c1, c1 - c2);
388
389         if (rc != 0)
390                 return rc;
391
392         if (erase)
393         {
394                 rc = erase_range(ef, node, node->size, size);
395                 if (rc != 0)
396                         return rc;
397         }
398
399         exfat_update_mtime(node);
400         node->size = size;
401         node->flags |= EXFAT_ATTRIB_DIRTY;
402         return 0;
403 }
404
405 uint32_t exfat_count_free_clusters(const struct exfat* ef)
406 {
407         uint32_t free_clusters = 0;
408         uint32_t i;
409
410         for (i = 0; i < ef->cmap.size; i++)
411                 if (BMAP_GET(ef->cmap.chunk, i) == 0)
412                         free_clusters++;
413         return free_clusters;
414 }
415
416 static int find_used_clusters(const struct exfat* ef,
417                 cluster_t* a, cluster_t* b)
418 {
419         const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
420
421         /* find first used cluster */
422         for (*a = *b + 1; *a < end; (*a)++)
423                 if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
424                         break;
425         if (*a >= end)
426                 return 1;
427
428         /* find last contiguous used cluster */
429         for (*b = *a; *b < end; (*b)++)
430                 if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
431                 {
432                         (*b)--;
433                         break;
434                 }
435
436         return 0;
437 }
438
439 int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
440 {
441         cluster_t ca, cb;
442
443         if (*a == 0 && *b == 0)
444                 ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
445         else
446         {
447                 ca = s2c(ef, *a);
448                 cb = s2c(ef, *b);
449         }
450         if (find_used_clusters(ef, &ca, &cb) != 0)
451                 return 1;
452         if (*a != 0 || *b != 0)
453                 *a = c2s(ef, ca);
454         *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);
455         return 0;
456 }