Whamcloud - gitweb
b=12649
[fs/lustre-release.git] / lustre / tests / test-framework.sh
index 557f2c2..188dd51 100644 (file)
@@ -1,15 +1,19 @@
 #!/bin/bash
 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
 
-trap 'echo "test-framework exiting on error"' ERR
+trap 'print_summary && echo "test-framework exiting on error"' ERR
 set -e
 #set -x
 
 
-export REFORMAT=""
+export REFORMAT=${REFORMAT:-""}
 export VERBOSE=false
 export GMNALNID=${GMNALNID:-/usr/sbin/gmlndnid}
 export CATASTROPHE=${CATASTROPHE:-/proc/sys/lnet/catastrophe}
+export GSS=false
+export GSS_KRB5=false
+export GSS_PIPEFS=false
+#export PDSH="pdsh -S -Rssh -w"
 
 # eg, assert_env LUSTRE MDSNODES OSTNODES CLIENTS
 assert_env() {
@@ -30,6 +34,37 @@ usage() {
     exit
 }
 
+print_summary () {
+    [ -n "$ONLY" ] && echo "WARNING: ONLY is set to ${ONLY}."
+    local form="%-13s %-17s %s\n"
+    printf "$form" "status" "script" "skipped tests E(xcluded) S(low)"
+    echo "------------------------------------------------------------------------------------"
+    for O in $TESTSUITE_LIST; do
+        local skipped=""
+        local slow=""
+        local o=$(echo $O | tr "[:upper:]" "[:lower:]")
+        o=${o//_/-}
+        o=${o//tyn/tyN}
+        local log=${TMP}/${o}.log 
+        [ -f $log ] && skipped=$(grep excluded $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
+        [ -f $log ] && slow=$(grep SLOW $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
+        [ "${!O}" = "done" ] && \
+            printf "$form" "Done" "$O" "E=$skipped" && \
+            [ -n "$slow" ] && printf "$form" "-" "-" "S=$slow"
+
+    done
+
+    for O in $TESTSUITE_LIST; do
+        [ "${!O}" = "no" ] && \
+            printf "$form" "Skipped" "$O" ""
+    done
+
+    for O in $TESTSUITE_LIST; do
+        [ "${!O}" = "done" -o "${!O}" = "no" ] || \
+            printf "$form" "UNFINISHED" "$O" ""
+    done
+}
+
 init_test_env() {
     export LUSTRE=`absolute_path $LUSTRE`
     export TESTSUITE=`basename $0 .sh`
@@ -37,24 +72,43 @@ init_test_env() {
 
     [ -d /r ] && export ROOT=${ROOT:-/r}
     export TMP=${TMP:-$ROOT/tmp}
+    export TESTSUITELOG=${TMP}/${TESTSUITE}.log
 
-    export PATH=:$PATH:$LUSTRE/utils:$LUSTRE/tests
+    export PATH=:$PATH:$LUSTRE/utils:$LUSTRE/utils/gss:$LUSTRE/tests
     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
+    export LFS=${LFS:-"$LUSTRE/utils/lfs"}
+    [ ! -f "$LCTL" ] && export LCTL=$(which lctl) 
+    export LFS=${LFS:-"$LUSTRE/utils/lfs"}
+    [ ! -f "$LFS" ] && export LFS=$(which lfs) 
     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
+    [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre) 
     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
-    [ ! -f "$TUNEFS" ] && export TUNEFS=$((which tunefs.lustre)
-    export CHECKSTAT="${CHECKSTAT:-checkstat} "
-    export FSYTPE=${FSTYPE:-"ext3"}
+    [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre
+    export CHECKSTAT="${CHECKSTAT:-"checkstat -v"} "
+    export FSYTPE=${FSTYPE:-"ldiskfs"}
     export NAME=${NAME:-local}
     export LPROC=/proc/fs/lustre
+    export LGSSD=${LGSSD:-"$LUSTRE/utils/gss/lgssd"}
+    export LSVCGSSD=${LSVCGSSD:-"$LUSTRE/utils/gss/lsvcgssd"}
+    export KRB5DIR=${KRB5DIR:-"/usr/kerberos"}
+    export DIR2
 
     if [ "$ACCEPTOR_PORT" ]; then
         export PORT_OPT="--port $ACCEPTOR_PORT"
     fi
 
+    case "x$SEC" in
+        xkrb5*)
+            echo "Using GSS/krb5 ptlrpc security flavor"
+            GSS=true
+            GSS_KRB5=true
+            ;;
+    esac
+
     # Paths on remote nodes, if different 
     export RLUSTRE=${RLUSTRE:-$LUSTRE}
     export RPWD=${RPWD:-$PWD}
+    export I_MOUNTED=${I_MOUNTED:-"no"}
 
     # command line
     
@@ -69,14 +123,13 @@ init_test_env() {
 
     shift $((OPTIND - 1))
     ONLY=${ONLY:-$*}
-}
 
-case `uname -r` in
-2.4.*) EXT=".o"; USE_QUOTA=no; FSTYPE=ext3 ;;
-    *) EXT=".ko"; USE_QUOTA=yes; FSTYPE=ldiskfs ;;
-esac
+    [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
+
+}
 
 load_module() {
+    EXT=".ko"
     module=$1
     shift
     BASE=`basename $module $EXT`
@@ -85,7 +138,12 @@ load_module() {
         insmod ${LUSTRE}/${module}${EXT} $@
     else
         # must be testing a "make install" or "rpm" installation
-        modprobe $BASE $@
+        # note failed to load ptlrpc_gss is considered not fatal
+        if [ "$BASE" == "ptlrpc_gss" ]; then
+            modprobe $BASE $@ 2>/dev/null || echo "gss/krb5 is not supported"
+        else
+            modprobe $BASE $@
+        fi
     fi
 }
 
@@ -102,6 +160,11 @@ load_modules() {
 
     echo Loading modules from $LUSTRE
     load_module ../lnet/libcfs/libcfs
+    [ -f /etc/modprobe.conf ] && MODPROBECONF=/etc/modprobe.conf
+    [ -f /etc/modprobe.d/Lustre ] && MODPROBECONF=/etc/modprobe.d/Lustre
+    [ -z "$LNETOPTS" -a -n "$MODPROBECONF" ] && \
+        LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
+    echo "lnet options: '$LNETOPTS'"
     # note that insmod will ignore anything in modprobe.conf
     load_module ../lnet/lnet/lnet $LNETOPTS
     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
@@ -109,18 +172,33 @@ load_modules() {
     load_module lvfs/lvfs
     load_module obdclass/obdclass
     load_module ptlrpc/ptlrpc
-    [ "$USE_QUOTA" = "yes" ] && load_module quota/lquota
+    load_module ptlrpc/gss/ptlrpc_gss
+    # Now, some modules depend on lquota without USE_QUOTA check,
+    # will fix later. Disable check "$USE_QUOTA" = "yes" temporary.
+    #[ "$USE_QUOTA" = "yes" ] && load_module quota/lquota
+    load_module quota/lquota
+    load_module fid/fid
+    load_module fld/fld
+    load_module lmv/lmv
     load_module mdc/mdc
     load_module osc/osc
     load_module lov/lov
-    load_module mds/mds
-    [ "$FSTYPE" = "ldiskfs" ] && load_module ldiskfs/ldiskfs
-    load_module lvfs/fsfilt_$FSTYPE
-    load_module ost/ost
-    load_module obdfilter/obdfilter
-    load_module llite/lustre
     load_module mgc/mgc
-    load_module mgs/mgs
+    if [ -z "$CLIENTONLY" ]; then
+        [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
+        load_module mgs/mgs
+        load_module mds/mds
+        load_module mdd/mdd
+        load_module mdt/mdt
+        load_module lvfs/fsfilt_$FSTYPE
+        load_module cmm/cmm
+        load_module osd/osd
+        load_module ost/ost
+        load_module obdfilter/obdfilter
+    fi
+
+    load_module llite/lustre
+    load_module llite/llite_lloop
     rm -f $TMP/ogdb-`hostname`
     $LCTL modules > $TMP/ogdb-`hostname`
     # 'mount' doesn't look in $PATH, just sbin
@@ -139,7 +217,7 @@ wait_for_lnet() {
     MODULES=$($LCTL modules | awk '{ print $2 }')
     while [ -n "$MODULES" ]; do
     sleep 5
-    $RMMOD $MODULES >/dev/null 2>&1 || true
+    $RMMOD $MODULES > /dev/null 2>&1 || true
     MODULES=$($LCTL modules | awk '{ print $2 }')
         if [ -z "$MODULES" ]; then
         return 0
@@ -156,12 +234,14 @@ wait_for_lnet() {
 }
 
 unload_modules() {
+    wait_exit_ST client # bug 12845
+
     lsmod | grep lnet > /dev/null && $LCTL dl && $LCTL dk $TMP/debug
     local MODULES=$($LCTL modules | awk '{ print $2 }')
-    $RMMOD $MODULES >/dev/null 2>&1 || true
+    $RMMOD $MODULES > /dev/null 2>&1 || true
      # do it again, in case we tried to unload ksocklnd too early
     MODULES=$($LCTL modules | awk '{ print $2 }')
-    [ -n "$MODULES" ] && $RMMOD $MODULES >/dev/null || true
+    [ -n "$MODULES" ] && $RMMOD $MODULES > /dev/null 2>&1 || true
     MODULES=$($LCTL modules | awk '{ print $2 }')
     if [ -n "$MODULES" ]; then
     echo "Modules still loaded: "
@@ -172,7 +252,7 @@ unload_modules() {
         lsmod
         return 2
     else
-        echo "Lustre stopped, but LNET is still loaded"
+        echo "Lustre stopped but LNET is still loaded, waiting..."
         wait_for_lnet || return 3
     fi
     fi
@@ -185,13 +265,111 @@ unload_modules() {
         echo "$LEAK_PORTALS" 1>&2
         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
         echo "Memory leaks detected"
-       [ "$LEAK_LUSTRE" -a $(echo $LEAK_LUSTRE | awk 'leaked=$8 {print leaked % 56}') == 0 ] && echo "ignoring known bug 10818" && return 0
+       [ -n "$IGNORE_LEAK" ] && echo "ignoring leaks" && return 0
         return 254
     fi
     echo "modules unloaded."
     return 0
 }
 
+check_gss_daemon_facet() {
+    facet=$1
+    dname=$2
+
+    num=`do_facet $facet ps -o cmd -C $dname | grep $dname | wc -l`
+    if [ $num -ne 1 ]; then
+        echo "$num instance of $dname on $facet"
+        return 1
+    fi
+    return 0
+}
+
+send_sigint() {
+    local facet=$1
+    shift
+    do_facet $facet "killall -2 $@ 2>/dev/null || true"
+}
+
+start_gss_daemons() {
+    # starting on MDT
+    for num in `seq $MDSCOUNT`; do
+        do_facet mds$num "$LSVCGSSD -v"
+        if $GSS_PIPEFS; then
+            do_facet mds$num "$LGSSD -v"
+        fi
+    done
+    # starting on OSTs
+    for num in `seq $OSTCOUNT`; do
+        do_facet ost$num "$LSVCGSSD -v"
+    done
+    # starting on client
+    # FIXME: is "client" the right facet name?
+    if $GSS_PIPEFS; then
+        do_facet client "$LGSSD -v"
+    fi
+
+    # wait daemons entering "stable" status
+    sleep 5
+
+    #
+    # check daemons are running
+    #
+    for num in `seq $MDSCOUNT`; do
+        check_gss_daemon_facet mds$num lsvcgssd
+        if $GSS_PIPEFS; then
+            check_gss_daemon_facet mds$num lgssd
+        fi
+    done
+    for num in `seq $OSTCOUNT`; do
+        check_gss_daemon_facet ost$num lsvcgssd
+    done
+    if $GSS_PIPEFS; then
+        check_gss_daemon_facet client lgssd
+    fi
+}
+
+stop_gss_daemons() {
+    for num in `seq $MDSCOUNT`; do
+        send_sigint mds$num lsvcgssd lgssd
+    done
+    for num in `seq $OSTCOUNT`; do
+        send_sigint ost$num lsvcgssd
+    done
+    send_sigint client lgssd
+}
+
+init_krb5_env() {
+    if [ ! -z $SEC ]; then
+        MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS,sec=$SEC
+        OST_MOUNT_OPTS=$OST_MOUNT_OPTS,sec=$SEC
+    fi
+
+    if $GSS; then
+        start_gss_daemons
+    fi
+}
+
+cleanup_krb5_env() {
+    if $GSS; then
+        stop_gss_daemons
+        # maybe cleanup credential cache?
+    fi
+}
+
+mdsdevlabel() {
+    local num=$1
+    local device=`mdsdevname $num`
+    local label=`do_facet mds$num "e2label ${device}" | grep -v "CMD: "`
+    echo -n $label
+}
+
+ostdevlabel() {
+    local num=$1
+    local device=`ostdevname $num`
+    local label=`do_facet ost$num "e2label ${device}" | grep -v "CMD: "`
+    echo -n $label
+}
+
 # Facet functions
 # start facet device options 
 start() {
@@ -207,8 +385,12 @@ start() {
         echo mount -t lustre $@ ${device} ${MOUNT%/*}/${facet} 
         echo Start of ${device} on ${facet} failed ${RC}
     else 
+        do_facet ${facet} "sysctl -w lnet.debug=$PTLDEBUG; \
+        sysctl -w lnet.subsystem_debug=${SUBSYSTEM# }; \
+        sysctl -w lnet.debug_mb=${DEBUG_SIZE}"
+
         do_facet ${facet} sync
-        label=`do_facet ${facet} "e2label ${device}" | grep -v "CMD: "`
+        label=$(do_facet ${facet} "e2label ${device}")
         [ -z "$label" ] && echo no label for ${device} && exit 1
         eval export ${facet}_svc=${label}
         eval export ${facet}_dev=${device}
@@ -225,7 +407,7 @@ stop() {
     HOST=`facet_active_host $facet`
     [ -z $HOST ] && echo stop: no host for $facet && return 0
 
-    running=`do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts" | grep -v "CMD: "`
+    running=$(do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts") || true
     if [ ${running} -ne 0 ]; then
         echo "Stopping ${MOUNT%/*}/${facet} (opts:$@)"
         do_facet ${facet} umount -d $@ ${MOUNT%/*}/${facet}
@@ -233,24 +415,7 @@ stop() {
 
     # umount should block, but we should wait for unrelated obd's
     # like the MGS or MGC to also stop.
-    local WAIT=0
-    local INTERVAL=1
-    # conf-sanity 31 takes a long time cleanup
-    while [ $WAIT -lt 300 ]; do
-       running=$(do_facet ${facet} "[ -e $LPROC ] && grep ST' ' $LPROC/devices" | grep -v "CMD: ") || true
-       if [ -z "${running}" ]; then
-           return 0
-       fi
-       echo "waited $WAIT for${running}"
-       if [ $INTERVAL -lt 64 ]; then 
-           INTERVAL=$((INTERVAL + INTERVAL))
-       fi
-       sleep $INTERVAL
-       WAIT=$((WAIT + INTERVAL))
-    done
-    echo "service didn't stop after $WAIT seconds.  Still running:"
-    echo ${running}
-    exit 1
+    wait_exit_ST ${facet}
 }
 
 zconf_mount() {
@@ -271,7 +436,9 @@ zconf_mount() {
     do_node $client mkdir -p $mnt
     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
 
-    do_node $client "sysctl -w lnet.debug=$PTLDEBUG; sysctl -w lnet.subsystem_debug=${SUBSYSTEM# }"
+    do_node $client "sysctl -w lnet.debug=$PTLDEBUG;
+        sysctl -w lnet.subsystem_debug=${SUBSYSTEM# };
+        sysctl -w lnet.debug_mb=${DEBUG_SIZE}"
     [ -d /r ] && $LCTL modules > /r/tmp/ogdb-`hostname`
     return 0
 }
@@ -280,7 +447,7 @@ zconf_umount() {
     client=$1
     mnt=$2
     [ "$3" ] && force=-f
-    local running=`do_node $client "grep -c $mnt' ' /proc/mounts" | grep -v "CMD: "`
+    local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
     if [ $running -ne 0 ]; then
         echo "Stopping client $mnt (opts:$force)"
         do_node $client umount $force $mnt
@@ -337,6 +504,24 @@ cleanup_check() {
     return 0
 }
 
+wait_delete_completed () {
+    local TOTALPREV=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
+            $LPROC/osc/*/kbytesavail`
+
+    local WAIT=0
+    local MAX_WAIT=20
+    while [ "$WAIT" -ne "$MAX_WAIT" ]; do
+        sleep 1
+        TOTAL=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
+            $LPROC/osc/*/kbytesavail`
+        [ "$TOTAL" -eq "$TOTALPREV" ] && break
+        echo "Waiting delete completed ... prev: $TOTALPREV current: $TOTAL "
+        TOTALPREV=$TOTAL
+        WAIT=$(( WAIT + 1))
+    done
+    echo "Delete completed."
+}
+
 wait_for_host() {
     HOST=$1
     check_network "$HOST" 900
@@ -349,6 +534,43 @@ wait_for() {
     wait_for_host $HOST
 }
 
+wait_mds_recovery_done () {
+    local timeout=`do_facet mds cat /proc/sys/lustre/timeout`
+#define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2)
+# as we are in process of changing obd_timeout in different ways
+# let's set MAX longer than that
+    MAX=$(( timeout * 4 ))
+    WAIT=0
+    while [ $WAIT -lt $MAX ]; do
+        STATUS=`do_facet mds grep status /proc/fs/lustre/mdt/*-MDT*/recovery_status`
+        echo $STATUS | grep COMPLETE && return 0
+        sleep 5
+        WAIT=$((WAIT + 5))
+        echo "Waiting $(($MAX - $WAIT)) secs for MDS recovery done"
+    done
+    echo "MDS recovery not done in $MAX sec"
+    return 1            
+}
+
+wait_exit_ST () {
+    local facet=$1
+
+    local WAIT=0
+    local INTERVAL=1
+    # conf-sanity 31 takes a long time cleanup
+    while [ $WAIT -lt 300 ]; do
+        running=$(do_facet ${facet} "[ -e $LPROC ] && grep ST' ' $LPROC/devices") || true
+        [ -z "${running}" ] && return 0
+        echo "waited $WAIT for${running}"
+        [ $INTERVAL -lt 64 ] && INTERVAL=$((INTERVAL + INTERVAL))
+        sleep $INTERVAL
+        WAIT=$((WAIT + INTERVAL))
+    done
+    echo "service didn't stop after $WAIT seconds.  Still running:"
+    echo ${running}
+    return 1
+}
+
 client_df() {
     # not every config has many clients
     if [ ! -z "$CLIENTS" ]; then
@@ -381,7 +603,7 @@ facet_failover() {
     wait_for $facet
     local dev=${facet}_dev
     local opt=${facet}_opt
-    start $facet ${!dev} ${!opt}
+    start $facet ${!dev} ${!opt} || error "Restart of $facet failed"
 }
 
 obd_name() {
@@ -411,12 +633,12 @@ replay_barrier_nodf() {
 }
 
 mds_evict_client() {
-    UUID=`cat /proc/fs/lustre/mdc/${mds_svc}-mdc-*/uuid`
-    do_facet mds "echo $UUID > /proc/fs/lustre/mds/${mds_svc}/evict_client"
+    UUID=`cat /proc/fs/lustre/mdc/${mds1_svc}-mdc-*/uuid`
+    do_facet mds1 "echo $UUID > /proc/fs/lustre/mdt/${mds1_svc}/evict_client"
 }
 
 ost_evict_client() {
-    UUID=`cat /proc/fs/lustre/osc/${ost1_svc}-osc-*/uuid`
+    UUID=`grep ${ost1_svc}-osc- $LPROC/devices | egrep -v 'MDT' | awk '{print $5}'`
     do_facet ost1 "echo $UUID > /proc/fs/lustre/obdfilter/${ost1_svc}/evict_client"
 }
 
@@ -451,11 +673,27 @@ h2gm () {
     fi
 }
 
-h2tcp() {
+h2name_or_ip() {
     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
-        echo $1"@tcp
+        echo $1"@$2
     fi
 }
+
+h2ptl() {
+   if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
+       ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
+       if [ -z "$ID" ]; then
+           echo "Could not get a ptl id for $1..."
+           exit 1
+       fi
+       echo $ID"@ptl"
+   fi
+}
+declare -fx h2ptl
+
+h2tcp() {
+    h2name_or_ip "$1" "tcp"
+}
 declare -fx h2tcp
 
 h2elan() {
@@ -471,13 +709,15 @@ h2elan() {
 declare -fx h2elan
 
 h2openib() {
-    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
-        ID=`echo $1 | sed 's/[^0-9]*//g'`
-        echo $ID"@openib"
-    fi
+    h2name_or_ip "$1" "openib"
 }
 declare -fx h2openib
 
+h2o2ib() {
+    h2name_or_ip "$1" "o2ib"
+}
+declare -fx h2o2ib
+
 facet_host() {
     local facet=$1
     varname=${facet}_HOST
@@ -539,10 +779,11 @@ do_node() {
         myPDSH="no_dsh"
     fi
     if $VERBOSE; then
-        echo "CMD: $HOST $@"
+        echo "CMD: $HOST $@" >&2
         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
     fi
-    $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")"
+    $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
+    return ${PIPESTATUS[0]}
 }
 
 do_facet() {
@@ -570,21 +811,32 @@ ostdevname() {
     echo -n $DEVPTR
 }
 
+mdsdevname() {
+    num=$1
+    DEVNAME=MDSDEV$num
+    #if $MDSDEVn isn't defined, default is $MDSDEVBASE + num
+    eval DEVPTR=${!DEVNAME:=${MDSDEVBASE}${num}}
+    echo -n $DEVPTR
+}
+
 ########
 ## MountConf setup
 
 stopall() {
     # make sure we are using the primary server, so test-framework will
     # be able to clean up properly.
-    activemds=`facet_active mds`
-    if [ $activemds != "mds" ]; then
-        fail mds
+    activemds=`facet_active mds1`
+    if [ $activemds != "mds1" ]; then
+        fail mds1
     fi
     
     # assume client mount is local 
     grep " $MOUNT " /proc/mounts && zconf_umount `hostname` $MOUNT $*
     grep " $MOUNT2 " /proc/mounts && zconf_umount `hostname` $MOUNT2 $*
-    stop mds -f
+    [ "$CLIENTONLY" ] && return
+    for num in `seq $MDSCOUNT`; do
+        stop mds$num -f
+    done
     for num in `seq $OSTCOUNT`; do
         stop ost$num -f
     done
@@ -594,20 +846,34 @@ stopall() {
 cleanupall() {
     stopall $*
     unload_modules
+    cleanup_krb5_env
+}
+
+mdsmkfsopts()
+{
+    local nr=$1
+    test $nr = 1 && echo -n $MDS_MKFS_OPTS || echo -n $MDSn_MKFS_OPTS
 }
 
 formatall() {
+    [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
+
     stopall
     # We need ldiskfs here, may as well load them all
     load_modules
-    echo Formatting mds, osts
-    if $VERBOSE; then
-        add mds $MDS_MKFS_OPTS --reformat $MDSDEV || exit 10
-    else
-        add mds $MDS_MKFS_OPTS --reformat $MDSDEV > /dev/null || exit 10
-    fi
+    [ "$CLIENTONLY" ] && return
+    echo "Formatting mdts, osts"
+    for num in `seq $MDSCOUNT`; do
+        echo "Format mds$num: $(mdsdevname $num)"
+        if $VERBOSE; then
+            add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` || exit 9
+        else
+            add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` > /dev/null || exit 9
+        fi
+    done
 
     for num in `seq $OSTCOUNT`; do
+        echo "Format ost$num: $(ostdevname $num)"
         if $VERBOSE; then
             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` || exit 10
         else
@@ -622,17 +888,67 @@ mount_client() {
 
 setupall() {
     load_modules
-    echo Setup mdt, osts
-    start mds $MDSDEV $MDS_MOUNT_OPTS
-    for num in `seq $OSTCOUNT`; do
-        DEVNAME=`ostdevname $num`
-        start ost$num $DEVNAME $OST_MOUNT_OPTS
-    done
+    init_krb5_env
+    if [ -z "$CLIENTONLY" ]; then
+        echo "Setup mdts, osts"
+        for num in `seq $MDSCOUNT`; do
+            DEVNAME=$(mdsdevname $num)
+            echo $REFORMAT | grep -q "reformat" \
+            || do_facet mds$num "$TUNEFS --writeconf $DEVNAME"
+            start mds$num $DEVNAME $MDS_MOUNT_OPTS
+        done
+        for num in `seq $OSTCOUNT`; do
+            DEVNAME=$(ostdevname $num)
+            start ost$num $DEVNAME $OST_MOUNT_OPTS
+        done
+    fi
     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
     mount_client $MOUNT
+    if [ "$MOUNT_2" ]; then
+       mount_client $MOUNT2
+    fi
     sleep 5
 }
 
+mounted_lustre_filesystems() {
+       awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
+}
+
+check_and_setup_lustre() {
+    MOUNTED="`mounted_lustre_filesystems`"
+    if [ -z "$MOUNTED" ]; then
+        [ "$REFORMAT" ] && formatall
+        setupall
+        MOUNTED="`mounted_lustre_filesystems`"
+        [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
+        export I_MOUNTED=yes
+    fi
+    if [ "$ONLY" == "setup" ]; then
+        exit 0
+    fi
+}
+
+cleanup_and_setup_lustre() {
+    if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
+        sysctl -w lnet.debug=0 || true
+        cleanupall
+        if [ "$ONLY" == "cleanup" ]; then 
+           exit 0
+        fi
+    fi
+    check_and_setup_lustre
+}
+
+check_and_cleanup_lustre() {
+    if [ "`mount | grep $MOUNT`" ]; then
+        rm -rf $DIR/[Rdfs][0-9]*
+        rm -f $DIR/${TESTSUITE}/[Rdfs][1-9]*
+    fi
+    if [ "$I_MOUNTED" = "yes" ]; then
+        cleanupall -f || error "cleanup failed"
+    fi
+    unset I_MOUNTED
+}
 
 ####### 
 # General functions
@@ -773,13 +1089,49 @@ pgcache_empty() {
     return 0
 }
 
+debugsave() {
+    DEBUGSAVE="$(sysctl -n lnet.debug)"
+}
+
+debugrestore() {
+    [ -n "$DEBUGSAVE" ] && sysctl -w lnet.debug="${DEBUGSAVE}"
+    DEBUGSAVE=""
+}
+
 ##################################
 # Test interface 
+##################################
+
 error() {
+    local FAIL_ON_ERROR=${FAIL_ON_ERROR:-true}
+    local TYPE=${TYPE:-"FAIL"}
+    local ERRLOG
     sysctl -w lustre.fail_loc=0 2> /dev/null || true
-    echo "${TESTSUITE}: **** FAIL:" $@
-    log "FAIL: $TESTNAME $@"
-    exit 1
+    log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
+    ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
+    echo "Dumping lctl log to $ERRLOG"
+    # We need to dump the logs on all nodes
+    local NODES=$(nodes_list)
+    for NODE in $NODES; do
+        do_node $NODE $LCTL dk $ERRLOG
+    done
+    debugrestore
+    [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
+    if $FAIL_ON_ERROR; then
+       exit 1
+    fi
+}
+
+# use only if we are ignoring failures for this test, bugno required.
+# (like ALWAYS_EXCEPT, but run the test and ignore the results.)
+# e.g. error_ignore 5494 "your message"
+error_ignore() {
+    FAIL_ON_ERROR=false TYPE="IGNORE (bz$1)" error $2
+}
+
+skip () {
+       log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
+       [ "$TESTSUITELOG" ] && echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG
 }
 
 build_test_filter() {
@@ -789,9 +1141,17 @@ build_test_filter() {
     done
     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
         log "skipping tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
+    [ "$EXCEPT_SLOW" ] && \
+        log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
     for E in $EXCEPT $ALWAYS_EXCEPT; do
         eval EXCEPT_${E}=true
     done
+    for E in $EXCEPT_SLOW; do
+        eval EXCEPT_SLOW_${E}=true
+    done
+    for G in $GRANT_CHECK_LIST; do
+        eval GCHECK_ONLY_${G}=true
+       done
 }
 
 _basetest() {
@@ -820,14 +1180,25 @@ run_test() {
     fi
     testname=EXCEPT_$1
     if [ ${!testname}x != x ]; then
-        log "skipping excluded test $1"
+        TESTNAME=test_$1 skip "skipping excluded test $1"
         return 0
     fi
     testname=EXCEPT_$base
     if [ ${!testname}x != x ]; then
-        log "skipping excluded test $1 (base $base)"
+        TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
+        return 0
+    fi
+    testname=EXCEPT_SLOW_$1
+    if [ ${!testname}x != x ]; then
+        TESTNAME=test_$1 skip "skipping SLOW test $1"
+        return 0
+    fi
+    testname=EXCEPT_SLOW_$base
+    if [ ${!testname}x != x ]; then
+        TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
         return 0
     fi
+
     run_one $1 "$2"
     
     return $?
@@ -839,13 +1210,32 @@ equals_msg() {
 
     local suffixlen=$((${#EQUALS} - ${#msg}))
     [ $suffixlen -lt 5 ] && suffixlen=5
-    printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS
+    log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
 }
 
 log() {
     echo "$*"
     lsmod | grep lnet > /dev/null || load_modules
-    $LCTL mark "$*" 2> /dev/null || true
+
+    local MSG="$*"
+    # Get rif of '
+    MSG=${MSG//\'/\\\'}
+    MSG=${MSG//\(/\\\(}
+    MSG=${MSG//\)/\\\)}
+    MSG=${MSG//\;/\\\;}
+    MSG=${MSG//\|/\\\|}
+    local NODES=$(nodes_list)
+    for NODE in $NODES; do
+        do_node $NODE $LCTL mark "$MSG" 2> /dev/null || true
+    done
+}
+
+trace() {
+       log "STARTING: $*"
+       strace -o $TMP/$1.strace -ttt $*
+       RC=$?
+       log "FINISHED: $*: rc $RC"
+       return 1
 }
 
 pass() {
@@ -862,10 +1252,10 @@ run_one() {
     testnum=$1
     message=$2
     tfile=f${testnum}
-    tdir=d${base}
-
-    # Pretty tests run faster.
-    equals_msg $testnum: $message
+    export tdir=d${TESTSUITE}/d${base}
+    local SAVE_UMASK=`umask`
+    umask 0022
+    mkdir -p $DIR/$tdir
 
     BEFORE=`date +%s`
     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
@@ -873,13 +1263,154 @@ run_one() {
     export TESTNAME=test_$testnum
     test_${testnum} || error "test_$testnum failed with $?"
     #check_mds
+    check_grant ${testnum} || error "check_grant $testnum failed with $?"
     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
         error "LBUG/LASSERT detected"
     pass "($((`date +%s` - $BEFORE))s)"
+    rmdir ${DIR}/$tdir >/dev/null 2>&1 || true
     unset TESTNAME
+    unset tdir
+    umask $SAVE_UMASK
+    cd $SAVE_PWD
+    $CLEANUP
 }
 
 canonical_path() {
     (cd `dirname $1`; echo $PWD/`basename $1`)
 }
 
+sync_clients() {
+    [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync 
+    [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync 
+       cd $SAVE_PWD
+}
+
+check_grant() {
+    export base=`basetest $1`
+    [ "$CHECK_GRANT" == "no" ] && return 0
+
+       testname=GCHECK_ONLY_${base}
+        [ ${!testname}x == x ] && return 0
+
+       echo -n "checking grant......"
+       cd $SAVE_PWD
+       # write some data to sync client lost_grant
+       rm -f $DIR1/${tfile}_check_grant_* 2>&1
+       for i in `seq $OSTCOUNT`; do
+               $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
+               dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
+                                             count=1 > /dev/null 2>&1 
+       done
+       # sync all the data and make sure no pending data on server
+       sync_clients
+       
+       #get client grant and server grant 
+       client_grant=0
+    for d in ${LPROC}/osc/*/cur_grant_bytes; do 
+               client_grant=$((client_grant + `cat $d`))
+       done
+       server_grant=0
+       for d in ${LPROC}/obdfilter/*/tot_granted; do
+               server_grant=$((server_grant + `cat $d`))
+       done
+
+       # cleanup the check_grant file
+       for i in `seq $OSTCOUNT`; do
+               rm $DIR1/${tfile}_check_grant_$i
+       done
+
+       #check whether client grant == server grant 
+       if [ $client_grant != $server_grant ]; then
+               echo "failed: client:${client_grant} server: ${server_grant}"
+               return 1
+       else
+               echo "pass"
+       fi
+}
+
+########################
+# helper functions
+
+osc_to_ost()
+{
+    osc=$1
+    ost=`echo $1 | awk -F_ '{print $3}'`
+    if [ -z $ost ]; then
+        ost=`echo $1 | sed 's/-osc.*//'`
+    fi
+    echo $ost
+}
+
+remote_mds ()
+{
+    [ ! -e /proc/fs/lustre/mdt/*MDT* ]
+}
+
+remote_ost ()
+{
+    [ $(grep -c obdfilter $LPROC/devices) -eq 0 ]
+}
+
+mdts_nodes () {
+    local MDSNODES=$(facet_host $SINGLEMDS)
+
+    # FIXME: Currenly we use only $SINGLEMDS,
+    # should be fixed when we will start to test cmd.
+    echo $MDSNODES
+    return
+
+    for num in `seq $MDSCOUNT`; do
+        local myMDS=$(facet_host mds$num)
+        [[ ! '\ '"$MDSNODES"'\ ' = *'\ '"$myMDS"'\ '* ]] && MDSNODES="$MDSNODES $myMDS"
+    done
+
+    echo $MDSNODES
+}
+
+osts_nodes () {
+    local OSTNODES=$(facet_host ost1)
+
+    for num in `seq $OSTCOUNT`; do
+        local myOST=$(facet_host ost$num)
+        [[ ! '\ '"$OSTNODES"'\ ' = *'\ '"$myOST"'\ '* ]] && OSTNODES="$OSTNODES $myOST"
+    done
+
+    echo $OSTNODES
+}
+
+nodes_list () {
+    # FIXME. We need a list of clients
+    local myNODES=`hostname`
+
+    local OSTNODES=$(osts_nodes)
+    local myOSTNODES=`echo ' '"$OSTNODES"' ' | sed -e s/[\ ]$(hostname)[\ ]/\ /`
+    [ -n "$myOSTNODES" ] && myNODES="$myNODES $myOSTNODES"
+
+    local myNODES=${myNODES% } 
+    # Add to list only not listed mds nodes
+    local MDSNODES=$(mdts_nodes)
+    for myMDS in $MDSNODES; do
+        [[ ! "'\ '$myNODES'\ '" = *'\ '"$myMDS"'\ '* ]] && myNODES="$myNODES $myMDS"
+    done
+
+    echo $myNODES
+}
+
+is_patchless ()
+{
+    grep -q patchless $LPROC/version
+}
+
+check_runas_id() {
+    local myRUNAS_ID=$1
+    shift
+    local myRUNAS=$@
+    mkdir $DIR/d0_runas_test
+    chmod 0755 $DIR
+    chown $myRUNAS_ID:$myRUNAS_ID $DIR/d0_runas_test
+    $myRUNAS touch $DIR/d0_runas_test/f$$ || \
+        error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_ID. 
+        Please set RUNAS_ID to some UID which exists on MDS and client or 
+        add user $myRUNAS_ID:$myRUNAS_ID on these nodes."
+    rm -rf $DIR/d0_runas_test
+}