1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6 #include <linux/blkdev.h>
7 #include <linux/slab.h>
8 #include <linux/bitmap.h>
9 #include <linux/buffer_head.h>
10 #include <linux/backing-dev.h>
11
12 #include "exfat_raw.h"
13 #include "exfat_fs.h"
14
15 #if BITS_PER_LONG == 32
16 #define __le_long __le32
17 #define lel_to_cpu(A) le32_to_cpu(A)
18 #define cpu_to_lel(A) cpu_to_le32(A)
19 #elif BITS_PER_LONG == 64
20 #define __le_long __le64
21 #define lel_to_cpu(A) le64_to_cpu(A)
22 #define cpu_to_lel(A) cpu_to_le64(A)
23 #else
24 #error "BITS_PER_LONG not 32 or 64"
25 #endif
26
27 /*
28 * Allocation Bitmap Management Functions
29 */
exfat_test_bitmap_range(struct super_block * sb,unsigned int clu,unsigned int count)30 static bool exfat_test_bitmap_range(struct super_block *sb, unsigned int clu,
31 unsigned int count)
32 {
33 struct exfat_sb_info *sbi = EXFAT_SB(sb);
34 unsigned int start = clu;
35 unsigned int end = clu + count;
36 unsigned int ent_idx, i, b;
37 unsigned int bit_offset, bits_to_check;
38 __le_long *bitmap_le;
39 unsigned long mask, word;
40
41 if (!is_valid_cluster(sbi, start) || !is_valid_cluster(sbi, end - 1))
42 return false;
43
44 while (start < end) {
45 ent_idx = CLUSTER_TO_BITMAP_ENT(start);
46 i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
47 b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
48
49 bitmap_le = (__le_long *)sbi->vol_amap[i]->b_data;
50
51 /* Calculate how many bits we can check in the current word */
52 bit_offset = b % BITS_PER_LONG;
53 bits_to_check = min(end - start,
54 (unsigned int)(BITS_PER_LONG - bit_offset));
55
56 /* Create a bitmask for the range of bits to check */
57 if (bits_to_check >= BITS_PER_LONG)
58 mask = ~0UL;
59 else
60 mask = ((1UL << bits_to_check) - 1) << bit_offset;
61 word = lel_to_cpu(bitmap_le[b / BITS_PER_LONG]);
62
63 /* Check if all bits in the mask are set */
64 if ((word & mask) != mask)
65 return false;
66
67 start += bits_to_check;
68 }
69
70 return true;
71 }
72
exfat_allocate_bitmap(struct super_block * sb,struct exfat_dentry * ep)73 static int exfat_allocate_bitmap(struct super_block *sb,
74 struct exfat_dentry *ep)
75 {
76 struct exfat_sb_info *sbi = EXFAT_SB(sb);
77 struct blk_plug plug;
78 long long map_size;
79 unsigned int i, j, need_map_size;
80 sector_t sector;
81 unsigned int max_ra_count;
82
83 sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
84 map_size = le64_to_cpu(ep->dentry.bitmap.size);
85 need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE)
86 + 1;
87 if (need_map_size != map_size) {
88 exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)",
89 need_map_size, map_size);
90 /*
91 * Only allowed when bogus allocation
92 * bitmap size is large
93 */
94 if (need_map_size > map_size)
95 return -EIO;
96 }
97 sbi->map_sectors = ((need_map_size - 1) >>
98 (sb->s_blocksize_bits)) + 1;
99 sbi->vol_amap = kvmalloc_array(sbi->map_sectors,
100 sizeof(struct buffer_head *), GFP_KERNEL);
101 if (!sbi->vol_amap)
102 return -ENOMEM;
103
104 sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
105 max_ra_count = min(sb->s_bdi->ra_pages, sb->s_bdi->io_pages) <<
106 (PAGE_SHIFT - sb->s_blocksize_bits);
107 for (i = 0; i < sbi->map_sectors; i++) {
108 /* Trigger the next readahead in advance. */
109 if (0 == (i % max_ra_count)) {
110 blk_start_plug(&plug);
111 for (j = i; j < min(max_ra_count, sbi->map_sectors - i) + i; j++)
112 sb_breadahead(sb, sector + j);
113 blk_finish_plug(&plug);
114 }
115
116 sbi->vol_amap[i] = sb_bread(sb, sector + i);
117 if (!sbi->vol_amap[i])
118 goto err_out;
119 }
120
121 if (exfat_test_bitmap_range(sb, sbi->map_clu,
122 EXFAT_B_TO_CLU_ROUND_UP(map_size, sbi)) == false)
123 goto err_out;
124
125 return 0;
126
127 err_out:
128 j = 0;
129 /* release all buffers and free vol_amap */
130 while (j < i)
131 brelse(sbi->vol_amap[j++]);
132
133 kvfree(sbi->vol_amap);
134 sbi->vol_amap = NULL;
135 return -EIO;
136 }
137
exfat_load_bitmap(struct super_block * sb)138 int exfat_load_bitmap(struct super_block *sb)
139 {
140 unsigned int i, type;
141 struct exfat_chain clu;
142 struct exfat_sb_info *sbi = EXFAT_SB(sb);
143
144 exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
145 while (clu.dir != EXFAT_EOF_CLUSTER) {
146 for (i = 0; i < sbi->dentries_per_clu; i++) {
147 struct exfat_dentry *ep;
148 struct buffer_head *bh;
149
150 ep = exfat_get_dentry(sb, &clu, i, &bh);
151 if (!ep)
152 return -EIO;
153
154 type = exfat_get_entry_type(ep);
155 if (type == TYPE_BITMAP &&
156 ep->dentry.bitmap.flags == 0x0) {
157 int err;
158
159 err = exfat_allocate_bitmap(sb, ep);
160 brelse(bh);
161 return err;
162 }
163 brelse(bh);
164
165 if (type == TYPE_UNUSED)
166 return -EINVAL;
167 }
168
169 if (exfat_get_next_cluster(sb, &clu.dir))
170 return -EIO;
171 }
172
173 return -EINVAL;
174 }
175
exfat_free_bitmap(struct exfat_sb_info * sbi)176 void exfat_free_bitmap(struct exfat_sb_info *sbi)
177 {
178 int i;
179
180 for (i = 0; i < sbi->map_sectors; i++)
181 __brelse(sbi->vol_amap[i]);
182
183 kvfree(sbi->vol_amap);
184 }
185
exfat_set_bitmap(struct inode * inode,unsigned int clu,bool sync)186 int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
187 {
188 int i, b;
189 unsigned int ent_idx;
190 struct super_block *sb = inode->i_sb;
191 struct exfat_sb_info *sbi = EXFAT_SB(sb);
192
193 if (!is_valid_cluster(sbi, clu))
194 return -EINVAL;
195
196 ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
197 i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
198 b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
199
200 set_bit_le(b, sbi->vol_amap[i]->b_data);
201 exfat_update_bh(sbi->vol_amap[i], sync);
202 return 0;
203 }
204
exfat_clear_bitmap(struct inode * inode,unsigned int clu,bool sync)205 int exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
206 {
207 int i, b;
208 unsigned int ent_idx;
209 struct super_block *sb = inode->i_sb;
210 struct exfat_sb_info *sbi = EXFAT_SB(sb);
211
212 if (!is_valid_cluster(sbi, clu))
213 return -EIO;
214
215 ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
216 i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
217 b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
218
219 if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
220 return -EIO;
221
222 clear_bit_le(b, sbi->vol_amap[i]->b_data);
223
224 exfat_update_bh(sbi->vol_amap[i], sync);
225
226 return 0;
227 }
228
229 /*
230 * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
231 * the cluster heap.
232 */
exfat_find_free_bitmap(struct super_block * sb,unsigned int clu)233 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
234 {
235 unsigned int i, map_i, map_b, ent_idx;
236 unsigned int clu_base, clu_free;
237 unsigned long clu_bits, clu_mask;
238 struct exfat_sb_info *sbi = EXFAT_SB(sb);
239 __le_long bitval;
240
241 WARN_ON(clu < EXFAT_FIRST_CLUSTER);
242 ent_idx = ALIGN_DOWN(CLUSTER_TO_BITMAP_ENT(clu), BITS_PER_LONG);
243 clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx);
244 clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
245
246 map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
247 map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
248
249 for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
250 i += BITS_PER_LONG) {
251 bitval = *(__le_long *)(sbi->vol_amap[map_i]->b_data + map_b);
252 if (clu_mask > 0) {
253 bitval |= cpu_to_lel(clu_mask);
254 clu_mask = 0;
255 }
256 if (lel_to_cpu(bitval) != ULONG_MAX) {
257 clu_bits = lel_to_cpu(bitval);
258 clu_free = clu_base + ffz(clu_bits);
259 if (clu_free < sbi->num_clusters)
260 return clu_free;
261 }
262 clu_base += BITS_PER_LONG;
263 map_b += sizeof(long);
264
265 if (map_b >= sb->s_blocksize ||
266 clu_base >= sbi->num_clusters) {
267 if (++map_i >= sbi->map_sectors) {
268 clu_base = EXFAT_FIRST_CLUSTER;
269 map_i = 0;
270 }
271 map_b = 0;
272 }
273 }
274
275 return EXFAT_EOF_CLUSTER;
276 }
277
exfat_count_used_clusters(struct super_block * sb,unsigned int * ret_count)278 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
279 {
280 struct exfat_sb_info *sbi = EXFAT_SB(sb);
281 unsigned int count = 0;
282 unsigned int i, map_i = 0, map_b = 0;
283 unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
284 unsigned int last_mask = total_clus & (BITS_PER_LONG - 1);
285 unsigned long *bitmap, clu_bits;
286
287 total_clus &= ~last_mask;
288 for (i = 0; i < total_clus; i += BITS_PER_LONG) {
289 bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
290 count += hweight_long(*bitmap);
291 map_b += sizeof(long);
292 if (map_b >= (unsigned int)sb->s_blocksize) {
293 map_i++;
294 map_b = 0;
295 }
296 }
297
298 if (last_mask) {
299 bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
300 clu_bits = lel_to_cpu(*(__le_long *)bitmap);
301 count += hweight_long(clu_bits & BITMAP_LAST_WORD_MASK(last_mask));
302 }
303
304 *ret_count = count;
305 return 0;
306 }
307
exfat_trim_fs(struct inode * inode,struct fstrim_range * range)308 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
309 {
310 unsigned int trim_begin, trim_end, count, next_free_clu;
311 u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
312 struct super_block *sb = inode->i_sb;
313 struct exfat_sb_info *sbi = EXFAT_SB(sb);
314 int err = 0;
315
316 clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
317 EXFAT_FIRST_CLUSTER);
318 clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
319 trim_minlen = range->minlen >> sbi->cluster_size_bits;
320
321 if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
322 return -EINVAL;
323
324 if (clu_end >= sbi->num_clusters)
325 clu_end = sbi->num_clusters - 1;
326
327 mutex_lock(&sbi->bitmap_lock);
328
329 trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
330 if (trim_begin == EXFAT_EOF_CLUSTER)
331 goto unlock;
332
333 next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
334 if (next_free_clu == EXFAT_EOF_CLUSTER)
335 goto unlock;
336
337 do {
338 if (next_free_clu == trim_end + 1) {
339 /* extend trim range for continuous free cluster */
340 trim_end++;
341 } else {
342 /* trim current range if it's larger than trim_minlen */
343 count = trim_end - trim_begin + 1;
344 if (count >= trim_minlen) {
345 err = sb_issue_discard(sb,
346 exfat_cluster_to_sector(sbi, trim_begin),
347 count * sbi->sect_per_clus, GFP_NOFS, 0);
348 if (err)
349 goto unlock;
350
351 trimmed_total += count;
352 }
353
354 /* set next start point of the free hole */
355 trim_begin = trim_end = next_free_clu;
356 }
357
358 if (next_free_clu >= clu_end)
359 break;
360
361 if (fatal_signal_pending(current)) {
362 err = -ERESTARTSYS;
363 goto unlock;
364 }
365
366 next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
367 } while (next_free_clu != EXFAT_EOF_CLUSTER &&
368 next_free_clu > trim_end);
369
370 /* try to trim remainder */
371 count = trim_end - trim_begin + 1;
372 if (count >= trim_minlen) {
373 err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
374 count * sbi->sect_per_clus, GFP_NOFS, 0);
375 if (err)
376 goto unlock;
377
378 trimmed_total += count;
379 }
380
381 unlock:
382 mutex_unlock(&sbi->bitmap_lock);
383 range->len = trimmed_total << sbi->cluster_size_bits;
384
385 return err;
386 }
387