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