common/rc: add _scratch_{u}mount_idmapped() helpers
[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
37 _require_check_dmesg
38 _require_scratch
39 _require_test_program "punch-alternating"
40 _require_xfs_io_command "falloc"
41 _require_xfs_db_write_array
42
43 # This is only a v5 filesystem problem
44 _require_scratch_xfs_crc
45
46 # Disable the scratch rt device to avoid test failures relating to the rt
47 # bitmap consuming free space in our small data device and throwing off the
48 # filestreams allocator.
49 unset SCRATCH_RTDEV
50
51 mount_loop() {
52         if ! _try_scratch_mount >> $seqres.full 2>&1; then
53                 echo "scratch mount failed" >> $seqres.full
54                 return
55         fi
56
57         # Trigger agfl fixing by fragmenting free space enough to cause
58         # a bnobt split
59         blksz=$(_get_file_block_size ${SCRATCH_MNT})
60         bno_maxrecs=$(( blksz / 8 ))
61         filesz=$((bno_maxrecs * 3 * blksz))
62         rm -rf $SCRATCH_MNT/a
63         $XFS_IO_PROG -f -c "falloc 0 $filesz" $SCRATCH_MNT/a >> $seqres.full 2>&1
64         test -e $SCRATCH_MNT/a && $here/src/punch-alternating $SCRATCH_MNT/a
65         rm -rf $SCRATCH_MNT/a
66
67         _scratch_unmount 2>&1 | _filter_scratch
68 }
69
70 dump_ag0() {
71         _scratch_xfs_db -c 'sb 0' -c 'p' -c 'agf 0' -c 'p' -c 'agfl 0' -c 'p'
72 }
73
74 runtest() {
75         cmd="$1"
76
77         # Format filesystem
78         echo "TEST $cmd" | tee /dev/ttyprintk
79         echo "TEST $cmd" >> $seqres.full
80         _scratch_mkfs >> $seqres.full
81
82         # Record what was here before
83         echo "FS BEFORE" >> $seqres.full
84         dump_ag0 > $tmp.before
85         cat $tmp.before >> $seqres.full
86
87         sectsize=$(_scratch_xfs_get_metadata_field "sectsize" "sb 0")
88         flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0")
89         fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0")
90         flcount=$(_scratch_xfs_get_metadata_field "flcount" "agf 0")
91
92         # Due to a padding bug in the original v5 struct xfs_agfl,
93         # XFS_AGFL_SIZE could be 36 on 32-bit or 40 on 64-bit.  On a system
94         # with 512b sectors, this means that the AGFL length could be
95         # ((512 - 36) / 4) = 119 entries on 32-bit or ((512 - 40) / 4) = 118
96         # entries on 64-bit.
97         #
98         # We now have code to figure out if the AGFL list wraps incorrectly
99         # according to the kernel's agfl size and fix it by resetting the agfl
100         # to zero length.  Mutate ag 0's agfl to be in various configurations
101         # and see if we can trigger the reset.
102         #
103         # Don't hardcode the numbers, calculate them.
104
105         # Have to have at least three agfl items to test full wrap
106         test "$flcount" -ge 3 || _notrun "insufficient agfl flcount"
107
108         # mkfs should be able to make us a nice neat flfirst < fllast setup
109         test "$flfirst" -lt "$fllast" || _notrun "fresh agfl already wrapped?"
110
111         bad_agfl_size=$(( (sectsize - 40) / 4 ))
112         good_agfl_size=$(( (sectsize - 36) / 4 ))
113         agfl_size=
114         case "$1" in
115         "fix_end")      # fllast points to the end w/ 40-byte padding
116                 new_flfirst=$(( bad_agfl_size - flcount ))
117                 agfl_size=$bad_agfl_size;;
118         "fix_start")    # flfirst points to the end w/ 40-byte padding
119                 new_flfirst=$(( bad_agfl_size - 1))
120                 agfl_size=$bad_agfl_size;;
121         "fix_wrap")     # list wraps around end w/ 40-byte padding
122                 new_flfirst=$(( bad_agfl_size - (flcount / 2) ))
123                 agfl_size=$bad_agfl_size;;
124         "start_zero")   # flfirst points to the start
125                 new_flfirst=0
126                 agfl_size=$good_agfl_size;;
127         "good_end")     # fllast points to the end w/ 36-byte padding
128                 new_flfirst=$(( good_agfl_size - flcount ))
129                 agfl_size=$good_agfl_size;;
130         "good_start")   # flfirst points to the end w/ 36-byte padding
131                 new_flfirst=$(( good_agfl_size - 1 ))
132                 agfl_size=$good_agfl_size;;
133         "good_wrap")    # list wraps around end w/ 36-byte padding
134                 new_flfirst=$(( good_agfl_size - (flcount / 2) ))
135                 agfl_size=$good_agfl_size;;
136         "bad_start")    # flfirst points off the end
137                 new_flfirst=$good_agfl_size
138                 agfl_size=$good_agfl_size;;
139         "no_move")      # whatever mkfs formats (flfirst points to start)
140                 new_flfirst=$flfirst
141                 agfl_size=$good_agfl_size;;
142         "simple_move")  # move list arbitrarily
143                 new_flfirst=$((fllast + 1))
144                 agfl_size=$good_agfl_size;;
145         *)
146                 _fail "Internal test error";;
147         esac
148         new_fllast=$(( (new_flfirst + flcount - 1) % agfl_size ))
149
150         # Log what we're doing...
151         cat >> $seqres.full << ENDL
152 sector size: $sectsize
153 bad_agfl_size: $bad_agfl_size [0 - $((bad_agfl_size - 1))]
154 good_agfl_size: $good_agfl_size [0 - $((good_agfl_size - 1))]
155 agfl_size: $agfl_size
156 flfirst: $flfirst
157 fllast: $fllast
158 flcount: $flcount
159 new_flfirst: $new_flfirst
160 new_fllast: $new_fllast
161 ENDL
162
163         # Remap the agfl blocks
164         echo "$((good_agfl_size - 1)) 0xffffffff" > $tmp.remap
165         seq "$flfirst" "$fllast" | while read f; do
166                 list_pos=$((f - flfirst))
167                 dest_pos=$(( (new_flfirst + list_pos) % agfl_size ))
168                 bno=$(_scratch_xfs_get_metadata_field "bno[$f]" "agfl 0")
169                 echo "$dest_pos $bno" >> $tmp.remap
170         done
171
172         cat $tmp.remap | while read dest_pos bno junk; do
173                 _scratch_xfs_set_metadata_field "bno[$dest_pos]" "$bno" \
174                                 "agfl 0" >> $seqres.full
175         done
176
177         # Set new flfirst/fllast
178         _scratch_xfs_set_metadata_field "fllast" "$new_fllast" \
179                         "agf 0" >> $seqres.full
180         _scratch_xfs_set_metadata_field "flfirst" "$new_flfirst" \
181                         "agf 0" >> $seqres.full
182
183         echo "FS AFTER" >> $seqres.full
184         dump_ag0 > $tmp.corrupt 2> /dev/null
185         diff -u $tmp.before $tmp.corrupt >> $seqres.full
186
187         # Mount and see what happens
188         mount_loop
189
190         # Did we end up with a non-wrapped list?
191         flfirst=$(_scratch_xfs_get_metadata_field "flfirst" "agf 0" 2>/dev/null)
192         fllast=$(_scratch_xfs_get_metadata_field "fllast" "agf 0" 2>/dev/null)
193         echo "flfirst=${flfirst} fllast=${fllast}" >> $seqres.full
194         if [ "${flfirst}" -ge "$((good_agfl_size - 1))" ]; then
195                 echo "ASSERT flfirst < good_agfl_size - 1" | tee -a $seqres.full
196         fi
197         if [ "${fllast}" -ge "$((good_agfl_size - 1))" ]; then
198                 echo "ASSERT fllast < good_agfl_size - 1" | tee -a $seqres.full
199         fi
200         if [ "${flfirst}" -ge "${fllast}" ]; then
201                 echo "ASSERT flfirst < fllast" | tee -a $seqres.full
202         fi
203
204         echo "FS MOUNTLOOP" >> $seqres.full
205         dump_ag0 > $tmp.mountloop 2> /dev/null
206         diff -u $tmp.corrupt $tmp.mountloop >> $seqres.full
207
208         # Let's see what repair thinks
209         echo "REPAIR" >> $seqres.full
210         _scratch_xfs_repair >> $seqres.full 2>&1
211
212         echo "FS REPAIR" >> $seqres.full
213         dump_ag0 > $tmp.repair 2> /dev/null
214         diff -u $tmp.mountloop $tmp.repair >> $seqres.full
215
216         # Exercise the filesystem again to make sure there aren't any lasting
217         # ill effects from either the agfl reset or the recommended subsequent
218         # repair run.
219         mount_loop
220
221         echo "FS REMOUNT" >> $seqres.full
222         dump_ag0 > $tmp.remount 2> /dev/null
223         diff -u $tmp.repair $tmp.remount >> $seqres.full
224 }
225
226 runtest fix_end
227 runtest fix_start
228 runtest fix_wrap
229 runtest start_zero
230 runtest good_end
231 runtest good_start
232 runtest good_wrap
233 runtest bad_start
234 runtest no_move
235 runtest simple_move
236
237 # Did we get the kernel warning too?
238 warn_str='WARNING: Reset corrupted AGFL'
239 _check_dmesg_for "${warn_str}" || echo "Missing dmesg string \"${warn_str}\"."
240
241 # Now run the regular dmesg check, filtering out the agfl warning
242 filter_agfl_reset_printk() {
243         grep -v "${warn_str}"
244 }
245 _check_dmesg filter_agfl_reset_printk
246
247 status=0
248 exit