Blame SOURCES/0053-super1-report-truncated-device.patch

37f2b0
From 171e9743881edf2dfb163ddff483566fbf913ccd Mon Sep 17 00:00:00 2001
37f2b0
From: NeilBrown <neilb@suse.de>
37f2b0
Date: Fri, 26 Aug 2022 08:55:56 +1000
37f2b0
Subject: [PATCH 53/83] super1: report truncated device
37f2b0
37f2b0
When the metadata is at the start of the device, it is possible that it
37f2b0
describes a device large than the one it is actually stored on.  When
37f2b0
this happens, report it loudly in --examine.
37f2b0
37f2b0
....
37f2b0
   Unused Space : before=1968 sectors, after=-2047 sectors DEVICE TOO SMALL
37f2b0
          State : clean TRUNCATED DEVICE
37f2b0
....
37f2b0
37f2b0
Also report in --assemble so that the failure which the kernel will
37f2b0
report will be explained.
37f2b0
37f2b0
mdadm: Device /dev/sdb is not large enough for data described in superblock
37f2b0
mdadm: no RAID superblock on /dev/sdb
37f2b0
mdadm: /dev/sdb has no superblock - assembly aborted
37f2b0
37f2b0
Scenario can be demonstrated as follows:
37f2b0
37f2b0
mdadm: Note: this array has metadata at the start and
37f2b0
    may not be suitable as a boot device.  If you plan to
37f2b0
    store '/boot' on this device please ensure that
37f2b0
    your boot-loader understands md/v1.x metadata, or use
37f2b0
    --metadata=0.90
37f2b0
mdadm: Defaulting to version 1.2 metadata
37f2b0
mdadm: array /dev/md/test started.
37f2b0
mdadm: stopped /dev/md/test
37f2b0
   Unused Space : before=1968 sectors, after=-2047 sectors DEVICE TOO SMALL
37f2b0
          State : clean TRUNCATED DEVICE
37f2b0
   Unused Space : before=1968 sectors, after=-2047 sectors DEVICE TOO SMALL
37f2b0
          State : clean TRUNCATED DEVICE
37f2b0
37f2b0
Signed-off-by: NeilBrown <neilb@suse.de>
37f2b0
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
37f2b0
---
37f2b0
 super1.c | 35 ++++++++++++++++++++++++++++-------
37f2b0
 1 file changed, 28 insertions(+), 7 deletions(-)
37f2b0
37f2b0
diff --git a/super1.c b/super1.c
37f2b0
index 71af860c..58345e68 100644
37f2b0
--- a/super1.c
37f2b0
+++ b/super1.c
37f2b0
@@ -406,12 +406,18 @@ static void examine_super1(struct supertype *st, char *homehost)
37f2b0
 
37f2b0
 	st->ss->getinfo_super(st, &info, NULL);
37f2b0
 	if (info.space_after != 1 &&
37f2b0
-	    !(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
37f2b0
-		printf("   Unused Space : before=%llu sectors, after=%llu sectors\n",
37f2b0
-		       info.space_before, info.space_after);
37f2b0
-
37f2b0
-	printf("          State : %s\n",
37f2b0
-	       (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
37f2b0
+	    !(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) {
37f2b0
+		printf("   Unused Space : before=%llu sectors, ",
37f2b0
+		       info.space_before);
37f2b0
+		if (info.space_after < INT64_MAX)
37f2b0
+			printf("after=%llu sectors\n", info.space_after);
37f2b0
+		else
37f2b0
+			printf("after=-%llu sectors DEVICE TOO SMALL\n",
37f2b0
+			       UINT64_MAX - info.space_after);
37f2b0
+	}
37f2b0
+	printf("          State : %s%s\n",
37f2b0
+	       (__le64_to_cpu(sb->resync_offset)+1) ? "active":"clean",
37f2b0
+	       (info.space_after > INT64_MAX)       ? " TRUNCATED DEVICE" : "");
37f2b0
 	printf("    Device UUID : ");
37f2b0
 	for (i=0; i<16; i++) {
37f2b0
 		if ((i&3)==0 && i != 0)
37f2b0
@@ -2206,6 +2212,7 @@ static int load_super1(struct supertype *st, int fd, char *devname)
37f2b0
 		tst.ss = &super;;
37f2b0
 		for (tst.minor_version = 0; tst.minor_version <= 2;
37f2b0
 		     tst.minor_version++) {
37f2b0
+			tst.ignore_hw_compat = st->ignore_hw_compat;
37f2b0
 			switch(load_super1(&tst, fd, devname)) {
37f2b0
 			case 0: super = tst.sb;
37f2b0
 				if (bestvers == -1 ||
37f2b0
@@ -2312,7 +2319,6 @@ static int load_super1(struct supertype *st, int fd, char *devname)
37f2b0
 		free(super);
37f2b0
 		return 2;
37f2b0
 	}
37f2b0
-	st->sb = super;
37f2b0
 
37f2b0
 	bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
37f2b0
 
37f2b0
@@ -2322,6 +2328,21 @@ static int load_super1(struct supertype *st, int fd, char *devname)
37f2b0
 	if (st->data_offset == INVALID_SECTORS)
37f2b0
 		st->data_offset = __le64_to_cpu(super->data_offset);
37f2b0
 
37f2b0
+	if (st->minor_version >= 1 &&
37f2b0
+	    st->ignore_hw_compat == 0 &&
37f2b0
+	    (dsize < (__le64_to_cpu(super->data_offset) +
37f2b0
+		      __le64_to_cpu(super->size))
37f2b0
+	     ||
37f2b0
+	     dsize < (__le64_to_cpu(super->data_offset) +
37f2b0
+		      __le64_to_cpu(super->data_size)))) {
37f2b0
+		if (devname)
37f2b0
+			pr_err("Device %s is not large enough for data described in superblock\n",
37f2b0
+			       devname);
37f2b0
+		free(super);
37f2b0
+		return 2;
37f2b0
+	}
37f2b0
+	st->sb = super;
37f2b0
+
37f2b0
 	/* Now check on the bitmap superblock */
37f2b0
 	if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
37f2b0
 		return 0;
37f2b0
-- 
37f2b0
2.38.1
37f2b0