ref: 94d428f1fb88c5c45eba3fd586fa73e5d346b9e0
parent: 90ed580cfaf169c57bc1a3e2117a6f72184b36ec
author: ngkaho1234 <[email protected]>
date: Thu Oct 1 15:10:57 EDT 2015
FIX: block bitmap is not correctly initialized.
--- a/lwext4/ext4_balloc.c
+++ b/lwext4/ext4_balloc.c
@@ -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 @@
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);
+ uint32_t baddr = 0;
+ if (ext4_get32(s, first_data_block))
+ baddr++;
- 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++;
-
- 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 @@
}
/* 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 @@
}
/* 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);
--- a/lwext4/ext4_balloc.h
+++ b/lwext4/ext4_balloc.h
@@ -48,13 +48,21 @@
#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
--- a/lwext4/ext4_config.h
+++ b/lwext4/ext4_config.h
@@ -114,7 +114,7 @@
/**@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*/
--- a/lwext4/ext4_fs.c
+++ b/lwext4/ext4_fs.c
@@ -271,6 +271,40 @@
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
@@ -277,10 +311,31 @@
*/
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 =
ext4_block_get(bg_ref->fs->bdev, &block_bitmap, bitmap_block_addr);
@@ -287,22 +342,67 @@
if (rc != EOK)
return rc;
- memset(block_bitmap.data, 0, ext4_sb_get_block_size(&bg_ref->fs->sb));
+ memset(block_bitmap.data, 0, block_size);
- /* Determine first block and first data block in group */
- uint32_t first_idx = 0;
+ 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);
- 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);
+ 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);
- /*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);
+ 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 */
--- a/lwext4/ext4_fs.h
+++ b/lwext4/ext4_fs.h
@@ -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--;