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