xref: /freebsd/tools/test/stress2/misc/fsck9.sh (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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) false CLEAN claim scenario
31
32# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T150359.txt
33# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T150359.gz
34
35# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T172428.txt
36# https://people.freebsd.org/~pho/fsck9.sh.diskimage.20220716T172428.gz
37
38. ../default.cfg
39
40[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
41
42cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1
43
44set -e
45u1=$mdstart
46u2=$((mdstart + 1))
47mp1=${mntpoint}$u1
48mp2=${mntpoint}$u2
49mkdir -p $mp1 $mp2
50log=$mp1/fsck9.sh.log
51diskimage=$mp1/fsck9.sh.diskimage
52backup=/tmp/fsck9.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz
53cleans=0
54reruns=0
55
56max=$((10 * 1024 * 1024))
57newfs_flags='-U'
58
59mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
60[ -c /dev/md$u1 ] && mdconfig -d -u $u1
61mdconfig -a -t swap -s 1g -u $u1
62newfs $newfs_flags -n /dev/md$u1 > /dev/null
63mount /dev/md$u1 $mp1
64
65[ -c /dev/md$u2 ] && mdconfig -d -u $u2
66dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
67mdconfig -a -t vnode -f $diskimage -u $u2
68backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
69    tail -1 | sed 's/,//g'`
70newfs $newfs_flags -n md$u2 > /dev/null
71mount /dev/md$u2 $mp2
72[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
73umount $mp2
74set +e
75
76chk() {
77	local i
78
79	clean=0
80	rerun=0
81	fsck_ffs -fy $1 > $log 2>&1
82	r=$?
83	if grep -qE "Cannot find file system superblock|Superblock check-hash failed" $log; then
84		for b in $backups; do
85			fsck_ffs -b $b -fy $1 > $log 2>&1
86			r=$?
87			grep -qE "Cannot find file system superblock|Superblock check-hash failed" $log ||
88			   break
89		done
90		usedasb=1
91	else
92		usedasb=0
93	fi
94	LANG=C egrep -q "[A-Z][A-Z]" $log && clean=0
95	grep -Eq "IS CLEAN|MARKED CLEAN" $log && clean=1
96	grep -q RERUN $log && rerun=1
97	[ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1"
98}
99
100cd $mp1
101clean=0
102s=0
103start=`date +%s`
104while [ $((`date +%s` - start)) -lt 300 ]; do
105	mount /dev/md$u2 $mp2 || { s=101; break; }
106	ls -lR $mp2 > /dev/null || { s=102; echo "ls failed"; break; }
107	touch $mp2/`jot -rc 8 a z | tr -d '\n'`
108	while mount | grep -q "on $mp2 "; do umount $mp2; done
109	echo * | grep -q core && break
110	sync
111	mdconfig -d -u $u2
112	/tmp/flip -n 5 $diskimage
113
114	sync
115	if [ `stat -f%z $diskimage` -gt $max ]; then
116		ls -lh $diskimage
117		truncate -s $max $diskimage
118	else
119		gzip < $diskimage > $backup
120	fi
121	fsync $backup
122
123	for i in `jot 5`; do
124		[ $i -gt 2 ] && echo "fsck run #$i"
125		chk $diskimage
126		[ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; }
127		[ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; }
128		[ -f fsck_ffs.core ] && break 2
129	done
130	if [ $clean -eq 1 ]; then
131#		echo "Checking clean claim"
132		fsck_ffs -fy $diskimage > $log 2>&1
133		if grep -q MODIFIED $log; then
134			echo "fsck of "clean" FS found more issues:"
135			cat $log
136			s=1
137			break
138		fi
139	fi
140	[ $clean -ne 1 ] && break
141	mdconfig -a -t vnode -f $diskimage -u $u2
142done
143sleep 2	# Wait for /dev to catch up
144[ -c /dev/md$u2 ] && r1=1 || r1=0
145for i in `jot 5`; do
146	mount | grep -q "on $mp2 " || break
147	umount $mp2 && break
148	sleep 2
149done
150mdconfig -d -u $u2 2>/dev/null # XXX when mount fails
151
152[ $s -eq 0 ] && rm -f $backup
153cd /tmp
154for i in `jot 5`; do
155	umount $mp1 && break
156	sleep 2
157done
158mdconfig -d -u $u1
159rm -f /tmp/flip
160exit $s
161