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 _begin_fstest auto quick
16 _register_cleanup "_cleanup; rm -f $tmp.*"
18 # Import common functions.
21 # real QA test starts here
26 _require_test_program "punch-alternating"
27 _require_xfs_io_command "falloc"
28 _require_xfs_db_write_array
30 # This is only a v5 filesystem problem
31 _require_scratch_xfs_crc
33 # Disable the scratch rt device to avoid test failures relating to the rt
34 # bitmap consuming free space in our small data device and throwing off the
35 # filestreams allocator.
39 if ! _try_scratch_mount >> $seqres.full 2>&1; then
40 echo "scratch mount failed" >> $seqres.full
44 # Trigger agfl fixing by fragmenting free space enough to cause
46 blksz=$(_get_file_block_size ${SCRATCH_MNT})
47 bno_maxrecs=$(( blksz / 8 ))
48 filesz=$((bno_maxrecs * 3 * blksz))
50 $XFS_IO_PROG -f -c "falloc 0 $filesz" $SCRATCH_MNT/a >> $seqres.full 2>&1
51 test -e $SCRATCH_MNT/a && $here/src/punch-alternating $SCRATCH_MNT/a
54 _scratch_unmount 2>&1 | _filter_scratch
58 _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
65 echo "TEST $cmd" | tee /dev/ttyprintk
66 echo "TEST $cmd" >> $seqres.full
67 _scratch_mkfs >> $seqres.full
69 # Record what was here before
70 echo "FS BEFORE" >> $seqres.full
71 dump_ag0 > $tmp.before
72 cat $tmp.before >> $seqres.full
74 sectsize=$(_scratch_xfs_get_metadata_field "sectsize" "sb 0")
75 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0")
76 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0")
77 flcount=$(_scratch_xfs_get_metadata_field "flcount" "agf 0")
79 # Due to a padding bug in the original v5 struct xfs_agfl,
80 # XFS_AGFL_SIZE could be 36 on 32-bit or 40 on 64-bit. On a system
81 # with 512b sectors, this means that the AGFL length could be
82 # ((512 - 36) / 4) = 119 entries on 32-bit or ((512 - 40) / 4) = 118
85 # We now have code to figure out if the AGFL list wraps incorrectly
86 # according to the kernel's agfl size and fix it by resetting the agfl
87 # to zero length. Mutate ag 0's agfl to be in various configurations
88 # and see if we can trigger the reset.
90 # Don't hardcode the numbers, calculate them.
92 # Have to have at least three agfl items to test full wrap
93 test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
95 # mkfs should be able to make us a nice neat flfirst < fllast setup
96 test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
98 bad_agfl_size=$(( (sectsize - 40) / 4 ))
99 good_agfl_size=$(( (sectsize - 36) / 4 ))
102 "fix_end") # fllast points to the end w/ 40-byte padding
103 new_flfirst=$(( bad_agfl_size - flcount ))
104 agfl_size=$bad_agfl_size;;
105 "fix_start") # flfirst points to the end w/ 40-byte padding
106 new_flfirst=$(( bad_agfl_size - 1))
107 agfl_size=$bad_agfl_size;;
108 "fix_wrap") # list wraps around end w/ 40-byte padding
109 new_flfirst=$(( bad_agfl_size - (flcount / 2) ))
110 agfl_size=$bad_agfl_size;;
111 "start_zero") # flfirst points to the start
113 agfl_size=$good_agfl_size;;
114 "good_end") # fllast points to the end w/ 36-byte padding
115 new_flfirst=$(( good_agfl_size - flcount ))
116 agfl_size=$good_agfl_size;;
117 "good_start") # flfirst points to the end w/ 36-byte padding
118 new_flfirst=$(( good_agfl_size - 1 ))
119 agfl_size=$good_agfl_size;;
120 "good_wrap") # list wraps around end w/ 36-byte padding
121 new_flfirst=$(( good_agfl_size - (flcount / 2) ))
122 agfl_size=$good_agfl_size;;
123 "bad_start") # flfirst points off the end
124 new_flfirst=$good_agfl_size
125 agfl_size=$good_agfl_size;;
126 "no_move") # whatever mkfs formats (flfirst points to start)
128 agfl_size=$good_agfl_size;;
129 "simple_move") # move list arbitrarily
130 new_flfirst=$((fllast + 1))
131 agfl_size=$good_agfl_size;;
133 _fail "Internal test error";;
135 new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
137 # Log what we're doing...
138 cat >> $seqres.full << ENDL
139 sector size: $sectsize
140 bad_agfl_size: $bad_agfl_size [0 - $((bad_agfl_size - 1))]
141 good_agfl_size: $good_agfl_size [0 - $((good_agfl_size - 1))]
142 agfl_size: $agfl_size
146 new_flfirst: $new_flfirst
147 new_fllast: $new_fllast
150 # Remap the agfl blocks
151 echo "$((good_agfl_size - 1)) 0xffffffff" > $tmp.remap
152 seq "$flfirst" "$fllast" | while read f; do
153 list_pos=$((f - flfirst))
154 dest_pos=$(( (new_flfirst + list_pos) % agfl_size ))
155 bno=$(_scratch_xfs_get_metadata_field "bno[$f]" "agfl 0")
156 echo "$dest_pos $bno" >> $tmp.remap
159 cat $tmp.remap | while read dest_pos bno junk; do
160 _scratch_xfs_set_metadata_field "bno[$dest_pos]" "$bno" \
161 "agfl 0" >> $seqres.full
164 # Set new flfirst/fllast
165 _scratch_xfs_set_metadata_field "fllast" "$new_fllast" \
166 "agf 0" >> $seqres.full
167 _scratch_xfs_set_metadata_field "flfirst" "$new_flfirst" \
168 "agf 0" >> $seqres.full
170 echo "FS AFTER" >> $seqres.full
171 dump_ag0 > $tmp.corrupt 2> /dev/null
172 diff -u $tmp.before $tmp.corrupt >> $seqres.full
174 # Mount and see what happens
177 # Did we end up with a non-wrapped list?
178 flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0" 2>/dev/null)
179 fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0" 2>/dev/null)
180 echo "flfirst=${flfirst} fllast=${fllast}" >> $seqres.full
181 if [ "${flfirst}" -ge "$((good_agfl_size - 1))" ]; then
182 echo "ASSERT flfirst < good_agfl_size - 1" | tee -a $seqres.full
184 if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
185 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
187 if [ "${flfirst}" -ge "${fllast}" ]; then
188 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
191 echo "FS MOUNTLOOP" >> $seqres.full
192 dump_ag0 > $tmp.mountloop 2> /dev/null
193 diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
195 # Let's see what repair thinks
196 echo "REPAIR" >> $seqres.full
197 _scratch_xfs_repair >> $seqres.full 2>&1
199 echo "FS REPAIR" >> $seqres.full
200 dump_ag0 > $tmp.repair 2> /dev/null
201 diff -u $tmp.mountloop $tmp.repair >> $seqres.full
203 # Exercise the filesystem again to make sure there aren't any lasting
204 # ill effects from either the agfl reset or the recommended subsequent
208 echo "FS REMOUNT" >> $seqres.full
209 dump_ag0 > $tmp.remount 2> /dev/null
210 diff -u $tmp.repair $tmp.remount >> $seqres.full
224 # Did we get the kernel warning too?
225 warn_str='WARNING: Reset corrupted AGFL'
226 _check_dmesg_for "${warn_str}" || echo "Missing dmesg string \"${warn_str}\"."
228 # Now run the regular dmesg check, filtering out the agfl warning
229 filter_agfl_reset_printk() {
230 grep -v "${warn_str}"
232 _check_dmesg filter_agfl_reset_printk