]> git-server-git.apps.pok.os.sepia.ceph.com Git - xfstests-dev.git/commitdiff
xfs: refactor filesystem feature detection logic
authorDarrick J. Wong <djwong@kernel.org>
Fri, 28 Oct 2022 17:41:53 +0000 (10:41 -0700)
committerZorro Lang <zlang@kernel.org>
Sun, 30 Oct 2022 10:39:20 +0000 (18:39 +0800)
There are a lot of places where we open-code detecting features of a
specific filesystem.  Refactor this into a couple of helpers in
preparation for adding stress tests for online repair and fuzzing.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Zorro Lang <zlang@redhat.com>
Signed-off-by: Zorro Lang <zlang@kernel.org>
common/populate
common/rc
common/xfs
tests/xfs/097
tests/xfs/151
tests/xfs/271
tests/xfs/307
tests/xfs/308
tests/xfs/348

index 58b07e33be602a895741b9814cded37b7328e017..9fa1a067985a00934e2659f00042e06fdc070a6e 100644 (file)
@@ -131,7 +131,7 @@ _populate_xfs_qmount_option()
        fi
 
        # Turn on all the quotas
-       if $XFS_INFO_PROG "${TEST_DIR}" | grep -q 'crc=1'; then
+       if _xfs_has_feature "$TEST_DIR" crc; then
                # v5 filesystems can have group & project quotas
                quota="usrquota,grpquota,prjquota"
        else
@@ -176,7 +176,7 @@ _scratch_xfs_populate() {
 
        blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
        dblksz="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep naming.*bsize | sed -e 's/^.*bsize=//g' -e 's/\([0-9]*\).*$/\1/g')"
-       crc="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep crc= | sed -e 's/^.*crc=//g' -e 's/\([0-9]*\).*$/\1/g')"
+       crc="$(_xfs_has_feature "$SCRATCH_MNT" crc -v)"
        if [ $crc -eq 1 ]; then
                leaf_hdr_size=64
        else
@@ -315,7 +315,7 @@ _scratch_xfs_populate() {
        done
 
        # Reverse-mapping btree
-       is_rmapbt="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'rmapbt=1')"
+       is_rmapbt="$(_xfs_has_feature "$SCRATCH_MNT" rmapbt -v)"
        if [ $is_rmapbt -gt 0 ]; then
                echo "+ rmapbt btree"
                nr="$((blksz * 2 / 24))"
@@ -332,7 +332,7 @@ _scratch_xfs_populate() {
        fi
 
        # Reference-count btree
-       is_reflink="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'reflink=1')"
+       is_reflink="$(_xfs_has_feature "$SCRATCH_MNT" reflink -v)"
        if [ $is_reflink -gt 0 ]; then
                echo "+ reflink btree"
                nr="$((blksz * 2 / 12))"
@@ -597,9 +597,9 @@ _scratch_xfs_populate_check() {
        leaf_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_LEAF")"
        node_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_NODE")"
        btree_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_BTREE")"
-       is_finobt=$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'finobt=1')
-       is_rmapbt=$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'rmapbt=1')
-       is_reflink=$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'reflink=1')
+       is_finobt=$(_xfs_has_feature "$SCRATCH_MNT" finobt -v)
+       is_rmapbt=$(_xfs_has_feature "$SCRATCH_MNT" rmapbt -v)
+       is_reflink=$(_xfs_has_feature "$SCRATCH_MNT" reflink -v)
 
        blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
        dblksz="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep naming.*bsize | sed -e 's/^.*bsize=//g' -e 's/\([0-9]*\).*$/\1/g')"
index f4785c17ca576f72586813e4bb75e435b61cdff9..8060c03b7d1861c72936232ac2cf1a6c94940849 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -247,7 +247,7 @@ _supports_filetype()
        local fstyp=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $2}'`
        case "$fstyp" in
        xfs)
-               $XFS_INFO_PROG $dir | grep -q "ftype=1"
+               _xfs_has_feature $dir ftype
                ;;
        ext2|ext3|ext4)
                local dev=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $1}'`
