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