#!/bin/sh
#
# runfix_test.sh - DF-2643 fix validation on the HARNESS+FIX kernel (K3 =
# stock + harness.diff + fix.diff).  Same forced-NULL state as K2.
#
# EXPECTED with the guard: no panic; every strategy write whose parent
# chain came back NULL is failed with EIO (biodone B_ERROR) -- visible as
# repeated "xop_strategy_write: error 1 loff=..." on the console (the
# buffer-flusher retries the still-dirty buffer; the harness makes EVERY
# attempt fail, a real race fails once).  Guest stays alive and responsive.
#
# A: knob OFF -> normal writes succeed (no regression)
# B: knob ON  -> per-attempt EIO, ZERO Fatal traps, guest alive
# C: knob OFF again -> writes succeed again
truncate -s 512M /root/w2.img
newfs_hammer2 /root/w2.img >/dev/null 2>&1
vn=$(vnconfig -c vn /root/w2.img)
mount_hammer2 /dev/$vn@LOCAL /mnt

echo "=== A: knob OFF (normal write)"
dd if=/dev/zero of=/mnt/tfile bs=64k count=2 2>&1 | tail -1
sync || true

echo "=== B: knob ON (forced NULL parent; K2 panicked here)"
sysctl vfs.hammer2.df2643_harness=1
dd if=/dev/zero of=/mnt/tfile bs=64k count=2 2>&1 | tail -1 || true
timeout 10 sync || echo "(sync returns/retries; harness makes every retry fail)"
dmesg | grep -c "xop_strategy_write: error 1" || true
dmesg | grep -c "Fatal trap" || true
sysctl vfs.hammer2.df2643_harness=0

echo "=== C: knob OFF again"
dd if=/dev/zero of=/mnt/tfile bs=64k count=1 2>&1 | tail -1
echo "DONE; guest alive: $(uptime)"
