From c7a83632760ff735e87d9d355616898b0ff01095 Mon Sep 17 00:00:00 2001 From: Josef Bacik Date: Mon, 7 Mar 2022 17:10:50 -0500 Subject: [PATCH] btrfs-progs: handle no bg item in extent tree for free space tree We have an ASSERT(ret == 0) when populating the free space tree as we should at least find the block group item with extent tree v1. However with v2 we no longer have the block group item in the extent tree, so fix the population logic to handle an empty block group (which occurs during mkfs) and only assert if ret != 0 and we don't have extent tree v2 turned on. Signed-off-by: Josef Bacik Signed-off-by: David Sterba --- kernel-shared/free-space-tree.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/kernel-shared/free-space-tree.c b/kernel-shared/free-space-tree.c index 0fdf5004..896bd3a2 100644 --- a/kernel-shared/free-space-tree.c +++ b/kernel-shared/free-space-tree.c @@ -1057,6 +1057,9 @@ int populate_free_space_tree(struct btrfs_trans_handle *trans, if (ret) goto out; + start = block_group->start; + end = block_group->start + block_group->length; + /* * Iterate through all of the extent and metadata items in this block * group, adding the free space between them and the free space at the @@ -1071,10 +1074,11 @@ int populate_free_space_tree(struct btrfs_trans_handle *trans, ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0); if (ret < 0) goto out; - ASSERT(ret == 0); + if (ret > 0) { + ASSERT(btrfs_fs_incompat(trans->fs_info, EXTENT_TREE_V2)); + goto done; + } - start = block_group->start; - end = block_group->start + block_group->length; while (1) { btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); @@ -1106,6 +1110,7 @@ int populate_free_space_tree(struct btrfs_trans_handle *trans, if (ret) break; } +done: if (start < end) { ret = __add_to_free_space_tree(trans, block_group, path2, start, end - start);