xref: /linux/fs/exfat/balloc.c (revision 06ed6aa56ffac9241e03a24649e8d048f8f1b10c)
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/buffer_head.h>
9 
10 #include "exfat_raw.h"
11 #include "exfat_fs.h"
12 
13 static const unsigned char free_bit[] = {
14 	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*  0 ~  19*/
15 	0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~  39*/
16 	0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~  59*/
17 	0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~  79*/
18 	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~  99*/
19 	0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/
20 	0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/
21 	0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/
22 	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/
23 	0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/
24 	0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1,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, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
42 	4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8             /*240 ~ 255*/
43 };
44 
45 /*
46  *  Allocation Bitmap Management Functions
47  */
48 static int exfat_allocate_bitmap(struct super_block *sb,
49 		struct exfat_dentry *ep)
50 {
51 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
52 	long long map_size;
53 	unsigned int i, need_map_size;
54 	sector_t sector;
55 
56 	sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
57 	map_size = le64_to_cpu(ep->dentry.bitmap.size);
58 	need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE)
59 		+ 1;
60 	if (need_map_size != map_size) {
61 		exfat_msg(sb, KERN_ERR,
62 				"bogus allocation bitmap size(need : %u, cur : %lld)",
63 				need_map_size, map_size);
64 		/*
65 		 * Only allowed when bogus allocation
66 		 * bitmap size is large
67 		 */
68 		if (need_map_size > map_size)
69 			return -EIO;
70 	}
71 	sbi->map_sectors = ((need_map_size - 1) >>
72 			(sb->s_blocksize_bits)) + 1;
73 	sbi->vol_amap = kmalloc_array(sbi->map_sectors,
74 				sizeof(struct buffer_head *), GFP_KERNEL);
75 	if (!sbi->vol_amap)
76 		return -ENOMEM;
77 
78 	sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
79 	for (i = 0; i < sbi->map_sectors; i++) {
80 		sbi->vol_amap[i] = sb_bread(sb, sector + i);
81 		if (!sbi->vol_amap[i]) {
82 			/* release all buffers and free vol_amap */
83 			int j = 0;
84 
85 			while (j < i)
86 				brelse(sbi->vol_amap[j++]);
87 
88 			kfree(sbi->vol_amap);
89 			sbi->vol_amap = NULL;
90 			return -EIO;
91 		}
92 	}
93 
94 	return 0;
95 }
96 
97 int exfat_load_bitmap(struct super_block *sb)
98 {
99 	unsigned int i, type;
100 	struct exfat_chain clu;
101 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
102 
103 	exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
104 	while (clu.dir != EXFAT_EOF_CLUSTER) {
105 		for (i = 0; i < sbi->dentries_per_clu; i++) {
106 			struct exfat_dentry *ep;
107 			struct buffer_head *bh;
108 
109 			ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
110 			if (!ep)
111 				return -EIO;
112 
113 			type = exfat_get_entry_type(ep);
114 			if (type == TYPE_UNUSED)
115 				break;
116 			if (type != TYPE_BITMAP)
117 				continue;
118 			if (ep->dentry.bitmap.flags == 0x0) {
119 				int err;
120 
121 				err = exfat_allocate_bitmap(sb, ep);
122 				brelse(bh);
123 				return err;
124 			}
125 			brelse(bh);
126 		}
127 
128 		if (exfat_get_next_cluster(sb, &clu.dir))
129 			return -EIO;
130 	}
131 
132 	return -EINVAL;
133 }
134 
135 void exfat_free_bitmap(struct exfat_sb_info *sbi)
136 {
137 	int i;
138 
139 	for (i = 0; i < sbi->map_sectors; i++)
140 		__brelse(sbi->vol_amap[i]);
141 
142 	kfree(sbi->vol_amap);
143 }
144 
145 /*
146  * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
147  * the cluster heap.
148  */
149 int exfat_set_bitmap(struct inode *inode, unsigned int clu)
150 {
151 	int i, b;
152 	unsigned int ent_idx;
153 	struct super_block *sb = inode->i_sb;
154 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
155 
156 	WARN_ON(clu < EXFAT_FIRST_CLUSTER);
157 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
158 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
159 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
160 
161 	set_bit_le(b, sbi->vol_amap[i]->b_data);
162 	exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode));
163 	return 0;
164 }
165 
166 /*
167  * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
168  * the cluster heap.
169  */
170 void exfat_clear_bitmap(struct inode *inode, unsigned int clu)
171 {
172 	int i, b;
173 	unsigned int ent_idx;
174 	struct super_block *sb = inode->i_sb;
175 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
176 	struct exfat_mount_options *opts = &sbi->options;
177 
178 	WARN_ON(clu < EXFAT_FIRST_CLUSTER);
179 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
180 	i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
181 	b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
182 
183 	clear_bit_le(b, sbi->vol_amap[i]->b_data);
184 	exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode));
185 
186 	if (opts->discard) {
187 		int ret_discard;
188 
189 		ret_discard = sb_issue_discard(sb,
190 			exfat_cluster_to_sector(sbi, clu +
191 						EXFAT_RESERVED_CLUSTERS),
192 			(1 << sbi->sect_per_clus_bits), GFP_NOFS, 0);
193 
194 		if (ret_discard == -EOPNOTSUPP) {
195 			exfat_msg(sb, KERN_ERR,
196 				"discard not supported by device, disabling");
197 			opts->discard = 0;
198 		}
199 	}
200 }
201 
202 /*
203  * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
204  * the cluster heap.
205  */
206 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
207 {
208 	unsigned int i, map_i, map_b, ent_idx;
209 	unsigned int clu_base, clu_free;
210 	unsigned char k, clu_mask;
211 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
212 
213 	WARN_ON(clu < EXFAT_FIRST_CLUSTER);
214 	ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
215 	clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx & ~(BITS_PER_BYTE_MASK));
216 	clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
217 
218 	map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
219 	map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
220 
221 	for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
222 	     i += BITS_PER_BYTE) {
223 		k = *(sbi->vol_amap[map_i]->b_data + map_b);
224 		if (clu_mask > 0) {
225 			k |= clu_mask;
226 			clu_mask = 0;
227 		}
228 		if (k < 0xFF) {
229 			clu_free = clu_base + free_bit[k];
230 			if (clu_free < sbi->num_clusters)
231 				return clu_free;
232 		}
233 		clu_base += BITS_PER_BYTE;
234 
235 		if (++map_b >= sb->s_blocksize ||
236 		    clu_base >= sbi->num_clusters) {
237 			if (++map_i >= sbi->map_sectors) {
238 				clu_base = EXFAT_FIRST_CLUSTER;
239 				map_i = 0;
240 			}
241 			map_b = 0;
242 		}
243 	}
244 
245 	return EXFAT_EOF_CLUSTER;
246 }
247 
248 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
249 {
250 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
251 	unsigned int count = 0;
252 	unsigned int i, map_i = 0, map_b = 0;
253 	unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
254 	unsigned int last_mask = total_clus & BITS_PER_BYTE_MASK;
255 	unsigned char clu_bits;
256 	const unsigned char last_bit_mask[] = {0, 0b00000001, 0b00000011,
257 		0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111};
258 
259 	total_clus &= ~last_mask;
260 	for (i = 0; i < total_clus; i += BITS_PER_BYTE) {
261 		clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
262 		count += used_bit[clu_bits];
263 		if (++map_b >= (unsigned int)sb->s_blocksize) {
264 			map_i++;
265 			map_b = 0;
266 		}
267 	}
268 
269 	if (last_mask) {
270 		clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
271 		clu_bits &= last_bit_mask[last_mask];
272 		count += used_bit[clu_bits];
273 	}
274 
275 	*ret_count = count;
276 	return 0;
277 }
278