Linux EXT4 FS development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: Alan <3164729207@qq.com>
Cc: linux-ext4@vger.kernel.org, tytso@mit.edu
Subject: Re: [PATCH] ext4: restore group extend counters when the bitmap update fails
Date: Wed, 02 Dec 2026 14:50:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-246001@kernel.org> (raw)
In-Reply-To: <tencent_D418BD3910B101F7FD61B0E136A583DE8C0A@qq.com>

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

      reply	other threads:[~2026-12-02 14:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-246001@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=3164729207@qq.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox