#!/bin/sh
# DF-2644 trigger v2 (fault-injection kernel A): metadata-only dirt so the
# FIRST error-path crash reached is the flush-time NULL deref at
# hammer2_flush.c:1336 (not the write-path KKASSERT at chain.c:3112 that
# fires when file-data assignment is still pending).
set -x
truncate -s 512M /root/h2644.img
vnconfig -c -S 512m -T vn2 /root/h2644.img
newfs_hammer2 -L DATA /dev/vn2 >/dev/null 2>&1
mkdir -p /mnt/h2644
mount_hammer2 /dev/vn2@DATA /mnt/h2644

# metadata-only dirt under the PFS root: mkdir + empty files
# (dirent + inode chains, ONFLUSH on iroot chain, no file data strategy)
mkdir /mnt/h2644/subdir
touch /mnt/h2644/subdir/e1 /mnt/h2644/subdir/e2 /mnt/h2644/subdir/e3
mkdir /mnt/h2644/subdir2
touch /mnt/h2644/subdir2/e4

# let all frontend xops complete (no pending strategy writes)
sleep 5

# simulate media failure of inode blocks (bounded count: enough for the
# stage-1 inode loads + the stage-2 iroot load, then self-disarms so the
# post-flush retry paths are not running against persistent failure --
# those hit the unrelated pre-existing chain.c:3112 KKASSERT)
sysctl vfs.hammer2.df2644_fail_inode_read=16

# trigger the stage-2 iroot flush explicitly
(sync) 2>/dev/null || /sbin/sync 2>/dev/null || /usr/sbin/sync 2>/dev/null || echo "no sync binary"
sleep 3
(sync) 2>/dev/null || true
sleep 2
echo "SURVIVED sync (unexpected on kernel A)"
