Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / tests / test-framework.sh
1 #!/bin/bash
2 # vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:
3
4 trap 'print_summary && echo "test-framework exiting on error"' ERR
5 set -e
6 #set -x
7
8
9 export REFORMAT=${REFORMAT:-""}
10 export 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 export QUOTA_AUTO=1
19
20 #export PDSH="pdsh -S -Rssh -w"
21
22 # function used by scripts run on remote nodes
23 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
24 . $LUSTRE/tests/functions.sh
25
26 LUSTRE_TESTS_CFG_DIR=${LUSTRE_TESTS_CFG_DIR:-${LUSTRE}/tests/cfg}
27
28 EXCEPT_LIST_FILE=${EXCEPT_LIST_FILE:-${LUSTRE_TESTS_CFG_DIR}/tests-to-skip.sh}
29
30 if [ -f "$EXCEPT_LIST_FILE" ]; then
31     echo "Reading test skip list from $EXCEPT_LIST_FILE"
32     cat $EXCEPT_LIST_FILE
33     . $EXCEPT_LIST_FILE
34 fi
35
36 assert_DIR () {
37     local failed=""
38     [[ $DIR/ = $MOUNT/* ]] || \
39         { failed=1 && echo "DIR=$DIR not in $MOUNT. Aborting."; }
40     [[ $DIR1/ = $MOUNT1/* ]] || \
41         { failed=1 && echo "DIR1=$DIR1 not in $MOUNT1. Aborting."; }
42     [[ $DIR2/ = $MOUNT2/* ]] || \
43         { failed=1 && echo "DIR2=$DIR2 not in $MOUNT2. Aborting"; }
44
45     [ -n "$failed" ] && exit 99 || true
46 }
47
48 usage() {
49     echo "usage: $0 [-r] [-f cfgfile]"
50     echo "       -r: reformat"
51
52     exit
53 }
54
55 print_summary () {
56     trap 0
57     [ "$TESTSUITE" == "lfscktest" ] && return 0
58     [ -n "$ONLY" ] && echo "WARNING: ONLY is set to ${ONLY}."
59     local form="%-13s %-17s %s\n"
60     printf "$form" "status" "script" "skipped tests E(xcluded) S(low)"
61     echo "------------------------------------------------------------------------------------"
62     for O in $TESTSUITE_LIST; do
63         local skipped=""
64         local slow=""
65         local o=$(echo $O | tr "[:upper:]" "[:lower:]")
66         o=${o//_/-}
67         o=${o//tyn/tyN}
68         local log=${TMP}/${o}.log
69         [ -f $log ] && skipped=$(grep excluded $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
70         [ -f $log ] && slow=$(grep SLOW $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
71         [ "${!O}" = "done" ] && \
72             printf "$form" "Done" "$O" "E=$skipped" && \
73             [ -n "$slow" ] && printf "$form" "-" "-" "S=$slow"
74
75     done
76
77     for O in $TESTSUITE_LIST; do
78         if [ "${!O}" = "no" ]; then
79             # FIXME.
80             # only for those tests suits which are run directly from acc-sm script:
81             # bonnie, iozone, etc.
82             if [ -f "$TESTSUITELOG" ] && grep FAIL $TESTSUITELOG | grep -q ' '$O  ; then
83                printf "$form" "UNFINISHED" "$O" ""  
84             else
85                printf "$form" "Skipped" "$O" ""
86             fi
87         fi
88     done
89
90     for O in $TESTSUITE_LIST; do
91         [ "${!O}" = "done" -o "${!O}" = "no" ] || \
92             printf "$form" "UNFINISHED" "$O" ""
93     done
94 }
95
96 init_test_env() {
97     export LUSTRE=`absolute_path $LUSTRE`
98     export TESTSUITE=`basename $0 .sh`
99     export TEST_FAILED=false
100     export FAIL_ON_SKIP_ENV=${FAIL_ON_SKIP_ENV:-false}
101
102     export MKE2FS=${MKE2FS:-mke2fs}
103     export DEBUGFS=${DEBUGFS:-debugfs}
104     export TUNE2FS=${TUNE2FS:-tune2fs}
105     export E2LABEL=${E2LABEL:-e2label}
106     export DUMPE2FS=${DUMPE2FS:-dumpe2fs}
107     export E2FSCK=${E2FSCK:-e2fsck}
108
109     #[ -d /r ] && export ROOT=${ROOT:-/r}
110     export TMP=${TMP:-$ROOT/tmp}
111     export TESTSUITELOG=${TMP}/${TESTSUITE}.log
112     export HOSTNAME=${HOSTNAME:-`hostname`}
113     if ! echo $PATH | grep -q $LUSTRE/utils; then
114         export PATH=$PATH:$LUSTRE/utils
115     fi
116     if ! echo $PATH | grep -q $LUSTRE/utils/gss; then
117         export PATH=$PATH:$LUSTRE/utils/gss
118     fi
119     if ! echo $PATH | grep -q $LUSTRE/tests; then
120         export PATH=$PATH:$LUSTRE/tests
121     fi
122     export MDSRATE=${MDSRATE:-"$LUSTRE/tests/mpi/mdsrate"}
123     [ ! -f "$MDSRATE" ] && export MDSRATE=$(which mdsrate 2> /dev/null)
124     if ! echo $PATH | grep -q $LUSTRE/tests/racer; then
125         export PATH=$PATH:$LUSTRE/tests/racer
126     fi
127     if ! echo $PATH | grep -q $LUSTRE/tests/mpi; then
128         export PATH=$PATH:$LUSTRE/tests/mpi
129     fi
130     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
131     [ ! -f "$LCTL" ] && export LCTL=$(which lctl)
132     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
133     [ ! -f "$LFS" ] && export LFS=$(which lfs)
134     export L_GETIDENTITY=${L_GETIDENTITY:-"$LUSTRE/utils/l_getidentity"}
135     if [ ! -f "$L_GETIDENTITY" ]; then
136         if `which l_getidentity > /dev/null 2>&1`; then
137             export L_GETIDENTITY=$(which l_getidentity)
138         else
139             export L_GETIDENTITY=NONE
140         fi
141     fi
142     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
143     [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre)
144     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
145     [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre)
146     export CHECKSTAT="${CHECKSTAT:-"checkstat -v"} "
147     export LUSTRE_RMMOD=${LUSTRE_RMMOD:-$LUSTRE/scripts/lustre_rmmod}
148     [ ! -f "$LUSTRE_RMMOD" ] && export LUSTRE_RMMOD=$(which lustre_rmmod 2> /dev/null)
149     export FSTYPE=${FSTYPE:-"ldiskfs"}
150     export NAME=${NAME:-local}
151     export LGSSD=${LGSSD:-"$LUSTRE/utils/gss/lgssd"}
152     [ "$GSS_PIPEFS" = "true" ] && [ ! -f "$LGSSD" ] && \
153         export LGSSD=$(which lgssd)
154     export LSVCGSSD=${LSVCGSSD:-"$LUSTRE/utils/gss/lsvcgssd"}
155     [ ! -f "$LSVCGSSD" ] && export LSVCGSSD=$(which lsvcgssd 2> /dev/null)
156     export KRB5DIR=${KRB5DIR:-"/usr/kerberos"}
157     export DIR2
158     export SAVE_PWD=${SAVE_PWD:-$LUSTRE/tests}
159     export AT_MAX_PATH
160
161     if [ "$ACCEPTOR_PORT" ]; then
162         export PORT_OPT="--port $ACCEPTOR_PORT"
163     fi
164
165     case "x$SEC" in
166         xkrb5*)
167             echo "Using GSS/krb5 ptlrpc security flavor"
168             which lgss_keyring > /dev/null 2>&1 || \
169                 error "built with gss disabled! SEC=$SEC"
170             GSS=true
171             GSS_KRB5=true
172             ;;
173     esac
174
175     case "x$IDUP" in
176         xtrue)
177             IDENTITY_UPCALL=true
178             ;;
179         xfalse)
180             IDENTITY_UPCALL=false
181             ;;
182     esac
183     export LOAD_MODULES_REMOTE=${LOAD_MODULES_REMOTE:-false}
184
185     # Paths on remote nodes, if different
186     export RLUSTRE=${RLUSTRE:-$LUSTRE}
187     export RPWD=${RPWD:-$PWD}
188     export I_MOUNTED=${I_MOUNTED:-"no"}
189     if [ ! -f /lib/modules/$(uname -r)/kernel/fs/lustre/mds.ko -a \
190         ! -f `dirname $0`/../mds/mds.ko ]; then
191         export CLIENTMODSONLY=yes
192     fi
193
194     # command line
195
196     while getopts "rvwf:" opt $*; do
197         case $opt in
198             f) CONFIG=$OPTARG;;
199             r) REFORMAT=--reformat;;
200             v) VERBOSE=true;;
201             w) WRITECONF=writeconf;;
202             \?) usage;;
203         esac
204     done
205
206     shift $((OPTIND - 1))
207     ONLY=${ONLY:-$*}
208
209     [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
210     rm -f $TMP/*active
211 }
212
213 case `uname -r` in
214 2.4.*) EXT=".o"; USE_QUOTA=no; [ ! "$CLIENTONLY" ] && FSTYPE=ext3;;
215     *) EXT=".ko"; USE_QUOTA=yes;;
216 esac
217
218
219 module_loaded () {
220    /sbin/lsmod | grep -q $1
221 }
222
223 load_module() {
224     EXT=".ko"
225     module=$1
226     shift
227     BASE=`basename $module $EXT`
228
229     module_loaded ${BASE} && return
230
231     if [ -f ${LUSTRE}/${module}${EXT} ]; then
232         insmod ${LUSTRE}/${module}${EXT} $@
233     else
234         # must be testing a "make install" or "rpm" installation
235         # note failed to load ptlrpc_gss is considered not fatal
236         if [ "$BASE" == "ptlrpc_gss" ]; then
237             modprobe $BASE $@ 2>/dev/null || echo "gss/krb5 is not supported"
238         else
239             modprobe $BASE $@
240         fi
241     fi
242 }
243
244 load_modules_local() {
245     if [ -n "$MODPROBE" ]; then
246         # use modprobe
247     return 0
248     fi
249     if [ "$HAVE_MODULES" = true ]; then
250     # we already loaded
251         return 0
252     fi
253     HAVE_MODULES=true
254
255     echo Loading modules from $LUSTRE
256     load_module ../libcfs/libcfs/libcfs
257     [ "$PTLDEBUG" ] && lctl set_param debug="$PTLDEBUG"
258     [ "$SUBSYSTEM" ] && lctl set_param subsystem_debug="${SUBSYSTEM# }"
259     local MODPROBECONF=
260     [ -f /etc/modprobe.conf ] && MODPROBECONF=/etc/modprobe.conf
261     [ ! "$MODPROBECONF" -a -d /etc/modprobe.d ] && MODPROBECONF=/etc/modprobe.d/Lustre
262     [ -z "$LNETOPTS" -a "$MODPROBECONF" ] && \
263         LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
264     echo $LNETOPTS | grep -q "accept=all"  || LNETOPTS="$LNETOPTS accept=all";
265     echo "lnet options: '$LNETOPTS'"
266     # note that insmod will ignore anything in modprobe.conf
267     load_module ../lnet/lnet/lnet $LNETOPTS
268     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
269     load_module ../lnet/klnds/$LNETLND
270     load_module lvfs/lvfs
271     load_module obdclass/obdclass
272     load_module ptlrpc/ptlrpc
273     load_module ptlrpc/gss/ptlrpc_gss
274     [ "$USE_QUOTA" = "yes" -a "$LQUOTA" != "no" ] && load_module quota/lquota
275     load_module fld/fld
276     load_module fid/fid
277     load_module lmv/lmv
278     load_module mdc/mdc
279     load_module osc/osc
280     load_module lov/lov
281     load_module mgc/mgc
282     if ! client_only; then
283         grep -q crc16 /proc/kallsyms || { modprobe crc16 2>/dev/null || true; }
284         grep -q jbd /proc/kallsyms || { modprobe jbd 2>/dev/null || true; }
285         [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
286         load_module mgs/mgs
287         load_module mds/mds
288         load_module mdd/mdd
289         load_module mdt/mdt
290         load_module lvfs/fsfilt_$FSTYPE
291         load_module cmm/cmm
292         load_module osd/osd
293         load_module ost/ost
294         load_module obdfilter/obdfilter
295     fi
296
297     load_module llite/lustre
298     load_module llite/llite_lloop
299     [ -d /r ] && OGDB=${OGDB:-"/r/tmp"}
300     OGDB=${OGDB:-$TMP}
301     rm -f $OGDB/ogdb-$HOSTNAME
302     $LCTL modules > $OGDB/ogdb-$HOSTNAME
303
304     # 'mount' doesn't look in $PATH, just sbin
305     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
306 }
307
308 load_modules () {
309     load_modules_local
310     # bug 19124
311     # load modules on remote nodes optionally
312     # lustre-tests have to be installed on these nodes
313     if $LOAD_MODULES_REMOTE ; then
314         local list=$(comma_list $(remote_nodes_list))
315         echo loading modules on $list
316         do_rpc_nodes $list load_modules 
317     fi
318 }
319
320 check_mem_leak () {
321     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd_memory.*leaked" || true)
322     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
323     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
324         echo "$LEAK_LUSTRE" 1>&2
325         echo "$LEAK_PORTALS" 1>&2
326         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
327         echo "Memory leaks detected"
328         [ -n "$IGNORE_LEAK" ] && { echo "ignoring leaks" && return 0; } || true
329         return 1
330     fi
331 }
332
333 unload_modules() {
334     wait_exit_ST client # bug 12845
335
336     $LUSTRE_RMMOD $FSTYPE || return 2
337
338     if $LOAD_MODULES_REMOTE ; then
339         local list=$(comma_list $(remote_nodes_list))
340         echo unloading modules on $list
341         do_rpc_nodes $list $LUSTRE_RMMOD $FSTYPE
342         do_rpc_nodes $list check_mem_leak
343     fi
344
345     HAVE_MODULES=false
346
347     check_mem_leak || return 254
348
349     echo "modules unloaded."
350     return 0
351 }
352
353 check_gss_daemon_facet() {
354     facet=$1
355     dname=$2
356
357     num=`do_facet $facet ps -o cmd -C $dname | grep $dname | wc -l`
358     if [ $num -ne 1 ]; then
359         echo "$num instance of $dname on $facet"
360         return 1
361     fi
362     return 0
363 }
364
365 send_sigint() {
366     local facet=$1
367     shift
368     do_facet $facet "killall -2 $@ 2>/dev/null || true"
369 }
370
371 start_gss_daemons() {
372     # starting on MDT
373     for num in `seq $MDSCOUNT`; do
374         do_facet mds$num "$LSVCGSSD -v"
375         if $GSS_PIPEFS; then
376             do_facet mds$num "$LGSSD -v"
377         fi
378     done
379     # starting on OSTs
380     for num in `seq $OSTCOUNT`; do
381         do_facet ost$num "$LSVCGSSD -v"
382     done
383     # starting on client
384     # FIXME: is "client" the right facet name?
385     if $GSS_PIPEFS; then
386         do_facet client "$LGSSD -v"
387     fi
388
389     # wait daemons entering "stable" status
390     sleep 5
391
392     #
393     # check daemons are running
394     #
395     for num in `seq $MDSCOUNT`; do
396         check_gss_daemon_facet mds$num lsvcgssd
397         if $GSS_PIPEFS; then
398             check_gss_daemon_facet mds$num lgssd
399         fi
400     done
401     for num in `seq $OSTCOUNT`; do
402         check_gss_daemon_facet ost$num lsvcgssd
403     done
404     if $GSS_PIPEFS; then
405         check_gss_daemon_facet client lgssd
406     fi
407 }
408
409 stop_gss_daemons() {
410     for num in `seq $MDSCOUNT`; do
411         send_sigint mds$num lsvcgssd lgssd
412     done
413     for num in `seq $OSTCOUNT`; do
414         send_sigint ost$num lsvcgssd
415     done
416     send_sigint client lgssd
417 }
418
419 init_gss() {
420     if $GSS; then
421         start_gss_daemons
422
423         if [ -n "$LGSS_KEYRING_DEBUG" ]; then
424             echo $LGSS_KEYRING_DEBUG > /proc/fs/lustre/sptlrpc/gss/lgss_keyring/debug_level
425         fi
426     fi
427 }
428
429 cleanup_gss() {
430     if $GSS; then
431         stop_gss_daemons
432         # maybe cleanup credential cache?
433     fi
434 }
435
436 mdsdevlabel() {
437     local num=$1
438     local device=`mdsdevname $num`
439     local label=`do_facet mds$num "e2label ${device}" | grep -v "CMD: "`
440     echo -n $label
441 }
442
443 ostdevlabel() {
444     local num=$1
445     local device=`ostdevname $num`
446     local label=`do_facet ost$num "e2label ${device}" | grep -v "CMD: "`
447     echo -n $label
448 }
449
450 # Facet functions
451 mount_facet() {
452     local facet=$1
453     shift
454     local dev=$(facet_active $facet)_dev
455     local opt=${facet}_opt
456     echo "Starting ${facet}: ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}"
457     do_facet ${facet} mount -t lustre ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}
458     RC=${PIPESTATUS[0]}
459     if [ $RC -ne 0 ]; then
460         echo "mount -t lustre $@ ${!dev} ${MOUNT%/*}/${facet}"
461         echo "Start of ${!dev} on ${facet} failed ${RC}"
462     else
463         do_facet ${facet} "lctl set_param debug=\\\"$PTLDEBUG\\\"; \
464             lctl set_param subsystem_debug=\\\"${SUBSYSTEM# }\\\"; \
465             lctl set_param debug_mb=${DEBUG_SIZE}; \
466             sync"
467
468         label=$(do_facet ${facet} "$E2LABEL ${!dev}")
469         [ -z "$label" ] && echo no label for ${!dev} && exit 1
470         eval export ${facet}_svc=${label}
471         echo Started ${label}
472     fi
473     return $RC
474 }
475
476 # start facet device options
477 start() {
478     local facet=$1
479     shift
480     local device=$1
481     shift
482     eval export ${facet}_dev=${device}
483     eval export ${facet}_opt=\"$@\"
484
485     local varname=${facet}failover_dev
486     if [ -n "${!varname}" ] ; then
487         eval export ${facet}failover_dev=${!varname}
488     else
489         eval export ${facet}failover_dev=$device
490     fi
491
492     do_facet ${facet} mkdir -p ${MOUNT%/*}/${facet}
493     mount_facet ${facet}
494     RC=$?
495     return $RC
496 }
497
498 stop() {
499     local running
500     local facet=$1
501     shift
502     HOST=`facet_active_host $facet`
503     [ -z $HOST ] && echo stop: no host for $facet && return 0
504
505     running=$(do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts") || true
506     if [ ${running} -ne 0 ]; then
507         echo "Stopping ${MOUNT%/*}/${facet} (opts:$@)"
508         do_facet ${facet} umount -d $@ ${MOUNT%/*}/${facet}
509     fi
510
511     # umount should block, but we should wait for unrelated obd's
512     # like the MGS or MGC to also stop.
513     wait_exit_ST ${facet}
514 }
515
516 # save quota version (both administrative and operational quotas)
517 # add an additional parameter if mountpoint is ever different from $MOUNT
518 quota_save_version() {
519     local fsname=${2:-$FSNAME}
520     local spec=$1
521     local ver=$(tr -c -d "123" <<< $spec)
522     local type=$(tr -c -d "ug" <<< $spec)
523
524     [ -n "$ver" -a "$ver" != "3" ] && error "wrong quota version specifier"
525
526     [ -n "$type" ] && { $LFS quotacheck -$type $MOUNT || error "quotacheck has failed"; }
527
528     do_facet mgs "lctl conf_param ${fsname}-MDT*.mdd.quota_type=$spec"
529     local varsvc
530     local osts=$(get_facets OST)
531     for ost in ${osts//,/ }; do
532         varsvc=${ost}_svc
533         do_facet mgs "lctl conf_param ${!varsvc}.ost.quota_type=$spec"
534     done
535 }
536
537 # client could mount several lustre 
538 quota_type () {
539     local fsname=${1:-$FSNAME}
540     local rc=0
541     do_facet mgs lctl get_param mdd.${fsname}-MDT*.quota_type || rc=$?
542     do_nodes $(comma_list $(osts_nodes)) \
543         lctl get_param obdfilter.${fsname}-OST*.quota_type || rc=$?
544     return $rc 
545 }
546
547 restore_quota_type () {
548    local mntpt=${1:-$MOUNT}
549    local quota_type=$(quota_type $FSNAME | grep MDT | cut -d "=" -f2)
550    if [ ! "$old_QUOTA_TYPE" ] || [ "$quota_type" = "$old_QUOTA_TYPE" ]; then
551         return
552    fi
553    quota_save_version $old_QUOTA_TYPE
554 }
555
556 setup_quota(){
557     local mntpt=$1
558
559     # We need save the original quota_type params, and restore them after testing
560
561     # Suppose that quota type the same on mds and ost
562     local quota_type=$(quota_type | grep MDT | cut -d "=" -f2)
563     [ ${PIPESTATUS[0]} -eq 0 ] || error "quota_type failed!"
564     echo "[HOST:$HOSTNAME] [old_quota_type:$quota_type] [new_quota_type:$QUOTA_TYPE]"
565     if [ "$quota_type" != "$QUOTA_TYPE" ]; then
566         export old_QUOTA_TYPE=$quota_type
567         quota_save_version $QUOTA_TYPE
568     else
569         qtype=$(tr -c -d "ug" <<< $QUOTA_TYPE)
570         $LFS quotacheck -$qtype $mntpt || error "quotacheck has failed for $type"
571     fi
572
573     local quota_usrs=$QUOTA_USERS
574
575     # get_filesystem_size
576     local disksz=$(lfs df $mntpt | grep "filesystem summary:"  | awk '{print $3}')
577     local blk_soft=$((disksz + 1024))
578     local blk_hard=$((blk_soft + blk_soft / 20)) # Go 5% over
579
580     local Inodes=$(lfs df -i $mntpt | grep "filesystem summary:"  | awk '{print $3}')
581     local i_soft=$Inodes
582     local i_hard=$((i_soft + i_soft / 20))
583
584     echo "Total disk size: $disksz  block-softlimit: $blk_soft block-hardlimit:
585         $blk_hard inode-softlimit: $i_soft inode-hardlimit: $i_hard"
586
587     local cmd
588     for usr in $quota_usrs; do
589         echo "Setting up quota on $HOSTNAME:$mntpt for $usr..."
590         for type in u g; do
591             cmd="$LFS setquota -$type $usr -b $blk_soft -B $blk_hard -i $i_soft -I $i_hard $mntpt"
592             echo "+ $cmd"
593             eval $cmd || error "$cmd FAILED!"
594         done
595         # display the quota status
596         echo "Quota settings for $usr : "
597         $LFS quota -v -u $usr $mntpt || true
598     done
599 }
600
601 zconf_mount() {
602     local OPTIONS
603     local client=$1
604     local mnt=$2
605     # Only supply -o to mount if we have options
606     if [ -n "$MOUNTOPT" ]; then
607         OPTIONS="-o $MOUNTOPT"
608     fi
609     local device=$MGSNID:/$FSNAME
610     if [ -z "$mnt" -o -z "$FSNAME" ]; then
611         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
612         exit 1
613     fi
614
615     echo "Starting client: $client: $OPTIONS $device $mnt"
616     do_node $client mkdir -p $mnt
617     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
618
619     do_node $client "lctl set_param debug=\\\"$PTLDEBUG\\\";
620         lctl set_param subsystem_debug=\\\"${SUBSYSTEM# }\\\";
621         lctl set_param debug_mb=${DEBUG_SIZE}"
622
623     return 0
624 }
625
626 zconf_umount() {
627     local client=$1
628     local mnt=$2
629     local force
630     local busy 
631     local need_kill
632
633     [ "$3" ] && force=-f
634     local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
635     if [ $running -ne 0 ]; then
636         echo "Stopping client $client $mnt (opts:$force)"
637         do_node $client lsof -t $mnt || need_kill=no
638         if [ "x$force" != "x" -a "x$need_kill" != "xno" ]; then
639             pids=$(do_node $client lsof -t $mnt | sort -u);
640             if [ -n $pids ]; then
641                 do_node $client kill -9 $pids || true
642             fi
643         fi
644
645         busy=$(do_node $client "umount $force $mnt 2>&1" | grep -c "busy") || true
646         if [ $busy -ne 0 ] ; then
647             echo "$mnt is still busy, wait one second" && sleep 1
648             do_node $client umount $force $mnt
649         fi
650     fi
651 }
652
653 # nodes is comma list
654 sanity_mount_check_nodes () {
655     local nodes=$1
656     shift
657     local mnts="$@"
658     local mnt
659
660     # FIXME: assume that all cluster nodes run the same os
661     [ "$(uname)" = Linux ] || return 0
662
663     local rc=0
664     for mnt in $mnts ; do
665         do_nodes $nodes "running=\\\$(grep -c $mnt' ' /proc/mounts);
666 mpts=\\\$(mount | grep -w -c $mnt);
667 if [ \\\$running -ne \\\$mpts ]; then
668     echo \\\$(hostname) env are INSANE!;
669     exit 1;
670 fi"
671     [ $? -eq 0 ] || rc=1 
672     done
673     return $rc
674 }
675
676 sanity_mount_check_servers () {
677     [ "$CLIENTONLY" ] && 
678         { echo "CLIENTONLY mode, skip mount_check_servers"; return 0; } || true
679     echo Checking servers environments
680
681     # FIXME: modify get_facets to display all facets wo params
682     local facets="$(get_facets OST),$(get_facets MDS),mgs"
683     local node
684     local mnt
685     local facet
686     for facet in ${facets//,/ }; do
687         node=$(facet_host ${facet})
688         mnt=${MOUNT%/*}/${facet}
689         sanity_mount_check_nodes $node $mnt ||
690             { error "server $node environments are insane!"; return 1; }
691     done
692 }
693
694 sanity_mount_check_clients () {
695     local clients=${1:-$CLIENTS}
696     local mntpt=${2:-$MOUNT}
697     local mntpt2=${3:-$MOUNT2}
698
699     [ -z $clients ] && clients=$(hostname)
700     echo Checking clients $clients environments
701
702     sanity_mount_check_nodes $clients $mntpt $mntpt2 ||
703        error "clients environments are insane!"
704 }
705
706 sanity_mount_check () {
707     sanity_mount_check_servers || return 1
708     sanity_mount_check_clients || return 2
709 }
710
711 # mount clients if not mouted
712 zconf_mount_clients() {
713     local clients=$1
714     local mnt=$2
715     local OPTIONS=${3:-$MOUNTOPT}
716
717     # Only supply -o to mount if we have options
718     if [ "$OPTIONS" ]; then
719         OPTIONS="-o $OPTIONS"
720     fi
721     local device=$MGSNID:/$FSNAME
722     if [ -z "$mnt" -o -z "$FSNAME" ]; then
723         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
724         exit 1
725     fi
726
727     echo "Starting client $clients: $OPTIONS $device $mnt"
728
729     do_nodes $clients "
730 running=\\\$(mount | grep -c $mnt' ');
731 rc=0;
732 if [ \\\$running -eq 0 ] ; then
733     mkdir -p $mnt;
734     mount -t lustre $OPTIONS $device $mnt;
735     rc=\\\$?;
736 fi;
737 exit \\\$rc" || return ${PIPESTATUS[0]}
738
739     echo "Started clients $clients: "
740     do_nodes $clients "mount | grep -w $mnt"
741
742     do_nodes $clients "lctl set_param debug=\\\"$PTLDEBUG\\\";
743         lctl set_param subsystem_debug=\\\"${SUBSYSTEM# }\\\";
744         lctl set_param debug_mb=${DEBUG_SIZE};"
745
746     return 0
747 }
748
749 zconf_umount_clients() {
750     local clients=$1
751     local mnt=$2
752     local force
753
754     [ "$3" ] && force=-f
755
756     echo "Stopping clients: $clients $mnt (opts:$force)"
757     do_nodes $clients "running=\\\$(grep -c $mnt' ' /proc/mounts);
758 if [ \\\$running -ne 0 ] ; then
759 echo Stopping client \\\$(hostname) $mnt opts:$force;
760 lsof -t $mnt || need_kill=no;
761 if [ "x$force" != "x" -a "x\\\$need_kill" != "xno" ]; then
762     pids=\\\$(lsof -t $mnt | sort -u);
763     if [ -n \\\"\\\$pids\\\" ]; then
764              kill -9 \\\$pids;
765     fi
766 fi;
767 busy=\\\$(umount $force $mnt 2>&1 | grep -c "busy");
768 if [ \\\$busy -ne 0 ] ; then
769     echo "$mnt is still busy, wait one second" && sleep 1;
770     umount $force $mnt;
771 fi
772 fi"
773 }
774
775 shudown_node_hard () {
776     local host=$1
777     local attempts=3
778
779     for i in $(seq $attempts) ; do
780         $POWER_DOWN $host
781         sleep 1
782         ping -w 3 -c 1 $host > /dev/null 2>&1 || return 0
783         echo "waiting for $host to fail attempts=$attempts"
784         [ $i -lt $attempts ] || \
785             { echo "$host still pingable after power down! attempts=$attempts" && return 1; } 
786     done
787 }
788
789 shutdown_client() {
790     local client=$1
791     local mnt=${2:-$MOUNT}
792     local attempts=3
793
794     if [ "$FAILURE_MODE" = HARD ]; then
795         shudown_node_hard $client 
796     else
797        zconf_umount_clients $client $mnt -f
798     fi
799 }
800
801 shutdown_facet() {
802     local facet=$1
803     if [ "$FAILURE_MODE" = HARD ]; then
804         shudown_node_hard $(facet_active_host $facet)
805     elif [ "$FAILURE_MODE" = SOFT ]; then
806         stop $facet
807     fi
808 }
809
810 reboot_facet() {
811     facet=$1
812     if [ "$FAILURE_MODE" = HARD ]; then
813         $POWER_UP `facet_active_host $facet`
814     else
815         sleep 10
816     fi
817 }
818
819 boot_node() {
820     local node=$1
821     if [ "$FAILURE_MODE" = HARD ]; then
822        $POWER_UP $node
823        wait_for_host $node
824     fi
825 }
826
827 # recovery-scale functions
828 check_progs_installed () {
829     local clients=$1
830     shift
831     local progs=$@
832
833     do_nodes $clients "PATH=:$PATH; status=true;
834 for prog in $progs; do
835     if ! [ \\\"\\\$(which \\\$prog)\\\"  -o  \\\"\\\${!prog}\\\" ]; then
836        echo \\\$prog missing on \\\$(hostname);
837        status=false;
838     fi
839 done;
840 eval \\\$status"
841 }
842
843 client_var_name() {
844     echo __$(echo $1 | tr '-' 'X')
845 }
846
847 start_client_load() {
848     local client=$1
849     local load=$2
850     local var=$(client_var_name $client)_load
851     eval export ${var}=$load
852
853     do_node $client "PATH=$PATH MOUNT=$MOUNT ERRORS_OK=$ERRORS_OK \
854                               BREAK_ON_ERROR=$BREAK_ON_ERROR \
855                               END_RUN_FILE=$END_RUN_FILE \
856                               LOAD_PID_FILE=$LOAD_PID_FILE \
857                               TESTSUITELOG=$TESTSUITELOG \
858                               run_${load}.sh" &
859     CLIENT_LOAD_PIDS="$CLIENT_LOAD_PIDS $!"
860     log "Started client load: ${load} on $client"
861
862     return 0
863 }
864
865 start_client_loads () {
866     local -a clients=(${1//,/ })
867     local numloads=${#CLIENT_LOADS[@]}
868     local testnum
869
870     for ((nodenum=0; nodenum < ${#clients[@]}; nodenum++ )); do
871         testnum=$((nodenum % numloads))
872         start_client_load ${clients[nodenum]} ${CLIENT_LOADS[testnum]}
873     done
874 }
875
876 # only for remote client 
877 check_client_load () {
878     local client=$1
879     local var=$(client_var_name $client)_load
880     local TESTLOAD=run_${!var}.sh
881
882     ps auxww | grep -v grep | grep $client | grep -q "$TESTLOAD" || return 1
883     
884     # bug 18914: try to connect several times not only when
885     # check ps, but  while check_catastrophe also
886     local tries=3
887     local RC=254
888     while [ $RC = 254 -a $tries -gt 0 ]; do
889         let tries=$tries-1
890         # assume success
891         RC=0
892         if ! check_catastrophe $client; then
893             RC=${PIPESTATUS[0]}
894             if [ $RC -eq 254 ]; then
895                 # FIXME: not sure how long we shuold sleep here
896                 sleep 10
897                 continue
898             fi
899             echo "check catastrophe failed: RC=$RC "
900             return $RC
901         fi
902     done
903     # We can continue try to connect if RC=254
904     # Just print the warning about this
905     if [ $RC = 254 ]; then
906         echo "got a return status of $RC from do_node while checking catastrophe on $client"
907     fi
908
909     # see if the load is still on the client
910     tries=3
911     RC=254
912     while [ $RC = 254 -a $tries -gt 0 ]; do
913         let tries=$tries-1
914         # assume success
915         RC=0
916         if ! do_node $client "ps auxwww | grep -v grep | grep -q $TESTLOAD"; then
917             RC=${PIPESTATUS[0]}
918             sleep 30
919         fi
920     done
921     if [ $RC = 254 ]; then
922         echo "got a return status of $RC from do_node while checking (catastrophe and 'ps') the client load on $client"
923         # see if we can diagnose a bit why this is
924     fi
925
926     return $RC
927 }
928 check_client_loads () {
929    local clients=${1//,/ }
930    local client=
931    local rc=0
932
933    for client in $clients; do
934       check_client_load $client
935       rc=${PIPESTATUS[0]}
936       if [ "$rc" != 0 ]; then
937         log "Client load failed on node $client, rc=$rc"
938         return $rc
939       fi
940    done
941 }
942
943 restart_client_loads () {
944     local clients=${1//,/ }
945     local expectedfail=${2:-""}
946     local client=
947     local rc=0
948
949     for client in $clients; do
950         check_client_load $client
951         rc=${PIPESTATUS[0]}
952         if [ "$rc" != 0 -a "$expectedfail" ]; then
953             local var=$(client_var_name $client)_load
954             start_client_load $client ${!var}
955             echo "Restarted client load ${!var}: on $client. Checking ..."
956             check_client_load $client
957             rc=${PIPESTATUS[0]}
958             if [ "$rc" != 0 ]; then
959                 log "Client load failed to restart on node $client, rc=$rc"
960                 # failure one client load means test fail
961                 # we do not need to check other 
962                 return $rc
963             fi
964         else
965             return $rc
966         fi
967     done
968 }
969 # End recovery-scale functions
970
971 # verify that lustre actually cleaned up properly
972 cleanup_check() {
973     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
974         error "LBUG/LASSERT detected"
975     BUSY=`dmesg | grep -i destruct || true`
976     if [ "$BUSY" ]; then
977         echo "$BUSY" 1>&2
978         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
979         exit 205
980     fi
981
982     check_mem_leak || exit 204
983
984     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
985         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
986
987     if module_loaded lnet || module_loaded libcfs; then
988         echo "$0: modules still loaded..." 1>&2
989         /sbin/lsmod 1>&2
990         return 203
991     fi
992     return 0
993 }
994
995 wait_update () {
996     local node=$1
997     local TEST=$2
998     local FINAL=$3
999     local MAX=${4:-90}
1000
1001         local RESULT
1002         local WAIT=0
1003         local sleep=5
1004         while [ true ]; do
1005             RESULT=$(do_node $node "$TEST")
1006             if [ "$RESULT" == "$FINAL" ]; then
1007                 echo "Updated after $WAIT sec: wanted '$FINAL' got '$RESULT'"
1008                 return 0
1009             fi
1010             [ $WAIT -ge $MAX ] && break
1011             echo "Waiting $((MAX - WAIT)) secs for update"
1012             WAIT=$((WAIT + sleep))
1013             sleep $sleep
1014         done
1015         echo "Update not seen after $MAX sec: wanted '$FINAL' got '$RESULT'"
1016         return 3
1017 }
1018
1019 wait_update_facet () {
1020     local facet=$1
1021     wait_update  $(facet_active_host $facet) "$@"
1022 }
1023
1024 wait_delete_completed () {
1025     local TOTALPREV=`lctl get_param -n osc.*.kbytesavail | \
1026                      awk 'BEGIN{total=0}; {total+=$1}; END{print total}'`
1027
1028     local WAIT=0
1029     local MAX_WAIT=20
1030     while [ "$WAIT" -ne "$MAX_WAIT" ]; do
1031         sleep 1
1032         TOTAL=`lctl get_param -n osc.*.kbytesavail | \
1033                awk 'BEGIN{total=0}; {total+=$1}; END{print total}'`
1034         [ "$TOTAL" -eq "$TOTALPREV" ] && return 0
1035         echo "Waiting delete completed ... prev: $TOTALPREV current: $TOTAL "
1036         TOTALPREV=$TOTAL
1037         WAIT=$(( WAIT + 1))
1038     done
1039     echo "Delete is not completed in $MAX_WAIT sec"
1040     return 1
1041 }
1042
1043 wait_for_host() {
1044     local host=$1
1045     check_network "$host" 900
1046     while ! do_node $host hostname  > /dev/null; do sleep 5; done
1047 }
1048
1049 wait_for() {
1050     local facet=$1
1051     local host=`facet_active_host $facet`
1052     wait_for_host $host
1053 }
1054
1055 wait_recovery_complete () {
1056     local facet=$1
1057
1058     # Use default policy if $2 is not passed by caller.
1059     #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2)
1060     # as we are in process of changing obd_timeout in different ways
1061     # let's set MAX longer than that
1062     local MAX=${2:-$(( TIMEOUT * 4 ))}
1063
1064     local var_svc=${facet}_svc
1065     local procfile="*.${!var_svc}.recovery_status"
1066     local WAIT=0
1067     local STATUS=
1068
1069     while [ $WAIT -lt $MAX ]; do
1070         STATUS=$(do_facet $facet lctl get_param -n $procfile | grep status)
1071         [[ $STATUS = "status: COMPLETE" ]] && return 0
1072         sleep 5
1073         WAIT=$((WAIT + 5))
1074         echo "Waiting $((MAX - WAIT)) secs for $facet recovery done. $STATUS"
1075     done
1076     echo "$facet recovery not done in $MAX sec. $STATUS"
1077     return 1
1078 }
1079
1080 wait_mds_ost_sync () {
1081     # just because recovery is done doesn't mean we've finished
1082     # orphan cleanup. Wait for llogs to get synchronized.
1083     echo "Waiting for orphan cleanup..."
1084     # MAX value includes time needed for MDS-OST reconnection
1085     local MAX=$(( TIMEOUT * 2 ))
1086     local WAIT=0
1087     while [ $WAIT -lt $MAX ]; do
1088         local -a sync=($(do_nodes $(comma_list $(osts_nodes)) \
1089             "$LCTL get_param -n obdfilter.*.mds_sync"))
1090         local con=1
1091         for ((i=0; i<${#sync[@]}; i++)); do
1092             [ ${sync[$i]} -eq 0 ] && continue
1093             # there is a not finished MDS-OST synchronization
1094             con=0
1095             break;
1096         done
1097         sleep 2 # increase waiting time and cover statfs cache
1098         [ ${con} -eq 1 ] && return 0
1099         echo "Waiting $WAIT secs for $facet mds-ost sync done."
1100         WAIT=$((WAIT + 2))
1101     done
1102     echo "$facet recovery not done in $MAX sec. $STATUS"
1103     return 1
1104 }
1105
1106 wait_destroy_complete () {
1107     echo "Waiting for destroy to be done..."
1108     # MAX value shouldn't be big as this mean server responsiveness
1109     # never increase this just to make test pass but investigate
1110     # why it takes so long time
1111     local MAX=5
1112     local WAIT=0
1113     while [ $WAIT -lt $MAX ]; do
1114         local -a RPCs=($($LCTL get_param -n osc.*.destroys_in_flight))
1115         local con=1
1116         for ((i=0; i<${#RPCs[@]}; i++)); do
1117             [ ${RPCs[$i]} -eq 0 ] && continue
1118             # there are still some destroy RPCs in flight
1119             con=0
1120             break;
1121         done
1122         sleep 1
1123         [ ${con} -eq 1 ] && return 0 # done waiting
1124         echo "Waiting $WAIT secs for destroys to be done."
1125         WAIT=$((WAIT + 1))
1126     done
1127     echo "Destroys weren't done in $MAX sec."
1128     return 1
1129 }
1130
1131 wait_exit_ST () {
1132     local facet=$1
1133
1134     local WAIT=0
1135     local INTERVAL=1
1136     local running
1137     # conf-sanity 31 takes a long time cleanup
1138     while [ $WAIT -lt 300 ]; do
1139         running=$(do_facet ${facet} "lsmod | grep lnet > /dev/null && lctl dl | grep ' ST '") || true
1140         [ -z "${running}" ] && return 0
1141         echo "waited $WAIT for${running}"
1142         [ $INTERVAL -lt 64 ] && INTERVAL=$((INTERVAL + INTERVAL))
1143         sleep $INTERVAL
1144         WAIT=$((WAIT + INTERVAL))
1145     done
1146     echo "service didn't stop after $WAIT seconds.  Still running:"
1147     echo ${running}
1148     return 1
1149 }
1150
1151 wait_remote_prog () {
1152    local prog=$1
1153    local WAIT=0
1154    local INTERVAL=5
1155    local rc=0
1156
1157    [ "$PDSH" = "no_dsh" ] && return 0
1158
1159    while [ $WAIT -lt $2 ]; do
1160         running=$(ps uax | grep "$PDSH.*$prog.*$MOUNT" | grep -v grep) || true
1161         [ -z "${running}" ] && return 0 || true
1162         echo "waited $WAIT for: "
1163         echo "$running"
1164         [ $INTERVAL -lt 60 ] && INTERVAL=$((INTERVAL + INTERVAL))
1165         sleep $INTERVAL
1166         WAIT=$((WAIT + INTERVAL))
1167     done
1168     local pids=$(ps  uax | grep "$PDSH.*$prog.*$MOUNT" | grep -v grep | awk '{print $2}')
1169     [ -z "$pids" ] && return 0
1170     echo "$PDSH processes still exists after $WAIT seconds.  Still running: $pids"
1171     # FIXME: not portable
1172     for pid in $pids; do
1173         cat /proc/${pid}/status || true
1174         cat /proc/${pid}/wchan || true
1175         echo "Killing $pid"
1176         kill -9 $pid || true
1177         sleep 1
1178         ps -P $pid && rc=1
1179     done
1180
1181     return $rc
1182 }
1183
1184 clients_up() {
1185     # not every config has many clients
1186     sleep 1
1187     if [ ! -z "$CLIENTS" ]; then
1188         $PDSH $CLIENTS "stat -f $MOUNT" > /dev/null
1189     else
1190         stat -f $MOUNT > /dev/null
1191     fi
1192 }
1193
1194 client_up() {
1195     local client=$1
1196     # usually checked on particular client or locally
1197     sleep 1
1198     if [ ! -z "$client" ]; then
1199         $PDSH $client "stat -f $MOUNT" > /dev/null
1200     else
1201         stat -f $MOUNT > /dev/null
1202     fi
1203 }
1204
1205 client_evicted() {
1206     ! client_up $1
1207 }
1208
1209 client_reconnect() {
1210     uname -n >> $MOUNT/recon
1211     if [ -z "$CLIENTS" ]; then
1212         df $MOUNT; uname -n >> $MOUNT/recon
1213     else
1214         do_nodes $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
1215     fi
1216     echo Connected clients:
1217     cat $MOUNT/recon
1218     ls -l $MOUNT/recon > /dev/null
1219     rm $MOUNT/recon
1220 }
1221
1222 facet_failover() {
1223     local facet=$1
1224     local sleep_time=$2
1225     echo "Failing $facet on node `facet_active_host $facet`"
1226     shutdown_facet $facet
1227     [ -n "$sleep_time" ] && sleep $sleep_time
1228     reboot_facet $facet
1229     change_active $facet
1230     local TO=`facet_active_host $facet`
1231     echo "Failover $facet to $TO"
1232     wait_for $facet
1233     mount_facet $facet || error "Restart of $facet failed"
1234 }
1235
1236 obd_name() {
1237     local facet=$1
1238 }
1239
1240 replay_barrier() {
1241     local facet=$1
1242     do_facet $facet sync
1243     df $MOUNT
1244     local svc=${facet}_svc
1245     do_facet $facet $LCTL --device %${!svc} notransno
1246     do_facet $facet $LCTL --device %${!svc} readonly
1247     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
1248     $LCTL mark "local REPLAY BARRIER on ${!svc}"
1249 }
1250
1251 replay_barrier_nodf() {
1252     local facet=$1    echo running=${running}
1253     do_facet $facet sync
1254     local svc=${facet}_svc
1255     echo Replay barrier on ${!svc}
1256     do_facet $facet $LCTL --device %${!svc} notransno
1257     do_facet $facet $LCTL --device %${!svc} readonly
1258     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
1259     $LCTL mark "local REPLAY BARRIER on ${!svc}"
1260 }
1261
1262 replay_barrier_nosync() {
1263     local facet=$1    echo running=${running}
1264     local svc=${facet}_svc
1265     echo Replay barrier on ${!svc}
1266     do_facet $facet $LCTL --device %${!svc} notransno
1267     do_facet $facet $LCTL --device %${!svc} readonly
1268     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
1269     $LCTL mark "local REPLAY BARRIER on ${!svc}"
1270 }
1271
1272 mds_evict_client() {
1273     UUID=`lctl get_param -n mdc.${mds1_svc}-mdc-*.uuid`
1274     do_facet mds1 "lctl set_param -n mdt.${mds1_svc}.evict_client $UUID"
1275 }
1276
1277 ost_evict_client() {
1278     UUID=`lctl get_param -n devices| grep ${ost1_svc}-osc- | egrep -v 'MDT' | awk '{print $5}'`
1279     do_facet ost1 "lctl set_param -n obdfilter.${ost1_svc}.evict_client $UUID"
1280 }
1281
1282 fail() {
1283     facet_failover $* || error "failover: $?"
1284     clients_up || error "post-failover df: $?"
1285 }
1286
1287 fail_nodf() {
1288         local facet=$1
1289         facet_failover $facet
1290 }
1291
1292 fail_abort() {
1293     local facet=$1
1294     stop $facet
1295     change_active $facet
1296     mount_facet $facet -o abort_recovery
1297     clients_up || echo "first df failed: $?"
1298     clients_up || error "post-failover df: $?"
1299 }
1300
1301 do_lmc() {
1302     echo There is no lmc.  This is mountconf, baby.
1303     exit 1
1304 }
1305
1306 h2gm () {
1307     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1308         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
1309         echo $ID"@gm"
1310     fi
1311 }
1312
1313 h2name_or_ip() {
1314     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1315         echo $1"@$2"
1316     fi
1317 }
1318
1319 h2ptl() {
1320    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1321        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
1322        if [ -z "$ID" ]; then
1323            echo "Could not get a ptl id for $1..."
1324            exit 1
1325        fi
1326        echo $ID"@ptl"
1327    fi
1328 }
1329 declare -fx h2ptl
1330
1331 h2tcp() {
1332     h2name_or_ip "$1" "tcp"
1333 }
1334 declare -fx h2tcp
1335
1336 h2elan() {
1337     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1338         if type __h2elan >/dev/null 2>&1; then
1339             ID=$(__h2elan $1)
1340         else
1341             ID=`echo $1 | sed 's/[^0-9]*//g'`
1342         fi
1343         echo $ID"@elan"
1344     fi
1345 }
1346 declare -fx h2elan
1347
1348 h2openib() {
1349     h2name_or_ip "$1" "openib"
1350 }
1351 declare -fx h2openib
1352
1353 h2o2ib() {
1354     h2name_or_ip "$1" "o2ib"
1355 }
1356 declare -fx h2o2ib
1357
1358 facet_host() {
1359     local facet=$1
1360
1361     [ "$facet" == client ] && echo -n $HOSTNAME && return
1362     varname=${facet}_HOST
1363     if [ -z "${!varname}" ]; then
1364         if [ "${facet:0:3}" == "ost" ]; then
1365             eval ${facet}_HOST=${ost_HOST}
1366         fi
1367     fi
1368     echo -n ${!varname}
1369 }
1370
1371 facet_active() {
1372     local facet=$1
1373     local activevar=${facet}active
1374
1375     if [ -f $TMP/${facet}active ] ; then
1376         source $TMP/${facet}active
1377     fi
1378
1379     active=${!activevar}
1380     if [ -z "$active" ] ; then
1381         echo -n ${facet}
1382     else
1383         echo -n ${active}
1384     fi
1385 }
1386
1387 facet_active_host() {
1388     local facet=$1
1389     local active=`facet_active $facet`
1390     if [ "$facet" == client ]; then
1391         echo $HOSTNAME
1392     else
1393         echo `facet_host $active`
1394     fi
1395 }
1396
1397 change_active() {
1398     local facet=$1
1399     local failover=${facet}failover
1400     host=`facet_host $failover`
1401     [ -z "$host" ] && return
1402     local curactive=`facet_active $facet`
1403     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
1404         eval export ${facet}active=$facet
1405     else
1406         eval export ${facet}active=$failover
1407     fi
1408     # save the active host for this facet
1409     local activevar=${facet}active
1410     echo "$activevar=${!activevar}" > $TMP/$activevar
1411 }
1412
1413 do_node() {
1414     local verbose=false
1415     # do not stripe off hostname if verbose, bug 19215
1416     if [ x$1 = x--verbose ]; then
1417         shift
1418         verbose=true
1419     fi
1420
1421     local HOST=$1
1422     shift
1423     local myPDSH=$PDSH
1424     if [ "$HOST" = "$HOSTNAME" ]; then
1425         myPDSH="no_dsh"
1426     elif [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" ]; then
1427         echo "cannot run remote command on $HOST with $myPDSH"
1428         return 128
1429     fi
1430     if $VERBOSE; then
1431         echo "CMD: $HOST $@" >&2
1432         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
1433     fi
1434
1435     if [ "$myPDSH" = "rsh" ]; then
1436 # we need this because rsh does not return exit code of an executed command
1437         local command_status="$TMP/cs"
1438         rsh $HOST ":> $command_status"
1439         rsh $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin;
1440                     cd $RPWD; sh -c \"$@\") ||
1441                     echo command failed >$command_status"
1442         [ -n "$($myPDSH $HOST cat $command_status)" ] && return 1 || true
1443         return 0
1444     fi
1445
1446     if $verbose ; then
1447         # print HOSTNAME for myPDSH="no_dsh"
1448         if [[ $myPDSH = no_dsh ]]; then
1449             $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed -e "s/^/${HOSTNAME}: /"
1450         else
1451             $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")"
1452         fi
1453     else
1454         $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
1455     fi
1456     return ${PIPESTATUS[0]}
1457 }
1458
1459 single_local_node () {
1460    [ "$1" = "$HOSTNAME" ]
1461 }
1462
1463 do_nodes() {
1464     local verbose=false
1465     # do not stripe off hostname if verbose, bug 19215
1466     if [ x$1 = x--verbose ]; then
1467         shift
1468         verbose=true
1469     fi
1470
1471     local rnodes=$1
1472     shift
1473
1474     if $(single_local_node $rnodes); then
1475         if $verbose; then
1476            do_node --verbose $rnodes $@
1477         else
1478            do_node $rnodes $@
1479         fi
1480         return $?
1481     fi
1482
1483     # This is part from do_node
1484     local myPDSH=$PDSH
1485
1486     [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" -o "$myPDSH" = "rsh" ] && \
1487         echo "cannot run remote command on $rnodes with $myPDSH" && return 128
1488
1489     if $VERBOSE; then
1490         echo "CMD: $rnodes $@" >&2
1491         $myPDSH $rnodes $LCTL mark "$@" > /dev/null 2>&1 || :
1492     fi
1493
1494     if $verbose ; then
1495         $myPDSH $rnodes "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")"
1496     else
1497         $myPDSH $rnodes "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed -re "s/\w+:\s//g"
1498     fi
1499     return ${PIPESTATUS[0]}
1500 }
1501
1502 do_facet() {
1503     local facet=$1
1504     shift
1505     local HOST=`facet_active_host $facet`
1506     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
1507     do_node $HOST "$@"
1508 }
1509
1510 add() {
1511     local facet=$1
1512     shift
1513     # make sure its not already running
1514     stop ${facet} -f
1515     rm -f $TMP/${facet}active
1516     do_facet ${facet} $MKFS $*
1517 }
1518
1519 ostdevname() {
1520     num=$1
1521     DEVNAME=OSTDEV$num
1522     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
1523     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
1524     echo -n $DEVPTR
1525 }
1526
1527 mdsdevname() {
1528     num=$1
1529     DEVNAME=MDSDEV$num
1530     #if $MDSDEVn isn't defined, default is $MDSDEVBASE + num
1531     eval DEVPTR=${!DEVNAME:=${MDSDEVBASE}${num}}
1532     echo -n $DEVPTR
1533 }
1534
1535 ########
1536 ## MountConf setup
1537
1538 stopall() {
1539     # make sure we are using the primary server, so test-framework will
1540     # be able to clean up properly.
1541     activemds=`facet_active mds1`
1542     if [ $activemds != "mds1" ]; then
1543         fail mds1
1544     fi
1545
1546     local clients=$CLIENTS
1547     [ -z $clients ] && clients=$(hostname)
1548
1549     zconf_umount_clients $clients $MOUNT "$*" || true
1550     [ -n "$MOUNT2" ] && zconf_umount_clients $clients $MOUNT2 "$*" || true
1551
1552     [ "$CLIENTONLY" ] && return
1553     # The add fn does rm ${facet}active file, this would be enough
1554     # if we use do_facet <facet> only after the facet added, but
1555     # currently we use do_facet mds in local.sh
1556     for num in `seq $MDSCOUNT`; do
1557         stop mds$num -f
1558         rm -f ${TMP}/mds${num}active
1559     done
1560
1561     for num in `seq $OSTCOUNT`; do
1562         stop ost$num -f
1563         rm -f $TMP/ost${num}active
1564     done
1565
1566     return 0
1567 }
1568
1569 cleanupall() {
1570     nfs_client_mode && return
1571
1572     stopall $*
1573     unload_modules
1574     cleanup_gss
1575 }
1576
1577 mdsmkfsopts()
1578 {
1579     local nr=$1
1580     test $nr = 1 && echo -n $MDS_MKFS_OPTS || echo -n $MDSn_MKFS_OPTS
1581 }
1582
1583 formatall() {
1584     if [ "$IAMDIR" == "yes" ]; then
1585         MDS_MKFS_OPTS="$MDS_MKFS_OPTS --iam-dir"
1586         MDSn_MKFS_OPTS="$MDSn_MKFS_OPTS --iam-dir"
1587     fi
1588
1589     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
1590
1591     if [ ! -z $SEC ]; then
1592         MDS_MKFS_OPTS="$MDS_MKFS_OPTS --param srpc.flavor.default=$SEC"
1593         MDSn_MKFS_OPTS="$MDSn_MKFS_OPTS --param srpc.flavor.default=$SEC"
1594         OST_MKFS_OPTS="$OST_MKFS_OPTS --param srpc.flavor.default=$SEC"
1595     fi
1596
1597     stopall
1598     # We need ldiskfs here, may as well load them all
1599     load_modules
1600     [ "$CLIENTONLY" ] && return
1601     echo Formatting mgs, mds, osts
1602     if [[ $MDSDEV1 != $MGSDEV ]] || [[ $mds1_HOST != $mgs_HOST ]]; then
1603         add mgs $mgs_MKFS_OPTS $FSTYPE_OPT --reformat $MGSDEV || exit 10
1604     fi
1605
1606     for num in `seq $MDSCOUNT`; do
1607         echo "Format mds$num: $(mdsdevname $num)"
1608         if $VERBOSE; then
1609             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` || exit 9
1610         else
1611             add mds$num `mdsmkfsopts $num` $FSTYPE_OPT --reformat `mdsdevname $num` > /dev/null || exit 9
1612         fi
1613     done
1614
1615     for num in `seq $OSTCOUNT`; do
1616         echo "Format ost$num: $(ostdevname $num)"
1617         if $VERBOSE; then
1618             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` || exit 10
1619         else
1620             add ost$num $OST_MKFS_OPTS --reformat `ostdevname $num` > /dev/null || exit 10
1621         fi
1622     done
1623 }
1624
1625 mount_client() {
1626     grep " $1 " /proc/mounts || zconf_mount $HOSTNAME $*
1627 }
1628
1629 umount_client() {
1630     grep " $1 " /proc/mounts && zconf_umount `hostname` $*
1631 }
1632
1633 # return value:
1634 # 0: success, the old identity set already.
1635 # 1: success, the old identity does not set.
1636 # 2: fail.
1637 switch_identity() {
1638     local num=$1
1639     local switch=$2
1640     local j=`expr $num - 1`
1641     local MDT="`(do_facet mds$num lctl get_param -N mdt.*MDT*$j 2>/dev/null | cut -d"." -f2 2>/dev/null) || true`"
1642
1643     if [ -z "$MDT" ]; then
1644         return 2
1645     fi
1646
1647     local old="`do_facet mds$num "lctl get_param -n mdt.$MDT.identity_upcall"`"
1648
1649     if $switch; then
1650         do_facet mds$num "lctl set_param -n mdt.$MDT.identity_upcall \"$L_GETIDENTITY\""
1651     else
1652         do_facet mds$num "lctl set_param -n mdt.$MDT.identity_upcall \"NONE\""
1653     fi
1654
1655     do_facet mds$num "lctl set_param -n mdt/$MDT/identity_flush \"-1\""
1656
1657     if [ $old = "NONE" ]; then
1658         return 1
1659     else
1660         return 0
1661     fi
1662 }
1663
1664 remount_client()
1665 {
1666         zconf_umount `hostname` $1 || error "umount failed"
1667         zconf_mount `hostname` $1 || error "mount failed"
1668 }
1669
1670 writeconf_facet () {
1671     local facet=$1
1672     local dev=$2
1673
1674     do_facet $facet "$TUNEFS --writeconf $dev"
1675 }
1676
1677 writeconf_all () {
1678     for num in `seq $MDSCOUNT`; do
1679         DEVNAME=$(mdsdevname $num)
1680         writeconf_facet mds$num $DEVNAME
1681     done
1682
1683     for num in `seq $OSTCOUNT`; do
1684         DEVNAME=$(ostdevname $num)
1685         writeconf_facet ost$num $DEVNAME
1686     done
1687 }
1688
1689 setupall() {
1690     nfs_client_mode && return
1691
1692     sanity_mount_check ||
1693         error "environments are insane!"
1694
1695     load_modules
1696     init_gss
1697     if [ -z "$CLIENTONLY" ]; then
1698         echo Setup mgs, mdt, osts
1699         echo $WRITECONF | grep -q "writeconf" && \
1700             writeconf_all
1701         if [[ $mds1_HOST != $mgs_HOST ]] || [[ $MDSDEV1 != $MGSDEV ]]; then
1702             start mgs $MGSDEV $mgs_MOUNT_OPTS
1703         fi
1704
1705         for num in `seq $MDSCOUNT`; do
1706             DEVNAME=$(mdsdevname $num)
1707             start mds$num $DEVNAME $MDS_MOUNT_OPTS
1708
1709             # We started mds, now we should set failover variables properly.
1710             # Set mds${num}failover_HOST if it is not set (the default failnode).
1711             local varname=mds${num}failover_HOST
1712             if [ -z "${!varname}" ]; then
1713                 eval mds${num}failover_HOST=$(facet_host mds$num)
1714             fi
1715
1716             if [ $IDENTITY_UPCALL != "default" ]; then
1717                 switch_identity $num $IDENTITY_UPCALL
1718             fi
1719         done
1720         for num in `seq $OSTCOUNT`; do
1721             DEVNAME=$(ostdevname $num)
1722             start ost$num $DEVNAME $OST_MOUNT_OPTS
1723
1724             # We started ost$num, now we should set ost${num}failover variable properly.
1725             # Set ost${num}failover_HOST if it is not set (the default failnode).
1726             varname=ost${num}failover_HOST
1727             if [ -z "${!varname}" ]; then
1728                 eval ost${num}failover_HOST=$(facet_host ost${num})
1729             fi
1730
1731         done
1732     fi
1733     # wait a while to allow sptlrpc configuration be propogated to targets,
1734     # only needed when mounting new target devices.
1735     $GSS && sleep 10
1736
1737     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
1738     mount_client $MOUNT
1739     [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT
1740
1741     if [ "$MOUNT_2" ]; then
1742         mount_client $MOUNT2
1743         [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT2
1744     fi
1745
1746     init_param_vars
1747
1748     # by remounting mdt before ost, initial connect from mdt to ost might
1749     # timeout because ost is not ready yet. wait some time to its fully
1750     # recovery. initial obd_connect timeout is 5s; in GSS case it's preceeded
1751     # by a context negotiation rpc with $TIMEOUT.
1752     # FIXME better by monitoring import status.
1753     if $GSS; then
1754         sleep $((TIMEOUT + 5))
1755     else
1756         sleep 5
1757     fi
1758 }
1759
1760 mounted_lustre_filesystems() {
1761         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
1762 }
1763
1764 init_facet_vars () {
1765     [ "$CLIENTONLY" ] && return 0
1766     local facet=$1
1767     shift
1768     local device=$1
1769
1770     shift
1771
1772     eval export ${facet}_dev=${device}
1773     eval export ${facet}_opt=\"$@\"
1774
1775     local dev=${facet}_dev
1776     local label=$(do_facet ${facet} "$E2LABEL ${!dev}")
1777     [ -z "$label" ] && echo no label for ${!dev} && exit 1
1778
1779     eval export ${facet}_svc=${label}
1780
1781     local varname=${facet}failover_HOST
1782     if [ -z "${!varname}" ]; then
1783        eval $varname=$(facet_host $facet) 
1784     fi
1785
1786     # ${facet}failover_dev is set in cfg file
1787     varname=${facet}failover_dev
1788     if [ -n "${!varname}" ] ; then
1789         eval export ${facet}failover_dev=${!varname}
1790     else
1791         eval export ${facet}failover_dev=$device
1792     fi
1793 }
1794
1795 init_facets_vars () {
1796     local DEVNAME
1797
1798     if ! remote_mds_nodsh; then 
1799         for num in `seq $MDSCOUNT`; do
1800             DEVNAME=`mdsdevname $num`
1801             init_facet_vars mds$num $DEVNAME $MDS_MOUNT_OPTS
1802         done
1803     fi
1804
1805     remote_ost_nodsh && return
1806
1807     for num in `seq $OSTCOUNT`; do
1808         DEVNAME=`ostdevname $num`
1809         init_facet_vars ost$num $DEVNAME $OST_MOUNT_OPTS
1810     done
1811 }
1812
1813 osc_ensure_active () {
1814     local facet=$1
1815     local type=$2
1816     local timeout=$3
1817     local period=0
1818
1819     while [ $period -lt $timeout ]; do
1820         count=$(do_facet $facet "lctl dl | grep '${FSNAME}-OST.*-osc-${type}' | grep ' IN ' 2>/dev/null | wc -l")
1821         if [ $count -eq 0 ]; then
1822             break
1823         fi
1824
1825         echo "There are $count OST are inactive, wait $period seconds, and try again"
1826         sleep 3
1827         period=$((period+3))
1828     done
1829
1830     [ $period -lt $timeout ] || log "$count OST are inactive after $timeout seconds, give up"
1831 }
1832
1833 som_check() {
1834     SOM_ENABLED=$(do_facet $SINGLEMDS "$LCTL get_param mdt.*.som" | awk -F= ' {print $2}' | head -n 1)
1835     echo $SOM_ENABLED
1836 }
1837
1838 init_param_vars () {
1839     if ! remote_ost_nodsh && ! remote_mds_nodsh; then
1840         export MDSVER=$(do_facet $SINGLEMDS "lctl get_param version" | cut -d. -f1,2)
1841         export OSTVER=$(do_facet ost1 "lctl get_param version" | cut -d. -f1,2)
1842         export CLIVER=$(lctl get_param version | cut -d. -f 1,2)
1843     fi
1844
1845     remote_mds_nodsh ||
1846         TIMEOUT=$(do_facet $SINGLEMDS "lctl get_param -n timeout")
1847
1848     log "Using TIMEOUT=$TIMEOUT"
1849
1850     osc_ensure_active $SINGLEMDS M $TIMEOUT
1851     osc_ensure_active client c $TIMEOUT
1852
1853     if [ x"$(som_check)" = x"enabled" ]; then
1854         ENABLE_QUOTA=""
1855         echo "disable quota temporary when SOM enabled"
1856     fi
1857     if [ $QUOTA_AUTO -ne 0 ]; then
1858         if [ "$ENABLE_QUOTA" ]; then
1859             echo "enable quota as required"
1860             setup_quota $MOUNT || return 2
1861         else
1862             echo "disable quota as required"
1863             $LFS quotaoff -ug $MOUNT > /dev/null 2>&1
1864         fi
1865     fi
1866
1867     return 0
1868 }
1869
1870 nfs_client_mode () {
1871     if [ "$NFSCLIENT" ]; then
1872         echo "NFSCLIENT mode: setup, cleanup, check config skipped"
1873         local clients=$CLIENTS
1874         [ -z $clients ] && clients=$(hostname)
1875
1876         # FIXME: remove hostname when 19215 fixed
1877         do_nodes $clients "echo \\\$(hostname); grep ' '$MOUNT' ' /proc/mounts"
1878         declare -a nfsexport=(`grep ' '$MOUNT' ' /proc/mounts | awk '{print $1}' | awk -F: '{print $1 " "  $2}'`)
1879         do_nodes ${nfsexport[0]} "echo \\\$(hostname); df -T  ${nfsexport[1]}"
1880         return
1881     fi
1882     return 1
1883 }
1884
1885 check_config () {
1886     nfs_client_mode && return
1887
1888     local mntpt=$1
1889
1890     local mounted=$(mount | grep " $mntpt ")
1891     if [ "$CLIENTONLY" ]; then
1892         # bug 18021
1893         # CLIENTONLY should not depend on *_HOST settings
1894         local mgc=$($LCTL device_list | awk '/MGC/ {print $4}')
1895         # in theory someone could create a new,
1896         # client-only config file that assumed lustre was already
1897         # configured and didn't set the MGSNID. If MGSNID is not set,
1898         # then we should use the mgs nid currently being used 
1899         # as the default value. bug 18021
1900         [[ x$MGSNID = x ]] &&
1901             MGSNID=${mgc//MGC/}
1902
1903         if [[ x$mgc != xMGC$MGSNID ]]; then
1904             if [ "$mgs_HOST" ]; then
1905                 local mgc_ip=$(ping -q -c1 -w1 $mgs_HOST | grep PING | awk '{print $3}' | sed -e "s/(//g" -e "s/)//g")
1906 #                [[ x$mgc = xMGC$mgc_ip@$NETTYPE ]] ||
1907 #                    error_exit "MGSNID=$MGSNID, mounted: $mounted, MGC : $mgc"
1908             fi
1909         fi
1910         return 0
1911     fi
1912
1913     local myMGS_host=$mgs_HOST   
1914     if [ "$NETTYPE" = "ptl" ]; then
1915         myMGS_host=$(h2ptl $mgs_HOST | sed -e s/@ptl//) 
1916     fi
1917
1918     echo Checking config lustre mounted on $mntpt
1919     local mgshost=$(mount | grep " $mntpt " | awk -F@ '{print $1}')
1920     mgshost=$(echo $mgshost | awk -F: '{print $1}')
1921
1922     if [ "$mgshost" != "$myMGS_host" ]; then
1923             log "Bad config file: lustre is mounted with mgs $mgshost, but mgs_HOST=$mgs_HOST, NETTYPE=$NETTYPE
1924                    Please use correct config or set mds_HOST correctly!"
1925     fi
1926
1927     sanity_mount_check ||
1928         error "environments are insane!"
1929 }
1930
1931 check_timeout () {
1932     local mdstimeout=$(do_facet $SINGLEMDS "lctl get_param -n timeout")
1933     local cltimeout=$(lctl get_param -n timeout)
1934     if [ $mdstimeout -ne $TIMEOUT ] || [ $mdstimeout -ne $cltimeout ]; then
1935         error "timeouts are wrong! mds: $mdstimeout, client: $cltimeout, TIMEOUT=$TIMEOUT"
1936         return 1
1937     fi
1938 }
1939
1940 check_and_setup_lustre() {
1941     nfs_client_mode && return
1942
1943     local MOUNTED=$(mounted_lustre_filesystems)
1944
1945     local do_check=true
1946     # MOUNT is not mounted
1947     if [ -z "$MOUNTED" ] || ! $(echo $MOUNTED | grep -w -q $MOUNT); then
1948         [ "$REFORMAT" ] && formatall
1949         setupall
1950         MOUNTED=$(mounted_lustre_filesystems | head -1)
1951         [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
1952         export I_MOUNTED=yes
1953         do_check=false
1954
1955     # MOUNT and MOUNT2 are mounted
1956     elif $(echo $MOUNTED | grep -w -q $MOUNT2); then
1957
1958         # MOUNT2 is mounted,  MOUNT_2 is not set
1959         if ! [ "$MOUNT_2" ]; then
1960             zconf_umount `hostname` $MOUNT2
1961             export I_UMOUNTED2=yes
1962
1963         # MOUNT2 is mounted, MOUNT_2 is set
1964         else
1965             check_config $MOUNT2
1966         fi 
1967     fi
1968
1969     if $do_check; then
1970         check_config $MOUNT
1971         init_facets_vars
1972         init_param_vars
1973
1974         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=\\\"$PTLDEBUG\\\";
1975             lctl set_param subsystem_debug=\\\"${SUBSYSTEM# }\\\";
1976             lctl set_param debug_mb=${DEBUG_SIZE};
1977             sync"
1978     fi
1979     if [ "$ONLY" == "setup" ]; then
1980         exit 0
1981     fi
1982 }
1983
1984 cleanup_and_setup_lustre() {
1985     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
1986         lctl set_param debug=0 || true
1987         cleanupall
1988         if [ "$ONLY" == "cleanup" ]; then
1989             exit 0
1990         fi
1991     fi
1992     check_and_setup_lustre
1993 }
1994
1995 check_and_cleanup_lustre() {
1996     if [ "`mount | grep $MOUNT`" ]; then
1997         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]*
1998         [ "$ENABLE_QUOTA" ] && restore_quota_type || true
1999     fi
2000     if [ "$I_UMOUNTED2" = "yes" ]; then
2001         mount_client $MOUNT2 || error "restore $MOUNT2 failed"
2002     fi
2003
2004     if [ "$I_MOUNTED" = "yes" ]; then
2005         cleanupall -f || error "cleanup failed"
2006     fi
2007     unset I_MOUNTED
2008 }
2009
2010 #######
2011 # General functions
2012
2013 check_network() {
2014     local NETWORK=0
2015     local WAIT=0
2016     local MAX=$2
2017     while [ $NETWORK -eq 0 ]; do
2018         if ping -c 1 -w 3 $1 > /dev/null; then
2019             NETWORK=1
2020         else
2021             WAIT=$((WAIT + 5))
2022             echo "waiting for $1, $((MAX - WAIT)) secs left"
2023             sleep 5
2024         fi
2025         if [ $WAIT -gt $MAX ]; then
2026             echo "Network not available"
2027             exit 1
2028         fi
2029     done
2030 }
2031 check_port() {
2032     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
2033         sleep 9
2034     done
2035 }
2036
2037 no_dsh() {
2038     shift
2039     eval $@
2040 }
2041
2042 comma_list() {
2043     # the sed converts spaces to commas, but leaves the last space
2044     # alone, so the line doesn't end with a comma.
2045     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
2046 }
2047
2048 # list, excluded are the comma separated lists
2049 exclude_items_from_list () {
2050     local list=$1
2051     local excluded=$2
2052     local item
2053
2054     list=${list//,/ }
2055     for item in ${excluded//,/ }; do
2056         list=$(echo " $list " | sed -re "s/\s+$item\s+/ /g")
2057     done
2058     echo $(comma_list $list) 
2059 }
2060
2061 # list, expand  are the comma separated lists
2062 expand_list () {
2063     local list=${1//,/ }
2064     local expand=${2//,/ }
2065     local expanded=
2066
2067     expanded=$(for i in $list $expand; do echo $i; done | sort -u)
2068     echo $(comma_list $expanded)
2069 }
2070
2071 testslist_filter () {
2072     local script=$LUSTRE/tests/${TESTSUITE}.sh
2073
2074     [ -f $script ] || return 0
2075
2076     local start_at=$START_AT
2077     local stop_at=$STOP_AT
2078
2079     local var=${TESTSUITE//-/_}_START_AT
2080     [ x"${!var}" != x ] && start_at=${!var}
2081     var=${TESTSUITE//-/_}_STOP_AT
2082     [ x"${!var}" != x ] && stop_at=${!var}
2083
2084     sed -n 's/^test_\([^ (]*\).*/\1/p' $script | \
2085         awk ' BEGIN { if ("'${start_at:-0}'" != 0) flag = 1 }
2086             /^'${start_at}'$/ {flag = 0}
2087             {if (flag == 1) print $0}
2088             /^'${stop_at}'$/ { flag = 1 }'
2089 }
2090
2091 absolute_path() {
2092     (cd `dirname $1`; echo $PWD/`basename $1`)
2093 }
2094
2095 get_facets () {
2096     local name=$(echo $1 | tr "[:upper:]" "[:lower:]")
2097     local type=$(echo $1 | tr "[:lower:]" "[:upper:]")
2098
2099     local list=""
2100     local count=${type}COUNT
2101     for ((i=1; i<=${!count}; i++)) do
2102         list="$list ${name}$i"
2103     done
2104     echo $(comma_list $list)
2105 }
2106
2107 ##################################
2108 # Adaptive Timeouts funcs
2109
2110 at_is_enabled() {
2111     # only check mds, we assume at_max is the same on all nodes
2112     local at_max=$(do_facet $SINGLEMDS "lctl get_param -n at_max")
2113     if [ $at_max -eq 0 ]; then
2114         return 1
2115     else
2116         return 0
2117     fi
2118 }
2119
2120 at_max_get() {
2121     local facet=$1
2122
2123     # suppose that all ost-s has the same at_max set
2124     if [ $facet == "ost" ]; then
2125         do_facet ost1 "lctl get_param -n at_max"
2126     else
2127         do_facet $facet "lctl get_param -n at_max"
2128     fi
2129 }
2130
2131 at_max_set() {
2132     local at_max=$1
2133     shift
2134
2135     local facet
2136     for facet in $@; do
2137         if [ $facet == "ost" ]; then
2138             for i in `seq $OSTCOUNT`; do
2139                 do_facet ost$i "lctl set_param at_max=$at_max"
2140
2141             done
2142         elif [ $facet == "mds" ]; then
2143             for i in `seq $MDSCOUNT`; do
2144                 do_facet mds$i "lctl set_param at_max=$at_max"
2145             done
2146         else
2147             do_facet $facet "lctl set_param at_max=$at_max"
2148         fi
2149     done
2150 }
2151
2152 ##################################
2153 # OBD_FAIL funcs
2154
2155 drop_request() {
2156 # OBD_FAIL_MDS_ALL_REQUEST_NET
2157     RC=0
2158     do_facet $SINGLEMDS lctl set_param fail_loc=0x123
2159     do_facet client "$1" || RC=$?
2160     do_facet $SINGLEMDS lctl set_param fail_loc=0
2161     return $RC
2162 }
2163
2164 drop_reply() {
2165 # OBD_FAIL_MDS_ALL_REPLY_NET
2166     RC=0
2167     do_facet $SINGLEMDS lctl set_param fail_loc=0x122
2168     do_facet client "$@" || RC=$?
2169     do_facet $SINGLEMDS lctl set_param fail_loc=0
2170     return $RC
2171 }
2172
2173 drop_reint_reply() {
2174 # OBD_FAIL_MDS_REINT_NET_REP
2175     RC=0
2176     do_facet $SINGLEMDS lctl set_param fail_loc=0x119
2177     do_facet client "$@" || RC=$?
2178     do_facet $SINGLEMDS lctl set_param fail_loc=0
2179     return $RC
2180 }
2181
2182 pause_bulk() {
2183 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
2184     RC=0
2185     do_facet ost1 lctl set_param fail_loc=0x214
2186     do_facet client "$1" || RC=$?
2187     do_facet client "sync"
2188     do_facet ost1 lctl set_param fail_loc=0
2189     return $RC
2190 }
2191
2192 drop_ldlm_cancel() {
2193 #define OBD_FAIL_LDLM_CANCEL             0x304
2194     RC=0
2195     do_facet client lctl set_param fail_loc=0x304
2196     do_facet client "$@" || RC=$?
2197     do_facet client lctl set_param fail_loc=0
2198     return $RC
2199 }
2200
2201 drop_bl_callback() {
2202 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
2203     RC=0
2204     do_facet client lctl set_param fail_loc=0x305
2205     do_facet client "$@" || RC=$?
2206     do_facet client lctl set_param fail_loc=0
2207     return $RC
2208 }
2209
2210 drop_ldlm_reply() {
2211 #define OBD_FAIL_LDLM_REPLY              0x30c
2212     RC=0
2213     do_facet $SINGLEMDS lctl set_param fail_loc=0x30c
2214     do_facet client "$@" || RC=$?
2215     do_facet $SINGLEMDS lctl set_param fail_loc=0
2216     return $RC
2217 }
2218
2219 clear_failloc() {
2220     facet=$1
2221     pause=$2
2222     sleep $pause
2223     echo "clearing fail_loc on $facet"
2224     do_facet $facet "lctl set_param fail_loc=0 2>/dev/null || true"
2225 }
2226
2227 set_nodes_failloc () {
2228     do_nodes $(comma_list $1)  lctl set_param fail_loc=$2
2229 }
2230
2231 cancel_lru_locks() {
2232     $LCTL mark "cancel_lru_locks $1 start"
2233     for d in `lctl get_param -N ldlm.namespaces.*.lru_size | egrep -i $1`; do
2234         $LCTL set_param -n $d=clear
2235     done
2236     $LCTL get_param ldlm.namespaces.*.lock_unused_count | egrep -i $1 | grep -v '=0'
2237     $LCTL mark "cancel_lru_locks $1 stop"
2238 }
2239
2240 default_lru_size()
2241 {
2242         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
2243         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
2244         echo "$DEFAULT_LRU_SIZE"
2245 }
2246
2247 lru_resize_enable()
2248 {
2249     lctl set_param ldlm.namespaces.*$1*.lru_size=0
2250 }
2251
2252 lru_resize_disable()
2253 {
2254     lctl set_param ldlm.namespaces.*$1*.lru_size $(default_lru_size)
2255 }
2256
2257 pgcache_empty() {
2258     local FILE
2259     for FILE in `lctl get_param -N "llite.*.dump_page_cache"`; do
2260         if [ `lctl get_param -n $FILE | wc -l` -gt 1 ]; then
2261             echo there is still data in page cache $FILE ?
2262             lctl get_param -n $FILE
2263             return 1
2264         fi
2265     done
2266     if [[ $MDSDEV1 != $MGSDEV ]]; then
2267         stop mgs 
2268     fi
2269
2270     return 0
2271 }
2272
2273 debugsave() {
2274     DEBUGSAVE="$(lctl get_param -n debug)"
2275 }
2276
2277 debugrestore() {
2278     [ -n "$DEBUGSAVE" ] && \
2279         do_nodes $(comma_list $(nodes_list)) "$LCTL set_param debug=\\\"${DEBUGSAVE}\\\";"
2280     DEBUGSAVE=""
2281 }
2282
2283 debug_size_save() {
2284     DEBUG_SIZE_SAVED="$(lctl get_param -n debug_mb)"
2285 }
2286
2287 debug_size_restore() {
2288     [ -n "$DEBUG_SIZE_SAVED" ] && \
2289         do_nodes $(comma_list $(nodes_list)) "$LCTL set_param debug_mb=$DEBUG_SIZE_SAVED"
2290     DEBUG_SIZE_SAVED=""
2291 }
2292
2293 start_full_debug_logging() {
2294     debugsave
2295     debug_size_save
2296
2297     local FULLDEBUG=-1
2298     local DEBUG_SIZE=150
2299
2300     do_nodes $(comma_list $(nodes_list)) "$LCTL set_param debug_mb=$DEBUG_SIZE"
2301     do_nodes $(comma_list $(nodes_list)) "$LCTL set_param debug=$FULLDEBUG;"
2302 }
2303
2304 stop_full_debug_logging() {
2305     debug_size_restore
2306     debugrestore
2307 }
2308
2309 ##################################
2310 # Test interface
2311 ##################################
2312
2313 error_noexit() {
2314     local TYPE=${TYPE:-"FAIL"}
2315     local ERRLOG
2316
2317     local dump=true
2318     # do not dump logs if $1=false
2319     if [ "x$1" = "xfalse" ]; then
2320         shift
2321         dump=false
2322     fi
2323
2324     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
2325
2326     if $dump; then
2327         ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
2328         echo "Dumping lctl log to $ERRLOG"
2329         # We need to dump the logs on all nodes
2330         do_nodes $(comma_list $(nodes_list)) $NODE $LCTL dk $ERRLOG
2331     fi
2332     debugrestore
2333     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
2334     TEST_FAILED=true
2335 }
2336
2337 error() {
2338     error_noexit "$@"
2339     if $FAIL_ON_ERROR; then
2340         reset_fail_loc
2341         exit 1
2342     fi
2343 }
2344
2345 error_exit() {
2346     error_noexit "$@"
2347     exit 1
2348 }
2349
2350 # use only if we are ignoring failures for this test, bugno required.
2351 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
2352 # e.g. error_ignore 5494 "your message"
2353 error_ignore() {
2354     local TYPE="IGNORE (bz$1)"
2355     shift
2356     error_noexit "$@"
2357 }
2358
2359 skip_env () {
2360     $FAIL_ON_SKIP_ENV && error false $@ || skip $@
2361 }
2362
2363 skip () {
2364     echo
2365     log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
2366     [ "$TESTSUITELOG" ] && \
2367         echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG || true
2368 }
2369
2370 build_test_filter() {
2371     EXCEPT="$EXCEPT $(testslist_filter)"
2372
2373     [ "$ONLY" ] && log "only running test `echo $ONLY`"
2374     for O in $ONLY; do
2375         eval ONLY_${O}=true
2376     done
2377     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
2378         log "excepting tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
2379     [ "$EXCEPT_SLOW" ] && \
2380         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
2381     for E in $EXCEPT $ALWAYS_EXCEPT; do
2382         eval EXCEPT_${E}=true
2383     done
2384     for E in $EXCEPT_SLOW; do
2385         eval EXCEPT_SLOW_${E}=true
2386     done
2387     for G in $GRANT_CHECK_LIST; do
2388         eval GCHECK_ONLY_${G}=true
2389         done
2390 }
2391
2392 basetest() {
2393     if [[ $1 = [a-z]* ]]; then
2394         echo $1
2395     else
2396         echo ${1%%[a-z]*}
2397     fi
2398 }
2399
2400 # print a newline if the last test was skipped
2401 export LAST_SKIPPED=
2402 run_test() {
2403     assert_DIR
2404
2405     export base=`basetest $1`
2406     if [ ! -z "$ONLY" ]; then
2407         testname=ONLY_$1
2408         if [ ${!testname}x != x ]; then
2409             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
2410             run_one $1 "$2"
2411             return $?
2412         fi
2413         testname=ONLY_$base
2414         if [ ${!testname}x != x ]; then
2415             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
2416             run_one $1 "$2"
2417             return $?
2418         fi
2419         LAST_SKIPPED="y"
2420         echo -n "."
2421         return 0
2422     fi
2423     testname=EXCEPT_$1
2424     if [ ${!testname}x != x ]; then
2425         LAST_SKIPPED="y"
2426         TESTNAME=test_$1 skip "skipping excluded test $1"
2427         return 0
2428     fi
2429     testname=EXCEPT_$base
2430     if [ ${!testname}x != x ]; then
2431         LAST_SKIPPED="y"
2432         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
2433         return 0
2434     fi
2435     testname=EXCEPT_SLOW_$1
2436     if [ ${!testname}x != x ]; then
2437         LAST_SKIPPED="y"
2438         TESTNAME=test_$1 skip "skipping SLOW test $1"
2439         return 0
2440     fi
2441     testname=EXCEPT_SLOW_$base
2442     if [ ${!testname}x != x ]; then
2443         LAST_SKIPPED="y"
2444         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
2445         return 0
2446     fi
2447
2448     LAST_SKIPPED=
2449     run_one $1 "$2"
2450
2451     return $?
2452 }
2453
2454 EQUALS="======================================================================"
2455 equals_msg() {
2456     msg="$@"
2457
2458     local suffixlen=$((${#EQUALS} - ${#msg}))
2459     [ $suffixlen -lt 5 ] && suffixlen=5
2460     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
2461 }
2462
2463 log() {
2464     echo "$*"
2465     module_loaded lnet || load_modules
2466
2467     local MSG="$*"
2468     # Get rid of '
2469     MSG=${MSG//\'/\\\'}
2470     MSG=${MSG//\(/\\\(}
2471     MSG=${MSG//\)/\\\)}
2472     MSG=${MSG//\;/\\\;}
2473     MSG=${MSG//\|/\\\|}
2474     MSG=${MSG//\>/\\\>}
2475     MSG=${MSG//\</\\\<}
2476     MSG=${MSG//\//\\\/}
2477     do_nodes $(comma_list $(nodes_list)) $LCTL mark "$MSG" 2> /dev/null || true
2478 }
2479
2480 trace() {
2481         log "STARTING: $*"
2482         strace -o $TMP/$1.strace -ttt $*
2483         RC=$?
2484         log "FINISHED: $*: rc $RC"
2485         return 1
2486 }
2487
2488 pass() {
2489     $TEST_FAILED && echo -n "FAIL " || echo -n "PASS " 
2490     echo $@
2491 }
2492
2493 check_mds() {
2494     FFREE=$(do_node $SINGLEMDS lctl get_param -n osd.*MDT*.filesfree | awk 'BEGIN{avail=0}; {avail+=$1}; END{print avail}')
2495     FTOTAL=$(do_node $SINGLEMDS lctl get_param -n osd.*MDT*.filestotal | awk 'BEGIN{avail=0}; {avail+=$1}; END{print avail}')
2496     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
2497 }
2498
2499 reset_fail_loc () {
2500     echo -n "Resetting fail_loc on all nodes..."
2501     do_nodes $(comma_list $(nodes_list)) "lctl set_param -n fail_loc=0 2>/dev/null || true"
2502     echo done.
2503 }
2504
2505 run_one() {
2506     testnum=$1
2507     message=$2
2508     tfile=f${testnum}
2509     export tdir=d0.${TESTSUITE}/d${base}
2510
2511     local SAVE_UMASK=`umask`
2512     umask 0022
2513
2514     local BEFORE=`date +%s`
2515     echo
2516     log "== test $testnum: $message == `date +%H:%M:%S` ($BEFORE)"
2517     #check_mds
2518     export TESTNAME=test_$testnum
2519     TEST_FAILED=false
2520     test_${testnum} || error "test_$testnum failed with $?"
2521     #check_mds
2522     cd $SAVE_PWD
2523     reset_fail_loc
2524     check_grant ${testnum} || error "check_grant $testnum failed with $?"
2525     check_catastrophe || error "LBUG/LASSERT detected"
2526     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
2527     pass "($((`date +%s` - $BEFORE))s)"
2528     TEST_FAILED=false
2529     unset TESTNAME
2530     unset tdir
2531     umask $SAVE_UMASK
2532 }
2533
2534 canonical_path() {
2535     (cd `dirname $1`; echo $PWD/`basename $1`)
2536 }
2537
2538 sync_clients() {
2539     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync
2540     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync
2541         cd $SAVE_PWD
2542 }
2543
2544 check_grant() {
2545     export base=`basetest $1`
2546     [ "$CHECK_GRANT" == "no" ] && return 0
2547
2548         testname=GCHECK_ONLY_${base}
2549         [ ${!testname}x == x ] && return 0
2550
2551     echo -n "checking grant......"
2552         cd $SAVE_PWD
2553         # write some data to sync client lost_grant
2554         rm -f $DIR1/${tfile}_check_grant_* 2>&1
2555         for i in `seq $OSTCOUNT`; do
2556                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
2557                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
2558                                               count=1 > /dev/null 2>&1
2559         done
2560         # sync all the data and make sure no pending data on server
2561         sync_clients
2562         
2563         #get client grant and server grant
2564         client_grant=0
2565     for d in `lctl get_param -n osc.*.cur_grant_bytes`; do
2566                 client_grant=$((client_grant + $d))
2567         done
2568         server_grant=0
2569         for d in `lctl get_param -n obdfilter.*.tot_granted`; do
2570                 server_grant=$((server_grant + $d))
2571         done
2572
2573         # cleanup the check_grant file
2574         for i in `seq $OSTCOUNT`; do
2575                 rm $DIR1/${tfile}_check_grant_$i
2576         done
2577
2578         #check whether client grant == server grant
2579         if [ $client_grant != $server_grant ]; then
2580                 echo "failed: client:${client_grant} server: ${server_grant}"
2581                 return 1
2582         else
2583                 echo "pass"
2584         fi
2585 }
2586
2587 ########################
2588 # helper functions
2589
2590 osc_to_ost()
2591 {
2592     osc=$1
2593     ost=`echo $1 | awk -F_ '{print $3}'`
2594     if [ -z $ost ]; then
2595         ost=`echo $1 | sed 's/-osc.*//'`
2596     fi
2597     echo $ost
2598 }
2599
2600 remote_node () {
2601     local node=$1
2602     [ "$node" != "$(hostname)" ]
2603 }
2604
2605 remote_mds ()
2606 {
2607     local node
2608     for node in $(mdts_nodes); do
2609         remote_node $node && return 0
2610     done
2611     return 1
2612 }
2613
2614 remote_mds_nodsh()
2615 {
2616     [ "$CLIENTONLY" ] && return 0 || true
2617     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
2618 }
2619
2620 remote_ost ()
2621 {
2622     local node
2623     for node in $(osts_nodes) ; do
2624         remote_node $node && return 0
2625     done
2626     return 1
2627 }
2628
2629 remote_ost_nodsh()
2630 {
2631     [ "$CLIENTONLY" ] && return 0 || true 
2632     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
2633 }
2634
2635 remote_mgs_nodsh()
2636 {
2637     local MGS 
2638     MGS=$(facet_host mgs)
2639     remote_node $MGS && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
2640 }
2641
2642 mdts_nodes () {
2643     local MDSNODES
2644     local NODES_sort
2645     for num in `seq $MDSCOUNT`; do
2646         MDSNODES="$MDSNODES $(facet_host mds$num)"
2647     done
2648     NODES_sort=$(for i in $MDSNODES; do echo $i; done | sort -u)
2649
2650     echo $NODES_sort
2651 }
2652
2653 remote_servers () {
2654     remote_ost && remote_mds
2655 }
2656
2657 osts_nodes () {
2658     local OSTNODES=$(facet_host ost1)
2659     local NODES_sort
2660
2661     for num in `seq $OSTCOUNT`; do
2662         local myOST=$(facet_host ost$num)
2663         OSTNODES="$OSTNODES $myOST"
2664     done
2665     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
2666
2667     echo $NODES_sort
2668 }
2669
2670 nodes_list () {
2671     # FIXME. We need a list of clients
2672     local myNODES=$HOSTNAME
2673     local myNODES_sort
2674
2675     # CLIENTS (if specified) contains the local client
2676     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
2677
2678     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
2679         myNODES="$myNODES $(osts_nodes) $(mdts_nodes)"
2680     fi
2681
2682     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
2683
2684     echo $myNODES_sort
2685 }
2686
2687 remote_nodes_list () {
2688     local rnodes=$(nodes_list)
2689     rnodes=$(echo " $rnodes " | sed -re "s/\s+$HOSTNAME\s+/ /g")
2690     echo $rnodes
2691 }
2692
2693 init_clients_lists () {
2694     # Sanity check: exclude the local client from RCLIENTS
2695     local rclients=$(echo " $RCLIENTS " | sed -re "s/\s+$HOSTNAME\s+/ /g")
2696
2697     # Sanity check: exclude the dup entries
2698     rclients=$(for i in $rclients; do echo $i; done | sort -u)
2699
2700     local clients="$SINGLECLIENT $HOSTNAME $rclients"
2701
2702     # Sanity check: exclude the dup entries from CLIENTS
2703     # for those configs which has SINGLCLIENT set to local client
2704     clients=$(for i in $clients; do echo $i; done | sort -u)
2705
2706     CLIENTS=`comma_list $clients`
2707     local -a remoteclients=($rclients)
2708     for ((i=0; $i<${#remoteclients[@]}; i++)); do
2709             varname=CLIENT$((i + 2))
2710             eval $varname=${remoteclients[i]}
2711     done
2712
2713     CLIENTCOUNT=$((${#remoteclients[@]} + 1))
2714 }
2715
2716 get_random_entry () {
2717     local rnodes=$1
2718
2719     rnodes=${rnodes//,/ }
2720
2721     local -a nodes=($rnodes)
2722     local num=${#nodes[@]} 
2723     local i=$((RANDOM * num * 2 / 65536))
2724
2725     echo ${nodes[i]}
2726 }
2727
2728 client_only () {
2729     [ "$CLIENTONLY" ] || [ "$CLIENTMODSONLY" = yes ]
2730 }
2731
2732 is_patchless ()
2733 {
2734     lctl get_param version | grep -q patchless
2735 }
2736
2737 check_versions () {
2738     [ "$MDSVER" = "$CLIVER" -a "$OSTVER" = "$CLIVER" ]
2739 }
2740
2741 get_node_count() {
2742     local nodes="$@"
2743     echo $nodes | wc -w || true
2744 }
2745
2746 mixed_ost_devs () {
2747     local nodes=$(osts_nodes)
2748     local osscount=$(get_node_count "$nodes")
2749     [ ! "$OSTCOUNT" = "$osscount" ]
2750 }
2751
2752 mixed_mdt_devs () {
2753     local nodes=$(mdts_nodes)
2754     local mdtcount=$(get_node_count "$nodes")
2755     [ ! "$MDSCOUNT" = "$mdtcount" ]
2756 }
2757
2758 generate_machine_file() {
2759     local nodes=${1//,/ }
2760     local machinefile=$2
2761     rm -f $machinefile || error "can't rm $machinefile"
2762     for node in $nodes; do
2763         echo $node >>$machinefile
2764     done
2765 }
2766
2767 get_stripe () {
2768     local file=$1/stripe
2769     touch $file
2770     $LFS getstripe -v $file || error
2771     rm -f $file
2772 }
2773
2774 check_runas_id_ret() {
2775     local myRC=0
2776     local myRUNAS_UID=$1
2777     local myRUNAS_GID=$2
2778     shift 2
2779     local myRUNAS=$@
2780     if [ -z "$myRUNAS" ]; then
2781         error_exit "myRUNAS command must be specified for check_runas_id"
2782     fi
2783     if $GSS_KRB5; then
2784         $myRUNAS krb5_login.sh || \
2785             error "Failed to refresh Kerberos V5 TGT for UID $myRUNAS_ID."
2786     fi
2787     mkdir $DIR/d0_runas_test
2788     chmod 0755 $DIR
2789     chown $myRUNAS_UID:$myRUNAS_GID $DIR/d0_runas_test
2790     $myRUNAS touch $DIR/d0_runas_test/f$$ || myRC=$?
2791     rm -rf $DIR/d0_runas_test
2792     return $myRC
2793 }
2794
2795 check_runas_id() {
2796     local myRUNAS_UID=$1
2797     local myRUNAS_GID=$2
2798     shift 2
2799     local myRUNAS=$@
2800     check_runas_id_ret $myRUNAS_UID $myRUNAS_GID $myRUNAS || \
2801         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_UID.
2802         Please set RUNAS_ID to some UID which exists on MDS and client or
2803         add user $myRUNAS_UID:$myRUNAS_GID on these nodes."
2804 }
2805
2806 # obtain the UID/GID for MPI_USER
2807 get_mpiuser_id() {
2808     local mpi_user=$1
2809
2810     MPI_USER_UID=$(do_facet client "getent passwd $mpi_user | cut -d: -f3;
2811 exit \\\${PIPESTATUS[0]}") || error_exit "failed to get the UID for $mpi_user"
2812
2813     MPI_USER_GID=$(do_facet client "getent passwd $mpi_user | cut -d: -f4;
2814 exit \\\${PIPESTATUS[0]}") || error_exit "failed to get the GID for $mpi_user"
2815 }
2816
2817 # obtain and cache Kerberos ticket-granting ticket
2818 refresh_krb5_tgt() {
2819     local myRUNAS_UID=$1
2820     local myRUNAS_GID=$2
2821     shift 2
2822     local myRUNAS=$@
2823     if [ -z "$myRUNAS" ]; then
2824         error_exit "myRUNAS command must be specified for refresh_krb5_tgt"
2825     fi
2826
2827     CLIENTS=${CLIENTS:-$HOSTNAME}
2828     do_nodes $CLIENTS "set -x
2829 if ! $myRUNAS krb5_login.sh; then
2830     echo "Failed to refresh Krb5 TGT for UID/GID $myRUNAS_UID/$myRUNAS_GID."
2831     exit 1
2832 fi"
2833 }
2834
2835 # Run multiop in the background, but wait for it to print
2836 # "PAUSING" to its stdout before returning from this function.
2837 multiop_bg_pause() {
2838     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
2839     FILE=$1
2840     ARGS=$2
2841
2842     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
2843     mkfifo $TMPPIPE
2844
2845     echo "$MULTIOP_PROG $FILE v$ARGS"
2846     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
2847
2848     echo "TMPPIPE=${TMPPIPE}"
2849     read -t 60 multiop_output < $TMPPIPE
2850     if [ $? -ne 0 ]; then
2851         rm -f $TMPPIPE
2852         return 1
2853     fi
2854     rm -f $TMPPIPE
2855     if [ "$multiop_output" != "PAUSING" ]; then
2856         echo "Incorrect multiop output: $multiop_output"
2857         kill -9 $PID
2858         return 1
2859     fi
2860
2861     return 0
2862 }
2863
2864 do_and_time () {
2865     local cmd=$1
2866     local rc
2867
2868     SECONDS=0
2869     eval '$cmd'
2870     
2871     [ ${PIPESTATUS[0]} -eq 0 ] || rc=1
2872
2873     echo $SECONDS
2874     return $rc
2875 }
2876
2877 inodes_available () {
2878     local IFree=$($LFS df -i $MOUNT | grep ^$FSNAME | awk '{print $4}' | sort -un | head -1) || return 1
2879     echo $IFree
2880 }
2881
2882 # reset llite stat counters
2883 clear_llite_stats(){
2884         lctl set_param -n llite.*.stats 0
2885 }
2886
2887 # sum llite stat items
2888 calc_llite_stats() {
2889         local res=$(lctl get_param -n llite.*.stats |
2890                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
2891         echo $res
2892 }
2893
2894 # reset osc stat counters
2895 clear_osc_stats(){
2896         lctl set_param -n osc.*.osc_stats 0
2897 }
2898
2899 # sum osc stat items
2900 calc_osc_stats() {
2901         local res=$(lctl get_param -n osc.*.osc_stats |
2902                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
2903         echo $res
2904 }
2905
2906 calc_sum () {
2907         awk 'BEGIN {s = 0}; {s += $1}; END {print s}'
2908 }
2909
2910 calc_osc_kbytes () {
2911         df $MOUNT > /dev/null
2912         $LCTL get_param -n osc.*[oO][sS][cC][-_][0-9a-f]*.$1 | calc_sum
2913 }
2914
2915 # save_lustre_params(node, parameter_mask)
2916 # generate a stream of formatted strings (<node> <param name>=<param value>)
2917 save_lustre_params() {
2918         local s
2919         do_nodes --verbose $1 "lctl get_param $2 | while read s; do echo \\\$s; done"
2920 }
2921
2922 # restore lustre parameters from input stream, produces by save_lustre_params
2923 restore_lustre_params() {
2924         local node
2925         local name
2926         local val
2927         while IFS=" =" read node name val; do
2928                 do_node ${node//:/} "lctl set_param -n $name $val"
2929         done
2930 }
2931
2932 check_catastrophe() {
2933     local rnodes=${1:-$(comma_list $(remote_nodes_list))}
2934     local C=$CATASTROPHE
2935     [ -f $C ] && [ $(cat $C) -ne 0 ] && return 1
2936
2937     if [ $rnodes ]; then
2938         do_nodes $rnodes "rc=\\\$([ -f $C ] && echo \\\$(< $C) || echo 0);
2939 if [ \\\$rc -ne 0 ]; then echo \\\$(hostname): \\\$rc; fi
2940 exit \\\$rc;"
2941     fi 
2942 }
2943
2944 # $1 node
2945 # $2 file
2946 # $3 $RUNAS
2947 get_stripe_info() {
2948         local tmp_file
2949
2950         stripe_size=0
2951         stripe_count=0
2952         stripe_index=0
2953         tmp_file=$(mktemp)
2954
2955         do_facet $1 $3 lfs getstripe -v $2 > $tmp_file
2956
2957         stripe_size=`awk '$1 ~ /size/ {print $2}' $tmp_file`
2958         stripe_count=`awk '$1 ~ /count/ {print $2}' $tmp_file`
2959         stripe_index=`awk '$1 ~ /stripe_offset/ {print $2}' $tmp_file`
2960         rm -f $tmp_file
2961 }
2962
2963 # CMD: determine mds index where directory inode presents
2964 get_mds_dir () {
2965     local dir=$1
2966     local file=$dir/f0.get_mds_dir_tmpfile
2967
2968     mkdir -p $dir
2969     rm -f $file
2970     sleep 1
2971     local iused=$(lfs df -i $dir | grep MDT | awk '{print $3}')
2972     local -a oldused=($iused)
2973
2974     openfile -f O_CREAT:O_LOV_DELAY_CREATE -m 0644 $file > /dev/null
2975     sleep 1
2976     iused=$(lfs df -i $dir | grep MDT | awk '{print $3}')
2977     local -a newused=($iused)
2978
2979     local num=0
2980     for ((i=0; i<${#newused[@]}; i++)); do
2981          if [ ${oldused[$i]} -lt ${newused[$i]} ];  then
2982              echo $(( i + 1 ))
2983              rm -f $file
2984              return 0
2985          fi
2986     done
2987     error "mdt-s : inodes count OLD ${oldused[@]} NEW ${newused[@]}"
2988 }
2989
2990 mdsrate_cleanup () {
2991     if [ -d $4 ]; then
2992         mpi_run -np $1 -machinefile $2 ${MDSRATE} --unlink --nfiles $3 --dir $4 --filefmt $5 $6
2993         rmdir $4
2994     fi
2995 }
2996
2997 delayed_recovery_enabled () {
2998     local var=${SINGLEMDS}_svc
2999     do_facet $SINGLEMDS lctl get_param -n mdd.${!var}.stale_export_age > /dev/null 2>&1
3000 }
3001
3002 ########################
3003
3004 convert_facet2label() { 
3005     local facet=$1
3006
3007     if [ x$facet = xost ]; then
3008        facet=ost1
3009     fi
3010
3011     local varsvc=${facet}_svc
3012
3013     if [ -n ${!varsvc} ]; then
3014         echo ${!varsvc}
3015     else  
3016         error "No lablel for $facet!"
3017     fi
3018 }
3019
3020 get_clientosc_proc_path() {
3021     local ost=$1
3022
3023     echo "{$1}-osc-*"
3024 }
3025
3026 get_lustre_version () {
3027     local node=${1:-"mds"}    
3028     do_facet $node $LCTL get_param -n version |  awk '/^lustre:/ {print $2}'
3029 }
3030
3031 get_mds_version_major () {
3032     local version=$(get_lustre_version mds)
3033     echo $version | awk -F. '{print $1}'
3034 }
3035
3036 get_mds_version_minor () {
3037     local version=$(get_lustre_version mds)
3038     echo $version | awk -F. '{print $2}'
3039 }
3040
3041 get_mdtosc_proc_path() {
3042     local ost=$1
3043     local major=$(get_mds_version_major)
3044     local minor=$(get_mds_version_minor)
3045     if [ $major -le 1 -a $minor -le 8 ] ; then
3046         echo "${ost}-osc"
3047     else
3048         echo "${ost}-osc-MDT0000"
3049     fi
3050 }
3051
3052 get_osc_import_name() {
3053     local facet=$1
3054     local ost=$2
3055     local label=$(convert_facet2label $ost)
3056
3057     if [ "$facet" == "mds" ]; then
3058         get_mdtosc_proc_path $label
3059         return 0
3060     fi
3061
3062     get_clientosc_proc_path $label
3063     return 0
3064 }
3065
3066 wait_import_state () {
3067     local expected=$1
3068     local CONN_PROC=$2
3069     local CONN_STATE
3070     local i=0
3071
3072     CONN_STATE=$($LCTL get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3073     while [ "${CONN_STATE}" != "${expected}" ]; do
3074         if [ "${expected}" == "DISCONN" ]; then
3075             # for disconn we can check after proc entry is removed
3076             [ "x${CONN_STATE}" == "x" ] && return 0
3077             #  with AT we can have connect request timeout ~ reconnect timeout
3078             # and test can't see real disconnect
3079             [ "${CONN_STATE}" == "CONNECTING" ] && return 0
3080         fi
3081         # disconnect rpc should be wait not more obd_timeout
3082         [ $i -ge $(($TIMEOUT * 3 / 2)) ] && \
3083             error "can't put import for $CONN_PROC into ${expected} state" && return 1
3084         sleep 1
3085         CONN_STATE=$($LCTL get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3086         i=$(($i + 1))
3087     done
3088
3089     log "$CONN_PROC now in ${CONN_STATE} state"
3090     return 0
3091 }
3092
3093 wait_osc_import_state() {
3094     local facet=$1
3095     local ost_facet=$2
3096     local expected=$3
3097     local ost=$(get_osc_import_name $facet $ost_facet)
3098     local CONN_PROC
3099     local CONN_STATE
3100     local i=0
3101
3102     CONN_PROC="osc.${ost}.ost_server_uuid"
3103     CONN_STATE=$(do_facet $facet lctl get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3104     while [ "${CONN_STATE}" != "${expected}" ]; do
3105         if [ "${expected}" == "DISCONN" ]; then 
3106             # for disconn we can check after proc entry is removed
3107             [ "x${CONN_STATE}" == "x" ] && return 0
3108             #  with AT we can have connect request timeout ~ reconnect timeout
3109             # and test can't see real disconnect
3110             [ "${CONN_STATE}" == "CONNECTING" ] && return 0
3111         fi
3112         # disconnect rpc should be wait not more obd_timeout
3113         [ $i -ge $(($TIMEOUT * 3 / 2)) ] && \
3114             error "can't put import for ${ost}(${ost_facet}) into ${expected} state" && return 1
3115         sleep 1
3116         CONN_STATE=$(do_facet $facet lctl get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3117         i=$(($i + 1))
3118     done
3119
3120     log "${ost_facet} now in ${CONN_STATE} state"
3121     return 0
3122 }
3123
3124 get_clientmdc_proc_path() {
3125     echo "${1}-mdc-*"
3126 }
3127
3128 do_rpc_nodes () {
3129     local list=$1
3130     shift
3131
3132     do_nodes --verbose $list "PATH=$LUSTRE/tests/:$PATH sh rpc.sh $@ " 
3133 }
3134
3135 wait_clients_import_state () {
3136     local list=$1
3137     local facet=$2
3138     local expected=$3
3139     shift
3140
3141     local label=$(convert_facet2label $facet)
3142     local proc_path
3143     case $facet in
3144         ost* ) proc_path="osc.$(get_clientosc_proc_path $label).ost_server_uuid" ;;
3145         mds* ) proc_path="mdc.$(get_clientmdc_proc_path $label).mds_server_uuid" ;;
3146         *) error "unknown facet!" ;;
3147     esac
3148
3149     if ! do_rpc_nodes $list wait_import_state $expected $proc_path; then
3150         error "import is not in ${expected} state"
3151         return 1
3152     fi
3153 }
3154
3155 oos_full() {
3156         local -a AVAILA
3157         local -a GRANTA
3158         local OSCFULL=1
3159         AVAILA=($(do_nodes $(comma_list $(osts_nodes)) \
3160                   $LCTL get_param obdfilter.*.kbytesavail))
3161         GRANTA=($(do_nodes $(comma_list $(osts_nodes)) \
3162                   $LCTL get_param -n obdfilter.*.tot_granted))
3163         for ((i=0; i<${#AVAILA[@]}; i++)); do
3164                 local -a AVAIL1=(${AVAILA[$i]//=/ })
3165                 GRANT=$((${GRANTA[$i]}/1024))
3166                 echo -n $(echo ${AVAIL1[0]} | cut -d"." -f2) avl=${AVAIL1[1]} grnt=$GRANT diff=$((AVAIL1[1] - GRANT))
3167                 [ $((AVAIL1[1] - GRANT)) -lt 400 ] && OSCFULL=0 && echo " FULL" || echo
3168         done
3169         return $OSCFULL
3170 }
3171
3172 pool_list () {
3173    do_facet mgs lctl pool_list $1
3174 }
3175
3176 create_pool() {
3177     local fsname=${1%%.*}
3178     local poolname=${1##$fsname.}
3179
3180     do_facet mgs lctl pool_new $1
3181     local RC=$?
3182     # get param should return err unless pool is created
3183     [[ $RC -ne 0 ]] && return $RC
3184
3185     wait_update $HOSTNAME "lctl get_param -n lov.$fsname-*.pools.$poolname \
3186         2>/dev/null || echo foo" "" || RC=1
3187     if [[ $RC -eq 0 ]]; then
3188         add_pool_to_list $1
3189     else
3190         error "pool_new failed $1"
3191     fi
3192     return $RC
3193 }
3194
3195 add_pool_to_list () {
3196     local fsname=${1%%.*}
3197     local poolname=${1##$fsname.}
3198
3199     local listvar=${fsname}_CREATED_POOLS
3200     eval export ${listvar}=$(expand_list ${!listvar} $poolname)
3201 }
3202
3203 remove_pool_from_list () {
3204     local fsname=${1%%.*}
3205     local poolname=${1##$fsname.}
3206
3207     local listvar=${fsname}_CREATED_POOLS
3208     eval export ${listvar}=$(exclude_items_from_list ${!listvar} $poolname)
3209 }
3210
3211 destroy_pool_int() {
3212     local ost
3213     local OSTS=$(do_facet $SINGLEMDS lctl pool_list $1 | \
3214         awk '$1 !~ /^Pool:/ {print $1}')
3215     for ost in $OSTS; do
3216         do_facet mgs lctl pool_remove $1 $ost
3217     done
3218     do_facet mgs lctl pool_destroy $1
3219 }
3220
3221 # <fsname>.<poolname> or <poolname>
3222 destroy_pool() {
3223     local fsname=${1%%.*}
3224     local poolname=${1##$fsname.}
3225
3226     [[ x$fsname = x$poolname ]] && fsname=$FSNAME
3227
3228     local RC
3229
3230     pool_list $fsname.$poolname || return $?
3231
3232     destroy_pool_int $fsname.$poolname
3233     RC=$?
3234     [[ $RC -ne 0 ]] && return $RC
3235
3236     wait_update $HOSTNAME "lctl get_param -n lov.$fsname-*.pools.$poolname \
3237       2>/dev/null || echo foo" "foo" || RC=1
3238
3239     if [[ $RC -eq 0 ]]; then
3240         remove_pool_from_list $fsname.$poolname
3241     else
3242         error "destroy pool failed $1"
3243     fi
3244     return $RC
3245 }
3246
3247 destroy_pools () {
3248     local fsname=${1:-$FSNAME}
3249     local poolname
3250     local listvar=${fsname}_CREATED_POOLS
3251
3252     pool_list $fsname
3253
3254     [ x${!listvar} = x ] && return 0
3255
3256     echo destroy the created pools: ${!listvar}
3257     for poolname in ${!listvar//,/ }; do
3258         destroy_pool $fsname.$poolname 
3259     done
3260 }
3261
3262 cleanup_pools () {
3263     local fsname=${1:-$FSNAME}
3264     trap 0
3265     destroy_pools $fsname
3266 }
3267
3268 gather_logs () {
3269     local list=$1
3270
3271     local ts=$(date +%s)
3272
3273     # bug 20237, comment 11
3274     # It would also be useful to provide the option
3275     # of writing the file to an NFS directory so it doesn't need to be copied.
3276     local tmp=$TMP
3277     local docp=true
3278     [ -d "$SHARED_DIR_LOGS" ] && tmp=$SHARED_DIR_LOGS && docp=false
3279
3280     # dump lustre logs, dmesg
3281     do_nodes $list "log=$tmp/\\\$(hostname)-debug-$ts.log ;
3282 lctl dk \\\$log >/dev/null;
3283 log=$tmp/\\\$(hostname)-dmesg-$ts.log;
3284 dmesg > \\\$log; "
3285
3286     # FIXME: does it make sense to collect the logs for $ts only, but all
3287     # TESTSUITE logs?
3288     # rsync $TMP/*${TESTSUITE}* to gather the logs dumped by error fn
3289     local logs=$TMP/'*'${TESTSUITE}'*'
3290     if $docp; then
3291         logs=$logs' '$tmp/'*'$ts'*'
3292     fi
3293     for node in ${list//,/ }; do
3294         rsync -az $node:"$logs" $TMP 
3295     done
3296
3297     local archive=$TMP/${TESTSUITE}-$ts.tar.bz2
3298     tar -jcf $archive $tmp/*$ts* $TMP/*${TESTSUITE}*
3299
3300     echo $archive
3301 }
3302
3303 cleanup_logs () {
3304     local list=${1:-$(comma_list $(nodes_list))}
3305
3306     [ -n ${TESTSUITE} ] && do_nodes $list "rm -f $TMP/*${TESTSUITE}*" || true
3307 }
3308
3309 do_ls () {
3310     local mntpt_root=$1
3311     local num_mntpts=$2
3312     local dir=$3
3313     local i
3314     local cmd
3315     local pids
3316     local rc=0
3317
3318     for i in $(seq 0 $num_mntpts); do
3319         cmd="ls -laf ${mntpt_root}$i/$dir"
3320         echo + $cmd;
3321         $cmd > /dev/null &
3322         pids="$pids $!"
3323     done
3324     echo pids=$pids
3325     for pid in $pids; do
3326         wait $pid || rc=$?
3327     done
3328
3329     return $rc
3330 }
3331