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) test. 31# "UFS /dev/md11 (/mnt11) cylinder checksum failed" seen. 32# Fixed by r341510. 33 34# 'panic: invalid counts on struct mount' seen: 35# https://people.freebsd.org/~pho/stress/log/fsck-4.txt 36 37[ $DEBUG ] || exit 0 # Still WiP 38 39. ../default.cfg 40 41[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 42 43cc -o /tmp/flip -Wall -Wextra -O2 ../tools/flip.c || exit 1 44 45echo 'int sync(void) { return (0); }' > /tmp/fsck_preload.c 46mycc -o /tmp/fsck_preload.so -shared -fpic /tmp/fsck_preload.c || exit 1 47cc -o /tmp/fsck_preload.so -shared -fpic /tmp/fsck_preload.c || exit 1 48rm /tmp/fsck_preload.c 49 50set -e 51u1=$mdstart 52u2=$((mdstart + 1)) 53mp1=${mntpoint}$u1 54mp2=${mntpoint}$u2 55mkdir -p $mp1 $mp2 56log=$mp1/fsck.sh.log 57diskimage=$mp1/fsck.sh.diskimage 58backup=/tmp/fsck.sh.diskimage.`date +%Y%m%dT%H%M%S`.gz 59asbs=0 60cleans=0 61reruns=0 62waccess=0 63 64max=$((10 * 1024 * 1024)) 65[ "$newfs_flags" = "-j" ] && 66 max=$((20 * 1024 * 1024)) 67 68set +e 69mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1 70[ -c /dev/md$u1 ] && mdconfig -d -u $u1 71mdconfig -a -t swap -s 1g -u $u1 72newfs $newfs_flags /dev/md$u1 > /dev/null 73mount /dev/md$u1 $mp1 74 75[ -c /dev/md$u2 ] && mdconfig -d -u $u2 76dd if=/dev/zero of=$diskimage bs=$max count=1 status=none 77mdconfig -a -t vnode -f $diskimage -u $u2 78backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \ 79 tail -1 | sed 's/,//g'` 80newfs $newfs_flags md$u2 > /dev/null 81mount /dev/md$u2 $mp2 82[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2 83umount $mp2 84 85chk() { 86 local i 87 88 LD_PRELOAD=/tmp/fsck_preload.so \ 89 fsck_ffs -fy $1 > $log 2>&1 90 r=$? 91 if grep -qE "Cannot find file system superblock|Superblock check-hash failed" $log; then 92 for b in $backups; do 93 echo "Using alternate SB $b" 94 asbs=$((asbs + 1)) 95 LD_PRELOAD=/tmp/fsck_preload.so \ 96 fsck_ffs -b $b -fy $1 > $log 2>&1 97 r=$? 98 grep -qE "Cannot find file system superblock|Superblock check-hash 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=$? 107 ! grep -q RERUN $log; rerun=$? 108 ! grep -q "NO WRITE ACCESS" $log; waccess=$? 109 [ $r -ne 0 -a $clean -eq 1 ] && echo "Exit code $r w/ clean == 1" 110 111} 112 113cd /tmp 114s=0 115start=`date +%s` 116while [ $((`date +%s` - start)) -lt 60 ]; do 117 gzip < $diskimage > $backup 118 fsync $backup; sync # ; sleep .2; sync; sleep .2; sync 119 mount /dev/md$u2 $mp2 || { s=101; break; } 120 touch $mp2/`jot -rc 8 a z | tr -d '\n'` 121 umount $mp2 122 /tmp/flip -n 4 $diskimage 123 for i in `jot 3`; do 124 chk /dev/md$u2 125 [ $clean -eq 1 ] && { cleans=$((cleans + 1)); break; } 126 [ $rerun -eq 1 ] && { reruns=$((reruns + 1)); continue; } 127 done 128 [ $r -ne 0 -a $clean -eq 1 ] && 129 { echo "CLEAN && non zero exit code"; break; } 130 [ $clean -eq 1 ] && continue 131 [ $usedasb -eq 1 ] && { echo "Alt. SB failed"; s=103; } 132 [ $waccess -eq 1 ] && { echo "No write access"; s=555; } 133 break 134done 135[ $DEBUG ] && 136 echo "$cleans cleans, $reruns reruns, $asbs alternate SBs." && cat $log 137if [ $clean -ne 1 ]; then 138 echo "FS still not clean. Last fsck_ffs exit code was $r." 139 cat $log 140 cp -v $log /tmp || rm $log 141 [ $s -eq 0 ] && s=104 142fi 143mdconfig -d -u $u2 || exit 1 144[ -f fsck_ffs.core ] && ls -l fsck_ffs.core 145 146umount $mp1 147mdconfig -d -u $u1 148rm -f /tmp/fsck_preload.so $backup /tmp/flip 149exit $s 150