##/bin/sh
#-----------------------------------------------------------------------
#  Copyright (c) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
#  USA
#
#  Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
#  Mountain View, CA 94043, USA, or: http://www.sgi.com
#-----------------------------------------------------------------------

dd()
{
   if [ "$HOSTOS" == "Linux" ]
   then	
	command dd --help | grep noxfer > /dev/null 2>&1
	
	if [ "$?" -eq 0 ]
	    then
		command dd status=noxfer $@
	    else
		command dd $@
    	fi
   else
	command dd $@
   fi
}

_mount_opts()
{
    case $FSTYP in
    xfs)
	export MOUNT_OPTIONS=$XFS_MOUNT_OPTIONS
	;;
    udf)
	export MOUNT_OPTIONS=$UDF_MOUNT_OPTIONS
	;;
    nfs)
	export MOUNT_OPTIONS=$NFS_MOUNT_OPTIONS
	;;
    *)
	;;
    esac
}

_mkfs_opts()
{
    case $FSTYP in
    xfs)
	export MKFS_OPTIONS=$XFS_MKFS_OPTIONS
	;;
    udf)
	[ ! -z "$udf_fsize" ] && \
	    UDF_MKFS_OPTIONS="$UDF_MKFS_OPTIONS -s $udf_fsize"
	export MKFS_OPTIONS=$UDF_MKFS_OPTIONS
	;;
    nfs)
	export MKFS_OPTIONS=$NFS_MKFS_OPTIONS
	;;
    *)
	;;
    esac
}

[ -z "$FSTYP" ] && FSTYP=xfs
[ -z "$MOUNT_OPTIONS" ] && _mount_opts
[ -z "$MKFS_OPTIONS" ] && _mkfs_opts


# we need common.config
if [ "$iam" != "check" ]
then
    if ! . ./common.config
        then
        echo "$iam: failed to source common.config"
        exit 1
    fi
fi

# make sure we have a standard umask
umask 022

_mount()
{
    $MOUNT_PROG `_mount_ops_filter $*`
}

_scratch_options()
{
    type=$1
    SCRATCH_OPTIONS=""

    if [ "$FSTYP" != "xfs" ]; then
        return
    fi

    case $type in
    mkfs)
	[ "$HOSTOS" != "IRIX" ] && SCRATCH_OPTIONS="$SCRATCH_OPTIONS -f"
	rt_opt="-r"
        log_opt="-l"
	;;
    mount)
	rt_opt="-o"
        log_opt="-o"
	;;
    esac
    [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
	SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${rt_opt}rtdev=$SCRATCH_RTDEV"
    [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
	SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}logdev=$SCRATCH_LOGDEV"
}

