[PATCH v2 06/10] super1: refactor the code for enum

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It prepares update_super1 for change context->update to enum.
Change if else statements into switch.

Signed-off-by: Mateusz Kusiak <mateusz.kusiak@xxxxxxxxx>
---
 super1.c | 152 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 91 insertions(+), 61 deletions(-)

diff --git a/super1.c b/super1.c
index 0b505a7e..b0a97016 100644
--- a/super1.c
+++ b/super1.c
@@ -1218,30 +1218,55 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 	int rv = 0;
 	struct mdp_superblock_1 *sb = st->sb;
 	bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
+	enum update_opt update_enum = map_name(update_options, update);
 
-	if (strcmp(update, "homehost") == 0 &&
-	    homehost) {
-		/* Note that 'homehost' is special as it is really
+	if (update_enum == UOPT_HOMEHOST && homehost) {
+		/*
+		 * Note that 'homehost' is special as it is really
 		 * a "name" update.
 		 */
 		char *c;
-		update = "name";
+		update_enum = UOPT_NAME;
 		c = strchr(sb->set_name, ':');
 		if (c)
-			strncpy(info->name, c+1, 31 - (c-sb->set_name));
+			snprintf(info->name, sizeof(info->name), "%s", c+1);
 		else
-			strncpy(info->name, sb->set_name, 32);
-		info->name[32] = 0;
+			snprintf(info->name, sizeof(info->name), "%s", sb->set_name);
 	}
 
-	if (strcmp(update, "force-one")==0) {
+	switch (update_enum) {
+	case UOPT_NAME: {
+		int namelen;
+
+		if (!info->name[0])
+			snprintf(info->name, sizeof(info->name), "%d", info->array.md_minor);
+		memset(sb->set_name, 0, sizeof(sb->set_name));
+
+		namelen = strnlen(homehost, MD_NAME_MAX) + 1 + strnlen(info->name, MD_NAME_MAX);
+		if (homehost &&
+		    strchr(info->name, ':') == NULL &&
+		    namelen < MD_NAME_MAX) {
+			strcpy(sb->set_name, homehost);
+			strcat(sb->set_name, ":");
+			strcat(sb->set_name, info->name);
+		} else {
+			namelen = min((int)strnlen(info->name, MD_NAME_MAX),
+				      (int)sizeof(sb->set_name) - 1);
+			memcpy(sb->set_name, info->name, namelen);
+			memset(&sb->set_name[namelen], '\0',
+			       sizeof(sb->set_name) - namelen);
+		}
+		break;
+	}
+	case UOPT_SPEC_FORCE_ONE:
 		/* Not enough devices for a working array,
 		 * so bring this one up-to-date
 		 */
 		if (sb->events != __cpu_to_le64(info->events))
 			rv = 1;
 		sb->events = __cpu_to_le64(info->events);
-	} else if (strcmp(update, "force-array")==0) {
+		break;
+	case UOPT_SPEC_FORCE_ARRAY:
 		/* Degraded array and 'force' requests to
 		 * maybe need to mark it 'clean'.
 		 */
@@ -1254,7 +1279,8 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 				rv = 1;
 			sb->resync_offset = MaxSector;
 		}
-	} else if (strcmp(update, "assemble")==0) {
+		break;
+	case UOPT_SPEC_ASSEMBLE: {
 		int d = info->disk.number;
 		int want;
 		if (info->disk.state & (1<<MD_DISK_ACTIVE))
@@ -1287,7 +1313,9 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 				__cpu_to_le64(info->reshape_progress);
 			rv = 1;
 		}
-	} else if (strcmp(update, "linear-grow-new") == 0) {
+		break;
+	}
+	case UOPT_SPEC_LINEAR_GROW_NEW: {
 		int i;
 		int fd;
 		int max = __le32_to_cpu(sb->max_dev);
@@ -1330,7 +1358,9 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 					ds - __le64_to_cpu(sb->data_offset));
 			}
 		}
