Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v3 3/4] btrfs: dev-replace: fix the error handling in btrfs_dev_replace_finishing()
Date: Mon, 24 Aug 2026 14:04:07 +0930	[thread overview]
Message-ID: <d1e76ae13f52a03bb4625d8b8034f443e27f8919.1787545216.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1787545216.git.wqu@suse.com>

In btrfs_dev_replace_finishing(), we have the following error paths that
do not do any cleanup:

- btrfs_start_delalloc_roots() error
- btrfs_start_transaction() error

Both will error out directly, leaving the existing dev_replace
untouched, meaning the dev-replace will still be treated as running,
blocking future dev_replace.

Fix the problem by letting the above errors to go to a new out label,
which will do the common dev_replace cleanup, with an error message
showing that dev-replace has failed.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/dev-replace.c | 39 +++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index be4031a6ce1d..df048cf69621 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -893,8 +893,9 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
 	 */
 	ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
 	if (ret) {
-		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
-		return ret;
+		mutex_lock(&fs_devices->device_list_mutex);
+		mutex_lock(&fs_info->chunk_mutex);
+		goto out;
 	}
 	btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
 
@@ -906,8 +907,10 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
 	while (1) {
 		trans = btrfs_start_transaction(root, 0);
 		if (IS_ERR(trans)) {
-			mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
-			return PTR_ERR(trans);
+			ret = PTR_ERR(trans);
+			mutex_lock(&fs_devices->device_list_mutex);
+			mutex_lock(&fs_info->chunk_mutex);
+			goto out;
 		}
 		ret = btrfs_commit_transaction(trans);
 		WARN_ON(ret);
@@ -925,10 +928,8 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
 		}
 	}
 
+out:
 	down_write(&dev_replace->rwsem);
-	dev_replace->replace_state =
-		scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
-			  : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
 	dev_replace->tgtdev = NULL;
 	dev_replace->srcdev = NULL;
 	dev_replace->time_stopped = ktime_get_real_seconds();
@@ -938,14 +939,17 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
 	 * Update allocation state in the new device and replace the old device
 	 * with the new one in the mapping tree.
 	 */
-	if (!scrub_ret) {
-		scrub_ret = btrfs_set_target_alloc_state(src_device, tgt_device);
-		if (scrub_ret)
-			goto error;
-		btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
-								src_device,
-								tgt_device);
-	} else {
+	if (!scrub_ret && !ret) {
+		ret = btrfs_set_target_alloc_state(src_device, tgt_device);
+		if (!ret)
+			btrfs_dev_replace_update_device_in_mapping_tree(fs_info,
+									src_device,
+									tgt_device);
+	}
+	dev_replace->replace_state =
+		(scrub_ret || ret) ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
+				   : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
+	if (scrub_ret || ret) {
 		if (scrub_ret == -ECANCELED)
 			btrfs_info(fs_info,
 				"dev_replace from %s (devid %llu) to %s canceled",
@@ -956,8 +960,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
 				 "dev_replace from %s (devid %llu) to %s failed %d",
 				 btrfs_dev_name(src_device),
 				 src_device->devid,
-				 btrfs_dev_name(tgt_device), scrub_ret);
-error:
+				 btrfs_dev_name(tgt_device), scrub_ret ? scrub_ret : ret);
 		up_write(&dev_replace->rwsem);
 		mutex_unlock(&fs_info->chunk_mutex);
 		mutex_unlock(&fs_devices->device_list_mutex);
@@ -967,7 +970,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
 		btrfs_rm_dev_replace_unblocked(fs_info);
 		mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
 
-		return scrub_ret;
+		return scrub_ret ? scrub_ret : ret;
 	}
 
 	btrfs_info(fs_info,
-- 
2.55.0


  parent reply	other threads:[~2026-08-24  4:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-24  4:34 [PATCH v3 0/4] btrfs: dev-replace: replace_task related cleanup Qu Wenruo
2026-08-24  4:34 ` [PATCH v3 1/4] btrfs: dev-replace: fix the incorrect error message Qu Wenruo
2026-08-24  4:34 ` [PATCH v3 2/4] btrfs: dev-replace: concentrate all messages into btrfs_dev_replace_finishing() Qu Wenruo
2026-08-24  4:34 ` Qu Wenruo [this message]
2026-08-24 17:11   ` [PATCH v3 3/4] btrfs: dev-replace: fix the error handling in btrfs_dev_replace_finishing() David Sterba
2026-08-24  4:34 ` [PATCH v3 4/4] btrfs: dev-replace: properly cleanup replace_task 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=d1e76ae13f52a03bb4625d8b8034f443e27f8919.1787545216.git.wqu@suse.com \
    --to=wqu@suse.com \
    --cc=linux-btrfs@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