#19189·zfs

zroot would not import after a clean shutdown

Author: tatuzemdpCreated Sep 25, 2026Updated Sep 25, 2026
LabelsType: Defect

System information

Type Version/Name
Distribution Name FreeBSD
Distribution Version 15.1p3
Kernel Version 15.1p3
Architecture AMD64
OpenZFS Version 2.4.2

Describe the problem you're observing

Report and workaround courtesy of Opus5.5

What happened.

There was no crash or power loss. The pool was nearly full, largely because of 1,015 unpruned syncoid snapshots. In its last transaction groups before the shutdown, ZFS could not find contiguous space and stored part of the pool's top-level metadata (the MOS dnode array index block) as a gang block, using the new 4 KB dynamic gang header format.

Why import fails During import, ZFS reads that block before it has loaded the pool's feature refcounts. It therefore assumes the old 512-byte gang header, the header's embedded checksum fails, and import stops with couldn't get 'config' value in MOS directory [error=5]. Every rewind (-F, -FX, -T) fails the same way, because all 32 retained uberblocks point at ganged metadata.

What we know works. A userspace zdb built with a small patch opens the pool and reads everything: 16 datasets, about 1,060 snapshots, all 5,120 MOS dnode blocks. zdb -B streams datasets straight into zfs receive, which is how the data is being recovered.

Describe how to reproduce the problem

N/A

Environment

  • Host : FreeBSD 15.1-RELEASE-p3 amd64, OpenZFS zfs-2.4.2-0-g6330a45b0
  • Damaged pool: zroot, GUID 17122747932025035133, single vdev nvd0p4 (freebsd-zfs, 464 GB), ashift 12
  • Recovery host: Same machine, reinstalled on a second NVMe (nvd1). Its new root pool is also named zroot, so the damaged pool must be addressed by GUID.
  • Last txg: 1645006, written 22 Sep 2026 22:05:45 UTC (00:05:45 CEST)
  • Relevant feature: com.klarasystems:dynamic_gang_header (listed in the labels' features_for_read)
  • Datasets: 16, including ROOT/default 47.8 GB, ROOT/stable 9.4 GB, home/XXXX 227.8 GB, usr/ports, usr/src, var/*
  • Snapshot: About 1,060 on home/XXXX: 1,015 syncoid_XXXX_* every 30 minutes, 48 autosnap_*
# zpool import -o readonly=on -N -R /mnt -f 17122747932025035133 rescue
cannot import 'zroot' as 'rescue': I/O error
        Destroy and re-create the pool from
        a backup source.

The ZFS debug log (sysctl kstat.zfs.misc.dbgmsg) shows the device opening fine and a valid uberblock being selected, then:

spa_load(rescue, config untrusted): using uberblock with txg=1645006
spa_load(rescue, config untrusted): FAILED: couldn't get 'config' value in MOS directory [error=5]
spa_load(rescue, config untrusted): spa_load_retry: rewind, max txg: 1645005
...
spa_load(rescue, config untrusted): spa_load_retry: rewind, max txg: 1644974
spa_load(rescue, config untrusted): FAILED: no valid uberblock found

Labels and uberblocks: good

zdb -l /dev/nvd0p4 showed all four labels intact. zdb -ul showed an unbroken ring of 32 uberblocks, one every 5 seconds, txg 1644975 to 1645006. That is only about 2.5 minutes of history, so there is nothing older to rewind to.

An independent on-disk reader

To see the on-disk state without relying on ZFS's own code, a read-only Python tool (zfsrescue.py) was written. It parses the labels, uberblocks, block pointers, dnodes and ZAPs directly, verifies every fletcher4 and SHA-256 checksum, and supports both gang header formats. Before use it was tested against real OpenZFS pools built with ztest, including one with every copy of the object directory destroyed on purpose.

Run against the damaged pool it found:

  • The MOS object set is fine. All three copies pass their checksums in the newest txgs.
  • The block that ZFS cannot read is a gang block. The path from the MOS to the object directory runs through a gang block that uses a 4 KB dynamic gang header.
  • With gang support the object directory reads perfectly: 21 entries, including config = 61 and root_dataset = 32.
  • All 5,120 MOS dnode blocks read, and all 16 datasets and every snapshot object set are readable.

So the data on disk is consistent. The problem is in how ZFS reads it during import.

Root cause

A gang block is a block that ZFS had to split into smaller pieces because it could not find contiguous free space. Its block pointer leads to a gang header: a small array of pointers to the pieces, with an embedded checksum at the end. Classic gang headers are 512 bytes. With dynamic_gang_header, the header is one minimum allocation unit, 4 KB on an ashift-12 vdev.

The reader must know which size to use. In OpenZFS 2.4.2 both places that decide consult the in-memory feature refcounts:

/* module/zfs/zio.c, zio_gang_tree_assemble() */
if (spa_feature_is_active(gio->io_spa, SPA_FEATURE_DYNAMIC_GANG_HEADER)) {
        ... gangblocksize = vdev_gang_header_psize(vd);   /* 4096 on ashift 12 */
} else {
        gangblocksize = SPA_OLD_GANGBLOCKSIZE;             /* 512 */
}

/* module/zfs/zio_checksum.c, zio_checksum_error() */
if (spa_feature_is_active(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER))
        size = zio->io_size;
else
        size = SPA_OLD_GANGBLOCKSIZE;

The fix (workaround)

While the refcounts are unknown, decide from the label's feature list, and let the checksum code trust the header size chosen at assembly time. Old-style 512-byte headers are still handled: zio_checksum_error() already falls back to 512 bytes when the larger size fails.

diff -ru a/sys/contrib/openzfs/module/zfs/zio.c b/sys/contrib/openzfs/module/zfs/zio.c
--- a/sys/contrib/openzfs/module/zfs/zio.c
+++ b/sys/contrib/openzfs/module/zfs/zio.c
@@ -3280,12 +3280,38 @@
         zio_gang_node_free(gnpp);
 }
 
