only read from *srcdev* if no other zero-defect mirror exists.
(enable this if your drive has lots of read errors, the access would be very
slow)
+ --abort-on-uncorrectable
+ cancel the replace if a block that is in use cannot be read from
+ any copy. The replace fails, the target device is released and
+ the source device stays in the filesystem. Without this option
+ such blocks are counted ("uncorr. read errs" in ``btrfs replace
+ status``) and skipped, which is what you want when rescuing
+ data from a failing device; with it, a replace of a healthy
+ device stops instead of completing with unreadable data.
+
+ The replace is canceled when the block group being copied is
+ finished, not immediately. Kernels that do not support this
+ option refuse to start the replace.
-f
force using and overwriting *targetdev* even if it looks like
it contains a valid btrfs filesystem.
OPTLINE("-r", "only read from <srcdev> if no other zero-defect mirror exists "
"(enable this if your drive has lots of read errors, the access "
"would be very slow)"),
+ OPTLINE("--abort-on-uncorrectable", "cancel the replace if a block "
+ "cannot be read from any copy, leaving <srcdev> in the "
+ "filesystem (by default such blocks are counted and skipped)"),
OPTLINE("-f", "force using and overwriting <targetdev> even if it looks like "
"containing a valid btrfs filesystem. A valid filesystem is "
"assumed if a btrfs superblock is found which contains a "
u64 dstdev_size;
bool enqueue = false;
bool discard = true;
+ bool abort_on_uncorrectable = false;
optind = 0;
while (1) {
int c;
- enum { GETOPT_VAL_ENQUEUE = GETOPT_VAL_FIRST };
+ enum {
+ GETOPT_VAL_ENQUEUE = GETOPT_VAL_FIRST,
+ GETOPT_VAL_ABORT_ON_UNCORRECTABLE,
+ };
static const struct option long_options[] = {
{ "enqueue", no_argument, NULL, GETOPT_VAL_ENQUEUE},
+ { "abort-on-uncorrectable", no_argument, NULL,
+ GETOPT_VAL_ABORT_ON_UNCORRECTABLE },
{ "nodiscard", no_argument, NULL, 'K' },
{ NULL, 0, NULL, 0}
};
case GETOPT_VAL_ENQUEUE:
enqueue = true;
break;
+ case GETOPT_VAL_ABORT_ON_UNCORRECTABLE:
+ abort_on_uncorrectable = true;
+ break;
default:
usage_unknown_option(cmd, argv);
}
avoid_reading_from_srcdev ?
BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID :
BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
+ if (abort_on_uncorrectable)
+ start_args.start.cont_reading_from_srcdev_mode |=
+ BTRFS_IOCTL_DEV_REPLACE_ABORT_ON_UNCORRECTABLE;
if (check_argc_exact(argc - optind, 3))
return 1;
path = argv[optind + 2];
error("ioctl(DEV_REPLACE_START) failed on \"%s\": %m, %s",
path, replace_dev_result2string(start_args.result));
- if (errno == EOPNOTSUPP)
+ if (abort_on_uncorrectable &&
+ (errno == EINVAL || errno == EOPNOTSUPP))
+ warning("this kernel may not support --abort-on-uncorrectable");
+ else if (errno == EOPNOTSUPP)
warning("device replace of RAID5/6 not supported with this kernel");
+ if (errno == EIO)
+ warning("the replace was canceled after a write error to the target%s, see 'btrfs replace status' and the kernel log",
+ abort_on_uncorrectable ?
+ " or an uncorrectable read error" : "");
goto leave_with_error;
}
static void replace_mode_to_str(u64 flags, char *ret)
{
+ const u64 mode = flags & BTRFS_DEV_REPLACE_ITEM_MODE_MASK;
+ size_t len;
+
ret[0] = 0;
- switch(flags) {
+ switch(mode) {
CASE_DEV_REPLACE_MODE_ENTRY(ret, ALWAYS);
CASE_DEV_REPLACE_MODE_ENTRY(ret, AVOID);
default:
- snprintf(ret, DEV_REPLACE_STRING_LEN, "unknown(%llu)", flags);
+ snprintf(ret, DEV_REPLACE_STRING_LEN, "unknown(%llu)", mode);
+ }
+ flags &= ~BTRFS_DEV_REPLACE_ITEM_MODE_MASK;
+ len = strlen(ret);
+ if (flags & BTRFS_DEV_REPLACE_ITEM_ABORT_ON_UNCORRECTABLE) {
+ snprintf(ret + len, DEV_REPLACE_STRING_LEN - len,
+ "|ABORT_ON_UNCORRECTABLE");
+ len = strlen(ret);
+ flags &= ~BTRFS_DEV_REPLACE_ITEM_ABORT_ON_UNCORRECTABLE;
}
+ if (flags)
+ snprintf(ret + len, DEV_REPLACE_STRING_LEN - len,
+ "|unknown(0x%llx)", flags);
}
#undef DEV_REPLACE_MODE_ENTRY
#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
#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 {
/*