Blame SOURCES/0067-mdadm-Add-option-validation-for-update-subarray.patch

91179e
From 2568ce89ea5c26225e8984733adc2ea7559d853a Mon Sep 17 00:00:00 2001
91179e
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
91179e
Date: Mon, 2 Jan 2023 09:35:15 +0100
91179e
Subject: [PATCH 67/83] mdadm: Add option validation for --update-subarray
91179e
91179e
Subset of options available for "--update" is not same as for "--update-subarray".
91179e
Define maps and enum for update options and use them instead of direct comparisons.
91179e
Add proper error message.
91179e
91179e
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
91179e
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
91179e
---
91179e
 ReadMe.c |  31 +++++++++++++++++
91179e
 maps.c   |  31 +++++++++++++++++
91179e
 mdadm.c  | 104 +++++++++++++++++--------------------------------------
91179e
 mdadm.h  |  32 ++++++++++++++++-
91179e
 4 files changed, 124 insertions(+), 74 deletions(-)
91179e
91179e
diff --git a/ReadMe.c b/ReadMe.c
91179e
index 50a5e36d..bd8d50d2 100644
91179e
--- a/ReadMe.c
91179e
+++ b/ReadMe.c
91179e
@@ -655,3 +655,34 @@ char *mode_help[mode_count] = {
91179e
 	[GROW]		= Help_grow,
91179e
 	[INCREMENTAL]	= Help_incr,
91179e
 };
91179e
+
91179e
+/**
91179e
+ * fprint_update_options() - Print valid update options depending on the mode.
91179e
+ * @outf: File (output stream)
91179e
+ * @update_mode: Used to distinguish update and update_subarray
91179e
+ */
91179e
+void fprint_update_options(FILE *outf, enum update_opt update_mode)
91179e
+{
91179e
+	int counter = UOPT_NAME, breakpoint = UOPT_HELP;
91179e
+	mapping_t *map = update_options;
91179e
+
91179e
+	if (!outf)
91179e
+		return;
91179e
+	if (update_mode == UOPT_SUBARRAY_ONLY) {
91179e
+		breakpoint = UOPT_SUBARRAY_ONLY;
91179e
+		fprintf(outf, "Valid --update options for update-subarray are:\n\t");
91179e
+	} else
91179e
+		fprintf(outf, "Valid --update options are:\n\t");
91179e
+	while (map->num) {
91179e
+		if (map->num >= breakpoint)
91179e
+			break;
91179e
+		fprintf(outf, "'%s', ", map->name);
91179e
+		if (counter % 5 == 0)
91179e
+			fprintf(outf, "\n\t");
91179e
+		counter++;
91179e
+		map++;
91179e
+	}
91179e
+	if ((counter - 1) % 5)
91179e
+		fprintf(outf, "\n");
91179e
+	fprintf(outf, "\r");
91179e
+}
91179e
diff --git a/maps.c b/maps.c
91179e
index 20fcf719..b586679a 100644
91179e
--- a/maps.c
91179e
+++ b/maps.c
91179e
@@ -165,6 +165,37 @@ mapping_t sysfs_array_states[] = {
91179e
 	{ "broken", ARRAY_BROKEN },
91179e
 	{ NULL, ARRAY_UNKNOWN_STATE }
91179e
 };
91179e
+/**
91179e
+ * mapping_t update_options - stores supported update options.
91179e
+ */
91179e
+mapping_t update_options[] = {
91179e
+	{ "name", UOPT_NAME },
91179e
+	{ "ppl", UOPT_PPL },
91179e
+	{ "no-ppl", UOPT_NO_PPL },
91179e
+	{ "bitmap", UOPT_BITMAP },
91179e
+	{ "no-bitmap", UOPT_NO_BITMAP },
91179e
+	{ "sparc2.2", UOPT_SPARC22 },
91179e
+	{ "super-minor", UOPT_SUPER_MINOR },
91179e
+	{ "summaries", UOPT_SUMMARIES },
91179e
+	{ "resync", UOPT_RESYNC },
91179e
+	{ "uuid", UOPT_UUID },
91179e
+	{ "homehost", UOPT_HOMEHOST },
91179e
+	{ "home-cluster", UOPT_HOME_CLUSTER },
91179e
+	{ "nodes", UOPT_NODES },
91179e
+	{ "devicesize", UOPT_DEVICESIZE },
91179e
+	{ "bbl", UOPT_BBL },
91179e
+	{ "no-bbl", UOPT_NO_BBL },
91179e
+	{ "force-no-bbl", UOPT_FORCE_NO_BBL },
91179e
+	{ "metadata", UOPT_METADATA },
91179e
+	{ "revert-reshape", UOPT_REVERT_RESHAPE },
91179e
+	{ "layout-original", UOPT_LAYOUT_ORIGINAL },
91179e
+	{ "layout-alternate", UOPT_LAYOUT_ALTERNATE },
91179e
+	{ "layout-unspecified", UOPT_LAYOUT_UNSPECIFIED },
91179e
+	{ "byteorder", UOPT_BYTEORDER },
91179e
+	{ "help", UOPT_HELP },
91179e
+	{ "?", UOPT_HELP },
91179e
+	{ NULL, UOPT_UNDEFINED}
91179e
+};
91179e
 
