2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Oracle, Inc.
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
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
20 trap "_cleanup; rm -f $tmp.*; exit \$status" 0 1 2 3 15
30 # get standard environment, filters and checks
34 # real QA test starts here
39 _require_test_program "punch-alternating"
40 _require_xfs_io_command "falloc"
41 _require_xfs_db_write_array
43 # This is only a v5 filesystem problem
44 _require_scratch_xfs_crc
46 # Disable the scratch rt device to avoid test failures relating to the rt
47 # bitmap consuming free space in our small data device and throwing off the
48 # filestreams allocator.
52 if ! _try_scratch_mount >> $seqres.full 2>&1; then
53 echo "scratch mount failed" >> $seqres.full
57 # Trigger agfl fixing by fragmenting free space enough to cause
59 blksz=$(_get_file_block_size ${SCRATCH_MNT})
60 bno_maxrecs=$(( blksz / 8 ))
61 filesz=$((bno_maxrecs * 3 * blksz))
63 $XFS_IO_PROG -f -c "falloc 0 $filesz" $SCRATCH_MNT/a >> $seqres.full 2>&1
64 test -e $SCRATCH_MNT/a && $here/src/punch-alternating $SCRATCH_MNT/a
67 _scratch_unmount 2>&1 | _filter_scratch
71 _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
78 echo "TEST $cmd" | tee /dev/ttyprintk
79 echo "TEST $cmd" >> $seqres.full
80 _scratch_mkfs >> $seqres.full
82 # Record what was here before
83 echo "FS BEFORE" >> $seqres.full
84 dump_ag0 > $tmp.before
85 cat $tmp.before >> $seqres.full
87 sectsize=$(_scratch_xfs_get_metadata_field "sectsize" "sb 0")
88 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0")
89 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0")
90 flcount=$(_scratch_xfs_get_metadata_field "flcount" "agf 0")
92 # Due to a padding bug in the original v5 struct xfs_agfl,
93 # XFS_AGFL_SIZE could be 36 on 32-bit or 40 on 64-bit. On a system
94 # with 512b sectors, this means that the AGFL length could be
95 # ((512 - 36) / 4) = 119 entries on 32-bit or ((512 - 40) / 4) = 118
98 # We now have code to figure out if the AGFL list wraps incorrectly
99 # according to the kernel's agfl size and fix it by resetting the agfl
100 # to zero length. Mutate ag 0's agfl to be in various configurations
101 # and see if we can trigger the reset.
103 # Don't hardcode the numbers, calculate them.
105 # Have to have at least three agfl items to test full wrap
106 test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
108 # mkfs should be able to make us a nice neat flfirst < fllast setup
109 test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
111 bad_agfl_size=$(( (sectsize - 40) / 4 ))
112 good_agfl_size=$(( (sectsize - 36) / 4 ))
115 "fix_end") # fllast points to the end w/ 40-byte padding
116 new_flfirst=$(( bad_agfl_size - flcount ))
117 agfl_size=$bad_agfl_size;;
118 "fix_start") # flfirst points to the end w/ 40-byte padding
119 new_flfirst=$(( bad_agfl_size - 1))
120 agfl_size=$bad_agfl_size;;
121 "fix_wrap") # list wraps around end w/ 40-byte padding
122 new_flfirst=$(( bad_agfl_size - (flcount / 2) ))
123 agfl_size=$bad_agfl_size;;
124 "start_zero") # flfirst points to the start
126 agfl_size=$good_agfl_size;;
127 "good_end") # fllast points to the end w/ 36-byte padding
128 new_flfirst=$(( good_agfl_size - flcount ))
129 agfl_size=$good_agfl_size;;
130 "good_start") # flfirst points to the end w/ 36-byte padding
131 new_flfirst=$(( good_agfl_size - 1 ))
132 agfl_size=$good_agfl_size;;
133 "good_wrap") # list wraps around end w/ 36-byte padding
134 new_flfirst=$(( good_agfl_size - (flcount / 2) ))
135 agfl_size=$good_agfl_size;;
136 "bad_start") # flfirst points off the end
137 new_flfirst=$good_agfl_size
138 agfl_size=$good_agfl_size;;
139 "no_move") # whatever mkfs formats (flfirst points to start)
141 agfl_size=$good_agfl_size;;
142 "simple_move") # move list arbitrarily
143 new_flfirst=$((fllast + 1))
144 agfl_size=$good_agfl_size;;
146 _fail "Internal test error";;
148 new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
150 # Log what we're doing...
151 cat >> $seqres.full << ENDL
152 sector size: $sectsize
153 bad_agfl_size: $bad_agfl_size [0 - $((bad_agfl_size - 1))]
154 good_agfl_size: $good_agfl_size [0 - $((good_agfl_size - 1))]
155 agfl_size: $agfl_size
159 new_flfirst: $new_flfirst
160 new_fllast: $new_fllast
163 # Remap the agfl blocks
164 echo "$((good_agfl_size - 1)) 0xffffffff" > $tmp.remap
165 seq "$flfirst" "$fllast" | while read f; do
166 list_pos=$((f - flfirst))
167 dest_pos=$(( (new_flfirst + list_pos) % agfl_size ))
168 bno=$(_scratch_xfs_get_metadata_field "bno[$f]" "agfl 0")
169 echo "$dest_pos $bno" >> $tmp.remap
172 cat $tmp.remap | while read dest_pos bno junk; do
173 _scratch_xfs_set_metadata_field "bno[$dest_pos]" "$bno" \
174 "agfl 0" >> $seqres.full
177 # Set new flfirst/fllast
178 _scratch_xfs_set_metadata_field "fllast" "$new_fllast" \
179 "agf 0" >> $seqres.full
180 _scratch_xfs_set_metadata_field "flfirst" "$new_flfirst" \
181 "agf 0" >> $seqres.full
183 echo "FS AFTER" >> $seqres.full
184 dump_ag0 > $tmp.corrupt 2> /dev/null
185 diff -u $tmp.before $tmp.corrupt >> $seqres.full
187 # Mount and see what happens
190 # Did we end up with a non-wrapped list?
191 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0" 2>/dev/null)
192 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0" 2>/dev/null)
193 echo "flfirst=${flfirst} fllast=${fllast}" >> $seqres.full
194 if [ "${flfirst}" -ge "$((good_agfl_size - 1))" ]; then
195 echo "ASSERT flfirst < good_agfl_size - 1" | tee -a $seqres.full
197 if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
198 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
200 if [ "${flfirst}" -ge "${fllast}" ]; then
201 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
204 echo "FS MOUNTLOOP" >> $seqres.full
205 dump_ag0 > $tmp.mountloop 2> /dev/null
206 diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
208 # Let's see what repair thinks
209 echo "REPAIR" >> $seqres.full
210 _scratch_xfs_repair >> $seqres.full 2>&1
212 echo "FS REPAIR" >> $seqres.full
213 dump_ag0 > $tmp.repair 2> /dev/null
214 diff -u $tmp.mountloop $tmp.repair >> $seqres.full
216 # Exercise the filesystem again to make sure there aren't any lasting
217 # ill effects from either the agfl reset or the recommended subsequent
221 echo "FS REMOUNT" >> $seqres.full
222 dump_ag0 > $tmp.remount 2> /dev/null
223 diff -u $tmp.repair $tmp.remount >> $seqres.full
237 # Did we get the kernel warning too?
238 warn_str='WARNING: Reset corrupted AGFL'
239 _check_dmesg_for "${warn_str}" || echo "Missing dmesg string \"${warn_str}\"."
241 # Now run the regular dmesg check, filtering out the agfl warning
242 filter_agfl_reset_printk() {
243 grep -v "${warn_str}"
245 _check_dmesg filter_agfl_reset_printk