Whamcloud - gitweb
- landed b_hd_cray_merge3
[fs/lustre-release.git] / lustre / tests / sanity-gns.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10 ALWAYS_EXCEPT=${ALWAYS_EXCEPT:-""}
11 [ "$ALWAYS_EXCEPT$EXCEPT" ] && echo "Skipping tests: $ALWAYS_EXCEPT $EXCEPT"
12
13 SRCDIR=`dirname $0`
14 export PATH=$PWD/$SRCDIR:$SRCDIR:$SRCDIR/../utils:$PATH
15 export SECURITY=${SECURITY:-"null"}
16
17 TMP=${TMP:-/tmp}
18 FSTYPE=${FSTYPE:-ext3}
19
20 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
21 CREATETEST=${CREATETEST:-createtest}
22 LFS=${LFS:-lfs}
23 LSTRIPE=${LSTRIPE:-"$LFS setstripe"}
24 LFIND=${LFIND:-"$LFS find"}
25 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
26 LCTL=${LCTL:-lctl}
27 MCREATE=${MCREATE:-mcreate}
28 OPENFILE=${OPENFILE:-openfile}
29 OPENUNLINK=${OPENUNLINK:-openunlink}
30 TOEXCL=${TOEXCL:-toexcl}
31 TRUNCATE=${TRUNCATE:-truncate}
32 MUNLINK=${MUNLINK:-munlink}
33 SOCKETSERVER=${SOCKETSERVER:-socketserver}
34 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
35 IOPENTEST1=${IOPENTEST1:-iopentest1}
36 IOPENTEST2=${IOPENTEST2:-iopentest2}
37 PTLDEBUG=${PTLDEBUG:-0}
38
39 . krb5_env.sh
40
41 if [ $UID -ne 0 ]; then
42         RUNAS_ID="$UID"
43         RUNAS=""
44 else
45         RUNAS_ID=${RUNAS_ID:-500}
46         RUNAS=${RUNAS:-"runas -u $RUNAS_ID"}
47 fi
48
49 if [ `using_krb5_sec $SECURITY` == 'y' ] ; then
50     start_krb5_kdc || exit 1
51     if [ $RUNAS_ID -ne $UID ]; then
52         $RUNAS ./krb5_refresh_cache.sh || exit 2
53     fi
54 fi
55
56 export NAME=${NAME:-local}
57
58 SAVE_PWD=$PWD
59
60 clean() {
61         echo -n "cln.."
62         sh llmountcleanup.sh > /dev/null || exit 20
63         I_MOUNTED=no
64 }
65 CLEAN=${CLEAN:-clean}
66
67 start() {
68         echo -n "mnt.."
69         sh llrmount.sh > /dev/null || exit 10
70         I_MOUNTED=yes
71         echo "done"
72 }
73 START=${START:-start}
74
75 log() {
76         echo "$*"
77         lctl mark "$*" 2> /dev/null || true
78 }
79
80 trace() {
81         log "STARTING: $*"
82         strace -o $TMP/$1.strace -ttt $*
83         RC=$?
84         log "FINISHED: $*: rc $RC"
85         return 1
86 }
87 TRACE=${TRACE:-""}
88
89 check_kernel_version() {
90         VERSION_FILE=/proc/fs/lustre/kernel_version
91         WANT_VER=$1
92         [ ! -f $VERSION_FILE ] && echo "can't find kernel version" && return 1
93         GOT_VER=`cat $VERSION_FILE`
94         [ $GOT_VER -ge $WANT_VER ] && return 0
95         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
96         return 1
97 }
98
99 run_one() {
100         if ! mount | grep -q $DIR; then
101                 $START
102         fi
103         echo $PTLDEBUG >/proc/sys/portals/debug 
104         log "== test $1: $2"
105         export TESTNAME=test_$1
106         test_$1 || error "test_$1: exit with rc=$?"
107         unset TESTNAME
108         pass
109         cd $SAVE_PWD
110         $CLEAN
111 }
112
113 build_test_filter() {
114         for O in $ONLY; do
115             eval ONLY_${O}=true
116         done
117         for E in $EXCEPT $ALWAYS_EXCEPT; do
118             eval EXCEPT_${E}=true
119         done
120 }
121
122 _basetest() {
123     echo $*
124 }
125
126 basetest() {
127     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
128 }
129
130 run_test() {
131          base=`basetest $1`
132          if [ "$ONLY" ]; then
133                  testname=ONLY_$1
134                  if [ ${!testname}x != x ]; then
135                         run_one $1 "$2"
136                         return $?
137                  fi
138                  testname=ONLY_$base
139                  if [ ${!testname}x != x ]; then
140                          run_one $1 "$2"
141                          return $?
142                  fi
143                  echo -n "."
144                  return 0
145         fi
146         testname=EXCEPT_$1
147         if [ ${!testname}x != x ]; then
148                  echo "skipping excluded test $1"
149                  return 0
150         fi
151         testname=EXCEPT_$base
152         if [ ${!testname}x != x ]; then
153                  echo "skipping excluded test $1 (base $base)"
154                  return 0
155         fi
156         run_one $1 "$2"
157         return $?
158 }
159
160 [ "$SANITYLOG" ] && rm -f $SANITYLOG || true
161
162 error() { 
163         log "FAIL: $@"
164         if [ "$SANITYLOG" ]; then
165                 echo "FAIL: $TESTNAME $@" >> $SANITYLOG
166         else
167                 exit 1
168         fi
169 }
170
171 pass() { 
172         echo PASS
173 }
174
175 MOUNT="`mount | awk '/^'$NAME' .* lustre_lite / { print $3 }'`"
176 if [ -z "$MOUNT" ]; then
177         sh llmount.sh
178         MOUNT="`mount | awk '/^'$NAME' .* lustre_lite / { print $3 }'`"
179         [ -z "$MOUNT" ] && error "NAME=$NAME not mounted"
180         I_MOUNTED=yes
181 fi
182
183 [ `echo $MOUNT | wc -w` -gt 1 ] && error "NAME=$NAME mounted more than once"
184
185 DIR=${DIR:-$MOUNT}
186 [ -z "`echo $DIR | grep $MOUNT`" ] && echo "$DIR not in $MOUNT" && exit 99
187
188 rm -rf $DIR/[Rdfs][1-9]*
189 build_test_filter
190
191 echo preparing for tests involving mounts
192 EXT2_DEV=${EXT2_DEV:-/tmp/SANITY.LOOP}
193 touch $EXT2_DEV
194 mke2fs -j -F $EXT2_DEV 8000 >/dev/null 2>&1
195
196 find_free_loop() {
197     local LOOP_DEV=""
198     test -b /dev/loop0 && 
199         base="/dev/loop" || base="/dev/loop/"
200
201     for ((i=0;i<256;i++)); do
202         test -b $base$i || continue
203         
204         losetup $base$i >/dev/null 2>&1 || {
205             LOOP_DEV="$base$i"
206             break
207         }
208     done
209     echo $LOOP_DEV
210 }
211
212 cleanup_loop() {
213     local LOOP_DEV=$1
214     local LOOP_FILE=$2
215     local LOOP_MNTPT=$3
216     
217     chmod u-s $LOOP_MNTPT >/dev/null 2>&1
218     umount $LOOP_MNTPT >/dev/null 2>&1
219     losetup -d $LOOP_DEV >/dev/null 2>&1
220     rm -fr $LOOP_FILE >/dev/null 2>&1
221     rm -fr $LOOP_MNTPT >/dev/null 2>&1
222 }
223
224 setup_loop() {
225     local LOOP_DEV=$1
226     local LOOP_FILE=$2
227     
228     dd if=/dev/zero of=$LOOP_FILE bs=1M count=10 2>/dev/null || return $?
229
230     losetup $LOOP_DEV $LOOP_FILE || {
231         rc=$?
232         cleanup_mount $LOOP_DEV $LOOP_FILE $DIR/gns_test_1a
233         return $rc
234     }
235     
236     mke2fs -F $LOOP_DEV >/dev/null 2>&1 || {
237         rc=$?
238         cleanup_mount $LOOP_DEV $LOOP_FILE $DIR/gns_test_1a
239         echo "cannot create test ext2 fs on $LOOP_DEV"
240         return $?
241     }
242     return 0
243 }
244
245 prep_upcall() {
246     local INJECTION=""
247     local UPCALL=$1
248     local MODE=$2
249     local LOG=$3
250
251     test "x$MODE" = "xDEADLOCK" &&
252     INJECTION="touch \$MNTPATH/file"
253     
254     cat > $UPCALL <<- EOF
255 #!/bin/sh
256
257 MOUNT=\`which mount 2>/dev/null\`
258 test "x\$MOUNT" = "x" && MOUNT="/bin/mount"
259
260 OPTIONS=\$1
261 MNTPATH=\$2
262
263 test "x\$OPTIONS" = "x" || "x\$MNTPATH" = "x" &&
264 exit 1
265
266 $INJECTION
267 \$MOUNT \$OPTIONS \$MNTPATH > $LOG 2>&1
268 exit \$?
269 EOF
270     chmod +x $UPCALL
271     return $?
272 }
273
274 check_gns() {
275     local LOG="/tmp/gns-log"
276     local UPCALL_PATH=""
277     
278     local UPCALL=$1
279     local OBJECT=$2
280     local TIMOUT=$3
281     local TICK=$4
282     
283     rm -fr $LOG >/dev/null 2>&1
284     UPCALL_PATH="/tmp/gns-upcall-$UPCALL.sh"
285     
286     echo "generating upcall $UPCALL_PATH"
287     prep_upcall $UPCALL_PATH $UPCALL $LOG || return $rc
288     echo "======================== upcall script ==========================="
289     cat $UPCALL_PATH 2>/dev/null || return $?
290     echo "=================================================================="
291    
292     echo "$UPCALL_PATH" > /proc/fs/lustre/llite/fs0/gns_upcall || return $?
293     echo "upcall:  $(cat /proc/fs/lustre/llite/fs0/gns_upcall)"
294
295     echo -n "mount on open $OBJECT/test_file1: "
296     echo -n "test data" > $OBJECT/test_file1 >/dev/null 2>&1 || return $?
297
298     local ENTRY="`basename $OBJECT`"
299     
300     cat /proc/mounts | grep -q "$ENTRY" || {
301         echo "fail"
302         test -f $LOG && {
303             echo "======================== upcall log ==========================="
304             cat $LOG
305             echo "==============================================================="
306         } || {
307             echo "upcall log file $LOG is not found"
308         }
309         return 1
310     }
311     echo "success"
312
313     local sleep_time=$TIMOUT
314     let sleep_time+=$TICK*2
315     echo -n "waiting for umount ${sleep_time}s (timeout + tick*2): "
316     sleep $sleep_time
317
318     cat /proc/mounts | grep -q "$ENTRY" && {
319         echo "failed"
320         return 2
321     }
322     echo "success"
323     return 0
324 }
325
326 test_1a() {
327     local LOOP_DEV=$(find_free_loop 2>/dev/null)
328     local UPCALL="/tmp/gns-upcall.sh"
329     local LOOP_FILE="/tmp/gns_loop"
330     local OBJECT=".mntinfo"
331     local TIMOUT=5
332     local TICK=1
333
334     test "x$LOOP_DEV" != "x" && test -b $LOOP_DEV ||
335         error "can't find free loop device"
336
337     echo "preparing loop device $LOOP_DEV <-> $LOOP_FILE..."
338     cleanup_loop $LOOP_DEV $LOOP_FILE $DIR/gns_test_1a
339     setup_loop $LOOP_DEV $LOOP_FILE || error
340
341     echo "setting up GNS timeouts and mount object..."
342     echo "$OBJECT" > /proc/fs/lustre/llite/fs0/gns_object_name || error
343     echo "$TIMOUT" > /proc/fs/lustre/llite/fs0/gns_timeout || error
344     echo "$TICK" > /proc/fs/lustre/llite/fs0/gns_tick || error
345
346     echo ""
347     echo "timeout: $(cat /proc/fs/lustre/llite/fs0/gns_timeout)s"
348     echo "object:  $(cat /proc/fs/lustre/llite/fs0/gns_object_name)"
349     echo "tick:    $(cat /proc/fs/lustre/llite/fs0/gns_tick)s"
350     echo ""
351
352     echo "preparing mount object at $DIR/gns_test_1a/$OBJECT..."
353     mkdir -p $DIR/gns_test_1a || error
354     echo -n "-t ext2 $LOOP_DEV" > $DIR/gns_test_1a/$OBJECT
355     echo "======================== mount object ==========================="
356     cat $DIR/gns_test_1a/$OBJECT
357     echo ""
358     echo "================================================================="
359     chmod u+s $DIR/gns_test_1a || error
360
361     echo ""
362     echo "testing GNS with GENERIC upcall 2 times on the row"
363     for ((i=0;i<2;i++)); do
364         check_gns GENERIC $DIR/gns_test_1a $TIMOUT $TICK || {
365             cleanup_loop $LOOP_DEV $LOOP_FILE $DIR/gns_test_1a
366             error
367         }
368     done
369     
370     echo ""
371     echo "testing GNS with DEADLOCK upcall 2 times on the row"
372     for ((i=0;i<2;i++)); do
373         check_gns DEADLOCK $DIR/gns_test_1a $TIMOUT $TICK || {
374             cleanup_loop $LOOP_DEV $LOOP_FILE $DIR/gns_test_1a
375             error
376         }
377     done
378     
379     cleanup_loop $LOOP_DEV $LOOP_FILE $DIR/gns_test_1a
380 }
381
382 run_test 1a " general GNS test - mounting/umount ===================="
383
384 TMPDIR=$OLDTMPDIR
385 TMP=$OLDTMP
386 HOME=$OLDHOME
387
388 log "cleanup: ==========================================================="
389 if [ "`mount | grep ^$NAME`" ]; then
390         rm -rf $DIR/[Rdfs][1-9]*
391         if [ "$I_MOUNTED" = "yes" ]; then
392                 sh llmountcleanup.sh || error
393         fi
394 fi
395
396 echo '=========================== finished ==============================='
397 [ -f "$SANITYLOG" ] && cat $SANITYLOG && exit 1 || true