xref: /linux/fs/fat/fatent.c (revision 87c9c16317882dd6dbbc07e349bc3223e14f3244)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2004, OGAWA Hirofumi
4  */
5 
6 #include <linux/blkdev.h>
7 #include <linux/sched/signal.h>
8 #include "fat.h"
9 
10 struct fatent_operations {
11 	void (*ent_blocknr)(struct super_block *, int, int *, sector_t *);
12 	void (*ent_set_ptr)(struct fat_entry *, int);
13 	int (*ent_bread)(struct super_block *, struct fat_entry *,
14 			 int, sector_t);
15 	int (*ent_get)(struct fat_entry *);
16 	void (*ent_put)(struct fat_entry *, int);
17 	int (*ent_next)(struct fat_entry *);
18 };
19 
20 static DEFINE_SPINLOCK(fat12_entry_lock);
21 
22 static void fat12_ent_blocknr(struct super_block *sb, int entry,
23 			      int *offset, sector_t *blocknr)
24 {
25 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
26 	int bytes = entry + (entry >> 1);
27 	WARN_ON(!fat_valid_entry(sbi, entry));
28 	*offset = bytes & (sb->s_blocksize - 1);
29 	*blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits);
30 }
31 
32 static void fat_ent_blocknr(struct super_block *sb, int entry,
33 			    int *offset, sector_t *blocknr)
34 {
35 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
36 	int bytes = (entry << sbi->fatent_shift);
37 	WARN_ON(!fat_valid_entry(sbi, entry));
38 	*offset = bytes & (sb->s_blocksize - 1);
39 	*blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits);
40 }
41 
42 static void fat12_ent_set_ptr(struct fat_entry *fatent, int offset)
43 {
44 	struct buffer_head **bhs = fatent->bhs;
45 	if (fatent->nr_bhs == 1) {
46 		WARN_ON(offset >= (bhs[0]->b_size - 1));
47 		fatent->u.ent12_p[0] = bhs[0]->b_data + offset;
48 		fatent->u.ent12_p[1] = bhs[0]->b_data + (offset + 1);
49 	} else {
50 		WARN_ON(offset != (bhs[0]->b_size - 1));
51 		fatent->u.ent12_p[0] = bhs[0]->b_data + offset;
52 		fatent->u.ent12_p[1] = bhs[1]->b_data;
53 	}
54 }
55 
56 static void fat16_ent_set_ptr(struct fat_entry *fatent, int offset)
57 {
58 	WARN_ON(offset & (2 - 1));
59 	fatent->u.ent16_p = (__le16 *)(fatent->bhs[0]->b_data + offset);
60 }
61 
62 static void fat32_ent_set_ptr(struct fat_entry *fatent, int offset)
63 {
64 	WARN_ON(offset & (4 - 1));
65 	fatent->u.ent32_p = (__le32 *)(fatent->bhs[0]->b_data + offset);
66 }
67 
68 static int fat12_ent_bread(struct super_block *sb, struct fat_entry *fatent,
69 			   int offset, sector_t blocknr)
70 {
71 	struct buffer_head **bhs = fatent->bhs;
72 
73 	WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
74 	fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
75 
76 	bhs[0] = sb_bread(sb, blocknr);
77 	if (!bhs[0])
78 		goto err;
79 
80 	if ((offset + 1) < sb->s_blocksize)
81 		fatent->nr_bhs = 1;
82 	else {
83 		/* This entry is block boundary, it needs the next block */
84 		blocknr++;
85 		bhs[1] = sb_bread(sb, blocknr);
86 		if (!bhs[1])
87 			goto err_brelse;
88 		fatent->nr_bhs = 2;
89 	}
90 	fat12_ent_set_ptr(fatent, offset);
91 	return 0;
92 
93 err_brelse:
94 	brelse(bhs[0]);
95 err:
96 	fat_msg(sb, KERN_ERR, "FAT read failed (blocknr %llu)", (llu)blocknr);
97 	return -EIO;
98 }
99 
100 static int fat_ent_bread(struct super_block *sb, struct fat_entry *fatent,
101 			 int offset, sector_t blocknr)
102 {
103 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
104 
105 	WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
106 	fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
107 	fatent->bhs[0] = sb_bread(sb, blocknr);
108 	if (!fatent->bhs[0]) {
109 		fat_msg(sb, KERN_ERR, "FAT read failed (blocknr %llu)",
110 		       (llu)blocknr);
111 		return -EIO;
112 	}
113 	fatent->nr_bhs = 1;
114 	ops->ent_set_ptr(fatent, offset);
115 	return 0;
116 }
117 
118 static int fat12_ent_get(struct fat_entry *fatent)
119 {
120 	u8 **ent12_p = fatent->u.ent12_p;
121 	int next;
122 
123 	spin_lock(&fat12_entry_lock);
124 	if (fatent->entry & 1)
125 		next = (*ent12_p[0] >> 4) | (*ent12_p[1] << 4);
126 	else
127 		next = (*ent12_p[1] << 8) | *ent12_p[0];
128 	spin_unlock(&fat12_entry_lock);
129 
130 	next &= 0x0fff;
131 	if (next >= BAD_FAT12)
132 		next = FAT_ENT_EOF;
133 	return next;
134 }
135 
136 static int fat16_ent_get(struct fat_entry *fatent)
137 {
138 	int next = le16_to_cpu(*fatent->u.ent16_p);
139 	WARN_ON((unsigned long)fatent->u.ent16_p & (2 - 1));
140 	if (next >= BAD_FAT16)
141 		next = FAT_ENT_EOF;
142 	return next;
143 }
144 
145 static int fat32_ent_get(struct fat_entry *fatent)
146 {
147 	int next = le32_to_cpu(*fatent->u.ent32_p) & 0x0fffffff;
148 	WARN_ON((unsigned long)fatent->u.ent32_p & (4 - 1));
149 	if (next >= BAD_FAT32)
150 		next = FAT_ENT_EOF;
151 	return next;
152 }
153 
154 static void fat12_ent_put(struct fat_entry *fatent, int new)
155 {
156 	u8 **ent12_p = fatent->u.ent12_p;
157 
158 	if (new == FAT_ENT_EOF)
159 		new = EOF_FAT12;
160 
161 	spin_lock(&fat12_entry_lock);
162 	if (fatent->entry & 1) {
163 		*ent12_p[0] = (new << 4) | (*ent12_p[0] & 0x0f);
164 		*ent12_p[1] = new >> 4;
165 	} else {
166 		*ent12_p[0] = new & 0xff;
167 		*ent12_p[1] = (*ent12_p[1] & 0xf0) | (new >> 8);
168 	}
169 	spin_unlock(&fat12_entry_lock);
170 
171 	mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
172 	if (fatent->nr_bhs == 2)
173 		mark_buffer_dirty_inode(fatent->bhs[1], fatent->fat_inode);
174 }
175 
176 static void fat16_ent_put(struct fat_entry *fatent, int new)
177 {
178 	if (new == FAT_ENT_EOF)
179 		new = EOF_FAT16;
180 
181 	*fatent->u.ent16_p = cpu_to_le16(new);
182 	mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
183 }
184 
185 static void fat32_ent_put(struct fat_entry *fatent, int new)
186 {
187 	WARN_ON(new & 0xf0000000);
188 	new |= le32_to_cpu(*fatent->u.ent32_p) & ~0x0fffffff;
189 	*fatent->u.ent32_p = cpu_to_le32(new);
190 	mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
191 }
192 
193 static int fat12_ent_next(struct fat_entry *fatent)
194 {
195 	u8 **ent12_p = fatent->u.ent12_p;
196 	struct buffer_head **bhs = fatent->bhs;
197 	u8 *nextp = ent12_p[1] + 1 + (fatent->entry & 1);
198 
199 	fatent->entry++;
200 	if (fatent->nr_bhs == 1) {
201 		WARN_ON(ent12_p[0] > (u8 *)(bhs[0]->b_data +
202 							(bhs[0]->b_size - 2)));
203 		WARN_ON(ent12_p[1] > (u8 *)(bhs[0]->b_data +
204 							(bhs[0]->b_size - 1)));
205 		if (nextp < (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))) {
206 			ent12_p[0] = nextp - 1;
207 			ent12_p[1] = nextp;
208 			return 1;
209 		}
210 	} else {
211 		WARN_ON(ent12_p[0] != (u8 *)(bhs[0]->b_data +
212 							(bhs[0]->b_size - 1)));
213 		WARN_ON(ent12_p[1] != (u8 *)bhs[1]->b_data);
214 		ent12_p[0] = nextp - 1;
215 		ent12_p[1] = nextp;
216 		brelse(bhs[0]);
217 		bhs[0] = bhs[1];
218 		fatent->nr_bhs = 1;
219 		return 1;
220 	}
221 	ent12_p[0] = NULL;
222 	ent12_p[1] = NULL;
223 	return 0;
224 }
225 
226 static int fat16_ent_next(struct fat_entry *fatent)
227 {
228 	const struct buffer_head *bh = fatent->bhs[0];
229 	fatent->entry++;
230 	if (fatent->u.ent16_p < (__le16 *)(bh->b_data + (bh->b_size - 2))) {
231 		fatent->u.ent16_p++;
232 		return 1;
233 	}
234 	fatent->u.ent16_p = NULL;
235 	return 0;
236 }
237 
238 static int fat32_ent_next(struct fat_entry *fatent)
239 {
240 	const struct buffer_head *bh = fatent->bhs[0];
241 	fatent->entry++;
242 	if (fatent->u.ent32_p < (__le32 *)(bh->b_data + (bh->b_size - 4))) {
243 		fatent->u.ent32_p++;
244 		return 1;
245 	}
246 	fatent->u.ent32_p = NULL;
247 	return 0;
248 }
249 
250 static const struct fatent_operations fat12_ops = {
251 	.ent_blocknr	= fat12_ent_blocknr,
252 	.ent_set_ptr	= fat12_ent_set_ptr,
253 	.ent_bread	= fat12_ent_bread,
254 	.ent_get	= fat12_ent_get,
255 	.ent_put	= fat12_ent_put,
256 	.ent_next	= fat12_ent_next,
257 };
258 
259 static const struct fatent_operations fat16_ops = {
260 	.ent_blocknr	= fat_ent_blocknr,
261 	.ent_set_ptr	= fat16_ent_set_ptr,
262 	.ent_bread	= fat_ent_bread,
263 	.ent_get	= fat16_ent_get,
264 	.ent_put	= fat16_ent_put,
265 	.ent_next	= fat16_ent_next,
266 };
267 
268 static const struct fatent_operations fat32_ops = {
269 	.ent_blocknr	= fat_ent_blocknr,
270 	.ent_set_ptr	= fat32_ent_set_ptr,
271 	.ent_bread	= fat_ent_bread,
272 	.ent_get	= fat32_ent_get,
273 	.ent_put	= fat32_ent_put,
274 	.ent_next	= fat32_ent_next,
275 };
276 
277 static inline void lock_fat(struct msdos_sb_info *sbi)
278 {
279 	mutex_lock(&sbi->fat_lock);
280 }
281 
282 static inline void unlock_fat(struct msdos_sb_info *sbi)
283 {
284 	mutex_unlock(&sbi->fat_lock);
285 }
286 
287 void fat_ent_access_init(struct super_block *sb)
288 {
289 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
290 
291 	mutex_init(&sbi->fat_lock);
292 
293 	if (is_fat32(sbi)) {
294 		sbi->fatent_shift = 2;
295 		sbi->fatent_ops = &fat32_ops;
296 	} else if (is_fat16(sbi)) {
297 		sbi->fatent_shift = 1;
298 		sbi->fatent_ops = &fat16_ops;
299 	} else if (is_fat12(sbi)) {
300 		sbi->fatent_shift = -1;
301 		sbi->fatent_ops = &fat12_ops;
302 	} else {
303 		fat_fs_error(sb, "invalid FAT variant, %u bits", sbi->fat_bits);
304 	}
305 }
306 
307 static void mark_fsinfo_dirty(struct super_block *sb)
308 {
309 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
310 
311 	if (sb_rdonly(sb) || !is_fat32(sbi))
312 		return;
313 
314 	__mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC);
315 }
316 
317 static inline int fat_ent_update_ptr(struct super_block *sb,
318 				     struct fat_entry *fatent,
319 				     int offset, sector_t blocknr)
320 {
321 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
322 	const struct fatent_operations *ops = sbi->fatent_ops;
323 	struct buffer_head **bhs = fatent->bhs;
324 
325 	/* Is this fatent's blocks including this entry? */
326 	if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
327 		return 0;
328 	if (is_fat12(sbi)) {
329 		if ((offset + 1) < sb->s_blocksize) {
330 			/* This entry is on bhs[0]. */
331 			if (fatent->nr_bhs == 2) {
332 				brelse(bhs[1]);
333 				fatent->nr_bhs = 1;
334 			}
335 		} else {
336 			/* This entry needs the next block. */
337 			if (fatent->nr_bhs != 2)
338 				return 0;
339 			if (bhs[1]->b_blocknr != (blocknr + 1))
340 				return 0;
341 		}
342 	}
343 	ops->ent_set_ptr(fatent, offset);
344 	return 1;
345 }
346 
347 int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry)
348 {
349 	struct super_block *sb = inode->i_sb;
350 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
351 	const struct fatent_operations *ops = sbi->fatent_ops;
352 	int err, offset;
353 	sector_t blocknr;
354 
355 	if (!fat_valid_entry(sbi, entry)) {
356 		fatent_brelse(fatent);
357 		fat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", entry);
358 		return -EIO;
359 	}
360 
361 	fatent_set_entry(fatent, entry);
362 	ops->ent_blocknr(sb, entry, &offset, &blocknr);
363 
364 	if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) {
365 		fatent_brelse(fatent);
366 		err = ops->ent_bread(sb, fatent, offset, blocknr);
367 		if (err)
368 			return err;
369 	}
370 	return ops->ent_get(fatent);
371 }
372 
373 /* FIXME: We can write the blocks as more big chunk. */
374 static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs,
375 			  int nr_bhs)
376 {
377 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
378 	struct buffer_head *c_bh;
379 	int err, n, copy;
380 
381 	err = 0;
382 	for (copy = 1; copy < sbi->fats; copy++) {
383 		sector_t backup_fat = sbi->fat_length * copy;
384 
385 		for (n = 0; n < nr_bhs; n++) {
386 			c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr);
387 			if (!c_bh) {
388 				err = -ENOMEM;
389 				goto error;
390 			}
391 			/* Avoid race with userspace read via bdev */
392 			lock_buffer(c_bh);
393 			memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize);
394 			set_buffer_uptodate(c_bh);
395 			unlock_buffer(c_bh);
396 			mark_buffer_dirty_inode(c_bh, sbi->fat_inode);
397 			if (sb->s_flags & SB_SYNCHRONOUS)
398 				err = sync_dirty_buffer(c_bh);
399 			brelse(c_bh);
400 			if (err)
401 				goto error;
402 		}
403 	}
404 error:
405 	return err;
406 }
407 
408 int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
409 		  int new, int wait)
410 {
411 	struct super_block *sb = inode->i_sb;
412 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
413 	int err;
414 
415 	ops->ent_put(fatent, new);
416 	if (wait) {
417 		err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
418 		if (err)
419 			return err;
420 	}
421 	return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
422 }
423 
424 static inline int fat_ent_next(struct msdos_sb_info *sbi,
425 			       struct fat_entry *fatent)
426 {
427 	if (sbi->fatent_ops->ent_next(fatent)) {
428 		if (fatent->entry < sbi->max_cluster)
429 			return 1;
430 	}
431 	return 0;
432 }
433 
434 static inline int fat_ent_read_block(struct super_block *sb,
435 				     struct fat_entry *fatent)
436 {
437 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
438 	sector_t blocknr;
439 	int offset;
440 
441 	fatent_brelse(fatent);
442 	ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
443 	return ops->ent_bread(sb, fatent, offset, blocknr);
444 }
445 
446 static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
447 			    struct fat_entry *fatent)
448 {
449 	int n, i;
450 
451 	for (n = 0; n < fatent->nr_bhs; n++) {
452 		for (i = 0; i < *nr_bhs; i++) {
453 			if (fatent->bhs[n] == bhs[i])
454 				break;
455 		}
456 		if (i == *nr_bhs) {
457 			get_bh(fatent->bhs[n]);
458 			bhs[i] = fatent->bhs[n];
459 			(*nr_bhs)++;
460 		}
461 	}
462 }
463 
464 int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
465 {
466 	struct super_block *sb = inode->i_sb;
467 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
468 	const struct fatent_operations *ops = sbi->fatent_ops;
469 	struct fat_entry fatent, prev_ent;
470 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
471 	int i, count, err, nr_bhs, idx_clus;
472 
473 	BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2));	/* fixed limit */
474 
475 	lock_fat(sbi);
476 	if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
477 	    sbi->free_clusters < nr_cluster) {
478 		unlock_fat(sbi);
479 		return -ENOSPC;
480 	}
481 
482 	err = nr_bhs = idx_clus = 0;
483 	count = FAT_START_ENT;
484 	fatent_init(&prev_ent);
485 	fatent_init(&fatent);
486 	fatent_set_entry(&fatent, sbi->prev_free + 1);
487 	while (count < sbi->max_cluster) {
488 		if (fatent.entry >= sbi->max_cluster)
489 			fatent.entry = FAT_START_ENT;
490 		fatent_set_entry(&fatent, fatent.entry);
491 		err = fat_ent_read_block(sb, &fatent);
492 		if (err)
493 			goto out;
494 
495 		/* Find the free entries in a block */
496 		do {
497 			if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
498 				int entry = fatent.entry;
499 
500 				/* make the cluster chain */
501 				ops->ent_put(&fatent, FAT_ENT_EOF);
502 				if (prev_ent.nr_bhs)
503 					ops->ent_put(&prev_ent, entry);
504 
505 				fat_collect_bhs(bhs, &nr_bhs, &fatent);
506 
507 				sbi->prev_free = entry;
508 				if (sbi->free_clusters != -1)
509 					sbi->free_clusters--;
510 
511 				cluster[idx_clus] = entry;
512 				idx_clus++;
513 				if (idx_clus == nr_cluster)
514 					goto out;
515 
516 				/*
517 				 * fat_collect_bhs() gets ref-count of bhs,
518 				 * so we can still use the prev_ent.
519 				 */
520 				prev_ent = fatent;
521 			}
522 			count++;
523 			if (count == sbi->max_cluster)
524 				break;
525 		} while (fat_ent_next(sbi, &fatent));
526 	}
527 
528 	/* Couldn't allocate the free entries */
529 	sbi->free_clusters = 0;
530 	sbi->free_clus_valid = 1;
531 	err = -ENOSPC;
532 
533 out:
534 	unlock_fat(sbi);
535 	mark_fsinfo_dirty(sb);
536 	fatent_brelse(&fatent);
537 	if (!err) {
538 		if (inode_needs_sync(inode))
539 			err = fat_sync_bhs(bhs, nr_bhs);
540 		if (!err)
541 			err = fat_mirror_bhs(sb, bhs, nr_bhs);
542 	}
543 	for (i = 0; i < nr_bhs; i++)
544 		brelse(bhs[i]);
545 
546 	if (err && idx_clus)
547 		fat_free_clusters(inode, cluster[0]);
548 
549 	return err;
550 }
551 
552 int fat_free_clusters(struct inode *inode, int cluster)
553 {
554 	struct super_block *sb = inode->i_sb;
555 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
556 	const struct fatent_operations *ops = sbi->fatent_ops;
557 	struct fat_entry fatent;
558 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
559 	int i, err, nr_bhs;
560 	int first_cl = cluster, dirty_fsinfo = 0;
561 
562 	nr_bhs = 0;
563 	fatent_init(&fatent);
564 	lock_fat(sbi);
565 	do {
566 		cluster = fat_ent_read(inode, &fatent, cluster);
567 		if (cluster < 0) {
568 			err = cluster;
569 			goto error;
570 		} else if (cluster == FAT_ENT_FREE) {
571 			fat_fs_error(sb, "%s: deleting FAT entry beyond EOF",
572 				     __func__);
573 			err = -EIO;
574 			goto error;
575 		}
576 
577 		if (sbi->options.discard) {
578 			/*
579 			 * Issue discard for the sectors we no longer
580 			 * care about, batching contiguous clusters
581 			 * into one request
582 			 */
583 			if (cluster != fatent.entry + 1) {
584 				int nr_clus = fatent.entry - first_cl + 1;
585 
586 				sb_issue_discard(sb,
587 					fat_clus_to_blknr(sbi, first_cl),
588 					nr_clus * sbi->sec_per_clus,
589 					GFP_NOFS, 0);
590 
591 				first_cl = cluster;
592 			}
593 		}
594 
595 		ops->ent_put(&fatent, FAT_ENT_FREE);
596 		if (sbi->free_clusters != -1) {
597 			sbi->free_clusters++;
598 			dirty_fsinfo = 1;
599 		}
600 
601 		if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
602 			if (sb->s_flags & SB_SYNCHRONOUS) {
603 				err = fat_sync_bhs(bhs, nr_bhs);
604 				if (err)
605 					goto error;
606 			}
607 			err = fat_mirror_bhs(sb, bhs, nr_bhs);
608 			if (err)
609 				goto error;
610 			for (i = 0; i < nr_bhs; i++)
611 				brelse(bhs[i]);
612 			nr_bhs = 0;
613 		}
614 		fat_collect_bhs(bhs, &nr_bhs, &fatent);
615 	} while (cluster != FAT_ENT_EOF);
616 
617 	if (sb->s_flags & SB_SYNCHRONOUS) {
618 		err = fat_sync_bhs(bhs, nr_bhs);
619 		if (err)
620 			goto error;
621 	}
622 	err = fat_mirror_bhs(sb, bhs, nr_bhs);
623 error:
624 	fatent_brelse(&fatent);
625 	for (i = 0; i < nr_bhs; i++)
626 		brelse(bhs[i]);
627 	unlock_fat(sbi);
628 	if (dirty_fsinfo)
629 		mark_fsinfo_dirty(sb);
630 
631 	return err;
632 }
633 EXPORT_SYMBOL_GPL(fat_free_clusters);
634 
635 struct fatent_ra {
636 	sector_t cur;
637 	sector_t limit;
638 
639 	unsigned int ra_blocks;
640 	sector_t ra_advance;
641 	sector_t ra_next;
642 	sector_t ra_limit;
643 };
644 
645 static void fat_ra_init(struct super_block *sb, struct fatent_ra *ra,
646 			struct fat_entry *fatent, int ent_limit)
647 {
648 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
649 	const struct fatent_operations *ops = sbi->fatent_ops;
650 	sector_t blocknr, block_end;
651 	int offset;
652 	/*
653 	 * This is the sequential read, so ra_pages * 2 (but try to
654 	 * align the optimal hardware IO size).
655 	 * [BTW, 128kb covers the whole sectors for FAT12 and FAT16]
656 	 */
657 	unsigned long ra_pages = sb->s_bdi->ra_pages;
658 	unsigned int reada_blocks;
659 
660 	if (fatent->entry >= ent_limit)
661 		return;
662 
663 	if (ra_pages > sb->s_bdi->io_pages)
664 		ra_pages = rounddown(ra_pages, sb->s_bdi->io_pages);
665 	reada_blocks = ra_pages << (PAGE_SHIFT - sb->s_blocksize_bits + 1);
666 
667 	/* Initialize the range for sequential read */
668 	ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
669 	ops->ent_blocknr(sb, ent_limit - 1, &offset, &block_end);
670 	ra->cur = 0;
671 	ra->limit = (block_end + 1) - blocknr;
672 
673 	/* Advancing the window at half size */
674 	ra->ra_blocks = reada_blocks >> 1;
675 	ra->ra_advance = ra->cur;
676 	ra->ra_next = ra->cur;
677 	ra->ra_limit = ra->cur + min_t(sector_t, reada_blocks, ra->limit);
678 }
679 
680 /* Assuming to be called before reading a new block (increments ->cur). */
681 static void fat_ent_reada(struct super_block *sb, struct fatent_ra *ra,
682 			  struct fat_entry *fatent)
683 {
684 	if (ra->ra_next >= ra->ra_limit)
685 		return;
686 
687 	if (ra->cur >= ra->ra_advance) {
688 		struct msdos_sb_info *sbi = MSDOS_SB(sb);
689 		const struct fatent_operations *ops = sbi->fatent_ops;
690 		struct blk_plug plug;
691 		sector_t blocknr, diff;
692 		int offset;
693 
694 		ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
695 
696 		diff = blocknr - ra->cur;
697 		blk_start_plug(&plug);
698 		/*
699 		 * FIXME: we would want to directly use the bio with
700 		 * pages to reduce the number of segments.
701 		 */
702 		for (; ra->ra_next < ra->ra_limit; ra->ra_next++)
703 			sb_breadahead(sb, ra->ra_next + diff);
704 		blk_finish_plug(&plug);
705 
706 		/* Advance the readahead window */
707 		ra->ra_advance += ra->ra_blocks;
708 		ra->ra_limit += min_t(sector_t,
709 				      ra->ra_blocks, ra->limit - ra->ra_limit);
710 	}
711 	ra->cur++;
712 }
713 
714 int fat_count_free_clusters(struct super_block *sb)
715 {
716 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
717 	const struct fatent_operations *ops = sbi->fatent_ops;
718 	struct fat_entry fatent;
719 	struct fatent_ra fatent_ra;
720 	int err = 0, free;
721 
722 	lock_fat(sbi);
723 	if (sbi->free_clusters != -1 && sbi->free_clus_valid)
724 		goto out;
725 
726 	free = 0;
727 	fatent_init(&fatent);
728 	fatent_set_entry(&fatent, FAT_START_ENT);
729 	fat_ra_init(sb, &fatent_ra, &fatent, sbi->max_cluster);
730 	while (fatent.entry < sbi->max_cluster) {
731 		/* readahead of fat blocks */
732 		fat_ent_reada(sb, &fatent_ra, &fatent);
733 
734 		err = fat_ent_read_block(sb, &fatent);
735 		if (err)
736 			goto out;
737 
738 		do {
739 			if (ops->ent_get(&fatent) == FAT_ENT_FREE)
740 				free++;
741 		} while (fat_ent_next(sbi, &fatent));
742 		cond_resched();
743 	}
744 	sbi->free_clusters = free;
745 	sbi->free_clus_valid = 1;
746 	mark_fsinfo_dirty(sb);
747 	fatent_brelse(&fatent);
748 out:
749 	unlock_fat(sbi);
750 	return err;
751 }
752 
753 static int fat_trim_clusters(struct super_block *sb, u32 clus, u32 nr_clus)
754 {
755 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
756 	return sb_issue_discard(sb, fat_clus_to_blknr(sbi, clus),
757 				nr_clus * sbi->sec_per_clus, GFP_NOFS, 0);
758 }
759 
760 int fat_trim_fs(struct inode *inode, struct fstrim_range *range)
761 {
762 	struct super_block *sb = inode->i_sb;
763 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
764 	const struct fatent_operations *ops = sbi->fatent_ops;
765 	struct fat_entry fatent;
766 	struct fatent_ra fatent_ra;
767 	u64 ent_start, ent_end, minlen, trimmed = 0;
768 	u32 free = 0;
769 	int err = 0;
770 
771 	/*
772 	 * FAT data is organized as clusters, trim at the granulary of cluster.
773 	 *
774 	 * fstrim_range is in byte, convert values to cluster index.
775 	 * Treat sectors before data region as all used, not to trim them.
776 	 */
777 	ent_start = max_t(u64, range->start>>sbi->cluster_bits, FAT_START_ENT);
778 	ent_end = ent_start + (range->len >> sbi->cluster_bits) - 1;
779 	minlen = range->minlen >> sbi->cluster_bits;
780 
781 	if (ent_start >= sbi->max_cluster || range->len < sbi->cluster_size)
782 		return -EINVAL;
783 	if (ent_end >= sbi->max_cluster)
784 		ent_end = sbi->max_cluster - 1;
785 
786 	fatent_init(&fatent);
787 	lock_fat(sbi);
788 	fatent_set_entry(&fatent, ent_start);
789 	fat_ra_init(sb, &fatent_ra, &fatent, ent_end + 1);
790 	while (fatent.entry <= ent_end) {
791 		/* readahead of fat blocks */
792 		fat_ent_reada(sb, &fatent_ra, &fatent);
793 
794 		err = fat_ent_read_block(sb, &fatent);
795 		if (err)
796 			goto error;
797 		do {
798 			if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
799 				free++;
800 			} else if (free) {
801 				if (free >= minlen) {
802 					u32 clus = fatent.entry - free;
803 
804 					err = fat_trim_clusters(sb, clus, free);
805 					if (err && err != -EOPNOTSUPP)
806 						goto error;
807 					if (!err)
808 						trimmed += free;
809 					err = 0;
810 				}
811 				free = 0;
812 			}
813 		} while (fat_ent_next(sbi, &fatent) && fatent.entry <= ent_end);
814 
815 		if (fatal_signal_pending(current)) {
816 			err = -ERESTARTSYS;
817 			goto error;
818 		}
819 
820 		if (need_resched()) {
821 			fatent_brelse(&fatent);
822 			unlock_fat(sbi);
823 			cond_resched();
824 			lock_fat(sbi);
825 		}
826 	}
827 	/* handle scenario when tail entries are all free */
828 	if (free && free >= minlen) {
829 		u32 clus = fatent.entry - free;
830 
831 		err = fat_trim_clusters(sb, clus, free);
832 		if (err && err != -EOPNOTSUPP)
833 			goto error;
834 		if (!err)
835 			trimmed += free;
836 		err = 0;
837 	}
838 
839 error:
840 	fatent_brelse(&fatent);
841 	unlock_fat(sbi);
842 
843 	range->len = trimmed << sbi->cluster_bits;
844 
845 	return err;
846 }
847