[PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device
From: Miroslav Cimerman
Date: Thu Nov 26 2026 - 19:33:31 EST
A device can fail between the wait for blocked devices and the device
selection of a write. The selection skips Faulty devices, so the write
completes before the superblock records the failure. Before the commits
below, the selection waited for a Blocked device.
Give the write to a Faulty device while its failure might not be
recorded: while it is Blocked, or after MD_BROKEN. raid1_submit_write()
fails the copy at once, and the write is held. Waiting for Blocked
again would miss MD_BROKEN: md_update_sb() clears Blocked even when the
superblock write failed.
This needs "md/raid1,raid10: hold writes that fail on a faulty device"
and "md/raid1,raid10: fail held writes when the array fails".
Fixes: 88ed59c4cc6c ("md/raid1: factor out helper to handle blocked rdev from raid1_write_request()")
Fixes: f2e7e269a752 ("md/raid10: pull the code that wait for blocked dev into one function")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Miroslav Cimerman <mc@xxxxxxxxxxx>
---
drivers/md/raid1-10.c | 12 ++++++++++++
drivers/md/raid1.c | 9 ++++++++-
drivers/md/raid10.c | 13 +++++++++----
3 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/drivers/md/raid1-10.c b/drivers/md/raid1-10.c
index 341a9785e3f2..f8fa9cdc4080 100644
--- a/drivers/md/raid1-10.c
+++ b/drivers/md/raid1-10.c
@@ -125,6 +125,18 @@ static inline void raid1_submit_write(struct bio *bio)
submit_bio_noacct(bio);
}
+/*
+ * Give a write to a Faulty device while its failure might not be recorded:
+ * Blocked is set until it is, and after MD_BROKEN it might never be.
+ * raid1_submit_write() fails the copy, and the write is held.
+ */
+static inline bool raid1_write_to_faulty(struct mddev *mddev,
+ struct md_rdev *rdev)
+{
+ return test_bit(Blocked, &rdev->flags) ||
+ (!mddev_is_dm(mddev) && test_bit(MD_BROKEN, &mddev->flags));
+}
+
static inline bool raid1_add_bio_to_plug(struct mddev *mddev, struct bio *bio,
blk_plug_cb_fn unplug, int copies)
{
diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index f596b36ec6e1..508673e8c02e 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -1524,8 +1524,15 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,
write_behind = false;
r1_bio->bios[i] = NULL;
- if (!rdev || test_bit(Faulty, &rdev->flags))
+ if (!rdev)
+ continue;
+ if (test_bit(Faulty, &rdev->flags)) {
+ if (raid1_write_to_faulty(mddev, rdev)) {
+ atomic_inc(&rdev->nr_pending);
+ r1_bio->bios[i] = bio;
+ }
continue;
+ }
if (test_bit(WriteErrorSeen, &rdev->flags)) {
sector_t first_bad;
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index 519b6ac0accd..6ea84eb8bf87 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -1387,14 +1387,19 @@ static bool raid10_write_request(struct mddev *mddev, struct bio *bio,
rdev = conf->mirrors[d].rdev;
rrdev = conf->mirrors[d].replacement;
- if (rdev && (test_bit(Faulty, &rdev->flags)))
+ r10_bio->devs[i].bio = NULL;
+ r10_bio->devs[i].repl_bio = NULL;
+
+ if (rdev && test_bit(Faulty, &rdev->flags)) {
+ if (raid1_write_to_faulty(mddev, rdev)) {
+ r10_bio->devs[i].bio = bio;
+ atomic_inc(&rdev->nr_pending);
+ }
rdev = NULL;
+ }
if (rrdev && (test_bit(Faulty, &rrdev->flags)))
rrdev = NULL;
- r10_bio->devs[i].bio = NULL;
- r10_bio->devs[i].repl_bio = NULL;
-
if (!rdev && !rrdev)
continue;
if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
--
2.53.0