Blame SOURCES/0030-mdadm-load-default-sysfs-attributes-after-assemblati.patch

2910d5
From b06815989179e0f153e44e4336290e655edce9a1 Mon Sep 17 00:00:00 2001
2910d5
From: Mariusz Dabrowski <mariusz.dabrowski@intel.com>
2910d5
Date: Wed, 10 Jul 2019 13:38:53 +0200
2910d5
Subject: [RHEL7.8 PATCH V2 30/47] mdadm: load default sysfs attributes after
2910d5
 assemblation
2910d5
2910d5
Added new type of line to mdadm.conf which allows to specify values of
2910d5
sysfs attributes for MD devices that should be loaded after the array is
2910d5
assembled. Each line is interpreted as list of structures containing
2910d5
sysname of MD device (md126 etc.) and list of sysfs attributes and their
2910d5
values.
2910d5
2910d5
Signed-off-by: Mariusz Dabrowski <mariusz.dabrowski@intel.com>
2910d5
Signed-off-by: Krzysztof Smolinski <krzysztof.smolinski@intel.com>
2910d5
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
2910d5
---
2910d5
 Assemble.c    |  12 +++--
2910d5
 Incremental.c |   1 +
2910d5
 config.c      |   7 ++-
2910d5
 mdadm.conf.5  |  25 ++++++++++
2910d5
 mdadm.h       |   3 ++
2910d5
 sysfs.c       | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2910d5
 6 files changed, 202 insertions(+), 4 deletions(-)
2910d5
2910d5
diff --git a/Assemble.c b/Assemble.c
2910d5
index 420c7b3..b2e6914 100644
2910d5
--- a/Assemble.c
2910d5
+++ b/Assemble.c
2910d5
@@ -1063,9 +1063,12 @@ static int start_array(int mdfd,
2910d5
 			       mddev, okcnt + sparecnt + journalcnt,
2910d5
 			       okcnt + sparecnt + journalcnt == 1 ? "" : "s");
2910d5
 			if (okcnt < (unsigned)content->array.raid_disks)
2910d5
-				fprintf(stderr, " (out of %d)",
2910d5
+				fprintf(stderr, " (out of %d)\n",
2910d5
 					content->array.raid_disks);
2910d5
-			fprintf(stderr, "\n");
2910d5
+			else {
2910d5
+				fprintf(stderr, "\n");
2910d5
+				sysfs_rules_apply(mddev, content);
2910d5
+			}
2910d5
 		}
2910d5
 
