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