[PATCH 5/8] md/raid1,raid10: hold writes that fail on a faulty device

From: Miroslav Cimerman

Date: Thu Nov 26 2026 - 19:33:21 EST


If a device is already Faulty when a copy of a write fails on it, the
end_io does not hold the write. The write completes at once with
success from the other copy, before the superblock records the failure.
This happens to writes in flight when another write fails the device,
to writes that raid1_submit_write() fails on a Faulty device, and with
failfast, where md_error() runs in the end_io.

Hold these writes too, but skip narrow_write_error() for a Faulty
device. The "end bio when the device faulty" commits avoided these
retries.

Fixes: eeba6809d8d5 ("md/raid1: end bio when the device faulty")
Fixes: 7cee6d4e6035 ("md/raid10: end bio when the device faulty")
Fixes: 212e7eb7a340 ("md/raid1: add failfast handling for writes.")
Fixes: 1919cbb23bf1 ("md/raid10: add failfast handling for writes.")
Cc: stable@xxxxxxxxxxxxxxx # v5.19+
Signed-off-by: Miroslav Cimerman <mc@xxxxxxxxxxx>
---
drivers/md/raid1.c | 16 ++++++----------
drivers/md/raid10.c | 20 ++++++--------------
2 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index fcdbccf6e368..54ba3013150e 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -493,16 +493,10 @@ static void raid1_end_write_request(struct bio *bio)
}

/*
- * When the device is faulty, it is not necessary to
- * handle write error.
+ * Hold the write until the superblock records the failure,
+ * also on a Faulty device.
*/
- if (!test_bit(Faulty, &rdev->flags))
- set_bit(R1BIO_WriteError, &r1_bio->state);
- else {
- /* Finished with this branch */
- r1_bio->bios[mirror] = NULL;
- to_put = bio;
- }
+ set_bit(R1BIO_WriteError, &r1_bio->state);
} else {
/*
* Set R1BIO_Uptodate in our master bio, so that we
@@ -2549,7 +2543,9 @@ static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
* errors.
*/
fail = true;
- narrow_write_error(r1_bio, m);
+ /* No retries on a Faulty device */
+ if (!test_bit(Faulty, &conf->mirrors[m].rdev->flags))
+ narrow_write_error(r1_bio, m);
rdev_dec_pending(conf->mirrors[m].rdev,
conf->mddev);
}
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 838ae71205c1..519b6ac0accd 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -473,7 +473,6 @@ static void raid10_end_write_request(struct bio *bio)
struct r10conf *conf = r10_bio->mddev->private;
int slot, repl;
struct md_rdev *rdev = NULL;
- struct bio *to_put = NULL;
bool ignore_error = !raid1_should_handle_error(bio) ||
(bio->bi_status && bio_op(bio) == REQ_OP_DISCARD);

@@ -505,17 +504,10 @@ static void raid10_end_write_request(struct bio *bio)
}

/*
- * When the device is faulty, it is not necessary to
- * handle write error.
+ * Hold the write until the superblock records the
+ * failure, also on a Faulty device.
*/
- if (!test_bit(Faulty, &rdev->flags))
- set_bit(R10BIO_WriteError, &r10_bio->state);
- else {
- /* Fail the request */
- r10_bio->devs[slot].bio = NULL;
- to_put = bio;
- dec_rdev = 1;
- }
+ set_bit(R10BIO_WriteError, &r10_bio->state);
}
} else {
/*
@@ -560,8 +552,6 @@ static void raid10_end_write_request(struct bio *bio)
one_write_done(r10_bio);
if (dec_rdev)
rdev_dec_pending(rdev, conf->mddev);
- if (to_put)
- bio_put(to_put);
}

/*
@@ -2926,7 +2916,9 @@ static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
rdev_dec_pending(rdev, conf->mddev);
} else if (bio != NULL && bio->bi_status) {
fail = true;
- narrow_write_error(r10_bio, m);
+ /* No retries on a Faulty device */
+ if (!test_bit(Faulty, &rdev->flags))
+ narrow_write_error(r10_bio, m);
rdev_dec_pending(rdev, conf->mddev);
}
bio = r10_bio->devs[m].repl_bio;
--
2.53.0