public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Mark Harmstone <mark@harmstone.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs: add remap-tree checks to check_block_group_item()
Date: Mon, 23 Mar 2026 20:43:48 +0100	[thread overview]
Message-ID: <20260323194348.GS5735@twin.jikos.cz> (raw)
In-Reply-To: <20260323172619.152258-1-mark@harmstone.com>

On Mon, Mar 23, 2026 at 05:26:13PM +0000, Mark Harmstone wrote:
> Add some write-time checks for block group items relating to the remap
> tree.
> 
> Here we're checking:
> * That the REMAPPED or METADATA_REMAP flags aren't set unless the
>   REMAP_TREE incompat flag is also set
> * That `remap_bytes` isn't more than the size of the block group
> * That `identity_remap_count` isn't more than the number of sectors in
>   the block group
> 
> Signed-off-by: Mark Harmstone <mark@harmstone.com>
> ---
>  fs/btrfs/tree-checker.c | 42 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 42 insertions(+)
> 
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index 495d1dd61e66c2..cad23f3fadfd34 100644
> --- a/fs/btrfs/tree-checker.c
> +++ b/fs/btrfs/tree-checker.c
> @@ -777,6 +777,48 @@ static int check_block_group_item(struct extent_buffer *leaf,
>  			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
>  		return -EUCLEAN;
>  	}
> +
> +	if (unlikely(!btrfs_fs_incompat(fs_info, REMAP_TREE) &&
> +		     type == BTRFS_BLOCK_GROUP_METADATA_REMAP)) {
> +		block_group_err(leaf, slot,
> +"invalid type, METADATA_REMAP set but REMAP_TREE incompat flag not set");
> +		return -EUCLEAN;
> +	}
> +
> +	if (unlikely(!btrfs_fs_incompat(fs_info, REMAP_TREE) &&
> +		     flags & BTRFS_BLOCK_GROUP_REMAPPED)) {
> +		block_group_err(leaf, slot,
> +"invalid flags, REMAPPED set but REMAP_TREE incompat flag not set");
> +		return -EUCLEAN;
> +	}
> +
> +	if (item_size == sizeof(struct btrfs_block_group_item_v2)) {
> +		struct btrfs_block_group_item_v2 *bgi2;
> +		u64 remap_bytes;
> +		u32 identity_remap_count;
> +
> +		bgi2 = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item_v2);
> +
> +		remap_bytes = btrfs_block_group_v2_remap_bytes(leaf, bgi2);
> +
> +		if (unlikely(remap_bytes > key->offset)) {
> +			block_group_err(leaf, slot,
> +				"invalid remap_bytes, have %llu expect [0, %llu]",
> +					remap_bytes, key->offset);
> +			return -EUCLEAN;
> +		}
> +
> +		identity_remap_count = btrfs_block_group_v2_identity_remap_count(leaf, bgi2);
> +		if (unlikely((u64)identity_remap_count >
> +			     key->offset / fs_info->sectorsize)) {

64bit / 32bit may not work on all arches, we have the sectorize_bits to
avoid it.

> +			block_group_err(leaf, slot,
> +				"invalid identity_remap_count, have %u expect [0, %llu]",
> +					identity_remap_count,
> +					key->offset / fs_info->sectorsize);
> +			return -EUCLEAN;
> +		}
> +	}
> +
>  	return 0;
>  }
>  
> -- 
> 2.52.0
> 

  reply	other threads:[~2026-03-23 19:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-23 17:26 [PATCH] btrfs: add remap-tree checks to check_block_group_item() Mark Harmstone
2026-03-23 19:43 ` David Sterba [this message]
2026-03-25 12:54   ` Mark Harmstone

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=20260323194348.GS5735@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=mark@harmstone.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