common/xfs: refactor commands to select a particular xfs backing device
[xfstests-dev.git] / common / xfs
index d9a9784f2fb9557a5747db88d3817a4a5947eae3..d7f2a0054d4652929bf6b769233e4d0317229761 100644 (file)
@@ -103,7 +103,7 @@ _scratch_find_xfs_min_logblocks()
        # try again without MKFS_OPTIONS because that's what _scratch_do_mkfs
        # will do if we pass in the log size option.
        if [ $mkfs_status -ne 0 ] &&
-          ! grep -q 'log size.*too small, minimum' $tmp.mkfserr; then
+          ! egrep -q '(log size.*too small, minimum|external log device.*too small, must be)' $tmp.mkfserr; then
                eval "$mkfs_cmd $extra_mkfs_options $SCRATCH_DEV" \
                        2>$tmp.mkfserr 1>$tmp.mkfsstd
                mkfs_status=$?
@@ -126,6 +126,12 @@ _scratch_find_xfs_min_logblocks()
                rm -f $tmp.mkfsstd $tmp.mkfserr
                return
        fi
+       if grep -q 'external log device.*too small, must be' $tmp.mkfserr; then
+               grep 'external log device.*too small, must be' $tmp.mkfserr | \
+                       sed -e 's/^.*must be at least \([0-9]*\) blocks/\1/g'
+               rm -f $tmp.mkfsstd $tmp.mkfserr
+               return
+       fi
 
        # Don't know what to do, so fail
        echo "Cannot determine minimum log size" >&2
@@ -168,6 +174,60 @@ _scratch_mkfs_xfs()
        return $mkfs_status
 }
 
+# Get the size of an allocation unit of a file.  Normally this is just the
+# block size of the file, but for realtime files, this is the realtime extent
+# size.
+_xfs_get_file_block_size()
+{
+       local path="$1"
+
+       if ! ($XFS_IO_PROG -c "stat -v" "$path" 2>&1 | egrep -q '(rt-inherit|realtime)'); then
+               _get_block_size "$path"
+               return
+       fi
+
+       # Otherwise, call xfs_info until we find a mount point or the root.
+       path="$(readlink -m "$path")"
+       while ! $XFS_INFO_PROG "$path" &>/dev/null && [ "$path" != "/" ]; do
+               path="$(dirname "$path")"
+       done
+       $XFS_INFO_PROG "$path" | grep realtime | sed -e 's/^.*extsz=\([0-9]*\).*$/\1/g'
+}
+
+# Set or clear the realtime status of every supplied path.  The first argument
+# is either 'data' or 'realtime'.  All other arguments should be paths to
+# existing directories or empty regular files.
+#
+# For each directory, each file subsequently created will target the given
+# device for file data allocations.  For each empty regular file, each
+# subsequent file data allocation will be on the given device.
+_xfs_force_bdev()
+{
+       local device="$1"
+       shift
+       local chattr_arg=""
+
+       case "$device" in
+       "data")         chattr_arg="-t";;
+       "realtime")     chattr_arg="+t";;
+       *)
+               echo "${device}: Don't know what device this is?"
+               return 1
+               ;;
+       esac
+
+       $XFS_IO_PROG -c "chattr $chattr_arg" "$@"
+}
+
+_xfs_get_fsxattr()
+{
+       local field="$1"
+       local path="$2"
+
+       local value=$($XFS_IO_PROG -c "stat" "$path" | grep -w "$field")
+       echo ${value##fsxattr.${field} = }
+}
+
 # xfs_check script is planned to be deprecated. But, we want to
 # be able to invoke "xfs_check" behavior in xfstests in order to
 # maintain the current verification levels.
@@ -218,12 +278,37 @@ _scratch_xfs_db()
        $XFS_DB_PROG "$@" $(_scratch_xfs_db_options)
 }
 
