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