#! /bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2001
#
#   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 implie; 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
#
#
#
#  FILE   : nfsstress
#
#  DESCRIPTION: This script sets up the NFS directories in the remote machine 
#               and invokes the program make_tree with parameters.
#  
#  SETUP: The home directory of root on the machine exported as "RHOST"
#         MUST have a ".rhosts" file with the hostname of the machine
#         where the test is executed.
#
#
#  HISTORY:
#    11/1/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Created
#
#***********************************************************************

#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}

$trace_logic

#-----------------------------------------------------------------------
# Initialize local variables
#-----------------------------------------------------------------------
TC=${TC:=nfsstress}
TCbin=${TCbin:=`pwd`}
TCdat=${TCdat:=$TCbin}
TCsrc=${TCsrc:=$TCbin}
TCtmp=${TCtmp:=$TCbin/$TC$$}
TCdump=${TCdump:=$TCbin}
RHOST=${RHOST:=`hostname|awk {'print $1'}`}

# Setting the NFS to version 2 with UDP by default
PID=$$
VERSION=${VERSION:=2}
SOCKET_TYPE=${SOCKET_TYPE:=udp}
TESTDIR=${TESTDIR:=/tmp/$TC$PID.testdir}
NFS_TYPE=${NFS_TYPE:=nfs}

# If CLEANUP is not set; set it to "ON"
CLEANUP=${CLEANUP:="ON"}

# If EXECUTABLES is not set; set it to default executables
EXECUTABLES=${EXECUTABLES:="make_tree"}

DIR_NUM=$1
FILE_NUM=$2
THREAD_NUM=$3

DIR_NUM=${DIR_NUM:=100}
FILE_NUM=${FILE_NUM:=100}
THREAD_NUM=${THREAD_NUM:=8}

#=============================================================================
# FUNCTION NAME:        setup_testcase
#
# FUNCTION DESCRIPTION: Perform the setup function for the testcase.
#
# PARAMETERS:   	None.
#
# RETURNS:      	None.
#=============================================================================

setup_testcase()
{
$trace_logic
   

    echo ""
    echo "Test Options:"
    echo " VERSION: $VERSION"
    echo " RHOST: $RHOST"
    echo " SOCKET_TYPE: $SOCKET_TYPE"
    echo " NFS_TYPE: $NFS_TYPE"
    echo ""
    echo "Test Parameters:"
    echo " Number of Directories: $DIR_NUM"
    echo " Number of Files per Directory: $FILE_NUM"
    echo " Number of Threads: $THREAD_NUM"
    echo ""
    
    if [ "x$NFS_TYPE" != "xnfs4" ]; then
        OPTS=${OPTS:="-o vers=$VERSION,proto=$SOCKET_TYPE "}
    fi
    REMOTE_DIR=${RHOST}:$TESTDIR
    LOCAL_DIR=$TCtmp/${RHOST}
    mkdir -p $TCtmp || end_testcase "Could not create $TCtmp"
    chmod 777 $TCtmp

    echo "Setting up remote machine: $RHOST"
    rsh -n $RHOST "mkdir $TESTDIR"
    [ $? = 0 ] || end_testcase "Could not create remote directory"

   if [ "x$NFS_TYPE" = "xnfs4" ]; then
        rsh -n $RHOST "mkdir -p /export$TESTDIR"
        [ $? = 0 ] || end_testcase "Could not create /export$TESTDIR on server"
        rsh -n $RHOST "mount --bind $TESTDIR /export$TESTDIR"
        [ $? = 0 ] || end_testcase "Could not bind $TESTDIR to export on server"
        rsh -n $RHOST "/usr/sbin/exportfs -o no_root_squash,rw,nohide,insecure,no_subtree_check *:$TESTDIR"
        [ $? = 0 ] || end_testcase "Could not export remote directory"
    else
        rsh -n $RHOST "/usr/sbin/exportfs -i -o no_root_squash,rw *:$TESTDIR"
        [ $? = 0 ] || end_testcase "Could not export remote directory"
    fi

    echo "Mounting NFS filesystem $REMOTE_DIR on $TCtmp with options '$OPTS'"
    mount -t $NFS_TYPE $OPTS $REMOTE_DIR $TCtmp || end_testcase "Cannot mount $TCtmp"
    [ $? = 0 ] || end_testcase "Could not mount $REMOTE_DIR"

    cd $TCtmp 
    echo "Removing old and create new directories"
    [ -d $LOCAL_DIR ] && rm -rf $LOCAL_DIR
    [ -d $LOCAL_DIR ] || { \
	mkdir -p $LOCAL_DIR || end_testcase "Could not create $LOCAL_DIR"
	chmod 777 $LOCAL_DIR
        cp $TCsrc/$EXECUTABLES $LOCAL_DIR || end_testcase "Could not write to mount point" 
    }

}


#=============================================================================
# FUNCTION NAME:        do_test
#
# FUNCTION DESCRIPTION: Perform the test
#
# PARAMETERS:   	None.
#
# RETURNS:      	None.
#=============================================================================
do_test()
{
$trace_logic
    EXEC=${EXEC:=$EXECUTABLES -d $DIR_NUM -f $FILE_NUM -t $THREAD_NUM} 
    cd $LOCAL_DIR
    echo "${LOCAL_DIR}/${EXEC} Starting"
    ./$EXEC &
    wait $!
    retval=$?
    echo "${LOCAL_DIR}/${EXEC} Finished"
    
    if [ "$retval" != 0 ]; then
      end_testcase "Errors have resulted from this test: $EXECUTABLES returned $retval."
    fi

    cd /
}


#=============================================================================
# FUNCTION NAME:        end_testcase
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:   	None.
#
# RETURNS:      	None.
#=============================================================================
end_testcase()
{
$trace_logic
    if [ "$CLEANUP" = "ON" ]; then
	cd \
	
	echo "Cleaning up testcase"
	rm -rf $LOCAL_DIR || echo "Cannot remove $LOCAL_DIR"
	sleep 2
        /bin/umount $TCtmp || echo "Cannot umount $TCtmp"
	sleep 2
	rm -rf $TCtmp || echo "Cannot remove $TCtmp"
        rsh -n $RHOST "/usr/sbin/exportfs -u *:$TESTDIR"
		 rsh -n $RHOST "rm -rf $TESTDIR"
    fi

    [ $# = 0 ] && { echo "Test Successful"; exit 0; }
    echo "Test Failed: $@"
    exit 1
}

#=============================================================================
# MAIN PROCEDURE
#=============================================================================

setup_testcase
do_test
end_testcase
