Skip to contentSkip to conversation

btrfs: remove ASSERT in populate_free_space_tree for empty extent tree btrfs_search_slot_for_read() returns 1 when no items are found

PATCH In review avinashlalotra 2 participants 2 messages Jun 7, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: remove ASSERT in populate_free_space_tree for empty extent tree btrfs_search_slot_for_read() returns 1 when no items are found

but populate_free_space_tree() has ASSERT(ret == 0) which panics on empty extent trees. Empty extent trees are valid (new block groups, after deletions) so remove the assert and handle ret == 1 by skipping the scan loop.

---
 fs/btrfs/free-space-tree.c | 62 ++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 30 deletions(-)
fs/btrfs/free-space-tree.c +32 -30
index 0c573d46639a..beffe52dfa59 100644
@@ -1115,43 +1115,45 @@ static 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_locked;
-	ASSERT(ret == 0);
 
 	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]);
-
-		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
-		    key.type == BTRFS_METADATA_ITEM_KEY) {
-			if (key.objectid >= end)
-				break;
 
-			if (start < key.objectid) {
-				ret = __add_to_free_space_tree(trans,
-							       block_group,
-							       path2, start,
-							       key.objectid -
-							       start);
-				if (ret)
-					goto out_locked;
+	if (ret == 0) {
+		while (1) {
+			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+			if (key.type == BTRFS_EXTENT_ITEM_KEY ||
+			    key.type == BTRFS_METADATA_ITEM_KEY) {
+				if (key.objectid >= end)
+					break;
+
+				if (start < key.objectid) {
+					ret = __add_to_free_space_tree(trans,
+								       block_group,
+								       path2, start,
+								       key.objectid -
+								       start);
+					if (ret)
+						goto out_locked;
+				}
+				start = key.objectid;
+				if (key.type == BTRFS_METADATA_ITEM_KEY)
+					start += trans->fs_info->nodesize;
+				else
+					start += key.offset;
+			} else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
+				if (key.objectid != block_group->start)
+					break;
 			}
-			start = key.objectid;
-			if (key.type == BTRFS_METADATA_ITEM_KEY)
-				start += trans->fs_info->nodesize;
-			else
-				start += key.offset;
-		} else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
-			if (key.objectid != block_group->start)
+
+			ret = btrfs_next_item(extent_root, path);
+			if (ret < 0)
+				goto out_locked;
+			if (ret)
 				break;
 		}
-
-		ret = btrfs_next_item(extent_root, path);
-		if (ret < 0)
-			goto out_locked;
-		if (ret)
-			break;
-	}
+	}	
 	if (start < end) {
 		ret = __add_to_free_space_tree(trans, block_group, path2,
 					       start, end - start);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 3 lines · avinashlalotra

but populate_free_space_tree() has ASSERT(ret == 0) which panics on empty extent trees. Empty extent trees are valid (new block groups, after

No, an empty extent tree is impossible.
There are always some metadata trees and a default subvolume tree, even on empty fs, and therefore there are metadata extents allocated and the extent tree is not empty.

You could have checked that with a fs just created with mkfs.btrfs for example...
So no, that's not the reason why the assertion is being triggered.

I also suggest next time to use a shorter subject instead of this:

"btrfs: remove ASSERT in populate_free_space_tree for empty extent tree btrfs_search_slot_for_read() returns 1 when no items are found"

That has to be the longest subject I have ever seen. There's no need to explain everything in a subject, just summarize.

Quoted 29 lines

deletions) so remove the assert and handle ret == 1 by skipping the scan loop.

---
 fs/btrfs/free-space-tree.c | 62 ++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 30 deletions(-)
fs/btrfs/free-space-tree.c +0 -8
index 0c573d46639a..beffe52dfa59 100644
@@ -1115,43 +1115,45 @@ static 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_locked;
-       ASSERT(ret == 0);
 
        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]);
-
-               if (key.type == BTRFS_EXTENT_ITEM_KEY ||
-                   key.type == BTRFS_METADATA_ITEM_KEY) {
-                       if (key.objectid >= end)
-                               break;
 

fs/btrfs/free-space-tree.c static int populate_free_space_tree(struct btrfs_trans_handle *trans, · line 1123

Quoted 10 lines
fs/btrfs/free-space-tree.c +2 -8
-                       if (start < key.objectid) {
-                               ret = __add_to_free_space_tree(trans,
-                                                              block_group,
-                                                              path2, start,
-                                                              key.objectid -
-                                                              start);
-                               if (ret)
-                                       goto out_locked;
+       if (ret == 0) {
+               while (1) {

There's no need to surround everything with such an if statement and add yet more indentation to the code...

Anyway this was already fixed several days ago at:

https://lore.kernel.org/linux-btrfs/f60761dc5dd7376ac91d0a645bd2c3a59a68cee7.1749153697.git.fdmanana@suse.com/

Thanks.

Quoted 54 lines
fs/btrfs/free-space-tree.c +30 -15
+                       btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+                       if (key.type == BTRFS_EXTENT_ITEM_KEY ||
+                           key.type == BTRFS_METADATA_ITEM_KEY) {
+                               if (key.objectid >= end)
+                                       break;
+
+                               if (start < key.objectid) {
+                                       ret = __add_to_free_space_tree(trans,
+                                                                      block_group,
+                                                                      path2, start,
+                                                                      key.objectid -
+                                                                      start);
+                                       if (ret)
+                                               goto out_locked;
+                               }
+                               start = key.objectid;
+                               if (key.type == BTRFS_METADATA_ITEM_KEY)
+                                       start += trans->fs_info->nodesize;
+                               else
+                                       start += key.offset;
+                       } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
+                               if (key.objectid != block_group->start)
+                                       break;
                        }
-                       start = key.objectid;
-                       if (key.type == BTRFS_METADATA_ITEM_KEY)
-                               start += trans->fs_info->nodesize;
-                       else
-                               start += key.offset;
-               } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
-                       if (key.objectid != block_group->start)
+
+                       ret = btrfs_next_item(extent_root, path);
+                       if (ret < 0)
+                               goto out_locked;
+                       if (ret)
                                break;
                }
-
-               ret = btrfs_next_item(extent_root, path);
-               if (ret < 0)
-                       goto out_locked;
-               if (ret)
-                       break;
-       }
+       }
        if (start < end) {
                ret = __add_to_free_space_tree(trans, block_group, path2,
                                               start, end - start);
--

2.43.0