dcavalca / rpms / mdadm

Forked from rpms/mdadm 3 years ago
Clone

Blame SOURCES/0003-policy-support-devices-with-multiple-paths.patch

c8f3db
From cd72f9d114da206baa01fd56ff2d8ffcc08f3239 Mon Sep 17 00:00:00 2001
c8f3db
From: NeilBrown <neilb@suse.com>
c8f3db
Date: Fri, 9 Nov 2018 17:12:33 +1100
c8f3db
Subject: [RHEL7.7 PATCH 03/21] policy: support devices with multiple paths.
c8f3db
c8f3db
As new releases of Linux some time change the name of
c8f3db
a path, some distros keep "legacy" names as well.  This
c8f3db
is useful, but confuses mdadm which assumes each device has
c8f3db
precisely one path.
c8f3db
c8f3db
So change this assumption:  allow a disk to have several
c8f3db
paths, and allow any to match when looking for a policy
c8f3db
which matches a disk.
c8f3db
c8f3db
Reported-and-tested-by: Mariusz Tkaczyk <mariusz.tkaczyk@intel.com>
c8f3db
Signed-off-by: NeilBrown <neilb@suse.com>
c8f3db
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
c8f3db
---
c8f3db
 Incremental.c |   5 +-
c8f3db
 mdadm.h       |   2 +-
c8f3db
 policy.c      | 163 ++++++++++++++++++++++++++++++++--------------------------
c8f3db
 3 files changed, 95 insertions(+), 75 deletions(-)
c8f3db
c8f3db
diff --git a/Incremental.c b/Incremental.c
c8f3db
index a4ff7d4..d4d3c35 100644
c8f3db
--- a/Incremental.c
c8f3db
+++ b/Incremental.c
c8f3db
@@ -1080,6 +1080,7 @@ static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
c8f3db
 		struct supertype *st2 = NULL;
c8f3db
 		char *devname = NULL;
c8f3db
 		unsigned long long devsectors;
c8f3db
+		char *pathlist[2];
c8f3db
 
c8f3db
 		if (de->d_ino == 0 || de->d_name[0] == '.' ||
c8f3db
 		    (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
c8f3db
@@ -1094,7 +1095,9 @@ static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
c8f3db
 			/* This is a partition - skip it */
c8f3db
 			goto next;
c8f3db
 
c8f3db
-		pol2 = path_policy(de->d_name, type_disk);
c8f3db
+		pathlist[0] = de->d_name;
c8f3db
+		pathlist[1] = NULL;
c8f3db
+		pol2 = path_policy(pathlist, type_disk);
c8f3db
 
c8f3db
 		domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
c8f3db
 		if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
c8f3db
diff --git a/mdadm.h b/mdadm.h
c8f3db
index 387e681..705bd9b 100644
c8f3db
--- a/mdadm.h
c8f3db
+++ b/mdadm.h
c8f3db
@@ -1247,7 +1247,7 @@ extern void policyline(char *line, char *type);
c8f3db
 extern void policy_add(char *type, ...);
c8f3db
 extern void policy_free(void);
c8f3db
 
c8f3db
-extern struct dev_policy *path_policy(char *path, char *type);
c8f3db
+extern struct dev_policy *path_policy(char **paths, char *type);
c8f3db
 extern struct dev_policy *disk_policy(struct mdinfo *disk);
c8f3db
 extern struct dev_policy *devid_policy(int devid);
c8f3db
 extern void dev_policy_free(struct dev_policy *p);
c8f3db
diff --git a/policy.c b/policy.c
c8f3db
index 258f393..fa67d55 100644
c8f3db
--- a/policy.c
c8f3db
+++ b/policy.c
c8f3db
@@ -189,15 +189,17 @@ struct dev_policy *pol_find(struct dev_policy *pol, char *name)
c8f3db
 	return pol;
c8f3db
 }
c8f3db
 
c8f3db
-static char *disk_path(struct mdinfo *disk)
c8f3db
+static char **disk_paths(struct mdinfo *disk)
c8f3db
 {
c8f3db
 	struct stat stb;
c8f3db
 	int prefix_len;
c8f3db
 	DIR *by_path;
c8f3db
 	char symlink[PATH_MAX] = "/dev/disk/by-path/";
c8f3db
-	char nm[PATH_MAX];
c8f3db
+	char **paths;
c8f3db
+	int cnt = 0;
c8f3db
 	struct dirent *ent;
c8f3db
-	int rv;
c8f3db
+
c8f3db
+	paths = xmalloc(sizeof(*paths) * (cnt+1));
c8f3db
 
c8f3db
 	by_path = opendir(symlink);
c8f3db
 	if (by_path) {
c8f3db
@@ -214,22 +216,13 @@ static char *disk_path(struct mdinfo *disk)
c8f3db
 				continue;
c8f3db
 			if (stb.st_rdev != makedev(disk->disk.major, disk->disk.minor))
c8f3db
 				continue;
c8f3db
-			closedir(by_path);
c8f3db
-			return xstrdup(ent->d_name);
c8f3db
+			paths[cnt++] = xstrdup(ent->d_name);
c8f3db
+			paths = xrealloc(paths, sizeof(*paths) * (cnt+1));
c8f3db
 		}
c8f3db
 		closedir(by_path);
c8f3db
 	}
c8f3db
-	/* A NULL path isn't really acceptable - use the devname.. */
c8f3db
-	sprintf(symlink, "/sys/dev/block/%d:%d", disk->disk.major, disk->disk.minor);
c8f3db
-	rv = readlink(symlink, nm, sizeof(nm)-1);
c8f3db
-	if (rv > 0) {
c8f3db
-		char *dname;
c8f3db
-		nm[rv] = 0;
c8f3db
-		dname = strrchr(nm, '/');
c8f3db
-		if (dname)
c8f3db
-			return xstrdup(dname + 1);
c8f3db
-	}
c8f3db
-	return xstrdup("unknown");
c8f3db
+	paths[cnt] = NULL;
c8f3db
+	return paths;
c8f3db
 }