_test_options()
{
    type=$1
    TEST_OPTIONS=""

    if [ "$FSTYP" != "xfs" ]; then
        return
    fi

    case $type in
    mkfs)
	rt_opt="-r"
        log_opt="-l"
	;;
    mount)
	rt_opt="-o"
        log_opt="-o"
	;;
    esac
    [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
	TEST_OPTIONS="$TEST_OPTIONS ${rt_opt}rtdev=$TEST_RTDEV"
    [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
	TEST_OPTIONS="$TEST_OPTIONS ${log_opt}logdev=$TEST_LOGDEV"
}

_mount_ops_filter()
{
    params="$*"
    
    #get mount point to handle dmapi mtpt option correctly
    last_index=`expr $# - 1`
    [ $last_index -gt 0 ] && shift $last_index
    FS_ESCAPED=$1
    
    # irix is fussy about how it is fed its mount options
    # - multiple -o's are not allowed
    # - no spaces between comma delimitered options
    # the sed script replaces all -o's (except the first) with a comma
    # not required for linux, but won't hurt
    
    echo $params | sed -e 's/[[:space:]]*-o[[:space:]]*/UnIqUe/1; s/[[:space:]]*-o[[:space:]]*/,/g; s/UnIqUe/ -o /1' \
        | sed -e 's/dmapi/dmi/' \
        | $PERL_PROG -ne "s#mtpt=[^,|^\n|^\s]*#mtpt=$FS_ESCAPED\1\2#; print;"

}

_scratch_mount_options()
{
    _scratch_options mount

    echo $SCRATCH_OPTIONS $MOUNT_OPTIONS $* $SCRATCH_DEV $SCRATCH_MNT
}

_scratch_mount()
{
    _mount -t $FSTYP `_scratch_mount_options $*`
}

_test_mount()
{
    _test_options mount
    _mount -t $FSTYP $TEST_OPTIONS $TEST_FS_MOUNT_OPTS $* $TEST_DEV $TEST_DIR
}

_scratch_mkfs_options()
{
    _scratch_options mkfs
    echo $SCRATCH_OPTIONS $MKFS_OPTIONS $* $SCRATCH_DEV
}

_scratch_mkfs_xfs()
{
    # extra mkfs options can be added by tests
    local extra_mkfs_options=$*

    local tmp_dir=/tmp/

    _scratch_options mkfs

    # save mkfs output in case conflict means we need to run again.
    # only the output for the mkfs that applies should be shown
    $MKFS_XFS_PROG $SCRATCH_OPTIONS $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 "** mkfs failed with extra mkfs options added to \"$MKFS_OPTIONS\" by test $seq **" \
            >>$here/$seq.full
        echo "** attempting to mkfs using only test $seq options: $extra_mkfs_options **" \
            >>$here/$seq.full
        # running mkfs again. overwrite previous mkfs output files
        $MKFS_XFS_PROG $SCRATCH_OPTIONS $extra_mkfs_options $SCRATCH_DEV \
            2>$tmp_dir.mkfserr 1>$tmp_dir.mkfsstd
        mkfs_status=$?
    fi

    # output stored mkfs output
    cat $tmp_dir.mkfserr >&2
    cat $tmp_dir.mkfsstd
    rm -f $tmp_dir.mkfserr $tmp_dir.mkfsstd

    if [ "$USE_BIG_LOOPFS" = yes ]; then
        [ -z "$RETAIN_AG_BYTES" ] && RETAIN_AG_BYTES=0
        ./tools/ag-wipe -q -r $RETAIN_AG_BYTES $SCRATCH_DEV
    fi

    return $mkfs_status
}

_scratch_mkfs()
{
    case $FSTYP in
    xfs)
        _scratch_mkfs_xfs $*
	;;
    nfs*)
	# do nothing for nfs
	;;
    udf)
        $MKFS_UDF_PROG $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
	;;
    *)
	/sbin/mkfs -t $FSTYP -- $MKFS_OPTIONS $* $SCRATCH_DEV > /dev/null
	;;
    esac
}

