OSDN Git Service

Change source files headings to meet FSF recommendations.
[android-x86/external-exfat.git] / libexfat / cluster.c
1 /*
2         cluster.c (03.09.09)
3         exFAT file system implementation library.
4
5         Copyright (C) 2009, 2010  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 #define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8)))
26 #define BMAP_SET(bitmap, index) (bitmap)[(index) / 8] |= (1u << ((index) % 8))
27 #define BMAP_CLR(bitmap, index) (bitmap)[(index) / 8] &= ~(1u << ((index) % 8))
28
29 /*
30  * Cluster to block.
31  */
32 static uint32_t c2b(const struct exfat* ef, cluster_t cluster)
33 {
34         if (cluster < EXFAT_FIRST_DATA_CLUSTER)
35                 exfat_bug("invalid cluster number %u", cluster);
36         return le32_to_cpu(ef->sb->cluster_block_start) +
37                 ((cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->bpc_bits);
38 }
39
40 /*
41  * Cluster to absolute offset.
42  */
43 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
44 {
45         return (off_t) c2b(ef, cluster) << ef->sb->block_bits;
46 }
47
48 /*
49  * Block to absolute offset.
50  */
51 static off_t b2o(const struct exfat* ef, uint32_t block)
52 {
53         return (off_t) block << ef->sb->block_bits;
54 }
55
56 /*
57  * Size in bytes to size in clusters (rounded upwards).
58  */
59 static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
60 {
61         uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
62         return (bytes + cluster_size - 1) / cluster_size;
63 }
64
65 cluster_t exfat_next_cluster(const struct exfat* ef,
66                 const struct exfat_node* node, cluster_t cluster)
67 {
68         cluster_t next;
69         off_t fat_offset;
70
71         if (cluster < EXFAT_FIRST_DATA_CLUSTER)
72                 exfat_bug("bad cluster 0x%x", cluster);
73
74         if (IS_CONTIGUOUS(*node))
75                 return cluster + 1;
76         fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
77                 + cluster * sizeof(cluster_t);
78         exfat_read_raw(&next, sizeof(next), fat_offset, ef->fd);
79         return next;
80 }
81
82 cluster_t exfat_advance_cluster(const struct exfat* ef,
83                 struct exfat_node* node, uint32_t count)
84 {
85         uint32_t i;
86
87         if (node->fptr_index > count)
88         {
89                 node->fptr_index = 0;
90                 node->fptr_cluster = node->start_cluster;
91         }
92
93         for (i = node->fptr_index; i < count; i++)
94         {
95                 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
96                 if (CLUSTER_INVALID(node->fptr_cluster))
97                         break;
98         }
99         node->fptr_index = count;
100         return node->fptr_cluster;
101 }
102
103 static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
104                 cluster_t end)
105 {
106         const cluster_t mid_start = (start + 7) / 8 * 8;
107         const cluster_t mid_end = end / 8 * 8;
108         cluster_t c;
109         cluster_t byte;
110
111         for (c = start; c < mid_start; c++)
112                 if (BMAP_GET(bitmap, c) == 0)
113                 {
114                         BMAP_SET(bitmap, c);
115                         return c + EXFAT_FIRST_DATA_CLUSTER;
116                 }
117
118         for (byte = mid_start / 8; byte < mid_end / 8; byte++)
119                 if (bitmap[byte] != 0xff)
120                 {
121                         cluster_t bit;
122
123                         for (bit = 0; bit < 8; bit++)
124                                 if (!(bitmap[byte] & (1u << bit)))
125                                 {
126                                         bitmap[byte] |= (1u << bit);
127                                         return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
128                                 }
129                 }
130
131         for (c = mid_end; c < end; c++)
132                 if (BMAP_GET(bitmap, c) == 0)
133                 {
134                         BMAP_SET(bitmap, c);
135                         return c + EXFAT_FIRST_DATA_CLUSTER;
136                 }
137
138         return EXFAT_CLUSTER_END;
139 }
140
141 void exfat_flush_cmap(struct exfat* ef)
142 {
143         exfat_write_raw(ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
144                         exfat_c2o(ef, ef->cmap.start_cluster), ef->fd);
145         ef->cmap.dirty = 0;
146 }
147
148 static void set_next_cluster(const struct exfat* ef, int contiguous,
149                 cluster_t current, cluster_t next)
150 {
151         off_t fat_offset;
152
153         if (contiguous)
154                 return;
155         fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
156                 + current * sizeof(cluster_t);
157         exfat_write_raw(&next, sizeof(next), fat_offset, ef->fd);
158 }
159
160 static void erase_cluster(struct exfat* ef, cluster_t cluster)
161 {
162         const int block_size = BLOCK_SIZE(*ef->sb);
163         const int blocks_in_cluster = CLUSTER_SIZE(*ef->sb) / block_size;
164         int i;
165
166         for (i = 0; i < blocks_in_cluster; i++)
167                 exfat_write_raw(ef->zero_block, block_size,
168                                 exfat_c2o(ef, cluster) + i * block_size, ef->fd);
169 }
170
171 static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
172 {
173         cluster_t cluster;
174
175         hint -= EXFAT_FIRST_DATA_CLUSTER;
176         if (hint >= ef->cmap.chunk_size)
177                 hint = 0;
178
179         cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
180         if (cluster == EXFAT_CLUSTER_END)
181                 cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
182         if (cluster == EXFAT_CLUSTER_END)
183         {
184                 exfat_error("no free space left");
185                 return EXFAT_CLUSTER_END;
186         }
187
188         erase_cluster(ef, cluster);
189         ef->cmap.dirty = 1;
190         /* FIXME update percentage of used space */
191         return cluster;
192 }
193
194 static void free_cluster(struct exfat* ef, cluster_t cluster)
195 {
196         if (CLUSTER_INVALID(cluster))
197                 exfat_bug("attempting to free invalid cluster");
198         if (cluster < EXFAT_FIRST_DATA_CLUSTER ||
199                 cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
200                 exfat_bug("bad cluster 0x%x (0x%x)", cluster, ef->cmap.size);
201
202         BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
203         ef->cmap.dirty = 1;
204         /* FIXME update percentage of used space */
205 }
206
207 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
208                 cluster_t last)
209 {
210         cluster_t c;
211
212         for (c = first; c < last; c++)
213                 set_next_cluster(ef, 0, c, c + 1);
214 }
215
216 static int grow_file(struct exfat* ef, struct exfat_node* node,
217                 uint32_t difference)
218 {
219         cluster_t previous;
220         cluster_t next;
221
222         if (difference == 0)
223                 exfat_bug("zero clusters count passed");
224
225         if (node->start_cluster != EXFAT_CLUSTER_FREE)
226         {
227                 /* get the last cluster of the file */
228                 previous = exfat_advance_cluster(ef, node,
229                                 bytes2clusters(ef, node->size) - 1);
230                 if (CLUSTER_INVALID(previous))
231                 {
232                         exfat_error("invalid cluster in file");
233                         return -EIO;
234                 }
235         }
236         else
237         {
238                 if (node->fptr_index != 0)
239                         exfat_bug("non-zero pointer index (%u)", node->fptr_index);
240                 /* file does not have clusters (i.e. is empty), allocate
241                    the first one for it */
242                 previous = allocate_cluster(ef, 0);
243                 if (CLUSTER_INVALID(previous))
244                         return -ENOSPC;
245                 node->fptr_cluster = node->start_cluster = previous;
246                 difference--;
247                 /* file consists of only one cluster, so it's contiguous */
248                 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
249         }
250
251         while (difference--)
252         {
253                 next = allocate_cluster(ef, previous + 1);
254                 if (CLUSTER_INVALID(next))
255                         return -ENOSPC;
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                 }
263                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
264                 previous = next;
265         }
266
267         set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
268         return 0;
269 }
270
271 static int shrink_file(struct exfat* ef, struct exfat_node* node,
272                 uint32_t difference)
273 {
274         uint32_t current = bytes2clusters(ef, node->size);
275         cluster_t previous;
276         cluster_t next;
277
278         if (difference == 0)
279                 exfat_bug("zero difference passed");
280         if (node->start_cluster == EXFAT_CLUSTER_FREE)
281                 exfat_bug("unable to shrink empty file (%u clusters)", current);
282         if (current < difference)
283                 exfat_bug("file underflow (%u < %u)", current, difference);
284
285         /* crop the file */
286         if (current > difference)
287         {
288                 cluster_t last = exfat_advance_cluster(ef, node,
289                                 current - difference - 1);
290                 if (CLUSTER_INVALID(last))
291                 {
292                         exfat_error("invalid cluster in file");
293                         return -EIO;
294                 }
295                 previous = exfat_next_cluster(ef, node, last);
296                 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
297         }
298         else
299         {
300                 previous = node->start_cluster;
301                 node->start_cluster = EXFAT_CLUSTER_FREE;
302         }
303         node->fptr_index = 0;
304         node->fptr_cluster = node->start_cluster;
305
306         /* free remaining clusters */
307         while (difference--)
308         {
309                 if (CLUSTER_INVALID(previous))
310                 {
311                         exfat_error("invalid cluster in file");
312                         return -EIO;
313                 }
314                 next = exfat_next_cluster(ef, node, previous);
315                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
316                                 EXFAT_CLUSTER_FREE);
317                 free_cluster(ef, previous);
318                 previous = next;
319         }
320         return 0;
321 }
322
323 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
324 {
325         uint32_t c1 = bytes2clusters(ef, node->size);
326         uint32_t c2 = bytes2clusters(ef, size);
327         int rc = 0;
328
329         if (c1 < c2)
330                 rc = grow_file(ef, node, c2 - c1);
331         else if (c1 > c2)
332                 rc = shrink_file(ef, node, c1 - c2);
333
334         if (rc != 0)
335                 return rc;
336
337         if (node->size != size)
338         {
339                 exfat_update_mtime(node);
340                 node->size = size;
341                 node->flags |= EXFAT_ATTRIB_DIRTY;
342         }
343         return 0;
344 }