2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2016 Oracle. All Rights Reserved.
5 # Routines for injecting errors into filesystems
9 # Tests whether $FSTYP is one of the filesystems that supports error injection
10 _require_error_injection()
14 grep -q 'debug 1' /proc/fs/xfs/stat || \
15 _notrun "XFS error injection requires CONFIG_XFS_DEBUG"
18 _notrun "Error injection not supported on filesystem type: $FSTYP"
22 # Find the errortag injection knob in sysfs for a given xfs mount's
24 _find_xfs_mountdev_errortag_knob()
28 shortdev="$(_short_dev "${dev}")"
29 tagfile="/sys/fs/xfs/${shortdev}/errortag/${knob}"
31 # Some of the new sysfs errortag knobs were previously available via
35 if [ ! -w "${tagfile}" ]; then
36 tagfile="/sys/fs/xfs/${shortdev}/log/log_badcrc_factor"
40 if [ ! -w "${tagfile}" ]; then
41 tagfile="/sys/fs/xfs/${shortdev}/drop_writes"
43 if [ ! -w "${tagfile}" ]; then
44 tagfile="/sys/fs/xfs/${shortdev}/fail_writes"
54 # Requires that xfs_io inject command knows about this error type
55 _require_xfs_io_error_injection()
58 _require_error_injection
60 # Can we find the error injection knobs via the new errortag
61 # configuration mechanism?
62 knob="$(_find_xfs_mountdev_errortag_knob "${TEST_DEV}" "${type}")"
63 test -w "${knob}" && return
65 # If the directory containing the sysfs error injection knob exists
66 # but the knob itself isn't usable, this kernel doesn't know about
67 # the knob. Skip the test.
68 if [ -d "$(dirname "${knob}")" ]; then
69 _notrun "XFS error injection $type unknown on this kernel."
72 # NOTE: We can't actually test error injection here because xfs
73 # hasn't always range checked the argument to xfs_errortag_add.
74 # We also don't want to trip an error before we're ready to deal
77 $XFS_IO_PROG -x -c 'inject' $TEST_DIR | grep -q "$type" || \
78 _notrun "XFS error injection $type unknown."
81 # Inject an error into the test fs
87 knob="$(_find_xfs_mountdev_errortag_knob "${TEST_DEV}" "${type}")"
88 if [ -w "${knob}" ]; then
89 test -z "${value}" && value="default"
90 echo -n "${value}" > "${knob}"
91 elif [ -z "${value}" ] || [ "${value}" = "default" ]; then
92 $XFS_IO_PROG -x -c "inject $type" $TEST_DIR
94 _fail "Cannot inject error ${type} value ${value}."
98 # Inject an error into the scratch fs
99 _scratch_inject_error()
104 knob="$(_find_xfs_mountdev_errortag_knob "${SCRATCH_DEV}" "${type}")"
105 if [ -w "${knob}" ]; then
106 test -z "${value}" && value="default"
107 echo -n "${value}" > "${knob}"
108 elif [ -z "${value}" ] || [ "${value}" = "default" ]; then
109 $XFS_IO_PROG -x -c "inject $type" $SCRATCH_MNT
111 _fail "Cannot inject error ${type} value ${value}."
115 # Unmount and remount the scratch device, dumping the log
116 _scratch_inject_logprint()
120 if test -n "$opts"; then
125 _scratch_mount "$opts"
128 # Unmount and remount the test device, dumping the log
129 _test_inject_logprint()
133 if test -n "$opts"; then