diff mbox series

[08/11] btrfs: add a debug range check for header_v2

Message ID 2d74a1a84f8b2ed6c3a6ba594fb75ceba54ede59.1646692474.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs: add snapshot_id to btrfs_header and root_item | expand

Commit Message

Josef Bacik March 7, 2022, 10:36 p.m. UTC
There's a few places where we modify the extent buffer data that I
missed in my initial conversion.  This check helped me catch the places
I missed.  Simply check to see if the eb has FLAG_V2 set and then make
sure we're not overlapping access with the header_v2.  This will go away
in the future, but is handy while I'm working on the code.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/extent_io.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
diff mbox series

Patch

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 31309aba3344..88dc53595192 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -6736,6 +6736,25 @@  static inline int check_eb_range(const struct extent_buffer *eb,
 	if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
 		return report_eb_range(eb, start, len);
 
+#ifdef CONFIG_BTRFS_DEBUG
+	/*
+	 * Catch places where we may have the wrong header math, this can go
+	 * away later.
+	 */
+	if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_V2)) {
+		if (start >= sizeof(struct btrfs_header_v2))
+			return false;
+		if (start == 0 && len == eb->len)
+			return false;
+		if (start + len > sizeof(struct btrfs_header_v2)) {
+			btrfs_warn(eb->fs_info,
+			   "access overlaps the header_v2 on eb %llu start %lu len %lu",
+			   eb->start, start, len);
+			return true;
+		}
+	}
+#endif
+
 	return false;
 }