xfs: convert tests to SPDX license tags
[xfstests-dev.git] / tests / xfs / 444
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Oracle, Inc.
4 #
5 # FS QA Test No. 444
6 #
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
11 # about the fix.
12 #
13 seq=`basename $0`
14 seqres=$RESULT_DIR/$seq
15 echo "QA output created by $seq"
16
17 here=`pwd`
18 tmp=/tmp/$$
19 status=1
20 trap "_cleanup; rm -f $tmp.*; exit \$status" 0 1 2 3 15
21
22 _cleanup()
23 {
24         cd /
25         rm -f $tmp.*
26 }
27
28 rm -f $seqres.full
29
30 # get standard environment, filters and checks
31 . ./common/rc
32 . ./common/filter
33
34 # real QA test starts here
35 _supported_fs xfs
36 _supported_os Linux
37
38 _require_check_dmesg
39 _require_scratch
40 _require_test_program "punch-alternating"
41 _require_xfs_db_write_array
42
43 # This is only a v5 filesystem problem
44 _require_scratch_xfs_crc
45
46 mount_loop() {
47         if ! _try_scratch_mount >> $seqres.full 2>&1; then
48                 echo "scratch mount failed" >> $seqres.full
49                 return
50         fi
51
52         # Trigger agfl fixing by fragmenting free space enough to cause
53         # a bnobt split
54         blksz=$(_get_file_block_size ${SCRATCH_MNT})
55         bno_maxrecs=$(( blksz / 8 ))
56         filesz=$((bno_maxrecs * 3 * blksz))
57         rm -rf $SCRATCH_MNT/a
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
60         rm -rf $SCRATCH_MNT/a
61
62         _scratch_unmount 2>&1 | _filter_scratch
63 }
64
65 dump_ag0() {
66         _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
67 }
68
69 runtest() {
70         cmd="$1"
71
72         # Format filesystem
73         echo "TEST $cmd" | tee /dev/ttyprintk
74         echo "TEST $cmd" >> $seqres.full
75         _scratch_mkfs >> $seqres.full
76
77         # Record what was here before
78         echo "FS BEFORE" >> $seqres.full
79         dump_ag0 > $tmp.before
80         cat $tmp.before >> $seqres.full
81
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")
86
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
91         # entries on 64-bit.
92         #
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.
97         #
98         # Don't hardcode the numbers, calculate them.
99
100         # Have to have at least three agfl items to test full wrap
101         test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
102
103         # mkfs should be able to make us a nice neat flfirst < fllast setup
104         test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
105
106         bad_agfl_size=$(( (sectsize - 40) / 4 ))
107         good_agfl_size=$(( (sectsize - 36) / 4 ))
108         agfl_size=
109         case "$1" in
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
120                 new_flfirst=0
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)
135                 new_flfirst=$flfirst
136                 agfl_size=$good_agfl_size;;
137         "simple_move")  # move list arbitrarily
138                 new_flfirst=$((fllast + 1))
139                 agfl_size=$good_agfl_size;;
140         *)
141                 _fail "Internal test error";;
142         esac
143         new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
144
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
151 flfirst: $flfirst
152 fllast: $fllast
153 flcount: $flcount
154 new_flfirst: $new_flfirst
155 new_fllast: $new_fllast
156 ENDL
157
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
165         done
166
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
170         done
171
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
177
178         echo "FS AFTER" >> $seqres.full
179         dump_ag0 > $tmp.corrupt 2> /dev/null
180         diff -u $tmp.before $tmp.corrupt >> $seqres.full
181
182         # Mount and see what happens
183         mount_loop
184
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
191         fi
192         if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
193                 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
194         fi
195         if [ "${flfirst}" -ge "${fllast}" ]; then
196                 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
197         fi
198
199         echo "FS MOUNTLOOP" >> $seqres.full
200         dump_ag0 > $tmp.mountloop 2> /dev/null
201         diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
202
203         # Let's see what repair thinks
204         echo "REPAIR" >> $seqres.full
205         _scratch_xfs_repair >> $seqres.full 2>&1
206
207         echo "FS REPAIR" >> $seqres.full
208         dump_ag0 > $tmp.repair 2> /dev/null
209         diff -u $tmp.mountloop $tmp.repair >> $seqres.full
210
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
213         # repair run.
214         mount_loop
215
216         echo "FS REMOUNT" >> $seqres.full
217         dump_ag0 > $tmp.remount 2> /dev/null
218         diff -u $tmp.repair $tmp.remount >> $seqres.full
219 }
220
221 runtest fix_end
222 runtest fix_start
223 runtest fix_wrap
224 runtest start_zero
225 runtest good_end
226 runtest good_start
227 runtest good_wrap
228 runtest bad_start
229 runtest no_move
230 runtest simple_move
231
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}\"."
235
236 # Now run the regular dmesg check, filtering out the agfl warning
237 filter_agfl_reset_printk() {
238         grep -v "${warn_str}"
239 }
240 _check_dmesg filter_agfl_reset_printk
241
242 status=0
243 exit