xref: /linux/fs/exfat/balloc.c (revision e40e023591ff7fa7863cacced9d6452f7805f8cf)
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 (max_ra_count && 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 super_block * sb,unsigned int clu,bool sync)186 int exfat_set_bitmap(struct super_block *sb, unsigned int clu, bool sync)
187 {
188 	int i, b;
189 	unsigned int ent_idx;
190 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
191 
192 	if (!is_valid_cluster(sbi, clu))
193 		return -EINVAL;
194 
195 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
196 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
197 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
198 
199 	set_bit_le(b, sbi->vol_amap[i]->b_data);
200 	exfat_update_bh(sbi->vol_amap[i], sync);
201 	return 0;
202 }
203 
exfat_clear_bitmap(struct super_block * sb,unsigned int clu,bool sync)204 int exfat_clear_bitmap(struct super_block *sb, unsigned int clu, bool sync)
205 {
206 	int i, b;
207 	unsigned int ent_idx;
208 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
209 
210 	if (!is_valid_cluster(sbi, clu))
211 		return -EIO;
212 
213 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
214 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
215 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
216 
217 	if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
218 		return -EIO;
219 
220 	clear_bit_le(b, sbi->vol_amap[i]->b_data);
221 
222 	exfat_update_bh(sbi->vol_amap[i], sync);
223 
224 	return 0;
225 }
226 
exfat_test_bitmap(struct super_block * sb,unsigned int clu)227 bool exfat_test_bitmap(struct super_block *sb, unsigned int clu)
228 {
229 	int i, b;
230 	unsigned int ent_idx;
231 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
232 
233 	if (!sbi->vol_amap)
234 		return true;
235 
236 	if (!is_valid_cluster(sbi, clu))
237 		return false;
238 
239 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
240 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
241 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
242 
243 	if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
244 		return false;
245 
246 	return true;
247 }
248 
249 /*
250  * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
251  * the cluster heap.
252  */
exfat_find_free_bitmap(struct super_block * sb,unsigned int clu)253 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
254 {
255 	unsigned int i, map_i, map_b, ent_idx;
256 	unsigned int clu_base, clu_free;
257 	unsigned long clu_bits, clu_mask;
258 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
259 	__le_long bitval;
260 
261 	WARN_ON(clu < EXFAT_FIRST_CLUSTER);
262 	ent_idx = ALIGN_DOWN(CLUSTER_TO_BITMAP_ENT(clu), BITS_PER_LONG);
263 	clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx);
264 	clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
265 
266 	map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
267 	map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
268 
269 	for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
270 	     i += BITS_PER_LONG) {
271 		bitval = *(__le_long *)(sbi->vol_amap[map_i]->b_data + map_b);
272 		if (clu_mask > 0) {
273 			bitval |= cpu_to_lel(clu_mask);
274 			clu_mask = 0;
275 		}
276 		if (lel_to_cpu(bitval) != ULONG_MAX) {
277 			clu_bits = lel_to_cpu(bitval);
278 			clu_free = clu_base + ffz(clu_bits);
279 			if (clu_free < sbi->num_clusters)
280 				return clu_free;
281 		}
282 		clu_base += BITS_PER_LONG;
283 		map_b += sizeof(long);
284 
285 		if (map_b >= sb->s_blocksize ||
286 		    clu_base >= sbi->num_clusters) {
287 			if (++map_i >= sbi->map_sectors) {
288 				clu_base = EXFAT_FIRST_CLUSTER;
289 				map_i = 0;
290 			}
291 			map_b = 0;
292 		}
293 	}
294 
295 	return EXFAT_EOF_CLUSTER;
296 }
297 
exfat_count_used_clusters(struct super_block * sb,unsigned int * ret_count)298 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
299 {
300 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
301 	unsigned int count = 0;
302 	unsigned int i, map_i = 0, map_b = 0;
303 	unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
304 	unsigned int last_mask = total_clus & (BITS_PER_LONG - 1);
305 	unsigned long *bitmap, clu_bits;
306 
307 	total_clus &= ~last_mask;
308 	for (i = 0; i < total_clus; i += BITS_PER_LONG) {
309 		bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
310 		count += hweight_long(*bitmap);
311 		map_b += sizeof(long);
312 		if (map_b >= (unsigned int)sb->s_blocksize) {
313 			map_i++;
314 			map_b = 0;
315 		}
316 	}
317 
318 	if (last_mask) {
319 		bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
320 		clu_bits = lel_to_cpu(*(__le_long *)bitmap);
321 		count += hweight_long(clu_bits & BITMAP_LAST_WORD_MASK(last_mask));
322 	}
323 
324 	*ret_count = count;
325 	return 0;
326 }
327 
exfat_trim_fs(struct inode * inode,struct fstrim_range * range)328 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
329 {
330 	unsigned int trim_begin, trim_end, count, next_free_clu;
331 	u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
332 	struct super_block *sb = inode->i_sb;
333 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
334 	int err = 0;
335 
336 	clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
337 				EXFAT_FIRST_CLUSTER);
338 	clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
339 	trim_minlen = range->minlen >> sbi->cluster_size_bits;
340 
341 	if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
342 		return -EINVAL;
343 
344 	if (clu_end >= sbi->num_clusters)
345 		clu_end = sbi->num_clusters - 1;
346 
347 	mutex_lock(&sbi->bitmap_lock);
348 
349 	trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
350 	if (trim_begin == EXFAT_EOF_CLUSTER)
351 		goto unlock;
352 
353 	next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
354 	if (next_free_clu == EXFAT_EOF_CLUSTER)
355 		goto unlock;
356 
357 	do {
358 		if (next_free_clu == trim_end + 1) {
359 			/* extend trim range for continuous free cluster */
360 			trim_end++;
361 		} else {
362 			/* trim current range if it's larger than trim_minlen */
363 			count = trim_end - trim_begin + 1;
364 			if (count >= trim_minlen) {
365 				err = sb_issue_discard(sb,
366 					exfat_cluster_to_sector(sbi, trim_begin),
367 					count * sbi->sect_per_clus, GFP_NOFS, 0);
368 				if (err)
369 					goto unlock;
370 
371 				trimmed_total += count;
372 			}
373 
374 			/* set next start point of the free hole */
375 			trim_begin = trim_end = next_free_clu;
376 		}
377 
378 		if (next_free_clu >= clu_end)
379 			break;
380 
381 		if (fatal_signal_pending(current)) {
382 			err = -ERESTARTSYS;
383 			goto unlock;
384 		}
385 
386 		next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
387 	} while (next_free_clu != EXFAT_EOF_CLUSTER &&
388 			next_free_clu > trim_end);
389 
390 	/* try to trim remainder */
391 	count = trim_end - trim_begin + 1;
392 	if (count >= trim_minlen) {
393 		err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
394 			count * sbi->sect_per_clus, GFP_NOFS, 0);
395 		if (err)
396 			goto unlock;
397 
398 		trimmed_total += count;
399 	}
400 
401 unlock:
402 	mutex_unlock(&sbi->bitmap_lock);
403 	range->len = trimmed_total << sbi->cluster_size_bits;
404 
405 	return err;
406 }
407