xref: /freebsd/tools/test/stress2/misc/fsck13.sh (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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# fsck_ffs(8) disk image fuzz test.
31# Test without mount(8) and umount(8)
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
39# Disable the calls to sync(2) in fsck_ffs(8) to speed up the test
40echo 'int sync(void) { return (0); }' > /tmp/fsck_preload.c
41mycc -o /tmp/fsck_preload.so -shared -fpic /tmp/fsck_preload.c || exit 1
42rm /tmp/fsck_preload.c
43
44set -eu
45u1=$mdstart
46u2=$((mdstart + 1))
47mp1=${mntpoint}$u1
48prog=$(basename "$0" .sh)
49mkdir -p $mp1
50
51max=$((2 * 1024 * 1024)) # Two alternate super blocks @ 192 and 2240
52i=`jot -r 1 1 3`
53[ $i -eq 1 ] && flags="-O2"
54[ $i -eq 2 ] && flags="-U"
55[ $i -eq 3 ] && { flags="-j"; max=$((8 * 1024 * 1024)); }
56
57backup=$mp1/$prog.diskimage.$flags.`date +%Y%m%dT%H%M%S`
58core=/tmp/$prog.core.`date +%Y%m%dT%H%M%S`
59diskimage=$mp1/$prog.diskimage
60log=$mp1/$prog.log
61
62asbs=0
63cleans=0
64reruns=0
65waccess=0
66
67set +e
68mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
69[ -c /dev/md$u1 ] && mdconfig -d -u $u1
70mdconfig -a -t swap -s 10g -u $u1
71newfs $newfs_flags /dev/md$u1 > /dev/null
72mount /dev/md$u1 $mp1
73
74[ -c /dev/md$u2 ] && mdconfig -d -u $u2
75dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
76mdconfig -a -t vnode -f $diskimage -u $u2
77backups=`newfs -N $flags md$u2 | grep -A1 "super-block backups" | \
78    tail -1 | sed 's/,//g'`
79echo "newfs $flags /dev/md$u2"
80newfs $flags md$u2 > /dev/null
81[ "$newfs_flags" = "" ] && tunefs -n disable md$u2
82mdconfig -d -u $u2
83
84chk() {
85	local i
86
87	clean=0
88	rerun=0
89	waccess=0
90	LD_PRELOAD=/tmp/fsck_preload.so  \
91	    timeout 2m fsck_ffs -fy $1 > $log 2>&1
92	r=$?
93	if grep -qiE "super-?block.*failed" $log; then
94		for b in $backups; do
95			asbs=$((asbs + 1))
96			LD_PRELOAD=/tmp/fsck_preload.so  \
97			    timeout 2m fsck_ffs -b $b -fy $1 > $log 2>&1
98			r=$?
99			grep -qiE "super-?block.*failed" $log ||
100			   break
101		done
102		usedasb=1
103	else
104		usedasb=0
105	fi
106	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
107	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
108	# For now regard a "was modified" as a cause for a rerun,
109	# disregarding "clean" claim.
110	grep -Eq "WAS MODIFIED" $log && rerun=1
111	grep -q RERUN $log && rerun=1
112	grep -q "NO WRITE ACCESS" $log && waccess=1
113	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
114}
115
116cd $mp1
117s=0
118start=`date +%s`
119while [ $((`date +%s` - start)) -lt 60 ]; do
120	/tmp/flip -n 50 $diskimage
121
122	cp $diskimage $backup
123
124	for i in `jot 10`; do
125		chk $diskimage
126		[ $i -eq 1 -a "$flags" = "-j" ] && continue # First run processes the journal
127		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
128		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
129		[ -f fsck_ffs.core ] && { s=1; break 2; }
130		[ $r -eq 124 ] && { s=2; break 2; } # timeout
131	done
132	[ $clean -ne 1 ] && break
133	[ $r -ne 0 -a $clean -eq 1 ] &&
134	    { echo "CLEAN && non zero exit code"; break; }
135	[ $clean -eq 1 ] && continue
136	[ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=104; }
137	[ $waccess -eq 1 ] && { echo "No write access"; s=105; }
138	break
139done
140
141echo "$cleans cleans, $reruns reruns, $asbs alternate SBs."
142if [ $clean -ne 1 ]; then
143	echo "FS still not clean. Last fsck_ffs exit code was $r."
144	[ $s -eq 0 ] && s=106
145fi
146grep -q "Superblock check-hash failed" $log && s=0 # Ignore for now
147grep -q "is not a file system superblock" $log && s=0 # Ignore for now
148[ $s -ne 0 ] && { gzip $backup; cp -v $backup.gz /tmp; }
149cd /tmp
150umount $mp1
151mdconfig -d -u $u1
152rm -f /tmp/flip /tmp/fsck_preload.so
153exit $s
154