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