+_test_xfs_db_options()
+{
+       TEST_OPTIONS=""
+       [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
+               TEST_OPTIONS="-l$TEST_LOGDEV"
+       echo $TEST_OPTIONS $* $TEST_DEV
+}
+
+_test_xfs_db()
+{
+       $XFS_DB_PROG "$@" $(_test_xfs_db_options)
+}
+
 _scratch_xfs_admin()
 {
        local options=("$SCRATCH_DEV")
+       local rt_opts=()
        [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
                options+=("$SCRATCH_LOGDEV")
-       $XFS_ADMIN_PROG "$@" "${options[@]}"
+       if [ "$USE_EXTERNAL" = yes ] && [ -n "$SCRATCH_RTDEV" ]; then
+               $XFS_ADMIN_PROG --help 2>&1 | grep -q 'rtdev' || \
+                       _notrun 'xfs_admin does not support rt devices'
+               rt_opts+=(-r "$SCRATCH_RTDEV")
+       fi
+
+       # xfs_admin in xfsprogs 5.11 has a bug where an external log device
+       # forces xfs_db to be invoked, potentially with zero command arguments.
+       # When this happens, xfs_db will wait for input on stdin, which causes
+       # fstests to hang.  Since xfs_admin is not an interactive tool, we
+       # can redirect stdin from /dev/null to prevent this issue.
+       $XFS_ADMIN_PROG "${rt_opts[@]}" "$@" "${options[@]}" < /dev/null
 }
 
 _scratch_xfs_logprint()
@@ -252,6 +337,14 @@ _scratch_xfs_check()
        _xfs_check $SCRATCH_OPTIONS $* $SCRATCH_DEV
 }
 
+# Check for secret debugging hooks in xfs_repair
+_require_libxfs_debug_flag() {
+       local hook="$1"
+
+       grep -q "$hook" "$(type -P xfs_repair)" || \
+               _notrun "libxfs debug hook $hook not detected?"
+}
+
 _scratch_xfs_repair()
 {
        SCRATCH_OPTIONS=""
@@ -393,6 +486,40 @@ _supports_xfs_scrub()
        return 0
 }
 
+# Save a snapshot of a corrupt xfs filesystem for later debugging.
+_xfs_metadump() {
+       local metadump="$1"
+       local device="$2"
+       local logdev="$3"
+       local compressopt="$4"
+       shift; shift; shift; shift
+       local options="$@"
+       test -z "$options" && options="-a -o"
+
+       if [ "$logdev" != "none" ]; then
+               options="$options -l $logdev"
+       fi
+
+       $XFS_METADUMP_PROG $options "$device" "$metadump"
+       res=$?
+       [ "$compressopt" = "compress" ] && [ -n "$DUMP_COMPRESSOR" ] &&
+               $DUMP_COMPRESSOR -f "$metadump" &> /dev/null
+       return $res
+}
+
+# Snapshot the metadata on the scratch device
+_scratch_xfs_metadump()
+{
+       local metadump=$1
+       shift
+       local logdev=none
+
+       [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
+               logdev=$SCRATCH_LOGDEV
+
+       _xfs_metadump "$metadump" "$SCRATCH_DEV" "$logdev" nocompress "$@"
+}
+
 # run xfs_check and friends on a FS.
 _check_xfs_filesystem()
 {
@@ -409,14 +536,16 @@ _check_xfs_filesystem()
                extra_options="-f"
        fi
 
-       if [ "$2" != "none" ]; then
-               extra_log_options="-l$2"
-               extra_mount_options="-ologdev=$2"
+       local logdev="$2"
+       if [ "$logdev" != "none" ]; then
+               extra_log_options="-l$logdev"
+               extra_mount_options="-ologdev=$logdev"
        fi
 
-       if [ "$3" != "none" ]; then
-               extra_rt_options="-r$3"
-               extra_mount_options=$extra_mount_options" -ortdev=$3"
+       local rtdev="$3"
+       if [ "$rtdev" != "none" ]; then
+               extra_rt_options="-r$rtdev"
+               extra_mount_options=$extra_mount_options" -ortdev=$rtdev"
        fi
        extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"
 
@@ -428,6 +557,17 @@ _check_xfs_filesystem()
        # Run online scrub if we can.
        mntpt="$(_is_dev_mounted $device)"
        if [ -n "$mntpt" ] && _supports_xfs_scrub "$mntpt" "$device"; then
+               # Tests can create a scenario in which a call to syncfs() issued
+               # at the end of the execution of the test script would return an
+               # error code. xfs_scrub internally calls syncfs() before
+               # starting the actual online consistency check operation. Since
+               # such a call to syncfs() fails, xfs_scrub ends up returning
+               # without performing consistency checks on the test
+               # filesystem. This can mask a possible on-disk data structure
+               # corruption. Hence consume such a possible syncfs() failure
+               # before executing a scrub operation.
+               $XFS_IO_PROG -c syncfs $mntpt >> $seqres.full 2>&1
+
                "$XFS_SCRUB_PROG" $scrubflag -v -d -n $mntpt > $tmp.scrub 2>&1
                if [ $? -ne 0 ]; then
                        _log_err "_check_xfs_filesystem: filesystem on $device failed scrub"
@@ -446,7 +586,7 @@ _check_xfs_filesystem()
 
        $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
                | tee $tmp.logprint | grep -q "<CLEAN>"
-       if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]; then
+       if [ $? -ne 0 ]; then
                _log_err "_check_xfs_filesystem: filesystem on $device has dirty log"
                echo "*** xfs_logprint -t output ***"   >>$seqres.full
                cat $tmp.logprint                       >>$seqres.full
@@ -481,8 +621,15 @@ _check_xfs_filesystem()
        fi
        rm -f $tmp.fs_check $tmp.logprint $tmp.repair
 
+       if [ "$ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
+               local flatdev="$(basename "$device")"
+               _xfs_metadump "$seqres.$flatdev.check.md" "$device" "$logdev" \
+                       compress >> $seqres.full
+       fi
+
        # Optionally test the index rebuilding behavior.
        if [ -n "$TEST_XFS_REPAIR_REBUILD" ]; then
+               rebuild_ok=1
                $XFS_REPAIR_PROG $extra_options $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
                if [ $? -ne 0 ]; then
                        _log_err "_check_xfs_filesystem: filesystem on $device is inconsistent (rebuild)"
@@ -491,6 +638,7 @@ _check_xfs_filesystem()
                        echo "*** end xfs_repair output"        >>$seqres.full
 
                        ok=0
+                       rebuild_ok=0
                fi
                rm -f $tmp.repair
 
@@ -502,8 +650,15 @@ _check_xfs_filesystem()
                        echo "*** end xfs_repair output"        >>$seqres.full
 
                        ok=0
+                       rebuild_ok=0
                fi
                rm -f $tmp.repair
+
+               if [ "$rebuild_ok" -ne 1 ] && [ "$DUMP_CORRUPT_FS" = "1" ]; then
+                       local flatdev="$(basename "$device")"
+                       _xfs_metadump "$seqres.$flatdev.rebuild.md" "$device" \
+                               "$logdev" compress >> $seqres.full
+               fi
        fi
 
        if [ $ok -eq 0 ]; then
@@ -656,6 +811,26 @@ _require_xfs_mkfs_without_validation()
        fi
 }
 
+_require_scratch_xfs_shrink()
+{
+       _require_scratch
+       _require_command "$XFS_GROWFS_PROG" xfs_growfs
+
+       _scratch_mkfs_xfs | _filter_mkfs 2>$tmp.mkfs >/dev/null
+       . $tmp.mkfs
+       _scratch_mount
+       # here just to check if kernel supports, no need do more extra work
+       local errmsg=$($XFS_GROWFS_PROG -D$((dblocks-1)) "$SCRATCH_MNT" 2>&1)
+       if [ "$?" -ne 0 ]; then
+               echo "$errmsg" | grep 'XFS_IOC_FSGROWFSDATA xfsctl failed: Invalid argument' > /dev/null && \
+                       _notrun "kernel does not support shrinking"
+               echo "$errmsg" | grep 'data size .* too small, old size is ' > /dev/null && \
+                       _notrun "xfsprogs does not support shrinking"
+               _fail "$XFS_GROWFS_PROG failed unexpectedly: $errmsg"
+       fi
+       _scratch_unmount
+}
+
 # XFS ability to change UUIDs on V5/CRC filesystems
 #
 _require_meta_uuid()
@@ -681,6 +856,16 @@ _require_xfs_mkfs_ciname()
                || _notrun "need case-insensitive naming support in mkfs.xfs"
 }
 
+# this test requires mkfs.xfs have configuration file support
+_require_xfs_mkfs_cfgfile()
+{
+       echo > /tmp/a
+       _scratch_mkfs_xfs_supported -c options=/tmp/a >/dev/null 2>&1
+       res=$?
+       rm -rf /tmp/a
+       test $res -eq 0 || _notrun "need configuration file support in mkfs.xfs"
+}
+
 # XFS_DEBUG requirements
 _require_xfs_debug()
 {
@@ -864,6 +1049,26 @@ _scratch_get_bmx_prefix() {
        return 1
 }
 
+_scratch_get_iext_count()
+{
+       local ino=$1
+       local whichfork=$2
+       local field=""
+
+       case $whichfork in
+               "attr")
+                       field=core.naextents
+                       ;;
+               "data")
+                       field=core.nextents
+                       ;;
+               *)
+                       return 1
+       esac
+
+       _scratch_xfs_get_metadata_field $field "inode $ino"
+}
+
 #
 # Ensures that we don't pass any mount options incompatible with XFS v4
 #
