xref: /freebsd/tools/test/stress2/misc/fsck12.sh (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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# "panic: ffs_copyonwrite: bad copy block" seen:
31# Hunt for fsck_ffs's INTERNAL ERROR message
32
33# https://people.freebsd.org/~pho/stress/log/log0426.txt
34
35. ../default.cfg
36
37[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
38
39cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1
40
41set -e
42prog=$(basename "$0" .sh)
43u1=$mdstart
44u2=$((mdstart + 1))
45mp1=${mntpoint}$u1
46mp2=${mntpoint}$u2
47mkdir -p $mp1 $mp2
48log=$mp1/$prog.sh.log
49diskimage=$mp1/$prog.sh.diskimage
50backup=/tmp/$prog.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
51cleans=0
52reruns=0
53
54mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
55[ -c /dev/md$u1 ] && mdconfig -d -u $u1
56mdconfig -a -t swap -s 1g -u $u1
57newfs $newfs_flags -n /dev/md$u1 > /dev/null
58mount /dev/md$u1 $mp1
59
60newfs_flags='-j'
61[ $# -eq 1 ] && newfs_flags="$1" # or use script argument
62max=$((2 * 1024 * 1024))
63[ "$newfs_flags" = "-j" ] && max=$((20 * 1024 * 1024)) # Make room for the journal file
64
65[ -c /dev/md$u2 ] && mdconfig -d -u $u2
66dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
67mdconfig -a -t vnode -f $diskimage -u $u2
68backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
69    tail -1 | sed 's/,//g'`
70echo "newfs $newfs_flags md$u2"
71newfs $newfs_flags md$u2 > /dev/null
72set +e
73
74chk() {
75	local i
76
77	clean=0
78	rerun=0
79	fsck_ffs -fy $1 > $log 2>&1
80	r=$?
81	grep "GOT TO reply" $log && exit 1
82	if grep -qiE "super-?block.*failed" $log; then
83		for b in $backups; do
84			echo "fsck_ffs -b $b -fy $1"
85			fsck_ffs -b $b -fy $1 > $log 2>&1
86			r=$?
87			grep "GOT TO reply" $log && exit 1
88			grep -qiE "super-?block.*failed" $log ||
89			   break
90			echo "Checking next SB"
91		done
92		usedasb=1
93	else
94		usedasb=0
95	fi
96	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
97	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
98	grep -q RERUN $log && rerun=1
99	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
100}
101
102cd $mp1
103clean=0
104errors=0
105s=0
106sno=0
107start=`date +%s`
108while [ $((`date +%s` - start)) -lt 300 ]; do
109	mount /dev/md$u2 $mp2 || break
110	if ! ls -lR $mp2 > /dev/null; then
111		s=102
112		echo "ls failed"; grep "core dumped" /var/log/messages | tail -1
113		break
114	fi
115	mkdir -p $mp2/d/d/d/d/d
116	cp /etc/group $mp2/d/d/d/d/d/f
117	for i  in `jot 20`; do
118		cp -r $mp2/d $i
119	done
120
121	rm -f $mp2/.snap/$sno
122	mksnap_ffs $mp2 $mp2/.snap/$sno &
123	sno=$(((sno + 1) % 10))
124
125	sleep 1
126	rm -rf $mp/?
127	wait
128	while mount | grep -q "on $mp2 "; do umount $mp2; done
129	echo * | grep -q core && break
130	mdconfig -d -u $u2
131
132	# Introduce 5 random single bit errors to the file system image
133	/tmp/flip -n 5 $diskimage
134
135	if [ `stat -f%z $diskimage` -gt $max ]; then
136		ls -lh $diskimage
137		truncate -s $max $diskimage
138	else
139		gzip < $diskimage > $backup
140	fi
141	fsync $backup
142	sync; sleep 1
143
144	[ $newfs_flags = "-j" ] &&
145		fsck -fy $diskimage > $log 2>&1	# process the journal file
146	for i in `jot 5`; do
147		[ $i -gt 2 ] && echo "fsck run #$i"
148		chk $diskimage
149		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
150		grep -q "MODIFIED" $log && continue # For now, do not trust CLEAN
151		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
152		if [ -f fsck_ffs.core ]; then
153			s=1
154			break 2
155		fi
156	done
157	[ $clean -ne 1 ] && { s=99; break; }	# broken image?
158	mdconfig -a -t vnode -f $diskimage -u $u2
159done
160for i in `jot 5`; do
161	mount | grep -q "on $mp2 " || break
162	umount $mp2 && break
163	sleep 2
164done
165mdconfig -l | grep -q $u2 && mdconfig -d -u $u2
166
167[ $s -eq 0 ] && rm -f $backup || echo "Preserved $backup due to status code $s"
168cd /tmp
169for i in `jot 5`; do
170	umount $mp1 && break
171	sleep 2
172done
173mdconfig -d -u $u1
174rm -f /tmp/flip
175exit $s
176