Whamcloud - gitweb
b=13572
[fs/lustre-release.git] / lustre / tests / test-framework.sh
1 #!/bin/bash
2 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
3
4 trap 'echo "test-framework exiting on error"' ERR
5 set -e
6 #set -x
7
8
9 export REFORMAT=${REFORMAT:-""}
10 export VERBOSE=false
11 export GMNALNID=${GMNALNID:-/usr/sbin/gmlndnid}
12 export CATASTROPHE=${CATASTROPHE:-/proc/sys/lnet/catastrophe}
13 export GSS=false
14 export GSS_KRB5=false
15 export GSS_PIPEFS=false
16 #export PDSH="pdsh -S -Rssh -w"
17
18 # eg, assert_env LUSTRE MDSNODES OSTNODES CLIENTS
19 assert_env() {
20     local failed=""
21     for name in $@; do
22         if [ -z "${!name}" ]; then
23             echo "$0: $name must be set"
24             failed=1
25         fi
26     done
27     [ $failed ] && exit 1 || true
28 }
29
30 usage() {
31     echo "usage: $0 [-r] [-f cfgfile]"
32     echo "       -r: reformat"
33
34     exit
35 }
36
37 init_test_env() {
38     export LUSTRE=`absolute_path $LUSTRE`
39     export TESTSUITE=`basename $0 .sh`
40     export LTESTDIR=${LTESTDIR:-$LUSTRE/../ltest}
41
42     [ -d /r ] && export ROOT=${ROOT:-/r}
43     export TMP=${TMP:-$ROOT/tmp}
44     export TESTSUITELOG=${TMP}/${TESTSUITE}.log
45
46     export PATH=:$PATH:$LUSTRE/utils:$LUSTRE/utils/gss:$LUSTRE/tests
47     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
48     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
49     [ ! -f "$LCTL" ] && export LCTL=$(which lctl) 
50     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
51     [ ! -f "$LFS" ] && export LFS=$(which lfs) 
52     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
53     [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre) 
54     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
55     [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre) 
56     export CHECKSTAT="${CHECKSTAT:-"checkstat -v"} "
57     export FSYTPE=${FSTYPE:-"ldiskfs"}
58     export NAME=${NAME:-local}
59     export LPROC=/proc/fs/lustre
60     export LGSSD=${LGSSD:-"$LUSTRE/utils/gss/lgssd"}
61     export LSVCGSSD=${LSVCGSSD:-"$LUSTRE/utils/gss/lsvcgssd"}
62     export KRB5DIR=${KRB5DIR:-"/usr/kerberos"}
63     export DIR2
64
65     if [ "$ACCEPTOR_PORT" ]; then
66         export PORT_OPT="--port $ACCEPTOR_PORT"
67     fi
68
69     case "x$SEC" in
70         xkrb5*)
71             echo "Using GSS/krb5 ptlrpc security flavor"
72             GSS=true
73             GSS_KRB5=true
74             ;;
75     esac
76
77     # Paths on remote nodes, if different 
78     export RLUSTRE=${RLUSTRE:-$LUSTRE}
79     export RPWD=${RPWD:-$PWD}
80     export I_MOUNTED=${I_MOUNTED:-"no"}
81
82     # command line
83     
84     while getopts "rvf:" opt $*; do 
85         case $opt in
86             f) CONFIG=$OPTARG;;
87             r) REFORMAT=--reformat;;
88             v) VERBOSE=true;;
89             \?) usage;;
90         esac
91     done
92
93     shift $((OPTIND - 1))
94     ONLY=${ONLY:-$*}
95
96     [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
97
98 }
99
100 load_module() {
101     EXT=".ko"
102     module=$1
103     shift
104     BASE=`basename $module $EXT`
105     lsmod | grep -q ${BASE} || \
106       if [ -f ${LUSTRE}/${module}${EXT} ]; then
107         insmod ${LUSTRE}/${module}${EXT} $@
108     else
109         # must be testing a "make install" or "rpm" installation
110         # note failed to load ptlrpc_gss is considered not fatal
111         if [ "$BASE" == "ptlrpc_gss" ]; then
112             modprobe $BASE $@ 2>/dev/null || echo "gss/krb5 is not supported"
113         else
114             modprobe $BASE $@
115         fi
116     fi
117 }
118
119 load_modules() {
120     if [ -n "$MODPROBE" ]; then
121         # use modprobe
122     return 0
123     fi
124     if [ "$HAVE_MODULES" = true ]; then
125     # we already loaded
126         return 0
127     fi
128     HAVE_MODULES=true
129
130     echo Loading modules from $LUSTRE
131     load_module ../lnet/libcfs/libcfs
132     [ -z "$LNETOPTS" ] && \
133         LNETOPTS=$(awk '/^options lnet/ { print $0}' /etc/modprobe.conf | sed 's/^options lnet //g')
134     echo "lnet options: '$LNETOPTS'"
135     # note that insmod will ignore anything in modprobe.conf
136     load_module ../lnet/lnet/lnet $LNETOPTS
137     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
138     load_module ../lnet/klnds/$LNETLND
139     [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
140     load_module lvfs/lvfs
141     load_module obdclass/obdclass
142     load_module lvfs/fsfilt_$FSTYPE
143     load_module ptlrpc/ptlrpc
144     load_module ptlrpc/gss/ptlrpc_gss
145     # Now, some modules depend on lquota without USE_QUOTA check,
146     # will fix later. Disable check "$USE_QUOTA" = "yes" temporary.
147     #[ "$USE_QUOTA" = "yes" ] && load_module quota/lquota
148     load_module quota/lquota
149     load_module fid/fid
150     load_module fld/fld
151     load_module lmv/lmv
152     load_module mdc/mdc
153     load_module osc/osc
154     load_module lov/lov
155     load_module mds/mds
156     load_module mdd/mdd
157     load_module mdt/mdt
158     load_module cmm/cmm
159     load_module osd/osd
160     load_module ost/ost
161     load_module obdfilter/obdfilter
162     load_module llite/lustre
163     load_module llite/llite_lloop
164     load_module mgc/mgc
165     load_module mgs/mgs
166     rm -f $TMP/ogdb-`hostname`
167     $LCTL modules > $TMP/ogdb-`hostname`
168     # 'mount' doesn't look in $PATH, just sbin
169     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
170 }
171
172 RMMOD=rmmod
173 if [ `uname -r | cut -c 3` -eq 4 ]; then
174     RMMOD="modprobe -r"
175 fi
176
177 wait_for_lnet() {
178     local UNLOADED=0
179     local WAIT=0
180     local MAX=60
181     MODULES=$($LCTL modules | awk '{ print $2 }')
182     while [ -n "$MODULES" ]; do
183     sleep 5
184     $RMMOD $MODULES > /dev/null 2>&1 || true
185     MODULES=$($LCTL modules | awk '{ print $2 }')
186         if [ -z "$MODULES" ]; then
187         return 0
188         else
189             WAIT=$((WAIT + 5))
190             echo "waiting, $((MAX - WAIT)) secs left"
191         fi
192         if [ $WAIT -eq $MAX ]; then
193             echo "LNET modules $MODULES will not unload"
194         lsmod
195             return 3
196         fi
197     done
198 }
199
200 unload_modules() {
201     lsmod | grep lnet > /dev/null && $LCTL dl && $LCTL dk $TMP/debug
202     local MODULES=$($LCTL modules | awk '{ print $2 }')
203     $RMMOD $MODULES > /dev/null 2>&1 || true
204      # do it again, in case we tried to unload ksocklnd too early
205     MODULES=$($LCTL modules | awk '{ print $2 }')
206     [ -n "$MODULES" ] && $RMMOD $MODULES > /dev/null 2>&1 || true
207     MODULES=$($LCTL modules | awk '{ print $2 }')
208     if [ -n "$MODULES" ]; then
209     echo "Modules still loaded: "
210     echo $MODULES 
211     if [ -e $LPROC ]; then
212         echo "Lustre still loaded"
213         cat $LPROC/devices || true
214         lsmod
215         return 2
216     else
217         echo "Lustre stopped but LNET is still loaded, waiting..."
218         wait_for_lnet || return 3
219     fi
220     fi
221     HAVE_MODULES=false
222
223     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd mem.*leaked" || true)
224     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
225     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
226         echo "$LEAK_LUSTRE" 1>&2
227         echo "$LEAK_PORTALS" 1>&2
228         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
229         echo "Memory leaks detected"
230         [ -n "$IGNORE_LEAK" ] && echo "ignoring leaks" && return 0
231         return 254
232     fi
233     echo "modules unloaded."
234     return 0
235 }
236
237 check_gss_daemon_facet() {
238     facet=$1
239     dname=$2
240
241     num=`do_facet $facet ps -o cmd -C $dname | grep $dname | wc -l`
242     if [ $num -ne 1 ]; then
243         echo "$num instance of $dname on $facet"
244         return 1
245     fi
246     return 0
247 }
248
249 send_sigint() {
250     local facet=$1
251     shift
252     do_facet $facet "killall -2 $@ 2>/dev/null || true"
253 }
254
255 start_gss_daemons() {
256     # starting on MDT
257     for num in `seq $MDSCOUNT`; do
258         do_facet mds$num "$LSVCGSSD -v"
259         if $GSS_PIPEFS; then
260             do_facet mds$num "$LGSSD -v"
261         fi
262     done
263     # starting on OSTs
264     for num in `seq $OSTCOUNT`; do
265         do_facet ost$num "$LSVCGSSD -v"
266     done
267     # starting on client
268     # FIXME: is "client" the right facet name?
269     if $GSS_PIPEFS; then
270         do_facet client "$LGSSD -v"
271     fi
272
273     # wait daemons entering "stable" status
274     sleep 5
275
276     #
277     # check daemons are running
278     #
279     for num in `seq $MDSCOUNT`; do
280         check_gss_daemon_facet mds$num lsvcgssd
281         if $GSS_PIPEFS; then
282             check_gss_daemon_facet mds$num lgssd
283         fi
284     done
285     for num in `seq $OSTCOUNT`; do
286         check_gss_daemon_facet ost$num lsvcgssd
287     done
288     if $GSS_PIPEFS; then
289         check_gss_daemon_facet client lgssd
290     fi
291 }
292
293 stop_gss_daemons() {
294     for num in `seq $MDSCOUNT`; do
295         send_sigint mds$num lsvcgssd lgssd
296     done
297     for num in `seq $OSTCOUNT`; do
298         send_sigint ost$num lsvcgssd
299     done
300     send_sigint client lgssd
301 }
302
303 init_krb5_env() {
304     if [ ! -z $SEC ]; then
305         MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS,sec=$SEC
306         OST_MOUNT_OPTS=$OST_MOUNT_OPTS,sec=$SEC
307     fi
308
309     if $GSS; then
310         start_gss_daemons
311     fi
312 }
313
314 cleanup_krb5_env() {
315     if $GSS; then
316         stop_gss_daemons
317         # maybe cleanup credential cache?
318     fi
319 }
320
321 mdsdevlabel() {
322     local num=$1
323     local device=`mdsdevname $num`
324     local label=`do_facet mds$num "e2label ${device}" | grep -v "CMD: "`
325     echo -n $label
326 }
327
328 ostdevlabel() {
329     local num=$1
330     local device=`ostdevname $num`
331     local label=`do_facet ost$num "e2label ${device}" | grep -v "CMD: "`
332     echo -n $label
333 }
334
335 # Facet functions
336 # start facet device options 
337 start() {
338     facet=$1
339     shift
340     device=$1
341     shift
342     echo "Starting ${facet}: $@ ${device} ${MOUNT%/*}/${facet}"
343     do_facet ${facet} mkdir -p ${MOUNT%/*}/${facet}
344     do_facet ${facet} mount -t lustre $@ ${device} ${MOUNT%/*}/${facet} 
345     RC=${PIPESTATUS[0]}
346     if [ $RC -ne 0 ]; then
347         echo mount -t lustre $@ ${device} ${MOUNT%/*}/${facet} 
348         echo Start of ${device} on ${facet} failed ${RC}
349     else 
350         do_facet ${facet} sync
351         label=$(do_facet ${facet} "e2label ${device}")
352         [ -z "$label" ] && echo no label for ${device} && exit 1
353         eval export ${facet}_svc=${label}
354         eval export ${facet}_dev=${device}
355         eval export ${facet}_opt=\"$@\"
356         echo Started ${label}
357     fi
358     return $RC
359 }
360
361 stop() {
362     local running
363     facet=$1
364     shift
365     HOST=`facet_active_host $facet`
366     [ -z $HOST ] && echo stop: no host for $facet && return 0
367
368     running=$(do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts") || true
369     if [ ${running} -ne 0 ]; then
370         echo "Stopping ${MOUNT%/*}/${facet} (opts:$@)"
371         do_facet ${facet} umount -d $@ ${MOUNT%/*}/${facet}
372     fi
373
374     # umount should block, but we should wait for unrelated obd's
375     # like the MGS or MGC to also stop.
376     local WAIT=0
377     local INTERVAL=1
378     # conf-sanity 31 takes a long time cleanup
379     while [ $WAIT -lt 300 ]; do
380         running=$(do_facet ${facet} "[ -e $LPROC ] && grep ST' ' $LPROC/devices") || true
381         if [ -z "${running}" ]; then
382                 return 0
383         fi
384         echo "waited $WAIT for${running}"
385         if [ $INTERVAL -lt 64 ]; then 
386             INTERVAL=$((INTERVAL + INTERVAL))
387         fi
388         sleep $INTERVAL
389         WAIT=$((WAIT + INTERVAL))
390     done
391     echo "service didn't stop after $WAIT seconds.  Still running:"
392     echo ${running}
393     exit 1
394 }
395
396 zconf_mount() {
397     local OPTIONS
398     local client=$1
399     local mnt=$2
400     # Only supply -o to mount if we have options
401     if [ -n "$MOUNTOPT" ]; then
402         OPTIONS="-o $MOUNTOPT"
403     fi
404     local device=$MGSNID:/$FSNAME
405     if [ -z "$mnt" -o -z "$FSNAME" ]; then
406         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
407         exit 1
408     fi
409
410     echo "Starting client: $OPTIONS $device $mnt" 
411     do_node $client mkdir -p $mnt
412     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
413
414     do_node $client "sysctl -w lnet.debug=$PTLDEBUG; sysctl -w lnet.subsystem_debug=${SUBSYSTEM# }"
415     [ -d /r ] && $LCTL modules > /r/tmp/ogdb-`hostname`
416     return 0
417 }
418
419 zconf_umount() {
420     client=$1
421     mnt=$2
422     [ "$3" ] && force=-f
423     local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
424     if [ $running -ne 0 ]; then
425         echo "Stopping client $mnt (opts:$force)"
426         do_node $client umount $force $mnt
427     fi
428 }
429
430 shutdown_facet() {
431     facet=$1
432     if [ "$FAILURE_MODE" = HARD ]; then
433         $POWER_DOWN `facet_active_host $facet`
434         sleep 2 
435     elif [ "$FAILURE_MODE" = SOFT ]; then
436         stop $facet
437     fi
438 }
439
440 reboot_facet() {
441     facet=$1
442     if [ "$FAILURE_MODE" = HARD ]; then
443         $POWER_UP `facet_active_host $facet`
444     else
445         sleep 10
446     fi
447 }
448
449 # verify that lustre actually cleaned up properly
450 cleanup_check() {
451     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
452         error "LBUG/LASSERT detected"
453     BUSY=`dmesg | grep -i destruct || true`
454     if [ "$BUSY" ]; then
455         echo "$BUSY" 1>&2
456         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
457         exit 205
458     fi
459     LEAK_LUSTRE=`dmesg | tail -n 30 | grep "obd mem.*leaked" || true`
460     LEAK_PORTALS=`dmesg | tail -n 20 | grep "Portals memory leaked" || true`
461     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
462         echo "$0: $LEAK_LUSTRE" 1>&2
463         echo "$0: $LEAK_PORTALS" 1>&2
464         echo "$0: Memory leak(s) detected..." 1>&2
465         mv $TMP/debug $TMP/debug-leak.`date +%s`
466         exit 204
467     fi
468
469     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
470         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
471
472     if [ "`/sbin/lsmod 2>&1 | egrep 'lnet|libcfs'`" ]; then
473         echo "$0: modules still loaded..." 1>&2
474         /sbin/lsmod 1>&2
475         return 203
476     fi
477     return 0
478 }
479
480 wait_delete_completed () {
481     local TOTALPREV=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
482             $LPROC/osc/*/kbytesavail`
483
484     local WAIT=0
485     local MAX_WAIT=20
486     while [ "$WAIT" -ne "$MAX_WAIT" ]; do
487         sleep 1
488         TOTAL=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
489             $LPROC/osc/*/kbytesavail`
490         [ "$TOTAL" -eq "$TOTALPREV" ] && break
491         echo "Waiting delete completed ... prev: $TOTALPREV current: $TOTAL "
492         TOTALPREV=$TOTAL
493         WAIT=$(( WAIT + 1))
494     done
495     echo "Delete completed."
496 }
497
498 wait_for_host() {
499     HOST=$1
500     check_network "$HOST" 900
501     while ! do_node $HOST "ls -d $LUSTRE " > /dev/null; do sleep 5; done
502 }
503
504 wait_for() {
505     facet=$1
506     HOST=`facet_active_host $facet`
507     wait_for_host $HOST
508 }
509
510 wait_mds_recovery_done () {
511     local timeout=`do_facet mds cat /proc/sys/lustre/timeout`
512 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2)
513 # as we are in process of changing obd_timeout in different ways
514 # let's set MAX longer than that
515     MAX=$(( timeout * 4 ))
516     WAIT=0
517     while [ $WAIT -lt $MAX ]; do
518         STATUS=`do_facet mds grep status /proc/fs/lustre/mdt/*-MDT*/recovery_status`
519         echo $STATUS | grep COMPLETE && return 0
520         sleep 5
521         WAIT=$((WAIT + 5))
522         echo "Waiting $(($MAX - $WAIT)) secs for MDS recovery done"
523     done
524     echo "MDS recovery not done in $MAX sec"
525     return 1            
526 }
527
528 client_df() {
529     # not every config has many clients
530     if [ ! -z "$CLIENTS" ]; then
531         $PDSH $CLIENTS "df $MOUNT" > /dev/null
532     fi
533 }
534
535 client_reconnect() {
536     uname -n >> $MOUNT/recon
537     if [ ! -z "$CLIENTS" ]; then
538         $PDSH $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
539     fi
540     echo Connected clients:
541     cat $MOUNT/recon
542     ls -l $MOUNT/recon > /dev/null
543     rm $MOUNT/recon
544 }
545
546 facet_failover() {
547     facet=$1
548     echo "Failing $facet on node `facet_active_host $facet`"
549     shutdown_facet $facet
550     reboot_facet $facet
551     client_df &
552     DFPID=$!
553     echo "df pid is $DFPID"
554     change_active $facet
555     TO=`facet_active_host $facet`
556     echo "Failover $facet to $TO"
557     wait_for $facet
558     local dev=${facet}_dev
559     local opt=${facet}_opt
560     start $facet ${!dev} ${!opt} || error "Restart of $facet failed"
561 }
562
563 obd_name() {
564     local facet=$1
565 }
566
567 replay_barrier() {
568     local facet=$1
569     do_facet $facet sync
570     df $MOUNT
571     local svc=${facet}_svc
572     do_facet $facet $LCTL --device %${!svc} readonly
573     do_facet $facet $LCTL --device %${!svc} notransno
574     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
575     $LCTL mark "local REPLAY BARRIER on ${!svc}"
576 }
577
578 replay_barrier_nodf() {
579     local facet=$1    echo running=${running}
580     do_facet $facet sync
581     local svc=${facet}_svc
582     echo Replay barrier on ${!svc}
583     do_facet $facet $LCTL --device %${!svc} readonly
584     do_facet $facet $LCTL --device %${!svc} notransno
585     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
586     $LCTL mark "local REPLAY BARRIER on ${!svc}"
587 }
588
589 mds_evict_client() {
590     UUID=`cat /proc/fs/lustre/mdc/${mds1_svc}-mdc-*/uuid`
591     do_facet mds1 "echo $UUID > /proc/fs/lustre/mdt/${mds1_svc}/evict_client"
592 }
593
594 ost_evict_client() {
595     UUID=`grep ${ost1_svc}-osc- $LPROC/devices | egrep -v 'MDT' | awk '{print $5}'`
596     do_facet ost1 "echo $UUID > /proc/fs/lustre/obdfilter/${ost1_svc}/evict_client"
597 }
598
599 fail() {
600     facet_failover $* || error "failover: $?"
601     df $MOUNT || error "post-failover df: $?"
602 }
603
604 fail_abort() {
605     local facet=$1
606     stop $facet
607     change_active $facet
608     local svc=${facet}_svc
609     local dev=${facet}_dev
610     local opt=${facet}_opt
611     start $facet ${!dev} ${!opt}
612     do_facet $facet lctl --device %${!svc} abort_recovery
613     df $MOUNT || echo "first df failed: $?"
614     sleep 1
615     df $MOUNT || error "post-failover df: $?"
616 }
617
618 do_lmc() {
619     echo There is no lmc.  This is mountconf, baby.
620     exit 1
621 }
622
623 h2gm () {
624     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
625         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
626         echo $ID"@gm"
627     fi
628 }
629
630 h2name_or_ip() {
631     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
632         echo $1"@$2" 
633     fi
634 }
635
636 h2ptl() {
637    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
638        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
639        if [ -z "$ID" ]; then
640            echo "Could not get a ptl id for $1..."
641            exit 1
642        fi
643        echo $ID"@ptl"
644    fi
645 }
646 declare -fx h2ptl
647
648 h2tcp() {
649     h2name_or_ip "$1" "tcp"
650 }
651 declare -fx h2tcp
652
653 h2elan() {
654     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
655         if type __h2elan >/dev/null 2>&1; then
656             ID=$(__h2elan $1)
657         else
658             ID=`echo $1 | sed 's/[^0-9]*//g'`
659         fi
660         echo $ID"@elan"
661     fi
662 }
663 declare -fx h2elan
664
665 h2openib() {
666     h2name_or_ip "$1" "openib"
667 }
668 declare -fx h2openib
669
670 h2o2ib() {
671     h2name_or_ip "$1" "o2ib"
672 }
673 declare -fx h2o2ib
674
675 facet_host() {
676     local facet=$1
677     varname=${facet}_HOST
678     if [ -z "${!varname}" ]; then
679         if [ "${facet:0:3}" == "ost" ]; then
680             eval ${facet}_HOST=${ost_HOST}
681         fi
682     fi
683     echo -n ${!varname}
684 }
685
686 facet_active() {
687     local facet=$1
688     local activevar=${facet}active
689
690     if [ -f ./${facet}active ] ; then
691         source ./${facet}active
692     fi
693
694     active=${!activevar}
695     if [ -z "$active" ] ; then 
696         echo -n ${facet}
697     else
698         echo -n ${active}
699     fi
700 }
701
702 facet_active_host() {
703     local facet=$1
704     local active=`facet_active $facet`
705     if [ "$facet" == client ]; then
706         hostname
707     else
708         echo `facet_host $active`
709     fi
710 }
711
712 change_active() {
713     local facet=$1
714     failover=${facet}failover 
715     host=`facet_host $failover`
716     [ -z "$host" ] && return
717     curactive=`facet_active $facet`
718     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
719         eval export ${facet}active=$facet
720     else
721         eval export ${facet}active=$failover
722     fi
723     # save the active host for this facet
724     activevar=${facet}active
725     echo "$activevar=${!activevar}" > ./$activevar
726 }
727
728 do_node() {
729     HOST=$1
730     shift
731     local myPDSH=$PDSH
732     if [ "$HOST" = "$(hostname)" ]; then
733         myPDSH="no_dsh"
734     fi
735     if $VERBOSE; then
736         echo "CMD: $HOST $@" >&2
737         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
738     fi
739     $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
740     return ${PIPESTATUS[0]}
741 }
742
743 do_facet() {
744     facet=$1
745     shift
746     HOST=`facet_active_host $facet`
747     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
748     do_node $HOST $@
749 }
750
751 add() {
752     local facet=$1
753     shift
754     # make sure its not already running
755     stop ${facet} -f
756     rm -f ${facet}active
757     do_facet ${facet} $MKFS $*
758 }
759
760 ostdevname() {
761     num=$1
762     DEVNAME=OSTDEV$num
763     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
764     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
765     echo -n $DEVPTR
766 }
767
768 mdsdevname() {
769     num=$1
770     DEVNAME=MDSDEV$num
771     #if $MDSDEVn isn't defined, default is $MDSDEVBASE + num
772     eval DEVPTR=${!DEVNAME:=${MDSDEVBASE}${num}}
773     echo -n $DEVPTR
774 }
775
776 ########
777 ## MountConf setup
778
779 stopall() {
780     # make sure we are using the primary server, so test-framework will
781     # be able to clean up properly.
782     activemds=`facet_active mds1`
783     if [ $activemds != "mds1" ]; then
784         fail mds1
785     fi
786     
787     # assume client mount is local 
788     grep " $MOUNT " /proc/mounts && zconf_umount `hostname` $MOUNT $*
789     grep " $MOUNT2 " /proc/mounts && zconf_umount `hostname` $MOUNT2 $*
790     for num in `seq $MDSCOUNT`; do
791         stop mds$num -f
792     done
793     for num in `seq $OSTCOUNT`; do
794         stop ost$num -f
795     done
796     return 0
797 }
798
799 cleanupall() {
800     stopall $*
801     unload_modules
802     cleanup_krb5_env
803 }
804
805 mdsmkfsopts()
806 {
807     local nr=$1
808     test $nr = 1 && echo -n $MDS_MKFS_OPTS || echo -n $MDSn_MKFS_OPTS
809 }
810
811 formatall() {
812     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
813
814     stopall
815     # We need ldiskfs here, may as well load them all
816     load_modules
817     [ "$CLIENTONLY" ] && return
818     echo "Formatting mdts, osts"
819     for num in `seq $MDSCOUNT`; do
820         echo "Format mds$num: $(mdsdevname $num)"
821         if $VERBOSE; then
822             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` || exit 9
823         else
824             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` > /dev/null || exit 9
825         fi
826     done
827
828     for num in `seq $OSTCOUNT`; do
829         echo "Format ost$num: $(ostdevname $num)"
830         if $VERBOSE; then
831             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` || exit 10
832         else
833             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` > /dev/null || exit 10
834         fi
835     done
836 }
837
838 mount_client() {
839     grep " $1 " /proc/mounts || zconf_mount `hostname` $*
840 }
841
842 setupall() {
843     load_modules
844     init_krb5_env
845     if [ -z "$CLIENTONLY" ]; then
846         echo "Setup mdts, osts"
847         for num in `seq $MDSCOUNT`; do
848             DEVNAME=$(mdsdevname $num)
849             echo $REFORMAT | grep -q "reformat" \
850             || do_facet mds$num "$TUNEFS --writeconf $DEVNAME"
851             start mds$num $DEVNAME $MDS_MOUNT_OPTS
852         done
853         for num in `seq $OSTCOUNT`; do
854             DEVNAME=$(ostdevname $num)
855             start ost$num $DEVNAME $OST_MOUNT_OPTS
856         done
857     fi
858     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
859     mount_client $MOUNT
860     if [ "$MOUNT_2" ]; then
861         mount_client $MOUNT2
862     fi
863     sleep 5
864 }
865
866 mounted_lustre_filesystems() {
867         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
868 }
869
870 check_and_setup_lustre() {
871     MOUNTED="`mounted_lustre_filesystems`"
872     if [ -z "$MOUNTED" ]; then
873         [ "$REFORMAT" ] && formatall
874         setupall
875         MOUNTED="`mounted_lustre_filesystems`"
876         [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
877         export I_MOUNTED=yes
878     fi
879     if [ "$ONLY" == "setup" ]; then
880         exit 0
881     fi
882 }
883
884 cleanup_and_setup_lustre() {
885     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
886         sysctl -w lnet.debug=0 || true
887         cleanupall
888         if [ "$ONLY" == "cleanup" ]; then 
889             exit 0
890         fi
891     fi
892     check_and_setup_lustre
893 }
894
895 check_and_cleanup_lustre() {
896     if [ "`mount | grep $MOUNT`" ]; then
897         rm -rf $DIR/[Rdfs][1-9]*
898     fi
899     if [ "$I_MOUNTED" = "yes" ]; then
900         cleanupall -f || error "cleanup failed"
901     fi
902     unset I_MOUNTED
903 }
904
905 ####### 
906 # General functions
907
908 check_network() {
909     local NETWORK=0
910     local WAIT=0
911     local MAX=$2
912     while [ $NETWORK -eq 0 ]; do
913         ping -c 1 -w 3 $1 > /dev/null
914         if [ $? -eq 0 ]; then
915             NETWORK=1
916         else
917             WAIT=$((WAIT + 5))
918             echo "waiting for $1, $((MAX - WAIT)) secs left"
919             sleep 5
920         fi
921         if [ $WAIT -gt $MAX ]; then
922             echo "Network not available"
923             exit 1
924         fi
925     done
926 }
927 check_port() {
928     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
929         sleep 9
930     done
931 }
932
933 no_dsh() {
934     shift
935     eval $@
936 }
937
938 comma_list() {
939     # the sed converts spaces to commas, but leaves the last space
940     # alone, so the line doesn't end with a comma.
941     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
942 }
943
944 absolute_path() {
945     (cd `dirname $1`; echo $PWD/`basename $1`)
946 }
947
948 ##################################
949 # OBD_FAIL funcs
950
951 drop_request() {
952 # OBD_FAIL_MDS_ALL_REQUEST_NET
953     RC=0
954     do_facet mds sysctl -w lustre.fail_loc=0x123
955     do_facet client "$1" || RC=$?
956     do_facet mds sysctl -w lustre.fail_loc=0
957     return $RC
958 }
959
960 drop_reply() {
961 # OBD_FAIL_MDS_ALL_REPLY_NET
962     RC=0
963     do_facet mds sysctl -w lustre.fail_loc=0x122
964     do_facet client "$@" || RC=$?
965     do_facet mds sysctl -w lustre.fail_loc=0
966     return $RC
967 }
968
969 drop_reint_reply() {
970 # OBD_FAIL_MDS_REINT_NET_REP
971     RC=0
972     do_facet mds sysctl -w lustre.fail_loc=0x119
973     do_facet client "$@" || RC=$?
974     do_facet mds sysctl -w lustre.fail_loc=0
975     return $RC
976 }
977
978 pause_bulk() {
979 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
980     RC=0
981     do_facet ost1 sysctl -w lustre.fail_loc=0x214
982     do_facet client "$1" || RC=$?
983     do_facet client "sync"
984     do_facet ost1 sysctl -w lustre.fail_loc=0
985     return $RC
986 }
987
988 drop_ldlm_cancel() {
989 #define OBD_FAIL_LDLM_CANCEL             0x304
990     RC=0
991     do_facet client sysctl -w lustre.fail_loc=0x304
992     do_facet client "$@" || RC=$?
993     do_facet client sysctl -w lustre.fail_loc=0
994     return $RC
995 }
996
997 drop_bl_callback() {
998 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
999     RC=0
1000     do_facet client sysctl -w lustre.fail_loc=0x305
1001     do_facet client "$@" || RC=$?
1002     do_facet client sysctl -w lustre.fail_loc=0
1003     return $RC
1004 }
1005
1006 drop_ldlm_reply() {
1007 #define OBD_FAIL_LDLM_REPLY              0x30c
1008     RC=0
1009     do_facet mds sysctl -w lustre.fail_loc=0x30c
1010     do_facet client "$@" || RC=$?
1011     do_facet mds sysctl -w lustre.fail_loc=0
1012     return $RC
1013 }
1014
1015 clear_failloc() {
1016     facet=$1
1017     pause=$2
1018     sleep $pause
1019     echo "clearing fail_loc on $facet"
1020     do_facet $facet "sysctl -w lustre.fail_loc=0"
1021 }
1022
1023 cancel_lru_locks() {
1024     $LCTL mark "cancel_lru_locks $1 start"
1025     for d in `find $LPROC/ldlm/namespaces | egrep -i $1`; do
1026         [ -f $d/lru_size ] && echo clear > $d/lru_size
1027         [ -f $d/lock_unused_count ] && grep [1-9] $d/lock_unused_count /dev/null
1028     done
1029     $LCTL mark "cancel_lru_locks $1 stop"
1030 }
1031
1032
1033 pgcache_empty() {
1034     for a in /proc/fs/lustre/llite/*/dump_page_cache; do
1035         if [ `wc -l $a | awk '{print $1}'` -gt 1 ]; then
1036             echo there is still data in page cache $a ?
1037             cat $a;
1038             return 1;
1039         fi
1040     done
1041     return 0
1042 }
1043
1044 debugsave() {
1045     DEBUGSAVE="$(sysctl -n lnet.debug)"
1046 }
1047
1048 debugrestore() {
1049     [ -n "$DEBUGSAVE" ] && sysctl -w lnet.debug="${DEBUGSAVE}"
1050     DEBUGSAVE=""
1051 }
1052
1053 ##################################
1054 # Test interface 
1055 ##################################
1056
1057 error() {
1058     local FAIL_ON_ERROR=${FAIL_ON_ERROR:-true}
1059     local TYPE=${TYPE:-"FAIL"}
1060     local ERRLOG
1061     sysctl -w lustre.fail_loc=0 2> /dev/null || true
1062     log "${TESTSUITE} ${TESTNAME}: **** ${TYPE}:" $@
1063     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
1064     echo "Dumping lctl log to $ERRLOG"
1065     # We need to dump the logs on all nodes
1066     $LCTL dk $ERRLOG
1067     [ ! "$mds_HOST" = "$(hostname)" ] && do_node $mds_HOST $LCTL dk $ERRLOG
1068     [ ! "$ost_HOST" = "$(hostname)" -a ! "$ost_HOST" = "$mds_HOST" ] && do_node $ost_HOST $LCTL dk $ERRLOG
1069     debugrestore
1070     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
1071     if $FAIL_ON_ERROR; then
1072         exit 1
1073     fi
1074 }
1075
1076 # use only if we are ignoring failures for this test, bugno required.
1077 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
1078 # e.g. error_ignore 5494 "your message"
1079 error_ignore() {
1080     FAIL_ON_ERROR=false TYPE="IGNORE (bz$1)" error $2
1081 }
1082
1083 skip () {
1084         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
1085         [ "$TESTSUITELOG" ] && echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG
1086 }
1087
1088 build_test_filter() {
1089     [ "$ONLY" ] && log "only running test `echo $ONLY`"
1090     for O in $ONLY; do
1091         eval ONLY_${O}=true
1092     done
1093     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
1094         log "skipping tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
1095     for E in $EXCEPT $ALWAYS_EXCEPT; do
1096         eval EXCEPT_${E}=true
1097     done
1098     for G in $GRANT_CHECK_LIST; do
1099         eval GCHECK_ONLY_${G}=true
1100         done
1101 }
1102
1103 _basetest() {
1104     echo $*
1105 }
1106
1107 basetest() {
1108     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
1109 }
1110
1111 run_test() {
1112     export base=`basetest $1`
1113     if [ ! -z "$ONLY" ]; then
1114         testname=ONLY_$1
1115         if [ ${!testname}x != x ]; then
1116             run_one $1 "$2"
1117             return $?
1118         fi
1119         testname=ONLY_$base
1120         if [ ${!testname}x != x ]; then
1121             run_one $1 "$2"
1122             return $?
1123         fi
1124         echo -n "."
1125         return 0
1126     fi
1127     testname=EXCEPT_$1
1128     if [ ${!testname}x != x ]; then
1129         TESTNAME=test_$1 skip "skipping excluded test $1"
1130         return 0
1131     fi
1132     testname=EXCEPT_$base
1133     if [ ${!testname}x != x ]; then
1134         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
1135         return 0
1136     fi
1137     run_one $1 "$2"
1138     
1139     return $?
1140 }
1141
1142 EQUALS="======================================================================"
1143 equals_msg() {
1144     msg="$@"
1145
1146     local suffixlen=$((${#EQUALS} - ${#msg}))
1147     [ $suffixlen -lt 5 ] && suffixlen=5
1148     printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS
1149 }
1150
1151 log() {
1152     echo "$*"
1153     lsmod | grep lnet > /dev/null || load_modules
1154     $LCTL mark "$*" 2> /dev/null || true
1155 }
1156
1157 trace() {
1158         log "STARTING: $*"
1159         strace -o $TMP/$1.strace -ttt $*
1160         RC=$?
1161         log "FINISHED: $*: rc $RC"
1162         return 1
1163 }
1164
1165 pass() {
1166     echo PASS $@
1167 }
1168
1169 check_mds() {
1170     FFREE=`cat /proc/fs/lustre/mds/*/filesfree`
1171     FTOTAL=`cat /proc/fs/lustre/mds/*/filestotal`
1172     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
1173 }
1174
1175 run_one() {
1176     testnum=$1
1177     message=$2
1178     tfile=f${testnum}
1179     export tdir=d${base}
1180
1181     BEFORE=`date +%s`
1182     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
1183     #check_mds
1184     export TESTNAME=test_$testnum
1185     test_${testnum} || error "test_$testnum failed with $?"
1186     #check_mds
1187     check_grant ${testnum} || error "check_grant $testnum failed with $?"
1188     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
1189         error "LBUG/LASSERT detected"
1190     pass "($((`date +%s` - $BEFORE))s)"
1191     unset TESTNAME
1192     unset tdir
1193     cd $SAVE_PWD
1194     $CLEANUP
1195 }
1196
1197 canonical_path() {
1198     (cd `dirname $1`; echo $PWD/`basename $1`)
1199 }
1200
1201 sync_clients() {
1202     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync 
1203     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync 
1204         cd $SAVE_PWD
1205 }
1206
1207 check_grant() {
1208     export base=`basetest $1`
1209     [ "$CHECK_GRANT" == "no" ] && return 0
1210
1211         testname=GCHECK_ONLY_${base}
1212         [ ${!testname}x == x ] && return 0
1213
1214         echo -n "checking grant......"
1215         cd $SAVE_PWD
1216         # write some data to sync client lost_grant
1217         rm -f $DIR1/${tfile}_check_grant_* 2>&1
1218         for i in `seq $OSTCOUNT`; do
1219                 $LFS setstripe $DIR1/${tfile}_check_grant_$i 0 $(($i -1)) 1
1220                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
1221                                               count=1 > /dev/null 2>&1 
1222         done
1223         # sync all the data and make sure no pending data on server
1224         sync_clients
1225         
1226         #get client grant and server grant 
1227         client_grant=0
1228     for d in ${LPROC}/osc/*/cur_grant_bytes; do 
1229                 client_grant=$((client_grant + `cat $d`))
1230         done
1231         server_grant=0
1232         for d in ${LPROC}/obdfilter/*/tot_granted; do
1233                 server_grant=$((server_grant + `cat $d`))
1234         done
1235
1236         # cleanup the check_grant file
1237         for i in `seq $OSTCOUNT`; do
1238                 rm $DIR1/${tfile}_check_grant_$i
1239         done
1240
1241         #check whether client grant == server grant 
1242         if [ $client_grant != $server_grant ]; then
1243                 echo "failed: client:${client_grant} server: ${server_grant}"
1244                 return 1
1245         else
1246                 echo "pass"
1247         fi
1248 }
1249
1250 ########################
1251 # helper functions
1252
1253 osc_to_ost()
1254 {
1255     osc=$1
1256     ost=`echo $1 | awk -F_ '{print $3}'`
1257     if [ -z $ost ]; then
1258         ost=`echo $1 | sed 's/-osc.*//'`
1259     fi
1260     echo $ost
1261 }
1262
1263 remote_mds ()
1264 {
1265     [ ! -e /proc/fs/lustre/mdt/*MDT* ]
1266 }
1267
1268 remote_ost ()
1269 {
1270     [ $(grep -c obdfilter $LPROC/devices) -eq 0 ]
1271 }
1272
1273 is_patchless ()
1274 {
1275     grep -q patchless $LPROC/version
1276 }