index 8334880e32875bd033ee496d28ec8c6cf461c00d..5b13c285962a291cef31258f991815739391820a 100644 (file)
@@ -413,6 +413,56 @@ _require_xfs_crc()
        _scratch_unmount
 }
 
+# If the xfs_info output for the given XFS filesystem mount mentions the given
+# feature.  If so, return 0 for success.  If not, return 1 for failure.  If the
+# third option is -v, echo 1 for success and 0 for not.
+#
+# Starting with xfsprogs 4.17, this also works for unmounted filesystems.
+_xfs_has_feature()
+{
+       local fs="$1"
+       local feat="$2"
+       local verbose="$3"
+
+       local answer="$($XFS_INFO_PROG "$fs" 2>&1 | grep -w -c "$feat=1")"
+       if [ "$answer" -ne 0 ]; then
+               test "$verbose" = "-v" && echo 1
+               return 0
+       fi
+
+       test "$verbose" = "-v" && echo 0
+       return 1
+}
+
+# Require that the xfs_info output for the given XFS filesystem mount mentions
+# the given feature flag.  If the third argument is -u (or is empty and the
+# second argument is $SCRATCH_MNT), unmount the fs on failure.  If a fourth
+# argument is supplied, it will be used as the _notrun message.
+_require_xfs_has_feature()
+{
+       local fs="$1"
+       local feat="$2"
+       local umount="$3"
+       local message="$4"
+
+       if [ -z "$umount" ] && [ "$fs" = "$SCRATCH_MNT" ]; then
+               umount="-u"
+       fi
+
+       _xfs_has_feature "$1" "$2" && return 0
+
+       test "$umount" = "-u" && umount "$fs" &>/dev/null
+
+       test -n "$message" && _notrun "$message"
+
+       case "$fs" in
+       "$TEST_DIR"|"$TEST_DEV")        fsname="test";;
+       "$SCRATCH_MNT"|"$SCRATCH_DEV")  fsname="scratch";;
+       *)                              fsname="$fs";;
+       esac
+       _notrun "$2 not supported by $fsname filesystem type: $FSTYP"
+}
+
 # this test requires the xfs kernel support crc feature on scratch device
 #
 _require_scratch_xfs_crc()
@@ -420,7 +470,8 @@ _require_scratch_xfs_crc()
        _scratch_mkfs_xfs >/dev/null 2>&1
        _try_scratch_mount >/dev/null 2>&1 \
           || _notrun "Kernel doesn't support crc feature"
-       $XFS_INFO_PROG $SCRATCH_MNT | grep -q 'crc=1' || _notrun "crc feature not supported by this filesystem"
+       _require_xfs_has_feature $SCRATCH_MNT crc -u \
+               "crc feature not supported by this filesystem"
        _scratch_unmount
 }
 
@@ -739,10 +790,7 @@ _check_xfs_test_fs()
 _require_xfs_test_rmapbt()
 {
        _require_test
-
-       if [ "$($XFS_INFO_PROG "$TEST_DIR" | grep -c "rmapbt=1")" -ne 1 ]; then
-               _notrun "rmapbt not supported by test filesystem type: $FSTYP"
-       fi
+       _require_xfs_has_feature "$TEST_DIR" rmapbt
 }
 
 _require_xfs_scratch_rmapbt()
@@ -751,10 +799,7 @@ _require_xfs_scratch_rmapbt()
 
        _scratch_mkfs > /dev/null
        _scratch_mount
-       if [ "$($XFS_INFO_PROG "$SCRATCH_MNT" | grep -c "rmapbt=1")" -ne 1 ]; then
-               _scratch_unmount
-               _notrun "rmapbt not supported by scratch filesystem type: $FSTYP"
-       fi
+       _require_xfs_has_feature "$SCRATCH_MNT" rmapbt
        _scratch_unmount
 }
 
@@ -1357,8 +1402,8 @@ _require_scratch_xfs_bigtime()
                _notrun "mkfs.xfs doesn't support bigtime feature"
        _try_scratch_mount || \
                _notrun "kernel doesn't support xfs bigtime feature"
