Whamcloud - gitweb
b=15255
[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 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 IDENTITY_UPCALL=default
17 #export PDSH="pdsh -S -Rssh -w"
18
19 # eg, assert_env LUSTRE MDSNODES OSTNODES CLIENTS
20 assert_env() {
21     local failed=""
22     for name in $@; do
23         if [ -z "${!name}" ]; then
24             echo "$0: $name must be set"
25             failed=1
26         fi
27     done
28     [ $failed ] && exit 1 || true
29 }
30
31 usage() {
32     echo "usage: $0 [-r] [-f cfgfile]"
33     echo "       -r: reformat"
34
35     exit
36 }
37
38 print_summary () {
39     [ "$TESTSUITE" == "lfscktest" ] && return 0
40     [ -n "$ONLY" ] && echo "WARNING: ONLY is set to ${ONLY}."
41     local form="%-13s %-17s %s\n"
42     printf "$form" "status" "script" "skipped tests E(xcluded) S(low)"
43     echo "------------------------------------------------------------------------------------"
44     for O in $TESTSUITE_LIST; do
45         local skipped=""
46         local slow=""
47         local o=$(echo $O | tr "[:upper:]" "[:lower:]")
48         o=${o//_/-}
49         o=${o//tyn/tyN}
50         local log=${TMP}/${o}.log
51         [ -f $log ] && skipped=$(grep excluded $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
52         [ -f $log ] && slow=$(grep SLOW $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
53         [ "${!O}" = "done" ] && \
54             printf "$form" "Done" "$O" "E=$skipped" && \
55             [ -n "$slow" ] && printf "$form" "-" "-" "S=$slow"
56
57     done
58
59     for O in $TESTSUITE_LIST; do
60         [ "${!O}" = "no" ] && \
61             printf "$form" "Skipped" "$O" ""
62     done
63
64     for O in $TESTSUITE_LIST; do
65         [ "${!O}" = "done" -o "${!O}" = "no" ] || \
66             printf "$form" "UNFINISHED" "$O" ""
67     done
68 }
69
70 init_test_env() {
71     export LUSTRE=`absolute_path $LUSTRE`
72     export TESTSUITE=`basename $0 .sh`
73     export LTESTDIR=${LTESTDIR:-$LUSTRE/../ltest}
74
75     [ -d /r ] && export ROOT=${ROOT:-/r}
76     export TMP=${TMP:-$ROOT/tmp}
77     export TESTSUITELOG=${TMP}/${TESTSUITE}.log
78     export HOSTNAME=${HOSTNAME:-`hostname`}
79
80     export PATH=:$PATH:$LUSTRE/utils:$LUSTRE/utils/gss:$LUSTRE/tests
81     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
82     [ ! -f "$LCTL" ] && export LCTL=$(which lctl)
83     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
84     [ ! -f "$LFS" ] && export LFS=$(which lfs)
85     export L_GETIDENTITY=${L_GETIDENTITY:-"$LUSTRE/utils/l_getidentity"}
86     if [ ! -f "$L_GETIDENTITY" ]; then
87         if `which l_getidentity > /dev/null 2>&1`; then
88             export L_GETIDENTITY=$(which l_getidentity)
89         else
90             export L_GETIDENTITY=NONE
91         fi
92     fi
93     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
94     [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre)
95     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
96     [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre)
97     export CHECKSTAT="${CHECKSTAT:-"checkstat -v"} "
98     export FSYTPE=${FSTYPE:-"ldiskfs"}
99     export NAME=${NAME:-local}
100     export LPROC=/proc/fs/lustre
101     export LGSSD=${LGSSD:-"$LUSTRE/utils/gss/lgssd"}
102     [ "$GSS_PIPEFS" = "true" ] && [ ! -f "$LGSSD" ] && \
103         export LGSSD=$(which lgssd)
104     export LSVCGSSD=${LSVCGSSD:-"$LUSTRE/utils/gss/lsvcgssd"}
105     [ ! -f "$LSVCGSSD" ] && export LSVCGSSD=$(which lsvcgssd)
106     export KRB5DIR=${KRB5DIR:-"/usr/kerberos"}
107     export DIR2
108
109     if [ "$ACCEPTOR_PORT" ]; then
110         export PORT_OPT="--port $ACCEPTOR_PORT"
111     fi
112
113     case "x$SEC" in
114         xkrb5*)
115             echo "Using GSS/krb5 ptlrpc security flavor"
116             GSS=true
117             GSS_KRB5=true
118             ;;
119     esac
120
121     case "x$IDUP" in
122         xtrue)
123             IDENTITY_UPCALL=true
124             ;;
125         xfalse)
126             IDENTITY_UPCALL=false
127             ;;
128     esac
129
130     # Paths on remote nodes, if different
131     export RLUSTRE=${RLUSTRE:-$LUSTRE}
132     export RPWD=${RPWD:-$PWD}
133     export I_MOUNTED=${I_MOUNTED:-"no"}
134
135     # command line
136
137     while getopts "rvf:" opt $*; do
138         case $opt in
139             f) CONFIG=$OPTARG;;
140             r) REFORMAT=--reformat;;
141             v) VERBOSE=true;;
142             \?) usage;;
143         esac
144     done
145
146     shift $((OPTIND - 1))
147     ONLY=${ONLY:-$*}
148
149     [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
150
151 }
152
153 load_module() {
154     EXT=".ko"
155     module=$1
156     shift
157     BASE=`basename $module $EXT`
158     lsmod | grep -q ${BASE} || \
159       if [ -f ${LUSTRE}/${module}${EXT} ]; then
160         insmod ${LUSTRE}/${module}${EXT} $@
161     else
162         # must be testing a "make install" or "rpm" installation
163         # note failed to load ptlrpc_gss is considered not fatal
164         if [ "$BASE" == "ptlrpc_gss" ]; then
165             modprobe $BASE $@ 2>/dev/null || echo "gss/krb5 is not supported"
166         else
167             modprobe $BASE $@
168         fi
169     fi
170 }
171
172 load_modules() {
173     if [ -n "$MODPROBE" ]; then
174         # use modprobe
175     return 0
176     fi
177     if [ "$HAVE_MODULES" = true ]; then
178     # we already loaded
179         return 0
180     fi
181     HAVE_MODULES=true
182
183     echo Loading modules from $LUSTRE
184     load_module ../lnet/libcfs/libcfs
185     [ "$PTLDEBUG" ] && sysctl -w lnet.debug=$PTLDEBUG
186     [ "$SUBSYSTEM" ] && sysctl -w lnet.subsystem_debug=${SUBSYSTEM# }
187     [ -f /etc/modprobe.conf ] && MODPROBECONF=/etc/modprobe.conf
188     [ -f /etc/modprobe.d/Lustre ] && MODPROBECONF=/etc/modprobe.d/Lustre
189     [ -z "$LNETOPTS" -a -n "$MODPROBECONF" ] && \
190         LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
191     echo "lnet options: '$LNETOPTS'"
192     # note that insmod will ignore anything in modprobe.conf
193     load_module ../lnet/lnet/lnet $LNETOPTS
194     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
195     load_module ../lnet/klnds/$LNETLND
196     load_module lvfs/lvfs
197     load_module obdclass/obdclass
198     load_module ptlrpc/ptlrpc
199     load_module ptlrpc/gss/ptlrpc_gss
200     # Now, some modules depend on lquota without USE_QUOTA check,
201     # will fix later. Disable check "$USE_QUOTA" = "yes" temporary.
202     #[ "$USE_QUOTA" = "yes" ] && load_module quota/lquota
203     load_module quota/lquota
204     load_module fid/fid
205     load_module fld/fld
206     load_module lmv/lmv
207     load_module mdc/mdc
208     load_module osc/osc
209     load_module lov/lov
210     load_module mgc/mgc
211     if [ -z "$CLIENTONLY" ] && [ -z "$CLIENTMODSONLY" ]; then
212         grep -q crc16 /proc/kallsyms || { modprobe crc16 2>/dev/null || true; }
213         [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
214         load_module mgs/mgs
215         load_module mds/mds
216         load_module mdd/mdd
217         load_module mdt/mdt
218         load_module lvfs/fsfilt_$FSTYPE
219         load_module cmm/cmm
220         load_module osd/osd
221         load_module ost/ost
222         load_module obdfilter/obdfilter
223     fi
224
225     load_module llite/lustre
226     load_module llite/llite_lloop
227     rm -f $TMP/ogdb-$HOSTNAME
228     OGDB=$TMP
229     [ -d /r ] && OGDB="/r/tmp"
230     $LCTL modules > $OGDB/ogdb-$HOSTNAME
231
232     # 'mount' doesn't look in $PATH, just sbin
233     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
234 }
235
236 RMMOD=rmmod
237 if [ `uname -r | cut -c 3` -eq 4 ]; then
238     RMMOD="modprobe -r"
239 fi
240
241 wait_for_lnet() {
242     local UNLOADED=0
243     local WAIT=0
244     local MAX=60
245     MODULES=$($LCTL modules | awk '{ print $2 }')
246     while [ -n "$MODULES" ]; do
247     sleep 5
248     $RMMOD $MODULES > /dev/null 2>&1 || true
249     MODULES=$($LCTL modules | awk '{ print $2 }')
250         if [ -z "$MODULES" ]; then
251         return 0
252         else
253             WAIT=$((WAIT + 5))
254             echo "waiting, $((MAX - WAIT)) secs left"
255         fi
256         if [ $WAIT -eq $MAX ]; then
257             echo "LNET modules $MODULES will not unload"
258         lsmod
259             return 3
260         fi
261     done
262 }
263
264 unload_modules() {
265     wait_exit_ST client # bug 12845
266
267     lsmod | grep libcfs > /dev/null && $LCTL dl
268     local MODULES=$($LCTL modules | awk '{ print $2 }' | grep -v libcfs) || true
269     $RMMOD $MODULES > /dev/null 2>&1 || true
270      # do it again, in case we tried to unload ksocklnd too early
271     MODULES=$($LCTL modules | awk '{ print $2 }' | grep -v libcfs) || true
272     [ -n "$MODULES" ] && $RMMOD $MODULES > /dev/null 2>&1 || true
273     lsmod | grep libcfs > /dev/null && $LCTL dk $TMP/debug
274     $RMMOD libcfs
275     MODULES=$($LCTL modules | awk '{ print $2 }')
276     if [ -n "$MODULES" ]; then
277         echo "Modules still loaded: "
278         echo $MODULES 
279         if [ "$(lctl dl)" ]; then
280             echo "Lustre still loaded"
281             lctl dl || true
282             lsmod
283             return 2
284         else
285             echo "Lustre stopped but LNET is still loaded, waiting..."
286             wait_for_lnet || return 3
287         fi
288
289     fi
290     HAVE_MODULES=false
291
292     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd mem.*leaked" || true)
293     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
294     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
295         echo "$LEAK_LUSTRE" 1>&2
296         echo "$LEAK_PORTALS" 1>&2
297         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
298         echo "Memory leaks detected"
299         [ -n "$IGNORE_LEAK" ] && echo "ignoring leaks" && return 0
300         return 254
301     fi
302     echo "modules unloaded."
303     return 0
304 }
305
306 check_gss_daemon_facet() {
307     facet=$1
308     dname=$2
309
310     num=`do_facet $facet ps -o cmd -C $dname | grep $dname | wc -l`
311     if [ $num -ne 1 ]; then
312         echo "$num instance of $dname on $facet"
313         return 1
314     fi
315     return 0
316 }
317
318 send_sigint() {
319     local facet=$1
320     shift
321     do_facet $facet "killall -2 $@ 2>/dev/null || true"
322 }
323
324 start_gss_daemons() {
325     # starting on MDT
326     for num in `seq $MDSCOUNT`; do
327         do_facet mds$num "$LSVCGSSD -v"
328         if $GSS_PIPEFS; then
329             do_facet mds$num "$LGSSD -v"
330         fi
331     done
332     # starting on OSTs
333     for num in `seq $OSTCOUNT`; do
334         do_facet ost$num "$LSVCGSSD -v"
335     done
336     # starting on client
337     # FIXME: is "client" the right facet name?
338     if $GSS_PIPEFS; then
339         do_facet client "$LGSSD -v"
340     fi
341
342     # wait daemons entering "stable" status
343     sleep 5
344
345     #
346     # check daemons are running
347     #
348     for num in `seq $MDSCOUNT`; do
349         check_gss_daemon_facet mds$num lsvcgssd
350         if $GSS_PIPEFS; then
351             check_gss_daemon_facet mds$num lgssd
352         fi
353     done
354     for num in `seq $OSTCOUNT`; do
355         check_gss_daemon_facet ost$num lsvcgssd
356     done
357     if $GSS_PIPEFS; then
358         check_gss_daemon_facet client lgssd
359     fi
360 }
361
362 stop_gss_daemons() {
363     for num in `seq $MDSCOUNT`; do
364         send_sigint mds$num lsvcgssd lgssd
365     done
366     for num in `seq $OSTCOUNT`; do
367         send_sigint ost$num lsvcgssd
368     done
369     send_sigint client lgssd
370 }
371
372 init_gss() {
373     if $GSS; then
374         start_gss_daemons
375     fi
376 }
377
378 cleanup_gss() {
379     if $GSS; then
380         stop_gss_daemons
381         # maybe cleanup credential cache?
382     fi
383 }
384
385 mdsdevlabel() {
386     local num=$1
387     local device=`mdsdevname $num`
388     local label=`do_facet mds$num "e2label ${device}" | grep -v "CMD: "`
389     echo -n $label
390 }
391
392 ostdevlabel() {
393     local num=$1
394     local device=`ostdevname $num`
395     local label=`do_facet ost$num "e2label ${device}" | grep -v "CMD: "`
396     echo -n $label
397 }
398
399 # Facet functions
400 mount_facet() {
401     local facet=$1
402     shift
403     local dev=${facet}_dev
404     local opt=${facet}_opt
405     echo "Starting ${facet}: ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}"
406     do_facet ${facet} mount -t lustre ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}     
407     RC=${PIPESTATUS[0]}
408     if [ $RC -ne 0 ]; then
409         echo "mount -t lustre $@ ${device} ${MOUNT%/*}/${facet}"
410         echo "Start of ${device} on ${facet} failed ${RC}"
411     else
412         do_facet ${facet} "sysctl -w lnet.debug=$PTLDEBUG; \
413             sysctl -w lnet.subsystem_debug=${SUBSYSTEM# }; \
414             sysctl -w lnet.debug_mb=${DEBUG_SIZE}; \
415             sync"
416     fi
417     return $RC
418 }
419
420 # start facet device options
421 start() {
422     facet=$1
423     shift
424     device=$1
425     shift
426     eval export ${facet}_dev=${device}
427     eval export ${facet}_opt=\"$@\"
428     do_facet ${facet} mkdir -p ${MOUNT%/*}/${facet}
429     mount_facet ${facet}
430     RC=$?
431     if [ $RC -eq 0 ]; then
432         label=$(do_facet ${facet} "e2label ${device}")
433         [ -z "$label" ] && echo no label for ${device} && exit 1
434         eval export ${facet}_svc=${label}
435         echo Started ${label}
436     fi
437     return $RC
438 }
439
440 stop() {
441     local running
442     facet=$1
443     shift
444     HOST=`facet_active_host $facet`
445     [ -z $HOST ] && echo stop: no host for $facet && return 0
446
447     running=$(do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts") || true
448     if [ ${running} -ne 0 ]; then
449         echo "Stopping ${MOUNT%/*}/${facet} (opts:$@)"
450         do_facet ${facet} umount -d $@ ${MOUNT%/*}/${facet}
451     fi
452
453     # umount should block, but we should wait for unrelated obd's
454     # like the MGS or MGC to also stop.
455     wait_exit_ST ${facet}
456 }
457
458 zconf_mount() {
459     local OPTIONS
460     local client=$1
461     local mnt=$2
462     # Only supply -o to mount if we have options
463     if [ -n "$MOUNTOPT" ]; then
464         OPTIONS="-o $MOUNTOPT"
465     fi
466     local device=$MGSNID:/$FSNAME
467     if [ -z "$mnt" -o -z "$FSNAME" ]; then
468         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
469         exit 1
470     fi
471
472     echo "Starting client: $client: $OPTIONS $device $mnt"
473     do_node $client mkdir -p $mnt
474     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
475
476     do_node $client "sysctl -w lnet.debug=$PTLDEBUG;
477         sysctl -w lnet.subsystem_debug=${SUBSYSTEM# };
478         sysctl -w lnet.debug_mb=${DEBUG_SIZE}"
479     [ -d /r ] && $LCTL modules > /r/tmp/ogdb-$HOSTNAME
480     return 0
481 }
482
483 zconf_umount() {
484     local client=$1
485     local mnt=$2
486     [ "$3" ] && force=-f
487     local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
488     if [ $running -ne 0 ]; then
489         echo "Stopping client $client $mnt (opts:$force)"
490         lsof | grep "$mnt" || true
491         do_node $client umount $force $mnt
492     fi
493 }
494
495 zconf_mount_clients() {
496     local clients=$1
497     local mnt=$2
498
499     echo "Mounting clients: $clients"
500     local client
501     for client in ${clients//,/ }; do
502         zconf_mount $client $mnt  || true
503     done
504 }
505
506 zconf_umount_clients() {
507     local clients=$1
508     local mnt=$2
509     [ "$3" ] && force=-f
510
511     echo "Umounting clients: $clients"
512     local client
513     for client in ${clients//,/ }; do
514         zconf_umount $client $mnt $force || true
515     done
516 }
517
518 shutdown_facet() {
519     facet=$1
520     if [ "$FAILURE_MODE" = HARD ]; then
521         $POWER_DOWN `facet_active_host $facet`
522         sleep 2
523     elif [ "$FAILURE_MODE" = SOFT ]; then
524         stop $facet
525     fi
526 }
527
528 reboot_facet() {
529     facet=$1
530     if [ "$FAILURE_MODE" = HARD ]; then
531         $POWER_UP `facet_active_host $facet`
532     else
533         sleep 10
534     fi
535 }
536
537 # verify that lustre actually cleaned up properly
538 cleanup_check() {
539     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
540         error "LBUG/LASSERT detected"
541     BUSY=`dmesg | grep -i destruct || true`
542     if [ "$BUSY" ]; then
543         echo "$BUSY" 1>&2
544         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
545         exit 205
546     fi
547     LEAK_LUSTRE=`dmesg | tail -n 30 | grep "obd mem.*leaked" || true`
548     LEAK_PORTALS=`dmesg | tail -n 20 | grep "Portals memory leaked" || true`
549     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
550         echo "$0: $LEAK_LUSTRE" 1>&2
551         echo "$0: $LEAK_PORTALS" 1>&2
552         echo "$0: Memory leak(s) detected..." 1>&2
553         mv $TMP/debug $TMP/debug-leak.`date +%s`
554         exit 204
555     fi
556
557     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
558         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
559
560     if [ "`/sbin/lsmod 2>&1 | egrep 'lnet|libcfs'`" ]; then
561         echo "$0: modules still loaded..." 1>&2
562         /sbin/lsmod 1>&2
563         return 203
564     fi
565     return 0
566 }
567
568 wait_delete_completed () {
569     local TOTALPREV=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
570             $LPROC/osc/*/kbytesavail`
571
572     local WAIT=0
573     local MAX_WAIT=20
574     while [ "$WAIT" -ne "$MAX_WAIT" ]; do
575         sleep 1
576         TOTAL=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
577             $LPROC/osc/*/kbytesavail`
578         [ "$TOTAL" -eq "$TOTALPREV" ] && break
579         echo "Waiting delete completed ... prev: $TOTALPREV current: $TOTAL "
580         TOTALPREV=$TOTAL
581         WAIT=$(( WAIT + 1))
582     done
583     echo "Delete completed."
584 }
585
586 wait_for_host() {
587     HOST=$1
588     check_network "$HOST" 900
589     while ! do_node $HOST "ls -d $LUSTRE " > /dev/null; do sleep 5; done
590 }
591
592 wait_for() {
593     facet=$1
594     HOST=`facet_active_host $facet`
595     wait_for_host $HOST
596 }
597
598 wait_mds_recovery_done () {
599     local timeout=`do_facet mds sysctl -n lustre.timeout`
600 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2)
601 # as we are in process of changing obd_timeout in different ways
602 # let's set MAX longer than that
603     MAX=$(( timeout * 4 ))
604     WAIT=0
605     while [ $WAIT -lt $MAX ]; do
606         STATUS=`do_facet $SINGLEMDS grep status /proc/fs/lustre/mdt/*-MDT*/recovery_status`
607         echo $STATUS | grep COMPLETE && return 0
608         sleep 5
609         WAIT=$((WAIT + 5))
610         echo "Waiting $(($MAX - $WAIT)) secs for MDS recovery done"
611     done
612     echo "MDS recovery not done in $MAX sec"
613     return 1
614 }
615
616 wait_exit_ST () {
617     local facet=$1
618
619     local WAIT=0
620     local INTERVAL=1
621     # conf-sanity 31 takes a long time cleanup
622     while [ $WAIT -lt 300 ]; do
623         running=$(do_facet ${facet} "lsmod | grep lnet > /dev/null && lctl dl | grep ' ST '") || true
624         [ -z "${running}" ] && return 0
625         echo "waited $WAIT for${running}"
626         [ $INTERVAL -lt 64 ] && INTERVAL=$((INTERVAL + INTERVAL))
627         sleep $INTERVAL
628         WAIT=$((WAIT + INTERVAL))
629     done
630     echo "service didn't stop after $WAIT seconds.  Still running:"
631     echo ${running}
632     return 1
633 }
634
635 client_df() {
636     # not every config has many clients
637     if [ ! -z "$CLIENTS" ]; then
638         $PDSH $CLIENTS "df $MOUNT" > /dev/null
639     else
640         df $MOUNT > /dev/null
641     fi
642 }
643
644 client_reconnect() {
645     uname -n >> $MOUNT/recon
646     if [ ! -z "$CLIENTS" ]; then
647         $PDSH $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
648     fi
649     echo Connected clients:
650     cat $MOUNT/recon
651     ls -l $MOUNT/recon > /dev/null
652     rm $MOUNT/recon
653 }
654
655 facet_failover() {
656     facet=$1
657     echo "Failing $facet on node `facet_active_host $facet`"
658     shutdown_facet $facet
659     reboot_facet $facet
660     client_df &
661     DFPID=$!
662     echo "df pid is $DFPID"
663     change_active $facet
664     TO=`facet_active_host $facet`
665     echo "Failover $facet to $TO"
666     wait_for $facet
667     mount_facet $facet || error "Restart of $facet failed"
668 }
669
670 obd_name() {
671     local facet=$1
672 }
673
674 replay_barrier() {
675     local facet=$1
676     do_facet $facet sync
677     df $MOUNT
678     local svc=${facet}_svc
679     do_facet $facet $LCTL --device %${!svc} readonly
680     do_facet $facet $LCTL --device %${!svc} notransno
681     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
682     $LCTL mark "local REPLAY BARRIER on ${!svc}"
683 }
684
685 replay_barrier_nodf() {
686     local facet=$1    echo running=${running}
687     do_facet $facet sync
688     local svc=${facet}_svc
689     echo Replay barrier on ${!svc}
690     do_facet $facet $LCTL --device %${!svc} readonly
691     do_facet $facet $LCTL --device %${!svc} notransno
692     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
693     $LCTL mark "local REPLAY BARRIER on ${!svc}"
694 }
695
696 mds_evict_client() {
697     UUID=`cat /proc/fs/lustre/mdc/${mds1_svc}-mdc-*/uuid`
698     do_facet mds1 "echo $UUID > /proc/fs/lustre/mdt/${mds1_svc}/evict_client"
699 }
700
701 ost_evict_client() {
702     UUID=`grep ${ost1_svc}-osc- $LPROC/devices | egrep -v 'MDT' | awk '{print $5}'`
703     do_facet ost1 "echo $UUID > /proc/fs/lustre/obdfilter/${ost1_svc}/evict_client"
704 }
705
706 fail() {
707     facet_failover $* || error "failover: $?"
708     df $MOUNT || error "post-failover df: $?"
709 }
710
711 fail_nodf() {
712         local facet=$1
713         facet_failover $facet
714 }
715
716 fail_abort() {
717     local facet=$1
718     stop $facet
719     change_active $facet
720     mount_facet $facet -o abort_recovery
721     df $MOUNT || echo "first df failed: $?"
722     sleep 1
723     df $MOUNT || error "post-failover df: $?"
724 }
725
726 do_lmc() {
727     echo There is no lmc.  This is mountconf, baby.
728     exit 1
729 }
730
731 h2gm () {
732     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
733         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
734         echo $ID"@gm"
735     fi
736 }
737
738 h2name_or_ip() {
739     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
740         echo $1"@$2"
741     fi
742 }
743
744 h2ptl() {
745    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
746        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
747        if [ -z "$ID" ]; then
748            echo "Could not get a ptl id for $1..."
749            exit 1
750        fi
751        echo $ID"@ptl"
752    fi
753 }
754 declare -fx h2ptl
755
756 h2tcp() {
757     h2name_or_ip "$1" "tcp"
758 }
759 declare -fx h2tcp
760
761 h2elan() {
762     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
763         if type __h2elan >/dev/null 2>&1; then
764             ID=$(__h2elan $1)
765         else
766             ID=`echo $1 | sed 's/[^0-9]*//g'`
767         fi
768         echo $ID"@elan"
769     fi
770 }
771 declare -fx h2elan
772
773 h2openib() {
774     h2name_or_ip "$1" "openib"
775 }
776 declare -fx h2openib
777
778 h2o2ib() {
779     h2name_or_ip "$1" "o2ib"
780 }
781 declare -fx h2o2ib
782
783 facet_host() {
784     local facet=$1
785     varname=${facet}_HOST
786     if [ -z "${!varname}" ]; then
787         if [ "${facet:0:3}" == "ost" ]; then
788             eval ${facet}_HOST=${ost_HOST}
789         fi
790     fi
791     echo -n ${!varname}
792 }
793
794 facet_active() {
795     local facet=$1
796     local activevar=${facet}active
797
798     if [ -f ./${facet}active ] ; then
799         source ./${facet}active
800     fi
801
802     active=${!activevar}
803     if [ -z "$active" ] ; then
804         echo -n ${facet}
805     else
806         echo -n ${active}
807     fi
808 }
809
810 facet_active_host() {
811     local facet=$1
812     local active=`facet_active $facet`
813     if [ "$facet" == client ]; then
814         echo $HOSTNAME
815     else
816         echo `facet_host $active`
817     fi
818 }
819
820 change_active() {
821     local facet=$1
822     failover=${facet}failover
823     host=`facet_host $failover`
824     [ -z "$host" ] && return
825     curactive=`facet_active $facet`
826     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
827         eval export ${facet}active=$facet
828     else
829         eval export ${facet}active=$failover
830     fi
831     # save the active host for this facet
832     activevar=${facet}active
833     echo "$activevar=${!activevar}" > ./$activevar
834 }
835
836 do_node() {
837     HOST=$1
838     shift
839     local myPDSH=$PDSH
840     if [ "$HOST" = "$HOSTNAME" ]; then
841         myPDSH="no_dsh"
842     elif [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" ]; then
843         echo "cannot run remote command on $HOST with $myPDSH"
844         return 128
845     fi
846     if $VERBOSE; then
847         echo "CMD: $HOST $@" >&2
848         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
849     fi
850
851     if [ "$myPDSH" = "rsh" ]; then
852 # we need this because rsh does not return exit code of an executed command
853         local command_status="$TMP/cs"
854         rsh $HOST ":> $command_status"
855         rsh $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin;
856                     cd $RPWD; sh -c \"$@\") || 
857                     echo command failed >$command_status"
858         [ -n "$($myPDSH $HOST cat $command_status)" ] && return 1 || true
859         return 0
860     fi
861     $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
862     return ${PIPESTATUS[0]}
863 }
864
865 do_facet() {
866     facet=$1
867     shift
868     HOST=`facet_active_host $facet`
869     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
870     do_node $HOST $@
871 }
872
873 add() {
874     local facet=$1
875     shift
876     # make sure its not already running
877     stop ${facet} -f
878     rm -f ${facet}active
879     do_facet ${facet} $MKFS $*
880 }
881
882 ostdevname() {
883     num=$1
884     DEVNAME=OSTDEV$num
885     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
886     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
887     echo -n $DEVPTR
888 }
889
890 mdsdevname() {
891     num=$1
892     DEVNAME=MDSDEV$num
893     #if $MDSDEVn isn't defined, default is $MDSDEVBASE + num
894     eval DEVPTR=${!DEVNAME:=${MDSDEVBASE}${num}}
895     echo -n $DEVPTR
896 }
897
898 ########
899 ## MountConf setup
900
901 stopall() {
902     # make sure we are using the primary server, so test-framework will
903     # be able to clean up properly.
904     activemds=`facet_active mds1`
905     if [ $activemds != "mds1" ]; then
906         fail mds1
907     fi
908
909     # assume client mount is local
910     grep " $MOUNT " /proc/mounts && zconf_umount $HOSTNAME $MOUNT $*
911     grep " $MOUNT2 " /proc/mounts && zconf_umount $HOSTNAME $MOUNT2 $*
912
913     if [ -n "$CLIENTS" ]; then
914             zconf_umount_clients $CLIENTS $MOUNT "$*" || true
915             zconf_umount_clients $CLIENTS $MOUNT2 "$*" || true
916     fi
917
918     [ "$CLIENTONLY" ] && return
919     for num in `seq $MDSCOUNT`; do
920         stop mds$num -f
921     done
922     for num in `seq $OSTCOUNT`; do
923         stop ost$num -f
924     done
925     return 0
926 }
927
928 cleanupall() {
929     stopall $*
930     unload_modules
931     cleanup_gss
932 }
933
934 mdsmkfsopts()
935 {
936     local nr=$1
937     test $nr = 1 && echo -n $MDS_MKFS_OPTS || echo -n $MDSn_MKFS_OPTS
938 }
939
940 formatall() {
941     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
942
943     if [ ! -z $SEC ]; then
944         MDS_MKFS_OPTS="$MDS_MKFS_OPTS --param srpc.flavor.default=$SEC"
945         OST_MKFS_OPTS="$OST_MKFS_OPTS --param srpc.flavor.default=$SEC"
946     fi
947
948     stopall
949     # We need ldiskfs here, may as well load them all
950     load_modules
951     [ "$CLIENTONLY" ] && return
952     echo "Formatting mdts, osts"
953     for num in `seq $MDSCOUNT`; do
954         echo "Format mds$num: $(mdsdevname $num)"
955         if $VERBOSE; then
956             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` || exit 9
957         else
958             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` > /dev/null || exit 9
959         fi
960     done
961
962     for num in `seq $OSTCOUNT`; do
963         echo "Format ost$num: $(ostdevname $num)"
964         if $VERBOSE; then
965             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` || exit 10
966         else
967             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` > /dev/null || exit 10
968         fi
969     done
970 }
971
972 mount_client() {
973     grep " $1 " /proc/mounts || zconf_mount $HOSTNAME $*
974 }
975
976 umount_client() {
977     grep " $1 " /proc/mounts && zconf_umount `hostname` $*
978 }
979
980 # return value:
981 # 0: success, the old identity set already.
982 # 1: success, the old identity does not set.
983 # 2: fail.
984 switch_identity() {
985     local num=$1
986     local switch=$2
987     local j=`expr $num - 1`
988     local MDT="`do_facet mds$num find $LPROC/mdt/ -name \*MDT\*$j -printf %f 2>/dev/null || true`"
989
990     if [ -z "$MDT" ]; then
991         return 2
992     fi
993
994     local old="`do_facet mds$num cat $LPROC/mdt/$MDT/identity_upcall`"
995
996     if $switch; then
997         do_facet mds$num "echo \"$L_GETIDENTITY\" > $LPROC/mdt/$MDT/identity_upcall"
998     else
999         do_facet mds$num "echo \"NONE\" > $LPROC/mdt/$MDT/identity_upcall"
1000     fi
1001
1002     do_facet mds$num "echo \"-1\" > $LPROC/mdt/$MDT/identity_flush"
1003
1004     if [ $old = "NONE" ]; then
1005         return 1
1006     else
1007         return 0
1008     fi
1009 }
1010
1011 remount_client()
1012 {
1013         zconf_umount `hostname` $1 || error "umount failed"
1014         zconf_mount `hostname` $1 || error "mount failed"
1015 }
1016
1017 set_obd_timeout() {
1018     local facet=$1
1019     local timeout=$2
1020
1021     do_facet $facet lsmod | grep -q obdclass || \
1022         do_facet $facet "modprobe obdclass"
1023
1024     do_facet $facet "sysctl -w lustre.timeout=$timeout"
1025 }
1026
1027 setupall() {
1028     load_modules
1029     init_gss
1030     if [ -z "$CLIENTONLY" ]; then
1031         echo "Setup mdts, osts"
1032         for num in `seq $MDSCOUNT`; do
1033             DEVNAME=$(mdsdevname $num)
1034             echo $REFORMAT | grep -q "reformat" \
1035             || do_facet mds$num "$TUNEFS --writeconf $DEVNAME"
1036             set_obd_timeout mds$num $TIMEOUT
1037             start mds$num $DEVNAME $MDS_MOUNT_OPTS
1038             if [ $IDENTITY_UPCALL != "default" ]; then
1039                 switch_identity $num $IDENTITY_UPCALL
1040             fi
1041         done
1042         for num in `seq $OSTCOUNT`; do
1043             DEVNAME=$(ostdevname $num)
1044             set_obd_timeout ost$num $TIMEOUT
1045             start ost$num $DEVNAME $OST_MOUNT_OPTS
1046         done
1047     fi
1048     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
1049     mount_client $MOUNT
1050     [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT
1051
1052     if [ "$MOUNT_2" ]; then
1053         mount_client $MOUNT2
1054         [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT2
1055     fi
1056
1057     # by remounting mdt before ost, initial connect from mdt to ost might
1058     # timeout because ost is not ready yet. wait some time to its fully
1059     # recovery. initial obd_connect timeout is 5s; in GSS case it's preceeded
1060     # by a context negotiation rpc with $TIMEOUT.
1061     # FIXME better by monitoring import status.
1062     if $GSS; then
1063         sleep $((TIMEOUT + 5))
1064     else
1065         sleep 5
1066     fi
1067 }
1068
1069 mounted_lustre_filesystems() {
1070         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
1071 }
1072
1073 check_and_setup_lustre() {
1074     MOUNTED="`mounted_lustre_filesystems`"
1075     if [ -z "$MOUNTED" ]; then
1076         [ "$REFORMAT" ] && formatall
1077         setupall
1078         MOUNTED="`mounted_lustre_filesystems`"
1079         [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
1080         export I_MOUNTED=yes
1081     fi
1082     if [ "$ONLY" == "setup" ]; then
1083         exit 0
1084     fi
1085 }
1086
1087 cleanup_and_setup_lustre() {
1088     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
1089         sysctl -w lnet.debug=0 || true
1090         cleanupall
1091         if [ "$ONLY" == "cleanup" ]; then
1092             exit 0
1093         fi
1094     fi
1095     check_and_setup_lustre
1096 }
1097
1098 check_and_cleanup_lustre() {
1099     if [ "`mount | grep $MOUNT`" ]; then
1100         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]*
1101     fi
1102     if [ "$I_MOUNTED" = "yes" ]; then
1103         cleanupall -f || error "cleanup failed"
1104     fi
1105     unset I_MOUNTED
1106 }
1107
1108 #######
1109 # General functions
1110
1111 check_network() {
1112     local NETWORK=0
1113     local WAIT=0
1114     local MAX=$2
1115     while [ $NETWORK -eq 0 ]; do
1116         ping -c 1 -w 3 $1 > /dev/null
1117         if [ $? -eq 0 ]; then
1118             NETWORK=1
1119         else
1120             WAIT=$((WAIT + 5))
1121             echo "waiting for $1, $((MAX - WAIT)) secs left"
1122             sleep 5
1123         fi
1124         if [ $WAIT -gt $MAX ]; then
1125             echo "Network not available"
1126             exit 1
1127         fi
1128     done
1129 }
1130 check_port() {
1131     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
1132         sleep 9
1133     done
1134 }
1135
1136 no_dsh() {
1137     shift
1138     eval $@
1139 }
1140
1141 comma_list() {
1142     # the sed converts spaces to commas, but leaves the last space
1143     # alone, so the line doesn't end with a comma.
1144     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
1145 }
1146
1147 absolute_path() {
1148     (cd `dirname $1`; echo $PWD/`basename $1`)
1149 }
1150
1151 ##################################
1152 # OBD_FAIL funcs
1153
1154 drop_request() {
1155 # OBD_FAIL_MDS_ALL_REQUEST_NET
1156     RC=0
1157     do_facet mds sysctl -w lustre.fail_loc=0x123
1158     do_facet client "$1" || RC=$?
1159     do_facet mds sysctl -w lustre.fail_loc=0
1160     return $RC
1161 }
1162
1163 drop_reply() {
1164 # OBD_FAIL_MDS_ALL_REPLY_NET
1165     RC=0
1166     do_facet mds sysctl -w lustre.fail_loc=0x122
1167     do_facet client "$@" || RC=$?
1168     do_facet mds sysctl -w lustre.fail_loc=0
1169     return $RC
1170 }
1171
1172 drop_reint_reply() {
1173 # OBD_FAIL_MDS_REINT_NET_REP
1174     RC=0
1175     do_facet mds sysctl -w lustre.fail_loc=0x119
1176     do_facet client "$@" || RC=$?
1177     do_facet mds sysctl -w lustre.fail_loc=0
1178     return $RC
1179 }
1180
1181 pause_bulk() {
1182 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
1183     RC=0
1184     do_facet ost1 sysctl -w lustre.fail_loc=0x214
1185     do_facet client "$1" || RC=$?
1186     do_facet client "sync"
1187     do_facet ost1 sysctl -w lustre.fail_loc=0
1188     return $RC
1189 }
1190
1191 drop_ldlm_cancel() {
1192 #define OBD_FAIL_LDLM_CANCEL             0x304
1193     RC=0
1194     do_facet client sysctl -w lustre.fail_loc=0x304
1195     do_facet client "$@" || RC=$?
1196     do_facet client sysctl -w lustre.fail_loc=0
1197     return $RC
1198 }
1199
1200 drop_bl_callback() {
1201 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
1202     RC=0
1203     do_facet client sysctl -w lustre.fail_loc=0x305
1204     do_facet client "$@" || RC=$?
1205     do_facet client sysctl -w lustre.fail_loc=0
1206     return $RC
1207 }
1208
1209 drop_ldlm_reply() {
1210 #define OBD_FAIL_LDLM_REPLY              0x30c
1211     RC=0
1212     do_facet mds sysctl -w lustre.fail_loc=0x30c
1213     do_facet client "$@" || RC=$?
1214     do_facet mds sysctl -w lustre.fail_loc=0
1215     return $RC
1216 }
1217
1218 clear_failloc() {
1219     facet=$1
1220     pause=$2
1221     sleep $pause
1222     echo "clearing fail_loc on $facet"
1223     do_facet $facet "sysctl -e -w lustre.fail_loc=0"
1224 }
1225
1226 set_nodes_failloc () {
1227     local nodes=$1
1228     local node
1229
1230     for node in $nodes ; do
1231         do_node $node sysctl -w lustre.fail_loc=$2
1232     done
1233 }
1234
1235 cancel_lru_locks() {
1236     $LCTL mark "cancel_lru_locks $1 start"
1237     for d in `find $LPROC/ldlm/namespaces | egrep -i $1`; do
1238         [ -f $d/lru_size ] && echo clear > $d/lru_size
1239         [ -f $d/lock_unused_count ] && grep [1-9] $d/lock_unused_count /dev/null
1240     done
1241     $LCTL mark "cancel_lru_locks $1 stop"
1242 }
1243
1244 default_lru_size()
1245 {
1246         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
1247         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
1248         echo "$DEFAULT_LRU_SIZE"
1249 }
1250
1251 lru_resize_enable()
1252 {
1253         NS=$1
1254         test "x$NS" = "x" && NS="mdc"
1255         for F in $LPROC/ldlm/namespaces/*$NS*/lru_size; do
1256                 D=$(dirname $F)
1257                 log "Enable lru resize for $(basename $D)"
1258                 echo "0" > $F
1259         done
1260 }
1261
1262 lru_resize_disable()
1263 {
1264         NS=$1
1265         test "x$NS" = "x" && NS="mdc"
1266         for F in $LPROC/ldlm/namespaces/*$NS*/lru_size; do
1267                 D=$(dirname $F)
1268                 log "Disable lru resize for $(basename $D)"
1269                 DEFAULT_LRU_SIZE=$(default_lru_size)
1270                 echo "$DEFAULT_LRU_SIZE" > $F
1271         done
1272 }
1273
1274 pgcache_empty() {
1275     for a in /proc/fs/lustre/llite/*/dump_page_cache; do
1276         if [ `wc -l $a | awk '{print $1}'` -gt 1 ]; then
1277             echo there is still data in page cache $a ?
1278             cat $a;
1279             return 1;
1280         fi
1281     done
1282     return 0
1283 }
1284
1285 debugsave() {
1286     DEBUGSAVE="$(sysctl -n lnet.debug)"
1287 }
1288
1289 debugrestore() {
1290     [ -n "$DEBUGSAVE" ] && sysctl -w lnet.debug="${DEBUGSAVE}"
1291     DEBUGSAVE=""
1292 }
1293
1294 ##################################
1295 # Test interface
1296 ##################################
1297
1298 error_noexit() {
1299     local TYPE=${TYPE:-"FAIL"}
1300     local ERRLOG
1301     sysctl -e -w lustre.fail_loc=0 || true
1302     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
1303     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
1304     echo "Dumping lctl log to $ERRLOG"
1305     # We need to dump the logs on all nodes
1306     local NODES=$(nodes_list)
1307     for NODE in $NODES; do
1308         do_node $NODE $LCTL dk $ERRLOG
1309     done
1310     debugrestore
1311     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
1312 }
1313
1314 error() {
1315     error_noexit "$@"
1316     $FAIL_ON_ERROR && exit 1 || true
1317 }
1318
1319 error_exit() {
1320     error_noexit "$@"
1321     exit 1
1322 }
1323
1324 # use only if we are ignoring failures for this test, bugno required.
1325 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
1326 # e.g. error_ignore 5494 "your message"
1327 error_ignore() {
1328     TYPE="IGNORE (bz$1)"
1329     shift
1330     error_noexit "$@"
1331 }
1332
1333 skip () {
1334         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
1335         [ "$TESTSUITELOG" ] && echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG
1336 }
1337
1338 build_test_filter() {
1339     [ "$ONLY" ] && log "only running test `echo $ONLY`"
1340     for O in $ONLY; do
1341         eval ONLY_${O}=true
1342     done
1343     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
1344         log "skipping tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
1345     [ "$EXCEPT_SLOW" ] && \
1346         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
1347     for E in $EXCEPT $ALWAYS_EXCEPT; do
1348         eval EXCEPT_${E}=true
1349     done
1350     for E in $EXCEPT_SLOW; do
1351         eval EXCEPT_SLOW_${E}=true
1352     done
1353     for G in $GRANT_CHECK_LIST; do
1354         eval GCHECK_ONLY_${G}=true
1355         done
1356 }
1357
1358 _basetest() {
1359     echo $*
1360 }
1361
1362 basetest() {
1363     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
1364 }
1365
1366 run_test() {
1367     export base=`basetest $1`
1368     if [ ! -z "$ONLY" ]; then
1369         testname=ONLY_$1
1370         if [ ${!testname}x != x ]; then
1371             run_one $1 "$2"
1372             return $?
1373         fi
1374         testname=ONLY_$base
1375         if [ ${!testname}x != x ]; then
1376             run_one $1 "$2"
1377             return $?
1378         fi
1379         echo -n "."
1380         return 0
1381     fi
1382     testname=EXCEPT_$1
1383     if [ ${!testname}x != x ]; then
1384         TESTNAME=test_$1 skip "skipping excluded test $1"
1385         return 0
1386     fi
1387     testname=EXCEPT_$base
1388     if [ ${!testname}x != x ]; then
1389         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
1390         return 0
1391     fi
1392     testname=EXCEPT_SLOW_$1
1393     if [ ${!testname}x != x ]; then
1394         TESTNAME=test_$1 skip "skipping SLOW test $1"
1395         return 0
1396     fi
1397     testname=EXCEPT_SLOW_$base
1398     if [ ${!testname}x != x ]; then
1399         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
1400         return 0
1401     fi
1402
1403     run_one $1 "$2"
1404
1405     return $?
1406 }
1407
1408 EQUALS="======================================================================"
1409 equals_msg() {
1410     msg="$@"
1411
1412     local suffixlen=$((${#EQUALS} - ${#msg}))
1413     [ $suffixlen -lt 5 ] && suffixlen=5
1414     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
1415 }
1416
1417 log() {
1418     echo "$*"
1419     lsmod | grep lnet > /dev/null || load_modules
1420
1421     local MSG="$*"
1422     # Get rif of '
1423     MSG=${MSG//\'/\\\'}
1424     MSG=${MSG//\(/\\\(}
1425     MSG=${MSG//\)/\\\)}
1426     MSG=${MSG//\;/\\\;}
1427     MSG=${MSG//\|/\\\|}
1428     MSG=${MSG//\>/\\\>}
1429     MSG=${MSG//\</\\\<}
1430     local NODES=$(nodes_list)
1431     for NODE in $NODES; do
1432         do_node $NODE $LCTL mark "$MSG" 2> /dev/null || true
1433     done
1434 }
1435
1436 trace() {
1437         log "STARTING: $*"
1438         strace -o $TMP/$1.strace -ttt $*
1439         RC=$?
1440         log "FINISHED: $*: rc $RC"
1441         return 1
1442 }
1443
1444 pass() {
1445     echo PASS $@
1446 }
1447
1448 check_mds() {
1449     FFREE=`cat /proc/fs/lustre/osd/*MDT*/filesfree`
1450     FTOTAL=`cat /proc/fs/lustre/osd/*MDT*/filestotal`
1451     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
1452 }
1453
1454 reset_fail_loc () {
1455     local myNODES=$(nodes_list)
1456     local NODE
1457
1458     for NODE in $myNODES; do
1459         do_node $NODE sysctl -e -w lustre.fail_loc=0 || true
1460     done
1461 }
1462
1463 run_one() {
1464     testnum=$1
1465     message=$2
1466     tfile=f${testnum}
1467     export tdir=d0.${TESTSUITE}/d${base}
1468     local SAVE_UMASK=`umask`
1469     umask 0022
1470     mkdir -p $DIR/$tdir
1471
1472     BEFORE=`date +%s`
1473     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
1474     #check_mds
1475     export TESTNAME=test_$testnum
1476     test_${testnum} || error "test_$testnum failed with $?"
1477     #check_mds
1478     reset_fail_loc
1479     check_grant ${testnum} || error "check_grant $testnum failed with $?"
1480     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
1481         error "LBUG/LASSERT detected"
1482     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
1483     pass "($((`date +%s` - $BEFORE))s)"
1484     rmdir ${DIR}/$tdir >/dev/null 2>&1 || true
1485     unset TESTNAME
1486     unset tdir
1487     umask $SAVE_UMASK
1488     cd $SAVE_PWD
1489     $CLEANUP
1490 }
1491
1492 canonical_path() {
1493     (cd `dirname $1`; echo $PWD/`basename $1`)
1494 }
1495
1496 sync_clients() {
1497     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync
1498     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync
1499         cd $SAVE_PWD
1500 }
1501
1502 check_grant() {
1503     export base=`basetest $1`
1504     [ "$CHECK_GRANT" == "no" ] && return 0
1505
1506         testname=GCHECK_ONLY_${base}
1507         [ ${!testname}x == x ] && return 0
1508
1509         echo -n "checking grant......"
1510         cd $SAVE_PWD
1511         # write some data to sync client lost_grant
1512         rm -f $DIR1/${tfile}_check_grant_* 2>&1
1513         for i in `seq $OSTCOUNT`; do
1514                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
1515                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
1516                                               count=1 > /dev/null 2>&1
1517         done
1518         # sync all the data and make sure no pending data on server
1519         sync_clients
1520         
1521         #get client grant and server grant
1522         client_grant=0
1523     for d in ${LPROC}/osc/*/cur_grant_bytes; do
1524                 client_grant=$((client_grant + `cat $d`))
1525         done
1526         server_grant=0
1527         for d in ${LPROC}/obdfilter/*/tot_granted; do
1528                 server_grant=$((server_grant + `cat $d`))
1529         done
1530
1531         # cleanup the check_grant file
1532         for i in `seq $OSTCOUNT`; do
1533                 rm $DIR1/${tfile}_check_grant_$i
1534         done
1535
1536         #check whether client grant == server grant
1537         if [ $client_grant != $server_grant ]; then
1538                 echo "failed: client:${client_grant} server: ${server_grant}"
1539                 return 1
1540         else
1541                 echo "pass"
1542         fi
1543 }
1544
1545 ########################
1546 # helper functions
1547
1548 osc_to_ost()
1549 {
1550     osc=$1
1551     ost=`echo $1 | awk -F_ '{print $3}'`
1552     if [ -z $ost ]; then
1553         ost=`echo $1 | sed 's/-osc.*//'`
1554     fi
1555     echo $ost
1556 }
1557
1558 remote_mds ()
1559 {
1560     [ -z "$(lctl dl | grep mdt)" ]
1561 }
1562
1563 remote_mds_nodsh()
1564 {
1565     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
1566 }
1567
1568 remote_ost ()
1569 {
1570     [ -z "$(lctl dl | grep ost)" ]
1571 }
1572
1573 remote_ost_nodsh()
1574 {
1575     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
1576 }
1577
1578 mdts_nodes () {
1579     local MDSNODES=$(facet_host $SINGLEMDS)
1580     local NODES_sort
1581
1582     # FIXME: Currenly we use only $SINGLEMDS,
1583     # should be fixed when we will start to test cmd.
1584     echo $MDSNODES
1585     return
1586
1587     for num in `seq $MDSCOUNT`; do
1588         local myMDS=$(facet_host mds$num)
1589         MDSNODES="$MDSNODES $myMDS"
1590     done
1591     NODES_sort=$(for i in $MDSNODES; do echo $i; done | sort -u)
1592
1593     echo $NODES_sort
1594 }
1595
1596 osts_nodes () {
1597     local OSTNODES=$(facet_host ost1)
1598     local NODES_sort
1599
1600     for num in `seq $OSTCOUNT`; do
1601         local myOST=$(facet_host ost$num)
1602         OSTNODES="$OSTNODES $myOST"
1603     done
1604     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
1605
1606     echo $NODES_sort
1607 }
1608
1609 nodes_list () {
1610     # FIXME. We need a list of clients
1611     local myNODES=$HOSTNAME
1612     local myNODES_sort
1613
1614     # CLIENTS (if specified) contains the local client
1615     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
1616
1617     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
1618         myNODES="$myNODES $(osts_nodes) $(mdts_nodes)"
1619     fi
1620
1621     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
1622
1623     echo $myNODES_sort
1624 }
1625
1626 is_patchless ()
1627 {
1628     lctl get_param version | grep -q patchless
1629 }
1630
1631 check_runas_id_ret() {
1632     local myRC=0
1633     local myRUNAS_ID=$1
1634     shift
1635     local myRUNAS=$@
1636     if [ -z "$myRUNAS" ]; then
1637         error_exit "myRUNAS command must be specified for check_runas_id"
1638     fi
1639     if $GSS_KRB5; then
1640         $myRUNAS krb5_login.sh || \
1641             error "Failed to refresh Kerberos V5 TGT for UID $myRUNAS_ID."
1642     fi
1643     mkdir $DIR/d0_runas_test
1644     chmod 0755 $DIR
1645     chown $myRUNAS_ID:$myRUNAS_ID $DIR/d0_runas_test
1646     $myRUNAS touch $DIR/d0_runas_test/f$$ || myRC=$?
1647     rm -rf $DIR/d0_runas_test
1648     return $myRC
1649 }
1650
1651 check_runas_id() {
1652     local myRUNAS_ID=$1
1653     shift
1654     local myRUNAS=$@
1655     check_runas_id_ret $myRUNAS_ID $myRUNAS || \
1656         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_ID. 
1657         Please set RUNAS_ID to some UID which exists on MDS and client or 
1658         add user $myRUNAS_ID:$myRUNAS_ID on these nodes."
1659 }
1660
1661 # Run multiop in the background, but wait for it to print
1662 # "PAUSING" to its stdout before returning from this function.
1663 multiop_bg_pause() {
1664     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
1665     FILE=$1
1666     ARGS=$2
1667
1668     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
1669     mkfifo $TMPPIPE
1670
1671     echo "$MULTIOP_PROG $FILE v$ARGS"
1672     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
1673
1674     echo "TMPPIPE=${TMPPIPE}"
1675     read -t 60 multiop_output < $TMPPIPE
1676     if [ $? -ne 0 ]; then
1677         rm -f $TMPPIPE
1678         return 1
1679     fi
1680     rm -f $TMPPIPE
1681     if [ "$multiop_output" != "PAUSING" ]; then
1682         echo "Incorrect multiop output: $multiop_output"
1683         kill -9 $PID
1684         return 1
1685     fi
1686
1687     return 0
1688 }
1689
1690 # reset llite stat counters
1691 clear_llite_stats(){
1692         lctl set_param -n llite.*.stats 0
1693 }
1694
1695 # sum llite stat items
1696 calc_llite_stats() {
1697         local res=$(lctl get_param -n llite.*.stats |
1698                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
1699         echo $res
1700 }
1701
1702 # save_lustre_params(node, parameter_mask)
1703 # generate a stream of formatted strings (<node> <param name>=<param value>)
1704 save_lustre_params() {
1705         local s
1706         do_node $1 "lctl get_param $2" | while read s; do echo "$1 $s"; done
1707 }
1708
1709 # restore lustre parameters from input stream, produces by save_lustre_params
1710 restore_lustre_params() {
1711         local node
1712         local name
1713         local val
1714         while IFS=" =" read node name val; do
1715                 do_node $node "lctl set_param -n $name $val"
1716         done
1717 }
1718