FIX: block bitmap is not correctly initialized.
authorngkaho1234 <ngkaho1234@gmail.com>
Thu, 1 Oct 2015 11:10:57 +0000 (19:10 +0800)
committerngkaho1234 <ngkaho1234@gmail.com>
Thu, 1 Oct 2015 11:10:57 +0000 (19:10 +0800)
lwext4/ext4_balloc.c
lwext4/ext4_balloc.h
lwext4/ext4_config.h
lwext4/ext4_fs.c
lwext4/ext4_fs.h

index 73acf6789117669f66f925be7fbf26b013603539..71074585af8b7c3096b86f34f987256812aa10b9 100644 (file)
@@ -52,8 +52,8 @@
  * @param baddr Absolute address of block.
  * @return Block group index
  */
-static uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s,
-                                             uint32_t baddr)
+uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s,
+                                      uint32_t baddr)
 {
        if (ext4_get32(s, first_data_block))
                baddr--;
@@ -61,37 +61,20 @@ static uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s,
        return baddr / ext4_get32(s, blocks_per_group);
 }
 
-uint32_t
-ext4_balloc_get_first_data_block_in_group(struct ext4_sblock *s,
-                                         struct ext4_block_group_ref *bg_ref)
+/**@brief Compute the starting block address of a block group
+ * @param sb   superblock pointer.
+ * @param bgid block group index
+ * @return Block address
+ */
+uint32_t ext4_balloc_get_block_of_bgid(struct ext4_sblock *s,
+                                      uint32_t bgid)
 {
-       uint32_t block_group_count = ext4_block_group_cnt(s);
-       uint32_t inode_table_first_block =
-           ext4_bg_get_inode_table_first_block(bg_ref->block_group, s);
-       uint32_t block_size = ext4_sb_get_block_size(s);
-
-       uint16_t inode_size = ext4_get16(s, inode_size);
-       uint32_t inodes_per_group = ext4_get32(s, inodes_per_group);
-
-       uint32_t inode_table_bytes;
-
-       if (bg_ref->index < block_group_count - 1) {
-               inode_table_bytes = inodes_per_group * inode_size;
-       } else {
-               /* Last block group could be smaller */
-               uint32_t inodes_count_total = ext4_get32(s, inodes_count);
-               inode_table_bytes =
-                   (inodes_count_total -
-                    ((block_group_count - 1) * inodes_per_group)) *
-                   inode_size;
-       }
-
-       uint32_t inode_table_blocks = inode_table_bytes / block_size;
-
-       if (inode_table_bytes % block_size)
-               inode_table_blocks++;
+       uint32_t baddr = 0;
+       if (ext4_get32(s, first_data_block))
+               baddr++;
 
-       return inode_table_first_block + inode_table_blocks;
+       baddr += bgid * ext4_get32(s, blocks_per_group);
+       return baddr;
 }
 
 int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, uint32_t baddr)
@@ -369,8 +352,7 @@ int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref, uint32_t *fblock)
        }
 
        /* Compute indexes */
-       uint32_t first_in_group =
-           ext4_balloc_get_first_data_block_in_group(sb, &bg_ref);
+       uint32_t first_in_group = ext4_balloc_get_block_of_bgid(sb, bg_ref.index);
 
        uint32_t first_in_group_index =
            ext4_fs_baddr2_index_in_group(sb, first_in_group);
@@ -488,8 +470,7 @@ goal_failed:
                }
 
                /* Compute indexes */
-               first_in_group =
-                   ext4_balloc_get_first_data_block_in_group(sb, &bg_ref);
+               first_in_group = ext4_balloc_get_block_of_bgid(sb, bgid);
                index_in_group =
                    ext4_fs_baddr2_index_in_group(sb, first_in_group);
                blocks_in_group = ext4_blocks_in_group_cnt(sb, bgid);
index 06ad897ef6f4951e29b6cfe628d57290493a2654..b3c84885841b281cf1cc5188f2033130d504e5ae 100644 (file)
 #include <stdint.h>
 #include <stdbool.h>
 
