xref: /freebsd/tools/test/stress2/misc/fsck8.sh (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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	fsck_ffs -fy $1 > $log 2>&1
84	r=$?
85	if grep -qE "Cannot find file system superblock|Superblock check-hash 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 -qE "Cannot find file system superblock|Superblock check-hash 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 || { s=101; 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
132	for i in `jot 5`; do
133		[ $i -gt 2 ] && echo "fsck run #$i"
134		chk $diskimage
135		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
136		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
137		[ -f fsck_ffs.core ] &&
138		    { cp $diskimage \
139		        /tmp/fsck_ffs.core.diskimage.`date +%Y%m%dT%H%M%S`; break 2; }
140	done
141	[ $clean -ne 1 ] && break
142	mdconfig -a -t vnode -f $diskimage -u $u2
143	[ $r -ne 0 -a $clean -eq 1 ] &&
144	    { echo "CLEAN && non zero exit code"; break; }
145	[ $clean -eq 1 ] && continue
146	[ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=104; }
147	[ $waccess -eq 1 ] && { echo "No write access"; s=105; }
148	break
149done
150sleep 2	# Wait for /dev to catch up
151[ -c /dev/md$u2 ] && r1=1 || r1=0
152for i in `jot 5`; do
153	mount | grep -q "on $mp2 " || break
154	umount $mp2 && break
155	sleep 2
156done
157mdconfig -d -u $u2 2>/dev/null # XXX when mount fails
158
159echo "$cleans cleans, $reruns reruns, $asbs alternate SBs."
160if [ $clean -ne 1 ]; then
161	echo "FS still not clean. Last fsck_ffs exit code was $r."
162	echo =================
163	cat $log
164	echo =================
165	cp -v $log /tmp
166	[ $s -eq 0 ] && s=106
167fi
168echo * | grep -q core && { ls -l *.core; cp $log /tmp; exit 106; } ||
169    rm -f $backup
170[ $s -eq 101 ] && rm -f $backup	# mount error breakout
171cd /tmp
172for i in `jot 5`; do
173	umount $mp1 && break
174	sleep 2
175done
176mdconfig -d -u $u1
177rm -f /tmp/flip
178exit $s
179