xref: /freebsd/tools/test/stress2/misc/fsck13.sh (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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
81mdconfig -d -u $u2
82
83chk() {
84	local i
85
86	clean=0
87	rerun=0
88	waccess=0
89	LD_PRELOAD=/tmp/fsck_preload.so  \
90	    timeout 2m fsck_ffs -fy $1 > $log 2>&1
91	r=$?
92	if grep -qiE "super-?block.*failed" $log; then
93		for b in $backups; do
94			asbs=$((asbs + 1))
95			LD_PRELOAD=/tmp/fsck_preload.so  \
96			    timeout 2m fsck_ffs -b $b -fy $1 > $log 2>&1
97			r=$?
98			grep -qiE "super-?block.*failed" $log ||
99			   break
100		done
101		usedasb=1
102	else
103		usedasb=0
104	fi
105	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
106	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
107	# For now regard a "was modified" as a cause for a rerun,
108	# disregarding "clean" claim.
109	grep -Eq "WAS MODIFIED" $log && rerun=1
110	grep -q RERUN $log && rerun=1
111	grep -q "NO WRITE ACCESS" $log && waccess=1
112	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
113}
114
115cd $mp1
116s=0
117start=`date +%s`
118while [ $((`date +%s` - start)) -lt 60 ]; do
119	/tmp/flip -n 50 $diskimage
120
121	cp $diskimage $backup
122
123	for i in `jot 10`; do
124		chk $diskimage
125		[ $i -eq 1 -a "$flags" = "-j" ] && continue # First run processes the journal
126		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
127		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
128		[ -f fsck_ffs.core ] && { s=1; break 2; }
129		[ $r -eq 124 ] && { s=2; break 2; } # timeout
130	done
131	[ $clean -ne 1 ] && break
132	[ $r -ne 0 -a $clean -eq 1 ] &&
133	    { echo "CLEAN && non zero exit code"; break; }
134	[ $clean -eq 1 ] && continue
135	[ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=104; }
136	[ $waccess -eq 1 ] && { echo "No write access"; s=105; }
137	break
138done
139
140echo "$cleans cleans, $reruns reruns, $asbs alternate SBs."
141if [ $clean -ne 1 ]; then
142	echo "FS still not clean. Last fsck_ffs exit code was $r."
143	[ $s -eq 0 ] && s=106
144fi
145grep -q "Superblock check-hash failed" $log && s=0 # Ignore for now
146grep -q "is not a file system superblock" $log && s=0 # Ignore for now
147[ $s -ne 0 ] && { gzip $backup; cp -v $backup.gz /tmp; }
148cd /tmp
149umount $mp1
150mdconfig -d -u $u1
151rm -f /tmp/flip /tmp/fsck_preload.so
152exit $s
153