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