xfs: test agfl reset on bad list wrapping
[xfstests-dev.git] / tests / xfs / 444
1 #! /bin/bash
2 # FS QA Test No. 444
3 #
4 # Make sure XFS can fix a v5 AGFL that wraps over the last block.
5 # Refer to commit 96f859d52bcb ("libxfs: pack the agfl header structure so
6 # XFS_AGFL_SIZE is correct") for details on the original on-disk format error
7 # and the patch "xfs: detect agfl count corruption and reset agfl") for details
8 # about the fix.
9 #
10 #-----------------------------------------------------------------------
11 # Copyright (c) 2018 Oracle, Inc.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License as
15 # published by the Free Software Foundation.
16 #
17 # This program is distributed in the hope that it would be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write the Free Software Foundation,
24 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25 #
26 #-----------------------------------------------------------------------
27 #
28
29 seq=`basename $0`
30 seqres=$RESULT_DIR/$seq
31 echo "QA output created by $seq"
32
33 here=`pwd`
34 tmp=/tmp/$$
35 status=1
36 trap "_cleanup; rm -f $tmp.*; exit \$status" 0 1 2 3 15
37
38 _cleanup()
39 {
40         cd /
41         rm -f $tmp.*
42 }
43
44 rm -f $seqres.full
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter
49
50 # real QA test starts here
51 _supported_fs xfs
52 _supported_os Linux
53
54 _require_check_dmesg
55 _require_scratch
56 _require_test_program "punch-alternating"
57
58 # This is only a v5 filesystem problem
59 _require_scratch_xfs_crc
60
61 mount_loop() {
62         if ! _try_scratch_mount >> $seqres.full 2>&1; then
63                 echo "scratch mount failed" >> $seqres.full
64                 return
65         fi
66
67         # Trigger agfl fixing by fragmenting free space enough to cause
68         # a bnobt split
69         blksz=$(_get_file_block_size ${SCRATCH_MNT})
70         bno_maxrecs=$(( blksz / 8 ))
71         filesz=$((bno_maxrecs * 3 * blksz))
72         rm -rf $SCRATCH_MNT/a
73         $XFS_IO_PROG -f -c "falloc 0 $filesz" $SCRATCH_MNT/a >> $seqres.full 2>&1
74         test -e $SCRATCH_MNT/a && ./src/punch-alternating $SCRATCH_MNT/a
75         rm -rf $SCRATCH_MNT/a
76
77         _scratch_unmount 2>&1 | _filter_scratch
78 }
79
80 dump_ag0() {
81         _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
82 }
83
84 runtest() {
85         cmd="$1"
86
87         # Format filesystem
88         echo "TEST $cmd" | tee /dev/ttyprintk
89         echo "TEST $cmd" >> $seqres.full
90         _scratch_mkfs >> $seqres.full
91
92         # Record what was here before
93         echo "FS BEFORE" >> $seqres.full
94         dump_ag0 > $tmp.before
95         cat $tmp.before >> $seqres.full
96
97         sectsize=$(_scratch_xfs_get_metadata_field "sectsize" "sb 0")
98         flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0")
99         fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0")
100         flcount=$(_scratch_xfs_get_metadata_field "flcount" "agf 0")
101
102         # Due to a padding bug in the original v5 struct xfs_agfl,
103         # XFS_AGFL_SIZE could be 36 on 32-bit or 40 on 64-bit.  On a system
104         # with 512b sectors, this means that the AGFL length could be
105         # ((512 - 36) / 4) = 119 entries on 32-bit or ((512 - 40) / 4) = 118
106         # entries on 64-bit.
107         #
108         # We now have code to figure out if the AGFL list wraps incorrectly
109         # according to the kernel's agfl size and fix it by resetting the agfl
110         # to zero length.  Mutate ag 0's agfl to be in various configurations
111         # and see if we can trigger the reset.
112         #
113         # Don't hardcode the numbers, calculate them.
114
115         # Have to have at least three agfl items to test full wrap
116         test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
117
118         # mkfs should be able to make us a nice neat flfirst < fllast setup
119         test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
120
121         bad_agfl_size=$(( (sectsize - 40) / 4 ))
122         good_agfl_size=$(( (sectsize - 36) / 4 ))
123         agfl_size=
124         case "$1" in
125         "fix_end")      # fllast points to the end w/ 40-byte padding
126                 new_flfirst=$(( bad_agfl_size - flcount ))
127                 agfl_size=$bad_agfl_size;;
128         "fix_start")    # flfirst points to the end w/ 40-byte padding
129                 new_flfirst=$(( bad_agfl_size - 1))
130                 agfl_size=$bad_agfl_size;;
131         "fix_wrap")     # list wraps around end w/ 40-byte padding
132                 new_flfirst=$(( bad_agfl_size - (flcount / 2) ))
133                 agfl_size=$bad_agfl_size;;
134         "start_zero")   # flfirst points to the start
135                 new_flfirst=0
136                 agfl_size=$good_agfl_size;;
137         "good_end")     # fllast points to the end w/ 36-byte padding
138                 new_flfirst=$(( good_agfl_size - flcount ))
139                 agfl_size=$good_agfl_size;;
140         "good_start")   # flfirst points to the end w/ 36-byte padding
141                 new_flfirst=$(( good_agfl_size - 1 ))
142                 agfl_size=$good_agfl_size;;
143         "good_wrap")    # list wraps around end w/ 36-byte padding
144                 new_flfirst=$(( good_agfl_size - (flcount / 2) ))
145                 agfl_size=$good_agfl_size;;
146         "bad_start")    # flfirst points off the end
147                 new_flfirst=$good_agfl_size
148                 agfl_size=$good_agfl_size;;
149         "no_move")      # whatever mkfs formats (flfirst points to start)
150                 new_flfirst=$flfirst
151                 agfl_size=$good_agfl_size;;
152         "simple_move")  # move list arbitrarily
153                 new_flfirst=$((fllast + 1))
154                 agfl_size=$good_agfl_size;;
155         *)
156                 _fail "Internal test error";;
157         esac
158         new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
159
160         # Log what we're doing...
161         cat >> $seqres.full << ENDL
162 sector size: $sectsize
163 bad_agfl_size: $bad_agfl_size [0 - $((bad_agfl_size - 1))]
164 good_agfl_size: $good_agfl_size [0 - $((good_agfl_size - 1))]
165 agfl_size: $agfl_size
166 flfirst: $flfirst
167 fllast: $fllast
168 flcount: $flcount
169 new_flfirst: $new_flfirst
170 new_fllast: $new_fllast
171 ENDL
172
173         # Remap the agfl blocks
174         echo "$((good_agfl_size - 1)) 0xffffffff" > $tmp.remap
175         seq "$flfirst" "$fllast" | while read f; do
176                 list_pos=$((f - flfirst))
177                 dest_pos=$(( (new_flfirst + list_pos) % agfl_size ))
178                 bno=$(_scratch_xfs_get_metadata_field "bno[$f]" "agfl 0")
179                 echo "$dest_pos $bno" >> $tmp.remap
180         done
181
182         cat $tmp.remap | while read dest_pos bno junk; do
183                 _scratch_xfs_set_metadata_field "bno[$dest_pos]" "$bno" \
184                                 "agfl 0" >> $seqres.full
185         done
186
187         # Set new flfirst/fllast
188         _scratch_xfs_set_metadata_field "fllast" "$new_fllast" \
189                         "agf 0" >> $seqres.full
190         _scratch_xfs_set_metadata_field "flfirst" "$new_flfirst" \
191                         "agf 0" >> $seqres.full
192
193         echo "FS AFTER" >> $seqres.full
194         dump_ag0 > $tmp.corrupt 2> /dev/null
195         diff -u $tmp.before $tmp.corrupt >> $seqres.full
196
197         # Mount and see what happens
198         mount_loop
199
200         # Did we end up with a non-wrapped list?
201         flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0" 2>/dev/null)
202         fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0" 2>/dev/null)
203         echo "flfirst=${flfirst} fllast=${fllast}" >> $seqres.full
204         if [ "${flfirst}" -ge "$((good_agfl_size - 1))" ]; then
205                 echo "ASSERT flfirst < good_agfl_size - 1" | tee -a $seqres.full
206         fi
207         if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
208                 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
209         fi
210         if [ "${flfirst}" -ge "${fllast}" ]; then
211                 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
212         fi
213
214         echo "FS MOUNTLOOP" >> $seqres.full
215         dump_ag0 > $tmp.mountloop 2> /dev/null
216         diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
217
218         # Let's see what repair thinks
219         echo "REPAIR" >> $seqres.full
220         _scratch_xfs_repair >> $seqres.full 2>&1
221
222         echo "FS REPAIR" >> $seqres.full
223         dump_ag0 > $tmp.repair 2> /dev/null
224         diff -u $tmp.mountloop $tmp.repair >> $seqres.full
225
226         # Exercise the filesystem again to make sure there aren't any lasting
227         # ill effects from either the agfl reset or the recommended subsequent
228         # repair run.
229         mount_loop
230
231         echo "FS REMOUNT" >> $seqres.full
232         dump_ag0 > $tmp.remount 2> /dev/null
233         diff -u $tmp.repair $tmp.remount >> $seqres.full
234 }
235
236 runtest fix_end
237 runtest fix_start
238 runtest fix_wrap
239 runtest start_zero
240 runtest good_end
241 runtest good_start
242 runtest good_wrap
243 runtest bad_start
244 runtest no_move
245 runtest simple_move
246
247 # Did we get the kernel warning too?
248 warn_str='WARNING: Reset corrupted AGFL'
249 _check_dmesg_for "${warn_str}" || echo "Missing dmesg string \"${warn_str}\"."
250
251 # Now run the regular dmesg check, filtering out the agfl warning
252 filter_agfl_reset_printk() {
253         grep -v "${warn_str}"
254 }
255 _check_dmesg filter_agfl_reset_printk
256
257 status=0
258 exit