Whamcloud - gitweb
Branch b1_6
[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 VERBOSE=false
11 export GMNALNID=${GMNALNID:-/usr/sbin/gmlndnid}
12 export CATASTROPHE=${CATASTROPHE:-/proc/sys/lnet/catastrophe}
13 #export PDSH="pdsh -S -Rssh -w"
14
15 # eg, assert_env LUSTRE MDSNODES OSTNODES CLIENTS
16 assert_env() {
17     local failed=""
18     for name in $@; do
19         if [ -z "${!name}" ]; then
20             echo "$0: $name must be set"
21             failed=1
22         fi
23     done
24     [ $failed ] && exit 1 || true
25 }
26
27 usage() {
28     echo "usage: $0 [-r] [-f cfgfile]"
29     echo "       -r: reformat"
30
31     exit
32 }
33
34 print_summary () {
35     [ "$TESTSUITE" == "lfscktest" ] && return 0
36     [ -n "$ONLY" ] && echo "WARNING: ONLY is set to ${ONLY}."
37     local form="%-13s %-17s %s\n"
38     printf "$form" "status" "script" "skipped tests E(xcluded) S(low)"
39     echo "------------------------------------------------------------------------------------"
40     for O in $TESTSUITE_LIST; do
41         local skipped=""
42         local slow=""
43         local o=$(echo $O | tr "[:upper:]" "[:lower:]")
44         o=${o//_/-}
45         o=${o//tyn/tyN}
46         local log=${TMP}/${o}.log 
47         [ -f $log ] && skipped=$(grep excluded $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
48         [ -f $log ] && slow=$(grep SLOW $log | awk '{ printf " %s", $3 }' | sed 's/test_//g')
49         [ "${!O}" = "done" ] && \
50             printf "$form" "Done" "$O" "E=$skipped" && \
51             [ -n "$slow" ] && printf "$form" "-" "-" "S=$slow"
52
53     done
54
55     for O in $TESTSUITE_LIST; do
56         [ "${!O}" = "no" ] && \
57             printf "$form" "Skipped" "$O" ""
58     done
59
60     for O in $TESTSUITE_LIST; do
61         [ "${!O}" = "done" -o "${!O}" = "no" ] || \
62             printf "$form" "UNFINISHED" "$O" ""
63     done
64 }
65
66 init_test_env() {
67     export LUSTRE=`absolute_path $LUSTRE`
68     export TESTSUITE=`basename $0 .sh`
69     export LTESTDIR=${LTESTDIR:-$LUSTRE/../ltest}
70
71     [ -d /r ] && export ROOT=${ROOT:-/r}
72     export TMP=${TMP:-$ROOT/tmp}
73     export TESTSUITELOG=${TMP}/${TESTSUITE}.log
74     export HOSTNAME=${HOSTNAME:-`hostname`}
75
76     export PATH=:$PATH:$LUSTRE/utils:$LUSTRE/tests
77     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
78     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
79     [ ! -f "$LCTL" ] && export LCTL=$(which lctl) 
80     export LFS=${LFS:-"$LUSTRE/utils/lfs"}
81     [ ! -f "$LFS" ] && export LFS=$(which lfs) 
82     export MKFS=${MKFS:-"$LUSTRE/utils/mkfs.lustre"}
83     [ ! -f "$MKFS" ] && export MKFS=$(which mkfs.lustre) 
84     export TUNEFS=${TUNEFS:-"$LUSTRE/utils/tunefs.lustre"}
85     [ ! -f "$TUNEFS" ] && export TUNEFS=$(which tunefs.lustre) 
86     export CHECKSTAT="${CHECKSTAT:-"checkstat -v"} "
87     export FSYTPE=${FSTYPE:-"ldiskfs"}
88     export NAME=${NAME:-local}
89     export LPROC=/proc/fs/lustre
90     export DIR2
91     export AT_MAX_PATH
92
93     if [ "$ACCEPTOR_PORT" ]; then
94         export PORT_OPT="--port $ACCEPTOR_PORT"
95     fi
96
97     # Paths on remote nodes, if different 
98     export RLUSTRE=${RLUSTRE:-$LUSTRE}
99     export RPWD=${RPWD:-$PWD}
100     export I_MOUNTED=${I_MOUNTED:-"no"}
101
102     # command line
103     
104     while getopts "rvf:" opt $*; do 
105         case $opt in
106             f) CONFIG=$OPTARG;;
107             r) REFORMAT=--reformat;;
108             v) VERBOSE=true;;
109             \?) usage;;
110         esac
111     done
112
113     shift $((OPTIND - 1))
114     ONLY=${ONLY:-$*}
115
116     [ "$TESTSUITELOG" ] && rm -f $TESTSUITELOG || true
117
118 }
119
120 case `uname -r` in
121 2.4.*) EXT=".o"; USE_QUOTA=no; [ ! "$CLIENTONLY" ] && FSTYPE=ext3;;
122     *) EXT=".ko"; USE_QUOTA=yes;;
123 esac
124
125 load_module() {
126     module=$1
127     shift
128     BASE=`basename $module $EXT`
129     lsmod | grep -q ${BASE} || \
130       if [ -f ${LUSTRE}/${module}${EXT} ]; then
131         insmod ${LUSTRE}/${module}${EXT} $@
132     else
133         # must be testing a "make install" or "rpm" installation
134         modprobe $BASE $@
135     fi
136 }
137
138 load_modules() {
139     if [ -n "$MODPROBE" ]; then
140         # use modprobe
141     return 0
142     fi
143     if [ "$HAVE_MODULES" = true ]; then
144     # we already loaded
145         return 0
146     fi
147     HAVE_MODULES=true
148
149     echo Loading modules from $LUSTRE
150     load_module ../lnet/libcfs/libcfs
151     [ "$PTLDEBUG" ] && lctl set_param debug=$PTLDEBUG
152     [ "$SUBSYSTEM" ] && lctl set_param subsystem_debug=${SUBSYSTEM# }
153     [ -f /etc/modprobe.conf ] && MODPROBECONF=/etc/modprobe.conf
154     [ -f /etc/modprobe.d/Lustre ] && MODPROBECONF=/etc/modprobe.d/Lustre
155     [ -z "$LNETOPTS" -a -n "$MODPROBECONF" ] && \
156         LNETOPTS=$(awk '/^options lnet/ { print $0}' $MODPROBECONF | sed 's/^options lnet //g')
157     echo "lnet options: '$LNETOPTS'"
158     # note that insmod will ignore anything in modprobe.conf
159     load_module ../lnet/lnet/lnet $LNETOPTS
160     LNETLND=${LNETLND:-"socklnd/ksocklnd"}
161     load_module ../lnet/klnds/$LNETLND
162     load_module lvfs/lvfs
163     load_module obdclass/obdclass
164     load_module ptlrpc/ptlrpc
165     [ "$USE_QUOTA" = "yes" ] && load_module quota/lquota
166     load_module mdc/mdc
167     load_module osc/osc
168     load_module lov/lov
169     load_module mgc/mgc
170     if [ -z "$CLIENTONLY" ] && [ -z "$CLIENTMODSONLY" ]; then
171         load_module mgs/mgs
172         load_module mds/mds
173         grep -q crc16 /proc/kallsyms || { modprobe crc16 2>/dev/null || true; }
174         [ "$FSTYPE" = "ldiskfs" ] && load_module ../ldiskfs/ldiskfs/ldiskfs
175         load_module lvfs/fsfilt_$FSTYPE
176         load_module ost/ost
177         load_module obdfilter/obdfilter
178     fi
179
180     load_module llite/lustre
181     load_module llite/llite_lloop
182     rm -f $TMP/ogdb-$HOSTNAME
183     OGDB=$TMP
184     [ -d /r ] && OGDB="/r/tmp"
185     $LCTL modules > $OGDB/ogdb-$HOSTNAME
186     # 'mount' doesn't look in $PATH, just sbin
187     [ -f $LUSTRE/utils/mount.lustre ] && cp $LUSTRE/utils/mount.lustre /sbin/. || true
188 }
189
190 RMMOD=rmmod
191 if [ `uname -r | cut -c 3` -eq 4 ]; then
192     RMMOD="modprobe -r"
193 fi
194
195 wait_for_lnet() {
196     local UNLOADED=0
197     local WAIT=0
198     local MAX=60
199     MODULES=$($LCTL modules | awk '{ print $2 }')
200     while [ -n "$MODULES" ]; do
201     sleep 5
202     $RMMOD $MODULES >/dev/null 2>&1 || true
203     MODULES=$($LCTL modules | awk '{ print $2 }')
204         if [ -z "$MODULES" ]; then
205         return 0
206         else
207             WAIT=$((WAIT + 5))
208             echo "waiting, $((MAX - WAIT)) secs left"
209         fi
210         if [ $WAIT -eq $MAX ]; then
211             echo "LNET modules $MODULES will not unload"
212         lsmod
213             return 3
214         fi
215     done
216 }
217
218 unload_modules() {
219     wait_exit_ST client # bug 12845
220
221     lsmod | grep libcfs > /dev/null && $LCTL dl
222     local MODULES=$($LCTL modules | awk '{ print $2 }' | grep -v libcfs) || true
223     $RMMOD $MODULES > /dev/null 2>&1 || true
224      # do it again, in case we tried to unload ksocklnd too early
225     MODULES=$($LCTL modules | awk '{ print $2 }' | grep -v libcfs) || true
226     [ -n "$MODULES" ] && $RMMOD $MODULES > /dev/null 2>&1 || true
227     lsmod | grep libcfs > /dev/null && $LCTL dk $TMP/debug
228     $RMMOD libcfs
229     MODULES=$($LCTL modules | awk '{ print $2 }')
230     if [ -n "$MODULES" ]; then
231         echo "Modules still loaded: "
232         echo $MODULES 
233         if [ "$(lctl dl)" ]; then
234             echo "Lustre still loaded"
235             lctl dl || true
236             lsmod
237             return 2
238         else
239             echo "Lustre stopped but LNET is still loaded, waiting..."
240             wait_for_lnet || return 3
241         fi
242     fi
243     HAVE_MODULES=false
244
245     LEAK_LUSTRE=$(dmesg | tail -n 30 | grep "obd mem.*leaked" || true)
246     LEAK_PORTALS=$(dmesg | tail -n 20 | grep "Portals memory leaked" || true)
247     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
248         echo "$LEAK_LUSTRE" 1>&2
249         echo "$LEAK_PORTALS" 1>&2
250         mv $TMP/debug $TMP/debug-leak.`date +%s` || true
251         echo "Memory leaks detected"
252         [ -n "$IGNORE_LEAK" ] && echo "ignoring leaks" && return 0
253         return 254
254     fi
255     echo "modules unloaded."
256     return 0
257 }
258
259 # Facet functions
260 mount_facet() {
261     local facet=$1
262     shift
263     local dev=${facet}_dev
264     local opt=${facet}_opt
265     echo "Starting ${facet}: ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}"
266     do_facet ${facet} mount -t lustre ${!opt} $@ ${!dev} ${MOUNT%/*}/${facet}     
267     RC=${PIPESTATUS[0]}
268     if [ $RC -ne 0 ]; then
269         echo "mount -t lustre $@ ${device} ${MOUNT%/*}/${facet}" 
270         echo "Start of ${device} on ${facet} failed ${RC}"
271     else 
272         do_facet ${facet} "lctl set_param debug=$PTLDEBUG; \
273             lctl set_param subsystem_debug=${SUBSYSTEM# }; \
274             lctl set_param debug_mb=${DEBUG_SIZE}; \
275             sync"
276     fi
277     return $RC
278 }
279
280 # start facet device options 
281 start() {
282     facet=$1
283     shift
284     device=$1
285     shift
286     eval export ${facet}_dev=${device}
287     eval export ${facet}_opt=\"$@\"
288     do_facet ${facet} mkdir -p ${MOUNT%/*}/${facet}
289     mount_facet ${facet}
290     RC=$?
291     if [ $RC -eq 0 ]; then
292         label=$(do_facet ${facet} "e2label ${device}")
293         [ -z "$label" ] && echo no label for ${device} && exit 1
294         eval export ${facet}_svc=${label}
295         echo Started ${label}
296     fi
297     return $RC
298 }
299
300 stop() {
301     local running
302     facet=$1
303     shift
304     HOST=`facet_active_host $facet`
305     [ -z $HOST ] && echo stop: no host for $facet && return 0
306
307     running=$(do_facet ${facet} "grep -c ${MOUNT%/*}/${facet}' ' /proc/mounts") || true
308     if [ ${running} -ne 0 ]; then
309         echo "Stopping ${MOUNT%/*}/${facet} (opts:$@)"
310         do_facet ${facet} umount -d $@ ${MOUNT%/*}/${facet}
311     fi
312
313     # umount should block, but we should wait for unrelated obd's
314     # like the MGS or MGC to also stop.
315
316     wait_exit_ST ${facet}
317 }
318
319 zconf_mount() {
320     local OPTIONS
321     local client=$1
322     local mnt=$2
323     # Only supply -o to mount if we have options
324     if [ -n "$MOUNTOPT" ]; then
325         OPTIONS="-o $MOUNTOPT"
326     fi
327     local device=$MGSNID:/$FSNAME
328     if [ -z "$mnt" -o -z "$FSNAME" ]; then
329         echo Bad zconf mount command: opt=$OPTIONS dev=$device mnt=$mnt
330         exit 1
331     fi
332
333     echo "Starting client: $client: $OPTIONS $device $mnt" 
334     do_node $client mkdir -p $mnt
335     do_node $client mount -t lustre $OPTIONS $device $mnt || return 1
336
337     do_node $client "lctl set_param debug=$PTLDEBUG;
338         lctl set_param subsystem_debug=${SUBSYSTEM# };
339         lctl set_param debug_mb=${DEBUG_SIZE}"
340     [ -d /r ] && $LCTL modules > /r/tmp/ogdb-$HOSTNAME
341     return 0
342 }
343
344 zconf_umount() {
345     local client=$1
346     local mnt=$2
347     [ "$3" ] && force=-f
348     local running=$(do_node $client "grep -c $mnt' ' /proc/mounts") || true
349     if [ $running -ne 0 ]; then
350         echo "Stopping client $client $mnt (opts:$force)"
351         lsof | grep "$mnt" || true
352         do_node $client umount $force $mnt
353     fi
354 }
355
356 zconf_mount_clients() {
357     local clients=$1
358     local mnt=$2
359
360     echo "Mounting clients: $clients"
361     local client
362     for client in ${clients//,/ }; do
363         zconf_mount $client $mnt  || true
364     done
365 }
366
367 zconf_umount_clients() {
368     local clients=$1
369     local mnt=$2
370     [ "$3" ] && force=-f
371
372     echo "Umounting clients: $clients"
373     local client
374     for client in ${clients//,/ }; do
375         zconf_umount $client $mnt $force || true
376     done
377 }
378
379 shutdown_facet() {
380     facet=$1
381     if [ "$FAILURE_MODE" = HARD ]; then
382         $POWER_DOWN `facet_active_host $facet`
383         sleep 2 
384     elif [ "$FAILURE_MODE" = SOFT ]; then
385         stop $facet
386     fi
387 }
388
389 reboot_facet() {
390     facet=$1
391     if [ "$FAILURE_MODE" = HARD ]; then
392         $POWER_UP `facet_active_host $facet`
393     else
394         sleep 10
395     fi
396 }
397
398 # verify that lustre actually cleaned up properly
399 cleanup_check() {
400     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
401         error "LBUG/LASSERT detected"
402     BUSY=`dmesg | grep -i destruct || true`
403     if [ "$BUSY" ]; then
404         echo "$BUSY" 1>&2
405         [ -e $TMP/debug ] && mv $TMP/debug $TMP/debug-busy.`date +%s`
406         exit 205
407     fi
408     LEAK_LUSTRE=`dmesg | tail -n 30 | grep "obd mem.*leaked" || true`
409     LEAK_PORTALS=`dmesg | tail -n 20 | grep "Portals memory leaked" || true`
410     if [ "$LEAK_LUSTRE" -o "$LEAK_PORTALS" ]; then
411         echo "$0: $LEAK_LUSTRE" 1>&2
412         echo "$0: $LEAK_PORTALS" 1>&2
413         echo "$0: Memory leak(s) detected..." 1>&2
414         mv $TMP/debug $TMP/debug-leak.`date +%s`
415         exit 204
416     fi
417
418     [ "`lctl dl 2> /dev/null | wc -l`" -gt 0 ] && lctl dl && \
419         echo "$0: lustre didn't clean up..." 1>&2 && return 202 || true
420
421     if [ "`/sbin/lsmod 2>&1 | egrep 'lnet|libcfs'`" ]; then
422         echo "$0: modules still loaded..." 1>&2
423         /sbin/lsmod 1>&2
424         return 203
425     fi
426     return 0
427 }
428
429 wait_delete_completed () {
430     local TOTALPREV=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
431             $LPROC/osc/*/kbytesavail`
432
433     local WAIT=0
434     local MAX_WAIT=20
435     while [ "$WAIT" -ne "$MAX_WAIT" ]; do
436         sleep 1
437         TOTAL=`awk 'BEGIN{total=0}; {total+=$1}; END{print total}' \
438             $LPROC/osc/*/kbytesavail`
439         [ "$TOTAL" -eq "$TOTALPREV" ] && break
440         echo "Waiting delete completed ... prev: $TOTALPREV current: $TOTAL "
441         TOTALPREV=$TOTAL
442         WAIT=$(( WAIT + 1))
443     done
444     echo "Delete completed."
445 }
446
447 wait_for_host() {
448     HOST=$1
449     check_network "$HOST" 900
450     while ! do_node $HOST "ls -d $LUSTRE " > /dev/null; do sleep 5; done
451 }
452
453 wait_for() {
454     facet=$1
455     HOST=`facet_active_host $facet`
456     wait_for_host $HOST
457 }
458
459 wait_mds_recovery_done () {
460     local timeout=`do_facet mds lctl get_param  -n timeout`
461 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2)
462 # as we are in process of changing obd_timeout in different ways
463 # let's set MAX longer than that
464     MAX=$(( timeout * 4 ))
465     WAIT=0
466     while [ $WAIT -lt $MAX ]; do
467         STATUS=`do_facet mds grep status /proc/fs/lustre/mds/*-MDT*/recovery_status`
468         echo $STATUS | grep COMPLETE && return 0
469         sleep 5
470         WAIT=$((WAIT + 5))
471         echo "Waiting $(($MAX - $WAIT)) secs for MDS recovery done"
472     done
473     echo "MDS recovery not done in $MAX sec"
474     return 1            
475 }
476
477 wait_exit_ST () {
478     local facet=$1
479
480     local WAIT=0
481     local INTERVAL=1
482     # conf-sanity 31 takes a long time cleanup
483     while [ $WAIT -lt 300 ]; do
484         running=$(do_facet ${facet} "lsmod | grep lnet > /dev/null && lctl dl | grep ' ST '") || true
485         [ -z "${running}" ] && return 0
486         echo "waited $WAIT for${running}"
487         [ $INTERVAL -lt 64 ] && INTERVAL=$((INTERVAL + INTERVAL))
488         sleep $INTERVAL
489         WAIT=$((WAIT + INTERVAL))
490     done
491     echo "service didn't stop after $WAIT seconds.  Still running:"
492     echo ${running}
493     return 1
494 }
495
496 client_df() {
497     # not every config has many clients
498     if [ -n "$CLIENTS" ]; then
499         $PDSH $CLIENTS "df $MOUNT" > /dev/null
500     else
501         df $MOUNT > /dev/null
502     fi
503 }
504
505 client_reconnect() {
506     uname -n >> $MOUNT/recon
507     if [ ! -z "$CLIENTS" ]; then
508         $PDSH $CLIENTS "df $MOUNT; uname -n >> $MOUNT/recon" > /dev/null
509     fi
510     echo Connected clients:
511     cat $MOUNT/recon
512     ls -l $MOUNT/recon > /dev/null
513     rm $MOUNT/recon
514 }
515
516 facet_failover() {
517     facet=$1
518     echo "Failing $facet on node `facet_active_host $facet`"
519     shutdown_facet $facet
520     reboot_facet $facet
521     client_df &
522     DFPID=$!
523     echo "df pid is $DFPID"
524     change_active $facet
525     TO=`facet_active_host $facet`
526     echo "Failover $facet to $TO"
527     wait_for $facet
528     mount_facet $facet || error "Restart of $facet failed"
529 }
530
531 obd_name() {
532     local facet=$1
533 }
534
535 replay_barrier() {
536     local facet=$1
537     do_facet $facet sync
538     df $MOUNT
539     local svc=${facet}_svc
540     do_facet $facet $LCTL --device %${!svc} readonly
541     do_facet $facet $LCTL --device %${!svc} notransno
542     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
543     $LCTL mark "local REPLAY BARRIER on ${!svc}"
544 }
545
546 replay_barrier_nodf() {
547     local facet=$1    echo running=${running}
548     do_facet $facet sync
549     local svc=${facet}_svc
550     echo Replay barrier on ${!svc}
551     do_facet $facet $LCTL --device %${!svc} readonly
552     do_facet $facet $LCTL --device %${!svc} notransno
553     do_facet $facet $LCTL mark "$facet REPLAY BARRIER on ${!svc}"
554     $LCTL mark "local REPLAY BARRIER on ${!svc}"
555 }
556
557 mds_evict_client() {
558     UUID=`cat /proc/fs/lustre/mdc/${mds_svc}-mdc-*/uuid`
559     do_facet mds "echo $UUID > /proc/fs/lustre/mds/${mds_svc}/evict_client"
560 }
561
562 ost_evict_client() {
563     UUID=`cat /proc/fs/lustre/osc/${ost1_svc}-osc-*/uuid`
564     do_facet ost1 "echo $UUID > /proc/fs/lustre/obdfilter/${ost1_svc}/evict_client"
565 }
566
567 fail() {
568     facet_failover $* || error "failover: $?"
569     df $MOUNT || error "post-failover df: $?"
570 }
571
572 fail_nodf() {
573     local facet=$1
574     facet_failover $facet
575 }
576
577 fail_abort() {
578     local facet=$1
579     stop $facet
580     change_active $facet
581     mount_facet $facet -o abort_recovery
582     df $MOUNT || echo "first df failed: $?"
583     sleep 1
584     df $MOUNT || error "post-failover df: $?"
585 }
586
587 do_lmc() {
588     echo There is no lmc.  This is mountconf, baby.
589     exit 1
590 }
591
592 h2gm () {
593     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
594         ID=`$PDSH $1 $GMNALNID -l | cut -d\  -f2`
595         echo $ID"@gm"
596     fi
597 }
598
599 h2ptl() {
600    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
601        ID=`xtprocadmin -n $1 2>/dev/null | egrep -v 'NID' | awk '{print $1}'`
602        if [ -z "$ID" ]; then
603            echo "Could not get a ptl id for $1..."
604            exit 1
605        fi
606        echo $ID"@ptl"
607    fi
608 }
609 declare -fx h2ptl
610
611 h2tcp() {
612     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
613         echo $1"@tcp" 
614     fi
615 }
616 declare -fx h2tcp
617
618 h2elan() {
619     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
620         if type __h2elan >/dev/null 2>&1; then
621             ID=$(__h2elan $1)
622         else
623             ID=`echo $1 | sed 's/[^0-9]*//g'`
624         fi
625         echo $ID"@elan"
626     fi
627 }
628 declare -fx h2elan
629
630 h2openib() {
631     if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
632         ID=`echo $1 | sed 's/[^0-9]*//g'`
633         echo $ID"@openib"
634     fi
635 }
636 declare -fx h2openib
637
638 facet_host() {
639     local facet=$1
640     varname=${facet}_HOST
641     if [ -z "${!varname}" ]; then
642         if [ "${facet:0:3}" == "ost" ]; then
643             eval ${facet}_HOST=${ost_HOST}
644         fi
645     fi
646     echo -n ${!varname}
647 }
648
649 facet_active() {
650     local facet=$1
651     local activevar=${facet}active
652
653     if [ -f ./${facet}active ] ; then
654         source ./${facet}active
655     fi
656
657     active=${!activevar}
658     if [ -z "$active" ] ; then 
659         echo -n ${facet}
660     else
661         echo -n ${active}
662     fi
663 }
664
665 facet_active_host() {
666     local facet=$1
667     local active=`facet_active $facet`
668     if [ "$facet" == client ]; then
669         echo $HOSTNAME
670     else
671         echo `facet_host $active`
672     fi
673 }
674
675 change_active() {
676     local facet=$1
677     failover=${facet}failover 
678     host=`facet_host $failover`
679     [ -z "$host" ] && return
680     curactive=`facet_active $facet`
681     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
682         eval export ${facet}active=$facet
683     else
684         eval export ${facet}active=$failover
685     fi
686     # save the active host for this facet
687     activevar=${facet}active
688     echo "$activevar=${!activevar}" > ./$activevar
689 }
690
691 do_node() {
692     HOST=$1
693     shift
694     local myPDSH=$PDSH
695     if [ "$HOST" = "$HOSTNAME" ]; then
696         myPDSH="no_dsh"
697     elif [ -z "$myPDSH" -o "$myPDSH" = "no_dsh" ]; then
698         echo "cannot run remote command on $HOST with $myPDSH"
699         return 128
700     fi
701     if $VERBOSE; then
702         echo "CMD: $HOST $@" >&2
703         $myPDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
704     fi
705
706     if [ "$myPDSH" = "rsh" ]; then
707 # we need this because rsh does not return exit code of an executed command
708         local command_status="$TMP/cs"
709         rsh $HOST ":> $command_status"
710         rsh $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin;
711                     cd $RPWD; sh -c \"$@\") || 
712                     echo command failed >$command_status"
713         [ -n "$($myPDSH $HOST cat $command_status)" ] && return 1 || true
714         return 0
715     fi
716     $myPDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests:/sbin:/usr/sbin; cd $RPWD; sh -c \"$@\")" | sed "s/^${HOST}: //"
717     return ${PIPESTATUS[0]}
718 }
719
720 do_facet() {
721     facet=$1
722     shift
723     HOST=`facet_active_host $facet`
724     [ -z $HOST ] && echo No host defined for facet ${facet} && exit 1
725     do_node $HOST "$@"
726 }
727
728 add() {
729     local facet=$1
730     shift
731     # make sure its not already running
732     stop ${facet} -f
733     rm -f ${facet}active
734     do_facet ${facet} $MKFS $*
735 }
736
737 ostdevname() {
738     num=$1
739     DEVNAME=OSTDEV$num
740     #if $OSTDEVn isn't defined, default is $OSTDEVBASE + num
741     eval DEVPTR=${!DEVNAME:=${OSTDEVBASE}${num}}
742     echo -n $DEVPTR
743 }
744
745 ########
746 ## MountConf setup
747
748 stopall() {
749     # make sure we are using the primary server, so test-framework will
750     # be able to clean up properly.
751     activemds=`facet_active mds`
752     if [ $activemds != "mds" ]; then
753         fail mds
754     fi
755     
756     # assume client mount is local 
757     grep " $MOUNT " /proc/mounts && zconf_umount $HOSTNAME $MOUNT $*
758     grep " $MOUNT2 " /proc/mounts && zconf_umount $HOSTNAME $MOUNT2 $*
759
760     if [ -n "$CLIENTS" ]; then
761             zconf_umount_clients $CLIENTS $MOUNT "$*" || true
762             zconf_umount_clients $CLIENTS $MOUNT2 "$*" || true
763     fi
764
765     [ "$CLIENTONLY" ] && return
766     stop mds -f
767     for num in `seq $OSTCOUNT`; do
768         stop ost$num -f
769     done
770     return 0
771 }
772
773 cleanupall() {
774     stopall $*
775     unload_modules
776 }
777
778 formatall() {
779     [ "$FSTYPE" ] && FSTYPE_OPT="--backfstype $FSTYPE"
780
781     stopall
782     # We need ldiskfs here, may as well load them all
783     load_modules
784     [ "$CLIENTONLY" ] && return
785     echo Formatting mds, osts
786     if $VERBOSE; then
787         add mds $MDS_MKFS_OPTS $FSTYPE_OPT --reformat $MDSDEV || exit 10
788     else
789         add mds $MDS_MKFS_OPTS $FSTYPE_OPT --reformat $MDSDEV > /dev/null || exit 10
790     fi
791
792     for num in `seq $OSTCOUNT`; do
793         if $VERBOSE; then
794             add ost$num $OST_MKFS_OPTS $FSTYPE_OPT --reformat `ostdevname $num` || exit 10
795         else
796             add ost$num $OST_MKFS_OPTS $FSTYPE_OPT --reformat `ostdevname $num` > /dev/null || exit 10
797         fi
798     done
799 }
800
801 mount_client() {
802     grep " $1 " /proc/mounts || zconf_mount $HOSTNAME $*
803 }
804
805 remount_client()
806 {
807         zconf_umount `hostname` $1 || error "umount failed"
808         zconf_mount `hostname` $1 || error "mount failed"
809 }
810
811 set_obd_timeout() {
812     local facet=$1
813     local timeout=$2
814
815     do_facet $facet lsmod | grep -q obdclass || \
816         do_facet $facet "modprobe obdclass"
817
818     do_facet $facet "lctl set_param timeout=$timeout"
819 }
820
821 setupall() {
822     load_modules
823     if [ -z "$CLIENTONLY" ]; then
824         echo Setup mdt, osts
825         echo $REFORMAT | grep -q "reformat" \
826             || do_facet mds "$TUNEFS --writeconf $MDSDEV"
827         set_obd_timeout mds $TIMEOUT
828         start mds $MDSDEV $MDS_MOUNT_OPTS
829         for num in `seq $OSTCOUNT`; do
830             DEVNAME=`ostdevname $num`
831             set_obd_timeout ost$num $TIMEOUT
832             start ost$num $DEVNAME $OST_MOUNT_OPTS
833         done
834     fi
835     [ "$DAEMONFILE" ] && $LCTL debug_daemon start $DAEMONFILE $DAEMONSIZE
836     mount_client $MOUNT
837     [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT
838
839     if [ "$MOUNT_2" ]; then
840         mount_client $MOUNT2
841         [ -n "$CLIENTS" ] && zconf_mount_clients $CLIENTS $MOUNT2
842     fi
843     sleep 5
844 }
845
846 mounted_lustre_filesystems() {
847         awk '($3 ~ "lustre" && $1 ~ ":") { print $2 }' /proc/mounts
848 }
849
850 check_and_setup_lustre() {
851     MOUNTED="`mounted_lustre_filesystems`"
852     if [ -z "$MOUNTED" ]; then
853         [ "$REFORMAT" ] && formatall
854         setupall
855         MOUNTED="`mounted_lustre_filesystems`"
856         [ -z "$MOUNTED" ] && error "NAME=$NAME not mounted"
857         export I_MOUNTED=yes
858     fi
859     if [ "$ONLY" == "setup" ]; then
860         exit 0
861     fi
862 }
863
864 cleanup_and_setup_lustre() {
865     if [ "$ONLY" == "cleanup" -o "`mount | grep $MOUNT`" ]; then
866         lctl set_param debug=0 || true
867         cleanupall
868         if [ "$ONLY" == "cleanup" ]; then 
869             exit 0
870         fi
871     fi
872     check_and_setup_lustre
873 }
874
875 check_and_cleanup_lustre() {
876     if [ "`mount | grep $MOUNT`" ]; then
877         [ -n "$DIR" ] && rm -rf $DIR/[Rdfs][0-9]*
878     fi
879     if [ "$I_MOUNTED" = "yes" ]; then
880         cleanupall -f || error "cleanup failed"
881     fi
882     unset I_MOUNTED
883 }
884
885 ####### 
886 # General functions
887
888 check_network() {
889     local NETWORK=0
890     local WAIT=0
891     local MAX=$2
892     while [ $NETWORK -eq 0 ]; do
893         ping -c 1 -w 3 $1 > /dev/null
894         if [ $? -eq 0 ]; then
895             NETWORK=1
896         else
897             WAIT=$((WAIT + 5))
898             echo "waiting for $1, $((MAX - WAIT)) secs left"
899             sleep 5
900         fi
901         if [ $WAIT -gt $MAX ]; then
902             echo "Network not available"
903             exit 1
904         fi
905     done
906 }
907 check_port() {
908     while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
909         sleep 9
910     done
911 }
912
913 no_dsh() {
914     shift
915     eval $@
916 }
917
918 comma_list() {
919     # the sed converts spaces to commas, but leaves the last space
920     # alone, so the line doesn't end with a comma.
921     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
922 }
923
924 absolute_path() {
925     (cd `dirname $1`; echo $PWD/`basename $1`)
926 }
927
928 ##################################
929 # Adaptive Timeouts funcs
930
931 at_is_valid() {
932     if [ -z "$AT_MAX_PATH" ]; then
933         AT_MAX_PATH=$(do_facet mds "find /sys/ -name at_max")
934         [ -z "$AT_MAX_PATH" ] && echo "missing /sys/.../at_max " && return 1
935     fi
936     return 0
937 }
938
939 at_is_enabled() {
940     at_is_valid || error "invalid call"
941
942     # only check mds, we assume at_max is the same on all nodes
943     local at_max=$(do_facet mds "cat $AT_MAX_PATH")
944     if [ $at_max -eq 0 ]; then
945         return 1
946     else
947         return 0
948     fi
949 }
950
951 at_max_get() {
952     at_is_valid || error "invalid call"
953
954     do_facet $1 "cat $AT_MAX_PATH"
955 }
956
957 at_max_set() {
958     local at_max=$1
959     shift
960
961     at_is_valid || error "invalid call"
962
963     for facet in $@; do
964         if [ $facet == "ost" ]; then
965             for i in `seq $OSTCOUNT`; do
966                 do_facet ost$i "echo $at_max > $AT_MAX_PATH"
967             done
968         else
969             do_facet $facet "echo $at_max > $AT_MAX_PATH"
970         fi
971     done
972 }
973
974 ##################################
975 # OBD_FAIL funcs
976
977 drop_request() {
978 # OBD_FAIL_MDS_ALL_REQUEST_NET
979     RC=0
980     do_facet mds lctl set_param fail_loc=0x123
981     do_facet client "$1" || RC=$?
982     do_facet mds lctl set_param fail_loc=0
983     return $RC
984 }
985
986 drop_reply() {
987 # OBD_FAIL_MDS_ALL_REPLY_NET
988     RC=0
989     do_facet mds lctl set_param fail_loc=0x122
990     do_facet client "$@" || RC=$?
991     do_facet mds lctl set_param fail_loc=0
992     return $RC
993 }
994
995 drop_reint_reply() {
996 # OBD_FAIL_MDS_REINT_NET_REP
997     RC=0
998     do_facet mds lctl set_param fail_loc=0x119
999     do_facet client "$@" || RC=$?
1000     do_facet mds lctl set_param fail_loc=0
1001     return $RC
1002 }
1003
1004 pause_bulk() {
1005 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
1006     RC=0
1007     do_facet ost1 lctl set_param fail_loc=0x214
1008     do_facet client "$1" || RC=$?
1009     do_facet client "sync"
1010     do_facet ost1 lctl set_param fail_loc=0
1011     return $RC
1012 }
1013
1014 drop_ldlm_cancel() {
1015 #define OBD_FAIL_LDLM_CANCEL             0x304
1016     RC=0
1017     do_facet client lctl set_param fail_loc=0x304
1018     do_facet client "$@" || RC=$?
1019     do_facet client lctl set_param fail_loc=0
1020     return $RC
1021 }
1022
1023 drop_bl_callback() {
1024 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
1025     RC=0
1026     do_facet client lctl set_param fail_loc=0x305
1027     do_facet client "$@" || RC=$?
1028     do_facet client lctl set_param fail_loc=0
1029     return $RC
1030 }
1031
1032 drop_ldlm_reply() {
1033 #define OBD_FAIL_LDLM_REPLY              0x30c
1034     RC=0
1035     do_facet mds lctl set_param fail_loc=0x30c
1036     do_facet client "$@" || RC=$?
1037     do_facet mds lctl set_param fail_loc=0
1038     return $RC
1039 }
1040
1041 clear_failloc() {
1042     facet=$1
1043     pause=$2
1044     sleep $pause
1045     echo "clearing fail_loc on $facet"
1046     do_facet $facet "lctl set_param fail_loc=0 2>/dev/null || true"
1047 }
1048
1049 set_nodes_failloc () {
1050     local nodes=$1
1051     local node
1052
1053     for node in $nodes ; do
1054         do_node $node lctl set_param fail_loc=$2
1055     done
1056 }
1057
1058 cancel_lru_locks() {
1059     $LCTL mark "cancel_lru_locks $1 start"
1060     for d in `find $LPROC/ldlm/namespaces | egrep -i $1`; do
1061         [ -f $d/lru_size ] && echo clear > $d/lru_size
1062         [ -f $d/lock_unused_count ] && grep [1-9] $d/lock_unused_count /dev/null
1063     done
1064     $LCTL mark "cancel_lru_locks $1 stop"
1065 }
1066
1067 default_lru_size()
1068 {
1069         NR_CPU=$(grep -c "processor" /proc/cpuinfo)
1070         DEFAULT_LRU_SIZE=$((100 * NR_CPU))
1071         echo "$DEFAULT_LRU_SIZE"
1072 }
1073
1074 lru_resize_enable()
1075 {
1076         NS=$1
1077         test "x$NS" = "x" && NS="mdc"
1078         for F in $LPROC/ldlm/namespaces/*$NS*/lru_size; do
1079                 D=$(dirname $F)
1080                 log "Enable lru resize for $(basename $D)"
1081                 echo "0" > $F
1082         done
1083 }
1084
1085 lru_resize_disable()
1086 {
1087         NS=$1
1088         test "x$NS" = "x" && NS="mdc"
1089         for F in $LPROC/ldlm/namespaces/*$NS*/lru_size; do
1090                 D=$(dirname $F)
1091                 log "Disable lru resize for $(basename $D)"
1092                 DEFAULT_LRU_SIZE=$(default_lru_size)
1093                 echo "$DEFAULT_LRU_SIZE" > $F
1094         done
1095 }
1096
1097 pgcache_empty() {
1098     for a in /proc/fs/lustre/llite/*/dump_page_cache; do
1099         if [ `wc -l $a | awk '{print $1}'` -gt 1 ]; then
1100             echo there is still data in page cache $a ?
1101             cat $a;
1102             return 1;
1103         fi
1104     done
1105     return 0
1106 }
1107
1108 debugsave() {
1109     DEBUGSAVE="$(lctl get_param -n debug)"
1110 }
1111
1112 debugrestore() {
1113     [ -n "$DEBUGSAVE" ] && lctl set_param debug="${DEBUGSAVE}"
1114     DEBUGSAVE=""
1115 }
1116
1117 ##################################
1118 # Test interface 
1119 ##################################
1120
1121 error_noexit() {
1122     local TYPE=${TYPE:-"FAIL"}
1123     local ERRLOG
1124     lctl set_param fail_loc=0 2>/dev/null || true
1125     log " ${TESTSUITE} ${TESTNAME}: @@@@@@ ${TYPE}: $@ "
1126     ERRLOG=$TMP/lustre_${TESTSUITE}_${TESTNAME}.$(date +%s)
1127     echo "Dumping lctl log to $ERRLOG"
1128     # We need to dump the logs on all nodes
1129     local NODES=$(nodes_list)
1130     for NODE in $NODES; do
1131         do_node $NODE $LCTL dk $ERRLOG
1132     done
1133     debugrestore
1134     [ "$TESTSUITELOG" ] && echo "$0: ${TYPE}: $TESTNAME $@" >> $TESTSUITELOG
1135 }
1136
1137 error() {
1138     error_noexit "$@"
1139     $FAIL_ON_ERROR && exit 1 || true
1140 }
1141
1142 error_exit() {
1143     error_noexit "$@"
1144     exit 1
1145 }
1146
1147 # use only if we are ignoring failures for this test, bugno required.
1148 # (like ALWAYS_EXCEPT, but run the test and ignore the results.)
1149 # e.g. error_ignore 5494 "your message"
1150 error_ignore() {
1151     TYPE="IGNORE (bz$1)"
1152     shift
1153     error_noexit "$@"
1154 }
1155
1156 skip () {
1157         log " SKIP: ${TESTSUITE} ${TESTNAME} $@"
1158         [ "$TESTSUITELOG" ] && echo "${TESTSUITE}: SKIP: $TESTNAME $@" >> $TESTSUITELOG
1159 }
1160
1161 build_test_filter() {
1162     [ "$ONLY" ] && log "only running test `echo $ONLY`"
1163     for O in $ONLY; do
1164         eval ONLY_${O}=true
1165     done
1166     [ "$EXCEPT$ALWAYS_EXCEPT" ] && \
1167         log "skipping tests: `echo $EXCEPT $ALWAYS_EXCEPT`"
1168     [ "$EXCEPT_SLOW" ] && \
1169         log "skipping tests SLOW=no: `echo $EXCEPT_SLOW`"
1170     for E in $EXCEPT $ALWAYS_EXCEPT; do
1171         eval EXCEPT_${E}=true
1172     done
1173     for E in $EXCEPT_SLOW; do
1174         eval EXCEPT_SLOW_${E}=true
1175     done
1176     for G in $GRANT_CHECK_LIST; do
1177         eval GCHECK_ONLY_${G}=true
1178         done
1179 }
1180
1181 _basetest() {
1182     echo $*
1183 }
1184
1185 basetest() {
1186     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
1187 }
1188
1189 run_test() {
1190     export base=`basetest $1`
1191     if [ ! -z "$ONLY" ]; then
1192         testname=ONLY_$1
1193         if [ ${!testname}x != x ]; then
1194             run_one $1 "$2"
1195             return $?
1196         fi
1197         testname=ONLY_$base
1198         if [ ${!testname}x != x ]; then
1199             run_one $1 "$2"
1200             return $?
1201         fi
1202         echo -n "."
1203         return 0
1204     fi
1205     testname=EXCEPT_$1
1206     if [ ${!testname}x != x ]; then
1207         TESTNAME=test_$1 skip "skipping excluded test $1"
1208         return 0
1209     fi
1210     testname=EXCEPT_$base
1211     if [ ${!testname}x != x ]; then
1212         TESTNAME=test_$1 skip "skipping excluded test $1 (base $base)"
1213         return 0
1214     fi
1215     testname=EXCEPT_SLOW_$1
1216     if [ ${!testname}x != x ]; then
1217         TESTNAME=test_$1 skip "skipping SLOW test $1"
1218         return 0
1219     fi
1220     testname=EXCEPT_SLOW_$base
1221     if [ ${!testname}x != x ]; then
1222         TESTNAME=test_$1 skip "skipping SLOW test $1 (base $base)"
1223         return 0
1224     fi
1225
1226     run_one $1 "$2"
1227     
1228     return $?
1229 }
1230
1231 EQUALS="======================================================================"
1232 equals_msg() {
1233     msg="$@"
1234
1235     local suffixlen=$((${#EQUALS} - ${#msg}))
1236     [ $suffixlen -lt 5 ] && suffixlen=5
1237     log `echo $(printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS)`
1238 }
1239
1240 log() {
1241     echo "$*"
1242     lsmod | grep lnet > /dev/null || load_modules
1243
1244     local MSG="$*"
1245     # Get rif of '
1246     MSG=${MSG//\'/\\\'}
1247     MSG=${MSG//\(/\\\(}
1248     MSG=${MSG//\)/\\\)}
1249     MSG=${MSG//\;/\\\;}
1250     MSG=${MSG//\|/\\\|}
1251     MSG=${MSG//\>/\\\>}
1252     MSG=${MSG//\</\\\<}
1253     local NODES=$(nodes_list)
1254     for NODE in $NODES; do
1255         do_node $NODE $LCTL mark "$MSG" 2> /dev/null || true
1256     done
1257 }
1258
1259 trace() {
1260         log "STARTING: $*"
1261         strace -o $TMP/$1.strace -ttt $*
1262         RC=$?
1263         log "FINISHED: $*: rc $RC"
1264         return 1
1265 }
1266
1267 pass() {
1268     echo PASS $@
1269 }
1270
1271 check_mds() {
1272     FFREE=`cat /proc/fs/lustre/mds/*/filesfree`
1273     FTOTAL=`cat /proc/fs/lustre/mds/*/filestotal`
1274     [ $FFREE -ge $FTOTAL ] && error "files free $FFREE > total $FTOTAL" || true
1275 }
1276
1277 reset_fail_loc () {
1278     local myNODES=$(nodes_list)
1279     local NODE
1280
1281     for NODE in $myNODES; do
1282         do_node $NODE "lctl set_param fail_loc=0 2>/dev/null || true"
1283     done
1284 }
1285
1286 run_one() {
1287     testnum=$1
1288     message=$2
1289     tfile=f${testnum}
1290     export tdir=d0.${TESTSUITE}/d${base}
1291     local SAVE_UMASK=`umask`
1292     umask 0022
1293     mkdir -p $DIR/$tdir
1294
1295     BEFORE=`date +%s`
1296     log "== test $testnum: $message ============ `date +%H:%M:%S` ($BEFORE)"
1297     #check_mds
1298     export TESTNAME=test_$testnum
1299     test_${testnum} || error "test_$testnum failed with $?"
1300     #check_mds
1301     reset_fail_loc
1302     check_grant ${testnum} || error "check_grant $testnum failed with $?"
1303     [ -f $CATASTROPHE ] && [ `cat $CATASTROPHE` -ne 0 ] && \
1304         error "LBUG/LASSERT detected"
1305     ps auxww | grep -v grep | grep -q multiop && error "multiop still running"
1306     pass "($((`date +%s` - $BEFORE))s)"
1307     rmdir ${DIR}/$tdir >/dev/null 2>&1 || true
1308     unset TESTNAME
1309     unset tdir
1310     umask $SAVE_UMASK
1311     cd $SAVE_PWD
1312     $CLEANUP
1313 }
1314
1315 canonical_path() {
1316     (cd `dirname $1`; echo $PWD/`basename $1`)
1317 }
1318
1319 sync_clients() {
1320     [ -d $DIR1 ] && cd $DIR1 && sync; sleep 1; sync 
1321     [ -d $DIR2 ] && cd $DIR2 && sync; sleep 1; sync 
1322         cd $SAVE_PWD
1323 }
1324
1325 check_grant() {
1326     export base=`basetest $1`
1327     [ "$CHECK_GRANT" == "no" ] && return 0
1328
1329         testname=GCHECK_ONLY_${base}
1330         [ ${!testname}x == x ] && return 0
1331
1332         echo -n "checking grant......"
1333         cd $SAVE_PWD
1334         # write some data to sync client lost_grant
1335         rm -f $DIR1/${tfile}_check_grant_* 2>&1
1336         for i in `seq $OSTCOUNT`; do
1337                 $LFS setstripe $DIR1/${tfile}_check_grant_$i -i $(($i -1)) -c 1
1338                 dd if=/dev/zero of=$DIR1/${tfile}_check_grant_$i bs=4k \
1339                                               count=1 > /dev/null 2>&1 
1340         done
1341         # sync all the data and make sure no pending data on server
1342         sync_clients
1343         
1344         #get client grant and server grant 
1345         client_grant=0
1346     for d in ${LPROC}/osc/*/cur_grant_bytes; do 
1347                 client_grant=$((client_grant + `cat $d`))
1348         done
1349         server_grant=0
1350         for d in ${LPROC}/obdfilter/*/tot_granted; do
1351                 server_grant=$((server_grant + `cat $d`))
1352         done
1353
1354         # cleanup the check_grant file
1355         for i in `seq $OSTCOUNT`; do
1356                 rm $DIR1/${tfile}_check_grant_$i
1357         done
1358
1359         #check whether client grant == server grant 
1360         if [ $client_grant != $server_grant ]; then
1361                 echo "failed: client:${client_grant} server: ${server_grant}"
1362                 return 1
1363         else
1364                 echo "pass"
1365         fi
1366 }
1367
1368 ########################
1369 # helper functions
1370
1371 osc_to_ost()
1372 {
1373     osc=$1
1374     ost=`echo $1 | awk -F_ '{print $3}'`
1375     if [ -z $ost ]; then
1376         ost=`echo $1 | sed 's/-osc.*//'`
1377     fi
1378     echo $ost
1379 }
1380
1381 remote_mds ()
1382 {
1383     [ -z "$(lctl dl | grep mdt)" ]
1384 }
1385
1386 remote_mds_nodsh()
1387 {
1388     remote_mds && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$mds_HOST" ]
1389 }
1390
1391 remote_ost ()
1392 {
1393     [ -z "$(lctl dl | grep ost)" ]
1394 }
1395
1396 remote_ost_nodsh()
1397 {
1398     remote_ost && [ "$PDSH" = "no_dsh" -o -z "$PDSH" -o -z "$ost_HOST" ]
1399 }
1400
1401 osts_nodes () {
1402     local OSTNODES=$(facet_host ost1)
1403     local NODES_sort
1404
1405     for num in `seq $OSTCOUNT`; do
1406         local myOST=$(facet_host ost$num)
1407         OSTNODES="$OSTNODES $myOST"
1408     done
1409     NODES_sort=$(for i in $OSTNODES; do echo $i; done | sort -u)
1410
1411     echo $NODES_sort
1412 }
1413
1414 nodes_list () {
1415     # FIXME. We need a list of clients
1416     local myNODES=$HOSTNAME
1417     local myNODES_sort
1418
1419     # CLIENTS (if specified) contains the local client
1420     [ -n "$CLIENTS" ] && myNODES=${CLIENTS//,/ }
1421
1422     if [ "$PDSH" -a "$PDSH" != "no_dsh" ]; then
1423         myNODES="$myNODES $(osts_nodes) $mds_HOST"
1424     fi
1425
1426     myNODES_sort=$(for i in $myNODES; do echo $i; done | sort -u)
1427
1428     echo $myNODES_sort
1429 }
1430
1431 is_patchless ()
1432 {
1433     lctl get_param version | grep -q patchless
1434 }
1435
1436 check_runas_id_ret() {
1437     local myRC=0
1438     local myRUNAS_ID=$1
1439     shift
1440     local myRUNAS=$@
1441     if [ -z "$myRUNAS" ]; then
1442         error_exit "myRUNAS command must be specified for check_runas_id"
1443     fi
1444     mkdir $DIR/d0_runas_test
1445     chmod 0755 $DIR
1446     chown $myRUNAS_ID:$myRUNAS_ID $DIR/d0_runas_test
1447     $myRUNAS touch $DIR/d0_runas_test/f$$ || myRC=1
1448     rm -rf $DIR/d0_runas_test
1449     return $myRC
1450 }
1451
1452 check_runas_id() {
1453     local myRUNAS_ID=$1
1454     shift
1455     local myRUNAS=$@
1456     check_runas_id_ret $myRUNAS_ID $myRUNAS || \
1457         error "unable to write to $DIR/d0_runas_test as UID $myRUNAS_ID. 
1458         Please set RUNAS_ID to some UID which exists on MDS and client or 
1459         add user $myRUNAS_ID:$myRUNAS_ID on these nodes."
1460 }
1461
1462 # Run multiop in the background, but wait for it to print
1463 # "PAUSING" to its stdout before returning from this function.
1464 multiop_bg_pause() {
1465     MULTIOP_PROG=${MULTIOP_PROG:-multiop}
1466     FILE=$1
1467     ARGS=$2
1468
1469     TMPPIPE=/tmp/multiop_open_wait_pipe.$$
1470     mkfifo $TMPPIPE
1471
1472     echo "$MULTIOP_PROG $FILE v$ARGS"
1473     $MULTIOP_PROG $FILE v$ARGS > $TMPPIPE &
1474
1475     echo "TMPPIPE=${TMPPIPE}"
1476     read -t 60 multiop_output < $TMPPIPE
1477     if [ $? -ne 0 ]; then
1478         rm -f $TMPPIPE
1479         return 1
1480     fi
1481     rm -f $TMPPIPE
1482     if [ "$multiop_output" != "PAUSING" ]; then
1483         echo "Incorrect multiop output: $multiop_output"
1484         kill -9 $PID
1485         return 1
1486     fi
1487
1488     return 0
1489 }
1490
1491 # reset llite stat counters
1492 clear_llite_stats(){
1493         lctl set_param -n llite.*.stats 0
1494 }
1495
1496 # sum llite stat items
1497 calc_llite_stats() {
1498         local res=$(lctl get_param -n llite.*.stats |
1499                     awk 'BEGIN {s = 0} END {print s} /^'"$1"'/ {s += $2}')
1500         echo $res
1501 }
1502
1503 # save_lustre_params(node, parameter_mask)
1504 # generate a stream of formatted strings (<node> <param name>=<param value>)
1505 save_lustre_params() {
1506         local s
1507         do_node $1 "lctl get_param $2" | while read s; do echo "$1 $s"; done
1508 }
1509
1510 # restore lustre parameters from input stream, produces by save_lustre_params
1511 restore_lustre_params() {
1512         local node
1513         local name
1514         local val
1515         while IFS=" =" read node name val; do
1516                 do_node $node "lctl set_param -n $name $val"
1517         done
1518 }
1519