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