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
10 #-----------------------------------------------------------------------
11 # Copyright (c) 2018 Oracle, Inc.
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.
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.
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
26 #-----------------------------------------------------------------------
30 seqres=$RESULT_DIR/$seq
31 echo "QA output created by $seq"
36 trap "_cleanup; rm -f $tmp.*; exit \$status" 0 1 2 3 15
46 # get standard environment, filters and checks
50 # real QA test starts here
56 _require_test_program "punch-alternating"
57 _require_xfs_db_write_array
59 # This is only a v5 filesystem problem
60 _require_scratch_xfs_crc
63 if ! _try_scratch_mount >> $seqres.full 2>&1; then
64 echo "scratch mount failed" >> $seqres.full
68 # Trigger agfl fixing by fragmenting free space enough to cause
70 blksz=$(_get_file_block_size ${SCRATCH_MNT})
71 bno_maxrecs=$(( blksz / 8 ))
72 filesz=$((bno_maxrecs * 3 * blksz))
74 $XFS_IO_PROG -f -c "falloc 0 $filesz" $SCRATCH_MNT/a >> $seqres.full 2>&1
75 test -e $SCRATCH_MNT/a && ./src/punch-alternating $SCRATCH_MNT/a
78 _scratch_unmount 2>&1 | _filter_scratch
82 _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
89 echo "TEST $cmd" | tee /dev/ttyprintk
90 echo "TEST $cmd" >> $seqres.full
91 _scratch_mkfs >> $seqres.full
93 # Record what was here before
94 echo "FS BEFORE" >> $seqres.full
95 dump_ag0 > $tmp.before
96 cat $tmp.before >> $seqres.full
98 sectsize=$(_scratch_xfs_get_metadata_field "sectsize" "sb 0")
99 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0")
100 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0")
101 flcount=$(_scratch_xfs_get_metadata_field "flcount" "agf 0")
103 # Due to a padding bug in the original v5 struct xfs_agfl,
104 # XFS_AGFL_SIZE could be 36 on 32-bit or 40 on 64-bit. On a system
105 # with 512b sectors, this means that the AGFL length could be
106 # ((512 - 36) / 4) = 119 entries on 32-bit or ((512 - 40) / 4) = 118
109 # We now have code to figure out if the AGFL list wraps incorrectly
110 # according to the kernel's agfl size and fix it by resetting the agfl
111 # to zero length. Mutate ag 0's agfl to be in various configurations
112 # and see if we can trigger the reset.
114 # Don't hardcode the numbers, calculate them.
116 # Have to have at least three agfl items to test full wrap
117 test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
119 # mkfs should be able to make us a nice neat flfirst < fllast setup
120 test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
122 bad_agfl_size=$(( (sectsize - 40) / 4 ))
123 good_agfl_size=$(( (sectsize - 36) / 4 ))
126 "fix_end") # fllast points to the end w/ 40-byte padding
127 new_flfirst=$(( bad_agfl_size - flcount ))
128 agfl_size=$bad_agfl_size;;
129 "fix_start") # flfirst points to the end w/ 40-byte padding
130 new_flfirst=$(( bad_agfl_size - 1))
131 agfl_size=$bad_agfl_size;;
132 "fix_wrap") # list wraps around end w/ 40-byte padding
133 new_flfirst=$(( bad_agfl_size - (flcount / 2) ))
134 agfl_size=$bad_agfl_size;;
135 "start_zero") # flfirst points to the start
137 agfl_size=$good_agfl_size;;
138 "good_end") # fllast points to the end w/ 36-byte padding
139 new_flfirst=$(( good_agfl_size - flcount ))
140 agfl_size=$good_agfl_size;;
141 "good_start") # flfirst points to the end w/ 36-byte padding
142 new_flfirst=$(( good_agfl_size - 1 ))
143 agfl_size=$good_agfl_size;;
144 "good_wrap") # list wraps around end w/ 36-byte padding
145 new_flfirst=$(( good_agfl_size - (flcount / 2) ))
146 agfl_size=$good_agfl_size;;
147 "bad_start") # flfirst points off the end
148 new_flfirst=$good_agfl_size
149 agfl_size=$good_agfl_size;;
150 "no_move") # whatever mkfs formats (flfirst points to start)
152 agfl_size=$good_agfl_size;;
153 "simple_move") # move list arbitrarily
154 new_flfirst=$((fllast + 1))
155 agfl_size=$good_agfl_size;;
157 _fail "Internal test error";;
159 new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
161 # Log what we're doing...
162 cat >> $seqres.full << ENDL
163 sector size: $sectsize
164 bad_agfl_size: $bad_agfl_size [0 - $((bad_agfl_size - 1))]
165 good_agfl_size: $good_agfl_size [0 - $((good_agfl_size - 1))]
166 agfl_size: $agfl_size
170 new_flfirst: $new_flfirst
171 new_fllast: $new_fllast
174 # Remap the agfl blocks
175 echo "$((good_agfl_size - 1)) 0xffffffff" > $tmp.remap
176 seq "$flfirst" "$fllast" | while read f; do
177 list_pos=$((f - flfirst))
178 dest_pos=$(( (new_flfirst + list_pos) % agfl_size ))
179 bno=$(_scratch_xfs_get_metadata_field "bno[$f]" "agfl 0")
180 echo "$dest_pos $bno" >> $tmp.remap
183 cat $tmp.remap | while read dest_pos bno junk; do
184 _scratch_xfs_set_metadata_field "bno[$dest_pos]" "$bno" \
185 "agfl 0" >> $seqres.full
188 # Set new flfirst/fllast
189 _scratch_xfs_set_metadata_field "fllast" "$new_fllast" \
190 "agf 0" >> $seqres.full
191 _scratch_xfs_set_metadata_field "flfirst" "$new_flfirst" \
192 "agf 0" >> $seqres.full
194 echo "FS AFTER" >> $seqres.full
195 dump_ag0 > $tmp.corrupt 2> /dev/null
196 diff -u $tmp.before $tmp.corrupt >> $seqres.full
198 # Mount and see what happens
201 # Did we end up with a non-wrapped list?
202 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0" 2>/dev/null)
203 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0" 2>/dev/null)
204 echo "flfirst=${flfirst} fllast=${fllast}" >> $seqres.full
205 if [ "${flfirst}" -ge "$((good_agfl_size - 1))" ]; then
206 echo "ASSERT flfirst < good_agfl_size - 1" | tee -a $seqres.full
208 if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
209 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
211 if [ "${flfirst}" -ge "${fllast}" ]; then
212 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
215 echo "FS MOUNTLOOP" >> $seqres.full
216 dump_ag0 > $tmp.mountloop 2> /dev/null
217 diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
219 # Let's see what repair thinks
220 echo "REPAIR" >> $seqres.full
221 _scratch_xfs_repair >> $seqres.full 2>&1
223 echo "FS REPAIR" >> $seqres.full
224 dump_ag0 > $tmp.repair 2> /dev/null
225 diff -u $tmp.mountloop $tmp.repair >> $seqres.full
227 # Exercise the filesystem again to make sure there aren't any lasting
228 # ill effects from either the agfl reset or the recommended subsequent
232 echo "FS REMOUNT" >> $seqres.full
233 dump_ag0 > $tmp.remount 2> /dev/null
234 diff -u $tmp.repair $tmp.remount >> $seqres.full
248 # Did we get the kernel warning too?
249 warn_str='WARNING: Reset corrupted AGFL'
250 _check_dmesg_for "${warn_str}" || echo "Missing dmesg string \"${warn_str}\"."
252 # Now run the regular dmesg check, filtering out the agfl warning
253 filter_agfl_reset_printk() {
254 grep -v "${warn_str}"
256 _check_dmesg filter_agfl_reset_printk