1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2023 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# fsck -B test scenario with SUJ. -B implies "preen mode".
31
32# 'panic: ffs_blkfree_cg: bad size' seen:
33# https://people.freebsd.org/~pho/stress/log/log0465.txt
34# Fixed by: 220427da0e9b - Set UFS/FFS file type to snapshot before changing
35# its block pointers.
36
37[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
38. ../default.cfg
39mycc -o /tmp/flip -Wall -Wextra ../tools/flip.c || exit 2
40
41set -e
42md=/dev/md$mdstart
43prog=$(basename "$0" .sh)
44backup=/tmp/$prog.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
45log=/tmp/$prog.sh.log
46[ -c $md ] && mdconfig -d -u $mdstart
47dd if=/dev/zero of=$diskimage bs=128m count=1 status=none
48mdconfig -a -t vnode -f $diskimage -u $mdstart
49backups=`newfs -N $j $md | grep -A1 "super-block backups" | \
50    tail -1 | sed 's/,//g'`
51newfs -j $md > /dev/null 2>&1
52mount $md $mntpoint
53set +e
54
55jot 5000 | xargs -P0 -I% touch $mntpoint/a%
56while ! umount $mntpoint; do :; done
57/tmp/flip -n 10 $diskimage
58gzip < $diskimage > $backup
59fsync $diskimage $backup
60
61mount -f $md $mntpoint
62
63if ! fsck_ffs -B $md > $log 2>&1; then
64	grep MANUALLY $log
65	umount $mntpoint
66	fsck_ffs -fy $md > $log 2>&1; s=$?
67	grep -Eq "IS CLEAN|MARKED CLEAN" $log || {
68		cat $log
69		echo "fsck_ffs -f failed with exit code $s"
70		umount $mntpoint; mdconfig -d -u $mdstart
71		rm -f $log /tmp/flip $diskimage $backup
72		exit 1
73	}
74	mount $md $mntpoint
75fi
76
77jot 5000 | xargs -P0 -I% rm $mntpoint/a%
78jot 5000 | xargs -P0 -I% touch $mntpoint/b%
79
80ls -lR $mntpoint > /dev/null || {
81    echo "ls -lR $mntpoint failed after fsck -B"
82    umount $mntpoint; mdconfig -d -u $mdstart
83    rm -f $log /tmp/flip $diskimage $backup
84    exit 0 # For now, ignore non fatal errors
85}
86
87jot 5000 | xargs -P0 -I% rm $mntpoint/b% || {
88    echo "clean failed"
89    umount $mntpoint; mdconfig -d -u $mdstart
90    rm -f $log /tmp/flip $diskimage $backup
91    exit 0 # For now, ignore non fatal errors
92}
93umount $mntpoint
94
95r=0
96for i in `jot 4`; do
97	fsck_ffs -fy $diskimage > $log 2>&1; r=$?
98	if grep -qiE "super-?block.*failed" $log; then
99		for b in $backups; do
100			echo "fsck_ffs -b $b -fy $diskimage"
101			fsck_ffs -b $b -fy $diskimage > $log 2>&1
102			r=$?
103			grep -qiE "super-?block.*failed" $log ||
104			   break
105			echo "Checking next SB"
106		done
107	fi
108	[ $r -ne 0 ] && continue
109	grep -Eq "WAS MODIFIED" $log && continue
110	grep -Eq "CLEAN" $log && break
111done
112mount $md $mntpoint || exit 3
113ls -lR $mntpoint > /dev/null || { umount $mntpoint; mdconfig -d -u mdstart; echo "exit 4"; exit 4; }
114umount $mntpoint
115fsck_ffs -fy $md > $log 2>&1
116grep -Eq 'IS CLEAN|MARKED CLEAN' $log && s=0 || { s=1; cat $log; }
117mdconfig -d -u $mdstart
118rm -f $log /tmp/flip $diskimage $backup
119exit $s
120