Whamcloud - gitweb
Refactor start to encapsulate facet specific data.
[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: $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     client=$1
485     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 $mnt (opts:$force)"
490         lsof | grep "$mnt" || true
491         do_node $client umount $force $mnt
492     fi
493 }
494
495 shutdown_facet() {
496     facet=$1
497     if [ "$FAILURE_MODE" = HARD ]; then
498         $POWER_DOWN `facet_active_host $facet`
499         sleep 2
500     elif [ "$FAILURE_MODE" = SOFT ]; then
501         stop $facet
502     fi
503 }
504
505 reboot_facet() {
506     facet=$1
507     if [ "$FAILURE_MODE" = HARD ]; then
508         $POWER_UP `facet_active_host $facet`
509     else
510         sleep 10
511     fi
512 }
513
514 # verify that lustre actually cleaned up properly
515 cleanup_check() {
516     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
517         error "LBUG/LASSERT detected"
518     BUSY=`dmesg | grep -i destruct || true`
519     if [ "$BUSY" ]; then
520         echo "$BUSY" 1>&2
521         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
522         exit 205
523     fi
524     LEAK_LUSTRE=`dmesg | tail -n 30 | grep "obd mem.*leaked" || true`
525     LEAK_PORTALS=`dmesg | tail -n 20 | grep "Portals memory leaked" || true`
526     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
527         echo "$0: $LEAK_LUSTRE" 1>&2
528         echo "$0: $LEAK_PORTALS" 1>&2
529         echo "$0: Memory leak(s) detected..." 1>&2
530         mv $TMP/debug $TMP/debug-leak.`date +%s`
531         exit 204
532     fi
533
534     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
535         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
536
537     if [ "`/sbin/lsmod 2>&1 | egrep 'lnet|libcfs'`" ]; then
538         echo "$0: modules still loaded..." 1>&2
539         /sbin/lsmod 1>&2
540         return 203
541     fi
542     return 0
543 }
544
545 wait_delete_completed () {
546     local TOTALPREV=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
547             $LPROC/osc/*/kbytesavail`
548
549     local WAIT=0
550     local MAX_WAIT=20
551     while [ "$WAIT" -ne "$MAX_WAIT" ]; do
552         sleep 1
553         TOTAL=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
554             $LPROC/osc/*/kbytesavail`
555         [ "$TOTAL" -eq "$TOTALPREV" ] && break
556         echo "Waiting delete completed ... prev: $TOTALPREV current: $TOTAL "
557         TOTALPREV=$TOTAL
558         WAIT=$(( WAIT + 1))
559     done
560     echo "Delete completed."
561 }
562
563 wait_for_host() {
564     HOST=$1
565     check_network "$HOST" 900
566     while ! do_node $HOST "ls -d $LUSTRE " > /dev/null; do sleep 5; done
567 }
568
569 wait_for() {
570     facet=$1
571     HOST=`facet_active_host $facet`
572     wait_for_host $HOST
573 }
574
575 wait_mds_recovery_done () {
576     local timeout=`do_facet mds sysctl -n lustre.timeout`
577 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2)
578 # as we are in process of changing obd_timeout in different ways
579 # let's set MAX longer than that
580     MAX=$(( timeout * 4 ))
581     WAIT=0
582     while [ $WAIT -lt $MAX ]; do
583         STATUS=`do_facet $SINGLEMDS grep status /proc/fs/lustre/mdt/*-MDT*/recovery_status`
584         echo $STATUS | grep COMPLETE && return 0
585         sleep 5
586         WAIT=$((WAIT + 5))
587         echo "Waiting $(($MAX - $WAIT)) secs for MDS recovery done"
588     done
589     echo "MDS recovery not done in $MAX sec"
590     return 1
591 }
592
593 wait_exit_ST () {
594     local facet=$1
595
596     local WAIT=0
597     local INTERVAL=1
598     # conf-sanity 31 takes a long time cleanup
599     while [ $WAIT -lt 300 ]; do
600         running=$(do_facet ${facet} "lsmod | grep lnet > /dev/null && lctl dl | grep ' ST '") || true
601         [ -z "${running}" ] && return 0
602         echo "waited $WAIT for${running}"
603         [ $INTERVAL -lt 64 ] && INTERVAL=$((INTERVAL + INTERVAL))
604         sleep $INTERVAL
605         WAIT=$((WAIT + INTERVAL))
606     done
607     echo "service didn't stop after $WAIT seconds.  Still running:"
608     echo ${running}
609     return 1
610 }
611
612 client_df() {
613     # not every config has many clients
614     if [ ! -z "$CLIENTS" ]; then
615         $PDSH $CLIENTS "df $MOUNT" > /dev/null
616     fi
617 }
618
619 client_reconnect() {
620     uname -n >> $MOUNT/recon
621     if [ ! -z "$CLIENTS" ]; then
622         $PDSH $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
623     fi
624     echo Connected clients:
625     cat $MOUNT/recon
626     ls -l $MOUNT/recon > /dev/null
627     rm $MOUNT/recon
628 }
629
630 facet_failover() {
631     facet=$1
632     echo "Failing $facet on node `facet_active_host $facet`"
633     shutdown_facet $facet
634     reboot_facet $facet
635     client_df &
636     DFPID=$!
637     echo "df pid is $DFPID"
638     change_active $facet
639     TO=`facet_active_host $facet`
640     echo "Failover $facet to $TO"
641     wait_for $facet
642     mount_facet $facet || error "Restart of $facet failed"
643 }
644
645 obd_name() {
646     local facet=$1
647 }
648
649 replay_barrier() {
650     local facet=$1
651     do_facet $facet sync
652     df $MOUNT
653     local svc=${facet}_svc
654     do_facet $facet $LCTL --device %${!svc} readonly
655     do_facet $facet $LCTL --device %${!svc} notransno
656     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
657     $LCTL mark "local REPLAY BARRIER on ${!svc}"
658 }
659
660 replay_barrier_nodf() {
661     local facet=$1    echo running=${running}
662     do_facet $facet sync
663     local svc=${facet}_svc
664     echo Replay barrier on ${!svc}
665     do_facet $facet $LCTL --device %${!svc} readonly
666     do_facet $facet $LCTL --device %${!svc} notransno
667     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
668     $LCTL mark "local REPLAY BARRIER on ${!svc}"
669 }
670
671 mds_evict_client() {
672     UUID=`cat /proc/fs/lustre/mdc/${mds1_svc}-mdc-*/uuid`
673     do_facet mds1 "echo $UUID > /proc/fs/lustre/mdt/${mds1_svc}/evict_client"
674 }
675
676 ost_evict_client() {
677     UUID=`grep ${ost1_svc}-osc- $LPROC/devices | egrep -v 'MDT' | awk '{print $5}'`
678     do_facet ost1 "echo $UUID > /proc/fs/lustre/obdfilter/${ost1_svc}/evict_client"
679 }
680
681 fail() {
682     facet_failover $* || error "failover: $?"
683     df $MOUNT || error "post-failover df: $?"
684 }
685
686 fail_nodf() {
687         local facet=$1
688         facet_failover $facet
689 }
690
691 fail_abort() {
692     local facet=$1
693     stop $facet
694     change_active $facet
695     mount_facet $facet -o abort_recovery
696     df $MOUNT || echo "first df failed: $?"
697     sleep 1
698     df $MOUNT || error "post-failover df: $?"
699 }
700
701 do_lmc() {
702     echo There is no lmc.  This is mountconf, baby.
703     exit 1
704 }
705
706 h2gm () {
707     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
708         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
709         echo $ID"@gm"
710     fi
711 }
712
713 h2name_or_ip() {
714     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
715         echo $1"@$2"
716     fi
717 }
718
719 h2ptl() {
720    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
721        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
722        if [ -z "$ID" ]; then
723            echo "Could not get a ptl id for $1..."
724            exit 1
725        fi
726        echo $ID"@ptl"
727    fi
728 }
729 declare -fx h2ptl
730
731 h2tcp() {
732     h2name_or_ip "$1" "tcp"
733 }
734 declare -fx h2tcp
735
736 h2elan() {
737     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
738         if type __h2elan >/dev/null 2>&1; then
739             ID=$(__h2elan $1)
740         else
741             ID=`echo $1 | sed 's/[^0-9]*//g'`
742         fi
743         echo $ID"@elan"
744     fi
745 }
746 declare -fx h2elan
747
748 h2openib() {
749     h2name_or_ip "$1" "openib"
750 }
751 declare -fx h2openib
752
753 h2o2ib() {
754     h2name_or_ip "$1" "o2ib"
755 }
756 declare -fx h2o2ib
757
758 facet_host() {
759     local facet=$1
760     varname=${facet}_HOST
761     if [ -z "${!varname}" ]; then
762         if [ "${facet:0:3}" == "ost" ]; then
763             eval ${facet}_HOST=${ost_HOST}
764         fi
765     fi
766     echo -n ${!varname}
767 }
768
769 facet_active() {
770     local facet=$1
771     local activevar=${facet}active
772
773     if [ -f ./${facet}active ] ; then
774         source ./${facet}active
775     fi
776
777     active=${!activevar}
778     if [ -z "$active" ] ; then
779         echo -n ${facet}
780     else
781         echo -n ${active}
782     fi
783 }
784
785 facet_active_host() {
786     local facet=$1
787     local active=`facet_active $facet`
788     if [ "$facet" == client ]; then
789         echo $HOSTNAME
790     else
791         echo `facet_host $active`
792     fi
793 }
794
795 change_active() {
796     local facet=$1
797     failover=${facet}failover
798     host=`facet_host $failover`
799     [ -z "$host" ] && return
800     curactive=`facet_active $facet`
801     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
802         eval export ${facet}active=$facet
803     else
804         eval export ${facet}active=$failover
805     fi
806     # save the active host for this facet
807     activevar=${facet}active
808     echo "$activevar=${!activevar}" > ./$activevar
809 }
810
811 do_node() {
812     HOST=$1
813     shift
814     local myPDSH=$PDSH
815     if [ "$HOST" = "$HOSTNAME" ]; then
816         myPDSH="no_dsh"
817     elif [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" ]; then
818         echo "cannot run remote command on $HOST with $myPDSH"
819         return 128
820     fi
821     if $VERBOSE; then
822         echo "CMD: $HOST $@" >&2
823         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
824     fi
825
826     if [ "$myPDSH" = "rsh" ]; then
827 # we need this because rsh does not return exit code of an executed command
828         local command_status="$TMP/cs"
829         rsh $HOST ":> $command_status"
830         rsh $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin;
831                     cd $RPWD; sh -c \"$@\") || 
832                     echo command failed >$command_status"
833         [ -n "$($myPDSH $HOST cat $command_status)" ] && return 1 || true
834         return 0
835     fi
836     $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
837     return ${PIPESTATUS[0]}
838 }
839
840 do_facet() {
841     facet=$1
842     shift
843     HOST=`facet_active_host $facet`
844     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
845     do_node $HOST $@
846 }
847
848 add() {
849     local facet=$1
850     shift
851     # make sure its not already running
852     stop ${facet} -f
853     rm -f ${facet}active
854     do_facet ${facet} $MKFS $*
855 }
856
857 ostdevname() {
858     num=$1
859     DEVNAME=OSTDEV$num
860     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
861     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
862     echo -n $DEVPTR
863 }
864
865 mdsdevname() {
866     num=$1
867     DEVNAME=MDSDEV$num
868     #if $MDSDEVn isn't defined, default is $MDSDEVBASE + num
869     eval DEVPTR=${!DEVNAME:=${MDSDEVBASE}${num}}
870     echo -n $DEVPTR
871 }
872
873 ########
874 ## MountConf setup
875
876 stopall() {
877     # make sure we are using the primary server, so test-framework will
878     # be able to clean up properly.
879     activemds=`facet_active mds1`
880     if [ $activemds != "mds1" ]; then
881         fail mds1
882     fi
883
884     # assume client mount is local
885     grep " $MOUNT " /proc/mounts && zconf_umount $HOSTNAME $MOUNT $*
886     grep " $MOUNT2 " /proc/mounts && zconf_umount $HOSTNAME $MOUNT2 $*
887     [ "$CLIENTONLY" ] && return
888     for num in `seq $MDSCOUNT`; do
889         stop mds$num -f
890     done
891     for num in `seq $OSTCOUNT`; do
892         stop ost$num -f
893     done
894     return 0
895 }
896
897 cleanupall() {
898     stopall $*
899     unload_modules
900     cleanup_gss
901 }
902
903 mdsmkfsopts()
904 {
905     local nr=$1
906     test $nr = 1 && echo -n $MDS_MKFS_OPTS || echo -n $MDSn_MKFS_OPTS
907 }
908
909 formatall() {
910     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
911
912     if [ ! -z $SEC ]; then
913         MDS_MKFS_OPTS="$MDS_MKFS_OPTS --param srpc.flavor.default=$SEC"
914         OST_MKFS_OPTS="$OST_MKFS_OPTS --param srpc.flavor.default=$SEC"
915     fi
916
917     stopall
918     # We need ldiskfs here, may as well load them all
919     load_modules
920     [ "$CLIENTONLY" ] && return
921     echo "Formatting mdts, osts"
922     for num in `seq $MDSCOUNT`; do
923         echo "Format mds$num: $(mdsdevname $num)"
924         if $VERBOSE; then
925             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` || exit 9
926         else
927             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` > /dev/null || exit 9
928         fi
929     done
930
931     for num in `seq $OSTCOUNT`; do
932         echo "Format ost$num: $(ostdevname $num)"
933         if $VERBOSE; then
934             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` || exit 10
935         else
936             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` > /dev/null || exit 10
937         fi
938     done
939 }
940
941 mount_client() {
942     grep " $1 " /proc/mounts || zconf_mount $HOSTNAME $*
943 }
944
945 umount_client() {
946     grep " $1 " /proc/mounts && zconf_umount `hostname` $*
947 }
948
949 # return value:
950 # 0: success, the old identity set already.
951 # 1: success, the old identity does not set.
952 # 2: fail.
953 switch_identity() {
954     local num=$1
955     local switch=$2
956     local j=`expr $num - 1`
957     local MDT="`do_facet mds$num find $LPROC/mdt/ -name \*MDT\*$j -printf %f 2>/dev/null || true`"
958
959     if [ -z "$MDT" ]; then
960         return 2
961     fi
962
963     local old="`do_facet mds$num cat $LPROC/mdt/$MDT/identity_upcall`"
964
965     if $switch; then
966         do_facet mds$num "echo \"$L_GETIDENTITY\" > $LPROC/mdt/$MDT/identity_upcall"
967     else
968         do_facet mds$num "echo \"NONE\" > $LPROC/mdt/$MDT/identity_upcall"
969     fi
970
971     do_facet mds$num "echo \"-1\" > $LPROC/mdt/$MDT/identity_flush"
972
973     if [ $old = "NONE" ]; then
974         return 1
975     else
976         return 0
977     fi
978 }
979
980 remount_client()
981 {
982         zconf_umount `hostname` $1 || error "umount failed"
983         zconf_mount `hostname` $1 || error "mount failed"
984 }
985
986 set_obd_timeout() {
987     local facet=$1
988     local timeout=$2
989
990     do_facet $facet lsmod | grep -q obdclass || \
991         do_facet $facet "modprobe obdclass"
992
993     do_facet $facet "sysctl -w lustre.timeout=$timeout"
994 }
995
996 setupall() {
997     load_modules
998     init_gss
999     if [ -z "$CLIENTONLY" ]; then
1000         echo "Setup mdts, osts"
1001         for num in `seq $MDSCOUNT`; do
1002             DEVNAME=$(mdsdevname $num)
1003             echo $REFORMAT | grep -q "reformat" \
1004             || do_facet mds$num "$TUNEFS --writeconf $DEVNAME"
1005             set_obd_timeout mds$num $TIMEOUT
1006             start mds$num $DEVNAME $MDS_MOUNT_OPTS
1007             if [ $IDENTITY_UPCALL != "default" ]; then
1008                 switch_identity $num $IDENTITY_UPCALL
1009             fi
1010         done
1011         for num in `seq $OSTCOUNT`; do
1012             DEVNAME=$(ostdevname $num)
1013             set_obd_timeout ost$num $TIMEOUT
1014             start ost$num $DEVNAME $OST_MOUNT_OPTS
1015         done
1016     fi
1017     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
1018     mount_client $MOUNT
1019     if [ "$MOUNT_2" ]; then
1020         mount_client $MOUNT2
1021     fi
1022
1023     # by remounting mdt before ost, initial connect from mdt to ost might
1024     # timeout because ost is not ready yet. wait some time to its fully
1025     # recovery. initial obd_connect timeout is 5s; in GSS case it's preceeded
1026     # by a context negotiation rpc with $TIMEOUT.
1027     # FIXME better by monitoring import status.
1028     if $GSS; then
1029         sleep $((TIMEOUT + 5))
1030     else
1031         sleep 5
1032     fi
1033 }
1034
1035 mounted_lustre_filesystems() {
1036         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
1037 }
1038
1039 check_and_setup_lustre() {
1040     MOUNTED="`mounted_lustre_filesystems`"
1041     if [ -z "$MOUNTED" ]; then
1042         [ "$REFORMAT" ] && formatall
1043         setupall
1044         MOUNTED="`mounted_lustre_filesystems`"
1045         [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
1046         export I_MOUNTED=yes
1047     fi
1048     if [ "$ONLY" == "setup" ]; then
1049         exit 0
1050     fi
1051 }
1052
1053 cleanup_and_setup_lustre() {
1054     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
1055         sysctl -w lnet.debug=0 || true
1056         cleanupall
1057         if [ "$ONLY" == "cleanup" ]; then
1058             exit 0
1059         fi
1060     fi
1061     check_and_setup_lustre
1062 }
1063
1064 check_and_cleanup_lustre() {
1065     if [ "`mount | grep $MOUNT`" ]; then
1066         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]*
1067     fi
1068     if [ "$I_MOUNTED" = "yes" ]; then
1069         cleanupall -f || error "cleanup failed"
1070     fi
1071     unset I_MOUNTED
1072 }
1073
1074 #######
1075 # General functions
1076
1077 check_network() {
1078     local NETWORK=0
1079     local WAIT=0
1080     local MAX=$2
1081     while [ $NETWORK -eq 0 ]; do
1082         ping -c 1 -w 3 $1 > /dev/null
1083         if [ $? -eq 0 ]; then
1084             NETWORK=1
1085         else
1086             WAIT=$((WAIT + 5))
1087             echo "waiting for $1, $((MAX - WAIT)) secs left"
1088             sleep 5
1089         fi
1090         if [ $WAIT -gt $MAX ]; then
1091             echo "Network not available"
1092             exit 1
1093         fi
1094     done
1095 }
1096 check_port() {
1097     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
1098         sleep 9
1099     done
1100 }
1101
1102 no_dsh() {
1103     shift
1104     eval $@
1105 }
1106
1107 comma_list() {
1108     # the sed converts spaces to commas, but leaves the last space
1109     # alone, so the line doesn't end with a comma.
1110     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
1111 }
1112
1113 absolute_path() {
1114     (cd `dirname $1`; echo $PWD/`basename $1`)
1115 }
1116
1117 ##################################
1118 # OBD_FAIL funcs
1119
1120 drop_request() {
1121 # OBD_FAIL_MDS_ALL_REQUEST_NET
1122     RC=0
1123     do_facet mds sysctl -w lustre.fail_loc=0x123
1124     do_facet client "$1" || RC=$?
1125     do_facet mds sysctl -w lustre.fail_loc=0
1126     return $RC
1127 }
1128
1129 drop_reply() {
1130 # OBD_FAIL_MDS_ALL_REPLY_NET
1131     RC=0
1132     do_facet mds sysctl -w lustre.fail_loc=0x122
1133     do_facet client "$@" || RC=$?
1134     do_facet mds sysctl -w lustre.fail_loc=0
1135     return $RC
1136 }
1137
1138 drop_reint_reply() {
1139 # OBD_FAIL_MDS_REINT_NET_REP
1140     RC=0
1141     do_facet mds sysctl -w lustre.fail_loc=0x119
1142     do_facet client "$@" || RC=$?
1143     do_facet mds sysctl -w lustre.fail_loc=0
1144     return $RC
1145 }
1146
1147 pause_bulk() {
1148 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
1149     RC=0
1150     do_facet ost1 sysctl -w lustre.fail_loc=0x214
1151     do_facet client "$1" || RC=$?
1152     do_facet client "sync"
1153     do_facet ost1 sysctl -w lustre.fail_loc=0
1154     return $RC
1155 }
1156
1157 drop_ldlm_cancel() {
1158 #define OBD_FAIL_LDLM_CANCEL             0x304
1159     RC=0
1160     do_facet client sysctl -w lustre.fail_loc=0x304
1161     do_facet client "$@" || RC=$?
1162     do_facet client sysctl -w lustre.fail_loc=0
1163     return $RC
1164 }
1165
1166 drop_bl_callback() {
1167 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
1168     RC=0
1169     do_facet client sysctl -w lustre.fail_loc=0x305
1170     do_facet client "$@" || RC=$?
1171     do_facet client sysctl -w lustre.fail_loc=0
1172     return $RC
1173 }
1174
1175 drop_ldlm_reply() {
1176 #define OBD_FAIL_LDLM_REPLY              0x30c
1177     RC=0
1178     do_facet mds sysctl -w lustre.fail_loc=0x30c
1179     do_facet client "$@" || RC=$?
1180     do_facet mds sysctl -w lustre.fail_loc=0
1181     return $RC
1182 }
1183
1184 clear_failloc() {
1185     facet=$1
1186     pause=$2
1187     sleep $pause
1188     echo "clearing fail_loc on $facet"
1189     do_facet $facet "sysctl -e -w lustre.fail_loc=0"
1190 }
1191
1192 set_nodes_failloc () {
1193     local nodes=$1
1194     local node
1195
1196     for node in $nodes ; do
1197         do_node $node sysctl -w lustre.fail_loc=$2
1198     done
1199 }
1200
1201 cancel_lru_locks() {
1202     $LCTL mark "cancel_lru_locks $1 start"
1203     for d in `find $LPROC/ldlm/namespaces | egrep -i $1`; do
1204         [ -f $d/lru_size ] && echo clear > $d/lru_size
1205         [ -f $d/lock_unused_count ] && grep [1-9] $d/lock_unused_count /dev/null
1206     done
1207     $LCTL mark "cancel_lru_locks $1 stop"
1208 }
1209
1210 default_lru_size()
1211 {
1212         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
1213         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
1214         echo "$DEFAULT_LRU_SIZE"
1215 }
1216
1217 lru_resize_enable()
1218 {
1219         NS=$1
1220         test "x$NS" = "x" && NS="mdc"
1221         for F in $LPROC/ldlm/namespaces/*$NS*/lru_size; do
1222                 D=$(dirname $F)
1223                 log "Enable lru resize for $(basename $D)"
1224                 echo "0" > $F
1225         done
1226 }
1227
1228 lru_resize_disable()
1229 {
1230         NS=$1
1231         test "x$NS" = "x" && NS="mdc"
1232         for F in $LPROC/ldlm/namespaces/*$NS*/lru_size; do
1233                 D=$(dirname $F)
1234                 log "Disable lru resize for $(basename $D)"
1235                 DEFAULT_LRU_SIZE=$(default_lru_size)
1236                 echo "$DEFAULT_LRU_SIZE" > $F
1237         done
1238 }
1239
1240 pgcache_empty() {
1241     for a in /proc/fs/lustre/llite/*/dump_page_cache; do
1242         if [ `wc -l $a | awk '{print $1}'` -gt 1 ]; then
1243             echo there is still data in page cache $a ?
1244             cat $a;
1245             return 1;
1246         fi
1247     done
1248     return 0
1249 }
1250
1251 debugsave() {
1252     DEBUGSAVE="$(sysctl -n lnet.debug)"
1253 }
1254
1255 debugrestore() {
1256     [ -n "$DEBUGSAVE" ] && sysctl -w lnet.debug="${DEBUGSAVE}"
1257     DEBUGSAVE=""
1258 }
1259
1260 ##################################
1261 # Test interface
1262 ##################################
1263
1264 error_noexit() {
1265     local TYPE=${TYPE:-"FAIL"}
1266     local ERRLOG
1267     sysctl -e -w lustre.fail_loc=0 || true
1268     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
1269     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
1270     echo "Dumping lctl log to $ERRLOG"
1271     # We need to dump the logs on all nodes
1272     local NODES=$(nodes_list)
1273     for NODE in $NODES; do
1274         do_node $NODE $LCTL dk $ERRLOG
1275     done
1276     debugrestore
1277     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
1278 }
1279
1280 error() {
1281     error_noexit "$@"
1282     $FAIL_ON_ERROR && exit 1 || true
1283 }
1284
1285 error_exit() {
1286     error_noexit "$@"
1287     exit 1
1288 }
1289
1290 # use only if we are ignoring failures for this test, bugno required.
1291 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
1292 # e.g. error_ignore 5494 "your message"
1293 error_ignore() {
1294     TYPE="IGNORE (bz$1)"
1295     shift
1296     error_noexit "$@"
1297 }
1298
1299 skip () {
1300         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
1301         [ "$TESTSUITELOG" ] && echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG
1302 }
1303
1304 build_test_filter() {
1305     [ "$ONLY" ] && log "only running test `echo $ONLY`"
1306     for O in $ONLY; do
1307         eval ONLY_${O}=true
1308     done
1309     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
1310         log "skipping tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
1311     [ "$EXCEPT_SLOW" ] && \
1312         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
1313     for E in $EXCEPT $ALWAYS_EXCEPT; do
1314         eval EXCEPT_${E}=true
1315     done
1316     for E in $EXCEPT_SLOW; do
1317         eval EXCEPT_SLOW_${E}=true
1318     done
1319     for G in $GRANT_CHECK_LIST; do
1320         eval GCHECK_ONLY_${G}=true
1321         done
1322 }
1323
1324 _basetest() {
1325     echo $*
1326 }
1327
1328 basetest() {
1329     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
1330 }
1331
1332 run_test() {
1333     export base=`basetest $1`
1334     if [ ! -z "$ONLY" ]; then
1335         testname=ONLY_$1
1336         if [ ${!testname}x != x ]; then
1337             run_one $1 "$2"
1338             return $?
1339         fi
1340         testname=ONLY_$base
1341         if [ ${!testname}x != x ]; then
1342             run_one $1 "$2"
1343             return $?
1344         fi
1345         echo -n "."
1346         return 0
1347     fi
1348     testname=EXCEPT_$1
1349     if [ ${!testname}x != x ]; then
1350         TESTNAME=test_$1 skip "skipping excluded test $1"
1351         return 0
1352     fi
1353     testname=EXCEPT_$base
1354     if [ ${!testname}x != x ]; then
1355         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
1356         return 0
1357     fi
1358     testname=EXCEPT_SLOW_$1
1359     if [ ${!testname}x != x ]; then
1360         TESTNAME=test_$1 skip "skipping SLOW test $1"
1361         return 0
1362     fi
1363     testname=EXCEPT_SLOW_$base
1364     if [ ${!testname}x != x ]; then
1365         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
1366         return 0
1367     fi
1368
1369     run_one $1 "$2"
1370
1371     return $?
1372 }
1373
1374 EQUALS="======================================================================"
1375 equals_msg() {
1376     msg="$@"
1377
1378     local suffixlen=$((${#EQUALS} - ${#msg}))
1379     [ $suffixlen -lt 5 ] && suffixlen=5
1380     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
1381 }
1382
1383 log() {
1384     echo "$*"
1385     lsmod | grep lnet > /dev/null || load_modules
1386
1387     local MSG="$*"
1388     # Get rif of '
1389     MSG=${MSG//\'/\\\'}
1390     MSG=${MSG//\(/\\\(}
1391     MSG=${MSG//\)/\\\)}
1392     MSG=${MSG//\;/\\\;}
1393     MSG=${MSG//\|/\\\|}
1394     MSG=${MSG//\>/\\\>}
1395     MSG=${MSG//\</\\\<}
1396     local NODES=$(nodes_list)
1397     for NODE in $NODES; do
1398         do_node $NODE $LCTL mark "$MSG" 2> /dev/null || true
1399     done
1400 }
1401
1402 trace() {
1403         log "STARTING: $*"
1404         strace -o $TMP/$1.strace -ttt $*
1405         RC=$?
1406         log "FINISHED: $*: rc $RC"
1407         return 1
1408 }
1409
1410 pass() {
1411     echo PASS $@
1412 }
1413
1414 check_mds() {
1415     FFREE=`cat /proc/fs/lustre/osd/*MDT*/filesfree`
1416     FTOTAL=`cat /proc/fs/lustre/osd/*MDT*/filestotal`
1417     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
1418 }
1419
1420 reset_fail_loc () {
1421     local myNODES=$(nodes_list)
1422     local NODE
1423
1424     for NODE in $myNODES; do
1425         do_node $NODE sysctl -e -w lustre.fail_loc=0 || true
1426     done
1427 }
1428
1429 run_one() {
1430     testnum=$1
1431     message=$2
1432     tfile=f${testnum}
1433     export tdir=d0.${TESTSUITE}/d${base}
1434     local SAVE_UMASK=`umask`
1435     umask 0022
1436     mkdir -p $DIR/$tdir
1437
1438     BEFORE=`date +%s`
1439     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
1440     #check_mds
1441     export TESTNAME=test_$testnum
1442     test_${testnum} || error "test_$testnum failed with $?"
1443     #check_mds
1444     reset_fail_loc
1445     check_grant ${testnum} || error "check_grant $testnum failed with $?"
1446     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
1447         error "LBUG/LASSERT detected"
1448     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
1449     pass "($((`date +%s` - $BEFORE))s)"
1450     rmdir ${DIR}/$tdir >/dev/null 2>&1 || true
1451     unset TESTNAME
1452     unset tdir
1453     umask $SAVE_UMASK
1454     cd $SAVE_PWD
1455     $CLEANUP
1456 }
1457
1458 canonical_path() {
1459     (cd `dirname $1`; echo $PWD/`basename $1`)
1460 }
1461
1462 sync_clients() {
1463     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync
1464     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync
1465         cd $SAVE_PWD
1466 }
1467
1468 check_grant() {
1469     export base=`basetest $1`
1470     [ "$CHECK_GRANT" == "no" ] && return 0
1471
1472         testname=GCHECK_ONLY_${base}
1473         [ ${!testname}x == x ] && return 0
1474
1475         echo -n "checking grant......"
1476         cd $SAVE_PWD
1477         # write some data to sync client lost_grant
1478         rm -f $DIR1/${tfile}_check_grant_* 2>&1
1479         for i in `seq $OSTCOUNT`; do
1480                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
1481                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
1482                                               count=1 > /dev/null 2>&1
1483         done
1484         # sync all the data and make sure no pending data on server
1485         sync_clients
1486         
1487         #get client grant and server grant
1488         client_grant=0
1489     for d in ${LPROC}/osc/*/cur_grant_bytes; do
1490                 client_grant=$((client_grant + `cat $d`))
1491         done
1492         server_grant=0
1493         for d in ${LPROC}/obdfilter/*/tot_granted; do
1494                 server_grant=$((server_grant + `cat $d`))
1495         done
1496
1497         # cleanup the check_grant file
1498         for i in `seq $OSTCOUNT`; do
1499                 rm $DIR1/${tfile}_check_grant_$i
1500         done
1501
1502         #check whether client grant == server grant
1503         if [ $client_grant != $server_grant ]; then
1504                 echo "failed: client:${client_grant} server: ${server_grant}"
1505                 return 1
1506         else
1507                 echo "pass"
1508         fi
1509 }
1510
1511 ########################
1512 # helper functions
1513
1514 osc_to_ost()
1515 {
1516     osc=$1
1517     ost=`echo $1 | awk -F_ '{print $3}'`
1518     if [ -z $ost ]; then
1519         ost=`echo $1 | sed 's/-osc.*//'`
1520     fi
1521     echo $ost
1522 }
1523
1524 remote_mds ()
1525 {
1526     [ -z "$(lctl dl | grep mdt)" ]
1527 }
1528
1529 remote_mds_nodsh()
1530 {
1531     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
1532 }
1533
1534 remote_ost ()
1535 {
1536     [ -z "$(lctl dl | grep ost)" ]
1537 }
1538
1539 remote_ost_nodsh()
1540 {
1541     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
1542 }
1543
1544 mdts_nodes () {
1545     local MDSNODES=$(facet_host $SINGLEMDS)
1546     local NODES_sort
1547
1548     # FIXME: Currenly we use only $SINGLEMDS,
1549     # should be fixed when we will start to test cmd.
1550     echo $MDSNODES
1551     return
1552
1553     for num in `seq $MDSCOUNT`; do
1554         local myMDS=$(facet_host mds$num)
1555         MDSNODES="$MDSNODES $myMDS"
1556     done
1557     NODES_sort=$(for i in $MDSNODES; do echo $i; done | sort -u)
1558
1559     echo $NODES_sort
1560 }
1561
1562 osts_nodes () {
1563     local OSTNODES=$(facet_host ost1)
1564     local NODES_sort
1565
1566     for num in `seq $OSTCOUNT`; do
1567         local myOST=$(facet_host ost$num)
1568         OSTNODES="$OSTNODES $myOST"
1569     done
1570     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
1571
1572     echo $NODES_sort
1573 }
1574
1575 nodes_list () {
1576     # FIXME. We need a list of clients
1577     local myNODES=$HOSTNAME
1578     local myNODES_sort
1579
1580     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
1581         myNODES="$myNODES $(osts_nodes) $(mdts_nodes)"
1582     fi
1583
1584     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
1585
1586     echo $myNODES_sort
1587 }
1588
1589 is_patchless ()
1590 {
1591     lctl get_param version | grep -q patchless
1592 }
1593
1594 check_runas_id_ret() {
1595     local myRC=0
1596     local myRUNAS_ID=$1
1597     shift
1598     local myRUNAS=$@
1599     if [ -z "$myRUNAS" ]; then
1600         error_exit "myRUNAS command must be specified for check_runas_id"
1601     fi
1602     if $GSS_KRB5; then
1603         $myRUNAS krb5_login.sh || \
1604             error "Failed to refresh Kerberos V5 TGT for UID $myRUNAS_ID."
1605     fi
1606     mkdir $DIR/d0_runas_test
1607     chmod 0755 $DIR
1608     chown $myRUNAS_ID:$myRUNAS_ID $DIR/d0_runas_test
1609     $myRUNAS touch $DIR/d0_runas_test/f$$ || myRC=$?
1610     rm -rf $DIR/d0_runas_test
1611     return $myRC
1612 }
1613
1614 check_runas_id() {
1615     local myRUNAS_ID=$1
1616     shift
1617     local myRUNAS=$@
1618     check_runas_id_ret $myRUNAS_ID $myRUNAS || \
1619         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_ID. 
1620         Please set RUNAS_ID to some UID which exists on MDS and client or 
1621         add user $myRUNAS_ID:$myRUNAS_ID on these nodes."
1622 }
1623
1624 # Run multiop in the background, but wait for it to print
1625 # "PAUSING" to its stdout before returning from this function.
1626 multiop_bg_pause() {
1627     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
1628     FILE=$1
1629     ARGS=$2
1630
1631     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
1632     mkfifo $TMPPIPE
1633
1634     echo "$MULTIOP_PROG $FILE v$ARGS"
1635     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
1636
1637     echo "TMPPIPE=${TMPPIPE}"
1638     read -t 60 multiop_output < $TMPPIPE
1639     if [ $? -ne 0 ]; then
1640         rm -f $TMPPIPE
1641         return 1
1642     fi
1643     rm -f $TMPPIPE
1644     if [ "$multiop_output" != "PAUSING" ]; then
1645         echo "Incorrect multiop output: $multiop_output"
1646         kill -9 $PID
1647         return 1
1648     fi
1649
1650     return 0
1651 }
1652
1653 # reset llite stat counters
1654 clear_llite_stats(){
1655         lctl set_param -n llite.*.stats 0
1656 }
1657
1658 # sum llite stat items
1659 calc_llite_stats() {
1660         local res=$(lctl get_param -n llite.*.stats |
1661                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
1662         echo $res
1663 }
1664
1665 # save_lustre_params(node, parameter_mask)
1666 # generate a stream of formatted strings (<node> <param name>=<param value>)
1667 save_lustre_params() {
1668         local s
1669         do_node $1 "lctl get_param $2" | while read s; do echo "$1 $s"; done
1670 }
1671
1672 # restore lustre parameters from input stream, produces by save_lustre_params
1673 restore_lustre_params() {
1674         local node
1675         local name
1676         local val
1677         while IFS=" =" read node name val; do
1678                 do_node $node "lctl set_param -n $name $val"
1679         done
1680 }
1681