Skip to contentSkip to conversation

btrfs: replace BUG_ON() with error return in cache_save_setup()

PATCH Superseded Teng Liu 2 participants 2 messages Mar 28 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Teng Liu

author
#0 lore raw

PATCH 1/1 btrfs: replace BUG_ON() with error return in cache_save_setup()

In cache_save_setup(), if create_free_space_inode() succeeds but the subsequent lookup_free_space_inode() still fails on retry, the BUG_ON(retries) will crash the kernel. This can happen due to I/O errors or transient failures, not just programming bugs.

Replace the BUG_ON with proper error handling that returns -EIO through the existing cleanup path. The callers already handle this gracefully:
disk_cache_state defaults to BTRFS_DC_ERROR, so the space cache simply won't be written for that block group.

---
 fs/btrfs/block-group.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +7 -1
index ebf507909..9f71362ba 100644
@@ -3343,7 +3343,13 @@ static int cache_save_setup(struct btrfs_block_group *block_group,
 	}
 
 	if (IS_ERR(inode)) {
-		BUG_ON(retries);
+		if (retries) {
+			btrfs_debug(fs_info,
+				    "free space inode not found after creation for block group %llu",
+				    block_group->start);
+			ret = -EIO;
+			goto out_free;
+		}
 		retries++;
 
 		if (block_group->ro)
Signature · 1 line
2.53.0
#1 lore raw
Quoted 20 lines · Teng Liu

In cache_save_setup(), if create_free_space_inode() succeeds but the subsequent lookup_free_space_inode() still fails on retry, the BUG_ON(retries) will crash the kernel. This can happen due to I/O errors or transient failures, not just programming bugs.

Replace the BUG_ON with proper error handling that returns -EIO through the existing cleanup path. The callers already handle this gracefully:
disk_cache_state defaults to BTRFS_DC_ERROR, so the space cache simply won't be written for that block group.

---
  fs/btrfs/block-group.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +0 -0
index ebf507909..9f71362ba 100644

fs/btrfs/block-group.c static int cache_save_setup(struct btrfs_block_group *block_group, · line 3347

Quoted 6 lines
fs/btrfs/block-group.c +2 -1
@@ -3343,7 +3343,13 @@ static int cache_save_setup(struct btrfs_block_group *block_group,
  	}
 
  	if (IS_ERR(inode)) {
-		BUG_ON(retries);
+		if (retries) {
+			btrfs_debug(fs_info,

You mentioned:

Quoted 2 lines

This can happen due to I/O
errors or transient failures, not just programming bugs.

If so, btrfs_debug() is definitely not noisy enough, as it's disabled by default.

fs/btrfs/block-group.c static int cache_save_setup(struct btrfs_block_group *block_group, · line 3350

Quoted 3 lines
fs/btrfs/block-group.c +3 -0
+				    "free space inode not found after creation for block group %llu",
+				    block_group->start);
+			ret = -EIO;

Furthermore, use the original error code from PTR_ERR(inode) is more meaningful, in case there is other causes of the inode lookup.

And showing the error code inside the error message will help indicating the bug.

Otherwise looks good to me.

Quoted 5 lines
fs/btrfs/block-group.c +2 -0
+			goto out_free;
+		}
  		retries++;
 
  		if (block_group->ro)