Whamcloud - gitweb
Branch b1_6
[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 PDSH="pdsh -S -Rssh -w"
14
15 # eg, assert_env LUSTRE MDSNODES OSTNODES CLIENTS
16 assert_env() {
17     local failed=""
18     for name in $@; do
19         if [ -z "${!name}" ]; then
20             echo "$0: $name must be set"
21             failed=1
22         fi
23     done
24     [ $failed ] && exit 1 || true
25 }
26
27 usage() {
28     echo "usage: $0 [-r] [-f cfgfile]"
29     echo "       -r: reformat"
30
31     exit
32 }
33
34 init_test_env() {
35     export LUSTRE=`absolute_path $LUSTRE`
36     export TESTSUITE=`basename $0 .sh`
37     export LTESTDIR=${LTESTDIR:-$LUSTRE/../ltest}
38
39     [ -d /r ] && export ROOT=${ROOT:-/r}
40     export TMP=${TMP:-$ROOT/tmp}
41
42     export PATH=:$PATH:$LUSTRE/utils:$LUSTRE/tests
43     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
44     [ ! -f "$LCTL" ] && export LCTL=$(which lctl) 
45     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
46     [ ! -f "$LFS" ] && export LFS=$(which lfs) 
47     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
48     [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre) 
49     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
50     [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre) 
51     export CHECKSTAT="${CHECKSTAT:-checkstat} "
52     export FSYTPE=${FSTYPE:-"ldiskfs"}
53     export NAME=${NAME:-local}
54     export LPROC=/proc/fs/lustre
55
56     if [ "$ACCEPTOR_PORT" ]; then
57         export PORT_OPT="--port $ACCEPTOR_PORT"
58     fi
59
60     # Paths on remote nodes, if different 
61     export RLUSTRE=${RLUSTRE:-$LUSTRE}
62     export RPWD=${RPWD:-$PWD}
63
64     # command line
65     
66     while getopts "rvf:" opt $*; do 
67         case $opt in
68             f) CONFIG=$OPTARG;;
69             r) REFORMAT=--reformat;;
70             v) VERBOSE=true;;
71             \?) usage;;
72         esac
73     done
74
75     shift $((OPTIND - 1))
76     ONLY=${ONLY:-$*}
77 }
78
79 case `uname -r` in
80 2.4.*) EXT=".o"; USE_QUOTA=no; [ ! "$CLIENTONLY" ] && FSTYPE=ext3;;
81     *) EXT=".ko"; USE_QUOTA=yes;;
82 esac
83
84 load_module() {
85     module=$1
86     shift
87     BASE=`basename $module $EXT`
88     lsmod | grep -q ${BASE} || \
89       if [ -f ${LUSTRE}/${module}${EXT} ]; then
90         insmod ${LUSTRE}/${module}${EXT} $@
91     else
92         # must be testing a "make install" or "rpm" installation
93         modprobe $BASE $@
94     fi
95 }
96
97 load_modules() {
98     if [ -n "$MODPROBE" ]; then
99         # use modprobe
100     return 0
101     fi
102     if [ "$HAVE_MODULES" = true ]; then
103     # we already loaded
104         return 0
105     fi
106     HAVE_MODULES=true
107
108     echo Loading modules from $LUSTRE
109     load_module ../lnet/libcfs/libcfs
110     [ -f /etc/modprobe.conf ] && MODPROBECONF=/etc/modprobe.conf
111     [ -f /etc/modprobe.d/Lustre ] && MODPROBECONF=/etc/modprobe.d/Lustre
112     [ -z "$LNETOPTS" -a -n "$MODPROBECONF" ] && \
113         LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
114     echo "lnet options: '$LNETOPTS'"
115     # note that insmod will ignore anything in modprobe.conf
116     load_module ../lnet/lnet/lnet $LNETOPTS
117     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
118     load_module ../lnet/klnds/$LNETLND
119     load_module lvfs/lvfs
120     load_module obdclass/obdclass
121     load_module ptlrpc/ptlrpc
122     [ "$USE_QUOTA" = "yes" ] && load_module quota/lquota
123     load_module mdc/mdc
124     load_module osc/osc
125     load_module lov/lov
126     load_module mgc/mgc
127     if [ -z "$CLIENTONLY" ]; then
128         load_module mgs/mgs
129         load_module mds/mds
130         [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
131         load_module lvfs/fsfilt_$FSTYPE
132         load_module ost/ost
133         load_module obdfilter/obdfilter
134     fi
135
136     load_module llite/lustre
137     rm -f $TMP/ogdb-`hostname`
138     OGDB=$TMP
139     [ -d /r ] && OGDB="/r/tmp"
140     $LCTL modules > $OGDB/ogdb-`hostname`
141     # 'mount' doesn't look in $PATH, just sbin
142     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
143 }
144
145 RMMOD=rmmod
146 if [ `uname -r | cut -c 3` -eq 4 ]; then
147     RMMOD="modprobe -r"
148 fi
149
150 wait_for_lnet() {
151     local UNLOADED=0
152     local WAIT=0
153     local MAX=60
154     MODULES=$($LCTL modules | awk '{ print $2 }')
155     while [ -n "$MODULES" ]; do
156     sleep 5
157     $RMMOD $MODULES >/dev/null 2>&1 || true
158     MODULES=$($LCTL modules | awk '{ print $2 }')
159         if [ -z "$MODULES" ]; then
160         return 0
161         else
162             WAIT=$((WAIT + 5))
163             echo "waiting, $((MAX - WAIT)) secs left"
164         fi
165         if [ $WAIT -eq $MAX ]; then
166             echo "LNET modules $MODULES will not unload"
167         lsmod
168             return 3
169         fi
170     done
171 }
172
173 unload_modules() {
174     lsmod | grep lnet > /dev/null && $LCTL dl && $LCTL dk $TMP/debug
175     local MODULES=$($LCTL modules | awk '{ print $2 }')
176     $RMMOD $MODULES > /dev/null 2>&1 || true
177      # do it again, in case we tried to unload ksocklnd too early
178     MODULES=$($LCTL modules | awk '{ print $2 }')
179     [ -n "$MODULES" ] && $RMMOD $MODULES > /dev/null 2>&1 || true
180     MODULES=$($LCTL modules | awk '{ print $2 }')
181     if [ -n "$MODULES" ]; then
182     echo "Modules still loaded: "
183     echo $MODULES 
184     if [ -e $LPROC ]; then
185         echo "Lustre still loaded"
186         cat $LPROC/devices || true
187         lsmod
188         return 2
189     else
190         echo "Lustre stopped but LNET is still loaded, waiting..."
191         wait_for_lnet || return 3
192     fi
193     fi
194     HAVE_MODULES=false
195
196     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd mem.*leaked" || true)
197     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
198     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
199         echo "$LEAK_LUSTRE" 1>&2
200         echo "$LEAK_PORTALS" 1>&2
201         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
202         echo "Memory leaks detected"
203         [ -n "$IGNORE_LEAK" ] && echo "ignoring leaks" && return 0
204         return 254
205     fi
206     echo "modules unloaded."
207     return 0
208 }
209
210 # Facet functions
211 # start facet device options 
212 start() {
213     facet=$1
214     shift
215     device=$1
216     shift
217     echo "Starting ${facet}: $@ ${device} ${MOUNT%/*}/${facet}"
218     do_facet ${facet} mkdir -p ${MOUNT%/*}/${facet}
219     do_facet ${facet} mount -t lustre $@ ${device} ${MOUNT%/*}/${facet} 
220     RC=${PIPESTATUS[0]}
221     if [ $RC -ne 0 ]; then
222         echo mount -t lustre $@ ${device} ${MOUNT%/*}/${facet} 
223         echo Start of ${device} on ${facet} failed ${RC}
224     else 
225         do_facet ${facet} sync
226         label=$(do_facet ${facet} "e2label ${device}")
227         [ -z "$label" ] && echo no label for ${device} && exit 1
228         eval export ${facet}_svc=${label}
229         eval export ${facet}_dev=${device}
230         eval export ${facet}_opt=\"$@\"
231         echo Started ${label}
232     fi
233     return $RC
234 }
235
236 stop() {
237     local running
238     facet=$1
239     shift
240     HOST=`facet_active_host $facet`
241     [ -z $HOST ] && echo stop: no host for $facet && return 0
242
243     running=$(do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts") || true
244     if [ ${running} -ne 0 ]; then
245         echo "Stopping ${MOUNT%/*}/${facet} (opts:$@)"
246         do_facet ${facet} umount -d $@ ${MOUNT%/*}/${facet}
247     fi
248
249     # umount should block, but we should wait for unrelated obd's
250     # like the MGS or MGC to also stop.
251     local WAIT=0
252     local INTERVAL=1
253     # conf-sanity 31 takes a long time cleanup
254     while [ $WAIT -lt 300 ]; do
255         running=$(do_facet ${facet} "[ -e $LPROC ] && grep ST' ' $LPROC/devices") || true
256         if [ -z "${running}" ]; then
257             return 0
258         fi
259         echo "waited $WAIT for${running}"
260         if [ $INTERVAL -lt 64 ]; then 
261             INTERVAL=$((INTERVAL + INTERVAL))
262         fi
263         sleep $INTERVAL
264         WAIT=$((WAIT + INTERVAL))
265     done
266     echo "service didn't stop after $WAIT seconds.  Still running:"
267     echo ${running}
268     exit 1
269 }
270
271 zconf_mount() {
272     local OPTIONS
273     local client=$1
274     local mnt=$2
275     # Only supply -o to mount if we have options
276     if [ -n "$MOUNTOPT" ]; then
277         OPTIONS="-o $MOUNTOPT"
278     fi
279     local device=$MGSNID:/$FSNAME
280     if [ -z "$mnt" -o -z "$FSNAME" ]; then
281         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
282         exit 1
283     fi
284
285     echo "Starting client: $OPTIONS $device $mnt" 
286     do_node $client mkdir -p $mnt
287     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
288
289     do_node $client "sysctl -w lnet.debug=$PTLDEBUG; sysctl -w lnet.subsystem_debug=${SUBSYSTEM# }"
290     [ -d /r ] && $LCTL modules > /r/tmp/ogdb-`hostname`
291     return 0
292 }
293
294 zconf_umount() {
295     client=$1
296     mnt=$2
297     [ "$3" ] && force=-f
298     local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
299     if [ $running -ne 0 ]; then
300         echo "Stopping client $mnt (opts:$force)"
301         do_node $client umount $force $mnt
302     fi
303 }
304
305 shutdown_facet() {
306     facet=$1
307     if [ "$FAILURE_MODE" = HARD ]; then
308         $POWER_DOWN `facet_active_host $facet`
309         sleep 2 
310     elif [ "$FAILURE_MODE" = SOFT ]; then
311         stop $facet
312     fi
313 }
314
315 reboot_facet() {
316     facet=$1
317     if [ "$FAILURE_MODE" = HARD ]; then
318         $POWER_UP `facet_active_host $facet`
319     else
320         sleep 10
321     fi
322 }
323
324 # verify that lustre actually cleaned up properly
325 cleanup_check() {
326     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
327         error "LBUG/LASSERT detected"
328     BUSY=`dmesg | grep -i destruct || true`
329     if [ "$BUSY" ]; then
330         echo "$BUSY" 1>&2
331         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
332         exit 205
333     fi
334     LEAK_LUSTRE=`dmesg | tail -n 30 | grep "obd mem.*leaked" || true`
335     LEAK_PORTALS=`dmesg | tail -n 20 | grep "Portals memory leaked" || true`
336     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
337         echo "$0: $LEAK_LUSTRE" 1>&2
338         echo "$0: $LEAK_PORTALS" 1>&2
339         echo "$0: Memory leak(s) detected..." 1>&2
340         mv $TMP/debug $TMP/debug-leak.`date +%s`
341         exit 204
342     fi
343
344     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
345         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
346
347     if [ "`/sbin/lsmod 2>&1 | egrep 'lnet|libcfs'`" ]; then
348         echo "$0: modules still loaded..." 1>&2
349         /sbin/lsmod 1>&2
350         return 203
351     fi
352     return 0
353 }
354
355 wait_for_host() {
356     HOST=$1
357     check_network "$HOST" 900
358     while ! do_node $HOST "ls -d $LUSTRE " > /dev/null; do sleep 5; done
359 }
360
361 wait_for() {
362     facet=$1
363     HOST=`facet_active_host $facet`
364     wait_for_host $HOST
365 }
366
367 client_df() {
368     # not every config has many clients
369     if [ -n "$CLIENTS" ]; then
370         $PDSH $CLIENTS "df $MOUNT" > /dev/null
371     else
372         df $MOUNT > /dev/null
373     fi
374 }
375
376 client_reconnect() {
377     uname -n >> $MOUNT/recon
378     if [ ! -z "$CLIENTS" ]; then
379         $PDSH $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
380     fi
381     echo Connected clients:
382     cat $MOUNT/recon
383     ls -l $MOUNT/recon > /dev/null
384     rm $MOUNT/recon
385 }
386
387 facet_failover() {
388     facet=$1
389     echo "Failing $facet on node `facet_active_host $facet`"
390     shutdown_facet $facet
391     reboot_facet $facet
392     client_df &
393     DFPID=$!
394     echo "df pid is $DFPID"
395     change_active $facet
396     TO=`facet_active_host $facet`
397     echo "Failover $facet to $TO"
398     wait_for $facet
399     local dev=${facet}_dev
400     local opt=${facet}_opt
401     start $facet ${!dev} ${!opt} || error "Restart of $facet failed"
402 }
403
404 obd_name() {
405     local facet=$1
406 }
407
408 replay_barrier() {
409     local facet=$1
410     do_facet $facet sync
411     df $MOUNT
412     local svc=${facet}_svc
413     do_facet $facet $LCTL --device %${!svc} readonly
414     do_facet $facet $LCTL --device %${!svc} notransno
415     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
416     $LCTL mark "local REPLAY BARRIER on ${!svc}"
417 }
418
419 replay_barrier_nodf() {
420     local facet=$1    echo running=${running}
421     do_facet $facet sync
422     local svc=${facet}_svc
423     echo Replay barrier on ${!svc}
424     do_facet $facet $LCTL --device %${!svc} readonly
425     do_facet $facet $LCTL --device %${!svc} notransno
426     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
427     $LCTL mark "local REPLAY BARRIER on ${!svc}"
428 }
429
430 mds_evict_client() {
431     UUID=`cat /proc/fs/lustre/mdc/${mds_svc}-mdc-*/uuid`
432     do_facet mds "echo $UUID > /proc/fs/lustre/mds/${mds_svc}/evict_client"
433 }
434
435 ost_evict_client() {
436     UUID=`cat /proc/fs/lustre/osc/${ost1_svc}-osc-*/uuid`
437     do_facet ost1 "echo $UUID > /proc/fs/lustre/obdfilter/${ost1_svc}/evict_client"
438 }
439
440 fail() {
441     facet_failover $* || error "failover: $?"
442     df $MOUNT || error "post-failover df: $?"
443 }
444
445 fail_abort() {
446     local facet=$1
447     stop $facet
448     change_active $facet
449     local svc=${facet}_svc
450     local dev=${facet}_dev
451     local opt=${facet}_opt
452     start $facet ${!dev} ${!opt}
453     do_facet $facet lctl --device %${!svc} abort_recovery
454     df $MOUNT || echo "first df failed: $?"
455     sleep 1
456     df $MOUNT || error "post-failover df: $?"
457 }
458
459 do_lmc() {
460     echo There is no lmc.  This is mountconf, baby.
461     exit 1
462 }
463
464 h2gm () {
465     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
466         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
467         echo $ID"@gm"
468     fi
469 }
470
471 h2ptl() {
472    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
473        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
474        if [ -z "$ID" ]; then
475            echo "Could not get a ptl id for $1..."
476            exit 1
477        fi
478        echo $ID"@ptl"
479    fi
480 }
481 declare -fx h2ptl
482
483 h2tcp() {
484     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
485         echo $1"@tcp" 
486     fi
487 }
488 declare -fx h2tcp
489
490 h2elan() {
491     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
492         if type __h2elan >/dev/null 2>&1; then
493             ID=$(__h2elan $1)
494         else
495             ID=`echo $1 | sed 's/[^0-9]*//g'`
496         fi
497         echo $ID"@elan"
498     fi
499 }
500 declare -fx h2elan
501
502 h2openib() {
503     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
504         ID=`echo $1 | sed 's/[^0-9]*//g'`
505         echo $ID"@openib"
506     fi
507 }
508 declare -fx h2openib
509
510 facet_host() {
511     local facet=$1
512     varname=${facet}_HOST
513     if [ -z "${!varname}" ]; then
514         if [ "${facet:0:3}" == "ost" ]; then
515             eval ${facet}_HOST=${ost_HOST}
516         fi
517     fi
518     echo -n ${!varname}
519 }
520
521 facet_active() {
522     local facet=$1
523     local activevar=${facet}active
524
525     if [ -f ./${facet}active ] ; then
526         source ./${facet}active
527     fi
528
529     active=${!activevar}
530     if [ -z "$active" ] ; then 
531         echo -n ${facet}
532     else
533         echo -n ${active}
534     fi
535 }
536
537 facet_active_host() {
538     local facet=$1
539     local active=`facet_active $facet`
540     if [ "$facet" == client ]; then
541         hostname
542     else
543         echo `facet_host $active`
544     fi
545 }
546
547 change_active() {
548     local facet=$1
549     failover=${facet}failover 
550     host=`facet_host $failover`
551     [ -z "$host" ] && return
552     curactive=`facet_active $facet`
553     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
554         eval export ${facet}active=$facet
555     else
556         eval export ${facet}active=$failover
557     fi
558     # save the active host for this facet
559     activevar=${facet}active
560     echo "$activevar=${!activevar}" > ./$activevar
561 }
562
563 do_node() {
564     HOST=$1
565     shift
566     local myPDSH=$PDSH
567     if [ "$HOST" = "$(hostname)" ]; then
568         myPDSH="no_dsh"
569     fi
570     if $VERBOSE; then
571         echo "CMD: $HOST $@" >&2
572         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
573     fi
574     $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
575     return ${PIPESTATUS[0]}
576 }
577
578 do_facet() {
579     facet=$1
580     shift
581     HOST=`facet_active_host $facet`
582     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
583     do_node $HOST $@
584 }
585
586 add() {
587     local facet=$1
588     shift
589     # make sure its not already running
590     stop ${facet} -f
591     rm -f ${facet}active
592     do_facet ${facet} $MKFS $*
593 }
594
595 ostdevname() {
596     num=$1
597     DEVNAME=OSTDEV$num
598     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
599     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
600     echo -n $DEVPTR
601 }
602
603 ########
604 ## MountConf setup
605
606 stopall() {
607     # make sure we are using the primary server, so test-framework will
608     # be able to clean up properly.
609     activemds=`facet_active mds`
610     if [ $activemds != "mds" ]; then
611         fail mds
612     fi
613     
614     # assume client mount is local 
615     grep " $MOUNT " /proc/mounts && zconf_umount `hostname` $MOUNT $*
616     grep " $MOUNT2 " /proc/mounts && zconf_umount `hostname` $MOUNT2 $*
617     [ "$CLIENTONLY" ] && return
618     stop mds -f
619     for num in `seq $OSTCOUNT`; do
620         stop ost$num -f
621     done
622     return 0
623 }
624
625 cleanupall() {
626     stopall $*
627     unload_modules
628 }
629
630 formatall() {
631     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
632
633     stopall
634     # We need ldiskfs here, may as well load them all
635     load_modules
636     [ "$CLIENTONLY" ] && return
637     echo Formatting mds, osts
638     if $VERBOSE; then
639         add mds $MDS_MKFS_OPTS $FSTYPE_OPT --reformat $MDSDEV || exit 10
640     else
641         add mds $MDS_MKFS_OPTS $FSTYPE_OPT --reformat $MDSDEV > /dev/null || exit 10
642     fi
643
644     for num in `seq $OSTCOUNT`; do
645         if $VERBOSE; then
646             add ost$num $OST_MKFS_OPTS $FSTYPE_OPT --reformat `ostdevname $num` || exit 10
647         else
648             add ost$num $OST_MKFS_OPTS $FSTYPE_OPT --reformat `ostdevname $num` > /dev/null || exit 10
649         fi
650     done
651 }
652
653 mount_client() {
654     grep " $1 " /proc/mounts || zconf_mount `hostname` $*
655 }
656
657 setupall() {
658     load_modules
659     if [ -z "$CLIENTONLY" ]; then
660         echo Setup mdt, osts
661         echo $REFORMAT | grep -q "reformat" \
662             || do_facet mds "$TUNEFS --writeconf $MDSDEV"
663         start mds $MDSDEV $MDS_MOUNT_OPTS
664         for num in `seq $OSTCOUNT`; do
665             DEVNAME=`ostdevname $num`
666             start ost$num $DEVNAME $OST_MOUNT_OPTS
667         done
668     fi
669     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
670     mount_client $MOUNT
671     if [ "$MOUNT_2" ]; then
672         mount_client $MOUNT2
673     fi
674     sleep 5
675 }
676
677
678 ####### 
679 # General functions
680
681 check_network() {
682     local NETWORK=0
683     local WAIT=0
684     local MAX=$2
685     while [ $NETWORK -eq 0 ]; do
686         ping -c 1 -w 3 $1 > /dev/null
687         if [ $? -eq 0 ]; then
688             NETWORK=1
689         else
690             WAIT=$((WAIT + 5))
691             echo "waiting for $1, $((MAX - WAIT)) secs left"
692             sleep 5
693         fi
694         if [ $WAIT -gt $MAX ]; then
695             echo "Network not available"
696             exit 1
697         fi
698     done
699 }
700 check_port() {
701     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
702         sleep 9
703     done
704 }
705
706 no_dsh() {
707     shift
708     eval $@
709 }
710
711 comma_list() {
712     # the sed converts spaces to commas, but leaves the last space
713     # alone, so the line doesn't end with a comma.
714     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
715 }
716
717 absolute_path() {
718     (cd `dirname $1`; echo $PWD/`basename $1`)
719 }
720
721 ##################################
722 # OBD_FAIL funcs
723
724 drop_request() {
725 # OBD_FAIL_MDS_ALL_REQUEST_NET
726     RC=0
727     do_facet mds sysctl -w lustre.fail_loc=0x123
728     do_facet client "$1" || RC=$?
729     do_facet mds sysctl -w lustre.fail_loc=0
730     return $RC
731 }
732
733 drop_reply() {
734 # OBD_FAIL_MDS_ALL_REPLY_NET
735     RC=0
736     do_facet mds sysctl -w lustre.fail_loc=0x122
737     do_facet client "$@" || RC=$?
738     do_facet mds sysctl -w lustre.fail_loc=0
739     return $RC
740 }
741
742 drop_reint_reply() {
743 # OBD_FAIL_MDS_REINT_NET_REP
744     RC=0
745     do_facet mds sysctl -w lustre.fail_loc=0x119
746     do_facet client "$@" || RC=$?
747     do_facet mds sysctl -w lustre.fail_loc=0
748     return $RC
749 }
750
751 pause_bulk() {
752 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
753     RC=0
754     do_facet ost1 sysctl -w lustre.fail_loc=0x214
755     do_facet client "$1" || RC=$?
756     do_facet client "sync"
757     do_facet ost1 sysctl -w lustre.fail_loc=0
758     return $RC
759 }
760
761 drop_ldlm_cancel() {
762 #define OBD_FAIL_LDLM_CANCEL             0x304
763     RC=0
764     do_facet client sysctl -w lustre.fail_loc=0x304
765     do_facet client "$@" || RC=$?
766     do_facet client sysctl -w lustre.fail_loc=0
767     return $RC
768 }
769
770 drop_bl_callback() {
771 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
772     RC=0
773     do_facet client sysctl -w lustre.fail_loc=0x305
774     do_facet client "$@" || RC=$?
775     do_facet client sysctl -w lustre.fail_loc=0
776     return $RC
777 }
778
779 drop_ldlm_reply() {
780 #define OBD_FAIL_LDLM_REPLY              0x30c
781     RC=0
782     do_facet mds sysctl -w lustre.fail_loc=0x30c
783     do_facet client "$@" || RC=$?
784     do_facet mds sysctl -w lustre.fail_loc=0
785     return $RC
786 }
787
788 clear_failloc() {
789     facet=$1
790     pause=$2
791     sleep $pause
792     echo "clearing fail_loc on $facet"
793     do_facet $facet "sysctl -w lustre.fail_loc=0"
794 }
795
796 cancel_lru_locks() {
797     $LCTL mark "cancel_lru_locks $1 start"
798     for d in `find $LPROC/ldlm/namespaces | egrep -i $1`; do
799         [ -f $d/lru_size ] && echo clear > $d/lru_size
800         [ -f $d/lock_unused_count ] && grep [1-9] $d/lock_unused_count /dev/null
801     done
802     $LCTL mark "cancel_lru_locks $1 stop"
803 }
804
805
806 pgcache_empty() {
807     for a in /proc/fs/lustre/llite/*/dump_page_cache; do
808         if [ `wc -l $a | awk '{print $1}'` -gt 1 ]; then
809             echo there is still data in page cache $a ?
810             cat $a;
811             return 1;
812         fi
813     done
814     return 0
815 }
816
817 debugsave() {
818     DEBUGSAVE="$(sysctl -n lnet.debug)"
819 }
820
821 debugrestore() {
822     [ -n "$DEBUGSAVE" ] && sysctl -w lnet.debug="${DEBUGSAVE}"
823     DEBUGSAVE=""
824 }
825
826 ##################################
827 # Test interface 
828 error() {
829     local ERRLOG
830     sysctl -w lustre.fail_loc=0 2> /dev/null || true
831     log "${TESTSUITE} ${TESTNAME}: **** FAIL:" $@
832     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
833     echo "Dumping lctl log to $ERRLOG"
834     $LCTL dk $ERRLOG
835     debugrestore
836     exit 1
837 }
838
839 build_test_filter() {
840     [ "$ONLY" ] && log "only running test `echo $ONLY`"
841     for O in $ONLY; do
842         eval ONLY_${O}=true
843     done
844     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
845         log "skipping tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
846     for E in $EXCEPT $ALWAYS_EXCEPT; do
847         eval EXCEPT_${E}=true
848     done
849 }
850
851 _basetest() {
852     echo $*
853 }
854
855 basetest() {
856     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
857 }
858
859 run_test() {
860     export base=`basetest $1`
861     if [ ! -z "$ONLY" ]; then
862         testname=ONLY_$1
863         if [ ${!testname}x != x ]; then
864             run_one $1 "$2"
865             return $?
866         fi
867         testname=ONLY_$base
868         if [ ${!testname}x != x ]; then
869             run_one $1 "$2"
870             return $?
871         fi
872         echo -n "."
873         return 0
874     fi
875     testname=EXCEPT_$1
876     if [ ${!testname}x != x ]; then
877         log "skipping excluded test $1"
878         return 0
879     fi
880     testname=EXCEPT_$base
881     if [ ${!testname}x != x ]; then
882         log "skipping excluded test $1 (base $base)"
883         return 0
884     fi
885     run_one $1 "$2"
886     
887     return $?
888 }
889
890 EQUALS="======================================================================"
891 equals_msg() {
892     msg="$@"
893
894     local suffixlen=$((${#EQUALS} - ${#msg}))
895     [ $suffixlen -lt 5 ] && suffixlen=5
896     printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS
897 }
898
899 log() {
900     echo "$*"
901     lsmod | grep lnet > /dev/null || load_modules
902     $LCTL mark "$*" 2> /dev/null || true
903 }
904
905 pass() {
906     echo PASS $@
907 }
908
909 check_mds() {
910     FFREE=`cat /proc/fs/lustre/mds/*/filesfree`
911     FTOTAL=`cat /proc/fs/lustre/mds/*/filestotal`
912     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
913 }
914
915 run_one() {
916     testnum=$1
917     message=$2
918     tfile=f${testnum}
919     tdir=d${base}
920
921     # Pretty tests run faster.
922     equals_msg $testnum: $message
923
924     BEFORE=`date +%s`
925     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
926     #check_mds
927     export TESTNAME=test_$testnum
928     test_${testnum} || error "test_$testnum failed with $?"
929     #check_mds
930     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
931         error "LBUG/LASSERT detected"
932     pass "($((`date +%s` - $BEFORE))s)"
933     unset TESTNAME
934 }
935
936 canonical_path() {
937     (cd `dirname $1`; echo $PWD/`basename $1`)
938 }
939