[PATCH 4/8] md/raid1,raid10: fail held writes that no working device has

From: Miroslav Cimerman

Date: Thu Nov 26 2026 - 19:32:59 EST


With a bad block log, a failed copy is recorded as a bad block, and its
device stays in the array. If the superblock write then fails on the
device with the good copy, md marks that device faulty while the write
is held. The write still completes with success, although no working
device has its data, and reads of it return EIO.

Check at release that a working device has all of the write, and fail
the write otherwise.

Fixes: 55ce74d4bfe1 ("md/raid1: ensure device failure recorded before write request returns.")
Fixes: 95af587e95aa ("md/raid10: ensure device failure recorded before write request returns.")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Miroslav Cimerman <mc@xxxxxxxxxxx>
---
drivers/md/raid1.c | 28 ++++++++++++++++++++++++++++
drivers/md/raid10.c | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 62 insertions(+)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 88bbe81c7798..fcdbccf6e368 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -2572,6 +2572,26 @@ static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
}
}

+/*
+ * A device that is In_sync, not Faulty and has no bad block in the range
+ * of the write was not skipped and did not fail, so it has the whole write.
+ */
+static bool raid1_held_write_has_copy(struct r1conf *conf,
+ struct r1bio *r1_bio)
+{
+ int i;
+
+ for (i = 0; i < conf->raid_disks * 2; i++) {
+ struct md_rdev *rdev = READ_ONCE(conf->mirrors[i].rdev);
+
+ if (rdev && test_bit(In_sync, &rdev->flags) &&
+ !test_bit(Faulty, &rdev->flags) &&
+ !rdev_has_badblock(rdev, r1_bio->sector, r1_bio->sectors))
+ return true;
+ }
+ return false;
+}
+
static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
{
struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev;
@@ -2649,6 +2669,14 @@ static void raid1d(struct md_thread *thread)
if (!mddev_is_dm(mddev) &&
test_bit(MD_BROKEN, &mddev->flags))
clear_bit(R1BIO_Uptodate, &r1_bio->state);
+ /*
+ * Fail the write if no working device has it now.
+ * Skip write-behind writes: they completed already,
+ * and nothing pins their rdevs.
+ */
+ if (!test_bit(R1BIO_Returned, &r1_bio->state) &&
+ !raid1_held_write_has_copy(conf, r1_bio))
+ clear_bit(R1BIO_Uptodate, &r1_bio->state);
if (test_bit(R1BIO_WriteError, &r1_bio->state))
close_write(r1_bio);
raid_end_bio_io(r1_bio);
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index a38b95585e35..838ae71205c1 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -2837,6 +2837,37 @@ static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
allow_barrier(conf);
}

+static bool raid10_rdev_has_copy(struct md_rdev *rdev, sector_t addr,
+ int sectors)
+{
+ return rdev && test_bit(In_sync, &rdev->flags) &&
+ !test_bit(Faulty, &rdev->flags) &&
+ !rdev_has_badblock(rdev, addr, sectors);
+}
+
+/*
+ * A device that is In_sync, not Faulty and has no bad block at the copy's
+ * address was not skipped and did not fail, so it has the copy. Check the
+ * replacement too: a finished replacement stays in its slot until moved.
+ */
+static bool raid10_held_write_has_copy(struct r10conf *conf,
+ struct r10bio *r10_bio)
+{
+ int i;
+
+ for (i = 0; i < conf->copies; i++) {
+ int d = r10_bio->devs[i].devnum;
+ sector_t addr = r10_bio->devs[i].addr;
+ struct md_rdev *rdev = READ_ONCE(conf->mirrors[d].rdev);
+ struct md_rdev *rrdev = READ_ONCE(conf->mirrors[d].replacement);
+
+ if (raid10_rdev_has_copy(rdev, addr, r10_bio->sectors) ||
+ raid10_rdev_has_copy(rrdev, addr, r10_bio->sectors))
+ return true;
+ }
+ return false;
+}
+
static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
{
/* Some sort of write request has finished and it
@@ -2963,6 +2994,9 @@ static void raid10d(struct md_thread *thread)
if (!mddev_is_dm(mddev) &&
test_bit(MD_BROKEN, &mddev->flags))
clear_bit(R10BIO_Uptodate, &r10_bio->state);
+ /* Fail the write if no working device has it now */
+ if (!raid10_held_write_has_copy(conf, r10_bio))
+ clear_bit(R10BIO_Uptodate, &r10_bio->state);
if (test_bit(R10BIO_WriteError,
&r10_bio->state))
close_write(r10_bio);
--
2.53.0