3 # Routines for fuzzing and scrubbing a filesystem.
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.
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.
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
21 #-----------------------------------------------------------------------
23 # Modify various files after a fuzzing operation
24 _scratch_fuzz_modify() {
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
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"
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"
45 echo "+++ remove files"
46 rm -rf "${SCRATCH_MNT}/test.moo"
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
54 echo "+++ cat files" >> $seqres.full
55 (find "${SCRATCH_MNT}/test.1/" -type f -size -1048576k -print0 | xargs -0 cat) >/dev/null 2>&1
58 # Do we have an online scrub program?
62 test -x "$XFS_SCRUB_PROG" || _notrun "xfs_scrub not found"
65 _notrun "No online scrub program for ${FSTYP}."
70 # Scrub the scratch filesystem metadata (online)
73 "xfs"|"ext4"|"ext3"|"ext2")
74 $XFS_SCRUB_PROG -d -T -v "$@" $SCRATCH_MNT
77 _fail "No online scrub program for ${FSTYP}."
82 # Filter the xfs_db print command's field debug information
83 # into field name and type.
84 __filter_xfs_db_print_fields() {
86 if [ -z "${filter}" ] || [ "${filter}" = "nofilter" ]; then
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
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}"
102 done | egrep "${filter}"
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() {
111 if [ -n "${SCRATCH_XFS_LIST_METADATA_FIELDS}" ]; then
112 echo "${SCRATCH_XFS_LIST_METADATA_FIELDS}" | tr '[ ,]' '[\n\n]'
118 cmds+=("-c" "${arg}")
120 _scratch_xfs_db "${cmds[@]}" -c print | __filter_xfs_db_print_fields "${filter}"
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() {
130 grep_key="$(echo "${key}" | tr '[]()' '....')"
133 cmds+=("-c" "${arg}")
135 _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
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() {
150 cmds+=("-c" "${arg}")
152 _scratch_xfs_db -x "${cmds[@]}" -c "write -d ${key} ${value}"
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() {
165 if [[ "${key}" == *crc ]]; then
170 oldval="$(_scratch_xfs_get_metadata_field "${key}" "$@")"
174 cmds+=("-c" "${arg}")
176 _scratch_xfs_db -x "${cmds[@]}" -c "fuzz ${fuzz_arg} ${key} ${value}"
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."
186 # Try to forcibly unmount the scratch fs
187 __scratch_xfs_fuzz_unmount()
189 while _scratch_unmount 2>/dev/null; do sleep 0.2; done
192 # Restore metadata to scratch device prior to field-fuzzing.
193 __scratch_xfs_fuzz_mdrestore()
195 test -e "${POPULATE_METADUMP}" || _fail "Need to set POPULATE_METADUMP"
197 __scratch_xfs_fuzz_unmount
198 xfs_mdrestore "${POPULATE_METADUMP}" "${SCRATCH_DEV}"
203 test -w /dev/ttyprintk && echo "$@" >> /dev/ttyprintk
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() {
216 # Set the new field value
217 __fuzz_notify "+ Fuzz ${field} = ${fuzzverb}"
218 echo "========================"
219 _scratch_xfs_fuzz_metadata_field "${field}" ${fuzzverb} "$@"
221 test $res -ne 0 && return
223 # Try to catch the error with scrub
224 echo "+ Try to catch the error"
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
235 (>&2 echo "scrub didn't fail with ${field} = ${fuzzverb}.")
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
244 (>&2 echo "online repair failed ($res) with ${field} = ${fuzzverb}.")
247 __scratch_xfs_fuzz_unmount
248 elif [ "${repair}" = "online" ] || [ "${repair}" = "both" ]; then
249 (>&2 echo "mount failed ($res) with ${field} = ${fuzzverb}.")
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
258 (>&2 echo "offline repair failed ($res) with ${field} = ${fuzzverb}.")
261 # See if repair finds a clean fs
262 echo "+ Make sure error is gone (offline)"
263 _scratch_xfs_repair -n 2>&1
266 (>&2 echo "offline re-scrub ($res) with ${field} = ${fuzzverb}.")
268 # See if scrub finds a clean fs
269 echo "+ Make sure error is gone (online)"
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
280 (>&2 echo "online re-scrub ($res) with ${field} = ${fuzzverb}.")
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
288 (>&2 echo "re-mount failed ($res) with ${field} = ${fuzzverb}.")
291 # See if repair finds a clean fs
292 echo "+ Re-check the filesystem (offline)"
293 _scratch_xfs_repair -n 2>&1
296 (>&2 echo "re-repair failed ($res) with ${field} = ${fuzzverb}.")
299 # Make sure we have all the pieces we need for field fuzzing
300 _require_scratch_xfs_fuzz_fields()
302 _require_scratch_nocheck
304 _require_populate_commands
305 _scratch_mkfs_xfs >/dev/null 2>&1
306 _require_xfs_db_command "fuzz"
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]'
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'
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.
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() {
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}")
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}" "$@"