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