xref: /freebsd/tools/test/stress2/misc/fsck11.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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
74set +e
75
76chk() {
77	local i
78
79	clean=0
80	rerun=0
81	fsck_ffs -fy $1 > $log 2>&1
82	r=$?
83	if grep -qiE "super-?block.*failed" $log; then
84		for b in $backups; do
85			echo "fsck_ffs -b $b -fy $1"
86			fsck_ffs -b $b -fy $1 > $log 2>&1
87			r=$?
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
106start=`date +%s`
107while [ $((`date +%s` - start)) -lt 300 ]; do
108	mount /dev/md$u2 $mp2 || break
109	if ! ls -lR $mp2 > /dev/null; then
110		s=102
111		echo "ls failed"; grep "core dumped" /var/log/messages | tail -1
112		break
113	fi
114
115	find $mp2 -type f | xargs cat > /dev/null
116	for j in `jot 9`; do
117		rm -rf $mp2/$j
118		mkdir $mp2/$j
119		jot 10 | xargs -P0 -I% cp /etc/group $mp2/$j/%
120	done 2>/dev/null
121
122	while mount | grep -q "on $mp2 "; do umount $mp2; done
123	echo * | grep -q core && break
124	mdconfig -d -u $u2
125
126	# Introduce 5 random single bit errors to the file system image
127	/tmp/flip -n 5 $diskimage
128
129	if [ `stat -f%z $diskimage` -gt $max ]; then
130		ls -lh $diskimage
131		truncate -s $max $diskimage
132	else
133		gzip < $diskimage > $backup
134	fi
135	fsync $backup
136	sync; sleep 1
137
138	[ "$newfs_flags" = "-j" ] &&
139		fsck -fy $diskimage > $log 2>&1	# process the journal file
140	for i in `jot 5`; do
141		[ $i -gt 2 ] && echo "fsck run #$i"
142		chk $diskimage
143		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
144		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
145		if [ -f fsck_ffs.core ]; then
146			tstamp=`date +%Y%m%dT%H%M%S`
147			gzip < $backup > /tmp/fsck_ffs.core.diskimage.$tstamp
148			gzip < fsck_ffs.core > /tmp/fsck_ffs.core.$tstamp
149			break 2
150		fi
151	done
152	[ $clean -ne 1 ] && { s=99; break; }	# broken image?
153	mdconfig -a -t vnode -f $diskimage -u $u2
154done
155for i in `jot 5`; do
156	mount | grep -q "on $mp2 " || break
157	umount $mp2 && break
158	sleep 2
159done
160mdconfig -l | grep -q $u2 && mdconfig -d -u $u2
161
162[ $s -eq 0 ] && rm -f $backup || echo "Preserved $backup due to status code $s"
163cd /tmp
164for i in `jot 5`; do
165	umount $mp1 && break
166	sleep 2
167done
168mdconfig -d -u $u1
169rm -f /tmp/flip
170exit $s
171