ea22c3ef08a335222bd4b4935b000d70892644a5
[xfstests-dev.git] / common / fuzzy
1 ##/bin/bash
2
3 # Routines for fuzzing and scrubbing a filesystem.
4 #
5 #-----------------------------------------------------------------------
6 #  Copyright (c) 2017 Oracle.  All Rights Reserved.
7 #  This program is free software; you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2 of the License, or
10 #  (at your option) any later version.
11 #
12 #  This program is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
20 #  USA
21 #-----------------------------------------------------------------------
22
23 # Modify various files after a fuzzing operation
24 _scratch_fuzz_modify() {
25         nr="$1"
26
27         test -z "${nr}" && nr=50000
28         echo "+++ touch ${nr} files"
29         blk_sz=$(stat -f -c '%s' ${SCRATCH_MNT})
30         $XFS_IO_PROG -f -c "pwrite -S 0x63 0 ${blk_sz}" "/tmp/afile" > /dev/null
31         date="$(date)"
32         find "${SCRATCH_MNT}/" -type f 2> /dev/null | head -n "${nr}" | while read f; do
33                 setfattr -n "user.date" -v "${date}" "$f"
34                 cat "/tmp/afile" >> "$f"
35                 mv "$f" "$f.longer"
36         done
37         sync
38         rm -rf "/tmp/afile"
39
40         echo "+++ create files"
41         mkdir -p "${SCRATCH_MNT}/test.moo"
42         $XFS_IO_PROG -f -c 'pwrite -S 0x80 0 65536' "${SCRATCH_MNT}/test.moo/urk" > /dev/null
43         sync
44
45         echo "+++ remove files"
46         rm -rf "${SCRATCH_MNT}/test.moo"
47 }
48
49 # Try to access files after fuzzing
50 _scratch_fuzz_test() {
51         echo "+++ ls -laR" >> $seqres.full
52         ls -laR "${SCRATCH_MNT}/test.1/" >/dev/null 2>&1
53
54         echo "+++ cat files" >> $seqres.full
55         (find "${SCRATCH_MNT}/test.1/" -type f -size -1048576k -print0 | xargs -0 cat) >/dev/null 2>&1
56 }
57
58 # Do we have an online scrub program?
59 _require_scrub() {
60         case "${FSTYP}" in
61         "xfs")
62                 test -x "$XFS_SCRUB_PROG" || _notrun "xfs_scrub not found"
63                 ;;
64         *)
65                 _notrun "No online scrub program for ${FSTYP}."
66                 ;;
67         esac
68 }
69
70 # Scrub the scratch filesystem metadata (online)
71 _scratch_scrub() {
72         case "${FSTYP}" in
73         "xfs")
74                 $XFS_SCRUB_PROG -d -T -v "$@" $SCRATCH_MNT
75                 ;;
76         *)
77                 _fail "No online scrub program for ${FSTYP}."
78                 ;;
79         esac
80 }
81
82 # Filter the xfs_db print command's field debug information
83 # into field name and type.
84 __filter_xfs_db_print_fields() {
85         filter="$1"
86         if [ -z "${filter}" ] || [ "${filter}" = "nofilter" ]; then
87                 filter='^'
88         fi
89         grep ' = ' | while read key equals value; do
90                 # Filter out any keys with an array index >= 10, and
91                 # collapse any array range ("[1-195]") to the first item.
92                 fuzzkey="$(echo "${key}" | sed -e '/\([a-z]*\)\[\([0-9][0-9]\+\)\].*/d' -e 's/\([a-zA-Z0-9_]*\)\[\([0-9]*\)-[0-9]*\]/\1[\2]/g')"
93                 if [ -z "${fuzzkey}" ]; then
94                         continue
95                 elif [[ "${value}" == "["* ]]; then
96                         echo "${value}" | sed -e 's/^.//g' -e 's/.$//g' -e 's/,/\n/g' | while read subfield; do
97                                 echo "${fuzzkey}.${subfield}"
98                         done
99                 else
100                         echo "${fuzzkey}"
101                 fi
102         done | egrep "${filter}"
103 }
104
105 # Navigate to some part of the filesystem and print the field info.
106 # The first argument is an egrep filter for the fields
107 # The rest of the arguments are xfs_db commands to locate the metadata.
108 _scratch_xfs_list_metadata_fields() {
109         filter="$1"
110         shift
111         if [ -n "${SCRATCH_XFS_LIST_METADATA_FIELDS}" ]; then
112                 echo "${SCRATCH_XFS_LIST_METADATA_FIELDS}" | tr '[ ,]' '[\n\n]'
113                 return;
114         fi
115
116         local cmds=()
117         for arg in "$@"; do
118                 cmds+=("-c" "${arg}")
119         done
120         _scratch_xfs_db "${cmds[@]}" -c print | __filter_xfs_db_print_fields "${filter}"
121 }
122
123 # Get a metadata field
124 # The first arg is the field name
125 # The rest of the arguments are xfs_db commands to find the metadata.
126 _scratch_xfs_get_metadata_field() {
127         key="$1"
128         shift
129
130         grep_key="$(echo "${key}" | tr '[]()' '....')"
131         local cmds=()
132         for arg in "$@"; do
133                 cmds+=("-c" "${arg}")
134         done
135         _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
136                 sed -e 's/^.* = //g'
137 }
138
139 # Set a metadata field
140 # The first arg is the field name
141 # The second arg is the new value
142 # The rest of the arguments are xfs_db commands to find the metadata.
143 _scratch_xfs_set_metadata_field() {
144         key="$1"
145         value="$2"
146         shift; shift
147
148         local cmds=()
149         for arg in "$@"; do
150                 cmds+=("-c" "${arg}")
151         done
152         _scratch_xfs_db -x "${cmds[@]}" -c "write -d ${key} ${value}"
153         echo
154 }
155
156 # Fuzz a metadata field
157 # The first arg is the field name
158 # The second arg is the xfs_db fuzz verb
159 # The rest of the arguments are xfs_db commands to find the metadata.
160 _scratch_xfs_fuzz_metadata_field() {
161         key="$1"
162         value="$2"
163         shift; shift
164
165         if [[ "${key}" == *crc ]]; then
166                 fuzz_arg="-c"
167         else
168                 fuzz_arg="-d"
169         fi
170         oldval="$(_scratch_xfs_get_metadata_field "${key}" "$@")"
171
172         local cmds=()
173         for arg in "$@"; do
174                 cmds+=("-c" "${arg}")
175         done
176         _scratch_xfs_db -x "${cmds[@]}" -c "fuzz ${fuzz_arg} ${key} ${value}"
177         echo
178         newval="$(_scratch_xfs_get_metadata_field "${key}" "$@" 2> /dev/null)"
179         if [ "${oldval}" = "${newval}" ]; then
180                 echo "Field ${key} already set to ${newval}, skipping test."
181                 return 1
182         fi
183         return 0
184 }
185
186 # Try to forcibly unmount the scratch fs
187 __scratch_xfs_fuzz_unmount()
188 {
189         while _scratch_unmount 2>/dev/null; do sleep 0.2; done
190 }
191
192 # Restore metadata to scratch device prior to field-fuzzing.
193 __scratch_xfs_fuzz_mdrestore()
194 {
195         test -e "${POPULATE_METADUMP}" || _fail "Need to set POPULATE_METADUMP"
196
197         __scratch_xfs_fuzz_unmount
198         xfs_mdrestore "${POPULATE_METADUMP}" "${SCRATCH_DEV}"
199 }
200
201 __fuzz_notify() {
202         echo "$@"
203         test -w /dev/ttyprintk && echo "$@" >> /dev/ttyprintk
204 }
205
206 # Fuzz one field of some piece of metadata.
207 # First arg is the field name
208 # Second arg is the fuzz verb (ones, zeroes, random, add, sub...)
209 # Third arg is the repair mode (online, offline, both)
210 __scratch_xfs_fuzz_field_test() {
211         field="$1"
212         fuzzverb="$2"
213         repair="$3"
214         shift; shift; shift
215
216         # Set the new field value
217         __fuzz_notify "+ Fuzz ${field} = ${fuzzverb}"
218         echo "========================"
219         _scratch_xfs_fuzz_metadata_field "${field}" ${fuzzverb} "$@"
220         res=$?
221         test $res -ne 0 && return
222
223         # Try to catch the error with scrub
224         echo "+ Try to catch the error"
225         _scratch_mount 2>&1
226         res=$?
227         if [ $res -eq 0 ]; then
228                 # Try an online scrub unless we're fuzzing ag 0's sb,
229                 # which scrub doesn't know how to fix.
230                 echo "++ Online scrub"
231                 if [ "$1" != "sb 0" ]; then
232                         _scratch_scrub -n -a 1 -e continue 2>&1
233                         res=$?
234                         test $res -eq 0 && \
235                                 (>&2 echo "scrub didn't fail with ${field} = ${fuzzverb}.")
236                 fi
237
238                 # Try fixing the filesystem online?!
239                 if [ "${repair}" = "online" ] || [ "${repair}" = "both" ]; then
240                         __fuzz_notify "++ Try to repair filesystem online"
241                         _scratch_scrub -y 2>&1
242                         res=$?
243                         test $res -ne 0 && \
244                                 (>&2 echo "online repair failed ($res) with ${field} = ${fuzzverb}.")
245                 fi
246
247                 __scratch_xfs_fuzz_unmount
248         elif [ "${repair}" = "online" ] || [ "${repair}" = "both" ]; then
249                 (>&2 echo "mount failed ($res) with ${field} = ${fuzzverb}.")
250         fi
251
252         # Repair the filesystem offline?
253         if [ "${repair}" = "offline" ] || [ "${repair}" = "both" ]; then
254                 echo "+ Try to repair the filesystem offline"
255                 _repair_scratch_fs 2>&1
256                 res=$?
257                 test $res -ne 0 && \
258                         (>&2 echo "offline repair failed ($res) with ${field} = ${fuzzverb}.")
259         fi
260
261         # See if repair finds a clean fs
262         echo "+ Make sure error is gone (offline)"
263         _scratch_xfs_repair -n 2>&1
264         res=$?
265         test $res -ne 0 && \
266                 (>&2 echo "offline re-scrub ($res) with ${field} = ${fuzzverb}.")
267
268         # See if scrub finds a clean fs
269         echo "+ Make sure error is gone (online)"
270         _scratch_mount 2>&1
271         res=$?
272         if [ $res -eq 0 ]; then
273                 # Try an online scrub unless we're fuzzing ag 0's sb,
274                 # which scrub doesn't know how to fix.
275                 echo "++ Online scrub"
276                 if [ "$1" != "sb 0" ]; then
277                         _scratch_scrub -e continue 2>&1
278                         res=$?
279                         test $res -ne 0 && \
280                                 (>&2 echo "online re-scrub ($res) with ${field} = ${fuzzverb}.")
281                 fi
282
283                 # Try modifying the filesystem again!
284                 __fuzz_notify "++ Try to write filesystem again"
285                 _scratch_fuzz_modify 100 2>&1
286                 __scratch_xfs_fuzz_unmount
287         else
288                 (>&2 echo "re-mount failed ($res) with ${field} = ${fuzzverb}.")
289         fi
290
291         # See if repair finds a clean fs
292         echo "+ Re-check the filesystem (offline)"
293         _scratch_xfs_repair -n 2>&1
294         res=$?
295         test $res -ne 0 && \
296                 (>&2 echo "re-repair failed ($res) with ${field} = ${fuzzverb}.")
297 }
298
299 # Make sure we have all the pieces we need for field fuzzing
300 _require_scratch_xfs_fuzz_fields()
301 {
302         _require_scratch_nocheck
303         _require_scrub
304         _require_populate_commands
305         _scratch_mkfs_xfs >/dev/null 2>&1
306         _require_xfs_db_command "fuzz"
307 }
308
309 # Grab the list of available fuzzing verbs
310 _scratch_xfs_list_fuzz_verbs() {
311         if [ -n "${SCRATCH_XFS_LIST_FUZZ_VERBS}" ]; then
312                 echo "${SCRATCH_XFS_LIST_FUZZ_VERBS}" | tr '[ ,]' '[\n\n]'
313                 return;
314         fi
315         _scratch_xfs_db -x -c 'sb 0' -c 'fuzz' | grep '^Verbs:' | \
316                 sed -e 's/[,.]//g' -e 's/Verbs: //g' -e 's/ /\n/g'
317 }
318
319 # Fuzz some of the fields of some piece of metadata
320 # The first argument is an egrep filter for the field names
321 # The second argument is the repair mode (online, offline, both)
322 # The rest of the arguments are xfs_db commands to locate the metadata.
323 #
324 # Users can specify the fuzz verbs via SCRATCH_XFS_LIST_FUZZ_VERBS
325 # They can specify the fields via SCRATCH_XFS_LIST_METADATA_FIELDS
326 _scratch_xfs_fuzz_metadata() {
327         filter="$1"
328         repair="$2"
329         shift; shift
330
331         fields="$(_scratch_xfs_list_metadata_fields "${filter}" "$@")"
332         verbs="$(_scratch_xfs_list_fuzz_verbs)"
333         echo "Fields we propose to fuzz under: $@"
334         echo $(echo "${fields}")
335         echo "Verbs we propose to fuzz with:"
336         echo $(echo "${verbs}")
337
338         echo "${fields}" | while read field; do
339                 echo "${verbs}" | while read fuzzverb; do
340                         __scratch_xfs_fuzz_mdrestore
341                         __scratch_xfs_fuzz_field_test "${field}" "${fuzzverb}" "${repair}" "$@"
342                 done
343         done
344 }