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