-	} else if (strcmp(update, "linear-grow-update") == 0) {
+		break;
+	}
+	case UOPT_SPEC_LINEAR_GROW_UPDATE: {
 		int max = __le32_to_cpu(sb->max_dev);
 		int i = info->disk.number;
 		if (max > MAX_DEVS || i > MAX_DEVS)
@@ -1342,19 +1372,20 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 		sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
 		sb->dev_roles[info->disk.number] =
 			__cpu_to_le16(info->disk.raid_disk);
-	} else if (strcmp(update, "resync") == 0) {
-		/* make sure resync happens */
-		sb->resync_offset = 0ULL;
-	} else if (strcmp(update, "uuid") == 0) {
+		break;
+	}
+	case UOPT_UUID:
 		copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
 
 		if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)
 			memcpy(bms->uuid, sb->set_uuid, 16);
-	} else if (strcmp(update, "no-bitmap") == 0) {
+		break;
+	case UOPT_NO_BITMAP:
 		sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
 		if (bms->version == BITMAP_MAJOR_CLUSTERED && !IsBitmapDirty(devname))
 			sb->resync_offset = MaxSector;
-	} else if (strcmp(update, "bbl") == 0) {
+		break;
+	case UOPT_BBL: {
 		/* only possible if there is room after the bitmap, or if
 		 * there is no bitmap
 		 */
@@ -1383,14 +1414,12 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 				bb_offset = bitmap_offset + bm_sectors;
 			while (bb_offset < (long)sb_offset + 8 + 32*2 &&
 			       bb_offset + 8+8 <= (long)data_offset)
-				/* too close to bitmap, and room to grow */
 				bb_offset += 8;
 			if (bb_offset + 8 <= (long)data_offset) {
 				sb->bblog_size = __cpu_to_le16(8);
 				sb->bblog_offset = __cpu_to_le32(bb_offset);
 			}
 		} else {
-			/* 1.0 - Put bbl just before super block */
 			if (bm_sectors && bitmap_offset < 0)
 				space = -bitmap_offset - bm_sectors;
 			else
@@ -1401,7 +1430,9 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 				sb->bblog_offset = __cpu_to_le32((unsigned)-8);
 			}
 		}
-	} else if (strcmp(update, "no-bbl") == 0) {
+		break;
+	}
+	case UOPT_NO_BBL:
 		if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
 			pr_err("Cannot remove active bbl from %s\n",devname);
 		else {
@@ -1409,12 +1440,14 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 			sb->bblog_shift = 0;
 			sb->bblog_offset = 0;
 		}
-	} else if (strcmp(update, "force-no-bbl") == 0) {
+		break;
+	case UOPT_FORCE_NO_BBL:
 		sb->feature_map &= ~ __cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
 		sb->bblog_size = 0;
 		sb->bblog_shift = 0;
 		sb->bblog_offset = 0;
-	} else if (strcmp(update, "ppl") == 0) {
+		break;
+	case UOPT_PPL: {
 		unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
 		unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
 		unsigned long long data_size = __le64_to_cpu(sb->data_size);
@@ -1464,37 +1497,26 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 		sb->ppl.offset = __cpu_to_le16(offset);
 		sb->ppl.size = __cpu_to_le16(space);
 		sb->feature_map |= __cpu_to_le32(MD_FEATURE_PPL);
-	} else if (strcmp(update, "no-ppl") == 0) {
+		break;
+	}
+	case UOPT_NO_PPL:
 		sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_PPL |
 						   MD_FEATURE_MUTLIPLE_PPLS);
