File 0089-sysfs-Parse-array_state-in-sysfs_read.patch of Package mdadm.14070

From 5e4ca8bb82e98400c9258cb3d7e4d030576f21df Mon Sep 17 00:00:00 2001
From: Jes Sorensen <jsorensen@fb.com>
Date: Wed, 19 Apr 2017 23:27:58 -0400
Subject: [PATCH] sysfs: Parse array_state in sysfs_read()
Git-commit: 5e4ca8bb82e98400c9258cb3d7e4d030576f21df
Patch-mainline: mdadm-4.0+
References: bsc#1069165, bsc#1069167, bsc#1068030

Rather than copying in the array_state string, parse it and use an
enum to indicate the state.

Signed-off-by: Jes Sorensen <jsorensen@fb.com>
Signed-off-by: Coly Li <colyli@suse.de>

---
 Manage.c |  2 +-
 maps.c   | 17 +++++++++++++++++
 mdadm.h  | 17 ++++++++++++++---
 sysfs.c  |  9 +++++----
 4 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/Manage.c b/Manage.c
index bb84d28..8966e33 100644
--- a/Manage.c
+++ b/Manage.c
@@ -929,7 +929,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
 			return -1;
 		}
 
-		if (strncmp(mdp->sysfs_array_state, "readonly", 8) != 0) {
+		if (mdp->array_state != ARRAY_READONLY) {
 			sysfs_free(mdp);
 			pr_err("%s is not readonly, cannot add journal.\n", devname);
 			return -1;
diff --git a/maps.c b/maps.c
index d9ee7de..a8a4639 100644
--- a/maps.c
+++ b/maps.c
@@ -139,6 +139,23 @@ mapping_t consistency_policies[] = {
 	{ NULL, 0}
 };
 
+mapping_t sysfs_array_states[] = {
+	/*
+	 * Beware map_name() uses strcmp() so active-idle must come before
+	 * active, to be detected correctly.
+	 */
+	{ "active-idle", ARRAY_ACTIVE_IDLE },
+	{ "active", ARRAY_ACTIVE },
+	{ "clear", ARRAY_CLEAR },
+	{ "inactive", ARRAY_INACTIVE },
+	{ "suspended", ARRAY_SUSPENDED },
+	{ "readonly", ARRAY_READONLY },
+	{ "read-auto", ARRAY_READ_AUTO },
+	{ "clean", ARRAY_CLEAN },
+	{ "write-pending", ARRAY_WRITE_PENDING },
+	{ NULL, 0 }
+};
+
 char *map_num(mapping_t *map, int num)
 {
 	while (map->name) {
diff --git a/mdadm.h b/mdadm.h
index f1f643c..a379973 100644
--- a/mdadm.h
+++ b/mdadm.h
@@ -335,8 +335,18 @@ struct mdinfo {
 	int prev_state, curr_state, next_state;
 
 	/* info read from sysfs */
-	char		sysfs_array_state[20];
-
+	enum {
+		ARRAY_CLEAR,
+		ARRAY_INACTIVE,
+		ARRAY_SUSPENDED,
+		ARRAY_READONLY,
+		ARRAY_READ_AUTO,
+		ARRAY_CLEAN,
+		ARRAY_ACTIVE,
+		ARRAY_WRITE_PENDING,
+		ARRAY_ACTIVE_IDLE,
+		ARRAY_UNKNOWN_STATE,
+	} array_state;
 	struct md_bb bb;
 };
 
@@ -716,7 +726,8 @@ extern int restore_stripes(int *dest, unsigned long long *offsets,
 
 extern char *map_num(mapping_t *map, int num);
 extern int map_name(mapping_t *map, char *name);
-extern mapping_t r5layout[], r6layout[], pers[], modes[], faultylayout[], consistency_policies[];
+extern mapping_t r5layout[], r6layout[], pers[], modes[], faultylayout[];
+extern mapping_t consistency_policies[], sysfs_array_states[];
 
 extern char *map_dev_preferred(int major, int minor, int create,
 			       char *prefer);
diff --git a/sysfs.c b/sysfs.c
index 51deb23..c6df9b0 100644
--- a/sysfs.c
+++ b/sysfs.c
@@ -247,11 +247,12 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
 
 	if (options & GET_ARRAY_STATE) {
 		strcpy(base, "array_state");
-		if (load_sys(fname, sra->sysfs_array_state,
-			     sizeof(sra->sysfs_array_state)))
+		if (load_sys(fname, buf, sizeof(buf)))
 			goto abort;
-	} else
-		sra->sysfs_array_state[0] = 0;
+		sra->array_state = map_name(sysfs_array_states, buf);
+		if (sra->array_state == UnSet)
+			sra->array_state = ARRAY_UNKNOWN_STATE;
+	}
 
 	if (options & GET_CONSISTENCY_POLICY) {
 		strcpy(base, "consistency_policy");
-- 
2.13.6

openSUSE Build Service is sponsored by