@@ -932,3 +1137,124 @@ _try_wipe_scratch_xfs()
        fi
        rm -f $tmp.mkfs
 }
+
+_require_xfs_copy()
+{
+       [ -n "$XFS_COPY_PROG" ] || _notrun "xfs_copy binary not yet installed"
+       [ "$USE_EXTERNAL" = yes ] && \
+               _notrun "Cannot xfs_copy with external devices"
+}
+
+__xfs_cowgc_interval_knob1="/proc/sys/fs/xfs/speculative_cow_prealloc_lifetime"
+__xfs_cowgc_interval_knob2="/proc/sys/fs/xfs/speculative_prealloc_lifetime"
+
+_xfs_set_cowgc_interval() {
+       if [ -w $__xfs_cowgc_interval_knob1 ]; then
+               echo "$@" > $__xfs_cowgc_interval_knob1
+       elif [ -w $__xfs_cowgc_interval_knob2 ]; then
+               echo "$@" > $__xfs_cowgc_interval_knob2
+       else
+               _fail "Can't find cowgc interval procfs knob?"
+       fi
+}
+
+_xfs_get_cowgc_interval() {
+       if [ -w $__xfs_cowgc_interval_knob1 ]; then
+               cat $__xfs_cowgc_interval_knob1
+       elif [ -w $__xfs_cowgc_interval_knob2 ]; then
+               cat $__xfs_cowgc_interval_knob2
+       else
+               _fail "Can't find cowgc interval procfs knob?"
+       fi
+}
+
+# Print the status of the given features on the scratch filesystem.
+# Returns 0 if all features are found, 1 otherwise.
+_check_scratch_xfs_features()
+{
+       local features="$(_scratch_xfs_db -c 'version')"
+       local output=("FEATURES:")
+       local found=0
+
+       for feature in "$@"; do
+               local status="NO"
+               if echo "${features}" | grep -q -w "${feature}"; then
+                       status="YES"
+                       found=$((found + 1))
+               fi
+               output+=("${feature}:${status}")
+       done
+
+       echo "${output[@]}"
+       test "${found}" -eq "$#"
+}
+
+# Skip a test if any of the given fs features aren't present on the scratch
+# filesystem.  The scratch fs must have been formatted already.
+_require_scratch_xfs_features()
+{
+       local features="$(_scratch_xfs_db -c 'version' 2>/dev/null)"
+
+       for feature in "$@"; do
+               echo "${features}" | grep -q -w "${feature}" ||
+                       _notrun "Missing scratch feature: ${feature}"
+       done
+}
+
+# Decide if xfs_repair knows how to set (or clear) a filesystem feature.
+_require_xfs_repair_upgrade()
+{
+       local type="$1"
+
+       $XFS_REPAIR_PROG -c "$type=garbagevalue" 2>&1 | \
+               grep -q 'unknown option' && \
+               _notrun "xfs_repair does not support upgrading fs with $type"
+}
+
+# Require that the scratch device exists, that mkfs can format with inobtcount
+# enabled, and that the kernel can mount such a filesystem.
+_require_scratch_xfs_inobtcount()
+{
+       _require_scratch
+
+       _scratch_mkfs -m inobtcount=1 &> /dev/null || \
+               _notrun "mkfs.xfs doesn't support inobtcount feature"
+       _try_scratch_mount || \
+               _notrun "kernel doesn't support xfs inobtcount feature"
+       _scratch_unmount
+}
+
+_xfs_timestamp_range()
+{
+       local device="$1"
+       local use_db=0
+       local dbprog="$XFS_DB_PROG $device"
+       test "$device" = "$SCRATCH_DEV" && dbprog=_scratch_xfs_db
+
+       $dbprog -f -c 'help timelimit' | grep -v -q 'not found' && use_db=1
+       if [ $use_db -eq 0 ]; then
+               # The "timelimit" command was added to xfs_db at the same time
+               # that bigtime was added to xfsprogs.  Therefore, we can assume
+               # the old timestamp range if the command isn't present.
+               echo "-$((1<<31)) $(((1<<31)-1))"
+       else
+               $dbprog -f -c 'timelimit --compact' | \
+                       awk '{printf("%s %s", $1, $2);}'
+       fi
+}
+
+# Require that the scratch device exists, that mkfs can format with bigtime
+# enabled, that the kernel can mount such a filesystem, and that xfs_info
+# advertises the presence of that feature.
+_require_scratch_xfs_bigtime()
+{
+       _require_scratch
+
+       _scratch_mkfs -m bigtime=1 &>/dev/null || \
+               _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?"
+       _scratch_unmount
+}