xref: /freebsd/tools/test/stress2/misc/fsck10.sh (revision 2e3f49888ec8851bafb22011533217487764fdb0)
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# Variation of fsck9.sh with smaller disk size and newfs options '', 'U' and 'O1'
31# fsck_ffs core dump seen
32
33. ../default.cfg
34
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36
37cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1
38
39set -e
40prog=$(basename "$0" .sh)
41u1=$mdstart
42u2=$((mdstart + 1))
43mp1=${mntpoint}$u1
44mp2=${mntpoint}$u2
45mkdir -p $mp1 $mp2
46log=$mp1/$prog.sh.log
47diskimage=$mp1/$prog.sh.diskimage
48backup=/tmp/$prog.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
49cleans=0
50reruns=0
51
52# Pick a random newfs flag
53newfs_flags=$(echo "" "-U" "-O1" | awk -v N=`jot -r 1 1 3` '{print $N}')
54[ $# -eq 1 ] && newfs_flags="$1" # or use script argument
55max=$((512 * 1024))
56
57mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
58[ -c /dev/md$u1 ] && mdconfig -d -u $u1
59mdconfig -a -t swap -s 1g -u $u1
60newfs $newfs_flags -n /dev/md$u1 > /dev/null
61mount /dev/md$u1 $mp1
62
63[ -c /dev/md$u2 ] && mdconfig -d -u $u2
64dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
65mdconfig -a -t vnode -f $diskimage -u $u2
66backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
67    tail -1 | sed 's/,//g'`
68echo "newfs $newfs_flags -n md$u2"
69newfs $newfs_flags -n md$u2 > /dev/null
70set +e
71
72chk() {
73	local i
74
75	clean=0
76	rerun=0
77	fsck_ffs -fy $1 > $log 2>&1
78	r=$?
79	if grep -qiE "super-?block.*failed" $log; then
80		for b in $backups; do
81			echo "fsck_ffs -b $b -fy $1"
82			fsck_ffs -b $b -fy $1 > $log 2>&1
83			r=$?
84			grep -qiE "super-?block.*failed" $log ||
85			   break
86			echo "Checking next SB"
87		done
88		usedasb=1
89	else
90		usedasb=0
91	fi
92	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
93	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
94	grep -q RERUN $log && rerun=1
95	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
96}
97
98cd $mp1
99clean=0
100errors=0
101s=0
102start=`date +%s`
103while [ $((`date +%s` - start)) -lt 300 ]; do
104	mount /dev/md$u2 $mp2 || break
105	if ! ls -lR $mp2 > /dev/null; then
106		s=102
107		echo "ls failed"; grep "core dumped" /var/log/messages | tail -1
108		break
109	fi
110	rm -f $mp2/????????
111	touch $mp2/`jot -rc 8 a z | tr -d '\n'`
112	mkdir -p $mp2/dir/dir/dir/dir
113	echo "abc" > $mp2/dir/dir/dir/dir/f
114	while mount | grep -q "on $mp2 "; do umount $mp2; done
115	echo * | grep -q core && break
116	mdconfig -d -u $u2
117
118	# Introduce 5 random single bit errors to the file system image
119	/tmp/flip -n 5 $diskimage
120
121	sync; sleep .1
122	if [ `stat -f%z $diskimage` -gt $max ]; then
123		ls -lh $diskimage
124		truncate -s $max $diskimage
125	else
126		gzip < $diskimage > $backup
127	fi
128	fsync $backup
129	sync; sleep .1
130
131	for i in `jot 5`; do
132		[ $i -gt 2 ] && echo "fsck run #$i"
133		chk $diskimage
134		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
135		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
136		if [ -f fsck_ffs.core ]; then
137			tstamp=`date +%Y%m%dT%H%M%S`
138			gzip < $backup > /tmp/fsck_ffs.core.diskimage.$tstamp
139			gzip < fsck_ffs.core > /tmp/fsck_ffs.core.$tstamp
140			break 2
141		fi
142	done
143	if [ $clean -eq 1 ]; then
144		fsck_ffs -fy $diskimage > $log 2>&1
145		if grep -q MODIFIED $log; then
146			echo "*** fsck of \"clean\" FS found more issues:"
147			cat $log
148			errors=$((errors + 1))
149			s=1
150			break
151		fi
152	fi
153	[ $clean -ne 1 ] && break
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