OSDN Git Service

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