common/rc: factor out _scratch_xfs_[get|set]_sb_field
[xfstests-dev.git] / common / inject
index 8ecc2901f7e9ff6e8f3dd6fa90f0eb600edfb356..f30d0ba71449d48fd66eaab77fd0965a68bd4fe4 100644 (file)
@@ -35,12 +35,49 @@ _require_error_injection()
        esac
 }
 
+# Find the errortag injection knob in sysfs for a given xfs mount's
+# block device.
+_find_xfs_mountdev_errortag_knob()
+{
+       dev="$1"
+       knob="$2"
+       shortdev="$(_short_dev "${dev}")"
+       tagfile="/sys/fs/xfs/${shortdev}/errortag/${knob}"
+
+       # Some of the new sysfs errortag knobs were previously available via
+       # another sysfs path.
+       case "${knob}" in
+       "log_bad_crc")
+               if [ ! -w "${tagfile}" ]; then
+                       tagfile="/sys/fs/xfs/${shortdev}/log/log_badcrc_factor"
+               fi
+               ;;
+       "drop_writes")
+               if [ ! -w "${tagfile}" ]; then
+                       tagfile="/sys/fs/xfs/${shortdev}/drop_writes"
+               fi
+               if [ ! -w "${tagfile}" ]; then
+                       tagfile="/sys/fs/xfs/${shortdev}/fail_writes"
+               fi
+               ;;
+       *)
+               ;;
+       esac
+
+       echo "${tagfile}"
+}
+
 # Requires that xfs_io inject command knows about this error type
 _require_xfs_io_error_injection()
 {
        type="$1"
        _require_error_injection
 
+       # Can we find the error injection knobs via the new errortag
+       # configuration mechanism?
+       knob="$(_find_xfs_mountdev_errortag_knob "${TEST_DEV}" "${type}")"
+       test -w "${knob}" && return
+
        # NOTE: We can't actually test error injection here because xfs
        # hasn't always range checked the argument to xfs_errortag_add.
        # We also don't want to trip an error before we're ready to deal
@@ -54,16 +91,34 @@ _require_xfs_io_error_injection()
 _test_inject_error()
 {
        type="$1"
+       value="$2"
 
-       $XFS_IO_PROG -x -c "inject $type" $TEST_DIR
+       knob="$(_find_xfs_mountdev_errortag_knob "${TEST_DEV}" "${type}")"
+       if [ -w "${knob}" ]; then
+               test -z "${value}" && value="default"
+               echo -n "${value}" > "${knob}"
+       elif [ -z "${value}" ] || [ "${value}" = "default" ]; then
+               $XFS_IO_PROG -x -c "inject $type" $TEST_DIR
+       else
+               _fail "Cannot inject error ${type} value ${value}."
+       fi
 }
 
 # Inject an error into the scratch fs
 _scratch_inject_error()
 {
        type="$1"
+       value="$2"
 
-       $XFS_IO_PROG -x -c "inject $type" $SCRATCH_MNT
+       knob="$(_find_xfs_mountdev_errortag_knob "${SCRATCH_DEV}" "${type}")"
+       if [ -w "${knob}" ]; then
+               test -z "${value}" && value="default"
+               echo -n "${value}" > "${knob}"
+       elif [ -z "${value}" ] || [ "${value}" = "default" ]; then
+               $XFS_IO_PROG -x -c "inject $type" $SCRATCH_MNT
+       else
+               _fail "Cannot inject error ${type} value ${value}."
+       fi
 }
 
 # Unmount and remount the scratch device, dumping the log