Whamcloud - gitweb
04b79964c73a29c4b0f49a0d2ccda605e31c2c19
[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 MDSRATE=${MDSRATE:-"$LUSTRE/tests/mpi/mdsrate"}
133     [ ! -f "$MDSRATE" ] && export MDSRATE=$(which mdsrate 2> /dev/null)
134     if ! echo $PATH | grep -q $LUSTRE/tests/racer; then
135         export PATH=$PATH:$LUSTRE/tests/racer
136     fi
137     if ! echo $PATH | grep -q $LUSTRE/tests/mpi; then
138         export PATH=$PATH:$LUSTRE/tests/mpi
139     fi
140     export RSYNC_RSH=${RSYNC_RSH:-rsh}
141     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
142     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
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     if $verbose ; then
1438         $myPDSH $rnodes "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")"
1439     else
1440         $myPDSH $rnodes "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed -re "s/\w+:\s//g"
1441     fi
1442     return ${PIPESTATUS[0]}
1443 }
1444
1445 do_facet() {
1446     local facet=$1
1447     shift
1448     local HOST=`facet_active_host $facet`
1449     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
1450     do_node $HOST "$@"
1451 }
1452
1453 do_nodesv() {
1454     do_nodes --verbose "$@"
1455 }
1456
1457 add() {
1458     local facet=$1
1459     shift
1460     # make sure its not already running
1461     stop ${facet} -f
1462     rm -f $TMP/${facet}active
1463     do_facet ${facet} $MKFS $*
1464 }
1465
1466 ostdevname() {
1467     num=$1
1468     DEVNAME=OSTDEV$num
1469     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
1470     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
1471     echo -n $DEVPTR
1472 }
1473
1474 facet_mntpt() {
1475     local facet=$1
1476     if combined_mgs_mds && [[ $facet = "mgs" ]]  ; then
1477         facet="mds"
1478     fi
1479     local var=${facet}_MOUNT
1480     eval mntpt=${!var:-${MOUNT%/*}/$facet}
1481
1482     echo -n $mntpt
1483 }
1484
1485 ########
1486 ## MountConf setup
1487
1488 stopall() {
1489     # make sure we are using the primary server, so test-framework will
1490     # be able to clean up properly.
1491     activemds=`facet_active mds`
1492     if [ $activemds != "mds" ]; then
1493         fail mds
1494     fi
1495
1496     local clients=$CLIENTS
1497     [ -z $clients ] && clients=$(hostname)
1498
1499     zconf_umount_clients $clients $MOUNT "$*" || true
1500     [ -n "$MOUNT2" ] && zconf_umount_clients $clients $MOUNT2 "$*" || true
1501
1502     [ "$CLIENTONLY" ] && return
1503     # The add fn does rm ${facet}active file, this would be enough
1504     # if we use do_facet <facet> only after the facet added, but
1505     # currently we use do_facet mds in local.sh
1506     stop mds -f
1507     rm -f ${TMP}/mdsactive
1508     for num in `seq $OSTCOUNT`; do
1509         stop ost$num -f
1510         rm -f $TMP/ost${num}active
1511     done
1512     if ! combined_mgs_mds ; then
1513         stop mgs 
1514     fi
1515
1516     return 0
1517 }
1518
1519 cleanup_echo_devs () {
1520     local devs=$($LCTL dl | grep echo | awk '{print $4}')
1521
1522     for dev in $devs; do
1523         $LCTL --device $dev cleanup
1524         $LCTL --device $dev detach
1525     done
1526 }
1527
1528 cleanupall() {
1529     nfs_client_mode && return
1530
1531     stopall $*
1532     cleanup_echo_devs
1533
1534     unload_modules
1535 }
1536
1537 combined_mgs_mds () {
1538     [[ $MDSDEV = $MGSDEV ]] && [[ $mds_HOST = $mgs_HOST ]]
1539 }
1540
1541 formatall() {
1542     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
1543
1544     stopall
1545     # We need ldiskfs here, may as well load them all
1546     load_modules
1547     [ "$CLIENTONLY" ] && return
1548     echo Formatting mgs, mds, osts
1549     if ! combined_mgs_mds ; then
1550         add mgs $mgs_MKFS_OPTS $FSTYPE_OPT --reformat $MGSDEV || exit 10
1551     fi
1552
1553     if $VERBOSE; then
1554         add mds $MDS_MKFS_OPTS $FSTYPE_OPT --reformat $MDSDEV || exit 10
1555     else
1556         add mds $MDS_MKFS_OPTS $FSTYPE_OPT --reformat $MDSDEV > /dev/null || exit 10
1557     fi
1558
1559     for num in `seq $OSTCOUNT`; do
1560         if $VERBOSE; then
1561             add ost$num $OST_MKFS_OPTS $FSTYPE_OPT --reformat `ostdevname $num` || exit 10
1562         else
1563             add ost$num $OST_MKFS_OPTS $FSTYPE_OPT --reformat `ostdevname $num` > /dev/null || exit 10
1564         fi
1565     done
1566 }
1567
1568 mount_client() {
1569     grep " $1 " /proc/mounts || zconf_mount $HOSTNAME $*
1570 }
1571
1572 remount_client()
1573 {
1574         zconf_umount `hostname` $1 || error "umount failed"
1575         zconf_mount `hostname` $1 || error "mount failed"
1576 }
1577
1578 writeconf_facet () {
1579     local facet=$1
1580     local dev=$2
1581
1582     do_facet $facet "$TUNEFS --writeconf $dev"
1583 }
1584
1585 writeconf_all () {
1586     writeconf_facet mds $MDSDEV
1587
1588     for num in `seq $OSTCOUNT`; do
1589         DEVNAME=`ostdevname $num`
1590         writeconf_facet ost$num $DEVNAME
1591     done
1592 }
1593
1594 setupall() {
1595     nfs_client_mode && return
1596
1597     sanity_mount_check ||
1598         error "environments are insane!"
1599
1600     load_modules
1601     if [ -z "$CLIENTONLY" ]; then
1602         echo Setup mgs, mdt, osts
1603
1604         echo $WRITECONF | grep -q "writeconf" && \
1605             writeconf_all
1606
1607         if ! combined_mgs_mds ; then
1608             start mgs $MGSDEV $mgs_MOUNT_OPTS
1609         fi
1610
1611         start mds $MDSDEV $MDS_MOUNT_OPTS
1612         # We started mds, now we should set failover variable properly.
1613         # Set mdsfailover_HOST if it is not set (the default failnode).
1614         if [ -z "$mdsfailover_HOST" ]; then
1615            mdsfailover_HOST=$(facet_host mds)
1616         fi
1617
1618         for num in `seq $OSTCOUNT`; do
1619             DEVNAME=`ostdevname $num`
1620             start ost$num $DEVNAME $OST_MOUNT_OPTS
1621
1622             # We started ost$num, now we should set ost${num}failover variable properly.
1623             # Set ost${num}failover_HOST if it is not set (the default failnode).
1624             varname=ost${num}failover_HOST
1625             if [ -z "${!varname}" ]; then
1626                 eval ost${num}failover_HOST=$(facet_host ost${num})
1627             fi
1628
1629         done
1630     fi
1631     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
1632     mount_client $MOUNT
1633     [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT
1634
1635     if [ "$MOUNT_2" ]; then
1636         mount_client $MOUNT2
1637         [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT2
1638     fi
1639     sleep 5
1640     init_param_vars
1641 }
1642
1643 mounted_lustre_filesystems() {
1644         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
1645 }
1646
1647 init_facet_vars () {
1648     [ "$CLIENTONLY" ] && return 0
1649     local facet=$1
1650     shift
1651     local device=$1
1652
1653     shift
1654
1655     eval export ${facet}_dev=${device}
1656     eval export ${facet}_opt=\"$@\"
1657
1658     local dev=${facet}_dev
1659     local label=$(do_facet ${facet} "$E2LABEL ${!dev}")
1660     [ -z "$label" ] && echo no label for ${!dev} && exit 1
1661
1662     eval export ${facet}_svc=${label}
1663
1664     local varname=${facet}failover_HOST
1665     if [ -z "${!varname}" ]; then
1666        eval $varname=$(facet_host $facet)
1667     fi
1668
1669     # ${facet}failover_dev is set in cfg file
1670     varname=${facet}failover_dev
1671     if [ -n "${!varname}" ] ; then
1672         eval export ${facet}failover_dev=${!varname}
1673     else
1674         eval export ${facet}failover_dev=$device
1675     fi
1676
1677     # get mount point of already mounted device
1678     # is facet_dev is already mounted then use the real
1679     #  mount point of this facet; otherwise use $(facet_mntpt $facet)
1680     # i.e. ${facet}_MOUNT if specified by user or default 
1681     local mntpt=$(do_facet ${facet} cat /proc/mounts | \
1682             awk '"'${!dev}'" == $1 && $3 == "lustre" { print $2 }')
1683     if [ -z $mntpt ]; then
1684         mntpt=$(facet_mntpt $facet)
1685     fi
1686     eval export ${facet}_MOUNT=$mntpt
1687 }
1688
1689 init_facets_vars () {
1690     remote_mds_nodsh ||
1691         init_facet_vars mds $MDSDEV $MDS_MOUNT_OPTS
1692
1693     remote_ost_nodsh && return
1694
1695     for num in `seq $OSTCOUNT`; do
1696         DEVNAME=`ostdevname $num`
1697         init_facet_vars ost$num $DEVNAME $OST_MOUNT_OPTS
1698     done
1699 }
1700
1701 init_param_vars () {
1702     if ! remote_ost_nodsh && ! remote_mds_nodsh; then
1703         export MDSVER=$(do_facet mds "lctl get_param version" | cut -d. -f1,2)
1704         export OSTVER=$(do_facet ost1 "lctl get_param version" | cut -d. -f1,2)
1705         export CLIVER=$(lctl get_param version | cut -d. -f 1,2)
1706     fi
1707
1708     remote_mds_nodsh ||
1709         TIMEOUT=$(do_facet mds "lctl get_param -n timeout")
1710
1711     log "Using TIMEOUT=$TIMEOUT"
1712
1713     if [ "$ENABLE_QUOTA" ]; then
1714         setup_quota $MOUNT  || return 2
1715     fi
1716 }
1717
1718 nfs_client_mode () {
1719     if [ "$NFSCLIENT" ]; then
1720         echo "NFSCLIENT mode: setup, cleanup, check config skipped"
1721         local clients=$CLIENTS
1722         [ -z $clients ] && clients=$(hostname)
1723
1724         # FIXME: remove hostname when 19215 fixed
1725         do_nodes $clients "echo \\\$(hostname); grep ' '$MOUNT' ' /proc/mounts"
1726         declare -a nfsexport=(`grep ' '$MOUNT' ' /proc/mounts | awk '{print $1}' | awk -F: '{print $1 " "  $2}'`)
1727         do_nodes ${nfsexport[0]} "echo \\\$(hostname); df -T  ${nfsexport[1]}"
1728         return
1729     fi
1730     return 1
1731 }
1732
1733 check_config_client () {
1734     local mntpt=$1
1735
1736     local mounted=$(mount | grep " $mntpt ")
1737     if [ "$CLIENTONLY" ]; then
1738         # bug 18021
1739         # CLIENTONLY should not depend on *_HOST settings
1740         local mgc=$($LCTL device_list | awk '/MGC/ {print $4}')
1741         # in theory someone could create a new,
1742         # client-only config file that assumed lustre was already
1743         # configured and didn't set the MGSNID. If MGSNID is not set,
1744         # then we should use the mgs nid currently being used 
1745         # as the default value. bug 18021
1746         [[ x$MGSNID = x ]] &&
1747             MGSNID=${mgc//MGC/}
1748
1749         if [[ x$mgc != xMGC$MGSNID ]]; then
1750             if [ "$mgs_HOST" ]; then
1751                 local mgc_ip=$(ping -q -c1 -w1 $mgs_HOST | grep PING | awk '{print $3}' | sed -e "s/(//g" -e "s/)//g")
1752                 [[ x$mgc = xMGC$mgc_ip@$NETTYPE ]] ||
1753                     error_exit "MGSNID=$MGSNID, mounted: $mounted, MGC : $mgc"
1754             fi
1755         fi
1756         return 0
1757     fi
1758
1759     local myMGS_host=$mgs_HOST
1760     if [ "$NETTYPE" = "ptl" ]; then
1761         myMGS_host=$(h2ptl $mgs_HOST | sed -e s/@ptl//)
1762     fi
1763
1764     echo Checking config lustre mounted on $mntpt
1765     local mgshost=$(mount | grep " $mntpt " | awk -F@ '{print $1}')
1766     mgshost=$(echo $mgshost | awk -F: '{print $1}')
1767
1768 #    if [ "$mgshost" != "$myMGS_host" ]; then
1769 #            error_exit "Bad config file: lustre is mounted with mgs $mgshost, but mgs_HOST=$mgs_HOST, NETTYPE=$NETTYPE
1770 #                   Please use correct config or set mds_HOST correctly!"
1771 #    fi
1772
1773 }
1774
1775 check_config_clients () {
1776     local clients=${CLIENTS:-$HOSTNAME}
1777     local mntpt=$1
1778
1779     nfs_client_mode && return
1780
1781     do_rpc_nodes $clients check_config_client $mntpt
1782
1783     sanity_mount_check ||
1784         error "environments are insane!"
1785 }
1786
1787 check_timeout () {
1788     local mdstimeout=$(do_facet mds "lctl get_param -n timeout")
1789     local cltimeout=$(lctl get_param -n timeout)
1790     if [ $mdstimeout -ne $TIMEOUT ] || [ $mdstimeout -ne $cltimeout ]; then
1791         error "timeouts are wrong! mds: $mdstimeout, client: $cltimeout, TIMEOUT=$TIMEOUT"
1792         return 1
1793     fi
1794 }
1795
1796 is_mounted () {
1797     local mntpt=$1
1798     local mounted=$(mounted_lustre_filesystems)
1799
1800     echo $mounted' ' | grep -w -q $mntpt' '
1801 }
1802
1803 is_empty_dir() {
1804     [ $(find $1 -maxdepth 1 -print | wc -l) = 1 ] && return 0
1805     return 1
1806 }
1807
1808 # empty lustre filesystem may have empty directories lost+found and .lustre
1809 is_empty_fs() {
1810     [ $(find $1 -maxdepth 1 -name lost+found -o -name .lustre -prune -o \
1811        -print | wc -l) = 1 ] || return 1
1812     [ ! -d $1/lost+found ] || is_empty_dir $1/lost+found && return 0
1813     [ ! -d $1/.lustre ] || is_empty_dir $1/.lustre && return 0
1814     return 1
1815 }
1816
1817 check_and_setup_lustre() {
1818     nfs_client_mode && return
1819
1820     local MOUNTED=$(mounted_lustre_filesystems)
1821
1822     local do_check=true
1823     # 1.
1824     # both MOUNT and MOUNT2 are not mounted
1825     if ! is_mounted $MOUNT && ! is_mounted $MOUNT2; then
1826         [ "$REFORMAT" ] && formatall
1827         # setupall mounts both MOUNT and MOUNT2 (if MOUNT_2 is set)
1828         setupall
1829         is_mounted $MOUNT || error "NAME=$NAME not mounted"
1830         export I_MOUNTED=yes
1831         do_check=false
1832     # 2.
1833     # MOUNT2 is mounted
1834     elif is_mounted $MOUNT2; then
1835             # 3.
1836             # MOUNT2 is mounted, while MOUNT_2 is not set
1837             if ! [ "$MOUNT_2" ]; then
1838                 cleanup_mount $MOUNT2
1839                 export I_UMOUNTED2=yes
1840
1841             # 4.
1842             # MOUNT2 is mounted, MOUNT_2 is set
1843             else
1844                 # FIXME: what to do if check_config failed?
1845                 # i.e. if:
1846                 # 1) remote client has mounted other Lustre fs ?
1847                 # 2) it has insane env ?
1848                 # let's try umount MOUNT2 on all clients and mount it again:
1849                 if ! check_config_clients $MOUNT2; then
1850                     cleanup_mount $MOUNT2
1851                     restore_mount $MOUNT2
1852                     export I_MOUNTED2=yes
1853                 fi
1854             fi 
1855
1856     # 5.
1857     # MOUNT is mounted MOUNT2 is not mounted
1858     elif [ "$MOUNT_2" ]; then
1859         restore_mount $MOUNT2
1860         export I_MOUNTED2=yes
1861     fi
1862
1863     if $do_check; then
1864         # FIXME: what to do if check_config failed?
1865         # i.e. if:
1866         # 1) remote client has mounted other Lustre fs?
1867         # 2) lustre is mounted on remote_clients atall ?
1868         check_config_clients $MOUNT
1869         init_facets_vars
1870         init_param_vars
1871
1872         do_nodes $(comma_list $(nodes_list)) "lctl set_param debug=\\\"$PTLDEBUG\\\";
1873             lctl set_param subsystem_debug=\\\"${SUBSYSTEM# }\\\";
1874             lctl set_param debug_mb=${DEBUG_SIZE};
1875             sync"
1876     fi
1877     if [ "$ONLY" == "setup" ]; then
1878         exit 0
1879     fi
1880 }
1881
1882 restore_mount () {
1883    local clients=${CLIENTS:-$HOSTNAME}
1884    local mntpt=$1
1885
1886    zconf_mount_clients $clients $mntpt
1887 }
1888
1889 cleanup_mount () {
1890     local clients=${CLIENTS:-$HOSTNAME}
1891     local mntpt=$1
1892
1893     zconf_umount_clients $clients $mntpt    
1894 }
1895
1896 cleanup_and_setup_lustre() {
1897     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
1898         lctl set_param debug=0 || true
1899         cleanupall
1900         if [ "$ONLY" == "cleanup" ]; then
1901             exit 0
1902         fi
1903     fi
1904     check_and_setup_lustre
1905 }
1906
1907 # Get all of the server target devices from a given server node and type.
1908 get_mnt_devs() {
1909     local node=$1
1910     local type=$2
1911     local obd_type
1912     local devs
1913     local dev
1914
1915     case $type in
1916     mdt) obd_type="osd" ;;
1917     ost) obd_type="obdfilter" ;; # needs to be fixed when OST also uses an OSD
1918     *) echo "invalid server type" && return 1 ;;
1919     esac
1920
1921     devs=$(do_node $node "lctl get_param -n $obd_type.*.mntdev")
1922     for dev in $devs; do
1923         case $dev in
1924         *loop*) do_node $node "losetup $dev" | \
1925                 sed -e "s/.*(//" -e "s/).*//" ;;
1926         *) echo $dev ;;
1927         esac
1928     done
1929 }
1930
1931 # Get all of the server target devices.
1932 get_svr_devs() {
1933     local i
1934
1935     # OST devices
1936     i=0
1937     for node in $(osts_nodes); do
1938         OSTDEVS[i]=$(get_mnt_devs $node ost)
1939         i=$((i + 1))
1940     done
1941 }
1942
1943 # Run e2fsck on MDT or OST device.
1944 run_e2fsck() {
1945     local node=$1
1946     local target_dev=$2
1947     local ostidx=$3
1948     local ostdb_opt=$4
1949
1950     df > /dev/null      # update statfs data on disk
1951     local cmd="$E2FSCK -d -v -f -n $MDSDB_OPT $ostdb_opt $target_dev"
1952     echo $cmd
1953     do_node $node $cmd
1954     local rc=${PIPESTATUS[0]}
1955     [ $rc -le $FSCK_MAX_ERR ] || \
1956         error "$cmd returned $rc, should be <= $FSCK_MAX_ERR"
1957     return 0
1958 }
1959
1960 # Run e2fsck on MDT and OST(s) to generate databases used for lfsck.
1961 generate_db() {
1962     local i
1963     local ostidx
1964     local dev
1965     local tmp_file
1966
1967     tmp_file=$(mktemp -p $SHARED_DIRECTORY || 
1968         error "fail to create file in $SHARED_DIRECTORY")
1969
1970     # make sure everything gets to the backing store
1971     local list=$(comma_list $CLIENTS $(facet_host mds) $(osts_nodes))
1972     do_nodes $list "sync; sleep 2; sync"
1973
1974     do_nodes $list ls $tmp_file || \
1975         error "$SHARED_DIRECTORY is not a shared directory"
1976     rm $tmp_file
1977
1978     run_e2fsck $(facet_host mds) $MDSDEV
1979
1980     i=0
1981     ostidx=0
1982     OSTDB_LIST=""
1983     for node in $(osts_nodes); do
1984         for dev in ${OSTDEVS[i]}; do
1985             local ostdb_opt=`eval echo $OSTDB_OPT`
1986             run_e2fsck $node $dev $ostidx "$ostdb_opt"
1987             OSTDB_LIST="$OSTDB_LIST $OSTDB-$ostidx"
1988             ostidx=$((ostidx + 1))
1989         done
1990         i=$((i + 1))
1991     done
1992 }
1993
1994 run_lfsck() {
1995     local cmd="$LFSCK1 -c -l --mdsdb $MDSDB --ostdb $OSTDB_LIST $MOUNT"
1996     echo $cmd
1997     eval $cmd
1998     local rc=${PIPESTATUS[0]}
1999     [ $rc -le $FSCK_MAX_ERR ] || \
2000         error "$cmd returned $rc, should be <= $FSCK_MAX_ERR"
2001     echo "lfsck finished with rc=$rc"
2002
2003     rm -rvf $MDSDB* $OSTDB* || true
2004
2005     return $rc
2006 }
2007
2008 check_and_cleanup_lustre() {
2009     if [ "$LFSCK_ALWAYS" = "yes" ]; then
2010         get_svr_devs
2011         generate_db
2012         if [ "$SKIP_LFSCK" == "no" ]; then
2013             local rc=0
2014             run_lfsck || rc=$?
2015         else
2016             echo "skip lfsck"
2017         fi
2018     fi
2019
2020     if is_mounted $MOUNT; then
2021         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]*
2022         [ "$ENABLE_QUOTA" ] && restore_quota_type || true
2023     fi
2024
2025     if [ "$I_UMOUNTED2" = "yes" ]; then
2026         restore_mount $MOUNT2 || error "restore $MOUNT2 failed"
2027     fi
2028
2029     if [ "$I_MOUNTED2" = "yes" ]; then
2030         cleanup_mount $MOUNT2
2031     fi
2032
2033     if [ "$I_MOUNTED" = "yes" ]; then
2034         cleanupall -f || error "cleanup failed"
2035         unset I_MOUNTED
2036     fi
2037 }
2038
2039 #######
2040 # General functions
2041
2042 check_network() {
2043     local NETWORK=0
2044     local WAIT=0
2045     local MAX=$2
2046     while [ $NETWORK -eq 0 ]; do
2047         if ping -c 1 -w 3 $1 > /dev/null; then
2048             NETWORK=1
2049         else
2050             WAIT=$((WAIT + 5))
2051             echo "waiting for $1, $((MAX - WAIT)) secs left"
2052             sleep 5
2053         fi
2054         if [ $WAIT -gt $MAX ]; then
2055             echo "Network not available"
2056             exit 1
2057         fi
2058     done
2059 }
2060 check_port() {
2061     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
2062         sleep 9
2063     done
2064 }
2065
2066 no_dsh() {
2067     shift
2068     eval $@
2069 }
2070
2071 comma_list() {
2072     # the sed converts spaces to commas, but leaves the last space
2073     # alone, so the line doesn't end with a comma.
2074     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
2075 }
2076
2077 # list, excluded are the comma separated lists
2078 exclude_items_from_list () {
2079     local list=$1
2080     local excluded=$2
2081     local item
2082
2083     list=${list//,/ }
2084     for item in ${excluded//,/ }; do
2085         list=$(echo " $list " | sed -re "s/\s+$item\s+/ /g")
2086     done
2087     echo $(comma_list $list)
2088 }
2089
2090 # list, expand  are the comma separated lists
2091 expand_list () {
2092     local list=${1//,/ }
2093     local expand=${2//,/ }
2094     local expanded=
2095
2096     expanded=$(for i in $list $expand; do echo $i; done | sort -u)
2097     echo $(comma_list $expanded)
2098 }
2099
2100 testslist_filter () {
2101     local script=$LUSTRE/tests/${TESTSUITE}.sh
2102
2103     [ -f $script ] || return 0
2104
2105     local start_at=$START_AT
2106     local stop_at=$STOP_AT
2107
2108     local var=${TESTSUITE//-/_}_START_AT
2109     [ x"${!var}" != x ] && start_at=${!var}
2110     var=${TESTSUITE//-/_}_STOP_AT
2111     [ x"${!var}" != x ] && stop_at=${!var}
2112
2113     sed -n 's/^test_\([^ (]*\).*/\1/p' $script | \
2114         awk ' BEGIN { if ("'${start_at:-0}'" != 0) flag = 1 }
2115             /^'${start_at}'$/ {flag = 0}
2116             {if (flag == 1) print $0}
2117             /^'${stop_at}'$/ { flag = 1 }'
2118 }
2119
2120 absolute_path() {
2121     (cd `dirname $1`; echo $PWD/`basename $1`)
2122 }
2123
2124 get_facets () {
2125     local name=$(echo $1 | tr "[:upper:]" "[:lower:]")
2126     local type=$(echo $1 | tr "[:lower:]" "[:upper:]")
2127
2128     local list=""
2129
2130     case $type in
2131         MDS )    list=mds;;
2132         OST )    for ((i=1; i<=$OSTCOUNT; i++)) do
2133                     list="$list ${name}$i"
2134                  done;;
2135           * )    error "Invalid facet type"
2136                  exit 1;;
2137     esac
2138     echo $(comma_list $list)
2139 }
2140
2141 ##################################
2142 # Adaptive Timeouts funcs
2143
2144 at_is_enabled() {
2145     # only check mds, we assume at_max is the same on all nodes
2146     local at_max=$(do_facet mds "lctl get_param -n at_max")
2147     if [ $at_max -eq 0 ]; then
2148         return 1
2149     else
2150         return 0
2151     fi
2152 }
2153
2154 at_max_get() {
2155     local facet=$1
2156
2157     # suppose that all ost-s has the same at_max set
2158     if [ $facet == "ost" ]; then
2159         do_facet ost1 "lctl get_param -n at_max"
2160     else
2161         do_facet $facet "lctl get_param -n at_max"
2162     fi
2163 }
2164
2165 at_max_set() {
2166     local at_max=$1
2167     shift
2168
2169     local facet
2170     for facet in $@; do
2171         if [ $facet == "ost" ]; then
2172             for i in `seq $OSTCOUNT`; do
2173                 do_facet ost$i "lctl set_param at_max=$at_max"
2174             done
2175         else
2176             do_facet $facet "lctl set_param at_max=$at_max"
2177         fi
2178     done
2179 }
2180
2181 ##################################
2182 # OBD_FAIL funcs
2183
2184 drop_request() {
2185 # OBD_FAIL_MDS_ALL_REQUEST_NET
2186     RC=0
2187     do_facet mds lctl set_param fail_loc=0x123
2188     do_facet client "$1" || RC=$?
2189     do_facet mds lctl set_param fail_loc=0
2190     return $RC
2191 }
2192
2193 drop_reply() {
2194 # OBD_FAIL_MDS_ALL_REPLY_NET
2195     RC=0
2196     do_facet mds lctl set_param fail_loc=0x122
2197     do_facet client "$@" || RC=$?
2198     do_facet mds lctl set_param fail_loc=0
2199     return $RC
2200 }
2201
2202 drop_reint_reply() {
2203 # OBD_FAIL_MDS_REINT_NET_REP
2204     RC=0
2205     do_facet mds lctl set_param fail_loc=0x119
2206     do_facet client "$@" || RC=$?
2207     do_facet mds lctl set_param fail_loc=0
2208     return $RC
2209 }
2210
2211 pause_bulk() {
2212 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
2213     RC=0
2214     do_facet ost1 lctl set_param fail_loc=0x214
2215     do_facet client "$1" || RC=$?
2216     do_facet client "sync"
2217     do_facet ost1 lctl set_param fail_loc=0
2218     return $RC
2219 }
2220
2221 drop_ldlm_cancel() {
2222 #define OBD_FAIL_LDLM_CANCEL             0x304
2223     RC=0
2224     do_facet client lctl set_param fail_loc=0x304
2225     do_facet client "$@" || RC=$?
2226     do_facet client lctl set_param fail_loc=0
2227     return $RC
2228 }
2229
2230 drop_bl_callback() {
2231 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
2232     RC=0
2233     do_facet client lctl set_param fail_loc=0x305
2234     do_facet client "$@" || RC=$?
2235     do_facet client lctl set_param fail_loc=0
2236     return $RC
2237 }
2238
2239 drop_ldlm_reply() {
2240 #define OBD_FAIL_LDLM_REPLY              0x30c
2241     RC=0
2242     do_facet mds lctl set_param fail_loc=0x30c
2243     do_facet client "$@" || RC=$?
2244     do_facet mds lctl set_param fail_loc=0
2245     return $RC
2246 }
2247
2248 clear_failloc() {
2249     facet=$1
2250     pause=$2
2251     sleep $pause
2252     echo "clearing fail_loc on $facet"
2253     do_facet $facet "lctl set_param fail_loc=0 2>/dev/null || true"
2254 }
2255
2256 set_nodes_failloc () {
2257     do_nodes $(comma_list $1)  lctl set_param fail_loc=$2
2258 }
2259
2260 cancel_lru_locks() {
2261     $LCTL mark "cancel_lru_locks $1 start"
2262     for d in `lctl get_param -N ldlm.namespaces.*.lru_size | egrep -i $1`; do
2263         $LCTL set_param -n $d=clear
2264     done
2265     $LCTL get_param ldlm.namespaces.*.lock_unused_count | egrep -i $1 | grep -v '=0'
2266     $LCTL mark "cancel_lru_locks $1 stop"
2267 }
2268
2269 default_lru_size()
2270 {
2271         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
2272         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
2273         echo "$DEFAULT_LRU_SIZE"
2274 }
2275
2276 lru_resize_enable()
2277 {
2278     lctl set_param ldlm.namespaces.*$1*.lru_size=0
2279 }
2280
2281 lru_resize_disable()
2282 {
2283     lctl set_param ldlm.namespaces.*$1*.lru_size $(default_lru_size)
2284 }
2285
2286 pgcache_empty() {
2287     local FILE
2288     for FILE in `lctl get_param -N "llite.*.dump_page_cache"`; do
2289         if [ `lctl get_param -n $FILE | wc -l` -gt 1 ]; then
2290             echo there is still data in page cache $FILE ?
2291             lctl get_param -n $FILE
2292             return 1
2293         fi
2294     done
2295     return 0
2296 }
2297
2298 create_fake_exports () {
2299     local facet=$1
2300     local num=$2
2301 #obd_fail_val = num;
2302 #define OBD_FAIL_TGT_FAKE_EXP 0x708
2303     do_facet $facet "lctl set_param fail_val=$num"
2304     do_facet $facet "lctl set_param fail_loc=0x80000708"
2305     fail $facet
2306 }
2307
2308 debugsave() {
2309     DEBUGSAVE="$(lctl get_param -n debug)"
2310 }
2311
2312 debugrestore() {
2313     [ -n "$DEBUGSAVE" ] && lctl set_param debug="${DEBUGSAVE}"
2314     DEBUGSAVE=""
2315 }
2316
2317 ##################################
2318 # Test interface
2319 ##################################
2320
2321 error_noexit() {
2322     local TYPE=${TYPE:-"FAIL"}
2323     local ERRLOG
2324
2325     local dump=true
2326     # do not dump logs if $1=false
2327     if [ "x$1" = "xfalse" ]; then
2328         shift
2329         dump=false
2330     fi
2331
2332     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
2333
2334     if $dump; then
2335         ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
2336         echo "Dumping lctl log to $ERRLOG"
2337         # We need to dump the logs on all nodes
2338         do_nodes $(comma_list $(nodes_list)) $NODE $LCTL dk $ERRLOG
2339     fi
2340     debugrestore
2341     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
2342     TEST_FAILED=true
2343 }
2344
2345 error() {
2346     error_noexit "$@"
2347     if $FAIL_ON_ERROR;  then
2348         reset_fail_loc
2349         exit 1
2350     fi
2351 }
2352
2353 error_exit() {
2354     error_noexit "$@"
2355     exit 1
2356 }
2357
2358 # use only if we are ignoring failures for this test, bugno required.
2359 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
2360 # e.g. error_ignore 5494 "your message"
2361 error_ignore() {
2362     local TYPE="IGNORE (bz$1)"
2363     shift
2364     error_noexit "$@"
2365 }
2366
2367 skip_env () {
2368     $FAIL_ON_SKIP_ENV && error false $@ || skip $@
2369 }
2370
2371 skip () {
2372         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
2373         [ "$TESTSUITELOG" ] && \
2374                 echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG || true
2375 }
2376
2377 build_test_filter() {
2378     EXCEPT="$EXCEPT $(testslist_filter)"
2379
2380     [ "$ONLY" ] && log "only running test `echo $ONLY`"
2381     for O in $ONLY; do
2382         eval ONLY_${O}=true
2383     done
2384     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
2385         log "excepting tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
2386     [ "$EXCEPT_SLOW" ] && \
2387         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
2388     for E in $EXCEPT $ALWAYS_EXCEPT; do
2389         eval EXCEPT_${E}=true
2390     done
2391     for E in $EXCEPT_SLOW; do
2392         eval EXCEPT_SLOW_${E}=true
2393     done
2394     for G in $GRANT_CHECK_LIST; do
2395         eval GCHECK_ONLY_${G}=true
2396         done
2397 }
2398
2399 basetest() {
2400     if [[ $1 = [a-z]* ]]; then
2401         echo $1
2402     else
2403         echo ${1%%[a-z]*}
2404     fi
2405 }
2406
2407 # print a newline if the last test was skipped
2408 export LAST_SKIPPED=
2409 run_test() {
2410     assert_DIR
2411
2412     export base=`basetest $1`
2413     if [ ! -z "$ONLY" ]; then
2414         testname=ONLY_$1
2415         if [ ${!testname}x != x ]; then
2416             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
2417             run_one $1 "$2"
2418             return $?
2419         fi
2420         testname=ONLY_$base
2421         if [ ${!testname}x != x ]; then
2422             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
2423             run_one $1 "$2"
2424             return $?
2425         fi
2426         LAST_SKIPPED="y"
2427         echo -n "."
2428         return 0
2429     fi
2430     testname=EXCEPT_$1
2431     if [ ${!testname}x != x ]; then
2432         LAST_SKIPPED="y"
2433         TESTNAME=test_$1 skip "skipping excluded test $1"
2434         return 0
2435     fi
2436     testname=EXCEPT_$base
2437     if [ ${!testname}x != x ]; then
2438         LAST_SKIPPED="y"
2439         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
2440         return 0
2441     fi
2442     testname=EXCEPT_SLOW_$1
2443     if [ ${!testname}x != x ]; then
2444         LAST_SKIPPED="y"
2445         TESTNAME=test_$1 skip "skipping SLOW test $1"
2446         return 0
2447     fi
2448     testname=EXCEPT_SLOW_$base
2449     if [ ${!testname}x != x ]; then
2450         LAST_SKIPPED="y"
2451         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
2452         return 0
2453     fi
2454
2455     LAST_SKIPPED=
2456     run_one $1 "$2"
2457
2458     return $?
2459 }
2460
2461 EQUALS="======================================================================"
2462 equals_msg() {
2463     msg="$@"
2464
2465     local suffixlen=$((${#EQUALS} - ${#msg}))
2466     [ $suffixlen -lt 5 ] && suffixlen=5
2467     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
2468 }
2469
2470 log() {
2471     echo "$*"
2472     module_loaded lnet || load_modules
2473
2474     local MSG="$*"
2475     # Get rid of '
2476     MSG=${MSG//\'/\\\'}
2477     MSG=${MSG//\(/\\\(}
2478     MSG=${MSG//\)/\\\)}
2479     MSG=${MSG//\;/\\\;}
2480     MSG=${MSG//\|/\\\|}
2481     MSG=${MSG//\>/\\\>}
2482     MSG=${MSG//\</\\\<}
2483     MSG=${MSG//\//\\\/}
2484     do_nodes $(comma_list $(nodes_list)) $LCTL mark "$MSG" 2> /dev/null || true
2485 }
2486
2487 trace() {
2488         log "STARTING: $*"
2489         strace -o $TMP/$1.strace -ttt $*
2490         RC=$?
2491         log "FINISHED: $*: rc $RC"
2492         return 1
2493 }
2494
2495 pass() {
2496     $TEST_FAILED && echo -n "FAIL " || echo -n "PASS "
2497     echo $@
2498 }
2499
2500 check_mds() {
2501     FFREE=`lctl get_param -n mds.*.filesfree`
2502     FTOTAL=`lctl get_param -n mds.*.filestotal`
2503     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
2504 }
2505
2506 reset_fail_loc () {
2507     echo -n "Resetting fail_loc on all nodes..."
2508     do_nodes $(comma_list $(nodes_list)) "lctl set_param -n fail_loc=0 2>/dev/null || true"
2509     echo done.
2510 }
2511
2512 run_one() {
2513     testnum=$1
2514     message=$2
2515     tfile=f${testnum}
2516     export tdir=d0.${TESTSUITE}/d${base}
2517
2518     local SAVE_UMASK=`umask`
2519     umask 0022
2520
2521     local BEFORE=`date +%s`
2522     echo
2523     log "== test $testnum: $message == `date +%H:%M:%S` ($BEFORE)"
2524     #check_mds
2525     export TESTNAME=test_$testnum
2526     TEST_FAILED=false
2527     test_${testnum} || error "test_$testnum failed with $?"
2528     #check_mds
2529     cd $SAVE_PWD
2530     reset_fail_loc
2531     check_grant ${testnum} || error "check_grant $testnum failed with $?"
2532     check_catastrophe || error "LBUG/LASSERT detected"
2533     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
2534     pass "($((`date +%s` - $BEFORE))s)"
2535     TEST_FAILED=false
2536     unset TESTNAME
2537     unset tdir
2538     umask $SAVE_UMASK
2539 }
2540
2541 canonical_path() {
2542     (cd `dirname $1`; echo $PWD/`basename $1`)
2543 }
2544
2545 sync_clients() {
2546     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync
2547     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync
2548         cd $SAVE_PWD
2549 }
2550
2551 check_grant() {
2552     export base=`basetest $1`
2553     [ "$CHECK_GRANT" == "no" ] && return 0
2554
2555         testname=GCHECK_ONLY_${base}
2556     [ ${!testname}x == x ] && return 0
2557
2558     echo -n "checking grant......"
2559         cd $SAVE_PWD
2560         # write some data to sync client lost_grant
2561         rm -f $DIR1/${tfile}_check_grant_* 2>&1
2562         for i in `seq $OSTCOUNT`; do
2563                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
2564                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
2565                                               count=1 > /dev/null 2>&1
2566         done
2567     # sync all the data and make sure no pending data on server
2568     sync_clients
2569
2570     #get client grant and server grant
2571     client_grant=0
2572     for d in `lctl get_param -n osc.*.cur_grant_bytes`; do
2573         client_grant=$((client_grant + $d))
2574     done
2575     server_grant=0
2576     for d in `lctl get_param -n obdfilter.*.tot_granted`; do
2577         server_grant=$((server_grant + $d))
2578     done
2579
2580         # cleanup the check_grant file
2581         for i in `seq $OSTCOUNT`; do
2582                 rm $DIR1/${tfile}_check_grant_$i
2583         done
2584
2585         #check whether client grant == server grant
2586         if [ $client_grant != $server_grant ]; then
2587                 echo "failed: client:${client_grant} server: ${server_grant}"
2588                 return 1
2589         else
2590                 echo "pass"
2591         fi
2592 }
2593
2594 ########################
2595 # helper functions
2596
2597 osc_to_ost()
2598 {
2599     osc=$1
2600     ost=`echo $1 | awk -F_ '{print $3}'`
2601     if [ -z $ost ]; then
2602         ost=`echo $1 | sed 's/-osc.*//'`
2603     fi
2604     echo $ost
2605 }
2606
2607 remote_node () {
2608     local node=$1
2609     [ "$node" != "$(hostname)" ]
2610 }
2611
2612 remote_mds ()
2613 {
2614     remote_node $mds_HOST
2615 }
2616
2617 remote_mds_nodsh()
2618 {
2619     [ "$CLIENTONLY" ] && return 0 || true
2620     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
2621 }
2622
2623 remote_ost ()
2624 {
2625     local node
2626     for node in $(osts_nodes) ; do
2627         remote_node $node && return 0
2628     done
2629     return 1
2630 }
2631
2632 remote_ost_nodsh()
2633 {
2634     [ "$CLIENTONLY" ] && return 0 || true 
2635     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
2636 }
2637
2638 remote_mgs_nodsh()
2639 {
2640     local MGS
2641     MGS=$(facet_host mgs)
2642     remote_node $MGS && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
2643 }
2644
2645 remote_servers () {
2646     remote_ost && remote_mds
2647 }
2648
2649 local_mode ()
2650 {
2651     remote_mds_nodsh || remote_ost_nodsh || \
2652         $(single_local_node $(comma_list $(nodes_list)))
2653 }
2654
2655 osts_nodes () {
2656     local OSTNODES=$(facet_host ost1)
2657     local NODES_sort
2658
2659     for num in `seq $OSTCOUNT`; do
2660         local myOST=$(facet_host ost$num)
2661         OSTNODES="$OSTNODES $myOST"
2662     done
2663     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
2664
2665     echo $NODES_sort
2666 }
2667
2668 nodes_list () {
2669     # FIXME. We need a list of clients
2670     local myNODES=$HOSTNAME
2671     local myNODES_sort
2672
2673     # CLIENTS (if specified) contains the local client
2674     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
2675
2676     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
2677         myNODES="$myNODES $(osts_nodes) $mds_HOST"
2678     fi
2679
2680     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
2681
2682     echo $myNODES_sort
2683 }
2684
2685 remote_nodes_list () {
2686     local rnodes=$(nodes_list)
2687     rnodes=$(echo " $rnodes " | sed -re "s/\s+$HOSTNAME\s+/ /g")
2688     echo $rnodes
2689 }
2690
2691 init_clients_lists () {
2692     # Sanity check: exclude the local client from RCLIENTS
2693     local rclients=$(echo " $RCLIENTS " | sed -re "s/\s+$HOSTNAME\s+/ /g")
2694
2695     # Sanity check: exclude the dup entries
2696     rclients=$(for i in $rclients; do echo $i; done | sort -u)
2697
2698     local clients="$SINGLECLIENT $HOSTNAME $rclients"
2699
2700     # Sanity check: exclude the dup entries from CLIENTS
2701     # for those configs which has SINGLCLIENT set to local client
2702     clients=$(for i in $clients; do echo $i; done | sort -u)
2703
2704     CLIENTS=`comma_list $clients`
2705     local -a remoteclients=($rclients)
2706     for ((i=0; $i<${#remoteclients[@]}; i++)); do
2707             varname=CLIENT$((i + 2))
2708             eval $varname=${remoteclients[i]}
2709     done
2710
2711     CLIENTCOUNT=$((${#remoteclients[@]} + 1))
2712 }
2713
2714 get_random_entry () {
2715     local rnodes=$1
2716
2717     rnodes=${rnodes//,/ }
2718
2719     local -a nodes=($rnodes)
2720     local num=${#nodes[@]}
2721     local i=$((RANDOM * num * 2 / 65536))
2722
2723     echo ${nodes[i]}
2724 }
2725
2726 client_only () {
2727     [ "$CLIENTONLY" ] || [ "$CLIENTMODSONLY" = yes ]
2728 }
2729
2730 is_patchless ()
2731 {
2732     lctl get_param version | grep -q patchless
2733 }
2734
2735 check_versions () {
2736     [ "$MDSVER" = "$CLIVER" -a "$OSTVER" = "$CLIVER" ]
2737 }
2738
2739 get_node_count() {
2740     local nodes="$@"
2741     echo $nodes | wc -w || true
2742 }
2743
2744 mixed_ost_devs () {
2745     local nodes=$(osts_nodes)
2746     local osscount=$(get_node_count "$nodes")
2747     [ ! "$OSTCOUNT" = "$osscount" ]
2748 }
2749
2750 generate_machine_file() {
2751     local nodes=${1//,/ }
2752     local machinefile=$2
2753     rm -f $machinefile
2754     for node in $nodes; do
2755         echo $node >>$machinefile || \
2756             { echo "can not generate machinefile $machinefile" && return 1; }
2757     done
2758 }
2759
2760 get_stripe () {
2761     local file=$1/stripe
2762     touch $file
2763     $LFS getstripe -v $file || error
2764     rm -f $file
2765 }
2766
2767 setstripe_nfsserver () {
2768     local dir=$1
2769
2770     local nfsserver=$(awk '"'$dir'" ~ $2 && $3 ~ "nfs" && $2 != "/" \
2771                 { print $1 }' /proc/mounts | cut -f 1 -d : | head -1)
2772
2773     [ -z $nfsserver ] && echo "$dir is not nfs mounted" && return 1
2774
2775     do_nodev $nfsserver lfs setstripe "$@"
2776 }
2777
2778 check_runas_id_ret() {
2779     local myRC=0
2780     local myRUNAS_UID=$1
2781     local myRUNAS_GID=$2
2782     shift 2
2783     local myRUNAS=$@
2784     if [ -z "$myRUNAS" ]; then
2785         error_exit "myRUNAS command must be specified for check_runas_id"
2786     fi
2787     mkdir $DIR/d0_runas_test
2788     chmod 0755 $DIR
2789     chown $myRUNAS_UID:$myRUNAS_GID $DIR/d0_runas_test
2790     if ! $myRUNAS touch $DIR/d0_runas_test/f$$ ; then
2791         do_nodesv $(comma_list $(nodes_list)) grep -w $myRUNAS_UID /etc/passwd
2792         myRC=1
2793     fi
2794     rm -rf $DIR/d0_runas_test
2795     return $myRC
2796 }
2797
2798 check_runas_id() {
2799     local myRUNAS_UID=$1
2800     local myRUNAS_GID=$2
2801     shift 2
2802     local myRUNAS=$@
2803     check_runas_id_ret $myRUNAS_UID $myRUNAS_GID $myRUNAS || \
2804         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_UID.
2805         Please set RUNAS_ID to some UID which exists on MDS and client or
2806         add user $myRUNAS_UID:$myRUNAS_GID on these nodes."
2807 }
2808
2809 # Run multiop in the background, but wait for it to print
2810 # "PAUSING" to its stdout before returning from this function.
2811 multiop_bg_pause() {
2812     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
2813     FILE=$1
2814     ARGS=$2
2815
2816     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
2817     mkfifo $TMPPIPE
2818
2819     echo "$MULTIOP_PROG $FILE v$ARGS"
2820     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
2821
2822     echo "TMPPIPE=${TMPPIPE}"
2823     local multiop_output
2824
2825     read -t 60 multiop_output < $TMPPIPE
2826     if [ $? -ne 0 ]; then
2827         rm -f $TMPPIPE
2828         return 1
2829     fi
2830     rm -f $TMPPIPE
2831     if [ "$multiop_output" != "PAUSING" ]; then
2832         echo "Incorrect multiop output: $multiop_output"
2833         kill -9 $PID
2834         return 1
2835     fi
2836
2837     return 0
2838 }
2839
2840 do_and_time () {
2841     local cmd=$1
2842     local rc
2843
2844     SECONDS=0
2845     eval '$cmd'
2846     
2847     [ ${PIPESTATUS[0]} -eq 0 ] || rc=1
2848
2849     echo $SECONDS
2850     return $rc
2851 }
2852
2853 inodes_available () {
2854     local IFree=$($LFS df -i $MOUNT | grep ^$FSNAME | awk '{print $4}' | sort -un | head -1) || return 1
2855     echo $IFree
2856 }
2857
2858 mdsrate_inodes_available () {
2859     echo $(($(inodes_available) - 1))
2860 }
2861
2862 # reset llite stat counters
2863 clear_llite_stats(){
2864         lctl set_param -n llite.*.stats 0
2865 }
2866
2867 # sum llite stat items
2868 calc_llite_stats() {
2869         local res=$(lctl get_param -n llite.*.stats |
2870                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
2871         echo $res
2872 }
2873
2874 calc_sum () {
2875         awk 'BEGIN {s = 0}; {s += $1}; END {print s}'
2876 }
2877
2878 calc_osc_kbytes () {
2879         df $MOUNT > /dev/null
2880         $LCTL get_param -n osc.*[oO][sS][cC][-_]*.$1 | calc_sum
2881 }
2882
2883 # save_lustre_params(node, parameter_mask)
2884 # generate a stream of formatted strings (<node> <param name>=<param value>)
2885 save_lustre_params() {
2886         local s
2887         do_nodesv $1 "lctl get_param $2 | while read s; do echo \\\$s; done"
2888 }
2889
2890 # restore lustre parameters from input stream, produces by save_lustre_params
2891 restore_lustre_params() {
2892         local node
2893         local name
2894         local val
2895         while IFS=" =" read node name val; do
2896                 do_node ${node//:/} "lctl set_param -n $name $val"
2897         done
2898 }
2899
2900 check_catastrophe() {
2901     local rnodes=${1:-$(comma_list $(remote_nodes_list))}
2902     local C=$CATASTROPHE
2903     [ -f $C ] && [ $(cat $C) -ne 0 ] && return 1
2904
2905     if [ $rnodes ]; then
2906         do_nodes $rnodes "rc=\\\$([ -f $C ] && echo \\\$(< $C) || echo 0);
2907 if [ \\\$rc -ne 0 ]; then echo \\\$(hostname): \\\$rc; fi
2908 exit \\\$rc;"
2909     fi
2910 }
2911
2912 # $1 node
2913 # $2 file
2914 # $3 $RUNAS
2915 get_stripe_info() {
2916         local tmp_file
2917
2918         stripe_size=0
2919         stripe_count=0
2920         stripe_index=0
2921         tmp_file=$(mktemp)
2922
2923         do_facet $1 $3 lfs getstripe -v $2 > $tmp_file
2924
2925         stripe_size=`awk '$1 ~ /size/ {print $2}' $tmp_file`
2926         stripe_count=`awk '$1 ~ /count/ {print $2}' $tmp_file`
2927         stripe_index=`awk '$1 ~ /stripe_offset/ {print $2}' $tmp_file`
2928         rm -f $tmp_file
2929 }
2930
2931 mdsrate_cleanup () {
2932     mpi_run -np $1 -machinefile $2 ${MDSRATE} --unlink --nfiles $3 --dir $4 --filefmt $5 $6
2933 }
2934
2935 delayed_recovery_enabled () {
2936     do_facet mds "lctl get_param -n mds.${mds_svc}.stale_export_age" > /dev/null 2>&1
2937 }
2938
2939 ################################################################################
2940 # The following functions are used to enable interop testing between
2941 # 1.8 and 2.0. The lprocfs layout changed from 1.8 to 2.0 as the followings:
2942 # mds -> mdt
2943 # {blocksize filesfree filestotal fstype kbytesavail kbytesfree kbytestotal mntdev} moved from mds to osd
2944 # mdt lov: fsname-mdtlov -> fsname-MDTXXXX-mdtlov
2945 # mdt osc: fsname-OSTXXXX-osc -> fsname-OSTXXXX-osc-MDTXXXX
2946 ################################################################################
2947
2948 get_lustre_version () {
2949     local node=${1:-"mds"}
2950     do_facet $node $LCTL get_param -n version |  awk '/^lustre:/ {print $2}'
2951 }
2952
2953 get_mds_version_major () {
2954     local version=$(get_lustre_version mds)
2955     echo $version | awk -F. '{print $1}'
2956 }
2957
2958 get_mds_version_minor () {
2959     local version=$(get_lustre_version mds)
2960     echo $version | awk -F. '{print $2}'
2961 }
2962
2963 get_mds_version_patch () {
2964     local version=$(get_lustre_version mds)
2965     echo $version | awk -F. '{print $3}'
2966 }
2967
2968 get_mds_version_fix () {
2969     local version=$(get_lustre_version mds)
2970     echo $version | awk -F. '{print $4}'
2971 }
2972
2973 get_mds_fsstat_proc_path() {
2974     local major=$(get_mds_version_major)
2975     local minor=$(get_mds_version_minor)
2976     if [ $major -le 1 -a $minor -le 8 ] ; then
2977         echo "mds"
2978     else
2979         echo "osd"
2980     fi
2981 }
2982
2983 get_mds_mntdev_proc_path() {
2984     local fsstat_dev=$(get_mds_fsstat_proc_path)
2985     echo "$fsstat_dev.*.mntdev"
2986 }
2987
2988 get_mdtlov_proc_path() {
2989     local fsname=$1
2990     local major=$(get_mds_version_major)
2991     local minor=$(get_mds_version_minor)
2992     if [ $major -le 1 -a $minor -le 8 ] ; then
2993         echo "${fsname}-mdtlov"
2994     else
2995         echo "${fsname}-MDT0000-mdtlov"
2996     fi
2997 }
2998
2999 get_mdtosc_proc_path() {
3000     local ost=$1
3001     local major=$(get_mds_version_major)
3002     local minor=$(get_mds_version_minor)
3003     if [ $major -le 1 -a $minor -le 8 ] ; then
3004         echo "${ost}-osc"
3005     else
3006         echo "${ost}-osc-MDT0000"
3007     fi
3008 }
3009
3010 get_mds_mdt_device_proc_path() {
3011     local major=$(get_mds_version_major)
3012     local minor=$(get_mds_version_minor)
3013     if [ $major -le 1 -a $minor -le 8 ] ; then
3014         echo "mds"
3015     else
3016         echo "mdt"
3017     fi
3018 }
3019
3020 get_md_name () {
3021     local major=$(get_mds_version_major)
3022     local minor=$(get_mds_version_minor)
3023     if [ $major -le 1 -a $minor -le 8 ] ; then
3024         echo "mdt"
3025     else
3026         echo "mdd"
3027     fi
3028 }
3029
3030 ########################
3031
3032 convert_facet2label() { 
3033     local facet=$1
3034
3035     if [ x$facet = xost ]; then
3036        facet=ost1
3037     fi
3038
3039     local varsvc=${facet}_svc
3040
3041     if [ -n ${!varsvc} ]; then
3042         echo ${!varsvc}
3043     else  
3044         error "No lablel for $facet!"
3045     fi
3046 }
3047
3048 get_clientosc_proc_path() {
3049     local ost=$1
3050
3051     echo "{$1}-osc-*"
3052 }
3053
3054 get_osc_import_name() {
3055     local facet=$1
3056     local ost=$2
3057     local label=$(convert_facet2label $ost)
3058
3059     if [ "$facet" == "mds" ]; then
3060         get_mdtosc_proc_path $label
3061         return 0
3062     fi
3063
3064     get_clientosc_proc_path $label
3065     return 0
3066 }
3067
3068 wait_import_state () {
3069     local expected=$1
3070     local CONN_PROC=$2
3071     local CONN_STATE
3072     local i=0
3073
3074     CONN_STATE=$($LCTL get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3075     while [ "${CONN_STATE}" != "${expected}" ]; do
3076         if [ "${expected}" == "DISCONN" ]; then
3077             # for disconn we can check after proc entry is removed
3078             [ "x${CONN_STATE}" == "x" ] && return 0
3079             #  with AT we can have connect request timeout ~ reconnect timeout
3080             # and test can't see real disconnect
3081             [ "${CONN_STATE}" == "CONNECTING" ] && return 0
3082         fi
3083         # disconnect rpc should be wait not more obd_timeout
3084         [ $i -ge $(($TIMEOUT * 3 / 2)) ] && \
3085             error "can't put import for $CONN_PROC into ${expected} state" && return 1
3086         sleep 1
3087         CONN_STATE=$($LCTL get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3088         i=$(($i + 1))
3089     done
3090
3091     log "$CONN_PROC now in ${CONN_STATE} state"
3092     return 0
3093 }
3094
3095 wait_osc_import_state() {
3096     local facet=$1
3097     local ost_facet=$2
3098     local expected=$3
3099     local ost=$(get_osc_import_name $facet $ost_facet)
3100     local CONN_PROC
3101     local CONN_STATE
3102     local i=0
3103
3104     CONN_PROC="osc.${ost}.ost_server_uuid"
3105     CONN_STATE=$(do_facet $facet lctl get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3106     while [ "${CONN_STATE}" != "${expected}" ]; do
3107         if [ "${expected}" == "DISCONN" ]; then 
3108             # for disconn we can check after proc entry is removed
3109             [ "x${CONN_STATE}" == "x" ] && return 0
3110             #  with AT enabled, we can have connect request timeout near of 
3111             # reconnect timeout and test can't see real disconnect
3112             [ "${CONN_STATE}" == "CONNECTING" ] && return 0
3113         fi
3114         # disconnect rpc should be wait not more obd_timeout
3115         [ $i -ge $(($TIMEOUT * 3 / 2)) ] && \
3116             error "can't put import for ${ost}(${ost_facet}) into ${expected} state" && return 1
3117         sleep 1
3118         CONN_STATE=$(do_facet $facet lctl get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3119         i=$(($i + 1))
3120     done
3121
3122     log "${ost_facet} now in ${CONN_STATE} state"
3123     return 0
3124 }
3125
3126 get_clientmdc_proc_path() {
3127     echo "${1}-mdc-*"
3128 }
3129
3130 do_rpc_nodes () {
3131     local list=$1
3132     shift
3133
3134     do_nodesv $list "PATH=$LUSTRE/tests/:$PATH sh rpc.sh $@ " 
3135 }
3136
3137 wait_clients_import_state () {
3138     local list=$1
3139     local facet=$2
3140     local expected=$3
3141     shift
3142
3143     local label=$(convert_facet2label $facet)
3144     local proc_path
3145     case $facet in
3146         ost* ) proc_path="osc.$(get_clientosc_proc_path $label).ost_server_uuid" ;;
3147         mds* ) proc_path="mdc.$(get_clientmdc_proc_path $label).mds_server_uuid" ;;
3148         *) error "unknown facet!" ;;
3149     esac
3150
3151     if ! do_rpc_nodes $list wait_import_state $expected $proc_path; then
3152         error "import is not in ${expected} state"
3153         return 1
3154     fi
3155 }
3156
3157 destroy_pool_int() {
3158     local ost
3159     local OSTS=$(do_facet mds lctl pool_list $1 | awk '$1 !~ /^Pool:/ {print $1}')
3160     for ost in $OSTS; do
3161         do_facet mgs lctl pool_remove $1 $ost
3162     done
3163     do_facet mgs lctl pool_destroy $1
3164 }
3165
3166 # <fsname>.<poolname> or <poolname>
3167 destroy_pool() {
3168     local fsname=${1%%.*}
3169     local poolname=${1##$fsname.}
3170
3171     [[ x$fsname = x$poolname ]] && fsname=$FSNAME
3172
3173     local RC
3174
3175     pool_list $fsname.$poolname || return $?
3176
3177     destroy_pool_int $fsname.$poolname
3178     RC=$?
3179     [[ $RC -ne 0 ]] && return $RC
3180
3181     wait_update $HOSTNAME "lctl get_param -n lov.$fsname-*.pools.$poolname \
3182       2>/dev/null || echo foo" "foo" || RC=1
3183
3184     if [[ $RC -eq 0 ]]; then
3185         remove_pool_from_list $fsname.$poolname
3186     else
3187         error "destroy pool failed $1"
3188     fi
3189     return $RC
3190 }
3191
3192 destroy_pools () {
3193     local fsname=${1:-$FSNAME}
3194     local poolname
3195     local listvar=${fsname}_CREATED_POOLS
3196
3197     pool_list $fsname
3198
3199     [ x${!listvar} = x ] && return 0
3200
3201     echo destroy the created pools: ${!listvar}
3202     for poolname in ${!listvar//,/ }; do
3203         destroy_pool $fsname.$poolname
3204     done
3205 }
3206
3207 cleanup_pools () {
3208     local fsname=${1:-$FSNAME}
3209     trap 0
3210     destroy_pools $fsname
3211 }
3212
3213 gather_logs () {
3214     local list=$1
3215
3216     local ts=$(date +%s)
3217
3218     # bug 20237, comment 11
3219     # It would also be useful to provide the option
3220     # of writing the file to an NFS directory so it doesn't need to be copied.
3221     local tmp=$TMP
3222     local docp=true
3223     [ -d "$SHARED_DIR_LOGS" ] && tmp=$SHARED_DIR_LOGS && docp=false
3224
3225     # dump lustre logs, dmesg
3226     do_nodes $list "log=$tmp/\\\$(hostname)-debug-$ts.log ;
3227 lctl dk \\\$log >/dev/null;
3228 log=$tmp/\\\$(hostname)-dmesg-$ts.log;
3229 dmesg > \\\$log; "
3230
3231     # FIXME: does it make sense to collect the logs for $ts only, but all
3232     # TESTSUITE logs?
3233     # rsync $TMP/*${TESTSUITE}* to gather the logs dumped by error fn
3234     local logs=$TMP/'*'${TESTSUITE}'*'
3235     if $docp; then
3236         logs=$logs' '$tmp/'*'$ts'*'
3237     fi
3238     for node in ${list//,/ }; do
3239         rsync -az $node:"$logs" $TMP
3240     done
3241
3242     local archive=$TMP/${TESTSUITE}-$ts.tar.bz2
3243     tar -jcf $archive $tmp/*$ts* $TMP/*${TESTSUITE}*
3244
3245     echo $archive
3246 }
3247
3248 cleanup_logs () {
3249     local list=${1:-$(comma_list $(nodes_list))}
3250
3251     [ -n ${TESTSUITE} ] && do_nodes $list "rm -f $TMP/*${TESTSUITE}*" || true
3252 }
3253
3254 do_ls () {
3255     local mntpt_root=$1
3256     local num_mntpts=$2
3257     local dir=$3
3258     local i
3259     local cmd
3260     local pids
3261     local rc=0
3262
3263     for i in $(seq 0 $num_mntpts); do
3264         cmd="ls -laf ${mntpt_root}$i/$dir"
3265         echo + $cmd;
3266         $cmd > /dev/null &
3267         pids="$pids $!"
3268     done
3269     echo pids=$pids
3270     for pid in $pids; do
3271         wait $pid || rc=$?
3272     done
3273
3274     return $rc
3275 }
3276
3277 # target_start_and_reset_recovery_timer()
3278 #        service_time = at_est2timeout(service_time);
3279 #        service_time += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC +
3280 #                             INITIAL_CONNECT_TIMEOUT);
3281 # CONNECTION_SWITCH_MAX : min(25U, max(CONNECTION_SWITCH_MIN,obd_timeout))
3282 #define CONNECTION_SWITCH_INC 1
3283 #define INITIAL_CONNECT_TIMEOUT max(CONNECTION_SWITCH_MIN,obd_timeout/20)
3284 #define CONNECTION_SWITCH_MIN 5U
3285
3286 max_recovery_time () {
3287     local init_connect_timeout=$(( TIMEOUT / 20 ))
3288     [[ $init_connect_timeout > 5 ]] || init_connect_timeout=5 
3289
3290     local service_time=$(( $(at_max_get client) + $(( 2 * $(( 25 + 1  + init_connect_timeout)) )) ))
3291
3292     echo $service_time 
3293 }
3294
3295 remove_mdt_files() {
3296     local facet=$1
3297     local mdtdev=$2
3298     shift 2
3299     local files="$@"
3300     local mntpt=${MOUNT%/*}/$facet
3301
3302     echo "removing files from $mdtdev on $facet: $files"
3303     mount -t $FSTYPE $MDS_MOUNT_OPTS $mdtdev $mntpt || return $?
3304     rc=0;
3305     for f in $files; do
3306         rm $mntpt/ROOT/$f || { rc=$?; break; }
3307     done
3308     umount -f $mntpt || return $?
3309     return $rc
3310 }
3311
3312 duplicate_mdt_files() {
3313     local facet=$1
3314     local mdtdev=$2
3315     shift 2
3316     local files="$@"
3317     local mntpt=${MOUNT%/*}/$facet
3318
3319     echo "duplicating files on $mdtdev on $facet: $files"
3320     mkdir -p $mntpt || return $?
3321     mount -t $FSTYPE $MDS_MOUNT_OPTS $mdtdev $mntpt || return $?
3322
3323     do_umount() {
3324         trap 0
3325         popd > /dev/null
3326         rm $tmp
3327         umount -f $mntpt
3328     }
3329     trap do_umount EXIT
3330
3331     tmp=$(mktemp $TMP/setfattr.XXXXXXXXXX)
3332     pushd $mntpt/ROOT > /dev/null || return $?
3333     rc=0
3334     for f in $files; do
3335         touch $f.bad || return $?
3336         getfattr -n trusted.lov $f | sed "s#$f#&.bad#" > $tmp
3337         rc=${PIPESTATUS[0]}
3338         [ $rc -eq 0 ] || return $rc
3339         setfattr --restore $tmp || return $?
3340     done
3341     do_umount
3342 }