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
40 _require_test_program "punch-alternating"
41 _require_xfs_db_write_array
43 # This is only a v5 filesystem problem
44 _require_scratch_xfs_crc
47 if ! _try_scratch_mount >> $seqres.full 2>&1; then
48 echo "scratch mount failed" >> $seqres.full
52 # Trigger agfl fixing by fragmenting free space enough to cause
54 blksz=$(_get_file_block_size ${SCRATCH_MNT})
55 bno_maxrecs=$(( blksz / 8 ))
56 filesz=$((bno_maxrecs * 3 * blksz))
58 $XFS_IO_PROG -f -c "falloc 0 $filesz" $SCRATCH_MNT/a >> $seqres.full 2>&1
59 test -e $SCRATCH_MNT/a && ./src/punch-alternating $SCRATCH_MNT/a
62 _scratch_unmount 2>&1 | _filter_scratch
66 _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
73 echo "TEST $cmd" | tee /dev/ttyprintk
74 echo "TEST $cmd" >> $seqres.full
75 _scratch_mkfs >> $seqres.full
77 # Record what was here before
78 echo "FS BEFORE" >> $seqres.full
79 dump_ag0 > $tmp.before
80 cat $tmp.before >> $seqres.full
82 sectsize=$(_scratch_xfs_get_metadata_field "sectsize" "sb 0")
83 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0")
84 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0")
85 flcount=$(_scratch_xfs_get_metadata_field "flcount" "agf 0")
87 # Due to a padding bug in the original v5 struct xfs_agfl,
88 # XFS_AGFL_SIZE could be 36 on 32-bit or 40 on 64-bit. On a system
89 # with 512b sectors, this means that the AGFL length could be
90 # ((512 - 36) / 4) = 119 entries on 32-bit or ((512 - 40) / 4) = 118
93 # We now have code to figure out if the AGFL list wraps incorrectly
94 # according to the kernel's agfl size and fix it by resetting the agfl
95 # to zero length. Mutate ag 0's agfl to be in various configurations
96 # and see if we can trigger the reset.
98 # Don't hardcode the numbers, calculate them.
100 # Have to have at least three agfl items to test full wrap
101 test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
103 # mkfs should be able to make us a nice neat flfirst < fllast setup
104 test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
106 bad_agfl_size=$(( (sectsize - 40) / 4 ))
107 good_agfl_size=$(( (sectsize - 36) / 4 ))
110 "fix_end") # fllast points to the end w/ 40-byte padding
111 new_flfirst=$(( bad_agfl_size - flcount ))
112 agfl_size=$bad_agfl_size;;
113 "fix_start") # flfirst points to the end w/ 40-byte padding
114 new_flfirst=$(( bad_agfl_size - 1))
115 agfl_size=$bad_agfl_size;;
116 "fix_wrap") # list wraps around end w/ 40-byte padding
117 new_flfirst=$(( bad_agfl_size - (flcount / 2) ))
118 agfl_size=$bad_agfl_size;;
119 "start_zero") # flfirst points to the start
121 agfl_size=$good_agfl_size;;
122 "good_end") # fllast points to the end w/ 36-byte padding
123 new_flfirst=$(( good_agfl_size - flcount ))
124 agfl_size=$good_agfl_size;;
125 "good_start") # flfirst points to the end w/ 36-byte padding
126 new_flfirst=$(( good_agfl_size - 1 ))
127 agfl_size=$good_agfl_size;;
128 "good_wrap") # list wraps around end w/ 36-byte padding
129 new_flfirst=$(( good_agfl_size - (flcount / 2) ))
130 agfl_size=$good_agfl_size;;
131 "bad_start") # flfirst points off the end
132 new_flfirst=$good_agfl_size
133 agfl_size=$good_agfl_size;;
134 "no_move") # whatever mkfs formats (flfirst points to start)
136 agfl_size=$good_agfl_size;;
137 "simple_move") # move list arbitrarily
138 new_flfirst=$((fllast + 1))
139 agfl_size=$good_agfl_size;;
141 _fail "Internal test error";;
143 new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
145 # Log what we're doing...
146 cat >> $seqres.full << ENDL
147 sector size: $sectsize
148 bad_agfl_size: $bad_agfl_size [0 - $((bad_agfl_size - 1))]
149 good_agfl_size: $good_agfl_size [0 - $((good_agfl_size - 1))]
150 agfl_size: $agfl_size
154 new_flfirst: $new_flfirst
155 new_fllast: $new_fllast
158 # Remap the agfl blocks
159 echo "$((good_agfl_size - 1)) 0xffffffff" > $tmp.remap
160 seq "$flfirst" "$fllast" | while read f; do
161 list_pos=$((f - flfirst))
162 dest_pos=$(( (new_flfirst + list_pos) % agfl_size ))
163 bno=$(_scratch_xfs_get_metadata_field "bno[$f]" "agfl 0")
164 echo "$dest_pos $bno" >> $tmp.remap
167 cat $tmp.remap | while read dest_pos bno junk; do
168 _scratch_xfs_set_metadata_field "bno[$dest_pos]" "$bno" \
169 "agfl 0" >> $seqres.full
172 # Set new flfirst/fllast
173 _scratch_xfs_set_metadata_field "fllast" "$new_fllast" \
174 "agf 0" >> $seqres.full
175 _scratch_xfs_set_metadata_field "flfirst" "$new_flfirst" \
176 "agf 0" >> $seqres.full
178 echo "FS AFTER" >> $seqres.full
179 dump_ag0 > $tmp.corrupt 2> /dev/null
180 diff -u $tmp.before $tmp.corrupt >> $seqres.full
182 # Mount and see what happens
185 # Did we end up with a non-wrapped list?
186 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0" 2>/dev/null)
187 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0" 2>/dev/null)
188 echo "flfirst=${flfirst} fllast=${fllast}" >> $seqres.full
189 if [ "${flfirst}" -ge "$((good_agfl_size - 1))" ]; then
190 echo "ASSERT flfirst < good_agfl_size - 1" | tee -a $seqres.full
192 if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
193 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
195 if [ "${flfirst}" -ge "${fllast}" ]; then
196 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
199 echo "FS MOUNTLOOP" >> $seqres.full
200 dump_ag0 > $tmp.mountloop 2> /dev/null
201 diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
203 # Let's see what repair thinks
204 echo "REPAIR" >> $seqres.full
205 _scratch_xfs_repair >> $seqres.full 2>&1
207 echo "FS REPAIR" >> $seqres.full
208 dump_ag0 > $tmp.repair 2> /dev/null
209 diff -u $tmp.mountloop $tmp.repair >> $seqres.full
211 # Exercise the filesystem again to make sure there aren't any lasting
212 # ill effects from either the agfl reset or the recommended subsequent
216 echo "FS REMOUNT" >> $seqres.full
217 dump_ag0 > $tmp.remount 2> /dev/null
218 diff -u $tmp.repair $tmp.remount >> $seqres.full
232 # Did we get the kernel warning too?
233 warn_str='WARNING: Reset corrupted AGFL'
234 _check_dmesg_for "${warn_str}" || echo "Missing dmesg string \"${warn_str}\"."
236 # Now run the regular dmesg check, filtering out the agfl warning
237 filter_agfl_reset_printk() {
238 grep -v "${warn_str}"
240 _check_dmesg filter_agfl_reset_printk