-       $XFS_INFO_PROG "$SCRATCH_MNT" | grep -q -w "bigtime=1" || \
-               _notrun "bigtime feature not advertised on mount?"
+       _require_xfs_has_feature $SCRATCH_MNT bigtime -u \
+               "crc feature not supported by this filesystem"
        _scratch_unmount
 }
 
index 4cad7216cd4f2a6048b6793d3ef1b008d937ee1c..1df34eeddcf770c89d279777853dd2f30a4a88ac 100755 (executable)
@@ -42,7 +42,7 @@ _scratch_mkfs_xfs -m crc=1,finobt=1 > /dev/null
 
 echo "+ mount fs image"
 _scratch_mount
-$XFS_INFO_PROG "${SCRATCH_MNT}" | grep -q "finobt=1" || _notrun "finobt not enabled"
+_require_xfs_has_feature "$SCRATCH_MNT" finobt
 blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
 
 echo "+ make some files"
index 66425f6710c1770890e154ef578e82b015a0614e..b2fe16aefb269313677225d0ef9fc6aea10e254d 100755 (executable)
@@ -24,8 +24,7 @@ echo "Format filesystem and populate"
 _scratch_mkfs > $seqres.full
 _scratch_mount >> $seqres.full
 
-$XFS_INFO_PROG $SCRATCH_MNT | grep -q ftype=1 || \
-       _notrun "filesystem does not support ftype"
+_require_xfs_has_feature "$SCRATCH_MNT" ftype
 
 filter_ls() {
        awk '
index 14d64cd0e55ff4ab2f88ff91971bd172d644818b..d67ac4d6c1f9f933c45a5d3e65789e1ca00af832 100755 (executable)
@@ -37,7 +37,7 @@ agcount=$(_xfs_mount_agcount $SCRATCH_MNT)
 # same owner (per-AG metadata) for rmap btree blocks and blocks on the AGFL and
 # the reverse mapping index merges records, the number of per-AG extents
 # reported will vary depending on whether the refcount btree is enabled.
-$XFS_INFO_PROG $SCRATCH_MNT | grep -q reflink=1
+_require_xfs_has_feature "$SCRATCH_MNT" reflink
 has_reflink=$(( 1 - $? ))
 perag_metadata_exts=2
 test $has_reflink -gt 0 && perag_metadata_exts=$((perag_metadata_exts + 1))
index ba7204dd00062c14bc2d3bd19923b779382c10c9..f3c970fadf46471185fbcf8a76e4c4c19e6f5620 100755 (executable)
@@ -22,7 +22,7 @@ _require_scratch_reflink
 echo "Format"
 _scratch_mkfs > $seqres.full 2>&1
 _scratch_mount >> $seqres.full
-is_rmap=$($XFS_INFO_PROG $SCRATCH_MNT | grep -c "rmapbt=1")
+is_rmap=$(_xfs_has_feature $SCRATCH_MNT rmapbt -v)
 _scratch_unmount
 
 _get_agf_data() {
index d0f47f5038e428a1af743bfb6fc9489e675de38e..6da6622e14a61c2af68f38e7d92e617678814bae 100755 (executable)
@@ -22,7 +22,7 @@ _require_scratch_reflink
 echo "Format"
 _scratch_mkfs > $seqres.full 2>&1
 _scratch_mount >> $seqres.full
-is_rmap=$($XFS_INFO_PROG $SCRATCH_MNT | grep -c "rmapbt=1")
+is_rmap=$(_xfs_has_feature $SCRATCH_MNT rmapbt -v)
 _scratch_xfs_unmount_dirty
 
 _get_agf_data() {
index faf2dca50b07ee3280dfa726773afcf11278cabc..d1645d9462e5a79193e66363fe61e704f7fed4db 100755 (executable)
@@ -39,7 +39,7 @@ mknod $testdir/CHRDEV c 1 1
 mknod $testdir/BLKDEV b 1 1
 mknod $testdir/FIFO p
 
-$XFS_INFO_PROG $SCRATCH_MNT | grep -q "ftype=1" && FTYPE_FEATURE=1
+_xfs_has_feature $SCRATCH_MNT ftype && FTYPE_FEATURE=1
 
 # Record test dir inode for xfs_repair filter
 inode_filter=$tmp.sed