xref: /linux/fs/exfat/balloc.c (revision a62fe21079978e5134ad863f8a9835eb24c06d43)
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 	long long map_size;
78 	unsigned int i, j, need_map_size;
79 	sector_t sector, end, ra;
80 	blkcnt_t ra_cnt = 0;
81 
82 	sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
83 	map_size = le64_to_cpu(ep->dentry.bitmap.size);
84 	need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE)
85 		+ 1;
86 	if (need_map_size != map_size) {
87 		exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)",
88 			  need_map_size, map_size);
89 		/*
90 		 * Only allowed when bogus allocation
91 		 * bitmap size is large
92 		 */
93 		if (need_map_size > map_size)
94 			return -EIO;
95 	}
96 	sbi->map_sectors = ((need_map_size - 1) >>
97 			(sb->s_blocksize_bits)) + 1;
98 	sbi->vol_amap = kvmalloc_objs(struct buffer_head *, sbi->map_sectors);
99 	if (!sbi->vol_amap)
100 		return -ENOMEM;
101 
102 	sector = ra = exfat_cluster_to_sector(sbi, sbi->map_clu);
103 	end = sector + sbi->map_sectors - 1;
104 
105 	for (i = 0; i < sbi->map_sectors; i++) {
106 		/* Trigger the next readahead in advance. */
107 		exfat_blk_readahead(sb, sector + i, &ra, &ra_cnt, end);
108 
109 		sbi->vol_amap[i] = sb_bread(sb, sector + i);
110 		if (!sbi->vol_amap[i])
111 			goto err_out;
112 	}
113 
114 	if (exfat_test_bitmap_range(sb, sbi->map_clu,
115 		EXFAT_B_TO_CLU_ROUND_UP(map_size, sbi)) == false)
116 		goto err_out;
117 
118 	return 0;
119 
120 err_out:
121 	j = 0;
122 	/* release all buffers and free vol_amap */
123 	while (j < i)
124 		brelse(sbi->vol_amap[j++]);
125 
126 	kvfree(sbi->vol_amap);
127 	sbi->vol_amap = NULL;
128 	return -EIO;
129 }
130 
exfat_load_bitmap(struct super_block * sb)131 int exfat_load_bitmap(struct super_block *sb)
132 {
133 	unsigned int i, type;
134 	struct exfat_chain clu;
135 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
136 
137 	exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
138 	while (clu.dir != EXFAT_EOF_CLUSTER) {
139 		for (i = 0; i < sbi->dentries_per_clu; i++) {
140 			struct exfat_dentry *ep;
141 			struct buffer_head *bh;
142 
143 			ep = exfat_get_dentry(sb, &clu, i, &bh);
144 			if (!ep)
145 				return -EIO;
146 
147 			type = exfat_get_entry_type(ep);
148 			if (type == TYPE_BITMAP &&
149 			    ep->dentry.bitmap.flags == 0x0) {
150 				int err;
151 
152 				err = exfat_allocate_bitmap(sb, ep);
153 				brelse(bh);
154 				return err;
155 			}
156 			brelse(bh);
157 
158 			if (type == TYPE_UNUSED)
159 				return -EINVAL;
160 		}
161 
162 		if (exfat_get_next_cluster(sb, &clu.dir))
163 			return -EIO;
164 	}
165 
166 	return -EINVAL;
167 }
168 
exfat_free_bitmap(struct exfat_sb_info * sbi)169 void exfat_free_bitmap(struct exfat_sb_info *sbi)
170 {
171 	int i;
172 
173 	for (i = 0; i < sbi->map_sectors; i++)
174 		__brelse(sbi->vol_amap[i]);
175 
176 	kvfree(sbi->vol_amap);
177 }
178 
exfat_set_bitmap(struct super_block * sb,unsigned int clu,bool sync)179 int exfat_set_bitmap(struct super_block *sb, unsigned int clu, bool sync)
180 {
181 	int i, b;
182 	unsigned int ent_idx;
183 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
184 
185 	if (!is_valid_cluster(sbi, clu))
186 		return -EINVAL;
187 
188 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
189 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
190 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
191 
192 	set_bit_le(b, sbi->vol_amap[i]->b_data);
193 	exfat_update_bh(sbi->vol_amap[i], sync);
194 	return 0;
195 }
196 
exfat_clear_bitmap(struct super_block * sb,unsigned int clu,bool sync)197 int exfat_clear_bitmap(struct super_block *sb, unsigned int clu, bool sync)
198 {
199 	int i, b;
200 	unsigned int ent_idx;
201 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
202 
203 	if (!is_valid_cluster(sbi, clu))
204 		return -EIO;
205 
206 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
207 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
208 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
209 
210 	if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
211 		return -EIO;
212 
213 	clear_bit_le(b, sbi->vol_amap[i]->b_data);
214 
215 	exfat_update_bh(sbi->vol_amap[i], sync);
216 
217 	return 0;
218 }
219 
exfat_test_bitmap(struct super_block * sb,unsigned int clu)220 bool exfat_test_bitmap(struct super_block *sb, unsigned int clu)
221 {
222 	int i, b;
223 	unsigned int ent_idx;
224 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
225 
226 	if (!sbi->vol_amap)
227 		return true;
228 
229 	if (!is_valid_cluster(sbi, clu))
230 		return false;
231 
232 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
233 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
234 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
235 
236 	if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
237 		return false;
238 
239 	return true;
240 }
241 
242 /*
243  * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
244  * the cluster heap.
245  */
exfat_find_free_bitmap(struct super_block * sb,unsigned int clu)246 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
247 {
248 	unsigned int i, map_i, map_b, ent_idx;
249 	unsigned int clu_base, clu_free;
250 	unsigned long clu_bits, clu_mask;
251 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
252 	__le_long bitval;
253 
254 	WARN_ON(clu < EXFAT_FIRST_CLUSTER);
255 	ent_idx = ALIGN_DOWN(CLUSTER_TO_BITMAP_ENT(clu), BITS_PER_LONG);
256 	clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx);
257 	clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
258 
259 	map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
260 	map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
261 
262 	for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
263 	     i += BITS_PER_LONG) {
264 		bitval = *(__le_long *)(sbi->vol_amap[map_i]->b_data + map_b);
265 		if (clu_mask > 0) {
266 			bitval |= cpu_to_lel(clu_mask);
267 			clu_mask = 0;
268 		}
269 		if (lel_to_cpu(bitval) != ULONG_MAX) {
270 			clu_bits = lel_to_cpu(bitval);
271 			clu_free = clu_base + ffz(clu_bits);
272 			if (clu_free < sbi->num_clusters)
273 				return clu_free;
274 		}
275 		clu_base += BITS_PER_LONG;
276 		map_b += sizeof(long);
277 
278 		if (map_b >= sb->s_blocksize ||
279 		    clu_base >= sbi->num_clusters) {
280 			if (++map_i >= sbi->map_sectors) {
281 				clu_base = EXFAT_FIRST_CLUSTER;
282 				map_i = 0;
283 			}
284 			map_b = 0;
285 		}
286 	}
287 
288 	return EXFAT_EOF_CLUSTER;
289 }
290 
exfat_count_used_clusters(struct super_block * sb,unsigned int * ret_count)291 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
292 {
293 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
294 	unsigned int count = 0;
295 	unsigned int i, map_i = 0, map_b = 0;
296 	unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
297 	unsigned int last_mask = total_clus & (BITS_PER_LONG - 1);
298 	unsigned long *bitmap, clu_bits;
299 
300 	total_clus &= ~last_mask;
301 	for (i = 0; i < total_clus; i += BITS_PER_LONG) {
302 		bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
303 		count += hweight_long(*bitmap);
304 		map_b += sizeof(long);
305 		if (map_b >= (unsigned int)sb->s_blocksize) {
306 			map_i++;
307 			map_b = 0;
308 		}
309 	}
310 
311 	if (last_mask) {
312 		bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
313 		clu_bits = lel_to_cpu(*(__le_long *)bitmap);
314 		count += hweight_long(clu_bits & BITMAP_LAST_WORD_MASK(last_mask));
315 	}
316 
317 	*ret_count = count;
318 	return 0;
319 }
320 
exfat_trim_fs(struct inode * inode,struct fstrim_range * range)321 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
322 {
323 	unsigned int trim_begin, trim_end, count, next_free_clu;
324 	u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
325 	struct super_block *sb = inode->i_sb;
326 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
327 	int err = 0;
328 
329 	clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
330 				EXFAT_FIRST_CLUSTER);
331 	clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
332 	trim_minlen = range->minlen >> sbi->cluster_size_bits;
333 
334 	if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
335 		return -EINVAL;
336 
337 	if (clu_end >= sbi->num_clusters)
338 		clu_end = sbi->num_clusters - 1;
339 
340 	mutex_lock(&sbi->bitmap_lock);
341 
342 	trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
343 	if (trim_begin == EXFAT_EOF_CLUSTER)
344 		goto unlock;
345 
346 	next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
347 	if (next_free_clu == EXFAT_EOF_CLUSTER)
348 		goto unlock;
349 
350 	do {
351 		if (next_free_clu == trim_end + 1) {
352 			/* extend trim range for continuous free cluster */
353 			trim_end++;
354 		} else {
355 			/* trim current range if it's larger than trim_minlen */
356 			count = trim_end - trim_begin + 1;
357 			if (count >= trim_minlen) {
358 				err = sb_issue_discard(sb,
359 					exfat_cluster_to_sector(sbi, trim_begin),
360 					count * sbi->sect_per_clus, GFP_NOFS, 0);
361 				if (err)
362 					goto unlock;
363 
364 				trimmed_total += count;
365 			}
366 
367 			/* set next start point of the free hole */
368 			trim_begin = trim_end = next_free_clu;
369 		}
370 
371 		if (next_free_clu >= clu_end)
372 			break;
373 
374 		if (fatal_signal_pending(current)) {
375 			err = -ERESTARTSYS;
376 			goto unlock;
377 		}
378 
379 		next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
380 	} while (next_free_clu != EXFAT_EOF_CLUSTER &&
381 			next_free_clu > trim_end);
382 
383 	/* try to trim remainder */
384 	count = trim_end - trim_begin + 1;
385 	if (count >= trim_minlen) {
386 		err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
387 			count * sbi->sect_per_clus, GFP_NOFS, 0);
388 		if (err)
389 			goto unlock;
390 
391 		trimmed_total += count;
392 	}
393 
394 unlock:
395 	mutex_unlock(&sbi->bitmap_lock);
396 	range->len = trimmed_total << sbi->cluster_size_bits;
397 
398 	return err;
399 }
400