xref: /freebsd/tools/test/stress2/misc/fsck8.sh (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2022 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_ffs(8) disk image fuzz test. SB focus.
31
32# panic: wrong length 4098 for sectorsize 512
33# FreeBSD 14.0-CURRENT #0 main-n255602-51adf913e8815: Fri May 13 07:55:32 CEST 2022
34# pho@mercat1.netperf.freebsd.org:/usr/src/sys/amd64/compile/PHO
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
43u1=$mdstart
44u2=$((mdstart + 1))
45mp1=${mntpoint}$u1
46mp2=${mntpoint}$u2
47mkdir -p $mp1 $mp2
48log=$mp1/fsck8.sh.log
49diskimage=$mp1/fsck8.sh.diskimage
50backup=/tmp/fsck8.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
51asbs=0
52cleans=0
53reruns=0
54waccess=0
55
56max=$((10 * 1024 * 1024))
57# UFS1 or UFS2 SU:
58[ `jot -r 1 0 1` -eq 0 ] && newfs_flags='-O 1' || newfs_flags='-O 2 -U'
59
60mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
61[ -c /dev/md$u1 ] && mdconfig -d -u $u1
62mdconfig -a -t swap -s 1g -u $u1
63newfs $newfs_flags -n /dev/md$u1 > /dev/null
64mount /dev/md$u1 $mp1
65
66[ -c /dev/md$u2 ] && mdconfig -d -u $u2
67dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
68mdconfig -a -t vnode -f $diskimage -u $u2
69backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
70    tail -1 | sed 's/,//g'`
71newfs $newfs_flags -n md$u2 > /dev/null
72mount /dev/md$u2 $mp2
73[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
74umount $mp2
75set +e
76
77chk() {
78	local i
79
80	clean=0
81	rerun=0
82	waccess=0
83	timeout 5m fsck_ffs -fy $1 > $log 2>&1
84	r=$?
85	if grep -qiE "super-?block.*failed" $log; then
86		for b in $backups; do
87			echo "Using alternate SB $b"
88			asbs=$((asbs + 1))
89			fsck_ffs -b $b -fy $1 > $log 2>&1
90			r=$?
91			grep -qiE "super-?block.*failed" $log ||
92			   break
93		done
94		usedasb=1
95	else
96		usedasb=0
97	fi
98	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
99	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
100	# For now regard a "was modified" as a cause for a rerun,
101	# disregarding the "clean" claim.
102	grep -Eq "WAS MODIFIED" $log && rerun=1
103	grep -q RERUN $log && rerun=1
104	grep -q "NO WRITE ACCESS" $log && waccess=1
105	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
106}
107
108cd $mp1
109clean=0
110s=0
111start=`date +%s`
112while [ $((`date +%s` - start)) -lt 300 ]; do
113	mount /dev/md$u2 $mp2 || break
114	ls -lR $mp2 > /dev/null || { s=102; echo "ls failed"; break; }
115	touch $mp2/`jot -rc 8 a z | tr -d '\n'`
116	while mount | grep -q "on $mp2 "; do umount $mp2; done
117	echo * | grep -q core && break
118	sync
119	mdconfig -d -u $u2
120	# SBLOCK = 64k, SBLOCKSIZE = 8k
121	/tmp/flip -n 5 -s $(((64 + 8) * 1024)) $diskimage
122
123	sync
124	if [ `stat -f%z $diskimage` -gt $max ]; then
125		ls -lh $diskimage
126		truncate -s $max $diskimage
127	else
128		gzip < $diskimage > $backup
129	fi
130	fsync $backup
131	sync
132
133	for i in `jot 5`; do
134		[ $i -gt 2 ] && echo "fsck run #$i"
135		chk $diskimage
136		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
137		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
138		[ -f fsck_ffs.core ] &&
139		    { cp -v $diskimage \
140		        /tmp/fsck_ffs.core.diskimage.`date +%Y%m%dT%H%M%S`; break 2; }
141	done
142	[ $clean -ne 1 ] && break
143	mdconfig -a -t vnode -f $diskimage -u $u2
144	[ $r -ne 0 -a $clean -eq 1 ] &&
145	    { echo "CLEAN && non zero exit code"; break; }
146	[ $clean -eq 1 ] && continue
147	[ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=104; }
148	[ $waccess -eq 1 ] && { echo "No write access"; s=105; }
149	break
150done
151sleep 2	# Wait for /dev to catch up
152[ -c /dev/md$u2 ] && r1=1 || r1=0
153for i in `jot 5`; do
154	mount | grep -q "on $mp2 " || break
155	umount $mp2 && break
156	sleep 2
157done
158mdconfig -d -u $u2 2>/dev/null # XXX when mount fails
159
160echo "$cleans cleans, $reruns reruns, $asbs alternate SBs."
161if [ $clean -ne 1 ]; then
162	echo "FS still not clean. Last fsck_ffs exit code was $r."
163	echo =================
164	cat $log
165	echo =================
166	cp -v $log /tmp
167	[ $s -eq 0 ] && s=106
168fi
169echo * | grep -q core && { ls -l *.core; cp -v $log /tmp; exit 106; } ||
170    rm -f $backup
171[ $s -eq 101 ] && rm -f $backup	# mount error breakout
172cd /tmp
173for i in `jot 5`; do
174	umount $mp1 && break
175	sleep 2
176done
177mdconfig -d -u $u1
178rm -f /tmp/flip
179exit $s
180