Blame SOURCES/0076-Change-char-to-enum-in-context-update-refactor-code.patch

2b63fb
From 3a87fa67112dc2c2c3664aeecd0b49cb4b6ceaa9 Mon Sep 17 00:00:00 2001
2b63fb
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
2b63fb
Date: Mon, 2 Jan 2023 09:35:24 +0100
2b63fb
Subject: [PATCH 76/83] Change char* to enum in context->update & refactor code
2b63fb
2b63fb
Storing update option in string is bad for frequent comparisons and
2b63fb
error prone.
2b63fb
Replace char array with enum so already existing enum is passed around
2b63fb
instead of string.
2b63fb
Adapt code to changes.
2b63fb
2b63fb
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
2b63fb
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
2b63fb
---
2b63fb
 Assemble.c | 40 +++++++++++++++++-----------------------
2b63fb
 mdadm.c    | 52 +++++++++++++++++++---------------------------------
2b63fb
 mdadm.h    |  2 +-
2b63fb
 3 files changed, 37 insertions(+), 57 deletions(-)
2b63fb
2b63fb
diff --git a/Assemble.c b/Assemble.c
2b63fb
index dba910cd..49804941 100644
2b63fb
--- a/Assemble.c
2b63fb
+++ b/Assemble.c
2b63fb
@@ -135,17 +135,17 @@ static int ident_matches(struct mddev_ident *ident,
2b63fb
 			 struct mdinfo *content,
2b63fb
 			 struct supertype *tst,
2b63fb
 			 char *homehost, int require_homehost,
2b63fb
-			 char *update, char *devname)
2b63fb
+			 enum update_opt update, char *devname)
2b63fb
 {
2b63fb
 
2b63fb
-	if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) &&
2b63fb
+	if (ident->uuid_set && update != UOPT_UUID &&
2b63fb
 	    same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0 &&
2b63fb
 	    memcmp(content->uuid, uuid_zero, sizeof(int[4])) != 0) {
2b63fb
 		if (devname)
2b63fb
 			pr_err("%s has wrong uuid.\n", devname);
2b63fb
 		return 0;
2b63fb
 	}