_scratch_xfs_db_options()
{
    SCRATCH_OPTIONS=""
    [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
        SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
    echo $SCRATCH_OPTIONS $* $SCRATCH_DEV
}

_scratch_xfs_logprint()
{
    SCRATCH_OPTIONS=""
    [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
        SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
    $XFS_LOGPRINT_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
}

_scratch_xfs_repair()
{
    SCRATCH_OPTIONS=""
    [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
        SCRATCH_OPTIONS="-l$SCRATCH_LOGDEV"
    [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
        SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -r$SCRATCH_RTDEV"
    [ "$USE_BIG_LOOPFS" = yes ] && SCRATCH_OPTIONS=$SCRATCH_OPTIONS" -t"
    $XFS_REPAIR_PROG $SCRATCH_OPTIONS $* $SCRATCH_DEV
}

_get_pids_by_name()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _get_pids_by_name process-name" 1>&2
	exit 1
    fi

    # Algorithm ... all ps(1) variants have a time of the form MM:SS or
    # HH:MM:SS before the psargs field, use this as the search anchor.
    #
    # Matches with $1 (process-name) occur if the first psarg is $1
    # or ends in /$1 ... the matching uses sed's regular expressions,
    # so passing a regex into $1 will work.

    ps $PS_ALL_FLAGS \
    | sed -n \
	-e 's/$/ /' \
	-e 's/[ 	][ 	]*/ /g' \
	-e 's/^ //' \
	-e 's/^[^ ]* //' \
	-e "/[0-9]:[0-9][0-9]  *[^ ]*\/$1 /s/ .*//p" \
	-e "/[0-9]:[0-9][0-9]  *$1 /s/ .*//p"
}

# fqdn for localhost
#
_get_fqdn()
{
    host=`hostname`
    $NSLOOKUP_PROG $host | $AWK_PROG '{ if ($1 == "Name:") print $2 }'
}

# fix malloc libs output
#
_fix_malloc()
{
    # filter out the Electric Fence notice
    $PERL_PROG -e '
        while (<>) {
            if (defined $o && /^\s+Electric Fence/) {
                chomp($o);
                print "$o";
                undef $o;
                next;
            }
            print $o if (defined $o);

            $o=$_;
        }
        print $o if (defined $o);
    '
}

# check if run as root
#
_need_to_be_root()
{
    id=`id | $SED_PROG -e 's/(.*//' -e 's/.*=//'`
    if [ "$id" -ne 0 ]
    then
	echo "Arrgh ... you need to be root (not uid=$id) to run this test"
	exit 1
    fi
}


#
# _df_device : get an IRIX style df line for a given device
#
#       - returns "" if not mounted
#       - returns fs type in field two (ala IRIX)
#       - joins line together if split by fancy df formatting
#       - strips header etc
#

_df_device()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _df_device device" 1>&2
	exit 1
    fi

    $DF_PROG 2>/dev/null | $AWK_PROG -v what=$1 '
        match($1,what) && NF==1 {
            v=$1
            getline
            print v, $0
            exit
        }
        match($1,what) {
            print
            exit
        }
    '
}

#
# _df_dir : get an IRIX style df line for device where a directory resides
#
#       - returns fs type in field two (ala IRIX)
#       - joins line together if split by fancy df formatting
#       - strips header etc
#

_df_dir()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _df_dir device" 1>&2
	exit 1
    fi

    $DF_PROG $1 2>/dev/null | $AWK_PROG -v what=$1 '
        NR == 2 && NF==1 {
            v=$1
            getline
            print v, $0;
            exit 0
        }
        NR == 2 {
            print;
            exit 0
        }
        {}
    '
    # otherwise, nada
}

# return percentage used disk space for mounted device

_used()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _used device" 1>&2
	exit 1
    fi

    _df_device $1 | $AWK_PROG '{ sub("%", "") ; print $6 }'
}

# return the FS type of a mounted device
#
_fs_type()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _fs_type device" 1>&2
	exit 1
    fi

    _df_device $1 | $AWK_PROG '{ print $2 }'
}

# return the FS mount options of a mounted device
#
# should write a version which just parses the output of mount for IRIX
# compatibility, but since this isn't used at all, at the moment I'll leave
# this for now
#
_fs_options()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _fs_options device" 1>&2
	exit 1
    fi

    $AWK_PROG -v dev=$1 '
        match($1,dev) { print $4 }
    ' </proc/mounts
}

# returns device number if a file is a block device
#
_is_block_dev()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _is_block_dev dev" 1>&2
	exit 1
    fi

    [ -b $1 ] && src/lstat64 $1 | $AWK_PROG '/Device type:/ { print $9 }'
}

# Do a command, log it to $seq.full, optionally test return status
# and die if command fails. If called with one argument _do executes the
# command, logs it, and returns its exit status. With two arguments _do
# first prints the message passed in the first argument, and then "done"
# or "fail" depending on the return status of the command passed in the
# second argument. If the command fails and the variable _do_die_on_error
# is set to "always" or the two argument form is used and _do_die_on_error
# is set to "message_only" _do will print an error message to
# $seq.out and exit.

