From: Chunguang Xu <brookxu@xxxxxxxxxxx> There is a need to check whether a block or a segment overlaps with metadata, since information of system_zone is incomplete, we need a more accurate function. Now we check whether it overlaps with block bitmap, inode bitmap, and inode table. Perhaps it is better to add a check of super_block and block group descriptor and provide a helper function. Signed-off-by: Chunguang Xu <brookxu@xxxxxxxxxxx> --- fs/ext4/ext4.h | 5 ++++- fs/ext4/mballoc.c | 57 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index bd88b4a..73d59cb 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -2801,7 +2801,10 @@ extern ext4_group_t ext4_mb_prefetch(struct super_block *sb, unsigned int nr, int *cnt); extern void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group, unsigned int nr); - +extern int ext4_metadata_block_overlaps(struct super_block *sb, + ext4_group_t block_group, + ext4_fsblk_t block, + unsigned long count); extern void ext4_free_blocks(handle_t *handle, struct inode *inode, struct buffer_head *bh, ext4_fsblk_t block, unsigned long count, int flags); diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c index 29dfeb04..d8704fe 100644 --- a/fs/ext4/mballoc.c +++ b/fs/ext4/mballoc.c @@ -5061,6 +5061,49 @@ static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi, kmem_cache_free(ext4_free_data_cachep, entry); } +/* + * Returns 1 if the passed-in block region (block, block+count) + * overlaps with some other filesystem metadata blocks. Others, + * return 0. + */ +int ext4_metadata_block_overlaps(struct super_block *sb, + ext4_group_t block_group, + ext4_fsblk_t block, + unsigned long count) +{ + struct ext4_sb_info *sbi = EXT4_SB(sb); + struct ext4_group_desc *gdp; + int gd_first = ext4_group_first_block_no(sb, block_group); + int itable, gd_blk; + int ret = 0; + + gdp = ext4_get_group_desc(sb, block_group, NULL); + // check block bitmap and inode bitmap + if (in_range(ext4_block_bitmap(sb, gdp), block, count) || + in_range(ext4_inode_bitmap(sb, gdp), block, count)) + ret = 1; + + // check inode table + itable = ext4_inode_table(sb, gdp); + if (!(block >= itable + sbi->s_itb_per_group || + block + count - 1 < itable)) + ret = 1; + + /* check super_block and block group descriptor table, the + * reserved space of the block group descriptor is managed + * by resize_inode, it may not be processed now due to + * performance. + */ + gd_blk = ext4_bg_has_super(sb, block_group) + + ext4_bg_num_gdb(sb, block_group); + if (gd_blk) { + if (!(block >= gd_first + gd_blk || + block + count - 1 < gd_first)) + ret = 1; + } + return ret; +} + static noinline_for_stack int ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b, struct ext4_free_data *new_entry) @@ -5360,13 +5403,7 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode, goto error_return; } - if (in_range(ext4_block_bitmap(sb, gdp), block, count) || - in_range(ext4_inode_bitmap(sb, gdp), block, count) || - in_range(block, ext4_inode_table(sb, gdp), - sbi->s_itb_per_group) || - in_range(block + count - 1, ext4_inode_table(sb, gdp), - sbi->s_itb_per_group)) { - + if (ext4_metadata_block_overlaps(sb, block_group, block, count)) { ext4_error(sb, "Freeing blocks in system zone - " "Block = %llu, count = %lu", block, count); /* err = 0. ext4_std_error should be a no op */ @@ -5552,11 +5589,7 @@ int ext4_group_add_blocks(handle_t *handle, struct super_block *sb, goto error_return; } - if (in_range(ext4_block_bitmap(sb, desc), block, count) || - in_range(ext4_inode_bitmap(sb, desc), block, count) || - in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) || - in_range(block + count - 1, ext4_inode_table(sb, desc), - sbi->s_itb_per_group)) { + if (ext4_metadata_block_overlaps(sb, block_group, block, count)) { ext4_error(sb, "Adding blocks in system zones - " "Block = %llu, count = %lu", block, count); -- 1.8.3.1