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

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