_do()
{
    if [ $# -eq 1 ]; then
	_cmd=$1
    elif [ $# -eq 2 ]; then
	_note=$1
	_cmd=$2
	echo -n "$_note... "
    else
	echo "Usage: _do [note] cmd" 1>&2
	status=1; exit
    fi

    (eval "echo '---' \"$_cmd\"") >>$here/$seq.full
    (eval "$_cmd") >$tmp._out 2>&1; ret=$?
    cat $tmp._out | _fix_malloc >>$here/$seq.full
    if [ $# -eq 2 ]; then
	if [ $ret -eq 0 ]; then
	    echo "done"
	else
	    echo "fail"
	fi
    fi
    if [ $ret -ne 0  ] \
	&& [ "$_do_die_on_error" = "always" \
	    -o \( $# -eq 2 -a "$_do_die_on_error" = "message_only" \) ]
    then
	[ $# -ne 2 ] && echo
	eval "echo \"$_cmd\" failed \(returned $ret\): see $seq.full"
	status=1; exit
    fi

    return $ret
}

# bail out, setting up .notrun file
#
_notrun()
{
    echo "$*" >$seq.notrun
    echo "$seq not run: $*"
    status=0
    exit
}

# just plain bail out
#
_fail()
{
    echo "$*" | tee -a $here/$seq.full
    echo "(see $seq.full for details)"
    status=1
    exit 1
}

# tests whether $FSTYP is one of the supported filesystems for a test
#
_supported_fs()
{
    for f
    do
	if [ "$f" = "$FSTYP" ]
	then
	    return
	fi
    done

    _notrun "not suitable for this filesystem type: $FSTYP"
}

# tests whether $FSTYP is one of the supported OSes for a test
#
_supported_os()
{
    for h
    do
	if [ "$h" = "$HOSTOS" ]
	then
	    return
	fi
    done

    _notrun "not suitable for this OS: $HOSTOS"
}

# this test needs a scratch partition - check we're ok & unmount it
#
_require_scratch()
{
    case "$FSTYP" in
	xfs|udf)
		 if [ -z "$SCRATCH_DEV" -o "`_is_block_dev $SCRATCH_DEV`" = "" ]
		 then
		     _notrun "this test requires a valid \$SCRATCH_DEV"
		 fi
		 if [ "`_is_block_dev $SCRATCH_DEV`" = "`_is_block_dev $TEST_DEV`" ]
		 then
		     _notrun "this test requires a valid \$SCRATCH_DEV"
		 fi
		 ;;
	nfs*|ext2|ext3|reiserfs)
		 echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
		 if [ ! -z "$SCRATCH_DEV" -a ! -b "$SCRATCH_DEV" -a "$?" != "0" ]
		 then
		     _notrun "this test requires a valid \$SCRATCH_DEV"
		 fi
		 ;;
	*)
		 _notrun "\$FSTYP ($FSTYP) unknown or not specified"
		 ;;
    esac

    # mounted?
    if _mount | grep -q $SCRATCH_DEV
    then
        # if it's mounted, make sure its on $SCRATCH_MNT
        if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
        then
            echo "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
            exit 1
        fi
        # and then unmount it
        if ! $UMOUNT_PROG $SCRATCH_DEV
        then
            echo "failed to unmount $SCRATCH_DEV"
            exit 1
        fi
    fi
}

# this test needs a logdev
#
_require_logdev()
{
    [ -z "$SCRATCH_LOGDEV" -o ! -b "$SCRATCH_LOGDEV" ] && \
        _notrun "This test requires a valid \$SCRATCH_LOGDEV"
    [ "$USE_EXTERNAL" != yes ] && \
        _notrun "This test requires USE_EXTERNAL to be enabled"

    # ensure its not mounted
    $UMOUNT_PROG $SCRATCH_LOGDEV 2>/dev/null
}

# this test requires loopback device support
#
_require_loop()
{
    if [ "$HOSTOS" != "Linux" ]
    then
	_notrun "This test requires linux for loopback device support"
    fi

    modprobe loop >/dev/null 2>&1
    if grep loop /proc/devices >/dev/null 2>&1
    then
	:
    else
	_notrun "This test requires loopback device support"
    fi
}

# this test requires that (large) loopback device files are not in use
#
_require_nobigloopfs()
{
    [ "$USE_BIG_LOOPFS" = yes ] && \
	_notrun "Large filesystem testing in progress, skipped this test"
}

# this test requires that a realtime subvolume is in use, and
# that the kernel supports realtime as well.
#
_require_realtime()
{
    [ "$USE_EXTERNAL" = yes ] || \
	_notrun "External volumes not in use, skipped this test"
    [ "$SCRATCH_RTDEV" = "" ] && \
	_notrun "Realtime device required, skipped this test"
}

# this test requires that a specified command (executable) exists
#
_require_command()
{
    [ -x "$1" ] || _notrun "$1 utility required, skipped this test"
}

# this test requires that external log/realtime devices are not in use
#
_require_nonexternal()
{
    [ "$USE_EXTERNAL" = yes ] && \
	_notrun "External device testing in progress, skipped this test"
}

# check for the fsgqa user on the machine
#
_require_user()
{
    qa_user=fsgqa
    cat /etc/passwd | grep -q $qa_user
    [ "$?" == "0" ] || _notrun "$qa_user user not defined."
}

# check that a FS is mounted as XFS. if so, return mount point
#
_xfs_mounted()
{
    if [ $# -ne 1 ]
    then
	echo "Usage: _xfs_mounted device" 1>&2
	exit 1
    fi

    device=$1

    if _mount | grep "$device " | $AWK_PROG '
        /type xfs/  { print $3 ; exit 0 }
        END         { exit 1 }
    '
    then
        echo "_xfs_mounted: $device is not a mounted XFS FS"
        exit 1
    fi
}


# remount a FS to a new mode (ro or rw)
#
_remount()
{
    if [ $# -ne 2 ]
    then
	echo "Usage: _remount device ro/rw" 1>&2
	exit 1
    fi
    device=$1
    mode=$2

    if ! mount -o remount,$mode $device
    then
        echo "_remount: failed to remount filesystem on $device as $mode"
        exit 1
    fi
}

# run xfs_check and friends on a FS.
#
# if the filesystem is mounted, it's either remounted ro before being
# checked or it's unmounted and then remounted
#

USE_REMOUNT=0

_check_xfs_filesystem()
{
    if [ $# -ne 3 ]
    then
	echo "Usage: _check_xfs_filesystem device <logdev>|none <rtdev>|none" 1>&2
	exit 1
    fi

    extra_mount_options=""
    device=$1
    if [ "$2" != "none" ]; then
       	extra_log_options="-l$2"
        extra_mount_options="-ologdev=$2"
    fi

    if [ "$3" != "none" ]; then
       	extra_rt_options="-r$3"
        extra_mount_options=$extra_mount_options" -ortdev=$3"
    fi
    extra_mount_options=$extra_mount_options" $MOUNT_OPTIONS"

    [ "$FSTYP" != xfs ] && return 0
    testoption=""
    [ "$USE_BIG_LOOPFS" = yes ] && testoption=-t

    type=`_fs_type $device`
    ok=1

    if [ "$type" = "xfs" ]
    then
        # mounted...

        if [ $USE_REMOUNT -eq 0 ]
        then
            mountpoint=`_xfs_mounted $device`
            $UMOUNT_PROG $device
        else
            _remount $device ro
        fi
    fi

    $XFS_LOGPRINT_PROG -t $extra_log_options $device 2>&1 \
                | tee $tmp.logprint | grep -q "<CLEAN>"
    if [ $? -ne 0 -a "$HOSTOS" = "Linux" ]
    then
        echo "_check_xfs_filesystem: filesystem on $device has dirty log (see $seq.full)"

        echo "_check_xfs_filesystem: filesystem on $device has dirty log"   >>$here/$seq.full
        echo "*** xfs_logprint -t output ***"                   >>$here/$seq.full
        cat $tmp.logprint                                       >>$here/$seq.full
        echo "*** end xfs_logprint output"                      >>$here/$seq.full

        ok=0
    fi

    $XFS_CHECK_PROG $testoption $extra_log_options $device 2>&1 |\
	 _fix_malloc >$tmp.fs_check
    if [ -s $tmp.fs_check ]
    then
        echo "_check_xfs_filesystem: filesystem on $device is inconsistent (c) (see $seq.full)"

        echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
        echo "*** xfs_check output ***"                         >>$here/$seq.full
        cat $tmp.fs_check                                       >>$here/$seq.full
        echo "*** end xfs_check output"                         >>$here/$seq.full

        ok=0
    fi
    # repair doesn't scale massively at this stage, optionally skip it for now
    [ "$USE_BIG_LOOPFS" = yes ] || \
    $XFS_REPAIR_PROG -n $extra_log_options $extra_rt_options $device >$tmp.repair 2>&1
    if [ $? -ne 0 ]
    then
        echo "_check_xfs_filesystem: filesystem on $device is inconsistent (r) (see $seq.full)"

        echo "_check_xfs_filesystem: filesystem on $device is inconsistent" >>$here/$seq.full
        echo "*** xfs_repair -n output ***"                     >>$here/$seq.full
        cat $tmp.repair | _fix_malloc                           >>$here/$seq.full
        echo "*** end xfs_repair output"                        >>$here/$seq.full

        ok=0
    fi
    rm -f $tmp.fs_check $tmp.logprint $tmp.repair

    if [ $ok -eq 0 ]
    then
        echo "*** mount output ***"                             >>$here/$seq.full
        _mount                                                  >>$here/$seq.full
        echo "*** end mount output"                             >>$here/$seq.full
    elif [ "$type" = "xfs" ]
    then
        # mounted...
        if [ $USE_REMOUNT -eq 0 ]
        then
            if ! _mount -t xfs $extra_mount_options $device $mountpoint
            then
                echo "!!! failed to remount $device on $mountpoint"
                ok=0
            fi
        else
            _remount $device rw
        fi
    fi

    [ $ok -eq 0 ] && exit 1
    return 0
}

# Filter the knowen errors the UDF Verifier reports.
_udf_test_known_error_filter()
{
	egrep -v "PVD  60  Error: Interchange Level: 1, Maximum Interchange Level: 0|FSD  28  Error: Interchange Level: 1, Maximum Interchange Level: 1,|PVD  72  Warning: Volume Set Identifier: \"\*IRIX UDF\",|Warning: [0-9]+ unused blocks NOT marked as unallocated."

}

_check_udf_filesystem()
{
    [ "$DISABLE_UDF_TEST" == "1" ] && return

    if [ $# -ne 1 -a $# -ne 2 ]
    then
	echo "Usage: _check_udf_filesystem device [last_block]" 1>&2
	exit 1
    fi

    if [ ! -x $here/src/udf_test ]
    then
	echo "udf_test not installed, please download and build the Philips"
	echo "UDF Verification Software from http://www.extra.research.philips.com/udf/."
	echo "Then copy the udf_test binary to $here/src/."
	echo "If you do not wish to run udf_test then set environment variable DISABLE_UDF_TEST"
	echo "to 1."
	return
    fi

    device=$1
    if [ $# -eq 2 ];
    then
        LAST_BLOCK=`expr \( $2 - 1 \)`
        OPT_ARG="-lastvalidblock $LAST_BLOCK"
    fi

    rm -f $seq.checkfs
    sleep 1 # Due to a problem with time stamps in udf_test
    $here/src/udf_test $OPT_ARG $device | tee $here/$seq.checkfs | egrep "Error|Warning" | \
	_udf_test_known_error_filter | \
	egrep -iv "Error count:.*[0-9]+.*total occurrences:.*[0-9]+|Warning count:.*[0-9]+.*total occurrences:.*[0-9]+" | \
	sed "s/^.*$/Warning UDF Verifier reported errors see $seq.checkfs./g"

}

_check_test_fs()
{
    if [ "$FSTYP" != "xfs" ]; then
        return
    fi

    TEST_LOG="none"
    TEST_RT="none"
    [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_LOGDEV" ] && \
        TEST_LOG="$TEST_LOGDEV"

    [ "$USE_EXTERNAL" = yes -a ! -z "$TEST_RTDEV" ] && \
        TEST_RT="$TEST_RTDEV"

    _check_xfs_filesystem $TEST_DEV $TEST_LOG $TEST_RT

    # check for ipath consistency
    if $XFS_GROWFS_PROG -n $TEST_DIR | grep -q 'inode-paths=1'; then
	# errors go to stderr
	xfs_check_ipaths $TEST_DIR >/dev/null
	xfs_repair_ipaths -n $TEST_DIR >/dev/null
    fi
}

_check_scratch_fs()
{
    case $FSTYP in
    xfs)
	SCRATCH_LOG="none"
	SCRATCH_RT="none"
	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
	    SCRATCH_LOG="$SCRATCH_LOGDEV"

	[ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_RTDEV" ] && \
	    SCRATCH_RT="$SCRATCH_RTDEV"

	_check_xfs_filesystem $SCRATCH_DEV $SCRATCH_LOG $SCRATCH_RT
	;;
    udf)
	_check_udf_filesystem $SCRATCH_DEV $udf_fsize
	;;
    nfs*)
	# Don't know how to check an NFS filesystem, yet.
	;;
    *)
	;;
    esac
}

_full_fstyp_details()
{
     [ -z "$FSTYP" ] && FSTYP=xfs
     if [ $FSTYP = xfs ]; then
	if [ -d /proc/fs/xfs ]; then
	    if grep -q 'debug 0' /proc/fs/xfs/stat; then
		FSTYP="$FSTYP (non-debug)"
	    elif grep -q 'debug 1' /proc/fs/xfs/stat; then
		FSTYP="$FSTYP (debug)"
	    fi
	else
	    if uname -a | grep -qi 'debug'; then
		FSTYP="$FSTYP (debug)"
	    else
		FSTYP="$FSTYP (non-debug)"
	    fi
	fi
     fi
     echo $FSTYP
}

_full_platform_details()
{
     os=`uname -s`
     host=`hostname -s`
     kernel=`uname -r`
     platform=`uname -m`
     echo "$os/$platform $host $kernel"
}

_check_testdir()
{
    case $FSTYP in
    xfs)
	_check_test_fs
	;;
    udf)
	_cleanup_testdir
	_check_scratch_fs
	_scratch_mount
	;;
    nfs*)
	# Don't know how to check an NFS filesystem, yet.
	;;
    *)
	;;
    esac
}


_setup_xfs_testdir()
{
    [ "$FSTYP" != "xfs" ] \
	&& _fail "setup_xfs_testdir: \$FSTYP ($FSTYP) is not xfs"

    testdir=$TEST_DIR
}

_setup_udf_scratchdir()
{
    [ "$FSTYP" != "udf" ] \
	&& _fail "setup_udf_testdir: \$FSTYP is not udf"
    [ -z "$SCRATCH_DEV" -o ! -b "$SCRATCH_DEV" ] \
	&& _notrun "this test requires a valid \$SCRATCH_DEV"
    [ -z "$SCRATCH_MNT" ] \
	&& _notrun "this test requires a valid \$SCRATCH_MNT"

    # mounted?
    if _mount | grep -q $SCRATCH_DEV
    then
        # if it's mounted, make sure its on $TEST_RW_DIR
        if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
        then
            _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
        fi
    	$UMOUNT_PROG $SCRATCH_DEV
    fi

    _scratch_mkfs
    _scratch_mount

    testdir=$SCRATCH_MNT
}

_setup_nfs_scratchdir()
{
    [ "$FSTYP" != "nfs" ] \
	&& _fail "setup_nfs_testdir: \$FSTYP is not nfs"
    [ -z "$SCRATCH_DEV" ] \
	&& _notrun "this test requires a valid host fs for \$SCRATCH_DEV"
    [ -z "$SCRATCH_MNT" ] \
	&& _notrun "this test requires a valid \$SCRATCH_MNT"

    # mounted?
    if _mount | grep -q $SCRATCH_DEV
    then
        # if it's mounted, make sure its on $SCRATCH_MNT
        if ! _mount | grep $SCRATCH_DEV | grep -q $SCRATCH_MNT
        then
            _fail "\$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT - aborting"
        fi
    	$UMOUNT_PROG $SCRATCH_DEV
    fi

    _scratch_mkfs
    _scratch_mount

    testdir=$SCRATCH_MNT
}

#
# Warning for UDF and NFS this function calls _setup_udf_scratchdir and
# _setup_udf_scratchdir. This is done because testdir is a persistent
# XFS only partition.
#
_setup_testdir()
{
    case $FSTYP in
    xfs)
	_setup_xfs_testdir
	;;
    udf)
	_setup_udf_scratchdir
	;;
    nfs*)
	_setup_nfs_scratchdir
	;;
    *)
	_fail "\$FSTYP is not xfs, udf or nfs"
	;;
    esac
}

