xref: /openbmc/linux/fs/exfat/fatent.c (revision 593692d2)
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 
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 
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 
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 
84 static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
85 		unsigned int clus)
86 {
87 	return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;
88 }
89 
90 int exfat_ent_get(struct super_block *sb, unsigned int loc,
91 		unsigned int *content)
92 {
93 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
94 	int err;
95 
96 	if (!is_valid_cluster(sbi, loc)) {
97 		exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
98 			loc);
99 		return -EIO;
100 	}
101 
102 	err = __exfat_ent_get(sb, loc, content);
103 	if (err) {
104 		exfat_fs_error(sb,
105 			"failed to access to FAT (entry 0x%08x, err:%d)",
106 			loc, err);
107 		return err;
108 	}
109 
110 	if (*content == EXFAT_FREE_CLUSTER) {
111 		exfat_fs_error(sb,
112 			"invalid access to FAT free cluster (entry 0x%08x)",
113 			loc);
114 		return -EIO;
115 	}
116 
117 	if (*content == EXFAT_BAD_CLUSTER) {
118 		exfat_fs_error(sb,
119 			"invalid access to FAT bad cluster (entry 0x%08x)",
120 			loc);
121 		return -EIO;
122 	}
123 
124 	if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
125 		exfat_fs_error(sb,
126 			"invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
127 			loc, *content);
128 		return -EIO;
129 	}
130 
131 	return 0;
132 }
133 
134 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
135 		unsigned int len)
136 {
137 	if (!len)
138 		return 0;
139 
140 	while (len > 1) {
141 		if (exfat_ent_set(sb, chain, chain + 1))
142 			return -EIO;
143 		chain++;
144 		len--;
145 	}
146 
147 	if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
148 		return -EIO;
149 	return 0;
150 }
151 
152 /* This function must be called with bitmap_lock held */
153 static int __exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
154 {
155 	struct super_block *sb = inode->i_sb;
156 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
157 	int cur_cmap_i, next_cmap_i;
158 	unsigned int num_clusters = 0;
159 	unsigned int clu;
160 
161 	/* invalid cluster number */
162 	if (p_chain->dir == EXFAT_FREE_CLUSTER ||
163 	    p_chain->dir == EXFAT_EOF_CLUSTER ||
164 	    p_chain->dir < EXFAT_FIRST_CLUSTER)
165 		return 0;
166 
167 	/* no cluster to truncate */
168 	if (p_chain->size == 0)
169 		return 0;
170 
171 	/* check cluster validation */
172 	if (!is_valid_cluster(sbi, p_chain->dir)) {
173 		exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
174 		return -EIO;
175 	}
176 
177 	clu = p_chain->dir;
178 
179 	cur_cmap_i = next_cmap_i =
180 		BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu));
181 
182 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
183 		unsigned int last_cluster = p_chain->dir + p_chain->size - 1;
184 		do {
185 			bool sync = false;
186 
187 			if (clu < last_cluster)
188 				next_cmap_i =
189 				  BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu+1));
190 
191 			/* flush bitmap only if index would be changed or for last cluster */
192 			if (clu == last_cluster || cur_cmap_i != next_cmap_i) {
193 				sync = true;
194 				cur_cmap_i = next_cmap_i;
195 			}
196 
197 			exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
198 			clu++;
199 			num_clusters++;
200 		} while (num_clusters < p_chain->size);
201 	} else {
202 		do {
203 			bool sync = false;
204 			unsigned int n_clu = clu;
205 			int err = exfat_get_next_cluster(sb, &n_clu);
206 
207 			if (err || n_clu == EXFAT_EOF_CLUSTER)
208 				sync = true;
209 			else
210 				next_cmap_i =
211 				  BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(n_clu));
212 
213 			if (cur_cmap_i != next_cmap_i) {
214 				sync = true;
215 				cur_cmap_i = next_cmap_i;
216 			}
217 
218 			exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
219 			clu = n_clu;
220 			num_clusters++;
221 
222 			if (err)
223 				goto dec_used_clus;
224 		} while (clu != EXFAT_EOF_CLUSTER);
225 	}
226 
227 dec_used_clus:
228 	sbi->used_clusters -= num_clusters;
229 	return 0;
230 }
231 
232 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
233 {
234 	int ret = 0;
235 
236 	mutex_lock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
237 	ret = __exfat_free_cluster(inode, p_chain);
238 	mutex_unlock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
239 
240 	return ret;
241 }
242 
243 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
244 		unsigned int *ret_clu)
245 {
246 	unsigned int clu, next;
247 	unsigned int count = 0;
248 
249 	next = p_chain->dir;
250 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
251 		*ret_clu = next + p_chain->size - 1;
252 		return 0;
253 	}
254 
255 	do {
256 		count++;
257 		clu = next;
258 		if (exfat_ent_get(sb, clu, &next))
259 			return -EIO;
260 	} while (next != EXFAT_EOF_CLUSTER);
261 
262 	if (p_chain->size != count) {
263 		exfat_fs_error(sb,
264 			"bogus directory size (clus : ondisk(%d) != counted(%d))",
265 			p_chain->size, count);
266 		return -EIO;
267 	}
268 
269 	*ret_clu = clu;
270 	return 0;
271 }
272 
273 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
274 {
275 	struct super_block *sb = dir->i_sb;
276 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
277 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
278 	int nr_bhs = MAX_BUF_PER_PAGE;
279 	sector_t blknr, last_blknr;
280 	int err, i, n;
281 
282 	blknr = exfat_cluster_to_sector(sbi, clu);
283 	last_blknr = blknr + sbi->sect_per_clus;
284 
285 	if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
286 		exfat_fs_error_ratelimit(sb,
287 			"%s: out of range(sect:%llu len:%u)",
288 			__func__, (unsigned long long)blknr,
289 			sbi->sect_per_clus);
290 		return -EIO;
291 	}
292 
293 	/* Zeroing the unused blocks on this cluster */
294 	while (blknr < last_blknr) {
295 		for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) {
296 			bhs[n] = sb_getblk(sb, blknr);
297 			if (!bhs[n]) {
298 				err = -ENOMEM;
299 				goto release_bhs;
300 			}
301 			memset(bhs[n]->b_data, 0, sb->s_blocksize);
302 		}
303 
304 		err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir));
305 		if (err)
306 			goto release_bhs;
307 
308 		for (i = 0; i < n; i++)
309 			brelse(bhs[i]);
310 	}
311 	return 0;
312 
313 release_bhs:
314 	exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
315 	for (i = 0; i < n; i++)
316 		bforget(bhs[i]);
317 	return err;
318 }
319 
320 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
321 		struct exfat_chain *p_chain, bool sync_bmap)
322 {
323 	int ret = -ENOSPC;
324 	unsigned int num_clusters = 0, total_cnt;
325 	unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
326 	struct super_block *sb = inode->i_sb;
327 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
328 
329 	total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
330 
331 	if (unlikely(total_cnt < sbi->used_clusters)) {
332 		exfat_fs_error_ratelimit(sb,
333 			"%s: invalid used clusters(t:%u,u:%u)\n",
334 			__func__, total_cnt, sbi->used_clusters);
335 		return -EIO;
336 	}
337 
338 	if (num_alloc > total_cnt - sbi->used_clusters)
339 		return -ENOSPC;
340 
341 	mutex_lock(&sbi->bitmap_lock);
342 
343 	hint_clu = p_chain->dir;
344 	/* find new cluster */
345 	if (hint_clu == EXFAT_EOF_CLUSTER) {
346 		if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
347 			exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
348 				  sbi->clu_srch_ptr);
349 			sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
350 		}
351 
352 		hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
353 		if (hint_clu == EXFAT_EOF_CLUSTER) {
354 			ret = -ENOSPC;
355 			goto unlock;
356 		}
357 	}
358 
359 	/* check cluster validation */
360 	if (!is_valid_cluster(sbi, hint_clu)) {
361 		exfat_err(sb, "hint_cluster is invalid (%u)",
362 			hint_clu);
363 		hint_clu = EXFAT_FIRST_CLUSTER;
364 		if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
365 			if (exfat_chain_cont_cluster(sb, p_chain->dir,
366 					num_clusters)) {
367 				ret = -EIO;
368 				goto unlock;
369 			}
370 			p_chain->flags = ALLOC_FAT_CHAIN;
371 		}
372 	}
373 
374 	p_chain->dir = EXFAT_EOF_CLUSTER;
375 
376 	while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
377 	       EXFAT_EOF_CLUSTER) {
378 		if (new_clu != hint_clu &&
379 		    p_chain->flags == ALLOC_NO_FAT_CHAIN) {
380 			if (exfat_chain_cont_cluster(sb, p_chain->dir,
381 					num_clusters)) {
382 				ret = -EIO;
383 				goto free_cluster;
384 			}
385 			p_chain->flags = ALLOC_FAT_CHAIN;
386 		}
387 
388 		/* update allocation bitmap */
389 		if (exfat_set_bitmap(inode, new_clu, sync_bmap)) {
390 			ret = -EIO;
391 			goto free_cluster;
392 		}
393 
394 		num_clusters++;
395 
396 		/* update FAT table */
397 		if (p_chain->flags == ALLOC_FAT_CHAIN) {
398 			if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
399 				ret = -EIO;
400 				goto free_cluster;
401 			}
402 		}
403 
404 		if (p_chain->dir == EXFAT_EOF_CLUSTER) {
405 			p_chain->dir = new_clu;
406 		} else if (p_chain->flags == ALLOC_FAT_CHAIN) {
407 			if (exfat_ent_set(sb, last_clu, new_clu)) {
408 				ret = -EIO;
409 				goto free_cluster;
410 			}
411 		}
412 		last_clu = new_clu;
413 
414 		if (--num_alloc == 0) {
415 			sbi->clu_srch_ptr = hint_clu;
416 			sbi->used_clusters += num_clusters;
417 
418 			p_chain->size += num_clusters;
419 			mutex_unlock(&sbi->bitmap_lock);
420 			return 0;
421 		}
422 
423 		hint_clu = new_clu + 1;
424 		if (hint_clu >= sbi->num_clusters) {
425 			hint_clu = EXFAT_FIRST_CLUSTER;
426 
427 			if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
428 				if (exfat_chain_cont_cluster(sb, p_chain->dir,
429 						num_clusters)) {
430 					ret = -EIO;
431 					goto free_cluster;
432 				}
433 				p_chain->flags = ALLOC_FAT_CHAIN;
434 			}
435 		}
436 	}
437 free_cluster:
438 	if (num_clusters)
439 		__exfat_free_cluster(inode, p_chain);
440 unlock:
441 	mutex_unlock(&sbi->bitmap_lock);
442 	return ret;
443 }
444 
445 int exfat_count_num_clusters(struct super_block *sb,
446 		struct exfat_chain *p_chain, unsigned int *ret_count)
447 {
448 	unsigned int i, count;
449 	unsigned int clu;
450 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
451 
452 	if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
453 		*ret_count = 0;
454 		return 0;
455 	}
456 
457 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
458 		*ret_count = p_chain->size;
459 		return 0;
460 	}
461 
462 	clu = p_chain->dir;
463 	count = 0;
464 	for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
465 		count++;
466 		if (exfat_ent_get(sb, clu, &clu))
467 			return -EIO;
468 		if (clu == EXFAT_EOF_CLUSTER)
469 			break;
470 	}
471 
472 	*ret_count = count;
473 	return 0;
474 }
475