* [PATCH] ext4: restore group extend counters when the bitmap update fails
@ 2026-12-02 14:32 Alan
2026-12-02 14:50 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Alan @ 2026-12-02 14:32 UTC (permalink / raw)
To: tytso
Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
linux-ext4, linux-kernel, stable
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] ext4: restore group extend counters when the bitmap update fails
2026-12-02 14:32 [PATCH] ext4: restore group extend counters when the bitmap update fails Alan
@ 2026-12-02 14:50 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 14:50 UTC (permalink / raw)
To: Alan; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] ext4: superblock rollback in ext4_group_extend_no_check()
runs after commit
- [Medium] ext4: partial rollback in ext4_group_extend_no_check() after
bitmap update
- [Low] ext4: unlocked o_free_blocks_count read in
ext4_group_extend_no_check()
--
commit 6330aec87a7c1cfd65a1603ed9df385751f61752
Author: Alan <3164729207@qq.com>
ext4: restore group extend counters when the bitmap update fails
This commit restores the superblock block and free block counts in
ext4_group_extend_no_check() if ext4_group_add_blocks() or
ext4_handle_dirty_metadata() fails.
> diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c
> index 5e3ad8393cf83..0a99ce4d1c2ba 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);
[Severity: Low]
Should o_free_blocks_count be sampled under lock_buffer(EXT4_SB(sb)->s_sbh)
when the superblock counters are updated?
Reading ext4_free_blocks_count(es) at function entry happens without the
superblock buffer lock and before ext4_journal_start_sb() and
ext4_journal_get_write_access(), both of which can sleep while a concurrent
ext4_update_super() updates s_free_blocks_count_lo and s_free_blocks_count_hi
under lock_buffer(s_sbh). A rollback could then write back a stale or torn
64-bit free block count.
> 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;
[Severity: Medium]
If ext4_handle_dirty_metadata() fails here (or inside ext4_mb_mark_context()
after clearing the bitmap bits and setting changed > 0), does jumping to
rollback leave the superblock inconsistent with the bitmap and in-memory
allocator state?
By this point ext4_group_add_blocks() has already marked the new blocks free
in the block bitmap, updated the group descriptor and flex_bg free cluster
counts, freed the clusters into the buddy cache via mb_free_blocks(), and
added changed to sbi->s_freeclusters_counter. Reverting only the superblock
counters leaves the buddy allocator able to hand out blocks above the
restored o_blocks_count (which then fail ext4_sb_block_valid()), and the next
ext4_update_super() will overwrite es->s_free_blocks_count from
sbi->s_freeclusters_counter.
> 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);
[Severity: Medium]
Does this rollback run too late when ext4_group_add_blocks() hits an error on
a filesystem mounted with errors=remount-ro or without a journal?
When ext4_group_add_blocks() fails (for example, on a block bitmap read EIO in
ext4_wait_block_bitmap()), it calls ext4_error_err() and ext4_std_error() ->
ext4_handle_error() before returning to ext4_group_extend_no_check():
fs/ext4/super.c:ext4_handle_error() {
...
if (continue_fs && journal &&
!ext4_test_mount_flag(sb, EXT4_MF_JOURNAL_DESTROY))
schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
else
ext4_commit_super(sb);
...
}
When !continue_fs or !journal, ext4_handle_error() synchronously calls
ext4_commit_super(sb) while es->s_blocks_count in s_sbh is still set to
o_blocks_count + add, writing the un-rolled-back counts to disk. For
errors=remount-ro it also sets EXT4_FLAGS_EMERGENCY_RO, which causes
ext4_put_super() to skip ext4_commit_super(sb) on unmount, so the subsequent
in-memory rollback is never written out. Even with errors=continue and a
journal, schedule_work(&EXT4_SB(sb)->s_sb_upd_work) races with the rollback
label.
--
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_D418BD3910B101F7FD61B0E136A583DE8C0A@qq.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 14:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 14:32 [PATCH] ext4: restore group extend counters when the bitmap update fails Alan
2026-12-02 14:50 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox