xref: /OK3568_Linux_fs/kernel/fs/exfat/fatent.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4  */
5 
6 #include <linux/slab.h>
7 #include <asm/unaligned.h>
8 #include <linux/buffer_head.h>
9 
10 #include "exfat_raw.h"
11 #include "exfat_fs.h"
12 
exfat_mirror_bh(struct super_block * sb,sector_t sec,struct buffer_head * bh)13 static int exfat_mirror_bh(struct super_block *sb, sector_t sec,
14 		struct buffer_head *bh)
15 {
16 	struct buffer_head *c_bh;
17 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
18 	sector_t sec2;
19 	int err = 0;
20 
21 	if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) {
22 		sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector;
23 		c_bh = sb_getblk(sb, sec2);
24 		if (!c_bh)
25 			return -ENOMEM;
26 		memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
27 		set_buffer_uptodate(c_bh);
28 		mark_buffer_dirty(c_bh);
29 		if (sb->s_flags & SB_SYNCHRONOUS)
30 			err = sync_dirty_buffer(c_bh);
31 		brelse(c_bh);
32 	}
33 
34 	return err;
35 }
36 
__exfat_ent_get(struct super_block * sb,unsigned int loc,unsigned int * content)37 static int __exfat_ent_get(struct super_block *sb, unsigned int loc,
38 		unsigned int *content)
39 {
40 	unsigned int off;
41 	sector_t sec;
42 	struct buffer_head *bh;
43 
44 	sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
45 	off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
46 
47 	bh = sb_bread(sb, sec);
48 	if (!bh)
49 		return -EIO;
50 
51 	*content = le32_to_cpu(*(__le32 *)(&bh->b_data[off]));
52 
53 	/* remap reserved clusters to simplify code */
54 	if (*content > EXFAT_BAD_CLUSTER)
55 		*content = EXFAT_EOF_CLUSTER;
56 
57 	brelse(bh);
58 	return 0;
59 }
60 
exfat_ent_set(struct super_block * sb,unsigned int loc,unsigned int content)61 int exfat_ent_set(struct super_block *sb, unsigned int loc,
62 		unsigned int content)
63 {
64 	unsigned int off;
65 	sector_t sec;
66 	__le32 *fat_entry;
67 	struct buffer_head *bh;
68 
69 	sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
70 	off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
71 
72 	bh = sb_bread(sb, sec);
73 	if (!bh)
74 		return -EIO;
75 
76 	fat_entry = (__le32 *)&(bh->b_data[off]);
77 	*fat_entry = cpu_to_le32(content);
78 	exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS);
79 	exfat_mirror_bh(sb, sec, bh);
80 	brelse(bh);
81 	return 0;
82 }
83 
exfat_ent_get(struct super_block * sb,unsigned int loc,unsigned int * content)84 int exfat_ent_get(struct super_block *sb, unsigned int loc,
85 		unsigned int *content)
86 {
87 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
88 	int err;
89 
90 	if (!is_valid_cluster(sbi, loc)) {
91 		exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
92 			loc);
93 		return -EIO;
94 	}
95 
96 	err = __exfat_ent_get(sb, loc, content);
97 	if (err) {
98 		exfat_fs_error(sb,
99 			"failed to access to FAT (entry 0x%08x, err:%d)",
100 			loc, err);
101 		return err;
102 	}
103 
104 	if (*content == EXFAT_FREE_CLUSTER) {
105 		exfat_fs_error(sb,
106 			"invalid access to FAT free cluster (entry 0x%08x)",
107 			loc);
108 		return -EIO;
109 	}
110 
111 	if (*content == EXFAT_BAD_CLUSTER) {
112 		exfat_fs_error(sb,
113 			"invalid access to FAT bad cluster (entry 0x%08x)",
114 			loc);
115 		return -EIO;
116 	}
117 
118 	if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
119 		exfat_fs_error(sb,
120 			"invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
121 			loc, *content);
122 		return -EIO;
123 	}
124 
125 	return 0;
126 }
127 
exfat_chain_cont_cluster(struct super_block * sb,unsigned int chain,unsigned int len)128 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
129 		unsigned int len)
130 {
131 	if (!len)
132 		return 0;
133 
134 	while (len > 1) {
135 		if (exfat_ent_set(sb, chain, chain + 1))
136 			return -EIO;
137 		chain++;
138 		len--;
139 	}
140 
141 	if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
142 		return -EIO;
143 	return 0;
144 }
145 
exfat_free_cluster(struct inode * inode,struct exfat_chain * p_chain)146 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
147 {
148 	unsigned int num_clusters = 0;
149 	unsigned int clu;
150 	struct super_block *sb = inode->i_sb;
151 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
152 	int cur_cmap_i, next_cmap_i;
153 
154 	/* invalid cluster number */
155 	if (p_chain->dir == EXFAT_FREE_CLUSTER ||
156 	    p_chain->dir == EXFAT_EOF_CLUSTER ||
157 	    p_chain->dir < EXFAT_FIRST_CLUSTER)
158 		return 0;
159 
160 	/* no cluster to truncate */
161 	if (p_chain->size == 0)
162 		return 0;
163 
164 	/* check cluster validation */
165 	if (!is_valid_cluster(sbi, p_chain->dir)) {
166 		exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
167 		return -EIO;
168 	}
169 
170 	clu = p_chain->dir;
171 
172 	cur_cmap_i = next_cmap_i =
173 			BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu));
174 
175 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
176 		unsigned int last_cluster = p_chain->dir + p_chain->size - 1;
177 
178 		do {
179 			bool sync = false;
180 
181 			if (clu < last_cluster)
182 				next_cmap_i =
183 				  BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu+1));
184 
185 			/* flush bitmap only if index would be changed or for last cluster */
186 			if (clu == last_cluster || cur_cmap_i != next_cmap_i) {
187 				sync = true;
188 				cur_cmap_i = next_cmap_i;
189 			}
190 
191 			exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
192 			clu++;
193 			num_clusters++;
194 		} while (num_clusters < p_chain->size);
195 	} else {
196 		do {
197 			bool sync = false;
198 			unsigned int n_clu = clu;
199 			int err = exfat_get_next_cluster(sb, &n_clu);
200 
201 			if (err || n_clu == EXFAT_EOF_CLUSTER)
202 				sync = true;
203 			else
204 				next_cmap_i =
205 				  BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(n_clu));
206 
207 			if (cur_cmap_i != next_cmap_i) {
208 				sync = true;
209 				cur_cmap_i = next_cmap_i;
210 			}
211 
212 			exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
213 			clu = n_clu;
214 
215 			num_clusters++;
216 
217 			if (err)
218 				goto dec_used_clus;
219 		} while (clu != EXFAT_EOF_CLUSTER);
220 	}
221 
222 dec_used_clus:
223 	sbi->used_clusters -= num_clusters;
224 	return 0;
225 }
226 
exfat_find_last_cluster(struct super_block * sb,struct exfat_chain * p_chain,unsigned int * ret_clu)227 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
228 		unsigned int *ret_clu)
229 {
230 	unsigned int clu, next;
231 	unsigned int count = 0;
232 
233 	next = p_chain->dir;
234 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
235 		*ret_clu = next + p_chain->size - 1;
236 		return 0;
237 	}
238 
239 	do {
240 		count++;
241 		clu = next;
242 		if (exfat_ent_get(sb, clu, &next))
243 			return -EIO;
244 	} while (next != EXFAT_EOF_CLUSTER);
245 
246 	if (p_chain->size != count) {
247 		exfat_fs_error(sb,
248 			"bogus directory size (clus : ondisk(%d) != counted(%d))",
249 			p_chain->size, count);
250 		return -EIO;
251 	}
252 
253 	*ret_clu = clu;
254 	return 0;
255 }
256 
exfat_zeroed_cluster(struct inode * dir,unsigned int clu)257 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
258 {
259 	struct super_block *sb = dir->i_sb;
260 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
261 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
262 	int nr_bhs = MAX_BUF_PER_PAGE;
263 	sector_t blknr, last_blknr;
264 	int err, i, n;
265 
266 	blknr = exfat_cluster_to_sector(sbi, clu);
267 	last_blknr = blknr + sbi->sect_per_clus;
268 
269 	if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
270 		exfat_fs_error_ratelimit(sb,
271 			"%s: out of range(sect:%llu len:%u)",
272 			__func__, (unsigned long long)blknr,
273 			sbi->sect_per_clus);
274 		return -EIO;
275 	}
276 
277 	/* Zeroing the unused blocks on this cluster */
278 	while (blknr < last_blknr) {
279 		for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) {
280 			bhs[n] = sb_getblk(sb, blknr);
281 			if (!bhs[n]) {
282 				err = -ENOMEM;
283 				goto release_bhs;
284 			}
285 			memset(bhs[n]->b_data, 0, sb->s_blocksize);
286 		}
287 
288 		err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir));
289 		if (err)
290 			goto release_bhs;
291 
292 		for (i = 0; i < n; i++)
293 			brelse(bhs[i]);
294 	}
295 	return 0;
296 
297 release_bhs:
298 	exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
299 	for (i = 0; i < n; i++)
300 		bforget(bhs[i]);
301 	return err;
302 }
303 
exfat_alloc_cluster(struct inode * inode,unsigned int num_alloc,struct exfat_chain * p_chain,bool sync_bmap)304 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
305 		struct exfat_chain *p_chain, bool sync_bmap)
306 {
307 	int ret = -ENOSPC;
308 	unsigned int num_clusters = 0, total_cnt;
309 	unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
310 	struct super_block *sb = inode->i_sb;
311 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
312 
313 	total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
314 
315 	if (unlikely(total_cnt < sbi->used_clusters)) {
316 		exfat_fs_error_ratelimit(sb,
317 			"%s: invalid used clusters(t:%u,u:%u)\n",
318 			__func__, total_cnt, sbi->used_clusters);
319 		return -EIO;
320 	}
321 
322 	if (num_alloc > total_cnt - sbi->used_clusters)
323 		return -ENOSPC;
324 
325 	hint_clu = p_chain->dir;
326 	/* find new cluster */
327 	if (hint_clu == EXFAT_EOF_CLUSTER) {
328 		if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
329 			exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
330 				  sbi->clu_srch_ptr);
331 			sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
332 		}
333 
334 		hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
335 		if (hint_clu == EXFAT_EOF_CLUSTER)
336 			return -ENOSPC;
337 	}
338 
339 	/* check cluster validation */
340 	if (!is_valid_cluster(sbi, hint_clu)) {
341 		exfat_err(sb, "hint_cluster is invalid (%u)",
342 			hint_clu);
343 		hint_clu = EXFAT_FIRST_CLUSTER;
344 		if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
345 			if (exfat_chain_cont_cluster(sb, p_chain->dir,
346 					num_clusters))
347 				return -EIO;
348 			p_chain->flags = ALLOC_FAT_CHAIN;
349 		}
350 	}
351 
352 	p_chain->dir = EXFAT_EOF_CLUSTER;
353 
354 	while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
355 	       EXFAT_EOF_CLUSTER) {
356 		if (new_clu != hint_clu &&
357 		    p_chain->flags == ALLOC_NO_FAT_CHAIN) {
358 			if (exfat_chain_cont_cluster(sb, p_chain->dir,
359 					num_clusters)) {
360 				ret = -EIO;
361 				goto free_cluster;
362 			}
363 			p_chain->flags = ALLOC_FAT_CHAIN;
364 		}
365 
366 		/* update allocation bitmap */
367 		if (exfat_set_bitmap(inode, new_clu, sync_bmap)) {
368 			ret = -EIO;
369 			goto free_cluster;
370 		}
371 
372 		num_clusters++;
373 
374 		/* update FAT table */
375 		if (p_chain->flags == ALLOC_FAT_CHAIN) {
376 			if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
377 				ret = -EIO;
378 				goto free_cluster;
379 			}
380 		}
381 
382 		if (p_chain->dir == EXFAT_EOF_CLUSTER) {
383 			p_chain->dir = new_clu;
384 		} else if (p_chain->flags == ALLOC_FAT_CHAIN) {
385 			if (exfat_ent_set(sb, last_clu, new_clu)) {
386 				ret = -EIO;
387 				goto free_cluster;
388 			}
389 		}
390 		last_clu = new_clu;
391 
392 		if (--num_alloc == 0) {
393 			sbi->clu_srch_ptr = hint_clu;
394 			sbi->used_clusters += num_clusters;
395 
396 			p_chain->size += num_clusters;
397 			return 0;
398 		}
399 
400 		hint_clu = new_clu + 1;
401 		if (hint_clu >= sbi->num_clusters) {
402 			hint_clu = EXFAT_FIRST_CLUSTER;
403 
404 			if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
405 				if (exfat_chain_cont_cluster(sb, p_chain->dir,
406 						num_clusters)) {
407 					ret = -EIO;
408 					goto free_cluster;
409 				}
410 				p_chain->flags = ALLOC_FAT_CHAIN;
411 			}
412 		}
413 	}
414 free_cluster:
415 	if (num_clusters)
416 		exfat_free_cluster(inode, p_chain);
417 	return ret;
418 }
419 
exfat_count_num_clusters(struct super_block * sb,struct exfat_chain * p_chain,unsigned int * ret_count)420 int exfat_count_num_clusters(struct super_block *sb,
421 		struct exfat_chain *p_chain, unsigned int *ret_count)
422 {
423 	unsigned int i, count;
424 	unsigned int clu;
425 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
426 
427 	if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
428 		*ret_count = 0;
429 		return 0;
430 	}
431 
432 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
433 		*ret_count = p_chain->size;
434 		return 0;
435 	}
436 
437 	clu = p_chain->dir;
438 	count = 0;
439 	for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
440 		count++;
441 		if (exfat_ent_get(sb, clu, &clu))
442 			return -EIO;
443 		if (clu == EXFAT_EOF_CLUSTER)
444 			break;
445 	}
446 
447 	*ret_count = count;
448 	return 0;
449 }
450