linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Leo Martins <loemra.dev@gmail.com>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH v3 3/3] btrfs-progs: free-space-info tree-checker
Date: Mon,  7 Oct 2024 17:27:48 -0700	[thread overview]
Message-ID: <41fc4441860cc2769fd11c236067ff979547faf7.1728346056.git.loemra.dev@gmail.com> (raw)
In-Reply-To: <cover.1728346056.git.loemra.dev@gmail.com>

Add a new check in check_leaf_item for btrfs_free_space_info. This check
performs exactly the same check that is performed in btrfs check. That
is it searches for the block group that the current free-space-info
belogns to and warns if there is none. When I was testing I found that
sometimes this would be called before the block group cache was
initialized leading to incorrect warnings so I added a check to make
sure that the cache was initialized.

I also chose to not return an error since this bug does not really affect
the ability of the system to function properly.

I'm still not convinced that this tree-checker is helpful or necessary
so if anyone has any opinions I would love to hear them! If this is
deemed helpful I will send out another patch to add this check to the
kernel tree-checker.

Signed-off-by: Leo Martins <loemra.dev@gmail.com>
---
 kernel-shared/tree-checker.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/kernel-shared/tree-checker.c b/kernel-shared/tree-checker.c
index 0be8f022..cf2e389e 100644
--- a/kernel-shared/tree-checker.c
+++ b/kernel-shared/tree-checker.c
@@ -1737,6 +1737,32 @@ static int check_raid_stripe_extent(const struct extent_buffer *leaf,
 	return 0;
 }
 
+static int check_free_space_info_item(const struct extent_buffer *leaf,
+				      const struct btrfs_key *key, int slot)
+{
+	struct btrfs_fs_info *fs_info = leaf->fs_info;
+	struct btrfs_block_group *bg;
+
+	/* block_group_cache is uninitialized at this point */
+	if (!fs_info->block_group_cache_tree.rb_node)
+		return 0;
+
+	bg = btrfs_lookup_first_block_group(fs_info, key->objectid);
+	if (unlikely(!bg || key->objectid != bg->start ||
+		     key->offset != bg->length)) {
+		generic_err(
+			leaf, slot,
+			"We have a space info key [%llu %u %llu] for a block group that "
+			"doesn't exist.\n"
+			"This is likely due to a minor bug in mkfs.btrfs that doesn't properly\n"
+			"cleanup free spaces and can be fixed using btrfs rescue "
+			"clear-space-cache v2\n",
+			key->objectid, key->type, key->offset);
+	}
+
+	return 0;
+}
+
 /*
  * Common point to switch the item-specific validation.
  */
@@ -1798,6 +1824,9 @@ static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
 	case BTRFS_RAID_STRIPE_KEY:
 		ret = check_raid_stripe_extent(leaf, key, slot);
 		break;
+	case BTRFS_FREE_SPACE_INFO_KEY:
+		ret = check_free_space_info_item(leaf, key, slot);
+		break;
 	}
 
 	if (ret)
-- 
2.43.5


      parent reply	other threads:[~2024-10-08  0:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-08  0:27 [PATCH v3 0/3] btrfs-progs: mkfs free-space-info bug Leo Martins
2024-10-08  0:27 ` [PATCH v3 1/3] btrfs-progs: remove block group free space Leo Martins
2024-10-08  0:27 ` [PATCH v3 2/3] btrfs-progs: check free space maps to block group Leo Martins
2025-07-21  9:15   ` Mark Harmstone
2024-10-08  0:27 ` Leo Martins [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=41fc4441860cc2769fd11c236067ff979547faf7.1728346056.git.loemra.dev@gmail.com \
    --to=loemra.dev@gmail.com \
    --cc=kernel-team@fb.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).