xref: /openbmc/linux/fs/exfat/fatent.c (revision e149ca29)
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 (p_chain->dir < 2 && p_chain->dir >= sbi->num_clusters) {
173 		exfat_msg(sb, KERN_ERR, "invalid start cluster (%u)",
174 				p_chain->dir);
175 		return -EIO;
176 	}
177 
178 	set_bit(EXFAT_SB_DIRTY, &sbi->s_state);
179 	clu = p_chain->dir;
180 
181 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
182 		do {
183 			exfat_clear_bitmap(inode, clu);
184 			clu++;
185 
186 			num_clusters++;
187 		} while (num_clusters < p_chain->size);
188 	} else {
189 		do {
190 			exfat_clear_bitmap(inode, clu);
191 
192 			if (exfat_get_next_cluster(sb, &clu))
193 				goto dec_used_clus;
194 
195 			num_clusters++;
196 		} while (clu != EXFAT_EOF_CLUSTER);
197 	}
198 
199 dec_used_clus:
200 	sbi->used_clusters -= num_clusters;
201 	return 0;
202 }
203 
204 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
205 		unsigned int *ret_clu)
206 {
207 	unsigned int clu, next;
208 	unsigned int count = 0;
209 
210 	next = p_chain->dir;
211 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
212 		*ret_clu = next + p_chain->size - 1;
213 		return 0;
214 	}
215 
216 	do {
217 		count++;
218 		clu = next;
219 		if (exfat_ent_get(sb, clu, &next))
220 			return -EIO;
221 	} while (next != EXFAT_EOF_CLUSTER);
222 
223 	if (p_chain->size != count) {
224 		exfat_fs_error(sb,
225 			"bogus directory size (clus : ondisk(%d) != counted(%d))",
226 			p_chain->size, count);
227 		return -EIO;
228 	}
229 
230 	*ret_clu = clu;
231 	return 0;
232 }
233 
234 static inline int exfat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
235 {
236 	int i, err = 0;
237 
238 	for (i = 0; i < nr_bhs; i++)
239 		write_dirty_buffer(bhs[i], 0);
240 
241 	for (i = 0; i < nr_bhs; i++) {
242 		wait_on_buffer(bhs[i]);
243 		if (!err && !buffer_uptodate(bhs[i]))
244 			err = -EIO;
245 	}
246 	return err;
247 }
248 
249 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
250 {
251 	struct super_block *sb = dir->i_sb;
252 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
253 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
254 	int nr_bhs = MAX_BUF_PER_PAGE;
255 	sector_t blknr, last_blknr;
256 	int err, i, n;
257 
258 	blknr = exfat_cluster_to_sector(sbi, clu);
259 	last_blknr = blknr + sbi->sect_per_clus;
260 
261 	if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
262 		exfat_fs_error_ratelimit(sb,
263 			"%s: out of range(sect:%llu len:%u)",
264 			__func__, (unsigned long long)blknr,
265 			sbi->sect_per_clus);
266 		return -EIO;
267 	}
268 
269 	/* Zeroing the unused blocks on this cluster */
270 	n = 0;
271 	while (blknr < last_blknr) {
272 		bhs[n] = sb_getblk(sb, blknr);
273 		if (!bhs[n]) {
274 			err = -ENOMEM;
275 			goto release_bhs;
276 		}
277 		memset(bhs[n]->b_data, 0, sb->s_blocksize);
278 		exfat_update_bh(sb, bhs[n], 0);
279 
280 		n++;
281 		blknr++;
282 
283 		if (n == nr_bhs) {
284 			if (IS_DIRSYNC(dir)) {
285 				err = exfat_sync_bhs(bhs, n);
286 				if (err)
287 					goto release_bhs;
288 			}
289 
290 			for (i = 0; i < n; i++)
291 				brelse(bhs[i]);
292 			n = 0;
293 		}
294 	}
295 
296 	if (IS_DIRSYNC(dir)) {
297 		err = exfat_sync_bhs(bhs, n);
298 		if (err)
299 			goto release_bhs;
300 	}
301 
302 	for (i = 0; i < n; i++)
303 		brelse(bhs[i]);
304 
305 	return 0;
306 
307 release_bhs:
308 	exfat_msg(sb, KERN_ERR, "failed zeroed sect %llu\n",
309 		(unsigned long long)blknr);
310 	for (i = 0; i < n; i++)
311 		bforget(bhs[i]);
312 	return err;
313 }
314 
315 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
316 		struct exfat_chain *p_chain)
317 {
318 	int ret = -ENOSPC;
319 	unsigned int num_clusters = 0, total_cnt;
320 	unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
321 	struct super_block *sb = inode->i_sb;
322 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
323 
324 	total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
325 
326 	if (unlikely(total_cnt < sbi->used_clusters)) {
327 		exfat_fs_error_ratelimit(sb,
328 			"%s: invalid used clusters(t:%u,u:%u)\n",
329 			__func__, total_cnt, sbi->used_clusters);
330 		return -EIO;
331 	}
332 
333 	if (num_alloc > total_cnt - sbi->used_clusters)
334 		return -ENOSPC;
335 
336 	hint_clu = p_chain->dir;
337 	/* find new cluster */
338 	if (hint_clu == EXFAT_EOF_CLUSTER) {
339 		if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
340 			exfat_msg(sb, KERN_ERR,
341 				"sbi->clu_srch_ptr is invalid (%u)\n",
342 				sbi->clu_srch_ptr);
343 			sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
344 		}
345 
346 		hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
347 		if (hint_clu == EXFAT_EOF_CLUSTER)
348 			return -ENOSPC;
349 	}
350 
351 	/* check cluster validation */
352 	if (hint_clu < EXFAT_FIRST_CLUSTER && hint_clu >= sbi->num_clusters) {
353 		exfat_msg(sb, KERN_ERR, "hint_cluster is invalid (%u)\n",
354 			hint_clu);
355 		hint_clu = EXFAT_FIRST_CLUSTER;
356 		if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
357 			if (exfat_chain_cont_cluster(sb, p_chain->dir,
358 					num_clusters))
359 				return -EIO;
360 			p_chain->flags = ALLOC_FAT_CHAIN;
361 		}
362 	}
363 
364 	set_bit(EXFAT_SB_DIRTY, &sbi->s_state);
365 
366 	p_chain->dir = EXFAT_EOF_CLUSTER;
367 
368 	while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
369 	       EXFAT_EOF_CLUSTER) {
370 		if (new_clu != hint_clu &&
371 		    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 		/* update allocation bitmap */
381 		if (exfat_set_bitmap(inode, new_clu)) {
382 			ret = -EIO;
383 			goto free_cluster;
384 		}
385 
386 		num_clusters++;
387 
388 		/* update FAT table */
389 		if (p_chain->flags == ALLOC_FAT_CHAIN) {
390 			if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
391 				ret = -EIO;
392 				goto free_cluster;
393 			}
394 		}
395 
396 		if (p_chain->dir == EXFAT_EOF_CLUSTER) {
397 			p_chain->dir = new_clu;
398 		} else if (p_chain->flags == ALLOC_FAT_CHAIN) {
399 			if (exfat_ent_set(sb, last_clu, new_clu)) {
400 				ret = -EIO;
401 				goto free_cluster;
402 			}
403 		}
404 		last_clu = new_clu;
405 
406 		if (--num_alloc == 0) {
407 			sbi->clu_srch_ptr = hint_clu;
408 			sbi->used_clusters += num_clusters;
409 
410 			p_chain->size += num_clusters;
411 			return 0;
412 		}
413 
414 		hint_clu = new_clu + 1;
415 		if (hint_clu >= sbi->num_clusters) {
416 			hint_clu = EXFAT_FIRST_CLUSTER;
417 
418 			if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
419 				if (exfat_chain_cont_cluster(sb, p_chain->dir,
420 						num_clusters)) {
421 					ret = -EIO;
422 					goto free_cluster;
423 				}
424 				p_chain->flags = ALLOC_FAT_CHAIN;
425 			}
426 		}
427 	}
428 free_cluster:
429 	if (num_clusters)
430 		exfat_free_cluster(inode, p_chain);
431 	return ret;
432 }
433 
434 int exfat_count_num_clusters(struct super_block *sb,
435 		struct exfat_chain *p_chain, unsigned int *ret_count)
436 {
437 	unsigned int i, count;
438 	unsigned int clu;
439 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
440 
441 	if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
442 		*ret_count = 0;
443 		return 0;
444 	}
445 
446 	if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
447 		*ret_count = p_chain->size;
448 		return 0;
449 	}
450 
451 	clu = p_chain->dir;
452 	count = 0;
453 	for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
454 		count++;
455 		if (exfat_ent_get(sb, clu, &clu))
456 			return -EIO;
457 		if (clu == EXFAT_EOF_CLUSTER)
458 			break;
459 	}
460 
461 	*ret_count = count;
462 	return 0;
463 }
464