public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] btrfs: add remap-tree checks to check_block_group_item()
@ 2026-03-25 12:53 Mark Harmstone
  2026-03-26 14:16 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Harmstone @ 2026-03-25 12:53 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Mark Harmstone

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>
---
The original version used a 64-bit division, which is not implemented on
32-bit arches - this changes it to a right shift instead.

 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 6117381e4d57f7..75c4b9eedaf9f0 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_bits)) {
+			block_group_err(leaf, slot,
+				"invalid identity_remap_count, have %u expect [0, %llu]",
+					identity_remap_count,
+					key->offset >> fs_info->sectorsize_bits);
+			return -EUCLEAN;
+		}
+	}
+
 	return 0;
 }
 
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] btrfs: add remap-tree checks to check_block_group_item()
  2026-03-25 12:53 [PATCH v2] btrfs: add remap-tree checks to check_block_group_item() Mark Harmstone
@ 2026-03-26 14:16 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2026-03-26 14:16 UTC (permalink / raw)
  To: Mark Harmstone; +Cc: linux-btrfs

For tree-checker please add prefix "btrfs: tree-checker: ..."

On Wed, Mar 25, 2026 at 12:53:43PM +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>

Added to for-next, thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-03-26 14:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-25 12:53 [PATCH v2] btrfs: add remap-tree checks to check_block_group_item() Mark Harmstone
2026-03-26 14:16 ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox