xfs: test that the needsrepair feature works as advertised
[xfstests-dev.git] / common / xfs
index eab5e2929a90d7f3799f8ed80397a25b588f32db..cf4afb5341339ed295d1f27086c835dbef3c1a68 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,35 @@ _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'
+}
+
+_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.
@@ -234,9 +269,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()
@@ -265,6 +312,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=""
@@ -406,6 +461,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 "$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()
 {
@@ -422,14 +511,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"
 
@@ -441,6 +532,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"
@@ -459,7 +561,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
@@ -494,8 +596,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)"
@@ -504,6 +613,7 @@ _check_xfs_filesystem()
                        echo "*** end xfs_repair output"        >>$seqres.full
 
                        ok=0
+                       rebuild_ok=0
                fi
                rm -f $tmp.repair
 
@@ -515,8 +625,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
@@ -694,6 +811,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()
 {
@@ -877,6 +1004,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
 #
@@ -945,3 +1092,54 @@ _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 "$#"
+}