[PATCH 3/3] dm zoned: fix the search for the secondary superblock

From: Yingjie Wang

Date: Mon Nov 30 2026 - 11:17:27 EST


If the primary superblock of a volume formatted with dmzadm is
corrupted, dmz_lookup_secondary_sb() fails to find the valid secondary
one in two cases:

- After the first iteration, sb[1].block is advanced to the next zone,
but sb[1].zone is set to the zone that was just examined. Unless
the superblock is in the first zone that is searched, dmz_check_sb()
then rejects it because it does not appear to be in the right zone.
With 256 MiB zones, this affects volumes larger than about 8.8 TB,
which need more than one zone per metadata set:

Invalid superblock position (is 131072 expected 65536)

- The number of random zones is used as the limit. With multiple
devices, the metadata is on the cache device instead, and nothing is
searched at all if the zoned devices have no random zones. Commit
b38c0ad57f8e ("dm zoned: Fix metadata zone size check") fixed the
same problem in dmz_check_sb().

Set the block and the zone together at the start of each iteration, and
use the number of cache zones if there are multiple devices.

This depends on the previous patch, which rejects superblocks that must
not be used for recovery.

Fixes: a92fbc446d1a ("dm zoned: convert to xarray")
Fixes: 34f5affd04c4 ("dm zoned: separate random and cache zones")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yingjie Wang <x-professor@xxxxxxxxxxx>
---
drivers/md/dm-zoned-metadata.c | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/md/dm-zoned-metadata.c b/drivers/md/dm-zoned-metadata.c
index 91cc8d28e524..687573bbc091 100644
--- a/drivers/md/dm-zoned-metadata.c
+++ b/drivers/md/dm-zoned-metadata.c
@@ -1125,10 +1125,11 @@ static int dmz_read_sb(struct dmz_metadata *zmd, struct dmz_sb *sb, int set)
*/
static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd)
{
- unsigned int zone_nr_blocks = zmd->zone_nr_blocks;
+ unsigned int nr_zones = zmd->nr_devs > 1 ? zmd->nr_cache_zones :
+ zmd->nr_rnd_zones;
struct dmz_mblock *mblk;
unsigned int zone_id = zmd->sb[0].zone->id;
- int i;
+ unsigned int i;

/* Allocate a block */
mblk = dmz_alloc_mblock(zmd, 0);
@@ -1139,16 +1140,14 @@ static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd)
zmd->sb[1].sb = mblk->data;

/* Bad first super block: search for the second one */
- zmd->sb[1].block = zmd->sb[0].block + zone_nr_blocks;
- zmd->sb[1].zone = dmz_get(zmd, zone_id + 1);
zmd->sb[1].dev = zmd->sb[0].dev;
- for (i = 1; i < zmd->nr_rnd_zones; i++) {
+ for (i = 1; i < nr_zones; i++) {
+ zmd->sb[1].block = zmd->sb[0].block + i * zmd->zone_nr_blocks;
+ zmd->sb[1].zone = dmz_get(zmd, zone_id + i);
if (dmz_read_sb(zmd, &zmd->sb[1], 1) != 0)
break;
if (le32_to_cpu(zmd->sb[1].sb->magic) == DMZ_MAGIC)
return 0;
- zmd->sb[1].block += zone_nr_blocks;
- zmd->sb[1].zone = dmz_get(zmd, zone_id + i);
}

dmz_free_mblock(zmd, mblk);