[PATCH AUTOSEL 4.4 25/32] MD: fix invalid stored role for a disk

From: Sasha Levin
Date: Wed Oct 31 2018 - 19:15:07 EST

From: Shaohua Li <shli@xxxxxx>

[ Upstream commit d595567dc4f0c1d90685ec1e2e296e2cad2643ac ]

If we change the number of array's device after device is removed from array,
then add the device back to array, we can see that device is added as active
role instead of spare which we expected.

Please see the below link for details:

This is caused by that we prefer to use device's previous role which is
recorded by saved_raid_disk, but we should respect the new number of
conf->raid_disks since it could be changed after device is removed.

Reported-by: Gioh Kim <gi-oh.kim@xxxxxxxxxxxxxxxx>
Tested-by: Gioh Kim <gi-oh.kim@xxxxxxxxxxxxxxxx>
Acked-by: Guoqing Jiang <gqjiang@xxxxxxxx>
Signed-off-by: Shaohua Li <shli@xxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
drivers/md/md.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 07f307402351..4caf240693cd 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1670,6 +1670,10 @@ static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
} else
set_bit(In_sync, &rdev->flags);
rdev->raid_disk = role;
+ if (role >= mddev->raid_disks) {
+ rdev->saved_raid_disk = -1;
+ rdev->raid_disk = -1;
+ }
if (sb->devflags & WriteMostly1)