-	} else if (strcmp(update, "name") == 0) {
-		if (info->name[0] == 0)
-			sprintf(info->name, "%d", info->array.md_minor);
-		memset(sb->set_name, 0, sizeof(sb->set_name));
-		if (homehost &&
-		    strchr(info->name, ':') == NULL &&
-		    strlen(homehost)+1+strlen(info->name) < 32) {
-			strcpy(sb->set_name, homehost);
-			strcat(sb->set_name, ":");
-			strcat(sb->set_name, info->name);
-		} else {
-			int namelen;
-
-			namelen = min((int)strlen(info->name),
-				      (int)sizeof(sb->set_name) - 1);
-			memcpy(sb->set_name, info->name, namelen);
-			memset(&sb->set_name[namelen], '\0',
-			       sizeof(sb->set_name) - namelen);
-		}
-	} else if (strcmp(update, "devicesize") == 0 &&
-		   __le64_to_cpu(sb->super_offset) <
-		   __le64_to_cpu(sb->data_offset)) {
-		/* set data_size to device size less data_offset */
+		break;
+	case UOPT_DEVICESIZE:
+		if (__le64_to_cpu(sb->super_offset) >=
+		    __le64_to_cpu(sb->data_offset))
+			break;
+		/*
+		 * set data_size to device size less data_offset
+		 */
 		struct misc_dev_info *misc = (struct misc_dev_info*)
 			(st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
 		sb->data_size = __cpu_to_le64(
 			misc->device_size - __le64_to_cpu(sb->data_offset));
-	} else if (strncmp(update, "revert-reshape", 14) == 0) {
+		break;
+	case UOPT_SPEC_REVERT_RESHAPE_NOBACKUP:
+	case UOPT_REVERT_RESHAPE:
 		rv = -2;
 		if (!(sb->feature_map &
 		      __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)))
@@ -1512,7 +1534,7 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 			 * If that couldn't happen, the "-nobackup" version
 			 * will be used.
 			 */
-			if (strcmp(update, "revert-reshape-nobackup") == 0 &&
+			if (update_enum == UOPT_SPEC_REVERT_RESHAPE_NOBACKUP &&
 			    sb->reshape_position == 0 &&
 			    (__le32_to_cpu(sb->delta_disks) > 0 ||
 			     (__le32_to_cpu(sb->delta_disks) == 0 &&
@@ -1575,32 +1597,40 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
 			}
 		done:;
 		}
-	} else if (strcmp(update, "_reshape_progress") == 0)
+		break;
+	case UOPT_SPEC__RESHAPE_PROGRESS:
 		sb->reshape_position = __cpu_to_le64(info->reshape_progress);
-	else if (strcmp(update, "writemostly") == 0)
+		break;
+	case UOPT_SPEC_WRITEMOSTLY:
 		sb->devflags |= WriteMostly1;
-	else if (strcmp(update, "readwrite") == 0)
+		break;
+	case UOPT_SPEC_READWRITE:
 		sb->devflags &= ~WriteMostly1;
-	else if (strcmp(update, "failfast") == 0)
+		break;
+	case UOPT_SPEC_FAILFAST:
 		sb->devflags |= FailFast1;
-	else if (strcmp(update, "nofailfast") == 0)
+		break;
+	case UOPT_SPEC_NOFAILFAST:
 		sb->devflags &= ~FailFast1;
-	else if (strcmp(update, "layout-original") == 0 ||
-		 strcmp(update, "layout-alternate") == 0 ||
-		 strcmp(update, "layout-unspecified") == 0) {
+		break;
+	case UOPT_LAYOUT_ORIGINAL:
+	case UOPT_LAYOUT_ALTERNATE:
+	case UOPT_LAYOUT_UNSPECIFIED:
 		if (__le32_to_cpu(sb->level) != 0) {
 			pr_err("%s: %s only supported for RAID0\n",
-			       devname?:"", update);
+			       devname ?: "", map_num(update_options, update_enum));
 			rv = -1;
-		} else if (strcmp(update, "layout-unspecified") == 0) {
+		} else if (update_enum == UOPT_LAYOUT_UNSPECIFIED) {
 			sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_RAID0_LAYOUT);
 			sb->layout = 0;
 		} else {
 			sb->feature_map |= __cpu_to_le32(MD_FEATURE_RAID0_LAYOUT);
-			sb->layout = __cpu_to_le32(update[7] == 'o' ? 1 : 2);
+			sb->layout = __cpu_to_le32(update_enum == UOPT_LAYOUT_ORIGINAL ? 1 : 2);
 		}
-	} else
+		break;
+	default:
 		rv = -1;
+	}
 
 	sb->sb_csum = calc_sb_1_csum(sb);
 
-- 
2.26.2




[Index of Archives]     [Linux RAID Wiki]     [ATA RAID]     [Linux SCSI Target Infrastructure]     [Linux Block]     [Linux IDE]     [Linux SCSI]     [Linux Hams]     [Device Mapper]     [Device Mapper Cryptographics]     [Kernel]     [Linux Admin]     [Linux Net]     [GFS]     [RPM]     [git]     [Yosemite Forum]


  Powered by Linux