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