c8f3db
 
c8f3db
 char type_part[] = "part";
c8f3db
@@ -246,18 +239,53 @@ static char *disk_type(struct mdinfo *disk)
c8f3db
 		return type_disk;
c8f3db
 }
c8f3db
 
c8f3db
-static int pol_match(struct rule *rule, char *path, char *type)
c8f3db
+static int path_has_part(char *path, char **part)
c8f3db
+{
c8f3db
+	/* check if path ends with "-partNN" and
c8f3db
+	 * if it does, place a pointer to "-pathNN"
c8f3db
+	 * in 'part'.
c8f3db
+	 */
c8f3db
+	int l;
c8f3db
+	if (!path)
c8f3db
+		return 0;
c8f3db
+	l = strlen(path);
c8f3db
+	while (l > 1 && isdigit(path[l-1]))
c8f3db
+		l--;
c8f3db
+	if (l < 5 || strncmp(path+l-5, "-part", 5) != 0)
c8f3db
+		return 0;
c8f3db
+	*part = path+l-5;
c8f3db
+	return 1;
c8f3db
+}
c8f3db
+
c8f3db
+static int pol_match(struct rule *rule, char **paths, char *type, char **part)
c8f3db
 {
c8f3db
-	/* check if this rule matches on path and type */
c8f3db
+	/* Check if this rule matches on any path and type.
c8f3db
+	 * If 'part' is not NULL, then 'path' must end in -partN, which
c8f3db
+	 * we ignore for matching, and return in *part on success.
c8f3db
+	 */
c8f3db
 	int pathok = 0; /* 0 == no path, 1 == match, -1 == no match yet */
c8f3db
 	int typeok = 0;
c8f3db
 
c8f3db
-	while (rule) {
c8f3db
+	for (; rule; rule = rule->next) {
c8f3db
 		if (rule->name == rule_path) {
c8f3db
+			char *p;
c8f3db
+			int i;
c8f3db
 			if (pathok == 0)
c8f3db
 				pathok = -1;
c8f3db
-			if (path && fnmatch(rule->value, path, 0) == 0)
c8f3db
-				pathok = 1;
c8f3db
+			if (!paths)
c8f3db
+				continue;
c8f3db
+			for (i = 0; paths[i]; i++) {
c8f3db
+				if (part) {
c8f3db
+					if (!path_has_part(paths[i], &p))
c8f3db
+						continue;
c8f3db
+					*p = '\0';
c8f3db
+					*part = p+1;
c8f3db
+				}
c8f3db
+				if (fnmatch(rule->value, paths[i], 0) == 0)
c8f3db
+					pathok = 1;
c8f3db
+				if (part)
c8f3db
+					*p = '-';
c8f3db
+			}
c8f3db
 		}
c8f3db
 		if (rule->name == rule_type) {
c8f3db
 			if (typeok == 0)
c8f3db
@@ -265,7 +293,6 @@ static int pol_match(struct rule *rule, char *path, char *type)
c8f3db
 			if (type && strcmp(rule->value, type) == 0)
c8f3db
 				typeok = 1;
c8f3db
 		}
c8f3db
-		rule = rule->next;
c8f3db
 	}
c8f3db
 	return pathok >= 0 && typeok >= 0;
c8f3db
 }
c8f3db
@@ -286,24 +313,6 @@ static void pol_merge(struct dev_policy **pol, struct rule *rule)
c8f3db
 			pol_new(pol, r->name, r->value, metadata);
c8f3db
 }