91179e
 /**
91179e
  * map_num_s() - Safer alternative of map_num() function.
91179e
diff --git a/mdadm.c b/mdadm.c
91179e
index 74fdec31..f5f505fe 100644
91179e
--- a/mdadm.c
91179e
+++ b/mdadm.c
91179e
@@ -100,7 +100,7 @@ int main(int argc, char *argv[])
91179e
 	char *dump_directory = NULL;
91179e
 
91179e
 	int print_help = 0;
91179e
-	FILE *outf;
91179e
+	FILE *outf = NULL;
91179e
 
91179e
 	int mdfd = -1;
91179e
 	int locked = 0;
91179e
@@ -723,7 +723,11 @@ int main(int argc, char *argv[])
91179e
 			continue;
91179e
 
91179e
 		case O(ASSEMBLE,'U'): /* update the superblock */
91179e
-		case O(MISC,'U'):
91179e
+		case O(MISC,'U'): {
91179e
+			enum update_opt updateopt = map_name(update_options, c.update);
91179e
+			enum update_opt print_mode = UOPT_HELP;
91179e
+			const char *error_addon = "update option";
91179e
+
91179e
 			if (c.update) {
91179e
 				pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
91179e
 					c.update, optarg);
91179e
@@ -733,83 +737,37 @@ int main(int argc, char *argv[])
91179e
 				pr_err("Only subarrays can be updated in misc mode\n");
91179e
 				exit(2);
91179e
 			}
91179e
+
91179e
 			c.update = optarg;
91179e
-			if (strcmp(c.update, "sparc2.2") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "super-minor") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "summaries") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "resync") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "uuid") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "name") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "homehost") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "home-cluster") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "nodes") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "devicesize") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "bitmap") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "no-bitmap") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "bbl") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "no-bbl") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "force-no-bbl") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "ppl") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "no-ppl") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "metadata") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "revert-reshape") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "layout-original") == 0 ||
91179e
-			    strcmp(c.update, "layout-alternate") == 0 ||
91179e
-			    strcmp(c.update, "layout-unspecified") == 0)
91179e
-				continue;
91179e
-			if (strcmp(c.update, "byteorder") == 0) {
91179e
+
91179e
+			if (devmode == UpdateSubarray) {
91179e
+				print_mode = UOPT_SUBARRAY_ONLY;
91179e
+				error_addon = "update-subarray option";
91179e
+
91179e
+				if (updateopt > UOPT_SUBARRAY_ONLY && updateopt < UOPT_HELP)
91179e
+					updateopt = UOPT_UNDEFINED;
91179e
+			}
91179e
+
91179e
+			switch (updateopt) {
91179e
+			case UOPT_UNDEFINED:
91179e
+				pr_err("'--update=%s' is invalid %s. ",
91179e
+					c.update, error_addon);
91179e
+				outf = stderr;
91179e
+			case UOPT_HELP:
91179e
+				if (!outf)
91179e
+					outf = stdout;
91179e
+				fprint_update_options(outf, print_mode);
91179e
+				exit(outf == stdout ? 0 : 2);
91179e
+			case UOPT_BYTEORDER:
91179e
 				if (ss) {
91179e
 					pr_err("must not set metadata type with --update=byteorder.\n");
91179e
 					exit(2);
91179e
 				}
91179e
-				for(i = 0; !ss && superlist[i]; i++)
91179e
-					ss = superlist[i]->match_metadata_desc(
91179e
-						"0.swap");
91179e
-				if (!ss) {
91179e
-					pr_err("INTERNAL ERROR cannot find 0.swap\n");
91179e
-					exit(2);
91179e
-				}
91179e
-
91179e
-				continue;
91179e
+			default:
91179e
+				break;
91179e
 			}
91179e
-			if (strcmp(c.update,"?") == 0 ||
91179e
-			    strcmp(c.update, "help") == 0) {
91179e
-				outf = stdout;
91179e
-				fprintf(outf, "%s: ", Name);
91179e
-			} else {
91179e
-				outf = stderr;
91179e
-				fprintf(outf,
91179e
-					"%s: '--update=%s' is invalid.  ",
91179e
-					Name, c.update);
91179e
-			}
91179e
-			fprintf(outf, "Valid --update options are:\n"
91179e
-		"     'sparc2.2', 'super-minor', 'uuid', 'name', 'nodes', 'resync',\n"
91179e
-		"     'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
91179e
-		"     'bitmap', 'no-bitmap', 'metadata', 'revert-reshape'\n"
91179e
-		"     'bbl', 'no-bbl', 'force-no-bbl', 'ppl', 'no-ppl'\n"
91179e
-		"     'layout-original', 'layout-alternate', 'layout-unspecified'\n"
91179e
-				);
91179e
-			exit(outf == stdout ? 0 : 2);
91179e
-
91179e
+			continue;
91179e
+		}
91179e
 		case O(MANAGE,'U'):