_cleanup_testdir()
{
    case $FSTYP in
    xfs)
	# do nothing, testdir is $TEST_DIR
	:
	;;
    udf)
	# umount testdir as it is $SCRATCH_MNT which could be used by xfs next
	[ -n "$testdir" ] && $UMOUNT_PROG $testdir
	;;
    nfs*)
	# umount testdir as it is $SCRATCH_MNT which could be used by xfs next
	[ -n "$testdir" ] && $UMOUNT_PROG $testdir
	;;
    *)
	_fail "\$FSTYP is not xfs, udf or nfs"
	;;
    esac
}

_link_out_file()
{
   if [ -z "$1" ]; then
      echo Error must pass \$seq.
      exit
   fi
   rm -f $1
   if [ "`uname`" == "IRIX64" ] || [ "`uname`" == "IRIX" ]; then
      ln -s $1.irix $1
   elif [ "`uname`" == "Linux" ]; then
      ln -s $1.linux $1
   else
      echo Error test $seq does not run on the operating system: `uname`
      exit
   fi
}

_die()
{
        echo $@
        exit 1
}

_nfiles()
{
        f=0
        while [ $f -lt $1 ]
        do
                file=f$f
                touch $file
                if [ $size -gt 0 ]; then
                    dd if=/dev/zero of=$file bs=1024 count=$size
                fi
                f=`expr $f + 1`
        done
}

