All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v4 7/7] btrfs: tree-checker: Introduce checks for skinny block group item
Date: Tue,  5 May 2020 07:58:25 +0800	[thread overview]
Message-ID: <20200504235825.4199-8-wqu@suse.com> (raw)
In-Reply-To: <20200504235825.4199-1-wqu@suse.com>

The check is pretty simple, just two checks:
- Tree owner check
  Skinny block group item should only exist in block group tree.

- Used num bytes (key->offset)
  To avoid possible later chunk size change, here we use super large
  value (64T) as threshold to reduce false alert.

- Duplicated skinny block group items
  There shouldn't be duplicated items for the same block group.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/tree-checker.c | 56 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index 517b44300a05..f5eb91b4139f 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -621,11 +621,18 @@ static void block_group_err(const struct extent_buffer *eb, int slot,
 	vaf.fmt = fmt;
 	vaf.va = &args;
 
-	btrfs_crit(fs_info,
-	"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
-		btrfs_header_level(eb) == 0 ? "leaf" : "node",
-		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
-		key.objectid, key.offset, &vaf);
+	if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
+		btrfs_crit(fs_info,
+"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
+			btrfs_header_level(eb) == 0 ? "leaf" : "node",
+			btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
+			key.objectid, key.offset, &vaf);
+	else
+		btrfs_crit(fs_info,
+"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu, %pV",
+			btrfs_header_level(eb) == 0 ? "leaf" : "node",
+			btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
+			key.objectid, &vaf);
 	va_end(args);
 }
 
@@ -698,6 +705,42 @@ static int check_block_group_item(struct extent_buffer *leaf,
 	return 0;
 }
 
+static int check_skinny_block_group_item(struct extent_buffer *leaf,
+					 struct btrfs_key *key,
+					 struct btrfs_key *prev_key, int slot)
+{
+	if (btrfs_header_owner(leaf) != BTRFS_BLOCK_GROUP_TREE_OBJECTID) {
+		block_group_err(leaf, slot,
+	"bad tree owner for skinny block group item, have %llu expect %llu",
+				btrfs_header_owner(leaf),
+				BTRFS_BLOCK_GROUP_TREE_OBJECTID);
+		return -EUCLEAN;
+	}
+	/*
+	 * We can't do direct key->offset check, but at least we shouldn't
+	 * have anything larger than max chunk size.
+	 * Here we use something way larger than that to ensure we only catch
+	 * obviouly wrong result.
+	 */
+	if (key->offset >= SZ_64T) {
+		block_group_err(leaf, slot,
+			"too large used num bytes, have %llu expect [0, %llu)",
+				key->offset, BTRFS_MAX_DATA_CHUNK_SIZE);
+		return -EUCLEAN;
+	}
+
+	/*
+	 * There shouldn't be any duplicated skinny bg items
+	 * (same objectid but different offset)
+	 */
+	if (slot > 0 && prev_key->objectid == key->objectid) {
+		block_group_err(leaf, slot,
+			"duplicated skinny block group items found");
+		return -EUCLEAN;
+	}
+	return 0;
+}
+
 __printf(4, 5)
 __cold
 static void chunk_err(const struct extent_buffer *leaf,
@@ -1511,6 +1554,9 @@ static int check_leaf_item(struct extent_buffer *leaf,
 	case BTRFS_BLOCK_GROUP_ITEM_KEY:
 		ret = check_block_group_item(leaf, key, slot);
 		break;
+	case BTRFS_SKINNY_BLOCK_GROUP_ITEM_KEY:
+		ret = check_skinny_block_group_item(leaf, key, prev_key, slot);
+		break;
 	case BTRFS_CHUNK_ITEM_KEY:
 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
 		ret = check_leaf_chunk_item(leaf, chunk, key, slot);
-- 
2.26.2


  parent reply	other threads:[~2020-05-04 23:58 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-04 23:58 [PATCH v4 0/7] btrfs: Introduce new incompat feature SKINNY_BG_TREE to hugely reduce mount time Qu Wenruo
2020-05-04 23:58 ` [PATCH v4 1/7] btrfs: block-group: Don't set the wrong READA flag for btrfs_read_block_groups() Qu Wenruo
2020-05-04 23:58 ` [PATCH v4 2/7] btrfs: block-group: Refactor how we read one block group item Qu Wenruo
2020-05-04 23:58 ` [PATCH v4 3/7] btrfs: block-group: Refactor how we delete " Qu Wenruo
2020-05-05  8:47   ` Johannes Thumshirn
2020-05-05  8:55     ` Qu Wenruo
2020-05-05  8:56       ` Johannes Thumshirn
2020-05-04 23:58 ` [PATCH v4 4/7] btrfs: block-group: Refactor how we insert a " Qu Wenruo
2020-05-05  8:59   ` Johannes Thumshirn
2020-05-04 23:58 ` [PATCH v4 5/7] btrfs: block-group: Rename write_one_cahce_group() Qu Wenruo
2020-05-05  9:11   ` Johannes Thumshirn
2020-05-11 19:19   ` David Sterba
2020-05-04 23:58 ` [PATCH v4 6/7] btrfs: Introduce new incompat feature, SKINNY_BG_TREE, to hugely reduce mount time Qu Wenruo
2020-05-05 19:10   ` Johannes Thumshirn
2020-05-04 23:58 ` Qu Wenruo [this message]
2020-05-11 19:21 ` [PATCH v4 0/7] btrfs: Introduce new incompat feature SKINNY_BG_TREE " David Sterba

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=20200504235825.4199-8-wqu@suse.com \
    --to=wqu@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.