common: replace chattr with $CHATTR_PROG
[xfstests-dev.git] / tests / xfs / 083
1 #! /bin/bash
2 # FS QA Test No. 083
3 #
4 # Create and populate an XFS filesystem, fuzz the metadata, then see how
5 # the kernel reacts, how xfs_repair fares in fixing the mess, and then
6 # try more kernel accesses to see if it really fixed things.
7 #
8 #-----------------------------------------------------------------------
9 # Copyright (c) 2015 Oracle, Inc.  All Rights Reserved.
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation.
14 #
15 # This program is distributed in the hope that it would be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write the Free Software Foundation,
22 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 #-----------------------------------------------------------------------
24 #
25
26 seq=`basename $0`
27 seqres=$RESULT_DIR/$seq
28 echo "QA output created by $seq"
29
30 here=`pwd`
31 tmp=/tmp/$$
32 status=1        # failure is the default!
33 trap "_cleanup; exit \$status" 0 1 2 3 15
34
35 _cleanup()
36 {
37     cd /
38     #rm -f $tmp.*
39 }
40
41 # get standard environment, filters and checks
42 . ./common/rc
43 . ./common/filter
44 . ./common/attr
45 . ./common/populate
46
47 # real QA test starts here
48 _supported_fs xfs
49 _supported_os Linux
50
51 _require_xfs_io_command "falloc"
52 _require_xfs_io_command "fpunch"
53 _require_scratch
54 #_require_xfs_crc       # checksum not required, but you probably want it anyway...
55 #_require_xfs_mkfs_crc
56 _require_attrs
57
58 scratch_repair() {
59         fsck_pass="$1"
60
61         FSCK_LOG="${tmp}-fuzz-${fsck_pass}.log"
62         echo "++ fsck pass ${fsck_pass}" > "${FSCK_LOG}"
63         _scratch_xfs_repair >> "${FSCK_LOG}" 2>&1
64         res=$?
65         if [ "${res}" -eq 0 ]; then
66                 echo "++ allegedly fixed, reverify" >> "${FSCK_LOG}"
67                 _scratch_xfs_repair -n >> "${FSCK_LOG}" 2>&1
68                 res=$?
69         fi
70         echo "++ fsck returns ${res}" >> "${FSCK_LOG}"
71         if [ "${res}" -eq 0 ]; then
72                 echo "++ fsck thinks we are done" >> "${FSCK_LOG}"
73                 cat "${FSCK_LOG}"
74                 return 0
75         elif [ "${res}" -eq 2 ]; then
76                 # replay log?
77                 echo "+++ replaying log" >> "${FSCK_LOG}"
78                 _scratch_mount >> "${FSCK_LOG}" 2>&1
79                 res=$?
80                 echo "+++ mount returns ${res}" >> "${FSCK_LOG}"
81                 if [ "${res}" -gt 0 ]; then
82                         echo "+++ zeroing log" >> "${FSCK_LOG}"
83                         _scratch_xfs_repair -L >> "${FSCK_LOG}" 2>&1
84                         echo "+++ returns $?" >> "${FSCK_LOG}"
85                 else
86                         umount "${SCRATCH_MNT}" >> "${FSCK_LOG}" 2>&1
87                 fi
88         elif [ "${fsck_pass}" -eq "${FSCK_PASSES}" ]; then
89                 echo "++ fsck did not fix in ${FSCK_PASSES} passes." >> "${FSCK_LOG}"
90                 cat "${FSCK_LOG}"
91                 return 0
92         fi
93         cat "${FSCK_LOG}"
94         if [ "${fsck_pass}" -gt 1 ]; then
95                 cmp -s "${tmp}-fuzz-$((fsck_pass - 1)).log" "${FSCK_LOG}"
96                 if [ $? -eq 0 ]; then
97                         echo "++ fsck makes no progress"
98                         return 2
99                 fi
100         fi
101         return 1
102 }
103
104 rm -f $seqres.full
105 echo "See interesting results in $seqres.full" | sed -e "s,$RESULT_DIR,RESULT_DIR,g"
106 SRCDIR=`pwd`
107 test -z "${FUZZ_ARGS}" && FUZZ_ARGS="-3 -n 32"
108 test -z "${FSCK_PASSES}" && FSCK_PASSES=10
109 BLK_SZ=4096
110
111 echo "fuzzing xfs with FUZZ_ARGS=$FUZZ_ARGS and FSCK_PASSES=$FSCK_PASSES" > $seqres.full
112
113 echo "+ create scratch fs" >> $seqres.full
114 _scratch_mkfs_xfs >> $seqres.full 2>&1
115
116 echo "+ populate fs image" >> $seqres.full
117 _scratch_populate >> $seqres.full
118
119 echo "+ check fs" >> $seqres.full
120 _scratch_xfs_repair >> $seqres.full 2>&1 || _fail "should pass initial fsck"
121
122 echo "++ corrupt image" >> $seqres.full
123 xfs_db -x -c blockget -c "blocktrash ${FUZZ_ARGS}" "${SCRATCH_DEV}" >> $seqres.full 2>&1
124
125 echo "++ mount image" >> $seqres.full
126 _scratch_mount >> $seqres.full 2>&1
127
128 echo "+++ test scratch" >> $seqres.full
129 _scratch_fuzz_test >> $seqres.full 2>&1
130
131 echo "+++ modify scratch" >> $seqres.full
132 _scratch_fuzz_modify >> $seqres.full 2>&1
133
134 echo "++ umount" >> $seqres.full
135 umount "${SCRATCH_MNT}"
136
137 # repair in a loop...
138 for p in $(seq 1 "${FSCK_PASSES}"); do
139         scratch_repair "$p" >> $seqres.full 2>&1 && break
140 done
141 echo "+ fsck loop returns ${fsck_loop_ret}" >> $seqres.full
142
143 echo "++ check fs for round 2" >> $seqres.full
144 _scratch_xfs_repair >> $seqres.full 2>&1
145
146 ROUND2_LOG="${tmp}-round2-${fsck_pass}.log"
147 echo "++ mount image (2)" >> $ROUND2_LOG
148 _scratch_mount >> $ROUND2_LOG 2>&1
149
150 echo "++ chattr -R -i" >> $ROUND2_LOG
151 $CHATTR_PROG -R -f -i "${SCRATCH_MNT}/" > /dev/null 2>> $ROUND2_LOG
152
153 echo "+++ test scratch" >> $ROUND2_LOG
154 _scratch_fuzz_test >> $ROUND2_LOG 2>&1
155
156 echo "+++ modify scratch" >> $ROUND2_LOG
157 _scratch_fuzz_modify >> $ROUND2_LOG 2>&1
158
159 echo "++ umount" >> $ROUND2_LOG
160 umount "${SCRATCH_MNT}" >> $ROUND2_LOG 2>&1
161
162 cat "$ROUND2_LOG" >> $seqres.full
163
164 echo "++ check fs (2)" >> $seqres.full
165 _scratch_xfs_repair >> $seqres.full 2>&1
166
167 egrep -q '(did not fix|makes no progress)' $seqres.full && echo "xfs_repair failed" | tee -a $seqres.full
168 if [ "$(wc -l < "$ROUND2_LOG")" -ne 8 ]; then
169         echo "xfs_repair did not fix everything" | tee -a $seqres.full
170 fi
171 echo "finished fuzzing" | tee -a "$seqres.full"
172
173 status=0
174 exit