common/xfs: Fix _require_scratch_xfs_shrink bug
[xfstests-dev.git] / common / xfs
index 4469f045e082211aa7099754e7d234e898738545..c5e3942721dbc1cd0b63462b73aa47c568e2ec40 100644 (file)
@@ -194,6 +194,40 @@ _xfs_get_file_block_size()
        $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.
@@ -260,9 +294,21 @@ _test_xfs_db()
 _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()
@@ -291,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=""
@@ -449,7 +503,7 @@ _xfs_metadump() {
        $XFS_METADUMP_PROG $options "$device" "$metadump"
        res=$?
        [ "$compressopt" = "compress" ] && [ -n "$DUMP_COMPRESSOR" ] &&
-               $DUMP_COMPRESSOR "$metadump" &> /dev/null
+               $DUMP_COMPRESSOR -f "$metadump" &> /dev/null
        return $res
 }
 
@@ -757,6 +811,27 @@ _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
+       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()
@@ -975,6 +1050,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
 #
@@ -1050,3 +1145,117 @@ _require_xfs_copy()
        [ "$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
+}