From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Dongjiang Zhu <zhudongjiang@fnnas.com>, linux-btrfs@vger.kernel.org
Cc: dsterba@suse.com
Subject: Re: [PATCH 1/3] btrfs: avoid NULL pointer dereference when block group tree root is missing
Date: Sat, 11 Jul 2026 15:50:02 +0930 [thread overview]
Message-ID: <2fc07620-861f-4bb8-ad36-71128ec1ab09@gmx.com> (raw)
In-Reply-To: <20260711051909.1313584-2-zhudongjiang@fnnas.com>
在 2026/7/11 14:49, Dongjiang Zhu 写道:
> [BUG]
> For a filesystem using the block group tree feature, corrupting the block
> group tree root and mounting with rescue=ibadroots triggers the following
> NULL pointer dereference:
>
> BTRFS warning (device loop0 state E): checksum verify failed on logical 30654464 mirror 1 wanted 0x0f55b851 found 0xd7efdde7 level 0
> BTRFS warning (device loop0 state E): checksum verify failed on logical 30654464 mirror 2 wanted 0x0f55b851 found 0xd7efdde7 level 0
> BUG: kernel NULL pointer dereference, address: 0000000000000100
> #PF: supervisor read access in kernel mode
> #PF: error_code(0x0000) - not-present page
> Oops: Oops: 0000 [#1] SMP NOPTI
> RIP: 0010:btrfs_update_global_block_rsv+0x9d/0x1c0 [btrfs]
The problem is why we need to use global block rsv in the first place?
The whole fs is never going to be mounted RW as long as "rescue=" mount
option is utilized.
In the case of a rescue mount, we should just fill in a dummy block rsv,
with block_rsv->full set.
This will also fix the next bug you're fixing.
> Call Trace:
> fill_dummy_bgs+0xd4/0x120 [btrfs]
> open_ctree+0xc6e/0x1ca0 [btrfs]
> btrfs_get_tree+0x50d/0xa40 [btrfs]
>
> [CAUSE]
> With rescue=ibadroots, btrfs_read_roots() ignores a failure to read the
> block group tree root and leaves fs_info->block_group_root NULL.
>
> btrfs_read_block_groups() then falls back to fill_dummy_bgs(), which
> initializes the global block reserve. btrfs_update_global_block_rsv()
> checks only the BLOCK_GROUP_TREE feature bit before dereferencing
> fs_info->block_group_root.
>
> [FIX]
> Check that the block group tree root was successfully loaded before
> accounting it in the global block reserve. This does not affect regular
> mounts, which fail earlier if the root cannot be loaded.
>
> Also update the comment in btrfs_read_block_groups() to cover both the
> extent tree root and the block group tree root being missing.
>
> Fixes: 8dbfc14fc736 ("btrfs: account block group tree when calculating global reserve size")
> Signed-off-by: Dongjiang Zhu <zhudongjiang@fnnas.com>
> ---
> fs/btrfs/block-group.c | 7 ++++---
> fs/btrfs/block-rsv.c | 3 ++-
> 2 files changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
> index fe5de4928f6f..9d551372f37f 100644
> --- a/fs/btrfs/block-group.c
> +++ b/fs/btrfs/block-group.c
> @@ -2673,9 +2673,10 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
> u64 cache_gen;
>
> /*
> - * Either no extent root (with ibadroots rescue option) or we have
> - * unsupported RO options. The fs can never be mounted read-write, so no
> - * need to waste time searching block group items.
> + * Either the extent tree root or the block group tree root is missing
> + * (with the ibadroots rescue option), or we have unsupported RO options.
> + * The fs can never be mounted read-write, so no need to waste time
> + * searching block group items.
> *
> * This also allows new extent tree related changes to be RO compat,
> * no need for a full incompat flag.
> diff --git a/fs/btrfs/block-rsv.c b/fs/btrfs/block-rsv.c
> index 9efb3016ef11..647a065356f4 100644
> --- a/fs/btrfs/block-rsv.c
> +++ b/fs/btrfs/block-rsv.c
> @@ -347,7 +347,8 @@ void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info)
> }
> read_unlock(&fs_info->global_root_lock);
>
> - if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) {
> + if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
> + fs_info->block_group_root) {
> num_bytes += btrfs_root_used(&fs_info->block_group_root->root_item);
> min_items++;
> }
next prev parent reply other threads:[~2026-07-11 6:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-11 5:19 [PATCH 0/3] btrfs: handle missing block group and raid stripe tree roots Dongjiang Zhu
2026-07-11 5:19 ` [PATCH 1/3] btrfs: avoid NULL pointer dereference when block group tree root is missing Dongjiang Zhu
2026-07-11 6:20 ` Qu Wenruo [this message]
2026-07-11 9:52 ` Dongjiang Zhu
2026-07-11 10:17 ` Qu Wenruo
2026-07-11 10:33 ` Dongjiang Zhu
2026-07-11 5:19 ` [PATCH 2/3] btrfs: avoid NULL pointer dereference when raid stripe " Dongjiang Zhu
2026-07-11 5:19 ` [PATCH 3/3] btrfs: report missing raid stripe tree root during lookup Dongjiang Zhu
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=2fc07620-861f-4bb8-ad36-71128ec1ab09@gmx.com \
--to=quwenruo.btrfs@gmx.com \
--cc=dsterba@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=zhudongjiang@fnnas.com \
/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