-/**@brief   Get first datablock in block group
- * @param   s superblock descriptor
- * @param   bg_ref block group reference
- * @return  block id of the first datablock in block group*/
-uint32_t
-ext4_balloc_get_first_data_block_in_group(struct ext4_sblock *s,
-                                         struct ext4_block_group_ref *bg_ref);
+/**@brief Compute number of block group from block address.
+ * @param sb superblock pointer.
+ * @param baddr Absolute address of block.
+ * @return Block group index
+ */
+uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s,
+                                      uint32_t baddr);
+
+/**@brief Compute the starting block address of a block group
+ * @param sb   superblock pointer.
+ * @param bgid block group index
+ * @return Block address
+ */
+uint32_t ext4_balloc_get_block_of_bgid(struct ext4_sblock *s,
+                                      uint32_t bgid);
 
 /**@brief   Free block from inode.
  * @param   inode_ref inode reference
index 65c8e102e363eb6183985f4982f283df4f2966d1..5b0af0c2c62bb228c6d475027f088d277a8d4b84 100644 (file)
 
 /**@brief   Include assert codes from ext4_debug or standard library.*/
 #ifndef CONFIG_HAVE_OWN_ASSERT
-#define CONFIG_HAVE_OWN_ASSERT 1
+#define CONFIG_HAVE_OWN_ASSERT 0
 #endif
 
 /**@brief   Statistics of block device*/
index dccde5e6745f55b8b54de5fba71897f51ebf2203..0a97495f05b720b0965446906658844d61dbace3 100644 (file)
@@ -271,15 +271,70 @@ int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only)
        return EOK;
 }
 
+/**@brief Determine whether the block is inside the group.
+ * @param baddr   block address
+ * @param bgid    block group id
+ * @return Error code
+ */
+static int ext4_block_in_group(struct ext4_sblock *s,
+                              uint32_t baddr,
+                              uint32_t bgid)
+{
+       uint32_t actual_bgid;
+       actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr);
+       if (actual_bgid == bgid)
+               return 1;
+       return 0;
+}
+
+/**@brief   To avoid calling the atomic setbit hundreds or thousands of times, we only
+ *          need to use it within a single byte (to ensure we get endianness right).
+ *          We can use memset for the rest of the bitmap as there are no other users.
+ */
+static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap)
+{
+       int i;
+
+       if (start_bit >= end_bit)
+               return;
+
+       for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++)
+               ext4_bmap_bit_set(bitmap, i);
+
+       if (i < end_bit)
+               memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
+}
+
 /**@brief Initialize block bitmap in block group.
  * @param bg_ref Reference to block group
  * @return Error code
  */
 static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
 {
-       uint32_t i;
+       uint32_t i, bit, bit_max;
+       uint32_t group_blocks;
+       uint16_t inode_size = ext4_get16(&bg_ref->fs->sb, inode_size);
+       uint32_t block_size = ext4_sb_get_block_size(&bg_ref->fs->sb);
+       uint32_t inodes_per_group = ext4_get32(&bg_ref->fs->sb, inodes_per_group);
        uint32_t bitmap_block_addr =
            ext4_bg_get_block_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
+       uint32_t bitmap_inode_addr =
+           ext4_bg_get_inode_bitmap(bg_ref->block_group, &bg_ref->fs->sb);
+       uint32_t inode_table_addr =
+           ext4_bg_get_inode_table_first_block(bg_ref->block_group,
+                                               &bg_ref->fs->sb);
+       uint32_t first_group_addr =
+           ext4_balloc_get_block_of_bgid(&bg_ref->fs->sb, bg_ref->index);
+
+       uint32_t dsc_per_block =
+           ext4_sb_get_block_size(&bg_ref->fs->sb) /
+           ext4_sb_get_desc_size(&bg_ref->fs->sb);
+
+       bool flex_bg =
+               ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
+                                                EXT4_FEATURE_INCOMPAT_FLEX_BG);
+
+       uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size;
 
        struct ext4_block block_bitmap;
        int rc =
