drm/bridge/parade: Drop legacy GPIO header
[linux/fpc-iii.git] / fs / fat / fatent.c
blob265983635f2be7b5d67078cc7127e43ea74cf288
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2004, OGAWA Hirofumi
4 */
6 #include <linux/blkdev.h>
7 #include <linux/sched/signal.h>
8 #include "fat.h"
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 *);
20 static DEFINE_SPINLOCK(fat12_entry_lock);
22 static void fat12_ent_blocknr(struct super_block *sb, int entry,
23 int *offset, sector_t *blocknr)
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);
32 static void fat_ent_blocknr(struct super_block *sb, int entry,
33 int *offset, sector_t *blocknr)
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);
42 static void fat12_ent_set_ptr(struct fat_entry *fatent, int offset)
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;
56 static void fat16_ent_set_ptr(struct fat_entry *fatent, int offset)
58 WARN_ON(offset & (2 - 1));
59 fatent->u.ent16_p = (__le16 *)(fatent->bhs[0]->b_data + offset);
62 static void fat32_ent_set_ptr(struct fat_entry *fatent, int offset)
64 WARN_ON(offset & (4 - 1));
65 fatent->u.ent32_p = (__le32 *)(fatent->bhs[0]->b_data + offset);
68 static int fat12_ent_bread(struct super_block *sb, struct fat_entry *fatent,
69 int offset, sector_t blocknr)
71 struct buffer_head **bhs = fatent->bhs;
73 WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
74 fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
76 bhs[0] = sb_bread(sb, blocknr);
77 if (!bhs[0])
78 goto err;
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;
90 fat12_ent_set_ptr(fatent, offset);
91 return 0;
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;
100 static int fat_ent_bread(struct super_block *sb, struct fat_entry *fatent,
101 int offset, sector_t blocknr)
103 const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
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;
113 fatent->nr_bhs = 1;
114 ops->ent_set_ptr(fatent, offset);
115 return 0;
118 static int fat12_ent_get(struct fat_entry *fatent)
120 u8 **ent12_p = fatent->u.ent12_p;
121 int next;
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);
130 next &= 0x0fff;
131 if (next >= BAD_FAT12)
132 next = FAT_ENT_EOF;
133 return next;
136 static int fat16_ent_get(struct fat_entry *fatent)
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;
145 static int fat32_ent_get(struct fat_entry *fatent)
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;
154 static void fat12_ent_put(struct fat_entry *fatent, int new)
156 u8 **ent12_p = fatent->u.ent12_p;
158 if (new == FAT_ENT_EOF)
159 new = EOF_FAT12;
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);
169 spin_unlock(&fat12_entry_lock);
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);
176 static void fat16_ent_put(struct fat_entry *fatent, int new)
178 if (new == FAT_ENT_EOF)
179 new = EOF_FAT16;
181 *fatent->u.ent16_p = cpu_to_le16(new);
182 mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
185 static void fat32_ent_put(struct fat_entry *fatent, int new)
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);
193 static int fat12_ent_next(struct fat_entry *fatent)
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);
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;
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;
221 ent12_p[0] = NULL;
222 ent12_p[1] = NULL;
223 return 0;
226 static int fat16_ent_next(struct fat_entry *fatent)
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;
234 fatent->u.ent16_p = NULL;
235 return 0;
238 static int fat32_ent_next(struct fat_entry *fatent)
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;
246 fatent->u.ent32_p = NULL;
247 return 0;
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,
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,
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,
277 static inline void lock_fat(struct msdos_sb_info *sbi)
279 mutex_lock(&sbi->fat_lock);
282 static inline void unlock_fat(struct msdos_sb_info *sbi)
284 mutex_unlock(&sbi->fat_lock);
287 void fat_ent_access_init(struct super_block *sb)
289 struct msdos_sb_info *sbi = MSDOS_SB(sb);
291 mutex_init(&sbi->fat_lock);
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);
307 static void mark_fsinfo_dirty(struct super_block *sb)
309 struct msdos_sb_info *sbi = MSDOS_SB(sb);
311 if (sb_rdonly(sb) || !is_fat32(sbi))
312 return;
314 __mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC);
317 static inline int fat_ent_update_ptr(struct super_block *sb,
318 struct fat_entry *fatent,
319 int offset, sector_t blocknr)
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;
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;
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;
343 ops->ent_set_ptr(fatent, offset);
344 return 1;
347 int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry)
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;
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;
361 fatent_set_entry(fatent, entry);
362 ops->ent_blocknr(sb, entry, &offset, &blocknr);
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;
370 return ops->ent_get(fatent);
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)
377 struct msdos_sb_info *sbi = MSDOS_SB(sb);
378 struct buffer_head *c_bh;
379 int err, n, copy;
381 err = 0;
382 for (copy = 1; copy < sbi->fats; copy++) {
383 sector_t backup_fat = sbi->fat_length * copy;
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;
391 memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize);
392 set_buffer_uptodate(c_bh);
393 mark_buffer_dirty_inode(c_bh, sbi->fat_inode);
394 if (sb->s_flags & SB_SYNCHRONOUS)
395 err = sync_dirty_buffer(c_bh);
396 brelse(c_bh);
397 if (err)
398 goto error;
401 error:
402 return err;
405 int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
406 int new, int wait)
408 struct super_block *sb = inode->i_sb;
409 const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
410 int err;
412 ops->ent_put(fatent, new);
413 if (wait) {
414 err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
415 if (err)
416 return err;
418 return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
421 static inline int fat_ent_next(struct msdos_sb_info *sbi,
422 struct fat_entry *fatent)
424 if (sbi->fatent_ops->ent_next(fatent)) {
425 if (fatent->entry < sbi->max_cluster)
426 return 1;
428 return 0;
431 static inline int fat_ent_read_block(struct super_block *sb,
432 struct fat_entry *fatent)
434 const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
435 sector_t blocknr;
436 int offset;
438 fatent_brelse(fatent);
439 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
440 return ops->ent_bread(sb, fatent, offset, blocknr);
443 static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
444 struct fat_entry *fatent)
446 int n, i;
448 for (n = 0; n < fatent->nr_bhs; n++) {
449 for (i = 0; i < *nr_bhs; i++) {
450 if (fatent->bhs[n] == bhs[i])
451 break;
453 if (i == *nr_bhs) {
454 get_bh(fatent->bhs[n]);
455 bhs[i] = fatent->bhs[n];
456 (*nr_bhs)++;
461 int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
463 struct super_block *sb = inode->i_sb;
464 struct msdos_sb_info *sbi = MSDOS_SB(sb);
465 const struct fatent_operations *ops = sbi->fatent_ops;
466 struct fat_entry fatent, prev_ent;
467 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
468 int i, count, err, nr_bhs, idx_clus;
470 BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */
472 lock_fat(sbi);
473 if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
474 sbi->free_clusters < nr_cluster) {
475 unlock_fat(sbi);
476 return -ENOSPC;
479 err = nr_bhs = idx_clus = 0;
480 count = FAT_START_ENT;
481 fatent_init(&prev_ent);
482 fatent_init(&fatent);
483 fatent_set_entry(&fatent, sbi->prev_free + 1);
484 while (count < sbi->max_cluster) {
485 if (fatent.entry >= sbi->max_cluster)
486 fatent.entry = FAT_START_ENT;
487 fatent_set_entry(&fatent, fatent.entry);
488 err = fat_ent_read_block(sb, &fatent);
489 if (err)
490 goto out;
492 /* Find the free entries in a block */
493 do {
494 if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
495 int entry = fatent.entry;
497 /* make the cluster chain */
498 ops->ent_put(&fatent, FAT_ENT_EOF);
499 if (prev_ent.nr_bhs)
500 ops->ent_put(&prev_ent, entry);
502 fat_collect_bhs(bhs, &nr_bhs, &fatent);
504 sbi->prev_free = entry;
505 if (sbi->free_clusters != -1)
506 sbi->free_clusters--;
508 cluster[idx_clus] = entry;
509 idx_clus++;
510 if (idx_clus == nr_cluster)
511 goto out;
514 * fat_collect_bhs() gets ref-count of bhs,
515 * so we can still use the prev_ent.
517 prev_ent = fatent;
519 count++;
520 if (count == sbi->max_cluster)
521 break;
522 } while (fat_ent_next(sbi, &fatent));
525 /* Couldn't allocate the free entries */
526 sbi->free_clusters = 0;
527 sbi->free_clus_valid = 1;
528 err = -ENOSPC;
530 out:
531 unlock_fat(sbi);
532 mark_fsinfo_dirty(sb);
533 fatent_brelse(&fatent);
534 if (!err) {
535 if (inode_needs_sync(inode))
536 err = fat_sync_bhs(bhs, nr_bhs);
537 if (!err)
538 err = fat_mirror_bhs(sb, bhs, nr_bhs);
540 for (i = 0; i < nr_bhs; i++)
541 brelse(bhs[i]);
543 if (err && idx_clus)
544 fat_free_clusters(inode, cluster[0]);
546 return err;
549 int fat_free_clusters(struct inode *inode, int cluster)
551 struct super_block *sb = inode->i_sb;
552 struct msdos_sb_info *sbi = MSDOS_SB(sb);
553 const struct fatent_operations *ops = sbi->fatent_ops;
554 struct fat_entry fatent;
555 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
556 int i, err, nr_bhs;
557 int first_cl = cluster, dirty_fsinfo = 0;
559 nr_bhs = 0;
560 fatent_init(&fatent);
561 lock_fat(sbi);
562 do {
563 cluster = fat_ent_read(inode, &fatent, cluster);
564 if (cluster < 0) {
565 err = cluster;
566 goto error;
567 } else if (cluster == FAT_ENT_FREE) {
568 fat_fs_error(sb, "%s: deleting FAT entry beyond EOF",
569 __func__);
570 err = -EIO;
571 goto error;
574 if (sbi->options.discard) {
576 * Issue discard for the sectors we no longer
577 * care about, batching contiguous clusters
578 * into one request
580 if (cluster != fatent.entry + 1) {
581 int nr_clus = fatent.entry - first_cl + 1;
583 sb_issue_discard(sb,
584 fat_clus_to_blknr(sbi, first_cl),
585 nr_clus * sbi->sec_per_clus,
586 GFP_NOFS, 0);
588 first_cl = cluster;
592 ops->ent_put(&fatent, FAT_ENT_FREE);
593 if (sbi->free_clusters != -1) {
594 sbi->free_clusters++;
595 dirty_fsinfo = 1;
598 if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
599 if (sb->s_flags & SB_SYNCHRONOUS) {
600 err = fat_sync_bhs(bhs, nr_bhs);
601 if (err)
602 goto error;
604 err = fat_mirror_bhs(sb, bhs, nr_bhs);
605 if (err)
606 goto error;
607 for (i = 0; i < nr_bhs; i++)
608 brelse(bhs[i]);
609 nr_bhs = 0;
611 fat_collect_bhs(bhs, &nr_bhs, &fatent);
612 } while (cluster != FAT_ENT_EOF);
614 if (sb->s_flags & SB_SYNCHRONOUS) {
615 err = fat_sync_bhs(bhs, nr_bhs);
616 if (err)
617 goto error;
619 err = fat_mirror_bhs(sb, bhs, nr_bhs);
620 error:
621 fatent_brelse(&fatent);
622 for (i = 0; i < nr_bhs; i++)
623 brelse(bhs[i]);
624 unlock_fat(sbi);
625 if (dirty_fsinfo)
626 mark_fsinfo_dirty(sb);
628 return err;
630 EXPORT_SYMBOL_GPL(fat_free_clusters);
632 /* 128kb is the whole sectors for FAT12 and FAT16 */
633 #define FAT_READA_SIZE (128 * 1024)
635 static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent,
636 unsigned long reada_blocks)
638 const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
639 sector_t blocknr;
640 int i, offset;
642 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
644 for (i = 0; i < reada_blocks; i++)
645 sb_breadahead(sb, blocknr + i);
648 int fat_count_free_clusters(struct super_block *sb)
650 struct msdos_sb_info *sbi = MSDOS_SB(sb);
651 const struct fatent_operations *ops = sbi->fatent_ops;
652 struct fat_entry fatent;
653 unsigned long reada_blocks, reada_mask, cur_block;
654 int err = 0, free;
656 lock_fat(sbi);
657 if (sbi->free_clusters != -1 && sbi->free_clus_valid)
658 goto out;
660 reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
661 reada_mask = reada_blocks - 1;
662 cur_block = 0;
664 free = 0;
665 fatent_init(&fatent);
666 fatent_set_entry(&fatent, FAT_START_ENT);
667 while (fatent.entry < sbi->max_cluster) {
668 /* readahead of fat blocks */
669 if ((cur_block & reada_mask) == 0) {
670 unsigned long rest = sbi->fat_length - cur_block;
671 fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
673 cur_block++;
675 err = fat_ent_read_block(sb, &fatent);
676 if (err)
677 goto out;
679 do {
680 if (ops->ent_get(&fatent) == FAT_ENT_FREE)
681 free++;
682 } while (fat_ent_next(sbi, &fatent));
683 cond_resched();
685 sbi->free_clusters = free;
686 sbi->free_clus_valid = 1;
687 mark_fsinfo_dirty(sb);
688 fatent_brelse(&fatent);
689 out:
690 unlock_fat(sbi);
691 return err;
694 static int fat_trim_clusters(struct super_block *sb, u32 clus, u32 nr_clus)
696 struct msdos_sb_info *sbi = MSDOS_SB(sb);
697 return sb_issue_discard(sb, fat_clus_to_blknr(sbi, clus),
698 nr_clus * sbi->sec_per_clus, GFP_NOFS, 0);
701 int fat_trim_fs(struct inode *inode, struct fstrim_range *range)
703 struct super_block *sb = inode->i_sb;
704 struct msdos_sb_info *sbi = MSDOS_SB(sb);
705 const struct fatent_operations *ops = sbi->fatent_ops;
706 struct fat_entry fatent;
707 u64 ent_start, ent_end, minlen, trimmed = 0;
708 u32 free = 0;
709 unsigned long reada_blocks, reada_mask, cur_block = 0;
710 int err = 0;
713 * FAT data is organized as clusters, trim at the granulary of cluster.
715 * fstrim_range is in byte, convert vaules to cluster index.
716 * Treat sectors before data region as all used, not to trim them.
718 ent_start = max_t(u64, range->start>>sbi->cluster_bits, FAT_START_ENT);
719 ent_end = ent_start + (range->len >> sbi->cluster_bits) - 1;
720 minlen = range->minlen >> sbi->cluster_bits;
722 if (ent_start >= sbi->max_cluster || range->len < sbi->cluster_size)
723 return -EINVAL;
724 if (ent_end >= sbi->max_cluster)
725 ent_end = sbi->max_cluster - 1;
727 reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
728 reada_mask = reada_blocks - 1;
730 fatent_init(&fatent);
731 lock_fat(sbi);
732 fatent_set_entry(&fatent, ent_start);
733 while (fatent.entry <= ent_end) {
734 /* readahead of fat blocks */
735 if ((cur_block & reada_mask) == 0) {
736 unsigned long rest = sbi->fat_length - cur_block;
737 fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
739 cur_block++;
741 err = fat_ent_read_block(sb, &fatent);
742 if (err)
743 goto error;
744 do {
745 if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
746 free++;
747 } else if (free) {
748 if (free >= minlen) {
749 u32 clus = fatent.entry - free;
751 err = fat_trim_clusters(sb, clus, free);
752 if (err && err != -EOPNOTSUPP)
753 goto error;
754 if (!err)
755 trimmed += free;
756 err = 0;
758 free = 0;
760 } while (fat_ent_next(sbi, &fatent) && fatent.entry <= ent_end);
762 if (fatal_signal_pending(current)) {
763 err = -ERESTARTSYS;
764 goto error;
767 if (need_resched()) {
768 fatent_brelse(&fatent);
769 unlock_fat(sbi);
770 cond_resched();
771 lock_fat(sbi);
774 /* handle scenario when tail entries are all free */
775 if (free && free >= minlen) {
776 u32 clus = fatent.entry - free;
778 err = fat_trim_clusters(sb, clus, free);
779 if (err && err != -EOPNOTSUPP)
780 goto error;
781 if (!err)
782 trimmed += free;
783 err = 0;
786 error:
787 fatent_brelse(&fatent);
788 unlock_fat(sbi);
790 range->len = trimmed << sbi->cluster_bits;
792 return err;