]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/fat/fatent.c
fat: Fix fat_ent_update_ptr() for FAT12
[linux-2.6-omap-h63xx.git] / fs / fat / fatent.c
index 5fb366992b73dcfdbf6a936c510622087b324444..13513992da3cce40de0afa6a281f66b27e260a3d 100644 (file)
@@ -6,6 +6,8 @@
 #include <linux/module.h>
 #include <linux/fs.h>
 #include <linux/msdos_fs.h>
+#include <linux/blkdev.h>
+#include "fat.h"
 
 struct fatent_operations {
        void (*ent_blocknr)(struct super_block *, int, int *, sector_t *);
@@ -315,10 +317,20 @@ static inline int fat_ent_update_ptr(struct super_block *sb,
        /* Is this fatent's blocks including this entry? */
        if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
                return 0;
-       /* Does this entry need the next block? */
-       if (sbi->fat_bits == 12 && (offset + 1) >= sb->s_blocksize) {
-               if (fatent->nr_bhs != 2 || bhs[1]->b_blocknr != (blocknr + 1))
-                       return 0;
+       if (sbi->fat_bits == 12) {
+               if ((offset + 1) < sb->s_blocksize) {
+                       /* This entry is on bhs[0]. */
+                       if (fatent->nr_bhs == 2) {
+                               brelse(bhs[1]);
+                               fatent->nr_bhs = 1;
+                       }
+               } else {
+                       /* This entry needs the next block. */
+                       if (fatent->nr_bhs != 2)
+                               return 0;
+                       if (bhs[1]->b_blocknr != (blocknr + 1))
+                               return 0;
+               }
        }
        ops->ent_set_ptr(fatent, offset);
        return 1;
@@ -450,7 +462,8 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
        BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2));    /* fixed limit */
 
        lock_fat(sbi);
-       if (sbi->free_clusters != -1 && sbi->free_clusters < nr_cluster) {
+       if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
+           sbi->free_clusters < nr_cluster) {
                unlock_fat(sbi);
                return -ENOSPC;
        }
@@ -504,6 +517,7 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
 
        /* Couldn't allocate the free entries */
        sbi->free_clusters = 0;
+       sbi->free_clus_valid = 1;
        sb->s_dirt = 1;
        err = -ENOSPC;
 
@@ -533,6 +547,7 @@ int fat_free_clusters(struct inode *inode, int cluster)
        struct fat_entry fatent;
        struct buffer_head *bhs[MAX_BUF_PER_PAGE];
        int i, err, nr_bhs;
+       int first_cl = cluster;
 
        nr_bhs = 0;
        fatent_init(&fatent);
@@ -544,11 +559,23 @@ int fat_free_clusters(struct inode *inode, int cluster)
                        goto error;
                } else if (cluster == FAT_ENT_FREE) {
                        fat_fs_panic(sb, "%s: deleting FAT entry beyond EOF",
-                                    __FUNCTION__);
+                                    __func__);
                        err = -EIO;
                        goto error;
                }
 
+               /* 
+                * Issue discard for the sectors we no longer care about,
+                * batching contiguous clusters into one request
+                */
+               if (cluster != fatent.entry + 1) {
+                       int nr_clus = fatent.entry - first_cl + 1;
+
+                       sb_issue_discard(sb, fat_clus_to_blknr(sbi, first_cl),
+                                        nr_clus * sbi->sec_per_clus);
+                       first_cl = cluster;
+               }
+
                ops->ent_put(&fatent, FAT_ENT_FREE);
                if (sbi->free_clusters != -1) {
                        sbi->free_clusters++;
@@ -583,8 +610,6 @@ error:
                brelse(bhs[i]);
        unlock_fat(sbi);
 
-       fat_clusters_flush(sb);
-
        return err;
 }
 
@@ -615,7 +640,7 @@ int fat_count_free_clusters(struct super_block *sb)
        int err = 0, free;
 
        lock_fat(sbi);
-       if (sbi->free_clusters != -1)
+       if (sbi->free_clusters != -1 && sbi->free_clus_valid)
                goto out;
 
        reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
@@ -643,6 +668,7 @@ int fat_count_free_clusters(struct super_block *sb)
                } while (fat_ent_next(sbi, &fatent));
        }
        sbi->free_clusters = free;
+       sbi->free_clus_valid = 1;
        sb->s_dirt = 1;
        fatent_brelse(&fatent);
 out: