Commit 81e2ce1b authored by Yu Kuai's avatar Yu Kuai Committed by Song Liu

md: delay remove_and_add_spares() for read only array to md_start_sync()

Before this patch, for read-only array:

md_check_recovery() check that 'MD_RECOVERY_NEEDED' is set, then it will
call remove_and_add_spares() directly to try to remove and add rdevs
from array.

After this patch:

1) md_check_recovery() check that 'MD_RECOVERY_NEEDED' is set, and the
   worker 'sync_work' is not pending, and there are rdevs can be added
   or removed, then it will queue new work md_start_sync();
2) md_start_sync() will call remove_and_add_spares() and exist;

This change make sure that array reconfiguration is independent from
daemon, and it'll be much easier to synchronize it with io, consier
that io may rely on daemon thread to be done.

Also fix a problem that 'pers->spars_active' is called after
remove_and_add_spares(), which order is wrong, because spares must
active first, and then remove_and_add_spares() can add spares to the
array, like what read-write case does:

1) daemon set 'MD_RECOVERY_RUNNING', register new sync thread to do
   recovery;
2) recovery is done, md_do_sync() set 'MD_RECOVERY_DONE' before return;
3) daemon call 'pers->spars_active', and clear 'MD_RECOVERY_RUNNING';
4) in the next round of daemon, call remove_and_add_spares() to add
   spares to the array.
Signed-off-by: default avatarYu Kuai <yukuai3@huawei.com>
Signed-off-by: default avatarSong Liu <song@kernel.org>
Link: https://lore.kernel.org/r/20230825031622.1530464-8-yukuai1@huaweicloud.com
parent a0ae7e4e
...@@ -4875,6 +4875,7 @@ action_store(struct mddev *mddev, const char *page, size_t len) ...@@ -4875,6 +4875,7 @@ action_store(struct mddev *mddev, const char *page, size_t len)
/* A write to sync_action is enough to justify /* A write to sync_action is enough to justify
* canceling read-auto mode * canceling read-auto mode
*/ */
flush_work(&mddev->sync_work);
mddev->ro = MD_RDWR; mddev->ro = MD_RDWR;
md_wakeup_thread(mddev->sync_thread); md_wakeup_thread(mddev->sync_thread);
} }
...@@ -7649,6 +7650,10 @@ static int md_ioctl(struct block_device *bdev, blk_mode_t mode, ...@@ -7649,6 +7650,10 @@ static int md_ioctl(struct block_device *bdev, blk_mode_t mode,
mutex_unlock(&mddev->open_mutex); mutex_unlock(&mddev->open_mutex);
sync_blockdev(bdev); sync_blockdev(bdev);
} }
if (!md_is_rdwr(mddev))
flush_work(&mddev->sync_work);
err = mddev_lock(mddev); err = mddev_lock(mddev);
if (err) { if (err) {
pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
...@@ -8573,6 +8578,7 @@ bool md_write_start(struct mddev *mddev, struct bio *bi) ...@@ -8573,6 +8578,7 @@ bool md_write_start(struct mddev *mddev, struct bio *bi)
BUG_ON(mddev->ro == MD_RDONLY); BUG_ON(mddev->ro == MD_RDONLY);
if (mddev->ro == MD_AUTO_READ) { if (mddev->ro == MD_AUTO_READ) {
/* need to switch to read/write */ /* need to switch to read/write */
flush_work(&mddev->sync_work);
mddev->ro = MD_RDWR; mddev->ro = MD_RDWR;
set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
md_wakeup_thread(mddev->thread); md_wakeup_thread(mddev->thread);
...@@ -9233,6 +9239,16 @@ static bool rdev_addable(struct md_rdev *rdev) ...@@ -9233,6 +9239,16 @@ static bool rdev_addable(struct md_rdev *rdev)
return false; return false;
} }
static bool md_spares_need_change(struct mddev *mddev)
{
struct md_rdev *rdev;
rdev_for_each(rdev, mddev)
if (rdev_removeable(rdev) || rdev_addable(rdev))
return true;
return false;
}
static int remove_and_add_spares(struct mddev *mddev, static int remove_and_add_spares(struct mddev *mddev,
struct md_rdev *this) struct md_rdev *this)
{ {
...@@ -9360,6 +9376,18 @@ static void md_start_sync(struct work_struct *ws) ...@@ -9360,6 +9376,18 @@ static void md_start_sync(struct work_struct *ws)
mddev_lock_nointr(mddev); mddev_lock_nointr(mddev);
if (!md_is_rdwr(mddev)) {
/*
* On a read-only array we can:
* - remove failed devices
* - add already-in_sync devices if the array itself is in-sync.
* As we only add devices that are already in-sync, we can
* activate the spares immediately.
*/
remove_and_add_spares(mddev, NULL);
goto not_running;
}
if (!md_choose_sync_action(mddev, &spares)) if (!md_choose_sync_action(mddev, &spares))
goto not_running; goto not_running;
...@@ -9474,30 +9502,43 @@ void md_check_recovery(struct mddev *mddev) ...@@ -9474,30 +9502,43 @@ void md_check_recovery(struct mddev *mddev)
if (!md_is_rdwr(mddev)) { if (!md_is_rdwr(mddev)) {
struct md_rdev *rdev; struct md_rdev *rdev;
if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
/* sync_work already queued. */
clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
goto unlock;
}
if (!mddev->external && mddev->in_sync) if (!mddev->external && mddev->in_sync)
/* 'Blocked' flag not needed as failed devices /*
* 'Blocked' flag not needed as failed devices
* will be recorded if array switched to read/write. * will be recorded if array switched to read/write.
* Leaving it set will prevent the device * Leaving it set will prevent the device
* from being removed. * from being removed.
*/ */
rdev_for_each(rdev, mddev) rdev_for_each(rdev, mddev)
clear_bit(Blocked, &rdev->flags); clear_bit(Blocked, &rdev->flags);
/* On a read-only array we can:
* - remove failed devices /*
* - add already-in_sync devices if the array itself * There is no thread, but we need to call
* is in-sync.
* As we only add devices that are already in-sync,
* we can activate the spares immediately.
*/
remove_and_add_spares(mddev, NULL);
/* There is no thread, but we need to call
* ->spare_active and clear saved_raid_disk * ->spare_active and clear saved_raid_disk
*/ */
set_bit(MD_RECOVERY_INTR, &mddev->recovery); set_bit(MD_RECOVERY_INTR, &mddev->recovery);
md_reap_sync_thread(mddev); md_reap_sync_thread(mddev);
/*
* Let md_start_sync() to remove and add rdevs to the
* array.
*/
if (md_spares_need_change(mddev)) {
set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
queue_work(md_misc_wq, &mddev->sync_work);
}
clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
goto unlock; goto unlock;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment