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_io_command "falloc"
42 _require_xfs_db_write_array
44 # This is only a v5 filesystem problem
45 _require_scratch_xfs_crc
48 if ! _try_scratch_mount >> $seqres.full 2>&1; then
49 echo "scratch mount failed" >> $seqres.full
53 # Trigger agfl fixing by fragmenting free space enough to cause
55 blksz=$(_get_file_block_size ${SCRATCH_MNT})
56 bno_maxrecs=$(( blksz / 8 ))
57 filesz=$((bno_maxrecs * 3 * blksz))
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
63 _scratch_unmount 2>&1 | _filter_scratch
67 _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
74 echo "TEST $cmd" | tee /dev/ttyprintk
75 echo "TEST $cmd" >> $seqres.full
76 _scratch_mkfs >> $seqres.full
78 # Record what was here before
79 echo "FS BEFORE" >> $seqres.full
80 dump_ag0 > $tmp.before
81 cat $tmp.before >> $seqres.full
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")
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
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.
99 # Don't hardcode the numbers, calculate them.
101 # Have to have at least three agfl items to test full wrap
102 test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
104 # mkfs should be able to make us a nice neat flfirst < fllast setup
105 test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
107 bad_agfl_size=$(( (sectsize - 40) / 4 ))
108 good_agfl_size=$(( (sectsize - 36) / 4 ))
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
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)
137 agfl_size=$good_agfl_size;;
138 "simple_move") # move list arbitrarily
139 new_flfirst=$((fllast + 1))
140 agfl_size=$good_agfl_size;;
142 _fail "Internal test error";;
144 new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
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
155 new_flfirst: $new_flfirst
156 new_fllast: $new_fllast
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
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
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
179 echo "FS AFTER" >> $seqres.full
180 dump_ag0 > $tmp.corrupt 2> /dev/null
181 diff -u $tmp.before $tmp.corrupt >> $seqres.full
183 # Mount and see what happens
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
193 if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
194 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
196 if [ "${flfirst}" -ge "${fllast}" ]; then
197 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
200 echo "FS MOUNTLOOP" >> $seqres.full
201 dump_ag0 > $tmp.mountloop 2> /dev/null
202 diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
204 # Let's see what repair thinks
205 echo "REPAIR" >> $seqres.full
206 _scratch_xfs_repair >> $seqres.full 2>&1
208 echo "FS REPAIR" >> $seqres.full
209 dump_ag0 > $tmp.repair 2> /dev/null
210 diff -u $tmp.mountloop $tmp.repair >> $seqres.full
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
217 echo "FS REMOUNT" >> $seqres.full
218 dump_ag0 > $tmp.remount 2> /dev/null
219 diff -u $tmp.repair $tmp.remount >> $seqres.full
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}\"."
237 # Now run the regular dmesg check, filtering out the agfl warning
238 filter_agfl_reset_printk() {
239 grep -v "${warn_str}"
241 _check_dmesg filter_agfl_reset_printk