[PATCH -next 06/13] md: factor out a helper hot_add_rdev() from remove_and_add_spares()

From: Yu Kuai
Date: Thu Aug 03 2023 - 09:27:49 EST


From: Yu Kuai <yukuai3@xxxxxxxxxx>

There are no functional changes, just to make the code simpler and
prepare to refactoer remove_and_add_spares().

Signed-off-by: Yu Kuai <yukuai3@xxxxxxxxxx>
---
drivers/md/md.c | 34 ++++++++++++++++++++++------------
1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 74ee31f2706b..77c48f7b605c 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -9196,6 +9196,26 @@ static bool hot_remove_rdev(struct md_rdev *rdev)
return true;
}

+static bool hot_add_rdev(struct md_rdev *rdev)
+{
+ struct mddev *mddev;
+
+ if (!rdev_addable(rdev))
+ return false;
+
+ if (!test_bit(Journal, &rdev->flags))
+ rdev->recovery_offset = 0;
+
+ mddev = rdev->mddev;
+ if (mddev->pers->hot_add_disk(mddev, rdev))
+ return false;
+
+ sysfs_link_rdev(mddev, rdev);
+ md_new_event();
+ set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
+ return true;
+}
+
static int remove_and_add_spares(struct mddev *mddev,
struct md_rdev *this)
{
@@ -9222,18 +9242,8 @@ static int remove_and_add_spares(struct mddev *mddev,
continue;
if (rdev_is_spare(rdev))
spares++;
- if (!rdev_addable(rdev))
- continue;
- if (!test_bit(Journal, &rdev->flags))
- rdev->recovery_offset = 0;
- if (mddev->pers->hot_add_disk(mddev, rdev) == 0) {
- /* failure here is OK */
- sysfs_link_rdev(mddev, rdev);
- if (!test_bit(Journal, &rdev->flags))
- spares++;
- md_new_event();
- set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
- }
+ if (hot_add_rdev(rdev) && !test_bit(Journal, &rdev->flags))
+ spares++;
}
no_add:
if (removed)
--
2.39.2