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