From 9dad51d41853a1bf791dfe418b67fefac21c4670 Mon Sep 17 00:00:00 2001
From: NeilBrown <neilb@suse.de>
Date: Mon, 4 Jun 2012 12:31:40 +1000
Subject: [PATCH 1/3] Monitor: fix inconsistencies in values for ->percent

->percent sometimes stores negative values recording states
like 'pending' or 'delayed'.
The value '-2' means both 'delayed' and in Monitor, 'unknown'.
Also, '-1' has a meaning but not #define.

So change the #defines to be prefixed with "RESYNC_", instead
of "PROCESS_", add new "_NONE" and "_UNKNOWN", and use correct
value in each location.

Signed-off-by: NeilBrown <neilb@suse.de>
---
 Detail.c  |    8 ++++----
 Monitor.c |   10 +++++-----
 mdadm.h   |    6 ++++--
 mdstat.c  |   12 ++++++------
 4 files changed, 19 insertions(+), 17 deletions(-)

--- mdadm-3.2.2.orig/Detail.c
+++ mdadm-3.2.2/Detail.c
@@ -374,11 +374,11 @@ int Detail(char *dev, int brief, int exp
 
 			printf("          State : %s%s%s%s%s%s \n",
 			       (array.state&(1<<MD_SB_CLEAN))?"clean":"active", st,
-			       (!e || (e->percent < 0 && e->percent != PROCESS_PENDING &&
-			       e->percent != PROCESS_DELAYED)) ? "" : sync_action[e->resync],
+			       (!e || (e->percent < 0 && e->percent != RESYNC_PENDING &&
+			       e->percent != RESYNC_DELAYED)) ? "" : sync_action[e->resync],
 			       larray_size ? "": ", Not Started",
-			       e->percent == PROCESS_DELAYED ? " (DELAYED)": "",
-			       e->percent == PROCESS_PENDING ? " (PENDING)": "");
+			       e->percent == RESYNC_DELAYED ? " (DELAYED)": "",
+			       e->percent == RESYNC_PENDING ? " (PENDING)": "");
 		}
 		if (array.raid_disks)
 			printf(" Active Devices : %d\n", array.active_disks);
--- mdadm-3.2.2.orig/Monitor.c
+++ mdadm-3.2.2/Monitor.c
@@ -177,7 +177,7 @@ int Monitor(struct mddev_dev *devlist,
 			}
 			st->next = statelist;
 			st->devnum = INT_MAX;
-			st->percent = -2;
+			st->percent = RESYNC_UNKNOWN;
 			st->expected_spares = mdlist->spare_disks;
 			if (mdlist->spare_group)
 				st->spare_group = strdup(mdlist->spare_group);
@@ -193,7 +193,7 @@ int Monitor(struct mddev_dev *devlist,
 			st->devname = strdup(dv->devname);
 			st->next = statelist;
 			st->devnum = INT_MAX;
-			st->percent = -2;
+			st->percent = RESYNC_UNKNOWN;
 			st->expected_spares = -1;
 			if (mdlist) {
 				st->expected_spares = mdlist->spare_disks;
@@ -528,7 +528,7 @@ static int check_array(struct state *st,
 	    st->expected_spares > 0 &&
 	    array.spare_disks < st->expected_spares)
 		alert("SparesMissing", dev, NULL, ainfo);
-	if (st->percent == -1 &&
+	if (st->percent < 0 && st->percent != RESYNC_UNKNOWN &&
 	    mse->percent >= 0)
 		alert("RebuildStarted", dev, NULL, ainfo);
 	if (st->percent >= 0 &&
@@ -544,7 +544,7 @@ static int check_array(struct state *st,
 		alert(percentalert, dev, NULL, ainfo);
 	}
 
-	if (mse->percent == -1 &&
+	if (mse->percent == RESYNC_NONE &&
 	    st->percent >= 0) {
 		/* Rebuild/sync/whatever just finished.
 		 * If there is a number in /mismatch_cnt,
@@ -697,7 +697,7 @@ static int add_new_arrays(struct mdstat_
 			st->next = *statelist;
 			st->err = 1;
 			st->devnum = mse->devnum;
-			st->percent = -2;
+			st->percent = RESYNC_UNKNOWN;
 			st->expected_spares = -1;
 			if (mse->metadata_version &&
 			    strncmp(mse->metadata_version, "external:", 9) == 0 &&
--- mdadm-3.2.2.orig/mdadm.h
+++ mdadm-3.2.2/mdadm.h
@@ -1401,8 +1401,10 @@ static inline int xasprintf(char **strp,
 #define PATH_MAX	4096
 #endif
 
-#define PROCESS_DELAYED -2
-#define PROCESS_PENDING -3
+#define RESYNC_NONE -1
+#define RESYNC_DELAYED -2
+#define RESYNC_PENDING -3
+#define RESYNC_UNKNOWN -4
 
 /* When using "GET_DISK_INFO" it isn't certain how high
  * we need to check.  So we impose an absolute limit of
--- mdadm-3.2.2.orig/mdstat.c
+++ mdadm-3.2.2/mdstat.c
@@ -180,7 +180,7 @@ struct mdstat_ent *mdstat_read(int hold,
 		}
 		ent->dev = ent->level = ent->pattern= NULL;
 		ent->next = NULL;
-		ent->percent = -1;
+		ent->percent = RESYNC_NONE;
 		ent->active = -1;
 		ent->resync = 0;
 		ent->metadata_version = NULL;
@@ -235,7 +235,7 @@ struct mdstat_ent *mdstat_read(int hold,
 				ent->pattern = strdup(w+1);
 				if (ent->pattern[l-2]==']')
 					ent->pattern[l-2] = '\0';
-			} else if (ent->percent == -1 &&
+			} else if (ent->percent == RESYNC_NONE &&
 				   strncmp(w, "re", 2)== 0 &&
 				   w[l-1] == '%' &&
 				   (eq=strchr(w, '=')) != NULL ) {
@@ -246,7 +246,7 @@ struct mdstat_ent *mdstat_read(int hold,
 					ent->resync = 2;
 				else
 					ent->resync = 0;
-			} else if (ent->percent == -1 &&
+			} else if (ent->percent == RESYNC_NONE &&
 				   (w[0] == 'r' || w[0] == 'c')) {
 				if (strncmp(w, "resync", 4)==0)
 					ent->resync = 1;
@@ -258,10 +258,10 @@ struct mdstat_ent *mdstat_read(int hold,
 					ent->resync = 3;
 
 				if (l > 8 && strcmp(w+l-8, "=DELAYED") == 0)
-					ent->percent = PROCESS_DELAYED;
+					ent->percent = RESYNC_DELAYED;
 				if (l > 8 && strcmp(w+l-8, "=PENDING") == 0)
-					ent->percent = PROCESS_PENDING;
-			} else if (ent->percent == -1 &&
+					ent->percent = RESYNC_PENDING;
+			} else if (ent->percent == RESYNC_NONE &&
 				   w[0] >= '0' &&
 				   w[0] <= '9' &&
 				   w[l-1] == '%') {
