]> git.hungrycats.org Git - linux/commitdiff
btrfs: dev-replace: optionally abort on uncorrectable read errors
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 16 Oct 2026 17:02:21 +0000 (13:02 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:17:14 +0000 (21:17 -0500)
When a sector that is in use cannot be read from any copy, device
replace counts it and goes on, leaving the sector unwritten on the
target.  That is what a user rescuing data from a failing device wants:
copy everything that can still be read, as fast as possible, and find
the damage afterwards with the checksums.

A user replacing a healthy device, e.g. with a larger one, wants the
opposite.  An uncorrectable sector means the array has already lost data
it was expected to keep, and the user wants to stop and find out why
before the source device is removed for good.
Today such a user has to add the new device and remove the old one
instead, because relocation stops at the first unreadable block, at the
cost of a much slower relocation.

Add BTRFS_IOCTL_DEV_REPLACE_ABORT_ON_UNCORRECTABLE.  When it is set,
replace fails with -EIO and is canceled, like it is on write errors to
the target, at the end of the block group where the first uncorrectable
read error was counted.  The source device stays in the filesystem.

The flag goes in the high 32 bits of cont_reading_from_srcdev_mode,
which has only ever held 0 or 1.  Kernels without this patch reject any
other value with -EINVAL, so a user asking to abort cannot get a replace
that silently does not.  Unknown flags now get -EOPNOTSUPP.  The same
bit is stored in the dev_replace item next to the mode, so a replace
resumed after a remount still aborts.  An older kernel resuming such a
replace ignores the flag, and since the mode then no longer compares
equal to BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID, it
also reads from the source device again, as if -r was not given.

This depends on the counting of uncorrectable read errors added by
"btrfs: dev-replace: count uncorrectable read errors".

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/dev-replace.c
fs/btrfs/fs.h
fs/btrfs/scrub.c
include/uapi/linux/btrfs.h
include/uapi/linux/btrfs_tree.h

index cf774f9114d5c21e4167ddbaf91baccc70999cb7..c3e88f9729a66c4362f3ed850be8bd194caa4427 100644 (file)
@@ -81,6 +81,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
        int item_size;
        struct btrfs_dev_replace_item *ptr;
        u64 src_devid;
+       u64 mode;
 
        if (!dev_root)
                return 0;
@@ -108,6 +109,7 @@ no_valid_dev_replace_entry_found:
                        BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED;
                dev_replace->cont_reading_from_srcdev_mode =
                    BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
+               dev_replace->flags = 0;
                dev_replace->time_started = 0;
                dev_replace->time_stopped = 0;
                atomic64_set(&dev_replace->num_write_errors, 0);
@@ -134,8 +136,10 @@ no_valid_dev_replace_entry_found:
        }
 
        src_devid = btrfs_dev_replace_src_devid(eb, ptr);
+       mode = btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
        dev_replace->cont_reading_from_srcdev_mode =
-               btrfs_dev_replace_cont_reading_from_srcdev_mode(eb, ptr);
+               mode & BTRFS_DEV_REPLACE_ITEM_MODE_MASK;
+       dev_replace->flags = mode & BTRFS_DEV_REPLACE_ITEM_ABORT_ON_UNCORRECTABLE;
        dev_replace->replace_state = btrfs_dev_replace_replace_state(eb, ptr);
        dev_replace->time_started = btrfs_dev_replace_time_started(eb, ptr);
        dev_replace->time_stopped =
@@ -421,7 +425,7 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
        else
                btrfs_set_dev_replace_src_devid(eb, ptr, (u64)-1);
        btrfs_set_dev_replace_cont_reading_from_srcdev_mode(eb, ptr,
-               dev_replace->cont_reading_from_srcdev_mode);
+               dev_replace->cont_reading_from_srcdev_mode | dev_replace->flags);
        btrfs_set_dev_replace_replace_state(eb, ptr,
                dev_replace->replace_state);
        btrfs_set_dev_replace_time_started(eb, ptr, dev_replace->time_started);
@@ -591,7 +595,7 @@ bool btrfs_finish_block_group_to_copy(struct btrfs_device *srcdev,
 
 static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
                const char *tgtdev_name, u64 srcdevid, const char *srcdev_name,
-               int read_src)
+               int read_src, u64 flags)
 {
        struct btrfs_root *root = fs_info->dev_root;
        struct btrfs_trans_handle *trans;
@@ -659,14 +663,17 @@ static int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info,
        }
 
        dev_replace->cont_reading_from_srcdev_mode = read_src;
