1 /*
2  * Copyright (C) 2004, OGAWA Hirofumi
3  * Released under GPL v2.
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(sb, KERN_ERR, "FAT read failed (blocknr %llu)", (llu)blocknr);
97 	return -EIO;
98 }
99 
fat_ent_bread(struct super_block * sb,struct fat_entry * fatent,int offset,sector_t blocknr)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 
fat12_ent_get(struct fat_entry * fatent)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 
fat16_ent_get(struct fat_entry * fatent)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 
fat32_ent_get(struct fat_entry * fatent)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 
fat12_ent_put(struct fat_entry * fatent,int new)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 
fat16_ent_put(struct fat_entry * fatent,int new)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 
fat32_ent_put(struct fat_entry * fatent,int new)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 
fat12_ent_next(struct fat_entry * fatent)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 
fat16_ent_next(struct fat_entry * fatent)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 
fat32_ent_next(struct fat_entry * fatent)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 
lock_fat(struct msdos_sb_info * sbi)277 static inline void lock_fat(struct msdos_sb_info *sbi)
278 {
279 	mutex_lock(&sbi->fat_lock);
280 }
281 
unlock_fat(struct msdos_sb_info * sbi)282 static inline void unlock_fat(struct msdos_sb_info *sbi)
283 {
284 	mutex_unlock(&sbi->fat_lock);
285 }
286 
fat_ent_access_init(struct super_block * sb)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 	switch (sbi->fat_bits) {
294 	case 32:
295 		sbi->fatent_shift = 2;
296 		sbi->fatent_ops = &fat32_ops;
297 		break;
298 	case 16:
299 		sbi->fatent_shift = 1;
300 		sbi->fatent_ops = &fat16_ops;
301 		break;
302 	case 12:
303 		sbi->fatent_shift = -1;
304 		sbi->fatent_ops = &fat12_ops;
305 		break;
306 	}
307 }
308 
mark_fsinfo_dirty(struct super_block * sb)309 static void mark_fsinfo_dirty(struct super_block *sb)
310 {
311 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
312 
313 	if (sb_rdonly(sb) || sbi->fat_bits != 32)
314 		return;
315 
316 	__mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC);
317 }
318 
fat_ent_update_ptr(struct super_block * sb,struct fat_entry * fatent,int offset,sector_t blocknr)319 static inline int fat_ent_update_ptr(struct super_block *sb,
320 				     struct fat_entry *fatent,
321 				     int offset, sector_t blocknr)
322 {
323 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
324 	const struct fatent_operations *ops = sbi->fatent_ops;
325 	struct buffer_head **bhs = fatent->bhs;
326 
327 	/* Is this fatent's blocks including this entry? */
328 	if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
329 		return 0;
330 	if (sbi->fat_bits == 12) {
331 		if ((offset + 1) < sb->s_blocksize) {
332 			/* This entry is on bhs[0]. */
333 			if (fatent->nr_bhs == 2) {
334 				brelse(bhs[1]);
335 				fatent->nr_bhs = 1;
336 			}
337 		} else {
338 			/* This entry needs the next block. */
339 			if (fatent->nr_bhs != 2)
340 				return 0;
341 			if (bhs[1]->b_blocknr != (blocknr + 1))
342 				return 0;
343 		}
344 	}
345 	ops->ent_set_ptr(fatent, offset);
346 	return 1;
347 }
348 
fat_ent_read(struct inode * inode,struct fat_entry * fatent,int entry)349 int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry)
350 {
351 	struct super_block *sb = inode->i_sb;
352 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
353 	const struct fatent_operations *ops = sbi->fatent_ops;
354 	int err, offset;
355 	sector_t blocknr;
356 
357 	if (!fat_valid_entry(sbi, entry)) {
358 		fatent_brelse(fatent);
359 		fat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", entry);
360 		return -EIO;
361 	}
362 
363 	fatent_set_entry(fatent, entry);
364 	ops->ent_blocknr(sb, entry, &offset, &blocknr);
365 
366 	if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) {
367 		fatent_brelse(fatent);
368 		err = ops->ent_bread(sb, fatent, offset, blocknr);
369 		if (err)
370 			return err;
371 	}
372 	return ops->ent_get(fatent);
373 }
374 
375 /* FIXME: We can write the blocks as more big chunk. */
fat_mirror_bhs(struct super_block * sb,struct buffer_head ** bhs,int nr_bhs)376 static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs,
377 			  int nr_bhs)
378 {
379 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
380 	struct buffer_head *c_bh;
381 	int err, n, copy;
382 
383 	err = 0;
384 	for (copy = 1; copy < sbi->fats; copy++) {
385 		sector_t backup_fat = sbi->fat_length * copy;
386 
387 		for (n = 0; n < nr_bhs; n++) {
388 			c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr);
389 			if (!c_bh) {
390 				err = -ENOMEM;
391 				goto error;
392 			}
393 			memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize);
394 			set_buffer_uptodate(c_bh);
395 			mark_buffer_dirty_inode(c_bh, sbi->fat_inode);
396 			if (sb->s_flags & SB_SYNCHRONOUS)
397 				err = sync_dirty_buffer(c_bh);
398 			brelse(c_bh);
399 			if (err)
400 				goto error;
401 		}
402 	}
403 error:
404 	return err;
405 }
406 
fat_ent_write(struct inode * inode,struct fat_entry * fatent,int new,int wait)407 int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
408 		  int new, int wait)
409 {
410 	struct super_block *sb = inode->i_sb;
411 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
412 	int err;
413 
414 	ops->ent_put(fatent, new);
415 	if (wait) {
416 		err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
417 		if (err)
418 			return err;
419 	}
420 	return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
421 }
422 
fat_ent_next(struct msdos_sb_info * sbi,struct fat_entry * fatent)423 static inline int fat_ent_next(struct msdos_sb_info *sbi,
424 			       struct fat_entry *fatent)
425 {
426 	if (sbi->fatent_ops->ent_next(fatent)) {
427 		if (fatent->entry < sbi->max_cluster)
428 			return 1;
429 	}
430 	return 0;
431 }
432 
fat_ent_read_block(struct super_block * sb,struct fat_entry * fatent)433 static inline int fat_ent_read_block(struct super_block *sb,
434 				     struct fat_entry *fatent)
435 {
436 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
437 	sector_t blocknr;
438 	int offset;
439 
440 	fatent_brelse(fatent);
441 	ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
442 	return ops->ent_bread(sb, fatent, offset, blocknr);
443 }
444 
fat_collect_bhs(struct buffer_head ** bhs,int * nr_bhs,struct fat_entry * fatent)445 static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
446 			    struct fat_entry *fatent)
447 {
448 	int n, i;
449 
450 	for (n = 0; n < fatent->nr_bhs; n++) {
451 		for (i = 0; i < *nr_bhs; i++) {
452 			if (fatent->bhs[n] == bhs[i])
453 				break;
454 		}
455 		if (i == *nr_bhs) {
456 			get_bh(fatent->bhs[n]);
457 			bhs[i] = fatent->bhs[n];
458 			(*nr_bhs)++;
459 		}
460 	}
461 }
462 
fat_alloc_clusters(struct inode * inode,int * cluster,int nr_cluster)463 int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
464 {
465 	struct super_block *sb = inode->i_sb;
466 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
467 	const struct fatent_operations *ops = sbi->fatent_ops;
468 	struct fat_entry fatent, prev_ent;
469 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
470 	int i, count, err, nr_bhs, idx_clus;
471 
472 	BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2));	/* fixed limit */
473 
474 	lock_fat(sbi);
475 	if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
476 	    sbi->free_clusters < nr_cluster) {
477 		unlock_fat(sbi);
478 		return -ENOSPC;
479 	}
480 
481 	err = nr_bhs = idx_clus = 0;
482 	count = FAT_START_ENT;
483 	fatent_init(&prev_ent);
484 	fatent_init(&fatent);
485 	fatent_set_entry(&fatent, sbi->prev_free + 1);
486 	while (count < sbi->max_cluster) {
487 		if (fatent.entry >= sbi->max_cluster)
488 			fatent.entry = FAT_START_ENT;
489 		fatent_set_entry(&fatent, fatent.entry);
490 		err = fat_ent_read_block(sb, &fatent);
491 		if (err)
492 			goto out;
493 
494 		/* Find the free entries in a block */
495 		do {
496 			if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
497 				int entry = fatent.entry;
498 
499 				/* make the cluster chain */
500 				ops->ent_put(&fatent, FAT_ENT_EOF);
501 				if (prev_ent.nr_bhs)
502 					ops->ent_put(&prev_ent, entry);
503 
504 				fat_collect_bhs(bhs, &nr_bhs, &fatent);
505 
506 				sbi->prev_free = entry;
507 				if (sbi->free_clusters != -1)
508 					sbi->free_clusters--;
509 
510 				cluster[idx_clus] = entry;
511 				idx_clus++;
512 				if (idx_clus == nr_cluster)
513 					goto out;
514 
515 				/*
516 				 * fat_collect_bhs() gets ref-count of bhs,
517 				 * so we can still use the prev_ent.
518 				 */
519 				prev_ent = fatent;
520 			}
521 			count++;
522 			if (count == sbi->max_cluster)
523 				break;
524 		} while (fat_ent_next(sbi, &fatent));
525 	}
526 
527 	/* Couldn't allocate the free entries */
528 	sbi->free_clusters = 0;
529 	sbi->free_clus_valid = 1;
530 	err = -ENOSPC;
531 
532 out:
533 	unlock_fat(sbi);
534 	mark_fsinfo_dirty(sb);
535 	fatent_brelse(&fatent);
536 	if (!err) {
537 		if (inode_needs_sync(inode))
538 			err = fat_sync_bhs(bhs, nr_bhs);
539 		if (!err)
540 			err = fat_mirror_bhs(sb, bhs, nr_bhs);
541 	}
542 	for (i = 0; i < nr_bhs; i++)
543 		brelse(bhs[i]);
544 
545 	if (err && idx_clus)
546 		fat_free_clusters(inode, cluster[0]);
547 
548 	return err;
549 }
550 
fat_free_clusters(struct inode * inode,int cluster)551 int fat_free_clusters(struct inode *inode, int cluster)
552 {
553 	struct super_block *sb = inode->i_sb;
554 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
555 	const struct fatent_operations *ops = sbi->fatent_ops;
556 	struct fat_entry fatent;
557 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
558 	int i, err, nr_bhs;
559 	int first_cl = cluster, dirty_fsinfo = 0;
560 
561 	nr_bhs = 0;
562 	fatent_init(&fatent);
563 	lock_fat(sbi);
564 	do {
565 		cluster = fat_ent_read(inode, &fatent, cluster);
566 		if (cluster < 0) {
567 			err = cluster;
568 			goto error;
569 		} else if (cluster == FAT_ENT_FREE) {
570 			fat_fs_error(sb, "%s: deleting FAT entry beyond EOF",
571 				     __func__);
572 			err = -EIO;
573 			goto error;
574 		}
575 
576 		if (sbi->options.discard) {
577 			/*
578 			 * Issue discard for the sectors we no longer
579 			 * care about, batching contiguous clusters
580 			 * into one request
581 			 */
582 			if (cluster != fatent.entry + 1) {
583 				int nr_clus = fatent.entry - first_cl + 1;
584 
585 				sb_issue_discard(sb,
586 					fat_clus_to_blknr(sbi, first_cl),
587 					nr_clus * sbi->sec_per_clus,
588 					GFP_NOFS, 0);
589 
590 				first_cl = cluster;
591 			}
592 		}
593 
594 		ops->ent_put(&fatent, FAT_ENT_FREE);
595 		if (sbi->free_clusters != -1) {
596 			sbi->free_clusters++;
597 			dirty_fsinfo = 1;
598 		}
599 
600 		if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
601 			if (sb->s_flags & SB_SYNCHRONOUS) {
602 				err = fat_sync_bhs(bhs, nr_bhs);
603 				if (err)
604 					goto error;
605 			}
606 			err = fat_mirror_bhs(sb, bhs, nr_bhs);
607 			if (err)
608 				goto error;
609 			for (i = 0; i < nr_bhs; i++)
610 				brelse(bhs[i]);
611 			nr_bhs = 0;
612 		}
613 		fat_collect_bhs(bhs, &nr_bhs, &fatent);
614 	} while (cluster != FAT_ENT_EOF);
615 
616 	if (sb->s_flags & SB_SYNCHRONOUS) {
617 		err = fat_sync_bhs(bhs, nr_bhs);
618 		if (err)
619 			goto error;
620 	}
621 	err = fat_mirror_bhs(sb, bhs, nr_bhs);
622 error:
623 	fatent_brelse(&fatent);
624 	for (i = 0; i < nr_bhs; i++)
625 		brelse(bhs[i]);
626 	unlock_fat(sbi);
627 	if (dirty_fsinfo)
628 		mark_fsinfo_dirty(sb);
629 
630 	return err;
631 }
632 EXPORT_SYMBOL_GPL(fat_free_clusters);
633 
634 /* 128kb is the whole sectors for FAT12 and FAT16 */
635 #define FAT_READA_SIZE		(128 * 1024)
636 
fat_ent_reada(struct super_block * sb,struct fat_entry * fatent,unsigned long reada_blocks)637 static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent,
638 			  unsigned long reada_blocks)
639 {
640 	const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
641 	sector_t blocknr;
642 	int i, offset;
643 
644 	ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
645 
646 	for (i = 0; i < reada_blocks; i++)
647 		sb_breadahead(sb, blocknr + i);
648 }
649 
fat_count_free_clusters(struct super_block * sb)650 int fat_count_free_clusters(struct super_block *sb)
651 {
652 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
653 	const struct fatent_operations *ops = sbi->fatent_ops;
654 	struct fat_entry fatent;
655 	unsigned long reada_blocks, reada_mask, cur_block;
656 	int err = 0, free;
657 
658 	lock_fat(sbi);
659 	if (sbi->free_clusters != -1 && sbi->free_clus_valid)
660 		goto out;
661 
662 	reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
663 	reada_mask = reada_blocks - 1;
664 	cur_block = 0;
665 
666 	free = 0;
667 	fatent_init(&fatent);
668 	fatent_set_entry(&fatent, FAT_START_ENT);
669 	while (fatent.entry < sbi->max_cluster) {
670 		/* readahead of fat blocks */
671 		if ((cur_block & reada_mask) == 0) {
672 			unsigned long rest = sbi->fat_length - cur_block;
673 			fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
674 		}
675 		cur_block++;
676 
677 		err = fat_ent_read_block(sb, &fatent);
678 		if (err)
679 			goto out;
680 
681 		do {
682 			if (ops->ent_get(&fatent) == FAT_ENT_FREE)
683 				free++;
684 		} while (fat_ent_next(sbi, &fatent));
685 		cond_resched();
686 	}
687 	sbi->free_clusters = free;
688 	sbi->free_clus_valid = 1;
689 	mark_fsinfo_dirty(sb);
690 	fatent_brelse(&fatent);
691 out:
692 	unlock_fat(sbi);
693 	return err;
694 }
695 
fat_trim_clusters(struct super_block * sb,u32 clus,u32 nr_clus)696 static int fat_trim_clusters(struct super_block *sb, u32 clus, u32 nr_clus)
697 {
698 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
699 	return sb_issue_discard(sb, fat_clus_to_blknr(sbi, clus),
700 				nr_clus * sbi->sec_per_clus, GFP_NOFS, 0);
701 }
702 
fat_trim_fs(struct inode * inode,struct fstrim_range * range)703 int fat_trim_fs(struct inode *inode, struct fstrim_range *range)
704 {
705 	struct super_block *sb = inode->i_sb;
706 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
707 	const struct fatent_operations *ops = sbi->fatent_ops;
708 	struct fat_entry fatent;
709 	u64 ent_start, ent_end, minlen, trimmed = 0;
710 	u32 free = 0;
711 	unsigned long reada_blocks, reada_mask, cur_block = 0;
712 	int err = 0;
713 
714 	/*
715 	 * FAT data is organized as clusters, trim at the granulary of cluster.
716 	 *
717 	 * fstrim_range is in byte, convert vaules to cluster index.
718 	 * Treat sectors before data region as all used, not to trim them.
719 	 */
720 	ent_start = max_t(u64, range->start>>sbi->cluster_bits, FAT_START_ENT);
721 	ent_end = ent_start + (range->len >> sbi->cluster_bits) - 1;
722 	minlen = range->minlen >> sbi->cluster_bits;
723 
724 	if (ent_start >= sbi->max_cluster || range->len < sbi->cluster_size)
725 		return -EINVAL;
726 	if (ent_end >= sbi->max_cluster)
727 		ent_end = sbi->max_cluster - 1;
728 
729 	reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
730 	reada_mask = reada_blocks - 1;
731 
732 	fatent_init(&fatent);
733 	lock_fat(sbi);
734 	fatent_set_entry(&fatent, ent_start);
735 	while (fatent.entry <= ent_end) {
736 		/* readahead of fat blocks */
737 		if ((cur_block & reada_mask) == 0) {
738 			unsigned long rest = sbi->fat_length - cur_block;
739 			fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
740 		}
741 		cur_block++;
742 
743 		err = fat_ent_read_block(sb, &fatent);
744 		if (err)
745 			goto error;
746 		do {
747 			if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
748 				free++;
749 			} else if (free) {
750 				if (free >= minlen) {
751 					u32 clus = fatent.entry - free;
752 
753 					err = fat_trim_clusters(sb, clus, free);
754 					if (err && err != -EOPNOTSUPP)
755 						goto error;
756 					if (!err)
757 						trimmed += free;
758 					err = 0;
759 				}
760 				free = 0;
761 			}
762 		} while (fat_ent_next(sbi, &fatent) && fatent.entry <= ent_end);
763 
764 		if (fatal_signal_pending(current)) {
765 			err = -ERESTARTSYS;
766 			goto error;
767 		}
768 
769 		if (need_resched()) {
770 			fatent_brelse(&fatent);
771 			unlock_fat(sbi);
772 			cond_resched();
773 			lock_fat(sbi);
774 		}
775 	}
776 	/* handle scenario when tail entries are all free */
777 	if (free && free >= minlen) {
778 		u32 clus = fatent.entry - free;
779 
780 		err = fat_trim_clusters(sb, clus, free);
781 		if (err && err != -EOPNOTSUPP)
782 			goto error;
783 		if (!err)
784 			trimmed += free;
785 		err = 0;
786 	}
787 
788 error:
789 	fatent_brelse(&fatent);
790 	unlock_fat(sbi);
791 
792 	range->len = trimmed << sbi->cluster_bits;
793 
794 	return err;
795 }
796