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