2b63fb
-	if (ident->name[0] && (!update || strcmp(update, "name")!= 0) &&
2b63fb
+	if (ident->name[0] && update != UOPT_NAME &&
2b63fb
 	    name_matches(content->name, ident->name, homehost, require_homehost)==0) {
2b63fb
 		if (devname)
2b63fb
 			pr_err("%s has wrong name.\n", devname);
2b63fb
@@ -648,11 +648,10 @@ static int load_devices(struct devs *devices, char *devmap,
2b63fb
 			int err;
2b63fb
 			fstat(mdfd, &stb2);
2b63fb
 
2b63fb
-			if (strcmp(c->update, "uuid") == 0 && !ident->uuid_set)
2b63fb
+			if (c->update == UOPT_UUID && !ident->uuid_set)
2b63fb
 				random_uuid((__u8 *)ident->uuid);
2b63fb
 
2b63fb
-			if (strcmp(c->update, "ppl") == 0 &&
2b63fb
-			    ident->bitmap_fd >= 0) {
2b63fb
+			if (c->update == UOPT_PPL && ident->bitmap_fd >= 0) {
2b63fb
 				pr_err("PPL is not compatible with bitmap\n");
2b63fb
 				close(mdfd);
2b63fb
 				free(devices);
2b63fb
@@ -684,34 +683,30 @@ static int load_devices(struct devs *devices, char *devmap,
2b63fb
 			strcpy(content->name, ident->name);
2b63fb
 			content->array.md_minor = minor(stb2.st_rdev);
2b63fb
 
2b63fb
-			if (strcmp(c->update, "byteorder") == 0)
2b63fb
+			if (c->update == UOPT_BYTEORDER)
2b63fb
 				err = 0;
2b63fb
-			else if (strcmp(c->update, "home-cluster") == 0) {
2b63fb
+			else if (c->update == UOPT_HOME_CLUSTER) {
2b63fb
 				tst->cluster_name = c->homecluster;
2b63fb
 				err = tst->ss->write_bitmap(tst, dfd, NameUpdate);
2b63fb
-			} else if (strcmp(c->update, "nodes") == 0) {
2b63fb
+			} else if (c->update == UOPT_NODES) {
2b63fb
 				tst->nodes = c->nodes;
2b63fb
 				err = tst->ss->write_bitmap(tst, dfd, NodeNumUpdate);
2b63fb
-			} else if (strcmp(c->update, "revert-reshape") == 0 &&
2b63fb
-				   c->invalid_backup)
2b63fb
+			} else if (c->update == UOPT_REVERT_RESHAPE && c->invalid_backup)
2b63fb
 				err = tst->ss->update_super(tst, content,
2b63fb
 							    UOPT_SPEC_REVERT_RESHAPE_NOBACKUP,
2b63fb
 							    devname, c->verbose,
2b63fb
 							    ident->uuid_set,
2b63fb
 							    c->homehost);
2b63fb
 			else
2b63fb
-				/*
2b63fb
-				 * Mapping is temporary, will be removed in this patchset
2b63fb
-				 */
2b63fb
 				err = tst->ss->update_super(tst, content,
2b63fb
-							    map_name(update_options, c->update),
2b63fb
+							    c->update,
2b63fb
 							    devname, c->verbose,
2b63fb
 							    ident->uuid_set,
2b63fb
 							    c->homehost);
2b63fb
 			if (err < 0) {
2b63fb
 				if (err == -1)
2b63fb
 					pr_err("--update=%s not understood for %s metadata\n",
2b63fb
-					       c->update, tst->ss->name);
2b63fb
+					       map_num(update_options, c->update), tst->ss->name);
2b63fb
 				tst->ss->free_super(tst);
2b63fb
 				free(tst);
2b63fb
 				close(mdfd);
2b63fb
@@ -721,7 +716,7 @@ static int load_devices(struct devs *devices, char *devmap,
2b63fb
 				*stp = st;
2b63fb
 				return -1;
2b63fb
 			}
2b63fb
-			if (strcmp(c->update, "uuid")==0 &&
2b63fb
+			if (c->update == UOPT_UUID &&
2b63fb
 			    !ident->uuid_set) {
2b63fb
 				ident->uuid_set = 1;
2b63fb
 				memcpy(ident->uuid, content->uuid, 16);
2b63fb
@@ -730,7 +725,7 @@ static int load_devices(struct devs *devices, char *devmap,
2b63fb
 				pr_err("Could not re-write superblock on %s.\n",
2b63fb
 				       devname);
2b63fb
 
2b63fb
-			if (strcmp(c->update, "uuid")==0 &&
2b63fb
+			if (c->update == UOPT_UUID &&
2b63fb
 			    ident->bitmap_fd >= 0 && !bitmap_done) {
2b63fb
 				if (bitmap_update_uuid(ident->bitmap_fd,
2b63fb
 						       content->uuid,
2b63fb
@@ -1188,8 +1183,7 @@ static int start_array(int mdfd,
2b63fb
 				pr_err("%s: Need a backup file to complete reshape of this array.\n",
2b63fb
 				       mddev);
2b63fb
 				pr_err("Please provided one with \"--backup-file=...\"\n");
2b63fb
-				if (c->update &&
2b63fb
-				    strcmp(c->update, "revert-reshape") == 0)
2b63fb
+				if (c->update == UOPT_REVERT_RESHAPE)
2b63fb
 					pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
2b63fb
 				return 1;
2b63fb
 			}
2b63fb
@@ -1487,7 +1481,7 @@ try_again:
2b63fb
 	 */
2b63fb
 	if (map_lock(&map))
2b63fb
 		pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
2b63fb
-	if (c->update && strcmp(c->update,"uuid") == 0)
2b63fb
+	if (c->update == UOPT_UUID)
2b63fb
 		mp = NULL;
2b63fb
 	else
2b63fb
 		mp = map_by_uuid(&map, content->uuid);
2b63fb
@@ -1634,7 +1628,7 @@ try_again:
2b63fb
 		goto out;
2b63fb
 	}
2b63fb
 
2b63fb
-	if (c->update && strcmp(c->update, "byteorder")==0)
2b63fb
+	if (c->update == UOPT_BYTEORDER)
2b63fb
 		st->minor_version = 90;
2b63fb
 
2b63fb
 	st->ss->getinfo_super(st, content, NULL);
2b63fb
@@ -1902,7 +1896,7 @@ try_again:
2b63fb
 	/* First, fill in the map, so that udev can find our name
2b63fb
 	 * as soon as we become active.
2b63fb
 	 */
2b63fb
-	if (c->update && strcmp(c->update, "metadata")==0) {
2b63fb
+	if (c->update == UOPT_METADATA) {
2b63fb
 		content->array.major_version = 1;
2b63fb
 		content->array.minor_version = 0;
2b63fb
 		strcpy(content->text_version, "1.0");
2b63fb
diff --git a/mdadm.c b/mdadm.c
2b63fb
index d06e2820..57e8e6fa 100644
2b63fb
--- a/mdadm.c
2b63fb
+++ b/mdadm.c
2b63fb
@@ -724,13 +724,12 @@ int main(int argc, char *argv[])
2b63fb
 
2b63fb
 		case O(ASSEMBLE,'U'): /* update the superblock */
2b63fb
 		case O(MISC,'U'): {
2b63fb
-			enum update_opt updateopt = map_name(update_options, c.update);
2b63fb
 			enum update_opt print_mode = UOPT_HELP;
2b63fb
 			const char *error_addon = "update option";
2b63fb
 
2b63fb
 			if (c.update) {
2b63fb
 				pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
2b63fb
-					c.update, optarg);
2b63fb
+					map_num(update_options, c.update), optarg);
2b63fb
 				exit(2);
2b63fb
 			}
2b63fb
 			if (mode == MISC && !c.subarray) {
2b63fb
@@ -738,20 +737,20 @@ int main(int argc, char *argv[])
2b63fb
 				exit(2);
2b63fb
 			}
2b63fb
 
2b63fb
-			c.update = optarg;
2b63fb
+			c.update = map_name(update_options, optarg);
2b63fb
 
2b63fb
 			if (devmode == UpdateSubarray) {
2b63fb
 				print_mode = UOPT_SUBARRAY_ONLY;
2b63fb
 				error_addon = "update-subarray option";
2b63fb
 
2b63fb
-				if (updateopt > UOPT_SUBARRAY_ONLY && updateopt < UOPT_HELP)
2b63fb
-					updateopt = UOPT_UNDEFINED;
2b63fb
+				if (c.update > UOPT_SUBARRAY_ONLY && c.update < UOPT_HELP)
2b63fb
+					c.update = UOPT_UNDEFINED;
2b63fb
 			}
2b63fb
 
2b63fb
-			switch (updateopt) {
2b63fb
+			switch (c.update) {
2b63fb
 			case UOPT_UNDEFINED:
2b63fb
 				pr_err("'--update=%s' is invalid %s. ",
2b63fb
-					c.update, error_addon);
2b63fb
+					optarg, error_addon);
2b63fb
 				outf = stderr;
2b63fb
 			case UOPT_HELP:
2b63fb
 				if (!outf)
2b63fb
@@ -776,14 +775,14 @@ int main(int argc, char *argv[])
2b63fb
 			}
2b63fb
 			if (c.update) {
2b63fb
 				pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
2b63fb
-					c.update, optarg);
2b63fb
+					map_num(update_options, c.update), optarg);
2b63fb
 				exit(2);
2b63fb
 			}
2b63fb
-			c.update = optarg;
2b63fb
-			if (strcmp(c.update, "devicesize") != 0 &&
2b63fb
-			    strcmp(c.update, "bbl") != 0 &&
2b63fb
-			    strcmp(c.update, "force-no-bbl") != 0 &&
2b63fb
-			    strcmp(c.update, "no-bbl") != 0) {
2b63fb
+			c.update = map_name(update_options, optarg);
2b63fb
+			if (c.update != UOPT_DEVICESIZE &&
2b63fb
+			    c.update != UOPT_BBL &&
2b63fb
+			    c.update != UOPT_NO_BBL &&
2b63fb
+			    c.update != UOPT_FORCE_NO_BBL) {
2b63fb
 				pr_err("only 'devicesize', 'bbl', 'no-bbl', and 'force-no-bbl' can be updated with --re-add\n");
2b63fb
 				exit(2);
2b63fb
 			}
2b63fb
@@ -1357,7 +1356,7 @@ int main(int argc, char *argv[])
2b63fb
 		}
2b63fb
 	}
2b63fb
 
2b63fb
-	if (c.update && strcmp(c.update, "nodes") == 0 && c.nodes == 0) {
2b63fb
+	if (c.update && c.update == UOPT_NODES && c.nodes == 0) {
2b63fb
 		pr_err("Please specify nodes number with --nodes\n");
2b63fb
 		exit(1);
2b63fb
 	}
2b63fb
@@ -1402,22 +1401,10 @@ int main(int argc, char *argv[])
2b63fb
 		/* readonly, add/remove, readwrite, runstop */
2b63fb
 		if (c.readonly > 0)
2b63fb
 			rv = Manage_ro(devlist->devname, mdfd, c.readonly);
2b63fb
-		if (!rv && devs_found > 1) {
2b63fb
-			/*
2b63fb
-			 * This is temporary and will be removed in next patches
2b63fb
-			 * Null c.update will cause segfault
2b63fb
-			 */
2b63fb
-			if (c.update)
2b63fb
-				rv = Manage_subdevs(devlist->devname, mdfd,
2b63fb
-						devlist->next, c.verbose, c.test,
2b63fb
-						map_name(update_options, c.update),
2b63fb
-						c.force);
2b63fb
-			else
2b63fb
-				rv = Manage_subdevs(devlist->devname, mdfd,
2b63fb
-						devlist->next, c.verbose, c.test,
2b63fb
-						UOPT_UNDEFINED,
2b63fb
-						c.force);
2b63fb
-		}
2b63fb
+		if (!rv && devs_found > 1)
2b63fb
+			rv = Manage_subdevs(devlist->devname, mdfd,
2b63fb
+					    devlist->next, c.verbose,
2b63fb
+					    c.test, c.update, c.force);
2b63fb
 		if (!rv && c.readonly < 0)
2b63fb
 			rv = Manage_ro(devlist->devname, mdfd, c.readonly);
2b63fb
 		if (!rv && c.runstop > 0)
2b63fb
@@ -1937,14 +1924,13 @@ static int misc_list(struct mddev_dev *devlist,
2b63fb
 			rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
2b63fb
 			continue;
2b63fb
 		case UpdateSubarray:
2b63fb
-			if (c->update == NULL) {
2b63fb
+			if (!c->update) {
2b63fb
 				pr_err("-U/--update must be specified with --update-subarray\n");
2b63fb
 				rv |= 1;
2b63fb
 				continue;
2b63fb
 			}
2b63fb
 			rv |= Update_subarray(dv->devname, c->subarray,
2b63fb
-					      map_name(update_options, c->update),
2b63fb
-					      ident, c->verbose);
2b63fb
+					      c->update, ident, c->verbose);
2b63fb
 			continue;
2b63fb
 		case Dump:
2b63fb
 			rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
2b63fb
diff --git a/mdadm.h b/mdadm.h
2b63fb
index 924f4b63..13f8b4cb 100644
2b63fb
--- a/mdadm.h
2b63fb
+++ b/mdadm.h
2b63fb
@@ -616,7 +616,7 @@ struct context {
2b63fb
 	int	export;
2b63fb
 	int	test;
2b63fb
 	char	*subarray;
2b63fb
-	char	*update;
2b63fb
+	enum	update_opt update;
2b63fb
 	int	scan;
2b63fb
 	int	SparcAdjust;
2b63fb
 	int	autof;
2b63fb
-- 
2b63fb
2.38.1
2b63fb