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"
58 # This is only a v5 filesystem problem
59 _require_scratch_xfs_crc
62 if ! _try_scratch_mount >> $seqres.full 2>&1; then
63 echo "scratch mount failed" >> $seqres.full
67 # Trigger agfl fixing by fragmenting free space enough to cause
69 blksz=$(_get_file_block_size ${SCRATCH_MNT})
70 bno_maxrecs=$(( blksz / 8 ))
71 filesz=$((bno_maxrecs * 3 * blksz))
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
77 _scratch_unmount 2>&1 | _filter_scratch
81 _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
88 echo "TEST $cmd" | tee /dev/ttyprintk
89 echo "TEST $cmd" >> $seqres.full
90 _scratch_mkfs >> $seqres.full
92 # Record what was here before
93 echo "FS BEFORE" >> $seqres.full
94 dump_ag0 > $tmp.before
95 cat $tmp.before >> $seqres.full
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")
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
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.
113 # Don't hardcode the numbers, calculate them.
115 # Have to have at least three agfl items to test full wrap
116 test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
118 # mkfs should be able to make us a nice neat flfirst < fllast setup
119 test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
121 bad_agfl_size=$(( (sectsize - 40) / 4 ))
122 good_agfl_size=$(( (sectsize - 36) / 4 ))
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
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)
151 agfl_size=$good_agfl_size;;
152 "simple_move") # move list arbitrarily
153 new_flfirst=$((fllast + 1))
154 agfl_size=$good_agfl_size;;
156 _fail "Internal test error";;
158 new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
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
169 new_flfirst: $new_flfirst
170 new_fllast: $new_fllast
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
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
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
193 echo "FS AFTER" >> $seqres.full
194 dump_ag0 > $tmp.corrupt 2> /dev/null
195 diff -u $tmp.before $tmp.corrupt >> $seqres.full
197 # Mount and see what happens
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
207 if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
208 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
210 if [ "${flfirst}" -ge "${fllast}" ]; then
211 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
214 echo "FS MOUNTLOOP" >> $seqres.full
215 dump_ag0 > $tmp.mountloop 2> /dev/null
216 diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
218 # Let's see what repair thinks
219 echo "REPAIR" >> $seqres.full
220 _scratch_xfs_repair >> $seqres.full 2>&1
222 echo "FS REPAIR" >> $seqres.full
223 dump_ag0 > $tmp.repair 2> /dev/null
224 diff -u $tmp.mountloop $tmp.repair >> $seqres.full
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
231 echo "FS REMOUNT" >> $seqres.full
232 dump_ag0 > $tmp.remount 2> /dev/null
233 diff -u $tmp.repair $tmp.remount >> $seqres.full
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}\"."
251 # Now run the regular dmesg check, filtering out the agfl warning
252 filter_agfl_reset_printk() {
253 grep -v "${warn_str}"
255 _check_dmesg filter_agfl_reset_printk