1249268Sglebius#!/bin/sh
2249268Sglebius
3249268Sglebius#
4249268Sglebius# SPDX-License-Identifier: BSD-2-Clause
5249268Sglebius#
6249268Sglebius# Copyright (c) 2022 Peter Holm <pho@FreeBSD.org>
7249268Sglebius#
8249268Sglebius# Redistribution and use in source and binary forms, with or without
9249268Sglebius# modification, are permitted provided that the following conditions
10249268Sglebius# are met:
11249268Sglebius# 1. Redistributions of source code must retain the above copyright
12249268Sglebius#    notice, this list of conditions and the following disclaimer.
13249268Sglebius# 2. Redistributions in binary form must reproduce the above copyright
14249268Sglebius#    notice, this list of conditions and the following disclaimer in the
15249268Sglebius#    documentation and/or other materials provided with the distribution.
16249268Sglebius#
17249268Sglebius# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18249268Sglebius# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19249268Sglebius# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20249268Sglebius# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21249268Sglebius# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22249268Sglebius# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23249268Sglebius# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24249268Sglebius# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25249268Sglebius# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26249268Sglebius# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27249268Sglebius# SUCH DAMAGE.
28249268Sglebius#
29249268Sglebius
30249268Sglebius# msdosfs disk image fuzz test.
31249268Sglebius
32249268Sglebius# "panic: wrong dirclust" seen:
33249268Sglebius# https://people.freebsd.org/~pho/stress/log/log0206.txt
34249268Sglebius
35249268Sglebius. ../default.cfg
36249268Sglebius
37249268Sglebius[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
38249268Sglebius
39249268Sglebiuscc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1
40249268Sglebius
41249268Sglebiusset -e
42249268Sglebiusu1=$mdstart
43249268Sglebiusu2=$((mdstart + 1))
44249268Sglebiusmp1=${mntpoint}$u1
45249268Sglebiusmp2=${mntpoint}$u2
46249268Sglebiusmkdir -p $mp1 $mp2
47249268Sglebiuslog=$mp1/msdos15.sh.log
48249268Sglebiusdiskimage=$mp1/msdos15.sh.diskimage
49249314Skibcap=$((32 * 1024))		# Only fuzz the first 32k
50249314Skibmax=$((10 * 1024 * 1024))	# dos disk size
51249268Sglebius
52249268Sglebiusset +e
53249268Sglebiusmount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
54249268Sglebius[ -c /dev/md$u1 ] && mdconfig -d -u $u1
55249268Sglebiusmdconfig -a -t swap -s 1g -u $u1
56249268Sglebiusnewfs -U /dev/md$u1 > /dev/null
57249268Sglebiusmount /dev/md$u1 $mp1
58249268Sglebius
59249268Sglebius[ -c /dev/md$u2 ] && mdconfig -d -u $u2
60249268Sglebiusdd if=/dev/zero of=$diskimage bs=$max count=1 status=none
61249268Sglebiusmdconfig -a -t vnode -f $diskimage -u $u2
62249268Sglebiusnewfs_msdos /dev/md$u2 > /dev/null 2>&1	# FAT12
63249268Sglebiusmount -t msdosfs /dev/md$u2 $mp2
64249268Sglebius[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
65249268Sglebiusumount $mp2
66249314Skib
67249268Sglebiuscd $mp1
68249268Sglebiusstart=`date +%s`
69249268Sglebiuswhile [ $((`date +%s` - start)) -lt 60 ]; do
70252434Skib	mount -t msdosfs /dev/md$u2 $mp2 2>/dev/null || break
71252434Skib	ls -lR $mp2 > /dev/null 2>&1 || break
72252434Skib	rm -rf $mp2/* > /dev/null 2>&1 || break
73252434Skib	touch $mp2/`jot -rc 8 a z | tr -d '\n'` || break
74252434Skib	while mount | grep -q "on $mp2 "; do umount $mp2; done
75252434Skib	echo * | grep -q core && break
76252434Skib	sync
77252434Skib	mdconfig -d -u $u2
78252434Skib	/tmp/flip -n 10 -s $cap $diskimage
79252434Skib	sync
80252434Skib	mdconfig -a -t vnode -f $diskimage -u $u2
81252434Skibdone
82252434Skibmount | grep -q "on $mp2 " && umount $mp2
83252434Skibmdconfig -d -u $u2 || exit 1
84252434Skib
85252434Skibecho * | grep -q core && { ls -l *.core; cp $log /tmp; exit 106; } ||
86252434Skibcd /tmp
87252434Skibumount $mp1
88252434Skibmdconfig -d -u $u1
89252434Skibrm -f /tmp/flip
90252434Skibexit 0
91252434Skib