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