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     export AT_MAX_PATH
128
129     if [ "$ACCEPTOR_PORT" ]; then
130         export PORT_OPT="--port $ACCEPTOR_PORT"
131     fi
132
133     case "x$SEC" in
134         xkrb5*)
135             echo "Using GSS/krb5 ptlrpc security flavor"
136             GSS=true
137             GSS_KRB5=true
138             ;;
139     esac
140
141     case "x$IDUP" in
142         xtrue)
143             IDENTITY_UPCALL=true
144             ;;
145         xfalse)
146             IDENTITY_UPCALL=false
147             ;;
148     esac
149
150     # Paths on remote nodes, if different
151     export RLUSTRE=${RLUSTRE:-$LUSTRE}
152     export RPWD=${RPWD:-$PWD}
153     export I_MOUNTED=${I_MOUNTED:-"no"}
154
155     # command line
156
157     while getopts "rvf:" opt $*; do
158         case $opt in
159             f) CONFIG=$OPTARG;;
160             r) REFORMAT=--reformat;;
161             v) VERBOSE=true;;
162             \?) usage;;
163         esac
164     done
165
166     shift $((OPTIND - 1))
167     ONLY=${ONLY:-$*}
168
169     [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
170     rm -f $TMP/*active
171
172 }
173
174 load_module() {
175     EXT=".ko"
176     module=$1
177     shift
178     BASE=`basename $module $EXT`
179     lsmod | grep -q ${BASE} || \
180       if [ -f ${LUSTRE}/${module}${EXT} ]; then
181         insmod ${LUSTRE}/${module}${EXT} $@
182     else
183         # must be testing a "make install" or "rpm" installation
184         # note failed to load ptlrpc_gss is considered not fatal
185         if [ "$BASE" == "ptlrpc_gss" ]; then
186             modprobe $BASE $@ 2>/dev/null || echo "gss/krb5 is not supported"
187         else
188             modprobe $BASE $@
189         fi
190     fi
191 }
192
193 load_modules() {
194     if [ -n "$MODPROBE" ]; then
195         # use modprobe
196     return 0
197     fi
198     if [ "$HAVE_MODULES" = true ]; then
199     # we already loaded
200         return 0
201     fi
202     HAVE_MODULES=true
203
204     echo Loading modules from $LUSTRE
205     load_module ../libcfs/libcfs/libcfs
206     [ "$PTLDEBUG" ] && lctl set_param debug=$PTLDEBUG
207     [ "$SUBSYSTEM" ] && lctl set_param subsystem_debug=${SUBSYSTEM# }
208     [ -f /etc/modprobe.conf ] && MODPROBECONF=/etc/modprobe.conf
209     [ -f /etc/modprobe.d/Lustre ] && MODPROBECONF=/etc/modprobe.d/Lustre
210     [ -z "$LNETOPTS" -a -n "$MODPROBECONF" ] && \
211         LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
212     echo "lnet options: '$LNETOPTS'"
213     # note that insmod will ignore anything in modprobe.conf
214     load_module ../lnet/lnet/lnet $LNETOPTS
215     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
216     load_module ../lnet/klnds/$LNETLND
217     load_module lvfs/lvfs
218     load_module obdclass/obdclass
219     load_module ptlrpc/ptlrpc
220     load_module ptlrpc/gss/ptlrpc_gss
221     # Now, some modules depend on lquota without USE_QUOTA check,
222     # will fix later. Disable check "$USE_QUOTA" = "yes" temporary.
223     #[ "$USE_QUOTA" = "yes" ] && load_module quota/lquota
224     load_module quota/lquota
225     load_module fid/fid
226     load_module fld/fld
227     load_module lmv/lmv
228     load_module mdc/mdc
229     load_module osc/osc
230     load_module lov/lov
231     load_module mgc/mgc
232     if [ -z "$CLIENTONLY" ] && [ -z "$CLIENTMODSONLY" ]; then
233         grep -q crc16 /proc/kallsyms || { modprobe crc16 2>/dev/null || true; }
234         [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
235         load_module mgs/mgs
236         load_module mds/mds
237         load_module mdd/mdd
238         load_module mdt/mdt
239         load_module lvfs/fsfilt_$FSTYPE
240         load_module cmm/cmm
241         load_module osd/osd
242         load_module ost/ost
243         load_module obdfilter/obdfilter
244     fi
245
246     load_module llite/lustre
247     load_module llite/llite_lloop
248     rm -f $TMP/ogdb-$HOSTNAME
249     OGDB=$TMP
250     [ -d /r ] && OGDB="/r/tmp"
251     $LCTL modules > $OGDB/ogdb-$HOSTNAME
252
253     # 'mount' doesn't look in $PATH, just sbin
254     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
255 }
256
257 RMMOD=rmmod
258 if [ `uname -r | cut -c 3` -eq 4 ]; then
259     RMMOD="modprobe -r"
260 fi
261
262 wait_for_lnet() {
263     local UNLOADED=0
264     local WAIT=0
265     local MAX=60
266     MODULES=$($LCTL modules | awk '{ print $2 }')
267     while [ -n "$MODULES" ]; do
268     sleep 5
269     $RMMOD $MODULES > /dev/null 2>&1 || true
270     MODULES=$($LCTL modules | awk '{ print $2 }')
271         if [ -z "$MODULES" ]; then
272         return 0
273         else
274             WAIT=$((WAIT + 5))
275             echo "waiting, $((MAX - WAIT)) secs left"
276         fi
277         if [ $WAIT -eq $MAX ]; then
278             echo "LNET modules $MODULES will not unload"
279         lsmod
280             return 3
281         fi
282     done
283 }
284
285 unload_modules() {
286     wait_exit_ST client # bug 12845
287
288     lsmod | grep libcfs > /dev/null && $LCTL dl
289     local MODULES=$($LCTL modules | awk '{ print $2 }' | grep -v libcfs) || true
290     $RMMOD $MODULES > /dev/null 2>&1 || true
291      # do it again, in case we tried to unload ksocklnd too early
292     MODULES=$($LCTL modules | awk '{ print $2 }' | grep -v libcfs) || true
293     [ -n "$MODULES" ] && $RMMOD $MODULES > /dev/null 2>&1 || true
294     lsmod | grep libcfs > /dev/null && $LCTL dk $TMP/debug
295     $RMMOD libcfs
296     MODULES=$($LCTL modules | awk '{ print $2 }')
297     if [ -n "$MODULES" ]; then
298         echo "Modules still loaded: "
299         echo $MODULES 
300         if [ "$(lctl dl)" ]; then
301             echo "Lustre still loaded"
302             lctl dl || true
303             lsmod
304             return 2
305         else
306             echo "Lustre stopped but LNET is still loaded, waiting..."
307             wait_for_lnet || return 3
308         fi
309
310     fi
311     HAVE_MODULES=false
312
313     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd mem.*leaked" || true)
314     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
315     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
316         echo "$LEAK_LUSTRE" 1>&2
317         echo "$LEAK_PORTALS" 1>&2
318         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
319         echo "Memory leaks detected"
320         [ -n "$IGNORE_LEAK" ] && echo "ignoring leaks" && return 0
321         return 254
322     fi
323     echo "modules unloaded."
324     return 0
325 }
326
327 check_gss_daemon_facet() {
328     facet=$1
329     dname=$2
330
331     num=`do_facet $facet ps -o cmd -C $dname | grep $dname | wc -l`
332     if [ $num -ne 1 ]; then
333         echo "$num instance of $dname on $facet"
334         return 1
335     fi
336     return 0
337 }
338
339 send_sigint() {
340     local facet=$1
341     shift
342     do_facet $facet "killall -2 $@ 2>/dev/null || true"
343 }
344
345 start_gss_daemons() {
346     # starting on MDT
347     for num in `seq $MDSCOUNT`; do
348         do_facet mds$num "$LSVCGSSD -v"
349         if $GSS_PIPEFS; then
350             do_facet mds$num "$LGSSD -v"
351         fi
352     done
353     # starting on OSTs
354     for num in `seq $OSTCOUNT`; do
355         do_facet ost$num "$LSVCGSSD -v"
356     done
357     # starting on client
358     # FIXME: is "client" the right facet name?
359     if $GSS_PIPEFS; then
360         do_facet client "$LGSSD -v"
361     fi
362
363     # wait daemons entering "stable" status
364     sleep 5
365
366     #
367     # check daemons are running
368     #
369     for num in `seq $MDSCOUNT`; do
370         check_gss_daemon_facet mds$num lsvcgssd
371         if $GSS_PIPEFS; then
372             check_gss_daemon_facet mds$num lgssd
373         fi
374     done
375     for num in `seq $OSTCOUNT`; do
376         check_gss_daemon_facet ost$num lsvcgssd
377     done
378     if $GSS_PIPEFS; then
379         check_gss_daemon_facet client lgssd
380     fi
381 }
382
383 stop_gss_daemons() {
384     for num in `seq $MDSCOUNT`; do
385         send_sigint mds$num lsvcgssd lgssd
386     done
387     for num in `seq $OSTCOUNT`; do
388         send_sigint ost$num lsvcgssd
389     done
390     send_sigint client lgssd
391 }
392
393 init_gss() {
394     if $GSS; then
395         start_gss_daemons
396     fi
397 }
398
399 cleanup_gss() {
400     if $GSS; then
401         stop_gss_daemons
402         # maybe cleanup credential cache?
403     fi
404 }
405
406 mdsdevlabel() {
407     local num=$1
408     local device=`mdsdevname $num`
409     local label=`do_facet mds$num "e2label ${device}" | grep -v "CMD: "`
410     echo -n $label
411 }
412
413 ostdevlabel() {
414     local num=$1
415     local device=`ostdevname $num`
416     local label=`do_facet ost$num "e2label ${device}" | grep -v "CMD: "`
417     echo -n $label
418 }
419
420 # Facet functions
421 mount_facet() {
422     local facet=$1
423     shift
424     local dev=${facet}_dev
425     local opt=${facet}_opt
426     echo "Starting ${facet}: ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}"
427     do_facet ${facet} mount -t lustre ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}     
428     RC=${PIPESTATUS[0]}
429     if [ $RC -ne 0 ]; then
430         echo "mount -t lustre $@ ${!dev} ${MOUNT%/*}/${facet}"
431         echo "Start of ${!dev} on ${facet} failed ${RC}"
432     else
433         do_facet ${facet} "lctl set_param debug=$PTLDEBUG; \
434             lctl set_param subsystem_debug=${SUBSYSTEM# }; \
435             lctl set_param debug_mb=${DEBUG_SIZE}; \
436             sync"
437  
438         label=$(do_facet ${facet} "e2label ${!dev}")
439         [ -z "$label" ] && echo no label for ${!dev} && exit 1
440         eval export ${facet}_svc=${label}
441         echo Started ${label}
442     fi
443     return $RC
444 }
445
446 # start facet device options
447 start() {
448     facet=$1
449     shift
450     device=$1
451     shift
452     eval export ${facet}_dev=${device}
453     eval export ${facet}_opt=\"$@\"
454     do_facet ${facet} mkdir -p ${MOUNT%/*}/${facet}
455     mount_facet ${facet}
456     RC=$?
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 # Adaptive Timeouts funcs
1251
1252 at_is_valid() {
1253     if [ -z "$AT_MAX_PATH" ]; then
1254         AT_MAX_PATH=$(do_facet mds "find /sys/ -name at_max")
1255         [ -z "$AT_MAX_PATH" ] && echo "missing /sys/.../at_max " && return 1
1256     fi
1257     return 0
1258 }
1259
1260 at_is_enabled() {
1261     at_is_valid || error "invalid call"
1262
1263     # only check mds, we assume at_max is the same on all nodes
1264     local at_max=$(do_facet mds "cat $AT_MAX_PATH")
1265     if [ $at_max -eq 0 ]; then
1266         return 1
1267     else
1268         return 0
1269     fi
1270 }
1271
1272 at_max_get() {
1273     at_is_valid || error "invalid call"
1274
1275     do_facet $1 "cat $AT_MAX_PATH"
1276 }
1277
1278 at_max_set() {
1279     local at_max=$1
1280     shift
1281
1282     at_is_valid || error "invalid call"
1283
1284     for facet in $@; do
1285         if [ $facet == "ost" ]; then
1286             for i in `seq $OSTCOUNT`; do
1287                 do_facet ost$i "echo $at_max > $AT_MAX_PATH"
1288             done
1289         elif [ $facet == "mds" ]; then
1290             for i in `seq $MDSCOUNT`; do
1291                 do_facet mds$i "echo $at_max > $AT_MAX_PATH"
1292             done
1293         else
1294             do_facet $facet "echo $at_max > $AT_MAX_PATH"
1295         fi
1296     done
1297 }
1298
1299 ##################################
1300 # OBD_FAIL funcs
1301
1302 drop_request() {
1303 # OBD_FAIL_MDS_ALL_REQUEST_NET
1304     RC=0
1305     do_facet mds lctl set_param fail_loc=0x123
1306     do_facet client "$1" || RC=$?
1307     do_facet mds lctl set_param fail_loc=0
1308     return $RC
1309 }
1310
1311 drop_reply() {
1312 # OBD_FAIL_MDS_ALL_REPLY_NET
1313     RC=0
1314     do_facet mds lctl set_param fail_loc=0x122
1315     do_facet client "$@" || RC=$?
1316     do_facet mds lctl set_param fail_loc=0
1317     return $RC
1318 }
1319
1320 drop_reint_reply() {
1321 # OBD_FAIL_MDS_REINT_NET_REP
1322     RC=0
1323     do_facet mds lctl set_param fail_loc=0x119
1324     do_facet client "$@" || RC=$?
1325     do_facet mds lctl set_param fail_loc=0
1326     return $RC
1327 }
1328
1329 pause_bulk() {
1330 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
1331     RC=0
1332     do_facet ost1 lctl set_param fail_loc=0x214
1333     do_facet client "$1" || RC=$?
1334     do_facet client "sync"
1335     do_facet ost1 lctl set_param fail_loc=0
1336     return $RC
1337 }
1338
1339 drop_ldlm_cancel() {
1340 #define OBD_FAIL_LDLM_CANCEL             0x304
1341     RC=0
1342     do_facet client lctl set_param fail_loc=0x304
1343     do_facet client "$@" || RC=$?
1344     do_facet client lctl set_param fail_loc=0
1345     return $RC
1346 }
1347
1348 drop_bl_callback() {
1349 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
1350     RC=0
1351     do_facet client lctl set_param fail_loc=0x305
1352     do_facet client "$@" || RC=$?
1353     do_facet client lctl set_param fail_loc=0
1354     return $RC
1355 }
1356
1357 drop_ldlm_reply() {
1358 #define OBD_FAIL_LDLM_REPLY              0x30c
1359     RC=0
1360     do_facet mds lctl set_param fail_loc=0x30c
1361     do_facet client "$@" || RC=$?
1362     do_facet mds lctl set_param fail_loc=0
1363     return $RC
1364 }
1365
1366 clear_failloc() {
1367     facet=$1
1368     pause=$2
1369     sleep $pause
1370     echo "clearing fail_loc on $facet"
1371     do_facet $facet "lctl set_param fail_loc=0 2>/dev/null || true"
1372 }
1373
1374 set_nodes_failloc () {
1375     local nodes=$1
1376     local node
1377
1378     for node in $nodes ; do
1379         do_node $node lctl set_param fail_loc=$2
1380     done
1381 }
1382
1383 set_nodes_failloc () {
1384     local nodes=$1
1385     local node
1386
1387     for node in $nodes ; do
1388         do_node $node lctl set_param fail_loc=$2
1389     done
1390 }
1391
1392 cancel_lru_locks() {
1393     $LCTL mark "cancel_lru_locks $1 start"
1394     lctl set_param ldlm.namespaces.*$1*.lru_size=0
1395     lctl get_param ldlm.namespaces.*$1*.lock_unused_count | grep -v '=0'
1396     $LCTL mark "cancel_lru_locks $1 stop"
1397 }
1398
1399 default_lru_size()
1400 {
1401         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
1402         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
1403         echo "$DEFAULT_LRU_SIZE"
1404 }
1405
1406 lru_resize_enable()
1407 {
1408     lctl set_param ldlm.namespaces.*$1*.lru_size=0
1409 }
1410
1411 lru_resize_disable()
1412 {
1413     lctl set_param ldlm.namespaces.*$1*.lru_size $(default_lru_size)
1414 }
1415
1416 pgcache_empty() {
1417     local FILE
1418     for FILE in `lctl get_param -N "llite.*.dump_page_cache"`; do
1419         if [ `lctl get_param -n $FILE | wc -l` -gt 1 ]; then
1420             echo there is still data in page cache $FILE ?
1421             lctl get_param -n $FILE
1422             return 1
1423         fi
1424     done
1425     return 0
1426 }
1427
1428 debugsave() {
1429     DEBUGSAVE="$(lctl get_param -n debug)"
1430 }
1431
1432 debugrestore() {
1433     [ -n "$DEBUGSAVE" ] && lctl set_param debug="${DEBUGSAVE}"
1434     DEBUGSAVE=""
1435 }
1436
1437 ##################################
1438 # Test interface
1439 ##################################
1440
1441 error_noexit() {
1442     local TYPE=${TYPE:-"FAIL"}
1443     local ERRLOG
1444     lctl set_param fail_loc=0 2>/dev/null || true
1445     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
1446     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
1447     echo "Dumping lctl log to $ERRLOG"
1448     # We need to dump the logs on all nodes
1449     local NODES=$(nodes_list)
1450     for NODE in $NODES; do
1451         do_node $NODE $LCTL dk $ERRLOG
1452     done
1453     debugrestore
1454     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
1455 }
1456
1457 error() {
1458     error_noexit "$@"
1459     $FAIL_ON_ERROR && exit 1 || true
1460 }
1461
1462 error_exit() {
1463     error_noexit "$@"
1464     exit 1
1465 }
1466
1467 # use only if we are ignoring failures for this test, bugno required.
1468 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
1469 # e.g. error_ignore 5494 "your message"
1470 error_ignore() {
1471     local TYPE="IGNORE (bz$1)"
1472     shift
1473     error_noexit "$@"
1474 }
1475
1476 skip () {
1477         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
1478         [ "$TESTSUITELOG" ] && echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG
1479 }
1480
1481 build_test_filter() {
1482     [ "$ONLY" ] && log "only running test `echo $ONLY`"
1483     for O in $ONLY; do
1484         eval ONLY_${O}=true
1485     done
1486     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
1487         log "excepting tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
1488     [ "$EXCEPT_SLOW" ] && \
1489         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
1490     for E in $EXCEPT $ALWAYS_EXCEPT; do
1491         eval EXCEPT_${E}=true
1492     done
1493     for E in $EXCEPT_SLOW; do
1494         eval EXCEPT_SLOW_${E}=true
1495     done
1496     for G in $GRANT_CHECK_LIST; do
1497         eval GCHECK_ONLY_${G}=true
1498         done
1499 }
1500
1501 _basetest() {
1502     echo $*
1503 }
1504
1505 basetest() {
1506     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
1507 }
1508
1509 run_test() {
1510     assert_DIR
1511
1512     export base=`basetest $1`
1513     if [ ! -z "$ONLY" ]; then
1514         testname=ONLY_$1
1515         if [ ${!testname}x != x ]; then
1516             run_one $1 "$2"
1517             return $?
1518         fi
1519         testname=ONLY_$base
1520         if [ ${!testname}x != x ]; then
1521             run_one $1 "$2"
1522             return $?
1523         fi
1524         echo -n "."
1525         return 0
1526     fi
1527     testname=EXCEPT_$1
1528     if [ ${!testname}x != x ]; then
1529         TESTNAME=test_$1 skip "skipping excluded test $1"
1530         return 0
1531     fi
1532     testname=EXCEPT_$base
1533     if [ ${!testname}x != x ]; then
1534         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
1535         return 0
1536     fi
1537     testname=EXCEPT_SLOW_$1
1538     if [ ${!testname}x != x ]; then
1539         TESTNAME=test_$1 skip "skipping SLOW test $1"
1540         return 0
1541     fi
1542     testname=EXCEPT_SLOW_$base
1543     if [ ${!testname}x != x ]; then
1544         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
1545         return 0
1546     fi
1547
1548     run_one $1 "$2"
1549
1550     return $?
1551 }
1552
1553 EQUALS="======================================================================"
1554 equals_msg() {
1555     msg="$@"
1556
1557     local suffixlen=$((${#EQUALS} - ${#msg}))
1558     [ $suffixlen -lt 5 ] && suffixlen=5
1559     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
1560 }
1561
1562 log() {
1563     echo "$*"
1564     lsmod | grep lnet > /dev/null || load_modules
1565
1566     local MSG="$*"
1567     # Get rif of '
1568     MSG=${MSG//\'/\\\'}
1569     MSG=${MSG//\(/\\\(}
1570     MSG=${MSG//\)/\\\)}
1571     MSG=${MSG//\;/\\\;}
1572     MSG=${MSG//\|/\\\|}
1573     MSG=${MSG//\>/\\\>}
1574     MSG=${MSG//\</\\\<}
1575     local NODES=$(nodes_list)
1576     for NODE in $NODES; do
1577         do_node $NODE $LCTL mark "$MSG" 2> /dev/null || true
1578     done
1579 }
1580
1581 trace() {
1582         log "STARTING: $*"
1583         strace -o $TMP/$1.strace -ttt $*
1584         RC=$?
1585         log "FINISHED: $*: rc $RC"
1586         return 1
1587 }
1588
1589 pass() {
1590     echo PASS $@
1591 }
1592
1593 check_mds() {
1594     FFREE=`lctl get_param -n osd.*MDT*.filesfree`
1595     FTOTAL=`lctl get_param -n osd.*MDT*.filestotal`
1596     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
1597 }
1598
1599 reset_fail_loc () {
1600     local myNODES=$(nodes_list)
1601     local NODE
1602
1603     for NODE in $myNODES; do
1604         do_node $NODE "lctl set_param fail_loc=0 2>/dev/null || true"
1605     done
1606 }
1607
1608 run_one() {
1609     testnum=$1
1610     message=$2
1611     tfile=f${testnum}
1612     export tdir=d0.${TESTSUITE}/d${base}
1613     local SAVE_UMASK=`umask`
1614     umask 0022
1615
1616     BEFORE=`date +%s`
1617     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
1618     #check_mds
1619     export TESTNAME=test_$testnum
1620     test_${testnum} || error "test_$testnum failed with $?"
1621     #check_mds
1622     cd $SAVE_PWD
1623     reset_fail_loc
1624     check_grant ${testnum} || error "check_grant $testnum failed with $?"
1625     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
1626         error "LBUG/LASSERT detected"
1627     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
1628     pass "($((`date +%s` - $BEFORE))s)"
1629     unset TESTNAME
1630     unset tdir
1631     umask $SAVE_UMASK
1632     $CLEANUP
1633 }
1634
1635 canonical_path() {
1636     (cd `dirname $1`; echo $PWD/`basename $1`)
1637 }
1638
1639 sync_clients() {
1640     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync
1641     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync
1642         cd $SAVE_PWD
1643 }
1644
1645 check_grant() {
1646     export base=`basetest $1`
1647     [ "$CHECK_GRANT" == "no" ] && return 0
1648
1649         testname=GCHECK_ONLY_${base}
1650         [ ${!testname}x == x ] && return 0
1651
1652     echo -n "checking grant......"
1653         cd $SAVE_PWD
1654         # write some data to sync client lost_grant
1655         rm -f $DIR1/${tfile}_check_grant_* 2>&1
1656         for i in `seq $OSTCOUNT`; do
1657                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
1658                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
1659                                               count=1 > /dev/null 2>&1
1660         done
1661         # sync all the data and make sure no pending data on server
1662         sync_clients
1663         
1664         #get client grant and server grant
1665         client_grant=0
1666     for d in `lctl get_param -n osc.*.cur_grant_bytes`; do
1667                 client_grant=$((client_grant + $d))
1668         done
1669         server_grant=0
1670         for d in `lctl get_param -n obdfilter.*.tot_granted`; do
1671                 server_grant=$((server_grant + $d))
1672         done
1673
1674         # cleanup the check_grant file
1675         for i in `seq $OSTCOUNT`; do
1676                 rm $DIR1/${tfile}_check_grant_$i
1677         done
1678
1679         #check whether client grant == server grant
1680         if [ $client_grant != $server_grant ]; then
1681                 echo "failed: client:${client_grant} server: ${server_grant}"
1682                 return 1
1683         else
1684                 echo "pass"
1685         fi
1686 }
1687
1688 ########################
1689 # helper functions
1690
1691 osc_to_ost()
1692 {
1693     osc=$1
1694     ost=`echo $1 | awk -F_ '{print $3}'`
1695     if [ -z $ost ]; then
1696         ost=`echo $1 | sed 's/-osc.*//'`
1697     fi
1698     echo $ost
1699 }
1700
1701 remote_mds ()
1702 {
1703     [ -z "$(lctl dl | grep mdt)" ]
1704 }
1705
1706 remote_mds_nodsh()
1707 {
1708     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
1709 }
1710
1711 remote_ost ()
1712 {
1713     [ -z "$(lctl dl | grep ost)" ]
1714 }
1715
1716 remote_ost_nodsh()
1717 {
1718     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
1719 }
1720
1721 mdts_nodes () {
1722     local MDSNODES=$(facet_host $SINGLEMDS)
1723     local NODES_sort
1724
1725     # FIXME: Currenly we use only $SINGLEMDS,
1726     # should be fixed when we will start to test cmd.
1727     echo $MDSNODES
1728     return
1729
1730     for num in `seq $MDSCOUNT`; do
1731         local myMDS=$(facet_host mds$num)
1732         MDSNODES="$MDSNODES $myMDS"
1733     done
1734     NODES_sort=$(for i in $MDSNODES; do echo $i; done | sort -u)
1735
1736     echo $NODES_sort
1737 }
1738
1739 osts_nodes () {
1740     local OSTNODES=$(facet_host ost1)
1741     local NODES_sort
1742
1743     for num in `seq $OSTCOUNT`; do
1744         local myOST=$(facet_host ost$num)
1745         OSTNODES="$OSTNODES $myOST"
1746     done
1747     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
1748
1749     echo $NODES_sort
1750 }
1751
1752 nodes_list () {
1753     # FIXME. We need a list of clients
1754     local myNODES=$HOSTNAME
1755     local myNODES_sort
1756
1757     # CLIENTS (if specified) contains the local client
1758     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
1759
1760     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
1761         myNODES="$myNODES $(osts_nodes) $(mdts_nodes)"
1762     fi
1763
1764     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
1765
1766     echo $myNODES_sort
1767 }
1768
1769 is_patchless ()
1770 {
1771     lctl get_param version | grep -q patchless
1772 }
1773
1774 get_node_count() {
1775    local nodes="$@"
1776    echo $nodes | wc -w || true
1777 }
1778
1779 mixed_ost_devs () {
1780     local nodes=$(osts_nodes)
1781     local osscount=$(get_node_count "$nodes")
1782     [ ! "$OSTCOUNT" = "$osscount" ]
1783 }
1784
1785 check_runas_id_ret() {
1786     local myRC=0
1787     local myRUNAS_ID=$1
1788     shift
1789     local myRUNAS=$@
1790     if [ -z "$myRUNAS" ]; then
1791         error_exit "myRUNAS command must be specified for check_runas_id"
1792     fi
1793     if $GSS_KRB5; then
1794         $myRUNAS krb5_login.sh || \
1795             error "Failed to refresh Kerberos V5 TGT for UID $myRUNAS_ID."
1796     fi
1797     mkdir $DIR/d0_runas_test
1798     chmod 0755 $DIR
1799     chown $myRUNAS_ID:$myRUNAS_ID $DIR/d0_runas_test
1800     $myRUNAS touch $DIR/d0_runas_test/f$$ || myRC=$?
1801     rm -rf $DIR/d0_runas_test
1802     return $myRC
1803 }
1804
1805 check_runas_id() {
1806     local myRUNAS_ID=$1
1807     shift
1808     local myRUNAS=$@
1809     check_runas_id_ret $myRUNAS_ID $myRUNAS || \
1810         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_ID. 
1811         Please set RUNAS_ID to some UID which exists on MDS and client or 
1812         add user $myRUNAS_ID:$myRUNAS_ID on these nodes."
1813 }
1814
1815 # Run multiop in the background, but wait for it to print
1816 # "PAUSING" to its stdout before returning from this function.
1817 multiop_bg_pause() {
1818     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
1819     FILE=$1
1820     ARGS=$2
1821
1822     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
1823     mkfifo $TMPPIPE
1824
1825     echo "$MULTIOP_PROG $FILE v$ARGS"
1826     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
1827
1828     echo "TMPPIPE=${TMPPIPE}"
1829     read -t 60 multiop_output < $TMPPIPE
1830     if [ $? -ne 0 ]; then
1831         rm -f $TMPPIPE
1832         return 1
1833     fi
1834     rm -f $TMPPIPE
1835     if [ "$multiop_output" != "PAUSING" ]; then
1836         echo "Incorrect multiop output: $multiop_output"
1837         kill -9 $PID
1838         return 1
1839     fi
1840
1841     return 0
1842 }
1843
1844 # reset llite stat counters
1845 clear_llite_stats(){
1846         lctl set_param -n llite.*.stats 0
1847 }
1848
1849 # sum llite stat items
1850 calc_llite_stats() {
1851         local res=$(lctl get_param -n llite.*.stats |
1852                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
1853         echo $res
1854 }
1855
1856 # save_lustre_params(node, parameter_mask)
1857 # generate a stream of formatted strings (<node> <param name>=<param value>)
1858 save_lustre_params() {
1859         local s
1860         do_node $1 "lctl get_param $2" | while read s; do echo "$1 $s"; done
1861 }
1862
1863 # restore lustre parameters from input stream, produces by save_lustre_params
1864 restore_lustre_params() {
1865         local node
1866         local name
1867         local val
1868         while IFS=" =" read node name val; do
1869                 do_node $node "lctl set_param -n $name $val"
1870         done
1871 }
1872