]> www.infradead.org Git - users/hch/xfstests-dev.git/commitdiff
common: refactor sysfs_attr functions
authorBoris Burkov <boris@bur.io>
Thu, 28 Sep 2023 23:16:43 +0000 (16:16 -0700)
committerZorro Lang <zlang@kernel.org>
Thu, 5 Oct 2023 14:32:01 +0000 (22:32 +0800)
Expand the has/get/require functions to allow passing a dev by
parameter, and implement the test_dev specific one in terms of the new
generic one.

Signed-off-by: Boris Burkov <boris@bur.io>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Zorro Lang <zlang@kernel.org>
common/rc

index 76a7e774031814176721d6165f4d8cbf3fbbe530..5fd2d66a83b9a116d7a1bba0d227a8db8b06d204 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -4705,47 +4705,33 @@ run_fsx()
        rm -f $tmp.fsx
 }
 
-# Test for the existence of a sysfs entry at /sys/fs/$FSTYP/DEV/$ATTR
+_require_statx()
+{
+       $here/src/stat_test --check-statx ||
+       _notrun "This test requires the statx system call"
+}
+
+# Get the path to the sysfs directory for the fs on a device
 #
 # Only one argument is needed:
-#  - attr: path name under /sys/fs/$FSTYP/DEV
+#  - dev: mounted block device for the fs
 #
 # Usage example:
-#   _require_fs_sysfs error/fail_at_unmount
-_has_fs_sysfs()
+#   _fs_sysfs_dname /dev/mapper/scratch-dev
+_fs_sysfs_dname()
 {
-       local attr=$1
-       local dname
+       local dev=$1
+
+       if [ ! -b "$dev" ]; then
+               _fail "Usage: _fs_sysfs_dname <mounted_device>"
+       fi
 
        case "$FSTYP" in
        btrfs)
-               dname=$(findmnt -n -o UUID $TEST_DEV) ;;
+               findmnt -n -o UUID ${dev} ;;
        *)
-               dname=$(_short_dev $TEST_DEV) ;;
+               _short_dev $dev ;;
        esac
-
-       if [ -z "$attr" -o -z "$dname" ];then
-               _fail "Usage: _require_fs_sysfs <sysfs_attr_path>"
-       fi
-
-       test -e /sys/fs/${FSTYP}/${dname}/${attr}
-}
-
-# Require the existence of a sysfs entry at /sys/fs/$FSTYP/DEV/$ATTR
-_require_fs_sysfs()
-{
-       _has_fs_sysfs "$@" && return
-
-       local attr=$1
-       local dname=$(_short_dev $TEST_DEV)
-
-       _notrun "This test requires /sys/fs/${FSTYP}/${dname}/${attr}"
-}
-
-_require_statx()
-{
-       $here/src/stat_test --check-statx ||
-       _notrun "This test requires the statx system call"
 }
 
 # Write "content" into /sys/fs/$FSTYP/$DEV/$ATTR
@@ -4769,13 +4755,7 @@ _set_fs_sysfs_attr()
                _fail "Usage: _set_fs_sysfs_attr <mounted_device> <attr> <content>"
        fi
 
-       local dname
-       case "$FSTYP" in
-       btrfs)
-               dname=$(findmnt -n -o UUID ${dev}) ;;
-       *)
-               dname=$(_short_dev $dev) ;;
-       esac
+       local dname=$(_fs_sysfs_dname $dev)
 
        echo "$content" > /sys/fs/${FSTYP}/${dname}/${attr}
 }
@@ -4797,17 +4777,74 @@ _get_fs_sysfs_attr()
                _fail "Usage: _get_fs_sysfs_attr <mounted_device> <attr>"
        fi
 
-       local dname
-       case "$FSTYP" in
-       btrfs)
-               dname=$(findmnt -n -o UUID ${dev}) ;;
-       *)
-               dname=$(_short_dev $dev) ;;
-       esac
+       local dname=$(_fs_sysfs_dname $dev)
 
        cat /sys/fs/${FSTYP}/${dname}/${attr}
 }
 
+# Test for the existence of a sysfs entry at /sys/fs/$FSTYP/$DEV/$ATTR
+#
+# All arguments are necessary, and in this order:
+#  - dev: device name, e.g. $SCRATCH_DEV
+#  - attr: path name under /sys/fs/$FSTYP/$dev
+#
+# Usage example:
+#   _has_fs_sysfs_attr /dev/mapper/scratch-dev error/fail_at_unmount
+_has_fs_sysfs_attr()
+{
+       local dev=$1
+       local attr=$2
+
+       if [ ! -b "$dev" -o -z "$attr" ];then
+               _fail "Usage: _has_fs_sysfs_attr <mounted_device> <attr>"
+       fi
+
+       local dname=$(_fs_sysfs_dname $dev)
+
+       test -e /sys/fs/${FSTYP}/${dname}/${attr}
+}
+
+# Require the existence of a sysfs entry at /sys/fs/$FSTYP/$DEV/$ATTR
+# All arguments are necessary, and in this order:
+#  - dev: device name, e.g. $SCRATCH_DEV
+#  - attr: path name under /sys/fs/$FSTYP/$dev
+#
+# Usage example:
+#   _require_fs_sysfs_attr /dev/mapper/scratch-dev error/fail_at_unmount
+_require_fs_sysfs_attr()
+{
+       _has_fs_sysfs_attr "$@" && return
+
+       local dev=$1
+       local attr=$2
+       local dname=$(_fs_sysfs_dname $dev)
+
+       _notrun "This test requires /sys/fs/${FSTYP}/${dname}/${attr}"
+}
+
+# Test for the existence of a sysfs entry at /sys/fs/$FSTYP/DEV/$ATTR
+#
+# Only one argument is needed:
+#  - attr: path name under /sys/fs/$FSTYP/DEV
+#
+# Usage example:
+#   _has_fs_sysfs error/fail_at_unmount
+_has_fs_sysfs()
+{
+       _has_fs_sysfs_attr $TEST_DEV "$@"
+}
+
+# Require the existence of a sysfs entry at /sys/fs/$FSTYP/DEV/$ATTR
+_require_fs_sysfs()
+{
+       _has_fs_sysfs "$@" && return
+
+       local attr=$1
+       local dname=$(_short_dev $TEST_DEV)
+
+       _notrun "This test requires /sys/fs/${FSTYP}/${dname}/${attr}"
+}
+
 # Generic test for specific filesystem feature.
 # Currently only implemented to test overlayfs features.
 _require_scratch_feature()