91179e
 			/* update=devicesize is allowed with --re-add */
91179e
 			if (devmode != 'A') {
91179e
diff --git a/mdadm.h b/mdadm.h
91179e
index 23ffe977..51f1db2d 100644
91179e
--- a/mdadm.h
91179e
+++ b/mdadm.h
91179e
@@ -497,6 +497,36 @@ enum special_options {
91179e
 	ConsistencyPolicy,
91179e
 };
91179e
 
91179e
+enum update_opt {
91179e
+	UOPT_NAME = 1,
91179e
+	UOPT_PPL,
91179e
+	UOPT_NO_PPL,
91179e
+	UOPT_BITMAP,
91179e
+	UOPT_NO_BITMAP,
91179e
+	UOPT_SUBARRAY_ONLY,
91179e
+	UOPT_SPARC22,
91179e
+	UOPT_SUPER_MINOR,
91179e
+	UOPT_SUMMARIES,
91179e
+	UOPT_RESYNC,
91179e
+	UOPT_UUID,
91179e
+	UOPT_HOMEHOST,
91179e
+	UOPT_HOME_CLUSTER,
91179e
+	UOPT_NODES,
91179e
+	UOPT_DEVICESIZE,
91179e
+	UOPT_BBL,
91179e
+	UOPT_NO_BBL,
91179e
+	UOPT_FORCE_NO_BBL,
91179e
+	UOPT_METADATA,
91179e
+	UOPT_REVERT_RESHAPE,
91179e
+	UOPT_LAYOUT_ORIGINAL,
91179e
+	UOPT_LAYOUT_ALTERNATE,
91179e
+	UOPT_LAYOUT_UNSPECIFIED,
91179e
+	UOPT_BYTEORDER,
91179e
+	UOPT_HELP,
91179e
+	UOPT_UNDEFINED
91179e
+};
91179e
+extern void fprint_update_options(FILE *outf, enum update_opt update_mode);
91179e
+
91179e
 enum prefix_standard {
91179e
 	JEDEC,
91179e
 	IEC
91179e
@@ -777,7 +807,7 @@ extern char *map_num(mapping_t *map, int num);
91179e
 extern int map_name(mapping_t *map, char *name);
91179e
 extern mapping_t r0layout[], r5layout[], r6layout[],
91179e
 	pers[], modes[], faultylayout[];
91179e
-extern mapping_t consistency_policies[], sysfs_array_states[];
91179e
+extern mapping_t consistency_policies[], sysfs_array_states[], update_options[];
91179e
 
91179e
 extern char *map_dev_preferred(int major, int minor, int create,
91179e
 			       char *prefer);
91179e
-- 
91179e
2.38.1
91179e