diff mbox series

[v3,2/3] btrfs-progs: check free space maps to block group

Message ID fe95778b1f5e29c035720ca7c02946fde25dcbb2.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
Check that the block-group that is found matches the objectid and offset
of the free-space-info. Without this the check only verifies that there
is some block-group that exists with objectid >= free-space-info's
objectid.

I have softened the language of the warning and included instructions on
how to fix the problem. This can be done in a couple of ways:
- btrfs check --repair
- btrfs rescue clear-space-cache v2

I chose to include btrfs rescue as it is more targeted.

Signed-off-by: Leo Martins <loemra.dev@gmail.com>
---
CHANGELOG:
v3:
- softened the warning and added instructions
---
 common/clear-cache.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/common/clear-cache.c b/common/clear-cache.c
index 6493866d..6b362f64 100644
--- a/common/clear-cache.c
+++ b/common/clear-cache.c
@@ -165,9 +165,16 @@  static int check_free_space_tree(struct btrfs_root *root)
 		}
 
 		bg = btrfs_lookup_first_block_group(fs_info, key.objectid);
-		if (!bg) {
+		if (!bg || key.objectid != bg->start ||
+		    key.offset != bg->length) {
 			fprintf(stderr,
-		"We have a space info key for a block group that doesn't exist\n");
+				"We have a space info key [%llu %u %llu] for a block group that "
+				"doesn't exist.\n",
+				key.objectid, key.type, key.offset);
+			fprintf(stderr,
+				"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");
 			ret = -EINVAL;
 			goto out;
 		}