fstests: _require_chattr() must get an input arg
[xfstests-dev.git] / common / rc
index 00b53643f24855ecdd07d8689e7fc86f3fb75cad..ae3add319ae7d8bdf52ce9def89f5803ac54cbfe 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -68,14 +68,6 @@ dd()
    fi
 }
 
-_btrfs_get_subvolid()
-{
-       mnt=$1
-       name=$2
-
-       $BTRFS_UTIL_PROG sub list $mnt | grep $name | awk '{ print $2 }'
-}
-
 # Prints the md5 checksum of a given file
 _md5_checksum()
 {
@@ -123,10 +115,29 @@ then
     fi
 fi
 
+_dump_err()
+{
+    err_msg="$*"
+    echo "$err_msg"
+}
+
+_dump_err2()
+{
+    err_msg="$*"
+    >2& echo "$err_msg"
+}
+
+_log_err()
+{
+    err_msg="$*"
+    echo "$err_msg" | tee -a $seqres.full
+    echo "(see $seqres.full for details)"
+}
+
 # make sure we have a standard umask
 umask 022
 
-# check for correct setup
+# check for correct setup and source the $FSTYP specific functions now
 case "$FSTYP" in
     xfs)
         [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
@@ -134,7 +145,6 @@ case "$FSTYP" in
         [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
         [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found"
 
-        #source the XFS specific functions now.
         . ./common/xfs
         ;;
     udf)
@@ -142,6 +152,8 @@ case "$FSTYP" in
         ;;
     btrfs)
         [ "$MKFS_BTRFS_PROG" = "" ] && _fatal "mkfs.btrfs not found"
+
+        . ./common/btrfs
         ;;
     ext4)
         [ "$MKFS_EXT4_PROG" = "" ] && _fatal "mkfs.ext4 not found"
@@ -155,6 +167,8 @@ case "$FSTYP" in
         ;;
     ceph)
         ;;
+    glusterfs)
+        ;;
     overlay)
         ;;
     reiser4)
@@ -162,11 +176,51 @@ case "$FSTYP" in
         ;;
 esac
 
+if [ ! -z "$REPORT_LIST" ]; then
+       . ./common/report
+       _assert_report_list
+fi
+
 _mount()
 {
     $MOUNT_PROG `_mount_ops_filter $*`
 }
 
+# Call _mount to do mount operation but also save mountpoint to
+# MOUNTED_POINT_STACK. Note that the mount point must be the last parameter
+_get_mount()
+{
+       local mnt_point=${!#}
+
+       _mount $*
+       if [ $? -eq 0 ]; then
+               MOUNTED_POINT_STACK="$mnt_point $MOUNTED_POINT_STACK"
+       else
+               return 1
+       fi
+}
+
+# Unmount the last mounted mountpoint in MOUNTED_POINT_STACK
+# and return it to caller
+_put_mount()
+{
+       local last_mnt=`echo $MOUNTED_POINT_STACK | awk '{print $1}'`
+
+       if [ -n "$last_mnt" ]; then
+               $UMOUNT_PROG $last_mnt
+       fi
+       MOUNTED_POINT_STACK=`echo $MOUNTED_POINT_STACK | cut -d\  -f2-`
+}
+
+# Unmount all mountpoints in MOUNTED_POINT_STACK and clear the stack
+_clear_mount_stack()
+{
+       if [ -n "$MOUNTED_POINT_STACK" ]; then
+               $UMOUNT_PROG $MOUNTED_POINT_STACK
+       fi
+       MOUNTED_POINT_STACK=""
+}
+
 _scratch_options()
 {
     type=$1
@@ -249,7 +303,7 @@ _common_dev_mount_options()
 
 _overlay_basic_mount_options()
 {
-       echo "-o lowerdir=$1/$OVERLAY_LOWER_DIR,upperdir=$1/$OVERLAY_UPPER_DIR,workdir=$1/$OVERLAY_WORK_DIR"
+       echo "-o lowerdir=$1/$OVL_LOWER,upperdir=$1/$OVL_UPPER,workdir=$1/$OVL_WORK"
 }
 
 _overlay_mount_options()
@@ -264,13 +318,38 @@ _scratch_mount_options()
        _scratch_options mount
 
        if [ "$FSTYP" == "overlay" ]; then
-               echo `_overlay_mount_options $SCRATCH_DEV`
+               echo `_overlay_mount_options $OVL_BASE_SCRATCH_MNT`
                return 0
        fi
        echo `_common_dev_mount_options $*` $SCRATCH_OPTIONS \
                                        $SCRATCH_DEV $SCRATCH_MNT
 }
 
+_supports_filetype()
+{
+       local dir=$1
+
+       local fstyp=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $2}'`
+       case "$fstyp" in
+       xfs)
+               xfs_info $dir | grep -q "ftype=1"
+               ;;
+       ext2|ext3|ext4)
+               local dev=`$DF_PROG $dir | tail -1 | $AWK_PROG '{print $1}'`
+               tune2fs -l $dev | grep -q filetype
+               ;;
+       *)
+               local testfile=$dir/$$.ftype
+               touch $testfile
+               # look for DT_UNKNOWN files
+               local unknowns=$(src/t_dir_type $dir u | wc -l)
+               rm $testfile
+               # 0 unknowns is success
+               return $unknowns
+               ;;
+       esac
+}
+
 # helper function to do the actual overlayfs mount operation
 _overlay_mount_dirs()
 {
@@ -283,7 +362,17 @@ _overlay_mount_dirs()
                    -o workdir=$workdir $*
 }
 
-# Given a dir, set up 3 subdirectories and mount on the given mnt.
+_overlay_mkdirs()
+{
+       local dir=$1
+
+       mkdir -p $dir/$OVL_UPPER
+       mkdir -p $dir/$OVL_LOWER
+       mkdir -p $dir/$OVL_WORK
+       mkdir -p $dir/$OVL_MNT
+}
+
+# Given a base fs dir, set up overlay directories and mount on the given mnt.
 # The dir is used as the mount device so it can be seen from df or mount
 _overlay_mount()
 {
@@ -291,48 +380,89 @@ _overlay_mount()
        local mnt=$2
        shift 2
 
-       local upper_fst=$(df --output=fstype $dir | tail -1)
-       case "$upper_fst" in
-       xfs)
-               if ! xfs_info $dir | grep -q "ftype=1" ; then
-                       _notrun "upper fs needs to support d_type"
-               fi
-               ;;
-       ext2|ext3|ext4)
-               if ! tune2fs -l $(df --output=source $dir | tail -1) | \
-                  grep -q filetype ; then
-                       _notrun "upper fs needs to support d_type"
-               fi
-               ;;
-       esac
+       _supports_filetype $dir || _notrun "upper fs needs to support d_type"
 
