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