Whamcloud - gitweb
LU-1526 tests: add --index support to the test framework
[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 && touch $TF_FAIL && \
5     echo "test-framework exiting on error"' ERR
6 set -e
7 #set -x
8
9
10 export REFORMAT=${REFORMAT:-""}
11 export WRITECONF=${WRITECONF:-""}
12 export VERBOSE=${VERBOSE:-false}
13 export GMNALNID=${GMNALNID:-/usr/sbin/gmlndnid}
14 export CATASTROPHE=${CATASTROPHE:-/proc/sys/lnet/catastrophe}
15 #export PDSH="pdsh -S -Rssh -w"
16
17 # function used by scripts run on remote nodes
18 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
19 . $LUSTRE/tests/functions.sh
20 . $LUSTRE/tests/yaml.sh
21
22 LUSTRE_TESTS_CFG_DIR=${LUSTRE_TESTS_CFG_DIR:-${LUSTRE}/tests/cfg}
23
24 EXCEPT_LIST_FILE=${EXCEPT_LIST_FILE:-${LUSTRE_TESTS_CFG_DIR}/tests-to-skip.sh}
25
26 if [ -f "$EXCEPT_LIST_FILE" ]; then
27     echo "Reading test skip list from $EXCEPT_LIST_FILE"
28     cat $EXCEPT_LIST_FILE
29     . $EXCEPT_LIST_FILE
30 fi
31
32 # check config files for options in decreasing order of preference
33 [ -z "$MODPROBECONF" -a -f /etc/modprobe.d/lustre.conf ] &&
34     MODPROBECONF=/etc/modprobe.d/lustre.conf
35 [ -z "$MODPROBECONF" -a -f /etc/modprobe.d/Lustre ] &&
36     MODPROBECONF=/etc/modprobe.d/Lustre
37 [ -z "$MODPROBECONF" -a -f /etc/modprobe.conf ] &&
38     MODPROBECONF=/etc/modprobe.conf
39
40 assert_DIR () {
41     local failed=""
42     [[ $DIR/ = $MOUNT/* ]] || \
43         { failed=1 && echo "DIR=$DIR not in $MOUNT. Aborting."; }
44     [[ $DIR1/ = $MOUNT1/* ]] || \
45         { failed=1 && echo "DIR1=$DIR1 not in $MOUNT1. Aborting."; }
46     [[ $DIR2/ = $MOUNT2/* ]] || \
47         { failed=1 && echo "DIR2=$DIR2 not in $MOUNT2. Aborting"; }
48
49     [ -n "$failed" ] && exit 99 || true
50 }
51
52 usage() {
53     echo "usage: $0 [-r] [-f cfgfile]"
54     echo "       -r: reformat"
55
56     exit
57 }
58
59 print_summary () {
60     trap 0
61     [ "$TESTSUITE" == "lfsck" ] && return 0
62     [ -n "$ONLY" ] && echo "WARNING: ONLY is set to $(echo $ONLY)"
63     local details
64     local form="%-13s %-17s %-9s %s %s\n"
65     printf "$form" "status" "script" "Total(sec)" "E(xcluded) S(low)"
66     echo "------------------------------------------------------------------------------------"
67     for O in $DEFAULT_SUITES; do
68         [ "${!O}" = "no" ] && continue || true
69         O=$(echo $O  | tr "-" "_" | tr "[:lower:]" "[:upper:]")
70         local o=$(echo $O | tr "[:upper:]" "[:lower:]")
71         o=${o//_/-}
72         local log=${TMP}/${o}.log
73         if is_sanity_benchmark $o; then
74             log=${TMP}/sanity-benchmark.log
75         fi
76         local slow=
77         local skipped=
78         local total=
79         local status=Unfinished
80         if [ -f $log ]; then
81             skipped=$(grep excluded $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
82             slow=$(egrep "^PASS|^FAIL" $log | tr -d "("| sed s/s\)$//g | sort -nr -k 3  | head -5 |  awk '{ print $2":"$3"s" }')
83             total=$(grep duration $log | awk '{ print $2}')
84             if [ "${!O}" = "done" ]; then
85                 status=Done
86             fi
87             if $DDETAILS; then
88                 local durations=$(egrep "^PASS|^FAIL" $log |  tr -d "("| sed s/s\)$//g | awk '{ print $2":"$3"|" }')
89                 details=$(printf "%s\n%s %s %s\n" "$details" "DDETAILS" "$O" "$(echo $durations)")
90             fi
91         fi
92         printf "$form" $status "$O" "${total}" "E=$skipped"
93         printf "$form" "-" "-" "-" "S=$(echo $slow)"
94     done
95
96     for O in $DEFAULT_SUITES; do
97         O=$(echo $O  | tr "-" "_" | tr "[:lower:]" "[:upper:]")
98         if [ "${!O}" = "no" ]; then
99             # FIXME.
100             # only for those tests suits which are run directly from acc-sm script:
101             # bonnie, iozone, etc.
102             if [ -f "$TESTSUITELOG" ] && grep FAIL $TESTSUITELOG | grep -q ' '$O  ; then
103                printf "$form" "UNFINISHED" "$O" ""
104             else
105                printf "$form" "Skipped" "$O" ""
106             fi
107         fi
108     done
109
110     for O in $DEFAULT_SUITES; do
111         O=$(echo $O  | tr "-" "_" | tr "[:lower:]" "[:upper:]")
112         [ "${!O}" = "done" -o "${!O}" = "no" ] || \
113             printf "$form" "UNFINISHED" "$O" ""
114     done
115 }
116
117 init_test_env() {
118     export LUSTRE=`absolute_path $LUSTRE`
119     export TESTSUITE=`basename $0 .sh`
120     export TEST_FAILED=false
121     export FAIL_ON_SKIP_ENV=${FAIL_ON_SKIP_ENV:-false}
122
123     export MKE2FS=${MKE2FS:-mke2fs}
124     export DEBUGFS=${DEBUGFS:-debugfs}
125     export TUNE2FS=${TUNE2FS:-tune2fs}
126     export E2LABEL=${E2LABEL:-e2label}
127     export DUMPE2FS=${DUMPE2FS:-dumpe2fs}
128     export E2FSCK=${E2FSCK:-e2fsck}
129     export LFSCK_BIN=${LFSCK_BIN:-lfsck}
130
131     export LFSCK_ALWAYS=${LFSCK_ALWAYS:-"no"} # check fs after each test suite
132     export FSCK_MAX_ERR=4   # File system errors left uncorrected
133     declare -a OSTDEVS
134
135     #[ -d /r ] && export ROOT=${ROOT:-/r}
136     export TMP=${TMP:-$ROOT/tmp}
137     export TESTSUITELOG=${TMP}/${TESTSUITE}.log
138     export LOGDIR=${LOGDIR:-${TMP}/test_logs/$(date +%s)}
139     export TESTLOG_PREFIX=$LOGDIR/$TESTSUITE
140
141     export HOSTNAME=${HOSTNAME:-`hostname -s`}
142     if ! echo $PATH | grep -q $LUSTRE/utils; then
143         export PATH=$PATH:$LUSTRE/utils
144     fi
145     if ! echo ":$PATH:" | grep -q ":$LUSTRE/tests:"; then
146         export PATH=$LUSTRE/tests:$PATH
147     fi
148     if ! echo $PATH | grep -q $LUSTRE/../lustre-iokit/sgpdd-survey; then
149         export PATH=$PATH:$LUSTRE/../lustre-iokit/sgpdd-survey
150     fi
151     export LST=${LST:-"$LUSTRE/../lnet/utils/lst"}
152     [ ! -f "$LST" ] && export LST=$(which lst)
153     export SGPDDSURVEY=${SGPDDSURVEY:-"$LUSTRE/../lustre-iokit/sgpdd-survey/sgpdd-survey")}
154     [ ! -f "$SGPDDSURVEY" ] && export SGPDDSURVEY=$(which sgpdd-survey)
155     # Ubuntu, at least, has a truncate command in /usr/bin
156     # so fully path our truncate command.
157     export TRUNCATE=${TRUNCATE:-$LUSTRE/tests/truncate}
158     export MDSRATE=${MDSRATE:-"$LUSTRE/tests/mpi/mdsrate"}
159     [ ! -f "$MDSRATE" ] && export MDSRATE=$(which mdsrate 2> /dev/null)
160     if ! echo $PATH | grep -q $LUSTRE/tests/racer; then
161         export PATH=$LUSTRE/tests/racer:$PATH:
162     fi
163     if ! echo $PATH | grep -q $LUSTRE/tests/mpi; then
164         export PATH=$PATH:$LUSTRE/tests/mpi
165     fi
166     export RSYNC_RSH=${RSYNC_RSH:-rsh}
167     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
168     [ ! -f "$LCTL" ] && export LCTL=$(which lctl)
169     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
170     [ ! -f "$LFS" ] && export LFS=$(which lfs)
171     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
172     [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre)
173     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
174     [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre)
175     export CHECKSTAT="${CHECKSTAT:-"checkstat -v"} "
176     export LUSTRE_RMMOD=${LUSTRE_RMMOD:-$LUSTRE/scripts/lustre_rmmod}
177     [ ! -f "$LUSTRE_RMMOD" ] && export LUSTRE_RMMOD=$(which lustre_rmmod 2> /dev/null)
178     export FSTYPE=${FSTYPE:-"ldiskfs"}
179     export NAME=${NAME:-local}
180     export DIR2
181     export SAVE_PWD=${SAVE_PWD:-$LUSTRE/tests}
182
183     if [ "$ACCEPTOR_PORT" ]; then
184         export PORT_OPT="--port $ACCEPTOR_PORT"
185     fi
186
187     export LOAD_MODULES_REMOTE=${LOAD_MODULES_REMOTE:-false}
188
189     # Paths on remote nodes, if different
190     export RLUSTRE=${RLUSTRE:-$LUSTRE}
191     export RPWD=${RPWD:-$PWD}
192     export I_MOUNTED=${I_MOUNTED:-"no"}
193     if [ ! -f /lib/modules/$(uname -r)/kernel/fs/lustre/mds.ko -a \
194         ! -f /lib/modules/$(uname -r)/updates/kernel/fs/lustre/mds.ko -a \
195         ! -f `dirname $0`/../mds/mds.ko ]; then
196         export CLIENTMODSONLY=yes
197     fi
198
199     export SHUTDOWN_ATTEMPTS=${SHUTDOWN_ATTEMPTS:-3}
200
201     # command line
202
203     while getopts "rvwf:" opt $*; do
204         case $opt in
205             f) CONFIG=$OPTARG;;
206             r) REFORMAT=--reformat;;
207             v) VERBOSE=true;;
208             w) WRITECONF=writeconf;;
209             \?) usage;;
210         esac
211     done
212
213     shift $((OPTIND - 1))
214     ONLY=${ONLY:-$*}
215
216     # print the durations of each test if "true"
217     DDETAILS=${DDETAILS:-false}
218     [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
219     cntlog=0
220     rm -f $TMP/*active
221 }
222
223 case `uname -r` in
224     *) EXT=".ko"; USE_QUOTA=yes;;
225 esac
226
227 pool_list () {
228    do_facet mgs lctl pool_list $1
229 }
230
231 create_pool() {
232     local fsname=${1%%.*}
233     local poolname=${1##$fsname.}
234
235     do_facet mgs lctl pool_new $1
236     local RC=$?
237     # get param should return err unless pool is created
238     [[ $RC -ne 0 ]] && return $RC
239
240     wait_update $HOSTNAME "lctl get_param -n lov.$fsname-*.pools.$poolname \
241         2>/dev/null || echo foo" "" || RC=1
242     if [[ $RC -eq 0 ]]; then
243         add_pool_to_list $1
244     else
245         error "pool_new failed $1"
246     fi
247     return $RC
248 }
249
250 add_pool_to_list () {
251     local fsname=${1%%.*}
252     local poolname=${1##$fsname.}
253
254     local listvar=${fsname}_CREATED_POOLS
255     eval export ${listvar}=$(expand_list ${!listvar} $poolname)
256 }
257
258 remove_pool_from_list () {
259     local fsname=${1%%.*}
260     local poolname=${1##$fsname.}
261
262     local listvar=${fsname}_CREATED_POOLS
263     eval export ${listvar}=$(exclude_items_from_list ${!listvar} $poolname)
264 }
265
266 module_loaded () {
267    /sbin/lsmod | grep -q $1
268 }
269
270 load_module() {
271     module=$1
272     shift
273     BASE=`basename $module $EXT`
274
275     module_loaded ${BASE} && return
276
277     if [ "$BASE" == "lnet_selftest" ] && \
278             [ -f ${LUSTRE}/../lnet/selftest/${module}${EXT} ]; then
279         insmod ${LUSTRE}/../lnet/selftest/${module}${EXT}
280
281     elif [ -f ${LUSTRE}/${module}${EXT} ]; then
282         insmod ${LUSTRE}/${module}${EXT} $@
283     else
284         # must be testing a "make install" or "rpm" installation
285         modprobe $BASE $@
286     fi
287 }
288
289 load_module_llite_lloop() {
290     local n1=$(uname -r | cut -d. -f1)
291     local n2=$(uname -r | cut -d. -f2)
292     local n3=$(uname -r | cut -d- -f1 | cut -d. -f3)
293
294     # load the llite_lloop module for < 2.6.32 kernels
295     if [[ $n1 -lt 2 ]] || [[ $n1 -eq 2 && $n2 -lt 6 ]] || \
296         [[ $n1 -eq 2 && $n2 -eq 6 && $n3 -lt 32 ]]; then
297         load_module llite/llite_lloop
298     fi
299 }
300
301 load_modules_local() {
302     if [ -n "$MODPROBE" ]; then
303         # use modprobe
304     return 0
305     fi
306     if [ "$HAVE_MODULES" = true ]; then
307     # we already loaded
308         return 0
309     fi
310     HAVE_MODULES=true
311
312     echo Loading modules from $LUSTRE
313     load_module ../lnet/libcfs/libcfs
314     [ "$PTLDEBUG" ] && lctl set_param debug="$PTLDEBUG"
315     [ "$SUBSYSTEM" ] && lctl set_param subsystem_debug="${SUBSYSTEM# }"
316     [ -z "$LNETOPTS" -a "$MODPROBECONF" ] && \
317         LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
318     echo $LNETOPTS | grep -q "accept=all"  || LNETOPTS="$LNETOPTS accept=all";
319     # bug 19380
320     # disable it for now since it only hides the stack overflow upon test w/
321     # local servers
322 #    if [ "$NETTYPE" = "tcp" -o "$NETTYPE" = "o2ib" -o "$NETTYPE" = "ptl" ]; then
323 #        echo $LNETOPTS | grep -q "local_nid_dist_zero=0" ||
324 #        LNETOPTS="$LNETOPTS local_nid_dist_zero=0"
325 #    fi
326     echo "lnet options: '$LNETOPTS'"
327     # note that insmod will ignore anything in modprobe.conf
328     load_module ../lnet/lnet/lnet $LNETOPTS
329     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
330     load_module ../lnet/klnds/$LNETLND
331     load_module lvfs/lvfs
332     load_module obdclass/obdclass
333     load_module ptlrpc/ptlrpc
334     [ "$USE_QUOTA" = "yes" ] && load_module quota/lquota $LQUOTAOPTS
335     load_module mdc/mdc
336     load_module osc/osc
337     load_module lov/lov
338     load_module mgc/mgc
339     if ! client_only; then
340         load_module mgs/mgs
341         load_module mds/mds
342         grep -q crc16 /proc/kallsyms || { modprobe crc16 2>/dev/null || true; }
343         grep -q -w jbd /proc/kallsyms || { modprobe jbd 2>/dev/null || true; }
344         grep -q -w jbd2 /proc/kallsyms || { modprobe jbd2 2>/dev/null || true; }
345         [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
346         load_module lvfs/fsfilt_$FSTYPE
347         load_module ost/ost
348         load_module obdfilter/obdfilter
349     fi
350
351     load_module llite/lustre
352     load_module_llite_lloop
353     rm -f $TMP/ogdb-$HOSTNAME
354     OGDB=$TMP
355     [ -d /r ] && OGDB="/r/tmp"
356     $LCTL modules > $OGDB/ogdb-$HOSTNAME
357     # 'mount' doesn't look in $PATH, just sbin
358     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
359 }
360
361 load_modules () {
362     load_modules_local
363     # bug 19124
364     # load modules on remote nodes optionally
365     # lustre-tests have to be installed on these nodes
366     if $LOAD_MODULES_REMOTE ; then
367         local list=$(comma_list $(remote_nodes_list))
368         echo loading modules on $list
369         do_rpc_nodes $list load_modules
370     fi
371 }
372
373 check_mem_leak () {
374     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd_memory.*leaked" || true)
375     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
376     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
377         echo "$LEAK_LUSTRE" 1>&2
378         echo "$LEAK_PORTALS" 1>&2
379         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
380         echo "Memory leaks detected"
381         [ -n "$IGNORE_LEAK" ] && { echo "ignoring leaks" && return 0; } || true
382         return 1
383     fi
384 }
385
386 unload_modules() {
387     wait_exit_ST client # bug 12845
388
389     if $LOAD_MODULES_REMOTE ; then
390         local list=$(comma_list $(remote_nodes_list))
391         if [ ! -z $list ]; then
392             echo unloading modules on $list
393             do_rpc_nodes $list $LUSTRE_RMMOD $FSTYPE
394             do_rpc_nodes $list check_mem_leak
395         fi
396     fi
397
398     $LUSTRE_RMMOD $FSTYPE || return 2
399
400     HAVE_MODULES=false
401
402     check_mem_leak || return 254
403
404     echo "modules unloaded."
405     return 0
406 }
407
408 set_debug_size () {
409     local dz=${1:-$DEBUG_SIZE}
410     local cpus=$(getconf _NPROCESSORS_CONF)
411
412     # bug 19944, adjust size to be -gt num_possible_cpus()
413     # promise 2MB for every cpu at least
414     if [ -n "$cpus" ] && [ $((cpus * 2)) -gt $dz ]; then
415         dz=$((cpus * 2))
416     fi
417     lctl set_param debug_mb=$dz
418 }
419
420 set_default_debug () {
421     local debug=${1:-"$PTLDEBUG"}
422     local subsystem_debug=${2:-"$SUBSYSTEM"}
423     local debug_size=${3:-$DEBUG_SIZE}
424
425     lctl set_param debug="$debug"
426     lctl set_param subsystem_debug="${subsystem_debug# }"
427
428     set_debug_size $debug_size
429     sync
430 }
431
432 set_default_debug_nodes () {
433     local nodes=$1
434
435     if [[ ,$nodes, = *,$HOSTNAME,* ]]; then
436         nodes=$(exclude_items_from_list "$nodes" "$HOSTNAME")
437             set_default_debug
438     fi
439
440     [[ -n $nodes ]] && do_rpc_nodes $nodes set_default_debug \
441             \\\"$PTLDEBUG\\\" \\\"$SUBSYSTEM\\\" $DEBUG_SIZE || true
442 }
443
444 set_default_debug_facet () {
445     local facet=$1
446     local node=$(facet_active_host $facet)
447     [ -z "$node" ] && echo "No host defined for facet $facet" && exit 1
448
449     set_default_debug_nodes $node
450 }
451
452 # Facet functions
453 mount_facets () {
454     local facets=${1:-$(get_facets)}
455     local facet
456
457     for facet in ${facets//,/ }; do
458         mount_facet $facet || error "Restart of $facet failed!"
459     done
460 }
461
462 mount_facet() {
463     local facet=$1
464     shift
465     local dev=$(facet_active $facet)_dev
466     local opt=${facet}_opt
467     local mntpt=$(facet_mntpt $facet)
468
469     echo "Starting ${facet}: ${!opt} $@ ${!dev} $mntpt"
470     do_facet ${facet} "mkdir -p $mntpt; mount -t lustre ${!opt} $@ ${!dev} $mntpt"
471     RC=${PIPESTATUS[0]}
472     if [ $RC -ne 0 ]; then
473         echo "mount -t lustre $@ ${!dev} $mntpt"
474         echo "Start of ${!dev} on ${facet} failed ${RC}"
475     else
476         set_default_debug_facet $facet
477
478         label=$(do_facet ${facet} "$E2LABEL ${!dev}")
479         [ -z "$label" ] && echo no label for ${!dev} && exit 1
480         eval export ${facet}_svc=${label}
481         echo Started ${label}
482     fi
483     return $RC
484 }
485
486 # start facet device options
487 start() {
488     local facet=$1
489     shift
490     local device=$1
491     shift
492     eval export ${facet}_dev=${device}
493     eval export ${facet}_opt=\"$@\"
494
495     local varname=${facet}failover_dev
496     if [ -n "${!varname}" ] ; then
497         eval export ${facet}failover_dev=${!varname}
498     else
499         eval export ${facet}failover_dev=$device
500     fi
501
502     local mntpt=$(facet_mntpt $facet)
503     do_facet ${facet} mkdir -p $mntpt
504     eval export ${facet}_MOUNT=$mntpt
505     mount_facet ${facet}
506     RC=$?
507     return $RC
508 }
509
510 stop() {
511     local running
512     local facet=$1
513     shift
514     local HOST=`facet_active_host $facet`
515     [ -z $HOST ] && echo stop: no host for $facet && return 0
516
517     local mntpt=$(facet_mntpt $facet)
518     running=$(do_facet ${facet} "grep -c $mntpt' ' /proc/mounts") || true
519     if [ ${running} -ne 0 ]; then
520         echo "Stopping $mntpt (opts:$@)"
521         do_facet ${facet} umount -d $@ $mntpt
522     fi
523
524     # umount should block, but we should wait for unrelated obd's
525     # like the MGS or MGC to also stop.
526
527     wait_exit_ST ${facet}
528 }
529
530 # set quota version (both administrative and operational quotas)
531 quota_set_version() {
532         do_facet mds "lctl set_param lquota.${FSNAME}-MDT*.quota_type=$1"
533         local varsvc
534         local osts=$(get_facets OST)
535         for ost in ${osts//,/ }; do
536                 varsvc=${ost}_svc
537                 do_facet $ost "lctl set_param lquota.${!varsvc}.quota_type=$1"
538         done
539 }
540
541 # save quota version (both administrative and operational quotas)
542 # the function will also switch to the new version and the new type
543 quota_save_version() {
544     local spec=$1
545     local ver=$(tr -c -d "123" <<< $spec)
546     local type=$(tr -c -d "ug" <<< $spec)
547
548     local lustre_version=$(get_lustre_version mds)
549     if [[ $lustre_version = 1.8* ]] ; then
550         $LFS quotaoff -ug $MOUNT # just in case
551         [ -n "$ver" ] && quota_set_version $ver
552     else
553         echo mds running $lustre_version
554         [ -n "$ver" -a "$ver" != "3" ] && error "wrong quota version specifier"
555     fi
556
557     [ -n "$type" ] && { $LFS quotacheck -$type $MOUNT || error "quotacheck has failed"; }
558
559     do_facet mgs "lctl conf_param ${FSNAME}-MDT*.$(get_md_name).quota_type=$spec"
560     local varsvc
561     local osts=$(get_facets OST)
562     for ost in ${osts//,/ }; do
563         varsvc=${ost}_svc
564         do_facet mgs "lctl conf_param ${!varsvc}.ost.quota_type=$spec"
565     done
566 }
567
568 # client could mount several lustre
569 quota_type () {
570     local fsname=${1:-$FSNAME}
571     local rc=0
572     do_facet mgs lctl get_param md*.${fsname}-MDT*.quota_type || rc=$?
573     do_nodes $(comma_list $(osts_nodes)) \
574         lctl get_param obdfilter.${fsname}-OST*.quota_type || rc=$?
575     return $rc
576 }
577
578 restore_quota_type () {
579    local mntpt=${1:-$MOUNT}
580    local quota_type=$(quota_type $FSNAME | grep MDT | cut -d "=" -f2)
581    if [ ! "$old_QUOTA_TYPE" ] || [ "$quota_type" = "$old_QUOTA_TYPE" ]; then
582         return
583    fi
584    quota_save_version $old_QUOTA_TYPE
585 }
586
587 setup_quota(){
588     local mntpt=$1
589
590     # We need:
591     # 1. run quotacheck only if quota is off
592     # 2. save the original quota_type params, restore them after testing
593
594     # Suppose that quota type the same on mds and ost
595     local quota_type=$(quota_type | grep MDT | cut -d "=" -f2)
596     [ ${PIPESTATUS[0]} -eq 0 ] || error "quota_type failed!"
597     echo "[HOST:$HOSTNAME] [old_quota_type:$quota_type] [new_quota_type:$QUOTA_TYPE]"
598     if [ "$quota_type" != "$QUOTA_TYPE" ]; then
599         export old_QUOTA_TYPE=$quota_type
600         quota_save_version $QUOTA_TYPE
601     fi
602
603     local quota_usrs=$QUOTA_USERS
604
605     # get_filesystem_size
606     local disksz=$(lfs df $mntpt | grep "filesystem summary:"  | awk '{print $3}')
607     local blk_soft=$((disksz + 1024))
608     local blk_hard=$((blk_soft + blk_soft / 20)) # Go 5% over
609
610     local Inodes=$(lfs df -i $mntpt | grep "filesystem summary:"  | awk '{print $3}')
611     local i_soft=$Inodes
612     local i_hard=$((i_soft + i_soft / 20))
613
614     echo "Total disk size: $disksz  block-softlimit: $blk_soft block-hardlimit:
615         $blk_hard inode-softlimit: $i_soft inode-hardlimit: $i_hard"
616
617     local cmd
618     for usr in $quota_usrs; do
619         echo "Setting up quota on $client:$mntpt for $usr..."
620         for type in u g; do
621             cmd="$LFS setquota -$type $usr -b $blk_soft -B $blk_hard -i $i_soft -I $i_hard $mntpt"
622             echo "+ $cmd"
623             eval $cmd || error "$cmd FAILED!"
624         done
625         # display the quota status
626         echo "Quota settings for $usr : "
627         $LFS quota -v -u $usr $mntpt || true
628     done
629 }
630
631 zconf_mount() {
632     local client=$1
633     local mnt=$2
634     local OPTIONS=${3:-$MOUNTOPT}
635
636     local device=$MGSNID:/$FSNAME
637     if [ -z "$mnt" -o -z "$FSNAME" ]; then
638         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
639         exit 1
640     fi
641
642     echo "Starting client: $client: $OPTIONS $device $mnt"
643     do_node $client mkdir -p $mnt
644     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
645     set_default_debug_nodes $client
646
647     return 0
648 }
649
650 zconf_umount() {
651     local client=$1
652     local mnt=$2
653     local force
654     local busy
655     local need_kill
656
657     [ "$3" ] && force=-f
658     local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
659     if [ $running -ne 0 ]; then
660         echo "Stopping client $client $mnt (opts:$force)"
661         do_node $client lsof -t $mnt || need_kill=no
662         if [ "x$force" != "x" -a "x$need_kill" != "xno" ]; then
663             pids=$(do_node $client lsof -t $mnt | sort -u);
664             if [ -n $pids ]; then
665                 do_node $client kill -9 $pids || true
666             fi
667         fi
668
669         busy=$(do_node $client "umount $force $mnt 2>&1" | grep -c "busy") || true
670         if [ $busy -ne 0 ] ; then
671             echo "$mnt is still busy, wait one second" && sleep 1
672             do_node $client umount $force $mnt
673         fi
674     fi
675 }
676
677 # nodes is comma list
678 sanity_mount_check_nodes () {
679     local nodes=$1
680     shift
681     local mnts="$@"
682     local mnt
683
684     # FIXME: assume that all cluster nodes run the same os
685     [ "$(uname)" = Linux ] || return 0
686
687     local rc=0
688     for mnt in $mnts ; do
689         do_nodes $nodes "running=\\\$(grep -c $mnt' ' /proc/mounts);
690 mpts=\\\$(mount | grep -c $mnt' ');
691 if [ \\\$running -ne \\\$mpts ]; then
692     echo \\\$(hostname) env are INSANE!;
693     exit 1;
694 fi"
695     [ $? -eq 0 ] || rc=1
696     done
697     return $rc
698 }
699
700 sanity_mount_check_servers () {
701     [ "$CLIENTONLY" ] &&
702         { echo "CLIENTONLY mode, skip mount_check_servers"; return 0; } || true
703     echo Checking servers environments
704
705     # FIXME: modify get_facets to display all facets wo params
706     local facets="$(get_facets OST),$(get_facets MDS),mgs"
707     local node
708     local mntpt
709     local facet
710     for facet in ${facets//,/ }; do
711         node=$(facet_host ${facet})
712         mntpt=$(facet_mntpt $facet)
713         sanity_mount_check_nodes $node $mntpt ||
714             { error "server $node environments are insane!"; return 1; }
715     done
716 }
717
718 sanity_mount_check_clients () {
719     local clients=${1:-$CLIENTS}
720     local mntpt=${2:-$MOUNT}
721     local mntpt2=${3:-$MOUNT2}
722
723     [ -z $clients ] && clients=$(hostname)
724     echo Checking clients $clients environments
725
726     sanity_mount_check_nodes $clients $mntpt $mntpt2 ||
727        error "clients environments are insane!"
728 }
729
730 sanity_mount_check () {
731     sanity_mount_check_servers || return 1
732     sanity_mount_check_clients || return 2
733 }
734
735 # mount clients if not mouted
736 zconf_mount_clients() {
737     local clients=$1
738     local mnt=$2
739     local OPTIONS=${3:-$MOUNTOPT}
740
741     local device=$MGSNID:/$FSNAME
742     if [ -z "$mnt" -o -z "$FSNAME" ]; then
743         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
744         exit 1
745     fi
746
747     echo "Starting client $clients: $OPTIONS $device $mnt"
748
749     do_nodes $clients "
750 running=\\\$(mount | grep -c $mnt' ');
751 rc=0;
752 if [ \\\$running -eq 0 ] ; then
753     mkdir -p $mnt;
754     mount -t lustre $OPTIONS $device $mnt;
755     rc=$?;
756 fi;
757 exit $rc"
758
759     echo "Started clients $clients: "
760     do_nodes $clients "mount | grep $mnt' '"
761
762     set_default_debug_nodes $clients
763
764     return 0
765 }
766
767 zconf_umount_clients() {
768     local clients=$1
769     local mnt=$2
770     local force
771
772     [ "$3" ] && force=-f
773
774     echo "Stopping clients: $clients $mnt (opts:$force)"
775     do_nodes $clients "running=\\\$(grep -c $mnt' ' /proc/mounts);
776 if [ \\\$running -ne 0 ] ; then
777 echo Stopping client \\\$(hostname) $mnt opts:$force;
778 lsof -t $mnt || need_kill=no;
779 if [ "x$force" != "x" -a "x\\\$need_kill" != "xno" ]; then
780     pids=\\\$(lsof -t $mnt | sort -u);
781     if [ -n \\\"\\\$pids\\\" ]; then
782              kill -9 \\\$pids;
783     fi
784 fi;
785 busy=\\\$(umount $force $mnt 2>&1 | grep -c "busy");
786 if [ \\\$busy -ne 0 ] ; then
787     echo "$mnt is still busy, wait one second" && sleep 1;
788     umount $force $mnt;
789 fi
790 fi"
791 }
792
793 shutdown_node () {
794     local node=$1
795     echo + $POWER_DOWN $node
796     $POWER_DOWN $node
797 }
798
799 shutdown_node_hard () {
800     local host=$1
801     local attempts=$SHUTDOWN_ATTEMPTS
802
803     for i in $(seq $attempts) ; do
804         shutdown_node $host
805         sleep 1
806         wait_for_function --quiet "! ping -w 3 -c 1 $host" 5 1 && return 0
807         echo "waiting for $host to fail attempts=$attempts"
808         [ $i -lt $attempts ] || \
809             { echo "$host still pingable after power down! attempts=$attempts" && return 1; }
810     done
811 }
812
813 shutdown_client() {
814     local client=$1
815     local mnt=${2:-$MOUNT}
816     local attempts=3
817
818     if [ "$FAILURE_MODE" = HARD ]; then
819         shutdown_node_hard $client
820     else
821        zconf_umount_clients $client $mnt -f
822     fi
823 }
824
825 facets_on_host () {
826     local host=$1
827     local facets="$(get_facets OST),$(get_facets MDS)"
828     local affected
829
830     combined_mgs_mds || facets="$facets,mgs"
831
832     for facet in ${facets//,/ }; do
833         if [ $(facet_active_host $facet) == $host ]; then
834            affected="$affected $facet"
835         fi
836     done
837
838     echo $(comma_list $affected)
839 }
840
841 facet_up () {
842     local facet=$1
843     local host=${2:-$(facet_host $facet)}
844
845     local label=$(convert_facet2label $facet)
846     do_node $host lctl dl | awk '{print $4}' | grep -q $label
847 }
848
849 facets_up_on_host () {
850     local host=$1
851     local facets=$(facets_on_host $host)
852     local affected_up
853
854     for facet in ${facets//,/ }; do
855         if $(facet_up $facet $host); then
856             affected_up="$affected_up $facet"
857         fi
858     done
859
860     echo $(comma_list $affected_up)
861 }
862
863 shutdown_facet() {
864     local facet=$1
865
866     if [ "$FAILURE_MODE" = HARD ]; then
867         shutdown_node_hard $(facet_active_host $facet)
868     else
869         stop $facet
870     fi
871 }
872
873 reboot_node() {
874     local node=$1
875     echo + $POWER_UP $node
876     $POWER_UP $node
877 }
878
879 remount_facet() {
880     local facet=$1
881
882     stop $facet
883     mount_facet $facet
884 }
885
886 reboot_facet() {
887     local facet=$1
888     if [ "$FAILURE_MODE" = HARD ]; then
889         reboot_node $(facet_active_host $facet)
890     else
891         sleep 10
892     fi
893 }
894
895 boot_node() {
896     local node=$1
897     if [ "$FAILURE_MODE" = HARD ]; then
898        reboot_node $node
899        wait_for_host $node
900     fi
901 }
902
903 facets_hosts () {
904     local facets=$1
905     local hosts
906
907     for facet in ${facets//,/ }; do
908         hosts=$(expand_list $hosts $(facet_host $facet) )
909     done
910
911     echo $hosts
912 }
913
914 _check_progs_installed () {
915     local progs=$@
916     local rc=0
917
918     for prog in $progs; do
919         if ! [ "$(which $prog)"  -o  "${!prog}" ]; then
920            echo $prog missing on $(hostname)
921            rc=1
922         fi
923     done
924     return $rc
925 }
926
927 check_progs_installed () {
928     local nodes=$1
929     shift
930
931     do_rpc_nodes $nodes _check_progs_installed $@
932 }
933
934 node_var_name() {
935     local node=${1//./_}
936     echo __${node//-/_}
937 }
938
939 start_client_load() {
940     local client=$1
941     local load=$2
942     local var=$(node_var_name $client)_load
943     eval export ${var}=$load
944
945     do_node $client "PATH=$PATH MOUNT=$MOUNT ERRORS_OK=$ERRORS_OK \
946 BREAK_ON_ERROR=$BREAK_ON_ERROR \
947 END_RUN_FILE=$END_RUN_FILE \
948 LOAD_PID_FILE=$LOAD_PID_FILE \
949 TESTLOG_PREFIX=$TESTLOG_PREFIX \
950 TESTNAME=$TESTNAME \
951 DBENCH_LIB=$DBENCH_LIB \
952 DBENCH_SRC=$DBENCH_SRC \
953 run_${load}.sh" &
954     local ppid=$!
955     log "Started client load: ${load} on $client"
956
957     # get the children process IDs
958     local pids=$(ps --ppid $ppid -o pid= | xargs)
959     CLIENT_LOAD_PIDS="$CLIENT_LOAD_PIDS $ppid $pids"
960     return 0
961 }
962
963 start_client_loads () {
964     local -a clients=(${1//,/ })
965     local numloads=${#CLIENT_LOADS[@]}
966     local testnum
967
968     for ((nodenum=0; nodenum < ${#clients[@]}; nodenum++ )); do
969         testnum=$((nodenum % numloads))
970         start_client_load ${clients[nodenum]} ${CLIENT_LOADS[testnum]}
971     done
972     # bug 22169: wait the background threads to start
973     sleep 2
974 }
975
976 # only for remote client
977 check_client_load () {
978     local client=$1
979     local var=$(node_var_name $client)_load
980     local TESTLOAD=run_${!var}.sh
981
982     ps auxww | grep -v grep | grep $client | grep -q "$TESTLOAD" || return 1
983
984     # bug 18914: try to connect several times not only when
985     # check ps, but  while check_catastrophe also
986     local tries=3
987     local RC=254
988     while [ $RC = 254 -a $tries -gt 0 ]; do
989         let tries=$tries-1
990         # assume success
991         RC=0
992         if ! check_catastrophe $client; then
993             RC=${PIPESTATUS[0]}
994             if [ $RC -eq 254 ]; then
995                 # FIXME: not sure how long we shuold sleep here
996                 sleep 10
997                 continue
998             fi
999             echo "check catastrophe failed: RC=$RC "
1000             return $RC
1001         fi
1002     done
1003
1004     # We can continue try to connect if RC=254
1005     # Just print the warning about this
1006     if [ $RC = 254 ]; then
1007         echo "got a return status of $RC from do_node while checking catastrophe on $client"
1008     fi
1009
1010     # see if the load is still on the client
1011     tries=3
1012     RC=254
1013     while [ $RC = 254 -a $tries -gt 0 ]; do
1014         let tries=$tries-1
1015         # assume success
1016         RC=0
1017         if ! do_node $client "ps auxwww | grep -v grep | grep -q $TESTLOAD"; then
1018             RC=${PIPESTATUS[0]}
1019             sleep 30
1020         fi
1021     done
1022     if [ $RC = 254 ]; then
1023         echo "got a return status of $RC from do_node while checking (catastrophe and 'ps') the client load on $client"
1024         # see if we can diagnose a bit why this is
1025     fi
1026
1027     return $RC
1028 }
1029 check_client_loads () {
1030    local clients=${1//,/ }
1031    local client=
1032    local rc=0
1033
1034    for client in $clients; do
1035       check_client_load $client
1036       rc=${PIPESTATUS[0]}
1037       if [ "$rc" != 0 ]; then
1038         log "Client load failed on node $client, rc=$rc"
1039         return $rc
1040       fi
1041    done
1042 }
1043
1044 restart_client_loads () {
1045     local clients=${1//,/ }
1046     local expectedfail=${2:-""}
1047     local client=
1048     local rc=0
1049
1050     for client in $clients; do
1051         check_client_load $client
1052         rc=${PIPESTATUS[0]}
1053         if [ "$rc" != 0 -a "$expectedfail" ]; then
1054             local var=$(node_var_name $client)_load
1055             start_client_load $client ${!var}
1056             echo "Restarted client load ${!var}: on $client. Checking ..."
1057             check_client_load $client
1058             rc=${PIPESTATUS[0]}
1059             if [ "$rc" != 0 ]; then
1060                 log "Client load failed to restart on node $client, rc=$rc"
1061                 # failure one client load means test fail
1062                 # we do not need to check other
1063                 return $rc
1064             fi
1065         else
1066             return $rc
1067         fi
1068     done
1069 }
1070
1071 # Start vmstat and save its process ID in a file.
1072 start_vmstat() {
1073     local nodes=$1
1074     local pid_file=$2
1075
1076     [ -z "$nodes" -o -z "$pid_file" ] && return 0
1077
1078     do_nodes $nodes \
1079         "vmstat 1 > $TESTLOG_PREFIX.$TESTNAME.vmstat.\\\$(hostname -s).log \
1080         2>/dev/null </dev/null & echo \\\$! > $pid_file"
1081 }
1082
1083 # Display the nodes on which client loads failed.
1084 print_end_run_file() {
1085     local file=$1
1086     local node
1087
1088     [ -s $file ] || return 0
1089
1090     echo "Found the END_RUN_FILE file: $file"
1091     cat $file
1092
1093     # A client load will stop if it finds the END_RUN_FILE file.
1094     # That does not mean the client load actually failed though.
1095     # The first node in END_RUN_FILE is the one we are interested in.
1096     read node < $file
1097
1098     if [ -n "$node" ]; then
1099         local var=$(node_var_name $node)_load
1100
1101         local prefix=$TESTLOG_PREFIX
1102         [ -n "$TESTNAME" ] && prefix=$prefix.$TESTNAME
1103         local stdout_log=$prefix.run_${!var}_stdout.$node.log
1104         local debug_log=$(echo $stdout_log | sed 's/\(.*\)stdout/\1debug/')
1105
1106         echo "Client load ${!var} failed on node $node:"
1107         echo "$stdout_log"
1108         echo "$debug_log"
1109     fi
1110 }
1111
1112 # Stop the process which had its PID saved in a file.
1113 stop_process() {
1114     local nodes=$1
1115     local pid_file=$2
1116
1117     [ -z "$nodes" -o -z "$pid_file" ] && return 0
1118
1119     do_nodes $nodes "test -f $pid_file &&
1120         { kill -s TERM \\\$(cat $pid_file); rm -f $pid_file; }" || true
1121 }
1122
1123 # Stop all client loads.
1124 stop_client_loads() {
1125     local nodes=${1:-$CLIENTS}
1126     local pid_file=$2
1127
1128     # stop the client loads
1129     stop_process $nodes $pid_file
1130
1131     # clean up the processes that started them
1132     [ -n "$CLIENT_LOAD_PIDS" ] && kill -9 $CLIENT_LOAD_PIDS 2>/dev/null || true
1133 }
1134
1135 # verify that lustre actually cleaned up properly
1136 cleanup_check() {
1137     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
1138         error "LBUG/LASSERT detected"
1139     BUSY=`dmesg | grep -i destruct || true`
1140     if [ "$BUSY" ]; then
1141         echo "$BUSY" 1>&2
1142         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
1143         exit 205
1144     fi
1145
1146     check_mem_leak || exit 204
1147
1148     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
1149         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
1150
1151     if module_loaded lnet || module_loaded libcfs; then
1152         echo "$0: modules still loaded..." 1>&2
1153         /sbin/lsmod 1>&2
1154         return 203
1155     fi
1156     return 0
1157 }
1158
1159 wait_update () {
1160     local node=$1
1161     local TEST=$2
1162     local FINAL=$3
1163     local MAX=${4:-90}
1164
1165         local RESULT
1166         local WAIT=0
1167         local sleep=5
1168         while [ true ]; do
1169             RESULT=$(do_node $node "$TEST")
1170             if [ "$RESULT" == "$FINAL" ]; then
1171                 echo "Updated after $WAIT sec: wanted '$FINAL' got '$RESULT'"
1172                 return 0
1173             fi
1174             [ $WAIT -ge $MAX ] && break
1175             echo "Waiting $((MAX - WAIT)) secs for update"
1176             WAIT=$((WAIT + sleep))
1177             sleep $sleep
1178         done
1179         echo "Update not seen after $MAX sec: wanted '$FINAL' got '$RESULT'"
1180         return 3
1181 }
1182
1183 wait_update_facet () {
1184     local facet=$1
1185     shift
1186     wait_update  $(facet_active_host $facet) "$@"
1187 }
1188
1189 wait_delete_completed_mds() {
1190         [[ $(lustre_version_code mds) -lt $(version_code 2.2.58) ]] &&
1191                 return 0
1192
1193         local MAX_WAIT=${1:-20}
1194         local mds2sync=""
1195         local stime=`date +%s`
1196         local etime
1197         local node
1198         local changes
1199
1200         # find MDS with pending deletions
1201         for node in $(mdts_nodes); do
1202                 changes=$(do_node $node "lctl get_param -n osc.*MDT*.sync_*" \
1203                         2>/dev/null | calc_sum)
1204                 if [ -z "$changes" ] || [ $changes -eq 0 ]; then
1205                         continue
1206                 fi
1207                 mds2sync="$mds2sync $node"
1208         done
1209         if [ "$mds2sync" == "" ]; then
1210                 return
1211         fi
1212         mds2sync=$(comma_list $mds2sync)
1213
1214         # sync MDS transactions
1215         do_nodes $mds2sync "lctl set_param -n osd*.*MD*.force_sync 1"
1216
1217         # wait till all changes are sent and commmitted by OSTs
1218         # for ldiskfs space is released upon execution, but DMU
1219         # do this upon commit
1220
1221         local WAIT=0
1222         while [ "$WAIT" -ne "$MAX_WAIT" ]; do
1223                 changes=$(do_nodes $mds2sync "lctl get_param -n osc.*MDT*.sync_*" \
1224                         | calc_sum)
1225                 #echo "$node: $changes changes on all"
1226                 if [ "$changes" -eq "0" ]; then
1227                         etime=`date +%s`
1228                         #echo "delete took $((etime - stime)) seconds"
1229                         return
1230                 fi
1231                 sleep 1
1232                 WAIT=$(( WAIT + 1))
1233         done
1234
1235         etime=`date +%s`
1236         echo "Delete is not completed in $((etime - stime)) seconds"
1237         do_nodes $mds2sync "lctl get_param osc.*MDT*.sync_*"
1238 }
1239
1240 wait_for_host() {
1241     local hostlist=$1
1242
1243     # we can use "for" here because we are waiting the slowest
1244     for host in ${hostlist//,/ }; do
1245         check_network "$host" 900
1246     done
1247     while ! do_nodes $hostlist hostname  > /dev/null; do sleep 5; done
1248 }
1249
1250 wait_for_facet() {
1251     local facetlist=$1
1252     local hostlist
1253
1254     for facet in ${facetlist//,/ }; do
1255         hostlist=$(expand_list $hostlist $(facet_active_host $facet))
1256     done
1257     wait_for_host $hostlist
1258 }
1259
1260 _wait_recovery_complete () {
1261     local param=$1
1262
1263     # Use default policy if $2 is not passed by caller.
1264     local MAX=${2:-$(max_recovery_time)}
1265
1266     local WAIT=0
1267     local STATUS=
1268
1269     while [ $WAIT -lt $MAX ]; do
1270         STATUS=$(lctl get_param -n $param | grep status)
1271         echo $param $STATUS
1272         [[ $STATUS = "status: COMPLETE" || $STATUS = "status: INACTIVE" ]] && return 0
1273         sleep 5
1274         WAIT=$((WAIT + 5))
1275         echo "Waiting $((MAX - WAIT)) secs for $param recovery done. $STATUS"
1276     done
1277     echo "$param recovery not done in $MAX sec. $STATUS"
1278     return 1
1279 }
1280
1281 wait_recovery_complete () {
1282     local facet=$1
1283
1284     # with an assumption that at_max is the same on all nodes
1285     local MAX=${2:-$(max_recovery_time)}
1286
1287     local facets=$facet
1288     if [ "$FAILURE_MODE" = HARD ]; then
1289         facets=$(facets_on_host $(facet_active_host $facet))
1290     fi
1291     echo affected facets: $facets
1292
1293     # we can use "for" here because we are waiting the slowest
1294     for facet in ${facets//,/ }; do
1295         local var_svc=${facet}_svc
1296         local param="*.${!var_svc}.recovery_status"
1297
1298         local host=$(facet_active_host $facet)
1299         do_rpc_nodes $host _wait_recovery_complete $param $MAX
1300     done
1301 }
1302
1303 wait_mds_ost_sync () {
1304     # just because recovery is done doesn't mean we've finished
1305     # orphan cleanup. Wait for llogs to get synchronized.
1306     echo "Waiting for orphan cleanup..."
1307     # MAX value includes time needed for MDS-OST reconnection
1308     local MAX=$(( TIMEOUT * 2 ))
1309     local WAIT=0
1310     while [ $WAIT -lt $MAX ]; do
1311         local -a sync=($(do_nodes $(comma_list $(osts_nodes)) \
1312             "$LCTL get_param -n obdfilter.*.mds_sync"))
1313         local con=1
1314         for ((i=0; i<${#sync[@]}; i++)); do
1315             [ ${sync[$i]} -eq 0 ] && continue
1316             # there is a not finished MDS-OST synchronization
1317             con=0
1318             break;
1319         done
1320         sleep 2 # increase waiting time and cover statfs cache
1321         [ ${con} -eq 1 ] && return 0
1322         echo "Waiting $WAIT secs for $facet mds-ost sync done."
1323         WAIT=$((WAIT + 2))
1324     done
1325     echo "$facet recovery not done in $MAX sec. $STATUS"
1326     return 1
1327 }
1328
1329 wait_destroy_complete () {
1330         echo "Waiting for local destroys to complete"
1331         # MAX value shouldn't be big as this mean server responsiveness
1332         # never increase this just to make test pass but investigate
1333         # why it takes so long time
1334         local MAX=5
1335         local WAIT=0
1336         while [ $WAIT -lt $MAX ]; do
1337                 local -a RPCs=($($LCTL get_param -n osc.*.destroys_in_flight))
1338                 local con=1
1339                 local i
1340
1341                 for ((i=0; i<${#RPCs[@]}; i++)); do
1342                         [ ${RPCs[$i]} -eq 0 ] && continue
1343                         # there are still some destroy RPCs in flight
1344                         con=0
1345                         break;
1346                 done
1347                 sleep 1
1348                 [ ${con} -eq 1 ] && return 0 # done waiting
1349                 echo "Waiting ${WAIT}s for local destroys to complete"
1350                 WAIT=$((WAIT + 1))
1351         done
1352         echo "Local destroys weren't done in $MAX sec."
1353         return 1
1354 }
1355
1356 wait_delete_completed() {
1357         wait_delete_completed_mds $1 || return $?
1358         wait_destroy_complete
1359 }
1360
1361 wait_exit_ST () {
1362     local facet=$1
1363
1364     local WAIT=0
1365     local INTERVAL=1
1366     local running
1367     # conf-sanity 31 takes a long time cleanup
1368     while [ $WAIT -lt 300 ]; do
1369         running=$(do_facet ${facet} "lsmod | grep lnet > /dev/null && lctl dl | grep ' ST '") || true
1370         [ -z "${running}" ] && return 0
1371         echo "waited $WAIT for${running}"
1372         [ $INTERVAL -lt 64 ] && INTERVAL=$((INTERVAL + INTERVAL))
1373         sleep $INTERVAL
1374         WAIT=$((WAIT + INTERVAL))
1375     done
1376     echo "service didn't stop after $WAIT seconds.  Still running:"
1377     echo ${running}
1378     return 1
1379 }
1380
1381 wait_remote_prog () {
1382    local prog=$1
1383    local WAIT=0
1384    local INTERVAL=5
1385    local rc=0
1386
1387    [ "$PDSH" = "no_dsh" ] && return 0
1388
1389    while [ $WAIT -lt $2 ]; do
1390         running=$(ps uax | grep "$PDSH.*$prog.*$MOUNT" | grep -v grep) || true
1391         [ -z "${running}" ] && return 0 || true
1392         echo "waited $WAIT for: "
1393         echo "$running"
1394         [ $INTERVAL -lt 60 ] && INTERVAL=$((INTERVAL + INTERVAL))
1395         sleep $INTERVAL
1396         WAIT=$((WAIT + INTERVAL))
1397     done
1398     local pids=$(ps  uax | grep "$PDSH.*$prog.*$MOUNT" | grep -v grep | awk '{print $2}')
1399     [ -z "$pids" ] && return 0
1400     echo "$PDSH processes still exists after $WAIT seconds.  Still running: $pids"
1401     # FIXME: not portable
1402     for pid in $pids; do
1403         cat /proc/${pid}/status || true
1404         cat /proc/${pid}/wchan || true
1405         echo "Killing $pid"
1406         kill -9 $pid || true
1407         sleep 1
1408         ps -P $pid && rc=1
1409     done
1410
1411     return $rc
1412 }
1413
1414 clients_up() {
1415     # not every config has many clients
1416     sleep 1
1417     if [ -n "$CLIENTS" ]; then
1418         $PDSH $CLIENTS "stat -f $MOUNT" > /dev/null
1419     else
1420         stat -f $MOUNT > /dev/null
1421     fi
1422 }
1423
1424 client_up() {
1425     local client=$1
1426     # usually checked on particular client or locally
1427     sleep 1
1428     if [ ! -z "$client" ]; then
1429         $PDSH $client "stat -f $MOUNT" > /dev/null
1430     else
1431         stat -f $MOUNT > /dev/null
1432     fi
1433 }
1434
1435 client_evicted() {
1436     ! client_up $1
1437 }
1438
1439 client_reconnect() {
1440     uname -n >> $MOUNT/recon
1441     if [ -z "$CLIENTS" ]; then
1442         df $MOUNT; uname -n >> $MOUNT/recon
1443     else
1444         do_nodes $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
1445     fi
1446     echo Connected clients:
1447     cat $MOUNT/recon
1448     ls -l $MOUNT/recon > /dev/null
1449     rm $MOUNT/recon
1450 }
1451
1452 affected_facets () {
1453     local facet=$1
1454
1455     local host=$(facet_active_host $facet)
1456     local affected=$facet
1457
1458     if [ "$FAILURE_MODE" = HARD ]; then
1459         affected=$(facets_up_on_host $host)
1460     fi
1461     echo $affected
1462 }
1463
1464 facet_failover() {
1465     local facet=$1
1466     local sleep_time=$2
1467     local host=$(facet_active_host $facet)
1468
1469     echo "Failing $facet on node $host"
1470
1471     local affected=$(affected_facets $facet)
1472
1473     shutdown_facet $facet
1474
1475     echo affected facets: $affected
1476
1477     [ -n "$sleep_time" ] && sleep $sleep_time
1478
1479     reboot_facet $facet
1480     clients_up &
1481     local dfpid=$!
1482     RECOVERY_START_TIME=`date +%s`
1483     echo "df pid is $dfpid"
1484
1485     change_active $affected
1486
1487     wait_for_facet $affected
1488     # start mgs first if it is affected
1489     if ! combined_mgs_mds && list_member $affected mgs; then
1490         mount_facet mgs || error "Restart of mgs failed"
1491     fi
1492     # FIXME; has to be changed to mount all facets concurrently
1493     affected=$(exclude_items_from_list $affected mgs)
1494     mount_facets $affected
1495 }
1496
1497 obd_name() {
1498     local facet=$1
1499 }
1500
1501 replay_barrier() {
1502     local facet=$1
1503     do_facet $facet sync
1504     df $MOUNT
1505     local svc=${facet}_svc
1506     do_facet $facet $LCTL --device %${!svc} notransno
1507     do_facet $facet $LCTL --device %${!svc} readonly
1508     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
1509     $LCTL mark "local REPLAY BARRIER on ${!svc}"
1510 }
1511
1512 replay_barrier_nodf() {
1513     local facet=$1    echo running=${running}
1514     do_facet $facet sync
1515     local svc=${facet}_svc
1516     echo Replay barrier on ${!svc}
1517     do_facet $facet $LCTL --device %${!svc} notransno
1518     do_facet $facet $LCTL --device %${!svc} readonly
1519     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
1520     $LCTL mark "local REPLAY BARRIER on ${!svc}"
1521 }
1522
1523 mds_evict_client() {
1524     UUID=`lctl get_param -n mdc.${mds_svc}-mdc-*.uuid`
1525     local mdtdevice=$(get_mds_mdt_device_proc_path)
1526     do_facet mds "lctl set_param -n ${mdtdevice}.${mds_svc}.evict_client $UUID"
1527 }
1528
1529 ost_evict_client() {
1530     UUID=`lctl get_param -n osc.${ost1_svc}-osc-*.uuid`
1531     do_facet ost1 "lctl set_param -n obdfilter.${ost1_svc}.evict_client $UUID"
1532 }
1533
1534 fail() {
1535     facet_failover $* || error "failover: $?"
1536     clients_up || error "post-failover df: $?"
1537 }
1538
1539 fail_nodf() {
1540     local facet=$1
1541     facet_failover $facet
1542 }
1543
1544 fail_abort() {
1545     local facet=$1
1546     stop $facet
1547     change_active $facet
1548     wait_for_facet $facet
1549     mount_facet $facet -o abort_recovery
1550     clients_up || echo "first df failed: $?"
1551     clients_up || error "post-failover df: $?"
1552 }
1553
1554 do_lmc() {
1555     echo There is no lmc.  This is mountconf, baby.
1556     exit 1
1557 }
1558
1559 h2gm () {
1560     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1561         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
1562         echo $ID"@gm"
1563     fi
1564 }
1565
1566 h2name_or_ip() {
1567     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1568         echo $1"@$2"
1569     fi
1570 }
1571
1572 h2ptl() {
1573    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1574        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
1575        if [ -z "$ID" ]; then
1576            echo "Could not get a ptl id for $1..."
1577            exit 1
1578        fi
1579        echo $ID"@ptl"
1580    fi
1581 }
1582 declare -fx h2ptl
1583
1584 h2tcp() {
1585     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1586         echo $1"@tcp"
1587     fi
1588 }
1589 declare -fx h2tcp
1590
1591 h2elan() {
1592     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1593         if type __h2elan >/dev/null 2>&1; then
1594             ID=$(__h2elan $1)
1595         else
1596             ID=`echo $1 | sed 's/[^0-9]*//g'`
1597         fi
1598         echo $ID"@elan"
1599     fi
1600 }
1601 declare -fx h2elan
1602
1603 h2openib() {
1604     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
1605         ID=`echo $1 | sed 's/[^0-9]*//g'`
1606         echo $ID"@openib"
1607     fi
1608 }
1609 declare -fx h2openib
1610
1611 h2o2ib() {
1612     h2name_or_ip "$1" "o2ib"
1613 }
1614 declare -fx h2o2ib
1615
1616 facet_host() {
1617     local facet=$1
1618
1619     [ "$facet" == client ] && echo -n $HOSTNAME && return
1620     varname=${facet}_HOST
1621     if [ -z "${!varname}" ]; then
1622         if [ "${facet:0:3}" == "ost" ]; then
1623             eval ${facet}_HOST=${ost_HOST}
1624         fi
1625     fi
1626     echo -n ${!varname}
1627 }
1628
1629 facet_active() {
1630     local facet=$1
1631     local activevar=${facet}active
1632
1633     if [ -f $TMP/${facet}active ] ; then
1634         source $TMP/${facet}active
1635     fi
1636
1637     active=${!activevar}
1638     if [ -z "$active" ] ; then
1639         echo -n ${facet}
1640     else
1641         echo -n ${active}
1642     fi
1643 }
1644
1645 facet_active_host() {
1646     local facet=$1
1647     local active=`facet_active $facet`
1648     if [ "$facet" == client ]; then
1649         echo $HOSTNAME
1650     else
1651         echo `facet_host $active`
1652     fi
1653 }
1654
1655 change_active() {
1656     local facetlist=$1
1657     local facet
1658
1659     facetlist=$(exclude_items_from_list $facetlist mgs)
1660
1661     for facet in ${facetlist//,/ }; do
1662     local failover=${facet}failover
1663     local host=`facet_host $failover`
1664     [ -z "$host" ] && return
1665
1666     local curactive=`facet_active $facet`
1667     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
1668         eval export ${facet}active=$facet
1669     else
1670         eval export ${facet}active=$failover
1671     fi
1672     # save the active host for this facet
1673     local activevar=${facet}active
1674     echo "$activevar=${!activevar}" > $TMP/$activevar
1675     [[ $facet = mds ]] && combined_mgs_mds && \
1676         echo "mgsactive=${!activevar}" > $TMP/mgsactive
1677     local TO=`facet_active_host $facet`
1678     echo "Failover $facet to $TO"
1679     done
1680 }
1681
1682 do_node() {
1683     local verbose=false
1684     # do not stripe off hostname if verbose, bug 19215
1685     if [ x$1 = x--verbose ]; then
1686         shift
1687         verbose=true
1688     fi
1689
1690     local HOST=$1
1691     shift
1692     local myPDSH=$PDSH
1693     if [ "$HOST" = "$HOSTNAME" ]; then
1694         myPDSH="no_dsh"
1695     elif [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" ]; then
1696         echo "cannot run remote command on $HOST with $myPDSH"
1697         return 128
1698     fi
1699     if $VERBOSE; then
1700         echo "CMD: $HOST $@" >&2
1701         $myPDSH $HOST "$LCTL mark \"$@\"" > /dev/null 2>&1 || :
1702     fi
1703
1704     if [ "$myPDSH" = "rsh" ]; then
1705 # we need this because rsh does not return exit code of an executed command
1706         local command_status="$TMP/cs"
1707         rsh $HOST ":> $command_status"
1708         rsh $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin;
1709                     cd $RPWD; sh -c \"$@\") ||
1710                     echo command failed >$command_status"
1711         [ -n "$($myPDSH $HOST cat $command_status)" ] && return 1 || true
1712         return 0
1713     fi
1714
1715     if $verbose ; then
1716         # print HOSTNAME for myPDSH="no_dsh"
1717         if [[ $myPDSH = no_dsh ]]; then
1718             $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed -e "s/^/${HOSTNAME}: /"
1719         else
1720             $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")"
1721         fi
1722     else
1723         $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
1724     fi
1725     return ${PIPESTATUS[0]}
1726 }
1727
1728 do_nodev() {
1729     do_node --verbose "$@"
1730 }
1731
1732 single_local_node () {
1733    [ "$1" = "$HOSTNAME" ]
1734 }
1735
1736 do_nodes() {
1737     local verbose=false
1738     # do not stripe off hostname if verbose, bug 19215
1739     if [ x$1 = x--verbose ]; then
1740         shift
1741         verbose=true
1742     fi
1743
1744     local rnodes=$1
1745     shift
1746
1747     if single_local_node $rnodes; then
1748         if $verbose; then
1749            do_nodev $rnodes "$@"
1750         else
1751            do_node $rnodes "$@"
1752         fi
1753         return $?
1754     fi
1755
1756     # This is part from do_node
1757     local myPDSH=$PDSH
1758
1759     [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" -o "$myPDSH" = "rsh" ] && \
1760         echo "cannot run remote command on $rnodes with $myPDSH" && return 128
1761
1762     export FANOUT=$(get_node_count "${rnodes//,/ }")
1763     if $VERBOSE; then
1764         echo "CMD: $rnodes $@" >&2
1765         $myPDSH $rnodes "$LCTL mark \"$@\"" > /dev/null 2>&1 || :
1766     fi
1767
1768     # do not replace anything from pdsh output if -N is used
1769     # -N     Disable hostname: prefix on lines of output.
1770     if $verbose || [[ $myPDSH = *-N* ]]; then
1771         $myPDSH $rnodes "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")"
1772     else
1773         $myPDSH $rnodes "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed -re "s/^[^:]*: //g"
1774     fi
1775     return ${PIPESTATUS[0]}
1776 }
1777
1778 do_facet() {
1779     local facet=$1
1780     shift
1781     local HOST=`facet_active_host $facet`
1782     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
1783     do_node $HOST "$@"
1784 }
1785
1786 do_nodesv() {
1787     do_nodes --verbose "$@"
1788 }
1789
1790 add() {
1791     local facet=$1
1792     shift
1793     # make sure its not already running
1794     stop ${facet} -f
1795     rm -f $TMP/${facet}active
1796     [[ $facet = mds ]] && combined_mgs_mds && rm -f $TMP/mgsactive
1797     do_facet ${facet} $MKFS $*
1798 }
1799
1800 ostdevname() {
1801     num=$1
1802     DEVNAME=OSTDEV$num
1803     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
1804     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
1805     echo -n $DEVPTR
1806 }
1807
1808 facet_mntpt() {
1809     local facet=$1
1810     if combined_mgs_mds && [[ $facet = "mgs" ]]  ; then
1811         facet="mds"
1812     fi
1813     local var=${facet}_MOUNT
1814     eval mntpt=${!var:-${MOUNT%/*}/$facet}
1815
1816     echo -n $mntpt
1817 }
1818
1819 ########
1820 ## MountConf setup
1821
1822 stopall() {
1823     # make sure we are using the primary server, so test-framework will
1824     # be able to clean up properly.
1825     activemds=`facet_active mds`
1826     if [ $activemds != "mds" ]; then
1827         fail mds
1828     fi
1829
1830     local clients=$CLIENTS
1831     [ -z $clients ] && clients=$(hostname)
1832
1833     zconf_umount_clients $clients $MOUNT "$*" || true
1834     [ -n "$MOUNT2" ] && zconf_umount_clients $clients $MOUNT2 "$*" || true
1835
1836     [ "$CLIENTONLY" ] && return
1837     # The add fn does rm ${facet}active file, this would be enough
1838     # if we use do_facet <facet> only after the facet added, but
1839     # currently we use do_facet mds in local.sh
1840     stop mds -f
1841     rm -f ${TMP}/mdsactive
1842     combined_mgs_mds && rm -f $TMP/mgsactive
1843     for num in `seq $OSTCOUNT`; do
1844         stop ost$num -f
1845         rm -f $TMP/ost${num}active
1846     done
1847     if ! combined_mgs_mds ; then
1848         stop mgs
1849     fi
1850
1851     return 0
1852 }
1853
1854 cleanup_echo_devs () {
1855     local devs=$($LCTL dl | grep echo | awk '{print $4}')
1856
1857     for dev in $devs; do
1858         $LCTL --device $dev cleanup
1859         $LCTL --device $dev detach
1860     done
1861 }
1862
1863 cleanupall() {
1864     nfs_client_mode && return
1865
1866     stopall $*
1867     cleanup_echo_devs
1868
1869     unload_modules
1870 }
1871
1872 combined_mgs_mds () {
1873     [[ $MDSDEV = $MGSDEV ]] && [[ $mds_HOST = $mgs_HOST ]]
1874 }
1875
1876 facet_number() {
1877     local facet=$1
1878
1879     local number=$(echo -n $facet | sed -e 's/^fs[0-9]\+//' |
1880                    sed -e 's/^[a-z]\+//')
1881
1882     [[ -z $number ]] && number=1
1883
1884     echo -n $number
1885 }
1886
1887 mkfs_opts() {
1888     local facet=$1
1889
1890     local index=$(($(facet_number $facet) - 1))
1891     local tgt=$(echo $facet | tr -d [:digit:] | tr "[:lower:]" "[:upper:]")
1892     local optvar=${tgt}_MKFS_OPTS
1893     local opt=${!optvar}
1894
1895     # FIXME: ! combo  mgs/mds + mgsfailover is not supported yet
1896     [[ $facet = mgs ]] && echo $opt && return
1897
1898     # --index option
1899     [[ $opt != *--index* ]] && opt+=" --index=$index"
1900
1901     # 1.
1902     # --failnode options
1903     local var=${facet}failover_HOST
1904     if [ x"${!var}" != x ] && [ x"${!var}" != x$(facet_host $facet) ] ; then
1905         local failnode=$(h2$NETTYPE ${!var})
1906         failnode="--failnode=$failnode"
1907         # options does not contain
1908         # or contains wrong --failnode=
1909         if [[ $opt != *${failnode}* ]]; then
1910             opt=$(echo $opt | sed 's/--failnode=.* / /')
1911             opt="$opt $failnode"
1912         fi
1913     fi
1914
1915     # 2.
1916     # --mgsnode options
1917     # no additional mkfs mds "--mgsnode" option for this configuration
1918     if [[ $facet = mds ]] && combined_mgs_mds; then
1919         echo $opt
1920         return
1921     fi
1922
1923     # additional mkfs "--mgsnode"
1924     local mgsnode="--mgsnode=$MGSNID"
1925     opt=${opt//$mgsnode }
1926     for nid in ${MGSNID//:/ }; do
1927         local mgsnode="--mgsnode=$nid"
1928         # options does not contain
1929         # --mgsnode=$nid
1930         if [[ $opt != *${mgsnode}" "* ]]; then
1931             opt="$opt --mgsnode=$nid"
1932         fi
1933     done
1934
1935     echo $opt
1936 }
1937
1938 formatall() {
1939     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
1940
1941     stopall
1942     # We need ldiskfs here, may as well load them all
1943     load_modules
1944     [ "$CLIENTONLY" ] && return
1945     echo Formatting mgs, mds, osts
1946     if ! combined_mgs_mds ; then
1947         add mgs $(mkfs_opts mgs) $FSTYPE_OPT --reformat $MGSDEV || exit 10
1948     fi
1949
1950     if $VERBOSE; then
1951         add mds $(mkfs_opts mds) $FSTYPE_OPT --reformat $MDSDEV || exit 10
1952     else
1953         add mds $(mkfs_opts mds) $FSTYPE_OPT --reformat $MDSDEV > /dev/null || exit 10
1954     fi
1955
1956     # the ost-s could have different OST_MKFS_OPTS
1957     # because of different failnode-s
1958     for num in `seq $OSTCOUNT`; do
1959         if $VERBOSE; then
1960             add ost$num $(mkfs_opts ost${num}) $FSTYPE_OPT --reformat `ostdevname $num` || exit 10
1961         else
1962             add ost$num $(mkfs_opts ost${num}) $FSTYPE_OPT --reformat `ostdevname $num` > /dev/null || exit 10
1963         fi
1964     done
1965 }
1966
1967 mount_client() {
1968     grep " $1 " /proc/mounts || zconf_mount $HOSTNAME $*
1969 }
1970
1971 remount_client()
1972 {
1973         zconf_umount `hostname` $1 || error "umount failed"
1974         zconf_mount `hostname` $1 || error "mount failed"
1975 }
1976
1977 writeconf_facet () {
1978     local facet=$1
1979     local dev=$2
1980
1981     do_facet $facet "$TUNEFS --writeconf $dev"
1982 }
1983
1984 writeconf_all () {
1985     writeconf_facet mds $MDSDEV
1986
1987     for num in `seq $OSTCOUNT`; do
1988         DEVNAME=`ostdevname $num`
1989         writeconf_facet ost$num $DEVNAME
1990     done
1991 }
1992
1993 setupall() {
1994     nfs_client_mode && return
1995
1996     sanity_mount_check ||
1997         error "environments are insane!"
1998
1999     load_modules
2000     if [ -z "$CLIENTONLY" ]; then
2001         echo Setup mgs, mdt, osts
2002
2003         echo $WRITECONF | grep -q "writeconf" && \
2004             writeconf_all
2005
2006         if ! combined_mgs_mds ; then
2007             start mgs $MGSDEV $MGS_MOUNT_OPTS
2008         fi
2009
2010         start mds $MDSDEV $MDS_MOUNT_OPTS
2011         # We started mds, now we should set failover variable properly.
2012         # Set mdsfailover_HOST if it is not set (the default failnode).
2013         if [ -z "$mdsfailover_HOST" ]; then
2014            mdsfailover_HOST=$(facet_host mds)
2015         fi
2016
2017         for num in `seq $OSTCOUNT`; do
2018             DEVNAME=`ostdevname $num`
2019             start ost$num $DEVNAME $OST_MOUNT_OPTS
2020
2021             # We started ost$num, now we should set ost${num}failover variable properly.
2022             # Set ost${num}failover_HOST if it is not set (the default failnode).
2023             varname=ost${num}failover_HOST
2024             if [ -z "${!varname}" ]; then
2025                 eval ost${num}failover_HOST=$(facet_host ost${num})
2026             fi
2027
2028         done
2029     fi
2030     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
2031     mount_client $MOUNT
2032     [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT
2033     clients_up
2034
2035     if [ "$MOUNT_2" ]; then
2036         mount_client $MOUNT2
2037         [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT2
2038     fi
2039     sleep 5
2040     init_param_vars
2041 }
2042
2043 mounted_lustre_filesystems() {
2044         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
2045 }
2046
2047 init_facet_vars () {
2048     [ "$CLIENTONLY" ] && return 0
2049     local facet=$1
2050     shift
2051     local device=$1
2052
2053     shift
2054
2055     eval export ${facet}_dev=${device}
2056     eval export ${facet}_opt=\"$@\"
2057
2058     local dev=${facet}_dev
2059     local label=$(do_facet ${facet} "$E2LABEL ${!dev}")
2060     [ -z "$label" ] && echo no label for ${!dev} && exit 1
2061
2062     eval export ${facet}_svc=${label}
2063
2064     local varname=${facet}failover_HOST
2065     if [ -z "${!varname}" ]; then
2066        eval $varname=$(facet_host $facet)
2067     fi
2068
2069     # ${facet}failover_dev is set in cfg file
2070     varname=${facet}failover_dev
2071     if [ -n "${!varname}" ] ; then
2072         eval export ${facet}failover_dev=${!varname}
2073     else
2074         eval export ${facet}failover_dev=$device
2075     fi
2076
2077     # get mount point of already mounted device
2078     # is facet_dev is already mounted then use the real
2079     #  mount point of this facet; otherwise use $(facet_mntpt $facet)
2080     # i.e. ${facet}_MOUNT if specified by user or default
2081     local mntpt=$(do_facet ${facet} cat /proc/mounts | \
2082             awk '"'${!dev}'" == $1 && $3 == "lustre" { print $2 }')
2083     if [ -z $mntpt ]; then
2084         mntpt=$(facet_mntpt $facet)
2085     fi
2086     eval export ${facet}_MOUNT=$mntpt
2087 }
2088
2089 init_facets_vars () {
2090     remote_mds_nodsh ||
2091         init_facet_vars mds $MDSDEV $MDS_MOUNT_OPTS
2092
2093     combined_mgs_mds || init_facet_vars mgs $MGSDEV $MGS_MOUNT_OPTS
2094
2095     remote_ost_nodsh && return
2096
2097     for num in `seq $OSTCOUNT`; do
2098         DEVNAME=`ostdevname $num`
2099         init_facet_vars ost$num $DEVNAME $OST_MOUNT_OPTS
2100     done
2101 }
2102
2103 init_param_vars () {
2104     if ! remote_ost_nodsh && ! remote_mds_nodsh; then
2105         export MDSVER=$(do_facet mds "lctl get_param version" | cut -d. -f1,2)
2106         export OSTVER=$(do_facet ost1 "lctl get_param version" | cut -d. -f1,2)
2107         export CLIVER=$(lctl get_param version | cut -d. -f 1,2)
2108     fi
2109
2110     remote_mds_nodsh ||
2111         TIMEOUT=$(do_facet mds "lctl get_param -n timeout")
2112
2113     log "Using TIMEOUT=$TIMEOUT"
2114
2115     if [ "$ENABLE_QUOTA" ]; then
2116         setup_quota $MOUNT  || return 2
2117     fi
2118 }
2119
2120 nfs_client_mode () {
2121     if [ "$NFSCLIENT" ]; then
2122         echo "NFSCLIENT mode: setup, cleanup, check config skipped"
2123         local clients=$CLIENTS
2124         [ -z $clients ] && clients=$(hostname)
2125
2126         do_nodesv $clients "grep ' '$MOUNT' ' /proc/mounts"
2127         declare -a nfsexport=(`grep ' '$MOUNT' ' /proc/mounts | awk '{print $1}' | awk -F: '{print $1 " "  $2}'`)
2128         if [[ ${#nfsexport[@]} -eq 0 ]]; then
2129                 error_exit NFSCLIENT=$NFSCLIENT mode, but no NFS export found!
2130         fi
2131         do_nodesv ${nfsexport[0]} "df -T  ${nfsexport[1]}"
2132         return
2133     fi
2134     return 1
2135 }
2136
2137 check_config_client () {
2138     local mntpt=$1
2139
2140     local mounted=$(mount | grep " $mntpt ")
2141     if [ "$CLIENTONLY" ]; then
2142         # bug 18021
2143         # CLIENTONLY should not depend on *_HOST settings
2144         local mgc=$($LCTL device_list | awk '/MGC/ {print $4}')
2145         # in theory someone could create a new,
2146         # client-only config file that assumed lustre was already
2147         # configured and didn't set the MGSNID. If MGSNID is not set,
2148         # then we should use the mgs nid currently being used
2149         # as the default value. bug 18021
2150         [[ x$MGSNID = x ]] &&
2151             MGSNID=${mgc//MGC/}
2152
2153         if [[ x$mgc != xMGC$MGSNID ]]; then
2154             if [ "$mgs_HOST" ]; then
2155                 local mgc_ip=$(ping -q -c1 -w1 $mgs_HOST | grep PING | awk '{print $3}' | sed -e "s/(//g" -e "s/)//g")
2156                 [[ x$mgc = xMGC$mgc_ip@$NETTYPE ]] ||
2157                     error_exit "MGSNID=$MGSNID, mounted: $mounted, MGC : $mgc"
2158             fi
2159         fi
2160         return 0
2161     fi
2162
2163     local myMGS_host=$mgs_HOST
2164     if [ "$NETTYPE" = "ptl" ]; then
2165         myMGS_host=$(h2ptl $mgs_HOST | sed -e s/@ptl//)
2166     fi
2167
2168     echo Checking config lustre mounted on $mntpt
2169     local mgshost=$(mount | grep " $mntpt " | awk -F@ '{print $1}')
2170     mgshost=$(echo $mgshost | awk -F: '{print $1}')
2171
2172 #    if [ "$mgshost" != "$myMGS_host" ]; then
2173 #            error_exit "Bad config file: lustre is mounted with mgs $mgshost, but mgs_HOST=$mgs_HOST, NETTYPE=$NETTYPE
2174 #                   Please use correct config or set mds_HOST correctly!"
2175 #    fi
2176
2177 }
2178
2179 check_config_clients () {
2180     local clients=${CLIENTS:-$HOSTNAME}
2181     local mntpt=$1
2182
2183     nfs_client_mode && return
2184
2185     do_rpc_nodes $clients check_config_client $mntpt
2186
2187     sanity_mount_check ||
2188         error "environments are insane!"
2189 }
2190
2191 check_timeout () {
2192     local mdstimeout=$(do_facet mds "lctl get_param -n timeout")
2193     local cltimeout=$(lctl get_param -n timeout)
2194     if [ $mdstimeout -ne $TIMEOUT ] || [ $mdstimeout -ne $cltimeout ]; then
2195         error "timeouts are wrong! mds: $mdstimeout, client: $cltimeout, TIMEOUT=$TIMEOUT"
2196         return 1
2197     fi
2198 }
2199
2200 is_mounted () {
2201     local mntpt=$1
2202     local mounted=$(mounted_lustre_filesystems)
2203
2204     echo $mounted' ' | grep -w -q $mntpt' '
2205 }
2206
2207 is_empty_dir() {
2208     [ $(find $1 -maxdepth 1 -print | wc -l) = 1 ] && return 0
2209     return 1
2210 }
2211
2212 # empty lustre filesystem may have empty directories lost+found and .lustre
2213 is_empty_fs() {
2214     [ $(find $1 -maxdepth 1 -name lost+found -o -name .lustre -prune -o \
2215        -print | wc -l) = 1 ] || return 1
2216     [ ! -d $1/lost+found ] || is_empty_dir $1/lost+found && return 0
2217     [ ! -d $1/.lustre ] || is_empty_dir $1/.lustre && return 0
2218     return 1
2219 }
2220
2221 check_and_setup_lustre() {
2222     nfs_client_mode && return
2223
2224     local MOUNTED=$(mounted_lustre_filesystems)
2225
2226     local do_check=true
2227     # 1.
2228     # both MOUNT and MOUNT2 are not mounted
2229     if ! is_mounted $MOUNT && ! is_mounted $MOUNT2; then
2230         [ "$REFORMAT" ] && formatall
2231         # setupall mounts both MOUNT and MOUNT2 (if MOUNT_2 is set)
2232         setupall
2233         is_mounted $MOUNT || error "NAME=$NAME not mounted"
2234         export I_MOUNTED=yes
2235         do_check=false
2236     # 2.
2237     # MOUNT2 is mounted
2238     elif is_mounted $MOUNT2; then
2239             # 3.
2240             # MOUNT2 is mounted, while MOUNT_2 is not set
2241             if ! [ "$MOUNT_2" ]; then
2242                 cleanup_mount $MOUNT2
2243                 export I_UMOUNTED2=yes
2244
2245             # 4.
2246             # MOUNT2 is mounted, MOUNT_2 is set
2247             else
2248                 # FIXME: what to do if check_config failed?
2249                 # i.e. if:
2250                 # 1) remote client has mounted other Lustre fs ?
2251                 # 2) it has insane env ?
2252                 # let's try umount MOUNT2 on all clients and mount it again:
2253                 if ! check_config_clients $MOUNT2; then
2254                     cleanup_mount $MOUNT2
2255                     restore_mount $MOUNT2
2256                     export I_MOUNTED2=yes
2257                 fi
2258             fi
2259
2260     # 5.
2261     # MOUNT is mounted MOUNT2 is not mounted
2262     elif [ "$MOUNT_2" ]; then
2263         restore_mount $MOUNT2
2264         export I_MOUNTED2=yes
2265     fi
2266
2267     if $do_check; then
2268         # FIXME: what to do if check_config failed?
2269         # i.e. if:
2270         # 1) remote client has mounted other Lustre fs?
2271         # 2) lustre is mounted on remote_clients atall ?
2272         check_config_clients $MOUNT
2273         init_facets_vars
2274         init_param_vars
2275
2276         set_default_debug_nodes $(comma_list $(nodes_list))
2277     fi
2278     if [ "$ONLY" == "setup" ]; then
2279         exit 0
2280     fi
2281 }
2282
2283 restore_mount () {
2284    local clients=${CLIENTS:-$HOSTNAME}
2285    local mntpt=$1
2286
2287    zconf_mount_clients $clients $mntpt
2288 }
2289
2290 cleanup_mount () {
2291     local clients=${CLIENTS:-$HOSTNAME}
2292     local mntpt=$1
2293
2294     zconf_umount_clients $clients $mntpt
2295 }
2296
2297 cleanup_and_setup_lustre() {
2298     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
2299         lctl set_param debug=0 || true
2300         cleanupall
2301         if [ "$ONLY" == "cleanup" ]; then
2302             exit 0
2303         fi
2304     fi
2305     check_and_setup_lustre
2306 }
2307
2308 # Get all of the server target devices from a given server node and type.
2309 get_mnt_devs() {
2310     local node=$1
2311     local type=$2
2312     local obd_type
2313     local devs
2314     local dev
2315
2316     case $type in
2317     mdt) obd_type="osd" ;;
2318     ost) obd_type="obdfilter" ;; # needs to be fixed when OST also uses an OSD
2319     *) echo "invalid server type" && return 1 ;;
2320     esac
2321
2322     devs=$(do_node $node "lctl get_param -n $obd_type.*.mntdev")
2323     for dev in $devs; do
2324         case $dev in
2325         *loop*) do_node $node "losetup $dev" | \
2326                 sed -e "s/.*(//" -e "s/).*//" ;;
2327         *) echo $dev ;;
2328         esac
2329     done
2330 }
2331
2332 # Get all of the server target devices.
2333 get_svr_devs() {
2334     local i
2335
2336     # OST devices
2337     i=0
2338     for node in $(osts_nodes); do
2339         OSTDEVS[i]=$(get_mnt_devs $node ost)
2340         i=$((i + 1))
2341     done
2342 }
2343
2344 # Run e2fsck on MDT or OST device.
2345 run_e2fsck() {
2346     local node=$1
2347     local target_dev=$2
2348     local extra_opts=$3
2349
2350     df > /dev/null    # update statfs data on disk
2351     local cmd="$E2FSCK -d -v -t -t -f -n $extra_opts $target_dev"
2352     echo $cmd
2353     local rc=0
2354     do_node $node $cmd || rc=$?
2355     [ $rc -le $FSCK_MAX_ERR ] || \
2356         error_exit "$cmd returned $rc, should be <= $FSCK_MAX_ERR"
2357     return 0
2358 }
2359
2360 # verify a directory is shared among nodes.
2361 check_shared_dir() {
2362     local dir=$1
2363
2364     [ -z "$dir" ] && return 1
2365     do_rpc_nodes $(comma_list $(nodes_list)) check_logdir $dir
2366     check_write_access $dir || return 1
2367     return 0
2368 }
2369
2370 # Run e2fsck on MDT and OST(s) to generate databases used for lfsck.
2371 generate_db() {
2372     local i
2373     local ostidx
2374     local dev
2375
2376     check_shared_dir $SHARED_DIRECTORY ||
2377         error "$SHARED_DIRECTORY isn't a shared directory"
2378
2379     export MDSDB=$SHARED_DIRECTORY/mdsdb
2380     export OSTDB=$SHARED_DIRECTORY/ostdb
2381
2382     run_e2fsck $(facet_host mds) $MDSDEV "--mdsdb $MDSDB"
2383
2384     i=0
2385     ostidx=0
2386     OSTDB_LIST=""
2387     for node in $(osts_nodes); do
2388         for dev in ${OSTDEVS[i]}; do
2389             run_e2fsck $node $dev "--mdsdb $MDSDB --ostdb $OSTDB-$ostidx"
2390             OSTDB_LIST="$OSTDB_LIST $OSTDB-$ostidx"
2391             ostidx=$((ostidx + 1))
2392         done
2393         i=$((i + 1))
2394     done
2395 }
2396
2397 run_lfsck() {
2398     local cmd="$LFSCK_BIN -c -l --mdsdb $MDSDB --ostdb $OSTDB_LIST $MOUNT"
2399     echo $cmd
2400     local rc=0
2401     eval $cmd || rc=$?
2402     [ $rc -le $FSCK_MAX_ERR ] || \
2403         error_exit "$cmd returned $rc, should be <= $FSCK_MAX_ERR"
2404     echo "lfsck finished with rc=$rc"
2405
2406     rm -rvf $MDSDB* $OSTDB* || true
2407     return 0
2408 }
2409
2410 check_and_cleanup_lustre() {
2411     if [ "$LFSCK_ALWAYS" = "yes" -a "$TESTSUITE" != "lfsck" ]; then
2412         get_svr_devs
2413         generate_db
2414         run_lfsck
2415     fi
2416
2417     if is_mounted $MOUNT; then
2418         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]* ||
2419             error "remove sub-test dirs failed"
2420         [ "$ENABLE_QUOTA" ] && restore_quota_type || true
2421     fi
2422
2423     if [ "$I_UMOUNTED2" = "yes" ]; then
2424         restore_mount $MOUNT2 || error "restore $MOUNT2 failed"
2425     fi
2426
2427     if [ "$I_MOUNTED2" = "yes" ]; then
2428         cleanup_mount $MOUNT2
2429     fi
2430
2431     if [ "$I_MOUNTED" = "yes" ]; then
2432         cleanupall -f || error "cleanup failed"
2433         unset I_MOUNTED
2434     fi
2435 }
2436
2437 #######
2438 # General functions
2439
2440 wait_for_function () {
2441     local quiet=""
2442
2443     # suppress fn both stderr and stdout
2444     if [ "$1" = "--quiet" ]; then
2445         shift
2446         quiet=" > /dev/null 2>&1"
2447     fi
2448
2449     local fn=$1
2450     local max=${2:-900}
2451     local sleep=${3:-5}
2452
2453     local wait=0
2454
2455     while true; do
2456
2457         eval $fn $quiet && return 0
2458
2459         wait=$((wait + sleep))
2460         [ $wait -lt $max ] || return 1
2461         echo waiting $fn, $((max - wait)) secs left ...
2462         sleep $sleep
2463     done
2464 }
2465
2466 check_network() {
2467     local host=$1
2468     local max=$2
2469     local sleep=${3:-5}
2470
2471     echo `date +"%H:%M:%S (%s)"` waiting for $host network $max secs ...
2472     if ! wait_for_function --quiet "ping -c 1 -w 3 $host" $max $sleep ; then
2473         echo "Network not available!"
2474         exit 1
2475     fi
2476
2477     echo `date +"%H:%M:%S (%s)"` network interface is UP
2478 }
2479
2480 no_dsh() {
2481     shift
2482     eval $@
2483 }
2484
2485 comma_list() {
2486     # the sed converts spaces to commas, but leaves the last space
2487     # alone, so the line doesn't end with a comma.
2488     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
2489 }
2490
2491 list_member () {
2492     local list=$1
2493     local item=$2
2494     echo $list | grep -qw $item
2495 }
2496
2497 # list, excluded are the comma separated lists
2498 exclude_items_from_list () {
2499     local list=$1
2500     local excluded=$2
2501     local item
2502
2503     list=${list//,/ }
2504     for item in ${excluded//,/ }; do
2505         list=$(echo " $list " | sed -re "s/\s+$item\s+/ /g")
2506     done
2507     echo $(comma_list $list)
2508 }
2509
2510 # list, expand  are the comma separated lists
2511 expand_list () {
2512     local list=${1//,/ }
2513     local expand=${2//,/ }
2514     local expanded=
2515
2516     expanded=$(for i in $list $expand; do echo $i; done | sort -u)
2517     echo $(comma_list $expanded)
2518 }
2519
2520 testslist_filter () {
2521     local script=$LUSTRE/tests/${TESTSUITE}.sh
2522
2523     [ -f $script ] || return 0
2524
2525     local start_at=$START_AT
2526     local stop_at=$STOP_AT
2527
2528     local var=${TESTSUITE//-/_}_START_AT
2529     [ x"${!var}" != x ] && start_at=${!var}
2530     var=${TESTSUITE//-/_}_STOP_AT
2531     [ x"${!var}" != x ] && stop_at=${!var}
2532
2533     sed -n 's/^test_\([^ (]*\).*/\1/p' $script | \
2534         awk ' BEGIN { if ("'${start_at:-0}'" != 0) flag = 1 }
2535             /^'${start_at}'$/ {flag = 0}
2536             {if (flag == 1) print $0}
2537             /^'${stop_at}'$/ { flag = 1 }'
2538 }
2539
2540 absolute_path() {
2541     (cd `dirname $1`; echo $PWD/`basename $1`)
2542 }
2543
2544 get_facets () {
2545     local types=${1:-"OST MDS MGS"}
2546
2547     local list=""
2548     for entry in $types; do
2549         local name=$(echo $entry | tr "[:upper:]" "[:lower:]")
2550         local type=$(echo $entry | tr "[:lower:]" "[:upper:]")
2551
2552         case $type in
2553             MDS|MGS ) list="$list $name";;
2554                 OST ) for ((i=1; i<=$OSTCOUNT; i++)) do
2555                           list="$list ${name}$i"
2556                       done;;
2557                   * ) error "Invalid facet type"
2558                  exit 1;;
2559         esac
2560     done
2561     echo $(comma_list $list)
2562 }
2563
2564 ##################################
2565 # Adaptive Timeouts funcs
2566
2567 at_is_enabled() {
2568     # only check mds, we assume at_max is the same on all nodes
2569     local at_max=$(do_facet mds "lctl get_param -n at_max")
2570     if [ $at_max -eq 0 ]; then
2571         return 1
2572     else
2573         return 0
2574     fi
2575 }
2576
2577 at_get() {
2578     local facet=$1
2579     local at=$2
2580
2581     # suppose that all ost-s have the same $at value set
2582     [ $facet != "ost" ] || facet=ost1
2583
2584     do_facet $facet "lctl get_param -n $at"
2585 }
2586
2587 at_max_get() {
2588     at_get $1 at_max
2589 }
2590
2591 at_max_set() {
2592     local at_max=$1
2593     shift
2594
2595     local facet
2596     local hosts
2597     for facet in $@; do
2598         if [ $facet == "ost" ]; then
2599             facet=$(get_facets OST)
2600         fi
2601         hosts=$(expand_list $hosts $(facets_hosts $facet))
2602     done
2603
2604     do_nodes $hosts lctl set_param at_max=$at_max
2605 }
2606
2607 ##################################
2608 # OBD_FAIL funcs
2609
2610 drop_request() {
2611 # OBD_FAIL_MDS_ALL_REQUEST_NET
2612     RC=0
2613     do_facet mds lctl set_param fail_loc=0x123
2614     do_facet client "$1" || RC=$?
2615     do_facet mds lctl set_param fail_loc=0
2616     return $RC
2617 }
2618
2619 drop_reply() {
2620 # OBD_FAIL_MDS_ALL_REPLY_NET
2621     RC=0
2622     do_facet mds lctl set_param fail_loc=0x122
2623     do_facet client "$@" || RC=$?
2624     do_facet mds lctl set_param fail_loc=0
2625     return $RC
2626 }
2627
2628 drop_reint_reply() {
2629 # OBD_FAIL_MDS_REINT_NET_REP
2630     RC=0
2631     do_facet mds lctl set_param fail_loc=0x119
2632     do_facet client "$@" || RC=$?
2633     do_facet mds lctl set_param fail_loc=0
2634     return $RC
2635 }
2636
2637 pause_bulk() {
2638 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
2639     RC=0
2640     do_facet ost1 lctl set_param fail_loc=0x214
2641     do_facet client "$1" || RC=$?
2642     do_facet client "sync"
2643     do_facet ost1 lctl set_param fail_loc=0
2644     return $RC
2645 }
2646
2647 drop_ldlm_cancel() {
2648 #define OBD_FAIL_LDLM_CANCEL             0x304
2649     RC=0
2650     do_facet client lctl set_param fail_loc=0x304
2651     do_facet client "$@" || RC=$?
2652     do_facet client lctl set_param fail_loc=0
2653     return $RC
2654 }
2655
2656 drop_bl_callback() {
2657 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
2658     RC=0
2659     do_facet client lctl set_param fail_loc=0x305
2660     do_facet client "$@" || RC=$?
2661     do_facet client lctl set_param fail_loc=0
2662     return $RC
2663 }
2664
2665 drop_ldlm_reply() {
2666 #define OBD_FAIL_LDLM_REPLY              0x30c
2667     RC=0
2668     do_facet mds lctl set_param fail_loc=0x30c
2669     do_facet client "$@" || RC=$?
2670     do_facet mds lctl set_param fail_loc=0
2671     return $RC
2672 }
2673
2674 clear_failloc() {
2675     facet=$1
2676     pause=$2
2677     sleep $pause
2678     echo "clearing fail_loc on $facet"
2679     do_facet $facet "lctl set_param fail_loc=0 2>/dev/null || true"
2680 }
2681
2682 set_nodes_failloc () {
2683     do_nodes $(comma_list $1)  lctl set_param fail_loc=$2
2684 }
2685
2686 cancel_lru_locks() {
2687     $LCTL mark "cancel_lru_locks $1 start"
2688     for d in `lctl get_param -N ldlm.namespaces.*.lru_size | egrep -i $1`; do
2689         $LCTL set_param -n $d=clear
2690     done
2691     $LCTL get_param ldlm.namespaces.*.lock_unused_count | egrep -i $1 | grep -v '=0'
2692     $LCTL mark "cancel_lru_locks $1 stop"
2693 }
2694
2695 default_lru_size()
2696 {
2697         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
2698         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
2699         echo "$DEFAULT_LRU_SIZE"
2700 }
2701
2702 lru_resize_enable()
2703 {
2704     lctl set_param ldlm.namespaces.*$1*.lru_size=0
2705 }
2706
2707 lru_resize_disable()
2708 {
2709     lctl set_param ldlm.namespaces.*$1*.lru_size $(default_lru_size)
2710 }
2711
2712 pgcache_empty() {
2713     local FILE
2714     for FILE in `lctl get_param -N "llite.*.dump_page_cache"`; do
2715         if [ `lctl get_param -n $FILE | wc -l` -gt 1 ]; then
2716             echo there is still data in page cache $FILE ?
2717             lctl get_param -n $FILE
2718             return 1
2719         fi
2720     done
2721     return 0
2722 }
2723
2724 create_fake_exports () {
2725     local facet=$1
2726     local num=$2
2727 #obd_fail_val = num;
2728 #define OBD_FAIL_TGT_FAKE_EXP 0x708
2729     do_facet $facet "lctl set_param fail_val=$num"
2730     do_facet $facet "lctl set_param fail_loc=0x80000708"
2731     fail $facet
2732 }
2733
2734 debugsave() {
2735     DEBUGSAVE="$(lctl get_param -n debug)"
2736 }
2737
2738 debugrestore() {
2739     [ -n "$DEBUGSAVE" ] && lctl set_param debug="${DEBUGSAVE}"
2740     DEBUGSAVE=""
2741 }
2742
2743 ##################################
2744 # Test interface
2745 ##################################
2746
2747 error_noexit() {
2748     local TYPE=${TYPE:-"FAIL"}
2749
2750     local dump=true
2751     # do not dump logs if $1=false
2752     if [ "x$1" = "xfalse" ]; then
2753         shift
2754         dump=false
2755     fi
2756
2757     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
2758
2759     mkdir -p $LOGDIR
2760     # We need to dump the logs on all nodes
2761     if $dump; then
2762         gather_logs $(comma_list $(nodes_list))
2763     fi
2764
2765     debugrestore
2766     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
2767     echo "$@" > $LOGDIR/err
2768 }
2769
2770 exit_status () {
2771     local status=0
2772     local log=$TESTSUITELOG
2773
2774     [ -f "$log" ] && egrep -q .FAIL: $log && status=1
2775     exit $status
2776 }
2777
2778 error() {
2779     error_noexit "$@"
2780     if $FAIL_ON_ERROR;  then
2781         reset_fail_loc
2782         exit 1
2783     fi
2784 }
2785
2786 error_exit() {
2787     error_noexit "$@"
2788     exit 1
2789 }
2790
2791 # use only if we are ignoring failures for this test, bugno required.
2792 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
2793 # e.g. error_ignore 5494 "your message"
2794 error_ignore() {
2795     local TYPE="IGNORE (bz$1)"
2796     shift
2797     error_noexit "$@"
2798 }
2799
2800 skip_env () {
2801     $FAIL_ON_SKIP_ENV && error false $@ || skip $@
2802 }
2803
2804 skip () {
2805         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
2806         [ "$TESTSUITELOG" ] && \
2807                 echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG || true
2808 }
2809
2810 build_test_filter() {
2811     EXCEPT="$EXCEPT $(testslist_filter)"
2812
2813     [ "$ONLY" ] && log "only running test `echo $ONLY`"
2814     for O in $ONLY; do
2815         eval ONLY_${O}=true
2816     done
2817     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
2818         log "excepting tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
2819     [ "$EXCEPT_SLOW" ] && \
2820         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
2821     for E in $EXCEPT $ALWAYS_EXCEPT; do
2822         eval EXCEPT_${E}=true
2823     done
2824     for E in $EXCEPT_SLOW; do
2825         eval EXCEPT_SLOW_${E}=true
2826     done
2827     for G in $GRANT_CHECK_LIST; do
2828         eval GCHECK_ONLY_${G}=true
2829     done
2830 }
2831
2832 basetest() {
2833     if [[ $1 = [a-z]* ]]; then
2834         echo $1
2835     else
2836         echo ${1%%[a-z]*}
2837     fi
2838 }
2839
2840 # print a newline if the last test was skipped
2841 export LAST_SKIPPED=
2842 run_test() {
2843     assert_DIR
2844
2845     export base=`basetest $1`
2846     if [ ! -z "$ONLY" ]; then
2847         testname=ONLY_$1
2848         if [ ${!testname}x != x ]; then
2849             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
2850             run_one_logged $1 "$2"
2851             return $?
2852         fi
2853         testname=ONLY_$base
2854         if [ ${!testname}x != x ]; then
2855             [ "$LAST_SKIPPED" ] && echo "" && LAST_SKIPPED=
2856             run_one_logged $1 "$2"
2857             return $?
2858         fi
2859         LAST_SKIPPED="y"
2860         echo -n "."
2861         return 0
2862     fi
2863     testname=EXCEPT_$1
2864     if [ ${!testname}x != x ]; then
2865         LAST_SKIPPED="y"
2866         TESTNAME=test_$1 skip "skipping excluded test $1"
2867         return 0
2868     fi
2869     testname=EXCEPT_$base
2870     if [ ${!testname}x != x ]; then
2871         LAST_SKIPPED="y"
2872         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
2873         return 0
2874     fi
2875     testname=EXCEPT_SLOW_$1
2876     if [ ${!testname}x != x ]; then
2877         LAST_SKIPPED="y"
2878         TESTNAME=test_$1 skip "skipping SLOW test $1"
2879         return 0
2880     fi
2881     testname=EXCEPT_SLOW_$base
2882     if [ ${!testname}x != x ]; then
2883         LAST_SKIPPED="y"
2884         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
2885         return 0
2886     fi
2887
2888     LAST_SKIPPED=
2889     run_one_logged $1 "$2"
2890
2891     return $?
2892 }
2893
2894 EQUALS="======================================================================"
2895 equals_msg() {
2896     msg="$@"
2897
2898     local suffixlen=$((${#EQUALS} - ${#msg}))
2899     [ $suffixlen -lt 5 ] && suffixlen=5
2900     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
2901 }
2902
2903 log() {
2904     echo "$*"
2905     module_loaded lnet || load_modules
2906
2907     local MSG="$*"
2908     # Get rid of '
2909     MSG=${MSG//\'/\\\'}
2910     MSG=${MSG//\(/\\\(}
2911     MSG=${MSG//\)/\\\)}
2912     MSG=${MSG//\;/\\\;}
2913     MSG=${MSG//\|/\\\|}
2914     MSG=${MSG//\>/\\\>}
2915     MSG=${MSG//\</\\\<}
2916     MSG=${MSG//\//\\\/}
2917     do_nodes $(comma_list $(nodes_list)) $LCTL mark "$MSG" 2> /dev/null || true
2918 }
2919
2920 trace() {
2921         log "STARTING: $*"
2922         strace -o $TMP/$1.strace -ttt $*
2923         RC=$?
2924         log "FINISHED: $*: rc $RC"
2925         return 1
2926 }
2927
2928 complete () {
2929     equals_msg $1 test complete, duration $2 sec
2930     [ -f "$TESTSUITELOG" ] && grep ".IGNORE\|.FAIL" $TESTSUITELOG || true
2931     echo duration $2 >>$TESTSUITELOG
2932 }
2933
2934 pass() {
2935     # Set TEST_STATUS here; will be used for logging the result
2936     if [ -f $LOGDIR/err ]; then
2937         TEST_STATUS="FAIL"
2938     else
2939         TEST_STATUS="PASS"
2940     fi
2941     echo $TEST_STATUS " " $@
2942 }
2943
2944 check_mds() {
2945     FFREE=`lctl get_param -n mds.*.filesfree`
2946     FTOTAL=`lctl get_param -n mds.*.filestotal`
2947     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
2948 }
2949
2950 reset_fail_loc () {
2951     echo -n "Resetting fail_loc on all nodes..."
2952     do_nodes $(comma_list $(nodes_list)) "lctl set_param -n fail_loc=0 2>/dev/null || true"
2953     echo done.
2954 }
2955
2956 run_one() {
2957     testnum=$1
2958     message=$2
2959     tfile=f${testnum}
2960     export tdir=d0.${TESTSUITE}/d${base}
2961     export TESTNAME=test_$testnum
2962     local SAVE_UMASK=`umask`
2963     umask 0022
2964
2965     echo
2966     log "== test $testnum: $message == `date +%H:%M:%S`"
2967     test_${testnum} || error "test_$testnum failed with $?"
2968     cd $SAVE_PWD
2969     reset_fail_loc
2970     check_grant ${testnum} || error "check_grant $testnum failed with $?"
2971     check_catastrophe || error "LBUG/LASSERT detected"
2972     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
2973     unset TESTNAME
2974     unset tdir
2975     umask $SAVE_UMASK
2976     return 0
2977 }
2978
2979 run_one_logged() {
2980     local BEFORE=`date +%s`
2981     local TEST_ERROR
2982     local name=${TESTSUITE}.test_${1}.test_log.$(hostname -s).log
2983     local test_log=$LOGDIR/$name
2984     rm -rf $LOGDIR/err
2985     local SAVE_UMASK=`umask`
2986     umask 0022
2987
2988     log_sub_test_begin test_${1}
2989     (run_one $1 "$2") 2>&1 | tee $test_log
2990     local RC=${PIPESTATUS[0]}
2991
2992     [ $RC -ne 0 ] && [ ! -f $LOGDIR/err ] && \
2993         echo "test_$1 returned $RC" | tee $LOGDIR/err
2994
2995     duration=$((`date +%s` - $BEFORE))
2996     pass "(${duration}s)"
2997     [ -f $LOGDIR/err ] && TEST_ERROR=$(cat $LOGDIR/err)
2998     log_sub_test_end $TEST_STATUS $duration "$RC" "$TEST_ERROR"
2999
3000     if [ -f $LOGDIR/err ]; then
3001         $FAIL_ON_ERROR && exit $RC
3002     fi
3003
3004     umask $SAVE_UMASK
3005
3006     return 0
3007 }
3008
3009 canonical_path() {
3010     (cd `dirname $1`; echo $PWD/`basename $1`)
3011 }
3012
3013 check_grant() {
3014     export base=`basetest $1`
3015     [ "$CHECK_GRANT" == "no" ] && return 0
3016
3017         testname=GCHECK_ONLY_${base}
3018     [ ${!testname}x == x ] && return 0
3019
3020     echo -n "checking grant......"
3021
3022     local clients=$CLIENTS
3023     [ -z $clients ] && clients=$(hostname)
3024
3025     # sync all the data and make sure no pending data on server
3026     do_nodes $clients sync
3027
3028     # get client grant
3029     client_grant=`do_nodes $clients \
3030                     "$LCTL get_param -n osc.${FSNAME}-*.cur_*grant_bytes" | \
3031                     awk '{total += $1} END{print total}'`
3032
3033     # get server grant
3034     server_grant=`do_nodes $(comma_list $(osts_nodes)) \
3035                     "$LCTL get_param -n obdfilter.${FSNAME}-OST*.tot_granted" | \
3036                     awk '{total += $1} END{print total}'`
3037
3038     # check whether client grant == server grant
3039     if [ $client_grant -ne $server_grant ]; then
3040         echo "failed: client:${client_grant} server: ${server_grant}."
3041         do_nodes $(comma_list $(osts_nodes)) \
3042                    "$LCTL get_param obdfilter.${FSNAME}-OST*.tot*"
3043         do_nodes $clients "$LCTL get_param osc.${FSNAME}-*.cur_*_bytes"
3044         return 1
3045     else
3046         echo "pass: client:${client_grant} server: ${server_grant}"
3047     fi
3048 }
3049
3050 ########################
3051 # helper functions
3052
3053 osc_to_ost()
3054 {
3055     osc=$1
3056     ost=`echo $1 | awk -F_ '{print $3}'`
3057     if [ -z $ost ]; then
3058         ost=`echo $1 | sed 's/-osc.*//'`
3059     fi
3060     echo $ost
3061 }
3062
3063 ostuuid_from_index()
3064 {
3065     $LFS osts $2 | sed -ne "/^$1: /s/.* \(.*\) .*$/\1/p"
3066 }
3067
3068 ostname_from_index() {
3069     local uuid=$(ostuuid_from_index $1)
3070     echo ${uuid/_UUID/}
3071 }
3072
3073 index_from_ostuuid()
3074 {
3075     $LFS osts $2 | sed -ne "/${1}/s/\(.*\): .* .*$/\1/p"
3076 }
3077
3078 remote_node () {
3079     local node=$1
3080     [[ $node != $HOSTNAME ]]
3081 }
3082
3083 remote_mds ()
3084 {
3085     remote_node $mds_HOST
3086 }
3087
3088 remote_mds_nodsh()
3089 {
3090     [ "$CLIENTONLY" ] && return 0 || true
3091     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
3092 }
3093
3094 require_dsh_mds()
3095 {
3096         remote_mds_nodsh && echo "SKIP: $TESTSUITE: remote MDS with nodsh" && \
3097             MSKIPPED=1 && return 1
3098         return 0
3099 }
3100
3101 remote_ost ()
3102 {
3103     local node
3104     for node in $(osts_nodes) ; do
3105         remote_node $node && return 0
3106     done
3107     return 1
3108 }
3109
3110 remote_ost_nodsh()
3111 {
3112     [ "$CLIENTONLY" ] && return 0 || true
3113     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
3114 }
3115
3116 require_dsh_ost()
3117 {
3118         remote_ost_nodsh && echo "SKIP: $TESTSUITE: remote OST with nodsh" && \
3119             OSKIPPED=1 && return 1
3120         return 0
3121 }
3122
3123 remote_mgs_nodsh()
3124 {
3125     local MGS
3126     MGS=$(facet_host mgs)
3127     remote_node $MGS && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
3128 }
3129
3130 remote_servers () {
3131     remote_ost && remote_mds
3132 }
3133
3134 local_mode ()
3135 {
3136     remote_mds_nodsh || remote_ost_nodsh || \
3137         $(single_local_node $(comma_list $(nodes_list)))
3138 }
3139
3140 facets_nodes () {
3141     local facets=$1
3142     local nodes
3143     local NODES_sort
3144
3145     for facet in ${facets//,/ }; do
3146         if [ "$FAILURE_MODE" = HARD ]; then
3147             nodes="$nodes $(facet_active_host $facet)"
3148         else
3149             nodes="$nodes $(facet_host $facet)"
3150         fi
3151     done
3152     NODES_sort=$(for i in $nodes; do echo $i; done | sort -u)
3153
3154     echo $NODES_sort
3155 }
3156
3157 osts_nodes () {
3158     local facets=$(get_facets OST)
3159     local nodes=$(facets_nodes $facets)
3160
3161     echo $nodes
3162 }
3163
3164 nodes_list () {
3165     # FIXME. We need a list of clients
3166     local myNODES=$HOSTNAME
3167     local myNODES_sort
3168
3169     # CLIENTS (if specified) contains the local client
3170     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
3171
3172     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
3173         myNODES="$myNODES $(facets_nodes $(get_facets))"
3174     fi
3175
3176     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
3177
3178     echo $myNODES_sort
3179 }
3180
3181 remote_nodes_list () {
3182     local rnodes=$(nodes_list)
3183     rnodes=$(echo " $rnodes " | sed -re "s/\s+$HOSTNAME\s+/ /g")
3184     echo $rnodes
3185 }
3186
3187 init_clients_lists () {
3188     # Sanity check: exclude the local client from RCLIENTS
3189     local rclients=$(echo " $RCLIENTS " | sed -re "s/\s+$HOSTNAME\s+/ /g")
3190
3191     # Sanity check: exclude the dup entries
3192     rclients=$(for i in $rclients; do echo $i; done | sort -u)
3193
3194     local clients="$SINGLECLIENT $HOSTNAME $rclients"
3195
3196     # Sanity check: exclude the dup entries from CLIENTS
3197     # for those configs which has SINGLCLIENT set to local client
3198     clients=$(for i in $clients; do echo $i; done | sort -u)
3199
3200     CLIENTS=`comma_list $clients`
3201     local -a remoteclients=($rclients)
3202     for ((i=0; $i<${#remoteclients[@]}; i++)); do
3203             varname=CLIENT$((i + 2))
3204             eval $varname=${remoteclients[i]}
3205     done
3206
3207     CLIENTCOUNT=$((${#remoteclients[@]} + 1))
3208 }
3209
3210 get_random_entry () {
3211     local rnodes=$1
3212
3213     rnodes=${rnodes//,/ }
3214
3215     local -a nodes=($rnodes)
3216     local num=${#nodes[@]}
3217     local i=$((RANDOM * num * 2 / 65536))
3218
3219     echo ${nodes[i]}
3220 }
3221
3222 client_only () {
3223     [ "$CLIENTONLY" ] || [ "$CLIENTMODSONLY" = yes ]
3224 }
3225
3226 is_patchless ()
3227 {
3228     lctl get_param version | grep -q patchless
3229 }
3230
3231 check_versions () {
3232     [ "$MDSVER" = "$CLIVER" -a "$OSTVER" = "$CLIVER" ]
3233 }
3234
3235 get_node_count() {
3236     local nodes="$@"
3237     echo $nodes | wc -w || true
3238 }
3239
3240 mixed_ost_devs () {
3241     local nodes=$(osts_nodes)
3242     local osscount=$(get_node_count "$nodes")
3243     [ ! "$OSTCOUNT" = "$osscount" ]
3244 }
3245
3246 generate_machine_file() {
3247     local nodes=${1//,/ }
3248     local machinefile=$2
3249     rm -f $machinefile
3250     for node in $nodes; do
3251         echo $node >>$machinefile || \
3252             { echo "can not generate machinefile $machinefile" && return 1; }
3253     done
3254 }
3255
3256 get_stripe () {
3257     local file=$1/stripe
3258     touch $file
3259     $LFS getstripe -v $file || error
3260     rm -f $file
3261 }
3262
3263 nfs_server () {
3264     local dir=$1
3265
3266     local nfsserver=$(awk '"'$dir'" ~ $2 && $3 ~ "nfs" && $2 != "/" \
3267             { print $1 }' /proc/mounts | cut -f 1 -d : | head -1)
3268     echo $nfsserver
3269 }
3270
3271 setstripe_nfsserver () {
3272     local dir=$1
3273     local nfsserver=$(nfs_server $dir)
3274
3275     [ -z $nfsserver ] && echo "$dir is not nfs mounted" && return 1
3276
3277     do_nodev $nfsserver lfs setstripe "$@"
3278 }
3279
3280 check_runas_id_ret() {
3281     local myRC=0
3282     local myRUNAS_UID=$1
3283     local myRUNAS_GID=$2
3284     shift 2
3285     local myRUNAS=$@
3286     if [ -z "$myRUNAS" ]; then
3287         error_exit "myRUNAS command must be specified for check_runas_id"
3288     fi
3289     mkdir $DIR/d0_runas_test
3290     chmod 0755 $DIR
3291     chown $myRUNAS_UID:$myRUNAS_GID $DIR/d0_runas_test
3292     if ! $myRUNAS touch $DIR/d0_runas_test/f$$ ; then
3293         do_nodesv $(comma_list $(nodes_list)) grep -w $myRUNAS_UID /etc/passwd
3294         myRC=1
3295     fi
3296     rm -rf $DIR/d0_runas_test
3297     return $myRC
3298 }
3299
3300 check_runas_id() {
3301     local myRUNAS_UID=$1
3302     local myRUNAS_GID=$2
3303     shift 2
3304     local myRUNAS=$@
3305     check_runas_id_ret $myRUNAS_UID $myRUNAS_GID $myRUNAS || \
3306         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_UID.
3307         Please set RUNAS_ID to some UID which exists on MDS and client or
3308         add user $myRUNAS_UID:$myRUNAS_GID on these nodes."
3309 }
3310
3311 # Run multiop in the background, but wait for it to print
3312 # "PAUSING" to its stdout before returning from this function.
3313 multiop_bg_pause() {
3314     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
3315     FILE=$1
3316     ARGS=$2
3317
3318     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
3319     mkfifo $TMPPIPE
3320
3321     echo "$MULTIOP_PROG $FILE v$ARGS"
3322     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
3323
3324     echo "TMPPIPE=${TMPPIPE}"
3325     local multiop_output
3326
3327     read -t 60 multiop_output < $TMPPIPE
3328     if [ $? -ne 0 ]; then
3329         rm -f $TMPPIPE
3330         return 1
3331     fi
3332     rm -f $TMPPIPE
3333     if [ "$multiop_output" != "PAUSING" ]; then
3334         echo "Incorrect multiop output: $multiop_output"
3335         kill -9 $PID
3336         return 1
3337     fi
3338
3339     return 0
3340 }
3341
3342 rmultiop_start() {
3343     local uniq=$$
3344     if [ x$1 = x--uniq ]; then
3345         shift
3346         uniq=${uniq}_$1
3347         shift
3348     fi
3349
3350     local client=$1
3351     local file=$2
3352     local cmds=$3
3353
3354     # We need to run do_node in bg, because pdsh does not exit
3355     # if child process of run script exists.
3356     # I.e. pdsh does not exit when runmultiop_bg_pause exited,
3357     # because of multiop_bg_pause -> $MULTIOP_PROG &
3358     # By the same reason we need sleep a bit after do_nodes starts
3359     # to let runmultiop_bg_pause start muliop and
3360     # update /tmp/multiop_bg.pid ;
3361     # The rm /tmp/multiop_bg.pid guarantees here that
3362     # we have the updated by runmultiop_bg_pause
3363     # /tmp/multiop_bg.pid file
3364
3365     local pid_file=$TMP/multiop_bg.pid.$uniq
3366     do_node $client "rm -f $pid_file && MULTIOP_PID_FILE=$pid_file LUSTRE= runmultiop_bg_pause $file $cmds" &
3367     local pid=$!
3368     sleep 3
3369     local multiop_pid
3370     multiop_pid=$(do_node $client cat $pid_file)
3371     [ -n "$multiop_pid" ] || error "$client : Can not get multiop_pid from $pid_file "
3372     eval export $(node_var_name $client)_multiop_pid$uniq=$multiop_pid
3373     eval export $(node_var_name $client)_do_node_pid$uniq=$pid
3374     local var=$(node_var_name $client)_multiop_pid$uniq
3375     echo node $client multiop_bg started multiop_pid=${!var}
3376     return $?
3377 }
3378
3379 rmultiop_stop() {
3380     local uniq=$$
3381     if [ x$1 = x--uniq ]; then
3382         shift
3383         uniq=${uniq}_$1
3384         shift
3385     fi
3386
3387     local client=$1
3388     local multiop_pid=$(node_var_name $client)_multiop_pid$uniq
3389     local do_node_pid=$(node_var_name $client)_do_node_pid$uniq
3390
3391     echo "Stopping multiop_pid=${!multiop_pid} (kill ${!multiop_pid} on $client)"
3392     do_node $client kill -USR1 ${!multiop_pid}
3393
3394     wait ${!do_node_pid}
3395 }
3396
3397 do_and_time () {
3398     local cmd=$1
3399     local rc
3400
3401     SECONDS=0
3402     eval '$cmd'
3403
3404     [ ${PIPESTATUS[0]} -eq 0 ] || rc=1
3405
3406     echo $SECONDS
3407     return $rc
3408 }
3409
3410 inodes_available () {
3411     local IFree=$($LFS df -i $MOUNT | grep ^$FSNAME | awk '{print $4}' | sort -un | head -1) || return 1
3412     echo $IFree
3413 }
3414
3415 mdsrate_inodes_available () {
3416     local min_inodes=$(inodes_available)
3417     echo $((min_inodes * 99 / 100))
3418 }
3419
3420 # reset llite stat counters
3421 clear_llite_stats(){
3422         lctl set_param -n llite.*.stats 0
3423 }
3424
3425 # sum llite stat items
3426 calc_llite_stats() {
3427         local res=$(lctl get_param -n llite.*.stats |
3428                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
3429         echo $res
3430 }
3431
3432 calc_sum () {
3433         awk 'BEGIN {s = 0}; {s += $1}; END {print s}'
3434 }
3435
3436 calc_osc_kbytes () {
3437         df $MOUNT > /dev/null
3438         $LCTL get_param -n osc.*[oO][sS][cC][-_]*.$1 | calc_sum
3439 }
3440
3441 # save_lustre_params(node, parameter_mask)
3442 # generate a stream of formatted strings (<node> <param name>=<param value>)
3443 save_lustre_params() {
3444         local s
3445         do_nodesv $1 "lctl get_param $2 | while read s; do echo \\\$s; done"
3446 }
3447
3448 # restore lustre parameters from input stream, produces by save_lustre_params
3449 restore_lustre_params() {
3450         local node
3451         local name
3452         local val
3453         while IFS=" =" read node name val; do
3454                 do_node ${node//:/} "lctl set_param -n $name $val"
3455         done
3456 }
3457
3458 check_catastrophe() {
3459     local rnodes=${1:-$(comma_list $(remote_nodes_list))}
3460     local C=$CATASTROPHE
3461     [ -f $C ] && [ $(cat $C) -ne 0 ] && return 1
3462
3463     if [ $rnodes ]; then
3464         do_nodes $rnodes "rc=\\\$([ -f $C ] && echo \\\$(< $C) || echo 0);
3465 if [ \\\$rc -ne 0 ]; then echo \\\$(hostname): \\\$rc; fi
3466 exit \\\$rc;"
3467     fi
3468 }
3469
3470 # $1 node
3471 # $2 file
3472 # $3 $RUNAS
3473 get_stripe_info() {
3474         local tmp_file
3475
3476         stripe_size=0
3477         stripe_count=0
3478         stripe_index=0
3479         tmp_file=$(mktemp)
3480
3481         do_facet $1 $3 lfs getstripe -v $2 > $tmp_file
3482
3483         stripe_size=`awk '$1 ~ /size/ {print $2}' $tmp_file`
3484         stripe_count=`awk '$1 ~ /count/ {print $2}' $tmp_file`
3485         stripe_index=`awk '$1 ~ /stripe_offset/ {print $2}' $tmp_file`
3486         rm -f $tmp_file
3487 }
3488
3489 mdsrate_cleanup () {
3490     mpi_run -np $1 ${MACHINEFILE_OPTION} $2 ${MDSRATE} --unlink --nfiles $3 \
3491             --dir $4 --filefmt $5 $6
3492 }
3493
3494 delayed_recovery_enabled () {
3495     do_facet mds "lctl get_param -n mds.${mds_svc}.stale_export_age" > /dev/null 2>&1
3496 }
3497
3498 ################################################################################
3499 # The following functions are used to enable interop testing between
3500 # 1.8 and 2.0. The lprocfs layout changed from 1.8 to 2.0 as the followings:
3501 # mds -> mdt
3502 # {blocksize filesfree filestotal fstype kbytesavail kbytesfree kbytestotal mntdev} moved from mds to osd
3503 # mdt lov: fsname-mdtlov -> fsname-MDTXXXX-mdtlov
3504 # mdt osc: fsname-OSTXXXX-osc -> fsname-OSTXXXX-osc-MDTXXXX
3505 ################################################################################
3506
3507 get_lustre_version () {
3508     local node=${1:-"mds"}
3509     do_facet $node $LCTL get_param -n version |  awk '/^lustre:/ {print $2}'
3510 }
3511
3512 get_mds_version_major () {
3513     local version=$(get_lustre_version mds)
3514     echo $version | awk -F. '{print $1}'
3515 }
3516
3517 get_mds_version_minor () {
3518     local version=$(get_lustre_version mds)
3519     echo $version | awk -F. '{print $2}'
3520 }
3521
3522 get_mds_version_patch () {
3523     local version=$(get_lustre_version mds)
3524     echo $version | awk -F. '{print $3}'
3525 }
3526
3527 get_mds_version_fix () {
3528     local version=$(get_lustre_version mds)
3529     echo $version | awk -F. '{print $4}'
3530 }
3531
3532 get_kernel_version () {
3533     local facet=${1:-"mds"}
3534     do_facet $facet uname -r | awk -F "-" '{print $1}'
3535 }
3536
3537 get_mds_kernel_major () {
3538     local version=$(get_kernel_version mds)
3539     echo $version | awk -F. '{print $1"."$2}'
3540 }
3541
3542 get_mds_kernel_minor () {
3543     local version=$(get_kernel_version mds)
3544     echo $version | awk -F. '{print $3}'
3545 }
3546
3547 get_mds_fsstat_proc_path() {
3548     local major=$(get_mds_version_major)
3549     local minor=$(get_mds_version_minor)
3550     if [ $major -le 1 -a $minor -le 8 ] ; then
3551         echo "mds"
3552     else
3553         echo "osd*"
3554     fi
3555 }
3556
3557 get_mds_mntdev_proc_path() {
3558     local fsstat_dev=$(get_mds_fsstat_proc_path)
3559     echo "$fsstat_dev.*.mntdev"
3560 }
3561
3562 get_mdtlov_proc_path() {
3563     local fsname=$1
3564     local major=$(get_mds_version_major)
3565     local minor=$(get_mds_version_minor)
3566     if [ $major -le 1 -a $minor -le 8 ] ; then
3567         echo "${fsname}-mdtlov"
3568     else
3569         echo "${fsname}-MDT0000-mdtlov"
3570     fi
3571 }
3572
3573 get_mdtosc_proc_path() {
3574     local ost=$1
3575     local major=$(get_mds_version_major)
3576     local minor=$(get_mds_version_minor)
3577     if [ $major -le 1 -a $minor -le 8 ] ; then
3578         echo "${ost}-osc"
3579     else
3580         echo "${ost}-osc-MDT0000"
3581     fi
3582 }
3583
3584 get_mds_mdt_device_proc_path() {
3585     local major=$(get_mds_version_major)
3586     local minor=$(get_mds_version_minor)
3587     if [ $major -le 1 -a $minor -le 8 ] ; then
3588         echo "mds"
3589     else
3590         echo "mdt"
3591     fi
3592 }
3593
3594 get_md_name () {
3595     local major=$(get_mds_version_major)
3596     local minor=$(get_mds_version_minor)
3597     if [ $major -le 1 -a $minor -le 8 ] ; then
3598         echo "mdt"
3599     else
3600         echo "mdd"
3601     fi
3602 }
3603
3604 ########################
3605
3606 convert_facet2label() {
3607     local facet=$1
3608
3609     if [ x$facet = xost ]; then
3610        facet=ost1
3611     fi
3612
3613     local varsvc=${facet}_svc
3614
3615     if [ -n ${!varsvc} ]; then
3616         echo ${!varsvc}
3617     else
3618         error "No lablel for $facet!"
3619     fi
3620 }
3621
3622 get_clientosc_proc_path() {
3623     local ost=$1
3624
3625     echo "${1}-osc-*"
3626 }
3627
3628 get_osc_import_name() {
3629     local facet=$1
3630     local ost=$2
3631     local label=$(convert_facet2label $ost)
3632
3633     if [ "$facet" == "mds" ]; then
3634         get_mdtosc_proc_path $label
3635         return 0
3636     fi
3637
3638     get_clientosc_proc_path $label
3639     return 0
3640 }
3641
3642 _wait_import_state () {
3643     local expected=$1
3644     local CONN_PROC=$2
3645     local maxtime=${3:-$(max_recovery_time)}
3646     local CONN_STATE
3647     local i=0
3648
3649     CONN_STATE=$($LCTL get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3650     while [ "${CONN_STATE}" != "${expected}" ]; do
3651         if [ "${expected}" == "DISCONN" ]; then
3652             # for disconn we can check after proc entry is removed
3653             [ "x${CONN_STATE}" == "x" ] && return 0
3654             #  with AT enabled, we can have connect request timeout near of
3655             # reconnect timeout and test can't see real disconnect
3656             [ "${CONN_STATE}" == "CONNECTING" ] && return 0
3657         fi
3658         [ $i -ge $maxtime ] && \
3659             error "can't put import for $CONN_PROC into ${expected} state after $i sec, have ${CONN_STATE}" && \
3660             return 1
3661         sleep 1
3662         CONN_STATE=$($LCTL get_param -n $CONN_PROC 2>/dev/null | cut -f2)
3663         i=$(($i + 1))
3664     done
3665
3666     log "$CONN_PROC in ${CONN_STATE} state after $i sec"
3667     return 0
3668 }
3669
3670 wait_import_state() {
3671     local state=$1
3672     local params=$2
3673     local maxtime=${3:-$(max_recovery_time)}
3674     local param
3675
3676     for param in ${params//,/ }; do
3677         _wait_import_state $state $param $maxtime || return
3678     done
3679 }
3680
3681 # One client request could be timeouted because server was not ready
3682 # when request was sent by client.
3683 # The request timeout calculation details :
3684 # ptl_send_rpc ()
3685 #      /* We give the server rq_timeout secs to process the req, and
3686 #      add the network latency for our local timeout. */
3687 #      request->rq_deadline = request->rq_sent + request->rq_timeout +
3688 #           ptlrpc_at_get_net_latency(request) ;
3689 #
3690 # ptlrpc_connect_import ()
3691 #      request->rq_timeout = INITIAL_CONNECT_TIMEOUT
3692 #
3693 # init_imp_at () ->
3694 #   -> at_init(&at->iat_net_latency, 0, 0) -> iat_net_latency=0
3695 # ptlrpc_at_get_net_latency(request) ->
3696 #       at_get (max (iat_net_latency=0, at_min)) = at_min
3697 #
3698 # i.e.:
3699 # request->rq_timeout + ptlrpc_at_get_net_latency(request) =
3700 # INITIAL_CONNECT_TIMEOUT + at_min
3701 #
3702 # We will use obd_timeout instead of INITIAL_CONNECT_TIMEOUT
3703 # because we can not get this value in runtime,
3704 # the value depends on configure options, and it is not stored in /proc.
3705 # obd_support.h:
3706 # #define CONNECTION_SWITCH_MIN 5U
3707 # #ifndef CRAY_XT3
3708 # #define INITIAL_CONNECT_TIMEOUT max(CONNECTION_SWITCH_MIN,obd_timeout/20)
3709 # #else
3710 # #define INITIAL_CONNECT_TIMEOUT max(CONNECTION_SWITCH_MIN,obd_timeout/2)
3711
3712 request_timeout () {
3713     local facet=$1
3714
3715     # request->rq_timeout = INITIAL_CONNECT_TIMEOUT
3716     local init_connect_timeout=$TIMEOUT
3717     [[ $init_connect_timeout -ge 5 ]] || init_connect_timeout=5
3718
3719     local at_min=$(at_get $facet at_min)
3720
3721     echo $(( init_connect_timeout + at_min ))
3722 }
3723
3724 wait_osc_import_state() {
3725     local facet=$1
3726     local ost_facet=$2
3727     local expected=$3
3728     local ost=$(get_osc_import_name $facet $ost_facet)
3729
3730     local param="osc.${ost}.ost_server_uuid"
3731
3732     # 1. wait the deadline of client 1st request (it could be skipped)
3733     # 2. wait the deadline of client 2nd request
3734     local maxtime=$(( 2 * $(request_timeout $facet)))
3735
3736     if ! do_rpc_nodes $(facet_host $facet) \
3737 _wait_import_state $expected $param $maxtime; then
3738         error "import is not in ${expected} state"
3739         return 1
3740     fi
3741
3742     return 0
3743 }
3744
3745 get_clientmdc_proc_path() {
3746     echo "${1}-mdc-*"
3747 }
3748
3749 do_rpc_nodes () {
3750     local list=$1
3751     shift
3752
3753     # Add paths to lustre tests for 32 and 64 bit systems.
3754     local RPATH="PATH=$RLUSTRE/tests:/usr/lib/lustre/tests:/usr/lib64/lustre/tests:$PATH"
3755     do_nodesv $list "${RPATH} NAME=${NAME} sh rpc.sh $@ "
3756 }
3757
3758 wait_clients_import_state () {
3759     local list=$1
3760     local facet=$2
3761     local expected=$3
3762
3763     local facets=$facet
3764
3765     if [ "$FAILURE_MODE" = HARD ]; then
3766         facets=$(facets_on_host $(facet_active_host $facet))
3767     fi
3768
3769     for facet in ${facets//,/ }; do
3770     local label=$(convert_facet2label $facet)
3771     local proc_path
3772     case $facet in
3773         ost* ) proc_path="osc.$(get_clientosc_proc_path $label).ost_server_uuid" ;;
3774         mds* ) proc_path="mdc.$(get_clientmdc_proc_path $label).mds_server_uuid" ;;
3775         *) error "unknown facet!" ;;
3776     esac
3777     local params=$(expand_list $params $proc_path)
3778     done
3779
3780     if ! do_rpc_nodes $list wait_import_state $expected $params; then
3781         error "import is not in ${expected} state"
3782         return 1
3783     fi
3784 }
3785
3786 destroy_pool_int() {
3787     local ost
3788     local OSTS=$(do_facet mds lctl pool_list $1 | awk '$1 !~ /^Pool:/ {print $1}')
3789     for ost in $OSTS; do
3790         do_facet mgs lctl pool_remove $1 $ost
3791     done
3792     do_facet mgs lctl pool_destroy $1
3793 }
3794
3795 # <fsname>.<poolname> or <poolname>
3796 destroy_pool() {
3797     local fsname=${1%%.*}
3798     local poolname=${1##$fsname.}
3799
3800     [[ x$fsname = x$poolname ]] && fsname=$FSNAME
3801
3802     local RC
3803
3804     pool_list $fsname.$poolname || return $?
3805
3806     destroy_pool_int $fsname.$poolname
3807     RC=$?
3808     [[ $RC -ne 0 ]] && return $RC
3809
3810     wait_update $HOSTNAME "lctl get_param -n lov.$fsname-*.pools.$poolname \
3811       2>/dev/null || echo foo" "foo" || RC=1
3812
3813     if [[ $RC -eq 0 ]]; then
3814         remove_pool_from_list $fsname.$poolname
3815     else
3816         error "destroy pool failed $1"
3817     fi
3818     return $RC
3819 }
3820
3821 destroy_pools () {
3822     local fsname=${1:-$FSNAME}
3823     local poolname
3824     local listvar=${fsname}_CREATED_POOLS
3825
3826     pool_list $fsname
3827
3828     [ x${!listvar} = x ] && return 0
3829
3830     echo destroy the created pools: ${!listvar}
3831     for poolname in ${!listvar//,/ }; do
3832         destroy_pool $fsname.$poolname
3833     done
3834 }
3835
3836 cleanup_pools () {
3837     local fsname=${1:-$FSNAME}
3838     trap 0
3839     destroy_pools $fsname
3840 }
3841
3842 gather_logs () {
3843     local list=$1
3844
3845     local ts=$(date +%s)
3846     local docp=true
3847
3848     if [[ ! -f "$YAML_LOG" ]]; then
3849         # init_logging is not performed before gather_logs,
3850         # so the $LOGDIR needs to be checked here
3851         check_shared_dir $LOGDIR && touch $LOGDIR/shared
3852     fi
3853
3854     # bug 20237, comment 11
3855     # It would also be useful to provide the option
3856     # of writing the file to an NFS directory so it doesn't need to be copied.
3857     [ -f $LOGDIR/shared ] && docp=false
3858
3859     # dump lustre logs, dmesg
3860
3861     prefix="$LOGDIR/${TESTSUITE}.${TESTNAME}"
3862     suffix="$ts.log"
3863     echo "Dumping lctl log to ${prefix}.*.${suffix}"
3864
3865     if [ "$CLIENTONLY" -o "$PDSH" == "no_dsh" ]; then
3866         echo "Dumping logs only on local client."
3867         $LCTL dk > ${prefix}.debug_log.$(hostname -s).${suffix}
3868         dmesg > ${prefix}.dmesg.$(hostname -s).${suffix}
3869         return
3870     fi
3871
3872     do_nodes --verbose $list \
3873         "$LCTL dk > ${prefix}.debug_log.\\\$(hostname -s).${suffix};
3874          dmesg > ${prefix}.dmesg.\\\$(hostname -s).${suffix}"
3875     if $docp; then
3876         do_nodes $list rsync -az "${prefix}.*.${suffix}" $HOSTNAME:$LOGDIR
3877     fi
3878 }
3879
3880 do_ls () {
3881     local mntpt_root=$1
3882     local num_mntpts=$2
3883     local dir=$3
3884     local i
3885     local cmd
3886     local pids
3887     local rc=0
3888
3889     for i in $(seq 0 $num_mntpts); do
3890         cmd="ls -laf ${mntpt_root}$i/$dir"
3891         echo + $cmd;
3892         $cmd > /dev/null &
3893         pids="$pids $!"
3894     done
3895     echo pids=$pids
3896     for pid in $pids; do
3897         wait $pid || rc=$?
3898     done
3899
3900     return $rc
3901 }
3902
3903 # target_start_and_reset_recovery_timer()
3904 #        service_time = at_est2timeout(service_time);
3905 #        service_time += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC +
3906 #                             INITIAL_CONNECT_TIMEOUT);
3907 # CONNECTION_SWITCH_MAX : min(25U, max(CONNECTION_SWITCH_MIN,obd_timeout))
3908 #define CONNECTION_SWITCH_INC 1
3909 #define INITIAL_CONNECT_TIMEOUT max(CONNECTION_SWITCH_MIN,obd_timeout/20)
3910 #define CONNECTION_SWITCH_MIN 5U
3911
3912 max_recovery_time () {
3913     local init_connect_timeout=$(( TIMEOUT / 20 ))
3914     [[ $init_connect_timeout -ge 5 ]] || init_connect_timeout=5
3915
3916     local service_time=$(( $(at_max_get client) + $(( 2 * $(( 25 + 1  + init_connect_timeout)) )) ))
3917
3918     echo $service_time
3919 }
3920
3921 remove_mdt_files() {
3922     local facet=$1
3923     local mdtdev=$2
3924     shift 2
3925     local files="$@"
3926     local mntpt=${MOUNT%/*}/$facet
3927
3928     echo "removing files from $mdtdev on $facet: $files"
3929     mount -t $FSTYPE $MDS_MOUNT_OPTS $mdtdev $mntpt || return $?
3930     rc=0;
3931     for f in $files; do
3932         rm $mntpt/ROOT/$f || { rc=$?; break; }
3933     done
3934     umount -f $mntpt || return $?
3935     return $rc
3936 }
3937
3938 duplicate_mdt_files() {
3939     local facet=$1
3940     local mdtdev=$2
3941     shift 2
3942     local files="$@"
3943     local mntpt=${MOUNT%/*}/$facet
3944
3945     echo "duplicating files on $mdtdev on $facet: $files"
3946     mkdir -p $mntpt || return $?
3947     mount -t $FSTYPE $MDS_MOUNT_OPTS $mdtdev $mntpt || return $?
3948
3949     do_umount() {
3950         trap 0
3951         popd > /dev/null
3952         rm $tmp
3953         umount -f $mntpt
3954     }
3955     trap do_umount EXIT
3956
3957     tmp=$(mktemp $TMP/setfattr.XXXXXXXXXX)
3958     pushd $mntpt/ROOT > /dev/null || return $?
3959     rc=0
3960     for f in $files; do
3961         touch $f.bad || return $?
3962         getfattr -n trusted.lov $f | sed "s#$f#&.bad#" > $tmp
3963         rc=${PIPESTATUS[0]}
3964         [ $rc -eq 0 ] || return $rc
3965         setfattr --restore $tmp || return $?
3966     done
3967     do_umount
3968 }
3969
3970 run_sgpdd () {
3971     local devs=${1//,/ }
3972     shift
3973     local params=$@
3974     local rslt=$TMP/sgpdd_survey
3975
3976     # sgpdd-survey cleanups ${rslt}.* files
3977
3978     local cmd="rslt=$rslt $params scsidevs=\"$devs\" $SGPDDSURVEY"
3979     echo + $cmd
3980     eval $cmd
3981     cat ${rslt}.detail
3982 }
3983
3984 # returns the canonical name for an ldiskfs device
3985 ldiskfs_canon() {
3986         local dev="$1"
3987         local facet="$2"
3988
3989         do_facet $facet "dv=\\\$(lctl get_param -n $dev);
3990 if foo=\\\$(lvdisplay -c \\\$dv 2>/dev/null); then
3991     echo dm-\\\${foo##*:};
3992 else
3993     echo \\\$(basename \\\$dv);
3994 fi;"
3995 }
3996
3997 is_sanity_benchmark() {
3998     local benchmarks="dbench bonnie iozone fsx"
3999     local suite=$1
4000     for b in $benchmarks; do
4001         if [ "$b" == "$suite" ]; then
4002             return 0
4003         fi
4004     done
4005     return 1
4006 }
4007
4008 min_ost_size () {
4009     $LCTL get_param -n osc.*.kbytesavail | sort -n | head -n1
4010 }
4011
4012 check_logdir() {
4013     local dir=$1
4014     # Checking for shared logdir
4015     if [ ! -d $dir ]; then
4016         # Not found. Create local logdir
4017         mkdir -p $dir
4018     else
4019         touch $dir/check_file.$(hostname -s)
4020     fi
4021     return 0
4022 }
4023
4024 check_write_access() {
4025     local dir=$1
4026     local node
4027     local file
4028
4029     for node in $(nodes_list); do
4030         file=$dir/check_file.$(short_hostname $node)
4031         if [[ ! -f $file ]]; then
4032             # Logdir not accessible/writable from this node.
4033             return 1
4034         fi
4035         rm -f $file || return 1
4036     done
4037     return 0
4038 }
4039
4040 init_logging() {
4041     if [[ -n $YAML_LOG ]]; then
4042         return
4043     fi
4044     local SAVE_UMASK=`umask`
4045     umask 0000
4046
4047     export YAML_LOG=${LOGDIR}/results.yml
4048     mkdir -p $LOGDIR
4049     init_clients_lists
4050
4051     # If the yaml log already exists then we will just append to it.
4052     [[ -f $YAML_LOG ]] && return 0
4053
4054     if check_shared_dir $LOGDIR; then
4055         touch $LOGDIR/shared
4056         echo "Logging to shared log directory: $LOGDIR"
4057     else
4058         echo "Logging to local directory: $LOGDIR"
4059     fi
4060
4061     yml_nodes_file $LOGDIR >> $YAML_LOG
4062     yml_results_file >> $YAML_LOG
4063
4064     umask $SAVE_UMASK
4065 }
4066
4067 log_test() {
4068     yml_log_test $1 >> $YAML_LOG
4069 }
4070
4071 log_sub_test() {
4072     yml_log_sub_test $@ >> $YAML_LOG
4073 }
4074
4075 log_test_status() {
4076      yml_log_test_status $@ >> $YAML_LOG
4077 }
4078
4079 log_sub_test_begin() {
4080     yml_log_sub_test_begin $@ >> $YAML_LOG
4081 }
4082
4083 log_sub_test_end() {
4084     yml_log_sub_test_end $@ >> $YAML_LOG
4085 }