-       mkdir -p $dir/$OVERLAY_UPPER_DIR
-       mkdir -p $dir/$OVERLAY_LOWER_DIR
-       mkdir -p $dir/$OVERLAY_WORK_DIR
+       _overlay_mkdirs $dir
 
-       _overlay_mount_dirs $dir/$OVERLAY_LOWER_DIR $dir/$OVERLAY_UPPER_DIR \
-                           $dir/$OVERLAY_WORK_DIR $OVERLAY_MOUNT_OPTIONS \
+       _overlay_mount_dirs $dir/$OVL_LOWER $dir/$OVL_UPPER \
+                           $dir/$OVL_WORK $OVERLAY_MOUNT_OPTIONS \
                            $SELINUX_MOUNT_OPTIONS $* $dir $mnt
 }
 
+_overlay_base_test_mount()
+{
+       if [ -z "$OVL_BASE_TEST_DEV" -o -z "$OVL_BASE_TEST_DIR" ] || \
+               _check_mounted_on OVL_BASE_TEST_DEV $OVL_BASE_TEST_DEV \
+                               OVL_BASE_TEST_DIR $OVL_BASE_TEST_DIR
+       then
+               # no base fs or already mounted
+               return 0
+       elif [ $? -ne 1 ]
+       then
+               # base fs mounted but not on mount point
+               return 1
+       fi
+
+       _mount $TEST_FS_MOUNT_OPTS \
+               $SELINUX_MOUNT_OPTIONS \
+               $OVL_BASE_TEST_DEV $OVL_BASE_TEST_DIR
+}
+
 _overlay_test_mount()
 {
-       _overlay_mount $TEST_DEV $TEST_DIR $*
+       _overlay_base_test_mount && \
+               _overlay_mount $OVL_BASE_TEST_DIR $TEST_DIR $*
+}
+
+_overlay_base_scratch_mount()
+{
+       if [ -z "$OVL_BASE_SCRATCH_DEV" -o -z "$OVL_BASE_SCRATCH_MNT" ] || \
+               _check_mounted_on OVL_BASE_SCRATCH_DEV $OVL_BASE_SCRATCH_DEV \
+                               OVL_BASE_SCRATCH_MNT $OVL_BASE_SCRATCH_MNT
+       then
+               # no base fs or already mounted
+               return 0
+       elif [ $? -ne 1 ]
+       then
+               # base fs mounted but not on mount point
+               return 1
+       fi
+
+       _mount $OVL_BASE_MOUNT_OPTIONS \
+               $SELINUX_MOUNT_OPTIONS \
+               $OVL_BASE_SCRATCH_DEV $OVL_BASE_SCRATCH_MNT
+}
+
+_overlay_base_scratch_unmount()
+{
+       [ -n "$OVL_BASE_SCRATCH_DEV" -a -n "$OVL_BASE_SCRATCH_MNT" ] || return 0
+
+       $UMOUNT_PROG $OVL_BASE_SCRATCH_MNT
 }
 
 _overlay_scratch_mount()
 {
-       _overlay_mount $SCRATCH_DEV $SCRATCH_MNT $*
+       _overlay_base_scratch_mount && \
+               _overlay_mount $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
+}
+
+_overlay_base_test_unmount()
+{
+       [ -n "$OVL_BASE_TEST_DEV" -a -n "$OVL_BASE_TEST_DIR" ] || return 0
+
+       $UMOUNT_PROG $OVL_BASE_TEST_DIR
 }
 
 _overlay_test_unmount()
 {
        $UMOUNT_PROG $TEST_DIR
+       _overlay_base_test_unmount
 }
 
 _overlay_scratch_unmount()
 {
        $UMOUNT_PROG $SCRATCH_MNT
+       _overlay_base_scratch_unmount
 }
 
 _scratch_mount()
@@ -417,15 +547,62 @@ _scratch_mkfs_options()
     echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
 }
 
+# Do the actual mkfs work on SCRATCH_DEV. Firstly mkfs with both MKFS_OPTIONS
+# and user specified mkfs options, if that fails (due to conflicts between mkfs
+# options), do a second mkfs with only user provided mkfs options.
+#
+# First param is the mkfs command without any mkfs options and device.
+# Second param is the filter to remove unnecessary messages from mkfs stderr.
+# Other extra mkfs options are followed.
+_scratch_do_mkfs()
+{
+       local mkfs_cmd=$1
+       local mkfs_filter=$2
+       shift 2
+       local extra_mkfs_options=$*
+       local mkfs_status
+       local tmp=`mktemp`
+
+       # save mkfs output in case conflict means we need to run again.
+       # only the output for the mkfs that applies should be shown
+       eval "$mkfs_cmd $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV" \
+               2>$tmp.mkfserr 1>$tmp.mkfsstd
+       mkfs_status=$?
+
+       # a mkfs failure may be caused by conflicts between $MKFS_OPTIONS and
+       # $extra_mkfs_options
+       if [ $mkfs_status -ne 0 -a -n "$extra_mkfs_options" ]; then
+               (
+               echo -n "** mkfs failed with extra mkfs options "
+               echo "added to \"$MKFS_OPTIONS\" by test $seq **"
+               echo -n "** attempting to mkfs using only test $seq "
+               echo "options: $extra_mkfs_options **"
+               ) >> $seqres.full
+
+               # running mkfs again. overwrite previous mkfs output files
+               eval "$mkfs_cmd $extra_mkfs_options $SCRATCH_DEV" \
+                       2>$tmp.mkfserr 1>$tmp.mkfsstd
+               mkfs_status=$?
+       fi
+
+       # output stored mkfs output, filtering unnecessary output from stderr
+       cat $tmp.mkfsstd
+       eval "cat $tmp.mkfserr | $mkfs_filter" >&2
+
+       rm -f $tmp*
+       return $mkfs_status
+}
+
 _scratch_metadump()
 {
        dumpfile=$1
+       shift
        options=
 
        [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
                options="-l $SCRATCH_LOGDEV"
 
-       xfs_metadump $options $SCRATCH_DEV $dumpfile
+       xfs_metadump $options "$@" $SCRATCH_DEV $dumpfile
 }
 
 _setup_large_ext4_fs()
@@ -491,34 +668,18 @@ _setup_large_ext4_fs()
 
 _scratch_mkfs_ext4()
 {
-       # extra mkfs options can be added by tests
-       local extra_mkfs_options=$*
+       local mkfs_cmd="$MKFS_EXT4_PROG -F"
+       local mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \""
+       local tmp=`mktemp`
+       local mkfs_status
 
-       local tmp_dir=/tmp/
 
-       $MKFS_EXT4_PROG -F $MKFS_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
-                       2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
-       local mkfs_status=$?
-
-       # a mkfs failure may be caused by conflicts between
-       # $MKFS_OPTIONS and $extra_mkfs_options
-       if [ $mkfs_status -ne 0 -a ! -z "$extra_mkfs_options" ]; then
-               (
-               echo -n "** mkfs failed with extra mkfs options "
-               echo "added to \"$MKFS_OPTIONS\" by test $seq **"
-               echo -n "** attempting to mkfs using only test $seq "
-               echo "options: $extra_mkfs_options **"
-               ) >> $seqres.full
-
-               # running mkfs again. overwrite previous mkfs output files
-               $MKFS_EXT4_PROG -F $extra_mkfs_options $SCRATCH_DEV \
-                               2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
-               local mkfs_status=$?
-       fi
+       _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $* 2>$tmp.mkfserr 1>$tmp.mkfsstd
+       mkfs_status=$?
 
        if [ $mkfs_status -eq 0 -a "$LARGE_SCRATCH_DEV" = yes ]; then
                # manually parse the mkfs output to get the fs size in bytes
-               fs_size=`cat $tmp_dir.mkfsstd | awk ' \
+               fs_size=`cat $tmp.mkfsstd | awk ' \
                        /^Block size/ { split($2, a, "="); bs = a[2] ; } \
                        / inodes, / { blks = $3 } \
                        /reserved for the super user/ { resv = $1 } \
@@ -528,10 +689,9 @@ _scratch_mkfs_ext4()
                mkfs_status=$?
        fi
 
-       # output stored mkfs output
-       grep -v -e ^Warning: -e "^mke2fs " $tmp_dir.mkfserr >&2
-       cat $tmp_dir.mkfsstd
-       rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd
+       # output mkfs stdout and stderr
+       cat $tmp.mkfsstd
+       cat $tmp.mkfserr >&2
 
        return $mkfs_status
 }
@@ -548,6 +708,9 @@ _test_mkfs()
     ceph)
        # do nothing for ceph
        ;;
