Blame SOURCES/0104-mdadm-Detail-show-correct-state-for-clustered-array.patch

3c4af5
From 9c030dadba89b90a4e52b6afe0290076c809684c Mon Sep 17 00:00:00 2001
3c4af5
From: Zhao Heming <heming.zhao@suse.com>
3c4af5
Date: Sat, 24 Oct 2020 17:43:12 +0800
3c4af5
Subject: [PATCH 104/108] mdadm/Detail: show correct state for clustered array
3c4af5
3c4af5
After kernel md module commit 480523feae581, in clustered env,
3c4af5
mddev->in_sync always zero, it will make array.state never set
3c4af5
up MD_SB_CLEAN. it causes "mdadm -D /dev/mdX" show state 'active'
3c4af5
all the time.
3c4af5
3c4af5
bitmap.c: add a new API IsBitmapDirty() to support inquiry bitmap
3c4af5
dirty or clean.
3c4af5
3c4af5
Signed-off-by: Zhao Heming <heming.zhao@suse.com>
3c4af5
---
3c4af5
 Detail.c | 20 ++++++++++++++++-
3c4af5
 bitmap.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
3c4af5
 mdadm.h  |  1 +
3c4af5
 3 files changed, 86 insertions(+), 10 deletions(-)
3c4af5
3c4af5
diff --git a/Detail.c b/Detail.c
3c4af5
index ea86884..f8dea6f 100644
3c4af5
--- a/Detail.c
3c4af5
+++ b/Detail.c
3c4af5
@@ -498,8 +498,26 @@ int Detail(char *dev, struct context *c)
3c4af5
 							  sra->array_state);
3c4af5
 				else
3c4af5
 					arrayst = "clean";
