Blame SOURCES/0071-super0-refactor-the-code-for-enum.patch

2ad819
From 0a9e39383d3bf63e1f5cf10f64200083a1af8091 Mon Sep 17 00:00:00 2001
2ad819
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
2ad819
Date: Mon, 2 Jan 2023 09:35:19 +0100
2ad819
Subject: [PATCH 71/83] super0: refactor the code for enum
2ad819
2ad819
It prepares update_super0 for change context->update to enum.
2ad819
Change if else statements to switch.
2ad819
2ad819
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
2ad819
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
2ad819
---
2ad819
 super0.c | 102 ++++++++++++++++++++++++++++++++++---------------------
2ad819
 1 file changed, 63 insertions(+), 39 deletions(-)
2ad819
2ad819
diff --git a/super0.c b/super0.c
2ad819
index 93876e2e..d9f5bff4 100644
2ad819
--- a/super0.c
2ad819
+++ b/super0.c
2ad819
@@ -502,19 +502,39 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 	int rv = 0;
2ad819
 	int uuid[4];
2ad819
 	mdp_super_t *sb = st->sb;
2ad819
+	enum update_opt update_enum = map_name(update_options, update);
2ad819
 
2ad819
-	if (strcmp(update, "homehost") == 0 &&
2ad819
-	    homehost) {
2ad819
-		/* note that 'homehost' is special as it is really
2ad819
+	if (update_enum == UOPT_HOMEHOST && homehost) {
2ad819
+		/*
2ad819
+		 * note that 'homehost' is special as it is really
2ad819
 		 * a "uuid" update.
2ad819
 		 */
2ad819
 		uuid_set = 0;
2ad819
-		update = "uuid";
2ad819
+		update_enum = UOPT_UUID;
2ad819
 		info->uuid[0] = sb->set_uuid0;
2ad819
 		info->uuid[1] = sb->set_uuid1;
2ad819
 	}
2ad819
 