+/*
+ * Are gang headers on this pool possibly larger than SPA_OLD_GANGBLOCKSIZE?
+ *
+ * Feature refcounts are only loaded by spa_ld_check_features(), which runs
+ * after the MOS object directory and config have been read. Until then
+ * spa_feature_is_active() reports every feature as inactive, so a ganged
+ * MOS block written with a dynamic gang header would be read as a 512-byte
+ * header, fail its embedded checksum, and make the pool unimportable
+ * ("couldn't get 'config' value in MOS directory [error=5]").
+ *
+ * dynamic_gang_header is a ZFEATURE_FLAG_MOS feature, so when it is active
+ * it is listed in the vdev labels' features_for_read, which are loaded
+ * before any MOS block is read. Use that while the refcounts are unknown.
+ */
+static boolean_t
+zio_gang_header_is_dynamic(spa_t *spa)
+{
+        zfeature_info_t *zfi =
+            &spa_feature_table[SPA_FEATURE_DYNAMIC_GANG_HEADER];
+        uint64_t refcount;
+
+        if (feature_get_refcount(spa, zfi, &refcount) == 0)
+                return (refcount > 0);
+        return (spa->spa_label_features != NULL &&
+            nvlist_exists(spa->spa_label_features, zfi->fi_guid));
+}
+
 static void
 zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
 {
         uint64_t gangblocksize = UINT64_MAX;
-        if (spa_feature_is_active(gio->io_spa,
-            SPA_FEATURE_DYNAMIC_GANG_HEADER)) {
+        if (zio_gang_header_is_dynamic(gio->io_spa)) {
                 spa_config_enter(gio->io_spa, SCL_VDEV, FTAG, RW_READER);
                 for (int dva = 0; dva < BP_GET_NDVAS(bp); dva++) {
                         vdev_t *vd = vdev_lookup_top(gio->io_spa,
Only in b/sys/contrib/openzfs/module/zfs: zio.c.orig
diff -ru a/sys/contrib/openzfs/module/zfs/zio_checksum.c b/sys/contrib/openzfs/module/zfs/zio_checksum.c
--- a/sys/contrib/openzfs/module/zfs/zio_checksum.c
+++ b/sys/contrib/openzfs/module/zfs/zio_checksum.c
@@ -541,10 +541,13 @@
         spa_t *spa = zio->io_spa;
 
         if (bp && BP_IS_GANG(bp)) {
-                if (spa_feature_is_active(spa, SPA_FEATURE_DYNAMIC_GANG_HEADER))
-                        size = zio->io_size;
-                else
-                        size = SPA_OLD_GANGBLOCKSIZE;
+                /*
+                 * zio_gang_tree_assemble() sized this read as the gang header
+                 * size it expects (SPA_OLD_GANGBLOCKSIZE unless dynamic gang
+                 * headers may be in use), so trust io_size; an old-style header
+                 * is still caught by the fallback below.
+                 */
+                size = zio->io_size;
         }
 
         error = zio_checksum_error_impl(spa, bp, checksum, data, size,

Open issue: the kernel still fails

With the patched module loaded, the import still fails. The ZFS events (zpool events -v) now show where:

class      = "ereport.fs.zfs.data"
zio_err    = 0x61                     # 97 = EINTEGRITY (checksum)
zio_flags  = 0x1000080 [CANFAIL RAW_COMPRESS]
zio_objset = 0x0   zio_object = 0x0   zio_level = 0x1   zio_blkid = 0x0

Recovering the data

Because patched zdb can open the pool, it can also export datasets. zdb -B writes a full zfs send stream of an object set without importing the pool, which is the right tool here. Object set IDs come from zdb -d; home/XXXX is 315 and ROOT/default is 387.

bash
cd /usr/src
patch -p1 < zfs-dynamic-gang-import.patch
make -C cddl/lib/libzpool -j8 all
make -C cddl/usr.sbin/zdb -j8 all
LIBZPOOL=$(make -C cddl/lib/libzpool -V .OBJDIR)
ZDBBIN=$(make -C cddl/usr.sbin/zdb -V .OBJDIR)/zdb



env LD_LIBRARY_PATH=$LIBZPOOL $ZDBBIN -U /tmp/zdb.cache -e -p /dev/nvd0p4 -d 17122747932025035133

zfs create -o mountpoint=none zroot/rescue
env LD_LIBRARY_PATH=$LIBZPOOL $ZDBBIN -U /tmp/zdb.cache -e -p /dev/nvd0p4 \
    -B 17122747932025035133/315 eLc | zfs receive -u zroot/rescue/XXXX
zfs set mountpoint=/mnt/rescue/home zroot/rescue/XXXX
zfs mount zroot/rescue/XXXX