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