+       dev_replace->flags = flags;
        dev_replace->srcdev = src_device;
        dev_replace->tgtdev = tgt_device;
 
        btrfs_info(fs_info,
-                     "dev_replace from %s (devid %llu) to %s started",
+                     "dev_replace from %s (devid %llu) to %s started%s",
                      btrfs_dev_name(src_device),
                      src_device->devid,
-                     btrfs_dev_name(tgt_device));
+                     btrfs_dev_name(tgt_device),
+                     (flags & BTRFS_DEV_REPLACE_ITEM_ABORT_ON_UNCORRECTABLE) ?
+                     ", aborting on uncorrectable read errors" : "");
 
        /*
         * from now on, the writes to the srcdev are all duplicated to
@@ -749,9 +756,19 @@ static int btrfs_check_replace_dev_names(struct btrfs_ioctl_dev_replace_args *ar
 int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
                            struct btrfs_ioctl_dev_replace_args *args)
 {
+       const u64 mode = args->start.cont_reading_from_srcdev_mode &
+                        BTRFS_IOCTL_DEV_REPLACE_MODE_MASK;
+       const u64 flags = args->start.cont_reading_from_srcdev_mode &
+                         ~BTRFS_IOCTL_DEV_REPLACE_MODE_MASK;
        int ret;
 
-       switch (args->start.cont_reading_from_srcdev_mode) {
+       static_assert(BTRFS_IOCTL_DEV_REPLACE_ABORT_ON_UNCORRECTABLE ==
+                     BTRFS_DEV_REPLACE_ITEM_ABORT_ON_UNCORRECTABLE);
+
+       if (flags & ~BTRFS_IOCTL_DEV_REPLACE_SUPPORTED_FLAGS)
+               return -EOPNOTSUPP;
+
+       switch (mode) {
        case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS:
        case BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID:
                break;
@@ -764,8 +781,7 @@ int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
 
        ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
                                        args->start.srcdevid,
-                                       args->start.srcdev_name,
-                                       args->start.cont_reading_from_srcdev_mode);
+                                       args->start.srcdev_name, mode, flags);
        args->result = ret;
        /* don't warn if EINPROGRESS, someone else might be running scrub */
        if (ret == BTRFS_IOCTL_DEV_REPLACE_RESULT_SCRUB_INPROGRESS ||
index a3b0edf4025cee4a592316eb39746582602069c8..4cf6672459e3aba637f96641a09283b03ef9f1b9 100644 (file)
@@ -379,6 +379,8 @@ struct btrfs_dev_replace {
 
        /* See #define above */
        u64 cont_reading_from_srcdev_mode;
+       /* BTRFS_DEV_REPLACE_ITEM_* flags, stored with the mode in the item */
+       u64 flags;
 
        int is_valid;
        int item_needs_writeback;
index 3f888366c14422163971a99655cbebda2e988214..42837969d572a09fc84834329a48c2bdf7a17bc3 100644 (file)
@@ -2976,6 +2976,16 @@ skip_unfreeze:
                        ret = -EIO;
                        break;
                }
+               if (unlikely(sctx->is_dev_replace &&
+                            (dev_replace->flags &
+                             BTRFS_DEV_REPLACE_ITEM_ABORT_ON_UNCORRECTABLE) &&
+                            atomic64_read(&dev_replace->num_uncorrectable_read_errors) > 0)) {
+                       btrfs_err(fs_info,
+       "dev_replace: %lld uncorrectable read errors, aborting as requested",
+                                 atomic64_read(&dev_replace->num_uncorrectable_read_errors));
+                       ret = -EIO;
+                       break;
+               }
                if (sctx->stat.malloc_errors > 0) {
                        ret = -ENOMEM;
                        break;
index b6f196bfa600a9e1864c4141fe0c7c6ac19b0635..e53650a60d8a4296c6279ed78c4c300fe4c13251 100644 (file)
@@ -201,6 +201,19 @@ struct btrfs_ioctl_scrub_args {
 
 #define BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS   0
 #define BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID    1
+/*
+ * The low 32 bits of cont_reading_from_srcdev_mode hold one of the modes
+ * above, the high 32 bits hold flags.  Kernels that do not know a flag
+ * fail the start with -EINVAL (before this flag existed) or -EOPNOTSUPP.
+ */
+#define BTRFS_IOCTL_DEV_REPLACE_MODE_MASK              0xffffffffULL
+/*
+ * Cancel the replace, with -EIO, when a sector that is in use cannot be
+ * read from any copy.  Without it such sectors are counted and skipped.
+ */
+#define BTRFS_IOCTL_DEV_REPLACE_ABORT_ON_UNCORRECTABLE (1ULL << 32)
+#define BTRFS_IOCTL_DEV_REPLACE_SUPPORTED_FLAGS                \
+       (BTRFS_IOCTL_DEV_REPLACE_ABORT_ON_UNCORRECTABLE)
 struct btrfs_ioctl_dev_replace_start_params {
        __u64 srcdevid; /* in, if 0, use srcdev_name instead */
        __u64 cont_reading_from_srcdev_mode;    /* in, see #define
index 47ee52859b4589567683785edbfcaec8b24b7af1..6eb00d5c00b7722249565b54ef84d6700ef0aed1 100644 (file)
@@ -1141,6 +1141,13 @@ struct btrfs_dev_stats_item {
 
 #define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS    0
 #define BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID     1
+/*
+ * Flags in the high 32 bits of cont_reading_from_srcdev_mode, same values as
+ * the BTRFS_IOCTL_DEV_REPLACE_* flags.  Older kernels ignore them and read
+ * the mode as BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_ALWAYS.
+ */
+#define BTRFS_DEV_REPLACE_ITEM_MODE_MASK                       0xffffffffULL
+#define BTRFS_DEV_REPLACE_ITEM_ABORT_ON_UNCORRECTABLE          (1ULL << 32)
 
 struct btrfs_dev_replace_item {
        /*