OSDN Git Service

Clean up mtime updating.
[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                 struct exfat_node* node, uint32_t count)
73 {
74         uint32_t i;
75
76         if (node->fptr_index > count)
77         {
78                 node->fptr_index = 0;
79                 node->fptr_cluster = node->start_cluster;
80         }
81
82         for (i = node->fptr_index; i < count; i++)
83         {
84                 node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
85                 if (CLUSTER_INVALID(node->fptr_cluster))
86                         break;
87         }
88         node->fptr_index = count;
89         return node->fptr_cluster;
90 }
91
92 static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
93                 cluster_t end)
94 {
95         const cluster_t mid_start = (start + 7) / 8 * 8;
96         const cluster_t mid_end = end / 8 * 8;
97         cluster_t c;
98         cluster_t byte;
99
100         for (c = start; c < mid_start; c++)
101                 if (BMAP_GET(bitmap, c) == 0)
102                 {
103                         BMAP_SET(bitmap, c);
104                         return c + EXFAT_FIRST_DATA_CLUSTER;
105                 }
106
107         for (byte = mid_start / 8; byte < mid_end / 8; byte++)
108                 if (bitmap[byte] != 0xff)
109                 {
110                         cluster_t bit;
111
112                         for (bit = 0; bit < 8; bit++)
113                                 if (!(bitmap[byte] & (1u << bit)))
114                                 {
115                                         bitmap[byte] |= (1u << bit);
116                                         return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
117                                 }
118                 }
119
120         for (c = mid_end; c < end; c++)
121                 if (BMAP_GET(bitmap, c) == 0)
122                 {
123                         BMAP_SET(bitmap, c);
124                         return c + EXFAT_FIRST_DATA_CLUSTER;
125                 }
126
127         return EXFAT_CLUSTER_END;
128 }
129
130 void exfat_flush_cmap(struct exfat* ef)
131 {
132         exfat_write_raw(ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
133                         exfat_c2o(ef, ef->cmap.start_cluster), ef->fd);
134         ef->cmap.dirty = 0;
135 }
136
137 static void set_next_cluster(const struct exfat* ef, int contiguous,
138                 cluster_t current, cluster_t next)
139 {
140         off_t fat_offset;
141
142         if (contiguous)
143                 return;
144         fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
145                 + current * sizeof(cluster_t);
146         exfat_write_raw(&next, sizeof(next), fat_offset, ef->fd);
147 }
148
149 static void erase_cluster(struct exfat* ef, cluster_t cluster)
150 {
151         const int block_size = BLOCK_SIZE(*ef->sb);
152         const int blocks_in_cluster = CLUSTER_SIZE(*ef->sb) / block_size;
153         int i;
154
155         for (i = 0; i < blocks_in_cluster; i++)
156                 exfat_write_raw(ef->zero_block, block_size,
157                                 exfat_c2o(ef, cluster) + i * block_size, ef->fd);
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         erase_cluster(ef, cluster);
178         ef->cmap.dirty = 1;
179         /* FIXME update percentage of used space */
180         return cluster;
181 }
182
183 static void free_cluster(struct exfat* ef, cluster_t cluster)
184 {
185         if (CLUSTER_INVALID(cluster))
186                 exfat_bug("attempting to free invalid cluster");
187         if (cluster < EXFAT_FIRST_DATA_CLUSTER ||
188                 cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
189                 exfat_bug("bad cluster 0x%x (0x%x)", cluster, ef->cmap.size);
190
191         BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
192         ef->cmap.dirty = 1;
193         /* FIXME update percentage of used space */
194 }
195
196 static void make_noncontiguous(const struct exfat* ef, cluster_t first,
197                 cluster_t last)
198 {
199         cluster_t c;
200
201         for (c = first; c < last; c++)
202                 set_next_cluster(ef, 0, c, c + 1);
203 }
204
205 static int grow_file(struct exfat* ef, struct exfat_node* node,
206                 uint32_t difference)
207 {
208         cluster_t previous;
209         cluster_t next;
210
211         if (difference == 0)
212                 exfat_bug("zero clusters count passed");
213
214         if (node->start_cluster != EXFAT_CLUSTER_FREE)
215         {
216                 /* get the last cluster of the file */
217                 previous = exfat_advance_cluster(ef, node,
218                                 bytes2clusters(ef, node->size) - 1);
219                 if (CLUSTER_INVALID(previous))
220                 {
221                         exfat_error("invalid cluster in file");
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                 difference--;
236                 /* file consists of only one cluster, so it's contiguous */
237                 node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
238         }
239
240         while (difference--)
241         {
242                 next = allocate_cluster(ef, previous + 1);
243                 if (CLUSTER_INVALID(next))
244                         return -ENOSPC;
245                 if (next != previous - 1 && IS_CONTIGUOUS(*node))
246                 {
247                         /* it's a pity, but we are not able to keep the file contiguous
248                            anymore */
249                         make_noncontiguous(ef, node->start_cluster, previous);
250                         node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
251                 }
252                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
253                 previous = next;
254         }
255
256         set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
257         return 0;
258 }
259
260 static int shrink_file(struct exfat* ef, struct exfat_node* node,
261                 uint32_t difference)
262 {
263         uint32_t current = bytes2clusters(ef, node->size);
264         cluster_t previous;
265         cluster_t next;
266
267         if (difference == 0)
268                 exfat_bug("zero difference passed");
269         if (node->start_cluster == EXFAT_CLUSTER_FREE)
270                 exfat_bug("unable to shrink empty file (%u clusters)", current);
271         if (current < difference)
272                 exfat_bug("file underflow (%u < %u)", current, difference);
273
274         /* crop the file */
275         if (current > difference)
276         {
277                 cluster_t last = exfat_advance_cluster(ef, node,
278                                 current - difference - 1);
279                 if (CLUSTER_INVALID(last))
280                 {
281                         exfat_error("invalid cluster in file");
282                         return -EIO;
283                 }
284                 previous = exfat_next_cluster(ef, node, last);
285                 set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
286         }
287         else
288         {
289                 previous = node->start_cluster;
290                 node->start_cluster = EXFAT_CLUSTER_FREE;
291         }
292         node->fptr_index = 0;
293         node->fptr_cluster = node->start_cluster;
294
295         /* free remaining clusters */
296         while (difference--)
297         {
298                 if (CLUSTER_INVALID(previous))
299                 {
300                         exfat_error("invalid cluster in file");
301                         return -EIO;
302                 }
303                 next = exfat_next_cluster(ef, node, previous);
304                 set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
305                                 EXFAT_CLUSTER_FREE);
306                 free_cluster(ef, previous);
307                 previous = next;
308         }
309         return 0;
310 }
311
312 int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
313 {
314         uint32_t c1 = bytes2clusters(ef, node->size);
315         uint32_t c2 = bytes2clusters(ef, size);
316         int rc = 0;
317
318         if (c1 < c2)
319                 rc = grow_file(ef, node, c2 - c1);
320         else if (c1 > c2)
321                 rc = shrink_file(ef, node, c1 - c2);
322
323         if (rc != 0)
324                 return rc;
325
326         if (node->size != size)
327         {
328                 exfat_update_mtime(node);
329                 node->size = size;
330                 node->flags |= EXFAT_ATTRIB_DIRTY;
331         }
332         return 0;
333 }