From: David Sterba <dsterba@suse.cz>
To: ZhengYuan Huang <gality369@gmail.com>
Cc: dsterba@suse.com, clm@fb.com, wqu@suse.com, osandov@fb.com,
linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
baijiaju1990@gmail.com, r33s3n6@gmail.com, zzzccc427@gmail.com
Subject: Re: [PATCH v2] btrfs: tree-checker: introduce checks for FREE_SPACE_BITMAP
Date: Fri, 13 Mar 2026 20:42:47 +0100 [thread overview]
Message-ID: <20260313194247.GK5735@twin.jikos.cz> (raw)
In-Reply-To: <20260310105606.2134142-1-gality369@gmail.com>
On Tue, Mar 10, 2026 at 06:56:06PM +0800, ZhengYuan Huang wrote:
> Introduce checks for FREE_SPACE_BITMAP item, which include:
>
> - Key alignment check
> Same as FREE_SPACE_EXTENT, the objectid is the logical bytenr of the
> free space, and offset is the length of the free space, so both
> should be aligned to the fs block size.
>
> - Non-zero range check
> A zero key->offset would describe an empty bitmap, which is invalid.
>
> - Item size check
> The item must hold exactly DIV_ROUND_UP(key->offset >> sectorsize_bits,
> BITS_PER_BYTE) bytes. A mismatch indicates a truncated or otherwise
> corrupt bitmap item; without this check, the bitmap loading path would
> walk past the end of the leaf and trigger a NULL dereference in
> assert_eb_folio_uptodate().
>
> Signed-off-by: ZhengYuan Huang <gality369@gmail.com>
> ---
> [CHANGELOG]
> v2:
> - Move the FREE_SPACE_BITMAP item size validation from
> load_free_space_bitmaps() in free-space-tree.c into tree-checker, so
> corrupt bitmap items are rejected when the leaf is read from disk.
> - Drop the extent_buffer_test_bit() range check added in v1.
> - Rework the fix to follow Qu Wenruo's suggested tree-checker based
> validation.
> ---
> fs/btrfs/tree-checker.c | 39 +++++++++++++++++++++++++++++++++++++++
> 1 file changed, 39 insertions(+)
>
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index c10b4c242acf..0f12fe462b6c 100644
> --- a/fs/btrfs/tree-checker.c
> +++ b/fs/btrfs/tree-checker.c
> @@ -1901,6 +1901,42 @@ static int check_dev_extent_item(const struct extent_buffer *leaf,
> return 0;
> }
>
> +static int check_free_space_bitmap(struct extent_buffer *leaf,
> + struct btrfs_key *key, int slot)
> +{
> + struct btrfs_fs_info *fs_info = leaf->fs_info;
> + const u32 blocksize = fs_info->sectorsize;
> + u32 expected_item_size;
> +
> + if (unlikely(!IS_ALIGNED(key->objectid, blocksize) ||
> + !IS_ALIGNED(key->offset, blocksize))) {
> + generic_err(leaf, slot,
> + "free space bitmap key range is not aligned to %u, has (%llu %u %llu)",
We have a format specifier macro for keys, KEY_FMT, I'll fix it in git.
> + blocksize, key->objectid, key->type, key->offset);
prev parent reply other threads:[~2026-03-13 19:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-10 10:56 [PATCH v2] btrfs: tree-checker: introduce checks for FREE_SPACE_BITMAP ZhengYuan Huang
2026-03-10 22:01 ` Qu Wenruo
2026-03-13 19:42 ` David Sterba [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=20260313194247.GK5735@twin.jikos.cz \
--to=dsterba@suse.cz \
--cc=baijiaju1990@gmail.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=gality369@gmail.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=osandov@fb.com \
--cc=r33s3n6@gmail.com \
--cc=wqu@suse.com \
--cc=zzzccc427@gmail.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