c8f3db
 
c8f3db
-static int path_has_part(char *path, char **part)
c8f3db
-{
c8f3db
-	/* check if path ends with "-partNN" and
c8f3db
-	 * if it does, place a pointer to "-pathNN"
c8f3db
-	 * in 'part'.
c8f3db
-	 */
c8f3db
-	int l;
c8f3db
-	if (!path)
c8f3db
-		return 0;
c8f3db
-	l = strlen(path);
c8f3db
-	while (l > 1 && isdigit(path[l-1]))
c8f3db
-		l--;
c8f3db
-	if (l < 5 || strncmp(path+l-5, "-part", 5) != 0)
c8f3db
-		return 0;
c8f3db
-	*part = path+l-5;
c8f3db
-	return 1;
c8f3db
-}
c8f3db
-
c8f3db
 static void pol_merge_part(struct dev_policy **pol, struct rule *rule, char *part)
c8f3db
 {
c8f3db
 	/* copy any name assignments from rule into pol, appending
c8f3db
@@ -352,7 +361,7 @@ static int config_rules_has_path = 0;
c8f3db
  * path_policy() gathers policy information for the
c8f3db
  * disk described in the given a 'path' and a 'type'.
c8f3db
  */
c8f3db
-struct dev_policy *path_policy(char *path, char *type)
c8f3db
+struct dev_policy *path_policy(char **paths, char *type)
c8f3db
 {
c8f3db
 	struct pol_rule *rules;
c8f3db
 	struct dev_policy *pol = NULL;
c8f3db
@@ -361,27 +370,24 @@ struct dev_policy *path_policy(char *path, char *type)
c8f3db
 	rules = config_rules;
c8f3db
 
c8f3db
 	while (rules) {
c8f3db
-		char *part;
c8f3db
+		char *part = NULL;
c8f3db
 		if (rules->type == rule_policy)
c8f3db
-			if (pol_match(rules->rule, path, type))
c8f3db
+			if (pol_match(rules->rule, paths, type, NULL))
c8f3db
 				pol_merge(&pol, rules->rule);
c8f3db
 		if (rules->type == rule_part && strcmp(type, type_part) == 0)
c8f3db
-			if (path_has_part(path, &part)) {
c8f3db
-				*part = 0;
c8f3db
-				if (pol_match(rules->rule, path, type_disk))
c8f3db
-					pol_merge_part(&pol, rules->rule, part+1);
c8f3db
-				*part = '-';
c8f3db
-			}
c8f3db
+			if (pol_match(rules->rule, paths, type_disk, &part))
c8f3db
+					pol_merge_part(&pol, rules->rule, part);
c8f3db
 		rules = rules->next;
c8f3db
 	}
c8f3db
 
c8f3db
 	/* Now add any metadata-specific internal knowledge
c8f3db
 	 * about this path
c8f3db
 	 */
c8f3db
-	for (i=0; path && superlist[i]; i++)
c8f3db
+	for (i=0; paths[0] && superlist[i]; i++)
c8f3db
 		if (superlist[i]->get_disk_controller_domain) {
c8f3db
 			const char *d =
c8f3db
-				superlist[i]->get_disk_controller_domain(path);
c8f3db
+				superlist[i]->get_disk_controller_domain(
c8f3db
+					paths[0]);
c8f3db
 			if (d)
c8f3db
 				pol_new(&pol, pol_domain, d, superlist[i]->name);
c8f3db
 		}
c8f3db
@@ -400,22 +406,34 @@ void pol_add(struct dev_policy **pol,
c8f3db
 	pol_dedup(*pol);
c8f3db
 }
c8f3db
 
c8f3db
+static void free_paths(char **paths)
c8f3db
+{
c8f3db
+	int i;
c8f3db
+
c8f3db
+	if (!paths)
c8f3db
+		return;
c8f3db
+
c8f3db
+	for (i = 0; paths[i]; i++)
c8f3db
+		free(paths[i]);
c8f3db
+	free(paths);
c8f3db
+}
c8f3db
+
c8f3db
 /*
c8f3db
  * disk_policy() gathers policy information for the
c8f3db
  * disk described in the given mdinfo (disk.{major,minor}).
c8f3db
  */
c8f3db
 struct dev_policy *disk_policy(struct mdinfo *disk)
c8f3db
 {
c8f3db
-	char *path = NULL;
c8f3db
+	char **paths = NULL;
c8f3db
 	char *type = disk_type(disk);
c8f3db
 	struct dev_policy *pol = NULL;
c8f3db
 
c8f3db
 	if (config_rules_has_path)
c8f3db
-		path = disk_path(disk);
c8f3db
+		paths = disk_paths(disk);
c8f3db
 
c8f3db
-	pol = path_policy(path, type);
c8f3db
+	pol = path_policy(paths, type);
c8f3db
 
c8f3db
-	free(path);
c8f3db
+	free_paths(paths);
c8f3db
 	return pol;
c8f3db
 }
c8f3db
 
c8f3db
@@ -756,27 +774,26 @@ int policy_check_path(struct mdinfo *disk, struct map_ent *array)
c8f3db
 {
c8f3db
 	char path[PATH_MAX];
c8f3db
 	FILE *f = NULL;
c8f3db
-	char *id_path = disk_path(disk);
c8f3db
-	int rv;
c8f3db
+	char **id_paths = disk_paths(disk);
c8f3db
+	int i;
c8f3db
+	int rv = 0;
c8f3db
 
c8f3db
-	if (!id_path)
c8f3db
-		return 0;
c8f3db
+	for (i = 0; id_paths[i]; i++) {
c8f3db
+		snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_paths[i]);
c8f3db
+		f = fopen(path, "r");
c8f3db
+		if (!f)
c8f3db
+			continue;
c8f3db
 
c8f3db
-	snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_path);
c8f3db
-	f = fopen(path, "r");
c8f3db
-	if (!f) {
c8f3db
-		free(id_path);
c8f3db
-		return 0;
c8f3db
+		rv = fscanf(f, " %s %x:%x:%x:%x\n",
c8f3db
+			    array->metadata,
c8f3db
+			    array->uuid,
c8f3db
+			    array->uuid+1,
c8f3db
+			    array->uuid+2,
c8f3db
+			    array->uuid+3);
c8f3db
+		fclose(f);
c8f3db
+		break;
c8f3db
 	}
c8f3db
-
c8f3db
-	rv = fscanf(f, " %s %x:%x:%x:%x\n",
c8f3db
-		    array->metadata,
c8f3db
-		    array->uuid,
c8f3db
-		    array->uuid+1,
c8f3db
-		    array->uuid+2,
c8f3db
-		    array->uuid+3);
c8f3db
-	fclose(f);
c8f3db
-	free(id_path);
c8f3db
+	free_paths(id_paths);
c8f3db
 	return rv == 5;
c8f3db
 }
c8f3db
 
c8f3db
-- 
c8f3db
2.7.5
c8f3db