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