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

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