+    glusterfs)
+       # do nothing for glusterfs
+       ;;
     overlay)
        # do nothing for overlay
        ;;
@@ -607,10 +770,15 @@ _scratch_cleanup_files()
 {
        case $FSTYP in
        overlay)
-               # $SCRATCH_DEV is a valid directory in overlay case
-               rm -rf $SCRATCH_DEV/*
+               # Avoid rm -rf /* if we messed up
+               [ -n "$OVL_BASE_SCRATCH_MNT" ] || return 1
+               _overlay_base_scratch_mount || return 1
+               rm -rf $OVL_BASE_SCRATCH_MNT/* || return 1
+               _overlay_mkdirs $OVL_BASE_SCRATCH_MNT
+               # leave base fs mouted so tests can setup lower/upper dir files
                ;;
        *)
+               [ -n "$SCRATCH_MNT" ] || return 1
                _scratch_mount
                rm -rf $SCRATCH_MNT/*
                _scratch_unmount
@@ -620,51 +788,58 @@ _scratch_cleanup_files()
 
 _scratch_mkfs()
 {
-    case $FSTYP in
-    xfs)
-        _scratch_mkfs_xfs $*
-       ;;
-    nfs*)
-       # unable to re-create NFS, just remove all files in $SCRATCH_MNT to
-       # avoid EEXIST caused by the leftover files created in previous runs
-        _scratch_cleanup_files
-       ;;
-    cifs)
-       # unable to re-create CIFS, just remove all files in $SCRATCH_MNT to
-       # avoid EEXIST caused by the leftover files created in previous runs
-        _scratch_cleanup_files
-       ;;
-    ceph)
-       # Don't re-create CephFS, just remove all files
-       _scratch_cleanup_files
-       ;;
-    overlay)
-       # unable to re-create overlay, remove all files in $SCRATCH_MNT to
-       # avoid EEXIST caused by the leftover files created in previous runs
-        _scratch_cleanup_files
-       ;;
-    udf)
-        $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
-       ;;
-    btrfs)
-        $MKFS_BTRFS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
-       ;;
-    ext2|ext3)
-       $MKFS_PROG -t $FSTYP -- -F $MKFS_OPTIONS $* $SCRATCH_DEV
-       ;;
-    ext4)
-       _scratch_mkfs_ext4 $*
-       ;;
-    tmpfs)
-       # do nothing for tmpfs
-       ;;
-    f2fs)
-        $MKFS_F2FS_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
-       ;;
-    *)
-       yes | $MKFS_PROG -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV
-       ;;
-    esac
+       local mkfs_cmd=""
+       local mkfs_filter=""
+       local mkfs_status
+
+       case $FSTYP in
+       nfs*|cifs|ceph|overlay|glusterfs)
+               # unable to re-create this fstyp, just remove all files in
+               # $SCRATCH_MNT to avoid EEXIST caused by the leftover files
+               # created in previous runs
+               _scratch_cleanup_files
+               return $?
+               ;;
+       tmpfs)
+               # do nothing for tmpfs
+               return 0
+               ;;
+       ext4)
+               _scratch_mkfs_ext4 $*
+               return $?
+               ;;
+       xfs)
+               _scratch_mkfs_xfs $*
+               return $?
+               ;;
+       udf)
+               mkfs_cmd="$MKFS_UDF_PROG"
+               mkfs_filter="cat"
+               ;;
+       btrfs)
+               mkfs_cmd="$MKFS_BTRFS_PROG"
+               mkfs_filter="cat"
+               ;;
+       ext2|ext3)
+               mkfs_cmd="$MKFS_PROG -t $FSTYP -- -F"
+               mkfs_filter="grep -v -e ^Warning: -e \"^mke2fs \""
+               ;;
+       f2fs)
+               mkfs_cmd="$MKFS_F2FS_PROG"
+               mkfs_filter="cat"
+               ;;
+       ocfs2)
+               mkfs_cmd="yes | $MKFS_PROG -t $FSTYP --"
+               mkfs_filter="grep -v -e ^mkfs\.ocfs2"
+               ;;
+       *)
+               mkfs_cmd="yes | $MKFS_PROG -t $FSTYP --"
+               mkfs_filter="cat"
+               ;;
+       esac
+
+       _scratch_do_mkfs "$mkfs_cmd" "$mkfs_filter" $*
+       return $?
 }
 
 # Helper function to get a spare or replace-target device from
@@ -932,7 +1107,7 @@ _scratch_mkfs_blocksized()
        ${MKFS_PROG}.$FSTYP -F $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV
        ;;
     ocfs2)
-       yes | ${MKFS_PROG}.$FSTYP -F $MKFS_OPTIONS -b $blocksize $SCRATCH_DEV
+       yes | ${MKFS_PROG}.$FSTYP -F $MKFS_OPTIONS -b $blocksize -C $blocksize $SCRATCH_DEV
        ;;
     *)
        _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_blocksized"
@@ -975,7 +1150,9 @@ _repair_scratch_fs()
                _scratch_xfs_repair "$@" 2>&1
                res=$?
        fi
-       test $res -ne 0 && >&2 echo "xfs_repair failed, err=$res"
+       if [ $res -ne 0 ]; then
+               _dump_err2 "xfs_repair failed, err=$res"
+       fi
        return $res
         ;;
     *)
@@ -987,7 +1164,7 @@ _repair_scratch_fs()
                res=0
                ;;
        *)
-               >&2 echo "fsck.$FSTYP failed, err=$res"
+               _dump_err2 "fsck.$FSTYP failed, err=$res"
                ;;
        esac
        return $res
@@ -1135,7 +1312,8 @@ _fs_type()
     # Fix the filesystem type up here so that the callers don't
     # have to bother with this quirk.
     #
-    _df_device $1 | $AWK_PROG '{ print $2 }' | sed -e 's/nfs4/nfs/'
+    _df_device $1 | $AWK_PROG '{ print $2 }' | \
+        sed -e 's/nfs4/nfs/' -e 's/fuse.glusterfs/glusterfs/'
 }
 
 # return the FS mount options of a mounted device
@@ -1229,8 +1407,9 @@ _notrun()
 {
     echo "$*" > $seqres.notrun
     echo "$seq not run: $*"
-    rm -f ${RESULT_DIR}/require_test
-    rm -f ${RESULT_DIR}/require_scratch
+    rm -f ${RESULT_DIR}/require_test*
+    rm -f ${RESULT_DIR}/require_scratch*
+
     status=0
     exit
 }
@@ -1276,16 +1455,38 @@ _supported_os()
     _notrun "not suitable for this OS: $HOSTOS"
 }
 
-_require_btrfs_loadable()
+# check if a FS on a device is mounted
+# if so, verify that it is mounted on mount point
+# if fstype is given as argument, verify that it is also
+# mounted with correct fs type
+#
+_check_mounted_on()
 {
-       modprobe -r btrfs || _notrun "btrfs unloadable"
-       modprobe btrfs || _notrun "Can't load btrfs"
-}
+       local devname=$1
+       local dev=$2
+       local mntname=$3
+       local mnt=$4
+       local type=$5
 
-_reload_btrfs_ko()
-{
-       modprobe -r btrfs || _fail "btrfs unload failed"
-       modprobe btrfs || _fail "btrfs load failed"
+       # find $dev as the source, and print result in "$dev $mnt" format
+       local mount_rec=`findmnt -rncv -S $dev -o SOURCE,TARGET`
+       [ -n "$mount_rec" ] || return 1 # 1 = not mounted
+
+       # if it's mounted, make sure its on $mnt
+       if [ "$mount_rec" != "$dev $mnt" ]; then
+               echo "$devname=$dev is mounted but not on $mntname=$mnt - aborting"
+               echo "Already mounted result:"
+               echo $mount_rec
+               return 2 # 2 = mounted on wrong mnt
+       fi
+
+       if [ -n "$type" -a "`_fs_type $dev`" != "$type" ]; then
+               echo "$devname=$dev is mounted but not a type $type filesystem"
+               # raw $DF_PROG cannot handle NFS/CIFS/overlay correctly
+               _df_device $dev
+               return 3 # 3 = mounted as wrong type
+       fi
+       return 0 # 0 = mounted as expected
 }
 
 # this test needs a scratch partition - check we're ok & unmount it
@@ -1294,6 +1495,15 @@ _reload_btrfs_ko()
 _require_scratch_nocheck()
 {
     case "$FSTYP" in
+       glusterfs)
+               echo $SCRATCH_DEV | egrep -q ":/?" > /dev/null 2>&1
+               if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
+                       _notrun "this test requires a valid \$SCRATCH_DEV"
+               fi
+               if [ ! -d "$SCRATCH_MNT" ]; then
+                       _notrun "this test requires a valid \$SCRATCH_MNT"
+               fi
+               ;;
        nfs*|ceph)
                echo $SCRATCH_DEV | grep -q ":/" > /dev/null 2>&1
                if [ -z "$SCRATCH_DEV" -o "$?" != "0" ]; then
@@ -1313,10 +1523,13 @@ _require_scratch_nocheck()
                fi
                ;;
        overlay)
-               if [ -z "$SCRATCH_DEV" -o ! -d "$SCRATCH_DEV" ]; then
-                       _notrun "this test requires a valid \$SCRATCH_DEV as ovl base dir"
+               if [ -z "$OVL_BASE_SCRATCH_MNT" -o ! -d "$OVL_BASE_SCRATCH_MNT" ]; then
+                       _notrun "this test requires a valid \$OVL_BASE_SCRATCH_MNT as ovl base dir"
                fi
-               if [ ! -d "$SCRATCH_MNT" ]; then
+               # if $SCRATCH_MNT is derived from $OVL_BASE_SCRATCH_MNT then
+               # don't check $SCRATCH_MNT dir here because base fs may not be mounted
+               # and we will create the mount point anyway on _overlay_mount
+               if [ "$SCRATCH_MNT" != "$OVL_BASE_SCRATCH_MNT/$OVL_MNT" -a ! -d "$SCRATCH_MNT" ]; then
                        _notrun "this test requires a valid \$SCRATCH_MNT"
                fi
                ;;
@@ -1342,21 +1555,12 @@ _require_scratch_nocheck()
                 ;;
     esac
 
-    # mounted?
-    # Note that we use -F here so grep doesn't try to interpret an NFS over
-    # IPv6 server as a regular expression.
-    mount_rec=`_mount | grep -F $SCRATCH_DEV`
-    if [ "$mount_rec" ]
+    _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT
+    local err=$?
+    [ $err -le 1 ] || exit 1
+    if [ $err -eq 0 ]
     then
-        # if it's mounted, make sure its on $SCRATCH_MNT
-        if ! echo $mount_rec | grep -q $SCRATCH_MNT
-        then
-            echo "\$SCRATCH_DEV=$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT=$SCRATCH_MNT - aborting"
-            echo "Already mounted result:"
-            echo $mount_rec
-            exit 1
-        fi
-        # and then unmount it
+        # if it's mounted, unmount it
         if ! _scratch_unmount
         then
             echo "failed to unmount $SCRATCH_DEV"
@@ -1379,6 +1583,15 @@ _require_scratch()
 _require_test()
 {
     case "$FSTYP" in
+       glusterfs)
+               echo $TEST_DEV | egrep -q ":/?" > /dev/null 2>&1
+               if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
+                       _notrun "this test requires a valid \$TEST_DEV"
+               fi
+               if [ ! -d "$TEST_DIR" ]; then
+                       _notrun "this test requires a valid \$TEST_DIR"
+               fi
+               ;;
        nfs*|ceph)
                echo $TEST_DEV | grep -q ":/" > /dev/null 2>&1
                if [ -z "$TEST_DEV" -o "$?" != "0" ]; then
@@ -1398,8 +1611,8 @@ _require_test()
                fi
                ;;
        overlay)
-               if [ -z "$TEST_DEV" -o ! -d "$TEST_DEV" ]; then
-                       _notrun "this test requires a valid \$TEST_DEV as ovl base dir"
+               if [ -z "$OVL_BASE_TEST_DIR" -o ! -d "$OVL_BASE_TEST_DIR" ]; then
+                       _notrun "this test requires a valid \$TEST_DIR as ovl base dir"
                fi
                if [ ! -d "$TEST_DIR" ]; then
                        _notrun "this test requires a valid \$TEST_DIR"
@@ -1427,24 +1640,14 @@ _require_test()
                 ;;
     esac
 
-    # mounted?
-    # Note that we use -F here so grep doesn't try to interpret an NFS over
-    # IPv6 server as a regular expression.
-    mount_rec=`_mount | grep -F $TEST_DEV`
-    if [ "$mount_rec" ]
+    _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR
+    local err=$?
+    [ $err -le 1 ] || exit 1
+    if [ $err -ne 0 ]
     then
-        # if it's mounted, make sure its on $TEST_DIR
-        if ! echo $mount_rec | grep -q $TEST_DIR
-        then
-            echo "\$TEST_DEV=$TEST_DEV is mounted but not on \$TEST_DIR=$TEST_DIR - aborting"
-            echo "Already mounted result:"
-            echo $mount_rec
-            exit 1
-        fi
-    else
-       out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR`
-       if [ $? -ne 1 ]; then
-               echo $out
+       if ! _test_mount
+       then
+               echo "!!! failed to mount $TEST_DEV on $TEST_DIR"
                exit 1
        fi
     fi
@@ -1500,6 +1703,15 @@ _require_ext2()
     fi
 }
 
+# this test requires tmpfs filesystem support
+#
+_require_tmpfs()
+{
+       modprobe tmpfs >/dev/null 2>&1
+       grep -q tmpfs /proc/filesystems ||
+               _notrun "this test requires tmpfs support"
+}
+
 # this test requires that (large) loopback device files are not in use
 #
 _require_no_large_scratch_dev()
@@ -1594,12 +1806,22 @@ _require_scratch_ext4_crc()
        _scratch_unmount
 }
 
-# this test requires the bigalloc feature to be available in mkfs.ext4
-#
-_require_ext4_mkfs_bigalloc()
+# Check the specified feature whether it is available in mkfs.ext4 or not.
+_require_ext4_mkfs_feature()
 {
-       $MKFS_EXT4_PROG -F -O bigalloc -n $SCRATCH_DEV 512m >/dev/null 2>&1 \
-          || _notrun "mkfs.ext4 doesn't have bigalloc feature"
+       local feature=$1
+       local testfile=/tmp/$$.ext4_mkfs
+
+       if [ -z "$feature" ]; then
+                echo "Usage: _require_ext4_mkfs_feature feature"
+                exit 1
+        fi
+
+       touch $testfile
+       local result=$($MKFS_EXT4_PROG -F -O $feature -n $testfile 512m 2>&1)
+       rm -f $testfile
+       echo $result | grep -q "Invalid filesystem option" && \
+               _notrun "mkfs.ext4 doesn't support $feature feature"
 }
 
 # this test requires the ext4 kernel support bigalloc feature
@@ -1666,13 +1888,58 @@ _run_aiodio()
     return $status
 }
 
+# this test requires y2038 sysfs switch and filesystem
+# timestamp ranges support.
+_require_y2038()
+{
+       local device=${1:-$TEST_DEV}
+       local sysfsdir=/proc/sys/fs/fs-timestamp-check-on
+
+       if [ ! -e $sysfsdir ]; then
+               _notrun "no kernel support for y2038 sysfs switch"
+       fi
+
+       local tsmin tsmax
+       read tsmin tsmax <<<$(_filesystem_timestamp_range $device)
+       if [ $tsmin -eq -1 -a $tsmax -eq -1 ]; then
+               _notrun "filesystem $FSTYP timestamp bounds are unknown"
+       fi
+}
+
+_filesystem_timestamp_range()
+{
+       device=${1:-$TEST_DEV}
+       case $FSTYP in
+       ext4)
+               if [ $(dumpe2fs -h $device 2>/dev/null | grep "Inode size:" | cut -d: -f2) -gt 128 ]; then
+                       echo "-2147483648 15032385535"
+               else
+                       echo "-2147483648 2147483647"
+               fi
+               ;;
+
+       xfs)
+               echo "-2147483648 2147483647"
+               ;;
+       jfs)
+               echo "0 4294967295"
+               ;;
+       f2fs)
+               echo "-2147483648 2147483647"
+               ;;
+       *)
+               echo "-1 -1"
+               ;;
+       esac
+}
+
 # indicate whether YP/NIS is active or not
 #
 _yp_active()
 {
        local dn
        dn=$(domainname 2>/dev/null)
-       test -n "${dn}" -a "${dn}" != "(none)"
+       test -n "${dn}" -a "${dn}" != "(none)" -a "${dn}" != "localdomain"
        echo $?
 }
 
@@ -1734,7 +2001,7 @@ _user_do()
        then
        echo $1 | /bin/bash "su $qa_user 2>&1" | _filter_user_do
     else
-       echo $1 | su $qa_user 2>&1 | _filter_user_do
+       echo $1 | su -s /bin/bash $qa_user 2>&1 | _filter_user_do
     fi
 }
 
@@ -1776,6 +2043,17 @@ _require_xfs_io_command()
                echo $testio | egrep -q "Inappropriate ioctl" && \
                        _notrun "xfs_io $command support is missing"
                ;;
+       "open")
+               # -c "open $f" is broken in xfs_io <= 4.8. Along with the fix,
+               # a new -C flag was introduced to execute one shot commands.
+               # Check for -C flag support as an indication for the bug fix.
+               testio=`$XFS_IO_PROG -F -f -C "open $testfile" $testfile 2>&1`
+               echo $testio | egrep -q "invalid option" && \
+                       _notrun "xfs_io $command support is missing"
+               ;;
+       "utimes" )
+               testio=`$XFS_IO_PROG -f -c "utimes" 0 0 0 0 $testfile 2>&1`
+               ;;
        *)
                testio=`$XFS_IO_PROG -c "$command help" 2>&1`
        esac
@@ -1809,6 +2087,28 @@ _require_odirect()
        rm -f $testfile 2>&1 > /dev/null
 }
 
+# Check that the filesystem supports swapfiles
+_require_scratch_swapfile()
+{
+       _require_scratch
+
+       _scratch_mkfs >/dev/null
+       _scratch_mount
+
+       # Minimum size for mkswap is 10 pages
+       local size=$(($(get_page_size) * 10))
+
+       _pwrite_byte 0x61 0 "$size" "$SCRATCH_MNT/swap" >/dev/null 2>&1
+       mkswap "$SCRATCH_MNT/swap" >/dev/null 2>&1
+       if ! swapon "$SCRATCH_MNT/swap" >/dev/null 2>&1; then
+               _scratch_unmount
+               _notrun "swapfiles are not supported"
+       fi
+
+       swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
+       _scratch_unmount
+}
+
 # Check that a fs has enough free space (in 1024b blocks)
 #
 _require_fs_space()
@@ -2021,7 +2321,7 @@ _mount_or_remount_rw()
                        _overlay_mount $device $mountpoint
                fi
                if [ $? -ne 0 ]; then
-                       echo "!!! failed to remount $device on $mountpoint"
+                       _dump_err "!!! failed to remount $device on $mountpoint"
                        return 0 # ok=0
                fi
        else
@@ -2055,9 +2355,7 @@ _check_generic_filesystem()
     fsck -t $FSTYP $FSCK_OPTIONS $device >$tmp.fsck 2>&1
     if [ $? -ne 0 ]
     then
-        echo "_check_generic_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
-
-        echo "_check_generic filesystem: filesystem on $device is inconsistent" >>$seqres.full
+       _log_err "_check_generic_filesystem: filesystem on $device is inconsistent"
         echo "*** fsck.$FSTYP output ***"      >>$seqres.full
         cat $tmp.fsck                          >>$seqres.full
         echo "*** end fsck.$FSTYP output"      >>$seqres.full
@@ -2132,57 +2430,6 @@ _check_udf_filesystem()
     return 0
 }
 
-_check_btrfs_filesystem()
-{
-    device=$1
-
-    # If type is set, we're mounted
-    type=`_fs_type $device`
-    ok=1
-
-    if [ "$type" = "$FSTYP" ]
-    then
-        # mounted ...
-        mountpoint=`_umount_or_remount_ro $device`
-    fi
-
-    btrfsck $device >$tmp.fsck 2>&1
-    if [ $? -ne 0 ]
-    then
-        echo "_check_btrfs_filesystem: filesystem on $device is inconsistent (see $seqres.full)"
-
-        echo "_check_btrfs_filesystem: filesystem on $device is inconsistent" >>$seqres.full
-        echo "*** fsck.$FSTYP output ***"      >>$seqres.full
-        cat $tmp.fsck                          >>$seqres.full
-        echo "*** end fsck.$FSTYP output"      >>$seqres.full
-
-        ok=0
-    fi
-    rm -f $tmp.fsck
-
-    if [ $ok -eq 0 ]
-    then
-        echo "*** mount output ***"            >>$seqres.full
-        _mount                                 >>$seqres.full
-        echo "*** end mount output"            >>$seqres.full
-    elif [ "$type" = "$FSTYP" ]
-    then
-       # was mounted ...
-       _mount_or_remount_rw "$MOUNT_OPTIONS" $device $mountpoint
-       ok=$?
-    fi
-
-    if [ $ok -eq 0 ]; then
-       status=1
-       if [ "$iam" != "check" ]; then
-               exit 1
-       fi
-       return 1
-    fi
-
-    return 0
-}
-
 _check_test_fs()
 {
     case $FSTYP in
@@ -2198,6 +2445,9 @@ _check_test_fs()
     ceph)
        # no way to check consistency for CephFS
        ;;
+    glusterfs)
+       # no way to check consistency for GlusterFS
+       ;;
     overlay)
        # no way to check consistency for overlay
        ;;
@@ -2245,6 +2495,9 @@ _check_scratch_fs()
     ceph)
        # no way to check consistency for CephFS
        ;;
+    glusterfs)
+       # no way to check consistency for GlusterFS
+       ;;
     overlay)
        # no way to check consistency for overlay
        ;;
@@ -2356,6 +2609,12 @@ _die()
         exit 1
 }
 
+# convert urandom incompressible data to compressible text data
+_ddt()
+{
+       od /dev/urandom | dd iflag=fullblock ${*}
+}
+
 #takes files, randomdata
 _nfiles()
 {
@@ -2367,6 +2626,8 @@ _nfiles()
                 if [ $size -gt 0 ]; then
                     if [ "$2" == "false" ]; then
                         dd if=/dev/zero of=$file bs=1024 count=$size 2>&1 | _filter_dd
+                    elif [ "$2" == "comp" ]; then
+                        _ddt of=$file bs=1024 count=$size 2>&1 | _filter_dd
                     else
                         dd if=/dev/urandom of=$file bs=1024 count=$size 2>&1 | _filter_dd
                     fi
@@ -2411,10 +2672,10 @@ _populate_fs()
     depth=2         # depth of tree from root to leaves
     verbose=false
     root=root       # path of initial root of directory tree
-    randomdata=false # -x data type urandom or zero
+    randomdata=false # -x data type urandom, zero or compressible
 
     OPTIND=1
-    while getopts "d:f:n:r:s:v:x" c
+    while getopts "d:f:n:r:s:v:x:c" c
     do
         case $c in
         d)      depth=$OPTARG;;
@@ -2424,6 +2685,7 @@ _populate_fs()
         v)      verbose=true;;
         r)      root=$OPTARG;;
         x)      randomdata=true;;
+        c)      randomdata=comp;;
         esac
     done
 
@@ -2540,18 +2802,6 @@ _require_deletable_scratch_dev_pool()
        done
 }
 
-# We check for btrfs and (optionally) features of the btrfs command
-_require_btrfs()
-{
-       cmd=$1
-       _require_command "$BTRFS_UTIL_PROG" btrfs
-       if [ -z "$1" ]; then
-               return 1;
-       fi
-       $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
-       [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
-}
-
 # Check that fio is present, and it is able to execute given jobfile
 _require_fio()
 {
@@ -2587,6 +2837,19 @@ _require_scratch_shutdown()
        _scratch_unmount
 }
 
+# Does dax mount option work on this dev/fs?
+_require_scratch_dax()
+{
+       _require_scratch
+       _scratch_mkfs > /dev/null 2>&1
+       _scratch_mount -o dax
+       # Check options to be sure. XFS ignores dax option
+       # and goes on if dev underneath does not support dax.
+       _fs_options $SCRATCH_DEV | grep -qw "dax" || \
+               _notrun "$SCRATCH_DEV $FSTYP does not support -o dax"
+       _scratch_unmount
+}
+
 # Does norecovery support by this fs?
 _require_norecovery()
 {
@@ -2837,50 +3100,6 @@ run_check()
        "$@" >> $seqres.full 2>&1 || _fail "failed: '$@'"
 }
 
-_run_btrfs_util_prog()
-{
-       run_check $BTRFS_UTIL_PROG $*
-}
-
-_require_btrfs_send_stream_version()
-{
-       $BTRFS_UTIL_PROG send 2>&1 | \
-               grep '^[ \t]*\-\-stream\-version <version>' > /dev/null 2>&1
-       if [ $? -ne 0 ]; then
-               _notrun "Missing btrfs-progs send --stream-version command line option, skipped this test"
-       fi
-
-       # test if btrfs kernel supports send stream version 2
-       if [ ! -f /sys/fs/btrfs/send/stream_version ]; then
-               _notrun "Missing btrfs kernel patch for send stream version 2, skipped this test"
-       fi
-}
-
-_require_btrfs_mkfs_feature()
-{
-       if [ -z $1 ]; then
-               echo "Missing feature name argument for _require_btrfs_mkfs_feature"
-               exit 1
-       fi
-       feat=$1
-       $MKFS_BTRFS_PROG -O list-all 2>&1 | \
-               grep '^[ \t]*'"$feat"'\b' > /dev/null 2>&1
-       [ $? -eq 0 ] || \
-               _notrun "Feature $feat not supported in the available version of mkfs.btrfs"
-}
-
-_require_btrfs_fs_feature()
-{
-       if [ -z $1 ]; then
-               echo "Missing feature name argument for _require_btrfs_fs_feature"
-               exit 1
-       fi
-       feat=$1
-       modprobe btrfs > /dev/null 2>&1
-       [ -e /sys/fs/btrfs/features/$feat ] || \
-               _notrun "Feature $feat not supported by the available btrfs version"
-}
-
 _require_test_symlinks()
 {
        # IRIX UDF does not support symlinks
@@ -2904,13 +3123,6 @@ _require_test_fcntl_advisory_locks()
                _notrun "Require fcntl advisory locks support"
 }
 
-_require_btrfs_dev_del_by_devid()
-{
-       $BTRFS_UTIL_PROG device delete --help | egrep devid > /dev/null 2>&1
-       [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old "\
-                       "(must support 'btrfs device delete <devid> /<mnt>')"
-}
-
 _require_test_lsattr()
 {
        testio=$(lsattr -d $TEST_DIR 2>&1)
@@ -2922,18 +3134,21 @@ _require_test_lsattr()
 
 _require_chattr()
 {
-    attribute=$1
-
-    touch $TEST_DIR/syscalltest
-    chattr "+$attribute" $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
-    status=$?
-    chattr "-$attribute" $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
-    if [ "$status" -ne 0 ]; then
-      _notrun "file system doesn't support chattr +$attribute"
-    fi
-    cat $TEST_DIR/syscalltest.out >> $seqres.full
+       if [ -z "$1" ]; then
+               echo "Usage: _require_chattr <attr>"
+               exit 1
+       fi
+       local attribute=$1
 
-    rm -f $TEST_DIR/syscalltest.out
+       touch $TEST_DIR/syscalltest
+       chattr "+$attribute" $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
+       status=$?
+       chattr "-$attribute" $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
+       if [ "$status" -ne 0 ]; then
+               _notrun "file system doesn't support chattr +$attribute"
+       fi
+       cat $TEST_DIR/syscalltest.out >> $seqres.full
+       rm -f $TEST_DIR/syscalltest.out
 }
 
 _get_total_inode()
@@ -2994,203 +3209,6 @@ _get_available_space()
        echo $((avail_kb * 1024))
 }
 
-# get btrfs profile configs being tested
-#
-# A set of pre-set profile configs are exported via _btrfs_profile_configs
-# array. Default configs can be overridden by setting BTRFS_PROFILE_CONFIGS
-# var in the format "metadata_profile:data_profile", multiple configs can be
-# seperated by space, e.g.
-# export BTRFS_PROFILE_CONFIGS="raid0:raid0 raid1:raid1 dup:single"
-_btrfs_get_profile_configs()
-{
-       if [ "$FSTYP" != "btrfs" ]; then
-               return
-       fi
-
-       if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
-               # Default configurations to test.
-               local configs=(
-                       "single:single"
-                       "dup:single"
-                       "raid0:raid0"
-                       "raid1:raid0"
-                       "raid1:raid1"
-                       "raid10:raid10"
-                       "raid5:raid5"
-                       "raid6:raid6"
-               )
-       else
-               # User-provided configurations.
-               local configs=(${BTRFS_PROFILE_CONFIGS[@]})
-       fi
-
-       _btrfs_profile_configs=()
-       for cfg in "${configs[@]}"; do
-               local supported=true
-               local profiles=(${cfg/:/ })
-               if [ "$1" == "replace" ]; then
-                       # We can't do replace with these profiles because they
-                       # imply only one device ($SCRATCH_DEV), and we need to
-                       # keep $SCRATCH_DEV around for _scratch_mount
-                       # and _check_scratch_fs.
-                       local unsupported=(
-                               "dup"
-                       )
-               elif [ "$1" == "replace-missing" ]; then
-                       # We can't replace missing devices with these profiles
-                       # because there isn't enough redundancy.
-                       local unsupported=(
-                               "single"
-                               "dup"
-                               "raid0"
-                       )
-               else
-                       local unsupported=()
-               fi
-               for unsupp in "${unsupported[@]}"; do
-                       if [ "${profiles[0]}" == "$unsupp" -o "${profiles[1]}" == "$unsupp" ]; then
-                            if [ -z "$BTRFS_PROFILE_CONFIGS" ]; then
-                                    # For the default config, just omit it.
-                                    supported=false
-                            else
-                                    # For user-provided config, don't run the test.
-                                    _notrun "Profile $unsupp not supported for $1"
-                            fi
-                       fi
-               done
-               if "$supported"; then
-                       _btrfs_profile_configs+=("-m ${profiles[0]} -d ${profiles[1]}")
-               fi
-       done
-       export _btrfs_profile_configs
-}
-
-# stress btrfs by running balance operation in a loop
-_btrfs_stress_balance()
-{
-       local options=$@
-       while true; do
-               $BTRFS_UTIL_PROG balance start $options
-       done
-}
-
-# stress btrfs by creating/mounting/umounting/deleting subvolume in a loop
-_btrfs_stress_subvolume()
-{
-       local btrfs_dev=$1
-       local btrfs_mnt=$2
-       local subvol_name=$3
-       local subvol_mnt=$4
-       local stop_file=$5
-
-       mkdir -p $subvol_mnt
-       while [ ! -e $stop_file ]; do
-               $BTRFS_UTIL_PROG subvolume create $btrfs_mnt/$subvol_name
-               $MOUNT_PROG -o subvol=$subvol_name $btrfs_dev $subvol_mnt
-               $UMOUNT_PROG $subvol_mnt
-               $BTRFS_UTIL_PROG subvolume delete $btrfs_mnt/$subvol_name
-       done
-}
-
-# stress btrfs by running scrub in a loop
-_btrfs_stress_scrub()
-{
-       local btrfs_mnt=$1
-       while true; do
-               $BTRFS_UTIL_PROG scrub start -B $btrfs_mnt
-       done
-}
-
-# stress btrfs by defragmenting every file/dir in a loop and compress file
-# contents while defragmenting if second argument is not "nocompress"
-_btrfs_stress_defrag()
-{
-       local btrfs_mnt=$1
-       local compress=$2
-
-       while true; do
-               if [ "$compress" == "nocompress" ]; then
-                       find $btrfs_mnt \( -type f -o -type d \) -exec \
-                       $BTRFS_UTIL_PROG filesystem defrag {} \;
-               else
-                       find $btrfs_mnt \( -type f -o -type d \) -exec \
-                       $BTRFS_UTIL_PROG filesystem defrag -clzo {} \;
-                       find $btrfs_mnt \( -type f -o -type d \) -exec \
-                       $BTRFS_UTIL_PROG filesystem defrag -czlib {} \;
-               fi
-       done
-}
-
-# stress btrfs by remounting it with different compression algorithms in a loop
-# run this with fsstress running at background could exercise the compression
-# code path and ensure no race when switching compression algorithm with constant
-# I/O activity.
-_btrfs_stress_remount_compress()
-{
-       local btrfs_mnt=$1
-       while true; do
-               for algo in no zlib lzo; do
-                       $MOUNT_PROG -o remount,compress=$algo $btrfs_mnt
-               done
-       done
-}
-
-# stress btrfs by replacing devices in a loop
-# Note that at least 3 devices are needed in SCRATCH_DEV_POOL and the last
-# device should be free(not used by btrfs)
-_btrfs_stress_replace()
-{
-       local btrfs_mnt=$1
-
-       # The device number in SCRATCH_DEV_POOL should be at least 3,
-       # one is SCRATCH_DEV, one is to be replaced, one is free device
-       # we won't replace SCRATCH_DEV, see below for reason
-       if [ "`echo $SCRATCH_DEV_POOL | wc -w`" -lt 3 ]; then
-               echo "_btrfs_stress_replace requires at least 3 devices in SCRATCH_DEV_POOL"
-               return
-       fi
-
-       # take the last device as the first free_dev
-       local free_dev="`echo $SCRATCH_DEV_POOL | $AWK_PROG '{print $NF}'`"
-
-       # free_dev should be really free
-       if $BTRFS_UTIL_PROG filesystem show $btrfs_mnt | grep -q "$free_dev"; then
-               echo "_btrfs_stress_replace: $free_dev is used by btrfs"
-               return
-       fi
-
-       # dev_pool is device list being currently used by btrfs (excluding SCRATCH_DEV)
-       # and can be replaced. We don't replace SCRATCH_DEV because it will be used in
-       # _scratch_mount and _check_scratch_fs etc.
-       local dev_pool=`echo $SCRATCH_DEV_POOL | sed -e "s# *$SCRATCH_DEV *##" \
-                       -e "s# *$free_dev *##"`
-
-       # set the first device in dev_pool as the first src_dev to be replaced
-       local src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
-
-       echo "dev_pool=$dev_pool"
-       echo "free_dev=$free_dev, src_dev=$src_dev"
-       while true; do
-               echo "Replacing $src_dev with $free_dev"
-               $BTRFS_UTIL_PROG replace start -fB $src_dev $free_dev $btrfs_mnt
-               if [ $? -ne 0 ]; then
-                       # don't update src_dev and free_dev if replace failed
-                       continue
-               fi
-               dev_pool="$dev_pool $free_dev"
-               dev_pool=`echo $dev_pool | sed -e "s# *$src_dev *##"`
-               free_dev=$src_dev
-               src_dev=`echo $dev_pool | $AWK_PROG '{print $1}'`
-       done
-}
-
-# find the right option to force output in bytes, older versions of btrfs-progs
-# print that by default, newer print human readable numbers with unit suffix
-_btrfs_qgroup_units()
-{
-       $BTRFS_UTIL_PROG qgroup show --help 2>&1 | grep -q -- --raw && echo "--raw"
-}
-
 # return device size in kb
 _get_device_size()
 {
@@ -3225,7 +3243,7 @@ _check_dmesg()
             -e "general protection fault:" \
             $seqres.dmesg
        if [ $? -eq 0 ]; then
-               echo "_check_dmesg: something found in dmesg (see $seqres.dmesg)"
+               _dump_err "_check_dmesg: something found in dmesg (see $seqres.dmesg)"
                return 1
        else
                rm -f $seqres.dmesg
@@ -3268,13 +3286,17 @@ init_rc()
                fi
        fi
 
-       if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
-       then
-               echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
-               # raw $DF_PROG cannot handle NFS/CIFS/overlay correctly
-               _df_device $TEST_DEV
-               exit 1
+       # Sanity check that TEST partition is not mounted at another mount point
+       # or as another fs type
+       _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR $FSTYP || exit 1
+       if [ -n "$SCRATCH_DEV" ]; then
+               # Sanity check that SCRATCH partition is not mounted at another
+               # mount point, because it is about to be unmounted and formatted.
+               # Another fs type for scratch is fine (bye bye old fs type).
+               _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT
+               [ $? -le 1 ] || exit 1
        fi
+
        # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io
        $XFS_IO_PROG -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \
                export XFS_IO_PROG="$XFS_IO_PROG -F"
@@ -3328,13 +3350,30 @@ _sysfs_dev()
        echo /sys/dev/block/$_maj:$_min
 }
 
-get_block_size()
+# Get the minimum block size of a file.  Usually this is the
+# minimum fs block size, but some filesystems (ocfs2) do block
+# mappings in larger units.
+_get_file_block_size()
+{
+       if [ -z $1 ] || [ ! -d $1 ]; then
+               echo "Missing mount point argument for _get_file_block_size"
+               exit 1
+       fi
+       if [ "$FSTYP" = "ocfs2" ]; then
+               stat -c '%o' $1
+       else
+               _get_block_size $1
+       fi
+}
+
+# Get the minimum block size of an fs.
+_get_block_size()
 {
        if [ -z $1 ] || [ ! -d $1 ]; then
-               echo "Missing mount point argument for get_block_size"
+               echo "Missing mount point argument for _get_block_size"
                exit 1
        fi
-       echo `stat -f -c %S $1`
+       stat -f -c %S $1
 }
 
 get_page_size()
@@ -3425,7 +3464,6 @@ _get_fs_sysfs_attr()
 }
 
 
-
 init_rc
 
 ################################################################################