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