ext4_group_extend_no_check() publishes the new block and free block counts in the superblock before ext4_group_add_blocks() has marked the new blocks free in the block bitmap. If that call fails (for example, because the block bitmap cannot be read), the error path returns without restoring the counts. The modified in-memory superblock is written by ext4_commit_super() on unmount, so the filesystem ends up claiming capacity that the bitmap does not have; e2fsck reports block bitmap differences for the new blocks. The new block count still has to be published before ext4_group_add_blocks() because ext4_sb_block_valid() validates the new range against it. Restore the original block and free block counts if ext4_group_add_blocks() or ext4_handle_dirty_metadata() fails, so a failed resize leaves the filesystem at its old capacity. Tested on Linux 7.4-rc3 (d135ab453a7400) with QEMU blkdebug: a guest-visible EIO on the group 1 block bitmap made EXT4_IOC_GROUP_EXTEND return EIO. Before the change, a normal unmount left a superblock block count of 16000 and e2fsck reported block bitmap differences for the new blocks. After the change, the block count is restored to 12000 and e2fsck -fn returns 0 with no block bitmap differences. The no-fault control extends the filesystem to 16000 and passes e2fsck -fn. Fixes: 18e3143848f1 ("ext4: add a function which extends a group without checking parameters") Cc: stable@vger.kernel.org Signed-off-by: Alan <3164729207@qq.com> --- fs/ext4/resize.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c index 5e3ad8393cf833..0a99ce4d1c2baf 100644 --- a/fs/ext4/resize.c +++ b/fs/ext4/resize.c @@ -1774,6 +1774,7 @@ static int ext4_group_extend_no_check(struct super_block *sb, ext4_fsblk_t o_blocks_count, ext4_grpblk_t add) { struct ext4_super_block *es = EXT4_SB(sb)->s_es; + ext4_fsblk_t o_free_blocks_count = ext4_free_blocks_count(es); handle_t *handle; int err = 0, err2; @@ -1805,10 +1806,20 @@ static int ext4_group_extend_no_check(struct super_block *sb, /* We add the blocks to the bitmap and set the group need init bit */ err = ext4_group_add_blocks(handle, sb, o_blocks_count, add); if (err) - goto errout; - ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); + goto rollback; + err = ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh); + if (err) + goto rollback; ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, o_blocks_count + add); + goto errout; + +rollback: + lock_buffer(EXT4_SB(sb)->s_sbh); + ext4_blocks_count_set(es, o_blocks_count); + ext4_free_blocks_count_set(es, o_free_blocks_count); + ext4_superblock_csum_set(sb); + unlock_buffer(EXT4_SB(sb)->s_sbh); errout: err2 = ext4_journal_stop(handle); if (err2 && !err) base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e -- 2.53.0