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

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