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