Skip to contentSkip to conversation

md: cleanup md_check_recovery()

PATCH In review Wu Guanghao 3 participants 5 messages Sep 4, 2025 – Sep 8, 2025 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 md: cleanup md_check_recovery()

In md_check_recovery(), use new helpers to make code cleaner.

---
 drivers/md/md.c | 41 +++++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 12 deletions(-)
drivers/md/md.c +29 -12
index 1baaf52c603c..cbbb9ac14cf6 100644
@@ -9741,6 +9741,34 @@ static void unregister_sync_thread(struct mddev *mddev)
 	md_reap_sync_thread(mddev);
 }
 
+
+
+static bool md_should_do_recovery(struct mddev *mddev)
+{
+	/*
+	 * As long as one of the following flags is set,
+	 * recovery needs to do.
+	 */
+	if (test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
+	    test_bit(MD_RECOVERY_DONE, &mddev->recovery))
+		return true;
+
+	/*
+	 * If no flags are set and it is in read-only status,
+	 * there is nothing to do.
+	 */
+	if (!md_is_rdwr(mddev))
+		return false;
+
+	if ((mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
+	     (mddev->external == 0 && mddev->safemode == 1) ||
+	     (mddev->safemode == 2 && !mddev->in_sync &&
+	      mddev->resync_offset == MaxSector))
+		return true;
+
+	return false;
+}
+
 /*
  * This routine is regularly called by all per-raid-array threads to
  * deal with generic issues like resync and super-block update.
@@ -9777,18 +9805,7 @@ void md_check_recovery(struct mddev *mddev)
 		flush_signals(current);
 	}
 
-	if (!md_is_rdwr(mddev) &&
-	    !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
-	    !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
-		return;
-	if ( ! (
-		(mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
-		test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
-		test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
-		(mddev->external == 0 && mddev->safemode == 1) ||
-		(mddev->safemode == 2
-		 && !mddev->in_sync && mddev->resync_offset == MaxSector)
-		))
+	if (!md_should_do_recovery(mddev))
 		return;
 
 	if (mddev_trylock(mddev)) {
Signature · 1 line
2.27.0
#1 lore raw

Dear Guanghao,

Thank you for your patch. For the summary I suggest:

Factor out code into md_should_do_recovery()

Am 04.09.25 um 13:13 schrieb Wu Guanghao:

Quoted 1 line

In md_check_recovery(), use new helpers to make code cleaner.

Singular *helper*?

Quoted 10 lines
---
  drivers/md/md.c | 41 +++++++++++++++++++++++++++++------------
  1 file changed, 29 insertions(+), 12 deletions(-)
drivers/md/md.c +0 -0
index 1baaf52c603c..cbbb9ac14cf6 100644

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9745

Quoted 5 lines
drivers/md/md.c +2 -0
@@ -9741,6 +9741,34 @@ static void unregister_sync_thread(struct mddev *mddev)
  	md_reap_sync_thread(mddev);
  }
 
+
+

Why so many blank lines.

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9750

Quoted 5 lines
drivers/md/md.c +5 -0
+static bool md_should_do_recovery(struct mddev *mddev)
+{
+	/*
+	 * As long as one of the following flags is set,
+	 * recovery needs to do.

… recovery needs to be do*ne*?

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9758

Quoted 8 lines
drivers/md/md.c +8 -0
+	 */
+	if (test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
+	    test_bit(MD_RECOVERY_DONE, &mddev->recovery))
+		return true;
+
+	/*
+	 * If no flags are set and it is in read-only status,
+	 * there is nothing to do.

Ditto.

Quoted 26 lines
drivers/md/md.c +13 -6
+	 */
+	if (!md_is_rdwr(mddev))
+		return false;
+
+	if ((mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
+	     (mddev->external == 0 && mddev->safemode == 1) ||
+	     (mddev->safemode == 2 && !mddev->in_sync &&
+	      mddev->resync_offset == MaxSector))
+		return true;
+
+	return false;
+}
+
  /*
   * This routine is regularly called by all per-raid-array threads to
   * deal with generic issues like resync and super-block update.
@@ -9777,18 +9805,7 @@ void md_check_recovery(struct mddev *mddev)
  		flush_signals(current);
  	}
 
-	if (!md_is_rdwr(mddev) &&
-	    !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
-	    !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
-		return;
-	if ( ! (
-		(mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||

drivers/md/md.c void md_check_recovery(struct mddev *mddev) · line 9811

Quoted 10 lines
drivers/md/md.c +1 -6
-		test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
-		test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
-		(mddev->external == 0 && mddev->safemode == 1) ||
-		(mddev->safemode == 2
-		 && !mddev->in_sync && mddev->resync_offset == MaxSector)
-		))
+	if (!md_should_do_recovery(mddev))
  		return;
 
  	if (mddev_trylock(mddev)) {

The code diff looks good.

Kind regards,

Paul

#2 lore raw

Thank you for your suggestion, I will modify it in the next version.

Quoted 93 lines · Paul Menzel

Dear Guanghao,

Thank you for your patch. For the summary I suggest:

Factor out code into md_should_do_recovery()

Am 04.09.25 um 13:13 schrieb Wu Guanghao:

Quoted 1 line

In md_check_recovery(), use new helpers to make code cleaner.

Singular *helper*?

Quoted 10 lines

---
  drivers/md/md.c | 41 +++++++++++++++++++++++++++++------------

  1 file changed, 29 insertions(+), 12 deletions(-)
drivers/md/md.c +0 -0
index 1baaf52c603c..cbbb9ac14cf6 100644

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9745

Quoted 5 lines
drivers/md/md.c +2 -0
@@ -9741,6 +9741,34 @@ static void unregister_sync_thread(struct mddev *mddev)
       md_reap_sync_thread(mddev);
   }
 
+
+

Why so many blank lines.

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9750

Quoted 5 lines
drivers/md/md.c +5 -0
+static bool md_should_do_recovery(struct mddev *mddev)
+{
+    /*
+     * As long as one of the following flags is set,
+     * recovery needs to do.

… recovery needs to be do*ne*?

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9758

Quoted 8 lines
drivers/md/md.c +8 -0
+     */
+    if (test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
+        test_bit(MD_RECOVERY_DONE, &mddev->recovery))
+        return true;
+
+    /*
+     * If no flags are set and it is in read-only status,
+     * there is nothing to do.

Ditto.

Quoted 26 lines
drivers/md/md.c +13 -6
+     */
+    if (!md_is_rdwr(mddev))
+        return false;
+
+    if ((mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
+         (mddev->external == 0 && mddev->safemode == 1) ||
+         (mddev->safemode == 2 && !mddev->in_sync &&
+          mddev->resync_offset == MaxSector))
+        return true;
+
+    return false;
+}
+
   /*
    * This routine is regularly called by all per-raid-array threads to
    * deal with generic issues like resync and super-block update.
@@ -9777,18 +9805,7 @@ void md_check_recovery(struct mddev *mddev)
           flush_signals(current);
       }
 
-    if (!md_is_rdwr(mddev) &&
-        !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
-        !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
-        return;
-    if ( ! (
-        (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||

drivers/md/md.c void md_check_recovery(struct mddev *mddev) · line 9811

Quoted 10 lines
drivers/md/md.c +1 -6
-        test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
-        test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
-        (mddev->external == 0 && mddev->safemode == 1) ||
-        (mddev->safemode == 2
-         && !mddev->in_sync && mddev->resync_offset == MaxSector)
-        ))
+    if (!md_should_do_recovery(mddev))
           return;
 
       if (mddev_trylock(mddev)) {

The code diff looks good.

Kind regards,

Paul
.

#3 lore raw
In reply to Wu Guanghao

Hi,

Quoted 29 lines · Wu Guanghao

In md_check_recovery(), use new helpers to make code cleaner.

---
  drivers/md/md.c | 41 +++++++++++++++++++++++++++++------------
  1 file changed, 29 insertions(+), 12 deletions(-)
drivers/md/md.c +14 -0
index 1baaf52c603c..cbbb9ac14cf6 100644
@@ -9741,6 +9741,34 @@ static void unregister_sync_thread(struct mddev *mddev)
  	md_reap_sync_thread(mddev);
  }
 
+
+
+static bool md_should_do_recovery(struct mddev *mddev)
+{
+	/*
+	 * As long as one of the following flags is set,
+	 * recovery needs to do.
+	 */
+	if (test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
+	    test_bit(MD_RECOVERY_DONE, &mddev->recovery))
+		return true;
+
+	/*
+	 * If no flags are set and it is in read-only status,

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9767

Quoted 10 lines
drivers/md/md.c +10 -0
+	 * there is nothing to do.
+	 */
+	if (!md_is_rdwr(mddev))
+		return false;
+
+	if ((mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
+	     (mddev->external == 0 && mddev->safemode == 1) ||
+	     (mddev->safemode == 2 && !mddev->in_sync &&
+	      mddev->resync_offset == MaxSector))
+		return true;

Plese also split abouve conditions and add comments.

Thanks,
Kuai

Quoted 28 lines
drivers/md/md.c +5 -12
+
+	return false;
+}
+
  /*
   * This routine is regularly called by all per-raid-array threads to
   * deal with generic issues like resync and super-block update.
@@ -9777,18 +9805,7 @@ void md_check_recovery(struct mddev *mddev)
  		flush_signals(current);
  	}
 
-	if (!md_is_rdwr(mddev) &&
-	    !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
-	    !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
-		return;
-	if ( ! (
-		(mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
-		test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
-		test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
-		(mddev->external == 0 && mddev->safemode == 1) ||
-		(mddev->safemode == 2
-		 && !mddev->in_sync && mddev->resync_offset == MaxSector)
-		))
+	if (!md_should_do_recovery(mddev))
  		return;
 
  	if (mddev_trylock(mddev)) {
#4 lore raw
Quoted 45 lines · Yu Kuai

Hi,

Quoted 29 lines · Wu Guanghao

In md_check_recovery(), use new helpers to make code cleaner.

---
  drivers/md/md.c | 41 +++++++++++++++++++++++++++++------------

  1 file changed, 29 insertions(+), 12 deletions(-)
drivers/md/md.c +14 -0
index 1baaf52c603c..cbbb9ac14cf6 100644
@@ -9741,6 +9741,34 @@ static void unregister_sync_thread(struct mddev *mddev)
       md_reap_sync_thread(mddev);
   }
 
+
+
+static bool md_should_do_recovery(struct mddev *mddev)
+{
+    /*
+     * As long as one of the following flags is set,
+     * recovery needs to do.
+     */
+    if (test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
+        test_bit(MD_RECOVERY_DONE, &mddev->recovery))
+        return true;
+
+    /*
+     * If no flags are set and it is in read-only status,

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9767

Quoted 10 lines
drivers/md/md.c +10 -0
+     * there is nothing to do.
+     */
+    if (!md_is_rdwr(mddev))
+        return false;
+
+    if ((mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
+         (mddev->external == 0 && mddev->safemode == 1) ||
+         (mddev->safemode == 2 && !mddev->in_sync &&
+          mddev->resync_offset == MaxSector))
+        return true;

Plese also split abouve conditions and add comments.

Quoted 3 lines

Thanks,
Kuai

OK,I will make the changes in the next version.

Thanks,
Guanghao

Quoted 30 lines
Quoted 18 lines
drivers/md/md.c +4 -6
+
+    return false;
+}
+
   /*
    * This routine is regularly called by all per-raid-array threads to
    * deal with generic issues like resync and super-block update.
@@ -9777,18 +9805,7 @@ void md_check_recovery(struct mddev *mddev)
           flush_signals(current);
       }
 
-    if (!md_is_rdwr(mddev) &&
-        !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
-        !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
-        return;
-    if ( ! (
-        (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||

drivers/md/md.c void md_check_recovery(struct mddev *mddev) · line 9811

Quoted 10 lines
drivers/md/md.c +1 -6
-        test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
-        test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
-        (mddev->external == 0 && mddev->safemode == 1) ||
-        (mddev->safemode == 2
-         && !mddev->in_sync && mddev->resync_offset == MaxSector)
-        ))
+    if (!md_should_do_recovery(mddev))
           return;
 
       if (mddev_trylock(mddev)) {

.