2910d5
 		if (st->ss->validate_container) {
2910d5
@@ -1139,6 +1142,7 @@ static int start_array(int mdfd,
2910d5
 			rv = ioctl(mdfd, RUN_ARRAY, NULL);
2910d5
 		reopen_mddev(mdfd); /* drop O_EXCL */
2910d5
 		if (rv == 0) {
2910d5
+			sysfs_rules_apply(mddev, content);
2910d5
 			if (c->verbose >= 0) {
2910d5
 				pr_err("%s has been started with %d drive%s",
2910d5
 				       mddev, okcnt, okcnt==1?"":"s");
2910d5
@@ -2130,10 +2134,12 @@ int assemble_container_content(struct supertype *st, int mdfd,
2910d5
 			pr_err("array %s now has %d device%s",
2910d5
 			       chosen_name, working + preexist,
2910d5
 			       working + preexist == 1 ? "":"s");
2910d5
-		else
2910d5
+		else {
2910d5
+			sysfs_rules_apply(chosen_name, content);
2910d5
 			pr_err("Started %s with %d device%s",
2910d5
 			       chosen_name, working + preexist,
2910d5
 			       working + preexist == 1 ? "":"s");
2910d5
+		}
2910d5
 		if (preexist)
2910d5
 			fprintf(stderr, " (%d new)", working);
2910d5
 		if (expansion)
2910d5
diff --git a/Incremental.c b/Incremental.c
2910d5
index d4d3c35..98dbcd9 100644
2910d5
--- a/Incremental.c
2910d5
+++ b/Incremental.c
2910d5
@@ -480,6 +480,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
2910d5
 			pr_err("container %s now has %d device%s\n",
2910d5
 			       chosen_name, info.array.working_disks,
2910d5
 			       info.array.working_disks == 1?"":"s");
2910d5
+		sysfs_rules_apply(chosen_name, &info;;
2910d5
 		wait_for(chosen_name, mdfd);
2910d5
 		if (st->ss->external)
2910d5
 			strcpy(devnm, fd2devnm(mdfd));
2910d5
diff --git a/config.c b/config.c
2910d5
index e14eae0..7592b2d 100644
2910d5
--- a/config.c
2910d5
+++ b/config.c
2910d5
@@ -80,7 +80,8 @@ char DefaultAltConfFile[] = CONFFILE2;
2910d5
 char DefaultAltConfDir[] = CONFFILE2 ".d";
2910d5
 
2910d5
 enum linetype { Devices, Array, Mailaddr, Mailfrom, Program, CreateDev,
2910d5
-		Homehost, HomeCluster, AutoMode, Policy, PartPolicy, LTEnd };
2910d5
+		Homehost, HomeCluster, AutoMode, Policy, PartPolicy, Sysfs,
2910d5
+		LTEnd };
2910d5
 char *keywords[] = {
2910d5
 	[Devices]  = "devices",
2910d5
 	[Array]    = "array",
2910d5
@@ -93,6 +94,7 @@ char *keywords[] = {
2910d5
 	[AutoMode] = "auto",
2910d5
 	[Policy]   = "policy",
2910d5
 	[PartPolicy]="part-policy",
2910d5
+	[Sysfs]    = "sysfs",
2910d5
 	[LTEnd]    = NULL
2910d5
 };
2910d5
 
2910d5
@@ -764,6 +766,9 @@ void conf_file(FILE *f)
2910d5
 		case PartPolicy:
2910d5
 			policyline(line, rule_part);
2910d5
 			break;
2910d5
+		case Sysfs:
2910d5
+			sysfsline(line);
2910d5
+			break;
2910d5
 		default:
2910d5
 			pr_err("Unknown keyword %s\n", line);
2910d5
 		}
2910d5
diff --git a/mdadm.conf.5 b/mdadm.conf.5
2910d5
index 47c962a..27dbab1 100644
2910d5
--- a/mdadm.conf.5
2910d5
+++ b/mdadm.conf.5
2910d5
@@ -587,6 +587,26 @@ be based on the domain, but with
2910d5
 appended, when N is the partition number for the partition that was
2910d5
 found.
2910d5
 
2910d5
+.TP
2910d5
+.B SYSFS
2910d5
+The SYSFS line lists custom values of MD device's sysfs attributes which will be
2910d5
+stored in sysfs after the array is assembled. Multiple lines are allowed and each
2910d5
+line has to contain the uuid or the name of the device to which it relates.
2910d5
+.RS 4
2910d5
+.TP
2910d5
+.B uuid=
2910d5
+hexadecimal identifier of MD device. This has to match the uuid stored in the
2910d5
+superblock.
2910d5
+.TP
2910d5
+.B name=
2910d5
+name of the MD device as was given to
2910d5
+.I mdadm
2910d5
+when the array was created. It will be ignored if
2910d5
+.B uuid
2910d5
+is not empty.
2910d5
+.TP
2910d5
+.RS 7
2910d5
+
2910d5
 .SH EXAMPLE
2910d5
 DEVICE /dev/sd[bcdjkl]1
2910d5
 .br
2910d5
@@ -657,6 +677,11 @@ CREATE group=system mode=0640 auto=part\-8
2910d5
 HOMEHOST <system>
2910d5
 .br
2910d5
 AUTO +1.x homehost \-all
2910d5
+.br
2910d5
+SYSFS name=/dev/md/raid5 group_thread_cnt=4 sync_speed_max=1000000
2910d5
+.br
2910d5
+SYSFS uuid=bead5eb6:31c17a27:da120ba2:7dfda40d group_thread_cnt=4
2910d5
+sync_speed_max=1000000
2910d5
 
2910d5
 .SH SEE ALSO
2910d5
 .BR mdadm (8),
2910d5
diff --git a/mdadm.h b/mdadm.h
2910d5
index 0fa9e1b..c36d7fd 100644
2910d5
--- a/mdadm.h
2910d5
+++ b/mdadm.h
2910d5
@@ -1322,6 +1322,9 @@ void domain_add(struct domainlist **domp, char *domain);
2910d5
 extern void policy_save_path(char *id_path, struct map_ent *array);
2910d5
 extern int policy_check_path(struct mdinfo *disk, struct map_ent *array);
2910d5
 
2910d5
+extern void sysfs_rules_apply(char *devnm, struct mdinfo *dev);
2910d5
+extern void sysfsline(char *line);
2910d5
+
2910d5
 #if __GNUC__ < 3
2910d5
 struct stat64;
2910d5
 #endif
2910d5
diff --git a/sysfs.c b/sysfs.c
2910d5
index 2dd9ab6..c313781 100644
2910d5
--- a/sysfs.c
2910d5
+++ b/sysfs.c
2910d5
@@ -26,9 +26,22 @@
2910d5
 #include	"mdadm.h"
2910d5
 #include	<dirent.h>
2910d5
 #include	<ctype.h>
2910d5
+#include	"dlink.h"
2910d5
 
2910d5
 #define MAX_SYSFS_PATH_LEN	120
2910d5
 
2910d5
+struct dev_sysfs_rule {
2910d5
+	struct dev_sysfs_rule *next;
2910d5
+	char *devname;
2910d5
+	int uuid[4];
2910d5
+	int uuid_set;
2910d5
+	struct sysfs_entry {
2910d5
+		struct sysfs_entry *next;
2910d5
+		char *name;
2910d5
+		char *value;
2910d5
+	} *entry;
2910d5
+};
2910d5
+
2910d5
 int load_sys(char *path, char *buf, int len)
2910d5
 {
2910d5
 	int fd = open(path, O_RDONLY);
2910d5
@@ -999,3 +1012,148 @@ int sysfs_wait(int fd, int *msec)
2910d5
 	}
2910d5
 	return n;
2910d5
 }
2910d5
+
2910d5
+int sysfs_rules_apply_check(const struct mdinfo *sra,
2910d5
+			    const struct sysfs_entry *ent)
2910d5
+{
2910d5
+	/* Check whether parameter is regular file,
2910d5
+	 * exists and is under specified directory.
2910d5
+	 */
2910d5
+	char fname[MAX_SYSFS_PATH_LEN];
2910d5
+	char dname[MAX_SYSFS_PATH_LEN];
2910d5
+	char resolved_path[PATH_MAX];
2910d5
+	char resolved_dir[PATH_MAX];
2910d5
+
2910d5
+	if (sra == NULL || ent == NULL)
2910d5
+		return -1;
2910d5
+
2910d5
+	snprintf(dname, MAX_SYSFS_PATH_LEN, "/sys/block/%s/md/", sra->sys_name);
2910d5
+	snprintf(fname, MAX_SYSFS_PATH_LEN, "%s/%s", dname, ent->name);
2910d5
+
2910d5
+	if (realpath(fname, resolved_path) == NULL ||
2910d5
+	    realpath(dname, resolved_dir) == NULL)
2910d5
+		return -1;
2910d5
+
2910d5
+	if (strncmp(resolved_dir, resolved_path,
2910d5
+		    strnlen(resolved_dir, PATH_MAX)) != 0)
2910d5
+		return -1;
2910d5
+
2910d5
+	return 0;
2910d5
+}
2910d5
+
2910d5
+static struct dev_sysfs_rule *sysfs_rules;
2910d5
+
2910d5
+void sysfs_rules_apply(char *devnm, struct mdinfo *dev)
2910d5
+{
2910d5
+	struct dev_sysfs_rule *rules = sysfs_rules;
2910d5
+
2910d5
+	while (rules) {
2910d5
+		struct sysfs_entry *ent = rules->entry;
2910d5
+		int match  = 0;
2910d5
+
2910d5
+		if (!rules->uuid_set) {
2910d5
+			if (rules->devname)
2910d5
+				match = strcmp(devnm, rules->devname) == 0;
2910d5
+		} else {
2910d5
+			match = memcmp(dev->uuid, rules->uuid,
2910d5
+				       sizeof(int[4])) == 0;
2910d5
+		}
2910d5
+
2910d5
+		while (match && ent) {
2910d5
+			if (sysfs_rules_apply_check(dev, ent) < 0)
2910d5
+				pr_err("SYSFS: failed to write '%s' to '%s'\n",
2910d5
+					ent->value, ent->name);
2910d5
+			else
2910d5
+				sysfs_set_str(dev, NULL, ent->name, ent->value);
2910d5
+			ent = ent->next;
2910d5
+		}
2910d5
+		rules = rules->next;
2910d5
+	}
2910d5
+}
2910d5
+
2910d5
+static void sysfs_rule_free(struct dev_sysfs_rule *rule)
2910d5
+{
2910d5
+	struct sysfs_entry *entry;
2910d5
+
2910d5
+	while (rule) {
2910d5
+		struct dev_sysfs_rule *tmp = rule->next;
2910d5
+
2910d5
+		entry = rule->entry;
2910d5
+		while (entry) {
2910d5
+			struct sysfs_entry *tmp = entry->next;
2910d5
+
2910d5
+			free(entry->name);
2910d5
+			free(entry->value);
2910d5
+			free(entry);
2910d5
+			entry = tmp;
2910d5
+		}
2910d5
+
2910d5
+		if (rule->devname)
2910d5
+			free(rule->devname);
2910d5
+		free(rule);
2910d5
+		rule = tmp;
2910d5
+	}
2910d5
+}
2910d5
+
2910d5
+void sysfsline(char *line)
2910d5
+{
2910d5
+	struct dev_sysfs_rule *sr;
2910d5
+	char *w;
2910d5
+
2910d5
+	sr = xcalloc(1, sizeof(*sr));
2910d5
+	for (w = dl_next(line); w != line ; w = dl_next(w)) {
2910d5
+		if (strncasecmp(w, "name=", 5) == 0) {
2910d5
+			char *devname = w + 5;
2910d5
+
2910d5
+			if (strncmp(devname, "/dev/md/", 8) == 0) {
2910d5
+				if (sr->devname)
2910d5
+					pr_err("Only give one device per SYSFS line: %s\n",
2910d5
+						devname);
2910d5
+				else
2910d5
+					sr->devname = xstrdup(devname);
2910d5
+			} else {
2910d5
+				pr_err("%s is an invalid name for an md device - ignored.\n",
2910d5
+				       devname);
2910d5
+			}
2910d5
+		} else if (strncasecmp(w, "uuid=", 5) == 0) {
2910d5
+			char *uuid = w + 5;
2910d5
+
2910d5
+			if (sr->uuid_set) {
2910d5
+				pr_err("Only give one uuid per SYSFS line: %s\n",
2910d5
+					uuid);
2910d5
+			} else {
2910d5
+				if (parse_uuid(w + 5, sr->uuid) &&
2910d5
+				    memcmp(sr->uuid, uuid_zero,
2910d5
+					   sizeof(int[4])) != 0)
2910d5
+					sr->uuid_set = 1;
2910d5
+				else
2910d5
+					pr_err("Invalid uuid: %s\n", uuid);
2910d5
+			}
2910d5
+		} else {
2910d5
+			struct sysfs_entry *prop;
2910d5
+
2910d5
+			char *sep = strchr(w, '=');
2910d5
+
2910d5
+			if (sep == NULL || *(sep + 1) == 0) {
2910d5
+				pr_err("Cannot parse \"%s\" - ignoring.\n", w);
2910d5
+				continue;
2910d5
+			}
2910d5
+
2910d5
+			prop = xmalloc(sizeof(*prop));
2910d5
+			prop->value = xstrdup(sep + 1);
2910d5
+			*sep = 0;
2910d5
+			prop->name = xstrdup(w);
2910d5
+			prop->next = sr->entry;
2910d5
+			sr->entry = prop;
2910d5
+		}
2910d5
+	}
2910d5
+
2910d5
+	if (!sr->devname && !sr->uuid_set) {
2910d5
+		pr_err("Device name not found in sysfs config entry - ignoring.\n");
2910d5
+		sysfs_rule_free(sr);
2910d5
+		return;
2910d5
+	}
2910d5
+
2910d5
+	sr->next = sysfs_rules;
2910d5
+	sysfs_rules = sr;
2910d5
+}
2910d5
-- 
2910d5
2.7.5
2910d5