common/xfs: Add require_xfs_db_write_array function
[xfstests-dev.git] / tests / xfs / 444
1 #! /bin/bash
2 # FS QA Test No. 444
3 #
4 # Make sure XFS can fix a v5 AGFL that wraps over the last block.
5 # Refer to commit 96f859d52bcb ("libxfs: pack the agfl header structure so
6 # XFS_AGFL_SIZE is correct") for details on the original on-disk format error
7 # and the patch "xfs: detect agfl count corruption and reset agfl") for details
8 # about the fix.
9 #
10 #-----------------------------------------------------------------------
11 # Copyright (c) 2018 Oracle, Inc.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License as
15 # published by the Free Software Foundation.
16 #
17 # This program is distributed in the hope that it would be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write the Free Software Foundation,
24 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25 #
26 #-----------------------------------------------------------------------
27 #
28
29 seq=`basename $0`
30 seqres=$RESULT_DIR/$seq
31 echo "QA output created by $seq"
32
33 here=`pwd`
34 tmp=/tmp/$$
35 status=1
36 trap "_cleanup; rm -f $tmp.*; exit \$status" 0 1 2 3 15
37
38 _cleanup()
39 {
40         cd /
41         rm -f $tmp.*
42 }
43
44 rm -f $seqres.full
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter
49
50 # real QA test starts here
51 _supported_fs xfs
52 _supported_os Linux
53
54 _require_check_dmesg
55 _require_scratch
56 _require_test_program "punch-alternating"
57 _require_xfs_db_write_array
58
59 # This is only a v5 filesystem problem
60 _require_scratch_xfs_crc
61
62 mount_loop() {
63         if ! _try_scratch_mount >> $seqres.full 2>&1; then
64                 echo "scratch mount failed" >> $seqres.full
65                 return
66         fi
67
68         # Trigger agfl fixing by fragmenting free space enough to cause
69         # a bnobt split
70         blksz=$(_get_file_block_size ${SCRATCH_MNT})
71         bno_maxrecs=$(( blksz / 8 ))
72         filesz=$((bno_maxrecs * 3 * blksz))
73         rm -rf $SCRATCH_MNT/a
74         $XFS_IO_PROG -f -c "falloc 0 $filesz" $SCRATCH_MNT/a >> $seqres.full 2>&1
75         test -e $SCRATCH_MNT/a && ./src/punch-alternating $SCRATCH_MNT/a
76         rm -rf $SCRATCH_MNT/a
77
78         _scratch_unmount 2>&1 | _filter_scratch
79 }
80
81 dump_ag0() {
82         _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
83 }
84
85 runtest() {
86         cmd="$1"
87
88         # Format filesystem
89         echo "TEST $cmd" | tee /dev/ttyprintk
90         echo "TEST $cmd" >> $seqres.full
91         _scratch_mkfs >> $seqres.full
92
93         # Record what was here before
94         echo "FS BEFORE" >> $seqres.full
95         dump_ag0 > $tmp.before
96         cat $tmp.before >> $seqres.full
97
98         sectsize=$(_scratch_xfs_get_metadata_field "sectsize" "sb 0")
99         flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0")
100         fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0")
101         flcount=$(_scratch_xfs_get_metadata_field "flcount" "agf 0")
102
103         # Due to a padding bug in the original v5 struct xfs_agfl,
104         # XFS_AGFL_SIZE could be 36 on 32-bit or 40 on 64-bit.  On a system
105         # with 512b sectors, this means that the AGFL length could be
106         # ((512 - 36) / 4) = 119 entries on 32-bit or ((512 - 40) / 4) = 118
107         # entries on 64-bit.
108         #
109         # We now have code to figure out if the AGFL list wraps incorrectly
110         # according to the kernel's agfl size and fix it by resetting the agfl
111         # to zero length.  Mutate ag 0's agfl to be in various configurations
112         # and see if we can trigger the reset.
113         #
114         # Don't hardcode the numbers, calculate them.
115
116         # Have to have at least three agfl items to test full wrap
117         test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
118
119         # mkfs should be able to make us a nice neat flfirst < fllast setup
120         test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
121
122         bad_agfl_size=$(( (sectsize - 40) / 4 ))
123         good_agfl_size=$(( (sectsize - 36) / 4 ))
124         agfl_size=
125         case "$1" in
126         "fix_end")      # fllast points to the end w/ 40-byte padding
127                 new_flfirst=$(( bad_agfl_size - flcount ))
128                 agfl_size=$bad_agfl_size;;
129         "fix_start")    # flfirst points to the end w/ 40-byte padding
130                 new_flfirst=$(( bad_agfl_size - 1))
131                 agfl_size=$bad_agfl_size;;
132         "fix_wrap")     # list wraps around end w/ 40-byte padding
133                 new_flfirst=$(( bad_agfl_size - (flcount / 2) ))
134                 agfl_size=$bad_agfl_size;;
135         "start_zero")   # flfirst points to the start
136                 new_flfirst=0
137                 agfl_size=$good_agfl_size;;
138         "good_end")     # fllast points to the end w/ 36-byte padding
139                 new_flfirst=$(( good_agfl_size - flcount ))
140                 agfl_size=$good_agfl_size;;
141         "good_start")   # flfirst points to the end w/ 36-byte padding
142                 new_flfirst=$(( good_agfl_size - 1 ))
143                 agfl_size=$good_agfl_size;;
144         "good_wrap")    # list wraps around end w/ 36-byte padding
145                 new_flfirst=$(( good_agfl_size - (flcount / 2) ))
146                 agfl_size=$good_agfl_size;;
147         "bad_start")    # flfirst points off the end
148                 new_flfirst=$good_agfl_size
149                 agfl_size=$good_agfl_size;;
150         "no_move")      # whatever mkfs formats (flfirst points to start)
151                 new_flfirst=$flfirst
152                 agfl_size=$good_agfl_size;;
153         "simple_move")  # move list arbitrarily
154                 new_flfirst=$((fllast + 1))
155                 agfl_size=$good_agfl_size;;
156         *)
157                 _fail "Internal test error";;
158         esac
159         new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
160
161         # Log what we're doing...
162         cat >> $seqres.full << ENDL
163 sector size: $sectsize
164 bad_agfl_size: $bad_agfl_size [0 - $((bad_agfl_size - 1))]
165 good_agfl_size: $good_agfl_size [0 - $((good_agfl_size - 1))]
166 agfl_size: $agfl_size
167 flfirst: $flfirst
168 fllast: $fllast
169 flcount: $flcount
170 new_flfirst: $new_flfirst
171 new_fllast: $new_fllast
172 ENDL
173
174         # Remap the agfl blocks
175         echo "$((good_agfl_size - 1)) 0xffffffff" > $tmp.remap
176         seq "$flfirst" "$fllast" | while read f; do
177                 list_pos=$((f - flfirst))
178                 dest_pos=$(( (new_flfirst + list_pos) % agfl_size ))
179                 bno=$(_scratch_xfs_get_metadata_field "bno[$f]" "agfl 0")
180                 echo "$dest_pos $bno" >> $tmp.remap
181         done
182
183         cat $tmp.remap | while read dest_pos bno junk; do
184                 _scratch_xfs_set_metadata_field "bno[$dest_pos]" "$bno" \
185                                 "agfl 0" >> $seqres.full
186         done
187
188         # Set new flfirst/fllast
189         _scratch_xfs_set_metadata_field "fllast" "$new_fllast" \
190                         "agf 0" >> $seqres.full
191         _scratch_xfs_set_metadata_field "flfirst" "$new_flfirst" \
192                         "agf 0" >> $seqres.full
193
194         echo "FS AFTER" >> $seqres.full
195         dump_ag0 > $tmp.corrupt 2> /dev/null
196         diff -u $tmp.before $tmp.corrupt >> $seqres.full
197
198         # Mount and see what happens
199         mount_loop
200
201         # Did we end up with a non-wrapped list?
202         flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0" 2>/dev/null)
203         fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0" 2>/dev/null)
204         echo "flfirst=${flfirst} fllast=${fllast}" >> $seqres.full
205         if [ "${flfirst}" -ge "$((good_agfl_size - 1))" ]; then
206                 echo "ASSERT flfirst < good_agfl_size - 1" | tee -a $seqres.full
207         fi
208         if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
209                 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
210         fi
211         if [ "${flfirst}" -ge "${fllast}" ]; then
212                 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
213         fi
214
215         echo "FS MOUNTLOOP" >> $seqres.full
216         dump_ag0 > $tmp.mountloop 2> /dev/null
217         diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
218
219         # Let's see what repair thinks
220         echo "REPAIR" >> $seqres.full
221         _scratch_xfs_repair >> $seqres.full 2>&1
222
223         echo "FS REPAIR" >> $seqres.full
224         dump_ag0 > $tmp.repair 2> /dev/null
225         diff -u $tmp.mountloop $tmp.repair >> $seqres.full
226
227         # Exercise the filesystem again to make sure there aren't any lasting
228         # ill effects from either the agfl reset or the recommended subsequent
229         # repair run.
230         mount_loop
231
232         echo "FS REMOUNT" >> $seqres.full
233         dump_ag0 > $tmp.remount 2> /dev/null
234         diff -u $tmp.repair $tmp.remount >> $seqres.full
235 }
236
237 runtest fix_end
238 runtest fix_start
239 runtest fix_wrap
240 runtest start_zero
241 runtest good_end
242 runtest good_start
243 runtest good_wrap
244 runtest bad_start
245 runtest no_move
246 runtest simple_move
247
248 # Did we get the kernel warning too?
249 warn_str='WARNING: Reset corrupted AGFL'
250 _check_dmesg_for "${warn_str}" || echo "Missing dmesg string \"${warn_str}\"."
251
252 # Now run the regular dmesg check, filtering out the agfl warning
253 filter_agfl_reset_printk() {
254         grep -v "${warn_str}"
255 }
256 _check_dmesg filter_agfl_reset_printk
257
258 status=0
259 exit