ext4: bigalloc changes to block bitmap initialization functions
authorTheodore Ts'o <tytso@mit.edu>
Fri, 9 Sep 2011 22:44:51 +0000 (18:44 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 9 Sep 2011 22:44:51 +0000 (18:44 -0400)
Add bigalloc support to ext4_init_block_bitmap() and
ext4_free_blocks_after_init().

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
fs/ext4/balloc.c
fs/ext4/ext4.h

index 735d9fcc72e69efa8565d45c5ab49d1bd0422a97..1c6d777b35a298a7148de5ef175f5b7f2258d605 100644 (file)
@@ -23,9 +23,6 @@
 
 #include <trace/events/ext4.h>
 
-static unsigned int num_base_meta_blocks(struct super_block *sb,
-                                        ext4_group_t block_group);
-
 /*
  * balloc.c contains the blocks allocation and deallocation routines
  */
@@ -58,37 +55,87 @@ static int ext4_block_in_group(struct super_block *sb, ext4_fsblk_t block,
        return 0;
 }
 
-static int ext4_group_used_meta_blocks(struct super_block *sb,
-                                      ext4_group_t block_group,
-                                      struct ext4_group_desc *gdp)
+/* Return the number of clusters used for file system metadata; this
+ * represents the overhead needed by the file system.
+ */
+unsigned ext4_num_overhead_clusters(struct super_block *sb,
+                                   ext4_group_t block_group,
+                                   struct ext4_group_desc *gdp)
 {
-       ext4_fsblk_t tmp;
+       unsigned num_clusters;
+       int block_cluster = -1, inode_cluster = -1, itbl_cluster = -1, i, c;
+       ext4_fsblk_t start = ext4_group_first_block_no(sb, block_group);
+       ext4_fsblk_t itbl_blk;
        struct ext4_sb_info *sbi = EXT4_SB(sb);
-       /* block bitmap, inode bitmap, and inode table blocks */
-       int used_blocks = sbi->s_itb_per_group + 2;
 
-       if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
-               if (!ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp),
-                                       block_group))
-                       used_blocks--;
-
-               if (!ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp),
-                                       block_group))
-                       used_blocks--;
-
-               tmp = ext4_inode_table(sb, gdp);
-               for (; tmp < ext4_inode_table(sb, gdp) +
-                               sbi->s_itb_per_group; tmp++) {
-                       if (!ext4_block_in_group(sb, tmp, block_group))
-                               used_blocks -= 1;
+       /* This is the number of clusters used by the superblock,
+        * block group descriptors, and reserved block group
+        * descriptor blocks */
+       num_clusters = ext4_num_base_meta_clusters(sb, block_group);
+
+       /*
+        * For the allocation bitmaps and inode table, we first need
+        * to check to see if the block is in the block group.  If it
+        * is, then check to see if the cluster is already accounted
+        * for in the clusters used for the base metadata cluster, or
+        * if we can increment the base metadata cluster to include
+        * that block.  Otherwise, we will have to track the cluster
+        * used for the allocation bitmap or inode table explicitly.
+        * Normally all of these blocks are contiguous, so the special
+        * case handling shouldn't be necessary except for *very*
+        * unusual file system layouts.
+        */
+       if (ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), block_group)) {
+               block_cluster = EXT4_B2C(sbi, (start -
+                                              ext4_block_bitmap(sb, gdp)));
+               if (block_cluster < num_clusters)
+                       block_cluster = -1;
+               else if (block_cluster == num_clusters) {
+                       num_clusters++;
+                       block_cluster = -1;
+               }
+       }
+
+       if (ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), block_group)) {
+               inode_cluster = EXT4_B2C(sbi,
+                                        start - ext4_inode_bitmap(sb, gdp));
+               if (inode_cluster < num_clusters)
+                       inode_cluster = -1;
+               else if (inode_cluster == num_clusters) {
+                       num_clusters++;
+                       inode_cluster = -1;
+               }
+       }
+
+       itbl_blk = ext4_inode_table(sb, gdp);
+       for (i = 0; i < sbi->s_itb_per_group; i++) {
+               if (ext4_block_in_group(sb, itbl_blk + i, block_group)) {
+                       c = EXT4_B2C(sbi, start - itbl_blk + i);
+                       if ((c < num_clusters) || (c == inode_cluster) ||
+                           (c == block_cluster) || (c == itbl_cluster))
+                               continue;
+                       if (c == num_clusters) {
+                               num_clusters++;
+                               continue;
+                       }
+                       num_clusters++;
+                       itbl_cluster = c;
                }
        }
-       return used_blocks;
+
+       if (block_cluster != -1)
+               num_clusters++;
+       if (inode_cluster != -1)
+               num_clusters++;
+
+       return num_clusters;
 }
 
-static unsigned int num_blocks_in_group(struct super_block *sb,
-                                       ext4_group_t block_group)
+static unsigned int num_clusters_in_group(struct super_block *sb,
+                                         ext4_group_t block_group)
 {
+       unsigned int blocks;
+
        if (block_group == ext4_get_groups_count(sb) - 1) {
                /*
                 * Even though mke2fs always initializes the first and
@@ -96,10 +143,11 @@ static unsigned int num_blocks_in_group(struct super_block *sb,
                 * we need to make sure we calculate the right free
                 * blocks.
                 */
-               return ext4_blocks_count(EXT4_SB(sb)->s_es) -
+               blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) -
                        ext4_group_first_block_no(sb, block_group);
        } else
