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