Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Teng Liu <27rabbitlt@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.com, clm@fb.com, linux-kernel@vger.kernel.org,
	Teng Liu <27rabbitlt@gmail.com>
Subject: [PATCH] btrfs: replace BUG_ON() with error return in cache_save_setup()
Date: Sat, 28 Mar 2026 06:22:00 +0100	[thread overview]
Message-ID: <20260328052200.2838989-1-27rabbitlt@gmail.com> (raw)

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.

Signed-off-by: Teng Liu <27rabbitlt@gmail.com>
---
 fs/btrfs/block-group.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index ebf507909..9f71362ba 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -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)
-- 
2.53.0


             reply	other threads:[~2026-03-28  5:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-28  5:22 Teng Liu [this message]
2026-03-28  5:41 ` [PATCH] btrfs: replace BUG_ON() with error return in cache_save_setup() Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260328052200.2838989-1-27rabbitlt@gmail.com \
    --to=27rabbitlt@gmail.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox