diff mbox series

[v3,3/3] btrfs-progs: free-space-info tree-checker

Message ID 41fc4441860cc2769fd11c236067ff979547faf7.1728346056.git.loemra.dev@gmail.com (mailing list archive)
State New
Headers show
Series btrfs-progs: mkfs free-space-info bug | expand

Commit Message

Leo Martins Oct. 8, 2024, 12:27 a.m. UTC
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 mbox series

Patch

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)