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