2ad819
-	if (strcmp(update, "sparc2.2")==0 ) {
2ad819
+	switch (update_enum) {
2ad819
+	case UOPT_UUID:
2ad819
+		if (!uuid_set && homehost) {
2ad819
+			char buf[20];
2ad819
+			memcpy(info->uuid+2,
2ad819
+			       sha1_buffer(homehost, strlen(homehost), buf),
2ad819
+			       8);
2ad819
+		}
2ad819
+		sb->set_uuid0 = info->uuid[0];
2ad819
+		sb->set_uuid1 = info->uuid[1];
2ad819
+		sb->set_uuid2 = info->uuid[2];
2ad819
+		sb->set_uuid3 = info->uuid[3];
2ad819
+		if (sb->state & (1<
2ad819
+			struct bitmap_super_s *bm;
2ad819
+			bm = (struct bitmap_super_s *)(sb+1);
2ad819
+			uuid_from_super0(st, uuid);
2ad819
+			memcpy(bm->uuid, uuid, 16);
2ad819
+		}
2ad819
+		break;
2ad819
+	case UOPT_SPARC22: {
2ad819
 		/* 2.2 sparc put the events in the wrong place
2ad819
 		 * So we copy the tail of the superblock
2ad819
 		 * up 4 bytes before continuing
2ad819
@@ -527,12 +547,15 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 		if (verbose >= 0)
2ad819
 			pr_err("adjusting superblock of %s for 2.2/sparc compatibility.\n",
2ad819
 			       devname);
2ad819
-	} else if (strcmp(update, "super-minor") ==0) {
2ad819
+		break;
2ad819
+	}
2ad819
+	case UOPT_SUPER_MINOR:
2ad819
 		sb->md_minor = info->array.md_minor;
2ad819
 		if (verbose > 0)
2ad819
 			pr_err("updating superblock of %s with minor number %d\n",
2ad819
 				devname, info->array.md_minor);
2ad819
-	} else if (strcmp(update, "summaries") == 0) {
2ad819
+		break;
2ad819
+	case UOPT_SUMMARIES: {
2ad819
 		unsigned int i;
2ad819
 		/* set nr_disks, active_disks, working_disks,
2ad819
 		 * failed_disks, spare_disks based on disks[]
2ad819
@@ -559,7 +582,9 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 					sb->spare_disks++;
2ad819
 			} else if (i >= sb->raid_disks && sb->disks[i].number == 0)
2ad819
 				sb->disks[i].state = 0;
2ad819
-	} else if (strcmp(update, "force-one")==0) {
2ad819
+		break;
2ad819
+	}
2ad819
+	case UOPT_SPEC_FORCE_ONE: {
2ad819
 		/* Not enough devices for a working array, so
2ad819
 		 * bring this one up-to-date.
2ad819
 		 */
2ad819
@@ -569,7 +594,9 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 		if (sb->events_hi != ehi ||
2ad819
 		    sb->events_lo != elo)
2ad819
 			rv = 1;
2ad819
-	} else if (strcmp(update, "force-array")==0) {
2ad819
+		break;
2ad819
+	}
2ad819
+	case UOPT_SPEC_FORCE_ARRAY:
2ad819
 		/* degraded array and 'force' requested, so
2ad819
 		 * maybe need to mark it 'clean'
2ad819
 		 */
2ad819
@@ -579,7 +606,8 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 			sb->state |= (1 << MD_SB_CLEAN);
2ad819
 			rv = 1;
2ad819
 		}
2ad819
-	} else if (strcmp(update, "assemble")==0) {
2ad819
+		break;
2ad819
+	case UOPT_SPEC_ASSEMBLE: {
2ad819
 		int d = info->disk.number;
2ad819
 		int wonly = sb->disks[d].state & (1<
2ad819
 		int failfast = sb->disks[d].state & (1<
2ad819
@@ -609,7 +637,9 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 			sb->reshape_position = info->reshape_progress;
2ad819
 			rv = 1;
2ad819
 		}
2ad819
-	} else if (strcmp(update, "linear-grow-new") == 0) {
2ad819
+		break;
2ad819
+	}
2ad819
+	case UOPT_SPEC_LINEAR_GROW_NEW:
2ad819
 		memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
2ad819
 		sb->disks[info->disk.number].number = info->disk.number;
2ad819
 		sb->disks[info->disk.number].major = info->disk.major;
2ad819
@@ -617,7 +647,8 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 		sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
2ad819
 		sb->disks[info->disk.number].state = info->disk.state;
2ad819
 		sb->this_disk = sb->disks[info->disk.number];
2ad819
-	} else if (strcmp(update, "linear-grow-update") == 0) {
2ad819
+		break;
2ad819
+	case UOPT_SPEC_LINEAR_GROW_UPDATE:
2ad819
 		sb->raid_disks = info->array.raid_disks;
2ad819
 		sb->nr_disks = info->array.nr_disks;
2ad819
 		sb->active_disks = info->array.active_disks;
2ad819
@@ -628,29 +659,15 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 		sb->disks[info->disk.number].minor = info->disk.minor;
2ad819
 		sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
2ad819
 		sb->disks[info->disk.number].state = info->disk.state;
2ad819
-	} else if (strcmp(update, "resync") == 0) {
2ad819
-		/* make sure resync happens */
2ad819
+		break;
2ad819
+	case UOPT_RESYNC:
2ad819
+		/*
2ad819
+		 * make sure resync happens
2ad819
+		 */
2ad819
 		sb->state &= ~(1<
2ad819
 		sb->recovery_cp = 0;
2ad819
-	} else if (strcmp(update, "uuid") == 0) {
2ad819
-		if (!uuid_set && homehost) {
2ad819
-			char buf[20];
2ad819
-			char *hash = sha1_buffer(homehost,
2ad819
-						 strlen(homehost),
2ad819
-						 buf);
2ad819
-			memcpy(info->uuid+2, hash, 8);
2ad819
-		}
2ad819
-		sb->set_uuid0 = info->uuid[0];
2ad819
-		sb->set_uuid1 = info->uuid[1];
2ad819
-		sb->set_uuid2 = info->uuid[2];
2ad819
-		sb->set_uuid3 = info->uuid[3];
2ad819
-		if (sb->state & (1<
2ad819
-			struct bitmap_super_s *bm;
2ad819
-			bm = (struct bitmap_super_s*)(sb+1);
2ad819
-			uuid_from_super0(st, uuid);
2ad819
-			memcpy(bm->uuid, uuid, 16);
2ad819
-		}
2ad819
-	} else if (strcmp(update, "metadata") == 0) {
2ad819
+		break;
2ad819
+	case UOPT_METADATA:
2ad819
 		/* Create some v1.0 metadata to match ours but make the
2ad819
 		 * ctime bigger.  Also update info->array.*_version.
2ad819
 		 * We need to arrange that store_super writes out
2ad819
@@ -670,7 +687,8 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 			uuid_from_super0(st, info->uuid);
2ad819
 			st->other = super1_make_v0(st, info, st->sb);
2ad819
 		}
2ad819
-	} else if (strcmp(update, "revert-reshape") == 0) {
2ad819
+		break;
2ad819
+	case UOPT_REVERT_RESHAPE:
2ad819
 		rv = -2;
2ad819
 		if (sb->minor_version <= 90)
2ad819
 			pr_err("No active reshape to revert on %s\n",
2ad819
@@ -702,16 +720,22 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
2ad819
 			sb->new_chunk = sb->chunk_size;
2ad819
 			sb->chunk_size = tmp;
2ad819
 		}
2ad819
-	} else if (strcmp(update, "no-bitmap") == 0) {
2ad819
+		break;
2ad819
+	case UOPT_NO_BITMAP:
2ad819
 		sb->state &= ~(1<
2ad819
-	} else if (strcmp(update, "_reshape_progress")==0)
2ad819
+		break;
2ad819
+	case UOPT_SPEC__RESHAPE_PROGRESS:
2ad819
 		sb->reshape_position = info->reshape_progress;
2ad819
-	else if (strcmp(update, "writemostly")==0)
2ad819
+		break;
2ad819
+	case UOPT_SPEC_WRITEMOSTLY:
2ad819
 		sb->state |= (1<
2ad819
-	else if (strcmp(update, "readwrite")==0)
2ad819
+		break;
2ad819
+	case UOPT_SPEC_READWRITE:
2ad819
 		sb->state &= ~(1<
2ad819
-	else
2ad819
+		break;
2ad819
+	default:
2ad819
 		rv = -1;
2ad819
+	}
2ad819
 
2ad819
 	sb->sb_csum = calc_sb0_csum(sb);
2ad819
 	return rv;
2ad819
-- 
2ad819
2.38.1
2ad819