# takes dirname, depth
_descend()
{
        dirname=$1; depth=$2
        mkdir $dirname  || die "mkdir $dirname failed"
        cd $dirname

        _nfiles $files           # files for this dir

        [ $depth -eq 0 ] && return
        deep=`expr $depth - 1`  # go 1 down

        [ $verbose = true ] && echo "descending, depth from leaves = $deep"

        d=0
        while [ $d -lt $dirs ]
        do
                _descend d$d $deep &
                d=`expr $d + 1`
                wait
        done
}

# Populate a filesystem with inodes for performance experiments
#
# usage: populate [-v] [-n ndirs] [-f nfiles] [-d depth] [-r root] [-s size]
#
_populate_fs()
{
    here=`pwd`
    dirs=5          # ndirs in each subdir till leaves
    size=0          # sizeof files in K
    files=100       # num files in _each_ subdir
    depth=2         # depth of tree from root to leaves
    verbose=false
    root=root       # path of initial root of directory tree

    while getopts "d:f:n:r:s:v" c
    do
        case $c in
        d)      depth=$OPTARG;;
        n)      dirs=$OPTARG;;
        f)      files=$OPTARG;;
        s)      size=$OPTARG;;
        v)      verbose=true;;
        r)      root=$OPTARG;;
        esac
    done

    _descend $root $depth
    wait

    cd $here

    [ $verbose = true ] && echo done
}