-               return EXT4_BLOCKS_PER_GROUP(sb);
+               blocks = EXT4_BLOCKS_PER_GROUP(sb);
+       return EXT4_NUM_B2C(EXT4_SB(sb), blocks);
 }
 
 /* Initializes an uninitialized block bitmap */
@@ -107,7 +155,7 @@ void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
                            ext4_group_t block_group,
                            struct ext4_group_desc *gdp)
 {
-       unsigned int bit, bit_max = num_base_meta_blocks(sb, block_group);
+       unsigned int bit, bit_max;
        struct ext4_sb_info *sbi = EXT4_SB(sb);
        ext4_fsblk_t start, tmp;
        int flex_bg = 0;
@@ -126,6 +174,7 @@ void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
        }
        memset(bh->b_data, 0, sb->s_blocksize);
 
+       bit_max = ext4_num_base_meta_clusters(sb, block_group);
        for (bit = 0; bit < bit_max; bit++)
                ext4_set_bit(bit, bh->b_data);
 
@@ -137,24 +186,25 @@ void ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
        /* Set bits for block and inode bitmaps, and inode table */
        tmp = ext4_block_bitmap(sb, gdp);
        if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
-               ext4_set_bit(tmp - start, bh->b_data);
+               ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
 
        tmp = ext4_inode_bitmap(sb, gdp);
        if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
-               ext4_set_bit(tmp - start, bh->b_data);
+               ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
 
        tmp = ext4_inode_table(sb, gdp);
        for (; tmp < ext4_inode_table(sb, gdp) +
                     sbi->s_itb_per_group; tmp++) {
                if (!flex_bg || ext4_block_in_group(sb, tmp, block_group))
-                       ext4_set_bit(tmp - start, bh->b_data);
+                       ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
        }
+
        /*
         * Also if the number of blocks within the group is less than
         * the blocksize * 8 ( which is the size of bitmap ), set rest
         * of the block bitmap to 1
         */
-       ext4_mark_bitmap_end(num_blocks_in_group(sb, block_group),
+       ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
                             sb->s_blocksize * 8, bh->b_data);
 }
 
@@ -165,9 +215,8 @@ unsigned ext4_free_blocks_after_init(struct super_block *sb,
                                     ext4_group_t block_group,
                                     struct ext4_group_desc *gdp)
 {
-       return num_blocks_in_group(sb, block_group) -
-               num_base_meta_blocks(sb, block_group) -
-               ext4_group_used_meta_blocks(sb, block_group, gdp);
+       return num_clusters_in_group(sb, block_group) - 
+               ext4_num_overhead_clusters(sb, block_group, gdp);
 }
 
 /*
@@ -611,14 +660,14 @@ unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
 }
 
 /*
- * This function returns the number of file system metadata blocks at
+ * This function returns the number of file system metadata clusters at
  * the beginning of a block group, including the reserved gdt blocks.
  */
-static unsigned int num_base_meta_blocks(struct super_block *sb,
-                                        ext4_group_t block_group)
+unsigned ext4_num_base_meta_clusters(struct super_block *sb,
+                                    ext4_group_t block_group)
 {
        struct ext4_sb_info *sbi = EXT4_SB(sb);
-       int num;
+       unsigned num;
 
        /* Check for superblock and gdt backups in this group */
        num = ext4_bg_has_super(sb, block_group);
@@ -633,7 +682,7 @@ static unsigned int num_base_meta_blocks(struct super_block *sb,
        } else { /* For META_BG_BLOCK_GROUPS */
                num += ext4_bg_num_gdb(sb, block_group);
        }
-       return num;
+       return EXT4_NUM_B2C(sbi, num);
 }
 /**
  *     ext4_inode_to_goal_block - return a hint for block allocation
index b0b7b67e439d13584796bb684a38834c3cfc60ac..803cfa42e1e8e46d9c8cb70bfeb03938d2e5fc0a 100644 (file)
@@ -261,6 +261,14 @@ struct ext4_io_submit {
 #endif
 #define EXT4_BLOCK_ALIGN(size, blkbits)                ALIGN((size), (1 << (blkbits)))
 
+/* Translate a block number to a cluster number */
+#define EXT4_B2C(sbi, blk)     ((blk) >> (sbi)->s_cluster_bits)
+/* Translate a cluster number to a block number */
+#define EXT4_C2B(sbi, cluster) ((cluster) << (sbi)->s_cluster_bits)
+/* Translate # of blks to # of clusters */
+#define EXT4_NUM_B2C(sbi, blks)        (((blks) + (sbi)->s_cluster_ratio - 1) >> \
+                                (sbi)->s_cluster_bits)
+
 /*
  * Structure of a blocks group descriptor
  */
@@ -1770,6 +1778,11 @@ extern void ext4_init_block_bitmap(struct super_block *sb,
 extern unsigned ext4_free_blocks_after_init(struct super_block *sb,
                                            ext4_group_t block_group,
                                            struct ext4_group_desc *gdp);
+extern unsigned ext4_num_base_meta_clusters(struct super_block *sb,
+                                           ext4_group_t block_group);
+extern unsigned ext4_num_overhead_clusters(struct super_block *sb,
+                                          ext4_group_t block_group,
+                                          struct ext4_group_desc *gdp);
 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
 
 /* dir.c */