xref: /kernel/linux/linux-5.10/fs/exfat/fatent.c (revision 8c2ecf20)
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
13static 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
37static 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
61int 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
84int 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
128int 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
146int 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
153	/* invalid cluster number */
154	if (p_chain->dir == EXFAT_FREE_CLUSTER ||
155	    p_chain->dir == EXFAT_EOF_CLUSTER ||
156	    p_chain->dir < EXFAT_FIRST_CLUSTER)
157		return 0;
158
159	/* no cluster to truncate */
160	if (p_chain->size == 0)
161		return 0;
162
163	/* check cluster validation */
164	if (!is_valid_cluster(sbi, p_chain->dir)) {
165		exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
166		return -EIO;
167	}
168
169	clu = p_chain->dir;
170
171	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
172		do {
173			exfat_clear_bitmap(inode, clu);
174			clu++;
175
176			num_clusters++;
177		} while (num_clusters < p_chain->size);
178	} else {
179		do {
180			exfat_clear_bitmap(inode, clu);
181
182			if (exfat_get_next_cluster(sb, &clu))
183				goto dec_used_clus;
184
185			num_clusters++;
186		} while (clu != EXFAT_EOF_CLUSTER);
187	}
188
189dec_used_clus:
190	sbi->used_clusters -= num_clusters;
191	return 0;
192}
193
194int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
195		unsigned int *ret_clu)
196{
197	unsigned int clu, next;
198	unsigned int count = 0;
199
200	next = p_chain->dir;
201	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
202		*ret_clu = next + p_chain->size - 1;
203		return 0;
204	}
205
206	do {
207		count++;
208		clu = next;
209		if (exfat_ent_get(sb, clu, &next))
210			return -EIO;
211	} while (next != EXFAT_EOF_CLUSTER);
212
213	if (p_chain->size != count) {
214		exfat_fs_error(sb,
215			"bogus directory size (clus : ondisk(%d) != counted(%d))",
216			p_chain->size, count);
217		return -EIO;
218	}
219
220	*ret_clu = clu;
221	return 0;
222}
223
224int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
225{
226	struct super_block *sb = dir->i_sb;
227	struct exfat_sb_info *sbi = EXFAT_SB(sb);
228	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
229	int nr_bhs = MAX_BUF_PER_PAGE;
230	sector_t blknr, last_blknr;
231	int err, i, n;
232
233	blknr = exfat_cluster_to_sector(sbi, clu);
234	last_blknr = blknr + sbi->sect_per_clus;
235
236	if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
237		exfat_fs_error_ratelimit(sb,
238			"%s: out of range(sect:%llu len:%u)",
239			__func__, (unsigned long long)blknr,
240			sbi->sect_per_clus);
241		return -EIO;
242	}
243
244	/* Zeroing the unused blocks on this cluster */
245	while (blknr < last_blknr) {
246		for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) {
247			bhs[n] = sb_getblk(sb, blknr);
248			if (!bhs[n]) {
249				err = -ENOMEM;
250				goto release_bhs;
251			}
252			memset(bhs[n]->b_data, 0, sb->s_blocksize);
253		}
254
255		err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir));
256		if (err)
257			goto release_bhs;
258
259		for (i = 0; i < n; i++)
260			brelse(bhs[i]);
261	}
262	return 0;
263
264release_bhs:
265	exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
266	for (i = 0; i < n; i++)
267		bforget(bhs[i]);
268	return err;
269}
270
271int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
272		struct exfat_chain *p_chain)
273{
274	int ret = -ENOSPC;
275	unsigned int num_clusters = 0, total_cnt;
276	unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
277	struct super_block *sb = inode->i_sb;
278	struct exfat_sb_info *sbi = EXFAT_SB(sb);
279
280	total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
281
282	if (unlikely(total_cnt < sbi->used_clusters)) {
283		exfat_fs_error_ratelimit(sb,
284			"%s: invalid used clusters(t:%u,u:%u)\n",
285			__func__, total_cnt, sbi->used_clusters);
286		return -EIO;
287	}
288
289	if (num_alloc > total_cnt - sbi->used_clusters)
290		return -ENOSPC;
291
292	hint_clu = p_chain->dir;
293	/* find new cluster */
294	if (hint_clu == EXFAT_EOF_CLUSTER) {
295		if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
296			exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
297				  sbi->clu_srch_ptr);
298			sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
299		}
300
301		hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
302		if (hint_clu == EXFAT_EOF_CLUSTER)
303			return -ENOSPC;
304	}
305
306	/* check cluster validation */
307	if (!is_valid_cluster(sbi, hint_clu)) {
308		exfat_err(sb, "hint_cluster is invalid (%u)",
309			hint_clu);
310		hint_clu = EXFAT_FIRST_CLUSTER;
311		if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
312			if (exfat_chain_cont_cluster(sb, p_chain->dir,
313					num_clusters))
314				return -EIO;
315			p_chain->flags = ALLOC_FAT_CHAIN;
316		}
317	}
318
319	p_chain->dir = EXFAT_EOF_CLUSTER;
320
321	while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
322	       EXFAT_EOF_CLUSTER) {
323		if (new_clu != hint_clu &&
324		    p_chain->flags == ALLOC_NO_FAT_CHAIN) {
325			if (exfat_chain_cont_cluster(sb, p_chain->dir,
326					num_clusters)) {
327				ret = -EIO;
328				goto free_cluster;
329			}
330			p_chain->flags = ALLOC_FAT_CHAIN;
331		}
332
333		/* update allocation bitmap */
334		if (exfat_set_bitmap(inode, new_clu)) {
335			ret = -EIO;
336			goto free_cluster;
337		}
338
339		num_clusters++;
340
341		/* update FAT table */
342		if (p_chain->flags == ALLOC_FAT_CHAIN) {
343			if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
344				ret = -EIO;
345				goto free_cluster;
346			}
347		}
348
349		if (p_chain->dir == EXFAT_EOF_CLUSTER) {
350			p_chain->dir = new_clu;
351		} else if (p_chain->flags == ALLOC_FAT_CHAIN) {
352			if (exfat_ent_set(sb, last_clu, new_clu)) {
353				ret = -EIO;
354				goto free_cluster;
355			}
356		}
357		last_clu = new_clu;
358
359		if (--num_alloc == 0) {
360			sbi->clu_srch_ptr = hint_clu;
361			sbi->used_clusters += num_clusters;
362
363			p_chain->size += num_clusters;
364			return 0;
365		}
366
367		hint_clu = new_clu + 1;
368		if (hint_clu >= sbi->num_clusters) {
369			hint_clu = EXFAT_FIRST_CLUSTER;
370
371			if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
372				if (exfat_chain_cont_cluster(sb, p_chain->dir,
373						num_clusters)) {
374					ret = -EIO;
375					goto free_cluster;
376				}
377				p_chain->flags = ALLOC_FAT_CHAIN;
378			}
379		}
380	}
381free_cluster:
382	if (num_clusters)
383		exfat_free_cluster(inode, p_chain);
384	return ret;
385}
386
387int exfat_count_num_clusters(struct super_block *sb,
388		struct exfat_chain *p_chain, unsigned int *ret_count)
389{
390	unsigned int i, count;
391	unsigned int clu;
392	struct exfat_sb_info *sbi = EXFAT_SB(sb);
393
394	if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
395		*ret_count = 0;
396		return 0;
397	}
398
399	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
400		*ret_count = p_chain->size;
401		return 0;
402	}
403
404	clu = p_chain->dir;
405	count = 0;
406	for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
407		count++;
408		if (exfat_ent_get(sb, clu, &clu))
409			return -EIO;
410		if (clu == EXFAT_EOF_CLUSTER)
411			break;
412	}
413
414	*ret_count = count;
415	return 0;
416}
417