generic: test MADV_POPULATE_READ with IO errors
[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 . ./common/preamble
14 _begin_fstest auto quick prealloc
15
16 _register_cleanup "_cleanup; rm -f $tmp.*"
17
18 # Import common functions.
19 . ./common/filter
20
21 # real QA test starts here
22 _supported_fs xfs
23
24 _require_check_dmesg
25 _require_scratch
26 _require_test_program "punch-alternating"
27 _require_xfs_io_command "falloc"
28 _require_xfs_db_write_array
29
30 # This is only a v5 filesystem problem
31 _require_scratch_xfs_crc
32
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.
36 unset SCRATCH_RTDEV
37
38 mount_loop() {
39         if ! _try_scratch_mount >> $seqres.full 2>&1; then
40                 echo "scratch mount failed" >> $seqres.full
41                 return
42         fi
43
44         # Trigger agfl fixing by fragmenting free space enough to cause
45         # a bnobt split
46         blksz=$(_get_file_block_size ${SCRATCH_MNT})
47         bno_maxrecs=$(( blksz / 8 ))
48         filesz=$((bno_maxrecs * 3 * blksz))
49         rm -rf $SCRATCH_MNT/a
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
52         rm -rf $SCRATCH_MNT/a
53
54         _scratch_unmount 2>&1 | _filter_scratch
55 }
56
57 dump_ag0() {
58         _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
59 }
60
61 runtest() {
62         cmd="$1"
63
64         # Format filesystem
65         echo "TEST $cmd" | _tee_kernlog
66         echo "TEST $cmd" >> $seqres.full
67         _scratch_mkfs >> $seqres.full
68
69         # Record what was here before
70         echo "FS BEFORE" >> $seqres.full
71         dump_ag0 > $tmp.before
72         cat $tmp.before >> $seqres.full
73
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")
78
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
83         # entries on 64-bit.
84         #
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.
89         #
90         # Don't hardcode the numbers, calculate them.
91
92         # Have to have at least three agfl items to test full wrap
93         test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
94
95         # mkfs should be able to make us a nice neat flfirst < fllast setup
96         test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
97
98         bad_agfl_size=$(( (sectsize - 40) / 4 ))
99         good_agfl_size=$(( (sectsize - 36) / 4 ))
100         agfl_size=
101         case "$1" in
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
112                 new_flfirst=0
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)
127                 new_flfirst=$flfirst
128                 agfl_size=$good_agfl_size;;
129         "simple_move")  # move list arbitrarily
130                 new_flfirst=$((fllast + 1))
131                 agfl_size=$good_agfl_size;;
132         *)
133                 _fail "Internal test error";;
134         esac
135         new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
136
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
143 flfirst: $flfirst
144 fllast: $fllast
145 flcount: $flcount
146 new_flfirst: $new_flfirst
147 new_fllast: $new_fllast
148 ENDL
149
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
157         done
158
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
162         done
163
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
169
170         echo "FS AFTER" >> $seqres.full
171         dump_ag0 > $tmp.corrupt 2> /dev/null
172         diff -u $tmp.before $tmp.corrupt >> $seqres.full
173
174         # Mount and see what happens
175         mount_loop
176
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
183         fi
184         if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
185                 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
186         fi
187         if [ "${flfirst}" -ge "${fllast}" ]; then
188                 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
189         fi
190
191         echo "FS MOUNTLOOP" >> $seqres.full
192         dump_ag0 > $tmp.mountloop 2> /dev/null
193         diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
194
195         # Let's see what repair thinks
196         echo "REPAIR" >> $seqres.full
197         _scratch_xfs_repair >> $seqres.full 2>&1
198
199         echo "FS REPAIR" >> $seqres.full
200         dump_ag0 > $tmp.repair 2> /dev/null
201         diff -u $tmp.mountloop $tmp.repair >> $seqres.full
202
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
205         # repair run.
206         mount_loop
207
208         echo "FS REMOUNT" >> $seqres.full
209         dump_ag0 > $tmp.remount 2> /dev/null
210         diff -u $tmp.repair $tmp.remount >> $seqres.full
211 }
212
213 runtest fix_end
214 runtest fix_start
215 runtest fix_wrap
216 runtest start_zero
217 runtest good_end
218 runtest good_start
219 runtest good_wrap
220 runtest bad_start
221 runtest no_move
222 runtest simple_move
223
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}\"."
227
228 # Now run the regular dmesg check, filtering out the agfl warning
229 filter_agfl_reset_printk() {
230         grep -v "${warn_str}"
231 }
232 _check_dmesg filter_agfl_reset_printk
233
234 status=0
235 exit