+ 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);