Whamcloud - gitweb
b=17853
[fs/lustre-release.git] / lustre / tests / test-framework.sh
1 #!/bin/bash
2 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
3
4 trap 'print_summary && echo "test-framework exiting on error"' ERR
5 set -e
6 #set -x
7
8
9 export REFORMAT=${REFORMAT:-""}
10 export WRITECONF=${WRITECONF:-""}
11 export VERBOSE=false
12 export GMNALNID=${GMNALNID:-/usr/sbin/gmlndnid}
13 export CATASTROPHE=${CATASTROPHE:-/proc/sys/lnet/catastrophe}
14 export GSS=false
15 export GSS_KRB5=false
16 export GSS_PIPEFS=false
17 export IDENTITY_UPCALL=default
18
19 #export PDSH="pdsh -S -Rssh -w"
20
21 # eg, assert_env LUSTRE MDSNODES OSTNODES CLIENTS
22 assert_env() {
23     local failed=""
24     for name in $@; do
25         if [ -z "${!name}" ]; then
26             echo "$0: $name must be set"
27             failed=1
28         fi
29     done
30     [ $failed ] && exit 1 || true
31 }
32
33 assert_DIR () {
34     local failed=""
35     [[ $DIR/ = $MOUNT/* ]] || \
36         { failed=1 && echo "DIR=$DIR not in $MOUNT. Aborting."; }
37     [[ $DIR1/ = $MOUNT1/* ]] || \
38         { failed=1 && echo "DIR1=$DIR1 not in $MOUNT1. Aborting."; }
39     [[ $DIR2/ = $MOUNT2/* ]] || \
40         { failed=1 && echo "DIR2=$DIR2 not in $MOUNT2. Aborting"; }
41
42     [ -n "$failed" ] && exit 99 || true
43 }
44
45 usage() {
46     echo "usage: $0 [-r] [-f cfgfile]"
47     echo "       -r: reformat"
48
49     exit
50 }
51
52 print_summary () {
53     [ "$TESTSUITE" == "lfscktest" ] && return 0
54     [ -n "$ONLY" ] && echo "WARNING: ONLY is set to ${ONLY}."
55     local form="%-13s %-17s %s\n"
56     printf "$form" "status" "script" "skipped tests E(xcluded) S(low)"
57     echo "------------------------------------------------------------------------------------"
58     for O in $TESTSUITE_LIST; do
59         local skipped=""
60         local slow=""
61         local o=$(echo $O | tr "[:upper:]" "[:lower:]")
62         o=${o//_/-}
63         o=${o//tyn/tyN}
64         local log=${TMP}/${o}.log
65         [ -f $log ] && skipped=$(grep excluded $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
66         [ -f $log ] && slow=$(grep SLOW $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
67         [ "${!O}" = "done" ] && \
68             printf "$form" "Done" "$O" "E=$skipped" && \
69             [ -n "$slow" ] && printf "$form" "-" "-" "S=$slow"
70
71     done
72
73     for O in $TESTSUITE_LIST; do
74         [ "${!O}" = "no" ] && \
75             printf "$form" "Skipped" "$O" ""
76     done
77
78     for O in $TESTSUITE_LIST; do
79         [ "${!O}" = "done" -o "${!O}" = "no" ] || \
80             printf "$form" "UNFINISHED" "$O" ""
81     done
82 }
83
84 init_test_env() {
85     export LUSTRE=`absolute_path $LUSTRE`
86     export TESTSUITE=`basename $0 .sh`
87
88     #[ -d /r ] && export ROOT=${ROOT:-/r}
89     export TMP=${TMP:-$ROOT/tmp}
90     export TESTSUITELOG=${TMP}/${TESTSUITE}.log
91     export HOSTNAME=${HOSTNAME:-`hostname`}
92     if ! echo $PATH | grep -q $LUSTRE/utils; then
93         export PATH=$PATH:$LUSTRE/utils
94     fi
95     if ! echo $PATH | grep -q $LUSTRE/utils/gss; then
96         export PATH=$PATH:$LUSTRE/utils/gss
97     fi
98     if ! echo $PATH | grep -q $LUSTRE/tests; then
99         export PATH=$PATH:$LUSTRE/tests
100     fi
101     export MDSRATE=${MDSRATE:-"$LUSTRE/tests/mdsrate"}
102     [ ! -f "$MDSRATE" ] && export MDSRATE=$(which mdsrate 2> /dev/null)
103     if ! echo $PATH | grep -q $LUSTRE/test/racer; then
104         export PATH=$PATH:$LUSTRE/tests/racer
105     fi
106     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
107     [ ! -f "$LCTL" ] && export LCTL=$(which lctl)
108     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
109     [ ! -f "$LFS" ] && export LFS=$(which lfs)
110     export L_GETIDENTITY=${L_GETIDENTITY:-"$LUSTRE/utils/l_getidentity"}
111     if [ ! -f "$L_GETIDENTITY" ]; then
112         if `which l_getidentity > /dev/null 2>&1`; then
113             export L_GETIDENTITY=$(which l_getidentity)
114         else
115             export L_GETIDENTITY=NONE
116         fi
117     fi
118     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
119     [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre)
120     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
121     [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre)
122     export CHECKSTAT="${CHECKSTAT:-"checkstat -v"} "
123     export FSYTPE=${FSTYPE:-"ldiskfs"}
124     export NAME=${NAME:-local}
125     export LGSSD=${LGSSD:-"$LUSTRE/utils/gss/lgssd"}
126     [ "$GSS_PIPEFS" = "true" ] && [ ! -f "$LGSSD" ] && \
127         export LGSSD=$(which lgssd)
128     export LSVCGSSD=${LSVCGSSD:-"$LUSTRE/utils/gss/lsvcgssd"}
129     [ ! -f "$LSVCGSSD" ] && export LSVCGSSD=$(which lsvcgssd)
130     export KRB5DIR=${KRB5DIR:-"/usr/kerberos"}
131     export DIR2
132     export SAVE_PWD=${SAVE_PWD:-$LUSTRE/tests}
133     export AT_MAX_PATH
134
135     if [ "$ACCEPTOR_PORT" ]; then
136         export PORT_OPT="--port $ACCEPTOR_PORT"
137     fi
138
139     case "x$SEC" in
140         xkrb5*)
141             echo "Using GSS/krb5 ptlrpc security flavor"
142             which lgss_keyring > /dev/null 2>&1 || \
143                 error "built with gss disabled! SEC=$SEC"
144             GSS=true
145             GSS_KRB5=true
146             ;;
147     esac
148
149     case "x$IDUP" in
150         xtrue)
151             IDENTITY_UPCALL=true
152             ;;
153         xfalse)
154             IDENTITY_UPCALL=false
155             ;;
156     esac
157
158     # Paths on remote nodes, if different
159     export RLUSTRE=${RLUSTRE:-$LUSTRE}
160     export RPWD=${RPWD:-$PWD}
161     export I_MOUNTED=${I_MOUNTED:-"no"}
162
163     # command line
164
165     while getopts "rvwf:" opt $*; do
166         case $opt in
167             f) CONFIG=$OPTARG;;
168             r) REFORMAT=--reformat;;
169             v) VERBOSE=true;;
170             w) WRITECONF=writeconf;;
171             \?) usage;;
172         esac
173     done
174
175     shift $((OPTIND - 1))
176     ONLY=${ONLY:-$*}
177
178     [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
179     rm -f $TMP/*active
180
181 }
182
183 case `uname -r` in
184 2.4.*) EXT=".o"; USE_QUOTA=no; [ ! "$CLIENTONLY" ] && FSTYPE=ext3;;
185     *) EXT=".ko"; USE_QUOTA=yes;;
186 esac
187
188 load_module() {
189     EXT=".ko"
190     module=$1
191     shift
192     BASE=`basename $module $EXT`
193     lsmod | grep -q ${BASE} || \
194       if [ -f ${LUSTRE}/${module}${EXT} ]; then
195         insmod ${LUSTRE}/${module}${EXT} $@
196     else
197         # must be testing a "make install" or "rpm" installation
198         # note failed to load ptlrpc_gss is considered not fatal
199         if [ "$BASE" == "ptlrpc_gss" ]; then
200             modprobe $BASE $@ 2>/dev/null || echo "gss/krb5 is not supported"
201         else
202             modprobe $BASE $@
203         fi
204     fi
205 }
206
207 load_modules() {
208     if [ -n "$MODPROBE" ]; then
209         # use modprobe
210     return 0
211     fi
212     if [ "$HAVE_MODULES" = true ]; then
213     # we already loaded
214         return 0
215     fi
216     HAVE_MODULES=true
217
218     echo Loading modules from $LUSTRE
219     load_module ../libcfs/libcfs/libcfs
220     [ "$PTLDEBUG" ] && lctl set_param debug=$PTLDEBUG
221     [ "$SUBSYSTEM" ] && lctl set_param subsystem_debug=${SUBSYSTEM# }
222     local MODPROBECONF=
223     [ -f /etc/modprobe.conf ] && MODPROBECONF=/etc/modprobe.conf
224     [ ! "$MODPROBECONF" -a -d /etc/modprobe.d ] && MODPROBECONF=/etc/modprobe.d/Lustre
225     [ -z "$LNETOPTS" -a "$MODPROBECONF" ] && \
226         LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
227     echo $LNETOPTS | grep -q "accept=all"  || LNETOPTS="$LNETOPTS accept=all";
228     echo "lnet options: '$LNETOPTS'"
229     # note that insmod will ignore anything in modprobe.conf
230     load_module ../lnet/lnet/lnet $LNETOPTS
231     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
232     load_module ../lnet/klnds/$LNETLND
233     load_module lvfs/lvfs
234     load_module obdclass/obdclass
235     load_module ptlrpc/ptlrpc
236     load_module ptlrpc/gss/ptlrpc_gss
237     [ "$USE_QUOTA" = "yes" -a "$LQUOTA" != "no" ] && load_module quota/lquota
238     load_module fld/fld
239     load_module fid/fid
240     load_module lmv/lmv
241     load_module mdc/mdc
242     load_module osc/osc
243     load_module lov/lov
244     load_module mgc/mgc
245     if [ -z "$CLIENTONLY" ] && [ -z "$CLIENTMODSONLY" ]; then
246         grep -q crc16 /proc/kallsyms || { modprobe crc16 2>/dev/null || true; }
247         [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
248         load_module mgs/mgs
249         load_module mds/mds
250         load_module mdd/mdd
251         load_module mdt/mdt
252         load_module lvfs/fsfilt_$FSTYPE
253         load_module cmm/cmm
254         load_module osd/osd
255         load_module ost/ost
256         load_module obdfilter/obdfilter
257     fi
258
259     load_module llite/lustre
260     load_module llite/llite_lloop
261     OGDB=${OGDB:-$TMP}
262     rm -f $OGDB/ogdb-$HOSTNAME
263     [ -d /r ] && OGDB="/r/tmp"
264     $LCTL modules > $OGDB/ogdb-$HOSTNAME
265
266     # 'mount' doesn't look in $PATH, just sbin
267     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
268 }
269
270 RMMOD=rmmod
271 if [ `uname -r | cut -c 3` -eq 4 ]; then
272     RMMOD="modprobe -r"
273 fi
274
275 wait_for_lnet() {
276     local UNLOADED=0
277     local WAIT=0
278     local MAX=60
279     MODULES=$($LCTL modules | awk '{ print $2 }')
280     while [ -n "$MODULES" ]; do
281     sleep 5
282     $RMMOD $MODULES > /dev/null 2>&1 || true
283     MODULES=$($LCTL modules | awk '{ print $2 }')
284         if [ -z "$MODULES" ]; then
285         return 0
286         else
287             WAIT=$((WAIT + 5))
288             echo "waiting, $((MAX - WAIT)) secs left"
289         fi
290         if [ $WAIT -eq $MAX ]; then
291             echo "LNET modules $MODULES will not unload"
292         lsmod
293             return 3
294         fi
295     done
296 }
297
298 unload_dep_module() {
299     #lsmod output
300     #libcfs                107852  17 llite_lloop,lustre,obdfilter,ost,...
301     local MODULE=$1
302     local DEPS=$(lsmod | awk '($1 == "'$MODULE'") { print $4 }' | tr ',' ' ')
303     for SUBMOD in $DEPS; do
304         unload_dep_module $SUBMOD
305     done
306     [ "$MODULE" = "libcfs" ] && $LCTL dk $TMP/debug || true
307     $RMMOD $MODULE || true
308 }
309
310 check_mem_leak () {
311     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd_memory.*leaked" || true)
312     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
313     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
314         echo "$LEAK_LUSTRE" 1>&2
315         echo "$LEAK_PORTALS" 1>&2
316         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
317         echo "Memory leaks detected"
318         [ -n "$IGNORE_LEAK" ] && { echo "ignoring leaks" && return 0; } || true
319         return 1
320     fi
321 }
322
323 unload_modules() {
324     wait_exit_ST client # bug 12845
325
326     lsmod | grep libcfs > /dev/null && $LCTL dl
327     unload_dep_module $FSTYPE
328     unload_dep_module libcfs
329
330     local MODULES=$($LCTL modules | awk '{ print $2 }')
331     if [ -n "$MODULES" ]; then
332         echo "Modules still loaded: "
333         echo $MODULES
334         if [ "$(lctl dl)" ]; then
335             echo "Lustre still loaded"
336             lctl dl || true
337             lsmod
338             return 2
339         else
340             echo "Lustre stopped but LNET is still loaded, waiting..."
341             wait_for_lnet || return 3
342         fi
343
344     fi
345     HAVE_MODULES=false
346
347     check_mem_leak || return 254
348
349     echo "modules unloaded."
350     return 0
351 }
352
353 check_gss_daemon_facet() {
354     facet=$1
355     dname=$2
356
357     num=`do_facet $facet ps -o cmd -C $dname | grep $dname | wc -l`
358     if [ $num -ne 1 ]; then
359         echo "$num instance of $dname on $facet"
360         return 1
361     fi
362     return 0
363 }
364
365 send_sigint() {
366     local facet=$1
367     shift
368     do_facet $facet "killall -2 $@ 2>/dev/null || true"
369 }
370
371 start_gss_daemons() {
372     # starting on MDT
373     for num in `seq $MDSCOUNT`; do
374         do_facet mds$num "$LSVCGSSD -v"
375         if $GSS_PIPEFS; then
376             do_facet mds$num "$LGSSD -v"
377         fi
378     done
379     # starting on OSTs
380     for num in `seq $OSTCOUNT`; do
381         do_facet ost$num "$LSVCGSSD -v"
382     done
383     # starting on client
384     # FIXME: is "client" the right facet name?
385     if $GSS_PIPEFS; then
386         do_facet client "$LGSSD -v"
387     fi
388
389     # wait daemons entering "stable" status
390     sleep 5
391
392     #
393     # check daemons are running
394     #
395     for num in `seq $MDSCOUNT`; do
396         check_gss_daemon_facet mds$num lsvcgssd
397         if $GSS_PIPEFS; then
398             check_gss_daemon_facet mds$num lgssd
399         fi
400     done
401     for num in `seq $OSTCOUNT`; do
402         check_gss_daemon_facet ost$num lsvcgssd
403     done
404     if $GSS_PIPEFS; then
405         check_gss_daemon_facet client lgssd
406     fi
407 }
408
409 stop_gss_daemons() {
410     for num in `seq $MDSCOUNT`; do
411         send_sigint mds$num lsvcgssd lgssd
412     done
413     for num in `seq $OSTCOUNT`; do
414         send_sigint ost$num lsvcgssd
415     done
416     send_sigint client lgssd
417 }
418
419 init_gss() {
420     if $GSS; then
421         start_gss_daemons
422     fi
423 }
424
425 cleanup_gss() {
426     if $GSS; then
427         stop_gss_daemons
428         # maybe cleanup credential cache?
429     fi
430 }
431
432 mdsdevlabel() {
433     local num=$1
434     local device=`mdsdevname $num`
435     local label=`do_facet mds$num "e2label ${device}" | grep -v "CMD: "`
436     echo -n $label
437 }
438
439 ostdevlabel() {
440     local num=$1
441     local device=`ostdevname $num`
442     local label=`do_facet ost$num "e2label ${device}" | grep -v "CMD: "`
443     echo -n $label
444 }
445
446 # Facet functions
447 mount_facet() {
448     local facet=$1
449     shift
450     local dev=${facet}_dev
451     local opt=${facet}_opt
452     echo "Starting ${facet}: ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}"
453     do_facet ${facet} mount -t lustre ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}
454     RC=${PIPESTATUS[0]}
455     if [ $RC -ne 0 ]; then
456         echo "mount -t lustre $@ ${!dev} ${MOUNT%/*}/${facet}"
457         echo "Start of ${!dev} on ${facet} failed ${RC}"
458     else
459         do_facet ${facet} "lctl set_param debug=$PTLDEBUG; \
460             lctl set_param subsystem_debug=${SUBSYSTEM# }; \
461             lctl set_param debug_mb=${DEBUG_SIZE}; \
462             sync"
463
464         label=$(do_facet ${facet} "e2label ${!dev}")
465         [ -z "$label" ] && echo no label for ${!dev} && exit 1
466         eval export ${facet}_svc=${label}
467         echo Started ${label}
468     fi
469     return $RC
470 }
471
472 # start facet device options
473 start() {
474     local facet=$1
475     shift
476     local device=$1
477     shift
478     eval export ${facet}_dev=${device}
479     eval export ${facet}_opt=\"$@\"
480     do_facet ${facet} mkdir -p ${MOUNT%/*}/${facet}
481     mount_facet ${facet}
482     RC=$?
483     return $RC
484 }
485
486 stop() {
487     local running
488     local facet=$1
489     shift
490     HOST=`facet_active_host $facet`
491     [ -z $HOST ] && echo stop: no host for $facet && return 0
492
493     running=$(do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts") || true
494     if [ ${running} -ne 0 ]; then
495         echo "Stopping ${MOUNT%/*}/${facet} (opts:$@)"
496         do_facet ${facet} umount -d $@ ${MOUNT%/*}/${facet}
497     fi
498
499     # umount should block, but we should wait for unrelated obd's
500     # like the MGS or MGC to also stop.
501     wait_exit_ST ${facet}
502 }
503
504 zconf_mount() {
505     local OPTIONS
506     local client=$1
507     local mnt=$2
508     # Only supply -o to mount if we have options
509     if [ -n "$MOUNTOPT" ]; then
510         OPTIONS="-o $MOUNTOPT"
511     fi
512     local device=$MGSNID:/$FSNAME
513     if [ -z "$mnt" -o -z "$FSNAME" ]; then
514         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
515         exit 1
516     fi
517
518     echo "Starting client: $client: $OPTIONS $device $mnt"
519     do_node $client mkdir -p $mnt
520     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
521
522     do_node $client "lctl set_param debug=$PTLDEBUG;
523         lctl set_param subsystem_debug=${SUBSYSTEM# };
524         lctl set_param debug_mb=${DEBUG_SIZE}"
525
526     return 0
527 }
528
529 zconf_umount() {
530     local client=$1
531     local mnt=$2
532     [ "$3" ] && force=-f
533     local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
534     if [ $running -ne 0 ]; then
535         echo "Stopping client $client $mnt (opts:$force)"
536         lsof | grep "$mnt" || true
537         do_node $client umount $force $mnt
538     fi
539 }
540
541 # mount clients if not mouted
542 zconf_mount_clients() {
543     local OPTIONS
544     local clients=$1
545     local mnt=$2
546
547
548     # Only supply -o to mount if we have options
549     if [ -n "$MOUNTOPT" ]; then
550         OPTIONS="-o $MOUNTOPT"
551     fi
552     local device=$MGSNID:/$FSNAME
553     if [ -z "$mnt" -o -z "$FSNAME" ]; then
554         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
555         exit 1
556     fi
557
558     echo "Starting client $clients: $OPTIONS $device $mnt"
559     do_nodes $clients "mount | grep $mnt || { mkdir -p $mnt && mount -t lustre $OPTIONS $device $mnt || false; }"
560
561     echo "Started clients $clients: "
562     do_nodes $clients "mount | grep $mnt"
563
564     do_nodes $clients "lctl set_param debug=$PTLDEBUG;
565         lctl set_param subsystem_debug=${SUBSYSTEM# };
566         lctl set_param debug_mb=${DEBUG_SIZE};"
567
568     return 0
569 }
570
571 zconf_umount_clients() {
572     local clients=$1
573     local mnt=$2
574     [ "$3" ] && force=-f
575
576     echo "Umounting clients: $clients"
577     echo "Stopping clients: $clients $mnt (opts:$force)"
578     do_nodes $clients umount $force $mnt
579 }
580
581 shutdown_facet() {
582     facet=$1
583     if [ "$FAILURE_MODE" = HARD ]; then
584         $POWER_DOWN `facet_active_host $facet`
585         sleep 2
586     elif [ "$FAILURE_MODE" = SOFT ]; then
587         stop $facet
588     fi
589 }
590
591 reboot_facet() {
592     facet=$1
593     if [ "$FAILURE_MODE" = HARD ]; then
594         $POWER_UP `facet_active_host $facet`
595     else
596         sleep 10
597     fi
598 }
599
600 boot_node() {
601     local node=$1
602     if [ "$FAILURE_MODE" = HARD ]; then
603        $POWER_UP $node
604     fi
605 }
606
607 # verify that lustre actually cleaned up properly
608 cleanup_check() {
609     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
610         error "LBUG/LASSERT detected"
611     BUSY=`dmesg | grep -i destruct || true`
612     if [ "$BUSY" ]; then
613         echo "$BUSY" 1>&2
614         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
615         exit 205
616     fi
617
618     check_mem_leak || exit 204
619
620     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
621         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
622
623     if [ "`/sbin/lsmod 2>&1 | egrep 'lnet|libcfs'`" ]; then
624         echo "$0: modules still loaded..." 1>&2
625         /sbin/lsmod 1>&2
626         return 203
627     fi
628     return 0
629 }
630
631 wait_delete_completed () {
632     local TOTALPREV=`lctl get_param -n osc.*.kbytesavail | \
633                      awk 'BEGIN{total=0}; {total+=$1}; END{print total}'`
634
635     local WAIT=0
636     local MAX_WAIT=20
637     while [ "$WAIT" -ne "$MAX_WAIT" ]; do
638         sleep 1
639         TOTAL=`lctl get_param -n osc.*.kbytesavail | \
640                awk 'BEGIN{total=0}; {total+=$1}; END{print total}'`
641         [ "$TOTAL" -eq "$TOTALPREV" ] && break
642         echo "Waiting delete completed ... prev: $TOTALPREV current: $TOTAL "
643         TOTALPREV=$TOTAL
644         WAIT=$(( WAIT + 1))
645     done
646     echo "Delete completed."
647 }
648
649 wait_for_host() {
650     HOST=$1
651     check_network "$HOST" 900
652     while ! do_node $HOST "ls -d $LUSTRE " > /dev/null; do sleep 5; done
653 }
654
655 wait_for() {
656     facet=$1
657     HOST=`facet_active_host $facet`
658     wait_for_host $HOST
659 }
660
661 wait_mds_recovery_done () {
662     local timeout=`do_facet $SINGLEMDS lctl get_param  -n timeout`
663 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2)
664 # as we are in process of changing obd_timeout in different ways
665 # let's set MAX longer than that
666     MAX=$(( timeout * 4 ))
667     WAIT=0
668     while [ $WAIT -lt $MAX ]; do
669         STATUS=`do_facet $SINGLEMDS "lctl get_param -n mdt.*-MDT0000.recovery_status | grep status"`
670         echo $STATUS | grep COMPLETE && return 0
671         sleep 5
672         WAIT=$((WAIT + 5))
673         echo "Waiting $(($MAX - $WAIT)) secs for MDS recovery done"
674     done
675     echo "MDS recovery not done in $MAX sec"
676     return 1
677 }
678
679 wait_exit_ST () {
680     local facet=$1
681
682     local WAIT=0
683     local INTERVAL=1
684     local running
685     # conf-sanity 31 takes a long time cleanup
686     while [ $WAIT -lt 300 ]; do
687         running=$(do_facet ${facet} "lsmod | grep lnet > /dev/null && lctl dl | grep ' ST '") || true
688         [ -z "${running}" ] && return 0
689         echo "waited $WAIT for${running}"
690         [ $INTERVAL -lt 64 ] && INTERVAL=$((INTERVAL + INTERVAL))
691         sleep $INTERVAL
692         WAIT=$((WAIT + INTERVAL))
693     done
694     echo "service didn't stop after $WAIT seconds.  Still running:"
695     echo ${running}
696     return 1
697 }
698
699 wait_remote_prog () {
700    local prog=$1
701    local WAIT=0
702    local INTERVAL=5
703    local rc=0
704
705    [ "$PDSH" = "no_dsh" ] && return 0
706
707    while [ $WAIT -lt $2 ]; do
708         running=$(ps uax | grep "$PDSH.*$prog.*$MOUNT" | grep -v grep) || true
709         [ -z "${running}" ] && return 0 || true
710         echo "waited $WAIT for: "
711         echo "$running"
712         [ $INTERVAL -lt 60 ] && INTERVAL=$((INTERVAL + INTERVAL))
713         sleep $INTERVAL
714         WAIT=$((WAIT + INTERVAL))
715     done
716     local pids=$(ps  uax | grep "$PDSH.*$prog.*$MOUNT" | grep -v grep | awk '{print $2}')
717     [ -z "$pids" ] && return 0
718     echo "$PDSH processes still exists after $WAIT seconds.  Still running: $pids"
719     for pid in $pids; do
720         cat /proc/${pid}/status || true
721         cat /proc/${pid}/wchan || true
722         echo "Killing $pid"
723         kill -9 $pid || true
724         sleep 1
725         ps -P $pid && rc=1
726     done
727
728     return $rc
729 }
730
731 client_df() {
732     # not every config has many clients
733     if [ ! -z "$CLIENTS" ]; then
734         $PDSH $CLIENTS "df $MOUNT" > /dev/null
735     else
736         df $MOUNT > /dev/null
737     fi
738 }
739
740 client_reconnect() {
741     uname -n >> $MOUNT/recon
742     if [ -z "$CLIENTS" ]; then
743         df $MOUNT; uname -n >> $MOUNT/recon
744     else
745         do_nodes $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
746     fi
747     echo Connected clients:
748     cat $MOUNT/recon
749     ls -l $MOUNT/recon > /dev/null
750     rm $MOUNT/recon
751 }
752
753 facet_failover() {
754     facet=$1
755     sleep_time=$2
756     echo "Failing $facet on node `facet_active_host $facet`"
757     shutdown_facet $facet
758     [ -n "$sleep_time" ] && sleep $sleep_time
759     reboot_facet $facet
760     client_df &
761     DFPID=$!
762     echo "df pid is $DFPID"
763     change_active $facet
764     TO=`facet_active_host $facet`
765     echo "Failover $facet to $TO"
766     wait_for $facet
767     mount_facet $facet || error "Restart of $facet failed"
768 }
769
770 obd_name() {
771     local facet=$1
772 }
773
774 replay_barrier() {
775     local facet=$1
776     do_facet $facet sync
777     df $MOUNT
778     local svc=${facet}_svc
779     do_facet $facet $LCTL --device %${!svc} readonly
780     do_facet $facet $LCTL --device %${!svc} notransno
781     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
782     $LCTL mark "local REPLAY BARRIER on ${!svc}"
783 }
784
785 replay_barrier_nodf() {
786     local facet=$1    echo running=${running}
787     do_facet $facet sync
788     local svc=${facet}_svc
789     echo Replay barrier on ${!svc}
790     do_facet $facet $LCTL --device %${!svc} readonly
791     do_facet $facet $LCTL --device %${!svc} notransno
792     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
793     $LCTL mark "local REPLAY BARRIER on ${!svc}"
794 }
795
796 replay_barrier_nosync() {
797     local facet=$1    echo running=${running}
798     local svc=${facet}_svc
799     echo Replay barrier on ${!svc}
800     do_facet $facet $LCTL --device %${!svc} readonly
801     do_facet $facet $LCTL --device %${!svc} notransno
802     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
803     $LCTL mark "local REPLAY BARRIER on ${!svc}"
804 }
805
806 mds_evict_client() {
807     UUID=`lctl get_param -n mdc.${mds1_svc}-mdc-*.uuid`
808     do_facet mds1 "lctl set_param -n mdt.${mds1_svc}.evict_client $UUID"
809 }
810
811 ost_evict_client() {
812     UUID=`lctl get_param -n devices| grep ${ost1_svc}-osc- | egrep -v 'MDT' | awk '{print $5}'`
813     do_facet ost1 "lctl set_param -n obdfilter.${ost1_svc}.evict_client $UUID"
814 }
815
816 fail() {
817     facet_failover $* || error "failover: $?"
818     df $MOUNT || error "post-failover df: $?"
819 }
820
821 fail_nodf() {
822         local facet=$1
823         facet_failover $facet
824 }
825
826 fail_abort() {
827     local facet=$1
828     stop $facet
829     change_active $facet
830     mount_facet $facet -o abort_recovery
831     df $MOUNT || echo "first df failed: $?"
832     sleep 1
833     df $MOUNT || error "post-failover df: $?"
834 }
835
836 do_lmc() {
837     echo There is no lmc.  This is mountconf, baby.
838     exit 1
839 }
840
841 h2gm () {
842     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
843         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
844         echo $ID"@gm"
845     fi
846 }
847
848 h2name_or_ip() {
849     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
850         echo $1"@$2"
851     fi
852 }
853
854 h2ptl() {
855    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
856        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
857        if [ -z "$ID" ]; then
858            echo "Could not get a ptl id for $1..."
859            exit 1
860        fi
861        echo $ID"@ptl"
862    fi
863 }
864 declare -fx h2ptl
865
866 h2tcp() {
867     h2name_or_ip "$1" "tcp"
868 }
869 declare -fx h2tcp
870
871 h2elan() {
872     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
873         if type __h2elan >/dev/null 2>&1; then
874             ID=$(__h2elan $1)
875         else
876             ID=`echo $1 | sed 's/[^0-9]*//g'`
877         fi
878         echo $ID"@elan"
879     fi
880 }
881 declare -fx h2elan
882
883 h2openib() {
884     h2name_or_ip "$1" "openib"
885 }
886 declare -fx h2openib
887
888 h2o2ib() {
889     h2name_or_ip "$1" "o2ib"
890 }
891 declare -fx h2o2ib
892
893 facet_host() {
894     local facet=$1
895
896     [ "$facet" == client ] && echo -n $HOSTNAME && return
897     varname=${facet}_HOST
898     if [ -z "${!varname}" ]; then
899         if [ "${facet:0:3}" == "ost" ]; then
900             eval ${facet}_HOST=${ost_HOST}
901         fi
902     fi
903     echo -n ${!varname}
904 }
905
906 facet_active() {
907     local facet=$1
908     local activevar=${facet}active
909
910     if [ -f $TMP/${facet}active ] ; then
911         source $TMP/${facet}active
912     fi
913
914     active=${!activevar}
915     if [ -z "$active" ] ; then
916         echo -n ${facet}
917     else
918         echo -n ${active}
919     fi
920 }
921
922 facet_active_host() {
923     local facet=$1
924     local active=`facet_active $facet`
925     if [ "$facet" == client ]; then
926         echo $HOSTNAME
927     else
928         echo `facet_host $active`
929     fi
930 }
931
932 change_active() {
933     local facet=$1
934     failover=${facet}failover
935     host=`facet_host $failover`
936     [ -z "$host" ] && return
937     curactive=`facet_active $facet`
938     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
939         eval export ${facet}active=$facet
940     else
941         eval export ${facet}active=$failover
942     fi
943     # save the active host for this facet
944     activevar=${facet}active
945     echo "$activevar=${!activevar}" > $TMP/$activevar
946 }
947
948 do_node() {
949     HOST=$1
950     shift
951     local myPDSH=$PDSH
952     if [ "$HOST" = "$HOSTNAME" ]; then
953         myPDSH="no_dsh"
954     elif [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" ]; then
955         echo "cannot run remote command on $HOST with $myPDSH"
956         return 128
957     fi
958     if $VERBOSE; then
959         echo "CMD: $HOST $@" >&2
960         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
961     fi
962
963     if [ "$myPDSH" = "rsh" ]; then
964 # we need this because rsh does not return exit code of an executed command
965         local command_status="$TMP/cs"
966         rsh $HOST ":> $command_status"
967         rsh $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin;
968                     cd $RPWD; sh -c \"$@\") ||
969                     echo command failed >$command_status"
970         [ -n "$($myPDSH $HOST cat $command_status)" ] && return 1 || true
971         return 0
972     fi
973     $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
974     return ${PIPESTATUS[0]}
975 }
976
977 single_local_node () {
978    [ "$1" = "$HOSTNAME" ]
979 }
980
981 do_nodes() {
982     local rnodes=$1
983     shift
984
985     if $(single_local_node $rnodes); then
986         do_node $rnodes $@
987         return $?
988     fi
989
990     # This is part from do_node
991     local myPDSH=$PDSH
992
993     [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" -o "$myPDSH" = "rsh" ] && \
994         echo "cannot run remote command on $rnodes with $myPDSH" && return 128
995
996     if $VERBOSE; then
997         echo "CMD: $rnodes $@" >&2
998         $myPDSH $rnodes $LCTL mark "$@" > /dev/null 2>&1 || :
999     fi
1000
1001     $myPDSH $rnodes "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed -re "s/\w+:\s//g"
1002     return ${PIPESTATUS[0]}
1003 }
1004
1005 do_facet() {
1006     facet=$1
1007     shift
1008     HOST=`facet_active_host $facet`
1009     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
1010     do_node $HOST "$@"
1011 }
1012
1013 add() {
1014     local facet=$1
1015     shift
1016     # make sure its not already running
1017     stop ${facet} -f
1018     rm -f $TMP/${facet}active
1019     do_facet ${facet} $MKFS $*
1020 }
1021
1022 ostdevname() {
1023     num=$1
1024     DEVNAME=OSTDEV$num
1025     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
1026     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
1027     echo -n $DEVPTR
1028 }
1029
1030 mdsdevname() {
1031     num=$1
1032     DEVNAME=MDSDEV$num
1033     #if $MDSDEVn isn't defined, default is $MDSDEVBASE + num
1034     eval DEVPTR=${!DEVNAME:=${MDSDEVBASE}${num}}
1035     echo -n $DEVPTR
1036 }
1037
1038 ########
1039 ## MountConf setup
1040
1041 stopall() {
1042     # make sure we are using the primary server, so test-framework will
1043     # be able to clean up properly.
1044     activemds=`facet_active mds1`
1045     if [ $activemds != "mds1" ]; then
1046         fail mds1
1047     fi
1048
1049     # assume client mount is local
1050     grep " $MOUNT " /proc/mounts && zconf_umount $HOSTNAME $MOUNT $*
1051     grep " $MOUNT2 " /proc/mounts && zconf_umount $HOSTNAME $MOUNT2 $*
1052
1053     if [ -n "$CLIENTS" ]; then
1054             zconf_umount_clients $CLIENTS $MOUNT "$*" || true
1055             [ -n "$MOUNT2" ] && zconf_umount_clients $CLIENTS $MOUNT2 "$*" || true
1056     fi
1057
1058     [ "$CLIENTONLY" ] && return
1059     # The add fn does rm ${facet}active file, this would be enough
1060     # if we use do_facet <facet> only after the facet added, but
1061     # currently we use do_facet mds in local.sh
1062     for num in `seq $MDSCOUNT`; do
1063         stop mds$num -f
1064         rm -f ${TMP}/mds${num}active
1065     done
1066
1067     for num in `seq $OSTCOUNT`; do
1068         stop ost$num -f
1069         rm -f $TMP/ost${num}active
1070     done
1071
1072     return 0
1073 }
1074
1075 cleanupall() {
1076     stopall $*
1077     unload_modules
1078     cleanup_gss
1079 }
1080
1081 mdsmkfsopts()
1082 {
1083     local nr=$1
1084     test $nr = 1 && echo -n $MDS_MKFS_OPTS || echo -n $MDSn_MKFS_OPTS
1085 }
1086
1087 formatall() {
1088     if [ "$IAMDIR" == "yes" ]; then
1089         MDS_MKFS_OPTS="$MDS_MKFS_OPTS --iam-dir"
1090         MDSn_MKFS_OPTS="$MDSn_MKFS_OPTS --iam-dir"
1091     fi
1092
1093     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
1094
1095     if [ ! -z $SEC ]; then
1096         MDS_MKFS_OPTS="$MDS_MKFS_OPTS --param srpc.flavor.default=$SEC"
1097         MDSn_MKFS_OPTS="$MDSn_MKFS_OPTS --param srpc.flavor.default=$SEC"
1098         OST_MKFS_OPTS="$OST_MKFS_OPTS --param srpc.flavor.default=$SEC"
1099     fi
1100
1101     stopall
1102     # We need ldiskfs here, may as well load them all
1103     load_modules
1104     [ "$CLIENTONLY" ] && return
1105     echo "Formatting mdts, osts"
1106     for num in `seq $MDSCOUNT`; do
1107         echo "Format mds$num: $(mdsdevname $num)"
1108         if $VERBOSE; then
1109             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` || exit 9
1110         else
1111             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` > /dev/null || exit 9
1112         fi
1113     done
1114
1115     for num in `seq $OSTCOUNT`; do
1116         echo "Format ost$num: $(ostdevname $num)"
1117         if $VERBOSE; then
1118             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` || exit 10
1119         else
1120             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` > /dev/null || exit 10
1121         fi
1122     done
1123 }
1124
1125 mount_client() {
1126     grep " $1 " /proc/mounts || zconf_mount $HOSTNAME $*
1127 }
1128
1129 umount_client() {
1130     grep " $1 " /proc/mounts && zconf_umount `hostname` $*
1131 }
1132
1133 # return value:
1134 # 0: success, the old identity set already.
1135 # 1: success, the old identity does not set.
1136 # 2: fail.
1137 switch_identity() {
1138     local num=$1
1139     local switch=$2
1140     local j=`expr $num - 1`
1141     local MDT="`(do_facet mds$num lctl get_param -N mdt.*MDT*$j 2>/dev/null | cut -d"." -f2 2>/dev/null) || true`"
1142
1143     if [ -z "$MDT" ]; then
1144         return 2
1145     fi
1146
1147     local old="`do_facet mds$num "lctl get_param -n mdt.$MDT.identity_upcall"`"
1148
1149     if $switch; then
1150         do_facet mds$num "lctl set_param -n mdt.$MDT.identity_upcall \"$L_GETIDENTITY\""
1151     else
1152         do_facet mds$num "lctl set_param -n mdt.$MDT.identity_upcall \"NONE\""
1153     fi
1154
1155     do_facet mds$num "lctl set_param -n mdt/$MDT/identity_flush \"-1\""
1156
1157     if [ $old = "NONE" ]; then
1158         return 1
1159     else
1160         return 0
1161     fi
1162 }
1163
1164 remount_client()
1165 {
1166         zconf_umount `hostname` $1 || error "umount failed"
1167         zconf_mount `hostname` $1 || error "mount failed"
1168 }
1169
1170 set_obd_timeout() {
1171     local facet=$1
1172     local timeout=$2
1173
1174     do_facet $facet lsmod | grep -q obdclass || \
1175         do_facet $facet "modprobe obdclass"
1176
1177     do_facet $facet "lctl set_param timeout=$timeout"
1178 }
1179
1180 writeconf_facet () {
1181     local facet=$1
1182     local dev=$2
1183
1184     do_facet $facet "$TUNEFS --writeconf $dev"
1185 }
1186
1187 writeconf_all () {
1188     for num in `seq $MDSCOUNT`; do
1189         DEVNAME=$(mdsdevname $num)
1190         writeconf_facet mds$num $DEVNAME
1191     done
1192
1193     for num in `seq $OSTCOUNT`; do
1194         DEVNAME=$(ostdevname $num)
1195         writeconf_facet ost$num $DEVNAME
1196     done
1197 }
1198
1199 setupall() {
1200     load_modules
1201     init_gss
1202     if [ -z "$CLIENTONLY" ]; then
1203         echo "Setup mdts, osts"
1204         echo $WRITECONF | grep -q "writeconf" && \
1205             writeconf_all
1206         for num in `seq $MDSCOUNT`; do
1207             DEVNAME=$(mdsdevname $num)
1208             set_obd_timeout mds$num $TIMEOUT
1209             start mds$num $DEVNAME $MDS_MOUNT_OPTS
1210
1211             # We started mds, now we should set failover variables properly.
1212             # Set mds${num}failover_HOST if it is not set (the default failnode).
1213             local varname=mds${num}failover_HOST
1214             if [ -z "${!varname}" ]; then
1215                 eval mds${num}failover_HOST=$(facet_host mds$num)
1216             fi
1217
1218             if [ $IDENTITY_UPCALL != "default" ]; then
1219                 switch_identity $num $IDENTITY_UPCALL
1220             fi
1221         done
1222         for num in `seq $OSTCOUNT`; do
1223             DEVNAME=$(ostdevname $num)
1224             set_obd_timeout ost$num $TIMEOUT
1225             start ost$num $DEVNAME $OST_MOUNT_OPTS
1226
1227             # We started ost$num, now we should set ost${num}failover variable properly.
1228             # Set ost${num}failover_HOST if it is not set (the default failnode).
1229             varname=ost${num}failover_HOST
1230             if [ -z "${!varname}" ]; then
1231                 eval ost${num}failover_HOST=$(facet_host ost${num})
1232             fi
1233
1234         done
1235     fi
1236     # wait a while to allow sptlrpc configuration be propogated to targets,
1237     # only needed when mounting new target devices.
1238     $GSS && sleep 10
1239
1240     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
1241     mount_client $MOUNT
1242     [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT
1243
1244     if [ "$MOUNT_2" ]; then
1245         mount_client $MOUNT2
1246         [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT2
1247     fi
1248
1249     init_versions_vars
1250
1251     # by remounting mdt before ost, initial connect from mdt to ost might
1252     # timeout because ost is not ready yet. wait some time to its fully
1253     # recovery. initial obd_connect timeout is 5s; in GSS case it's preceeded
1254     # by a context negotiation rpc with $TIMEOUT.
1255     # FIXME better by monitoring import status.
1256     if $GSS; then
1257         sleep $((TIMEOUT + 5))
1258     else
1259         sleep 5
1260     fi
1261 }
1262
1263 mounted_lustre_filesystems() {
1264         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
1265 }
1266
1267 init_facet_vars () {
1268     local facet=$1
1269     shift
1270     local device=$1
1271
1272     shift
1273
1274     eval export ${facet}_dev=${device}
1275     eval export ${facet}_opt=\"$@\"
1276
1277     local dev=${facet}_dev
1278     local label=$(do_facet ${facet} "e2label ${!dev}")
1279     [ -z "$label" ] && echo no label for ${!dev} && exit 1
1280
1281     eval export ${facet}_svc=${label}
1282
1283     local varname=${facet}failover_HOST
1284     if [ -z "${!varname}" ]; then
1285        eval $varname=$(facet_host $facet) 
1286     fi
1287 }
1288
1289 init_facets_vars () {
1290     local DEVNAME
1291
1292     for num in `seq $MDSCOUNT`; do
1293         DEVNAME=`mdsdevname $num`
1294         init_facet_vars mds$num $DEVNAME $MDS_MOUNT_OPTS
1295     done
1296
1297     for num in `seq $OSTCOUNT`; do
1298         DEVNAME=`ostdevname $num`
1299         init_facet_vars ost$num $DEVNAME $OST_MOUNT_OPTS
1300     done
1301 }
1302
1303 init_versions_vars () {
1304     export MDSVER=$(do_facet $SINGLEMDS "lctl get_param version" | cut -d. -f1,2)
1305     export OSTVER=$(do_facet ost1 "lctl get_param version" | cut -d. -f1,2)
1306     export CLIVER=$(lctl get_param version | cut -d. -f 1,2)
1307 }
1308
1309 check_config () {
1310     local mntpt=$1
1311     local myMGS_host=$mgs_HOST   
1312     if [ "$NETTYPE" = "ptl" ]; then
1313         myMGS_host=$(h2ptl $mgs_HOST | sed -e s/@ptl//) 
1314     fi
1315
1316     echo Checking config lustre mounted on $mntpt
1317     local mgshost=$(mount | grep " $mntpt " | awk -F@ '{print $1}')
1318     mgshost=$(echo $mgshost | awk -F: '{print $1}')
1319
1320     if [ "$mgshost" != "$myMGS_host" ]; then
1321         FAIL_ON_ERROR=true \
1322             error "Bad config file: lustre is mounted with mgs $mgshost, but mgs_HOST=$mgs_HOST, NETTYPE=$NETTYPE
1323                    Please use correct config or set mds_HOST correctly!"
1324     fi
1325 }
1326
1327 check_and_setup_lustre() {
1328     local MOUNTED=$(mounted_lustre_filesystems)
1329     if [ -z "$MOUNTED" ] || ! $(echo $MOUNTED | grep -w -q $MOUNT); then
1330         [ "$REFORMAT" ] && formatall
1331         setupall
1332         MOUNTED=$(mounted_lustre_filesystems | head -1)
1333         [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
1334         export I_MOUNTED=yes
1335     else
1336         check_config $MOUNT
1337         init_facets_vars
1338         init_versions_vars
1339     fi
1340     if [ "$ONLY" == "setup" ]; then
1341         exit 0
1342     fi
1343 }
1344
1345 cleanup_and_setup_lustre() {
1346     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
1347         lctl set_param debug=0 || true
1348         cleanupall
1349         if [ "$ONLY" == "cleanup" ]; then
1350             exit 0
1351         fi
1352     fi
1353     check_and_setup_lustre
1354 }
1355
1356 check_and_cleanup_lustre() {
1357     if [ "`mount | grep $MOUNT`" ]; then
1358         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]*
1359     fi
1360     if [ "$I_MOUNTED" = "yes" ]; then
1361         cleanupall -f || error "cleanup failed"
1362     fi
1363     unset I_MOUNTED
1364 }
1365
1366 #######
1367 # General functions
1368
1369 check_network() {
1370     local NETWORK=0
1371     local WAIT=0
1372     local MAX=$2
1373     while [ $NETWORK -eq 0 ]; do
1374         ping -c 1 -w 3 $1 > /dev/null
1375         if [ $? -eq 0 ]; then
1376             NETWORK=1
1377         else
1378             WAIT=$((WAIT + 5))
1379             echo "waiting for $1, $((MAX - WAIT)) secs left"
1380             sleep 5
1381         fi
1382         if [ $WAIT -gt $MAX ]; then
1383             echo "Network not available"
1384             exit 1
1385         fi
1386     done
1387 }
1388 check_port() {
1389     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
1390         sleep 9
1391     done
1392 }
1393
1394 no_dsh() {
1395     shift
1396     eval $@
1397 }
1398
1399 comma_list() {
1400     # the sed converts spaces to commas, but leaves the last space
1401     # alone, so the line doesn't end with a comma.
1402     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
1403 }
1404
1405 absolute_path() {
1406     (cd `dirname $1`; echo $PWD/`basename $1`)
1407 }
1408
1409 ##################################
1410 # Adaptive Timeouts funcs
1411
1412 at_is_valid() {
1413     if [ -z "$AT_MAX_PATH" ]; then
1414         AT_MAX_PATH=$(do_facet $SINGLEMDS "find /sys/ -name at_max")
1415         [ -z "$AT_MAX_PATH" ] && echo "missing /sys/.../at_max " && return 1
1416     fi
1417     return 0
1418 }
1419
1420 at_is_enabled() {
1421     at_is_valid || error "invalid call"
1422
1423     # only check mds, we assume at_max is the same on all nodes
1424     local at_max=$(do_facet $SINGLEMDS "cat $AT_MAX_PATH")
1425     if [ $at_max -eq 0 ]; then
1426         return 1
1427     else
1428         return 0
1429     fi
1430 }
1431
1432 at_max_get() {
1433     local facet=$1
1434
1435     at_is_valid || error "invalid call"
1436
1437     # suppose that all ost-s has the same at_max set
1438     if [ $facet == "ost" ]; then
1439         do_facet ost1 "cat $AT_MAX_PATH"
1440     else
1441         do_facet $facet "cat $AT_MAX_PATH"
1442     fi
1443 }
1444
1445 at_max_set() {
1446     local at_max=$1
1447     shift
1448
1449     at_is_valid || error "invalid call"
1450
1451     local facet
1452     for facet in $@; do
1453         if [ $facet == "ost" ]; then
1454             for i in `seq $OSTCOUNT`; do
1455                 do_facet ost$i "echo $at_max > $AT_MAX_PATH"
1456             done
1457         elif [ $facet == "mds" ]; then
1458             for i in `seq $MDSCOUNT`; do
1459                 do_facet mds$i "echo $at_max > $AT_MAX_PATH"
1460             done
1461         else
1462             do_facet $facet "echo $at_max > $AT_MAX_PATH"
1463         fi
1464     done
1465 }
1466
1467 ##################################
1468 # OBD_FAIL funcs
1469
1470 drop_request() {
1471 # OBD_FAIL_MDS_ALL_REQUEST_NET
1472     RC=0
1473     do_facet $SINGLEMDS lctl set_param fail_loc=0x123
1474     do_facet client "$1" || RC=$?
1475     do_facet $SINGLEMDS lctl set_param fail_loc=0
1476     return $RC
1477 }
1478
1479 drop_reply() {
1480 # OBD_FAIL_MDS_ALL_REPLY_NET
1481     RC=0
1482     do_facet $SINGLEMDS lctl set_param fail_loc=0x122
1483     do_facet client "$@" || RC=$?
1484     do_facet $SINGLEMDS lctl set_param fail_loc=0
1485     return $RC
1486 }
1487
1488 drop_reint_reply() {
1489 # OBD_FAIL_MDS_REINT_NET_REP
1490     RC=0
1491     do_facet $SINGLEMDS lctl set_param fail_loc=0x119
1492     do_facet client "$@" || RC=$?
1493     do_facet $SINGLEMDS lctl set_param fail_loc=0
1494     return $RC
1495 }
1496
1497 pause_bulk() {
1498 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
1499     RC=0
1500     do_facet ost1 lctl set_param fail_loc=0x214
1501     do_facet client "$1" || RC=$?
1502     do_facet client "sync"
1503     do_facet ost1 lctl set_param fail_loc=0
1504     return $RC
1505 }
1506
1507 drop_ldlm_cancel() {
1508 #define OBD_FAIL_LDLM_CANCEL             0x304
1509     RC=0
1510     do_facet client lctl set_param fail_loc=0x304
1511     do_facet client "$@" || RC=$?
1512     do_facet client lctl set_param fail_loc=0
1513     return $RC
1514 }
1515
1516 drop_bl_callback() {
1517 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
1518     RC=0
1519     do_facet client lctl set_param fail_loc=0x305
1520     do_facet client "$@" || RC=$?
1521     do_facet client lctl set_param fail_loc=0
1522     return $RC
1523 }
1524
1525 drop_ldlm_reply() {
1526 #define OBD_FAIL_LDLM_REPLY              0x30c
1527     RC=0
1528     do_facet $SINGLEMDS lctl set_param fail_loc=0x30c
1529     do_facet client "$@" || RC=$?
1530     do_facet $SINGLEMDS lctl set_param fail_loc=0
1531     return $RC
1532 }
1533
1534 clear_failloc() {
1535     facet=$1
1536     pause=$2
1537     sleep $pause
1538     echo "clearing fail_loc on $facet"
1539     do_facet $facet "lctl set_param fail_loc=0 2>/dev/null || true"
1540 }
1541
1542 set_nodes_failloc () {
1543     local nodes=$1
1544     local node
1545
1546     for node in $nodes ; do
1547         do_node $node lctl set_param fail_loc=$2
1548     done
1549 }
1550
1551 cancel_lru_locks() {
1552     $LCTL mark "cancel_lru_locks $1 start"
1553     for d in `lctl get_param -N ldlm.namespaces.*.lru_size | egrep -i $1`; do
1554         $LCTL set_param -n $d=clear
1555     done
1556     $LCTL get_param ldlm.namespaces.*.lock_unused_count | egrep -i $1 | grep -v '=0'
1557     $LCTL mark "cancel_lru_locks $1 stop"
1558 }
1559
1560 default_lru_size()
1561 {
1562         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
1563         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
1564         echo "$DEFAULT_LRU_SIZE"
1565 }
1566
1567 lru_resize_enable()
1568 {
1569     lctl set_param ldlm.namespaces.*$1*.lru_size=0
1570 }
1571
1572 lru_resize_disable()
1573 {
1574     lctl set_param ldlm.namespaces.*$1*.lru_size $(default_lru_size)
1575 }
1576
1577 pgcache_empty() {
1578     local FILE
1579     for FILE in `lctl get_param -N "llite.*.dump_page_cache"`; do
1580         if [ `lctl get_param -n $FILE | wc -l` -gt 1 ]; then
1581             echo there is still data in page cache $FILE ?
1582             lctl get_param -n $FILE
1583             return 1
1584         fi
1585     done
1586     return 0
1587 }
1588
1589 debugsave() {
1590     DEBUGSAVE="$(lctl get_param -n debug)"
1591 }
1592
1593 debugrestore() {
1594     [ -n "$DEBUGSAVE" ] && lctl set_param debug="${DEBUGSAVE}"
1595     DEBUGSAVE=""
1596 }
1597
1598 ##################################
1599 # Test interface
1600 ##################################
1601
1602 error_noexit() {
1603     local TYPE=${TYPE:-"FAIL"}
1604     local ERRLOG
1605     lctl set_param fail_loc=0 2>/dev/null || true
1606     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
1607     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
1608     echo "Dumping lctl log to $ERRLOG"
1609     # We need to dump the logs on all nodes
1610     local NODES=$(nodes_list)
1611     for NODE in $NODES; do
1612         do_node $NODE $LCTL dk $ERRLOG
1613     done
1614     debugrestore
1615     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
1616 }
1617
1618 error() {
1619     error_noexit "$@"
1620     $FAIL_ON_ERROR && exit 1 || true
1621 }
1622
1623 error_exit() {
1624     error_noexit "$@"
1625     exit 1
1626 }
1627
1628 # use only if we are ignoring failures for this test, bugno required.
1629 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
1630 # e.g. error_ignore 5494 "your message"
1631 error_ignore() {
1632     local TYPE="IGNORE (bz$1)"
1633     shift
1634     error_noexit "$@"
1635 }
1636
1637 skip () {
1638         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
1639         [ "$TESTSUITELOG" ] && \
1640                 echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG || true
1641 }
1642
1643 build_test_filter() {
1644     [ "$ONLY" ] && log "only running test `echo $ONLY`"
1645     for O in $ONLY; do
1646         eval ONLY_${O}=true
1647     done
1648     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
1649         log "excepting tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
1650     [ "$EXCEPT_SLOW" ] && \
1651         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
1652     for E in $EXCEPT $ALWAYS_EXCEPT; do
1653         eval EXCEPT_${E}=true
1654     done
1655     for E in $EXCEPT_SLOW; do
1656         eval EXCEPT_SLOW_${E}=true
1657     done
1658     for G in $GRANT_CHECK_LIST; do
1659         eval GCHECK_ONLY_${G}=true
1660         done
1661 }
1662
1663 _basetest() {
1664     echo $*
1665 }
1666
1667 basetest() {
1668     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
1669 }
1670
1671 # print a newline if the last test was skipped
1672 export LAST_SKIPPED=
1673 run_test() {
1674     assert_DIR
1675
1676     export base=`basetest $1`
1677     if [ ! -z "$ONLY" ]; then
1678         testname=ONLY_$1
1679         if [ ${!testname}x != x ]; then
1680             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
1681             run_one $1 "$2"
1682             return $?
1683         fi
1684         testname=ONLY_$base
1685         if [ ${!testname}x != x ]; then
1686             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
1687             run_one $1 "$2"
1688             return $?
1689         fi
1690         LAST_SKIPPED="y"
1691         echo -n "."
1692         return 0
1693     fi
1694     testname=EXCEPT_$1
1695     if [ ${!testname}x != x ]; then
1696         LAST_SKIPPED="y"
1697         TESTNAME=test_$1 skip "skipping excluded test $1"
1698         return 0
1699     fi
1700     testname=EXCEPT_$base
1701     if [ ${!testname}x != x ]; then
1702         LAST_SKIPPED="y"
1703         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
1704         return 0
1705     fi
1706     testname=EXCEPT_SLOW_$1
1707     if [ ${!testname}x != x ]; then
1708         LAST_SKIPPED="y"
1709         TESTNAME=test_$1 skip "skipping SLOW test $1"
1710         return 0
1711     fi
1712     testname=EXCEPT_SLOW_$base
1713     if [ ${!testname}x != x ]; then
1714         LAST_SKIPPED="y"
1715         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
1716         return 0
1717     fi
1718
1719     LAST_SKIPPED=
1720     run_one $1 "$2"
1721
1722     return $?
1723 }
1724
1725 EQUALS="======================================================================"
1726 equals_msg() {
1727     msg="$@"
1728
1729     local suffixlen=$((${#EQUALS} - ${#msg}))
1730     [ $suffixlen -lt 5 ] && suffixlen=5
1731     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
1732 }
1733
1734 log() {
1735     echo "$*"
1736     lsmod | grep lnet > /dev/null || load_modules
1737
1738     local MSG="$*"
1739     # Get rif of '
1740     MSG=${MSG//\'/\\\'}
1741     MSG=${MSG//\(/\\\(}
1742     MSG=${MSG//\)/\\\)}
1743     MSG=${MSG//\;/\\\;}
1744     MSG=${MSG//\|/\\\|}
1745     MSG=${MSG//\>/\\\>}
1746     MSG=${MSG//\</\\\<}
1747     MSG=${MSG//\//\\\/}
1748     local NODES=$(nodes_list)
1749     for NODE in $NODES; do
1750         do_node $NODE $LCTL mark "$MSG" 2> /dev/null || true
1751     done
1752 }
1753
1754 trace() {
1755         log "STARTING: $*"
1756         strace -o $TMP/$1.strace -ttt $*
1757         RC=$?
1758         log "FINISHED: $*: rc $RC"
1759         return 1
1760 }
1761
1762 pass() {
1763     echo PASS $@
1764 }
1765
1766 check_mds() {
1767     FFREE=$(do_node $SINGLEMDS lctl get_param -n osd.*MDT*.filesfree | awk 'BEGIN{avail=0}; {avail+=$1}; END{print avail}')
1768     FTOTAL=$(do_node $SINGLEMDS lctl get_param -n osd.*MDT*.filestotal | awk 'BEGIN{avail=0}; {avail+=$1}; END{print avail}')
1769     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
1770 }
1771
1772 reset_fail_loc () {
1773     local myNODES=$(nodes_list)
1774     local NODE
1775
1776     for NODE in $myNODES; do
1777         do_node $NODE "lctl set_param fail_loc=0 2>/dev/null || true"
1778     done
1779 }
1780
1781 run_one() {
1782     testnum=$1
1783     message=$2
1784     tfile=f${testnum}
1785     export tdir=d0.${TESTSUITE}/d${base}
1786     local SAVE_UMASK=`umask`
1787     umask 0022
1788
1789     BEFORE=`date +%s`
1790     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
1791     #check_mds
1792     export TESTNAME=test_$testnum
1793     test_${testnum} || error "test_$testnum failed with $?"
1794     #check_mds
1795     cd $SAVE_PWD
1796     reset_fail_loc
1797     check_grant ${testnum} || error "check_grant $testnum failed with $?"
1798     check_catastrophe || error "LBUG/LASSERT detected"
1799     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
1800     pass "($((`date +%s` - $BEFORE))s)"
1801     unset TESTNAME
1802     unset tdir
1803     umask $SAVE_UMASK
1804 }
1805
1806 canonical_path() {
1807     (cd `dirname $1`; echo $PWD/`basename $1`)
1808 }
1809
1810 sync_clients() {
1811     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync
1812     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync
1813         cd $SAVE_PWD
1814 }
1815
1816 check_grant() {
1817     export base=`basetest $1`
1818     [ "$CHECK_GRANT" == "no" ] && return 0
1819
1820         testname=GCHECK_ONLY_${base}
1821         [ ${!testname}x == x ] && return 0
1822
1823     echo -n "checking grant......"
1824         cd $SAVE_PWD
1825         # write some data to sync client lost_grant
1826         rm -f $DIR1/${tfile}_check_grant_* 2>&1
1827         for i in `seq $OSTCOUNT`; do
1828                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
1829                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
1830                                               count=1 > /dev/null 2>&1
1831         done
1832         # sync all the data and make sure no pending data on server
1833         sync_clients
1834         
1835         #get client grant and server grant
1836         client_grant=0
1837     for d in `lctl get_param -n osc.*.cur_grant_bytes`; do
1838                 client_grant=$((client_grant + $d))
1839         done
1840         server_grant=0
1841         for d in `lctl get_param -n obdfilter.*.tot_granted`; do
1842                 server_grant=$((server_grant + $d))
1843         done
1844
1845         # cleanup the check_grant file
1846         for i in `seq $OSTCOUNT`; do
1847                 rm $DIR1/${tfile}_check_grant_$i
1848         done
1849
1850         #check whether client grant == server grant
1851         if [ $client_grant != $server_grant ]; then
1852                 echo "failed: client:${client_grant} server: ${server_grant}"
1853                 return 1
1854         else
1855                 echo "pass"
1856         fi
1857 }
1858
1859 ########################
1860 # helper functions
1861
1862 osc_to_ost()
1863 {
1864     osc=$1
1865     ost=`echo $1 | awk -F_ '{print $3}'`
1866     if [ -z $ost ]; then
1867         ost=`echo $1 | sed 's/-osc.*//'`
1868     fi
1869     echo $ost
1870 }
1871
1872 remote_node () {
1873     local node=$1
1874     [ "$node" != "$(hostname)" ]
1875 }
1876
1877 remote_mds ()
1878 {
1879     local node
1880     for node in $(mdts_nodes); do
1881         remote_node $node && return 0
1882     done
1883     return 1
1884 }
1885
1886 remote_mds_nodsh()
1887 {
1888     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
1889 }
1890
1891 remote_ost ()
1892 {
1893     local node
1894     for node in $(osts_nodes) ; do
1895         remote_node $node && return 0
1896     done
1897     return 1
1898 }
1899
1900 remote_ost_nodsh()
1901 {
1902     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
1903 }
1904
1905 mdts_nodes () {
1906     local MDSNODES
1907     local NODES_sort
1908     for num in `seq $MDSCOUNT`; do
1909         MDSNODES="$MDSNODES $(facet_host mds$num)"
1910     done
1911     NODES_sort=$(for i in $MDSNODES; do echo $i; done | sort -u)
1912
1913     echo $NODES_sort
1914 }
1915
1916 remote_servers () {
1917     remote_ost && remote_mds
1918 }
1919
1920 osts_nodes () {
1921     local OSTNODES=$(facet_host ost1)
1922     local NODES_sort
1923
1924     for num in `seq $OSTCOUNT`; do
1925         local myOST=$(facet_host ost$num)
1926         OSTNODES="$OSTNODES $myOST"
1927     done
1928     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
1929
1930     echo $NODES_sort
1931 }
1932
1933 nodes_list () {
1934     # FIXME. We need a list of clients
1935     local myNODES=$HOSTNAME
1936     local myNODES_sort
1937
1938     # CLIENTS (if specified) contains the local client
1939     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
1940
1941     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
1942         myNODES="$myNODES $(osts_nodes) $(mdts_nodes)"
1943     fi
1944
1945     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
1946
1947     echo $myNODES_sort
1948 }
1949
1950 remote_nodes_list () {
1951     local rnodes=$(nodes_list)
1952     rnodes=$(echo " $rnodes " | sed -re "s/\s+$HOSTNAME\s+/ /g")
1953     echo $rnodes
1954 }
1955
1956 init_clients_lists () {
1957     # Sanity check: exclude the local client from RCLIENTS
1958     local rclients=$(echo " $RCLIENTS " | sed -re "s/\s+$HOSTNAME\s+/ /g")
1959
1960     # Sanity check: exclude the dup entries
1961     rclients=$(for i in $rclients; do echo $i; done | sort -u)
1962
1963     local clients="$SINGLECLIENT $HOSTNAME $rclients"
1964
1965     # Sanity check: exclude the dup entries from CLIENTS
1966     # for those configs which has SINGLCLIENT set to local client
1967     clients=$(for i in $clients; do echo $i; done | sort -u)
1968
1969     CLIENTS=`comma_list $clients`
1970     local -a remoteclients=($rclients)
1971     for ((i=0; $i<${#remoteclients[@]}; i++)); do
1972             varname=CLIENT$((i + 2))
1973             eval $varname=${remoteclients[i]}
1974     done
1975
1976     CLIENTCOUNT=$((${#remoteclients[@]} + 1))
1977 }
1978
1979 is_patchless ()
1980 {
1981     lctl get_param version | grep -q patchless
1982 }
1983
1984 check_versions () {
1985     [ "$MDSVER" = "$CLIVER" -a "$OSTVER" = "$CLIVER" ]
1986 }
1987
1988 get_node_count() {
1989     local nodes="$@"
1990     echo $nodes | wc -w || true
1991 }
1992
1993 mixed_ost_devs () {
1994     local nodes=$(osts_nodes)
1995     local osscount=$(get_node_count "$nodes")
1996     [ ! "$OSTCOUNT" = "$osscount" ]
1997 }
1998
1999 mixed_mdt_devs () {
2000     local nodes=$(mdts_nodes)
2001     local mdtcount=$(get_node_count "$nodes")
2002     [ ! "$MDSCOUNT" = "$mdtcount" ]
2003 }
2004
2005 generate_machine_file() {
2006     local nodes=${1//,/ }
2007     local machinefile=$2
2008     rm -f $machinefile || error "can't rm $machinefile"
2009     for node in $nodes; do
2010         echo $node >>$machinefile
2011     done
2012 }
2013
2014 get_stripe () {
2015     local file=$1/stripe
2016     touch $file
2017     $LFS getstripe -v $file || error
2018     rm -f $file
2019 }
2020
2021 check_runas_id_ret() {
2022     local myRC=0
2023     local myRUNAS_ID=$1
2024     shift
2025     local myRUNAS=$@
2026     if [ -z "$myRUNAS" ]; then
2027         error_exit "myRUNAS command must be specified for check_runas_id"
2028     fi
2029     if $GSS_KRB5; then
2030         $myRUNAS krb5_login.sh || \
2031             error "Failed to refresh Kerberos V5 TGT for UID $myRUNAS_ID."
2032     fi
2033     mkdir $DIR/d0_runas_test
2034     chmod 0755 $DIR
2035     chown $myRUNAS_ID:$myRUNAS_ID $DIR/d0_runas_test
2036     $myRUNAS touch $DIR/d0_runas_test/f$$ || myRC=$?
2037     rm -rf $DIR/d0_runas_test
2038     return $myRC
2039 }
2040
2041 check_runas_id() {
2042     local myRUNAS_ID=$1
2043     shift
2044     local myRUNAS=$@
2045     check_runas_id_ret $myRUNAS_ID $myRUNAS || \
2046         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_ID.
2047         Please set RUNAS_ID to some UID which exists on MDS and client or
2048         add user $myRUNAS_ID:$myRUNAS_ID on these nodes."
2049 }
2050
2051 # Run multiop in the background, but wait for it to print
2052 # "PAUSING" to its stdout before returning from this function.
2053 multiop_bg_pause() {
2054     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
2055     FILE=$1
2056     ARGS=$2
2057
2058     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
2059     mkfifo $TMPPIPE
2060
2061     echo "$MULTIOP_PROG $FILE v$ARGS"
2062     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
2063
2064     echo "TMPPIPE=${TMPPIPE}"
2065     read -t 60 multiop_output < $TMPPIPE
2066     if [ $? -ne 0 ]; then
2067         rm -f $TMPPIPE
2068         return 1
2069     fi
2070     rm -f $TMPPIPE
2071     if [ "$multiop_output" != "PAUSING" ]; then
2072         echo "Incorrect multiop output: $multiop_output"
2073         kill -9 $PID
2074         return 1
2075     fi
2076
2077     return 0
2078 }
2079
2080 check_rate() {
2081     local OP=$1
2082     local TARGET_RATE=$2
2083     local NUM_CLIENTS=$3
2084     local LOG=$4
2085
2086     local RATE=$(awk '/^Rate: [0-9\.]+ '"${OP}"'s\/sec/ { print $2}' ${LOG})
2087
2088     # We need to use bc since the rate is a floating point number
2089     local RES=$(echo "${RATE} < ${TARGET_RATE}" | bc -l )
2090     if [ ${RES} -eq 0 ]; then
2091         echo "Success: ${RATE} ${OP}s/sec met target rate" \
2092              "${TARGET_RATE} ${OP}s/sec for ${NUM_CLIENTS} client(s)."
2093         return 0
2094     else
2095         echo "Failure: ${RATE} ${OP}s/sec did not meet target rate" \
2096              "${TARGET_RATE} ${OP}s/sec for ${NUM_CLIENTS} client(s)."
2097         return 1
2098     fi
2099 }
2100
2101 # reset llite stat counters
2102 clear_llite_stats(){
2103         lctl set_param -n llite.*.stats 0
2104 }
2105
2106 # sum llite stat items
2107 calc_llite_stats() {
2108         local res=$(lctl get_param -n llite.*.stats |
2109                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
2110         echo $res
2111 }
2112
2113 # reset osc stat counters
2114 clear_osc_stats(){
2115         lctl set_param -n osc.*.osc_stats 0
2116 }
2117
2118 # sum osc stat items
2119 calc_osc_stats() {
2120         local res=$(lctl get_param -n osc.*.osc_stats |
2121                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
2122         echo $res
2123 }
2124
2125 calc_sum () {
2126         awk 'BEGIN {s = 0}; {s += $1}; END {print s}'
2127 }
2128
2129 calc_osc_kbytes () {
2130         $LCTL get_param -n osc.*[oO][sS][cC][-_][0-9a-f]*.$1 | calc_sum
2131 }
2132
2133 # save_lustre_params(node, parameter_mask)
2134 # generate a stream of formatted strings (<node> <param name>=<param value>)
2135 save_lustre_params() {
2136         local s
2137         do_node $1 "lctl get_param $2" | while read s; do echo "$1 $s"; done
2138 }
2139
2140 # restore lustre parameters from input stream, produces by save_lustre_params
2141 restore_lustre_params() {
2142         local node
2143         local name
2144         local val
2145         while IFS=" =" read node name val; do
2146                 do_node $node "lctl set_param -n $name $val"
2147         done
2148 }
2149
2150 check_catastrophe () {
2151     local rnodes=$(comma_list $(remote_nodes_list))
2152
2153     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && return 1
2154     if [ $rnodes ]; then
2155         do_nodes $rnodes "[ -f $CATASTROPHE ] && { [ \`cat $CATASTROPHE\` -eq 0 ] || false; } || true"
2156     fi 
2157 }
2158
2159 # $1 node
2160 # $2 file
2161 get_stripe_info() {
2162         local tmp_file
2163
2164         stripe_size=0
2165         stripe_count=0
2166         stripe_index=0
2167         tmp_file=$(mktemp)
2168
2169         do_facet $1 lfs getstripe -v $2 > $tmp_file
2170
2171         stripe_size=`awk '$1 ~ /size/ {print $2}' $tmp_file`
2172         stripe_count=`awk '$1 ~ /count/ {print $2}' $tmp_file`
2173         stripe_index=`awk '/obdidx/ {start = 1; getline; print $1; exit}' $tmp_file`
2174         rm -f $tmp_file
2175 }