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