3c4af5
-			} else
3c4af5
+			} else {
3c4af5
 				arrayst = "active";
3c4af5
+				if (array.state & (1<
3c4af5
+					for (d = 0; d < max_disks * 2; d++) {
3c4af5
+						char *dv;
3c4af5
+						mdu_disk_info_t disk = disks[d];
3c4af5
+
3c4af5
+						/* only check first valid disk in cluster env */
3c4af5
+						if ((disk.state & (MD_DISK_SYNC | MD_DISK_ACTIVE))
3c4af5
+							&& (disk.major | disk.minor)) {
3c4af5
+							dv = map_dev_preferred(disk.major, disk.minor, 0,
3c4af5
+									c->prefer);
3c4af5
+							if (!dv)
3c4af5
+								continue;
3c4af5
+							arrayst = IsBitmapDirty(dv) ? "active" : "clean";
3c4af5
+							break;
3c4af5
+						}
3c4af5
+					}
3c4af5
+				}
3c4af5
+			}
3c4af5
 
3c4af5
 			printf("             State : %s%s%s%s%s%s%s \n",
3c4af5
 			       arrayst, st,
3c4af5
diff --git a/bitmap.c b/bitmap.c
3c4af5
index e38cb96..9a7ffe3 100644
3c4af5
--- a/bitmap.c
3c4af5
+++ b/bitmap.c
3c4af5
@@ -180,13 +180,14 @@ out:
3c4af5
 }
3c4af5
 
3c4af5
 static int
3c4af5
-bitmap_file_open(char *filename, struct supertype **stp, int node_num)
3c4af5
+bitmap_file_open(char *filename, struct supertype **stp, int node_num, int fd)
3c4af5
 {
3c4af5
-	int fd;
3c4af5
 	struct stat stb;
3c4af5
 	struct supertype *st = *stp;
3c4af5
 
3c4af5
-	fd = open(filename, O_RDONLY|O_DIRECT);
3c4af5
+	/* won't re-open filename when (fd >= 0) */
3c4af5
+	if (fd < 0)
3c4af5
+		fd = open(filename, O_RDONLY|O_DIRECT);
3c4af5
 	if (fd < 0) {
3c4af5
 		pr_err("failed to open bitmap file %s: %s\n",
3c4af5
 		       filename, strerror(errno));
3c4af5
@@ -249,7 +250,7 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
3c4af5
 	int fd, i;
3c4af5
 	__u32 uuid32[4];
3c4af5
 
3c4af5
-	fd = bitmap_file_open(filename, &st, 0);
3c4af5
+	fd = bitmap_file_open(filename, &st, 0, -1);
3c4af5
 	if (fd < 0)
3c4af5
 		return rv;
3c4af5
 
3c4af5
@@ -263,7 +264,6 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
3c4af5
 		pr_err("Reporting bitmap that would be used if this array were used\n");
3c4af5
 		pr_err("as a member of some other array\n");
3c4af5
 	}
3c4af5
-	close(fd);
3c4af5
 	printf("        Filename : %s\n", filename);
3c4af5
 	printf("           Magic : %08x\n", sb->magic);
3c4af5
 	if (sb->magic != BITMAP_MAGIC) {
3c4af5
@@ -332,15 +332,13 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
3c4af5
 		for (i = 0; i < (int)sb->nodes; i++) {
3c4af5
 			st = NULL;
3c4af5
 			free(info);
3c4af5
-			fd = bitmap_file_open(filename, &st, i);
3c4af5
+			fd = bitmap_file_open(filename, &st, i, fd);
3c4af5
 			if (fd < 0) {
3c4af5
 				printf("   Unable to open bitmap file on node: %i\n", i);
3c4af5
-
3c4af5
 				continue;
3c4af5
 			}
3c4af5
 			info = bitmap_fd_read(fd, brief);
3c4af5
 			if (!info) {
3c4af5
-				close(fd);
3c4af5
 				printf("   Unable to read bitmap on node: %i\n", i);
3c4af5
 				continue;
3c4af5
 			}
3c4af5
@@ -359,13 +357,72 @@ int ExamineBitmap(char *filename, int brief, struct supertype *st)
3c4af5
 			printf("          Bitmap : %llu bits (chunks), %llu dirty (%2.1f%%)\n",
3c4af5
 			       info->total_bits, info->dirty_bits,
3c4af5
 			       100.0 * info->dirty_bits / (info->total_bits?:1));
3c4af5
-			 close(fd);
3c4af5
 		}
3c4af5
 	}
3c4af5
 
3c4af5
 free_info:
3c4af5
+	close(fd);
3c4af5
+	free(info);
3c4af5
+	return rv;
3c4af5
+}
3c4af5
+
3c4af5
+int IsBitmapDirty(char *filename)
3c4af5
+{
3c4af5
+	/*
3c4af5
+	 * Read the bitmap file
3c4af5
+	 * It will break reading bitmap action immediately when meeting any error.
3c4af5
+	 *
3c4af5
+	 * Return: 1(dirty), 0 (clean), -1(error)
3c4af5
+	 */
3c4af5
+
3c4af5
+	int fd = -1, rv = 0, i;
3c4af5
+	struct supertype *st = NULL;
3c4af5
+	bitmap_info_t *info = NULL;
3c4af5
+	bitmap_super_t *sb = NULL;
3c4af5
+
3c4af5
+	fd = bitmap_file_open(filename, &st, 0, fd);
3c4af5
+	free(st);
3c4af5
+	if (fd < 0)
3c4af5
+		goto out;
3c4af5
+
3c4af5
+	info = bitmap_fd_read(fd, 0);
3c4af5
+	if (!info) {
3c4af5
+		close(fd);
3c4af5
+		goto out;
3c4af5
+	}
3c4af5
+
3c4af5
+	sb = &info->sb;
3c4af5
+	for (i = 0; i < (int)sb->nodes; i++) {
3c4af5
+		st = NULL;
3c4af5
+		free(info);
3c4af5
+		info = NULL;
3c4af5
+
3c4af5
+		fd = bitmap_file_open(filename, &st, i, fd);
3c4af5
+		free(st);
3c4af5
+		if (fd < 0)
3c4af5
+			goto out;
3c4af5
+
3c4af5
+		info = bitmap_fd_read(fd, 0);
3c4af5
+		if (!info) {
3c4af5
+			close(fd);
3c4af5
+			goto out;
3c4af5
+		}
3c4af5
+
3c4af5
+		sb = &info->sb;
3c4af5
+		if (sb->magic != BITMAP_MAGIC) { /* invalid bitmap magic */
3c4af5
+			free(info);
3c4af5
+			close(fd);
3c4af5
+			goto out;
3c4af5
+		}
3c4af5
+
3c4af5
+		if (info->dirty_bits)
3c4af5
+			rv = 1;
3c4af5
+	}
3c4af5
+	close(fd);
3c4af5
 	free(info);
3c4af5
 	return rv;
3c4af5
+out:
3c4af5
+	return -1;
3c4af5
 }
3c4af5
 
3c4af5
 int CreateBitmap(char *filename, int force, char uuid[16],
3c4af5
diff --git a/mdadm.h b/mdadm.h
3c4af5
index 56b1b19..1ee6c92 100644
3c4af5
--- a/mdadm.h
3c4af5
+++ b/mdadm.h
3c4af5
@@ -1455,6 +1455,7 @@ extern int CreateBitmap(char *filename, int force, char uuid[16],
3c4af5
 			unsigned long long array_size,
3c4af5
 			int major);
3c4af5
 extern int ExamineBitmap(char *filename, int brief, struct supertype *st);
3c4af5
+extern int IsBitmapDirty(char *filename);
3c4af5
 extern int Write_rules(char *rule_name);
3c4af5
 extern int bitmap_update_uuid(int fd, int *uuid, int swap);
3c4af5
 
3c4af5
-- 
3c4af5
2.7.5
3c4af5