xref: /freebsd/tools/test/stress2/misc/fsck.sh (revision 3078531de10dcae44b253a35125c949ff4235284)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2018 Dell EMC Isilon
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.
31
32# "UFS /dev/md11 (/mnt11) cylinder checksum failed" seen.
33# Fixed by r341510.
34
35# 'panic: invalid counts on struct mount' seen:
36# https://people.freebsd.org/~pho/stress/log/fsck-4.txt
37
38# "panic: softdep_load_inodeblock: negative i_effnlink" seen.
39
40. ../default.cfg
41
42[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
43
44cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1
45
46set -e
47u1=$mdstart
48u2=$((mdstart + 1))
49mp1=${mntpoint}$u1
50mp2=${mntpoint}$u2
51mkdir -p $mp1 $mp2
52log=$mp1/fsck.sh.log
53diskimage=$mp1/fsck.sh.diskimage
54backup=/tmp/fsck.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
55asbs=0
56cleans=0
57reruns=0
58waccess=0
59
60max=$((10 * 1024 * 1024))
61[ "$newfs_flags" = "-j" ] &&
62    max=$((20 * 1024 * 1024))
63
64set +e
65mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
66[ -c /dev/md$u1 ] && mdconfig -d -u $u1
67mdconfig -a -t swap -s 1g -u $u1
68newfs $newfs_flags -n /dev/md$u1 > /dev/null
69mount /dev/md$u1 $mp1
70
71[ -c /dev/md$u2 ] && mdconfig -d -u $u2
72dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
73mdconfig -a -t vnode -f $diskimage -u $u2
74backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
75    tail -1 | sed 's/,//g'`
76newfs $newfs_flags md$u2 > /dev/null
77mount /dev/md$u2 $mp2
78[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
79umount $mp2
80
81chk() {
82	local i
83
84	clean=0
85	rerun=0
86	waccess=0
87	fsck_ffs -fy $1 > $log 2>&1
88	r=$?
89	if grep -qiE "super-?block.*failed" $log; then
90		for b in $backups; do
91			echo "Using alternate SB $b"
92			asbs=$((asbs + 1))
93			fsck_ffs -b $b -fy $1 > $log 2>&1
94			r=$?
95			grep -qiE "super-?block.*failed" $log ||
96			   break
97		done
98		usedasb=1
99	else
100		usedasb=0
101	fi
102	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
103	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
104	# For now regard a "was modified" as a cause for a rerun,
105	# disregarding "clean" claim.
106	grep -Eq "WAS MODIFIED" $log && rerun=1
107	grep -q RERUN $log && rerun=1
108	grep -q "NO WRITE ACCESS" $log && waccess=1
109	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
110}
111
112cd $mp1
113s=0
114start=`date +%s`
115while [ $((`date +%s` - start)) -lt 60 ]; do
116	mount /dev/md$u2 $mp2 || { s=101; break; }
117	ls -lR $mp2 > /dev/null || { s=102; echo "ls failed"; break; }
118	touch $mp2/`jot -rc 8 a z | tr -d '\n'`
119	while mount | grep -q "on $mp2 "; do umount $mp2; done
120	echo * | grep -q core && break
121	sync
122	mdconfig -d -u $u2
123	/tmp/flip -n 10 $diskimage
124
125	sync
126	gzip < $diskimage > $backup
127	fsync $backup
128
129	for i in `jot 3`; do
130		chk $diskimage
131		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
132		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
133		[ -f fsck_ffs.core ] &&
134		    { cp $diskimage \
135		        /tmp/fsck_ffs.core.diskimage.`date +%Y%m%dT%H%M%S`; break 2; }
136	done
137	[ $clean -ne 1 ] && break
138	mdconfig -a -t vnode -f $diskimage -u $u2
139	[ $r -ne 0 -a $clean -eq 1 ] &&
140	    { echo "CLEAN && non zero exit code"; break; }
141	[ $clean -eq 1 ] && continue
142	[ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=104; }
143	[ $waccess -eq 1 ] && { echo "No write access"; s=105; }
144	break
145done
146mount | grep -q "on $mp2 " && umount $mp2
147mdconfig -d -u $u2 || exit 1
148
149echo "$cleans cleans, $reruns reruns, $asbs alternate SBs."
150if [ $clean -ne 1 ]; then
151	echo "FS still not clean. Last fsck_ffs exit code was $r."
152	cat $log
153	cp -v $log /tmp
154	[ $s -eq 0 ] && s=106
155fi
156echo * | grep -q core && { ls -l *.core; cp $log /tmp; exit 106; } ||
157    rm -f $backup
158cd /tmp
159umount $mp1
160mdconfig -d -u $u1
161rm -f /tmp/flip
162exit $s
163