From 238c0a71dc3288807fd5f7328f2d9d3bdfe7a9f6 Mon Sep 17 00:00:00 2001
From: Adam Kwolek <adam.kwolek@intel.com>
Date: Wed, 7 Dec 2011 13:58:46 +0100
Subject: [PATCH] imsm: FIX: Use defines for device map access

Maps should not be accessed using "magic numbers" /0, 1,-1/.
Add proper definitions and change all map access to use them.
Change present definitions /MAP_0/MAP_1/ to values already used
in code /0, 1, -1//

Signed-off-by: Adam Kwolek <adam.kwolek@intel.com>
Signed-off-by: NeilBrown <neilb@suse.de>
---
 super-intel.c |  268 +++++++++++++++++++++++++++++-----------------------------
 1 file changed, 136 insertions(+), 132 deletions(-)

--- mdadm-3.2.2.orig/super-intel.c
+++ mdadm-3.2.2/super-intel.c
@@ -108,8 +108,9 @@ struct imsm_disk {
 
 /* map selector for map managment
  */
-#define MAP_0		2
-#define MAP_1		4
+#define MAP_0		0
+#define MAP_1		1
+#define MAP_X		-1
 
 /* RAID map configuration infos. */
 struct imsm_map {
@@ -667,9 +668,9 @@ struct imsm_map *get_imsm_map(struct ims
 {
 	/* A device can have 2 maps if it is in the middle of a migration.
 	 * If second_map is:
-	 *    MAP_0 or 0   - we return the first map
-	 *    MAP_1 or 1   - we return the second map if it exists, else NULL
-	 *   -1   - we return the second map if it exists, else the first
+	 *    MAP_0 - we return the first map
+	 *    MAP_1 - we return the second map if it exists, else NULL
+	 *    MAP_X - we return the second map if it exists, else the first
 	 */
 	struct imsm_map *map = &dev->vol.map[0];
 	struct imsm_map *map2 = NULL;
@@ -679,13 +680,11 @@ struct imsm_map *get_imsm_map(struct ims
 
 	switch (second_map) {
 	case MAP_0:
-	case 0:
 		break;
 	case MAP_1:
-	case 1:
 		map = map2;
 		break;
-	case -1:
+	case MAP_X:
 		if (map2)
 			map = map2;
 		break;
@@ -702,13 +701,13 @@ struct imsm_map *get_imsm_map(struct ims
 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
 {
 	size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
-		      sizeof_imsm_map(get_imsm_map(dev, 0));
+		      sizeof_imsm_map(get_imsm_map(dev, MAP_0));
 
 	/* migrating means an additional map */
 	if (dev->vol.migr_state)
-		size += sizeof_imsm_map(get_imsm_map(dev, 1));
+		size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
 	else if (migr_state)
-		size += sizeof_imsm_map(get_imsm_map(dev, 0));
+		size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
 
 	return size;
 }
@@ -762,9 +761,9 @@ static struct imsm_dev *get_imsm_dev(str
 
 /*
  * for second_map:
- *  == 0 get first map
- *  == 1 get second map
- *  == -1 than get map according to the current migr_state
+ *  == MAP_0 get first map
+ *  == MAP_1 get second map
+ *  == MAP_X than get map according to the current migr_state
  */
 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
 				  int slot,
@@ -835,7 +834,7 @@ static int count_memberships(struct dl *
 
 	for (i = 0; i < super->anchor->num_raid_devs; i++) {
 		struct imsm_dev *dev = get_imsm_dev(super, i);
-		struct imsm_map *map = get_imsm_map(dev, 0);
+		struct imsm_map *map = get_imsm_map(dev, MAP_0);
 
 		if (get_imsm_disk_slot(map, dl->index) >= 0)
 			memberships++;
@@ -870,7 +869,7 @@ static struct extent *get_extents(struct
 
 	for (i = 0; i < super->anchor->num_raid_devs; i++) {
 		struct imsm_dev *dev = get_imsm_dev(super, i);
-		struct imsm_map *map = get_imsm_map(dev, 0);
+		struct imsm_map *map = get_imsm_map(dev, MAP_0);
 
 		if (get_imsm_disk_slot(map, dl->index) >= 0) {
 			e->start = __le32_to_cpu(map->pba_of_lba0);
@@ -1045,8 +1044,8 @@ static void print_imsm_dev(struct intel_
 {
 	__u64 sz;
 	int slot, i;
-	struct imsm_map *map = get_imsm_map(dev, 0);
-	struct imsm_map *map2 = get_imsm_map(dev, 1);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
+	struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
 	__u32 ord;
 
 	printf("\n");
@@ -1062,14 +1061,14 @@ static void print_imsm_dev(struct intel_
 	printf("\n");
 	printf("          Slots : [");
 	for (i = 0; i < map->num_members; i++) {
-		ord = get_imsm_ord_tbl_ent(dev, i, 0);
+		ord = get_imsm_ord_tbl_ent(dev, i, MAP_0);
 		printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
 	}
 	printf("]");
 	if (map2) {
 		printf(" <-- [");
 		for (i = 0; i < map2->num_members; i++) {
-			ord = get_imsm_ord_tbl_ent(dev, i, 1);
+			ord = get_imsm_ord_tbl_ent(dev, i, MAP_1);
 			printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
 		}
 		printf("]");
@@ -1083,7 +1082,7 @@ static void print_imsm_dev(struct intel_
 	printf("\n");
 	slot = get_imsm_disk_slot(map, disk_idx);
 	if (slot >= 0) {
-		ord = get_imsm_ord_tbl_ent(dev, slot, -1);
+		ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
 		printf("      This Slot : %d%s\n", slot,
 		       ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
 	} else
@@ -1127,7 +1126,7 @@ static void print_imsm_dev(struct intel_
 		printf("idle\n");
 	printf("      Map State : %s", map_state_str[map->map_state]);
 	if (dev->vol.migr_state) {
-		struct imsm_map *map = get_imsm_map(dev, 1);
+		struct imsm_map *map = get_imsm_map(dev, MAP_1);
 
 		printf(" <-- %s", map_state_str[map->map_state]);
 		printf("\n     Checkpoint : %u ",
@@ -1862,7 +1861,7 @@ get_imsm_numerical_version(struct imsm_s
 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
 {
 	/* migr_strip_size when repairing or initializing parity */
-	struct imsm_map *map = get_imsm_map(dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
 	__u32 chunk = __le32_to_cpu(map->blocks_per_strip);
 
 	switch (get_imsm_raid_level(map)) {
@@ -1880,7 +1879,7 @@ static __u32 migr_strip_blocks_rebuild(s
 	 * this is different than migr_strip_size_resync(), but it's good
 	 * to be compatible
 	 */
-	struct imsm_map *map = get_imsm_map(dev, 1);
+	struct imsm_map *map = get_imsm_map(dev, MAP_1);
 	__u32 chunk = __le32_to_cpu(map->blocks_per_strip);
 
 	switch (get_imsm_raid_level(map)) {
@@ -1899,8 +1898,8 @@ static __u32 migr_strip_blocks_rebuild(s
 
 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
 {
-	struct imsm_map *lo = get_imsm_map(dev, 0);
-	struct imsm_map *hi = get_imsm_map(dev, 1);
+	struct imsm_map *lo = get_imsm_map(dev, MAP_0);
+	struct imsm_map *hi = get_imsm_map(dev, MAP_1);
 	__u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
 	__u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
 
@@ -1909,11 +1908,11 @@ static __u32 num_stripes_per_unit_resync
 
 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
 {
-	struct imsm_map *lo = get_imsm_map(dev, 0);
+	struct imsm_map *lo = get_imsm_map(dev, MAP_0);
 	int level = get_imsm_raid_level(lo);
 
 	if (level == 1 || level == 10) {
-		struct imsm_map *hi = get_imsm_map(dev, 1);
+		struct imsm_map *hi = get_imsm_map(dev, MAP_1);
 
 		return hi->num_domains;
 	} else
@@ -1942,7 +1941,7 @@ static __u8 imsm_num_data_members(struct
 
 static __u32 parity_segment_depth(struct imsm_dev *dev)
 {
-	struct imsm_map *map = get_imsm_map(dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
 	__u32 chunk =  __le32_to_cpu(map->blocks_per_strip);
 
 	switch(get_imsm_raid_level(map)) {
@@ -1958,7 +1957,7 @@ static __u32 parity_segment_depth(struct
 
 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
 {
-	struct imsm_map *map = get_imsm_map(dev, 1);
+	struct imsm_map *map = get_imsm_map(dev, MAP_1);
 	__u32 chunk = __le32_to_cpu(map->blocks_per_strip);
 	__u32 strip = block / chunk;
 
@@ -1997,7 +1996,7 @@ static __u64 blocks_per_migr_unit(struct
 	case MIGR_VERIFY:
 	case MIGR_REPAIR:
 	case MIGR_INIT: {
-		struct imsm_map *map = get_imsm_map(dev, 0);
+		struct imsm_map *map = get_imsm_map(dev, MAP_0);
 		__u32 stripes_per_unit;
 		__u32 blocks_per_unit;
 		__u32 parity_depth;
@@ -2013,7 +2012,7 @@ static __u64 blocks_per_migr_unit(struct
 		 */
 		stripes_per_unit = num_stripes_per_unit_resync(dev);
 		migr_chunk = migr_strip_blocks_resync(dev);
-		disks = imsm_num_data_members(dev, 0);
+		disks = imsm_num_data_members(dev, MAP_0);
 		blocks_per_unit = stripes_per_unit * migr_chunk * disks;
 		stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
 		segment = blocks_per_unit / stripe;
@@ -2286,8 +2285,8 @@ static void getinfo_super_imsm_volume(st
 	struct intel_super *super = st->sb;
 	struct migr_record *migr_rec = super->migr_rec;
 	struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
-	struct imsm_map *map = get_imsm_map(dev, 0);
-	struct imsm_map *prev_map = get_imsm_map(dev, 1);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
+	struct imsm_map *prev_map = get_imsm_map(dev, MAP_1);
 	struct imsm_map *map_to_analyse = map;
 	struct dl *dl;
 	char *devname;
@@ -2432,7 +2431,7 @@ static void getinfo_super_imsm_volume(st
 				(unsigned long long)blocks_per_unit,
 				info->reshape_progress);
 
-			used_disks = imsm_num_data_members(dev, 1);
+			used_disks = imsm_num_data_members(dev, MAP_1);
 			if (used_disks > 0) {
 				array_blocks = map->blocks_per_member *
 					used_disks;
@@ -2477,7 +2476,7 @@ static void getinfo_super_imsm_volume(st
 			dmap[i] = 0;
 			if (i < info->array.raid_disks) {
 				struct imsm_disk *dsk;
-				j = get_imsm_disk_idx(dev, i, -1);
+				j = get_imsm_disk_idx(dev, i, MAP_X);
 				dsk = get_imsm_disk(super, j);
 				if (dsk && (dsk->status & CONFIGURED_DISK))
 					dmap[i] = 1;
@@ -2500,8 +2499,7 @@ static void manage_second_map(struct int
 		struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
 
 		failed = imsm_count_failed(super, dev, MAP_1);
-		map_state = imsm_check_degraded(super, dev, failed,
-						MAP_1);
+		map_state = imsm_check_degraded(super, dev, failed, MAP_1);
 		if (map2->map_state != map_state) {
 			map2->map_state = map_state;
 			super->updates_pending++;
@@ -2570,13 +2568,13 @@ static void getinfo_super_imsm(struct su
 
 		failed = imsm_count_failed(super, dev, MAP_0);
 		state = imsm_check_degraded(super, dev, failed, MAP_0);
-		map = get_imsm_map(dev, 0);
+		map = get_imsm_map(dev, MAP_0);
 
 		/* any newly missing disks?
 		 * (catches single-degraded vs double-degraded)
 		 */
 		for (j = 0; j < map->num_members; j++) {
-			__u32 ord = get_imsm_ord_tbl_ent(dev, j, 0);
+			__u32 ord = get_imsm_ord_tbl_ent(dev, j, MAP_0);
 			__u32 idx = ord_to_idx(ord);
 
 			if (!(ord & IMSM_ORD_REBUILD) &&
@@ -3101,12 +3099,12 @@ static void migrate(struct imsm_dev *dev
 		    __u8 to_state, int migr_type)
 {
 	struct imsm_map *dest;
-	struct imsm_map *src = get_imsm_map(dev, 0);
+	struct imsm_map *src = get_imsm_map(dev, MAP_0);
 
 	dev->vol.migr_state = 1;
 	set_migr_type(dev, migr_type);
 	dev->vol.curr_migr_unit = 0;
-	dest = get_imsm_map(dev, 1);
+	dest = get_imsm_map(dev, MAP_1);
 
 	/* duplicate and then set the target end state in map[0] */
 	memcpy(dest, src, sizeof_imsm_map(src));
@@ -3131,8 +3129,9 @@ static void migrate(struct imsm_dev *dev
 static void end_migration(struct imsm_dev *dev, struct intel_super *super,
 			  __u8 map_state)
 {
-	struct imsm_map *map = get_imsm_map(dev, 0);
-	struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
+	struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state == 0 ?
+						    MAP_0 : MAP_1);
 	int i, j;
 
 	/* merge any IMSM_ORD_REBUILD bits that were not successfully
@@ -3264,8 +3263,8 @@ int check_mpb_migr_compatibility(struct
 		    dev_iter->vol.migr_state == 1 &&
 		    dev_iter->vol.migr_type == MIGR_GEN_MIGR) {
 			/* This device is migrating */
-			map0 = get_imsm_map(dev_iter, 0);
-			map1 = get_imsm_map(dev_iter, 1);
+			map0 = get_imsm_map(dev_iter, MAP_0);
+			map1 = get_imsm_map(dev_iter, MAP_1);
 			if (map0->pba_of_lba0 != map1->pba_of_lba0)
 				/* migration optimization area was used */
 				return -1;
@@ -4152,7 +4151,7 @@ static void imsm_update_version_info(str
 
 	for (i = 0; i < mpb->num_raid_devs; i++) {
 		dev = get_imsm_dev(super, i);
-		map = get_imsm_map(dev, 0);
+		map = get_imsm_map(dev, MAP_0);
 		if (__le32_to_cpu(dev->size_high) > 0)
 			mpb->attributes |= MPB_ATTRIB_2TB;
 
@@ -4331,7 +4330,7 @@ static int init_super_imsm_volume(struct
 	set_migr_type(dev, MIGR_INIT);
 	vol->dirty = !info->state;
 	vol->curr_migr_unit = 0;
-	map = get_imsm_map(dev, 0);
+	map = get_imsm_map(dev, MAP_0);
 	map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
 	map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
 	map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
@@ -4455,7 +4454,7 @@ static int add_to_super_imsm_volume(stru
 	int slot;
 
 	dev = get_imsm_dev(super, super->current_vol);
-	map = get_imsm_map(dev, 0);
+	map = get_imsm_map(dev, MAP_0);
 
 	if (! (dk->state & (1<<MD_DISK_SYNC))) {
 		fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
@@ -4490,7 +4489,7 @@ static int add_to_super_imsm_volume(stru
 	/* Check the device has not already been added */
 	slot = get_imsm_disk_slot(map, dl->index);
 	if (slot >= 0 &&
-	    (get_imsm_ord_tbl_ent(dev, slot, -1) & IMSM_ORD_REBUILD) == 0) {
+	    (get_imsm_ord_tbl_ent(dev, slot, MAP_X) & IMSM_ORD_REBUILD) == 0) {
 		fprintf(stderr, Name ": %s has been included in this array twice\n",
 			devname);
 		return 1;
@@ -4514,19 +4513,20 @@ static int add_to_super_imsm_volume(stru
 	/* refresh unset/failed slots to point to valid 'missing' entries */
 	for (df = super->missing; df; df = df->next)
 		for (slot = 0; slot < mpb->num_disks; slot++) {
-			__u32 ord = get_imsm_ord_tbl_ent(dev, slot, -1);
+			__u32 ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
 
 			if ((ord & IMSM_ORD_REBUILD) == 0)
 				continue;
 			set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
 			if (is_gen_migration(dev)) {
-				struct imsm_map *map2 = get_imsm_map(dev, 1);
+				struct imsm_map *map2 = get_imsm_map(dev,
+								     MAP_1);
 				int slot2 = get_imsm_disk_slot(map2, df->index);
 				if ((slot2 < map2->num_members) &&
 				    (slot2 >= 0)) {
 					__u32 ord2 = get_imsm_ord_tbl_ent(dev,
-									  slot2,
-									  1);
+									 slot2,
+									 MAP_1);
 					if ((unsigned)df->index ==
 							       ord_to_idx(ord2))
 						set_imsm_ord_tbl_ent(map2,
@@ -4845,7 +4845,7 @@ static int create_array(struct supertype
 	struct imsm_update_create_array *u;
 	struct intel_super *super = st->sb;
 	struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
-	struct imsm_map *map = get_imsm_map(dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
 	struct disk_info *inf;
 	struct imsm_disk *disk;
 	int i;
@@ -4864,7 +4864,7 @@ static int create_array(struct supertype
 	imsm_copy_dev(&u->dev, dev);
 	inf = get_disk_info(u);
 	for (i = 0; i < map->num_members; i++) {
-		int idx = get_imsm_disk_idx(dev, i, -1);
+		int idx = get_imsm_disk_idx(dev, i, MAP_X);
 
 		disk = get_imsm_disk(super, idx);
 		serialcpy(inf[i].serial, disk->serial);
@@ -5711,7 +5711,7 @@ static int is_rebuilding(struct imsm_dev
 	if (migr_type(dev) != MIGR_REBUILD)
 		return 0;
 
-	migr_map = get_imsm_map(dev, 1);
+	migr_map = get_imsm_map(dev, MAP_1);
 
 	if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
 		return 1;
@@ -5729,7 +5729,7 @@ static int is_initializing(struct imsm_d
 	if (migr_type(dev) != MIGR_INIT)
 		return 0;
 
-	migr_map = get_imsm_map(dev, 1);
+	migr_map = get_imsm_map(dev, MAP_1);
 
 	if (migr_map->map_state == IMSM_T_STATE_UNINITIALIZED)
 		return 1;
@@ -5827,8 +5827,8 @@ static struct mdinfo *container_content_
 			continue;
 
 		dev = get_imsm_dev(super, i);
-		map = get_imsm_map(dev, 0);
-		map2 = get_imsm_map(dev, 1);
+		map = get_imsm_map(dev, MAP_0);
+		map2 = get_imsm_map(dev, MAP_1);
 
 		/* do not publish arrays that are in the middle of an
 		 * unsupported migration
@@ -5887,8 +5887,8 @@ static struct mdinfo *container_content_
 			__u32 ord;
 
 			skip = 0;
-			idx = get_imsm_disk_idx(dev, slot, 0);
-			ord = get_imsm_ord_tbl_ent(dev, slot, -1);
+			idx = get_imsm_disk_idx(dev, slot, MAP_0);
+			ord = get_imsm_ord_tbl_ent(dev, slot, MAP_X);
 			for (d = super->disks; d ; d = d->next)
 				if (d->index == idx)
 					break;
@@ -6003,7 +6003,7 @@ static __u8 imsm_check_degraded(struct i
 		int insync = insync;
 
 		for (i = 0; i < map->num_members; i++) {
-			__u32 ord = get_imsm_ord_tbl_ent(dev, i, -1);
+			__u32 ord = get_imsm_ord_tbl_ent(dev, i, MAP_X);
 			int idx = ord_to_idx(ord);
 			struct imsm_disk *disk;
 
@@ -6064,8 +6064,11 @@ static int imsm_count_failed(struct inte
 
 	for (i = 0; i < map_for_loop->num_members; i++) {
 		idx_1 = -255;
+		/* when MAP_X is passed both maps failures are counted
+		 */
 		if (prev &&
-		    (look_in_map & MAP_1) && (i < prev->num_members)) {
+		    ((look_in_map == MAP_1) || (look_in_map == MAP_X)) &&
+		    (i < prev->num_members)) {
 			ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
 			idx_1 = ord_to_idx(ord);
 
@@ -6073,7 +6076,8 @@ static int imsm_count_failed(struct inte
 			if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
 				failed++;
 		}
-		if ((look_in_map & MAP_0) && (i < map->num_members)) {
+		if (((look_in_map == MAP_0) || (look_in_map == MAP_X)) &&
+		    (i < map->num_members)) {
 			ord = __le32_to_cpu(map->disk_ord_tbl[i]);
 			idx = ord_to_idx(ord);
 
@@ -6121,7 +6125,7 @@ static int is_resyncing(struct imsm_dev
 	if (migr_type(dev) == MIGR_GEN_MIGR)
 		return 0;
 
-	migr_map = get_imsm_map(dev, 1);
+	migr_map = get_imsm_map(dev, MAP_1);
 
 	if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
 	    (dev->vol.migr_type != MIGR_GEN_MIGR))
@@ -6140,7 +6144,7 @@ static int mark_failure(struct imsm_dev
 	unsigned int len, shift = 0;
 
 	/* new failures are always set in map[0] */
-	map = get_imsm_map(dev, 0);
+	map = get_imsm_map(dev, MAP_0);
 
 	slot = get_imsm_disk_slot(map, idx);
 	if (slot < 0)
@@ -6164,7 +6168,7 @@ static int mark_failure(struct imsm_dev
 	 * This is valid for migration, initialization and rebuild
 	 */
 	if (dev->vol.migr_state) {
-		struct imsm_map *map2 = get_imsm_map(dev, 1);
+		struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
 		int slot2 = get_imsm_disk_slot(map2, idx);
 
 		if ((slot2 < map2->num_members) &&
@@ -6214,7 +6218,7 @@ static void handle_missing(struct intel_
 
 static unsigned long long imsm_set_array_size(struct imsm_dev *dev)
 {
-	int used_disks = imsm_num_data_members(dev, 0);
+	int used_disks = imsm_num_data_members(dev, MAP_0);
 	unsigned long long array_blocks;
 	struct imsm_map *map;
 
@@ -6231,7 +6235,7 @@ static unsigned long long imsm_set_array
 
 	/* set array size in metadata
 	 */
-	map = get_imsm_map(dev, 0);
+	map = get_imsm_map(dev, MAP_0);
 	array_blocks = map->blocks_per_member * used_disks;
 
 	/* round array size down to closest MB
@@ -6259,7 +6263,7 @@ static void imsm_progress_container_resh
 
 	for (i = 0; i < mpb->num_raid_devs; i++) {
 		struct imsm_dev *dev = get_imsm_dev(super, i);
-		struct imsm_map *map = get_imsm_map(dev, 0);
+		struct imsm_map *map = get_imsm_map(dev, MAP_0);
 		struct imsm_map *map2;
 		int prev_num_members;
 
@@ -6284,7 +6288,7 @@ static void imsm_progress_container_resh
 		for (i = prev_num_members;
 		     i < map->num_members; i++)
 			set_imsm_ord_tbl_ent(map, i, i);
-		map2 = get_imsm_map(dev, 1);
+		map2 = get_imsm_map(dev, MAP_1);
 		/* Copy the current map */
 		memcpy(map2, map, copy_map_size);
 		map2->num_members = prev_num_members;
@@ -6304,7 +6308,7 @@ static int imsm_set_array_state(struct a
 	int inst = a->info.container_member;
 	struct intel_super *super = a->container->sb;
 	struct imsm_dev *dev = get_imsm_dev(super, inst);
-	struct imsm_map *map = get_imsm_map(dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
 	int failed = imsm_count_failed(super, dev, MAP_0);
 	__u8 map_state = imsm_check_degraded(super, dev, failed, MAP_0);
 	__u32 blocks_per_unit;
@@ -6328,12 +6332,14 @@ static int imsm_set_array_state(struct a
 				 * user action is required to recover process
 				 */
 				if (0) {
-				struct imsm_map *map2 = get_imsm_map(dev, 1);
-				dev->vol.migr_state = 0;
-				set_migr_type(dev, 0);
-				dev->vol.curr_migr_unit = 0;
-				memcpy(map, map2, sizeof_imsm_map(map2));
-				super->updates_pending++;
+					struct imsm_map *map2 =
+						get_imsm_map(dev, MAP_1);
+					dev->vol.migr_state = 0;
+					set_migr_type(dev, 0);
+					dev->vol.curr_migr_unit = 0;
+					memcpy(map, map2,
+					       sizeof_imsm_map(map2));
+					super->updates_pending++;
 				}
 			}
 			if (a->last_checkpoint >= a->info.component_size) {
@@ -6341,7 +6347,7 @@ static int imsm_set_array_state(struct a
 				int used_disks;
 				struct mdinfo *mdi;
 
-				used_disks = imsm_num_data_members(dev, 0);
+				used_disks = imsm_num_data_members(dev, MAP_0);
 				if (used_disks > 0) {
 					array_blocks =
 						map->blocks_per_member *
@@ -6448,7 +6454,7 @@ static void imsm_set_disk(struct active_
 	int inst = a->info.container_member;
 	struct intel_super *super = a->container->sb;
 	struct imsm_dev *dev = get_imsm_dev(super, inst);
-	struct imsm_map *map = get_imsm_map(dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
 	struct imsm_disk *disk;
 	int failed;
 	__u32 ord;
@@ -6463,7 +6469,7 @@ static void imsm_set_disk(struct active_
 
 	dprintf("imsm: set_disk %d:%x\n", n, state);
 
-	ord = get_imsm_ord_tbl_ent(dev, n, 0);
+	ord = get_imsm_ord_tbl_ent(dev, n, MAP_0);
 	disk = get_imsm_disk(super, ord_to_idx(ord));
 
 	/* check for new failures */
@@ -6474,7 +6480,7 @@ static void imsm_set_disk(struct active_
 
 	/* check if in_sync */
 	if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
-		struct imsm_map *migr_map = get_imsm_map(dev, 1);
+		struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
 
 		set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
 		super->updates_pending++;
@@ -6491,7 +6497,7 @@ static void imsm_set_disk(struct active_
 		if (is_rebuilding(dev)) {
 			dprintf("while rebuilding");
 			end_migration(dev, super, map_state);
-			map = get_imsm_map(dev, 0);
+			map = get_imsm_map(dev, MAP_0);
 			map->failed_disk_num = ~0;
 			super->updates_pending++;
 			a->last_checkpoint = 0;
@@ -6503,7 +6509,7 @@ static void imsm_set_disk(struct active_
 				end_migration(dev, super, map_state);
 			else
 				map->map_state = map_state;
-			map = get_imsm_map(dev, 0);
+			map = get_imsm_map(dev, MAP_0);
 			map->failed_disk_num = ~0;
 			super->updates_pending++;
 			break;
@@ -6617,7 +6623,7 @@ static void imsm_sync_metadata(struct su
 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
 {
 	struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
-	int i = get_imsm_disk_idx(dev, idx, -1);
+	int i = get_imsm_disk_idx(dev, idx, MAP_X);
 	struct dl *dl;
 
 	for (dl = super->disks; dl; dl = dl->next)
@@ -6638,7 +6644,7 @@ static struct dl *imsm_add_spare(struct
 				 struct mdinfo *additional_test_list)
 {
 	struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
-	int idx = get_imsm_disk_idx(dev, slot, -1);
+	int idx = get_imsm_disk_idx(dev, slot, MAP_X);
 	struct imsm_super *mpb = super->anchor;
 	struct imsm_map *map;
 	unsigned long long pos;
@@ -6700,7 +6706,7 @@ static struct dl *imsm_add_spare(struct
 		}
 		for (i = 0; i < mpb->num_raid_devs; i++) {
 			dev = get_imsm_dev(super, i);
-			map = get_imsm_map(dev, 0);
+			map = get_imsm_map(dev, MAP_0);
 
 			/* check if this disk is already a member of
 			 * this array
@@ -6756,10 +6762,9 @@ static int imsm_rebuild_allowed(struct s
 
 	dev2 = get_imsm_dev(cont->sb, dev_idx);
 	if (dev2) {
-		state = imsm_check_degraded(cont->sb, dev2, failed,
-					    MAP_0);
+		state = imsm_check_degraded(cont->sb, dev2, failed, MAP_0);
 		if (state == IMSM_T_STATE_FAILED) {
-			map = get_imsm_map(dev2, 0);
+			map = get_imsm_map(dev2, MAP_0);
 			if (!map)
 				return 1;
 			for (slot = 0; slot < map->num_members; slot++) {
@@ -6767,7 +6772,7 @@ static int imsm_rebuild_allowed(struct s
 				 * Check if failed disks are deleted from intel
 				 * disk list or are marked to be deleted
 				 */
-				idx = get_imsm_disk_idx(dev2, slot, -1);
+				idx = get_imsm_disk_idx(dev2, slot, MAP_X);
 				idisk = get_imsm_dl_disk(cont->sb, idx);
 				/*
 				 * Do not rebuild the array if failed disks
@@ -6801,7 +6806,7 @@ static struct mdinfo *imsm_activate_spar
 	struct intel_super *super = a->container->sb;
 	int inst = a->info.container_member;
 	struct imsm_dev *dev = get_imsm_dev(super, inst);
-	struct imsm_map *map = get_imsm_map(dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
 	int failed = a->info.array.raid_disks;
 	struct mdinfo *rv = NULL;
 	struct mdinfo *d;
@@ -6974,15 +6979,15 @@ static struct mdinfo *imsm_activate_spar
 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
 {
 	struct imsm_dev *dev = get_imsm_dev(super, idx);
-	struct imsm_map *map = get_imsm_map(dev, 0);
-	struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
+	struct imsm_map *new_map = get_imsm_map(&u->dev, MAP_0);
 	struct disk_info *inf = get_disk_info(u);
 	struct imsm_disk *disk;
 	int i;
 	int j;
 
 	for (i = 0; i < map->num_members; i++) {
-		disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, -1));
+		disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, MAP_X));
 		for (j = 0; j < new_map->num_members; j++)
 			if (serialcmp(disk->serial, inf[j].serial) == 0)
 				return 1;
@@ -7095,7 +7100,7 @@ static int apply_reshape_migration_updat
 			struct imsm_map *map;
 			struct imsm_dev *new_dev =
 				(struct imsm_dev *)*space_list;
-			struct imsm_map *migr_map = get_imsm_map(dev, 1);
+			struct imsm_map *migr_map = get_imsm_map(dev, MAP_1);
 			int to_state;
 			struct dl *new_disk;
 
@@ -7103,7 +7108,7 @@ static int apply_reshape_migration_updat
 				return ret_val;
 			*space_list = **space_list;
 			memcpy(new_dev, dev, sizeof_imsm_dev(dev, 0));
-			map = get_imsm_map(new_dev, 0);
+			map = get_imsm_map(new_dev, MAP_0);
 			if (migr_map) {
 				dprintf("imsm: Error: migration in progress");
 				return ret_val;
@@ -7123,7 +7128,7 @@ static int apply_reshape_migration_updat
 			migrate(new_dev, super, to_state, MIGR_GEN_MIGR);
 			if (u->new_level > -1)
 				map->raid_level = u->new_level;
-			migr_map = get_imsm_map(new_dev, 1);
+			migr_map = get_imsm_map(new_dev, MAP_1);
 			if ((u->new_level == 5) &&
 			    (migr_map->raid_level == 0)) {
 				int ord = map->num_members - 1;
@@ -7198,7 +7203,7 @@ static int apply_update_activate_spare(s
 {
 	struct imsm_super *mpb = super->anchor;
 	struct imsm_dev *dev = get_imsm_dev(super, u->array);
-	struct imsm_map *map = get_imsm_map(dev, 0);
+	struct imsm_map *map = get_imsm_map(dev, MAP_0);
 	struct imsm_map *migr_map;
 	struct active_array *a;
 	struct imsm_disk *disk;
@@ -7211,7 +7216,7 @@ static int apply_update_activate_spare(s
 	int second_map_created = 0;
 
 	for (; u; u = u->next) {
-		victim = get_imsm_disk_idx(dev, u->slot, -1);
+		victim = get_imsm_disk_idx(dev, u->slot, MAP_X);
 
 		if (victim < 0)
 			return 0;
@@ -7235,7 +7240,7 @@ static int apply_update_activate_spare(s
 			if (i == u->slot)
 				continue;
 			disk = get_imsm_disk(super,
-					     get_imsm_disk_idx(dev, i, -1));
+					     get_imsm_disk_idx(dev, i, MAP_X));
 			if (!disk || is_failed(disk))
 				failed++;
 		}
@@ -7250,15 +7255,14 @@ static int apply_update_activate_spare(s
 		disk->status &= ~SPARE_DISK;
 
 		/* mark rebuild */
-		to_state = imsm_check_degraded(super, dev, failed,
-					       MAP_0);
+		to_state = imsm_check_degraded(super, dev, failed, MAP_0);
 		if (!second_map_created) {
 			second_map_created = 1;
 			map->map_state = IMSM_T_STATE_DEGRADED;
 			migrate(dev, super, to_state, MIGR_REBUILD);
 		} else
 			map->map_state = to_state;
-		migr_map = get_imsm_map(dev, 1);
+		migr_map = get_imsm_map(dev, MAP_1);
 		set_imsm_ord_tbl_ent(map, u->slot, dl->index);
 		set_imsm_ord_tbl_ent(migr_map, u->slot,
 				     dl->index | IMSM_ORD_REBUILD);
@@ -7276,7 +7280,7 @@ static int apply_update_activate_spare(s
 		found = 0;
 		for (a = active_array; a ; a = a->next) {
 			dev = get_imsm_dev(super, a->info.container_member);
-			map = get_imsm_map(dev, 0);
+			map = get_imsm_map(dev, MAP_0);
 
 			if (get_imsm_disk_slot(map, victim) >= 0)
 				found++;
@@ -7368,8 +7372,8 @@ static int apply_reshape_container_disks
 		newdev = (void*)sp;
 		/* Copy the dev, but not (all of) the map */
 		memcpy(newdev, id->dev, sizeof(*newdev));
-		oldmap = get_imsm_map(id->dev, 0);
-		newmap = get_imsm_map(newdev, 0);
+		oldmap = get_imsm_map(id->dev, MAP_0);
+		newmap = get_imsm_map(newdev, MAP_0);
 		/* Copy the current map */
 		memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
 		/* update one device only
@@ -7389,7 +7393,7 @@ static int apply_reshape_container_disks
 			}
 			/* New map is correct, now need to save old map
 			 */
-			newmap = get_imsm_map(newdev, 1);
+			newmap = get_imsm_map(newdev, MAP_1);
 			memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
 
 			imsm_set_array_size(newdev);
@@ -7432,7 +7436,7 @@ static int apply_takeover_update(struct
 	if (dev == NULL)
 		return 0;
 
-	map = get_imsm_map(dev, 0);
+	map = get_imsm_map(dev, MAP_0);
 
 	if (u->direction == R10_TO_R0) {
 		/* Number of failed disks must be half of initial disk number */
@@ -7496,7 +7500,7 @@ static int apply_takeover_update(struct
 		dev_new = (void *)space;
 		memcpy(dev_new, dev, sizeof(*dev));
 		/* update new map */
-		map = get_imsm_map(dev_new, 0);
+		map = get_imsm_map(dev_new, MAP_0);
 		map->num_members = map->num_members * 2;
 		map->map_state = IMSM_T_STATE_DEGRADED;
 		map->num_domains = 2;
@@ -7640,7 +7644,7 @@ static void imsm_process_update(struct s
 			goto create_error;
 		}
 
-		new_map = get_imsm_map(&u->dev, 0);
+		new_map = get_imsm_map(&u->dev, MAP_0);
 		new_start = __le32_to_cpu(new_map->pba_of_lba0);
 		new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
 		inf = get_disk_info(u);
@@ -7651,7 +7655,7 @@ static void imsm_process_update(struct s
 		 */
 		for (i = 0; i < mpb->num_raid_devs; i++) {
 			dev = get_imsm_dev(super, i);
-			map = get_imsm_map(dev, 0);
+			map = get_imsm_map(dev, MAP_0);
 			start = __le32_to_cpu(map->pba_of_lba0);
 			end = start + __le32_to_cpu(map->blocks_per_member);
 			if ((new_start >= start && new_start <= end) ||
@@ -7832,7 +7836,7 @@ static void imsm_prepare_update(struct s
 		if (u->direction == R0_TO_R10) {
 			void **tail = (void **)&update->space_list;
 			struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
-			struct imsm_map *map = get_imsm_map(dev, 0);
+			struct imsm_map *map = get_imsm_map(dev, MAP_0);
 			int num_members = map->num_members;
 			void *space;
 			int size, i;
@@ -7964,7 +7968,7 @@ static void imsm_prepare_update(struct s
 				struct imsm_map *map;
 
 				dev = get_imsm_dev(super, u->subdev);
-				map = get_imsm_map(dev, 0);
+				map = get_imsm_map(dev, MAP_0);
 				current_level = map->raid_level;
 				break;
 			}
@@ -7998,7 +8002,7 @@ static void imsm_prepare_update(struct s
 		struct imsm_update_create_array *u = (void *) update->buf;
 		struct intel_dev *dv;
 		struct imsm_dev *dev = &u->dev;
-		struct imsm_map *map = get_imsm_map(dev, 0);
+		struct imsm_map *map = get_imsm_map(dev, MAP_0);
 		struct dl *dl;
 		struct disk_info *inf;
 		int i;
@@ -8083,20 +8087,20 @@ static void imsm_delete(struct intel_sup
 
 	for (i = 0; i < mpb->num_raid_devs; i++) {
 		dev = get_imsm_dev(super, i);
-		map = get_imsm_map(dev, 0);
+		map = get_imsm_map(dev, MAP_0);
 		num_members = map->num_members;
 		for (j = 0; j < num_members; j++) {
 			/* update ord entries being careful not to propagate
 			 * ord-flags to the first map
 			 */
-			ord = get_imsm_ord_tbl_ent(dev, j, -1);
+			ord = get_imsm_ord_tbl_ent(dev, j, MAP_X);
 
 			if (ord_to_idx(ord) <= index)
 				continue;
 
-			map = get_imsm_map(dev, 0);
+			map = get_imsm_map(dev, MAP_0);
 			set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
-			map = get_imsm_map(dev, 1);
+			map = get_imsm_map(dev, MAP_1);
 			if (map)
 				set_imsm_ord_tbl_ent(map, j, ord - 1);
 		}
@@ -8142,7 +8146,7 @@ static int imsm_get_allowed_degradation(
 		/* check map if all disks pairs not failed
 		 * in both maps
 		 */
-		map = get_imsm_map(dev, 0);
+		map = get_imsm_map(dev, MAP_0);
 		for (i = 0; i < ret_val; i++) {
 			int degradation = 0;
 			if (get_imsm_disk(super, i) == NULL)
@@ -8152,7 +8156,7 @@ static int imsm_get_allowed_degradation(
 			if (degradation == 2)
 				return 0;
 		}
-		map = get_imsm_map(dev, 1);
+		map = get_imsm_map(dev, MAP_1);
 		/* if there is no second map
 		 * result can be returned
 		 */
@@ -8261,8 +8265,8 @@ void init_migr_record_imsm(struct supert
 	struct mdinfo *sd;
 	char nm[30];
 	int fd;
-	struct imsm_map *map_dest = get_imsm_map(dev, 0);
-	struct imsm_map *map_src = get_imsm_map(dev, 1);
+	struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
+	struct imsm_map *map_src = get_imsm_map(dev, MAP_1);
 	unsigned long long num_migr_units;
 	unsigned long long array_blocks;
 
@@ -8275,7 +8279,7 @@ void init_migr_record_imsm(struct supert
 	migr_rec->dest_depth_per_unit = GEN_MIGR_AREA_SIZE /
 		max(map_dest->blocks_per_strip, map_src->blocks_per_strip);
 	migr_rec->dest_depth_per_unit *= map_dest->blocks_per_strip;
-	new_data_disks = imsm_num_data_members(dev, 0);
+	new_data_disks = imsm_num_data_members(dev, MAP_0);
 	migr_rec->blocks_per_unit =
 		__cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
 	migr_rec->dest_depth_per_unit =
@@ -8339,12 +8343,12 @@ int save_backup_imsm(struct supertype *s
 	unsigned long long *target_offsets = NULL;
 	int *targets = NULL;
 	int i;
-	struct imsm_map *map_dest = get_imsm_map(dev, 0);
+	struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
 	int new_disks = map_dest->num_members;
 	int dest_layout = 0;
 	int dest_chunk;
 	unsigned long long start;
-	int data_disks = imsm_num_data_members(dev, 0);
+	int data_disks = imsm_num_data_members(dev, MAP_0);
 
 	targets = malloc(new_disks * sizeof(int));
 	if (!targets)
@@ -8504,7 +8508,7 @@ int recover_backup_imsm(struct supertype
 	if (id == NULL)
 		return 1;
 
-	map_dest = get_imsm_map(id->dev, 0);
+	map_dest = get_imsm_map(id->dev, MAP_0);
 	new_disks = map_dest->num_members;
 
 	read_offset = (unsigned long long)
@@ -8889,7 +8893,7 @@ static int imsm_create_metadata_update_f
 	if (dev) {
 		struct imsm_map *map;
 
-		map = get_imsm_map(dev, 0);
+		map = get_imsm_map(dev, MAP_0);
 		if (map) {
 			int current_chunk_size =
 				__le16_to_cpu(map->blocks_per_strip) / 2;
@@ -9401,12 +9405,12 @@ static int imsm_manage_reshape(
 		goto abort;
 	}
 
-	map_src = get_imsm_map(dev, 1);
+	map_src = get_imsm_map(dev, MAP_1);
 	if (map_src == NULL)
 		goto abort;
 
-	ndata = imsm_num_data_members(dev, 0);
-	odata = imsm_num_data_members(dev, 1);
+	ndata = imsm_num_data_members(dev, MAP_0);
+	odata = imsm_num_data_members(dev, MAP_1);
 
 	chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
 	old_data_stripe_length = odata * chunk;