# query whether the given file has the given inode flag set
#
_test_inode_flag()
{
    flag=$1
    file=$2

    if which $XFS_IO_PROG >/dev/null; then
        if $XFS_IO_PROG -r -c 'lsattr -v' "$file" | grep -q "$flag" ; then
	    return 0
        fi
    fi
    return 1
}

# query the given files extsize allocator hint in bytes (if any)
#
_test_inode_extsz()
{
    file=$1
    blocks=""

    if which $XFS_IO_PROG >/dev/null; then
	blocks=`$XFS_IO_PROG -r -c 'stat' "$file" | \
		awk '/^xattr.extsize =/ { print $3 }'`
    fi
    [ -z "$blocks" ] && blocks="0"
    echo $blocks
}


################################################################################

if [ "$iam" != new -a "$iam" != bench ]
then
    # make some further configuration checks here

    if [ "$TEST_DEV" = ""  ]
    then
        echo "common.rc: Error: \$TEST_DEV is not set"
        exit 1
    fi

    # if $TEST_DEV is not mounted, mount it now as XFS
    if [ -z "`_fs_type $TEST_DEV`" ]
    then
        # $TEST_DEV is not mounted
        if ! _test_mount
        then
            echo "common.rc: retrying test device mount with external set"
            [ "$USE_EXTERNAL" != "yes" ] && export USE_EXTERNAL=yes
            if ! _test_mount
            then
                echo "common.rc: could not mount $TEST_DEV on $TEST_DIR"
                exit 1
            fi
        fi
    fi

    if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ]
    then
        echo "common.rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem"
        $DF_PROG $TEST_DEV
        exit 1
    fi
fi

# make sure this script returns success
/bin/true
