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