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