mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yingjie Wang <x-professor@outlook.com>
To: Alasdair Kergon <agk@redhat.com>,
	Mike Snitzer <snitzer@kernel.org>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Benjamin Marzinski <bmarzins@redhat.com>
Cc: "dm-devel@lists.linux.dev" <dm-devel@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Damien Le Moal <dlemoal@kernel.org>,
	Hannes Reinecke <hare@suse.de>,
	Yingjie Wang <x-professor@outlook.com>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: [PATCH v2 1/3] dm zoned: do not update metadata state from an invalid superblock
Date: Thu, 3 Dec 2026 09:02:50 +0000	[thread overview]
Message-ID: <20261203090248.1180909-1-x-professor@outlook.com> (raw)
In-Reply-To: <20261203090240.1180833-1-x-professor@outlook.com>

dmz_check_sb() stores the version, UUIDs and label of a superblock
before it has finished validating it.  If the superblock is then
rejected, zmd and the device are left with the values it contained.

If the secondary superblock is invalid, it is recovered from the primary
one using these values, and the next metadata flush writes them to the
primary superblock, too.  For example:

 - If the secondary superblock has a bad checksum and a version of 1,
   both superblocks become version 1, and the tertiary superblocks are
   not checked anymore.

 - If it has no device UUID, the device UUID is cleared in both
   superblocks.  Both are invalid then, and the table cannot be loaded
   anymore.

Conversely, if the primary superblock is rejected after its UUID and
label were stored, a valid secondary superblock is rejected, too, if
these are different, and the volume is not recovered.

Use local variables during validation and only update zmd and the
device once the superblock is known to be valid.

Fixes: bd5c40313a14 ("dm zoned: metadata version 2")
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
---
 drivers/md/dm-zoned-metadata.c | 38 ++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/drivers/md/dm-zoned-metadata.c b/drivers/md/dm-zoned-metadata.c
index 687573bbc091..a6b8372010d8 100644
--- a/drivers/md/dm-zoned-metadata.c
+++ b/drivers/md/dm-zoned-metadata.c
@@ -975,7 +975,7 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 	struct dmz_super *sb = dsb->sb;
 	struct dmz_dev *dev = dsb->dev;
 	unsigned int nr_meta_zones, nr_data_zones, i;
-	u32 crc, stored_crc;
+	u32 crc, stored_crc, version;
 	u64 gen, sb_block;
 
 	if (le32_to_cpu(sb->magic) != DMZ_MAGIC) {
@@ -984,13 +984,13 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 		return -ENXIO;
 	}
 
-	zmd->sb_version = le32_to_cpu(sb->version);
-	if (zmd->sb_version > DMZ_META_VER) {
+	version = le32_to_cpu(sb->version);
+	if (version > DMZ_META_VER) {
 		dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)",
-			    DMZ_META_VER, zmd->sb_version);
+			    DMZ_META_VER, version);
 		return -EINVAL;
 	}
-	if (zmd->sb_version < 2 && tertiary) {
+	if (version < 2 && tertiary) {
 		dmz_dev_err(dev, "Tertiary superblocks are not supported");
 		return -EINVAL;
 	}
@@ -1011,29 +1011,27 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 			    sb_block, (u64)dsb->zone->id << zmd->zone_nr_blocks_shift);
 		return -EINVAL;
 	}
-	if (zmd->sb_version > 1) {
-		uuid_t sb_uuid;
+	if (version > 1) {
+		uuid_t sb_uuid, dev_uuid;
 
 		import_uuid(&sb_uuid, sb->dmz_uuid);
 		if (uuid_is_null(&sb_uuid)) {
 			dmz_dev_err(dev, "NULL DM-Zoned uuid");
 			return -ENXIO;
-		} else if (uuid_is_null(&zmd->uuid)) {
-			uuid_copy(&zmd->uuid, &sb_uuid);
-		} else if (!uuid_equal(&zmd->uuid, &sb_uuid)) {
+		} else if (!uuid_is_null(&zmd->uuid) &&
+			   !uuid_equal(&zmd->uuid, &sb_uuid)) {
 			dmz_dev_err(dev, "mismatching DM-Zoned uuid, is %pUl expected %pUl",
 				    &sb_uuid, &zmd->uuid);
 			return -ENXIO;
 		}
-		if (!strlen(zmd->label))
-			memcpy(zmd->label, sb->dmz_label, BDEVNAME_SIZE);
-		else if (memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) {
+		if (strlen(zmd->label) &&
+		    memcmp(zmd->label, sb->dmz_label, BDEVNAME_SIZE)) {
 			dmz_dev_err(dev, "mismatching DM-Zoned label, is %s expected %s",
 				    sb->dmz_label, zmd->label);
 			return -ENXIO;
 		}
-		import_uuid(&dev->uuid, sb->dev_uuid);
-		if (uuid_is_null(&dev->uuid)) {
+		import_uuid(&dev_uuid, sb->dev_uuid);
+		if (uuid_is_null(&dev_uuid)) {
 			dmz_dev_err(dev, "NULL device uuid");
 			return -ENXIO;
 		}
@@ -1046,7 +1044,7 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 			if (gen != 0)
 				dmz_dev_warn(dev, "Invalid generation %llu",
 					    gen);
-			return 0;
+			goto valid;
 		}
 	}
 
@@ -1103,6 +1101,14 @@ static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_sb *dsb,
 	zmd->nr_meta_zones = nr_meta_zones;
 	zmd->nr_data_zones = nr_data_zones;
 
+valid:
+	zmd->sb_version = version;
+	if (version > 1) {
+		import_uuid(&zmd->uuid, sb->dmz_uuid);
+		memcpy(zmd->label, sb->dmz_label, BDEVNAME_SIZE);
+		import_uuid(&dev->uuid, sb->dev_uuid);
+	}
+
 	return 0;
 }
 
-- 
2.43.0


  reply	other threads:[~2026-12-03  9:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  9:02 [PATCH v2 0/3] dm zoned: fix the validation of the superblocks Yingjie Wang
2026-12-03  9:02 ` Yingjie Wang [this message]
2026-12-03  9:02 ` [PATCH v2 2/3] dm zoned: validate the metadata layout in the superblock Yingjie Wang
2026-12-03  9:03 ` [PATCH v2 3/3] dm zoned: handle labels that are not NUL-terminated Yingjie Wang
2026-12-04  7:42   ` Damien Le Moal
2026-12-05  6:21     ` Yingjie Wang
2026-12-05 23:35       ` Damien Le Moal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203090248.1180909-1-x-professor@outlook.com \
    --to=x-professor@outlook.com \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dlemoal@kernel.org \
    --cc=dm-devel@lists.linux.dev \
    --cc=hare@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=snitzer@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®