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