@@ -287,22 +342,67 @@ static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref)
        if (rc != EOK)
                return rc;
 
-       memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
-
-       /* Determine first block and first data block in group */
-       uint32_t first_idx = 0;
+       memset(block_bitmap.data, 0, block_size);
 
-       uint32_t first_data =
-           ext4_balloc_get_first_data_block_in_group(&bg_ref->fs->sb, bg_ref);
-       uint32_t first_data_idx =
-           ext4_fs_baddr2_index_in_group(&bg_ref->fs->sb, first_data);
-
-       /*Set bits from to first block to first data block - 1 to one
-        * (allocated)*/
-       /*TODO: Optimize it*/
-       for (i = first_idx; i < first_data_idx; ++i)
-               ext4_bmap_bit_set(block_bitmap.data, i);
+       bit_max = ext4_sb_is_super_in_bg(&bg_ref->fs->sb, bg_ref->index);
+       if (!ext4_sb_has_feature_incompatible(&bg_ref->fs->sb,
+                                             EXT4_FEATURE_INCOMPAT_META_BG) ||
+                       bg_ref->index < ext4_sb_first_meta_bg(&bg_ref->fs->sb) *
+                       dsc_per_block) {
+               if (bit_max) {
+                       bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
+                                                  bg_ref->index);
+                       bit_max +=
+                               ext4_get16(&bg_ref->fs->sb,
+                                          s_reserved_gdt_blocks);
+               }
+       } else { /* For META_BG_BLOCK_GROUPS */
+               bit_max += ext4_bg_num_gdb(&bg_ref->fs->sb,
+                                          bg_ref->index);
+       }
+        for (bit = 0; bit < bit_max; bit++)
+            ext4_bmap_bit_set(block_bitmap.data, bit);
 
+       if (bg_ref->index == ext4_block_group_cnt(&bg_ref->fs->sb) - 1) {
+               /*
+                * Even though mke2fs always initialize first and last group
+                * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need
+                * to make sure we calculate the right free blocks
+                */
+               group_blocks = (ext4_sb_get_blocks_cnt(&bg_ref->fs->sb) -
+                               ext4_get32(&bg_ref->fs->sb, first_data_block) -
+                               (ext4_get32(&bg_ref->fs->sb, blocks_per_group) *
+                                (ext4_block_group_cnt(&bg_ref->fs->sb) - 1)));
+       } else {
+               group_blocks = ext4_get32(&bg_ref->fs->sb, blocks_per_group);
+       }
+       if (!flex_bg ||
+           ext4_block_in_group(&bg_ref->fs->sb,
+                               bitmap_block_addr, bg_ref->index))
+               ext4_bmap_bit_set(block_bitmap.data,
+                                 bitmap_block_addr - first_group_addr);
+
+       if (!flex_bg ||
+           ext4_block_in_group(&bg_ref->fs->sb,
+                               bitmap_inode_addr, bg_ref->index))
+               ext4_bmap_bit_set(block_bitmap.data,
+                                 bitmap_inode_addr - first_group_addr);
+
+        for (i = inode_table_addr;
+               i < inode_table_addr + inode_table_bcnt; i++) {
+               if (!flex_bg ||
+                   ext4_block_in_group(&bg_ref->fs->sb,
+                                       i,
+                                       bg_ref->index))
+                       ext4_bmap_bit_set(block_bitmap.data,
+                                       i - first_group_addr);
+       }
+        /*
+         * 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_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data);
        block_bitmap.dirty = true;
 
        /* Save bitmap */
index d05b962e755b840ccb87dca3200dd3b2427a3453..b475af7d02be1b63c3e306604d98b1c015af99bf 100644 (file)
@@ -56,7 +56,6 @@
 static inline uint32_t ext4_fs_baddr2_index_in_group(struct ext4_sblock *s,
                                                     uint32_t baddr)
 {
-       ext4_assert(baddr);
        if (ext4_get32(s, first_data_block))
                baddr--;