Whamcloud - gitweb
b=17477
[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     mgshost=$(echo $mgshost | awk -F: '{print $1}')
1303     if [ "$mgshost" != "$mgs_HOST" ]; then
1304         FAIL_ON_ERROR=true \
1305             error "Bad config file: lustre is mounted with mgs $mgshost, but mgs_HOST=$mgs_HOST
1306                    Please use correct config or set mds_HOST correctly!"
1307     fi
1308 }
1309
1310 check_and_setup_lustre() {
1311     local MOUNTED=$(mounted_lustre_filesystems)
1312     if [ -z "$MOUNTED" ] || ! $(echo $MOUNTED | grep -w -q $MOUNT); then
1313         [ "$REFORMAT" ] && formatall
1314         setupall
1315         MOUNTED=$(mounted_lustre_filesystems | head -1)
1316         [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
1317         export I_MOUNTED=yes
1318     else
1319         check_config $MOUNT
1320         init_facets_vars
1321     fi
1322     if [ "$ONLY" == "setup" ]; then
1323         exit 0
1324     fi
1325 }
1326
1327 cleanup_and_setup_lustre() {
1328     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
1329         lctl set_param debug=0 || true
1330         cleanupall
1331         if [ "$ONLY" == "cleanup" ]; then
1332             exit 0
1333         fi
1334     fi
1335     check_and_setup_lustre
1336 }
1337
1338 check_and_cleanup_lustre() {
1339     if [ "`mount | grep $MOUNT`" ]; then
1340         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]*
1341     fi
1342     if [ "$I_MOUNTED" = "yes" ]; then
1343         cleanupall -f || error "cleanup failed"
1344     fi
1345     unset I_MOUNTED
1346 }
1347
1348 #######
1349 # General functions
1350
1351 check_network() {
1352     local NETWORK=0
1353     local WAIT=0
1354     local MAX=$2
1355     while [ $NETWORK -eq 0 ]; do
1356         ping -c 1 -w 3 $1 > /dev/null
1357         if [ $? -eq 0 ]; then
1358             NETWORK=1
1359         else
1360             WAIT=$((WAIT + 5))
1361             echo "waiting for $1, $((MAX - WAIT)) secs left"
1362             sleep 5
1363         fi
1364         if [ $WAIT -gt $MAX ]; then
1365             echo "Network not available"
1366             exit 1
1367         fi
1368     done
1369 }
1370 check_port() {
1371     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
1372         sleep 9
1373     done
1374 }
1375
1376 no_dsh() {
1377     shift
1378     eval $@
1379 }
1380
1381 comma_list() {
1382     # the sed converts spaces to commas, but leaves the last space
1383     # alone, so the line doesn't end with a comma.
1384     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
1385 }
1386
1387 absolute_path() {
1388     (cd `dirname $1`; echo $PWD/`basename $1`)
1389 }
1390
1391 ##################################
1392 # Adaptive Timeouts funcs
1393
1394 at_is_valid() {
1395     if [ -z "$AT_MAX_PATH" ]; then
1396         AT_MAX_PATH=$(do_facet $SINGLEMDS "find /sys/ -name at_max")
1397         [ -z "$AT_MAX_PATH" ] && echo "missing /sys/.../at_max " && return 1
1398     fi
1399     return 0
1400 }
1401
1402 at_is_enabled() {
1403     at_is_valid || error "invalid call"
1404
1405     # only check mds, we assume at_max is the same on all nodes
1406     local at_max=$(do_facet $SINGLEMDS "cat $AT_MAX_PATH")
1407     if [ $at_max -eq 0 ]; then
1408         return 1
1409     else
1410         return 0
1411     fi
1412 }
1413
1414 at_max_get() {
1415     local facet=$1
1416
1417     at_is_valid || error "invalid call"
1418
1419     # suppose that all ost-s has the same at_max set
1420     if [ $facet == "ost" ]; then
1421         do_facet ost1 "cat $AT_MAX_PATH"
1422     else
1423         do_facet $facet "cat $AT_MAX_PATH"
1424     fi
1425 }
1426
1427 at_max_set() {
1428     local at_max=$1
1429     shift
1430
1431     at_is_valid || error "invalid call"
1432
1433     local facet
1434     for facet in $@; do
1435         if [ $facet == "ost" ]; then
1436             for i in `seq $OSTCOUNT`; do
1437                 do_facet ost$i "echo $at_max > $AT_MAX_PATH"
1438             done
1439         elif [ $facet == "mds" ]; then
1440             for i in `seq $MDSCOUNT`; do
1441                 do_facet mds$i "echo $at_max > $AT_MAX_PATH"
1442             done
1443         else
1444             do_facet $facet "echo $at_max > $AT_MAX_PATH"
1445         fi
1446     done
1447 }
1448
1449 ##################################
1450 # OBD_FAIL funcs
1451
1452 drop_request() {
1453 # OBD_FAIL_MDS_ALL_REQUEST_NET
1454     RC=0
1455     do_facet $SINGLEMDS lctl set_param fail_loc=0x123
1456     do_facet client "$1" || RC=$?
1457     do_facet $SINGLEMDS lctl set_param fail_loc=0
1458     return $RC
1459 }
1460
1461 drop_reply() {
1462 # OBD_FAIL_MDS_ALL_REPLY_NET
1463     RC=0
1464     do_facet $SINGLEMDS lctl set_param fail_loc=0x122
1465     do_facet client "$@" || RC=$?
1466     do_facet $SINGLEMDS lctl set_param fail_loc=0
1467     return $RC
1468 }
1469
1470 drop_reint_reply() {
1471 # OBD_FAIL_MDS_REINT_NET_REP
1472     RC=0
1473     do_facet $SINGLEMDS lctl set_param fail_loc=0x119
1474     do_facet client "$@" || RC=$?
1475     do_facet $SINGLEMDS lctl set_param fail_loc=0
1476     return $RC
1477 }
1478
1479 pause_bulk() {
1480 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
1481     RC=0
1482     do_facet ost1 lctl set_param fail_loc=0x214
1483     do_facet client "$1" || RC=$?
1484     do_facet client "sync"
1485     do_facet ost1 lctl set_param fail_loc=0
1486     return $RC
1487 }
1488
1489 drop_ldlm_cancel() {
1490 #define OBD_FAIL_LDLM_CANCEL             0x304
1491     RC=0
1492     do_facet client lctl set_param fail_loc=0x304
1493     do_facet client "$@" || RC=$?
1494     do_facet client lctl set_param fail_loc=0
1495     return $RC
1496 }
1497
1498 drop_bl_callback() {
1499 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
1500     RC=0
1501     do_facet client lctl set_param fail_loc=0x305
1502     do_facet client "$@" || RC=$?
1503     do_facet client lctl set_param fail_loc=0
1504     return $RC
1505 }
1506
1507 drop_ldlm_reply() {
1508 #define OBD_FAIL_LDLM_REPLY              0x30c
1509     RC=0
1510     do_facet $SINGLEMDS lctl set_param fail_loc=0x30c
1511     do_facet client "$@" || RC=$?
1512     do_facet $SINGLEMDS lctl set_param fail_loc=0
1513     return $RC
1514 }
1515
1516 clear_failloc() {
1517     facet=$1
1518     pause=$2
1519     sleep $pause
1520     echo "clearing fail_loc on $facet"
1521     do_facet $facet "lctl set_param fail_loc=0 2>/dev/null || true"
1522 }
1523
1524 set_nodes_failloc () {
1525     local nodes=$1
1526     local node
1527
1528     for node in $nodes ; do
1529         do_node $node lctl set_param fail_loc=$2
1530     done
1531 }
1532
1533 cancel_lru_locks() {
1534     $LCTL mark "cancel_lru_locks $1 start"
1535     for d in `lctl get_param -N ldlm.namespaces.*.lru_size | egrep -i $1`; do
1536         $LCTL set_param -n $d=clear
1537     done
1538     $LCTL get_param ldlm.namespaces.*.lock_unused_count | egrep -i $1 | grep -v '=0'
1539     $LCTL mark "cancel_lru_locks $1 stop"
1540 }
1541
1542 default_lru_size()
1543 {
1544         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
1545         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
1546         echo "$DEFAULT_LRU_SIZE"
1547 }
1548
1549 lru_resize_enable()
1550 {
1551     lctl set_param ldlm.namespaces.*$1*.lru_size=0
1552 }
1553
1554 lru_resize_disable()
1555 {
1556     lctl set_param ldlm.namespaces.*$1*.lru_size $(default_lru_size)
1557 }
1558
1559 pgcache_empty() {
1560     local FILE
1561     for FILE in `lctl get_param -N "llite.*.dump_page_cache"`; do
1562         if [ `lctl get_param -n $FILE | wc -l` -gt 1 ]; then
1563             echo there is still data in page cache $FILE ?
1564             lctl get_param -n $FILE
1565             return 1
1566         fi
1567     done
1568     return 0
1569 }
1570
1571 debugsave() {
1572     DEBUGSAVE="$(lctl get_param -n debug)"
1573 }
1574
1575 debugrestore() {
1576     [ -n "$DEBUGSAVE" ] && lctl set_param debug="${DEBUGSAVE}"
1577     DEBUGSAVE=""
1578 }
1579
1580 ##################################
1581 # Test interface
1582 ##################################
1583
1584 error_noexit() {
1585     local TYPE=${TYPE:-"FAIL"}
1586     local ERRLOG
1587     lctl set_param fail_loc=0 2>/dev/null || true
1588     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
1589     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
1590     echo "Dumping lctl log to $ERRLOG"
1591     # We need to dump the logs on all nodes
1592     local NODES=$(nodes_list)
1593     for NODE in $NODES; do
1594         do_node $NODE $LCTL dk $ERRLOG
1595     done
1596     debugrestore
1597     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
1598 }
1599
1600 error() {
1601     error_noexit "$@"
1602     $FAIL_ON_ERROR && exit 1 || true
1603 }
1604
1605 error_exit() {
1606     error_noexit "$@"
1607     exit 1
1608 }
1609
1610 # use only if we are ignoring failures for this test, bugno required.
1611 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
1612 # e.g. error_ignore 5494 "your message"
1613 error_ignore() {
1614     local TYPE="IGNORE (bz$1)"
1615     shift
1616     error_noexit "$@"
1617 }
1618
1619 skip () {
1620         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
1621         [ "$TESTSUITELOG" ] && \
1622                 echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG || true
1623 }
1624
1625 build_test_filter() {
1626     [ "$ONLY" ] && log "only running test `echo $ONLY`"
1627     for O in $ONLY; do
1628         eval ONLY_${O}=true
1629     done
1630     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
1631         log "excepting tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
1632     [ "$EXCEPT_SLOW" ] && \
1633         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
1634     for E in $EXCEPT $ALWAYS_EXCEPT; do
1635         eval EXCEPT_${E}=true
1636     done
1637     for E in $EXCEPT_SLOW; do
1638         eval EXCEPT_SLOW_${E}=true
1639     done
1640     for G in $GRANT_CHECK_LIST; do
1641         eval GCHECK_ONLY_${G}=true
1642         done
1643 }
1644
1645 _basetest() {
1646     echo $*
1647 }
1648
1649 basetest() {
1650     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
1651 }
1652
1653 # print a newline if the last test was skipped
1654 export LAST_SKIPPED=
1655 run_test() {
1656     assert_DIR
1657
1658     export base=`basetest $1`
1659     if [ ! -z "$ONLY" ]; then
1660         testname=ONLY_$1
1661         if [ ${!testname}x != x ]; then
1662             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
1663             run_one $1 "$2"
1664             return $?
1665         fi
1666         testname=ONLY_$base
1667         if [ ${!testname}x != x ]; then
1668             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
1669             run_one $1 "$2"
1670             return $?
1671         fi
1672         LAST_SKIPPED="y"
1673         echo -n "."
1674         return 0
1675     fi
1676     testname=EXCEPT_$1
1677     if [ ${!testname}x != x ]; then
1678         LAST_SKIPPED="y"
1679         TESTNAME=test_$1 skip "skipping excluded test $1"
1680         return 0
1681     fi
1682     testname=EXCEPT_$base
1683     if [ ${!testname}x != x ]; then
1684         LAST_SKIPPED="y"
1685         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
1686         return 0
1687     fi
1688     testname=EXCEPT_SLOW_$1
1689     if [ ${!testname}x != x ]; then
1690         LAST_SKIPPED="y"
1691         TESTNAME=test_$1 skip "skipping SLOW test $1"
1692         return 0
1693     fi
1694     testname=EXCEPT_SLOW_$base
1695     if [ ${!testname}x != x ]; then
1696         LAST_SKIPPED="y"
1697         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
1698         return 0
1699     fi
1700
1701     LAST_SKIPPED=
1702     run_one $1 "$2"
1703
1704     return $?
1705 }
1706
1707 EQUALS="======================================================================"
1708 equals_msg() {
1709     msg="$@"
1710
1711     local suffixlen=$((${#EQUALS} - ${#msg}))
1712     [ $suffixlen -lt 5 ] && suffixlen=5
1713     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
1714 }
1715
1716 log() {
1717     echo "$*"
1718     lsmod | grep lnet > /dev/null || load_modules
1719
1720     local MSG="$*"
1721     # Get rif of '
1722     MSG=${MSG//\'/\\\'}
1723     MSG=${MSG//\(/\\\(}
1724     MSG=${MSG//\)/\\\)}
1725     MSG=${MSG//\;/\\\;}
1726     MSG=${MSG//\|/\\\|}
1727     MSG=${MSG//\>/\\\>}
1728     MSG=${MSG//\</\\\<}
1729     MSG=${MSG//\//\\\/}
1730     local NODES=$(nodes_list)
1731     for NODE in $NODES; do
1732         do_node $NODE $LCTL mark "$MSG" 2> /dev/null || true
1733     done
1734 }
1735
1736 trace() {
1737         log "STARTING: $*"
1738         strace -o $TMP/$1.strace -ttt $*
1739         RC=$?
1740         log "FINISHED: $*: rc $RC"
1741         return 1
1742 }
1743
1744 pass() {
1745     echo PASS $@
1746 }
1747
1748 check_mds() {
1749     FFREE=$(do_node $SINGLEMDS lctl get_param -n osd.*MDT*.filesfree | awk 'BEGIN{avail=0}; {avail+=$1}; END{print avail}')
1750     FTOTAL=$(do_node $SINGLEMDS lctl get_param -n osd.*MDT*.filestotal | awk 'BEGIN{avail=0}; {avail+=$1}; END{print avail}')
1751     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
1752 }
1753
1754 reset_fail_loc () {
1755     local myNODES=$(nodes_list)
1756     local NODE
1757
1758     for NODE in $myNODES; do
1759         do_node $NODE "lctl set_param fail_loc=0 2>/dev/null || true"
1760     done
1761 }
1762
1763 run_one() {
1764     testnum=$1
1765     message=$2
1766     tfile=f${testnum}
1767     export tdir=d0.${TESTSUITE}/d${base}
1768     local SAVE_UMASK=`umask`
1769     umask 0022
1770
1771     BEFORE=`date +%s`
1772     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
1773     #check_mds
1774     export TESTNAME=test_$testnum
1775     test_${testnum} || error "test_$testnum failed with $?"
1776     #check_mds
1777     cd $SAVE_PWD
1778     reset_fail_loc
1779     check_grant ${testnum} || error "check_grant $testnum failed with $?"
1780     check_catastrophe || error "LBUG/LASSERT detected"
1781     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
1782     pass "($((`date +%s` - $BEFORE))s)"
1783     unset TESTNAME
1784     unset tdir
1785     umask $SAVE_UMASK
1786 }
1787
1788 canonical_path() {
1789     (cd `dirname $1`; echo $PWD/`basename $1`)
1790 }
1791
1792 sync_clients() {
1793     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync
1794     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync
1795         cd $SAVE_PWD
1796 }
1797
1798 check_grant() {
1799     export base=`basetest $1`
1800     [ "$CHECK_GRANT" == "no" ] && return 0
1801
1802         testname=GCHECK_ONLY_${base}
1803         [ ${!testname}x == x ] && return 0
1804
1805     echo -n "checking grant......"
1806         cd $SAVE_PWD
1807         # write some data to sync client lost_grant
1808         rm -f $DIR1/${tfile}_check_grant_* 2>&1
1809         for i in `seq $OSTCOUNT`; do
1810                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
1811                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
1812                                               count=1 > /dev/null 2>&1
1813         done
1814         # sync all the data and make sure no pending data on server
1815         sync_clients
1816         
1817         #get client grant and server grant
1818         client_grant=0
1819     for d in `lctl get_param -n osc.*.cur_grant_bytes`; do
1820                 client_grant=$((client_grant + $d))
1821         done
1822         server_grant=0
1823         for d in `lctl get_param -n obdfilter.*.tot_granted`; do
1824                 server_grant=$((server_grant + $d))
1825         done
1826
1827         # cleanup the check_grant file
1828         for i in `seq $OSTCOUNT`; do
1829                 rm $DIR1/${tfile}_check_grant_$i
1830         done
1831
1832         #check whether client grant == server grant
1833         if [ $client_grant != $server_grant ]; then
1834                 echo "failed: client:${client_grant} server: ${server_grant}"
1835                 return 1
1836         else
1837                 echo "pass"
1838         fi
1839 }
1840
1841 ########################
1842 # helper functions
1843
1844 osc_to_ost()
1845 {
1846     osc=$1
1847     ost=`echo $1 | awk -F_ '{print $3}'`
1848     if [ -z $ost ]; then
1849         ost=`echo $1 | sed 's/-osc.*//'`
1850     fi
1851     echo $ost
1852 }
1853
1854 remote_node () {
1855     local node=$1
1856     [ "$node" != "$(hostname)" ]
1857 }
1858
1859 remote_mds ()
1860 {
1861     local node
1862     for node in $(mdts_nodes); do
1863         remote_node $node && return 0
1864     done
1865     return 1
1866 }
1867
1868 remote_mds_nodsh()
1869 {
1870     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
1871 }
1872
1873 remote_ost ()
1874 {
1875     local node
1876     for node in $(osts_nodes) ; do
1877         remote_node $node && return 0
1878     done
1879     return 1
1880 }
1881
1882 remote_ost_nodsh()
1883 {
1884     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
1885 }
1886
1887 mdts_nodes () {
1888     local MDSNODES
1889     local NODES_sort
1890     for num in `seq $MDSCOUNT`; do
1891         MDSNODES="$MDSNODES $(facet_host mds$num)"
1892     done
1893     NODES_sort=$(for i in $MDSNODES; do echo $i; done | sort -u)
1894
1895     echo $NODES_sort
1896 }
1897
1898 remote_servers () {
1899     remote_ost && remote_mds
1900 }
1901
1902 osts_nodes () {
1903     local OSTNODES=$(facet_host ost1)
1904     local NODES_sort
1905
1906     for num in `seq $OSTCOUNT`; do
1907         local myOST=$(facet_host ost$num)
1908         OSTNODES="$OSTNODES $myOST"
1909     done
1910     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
1911
1912     echo $NODES_sort
1913 }
1914
1915 nodes_list () {
1916     # FIXME. We need a list of clients
1917     local myNODES=$HOSTNAME
1918     local myNODES_sort
1919
1920     # CLIENTS (if specified) contains the local client
1921     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
1922
1923     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
1924         myNODES="$myNODES $(osts_nodes) $(mdts_nodes)"
1925     fi
1926
1927     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
1928
1929     echo $myNODES_sort
1930 }
1931
1932 remote_nodes_list () {
1933     local rnodes=$(nodes_list)
1934     rnodes=$(echo " $rnodes " | sed -re "s/\s+$HOSTNAME\s+/ /g")
1935     echo $rnodes
1936 }
1937
1938 init_clients_lists () {
1939     # Sanity check: exclude the local client from RCLIENTS
1940     local rclients=$(echo " $RCLIENTS " | sed -re "s/\s+$HOSTNAME\s+/ /g")
1941
1942     # Sanity check: exclude the dup entries
1943     rclients=$(for i in $rclients; do echo $i; done | sort -u)
1944
1945     local clients="$SINGLECLIENT $HOSTNAME $rclients"
1946
1947     # Sanity check: exclude the dup entries from CLIENTS
1948     # for those configs which has SINGLCLIENT set to local client
1949     clients=$(for i in $clients; do echo $i; done | sort -u)
1950
1951     CLIENTS=`comma_list $clients`
1952     local -a remoteclients=($rclients)
1953     for ((i=0; $i<${#remoteclients[@]}; i++)); do
1954             varname=CLIENT$((i + 2))
1955             eval $varname=${remoteclients[i]}
1956     done
1957
1958     CLIENTCOUNT=$((${#remoteclients[@]} + 1))
1959 }
1960
1961 is_patchless ()
1962 {
1963     lctl get_param version | grep -q patchless
1964 }
1965
1966 get_node_count() {
1967    local nodes="$@"
1968    echo $nodes | wc -w || true
1969 }
1970
1971 mixed_ost_devs () {
1972     local nodes=$(osts_nodes)
1973     local osscount=$(get_node_count "$nodes")
1974     [ ! "$OSTCOUNT" = "$osscount" ]
1975 }
1976
1977 mixed_mdt_devs () {
1978     local nodes=$(mdts_nodes)
1979     local mdtcount=$(get_node_count "$nodes")
1980     [ ! "$MDSCOUNT" = "$mdtcount" ]
1981 }
1982
1983 generate_machine_file() {
1984     local nodes=${1//,/ }
1985     local machinefile=$2
1986     rm -f $machinefile || error "can't rm $machinefile"
1987     for node in $nodes; do
1988         echo $node >>$machinefile
1989     done
1990 }
1991
1992 get_stripe () {
1993     local file=$1/stripe
1994     touch $file
1995     $LFS getstripe -v $file || error
1996     rm -f $file
1997 }
1998
1999 check_runas_id_ret() {
2000     local myRC=0
2001     local myRUNAS_ID=$1
2002     shift
2003     local myRUNAS=$@
2004     if [ -z "$myRUNAS" ]; then
2005         error_exit "myRUNAS command must be specified for check_runas_id"
2006     fi
2007     if $GSS_KRB5; then
2008         $myRUNAS krb5_login.sh || \
2009             error "Failed to refresh Kerberos V5 TGT for UID $myRUNAS_ID."
2010     fi
2011     mkdir $DIR/d0_runas_test
2012     chmod 0755 $DIR
2013     chown $myRUNAS_ID:$myRUNAS_ID $DIR/d0_runas_test
2014     $myRUNAS touch $DIR/d0_runas_test/f$$ || myRC=$?
2015     rm -rf $DIR/d0_runas_test
2016     return $myRC
2017 }
2018
2019 check_runas_id() {
2020     local myRUNAS_ID=$1
2021     shift
2022     local myRUNAS=$@
2023     check_runas_id_ret $myRUNAS_ID $myRUNAS || \
2024         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_ID.
2025         Please set RUNAS_ID to some UID which exists on MDS and client or
2026         add user $myRUNAS_ID:$myRUNAS_ID on these nodes."
2027 }
2028
2029 # Run multiop in the background, but wait for it to print
2030 # "PAUSING" to its stdout before returning from this function.
2031 multiop_bg_pause() {
2032     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
2033     FILE=$1
2034     ARGS=$2
2035
2036     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
2037     mkfifo $TMPPIPE
2038
2039     echo "$MULTIOP_PROG $FILE v$ARGS"
2040     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
2041
2042     echo "TMPPIPE=${TMPPIPE}"
2043     read -t 60 multiop_output < $TMPPIPE
2044     if [ $? -ne 0 ]; then
2045         rm -f $TMPPIPE
2046         return 1
2047     fi
2048     rm -f $TMPPIPE
2049     if [ "$multiop_output" != "PAUSING" ]; then
2050         echo "Incorrect multiop output: $multiop_output"
2051         kill -9 $PID
2052         return 1
2053     fi
2054
2055     return 0
2056 }
2057
2058 check_rate() {
2059     local OP=$1
2060     local TARGET_RATE=$2
2061     local NUM_CLIENTS=$3
2062     local LOG=$4
2063
2064     local RATE=$(awk '/^Rate: [0-9\.]+ '"${OP}"'s\/sec/ { print $2}' ${LOG})
2065
2066     # We need to use bc since the rate is a floating point number
2067     local RES=$(echo "${RATE} < ${TARGET_RATE}" | bc -l )
2068     if [ ${RES} -eq 0 ]; then
2069         echo "Success: ${RATE} ${OP}s/sec met target rate" \
2070              "${TARGET_RATE} ${OP}s/sec for ${NUM_CLIENTS} client(s)."
2071         return 0
2072     else
2073         echo "Failure: ${RATE} ${OP}s/sec did not meet target rate" \
2074              "${TARGET_RATE} ${OP}s/sec for ${NUM_CLIENTS} client(s)."
2075         return 1
2076     fi
2077 }
2078
2079 # reset llite stat counters
2080 clear_llite_stats(){
2081         lctl set_param -n llite.*.stats 0
2082 }
2083
2084 # sum llite stat items
2085 calc_llite_stats() {
2086         local res=$(lctl get_param -n llite.*.stats |
2087                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
2088         echo $res
2089 }
2090
2091 # reset osc stat counters
2092 clear_osc_stats(){
2093         lctl set_param -n osc.*.osc_stats 0
2094 }
2095
2096 # sum osc stat items
2097 calc_osc_stats() {
2098         local res=$(lctl get_param -n osc.*.osc_stats |
2099                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
2100         echo $res
2101 }
2102
2103 calc_sum () {
2104         awk 'BEGIN {s = 0}; {s += $1}; END {print s}'
2105 }
2106
2107 calc_osc_kbytes () {
2108         $LCTL get_param -n osc.*[oO][sS][cC][-_][0-9a-f]*.$1 | calc_sum
2109 }
2110
2111 # save_lustre_params(node, parameter_mask)
2112 # generate a stream of formatted strings (<node> <param name>=<param value>)
2113 save_lustre_params() {
2114         local s
2115         do_node $1 "lctl get_param $2" | while read s; do echo "$1 $s"; done
2116 }
2117
2118 # restore lustre parameters from input stream, produces by save_lustre_params
2119 restore_lustre_params() {
2120         local node
2121         local name
2122         local val
2123         while IFS=" =" read node name val; do
2124                 do_node $node "lctl set_param -n $name $val"
2125         done
2126 }
2127
2128 check_catastrophe () {
2129     local rnodes=$(comma_list $(remote_nodes_list))
2130
2131     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && return 1
2132     if [ $rnodes ]; then
2133         do_nodes $rnodes "[ -f $CATASTROPHE ] && { [ \`cat $CATASTROPHE\` -eq 0 ] || false; } || true"
2134     fi 
2135 }
2136