]> git.hungrycats.org Git - linux/commitdiff
btrfs: dev-replace: report the progress of a canceled replace
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 16 Oct 2026 08:07:34 +0000 (04:07 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:17:14 +0000 (21:17 -0500)
btrfs_dev_replace_progress() returns 0 for a canceled replace, so
"btrfs replace status" says it was canceled "at 0.0%" however far it
got.  The progress cannot be computed after the fact: it is derived from
cursor_left and the size of the source device, and the source device is
forgotten when the replace stops.

Remember the progress when a running replace fails or is canceled
(btrfs_dev_replace_finishing()) and when a suspended replace is canceled
(btrfs_dev_replace_cancel()), and report it for the CANCELED state.  The
value is kept in memory only; a canceled replace loaded from disk after a
remount still reports 0.  It is as coarse as the progress of a running
replace: scrub_enumerate_chunks() moves cursor_left to the end of a chunk
only when the copy of that chunk is complete, so a replace that stops
part way through a chunk is reported at the start of that chunk.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/dev-replace.c
fs/btrfs/fs.h

index b19e6536ac3273d1e7346ec61b1cdbe408922a47..cf774f9114d5c21e4167ddbaf91baccc70999cb7 100644 (file)
@@ -65,6 +65,7 @@
 
 static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
                                       int scrub_ret);
+static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info);
 static int btrfs_dev_replace_kthread(void *data);
 
 int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
@@ -940,6 +941,9 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
        }
 
        down_write(&dev_replace->rwsem);
+       if (scrub_ret)
+               dev_replace->canceled_progress_1000 =
+                       btrfs_dev_replace_progress(fs_info);
        dev_replace->replace_state =
                scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
                          : BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED;
@@ -1083,9 +1087,11 @@ static u64 btrfs_dev_replace_progress(struct btrfs_fs_info *fs_info)
 
        switch (dev_replace->replace_state) {
        case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
-       case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
                ret = 0;
                break;
+       case BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED:
+               ret = dev_replace->canceled_progress_1000;
+               break;
        case BTRFS_IOCTL_DEV_REPLACE_STATE_FINISHED:
                ret = 1000;
                break;
@@ -1169,6 +1175,8 @@ int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)
                result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
                tgt_device = dev_replace->tgtdev;
                src_device = dev_replace->srcdev;
+               dev_replace->canceled_progress_1000 =
+                       btrfs_dev_replace_progress(fs_info);
                dev_replace->tgtdev = NULL;
                dev_replace->srcdev = NULL;
                dev_replace->replace_state =
index 2dd167c32d612f9ccdd836140288fe1e1206f27f..a3b0edf4025cee4a592316eb39746582602069c8 100644 (file)
@@ -374,6 +374,8 @@ struct btrfs_dev_replace {
        u64 committed_cursor_left;
        u64 cursor_left_last_write_of_item;
        u64 cursor_right;
+       /* Progress (0..1000) when the replace was canceled, in memory only */
+       u64 canceled_progress_1000;
 
        /* See #define above */
        u64 cont_reading_from_srcdev_mode;