Whamcloud - gitweb
8b0e3c1e07b3be6eaf353760540449327653902c
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- mode: Bash; tab-width: 4; indent-tabs-mode: t; -*-
3 # vim:autoindent:shiftwidth=4:tabstop=4:
4 #
5 # Run select tests by setting ONLY, or as arguments to the script.
6 # Skip specific tests by setting EXCEPT.
7 #
8 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
9 set -e
10
11 ONLY=${ONLY:-"$*"}
12 # bug number for skipped test:  13297 2108 9789 3637 9789 3561 12622 15528/2330 5188 10764 16410
13 ALWAYS_EXCEPT=${ALWAYS_EXCEPT:-"27u  42a  42b  42c  42d  45   51d   62         68   75    76 $SANITY_EXCEPT"}
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 # Tests that fail on uml, maybe elsewhere, FIXME
17 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
18 #                                    buffer i/o errs             sock spc runas
19 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101"
20
21 case `uname -r` in
22 2.4*) FSTYPE=${FSTYPE:-ext3};    ALWAYS_EXCEPT="$ALWAYS_EXCEPT 76"
23         [ "$CPU" = "UML" ] && ALWAYS_EXCEPT="$ALWAYS_EXCEPT 105a";;
24 2.6*) FSTYPE=${FSTYPE:-ldiskfs}; ALWAYS_EXCEPT="$ALWAYS_EXCEPT " ;;
25 *) error "unsupported kernel" ;;
26 esac
27
28 SRCDIR=$(cd $(dirname $0); echo $PWD)
29 export PATH=$PATH:/sbin
30
31 TMP=${TMP:-/tmp}
32
33 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
34 CREATETEST=${CREATETEST:-createtest}
35 LFS=${LFS:-lfs}
36 SETSTRIPE=${SETSTRIPE:-"$LFS setstripe"}
37 GETSTRIPE=${GETSTRIPE:-"$LFS getstripe"}
38 LFIND=${LFIND:-"$LFS find"}
39 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
40 LCTL=${LCTL:-lctl}
41 MCREATE=${MCREATE:-mcreate}
42 OPENFILE=${OPENFILE:-openfile}
43 OPENUNLINK=${OPENUNLINK:-openunlink}
44 READS=${READS:-"reads"}
45 TRUNCATE=${TRUNCATE:-truncate}
46 MUNLINK=${MUNLINK:-munlink}
47 SOCKETSERVER=${SOCKETSERVER:-socketserver}
48 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
49 IOPENTEST1=${IOPENTEST1:-iopentest1}
50 IOPENTEST2=${IOPENTEST2:-iopentest2}
51 MEMHOG=${MEMHOG:-memhog}
52 DIRECTIO=${DIRECTIO:-directio}
53 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
54 UMOUNT=${UMOUNT:-"umount -d"}
55 CHECK_GRANT=${CHECK_GRANT:-"yes"}
56 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
57
58 export NAME=${NAME:-local}
59
60 SAVE_PWD=$PWD
61
62 CLEANUP=${CLEANUP:-:}
63 SETUP=${SETUP:-:}
64 TRACE=${TRACE:-""}
65 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
66 . $LUSTRE/tests/test-framework.sh
67 init_test_env $@
68 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
69
70 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 36f 36g 36h 51b 51c 60c 63 64b 68 71 73 77f 78 101 103 115 120g 124b"
71
72 SANITYLOG=${TESTSUITELOG:-$TMP/$(basename $0 .sh).log}
73 FAIL_ON_ERROR=${FAIL_ON_ERROR:-false}
74
75 cleanup() {
76         echo -n "cln.."
77         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
78         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
79 }
80 setup() {
81         echo -n "mnt.."
82         load_modules
83         setupall || exit 10
84         echo "done"
85 }
86
87
88 check_kernel_version() {
89         VERSION_FILE=version
90         WANT_VER=$1
91         GOT_VER=$(lctl get_param -n $VERSION_FILE | awk '/kernel:/ {print $2}')
92         case $GOT_VER in
93         patchless|patchless_client) return 0;;
94         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
95         esac
96         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
97         return 1
98 }
99
100 if [ "$ONLY" == "cleanup" ]; then
101         sh llmountcleanup.sh
102         exit 0
103 fi
104
105 [ "$SANITYLOG" ] && rm -f $SANITYLOG || true
106
107 check_and_setup_lustre
108
109 DIR=${DIR:-$MOUNT}
110 assert_DIR
111
112 LOVNAME=`lctl get_param -n llite.*.lov.common_name | tail -n 1`
113 OSTCOUNT=`lctl get_param -n lov.$LOVNAME.numobd`
114 STRIPECOUNT=`lctl get_param -n lov.$LOVNAME.stripecount`
115 STRIPESIZE=`lctl get_param -n lov.$LOVNAME.stripesize`
116 ORIGFREE=`lctl get_param -n lov.$LOVNAME.kbytesavail`
117 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
118
119 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
120 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
121 rm -rf $DIR/[Rdfs][0-9]*
122
123 # $RUNAS_ID may get set incorrectly somewhere else
124 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
125
126 check_runas_id $RUNAS_ID $RUNAS_ID $RUNAS
127
128 build_test_filter
129
130 echo "preparing for tests involving mounts"
131 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
132 touch $EXT2_DEV
133 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
134 echo # add a newline after mke2fs.
135
136 umask 077
137
138 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
139 lctl set_param debug=-1 2> /dev/null || true
140 test_0() {
141         touch $DIR/$tfile
142         $CHECKSTAT -t file $DIR/$tfile || error
143         rm $DIR/$tfile
144         $CHECKSTAT -a $DIR/$tfile || error
145 }
146 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
147
148 test_0b() {
149         chmod 0755 $DIR || error
150         $CHECKSTAT -p 0755 $DIR || error
151 }
152 run_test 0b "chmod 0755 $DIR ============================="
153
154 test_0c() {
155     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
156     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
157 }
158 run_test 0c "check import proc ============================="
159
160 test_1a() {
161         mkdir $DIR/d1
162         mkdir $DIR/d1/d2
163         $CHECKSTAT -t dir $DIR/d1/d2 || error
164 }
165 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
166
167 test_1b() {
168         rmdir $DIR/d1/d2
169         rmdir $DIR/d1
170         $CHECKSTAT -a $DIR/d1 || error
171 }
172 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
173
174 test_2a() {
175         mkdir $DIR/d2
176         touch $DIR/d2/f
177         $CHECKSTAT -t file $DIR/d2/f || error
178 }
179 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
180
181 test_2b() {
182         rm -r $DIR/d2
183         $CHECKSTAT -a $DIR/d2 || error
184 }
185 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
186
187 test_3a() {
188         mkdir $DIR/d3
189         $CHECKSTAT -t dir $DIR/d3 || error
190 }
191 run_test 3a "mkdir .../d3 ======================================"
192
193 test_3b() {
194         if [ ! -d $DIR/d3 ]; then
195                 mkdir $DIR/d3
196         fi
197         touch $DIR/d3/f
198         $CHECKSTAT -t file $DIR/d3/f || error
199 }
200 run_test 3b "touch .../d3/f ===================================="
201
202 test_3c() {
203         rm -r $DIR/d3
204         $CHECKSTAT -a $DIR/d3 || error
205 }
206 run_test 3c "rm -r .../d3 ======================================"
207
208 test_4a() {
209         mkdir $DIR/d4
210         $CHECKSTAT -t dir $DIR/d4 || error
211 }
212 run_test 4a "mkdir .../d4 ======================================"
213
214 test_4b() {
215         if [ ! -d $DIR/d4 ]; then
216                 mkdir $DIR/d4
217         fi
218         mkdir $DIR/d4/d2
219         $CHECKSTAT -t dir $DIR/d4/d2 || error
220 }
221 run_test 4b "mkdir .../d4/d2 ==================================="
222
223 test_5() {
224         mkdir $DIR/d5
225         mkdir $DIR/d5/d2
226         chmod 0707 $DIR/d5/d2
227         $CHECKSTAT -t dir -p 0707 $DIR/d5/d2 || error
228 }
229 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
230
231 test_6a() {
232         touch $DIR/f6a
233         chmod 0666 $DIR/f6a || error
234         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
235 }
236 run_test 6a "touch .../f6a; chmod .../f6a ======================"
237
238 test_6b() {
239         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
240         if [ ! -f $DIR/f6a ]; then
241                 touch $DIR/f6a
242                 chmod 0666 $DIR/f6a
243         fi
244         $RUNAS chmod 0444 $DIR/f6a && error
245         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
246 }
247 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
248
249 test_6c() {
250         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
251         touch $DIR/f6c
252         chown $RUNAS_ID $DIR/f6c || error
253         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
254 }
255 run_test 6c "touch .../f6c; chown .../f6c ======================"
256
257 test_6d() {
258         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
259         if [ ! -f $DIR/f6c ]; then
260                 touch $DIR/f6c
261                 chown $RUNAS_ID $DIR/f6c
262         fi
263         $RUNAS chown $UID $DIR/f6c && error
264         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
265 }
266 run_test 6d "$RUNAS chown .../f6c (should return error) =="
267
268 test_6e() {
269         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
270         touch $DIR/f6e
271         chgrp $RUNAS_ID $DIR/f6e || error
272         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
273 }
274 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
275
276 test_6f() {
277         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
278         if [ ! -f $DIR/f6e ]; then
279                 touch $DIR/f6e
280                 chgrp $RUNAS_ID $DIR/f6e
281         fi
282         $RUNAS chgrp $UID $DIR/f6e && error
283         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
284 }
285 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
286
287 test_6g() {
288         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
289         mkdir $DIR/d6g || error
290         chmod 777 $DIR/d6g || error
291         $RUNAS mkdir $DIR/d6g/d || error
292         chmod g+s $DIR/d6g/d || error
293         mkdir $DIR/d6g/d/subdir
294         $CHECKSTAT -g \#$RUNAS_ID $DIR/d6g/d/subdir || error
295 }
296 run_test 6g "Is new dir in sgid dir inheriting group?"
297
298 test_6h() { # bug 7331
299         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
300         touch $DIR/f6h || error "touch failed"
301         chown $RUNAS_ID:$RUNAS_ID $DIR/f6h || error "initial chown failed"
302         $RUNAS -G$RUNAS_ID chown $RUNAS_ID:0 $DIR/f6h && error "chown worked"
303         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_ID $DIR/f6h || error
304 }
305 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
306
307 test_7a() {
308         mkdir $DIR/d7
309         $MCREATE $DIR/d7/f
310         chmod 0666 $DIR/d7/f
311         $CHECKSTAT -t file -p 0666 $DIR/d7/f || error
312 }
313 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
314
315 test_7b() {
316         if [ ! -d $DIR/d7 ]; then
317                 mkdir $DIR/d7
318         fi
319         $MCREATE $DIR/d7/f2
320         echo -n foo > $DIR/d7/f2
321         [ "`cat $DIR/d7/f2`" = "foo" ] || error
322         $CHECKSTAT -t file -s 3 $DIR/d7/f2 || error
323 }
324 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
325
326 test_8() {
327         mkdir $DIR/d8
328         touch $DIR/d8/f
329         chmod 0666 $DIR/d8/f
330         $CHECKSTAT -t file -p 0666 $DIR/d8/f || error
331 }
332 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
333
334 test_9() {
335         mkdir $DIR/d9
336         mkdir $DIR/d9/d2
337         mkdir $DIR/d9/d2/d3
338         $CHECKSTAT -t dir $DIR/d9/d2/d3 || error
339 }
340 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
341
342 test_10() {
343         mkdir $DIR/d10
344         mkdir $DIR/d10/d2
345         touch $DIR/d10/d2/f
346         $CHECKSTAT -t file $DIR/d10/d2/f || error
347 }
348 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
349
350 test_11() {
351         mkdir $DIR/d11
352         mkdir $DIR/d11/d2
353         chmod 0666 $DIR/d11/d2
354         chmod 0705 $DIR/d11/d2
355         $CHECKSTAT -t dir -p 0705 $DIR/d11/d2 || error
356 }
357 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
358
359 test_12() {
360         mkdir $DIR/d12
361         touch $DIR/d12/f
362         chmod 0666 $DIR/d12/f
363         chmod 0654 $DIR/d12/f
364         $CHECKSTAT -t file -p 0654 $DIR/d12/f || error
365 }
366 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
367
368 test_13() {
369         mkdir $DIR/d13
370         dd if=/dev/zero of=$DIR/d13/f count=10
371         >  $DIR/d13/f
372         $CHECKSTAT -t file -s 0 $DIR/d13/f || error
373 }
374 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
375
376 test_14() {
377         mkdir $DIR/d14
378         touch $DIR/d14/f
379         rm $DIR/d14/f
380         $CHECKSTAT -a $DIR/d14/f || error
381 }
382 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
383
384 test_15() {
385         mkdir $DIR/d15
386         touch $DIR/d15/f
387         mv $DIR/d15/f $DIR/d15/f2
388         $CHECKSTAT -t file $DIR/d15/f2 || error
389 }
390 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
391
392 test_16() {
393         mkdir $DIR/d16
394         touch $DIR/d16/f
395         rm -rf $DIR/d16/f
396         $CHECKSTAT -a $DIR/d16/f || error
397 }
398 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
399
400 test_17a() {
401         mkdir -p $DIR/d17
402         touch $DIR/d17/f
403         ln -s $DIR/d17/f $DIR/d17/l-exist
404         ls -l $DIR/d17
405         $CHECKSTAT -l $DIR/d17/f $DIR/d17/l-exist || error
406         $CHECKSTAT -f -t f $DIR/d17/l-exist || error
407         rm -f $DIR/d17/l-exist
408         $CHECKSTAT -a $DIR/d17/l-exist || error
409 }
410 run_test 17a "symlinks: create, remove (real) =================="
411
412 test_17b() {
413         mkdir -p $DIR/d17
414         ln -s no-such-file $DIR/d17/l-dangle
415         ls -l $DIR/d17
416         $CHECKSTAT -l no-such-file $DIR/d17/l-dangle || error
417         $CHECKSTAT -fa $DIR/d17/l-dangle || error
418         rm -f $DIR/d17/l-dangle
419         $CHECKSTAT -a $DIR/d17/l-dangle || error
420 }
421 run_test 17b "symlinks: create, remove (dangling) =============="
422
423 test_17c() { # bug 3440 - don't save failed open RPC for replay
424         mkdir -p $DIR/d17
425         ln -s foo $DIR/d17/f17c
426         cat $DIR/d17/f17c && error "opened non-existent symlink" || true
427 }
428 run_test 17c "symlinks: open dangling (should return error) ===="
429
430 test_17d() {
431         mkdir -p $DIR/d17
432         ln -s foo $DIR/d17/f17d
433         touch $DIR/d17/f17d || error "creating to new symlink"
434 }
435 run_test 17d "symlinks: create dangling ========================"
436
437 test_17e() {
438         mkdir -p $DIR/$tdir
439         local foo=$DIR/$tdir/$tfile
440         ln -s $foo $foo || error "create symlink failed"
441         ls -l $foo || error "ls -l failed"
442         ls $foo && error "ls not failed" || true
443 }
444 run_test 17e "symlinks: create recursive symlink (should return error) ===="
445
446 test_17g() {
447         mkdir -p $DIR/$tdir
448         LONGSYMLINK="$(dd if=/dev/zero bs=4095 count=1 | tr '\0' 'x')"
449         ln -s $LONGSYMLINK $DIR/$tdir/$tfile
450         ls -l $DIR/$tdir
451 }
452 run_test 17g "symlinks: really long symlink name ==============================="
453
454 test_17h() { #bug 17378
455         mkdir -p $DIR/$tdir
456         $SETSTRIPE $DIR/$tdir -c -1
457 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
458         do_facet mds lctl set_param fail_loc=0x80000141
459         touch $DIR/$tdir/$tfile || true
460 }
461 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
462
463 test_17i() { #bug 20018
464         mkdir -p $DIR/$tdir
465         local foo=$DIR/$tdir/$tfile
466         ln -s $foo $foo || error "create symlink failed"
467 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
468         do_facet mds lctl set_param fail_loc=0x80000143
469         ls -l $foo && error "error not detected"
470         return 0
471 }
472 run_test 17i "don't panic on short symlink"
473
474 test_17k() { #bug 22301
475         rsync --help | grep -q xattr ||
476                 skip_env "$(rsync --version| head -1) does not support xattrs"
477         mkdir -p $DIR/{$tdir,$tdir.new}
478         touch $DIR/$tdir/$tfile
479         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
480         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
481                 error "rsync failed with xattrs enabled"
482 }
483 run_test 17k "symlinks: rsync with xattrs enabled ========================="
484
485 test_18() {
486         touch $DIR/f
487         ls $DIR || error
488 }
489 run_test 18 "touch .../f ; ls ... =============================="
490
491 test_19a() {
492         touch $DIR/f19
493         ls -l $DIR
494         rm $DIR/f19
495         $CHECKSTAT -a $DIR/f19 || error
496 }
497 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
498
499 test_19b() {
500         ls -l $DIR/f19 && error || true
501 }
502 run_test 19b "ls -l .../f19 (should return error) =============="
503
504 test_19c() {
505         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
506         $RUNAS touch $DIR/f19 && error || true
507 }
508 run_test 19c "$RUNAS touch .../f19 (should return error) =="
509
510 test_19d() {
511         cat $DIR/f19 && error || true
512 }
513 run_test 19d "cat .../f19 (should return error) =============="
514
515 test_20() {
516         touch $DIR/f
517         rm $DIR/f
518         log "1 done"
519         touch $DIR/f
520         rm $DIR/f
521         log "2 done"
522         touch $DIR/f
523         rm $DIR/f
524         log "3 done"
525         $CHECKSTAT -a $DIR/f || error
526 }
527 run_test 20 "touch .../f ; ls -l ... ==========================="
528
529 test_21() {
530         mkdir $DIR/d21
531         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
532         ln -s dangle $DIR/d21/link
533         echo foo >> $DIR/d21/link
534         cat $DIR/d21/dangle
535         $CHECKSTAT -t link $DIR/d21/link || error
536         $CHECKSTAT -f -t file $DIR/d21/link || error
537 }
538 run_test 21 "write to dangling link ============================"
539
540 test_22() {
541         WDIR=$DIR/$tdir
542         mkdir -p $WDIR
543         chown $RUNAS_ID $WDIR
544         (cd $WDIR || error "cd $WDIR failed";
545         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
546         $RUNAS tar xf -)
547         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
548         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
549         $CHECKSTAT -u \#$RUNAS_ID $WDIR/etc || error "checkstat -u failed"
550 }
551 run_test 22 "unpack tar archive as non-root user ==============="
552
553 # was test_23
554 test_23a() {
555         mkdir -p $DIR/$tdir
556         local file=$DIR/$tdir/$tfile
557
558         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
559         openfile -f O_CREAT:O_EXCL $file &&
560                 error "$file recreate succeeded" || true
561 }
562 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
563
564 test_23b() { # bug 18988
565         mkdir -p $DIR/$tdir
566         local file=$DIR/$tdir/$tfile
567
568         rm -f $file
569         echo foo > $file || error "write filed"
570         echo bar >> $file || error "append filed"
571         $CHECKSTAT -s 8 $file || error "wrong size"
572         rm $file
573 }
574 run_test 23b "O_APPEND check =========================="
575
576 test_24a() {
577         echo '== rename sanity =============================================='
578         echo '-- same directory rename'
579         mkdir $DIR/R1
580         touch $DIR/R1/f
581         mv $DIR/R1/f $DIR/R1/g
582         $CHECKSTAT -t file $DIR/R1/g || error
583 }
584 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
585
586 test_24b() {
587         mkdir $DIR/R2
588         touch $DIR/R2/{f,g}
589         mv $DIR/R2/f $DIR/R2/g
590         $CHECKSTAT -a $DIR/R2/f || error
591         $CHECKSTAT -t file $DIR/R2/g || error
592 }
593 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
594
595 test_24c() {
596         mkdir $DIR/R3
597         mkdir $DIR/R3/f
598         mv $DIR/R3/f $DIR/R3/g
599         $CHECKSTAT -a $DIR/R3/f || error
600         $CHECKSTAT -t dir $DIR/R3/g || error
601 }
602 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
603
604 test_24d() {
605         mkdir $DIR/R4
606         mkdir $DIR/R4/{f,g}
607         mrename $DIR/R4/f $DIR/R4/g
608         $CHECKSTAT -a $DIR/R4/f || error
609         $CHECKSTAT -t dir $DIR/R4/g || error
610 }
611 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
612
613 test_24e() {
614         echo '-- cross directory renames --'
615         mkdir $DIR/R5{a,b}
616         touch $DIR/R5a/f
617         mv $DIR/R5a/f $DIR/R5b/g
618         $CHECKSTAT -a $DIR/R5a/f || error
619         $CHECKSTAT -t file $DIR/R5b/g || error
620 }
621 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
622
623 test_24f() {
624         mkdir $DIR/R6{a,b}
625         touch $DIR/R6a/f $DIR/R6b/g
626         mv $DIR/R6a/f $DIR/R6b/g
627         $CHECKSTAT -a $DIR/R6a/f || error
628         $CHECKSTAT -t file $DIR/R6b/g || error
629 }
630 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
631
632 test_24g() {
633         mkdir $DIR/R7{a,b}
634         mkdir $DIR/R7a/d
635         mv $DIR/R7a/d $DIR/R7b/e
636         $CHECKSTAT -a $DIR/R7a/d || error
637         $CHECKSTAT -t dir $DIR/R7b/e || error
638 }
639 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R5b/e ======"
640
641 test_24h() {
642         mkdir $DIR/R8{a,b}
643         mkdir $DIR/R8a/d $DIR/R8b/e
644         mrename $DIR/R8a/d $DIR/R8b/e
645         $CHECKSTAT -a $DIR/R8a/d || error
646         $CHECKSTAT -t dir $DIR/R8b/e || error
647 }
648 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
649
650 test_24i() {
651         echo "-- rename error cases"
652         mkdir $DIR/R9
653         mkdir $DIR/R9/a
654         touch $DIR/R9/f
655         mrename $DIR/R9/f $DIR/R9/a
656         $CHECKSTAT -t file $DIR/R9/f || error
657         $CHECKSTAT -t dir  $DIR/R9/a || error
658         $CHECKSTAT -a $DIR/R9/a/f || error
659 }
660 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
661
662 test_24j() {
663         mkdir $DIR/R10
664         mrename $DIR/R10/f $DIR/R10/g
665         $CHECKSTAT -t dir $DIR/R10 || error
666         $CHECKSTAT -a $DIR/R10/f || error
667         $CHECKSTAT -a $DIR/R10/g || error
668 }
669 run_test 24j "source does not exist ============================"
670
671 test_24k() {
672         mkdir $DIR/R11a $DIR/R11a/d
673         touch $DIR/R11a/f
674         mv $DIR/R11a/f $DIR/R11a/d
675         $CHECKSTAT -a $DIR/R11a/f || error
676         $CHECKSTAT -t file $DIR/R11a/d/f || error
677 }
678 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
679
680 # bug 2429 - rename foo foo foo creates invalid file
681 test_24l() {
682         f="$DIR/f24l"
683         multiop $f OcNs || error
684 }
685 run_test 24l "Renaming a file to itself ========================"
686
687 test_24m() {
688         f="$DIR/f24m"
689         multiop $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
690         # on ext3 this does not remove either the source or target files
691         # though the "expected" operation would be to remove the source
692         $CHECKSTAT -t file ${f} || error "${f} missing"
693         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
694 }
695 run_test 24m "Renaming a file to a hard link to itself ========="
696
697 test_24n() {
698     f="$DIR/f24n"
699     # this stats the old file after it was renamed, so it should fail
700     touch ${f}
701     $CHECKSTAT ${f}
702     mv ${f} ${f}.rename
703     $CHECKSTAT ${f}.rename
704     $CHECKSTAT -a ${f}
705 }
706 run_test 24n "Statting the old file after renaming (Posix rename 2)"
707
708 test_24o() {
709         check_kernel_version 37 || return 0
710         mkdir -p $DIR/d24o
711         rename_many -s random -v -n 10 $DIR/d24o
712 }
713 run_test 24o "rename of files during htree split ==============="
714
715 test_24p() {
716         mkdir $DIR/R12{a,b}
717         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
718         mrename $DIR/R12a $DIR/R12b
719         $CHECKSTAT -a $DIR/R12a || error
720         $CHECKSTAT -t dir $DIR/R12b || error
721         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
722         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
723 }
724 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
725
726 test_24q() {
727         mkdir $DIR/R13{a,b}
728         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
729         multiop_bg_pause $DIR/R13b D_c || return 1
730         MULTIPID=$!
731
732         mrename $DIR/R13a $DIR/R13b
733         $CHECKSTAT -a $DIR/R13a || error
734         $CHECKSTAT -t dir $DIR/R13b || error
735         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
736         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
737         kill -USR1 $MULTIPID
738         wait $MULTIPID || error "multiop close failed"
739 }
740 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
741
742 test_24r() { #bug 3789
743         mkdir $DIR/R14a $DIR/R14a/b
744         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
745         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
746         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
747 }
748 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
749
750 test_24s() {
751         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
752         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
753         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
754         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
755 }
756 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
757 test_24t() {
758         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
759         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
760         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
761         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
762 }
763 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
764
765 test_24u() { # bug12192
766         multiop $DIR/$tfile C2w$((2048 * 1024))c || error
767         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
768 }
769 run_test 24u "create stripe file"
770
771 test_25a() {
772         echo '== symlink sanity ============================================='
773
774         mkdir $DIR/d25
775         ln -s d25 $DIR/s25
776         touch $DIR/s25/foo || error
777 }
778 run_test 25a "create file in symlinked directory ==============="
779
780 test_25b() {
781         [ ! -d $DIR/d25 ] && test_25a
782         $CHECKSTAT -t file $DIR/s25/foo || error
783 }
784 run_test 25b "lookup file in symlinked directory ==============="
785
786 test_26a() {
787         mkdir $DIR/d26
788         mkdir $DIR/d26/d26-2
789         ln -s d26/d26-2 $DIR/s26
790         touch $DIR/s26/foo || error
791 }
792 run_test 26a "multiple component symlink ======================="
793
794 test_26b() {
795         mkdir -p $DIR/d26b/d26-2
796         ln -s d26b/d26-2/foo $DIR/s26-2
797         touch $DIR/s26-2 || error
798 }
799 run_test 26b "multiple component symlink at end of lookup ======"
800
801 test_26c() {
802         mkdir $DIR/d26.2
803         touch $DIR/d26.2/foo
804         ln -s d26.2 $DIR/s26.2-1
805         ln -s s26.2-1 $DIR/s26.2-2
806         ln -s s26.2-2 $DIR/s26.2-3
807         chmod 0666 $DIR/s26.2-3/foo
808 }
809 run_test 26c "chain of symlinks ================================"
810
811 # recursive symlinks (bug 439)
812 test_26d() {
813         ln -s d26-3/foo $DIR/d26-3
814 }
815 run_test 26d "create multiple component recursive symlink ======"
816
817 test_26e() {
818         [ ! -h $DIR/d26-3 ] && test_26d
819         rm $DIR/d26-3
820 }
821 run_test 26e "unlink multiple component recursive symlink ======"
822
823 # recursive symlinks (bug 7022)
824 test_26f() {
825         mkdir -p $DIR/$tdir
826         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
827         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
828         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
829         mkdir $tfile             || error "mkdir $tfile failed"
830         cd $tfile                || error "cd $tfile failed"
831         ln -s .. dotdot          || error "ln dotdot failed"
832         ln -s dotdot/lndir lndir || error "ln lndir failed"
833         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
834         output=`ls $tfile/$tfile/lndir/bar1`
835         [ "$output" = bar1 ] && error "unexpected output"
836         rm -r $tfile             || error "rm $tfile failed"
837         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
838 }
839 run_test 26f "rm -r of a directory which has recursive symlink ="
840
841 test_27a() {
842         echo '== stripe sanity =============================================='
843         mkdir -p $DIR/d27 || error "mkdir failed"
844         $GETSTRIPE $DIR/d27
845         $SETSTRIPE $DIR/d27/f0 -c 1 || error "lstripe failed"
846         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
847         pass
848         log "== test 27b: write to one stripe file ========================="
849         cp /etc/hosts $DIR/d27/f0 || error
850 }
851 run_test 27a "one stripe file =================================="
852
853 test_27c() {
854         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
855         mkdir -p $DIR/d27
856         $SETSTRIPE $DIR/d27/f01 -c 2 || error "lstripe failed"
857         [ `$GETSTRIPE $DIR/d27/f01 | grep -A 10 obdidx | wc -l` -eq 4 ] ||
858                 error "two-stripe file doesn't have two stripes"
859         pass
860         log "== test 27d: write to two stripe file file f01 ================"
861         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
862 }
863 run_test 27c "create two stripe file f01 ======================="
864
865 test_27d() {
866         mkdir -p $DIR/d27
867         $SETSTRIPE -c0 -i-1 -s0 $DIR/d27/fdef || error "lstripe failed"
868         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
869         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
870 }
871 run_test 27d "create file with default settings ================"
872
873 test_27e() {
874         mkdir -p $DIR/d27
875         $SETSTRIPE $DIR/d27/f12 -c 2 || error "lstripe failed"
876         $SETSTRIPE $DIR/d27/f12 -c 2 && error "lstripe succeeded twice"
877         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
878 }
879 run_test 27e "setstripe existing file (should return error) ======"
880
881 test_27f() {
882         mkdir -p $DIR/d27
883         $SETSTRIPE $DIR/d27/fbad -s 100 -i 0 -c 1 && error "lstripe failed"
884         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
885         $GETSTRIPE $DIR/d27/fbad || error "lfs getstripe failed"
886 }
887 run_test 27f "setstripe with bad stripe size (should return error)"
888
889 test_27g() {
890         mkdir -p $DIR/d27
891         $MCREATE $DIR/d27/fnone || error "mcreate failed"
892         pass
893         log "== test 27h: lfs getstripe with no objects ===================="
894         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" || error "has object"
895         pass
896         log "== test 27i: lfs getstripe with some objects =================="
897         touch $DIR/d27/fsome || error "touch failed"
898         $GETSTRIPE $DIR/d27/fsome | grep obdidx || error "missing objects"
899 }
900 run_test 27g "test lfs getstripe ==========================================="
901
902 test_27j() {
903         mkdir -p $DIR/d27
904         $SETSTRIPE $DIR/d27/f27j -i $OSTCOUNT && error "lstripe failed"||true
905 }
906 run_test 27j "setstripe with bad stripe offset (should return error)"
907
908 test_27k() { # bug 2844
909         mkdir -p $DIR/d27
910         FILE=$DIR/d27/f27k
911         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
912         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
913         $SETSTRIPE $FILE -s 67108864 || error "lstripe failed"
914         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
915         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
916         dd if=/dev/zero of=$FILE bs=4k count=1
917         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
918         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
919 }
920 run_test 27k "limit i_blksize for broken user apps ============="
921
922 test_27l() {
923         mkdir -p $DIR/d27
924         mcreate $DIR/f27l || error "creating file"
925         $RUNAS $SETSTRIPE $DIR/f27l -c 1 && \
926                 error "lstripe should have failed" || true
927 }
928 run_test 27l "check setstripe permissions (should return error)"
929
930 test_27m() {
931         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
932         if [ $ORIGFREE -gt $MAXFREE ]; then
933                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
934                 return
935         fi
936         mkdir -p $DIR/d27
937         $SETSTRIPE $DIR/d27/f27m_1 -i 0 -c 1
938         dd if=/dev/zero of=$DIR/d27/f27m_1 bs=1024 count=$MAXFREE && \
939                 error "dd should fill OST0"
940         i=2
941         while $SETSTRIPE $DIR/d27/f27m_$i -i 0 -c 1 ; do
942                 i=`expr $i + 1`
943                 [ $i -gt 256 ] && break
944         done
945         i=`expr $i + 1`
946         touch $DIR/d27/f27m_$i
947         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
948                 error "OST0 was full but new created file still use it"
949         i=`expr $i + 1`
950         touch $DIR/d27/f27m_$i
951         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
952                 error "OST0 was full but new created file still use it"
953         rm -r $DIR/d27
954 }
955 run_test 27m "create file while OST0 was full =================="
956
957 sleep_maxage() {
958         local DELAY=$(do_facet mds lctl get_param -n lov.*.qos_maxage | awk '{print $1 * 2}')
959         sleep $DELAY
960 }
961
962 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
963 # if the OST isn't full anymore.
964 reset_enospc() {
965         local OSTIDX=${1:-""}
966
967         local list=$(comma_list $(osts_nodes))
968         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
969
970         do_nodes $list lctl set_param fail_loc=0
971         sleep_maxage
972 }
973
974 exhaust_precreations() {
975         local OSTIDX=$1
976         local FAILLOC=$2
977         local FAILIDX=${3:-$OSTIDX}
978
979         local OST=$(lfs osts | grep ${OSTIDX}": " | \
980                 awk '{print $2}' | sed -e 's/_UUID$//')
981
982         # on the mdt's osc
983         local mdtosc=$(get_mdtosc_proc_path $OST)
984         local last_id=$(do_facet mds lctl get_param -n osc.$mdtosc.prealloc_last_id)
985         local next_id=$(do_facet mds lctl get_param -n osc.$mdtosc.prealloc_next_id)
986
987         mkdir -p $DIR/$tdir/${OST}
988         $SETSTRIPE $DIR/$tdir/${OST} -i $OSTIDX -c 1
989         #define OBD_FAIL_OST_ENOSPC 0x215
990         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
991         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
992         echo "Creating to objid $last_id on ost $OST..."
993         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
994         do_facet mds "lctl get_param -n osc.$mdtosc.prealloc*" | grep '[0-9]'
995         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
996         sleep_maxage
997 }
998
999 exhaust_all_precreations() {
1000         local i
1001         for (( i=0; i < OSTCOUNT; i++ )) ; do
1002                 exhaust_precreations $i $1 -1
1003         done
1004 }
1005
1006 test_27n() {
1007         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1008         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1009         remote_ost_nodsh && skip "remote OST with nodsh" && return
1010
1011         reset_enospc
1012         rm -f $DIR/$tdir/$tfile
1013         exhaust_precreations 0 0x80000215
1014         $SETSTRIPE -c -1 $DIR/$tdir
1015         touch $DIR/$tdir/$tfile || error
1016         $GETSTRIPE $DIR/$tdir/$tfile
1017         reset_enospc
1018 }
1019 run_test 27n "create file with some full OSTs =================="
1020
1021 test_27o() {
1022         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1023         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1024         remote_ost_nodsh && skip "remote OST with nodsh" && return
1025
1026         reset_enospc
1027         rm -f $DIR/$tdir/$tfile
1028         exhaust_all_precreations 0x215
1029
1030         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1031
1032         reset_enospc
1033         rm -rf $DIR/$tdir/*
1034 }
1035 run_test 27o "create file with all full OSTs (should error) ===="
1036
1037 test_27p() {
1038         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1039         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1040         remote_ost_nodsh && skip "remote OST with nodsh" && return
1041
1042         reset_enospc
1043         rm -f $DIR/$tdir/$tfile
1044         mkdir -p $DIR/$tdir
1045
1046         $MCREATE $DIR/$tdir/$tfile || error
1047         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error
1048         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error
1049
1050         exhaust_precreations 0 0x80000215
1051         echo foo >> $DIR/$tdir/$tfile || error
1052         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error
1053
1054         reset_enospc
1055 }
1056 run_test 27p "append to a truncated file with some full OSTs ==="
1057
1058 test_27q() {
1059         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1060         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1061         remote_ost_nodsh && skip "remote OST with nodsh" && return
1062
1063         reset_enospc
1064         rm -f $DIR/$tdir/$tfile
1065
1066         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1067         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1068         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1069
1070         exhaust_all_precreations 0x215
1071
1072         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1073         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1074
1075         reset_enospc
1076 }
1077 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1078
1079 test_27r() {
1080         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1081         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1082         remote_ost_nodsh && skip "remote OST with nodsh" && return
1083
1084         reset_enospc
1085         rm -f $DIR/$tdir/$tfile
1086         exhaust_precreations 0 0x80000215
1087
1088         $SETSTRIPE $DIR/$tdir/$tfile -i 0 -c 2 # && error
1089
1090         reset_enospc
1091 }
1092 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1093
1094 test_27s() { # bug 10725
1095         mkdir -p $DIR/$tdir
1096         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1097         local stripe_count=0
1098         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1099         $SETSTRIPE $DIR/$tdir -s $stripe_size -c $stripe_count && \
1100                 error "stripe width >= 2^32 succeeded" || true
1101 }
1102 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1103
1104 test_27t() { # bug 10864
1105         WDIR=`pwd`
1106         WLFS=`which lfs`
1107         cd $DIR
1108         touch $tfile
1109         $WLFS getstripe $tfile
1110         cd $WDIR
1111 }
1112 run_test 27t "check that utils parse path correctly"
1113
1114 test_27u() { # bug 4900
1115         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1116         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1117
1118         #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1119
1120         do_facet mds lctl set_param fail_loc=0x139
1121         mkdir -p $DIR/$tdir
1122         createmany -o $DIR/$tdir/t- 1000
1123         do_facet mds lctl set_param fail_loc=0
1124
1125         TLOG=$DIR/$tfile.getstripe
1126         $GETSTRIPE $DIR/$tdir > $TLOG
1127         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1128         unlinkmany $DIR/$tdir/t- 1000
1129         [ $OBJS -gt 0 ] && \
1130                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1131 }
1132 run_test 27u "skip object creation on OSC w/o objects =========="
1133
1134 test_27v() { # bug 4900
1135         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1136         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1137         remote_ost_nodsh && skip "remote OST with nodsh" && return
1138
1139         exhaust_all_precreations 0x215
1140         reset_enospc
1141
1142         mkdir -p $DIR/$tdir
1143         $SETSTRIPE $DIR/$tdir -c 1         # 1 stripe / file
1144
1145         touch $DIR/$tdir/$tfile
1146         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1147         # all except ost1
1148         for (( i=0; i < OSTCOUNT; i++ )) ; do
1149                 do_facet ost$i lctl set_param fail_loc=0x705
1150         done
1151         local START=`date +%s`
1152         createmany -o $DIR/$tdir/$tfile 32
1153
1154         local FINISH=`date +%s`
1155         local TIMEOUT=`lctl get_param -n timeout`
1156         [ $((FINISH - START)) -ge $((TIMEOUT / 2)) ] && \
1157                error "$FINISH - $START >= $TIMEOUT / 2"
1158
1159         reset_enospc
1160 }
1161 run_test 27v "skip object creation on slow OST ================="
1162
1163 test_27w() { # bug 10997
1164         mkdir -p $DIR/$tdir || error "mkdir failed"
1165         $SETSTRIPE $DIR/$tdir/f0 -s 65536 || error "lstripe failed"
1166         size=`$GETSTRIPE $DIR/$tdir/f0 -s`
1167         [ $size -ne 65536 ] && error "stripe size $size != 65536" || true
1168         gsdir=$($LFS getstripe -d $DIR/$tdir)
1169         [ $(echo $gsdir | grep -c stripe_count) -ne 1 ] && error "$LFS getstripe -d $DIR/$tdir failed"
1170
1171         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping multiple stripe count/offset test" && return
1172         for i in `seq 1 $OSTCOUNT`; do
1173                 offset=$(($i-1))
1174                 log setstripe $DIR/$tdir/f$i -c $i -i $offset
1175                 $SETSTRIPE $DIR/$tdir/f$i -c $i -i $offset || error "lstripe -c $i -i $offset failed"
1176                 count=`$GETSTRIPE -c $DIR/$tdir/f$i`
1177                 index=`$GETSTRIPE -o $DIR/$tdir/f$i`
1178                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1179                 [ $index -ne $offset ] && error "stripe offset $index != $offset" || true
1180         done
1181 }
1182 run_test 27w "check lfs setstripe -c -s -i options ============="
1183
1184 test_27x() {
1185         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1186         OFFSET=$(($OSTCOUNT - 1))
1187         OSTIDX=0
1188         local OST=$(lfs osts | awk '/'${OSTIDX}': / { print $2 }' | sed -e 's/_UUID$//')
1189
1190         mkdir -p $DIR/$tdir
1191         $SETSTRIPE $DIR/$tdir -c 1      # 1 stripe per file
1192         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1193         sleep_maxage
1194         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1195         for i in `seq 0 $OFFSET`; do
1196                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1197                 error "OST0 was degraded but new created file still use it"
1198         done
1199         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1200 }
1201 run_test 27x "create files while OST0 is degraded"
1202
1203 test_27y() {
1204         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1205         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1206
1207         MDS_OSCS=`do_facet mds lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
1208         OFFSET=$(($OSTCOUNT-1))
1209         OST=-1
1210         for OSC in $MDS_OSCS; do
1211                 if [ $OST == -1 ]; then {
1212                         OST=`osc_to_ost $OSC`
1213                 } else {
1214                         echo $OSC "is Deactivate:"
1215                         do_facet mds lctl --device  %$OSC deactivate
1216                 } fi
1217         done
1218
1219         OSTIDX=$(lfs osts | grep ${OST} | awk '{print $1}' | sed -e 's/://')
1220         mkdir -p $DIR/$tdir
1221         $SETSTRIPE $DIR/$tdir -c 1      # 1 stripe / file
1222
1223         do_facet ost$OSTIDX lctl set_param -n obdfilter.$OST.degraded 1
1224         sleep_maxage
1225         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1226         do_facet ost$OSTIDX lctl set_param -n obdfilter.$OST.degraded 0
1227
1228         for i in `seq 0 $OFFSET`; do
1229                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "$OSTIDX"` ] || \
1230                       error "files created on deactivated OSTs instead of degraded OST"
1231         done
1232         for OSC in $MDS_OSCS; do
1233                 [ `osc_to_ost $OSC` != $OST  ] && {
1234                         echo $OSC "is activate"
1235                         do_facet mds lctl --device %$OSC activate
1236                 }
1237         done
1238 }
1239 run_test 27y "create files while OST0 is degraded and the rest inactive"
1240
1241 # createtest also checks that device nodes are created and
1242 # then visible correctly (#2091)
1243 test_28() { # bug 2091
1244         mkdir $DIR/d28
1245         $CREATETEST $DIR/d28/ct || error
1246 }
1247 run_test 28 "create/mknod/mkdir with bad file types ============"
1248
1249 test_29() {
1250         cancel_lru_locks mdc
1251         mkdir $DIR/d29
1252         touch $DIR/d29/foo
1253         log 'first d29'
1254         ls -l $DIR/d29
1255         LOCKCOUNTORIG=`lctl get_param -n ldlm.namespaces.*mdc*.lock_count`
1256         LOCKUNUSEDCOUNTORIG=`lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count`
1257         [ -z $"LOCKCOUNTORIG" ] && error "No mdc lock count" && return 1
1258         log 'second d29'
1259         ls -l $DIR/d29
1260         log 'done'
1261         LOCKCOUNTCURRENT=`lctl get_param -n ldlm.namespaces.*mdc*.lock_count`
1262         LOCKUNUSEDCOUNTCURRENT=`lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count`
1263         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1264                 lctl set_param -n ldlm.dump_namespaces ""
1265                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1266                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1267                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1268                 return 2
1269         fi
1270         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1271                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1272                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1273                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1274                 return 3
1275         fi
1276 }
1277 run_test 29 "IT_GETATTR regression  ============================"
1278
1279 test_30() {
1280         cp `which ls` $DIR
1281         $DIR/ls /
1282         rm $DIR/ls
1283 }
1284 run_test 30 "run binary from Lustre (execve) ==================="
1285
1286 test_31a() {
1287         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1288         $CHECKSTAT -a $DIR/f31 || error
1289 }
1290 run_test 31a "open-unlink file =================================="
1291
1292 test_31b() {
1293         touch $DIR/f31 || error
1294         ln $DIR/f31 $DIR/f31b || error
1295         multiop $DIR/f31b Ouc || error
1296         $CHECKSTAT -t file $DIR/f31 || error
1297 }
1298 run_test 31b "unlink file with multiple links while open ======="
1299
1300 test_31c() {
1301         touch $DIR/f31 || error
1302         ln $DIR/f31 $DIR/f31c || error
1303         multiop_bg_pause $DIR/f31 O_uc || return 1
1304         MULTIPID=$!
1305         multiop $DIR/f31c Ouc
1306         kill -USR1 $MULTIPID
1307         wait $MULTIPID
1308 }
1309 run_test 31c "open-unlink file with multiple links ============="
1310
1311 test_31d() {
1312         opendirunlink $DIR/d31d $DIR/d31d || error
1313         $CHECKSTAT -a $DIR/d31d || error
1314 }
1315 run_test 31d "remove of open directory ========================="
1316
1317 test_31e() { # bug 2904
1318         check_kernel_version 34 || return 0
1319         openfilleddirunlink $DIR/d31e || error
1320 }
1321 run_test 31e "remove of open non-empty directory ==============="
1322
1323 test_31f() { # bug 4554
1324         set -vx
1325         mkdir $DIR/d31f
1326         $SETSTRIPE $DIR/d31f -s 1048576 -c 1
1327         cp /etc/hosts $DIR/d31f
1328         ls -l $DIR/d31f
1329         $GETSTRIPE $DIR/d31f/hosts
1330         multiop_bg_pause $DIR/d31f D_c || return 1
1331         MULTIPID=$!
1332
1333         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1334         mkdir $DIR/d31f
1335         $SETSTRIPE $DIR/d31f -s 1048576 -c 1
1336         cp /etc/hosts $DIR/d31f
1337         ls -l $DIR/d31f
1338         $DIR/d31f/hosts
1339         multiop_bg_pause $DIR/d31f D_c || return 1
1340         MULTIPID2=$!
1341
1342         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1343         wait $MULTIPID || error "first opendir $MULTIPID failed"
1344
1345         sleep 6
1346
1347         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1348         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1349         set +vx
1350 }
1351 run_test 31f "remove of open directory with open-unlink file ==="
1352
1353 test_32a() {
1354         echo "== more mountpoints and symlinks ================="
1355         [ -e $DIR/d32a ] && rm -fr $DIR/d32a
1356         mkdir -p $DIR/d32a/ext2-mountpoint
1357         mount -t ext2 -o loop $EXT2_DEV $DIR/d32a/ext2-mountpoint || error
1358         $CHECKSTAT -t dir $DIR/d32a/ext2-mountpoint/.. || error
1359         $UMOUNT $DIR/d32a/ext2-mountpoint || error
1360 }
1361 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1362
1363 test_32b() {
1364         [ -e $DIR/d32b ] && rm -fr $DIR/d32b
1365         mkdir -p $DIR/d32b/ext2-mountpoint
1366         mount -t ext2 -o loop $EXT2_DEV $DIR/d32b/ext2-mountpoint || error
1367         ls -al $DIR/d32b/ext2-mountpoint/.. || error
1368         $UMOUNT $DIR/d32b/ext2-mountpoint || error
1369 }
1370 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1371
1372 test_32c() {
1373         [ -e $DIR/d32c ] && rm -fr $DIR/d32c
1374         mkdir -p $DIR/d32c/ext2-mountpoint
1375         mount -t ext2 -o loop $EXT2_DEV $DIR/d32c/ext2-mountpoint || error
1376         mkdir -p $DIR/d32c/d2/test_dir
1377         $CHECKSTAT -t dir $DIR/d32c/ext2-mountpoint/../d2/test_dir || error
1378         $UMOUNT $DIR/d32c/ext2-mountpoint || error
1379 }
1380 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1381
1382 test_32d() {
1383         [ -e $DIR/d32d ] && rm -fr $DIR/d32d
1384         mkdir -p $DIR/d32d/ext2-mountpoint
1385         mount -t ext2 -o loop $EXT2_DEV $DIR/d32d/ext2-mountpoint || error
1386         mkdir -p $DIR/d32d/d2/test_dir
1387         ls -al $DIR/d32d/ext2-mountpoint/../d2/test_dir || error
1388         $UMOUNT $DIR/d32d/ext2-mountpoint || error
1389 }
1390 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1391
1392 test_32e() {
1393         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1394         mkdir -p $DIR/d32e/tmp
1395         TMP_DIR=$DIR/d32e/tmp
1396         ln -s $DIR/d32e $TMP_DIR/symlink11
1397         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1398         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1399         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1400 }
1401 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1402
1403 test_32f() {
1404         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1405         mkdir -p $DIR/d32f/tmp
1406         TMP_DIR=$DIR/d32f/tmp
1407         ln -s $DIR/d32f $TMP_DIR/symlink11
1408         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1409         ls $DIR/d32f/tmp/symlink11  || error
1410         ls $DIR/d32f/symlink01 || error
1411 }
1412 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
1413
1414 test_32g() {
1415         TMP_DIR=$DIR/$tdir/tmp
1416         mkdir -p $TMP_DIR $DIR/${tdir}2
1417         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1418         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1419         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
1420         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
1421         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
1422         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
1423 }
1424 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1425
1426 test_32h() {
1427         rm -fr $DIR/$tdir $DIR/${tdir}2
1428         TMP_DIR=$DIR/$tdir/tmp
1429         mkdir -p $TMP_DIR $DIR/${tdir}2
1430         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1431         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1432         ls $TMP_DIR/symlink12 || error
1433         ls $DIR/$tdir/symlink02  || error
1434 }
1435 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1436
1437 test_32i() {
1438         [ -e $DIR/d32i ] && rm -fr $DIR/d32i
1439         mkdir -p $DIR/d32i/ext2-mountpoint
1440         mount -t ext2 -o loop $EXT2_DEV $DIR/d32i/ext2-mountpoint || error
1441         touch $DIR/d32i/test_file
1442         $CHECKSTAT -t file $DIR/d32i/ext2-mountpoint/../test_file || error
1443         $UMOUNT $DIR/d32i/ext2-mountpoint || error
1444 }
1445 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
1446
1447 test_32j() {
1448         [ -e $DIR/d32j ] && rm -fr $DIR/d32j
1449         mkdir -p $DIR/d32j/ext2-mountpoint
1450         mount -t ext2 -o loop $EXT2_DEV $DIR/d32j/ext2-mountpoint || error
1451         touch $DIR/d32j/test_file
1452         cat $DIR/d32j/ext2-mountpoint/../test_file || error
1453         $UMOUNT $DIR/d32j/ext2-mountpoint || error
1454 }
1455 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
1456
1457 test_32k() {
1458         rm -fr $DIR/d32k
1459         mkdir -p $DIR/d32k/ext2-mountpoint
1460         mount -t ext2 -o loop $EXT2_DEV $DIR/d32k/ext2-mountpoint
1461         mkdir -p $DIR/d32k/d2
1462         touch $DIR/d32k/d2/test_file || error
1463         $CHECKSTAT -t file $DIR/d32k/ext2-mountpoint/../d2/test_file || error
1464         $UMOUNT $DIR/d32k/ext2-mountpoint || error
1465 }
1466 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
1467
1468 test_32l() {
1469         rm -fr $DIR/d32l
1470         mkdir -p $DIR/d32l/ext2-mountpoint
1471         mount -t ext2 -o loop $EXT2_DEV $DIR/d32l/ext2-mountpoint || error
1472         mkdir -p $DIR/d32l/d2
1473         touch $DIR/d32l/d2/test_file
1474         cat  $DIR/d32l/ext2-mountpoint/../d2/test_file || error
1475         $UMOUNT $DIR/d32l/ext2-mountpoint || error
1476 }
1477 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
1478
1479 test_32m() {
1480         rm -fr $DIR/d32m
1481         mkdir -p $DIR/d32m/tmp
1482         TMP_DIR=$DIR/d32m/tmp
1483         ln -s $DIR $TMP_DIR/symlink11
1484         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1485         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
1486         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
1487 }
1488 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
1489
1490 test_32n() {
1491         rm -fr $DIR/d32n
1492         mkdir -p $DIR/d32n/tmp
1493         TMP_DIR=$DIR/d32n/tmp
1494         ln -s $DIR $TMP_DIR/symlink11
1495         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1496         ls -l $DIR/d32n/tmp/symlink11  || error
1497         ls -l $DIR/d32n/symlink01 || error
1498 }
1499 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
1500
1501 test_32o() {
1502         rm -fr $DIR/d32o $DIR/$tfile
1503         touch $DIR/$tfile
1504         mkdir -p $DIR/d32o/tmp
1505         TMP_DIR=$DIR/d32o/tmp
1506         ln -s $DIR/$tfile $TMP_DIR/symlink12
1507         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1508         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
1509         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
1510         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
1511         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
1512 }
1513 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
1514
1515 test_32p() {
1516     log 32p_1
1517         rm -fr $DIR/d32p
1518     log 32p_2
1519         rm -f $DIR/$tfile
1520     log 32p_3
1521         touch $DIR/$tfile
1522     log 32p_4
1523         mkdir -p $DIR/d32p/tmp
1524     log 32p_5
1525         TMP_DIR=$DIR/d32p/tmp
1526     log 32p_6
1527         ln -s $DIR/$tfile $TMP_DIR/symlink12
1528     log 32p_7
1529         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1530     log 32p_8
1531         cat $DIR/d32p/tmp/symlink12 || error
1532     log 32p_9
1533         cat $DIR/d32p/symlink02 || error
1534     log 32p_10
1535 }
1536 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
1537
1538 test_32q() {
1539         [ -e $DIR/d32q ] && rm -fr $DIR/d32q
1540         mkdir -p $DIR/d32q
1541         touch $DIR/d32q/under_the_mount
1542         mount -t ext2 -o loop $EXT2_DEV $DIR/d32q
1543         ls $DIR/d32q/under_the_mount && error || true
1544         $UMOUNT $DIR/d32q || error
1545 }
1546 run_test 32q "stat follows mountpoints in Lustre (should return error)"
1547
1548 test_32r() {
1549         [ -e $DIR/d32r ] && rm -fr $DIR/d32r
1550         mkdir -p $DIR/d32r
1551         touch $DIR/d32r/under_the_mount
1552         mount -t ext2 -o loop $EXT2_DEV $DIR/d32r
1553         ls $DIR/d32r | grep -q under_the_mount && error || true
1554         $UMOUNT $DIR/d32r || error
1555 }
1556 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
1557
1558 test_33() {
1559         rm -f $DIR/$tfile
1560         touch $DIR/$tfile
1561         chmod 444 $DIR/$tfile
1562         chown $RUNAS_ID $DIR/$tfile
1563         log 33_1
1564         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
1565         log 33_2
1566 }
1567 run_test 33 "write file with mode 444 (should return error) ===="
1568
1569 test_33a() {
1570         rm -fr $DIR/d33
1571         mkdir -p $DIR/d33
1572         chown $RUNAS_ID $DIR/d33
1573         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
1574         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
1575                 error "open RDWR" || true
1576 }
1577 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
1578
1579 test_33b() {
1580         rm -fr $DIR/d33
1581         mkdir -p $DIR/d33
1582         chown $RUNAS_ID $DIR/d33
1583         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
1584 }
1585 run_test 33b "test open file with malformed flags (No panic)"
1586
1587 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
1588 test_34a() {
1589         rm -f $DIR/f34
1590         $MCREATE $DIR/f34 || error
1591         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
1592         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
1593         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
1594         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
1595 }
1596 run_test 34a "truncate file that has not been opened ==========="
1597
1598 test_34b() {
1599         [ ! -f $DIR/f34 ] && test_34a
1600         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
1601         $OPENFILE -f O_RDONLY $DIR/f34
1602         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
1603         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
1604 }
1605 run_test 34b "O_RDONLY opening file doesn't create objects ====="
1606
1607 test_34c() {
1608         [ ! -f $DIR/f34 ] && test_34a
1609         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
1610         $OPENFILE -f O_RDWR $DIR/f34
1611         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
1612         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
1613 }
1614 run_test 34c "O_RDWR opening file-with-size works =============="
1615
1616 test_34d() {
1617         [ ! -f $DIR/f34 ] && test_34a
1618         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
1619         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
1620         rm $DIR/f34
1621 }
1622 run_test 34d "write to sparse file ============================="
1623
1624 test_34e() {
1625         rm -f $DIR/f34e
1626         $MCREATE $DIR/f34e || error
1627         $TRUNCATE $DIR/f34e 1000 || error
1628         $CHECKSTAT -s 1000 $DIR/f34e || error
1629         $OPENFILE -f O_RDWR $DIR/f34e
1630         $CHECKSTAT -s 1000 $DIR/f34e || error
1631 }
1632 run_test 34e "create objects, some with size and some without =="
1633
1634 test_34f() { # bug 6242, 6243
1635         SIZE34F=48000
1636         rm -f $DIR/f34f
1637         $MCREATE $DIR/f34f || error
1638         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
1639         dd if=$DIR/f34f of=$TMP/f34f
1640         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
1641         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
1642         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
1643         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
1644         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
1645 }
1646 run_test 34f "read from a file with no objects until EOF ======="
1647
1648 test_35a() {
1649         cp /bin/sh $DIR/f35a
1650         chmod 444 $DIR/f35a
1651         chown $RUNAS_ID $DIR/f35a
1652         $RUNAS $DIR/f35a && error || true
1653         rm $DIR/f35a
1654 }
1655 run_test 35a "exec file with mode 444 (should return and not leak) ====="
1656
1657 test_36a() {
1658         rm -f $DIR/f36
1659         utime $DIR/f36 || error
1660 }
1661 run_test 36a "MDS utime check (mknod, utime) ==================="
1662
1663 test_36b() {
1664         echo "" > $DIR/f36
1665         utime $DIR/f36 || error
1666 }
1667 run_test 36b "OST utime check (open, utime) ===================="
1668
1669 test_36c() {
1670         rm -f $DIR/d36/f36
1671         mkdir $DIR/d36
1672         chown $RUNAS_ID $DIR/d36
1673         $RUNAS utime $DIR/d36/f36 || error
1674 }
1675 run_test 36c "non-root MDS utime check (mknod, utime) =========="
1676
1677 test_36d() {
1678         [ ! -d $DIR/d36 ] && test_36c
1679         echo "" > $DIR/d36/f36
1680         $RUNAS utime $DIR/d36/f36 || error
1681 }
1682 run_test 36d "non-root OST utime check (open, utime) ==========="
1683
1684 test_36e() {
1685         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
1686         mkdir -p $DIR/$tdir
1687         touch $DIR/$tdir/$tfile
1688         $RUNAS utime $DIR/$tdir/$tfile && \
1689                 error "utime worked, expected failure" || true
1690 }
1691 run_test 36e "utime on non-owned file (should return error) ===="
1692
1693 subr_36fh() {
1694         local fl="$1"
1695         export LANG=C LC_LANG=C # for date language
1696
1697         DATESTR="Dec 20  2000"
1698         mkdir -p $DIR/$tdir
1699         lctl set_param fail_loc=$fl
1700         date; date +%s
1701         cp /etc/hosts $DIR/$tdir/$tfile
1702         sync & # write RPC generated with "current" inode timestamp, but delayed
1703         sleep 1
1704         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
1705         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
1706         cancel_lru_locks osc
1707         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
1708         date; date +%s
1709         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
1710                 echo "BEFORE: $LS_BEFORE" && \
1711                 echo "AFTER : $LS_AFTER" && \
1712                 echo "WANT  : $DATESTR" && \
1713                 error "$DIR/$tdir/$tfile timestamps changed" || true
1714 }
1715
1716 test_36f() {
1717         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
1718         subr_36fh "0x80000214"
1719 }
1720 run_test 36f "utime on file racing with OST BRW write =========="
1721
1722 test_36g() {
1723         remote_ost_nodsh && skip "remote OST with nodsh" && return
1724
1725         mkdir -p $DIR/$tdir
1726         export FMD_MAX_AGE=`do_facet ost1 lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | head -n 1`
1727         FMD_BEFORE="`awk '/ll_fmd_cache/ { print $2 }' /proc/slabinfo`"
1728         touch $DIR/$tdir/$tfile
1729         sleep $((FMD_MAX_AGE + 12))
1730         FMD_AFTER="`awk '/ll_fmd_cache/ { print $2 }' /proc/slabinfo`"
1731         [ "$FMD_AFTER" -gt "$FMD_BEFORE" ] && \
1732                 echo "AFTER : $FMD_AFTER > BEFORE $FMD_BEFORE" && \
1733                 error "fmd didn't expire after ping" || true
1734 }
1735 run_test 36g "filter mod data cache expiry ====================="
1736
1737 test_36h() {
1738         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
1739         subr_36fh "0x80000227"
1740 }
1741 run_test 36h "utime on file racing with OST BRW write =========="
1742
1743 test_37() {
1744         mkdir -p $DIR/$tdir
1745         echo f > $DIR/$tdir/fbugfile
1746         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
1747         ls $DIR/$tdir | grep "\<fbugfile\>" && error
1748         $UMOUNT $DIR/$tdir || error
1749         rm -f $DIR/$tdir/fbugfile || error
1750 }
1751 run_test 37 "ls a mounted file system to check old content ====="
1752
1753 test_38() {
1754         local file=$DIR/$tfile
1755         touch $file
1756         openfile -f O_DIRECTORY $file
1757         local RC=$?
1758         local ENOTDIR=20
1759         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
1760         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
1761 }
1762 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
1763
1764 test_39() {
1765         touch $DIR/$tfile
1766         touch $DIR/${tfile}2
1767 #       ls -l  $DIR/$tfile $DIR/${tfile}2
1768 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
1769 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
1770         sleep 2
1771         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
1772         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
1773                 echo "mtime"
1774                 ls -l  $DIR/$tfile $DIR/${tfile}2
1775                 echo "atime"
1776                 ls -lu  $DIR/$tfile $DIR/${tfile}2
1777                 echo "ctime"
1778                 ls -lc  $DIR/$tfile $DIR/${tfile}2
1779                 error "O_TRUNC didn't change timestamps"
1780         fi
1781 }
1782 run_test 39 "mtime changed on create ==========================="
1783
1784 function get_times() {
1785         FILE=$1
1786         TIME=$2
1787
1788         i=0
1789         for time in `stat -c "%X %Y %Z" $FILE`; do
1790                 eval "$TIME[$i]=$time"
1791                 i=$(($i + 1))
1792         done
1793 }
1794
1795 test_39b() {
1796         ATIME=0
1797         MTIME=1
1798         CTIME=2
1799         mkdir -p $DIR/$tdir
1800         cp -p /etc/passwd $DIR/$tdir/fopen
1801         cp -p /etc/passwd $DIR/$tdir/flink
1802         cp -p /etc/passwd $DIR/$tdir/funlink
1803         cp -p /etc/passwd $DIR/$tdir/frename
1804         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
1805
1806         get_times $DIR/$tdir/fopen OPEN_OLD
1807         get_times $DIR/$tdir/flink LINK_OLD
1808         get_times $DIR/$tdir/funlink UNLINK_OLD
1809         get_times $DIR/$tdir/frename RENAME_OLD
1810
1811         sleep 1
1812         echo "aaaaaa" >> $DIR/$tdir/fopen
1813         echo "aaaaaa" >> $DIR/$tdir/flink
1814         echo "aaaaaa" >> $DIR/$tdir/funlink
1815         echo "aaaaaa" >> $DIR/$tdir/frename
1816
1817         get_times $DIR/$tdir/fopen OPEN_NEW
1818         get_times $DIR/$tdir/flink LINK_NEW
1819         get_times $DIR/$tdir/funlink UNLINK_NEW
1820         get_times $DIR/$tdir/frename RENAME_NEW
1821
1822         cat $DIR/$tdir/fopen > /dev/null
1823         ln $DIR/$tdir/flink $DIR/$tdir/flink2
1824         rm -f $DIR/$tdir/funlink2
1825         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
1826
1827         get_times $DIR/$tdir/fopen OPEN_NEW2
1828         get_times $DIR/$tdir/flink LINK_NEW2
1829         get_times $DIR/$tdir/funlink UNLINK_NEW2
1830         get_times $DIR/$tdir/frename2 RENAME_NEW2
1831         echo ${OPEN_OLD[1]},${OPEN_NEW[$MTIME]},${OPEN_NEW2[$MTIME]}
1832         echo ${LINK_OLD[1]},${LINK_NEW[$MTIME]},${LINK_NEW2[$MTIME]}
1833         echo ${UNLINK_OLD[1]},${UNLINK_NEW[$MTIME]},${UNLINK_NEW2[$MTIME]}
1834         echo ${RENAME_OLD[1]},${RENAME_NEW[$MTIME]},${RENAME_NEW2[$MTIME]}
1835
1836         [ ${OPEN_NEW2[$MTIME]} -eq ${OPEN_NEW[$MTIME]} ] || error "open file reverses mtime"
1837         [ ${LINK_NEW2[$MTIME]} -eq ${LINK_NEW[$MTIME]} ] || error "link file reverses mtime"
1838         [ ${UNLINK_NEW2[$MTIME]} -eq ${UNLINK_NEW[$MTIME]} ] || error "unlink file reverses mtime"
1839         [ ${RENAME_NEW2[$MTIME]} -eq ${RENAME_NEW[$MTIME]} ] || error "rename file reverses mtime"
1840 }
1841 run_test 39b "mtime change on close ============================"
1842
1843 # bug 11063
1844 test_39c() {
1845         touch -m -d "10 years ago" $DIR1/$tfile
1846         local MTIME1=`stat -c %y $DIR1/$tfile`
1847         echo hello >> $DIR1/$tfile
1848         local MTIME2=`stat -c %y $DIR1/$tfile`
1849         mv $DIR1/$tfile $DIR1/$tfile-1
1850         local MTIME3=`stat -c %y $DIR1/$tfile-1`
1851         [ "$MTIME2" = "$MTIME3" ] ||
1852                 error "mtime ($MTIME2) changed (to $MTIME3) on rename (BZ#11063)"
1853 }
1854 run_test 39c "mtime change on rename ==========================="
1855
1856 test_40() {
1857         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
1858         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
1859         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
1860 }
1861 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
1862
1863 test_41() {
1864         # bug 1553
1865         small_write $DIR/f41 18
1866 }
1867 run_test 41 "test small file write + fstat ====================="
1868
1869 count_ost_writes() {
1870         lctl get_param -n osc.*.stats |
1871             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
1872 }
1873
1874 # decent default
1875 WRITEBACK_SAVE=500
1876 DIRTY_RATIO_SAVE=40
1877 MAX_DIRTY_RATIO=50
1878 BG_DIRTY_RATIO_SAVE=10
1879 MAX_BG_DIRTY_RATIO=25
1880
1881 start_writeback() {
1882         trap 0
1883         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
1884         # dirty_ratio, dirty_background_ratio
1885         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
1886                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
1887                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
1888                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
1889         else
1890                 # if file not here, we are a 2.4 kernel
1891                 kill -CONT `pidof kupdated`
1892         fi
1893 }
1894
1895 stop_writeback() {
1896         # setup the trap first, so someone cannot exit the test at the
1897         # exact wrong time and mess up a machine
1898         trap start_writeback EXIT
1899         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
1900         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
1901                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
1902                 sysctl -w vm.dirty_writeback_centisecs=0
1903                 # save and increase /proc/sys/vm/dirty_ratio
1904                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
1905                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
1906                 # save and increase /proc/sys/vm/dirty_background_ratio
1907                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
1908                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
1909         else
1910                 # if file not here, we are a 2.4 kernel
1911                 kill -STOP `pidof kupdated`
1912         fi
1913 }
1914
1915 # ensure that all stripes have some grant before we test client-side cache
1916 setup_test42() {
1917         [ "$SETUP_TEST42" ] && return
1918         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
1919                 dd if=/dev/zero of=$i bs=4k count=1
1920                 rm $i
1921         done
1922         SETUP_TEST42=DONE
1923 }
1924
1925 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
1926 # file truncation, and file removal.
1927 test_42a() {
1928         setup_test42
1929         cancel_lru_locks osc
1930         stop_writeback
1931         sync; sleep 1; sync # just to be safe
1932         BEFOREWRITES=`count_ost_writes`
1933         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
1934         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
1935         AFTERWRITES=`count_ost_writes`
1936         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
1937                 error "$BEFOREWRITES < $AFTERWRITES"
1938         start_writeback
1939 }
1940 run_test 42a "ensure that we don't flush on close =============="
1941
1942 test_42b() {
1943         setup_test42
1944         cancel_lru_locks osc
1945         stop_writeback
1946         sync
1947         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
1948         BEFOREWRITES=`count_ost_writes`
1949         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
1950         AFTERWRITES=`count_ost_writes`
1951         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
1952                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
1953         fi
1954         BEFOREWRITES=`count_ost_writes`
1955         sync || error "sync: $?"
1956         AFTERWRITES=`count_ost_writes`
1957         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
1958                 error "$BEFOREWRITES < $AFTERWRITES on sync"
1959         fi
1960         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
1961         start_writeback
1962         return 0
1963 }
1964 run_test 42b "test destroy of file with cached dirty data ======"
1965
1966 # if these tests just want to test the effect of truncation,
1967 # they have to be very careful.  consider:
1968 # - the first open gets a {0,EOF}PR lock
1969 # - the first write conflicts and gets a {0, count-1}PW
1970 # - the rest of the writes are under {count,EOF}PW
1971 # - the open for truncate tries to match a {0,EOF}PR
1972 #   for the filesize and cancels the PWs.
1973 # any number of fixes (don't get {0,EOF} on open, match
1974 # composite locks, do smarter file size management) fix
1975 # this, but for now we want these tests to verify that
1976 # the cancellation with truncate intent works, so we
1977 # start the file with a full-file pw lock to match against
1978 # until the truncate.
1979 trunc_test() {
1980         test=$1
1981         file=$DIR/$test
1982         offset=$2
1983         cancel_lru_locks osc
1984         stop_writeback
1985         # prime the file with 0,EOF PW to match
1986         touch $file
1987         $TRUNCATE $file 0
1988         sync; sync
1989         # now the real test..
1990         dd if=/dev/zero of=$file bs=1024 count=100
1991         BEFOREWRITES=`count_ost_writes`
1992         $TRUNCATE $file $offset
1993         cancel_lru_locks osc
1994         AFTERWRITES=`count_ost_writes`
1995         start_writeback
1996 }
1997
1998 test_42c() {
1999         trunc_test 42c 1024
2000         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2001             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2002         rm $file
2003 }
2004 run_test 42c "test partial truncate of file with cached dirty data"
2005
2006 test_42d() {
2007         trunc_test 42d 0
2008         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2009             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2010         rm $file
2011 }
2012 run_test 42d "test complete truncate of file with cached dirty data"
2013
2014 page_size() {
2015         getconf PAGE_SIZE
2016 }
2017
2018 test_42e() { # bug22074
2019         local TDIR=$DIR/${tdir}e
2020         local pagesz=$(page_size)
2021         local pages=16
2022         local files=$((OSTCOUNT * 500))
2023         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2024         local max_dirty_mb
2025         local warmup_files
2026
2027         mkdir -p $TDIR
2028         $LFS setstripe -c 1 $TDIR
2029         createmany -o $TDIR/f $files
2030
2031         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2032
2033         warmup_files=$((OSTCOUNT * max_dirty_mb))
2034         createmany -o $TDIR/w $warmup_files
2035
2036         # write a large amount of data into one file to get good avail_grant number
2037         for ((i=0; i<$warmup_files; i++)); do
2038                 idx=$($LFS getstripe -i $TDIR/w$i)
2039                 [ $idx -ne 0 ] && continue
2040                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2041                 break
2042         done
2043         sync    # write out any dirty data
2044
2045         $LCTL get_param $proc_osc0/cur_dirty_bytes
2046         $LCTL get_param $proc_osc0/cur_grant_bytes
2047
2048         # create as much dirty as we can while (hopefully) not triggering the
2049         # actual RPCs.
2050         for ((i=0; i<$warmup_files; i++)); do
2051                 idx=$($LFS getstripe -i $TDIR/w$i)
2052                 [ $idx -ne 0 ] && continue
2053                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2054         done
2055         $LCTL get_param $proc_osc0/cur_dirty_bytes
2056         $LCTL get_param $proc_osc0/cur_grant_bytes
2057
2058         # perform the real test
2059         $LCTL set_param $proc_osc0/rpc_stats 0
2060         for ((;i<$files; i++)); do
2061                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
2062                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2063         done
2064         sync
2065         $LCTL get_param $proc_osc0/rpc_stats
2066
2067         $LCTL get_param $proc_osc0/rpc_stats |
2068                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2069                         [ "$PPR" != "16:" ] && continue
2070                         [ $WPCT -lt 85 ] && error "$pages-page write RPCs only $WPCT% < 85%"
2071                         break # we only want the "pages per rpc" stat
2072                 done
2073         rm -rf $TDIR
2074 }
2075 run_test 42e "verify sub-RPC writes are not done synchronously"
2076
2077 test_43() {
2078         mkdir -p $DIR/$tdir
2079         cp -p /bin/ls $DIR/$tdir/$tfile
2080         multiop $DIR/$tdir/$tfile Ow_c &
2081         pid=$!
2082         # give multiop a chance to open
2083         sleep 1
2084
2085         $DIR/$tdir/$tfile && error || true
2086         kill -USR1 $pid
2087 }
2088 run_test 43 "execution of file opened for write should return -ETXTBSY"
2089
2090 test_43a() {
2091         mkdir -p $DIR/d43
2092         cp -p `which multiop` $DIR/d43/multiop
2093         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2094         MULTIOP_PID=$!
2095         multiop $DIR/d43/multiop Oc && error "expected error, got success"
2096         kill -USR1 $MULTIOP_PID || return 2
2097         wait $MULTIOP_PID || return 3
2098         rm $TMP/test43.junk
2099 }
2100 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2101
2102 test_43b() {
2103         mkdir -p $DIR/d43
2104         cp -p `which multiop` $DIR/d43/multiop
2105         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2106         MULTIOP_PID=$!
2107         truncate $DIR/d43/multiop 0 && error "expected error, got success"
2108         kill -USR1 $MULTIOP_PID || return 2
2109         wait $MULTIOP_PID || return 3
2110         rm $TMP/test43.junk
2111 }
2112 run_test 43b "truncate of file being executed should return -ETXTBSY"
2113
2114 test_43c() {
2115         local testdir="$DIR/d43c"
2116         mkdir -p $testdir
2117         cp $SHELL $testdir/
2118         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2119                 ( cd $testdir && md5sum -c)
2120 }
2121 run_test 43c "md5sum of copy into lustre========================"
2122
2123 test_44() {
2124         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2125         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2126         dd if=$DIR/f1 of=/dev/null bs=4k count=1
2127 }
2128 run_test 44 "zero length read from a sparse stripe ============="
2129
2130 test_44a() {
2131     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2132                          awk '{print $2}'`
2133     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2134     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2135     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2136                       awk '{print $2}'`
2137     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2138         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2139     fi
2140
2141     OFFSETS="0 $((stride/2)) $((stride-1))"
2142     for offset in $OFFSETS ; do
2143       for i in `seq 0 $((nstripe-1))`; do
2144         rm -f $DIR/d44a
2145         local GLOBALOFFSETS=""
2146         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2147         ll_sparseness_write $DIR/d44a $size  || error "ll_sparseness_write"
2148         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2149         ll_sparseness_verify $DIR/d44a $GLOBALOFFSETS \
2150                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2151
2152         for j in `seq 0 $((nstripe-1))`; do
2153             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2154             ll_sparseness_write $DIR/d44a $size || error "ll_sparseness_write"
2155             GLOBALOFFSETS="$GLOBALOFFSETS $size"
2156         done
2157         ll_sparseness_verify $DIR/d44a $GLOBALOFFSETS \
2158                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2159       done
2160     done
2161 }
2162 run_test 44a "test sparse pwrite ==============================="
2163
2164 dirty_osc_total() {
2165         tot=0
2166         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
2167                 tot=$(($tot + d))
2168         done
2169         echo $tot
2170 }
2171 do_dirty_record() {
2172         before=`dirty_osc_total`
2173         echo executing "\"$*\""
2174         eval $*
2175         after=`dirty_osc_total`
2176         echo before $before, after $after
2177 }
2178 test_45() {
2179         f="$DIR/f45"
2180         # Obtain grants from OST if it supports it
2181         echo blah > ${f}_grant
2182         stop_writeback
2183         sync
2184         do_dirty_record "echo blah > $f"
2185         [ $before -eq $after ] && error "write wasn't cached"
2186         do_dirty_record "> $f"
2187         [ $before -gt $after ] || error "truncate didn't lower dirty count"
2188         do_dirty_record "echo blah > $f"
2189         [ $before -eq $after ] && error "write wasn't cached"
2190         do_dirty_record "sync"
2191         [ $before -gt $after ] || error "writeback didn't lower dirty count"
2192         do_dirty_record "echo blah > $f"
2193         [ $before -eq $after ] && error "write wasn't cached"
2194         do_dirty_record "cancel_lru_locks osc"
2195         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
2196         start_writeback
2197 }
2198 run_test 45 "osc io page accounting ============================"
2199
2200 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
2201 # test tickles a bug where re-dirtying a page was failing to be mapped to the
2202 # objects offset and an assert hit when an rpc was built with 1023's mapped
2203 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
2204 test_46() {
2205         f="$DIR/f46"
2206         stop_writeback
2207         sync
2208         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2209         sync
2210         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
2211         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2212         sync
2213         start_writeback
2214 }
2215 run_test 46 "dirtying a previously written page ================"
2216
2217 # test_47 is removed "Device nodes check" is moved to test_28
2218
2219 test_48a() { # bug 2399
2220         check_kernel_version 34 || return 0
2221         mkdir -p $DIR/d48a
2222         cd $DIR/d48a
2223         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
2224         mkdir $DIR/d48a || error "recreate directory failed"
2225         touch foo || error "'touch foo' failed after recreating cwd"
2226         mkdir bar || error "'mkdir foo' failed after recreating cwd"
2227         if check_kernel_version 44; then
2228                 touch .foo || error "'touch .foo' failed after recreating cwd"
2229                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
2230         fi
2231         ls . > /dev/null || error "'ls .' failed after recreating cwd"
2232         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
2233         cd . || error "'cd .' failed after recreating cwd"
2234         mkdir . && error "'mkdir .' worked after recreating cwd"
2235         rmdir . && error "'rmdir .' worked after recreating cwd"
2236         ln -s . baz || error "'ln -s .' failed after recreating cwd"
2237         cd .. || error "'cd ..' failed after recreating cwd"
2238 }
2239 run_test 48a "Access renamed working dir (should return errors)="
2240
2241 test_48b() { # bug 2399
2242         check_kernel_version 34 || return 0
2243         mkdir -p $DIR/d48b
2244         cd $DIR/d48b
2245         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
2246         touch foo && error "'touch foo' worked after removing cwd"
2247         mkdir foo && error "'mkdir foo' worked after removing cwd"
2248         if check_kernel_version 44; then
2249                 touch .foo && error "'touch .foo' worked after removing cwd"
2250                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
2251         fi
2252         ls . > /dev/null && error "'ls .' worked after removing cwd"
2253         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
2254         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
2255         mkdir . && error "'mkdir .' worked after removing cwd"
2256         rmdir . && error "'rmdir .' worked after removing cwd"
2257         ln -s . foo && error "'ln -s .' worked after removing cwd"
2258         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
2259 }
2260 run_test 48b "Access removed working dir (should return errors)="
2261
2262 test_48c() { # bug 2350
2263         check_kernel_version 36 || return 0
2264         #lctl set_param debug=-1
2265         #set -vx
2266         mkdir -p $DIR/d48c/dir
2267         cd $DIR/d48c/dir
2268         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
2269         $TRACE touch foo && error "'touch foo' worked after removing cwd"
2270         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
2271         if check_kernel_version 44; then
2272                 touch .foo && error "'touch .foo' worked after removing cwd"
2273                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
2274         fi
2275         $TRACE ls . && error "'ls .' worked after removing cwd"
2276         $TRACE ls .. || error "'ls ..' failed after removing cwd"
2277         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
2278         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
2279         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
2280         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
2281         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
2282 }
2283 run_test 48c "Access removed working subdir (should return errors)"
2284
2285 test_48d() { # bug 2350
2286         check_kernel_version 36 || return 0
2287         #lctl set_param debug=-1
2288         #set -vx
2289         mkdir -p $DIR/d48d/dir
2290         cd $DIR/d48d/dir
2291         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
2292         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
2293         $TRACE touch foo && error "'touch foo' worked after removing parent"
2294         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
2295         if check_kernel_version 44; then
2296                 touch .foo && error "'touch .foo' worked after removing parent"
2297                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
2298         fi
2299         $TRACE ls . && error "'ls .' worked after removing parent"
2300         $TRACE ls .. && error "'ls ..' worked after removing parent"
2301         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
2302         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
2303         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
2304         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
2305         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
2306 }
2307 run_test 48d "Access removed parent subdir (should return errors)"
2308
2309 test_48e() { # bug 4134
2310         check_kernel_version 41 || return 0
2311         #lctl set_param debug=-1
2312         #set -vx
2313         mkdir -p $DIR/d48e/dir
2314         cd $DIR/d48e/dir
2315         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
2316         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
2317         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
2318         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
2319         # On a buggy kernel addition of "touch foo" after cd .. will
2320         # produce kernel oops in lookup_hash_it
2321         touch ../foo && error "'cd ..' worked after recreate parent"
2322         cd $DIR
2323         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
2324 }
2325 run_test 48e "Access to recreated parent subdir (should return errors)"
2326
2327 test_50() {
2328         # bug 1485
2329         mkdir $DIR/d50
2330         cd $DIR/d50
2331         ls /proc/$$/cwd || error
2332 }
2333 run_test 50 "special situations: /proc symlinks  ==============="
2334
2335 test_51a() {    # was test_51
2336         # bug 1516 - create an empty entry right after ".." then split dir
2337         mkdir $DIR/d51
2338         touch $DIR/d51/foo
2339         $MCREATE $DIR/d51/bar
2340         rm $DIR/d51/foo
2341         createmany -m $DIR/d51/longfile 201
2342         FNUM=202
2343         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
2344                 $MCREATE $DIR/d51/longfile$FNUM
2345                 FNUM=$(($FNUM + 1))
2346                 echo -n "+"
2347         done
2348         echo
2349         ls -l $DIR/d51 > /dev/null || error
2350 }
2351 run_test 51a "special situations: split htree with empty entry =="
2352
2353 export NUMTEST=70000
2354 test_51b() {
2355         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
2356         [ $NUMFREE -lt 21000 ] && \
2357                 skip "not enough free inodes ($NUMFREE)" && \
2358                 return
2359
2360         check_kernel_version 40 || NUMTEST=31000
2361         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 50))
2362
2363         mkdir -p $DIR/d51b
2364         createmany -d $DIR/d51b/t- $NUMTEST
2365 }
2366 run_test 51b "mkdir .../t-0 --- .../t-$NUMTEST ===================="
2367
2368 test_51c() {
2369         [ ! -d $DIR/d51b ] && skip "$DIR/51b missing" && \
2370                 return
2371
2372         unlinkmany -d $DIR/d51b/t- $NUMTEST
2373 }
2374 run_test 51c "rmdir .../t-0 --- .../t-$NUMTEST ===================="
2375
2376 test_51d() {
2377         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
2378         mkdir -p $DIR/d51d
2379         createmany -o $DIR/d51d/t- 1000
2380         $LFS getstripe $DIR/d51d > $TMP/files
2381         for N in `seq 0 $((OSTCOUNT - 1))`; do
2382             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
2383             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
2384             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
2385         done
2386         unlinkmany $DIR/d51d/t- 1000
2387
2388         NLAST=0
2389         for N in `seq 1 $((OSTCOUNT - 1))`; do
2390             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
2391                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
2392             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
2393                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
2394
2395             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
2396                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
2397             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
2398                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
2399             NLAST=$N
2400         done
2401 }
2402 run_test 51d "check object distribution ===================="
2403
2404 test_52a() {
2405         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
2406         mkdir -p $DIR/d52a
2407         touch $DIR/d52a/foo
2408         chattr +a $DIR/d52a/foo || error "chattr +a failed"
2409         echo bar >> $DIR/d52a/foo || error "append bar failed"
2410         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
2411         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
2412         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
2413         echo foo >> $DIR/d52a/foo || error "append foo failed"
2414         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
2415         # new lsattr displays 'e' flag for extents
2416         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
2417         chattr -a $DIR/d52a/foo || error "chattr -a failed"
2418
2419         rm -fr $DIR/d52a || error "cleanup rm failed"
2420 }
2421 run_test 52a "append-only flag test (should return errors) ====="
2422
2423 test_52b() {
2424         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
2425         mkdir -p $DIR/d52b
2426         touch $DIR/d52b/foo
2427         chattr +i $DIR/d52b/foo || error "chattr +a failed"
2428         cat test > $DIR/d52b/foo && error "cat test worked"
2429         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
2430         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
2431         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error "link worked"
2432         echo foo >> $DIR/d52b/foo && error "echo worked"
2433         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
2434         [ -f $DIR/d52b/foo ] || error
2435         [ -f $DIR/d52b/foo_ren ] && error
2436         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
2437         chattr -i $DIR/d52b/foo || error "chattr failed"
2438
2439         rm -fr $DIR/d52b || error "remove failed"
2440 }
2441 run_test 52b "immutable flag test (should return errors) ======="
2442
2443 test_52c() { # 12848 simulating client < 1.4.7
2444         [ -f $DIR/d52c/foo ] && chattr -i $DIR/d52b/foo
2445         mkdir -p $DIR/d52c
2446         touch $DIR/d52c/foo
2447         # skip MDS_BFLAG_EXT_FLAGS in mdc_getattr_pack
2448 #define OBD_FAIL_MDC_OLD_EXT_FLAGS       0x802
2449         lctl set_param fail_loc=0x802
2450         chattr +i $DIR/d52c/foo || error
2451         lsattr $DIR/d52c/foo | egrep -q "^-+i[-e]+ $DIR/d52c/foo" || error "lsattr failed"
2452         chattr -i $DIR/d52c/foo || error "chattr failed"
2453         lctl set_param -n fail_loc=0
2454
2455         rm -fr $DIR/d52c || error
2456 }
2457 run_test 52c "immutable flag test for client < 1.4.7 ======="
2458
2459 test_53() {
2460         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2461         remote_ost_nodsh && skip "remote OST with nodsh" && return
2462
2463         local param
2464         local ostname
2465         local mds_last
2466         local ost_last
2467         local ostnum
2468
2469         local mdtosc=$(get_mdtosc_proc_path '*')
2470         for VALUE in $(do_facet mds lctl get_param osc.$mdtosc.prealloc_last_id); do
2471                 param=`echo ${VALUE[0]} | cut -d "=" -f1`;
2472                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
2473                 mds_last=$(do_facet mds lctl get_param -n $param)
2474                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
2475                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id)
2476                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
2477                 if [ $ost_last != $mds_last ]; then
2478                         error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
2479                 fi
2480         done
2481 }
2482 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
2483
2484 test_54a() {
2485         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
2486         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
2487         $SOCKETSERVER $DIR/socket
2488         $SOCKETCLIENT $DIR/socket || error
2489         $MUNLINK $DIR/socket
2490 }
2491 run_test 54a "unix domain socket test =========================="
2492
2493 test_54b() {
2494         f="$DIR/f54b"
2495         mknod $f c 1 3
2496         chmod 0666 $f
2497         dd if=/dev/zero of=$f bs=`page_size` count=1
2498 }
2499 run_test 54b "char device works in lustre ======================"
2500
2501 find_loop_dev() {
2502         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
2503         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
2504         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
2505
2506         for i in `seq 3 7`; do
2507                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
2508                 LOOPDEV=$LOOPBASE$i
2509                 LOOPNUM=$i
2510                 break
2511         done
2512 }
2513
2514 test_54c() {
2515         tfile="$DIR/f54c"
2516         tdir="$DIR/d54c"
2517         loopdev="$DIR/loop54c"
2518
2519         find_loop_dev
2520         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
2521         mknod $loopdev b 7 $LOOPNUM
2522         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
2523         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
2524         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
2525         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
2526         mkdir -p $tdir
2527         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
2528         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
2529         df $tdir
2530         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
2531         $UMOUNT $tdir
2532         losetup -d $loopdev
2533         rm $loopdev
2534 }
2535 run_test 54c "block device works in lustre ====================="
2536
2537 test_54d() {
2538         f="$DIR/f54d"
2539         string="aaaaaa"
2540         mknod $f p
2541         [ "$string" = `echo $string > $f | cat $f` ] || error
2542 }
2543 run_test 54d "fifo device works in lustre ======================"
2544
2545 test_54e() {
2546         check_kernel_version 46 || return 0
2547         f="$DIR/f54e"
2548         string="aaaaaa"
2549         cp -aL /dev/console $f
2550         echo $string > $f || error
2551 }
2552 run_test 54e "console/tty device works in lustre ======================"
2553
2554 check_fstype() {
2555         grep -q $FSTYPE /proc/filesystems && return 1
2556         modprobe $FSTYPE
2557         grep -q $FSTYPE /proc/filesystems && return 1
2558         insmod ../$FSTYPE/$FSTYPE.o
2559         grep -q $FSTYPE /proc/filesystems && return 1
2560         insmod ../$FSTYPE/$FSTYPE.ko
2561         grep -q $FSTYPE /proc/filesystems && return 1
2562         return 0
2563 }
2564
2565 test_55() {
2566         rm -rf $DIR/d55
2567         mkdir $DIR/d55
2568         client_only && skip "Not a server" && return
2569         mount -t $FSTYPE -o loop,iopen $EXT2_DEV $DIR/d55 || error "mounting"
2570         touch $DIR/d55/foo
2571         $IOPENTEST1 $DIR/d55/foo $DIR/d55 || error "running $IOPENTEST1"
2572         $IOPENTEST2 $DIR/d55 || error "running $IOPENTEST2"
2573         echo "check for $EXT2_DEV. Please wait..."
2574         rm -rf $DIR/d55/*
2575         $UMOUNT $DIR/d55 || error "unmounting"
2576 }
2577 run_test 55 "check iopen_connect_dentry() ======================"
2578
2579 test_56a() {    # was test_56
2580         rm -rf $DIR/d56
2581         $SETSTRIPE -d $DIR
2582         mkdir $DIR/d56
2583         mkdir $DIR/d56/dir
2584         NUMFILES=3
2585         NUMFILESx2=$(($NUMFILES * 2))
2586         for i in `seq 1 $NUMFILES` ; do
2587                 touch $DIR/d56/file$i
2588                 touch $DIR/d56/dir/file$i
2589         done
2590
2591         # test lfs getstripe with --recursive
2592         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
2593         [ $FILENUM -eq $NUMFILESx2 ] || error \
2594                 "lfs getstripe --recursive $DIR/d56 wrong: found $FILENUM, expected $NUMFILESx2"
2595         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
2596         [ $FILENUM -eq $NUMFILES ] || error \
2597                 "lfs getstripe $DIR/d56 without --recursive wrong: found $FILENUM, expected $NUMFILES"
2598         echo "lfs getstripe --recursive passed."
2599
2600         # test lfs getstripe with file instead of dir
2601         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
2602         [ $FILENUM  -eq 1 ] || error \
2603                  "lfs getstripe $DIR/d56/file1 wrong:found $FILENUM, expected 1"
2604         echo "lfs getstripe file passed."
2605
2606         #test lfs getstripe with --verbose
2607         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||\
2608                 error "lfs getstripe --verbose $DIR/d56 wrong: should find $NUMFILES lmm_magic info"
2609         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] || error \
2610                 "lfs getstripe $DIR/d56 without --verbose wrong: should not show lmm_magic info"
2611         echo "lfs getstripe --verbose passed."
2612
2613         #test lfs getstripe with --obd
2614         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" || \
2615                 error "lfs getstripe --obd wrong_uuid should return error message"
2616
2617         [  "$OSTCOUNT" -lt 2 ] && \
2618                 skip_env "skipping other lfs getstripe --obd test" && return
2619         OSTIDX=1
2620         OBDUUID=$(lfs osts | grep ${OSTIDX}": " | awk '{print $2}')
2621         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
2622         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
2623         [ $FOUND -eq $FILENUM ] || \
2624                 error "lfs getstripe --obd wrong: found $FOUND, expected $FILENUM"
2625         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 | \
2626                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |\
2627                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] || \
2628                 error "lfs getstripe --obd wrong: should not show file on other obd"
2629         echo "lfs getstripe --obd passed."
2630 }
2631 run_test 56a "check lfs getstripe ===================================="
2632
2633 NUMFILES=3
2634 NUMDIRS=3
2635 setup_56() {
2636         LOCAL_NUMFILES=$1
2637         LOCAL_NUMDIRS=$2
2638         if [ ! -d "$DIR/${tdir}g" ] ; then
2639                 mkdir -p $DIR/${tdir}g
2640                 for i in `seq 1 $LOCAL_NUMFILES` ; do
2641                         touch $DIR/${tdir}g/file$i
2642                 done
2643                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
2644                         mkdir $DIR/${tdir}g/dir$i
2645                         for j in `seq 1 $LOCAL_NUMFILES` ; do
2646                                 touch $DIR/${tdir}g/dir$i/file$j
2647                         done
2648                 done
2649         fi
2650 }
2651
2652 setup_56_special() {
2653         LOCAL_NUMFILES=$1
2654         LOCAL_NUMDIRS=$2
2655         TDIR=$DIR/${tdir}g
2656         setup_56 $1 $2
2657         if [ ! -e "$TDIR/loop1b" ] ; then
2658                 for i in `seq 1 $LOCAL_NUMFILES` ; do
2659                         mknod $TDIR/loop${i}b b 7 $i
2660                         mknod $TDIR/null${i}c c 1 3
2661                         ln -s $TDIR/file1 $TDIR/link${i}l
2662                 done
2663                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
2664                         mknod $TDIR/dir$i/loop${i}b b 7 $i
2665                         mknod $TDIR/dir$i/null${i}c c 1 3
2666                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
2667                 done
2668         fi
2669 }
2670
2671 test_56g() {
2672         $SETSTRIPE -d $DIR
2673
2674         setup_56 $NUMFILES $NUMDIRS
2675
2676         EXPECTED=$(($NUMDIRS + 2))
2677         # test lfs find with -name
2678         for i in `seq 1 $NUMFILES` ; do
2679                 NUMS=`$LFIND -name "*$i" $DIR/${tdir}g | wc -l`
2680                 [ $NUMS -eq $EXPECTED ] || error \
2681                         "lfs find -name \"*$i\" $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
2682         done
2683         echo "lfs find -name passed."
2684 }
2685 run_test 56g "check lfs find -name ============================="
2686
2687 test_56h() {
2688         $SETSTRIPE -d $DIR
2689
2690         setup_56 $NUMFILES $NUMDIRS
2691
2692         EXPECTED=$((($NUMDIRS+1)*($NUMFILES-1)+$NUMFILES))
2693         # test lfs find with ! -name
2694         for i in `seq 1 $NUMFILES` ; do
2695                 NUMS=`$LFIND ! -name "*$i" $DIR/${tdir}g | wc -l`
2696                 [ $NUMS -eq $EXPECTED ] || error \
2697                         "lfs find ! -name \"*$i\" $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
2698         done
2699         echo "lfs find ! -name passed."
2700 }
2701 run_test 56h "check lfs find ! -name ============================="
2702
2703 test_56i() {
2704        tdir=${tdir}i
2705        mkdir -p $DIR/$tdir
2706        UUID=`$LFS osts $DIR/$tdir | awk '/0: / { print $2 }'`
2707        OUT="`$LFIND -ost $UUID $DIR/$tdir`"
2708        [ "$OUT" ] && error "$LFIND returned directory '$OUT'" || true
2709 }
2710 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
2711
2712 test_56j() {
2713         setup_56_special $NUMFILES $NUMDIRS
2714
2715         EXPECTED=$((NUMDIRS+1))
2716         NUMS=`$LFIND -type d $DIR/${tdir}g | wc -l`
2717         [ $NUMS -eq $EXPECTED ] || \
2718                 error "lfs find -type d $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
2719 }
2720 run_test 56j "check lfs find -type d ============================="
2721
2722 test_56k() {
2723         setup_56_special $NUMFILES $NUMDIRS
2724
2725         EXPECTED=$(((NUMDIRS+1) * NUMFILES))
2726         NUMS=`$LFIND -type f $DIR/${tdir}g | wc -l`
2727         [ $NUMS -eq $EXPECTED ] || \
2728                 error "lfs find -type f $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
2729 }
2730 run_test 56k "check lfs find -type f ============================="
2731
2732 test_56l() {
2733         setup_56_special $NUMFILES $NUMDIRS
2734
2735         EXPECTED=$((NUMDIRS + NUMFILES))
2736         NUMS=`$LFIND -type b $DIR/${tdir}g | wc -l`
2737         [ $NUMS -eq $EXPECTED ] || \
2738                 error "lfs find -type b $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
2739 }
2740 run_test 56l "check lfs find -type b ============================="
2741
2742 test_56m() {
2743         setup_56_special $NUMFILES $NUMDIRS
2744
2745         EXPECTED=$((NUMDIRS + NUMFILES))
2746         NUMS=`$LFIND -type c $DIR/${tdir}g | wc -l`
2747         [ $NUMS -eq $EXPECTED ] || \
2748                 error "lfs find -type c $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
2749 }
2750 run_test 56m "check lfs find -type c ============================="
2751
2752 test_56n() {
2753         setup_56_special $NUMFILES $NUMDIRS
2754
2755         EXPECTED=$((NUMDIRS + NUMFILES))
2756         NUMS=`$LFIND -type l $DIR/${tdir}g | wc -l`
2757         [ $NUMS -eq $EXPECTED ] || \
2758                 error "lfs find -type l $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
2759 }
2760 run_test 56n "check lfs find -type l ============================="
2761
2762 test_56o() {
2763         setup_56 $NUMFILES $NUMDIRS
2764         TDIR=$DIR/${tdir}g
2765
2766         utime $TDIR/file1 > /dev/null || error
2767         utime $TDIR/file2 > /dev/null || error
2768         utime $TDIR/dir1 > /dev/null || error
2769         utime $TDIR/dir2 > /dev/null || error
2770         utime $TDIR/dir1/file1 > /dev/null || error
2771
2772         EXPECTED=5
2773         NUMS=`$LFIND -mtime +1 $TDIR | wc -l`
2774         [ $NUMS -eq $EXPECTED ] || \
2775                 error "lfs find -mtime $TDIR wrong: found $NUMS, expected $EXPECTED"
2776 }
2777 run_test 56o "check lfs find -mtime for old files =========================="
2778
2779 test_56p() {
2780         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2781
2782         TDIR=$DIR/${tdir}g
2783         rm -rf $TDIR
2784
2785         setup_56 $NUMFILES $NUMDIRS
2786
2787         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
2788         EXPECTED=$NUMFILES
2789         NUMS="`$LFIND -uid $RUNAS_ID $TDIR | wc -l`"
2790         [ $NUMS -eq $EXPECTED ] || \
2791                 error "lfs find -uid $TDIR wrong: found $NUMS, expected $EXPECTED"
2792
2793         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
2794         NUMS="`$LFIND ! -uid $RUNAS_ID $TDIR | wc -l`"
2795         [ $NUMS -eq $EXPECTED ] || \
2796                 error "lfs find ! -uid $TDIR wrong: found $NUMS, expected $EXPECTED"
2797
2798         echo "lfs find -uid and ! -uid passed."
2799 }
2800 run_test 56p "check lfs find -uid and ! -uid ==============================="
2801
2802 test_56q() {
2803         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2804
2805         TDIR=$DIR/${tdir}g
2806         rm -rf $TDIR
2807
2808         setup_56 $NUMFILES $NUMDIRS
2809
2810         chgrp $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
2811         EXPECTED=$NUMFILES
2812         NUMS="`$LFIND -gid $RUNAS_ID $TDIR | wc -l`"
2813         [ $NUMS -eq $EXPECTED ] || \
2814                 error "lfs find -gid $TDIR wrong: found $NUMS, expected $EXPECTED"
2815
2816         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
2817         NUMS="`$LFIND ! -gid $RUNAS_ID $TDIR | wc -l`"
2818         [ $NUMS -eq $EXPECTED ] || \
2819                 error "lfs find ! -gid $TDIR wrong: found $NUMS, expected $EXPECTED"
2820
2821         echo "lfs find -gid and ! -gid passed."
2822 }
2823 run_test 56q "check lfs find -gid and ! -gid ==============================="
2824
2825 test_57a() {
2826         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2827
2828         local MNTDEV=$(get_mds_mntdev_proc_path)
2829         DEV=$(do_facet mds lctl get_param -n $MNTDEV)
2830         [ -z "$DEV" ] && error "can't access $MNTDEV"
2831         for DEV in $(do_facet mds lctl get_param -n $MNTDEV); do
2832                 do_facet mds $DUMPE2FS -h $DEV > $TMP/t57a.dump || error "can't access $DEV"
2833                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
2834                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
2835                 rm $TMP/t57a.dump
2836         done
2837 }
2838 run_test 57a "verify MDS filesystem created with large inodes =="
2839
2840 test_57b() {
2841         local dir=$DIR/d57b
2842
2843         local FILECOUNT=100
2844         local FILE1=$dir/f1
2845         local FILEN=$dir/f$FILECOUNT
2846
2847         rm -rf $dir || error "removing $dir"
2848         mkdir -p $dir || error "creating $dir"
2849
2850         echo "mcreating $FILECOUNT files"
2851         createmany -m $dir/f 1 $FILECOUNT || \
2852                 error "creating files in $dir"
2853
2854         # verify that files do not have EAs yet
2855         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
2856         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
2857
2858         local fsstat_dev=$(get_mds_fsstat_proc_path)
2859         local MDSFREE=$(do_facet mds lctl get_param -n $fsstat_dev.*.kbytesfree)
2860         local MDCFREE=$(lctl get_param -n mdc.*.kbytesfree | head -n 1)
2861         echo "opening files to create objects/EAs"
2862         local FILE
2863         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
2864                 $OPENFILE -f O_RDWR $FILE > /dev/null || error "opening $FILE"
2865         done
2866
2867         # verify that files have EAs now
2868         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
2869         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
2870
2871         sleep 1 # make sure we get new statfs data
2872         local MDSFREE2=$(do_facet mds lctl get_param -n $fsstat_dev.*.kbytesfree)
2873         local MDCFREE2=$(lctl get_param -n mdc.*.kbytesfree | head -n 1)
2874         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
2875                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
2876                         error "MDC before $MDCFREE != after $MDCFREE2"
2877                 else
2878                         echo "MDC before $MDCFREE != after $MDCFREE2"
2879                         echo "unable to confirm if MDS has large inodes"
2880                 fi
2881         fi
2882         rm -rf $dir
2883 }
2884 run_test 57b "default LOV EAs are stored inside large inodes ==="
2885
2886 test_58() {
2887     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
2888     wiretest
2889 }
2890 run_test 58 "verify cross-platform wire constants =============="
2891
2892 test_59() {
2893         echo "touch 130 files"
2894         createmany -o $DIR/f59- 130
2895         echo "rm 130 files"
2896         unlinkmany $DIR/f59- 130
2897         sync
2898         sleep 2
2899         # wait for commitment of removal
2900 }
2901 run_test 59 "verify cancellation of llog records async ========="
2902
2903 TEST60_HEAD="test_60 run $RANDOM"
2904 test_60a() {    # was test_60
2905         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
2906         log "$TEST60_HEAD - from kernel mode"
2907         sh run-llog.sh
2908 }
2909 run_test 60a "llog sanity tests run from kernel module =========="
2910
2911 test_60b() { # bug 6411
2912         dmesg > $DIR/$tfile
2913         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
2914                                  /llog.test/ {
2915                                          if (marker)
2916                                                  from_marker++
2917                                          from_begin++
2918                                  }
2919                                  END {
2920                                          if (marker)
2921                                                  print from_marker
2922                                          else
2923                                                  print from_begin
2924                                  }"`
2925         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
2926 }
2927 run_test 60b "limit repeated messages from CERROR/CWARN ========"
2928
2929 test_60c() {
2930         echo "create 5000 files"
2931         createmany -o $DIR/f60c- 5000
2932         #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
2933         lctl set_param fail_loc=0x80000137
2934         unlinkmany $DIR/f60c- 5000
2935 }
2936 run_test 60c "unlink file when mds full"
2937
2938 test_60d() {
2939         SAVEPRINTK=$(lctl get_param -n printk)
2940
2941         # verify "lctl mark" is even working"
2942         MESSAGE="test message ID $RANDOM $$"
2943         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
2944         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
2945
2946         lctl set_param printk=0 || error "set lnet.printk failed"
2947         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
2948         MESSAGE="new test message ID $RANDOM $$"
2949         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
2950         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
2951         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
2952
2953         lctl set_param -n printk="$SAVEPRINTK"
2954 }
2955 run_test 60d "test printk console message masking"
2956
2957 test_61() {
2958         f="$DIR/f61"
2959         dd if=/dev/zero of=$f bs=`page_size` count=1
2960         cancel_lru_locks osc
2961         multiop $f OSMWUc || error
2962         sync
2963 }
2964 run_test 61 "mmap() writes don't make sync hang ================"
2965
2966 # bug 2330 - insufficient obd_match error checking causes LBUG
2967 test_62() {
2968         f="$DIR/f62"
2969         echo foo > $f
2970         cancel_lru_locks osc
2971         #define OBD_FAIL_OSC_MATCH 0x405
2972         lctl set_param fail_loc=0x405
2973         cat $f && error "cat succeeded, expect -EIO"
2974         lctl set_param fail_loc=0
2975 }
2976 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
2977 # match every page all of the time.
2978 run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
2979
2980 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
2981 test_63a() {    # was test_63
2982         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
2983         lctl set_param -n osc.*.max_dirty_mb 0
2984         for i in `seq 10` ; do
2985                 dd if=/dev/zero of=$DIR/f63 bs=8k &
2986                 sleep 5
2987                 kill $!
2988                 sleep 1
2989         done
2990
2991         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
2992         rm -f $DIR/f63 || true
2993 }
2994 run_test 63a "Verify oig_wait interruption does not crash ======="
2995
2996 # bug 2248 - async write errors didn't return to application on sync
2997 # bug 3677 - async write errors left page locked
2998 test_63b() {
2999         debugsave
3000         lctl set_param debug=-1
3001
3002         # ensure we have a grant to do async writes
3003         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
3004         rm $DIR/$tfile
3005
3006         #define OBD_FAIL_OSC_BRW_PREP_REQ 0x406
3007         lctl set_param fail_loc=0x80000406
3008         multiop $DIR/$tfile Owy && \
3009                 error "sync didn't return ENOMEM"
3010         sync; sleep 2; sync     # do a real sync this time to flush page
3011         lctl get_param -n llite.*.dump_page_cache | grep locked && \
3012                 error "locked page left in cache after async error" || true
3013         debugrestore
3014 }
3015 run_test 63b "async write errors should be returned to fsync ==="
3016
3017 test_64a () {
3018         df $DIR
3019         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
3020 }
3021 run_test 64a "verify filter grant calculations (in kernel) ====="
3022
3023 test_64b () {
3024         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
3025         sh oos.sh $MOUNT
3026 }
3027 run_test 64b "check out-of-space detection on client ==========="
3028
3029 # bug 1414 - set/get directories' stripe info
3030 test_65a() {
3031         mkdir -p $DIR/d65
3032         touch $DIR/d65/f1
3033         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
3034 }
3035 run_test 65a "directory with no stripe info ===================="
3036
3037 test_65b() {
3038         mkdir -p $DIR/d65
3039         $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 2)) -i 0 -c 1 || error "setstripe"
3040         touch $DIR/d65/f2
3041         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
3042 }
3043 run_test 65b "directory setstripe $(($STRIPESIZE * 2)) 0 1 ==============="
3044
3045 test_65c() {
3046         if [ $OSTCOUNT -gt 1 ]; then
3047                 mkdir -p $DIR/d65
3048                 $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 4)) -i 1 \
3049                         -c $(($OSTCOUNT - 1)) || error "setstripe"
3050                 touch $DIR/d65/f3
3051                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
3052         fi
3053 }
3054 run_test 65c "directory setstripe $(($STRIPESIZE * 4)) 1 $(($OSTCOUNT - 1))"
3055
3056 test_65d() {
3057         mkdir -p $DIR/d65
3058         [ $STRIPECOUNT -le 0 ] && sc=1 || sc=$(($STRIPECOUNT - 1))
3059         $SETSTRIPE $DIR/d65 -s $STRIPESIZE -c $sc || error "setstripe"
3060         touch $DIR/d65/f4 $DIR/d65/f5
3061         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
3062 }
3063 run_test 65d "directory setstripe $STRIPESIZE -1 stripe_count =============="
3064
3065 test_65e() {
3066         mkdir -p $DIR/d65
3067
3068         $SETSTRIPE $DIR/d65 || error "setstripe"
3069         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
3070         touch $DIR/d65/f6
3071         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
3072 }
3073 run_test 65e "directory setstripe defaults ======================="
3074
3075 test_65f() {
3076         mkdir -p $DIR/d65f
3077         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
3078 }
3079 run_test 65f "dir setstripe permission (should return error) ==="
3080
3081 test_65g() {
3082         mkdir -p $DIR/d65
3083         $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 2)) -i 0 -c 1 || error "setstripe"
3084         $SETSTRIPE -d $DIR/d65 || error "setstripe"
3085         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
3086                 error "delete default stripe failed"
3087 }
3088 run_test 65g "directory setstripe -d ==========================="
3089
3090 test_65h() {
3091         mkdir -p $DIR/d65
3092         $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 2)) -i 0 -c 1 || error "setstripe"
3093         mkdir -p $DIR/d65/dd1
3094         [ "`$GETSTRIPE -v $DIR/d65 | grep "^count"`" == \
3095           "`$GETSTRIPE -v $DIR/d65/dd1 | grep "^count"`" ] || error "stripe info inherit failed"
3096 }
3097 run_test 65h "directory stripe info inherit ===================="
3098
3099 test_65i() { # bug6367
3100         $SETSTRIPE $MOUNT -s 65536 -c -1
3101 }
3102 run_test 65i "set non-default striping on root directory (bug 6367)="
3103
3104 test_65j() { # bug6367
3105         sync; sleep 1
3106         # if we aren't already remounting for each test, do so for this test
3107         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
3108                 cleanup || error "failed to unmount"
3109                 setup
3110         fi
3111         $SETSTRIPE -d $MOUNT || error "setstripe failed"
3112 }
3113 run_test 65j "set default striping on root directory (bug 6367)="
3114
3115 test_65k() { # bug11679
3116         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
3117         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3118
3119         echo "Check OST status: "
3120         MDS_OSCS=`do_facet mds lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
3121         for OSC in $MDS_OSCS; do
3122                 echo $OSC "is activate"
3123                 do_facet mds lctl --device %$OSC activate
3124         done
3125         do_facet client mkdir -p $DIR/$tdir
3126         for INACTIVE_OSC in $MDS_OSCS; do
3127                 echo $INACTIVE_OSC "is Deactivate:"
3128                 do_facet mds lctl --device  %$INACTIVE_OSC deactivate
3129                 for STRIPE_OSC in $MDS_OSCS; do
3130                         STRIPE_OST=`osc_to_ost $STRIPE_OSC`
3131                         STRIPE_INDEX=`do_facet mds lctl get_param -n lov.*md*.target_obd |
3132                                       grep $STRIPE_OST | awk -F: '{print $1}'`
3133                         echo "$SETSTRIPE $DIR/$tdir/${STRIPE_INDEX} -i ${STRIPE_INDEX} -c 1"
3134                         do_facet client $SETSTRIPE $DIR/$tdir/${STRIPE_INDEX} -i ${STRIPE_INDEX} -c 1
3135                         RC=$?
3136                         [ $RC -ne 0 ] && error "setstripe should have succeeded"
3137                 done
3138                 do_facet client rm -f $DIR/$tdir/*
3139                 echo $INACTIVE_OSC "is Activate."
3140                 do_facet mds lctl --device  %$INACTIVE_OSC activate
3141         done
3142 }
3143 run_test 65k "validate manual striping works properly with deactivated OSCs"
3144
3145 test_65l() { # bug 12836
3146         mkdir -p $DIR/$tdir/test_dir
3147         $SETSTRIPE $DIR/$tdir/test_dir -c -1
3148         $LFS find -mtime -1 $DIR/$tdir > /dev/null
3149 }
3150 run_test 65l "lfs find on -1 stripe dir ========================"
3151
3152 # bug 2543 - update blocks count on client
3153 test_66() {
3154         COUNT=${COUNT:-8}
3155         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
3156         sync; sleep 1; sync
3157         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
3158         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
3159 }
3160 run_test 66 "update inode blocks count on client ==============="
3161
3162 test_67a() { # was test_67 bug 3285 - supplementary group fails on MDS, passes on client
3163         [ "$RUNAS_ID" = "$UID" ] && skip "RUNAS_ID = UID = $UID -- skipping" && return
3164
3165         # interop 18 <-> 20
3166         local lustre_version=$(get_lustre_version mds)
3167         if [[ $lustre_version != 1.8* ]]; then
3168                 skip mds running $lustre_version, no group_upcall
3169                 return 0
3170         fi
3171         check_kernel_version 35 || return 0
3172         mkdir -p $DIR/$tdir
3173         chmod 771 $DIR/$tdir
3174         chgrp $RUNAS_ID $DIR/$tdir
3175         $RUNAS -u $RUNAS_ID -g $(($RUNAS_ID + 1)) -G1,2,$RUNAS_ID ls $DIR/$tdir
3176         RC=$?
3177         GROUP_UPCALL=$(do_facet mds lctl get_param -n mds.*.group_upcall)
3178         [ -z "$GROUP_UPCALL" ] && \
3179                 skip "lctl get_param failed! Useless to continue the test!" && return
3180         [ "$GROUP_UPCALL" = "NONE" -a $RC -eq 0 ] && \
3181                 error "no-upcall passed" || true
3182         [ "$GROUP_UPCALL" != "NONE" -a $RC -ne 0 ] && \
3183                 error "upcall failed" || true
3184 }
3185 run_test 67a "supplementary group failure (should return error) ="
3186
3187 cleanup_67b() {
3188         set +vx
3189         trap 0
3190         do_facet mds lctl set_param -n mds.*.group_upcall NONE
3191 }
3192
3193 test_67b() { # bug 3285 - supplementary group fails on MDS, passes on client
3194         # interop 18 <-> 20
3195         local lustre_version=$(get_lustre_version mds)
3196         if [[ $lustre_version != 1.8* ]]; then
3197                 skip mds running $lustre_version, no group_upcall
3198                 return 0
3199         fi
3200
3201         # needs to be in /etc/groups on MDS, gid == uid
3202         # Let's use RUNAS_ID
3203         T67_UID=${T67_UID:-$RUNAS_ID}
3204
3205         [ "$UID" = "$T67_UID" ] && skip "UID = T67_UID = $UID -- skipping" && return
3206         check_kernel_version 35 || return 0
3207         do_facet mds grep -q ":$T67_UID:$T67_UID" /etc/passwd || \
3208                 { skip "Need gid=$T67_UID group and gid == uid on mds !" && return; }
3209
3210         GROUP_UPCALL=$(do_facet mds lctl get_param -n mds.*.group_upcall)
3211         [ -z "$GROUP_UPCALL" ] && \
3212                 skip "lctl get_param failed! Useless to continue the test!" && return
3213         [ "$GROUP_UPCALL" != "NONE" ] && \
3214                 skip "skip test - upcall=$GROUP_UPCALL" && return
3215         set -vx
3216         trap cleanup_67b EXIT
3217         mkdir -p $DIR/$tdir
3218         chmod 771 $DIR/$tdir
3219         chgrp $T67_UID $DIR/$tdir
3220         local l_getgroups=$(do_facet mds which l_getgroups)
3221         do_facet mds lctl set_param -n mds.*.group_upcall $l_getgroups
3222         do_facet mds $l_getgroups -d $T67_UID
3223         $RUNAS -u $T67_UID -g 999 -G8,9,$T67_UID touch $DIR/$tdir/$tfile || \
3224                 error "'touch $DIR/$tdir/$tfile' failed"
3225         [ -f $DIR/$tdir/$tfile ] || error "$DIR/$tdir/$tfile create error"
3226         cleanup_67b
3227 }
3228 run_test 67b "supplementary group test ========================="
3229
3230 LLOOP=
3231 cleanup_68() {
3232         trap 0
3233         if [ ! -z "$LLOOP" ]; then
3234                 swapoff $LLOOP || error "swapoff failed"
3235                 $LCTL blockdev_detach $LLOOP || error "detach failed"
3236                 rm -f $LLOOP
3237                 unset LLOOP
3238         fi
3239         rm -f $DIR/f68
3240 }
3241
3242 meminfo() {
3243         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
3244 }
3245
3246 swap_used() {
3247         swapon -s | awk '($1 == "'$1'") { print $4 }'
3248 }
3249
3250
3251 # excercise swapping to lustre by adding a high priority swapfile entry
3252 # and then consuming memory until it is used.
3253 test_68() {
3254         [ "$UID" != 0 ] && skip_env "must run as root" && return
3255         lctl get_param -n devices | grep -q obdfilter && \
3256                 skip "local OST" && return
3257
3258         grep -q llite_lloop /proc/modules
3259         [ $? -ne 0 ] && skip_env "can't find module llite_lloop" && return
3260
3261         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
3262                 skip "can't reliably test swap with TCP" && return
3263
3264         MEMTOTAL=`meminfo MemTotal`
3265         NR_BLOCKS=$((MEMTOTAL>>8))
3266         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
3267
3268         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
3269         dd if=/dev/zero of=$DIR/f68 bs=64k seek=$NR_BLOCKS count=1
3270         mkswap $DIR/f68
3271
3272         $LCTL blockdev_attach $DIR/f68 $LLOOP || error "attach failed"
3273
3274         trap cleanup_68 EXIT
3275
3276         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
3277
3278         echo "before: `swapon -s | grep $LLOOP`"
3279         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
3280         echo "after: `swapon -s | grep $LLOOP`"
3281         SWAPUSED=`swap_used $LLOOP`
3282
3283         cleanup_68
3284
3285         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
3286 }
3287 run_test 68 "support swapping to Lustre ========================"
3288
3289 # bug5265, obdfilter oa2dentry return -ENOENT
3290 # #define OBD_FAIL_OST_ENOENT 0x217
3291 test_69() {
3292         remote_ost_nodsh && skip "remote OST with nodsh" && return
3293
3294         f="$DIR/$tfile"
3295         $SETSTRIPE $f -c 1 -i 0
3296
3297         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
3298
3299         #define OBD_FAIL_OST_ENOENT 0x217
3300         do_facet ost1 lctl set_param fail_loc=0x217
3301         truncate $f 1 # vmtruncate() will ignore truncate() error.
3302         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
3303
3304         do_facet ost1 lctl set_param fail_loc=0
3305         $DIRECTIO write $f 0 2 || error "write error"
3306
3307         cancel_lru_locks osc
3308         $DIRECTIO read $f 0 1 || error "read error"
3309
3310         #define OBD_FAIL_OST_ENOENT 0x217
3311         do_facet ost1 lctl set_param fail_loc=0x217
3312         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
3313
3314         do_facet ost1 lctl set_param fail_loc=0
3315         rm -f $f
3316 }
3317 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
3318
3319 test_71() {
3320     mkdir -p $DIR/$tdir
3321     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
3322 }
3323 run_test 71 "Running dbench on lustre (don't segment fault) ===="
3324
3325 test_72() { # bug 5695 - Test that on 2.6 remove_suid works properly
3326         check_kernel_version 43 || return 0
3327         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3328
3329         # Check that testing environment is properly set up. Skip if not
3330         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
3331                 skip_env "User $RUNAS_ID does not exist - skipping"
3332                 return 0
3333         }
3334         touch $DIR/f72
3335         chmod 777 $DIR/f72
3336         chmod ug+s $DIR/f72
3337         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
3338         # See if we are still setuid/sgid
3339         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
3340         # Now test that MDS is updated too
3341         cancel_lru_locks mdc
3342         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
3343         true
3344 }
3345 run_test 72 "Test that remove suid works properly (bug5695) ===="
3346
3347 # bug 3462 - multiple simultaneous MDC requests
3348 test_73() {
3349         mkdir $DIR/d73-1
3350         mkdir $DIR/d73-2
3351         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
3352         pid1=$!
3353
3354         #define OBD_FAIL_MDS_PAUSE_OPEN 0x129
3355         lctl set_param fail_loc=0x80000129
3356         multiop $DIR/d73-1/f73-2 Oc &
3357         sleep 1
3358         lctl set_param fail_loc=0
3359
3360         multiop $DIR/d73-2/f73-3 Oc &
3361         pid3=$!
3362
3363         kill -USR1 $pid1
3364         wait $pid1 || return 1
3365
3366         sleep 25
3367
3368         $CHECKSTAT -t file $DIR/d73-1/f73-1 || error "$DIR/d73-1/f73-1 not file"
3369         $CHECKSTAT -t file $DIR/d73-1/f73-2 || error "$DIR/d73-1/f73-2 not file"
3370         $CHECKSTAT -t file $DIR/d73-2/f73-3 || error "$DIR/d73-2/f73-3 not file"
3371
3372         rm -rf $DIR/d73-*
3373 }
3374 run_test 73 "multiple MDC requests (should not deadlock)"
3375
3376 test_74a() { # bug 6149, 6184
3377         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
3378         #
3379         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
3380         # will spin in a tight reconnection loop
3381         touch $DIR/f74a
3382         lctl set_param fail_loc=0x8000030e
3383         # get any lock that won't be difficult - lookup works.
3384         ls $DIR/f74a
3385         lctl set_param fail_loc=0
3386         true
3387 }
3388 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
3389
3390 test_74b() { # bug 13310
3391         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
3392         #
3393         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
3394         # will spin in a tight reconnection loop
3395         lctl set_param fail_loc=0x8000030e
3396         # get a "difficult" lock
3397         touch $DIR/f74b
3398         lctl set_param fail_loc=0
3399         true
3400 }
3401 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
3402
3403 JOIN=${JOIN:-"lfs join"}
3404 F75=$DIR/f75
3405 F128k=${F75}_128k
3406 FHEAD=${F75}_head
3407 FTAIL=${F75}_tail
3408 export T75_PREP=no
3409 test75_prep() {
3410         [ $T75_PREP = "yes" ] && return
3411         echo "using F75=$F75, F128k=$F128k, FHEAD=$FHEAD, FTAIL=$FTAIL"
3412
3413         dd if=/dev/urandom of=${F75}_128k bs=128k count=1 || error "dd failed"
3414         log "finished dd"
3415         chmod 777 ${F128k}
3416         T75_PREP=yes
3417 }
3418
3419 test_75a() {
3420         test75_prep
3421
3422         cp -p ${F128k} ${FHEAD}
3423         log "finished cp to $FHEAD"
3424         cp -p ${F128k} ${FTAIL}
3425         log "finished cp to $FTAIL"
3426         cat ${F128k} ${F128k} > ${F75}_sim_sim
3427
3428         $JOIN ${FHEAD} ${FTAIL} || error "join ${FHEAD} ${FTAIL} error"
3429         log "finished join $FHEAD to ${F75}_sim_sim"
3430         cmp ${FHEAD} ${F75}_sim_sim || error "${FHEAD} ${F75}_sim_sim differ"
3431         log "finished cmp $FHEAD to ${F75}_sim_sim"
3432         $CHECKSTAT -a ${FTAIL} || error "tail ${FTAIL} still exist after join"
3433 }
3434 run_test 75a "TEST join file ===================================="
3435
3436 test_75b() {
3437         test75_prep
3438
3439         cp -p ${F128k} ${FTAIL}
3440         cat ${F75}_sim_sim >> ${F75}_join_sim
3441         cat ${F128k} >> ${F75}_join_sim
3442         $JOIN ${FHEAD} ${FTAIL} || error "join ${FHEAD} ${FTAIL} error"
3443         cmp ${FHEAD} ${F75}_join_sim || \
3444                 error "${FHEAD} ${F75}_join_sim are different"
3445         $CHECKSTAT -a ${FTAIL} || error "tail ${FTAIL} exist after join"
3446 }
3447 run_test 75b "TEST join file 2 =================================="
3448
3449 test_75c() {
3450         test75_prep
3451
3452         cp -p ${F128k} ${FTAIL}
3453         cat ${F128k} >> ${F75}_sim_join
3454         cat ${F75}_join_sim >> ${F75}_sim_join
3455         $JOIN ${FTAIL} ${FHEAD} || error "join error"
3456         cmp ${FTAIL} ${F75}_sim_join || \
3457                 error "${FTAIL} ${F75}_sim_join are different"
3458         $CHECKSTAT -a ${FHEAD} || error "tail ${FHEAD} exist after join"
3459 }
3460 run_test 75c "TEST join file 3 =================================="
3461
3462 test_75d() {
3463         test75_prep
3464
3465         cp -p ${F128k} ${FHEAD}
3466         cp -p ${F128k} ${FHEAD}_tmp
3467         cat ${F75}_sim_sim >> ${F75}_join_join
3468         cat ${F75}_sim_join >> ${F75}_join_join
3469         $JOIN ${FHEAD} ${FHEAD}_tmp || error "join ${FHEAD} ${FHEAD}_tmp error"
3470         $JOIN ${FHEAD} ${FTAIL} || error "join ${FHEAD} ${FTAIL} error"
3471         cmp ${FHEAD} ${F75}_join_join ||error "${FHEAD} ${F75}_join_join differ"        $CHECKSTAT -a ${FHEAD}_tmp || error "${FHEAD}_tmp exist after join"
3472         $CHECKSTAT -a ${FTAIL} || error "tail ${FTAIL} exist after join (2)"
3473 }
3474 run_test 75d "TEST join file 4 =================================="
3475
3476 test_75e() {
3477         test75_prep
3478
3479         rm -rf ${FHEAD} || "delete join file error"
3480 }
3481 run_test 75e "TEST join file 5 (remove joined file) ============="
3482
3483 test_75f() {
3484         test75_prep
3485
3486         cp -p ${F128k} ${F75}_join_10_compare
3487         cp -p ${F128k} ${F75}_join_10
3488         for ((i = 0; i < 10; i++)); do
3489                 cat ${F128k} >> ${F75}_join_10_compare
3490                 cp -p ${F128k} ${FTAIL}
3491                 $JOIN ${F75}_join_10 ${FTAIL} || \
3492                         error "join ${F75}_join_10 ${FTAIL} error"
3493                 $CHECKSTAT -a ${FTAIL} || error "tail file exist after join"
3494         done
3495         cmp ${F75}_join_10 ${F75}_join_10_compare || \
3496                 error "files ${F75}_join_10 ${F75}_join_10_compare differ"
3497 }
3498 run_test 75f "TEST join file 6 (join 10 files) =================="
3499
3500 test_75g() {
3501         [ ! -f ${F75}_join_10 ] && echo "${F75}_join_10 missing" && return
3502         $LFS getstripe ${F75}_join_10
3503
3504         $OPENUNLINK ${F75}_join_10 ${F75}_join_10 || error "files unlink open"
3505
3506         ls -l $F75*
3507 }
3508 run_test 75g "TEST join file 7 (open unlink) ===================="
3509
3510 num_inodes() {
3511         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
3512 }
3513
3514 test_76() { # bug 1443
3515         DETH=$(grep deathrow /proc/kallsyms /proc/ksyms 2> /dev/null | wc -l)
3516         [ $DETH -eq 0 ] && skip "No _iget." && return 0
3517         BEFORE_INODES=`num_inodes`
3518         echo "before inodes: $BEFORE_INODES"
3519         local COUNT=1000
3520         [ "$SLOW" = "no" ] && COUNT=100
3521         for i in `seq $COUNT`; do
3522                 touch $DIR/$tfile
3523                 rm -f $DIR/$tfile
3524         done
3525         AFTER_INODES=`num_inodes`
3526         echo "after inodes: $AFTER_INODES"
3527         [ $AFTER_INODES -gt $((BEFORE_INODES + 32)) ] && \
3528                 error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
3529         true
3530 }
3531 run_test 76 "destroy duplicate inodes in client inode cache ===="
3532
3533 export ORIG_CSUM=""
3534 set_checksums()
3535 {
3536         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums |
3537                                        head -n1`
3538
3539         lctl set_param -n osc.*.checksums=$1
3540         return 0
3541 }
3542
3543 export ORIG_CSUM_TYPE=""
3544 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
3545 set_checksum_type()
3546 {
3547         [ "$ORIG_CSUM_TYPE" ] || \
3548                 ORIG_CSUM_TYPE=`lctl get_param -n osc.*osc-[^mM]*.checksum_type | sed 's/.*\[\(.*\)\].*/\1/g' \
3549                                 | head -n1`
3550         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
3551         log "set checksum type to $1"
3552         return 0
3553 }
3554 F77_TMP=$TMP/f77-temp
3555 F77SZ=8
3556 setup_f77() {
3557         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
3558                 error "error writing to $F77_TMP"
3559 }
3560
3561 test_77a() { # bug 10889
3562         [ ! -f $F77_TMP ] && setup_f77
3563         set_checksums 1
3564         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
3565         set_checksums 0
3566 }
3567 run_test 77a "normal checksum read/write operation ============="
3568
3569 test_77b() { # bug 10889
3570         [ ! -f $F77_TMP ] && setup_f77
3571         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
3572         lctl set_param fail_loc=0x80000409
3573         set_checksums 1
3574         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
3575                 error "dd error: $?"
3576         lctl set_param fail_loc=0
3577         set_checksums 0
3578 }
3579 run_test 77b "checksum error on client write ===================="
3580
3581 test_77c() { # bug 10889
3582         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
3583         set_checksums 1
3584         for algo in $CKSUM_TYPES; do
3585                 cancel_lru_locks osc
3586                 set_checksum_type $algo
3587                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
3588                 lctl set_param fail_loc=0x80000408
3589                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
3590                 lctl set_param fail_loc=0
3591         done
3592         set_checksums 0
3593         set_checksum_type $ORIG_CSUM_TYPE
3594 }
3595 run_test 77c "checksum error on client read ==================="
3596
3597 test_77d() { # bug 10889
3598         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
3599         lctl set_param fail_loc=0x80000409
3600         set_checksums 1
3601         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
3602                 error "direct write: rc=$?"
3603         lctl set_param fail_loc=0
3604         set_checksums 0
3605 }
3606 run_test 77d "checksum error on OST direct write ==============="
3607
3608 test_77e() { # bug 10889
3609         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
3610         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
3611         lctl set_param fail_loc=0x80000408
3612         set_checksums 1
3613         cancel_lru_locks osc
3614         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
3615                 error "direct read: rc=$?"
3616         lctl set_param fail_loc=0
3617         set_checksums 0
3618 }
3619 run_test 77e "checksum error on OST direct read ================"
3620
3621 test_77f() { # bug 10889
3622         set_checksums 1
3623         for algo in $CKSUM_TYPES; do
3624                 cancel_lru_locks osc
3625                 set_checksum_type $algo
3626                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
3627                 lctl set_param fail_loc=0x409
3628                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
3629                         error "direct write succeeded"
3630                 lctl set_param fail_loc=0
3631         done
3632         set_checksum_type $ORIG_CSUM_TYPE
3633         set_checksums 0
3634 }
3635 run_test 77f "repeat checksum error on write (expect error) ===="
3636
3637 test_77g() { # bug 10889
3638         remote_ost_nodsh && skip "remote OST with nodsh" && return
3639
3640         [ ! -f $F77_TMP ] && setup_f77
3641
3642         $SETSTRIPE $DIR/f77g -c 1 -i 0
3643         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
3644         do_facet ost1 lctl set_param fail_loc=0x8000021a
3645         set_checksums 1
3646         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
3647                 error "write error: rc=$?"
3648         do_facet ost1 lctl set_param fail_loc=0
3649         set_checksums 0
3650 }
3651 run_test 77g "checksum error on OST write ======================"
3652
3653 test_77h() { # bug 10889
3654         remote_ost_nodsh && skip "remote OST with nodsh" && return
3655
3656         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
3657         cancel_lru_locks osc
3658         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
3659         do_facet ost1 lctl set_param fail_loc=0x8000021b
3660         set_checksums 1
3661         cmp $F77_TMP $DIR/f77g || error "file compare failed"
3662         do_facet ost1 lctl set_param fail_loc=0
3663         set_checksums 0
3664 }
3665 run_test 77h "checksum error on OST read ======================="
3666
3667 test_77i() { # bug 13805
3668         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
3669         lctl set_param fail_loc=0x40b
3670         remount_client $MOUNT
3671         lctl set_param fail_loc=0
3672         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
3673                 param=`echo ${VALUE[0]} | cut -d "=" -f1`
3674                 algo=`lctl get_param -n $param | sed 's/.*\[\(.*\)\].*/\1/g'`
3675                 [ "$algo" = "crc32" ] || error "algo set to $algo instead of crc32"
3676         done
3677         remount_client $MOUNT
3678 }
3679 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
3680
3681 test_77j() { # bug 13805
3682         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
3683         lctl set_param fail_loc=0x40c
3684         remount_client $MOUNT
3685         lctl set_param fail_loc=0
3686         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
3687                 param=`echo ${VALUE[0]} | cut -d "=" -f1`
3688                 algo=`lctl get_param -n $param | sed 's/.*\[\(.*\)\].*/\1/g'`
3689                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
3690         done
3691         remount_client $MOUNT
3692 }
3693 run_test 77j "client only supporting ADLER32 ===================="
3694
3695 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
3696 rm -f $F77_TMP
3697 unset F77_TMP
3698
3699 test_78() { # bug 10901
3700         remote_ost || { skip_env "local OST" && return; }
3701
3702         cancel_lru_locks osc
3703         NSEQ=5
3704         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
3705         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
3706         # directio allocates the buffer twice, one for writes and another
3707         # one for reads, so that it can check the data consistency
3708         F78SIZE=$((F78SIZE / 2))
3709         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
3710         echo "MemTotal: $MEMTOTAL"
3711         # reserve 256MB of memory for the kernel and other running processes,
3712         # and then take 1/2 of the remaining memory for the read/write buffers.
3713         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
3714         echo "Mem to use for directio: $MEMTOTAL"
3715         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
3716         [ $F78SIZE -gt 512 ] && F78SIZE=512
3717         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
3718         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
3719         echo "Smallest OST: $SMALLESTOST"
3720         [ $SMALLESTOST -lt 10240 ] && \
3721                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
3722
3723         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 5)) ] && \
3724                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 5))
3725         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
3726         echo "File size: $F78SIZE"
3727         $SETSTRIPE $DIR/$tfile -c -1 || error "setstripe failed"
3728         for i in `seq 1 $NSEQ`
3729         do
3730                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
3731                 echo directIO rdwr round $i of $NSEQ
3732                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
3733         done
3734
3735         rm -f $DIR/$tfile
3736 }
3737 run_test 78 "handle large O_DIRECT writes correctly ============"
3738
3739 test_79() { # bug 12743
3740         wait_delete_completed
3741
3742         BKTOTAL=$(calc_osc_kbytes kbytestotal)
3743         BKFREE=$(calc_osc_kbytes kbytesfree)
3744         BKAVAIL=$(calc_osc_kbytes kbytesavail)
3745         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
3746         DFTOTAL=`echo $STRING | cut -d, -f1`
3747         DFUSED=`echo $STRING  | cut -d, -f2`
3748         DFAVAIL=`echo $STRING | cut -d, -f3`
3749         DFFREE=$(($DFTOTAL - $DFUSED))
3750
3751         ALLOWANCE=$((64 * $OSTCOUNT))
3752
3753         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
3754            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
3755                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
3756         fi
3757         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
3758            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
3759                 error "df free($DFFREE) mismatch OST free($BKFREE)"
3760         fi
3761         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
3762            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
3763                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
3764         fi
3765 }
3766 run_test 79 "df report consistency check ======================="
3767
3768 test_80() { # bug 10718
3769         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
3770         sync; sleep 1; sync
3771         local BEFORE=`date +%s`
3772         cancel_lru_locks osc
3773         local AFTER=`date +%s`
3774         local DIFF=$((AFTER-BEFORE))
3775         if [ $DIFF -gt 1 ] ; then
3776                 error "elapsed for 1M@1T = $DIFF"
3777         fi
3778         true
3779 }
3780 run_test 80 "Page eviction is equally fast at high offsets too  ===="
3781
3782 test_99a() {
3783         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
3784         mkdir -p $DIR/d99cvsroot || error "mkdir $DIR/d99cvsroot failed"
3785         chown $RUNAS_ID $DIR/d99cvsroot || error "chown $DIR/d99cvsroot failed"
3786         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
3787         cd $TMP
3788
3789         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
3790         cd $oldPWD
3791 }
3792 run_test 99a "cvs init ========================================="
3793
3794 test_99b() {
3795         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
3796         [ ! -d $DIR/d99cvsroot ] && test_99a
3797         $RUNAS [ ! -w /tmp ] && skip "/tmp has wrong w permission -- skipping" && return
3798         cd /etc/init.d || error "cd /etc/init.d failed"
3799         # some versions of cvs import exit(1) when asked to import links or
3800         # files they can't read.  ignore those files.
3801         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
3802                         ! -perm +4 -printf '-I %f\n')
3803         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
3804                 d99reposname vtag rtag > /dev/null || error "cvs import failed"
3805 }
3806 run_test 99b "cvs import ======================================="
3807
3808 test_99c() {
3809         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
3810         [ ! -d $DIR/d99cvsroot ] && test_99b
3811         cd $DIR || error "cd $DIR failed"
3812         mkdir -p $DIR/d99reposname || error "mkdir $DIR/d99reposname failed"
3813         chown $RUNAS_ID $DIR/d99reposname || \
3814                 error "chown $DIR/d99reposname failed"
3815         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname > /dev/null || \
3816                 error "cvs co d99reposname failed"
3817 }
3818 run_test 99c "cvs checkout ====================================="
3819
3820 test_99d() {
3821         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
3822         [ ! -d $DIR/d99cvsroot ] && test_99c
3823         cd $DIR/d99reposname
3824         $RUNAS touch foo99
3825         $RUNAS cvs add -m 'addmsg' foo99
3826 }
3827 run_test 99d "cvs add =========================================="
3828
3829 test_99e() {
3830         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
3831         [ ! -d $DIR/d99cvsroot ] && test_99c
3832         cd $DIR/d99reposname
3833         $RUNAS cvs update
3834 }
3835 run_test 99e "cvs update ======================================="
3836
3837 test_99f() {
3838         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
3839         [ ! -d $DIR/d99cvsroot ] && test_99d
3840         cd $DIR/d99reposname
3841         $RUNAS cvs commit -m 'nomsg' foo99
3842 }
3843 run_test 99f "cvs commit ======================================="
3844
3845 test_100() {
3846         [ "$NETTYPE" = tcp ] || \
3847                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
3848                         return ; }
3849
3850         remote_ost_nodsh && skip "remote OST with nodsh" && return
3851         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3852         remote_servers || \
3853                 { skip "useless for local single node setup" && return; }
3854
3855         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
3856                 [ "$PROT" != "tcp" ] && continue
3857                 RPORT=$(echo $REMOTE | cut -d: -f2)
3858                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
3859
3860                 rc=0
3861                 LPORT=`echo $LOCAL | cut -d: -f2`
3862                 if [ $LPORT -ge 1024 ]; then
3863                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
3864                         netstat -tna
3865                         error_exit "local: $LPORT > 1024, remote: $RPORT"
3866                 fi
3867         done
3868         [ "$rc" = 0 ] || error_exit "privileged port not found" )
3869 }
3870 run_test 100 "check local port using privileged port ==========="
3871
3872 function get_named_value()
3873 {
3874     local tag
3875
3876     tag=$1
3877     while read ;do
3878         line=$REPLY
3879         case $line in
3880         $tag*)
3881             echo $line | sed "s/^$tag//"
3882             break
3883             ;;
3884         esac
3885     done
3886 }
3887
3888 export CACHE_MAX=`lctl get_param -n llite/*/max_cached_mb | head -n 1`
3889 cleanup_101() {
3890         lctl set_param -n llite.*.max_cached_mb $CACHE_MAX
3891         trap 0
3892 }
3893
3894 test_101() {
3895         local s
3896         local discard
3897         local nreads=10000
3898         [ "$CPU" = "UML" ] && nreads=1000
3899         local cache_limit=32
3900
3901         lctl set_param -n osc.*.rpc_stats 0
3902         trap cleanup_101 EXIT
3903
3904         lctl set_param -n llite.*.read_ahead_stats 0
3905         lctl set_param -n llite.*.max_cached_mb $cache_limit
3906
3907         #
3908         # randomly read 10000 of 64K chunks from file 3x 32MB in size
3909         #
3910         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
3911         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
3912
3913         discard=0
3914         for s in `lctl get_param -n llite.*.read_ahead_stats | \
3915                 get_named_value 'read but discarded' | cut -d" " -f1`; do
3916                         discard=$(($discard + $s))
3917         done
3918         cleanup_101
3919
3920         if [ $(($discard * 10)) -gt $nreads ] ;then
3921                 lctl get_param osc.*.rpc_stats
3922                 lctl get_param llite.*.read_ahead_stats
3923                 error "too many ($discard) discarded pages"
3924         fi
3925         rm -f $DIR/$tfile || true
3926 }
3927 run_test 101 "check read-ahead for random reads ================"
3928
3929 export SETUP_TEST101b=no
3930 setup_101b() {
3931         [ "$SETUP_TEST101b" = "yes" ] && return
3932         mkdir -p $DIR/$tdir
3933         STRIPE_SIZE=1048576
3934         STRIPE_COUNT=$OSTCOUNT
3935         STRIPE_OFFSET=0
3936
3937         trap cleanup_101 EXIT
3938         # prepare the read-ahead file
3939         $SETSTRIPE $DIR/$tfile -s $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT
3940
3941         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
3942         SETUP_TEST101b=yes
3943 }
3944
3945 cleanup_101() {
3946         trap 0
3947         rm -rf $DIR/$tdir $DIR/$tfile
3948         SETUP_TEST101b=no
3949 }
3950
3951 calc_total() {
3952         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
3953 }
3954
3955 ra_check_101b() {
3956         local READ_SIZE=$1
3957         local STRIPE_SIZE=1048576
3958         local RA_INC=1048576
3959         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
3960         local FILE_LENGTH=$((64*100))
3961         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
3962                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
3963         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
3964                         get_named_value 'read but discarded' | \
3965                         cut -d" " -f1 | calc_total`
3966
3967         if [ $DISCARD -gt $discard_limit ]; then
3968                 lctl get_param llite.*.read_ahead_stats
3969                 error "Too many ($DISCARD) discarded pages (size ${READ_SIZE})"
3970         else
3971                 echo "Read-ahead success for size ${READ_SIZE}"
3972         fi
3973 }
3974
3975 test_101b() {
3976         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs for stride-read" && return
3977         local STRIPE_SIZE=1048576
3978         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
3979         local FILE_LENGTH=$((STRIPE_SIZE*100))
3980         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
3981         # prepare the read-ahead file
3982         setup_101b
3983         cancel_lru_locks osc
3984         for BIDX in 2 4 8 16 32 64 128 256; do
3985                 local BSIZE=$((BIDX*4096))
3986                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
3987                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
3988                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
3989                 $LCTL set_param -n llite.*.read_ahead_stats 0
3990                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
3991                         -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
3992                 cancel_lru_locks osc
3993                 ra_check_101b $BSIZE
3994         done
3995         true
3996 }
3997 run_test 101b "check stride-io mode read-ahead ================="
3998
3999 test_101c() {
4000         local STRIPE_SIZE=1048576
4001         local FILE_LENGTH=$((STRIPE_SIZE*100))
4002         local nreads=10000
4003
4004         setup_101b
4005
4006         cancel_lru_locks osc
4007         $LCTL set_param osc.*.rpc_stats 0
4008         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
4009         for OSC in `$LCTL  get_param -N osc.*`
4010         do
4011                 if [ "$OSC" == "osc.num_refs" ]; then
4012                         continue
4013                 fi
4014                 lines=`$LCTL get_param -n ${OSC}.rpc_stats | wc | awk '{print $1}'`
4015                 if [ $lines -le 20 ]; then
4016                         continue
4017                 fi
4018
4019                 rpc4k=$($LCTL get_param -n $OSC | awk '$1 == "1:" { print $2; exit; }')
4020                 rpc8k=$($LCTL get_param -n $OSC | awk '$1 == "2:" { print $2; exit; }')
4021                 rpc16k=$($LCTL get_param -n $OSC | awk '$1 == "4:" { print $2; exit; }')
4022                 rpc32k=$($LCTL get_param -n $OSC | awk '$1 == "8:" { print $2; exit; }')
4023
4024                 [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
4025                 [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
4026                 [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
4027                 [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
4028
4029                 echo "Small rpc check passed!"
4030                 rpc64k=$($LCTL get_param -n $OSC | awk '$1 == "16:" { print $2; exit; }')
4031                 rpc128k=$($LCTL get_param -n $OSC | awk '$1 == "32:" { print $2; exit; }')
4032                 rpc256k=$($LCTL get_param -n $OSC | awk '$1 == "64:" { print $2; exit; }')
4033                 rpc512k=$($LCTL get_param -n $OSC | awk '$1 == "128:" { print $2; exit; }')
4034                 rpc1024k=$($LCTL get_param -n $OSC | awk '$1 == "256:" { print $2; exit; }')
4035
4036                 [ $rpc64k == 0 ]   && error "No 64k readahead IO ${rpc64k}"
4037                 [ $rpc128k == 0 ]  && error "No 128k readahead IO ${rpc128k}"
4038                 [ $rpc256k == 0 ]  && error "No 256k readahead IO ${rpc256k}"
4039                 [ $rpc512k == 0 ]  && error "No 512k readahead IO ${rpc256k}"
4040                 [ $rpc1024k == 0 ] && error "No 1024k readahead IO ${rpc1024k}"
4041                 echo "Big rpc check passed!"
4042         done
4043         cleanup_101
4044         true
4045 }
4046 run_test 101c "check stripe_size aligned read-ahead ================="
4047
4048 set_read_ahead() {
4049    lctl get_param -n llite.*.max_read_ahead_mb | head -n 1
4050    lctl set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
4051 }
4052
4053 test_101d() {
4054     local file=$DIR/$tfile
4055     local size=${FILESIZE_101c:-500}
4056     local ra_MB=${READAHEAD_MB:-40}
4057
4058     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
4059     [ $space -gt $((size / 1024)) ] ||
4060         { skip "Need free space ${size}M, have $space" && return; }
4061
4062     echo Creating ${size}M test file $file
4063     dd if=/dev/zero of=$file bs=1M count=$size
4064     echo Cancel LRU locks on lustre client to flush the client cache
4065     cancel_lru_locks osc
4066
4067     echo Disable read-ahead
4068     local old_READAHEAD=$(set_read_ahead 0)
4069
4070     echo Reading the test file $file with read-ahead disabled
4071     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
4072
4073     echo Cancel LRU locks on lustre client to flush the client cache
4074     cancel_lru_locks osc
4075     echo Enable read-ahead with ${ra_MB}MB
4076     set_read_ahead $ra_MB
4077
4078     echo Reading the test file $file with read-ahead enabled
4079     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
4080
4081     echo read-ahead disabled time read $time_ra_OFF
4082     echo read-ahead enabled  time read $time_ra_ON
4083
4084     set_read_ahead $old_READAHEAD
4085     rm -f $file
4086
4087     [ $time_ra_ON -lt $time_ra_OFF ] ||
4088         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
4089                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
4090 }
4091 run_test 101d "file read with and without read-ahead enabled  ================="
4092
4093 export SETUP_TEST102=no
4094 setup_test102() {
4095         [ "$SETUP_TEST102" = "yes" ] && return
4096         mkdir -p $DIR/$tdir
4097         chown $RUNAS_ID $DIR/$tdir
4098         STRIPE_SIZE=65536
4099         STRIPE_OFFSET=1
4100         STRIPE_COUNT=$OSTCOUNT
4101         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
4102
4103         trap cleanup_test102 EXIT
4104         cd $DIR
4105         $1 $SETSTRIPE $tdir -s $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT
4106         cd $DIR/$tdir
4107         for num in 1 2 3 4
4108         do
4109                 for count in `seq 1 $STRIPE_COUNT`
4110                 do
4111                         for offset in `seq 0 $[$STRIPE_COUNT - 1]`
4112                         do
4113                                 local stripe_size=`expr $STRIPE_SIZE \* $num`
4114                                 local file=file"$num-$offset-$count"
4115                                 $1 $SETSTRIPE $file -s $stripe_size -i $offset -c $count
4116                         done
4117                 done
4118         done
4119
4120         cd $DIR
4121         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
4122         SETUP_TEST102=yes
4123 }
4124
4125 cleanup_test102() {
4126         trap 0
4127         [ "$SETUP_TEST102" = "yes" ] || return 0
4128         rm -f $TMP/f102.tar
4129         SETUP_TEST102=no
4130 }
4131
4132 test_102a() {
4133         local testfile=$DIR/xattr_testfile
4134
4135         rm -f $testfile
4136         touch $testfile
4137
4138         [ "$UID" != 0 ] && skip_env "must run as root" && return
4139         [ -z "`lctl get_param -n mdc.*.connect_flags | grep xattr`" ] &&
4140                 skip_env "must have user_xattr" && return
4141         [ -z "$(which setfattr 2>/dev/null)" ] && skip_env "could not find setfattr" && return
4142
4143         echo "set/get xattr..."
4144         setfattr -n trusted.name1 -v value1 $testfile || error
4145         [ "`getfattr -n trusted.name1 $testfile 2> /dev/null | \
4146         grep "trusted.name1"`" == "trusted.name1=\"value1\"" ] || error
4147
4148         setfattr -n user.author1 -v author1 $testfile || error
4149         [ "`getfattr -n user.author1 $testfile 2> /dev/null | \
4150         grep "user.author1"`" == "user.author1=\"author1\"" ] || error
4151
4152         echo "listxattr..."
4153         setfattr -n trusted.name2 -v value2 $testfile || error
4154         setfattr -n trusted.name3 -v value3 $testfile || error
4155         [ `getfattr -d -m "^trusted" $testfile 2> /dev/null | \
4156         grep "trusted.name" | wc -l` -eq 3 ] || error
4157
4158
4159         setfattr -n user.author2 -v author2 $testfile || error
4160         setfattr -n user.author3 -v author3 $testfile || error
4161         [ `getfattr -d -m "^user" $testfile 2> /dev/null | \
4162         grep "user" | wc -l` -eq 3 ] || error
4163
4164         echo "remove xattr..."
4165         setfattr -x trusted.name1 $testfile || error
4166         getfattr -d -m trusted $testfile 2> /dev/null | \
4167         grep "trusted.name1" && error || true
4168
4169         setfattr -x user.author1 $testfile || error
4170         getfattr -d -m user $testfile 2> /dev/null | \
4171         grep "user.author1" && error || true
4172
4173         # b10667: setting lustre special xattr be silently discarded
4174         echo "set lustre special xattr ..."
4175         setfattr -n "trusted.lov" -v "invalid value" $testfile || error
4176
4177         rm -f $testfile
4178 }
4179 run_test 102a "user xattr test =================================="
4180
4181 test_102b() {
4182         # b10930: get/set/list trusted.lov xattr
4183         echo "get/set/list trusted.lov xattr ..."
4184         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
4185         local testfile=$DIR/$tfile
4186         $SETSTRIPE -s 65536 -i 1 -c 2 $testfile || error "setstripe failed"
4187         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
4188         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
4189
4190         local testfile2=${testfile}2
4191         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
4192                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
4193
4194         $MCREATE $testfile2
4195         setfattr -n trusted.lov -v $value $testfile2
4196         local tmp_file=${testfile}3
4197         $GETSTRIPE -v $testfile2 > $tmp_file
4198         local stripe_size=`grep "size"  $tmp_file| awk '{print $2}'`
4199         local stripe_count=`grep "count"  $tmp_file| awk '{print $2}'`
4200         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
4201         [ $stripe_count -eq 2 ] || error "stripe count $stripe_count != 2"
4202 }
4203 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
4204
4205 test_102c() {
4206         # b10930: get/set/list lustre.lov xattr
4207         echo "get/set/list lustre.lov xattr ..."
4208         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
4209         mkdir -p $DIR/$tdir
4210         chown $RUNAS_ID $DIR/$tdir
4211         local testfile=$DIR/$tdir/$tfile
4212         $RUNAS $SETSTRIPE -s 65536 -i 1 -c 2 $testfile||error "setstripe failed"
4213         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
4214         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
4215
4216         local testfile2=${testfile}2
4217         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
4218                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
4219
4220         $RUNAS $MCREATE $testfile2
4221         $RUNAS setfattr -n lustre.lov -v $value $testfile2
4222         local tmp_file=${testfile}3
4223         $RUNAS $GETSTRIPE -v $testfile2 > $tmp_file
4224         local stripe_size=`grep "size"  $tmp_file| awk '{print $2}'`
4225         local stripe_count=`grep "count"  $tmp_file| awk '{print $2}'`
4226         [ "$stripe_size" -eq 65536 ] || error "stripe size $stripe_size != 65536"
4227         [ "$stripe_count" -eq 2 ] || error "stripe count $stripe_count != 2"
4228 }
4229 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
4230
4231 compare_stripe_info1() {
4232         local stripe_index_all_zero=1
4233
4234         for num in 1 2 3 4
4235         do
4236                 for count in `seq 1 $STRIPE_COUNT`
4237                 do
4238                         for offset in `seq 0 $[$STRIPE_COUNT - 1]`
4239                         do
4240                                 local size=`expr $STRIPE_SIZE \* $num`
4241                                 local file=file"$num-$offset-$count"
4242                                 get_stripe_info client $PWD/$file "$1"
4243                                 if [ $stripe_size -ne $size ]; then
4244                                         error "$file: different stripe size $stripe_size, expected $size" && return
4245                                 fi
4246                                 if [ $stripe_count -ne $count ]; then
4247                                         error "$file: different stripe count $stripe_count, expected $count" && return
4248                                 fi
4249                                 if [ $stripe_index -ne 0 ]; then
4250                                        stripe_index_all_zero=0
4251                                 fi
4252                         done
4253                 done
4254         done
4255         [ $stripe_index_all_zero -eq 1 ] && error "all files are being extracted starting from OST index 0"
4256         return 0
4257 }
4258
4259 find_lustre_tar() {
4260         [ -n "$(which tar 2>/dev/null)" ] && strings $(which tar) | grep -q lustre && echo tar
4261 }
4262
4263 test_102d() {
4264         # b10930: tar test for trusted.lov xattr
4265         TAR=$(find_lustre_tar)
4266         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
4267         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
4268         setup_test102
4269         mkdir -p $DIR/d102d
4270         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
4271         cd $DIR/d102d/$tdir
4272         compare_stripe_info1
4273 }
4274 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
4275
4276 test_102f() {
4277         # b10930: tar test for trusted.lov xattr
4278         TAR=$(find_lustre_tar)
4279         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
4280         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
4281         setup_test102
4282         mkdir -p $DIR/d102f
4283         cd $DIR
4284         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
4285         cd $DIR/d102f/$tdir
4286         compare_stripe_info1
4287 }
4288 run_test 102f "tar copy files, not keep osts ==========="
4289
4290 test_102h() { # bug 15777
4291         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
4292                 skip "must have user_xattr" && return
4293         [ -z "$(which setfattr 2>/dev/null)" ] &&
4294                 skip_env "could not find setfattr" && return
4295
4296         XBIG=trusted.big
4297         XSIZE=1024
4298         touch $DIR/$tfile
4299         VALUE=datadatadatadatadatadatadatadata
4300         while [ $(echo $VALUE | wc -c) -lt $XSIZE ]; do
4301                 VALUE="$VALUE$VALUE"
4302         done
4303         log "save $XBIG on $DIR/$tfile"
4304         setfattr -n $XBIG -v "$VALUE" $DIR/$tfile ||
4305                 error "saving $XBIG on $DIR/$tfile failed"
4306         ORIG=$(getfattr -n $XBIG $DIR/$tfile 2> /dev/null | grep $XBIG)
4307         OSIZE=$(echo $ORIG | wc -c)
4308         [ $OSIZE -lt $XSIZE ] && error "set $XBIG too small ($OSIZE < $XSIZE)"
4309
4310         XSML=trusted.sml
4311         log "save $XSML on $DIR/$tfile"
4312         setfattr -n $XSML -v val $DIR/$tfile ||
4313                 error "saving $XSML on $DIR/$tfile failed"
4314         NEW=$(getfattr -n $XBIG $DIR/$tfile 2> /dev/null | grep $XBIG)
4315         if [ "$NEW" != "$ORIG" ]; then
4316                 log "orig: $ORIG"
4317                 log "new: $NEW"
4318                 error "$XBIG different after saving $XSML"
4319         fi
4320
4321         log "grow $XSML on $DIR/$tfile"
4322         setfattr -n $XSML -v "$VALUE" $DIR/$tfile ||
4323                 error "growing $XSML on $DIR/$tfile failed"
4324         NEW=$(getfattr -n $XBIG $DIR/$tfile 2> /dev/null | grep $XBIG)
4325         if [ "$NEW" != "$ORIG" ]; then
4326                 log "orig: $ORIG"
4327                 log "new: $NEW"
4328                 error "$XBIG different after growing $XSML"
4329         fi
4330         log "$XBIG still valid after growing $XSML"
4331 }
4332 run_test 102h "grow xattr from inside inode to external block"
4333
4334 test_102i() { # bug 17038
4335         touch $DIR/$tfile
4336         ln -s $DIR/$tfile $DIR/${tfile}link
4337         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
4338         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 | grep -i "no such attr" || error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
4339         rm -f $DIR/$tfile $DIR/${tfile}link
4340 }
4341 run_test 102i "lgetxattr test on symbolic link ============"
4342
4343 test_102j() {
4344         TAR=$(find_lustre_tar)
4345         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
4346         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
4347         setup_test102 "$RUNAS"
4348         mkdir -p $DIR/d102j
4349         chown $RUNAS_ID $DIR/d102j
4350         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
4351         cd $DIR/d102j/$tdir
4352         compare_stripe_info1 "$RUNAS"
4353 }
4354 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
4355
4356 test_102k() {
4357         touch $DIR/$tfile
4358         # b22187 just check that does not crash for regular file.
4359         setfattr -n trusted.lov $DIR/$tfile
4360         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
4361         local test_kdir=$DIR/d102k
4362         mkdir $test_kdir
4363         local default_size=`$GETSTRIPE -s $test_kdir`
4364         local default_count=`$GETSTRIPE -c $test_kdir`
4365         local default_offset=`$GETSTRIPE -o $test_kdir`
4366         $SETSTRIPE -s 65536 -i 1 -c 2 $test_kdir || error 'dir setstripe failed'
4367         setfattr -n trusted.lov $test_kdir
4368         local stripe_size=`$GETSTRIPE -s $test_kdir`
4369         local stripe_count=`$GETSTRIPE -c $test_kdir`
4370         local stripe_offset=`$GETSTRIPE -o $test_kdir`
4371         [ $stripe_size -eq $default_size ] || error "stripe size $stripe_size != $default_size"
4372         [ $stripe_count -eq $default_count ] || error "stripe count $stripe_count != $default_count"
4373         [ $stripe_offset -eq $default_offset ] || error "stripe offset $stripe_offset != $default_offset"
4374         rm -rf $DIR/$tfile $test_kdir
4375 }
4376 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
4377
4378 cleanup_test102
4379
4380 run_acl_subtest()
4381 {
4382     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
4383     return $?
4384 }
4385
4386 test_103 () {
4387     [ "$UID" != 0 ] && skip_env "must run as root" && return
4388     [ -z "$(lctl get_param mdc.*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
4389     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
4390
4391     SAVE_UMASK=`umask`
4392     umask 0022
4393     cd $DIR
4394
4395     echo "performing cp ..."
4396     run_acl_subtest cp || error
4397     echo "performing getfacl-noacl..."
4398     run_acl_subtest getfacl-noacl > /dev/null || error "getfacl-noacl test failed"
4399     echo "performing misc..."
4400     run_acl_subtest misc > /dev/null || error "misc test failed"
4401 #    XXX add back permission test when we support supplementary groups.
4402 #    echo "performing permissions..."
4403 #    run_acl_subtest permissions || error
4404     echo "performing setfacl..."
4405     run_acl_subtest setfacl > /dev/null || error "setfacl test failed"
4406
4407     # inheritance test got from HP
4408     echo "performing inheritance..."
4409     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
4410     chmod +x make-tree || error "chmod +x failed"
4411     run_acl_subtest inheritance > /dev/null || error "inheritance test failed"
4412     rm -f make-tree
4413
4414     cd $SAVE_PWD
4415     umask $SAVE_UMASK
4416 }
4417 run_test 103 "acl test ========================================="
4418
4419 test_104a() {
4420         touch $DIR/$tfile
4421         lfs df || error "lfs df failed"
4422         lfs df -ih || error "lfs df -ih failed"
4423         lfs df -h $DIR || error "lfs df -h $DIR failed"
4424         lfs df -i $DIR || error "lfs df -i $DIR failed"
4425         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
4426         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
4427
4428         OSC=`lctl get_param -n devices | awk '/-osc-|OSC.*MNT/ {print $4}' | head -n 1`
4429         lctl --device %$OSC deactivate
4430         lfs df || error "lfs df with deactivated OSC failed"
4431         lctl --device %$OSC recover
4432         lfs df || error "lfs df with reactivated OSC failed"
4433 }
4434 run_test 104a "lfs df [-ih] [path] test ========================="
4435
4436 test_104b() {
4437         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4438         chmod 666 /dev/obd
4439         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
4440         if [ $denied_cnt -ne 0 ];
4441         then
4442                     error "lfs check servers test failed"
4443         fi
4444 }
4445 run_test 104b "$RUNAS lfs check servers test ===================="
4446
4447 test_105a() {
4448         # doesn't work on 2.4 kernels
4449         touch $DIR/$tfile
4450         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
4451         then
4452                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
4453         else
4454                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
4455         fi
4456 }
4457 run_test 105a "flock when mounted without -o flock test ========"
4458
4459 test_105b() {
4460         touch $DIR/$tfile
4461         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
4462         then
4463                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
4464         else
4465                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
4466         fi
4467 }
4468 run_test 105b "fcntl when mounted without -o flock test ========"
4469
4470 test_105c() {
4471         touch $DIR/$tfile
4472         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
4473         then
4474                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
4475         else
4476                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
4477         fi
4478 }
4479 run_test 105c "lockf when mounted without -o flock test ========"
4480
4481 test_105d() { # bug 15924
4482         mkdir -p $DIR/$tdir
4483         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
4484                 skip "mount w/o flock enabled" && return
4485         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
4486         $LCTL set_param fail_loc=0x80000315
4487         flocks_test 2 $DIR/$tdir
4488 }
4489 run_test 105d "flock race (should not freeze) ========"
4490
4491 test_106() { #bug 10921
4492         mkdir -p $DIR/$tdir
4493         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
4494         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
4495 }
4496 run_test 106 "attempt exec of dir followed by chown of that dir"
4497
4498 test_107() {
4499         CDIR=`pwd`
4500         cd $DIR
4501
4502         local file=core
4503         rm -f $file
4504
4505         local save_pattern=$(sysctl -n kernel.core_pattern)
4506         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
4507         sysctl -w kernel.core_pattern=$file
4508         sysctl -w kernel.core_uses_pid=0
4509
4510         ulimit -c unlimited
4511         sleep 60 &
4512         SLEEPPID=$!
4513
4514         sleep 1
4515
4516         kill -s 11 $SLEEPPID
4517         wait $SLEEPPID
4518         if [ -e $file ]; then
4519                 size=`stat -c%s $file`
4520                 [ $size -eq 0 ] && error "Zero length core file $file"
4521         else
4522                 error "Fail to create core file $file"
4523         fi
4524         rm -f $file
4525         sysctl -w kernel.core_pattern=$save_pattern
4526         sysctl -w kernel.core_uses_pid=$save_uses_pid
4527         cd $CDIR
4528 }
4529 run_test 107 "Coredump on SIG"
4530
4531 test_115() {
4532         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
4533             cut -c11-20)
4534         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
4535             return
4536         echo "Starting with $OSTIO_pre threads"
4537
4538         NUMTEST=20000
4539         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
4540         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
4541         echo "$NUMTEST creates/unlinks"
4542         mkdir -p $DIR/$tdir
4543         createmany -o $DIR/$tdir/$tfile $NUMTEST
4544         unlinkmany $DIR/$tdir/$tfile $NUMTEST
4545
4546         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
4547             cut -c11-20)
4548
4549         # don't return an error
4550         [ $OSTIO_post -eq $OSTIO_pre ] && echo \
4551             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&\
4552             echo "This may be fine, depending on what ran before this test" &&\
4553             echo "and how fast this system is." && return
4554
4555         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
4556 }
4557 run_test 115 "verify dynamic thread creation===================="
4558
4559 free_min_max () {
4560         wait_delete_completed
4561         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC][-_]*.kbytesavail))
4562         echo OST kbytes available: ${AVAIL[@]}
4563         MAXI=0; MAXV=${AVAIL[0]}
4564         MINI=0; MINV=${AVAIL[0]}
4565         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
4566             #echo OST $i: ${AVAIL[i]}kb
4567             if [ ${AVAIL[i]} -gt $MAXV ]; then
4568                 MAXV=${AVAIL[i]}; MAXI=$i
4569             fi
4570             if [ ${AVAIL[i]} -lt $MINV ]; then
4571                 MINV=${AVAIL[i]}; MINI=$i
4572             fi
4573         done
4574         echo Min free space: OST $MINI: $MINV
4575         echo Max free space: OST $MAXI: $MAXV
4576 }
4577
4578 test_116() {
4579         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
4580
4581         echo -n "Free space priority "
4582         lctl get_param -n lov.*.qos_prio_free
4583         declare -a AVAIL
4584         free_min_max
4585         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI" &&\
4586                 return
4587
4588         # generate uneven OSTs
4589         mkdir -p $DIR/$tdir/OST${MINI}
4590         declare -i FILL
4591         FILL=$(($MINV / 4))
4592         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
4593         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
4594         i=1
4595         while [ $FILL -gt 0 ]; do
4596             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
4597             FILL=$(($FILL - 2048))
4598             echo -n .
4599             i=$(($i + 1))
4600         done
4601         FILL=$(($MINV / 4))
4602         sync
4603         sleep_maxage
4604
4605         free_min_max
4606         DIFF=$(($MAXV - $MINV))
4607         DIFF2=$(($DIFF * 100 / $MINV))
4608         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
4609         if [ $DIFF2 -gt 20 ]; then
4610             echo "ok"
4611         else
4612             echo "failed - QOS mode won't be used"
4613             error_ignore "QOS imbalance criteria not met"
4614             return
4615         fi
4616
4617         MINI1=$MINI; MINV1=$MINV
4618         MAXI1=$MAXI; MAXV1=$MAXV
4619
4620         # now fill using QOS
4621         echo writing a bunch of files to QOS-assigned OSTs
4622         $SETSTRIPE $DIR/$tdir -c 1
4623         i=1
4624         while [ $FILL -gt 0 ]; do
4625             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
4626             FILL=$(($FILL - 200))
4627             echo -n .
4628             i=$(($i + 1))
4629         done
4630         echo "wrote $i 200k files"
4631         sync
4632         sleep_maxage
4633
4634         echo "Note: free space may not be updated, so measurements might be off"
4635         free_min_max
4636         DIFF2=$(($MAXV - $MINV))
4637         echo "free space delta: orig $DIFF final $DIFF2"
4638         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
4639         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
4640         echo "Wrote $DIFF to smaller OST $MINI1"
4641         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
4642         echo "Wrote $DIFF2 to larger OST $MAXI1"
4643         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
4644
4645         # Figure out which files were written where
4646         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd | awk '/'$MINI1': / {print $2; exit}')
4647         echo $UUID
4648         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
4649         echo "$MINC files created on smaller OST $MINI1"
4650         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd | (awk '/'$MAXI1': / {print $2; exit}'))
4651         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
4652         echo "$MAXC files created on larger OST $MAXI1"
4653         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
4654         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
4655
4656         rm -rf $DIR/$tdir
4657 }
4658 run_test 116 "stripe QOS: free space balance ==================="
4659
4660 test_117() # bug 10891
4661 {
4662         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
4663         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
4664         lctl set_param fail_loc=0x21e
4665         > $DIR/$tfile || error "truncate failed"
4666         lctl set_param fail_loc=0
4667         echo "Truncate succeeded."
4668 }
4669 run_test 117 "verify fsfilt_extend ============================="
4670
4671 export OLD_RESENDCOUNT=""
4672 set_resend_count () {
4673         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
4674         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
4675         lctl set_param -n $PROC_RESENDCOUNT $1
4676         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
4677 }
4678
4679 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
4680
4681 # Reset async IO behavior after error case
4682 reset_async() {
4683         FILE=$DIR/reset_async
4684
4685         # Ensure all OSCs are cleared
4686         $SETSTRIPE -c -1 $FILE
4687         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
4688         sync
4689         rm $FILE
4690 }
4691
4692 test_118a() #bug 11710
4693 {
4694         reset_async
4695
4696         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4697         DIRTY=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c dirty)
4698         WRITEBACK=$(lctl get_param "llite.*.dump_page_cache" | grep -c writeback)
4699
4700         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4701                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4702                 return 1;
4703         fi
4704 }
4705 run_test 118a "verify O_SYNC works =========="
4706
4707 test_118b()
4708 {
4709         remote_ost_nodsh && skip "remote OST with nodsh" && return
4710
4711         reset_async
4712
4713         #define OBD_FAIL_OST_ENOENT 0x217
4714         set_nodes_failloc "$(osts_nodes)" 0x217
4715         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4716         RC=$?
4717         set_nodes_failloc "$(osts_nodes)" 0
4718         DIRTY=$(lctl get_param llite.*.dump_page_cache | grep -c dirty)
4719         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
4720
4721         if [[ $RC -eq 0 ]]; then
4722                 error "Must return error due to dropped pages, rc=$RC"
4723                 return 1;
4724         fi
4725
4726         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4727                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4728                 return 1;
4729         fi
4730
4731         echo "Dirty pages not leaked on ENOENT"
4732
4733         # Due to the above error the OSC will issue all RPCs syncronously
4734         # until a subsequent RPC completes successfully without error.
4735         multiop $DIR/$tfile Ow4096yc
4736         rm -f $DIR/$tfile
4737
4738         return 0
4739 }
4740 run_test 118b "Reclaim dirty pages on fatal error =========="
4741
4742 test_118c()
4743 {
4744         remote_ost_nodsh && skip "remote OST with nodsh" && return
4745
4746         reset_async
4747
4748         #define OBD_FAIL_OST_EROFS               0x216
4749         set_nodes_failloc "$(osts_nodes)" 0x216
4750
4751         # multiop should block due to fsync until pages are written
4752         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
4753         MULTIPID=$!
4754         sleep 1
4755
4756         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
4757                 error "Multiop failed to block on fsync, pid=$MULTIPID"
4758         fi
4759
4760         WRITEBACK=$(lctl get_param -n llite/*/dump_page_cache | grep -c writeback)
4761         if [[ $WRITEBACK -eq 0 ]]; then
4762                 error "No page in writeback, writeback=$WRITEBACK"
4763         fi
4764
4765         set_nodes_failloc "$(osts_nodes)" 0
4766         wait $MULTIPID
4767         RC=$?
4768         if [[ $RC -ne 0 ]]; then
4769                 error "Multiop fsync failed, rc=$RC"
4770         fi
4771
4772         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
4773         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
4774         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4775                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4776         fi
4777
4778         rm -f $DIR/$tfile
4779         echo "Dirty pages flushed via fsync on EROFS"
4780         return 0
4781 }
4782 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
4783
4784 test_118d()
4785 {
4786         remote_ost_nodsh && skip "remote OST with nodsh" && return
4787
4788         reset_async
4789
4790         #define OBD_FAIL_OST_BRW_PAUSE_BULK
4791         set_nodes_failloc "$(osts_nodes)" 0x214
4792         # multiop should block due to fsync until pages are written
4793         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
4794         MULTIPID=$!
4795         sleep 1
4796
4797         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
4798                 error "Multiop failed to block on fsync, pid=$MULTIPID"
4799         fi
4800
4801         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
4802         if [[ $WRITEBACK -eq 0 ]]; then
4803                 error "No page in writeback, writeback=$WRITEBACK"
4804         fi
4805
4806         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
4807         set_nodes_failloc "$(osts_nodes)" 0
4808
4809         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
4810         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
4811         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4812                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4813         fi
4814
4815         rm -f $DIR/$tfile
4816         echo "Dirty pages gaurenteed flushed via fsync"
4817         return 0
4818 }
4819 run_test 118d "Fsync validation inject a delay of the bulk =========="
4820
4821 test_118f() {
4822         reset_async
4823
4824         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
4825         lctl set_param fail_loc=0x8000040a
4826
4827         # Should simulate EINVAL error which is fatal
4828         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4829         RC=$?
4830         if [[ $RC -eq 0 ]]; then
4831                 error "Must return error due to dropped pages, rc=$RC"
4832         fi
4833
4834         lctl set_param fail_loc=0x0
4835
4836         LOCKED=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c locked)
4837         DIRTY=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c dirty)
4838         WRITEBACK=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c writeback)
4839         if [[ $LOCKED -ne 0 ]]; then
4840                 error "Locked pages remain in cache, locked=$LOCKED"
4841         fi
4842
4843         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4844                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4845         fi
4846
4847         rm -f $DIR/$tfile
4848         echo "No pages locked after fsync"
4849
4850         reset_async
4851         return 0
4852 }
4853 run_test 118f "Simulate unrecoverable OSC side error =========="
4854
4855 test_118g() {
4856         reset_async
4857
4858         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4859         lctl set_param fail_loc=0x406
4860
4861         # simulate local -ENOMEM
4862         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4863         RC=$?
4864
4865         lctl set_param fail_loc=0
4866         if [[ $RC -eq 0 ]]; then
4867                 error "Must return error due to dropped pages, rc=$RC"
4868         fi
4869
4870         LOCKED=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c locked)
4871         DIRTY=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c dirty)
4872         WRITEBACK=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c writeback)
4873         if [[ $LOCKED -ne 0 ]]; then
4874                 error "Locked pages remain in cache, locked=$LOCKED"
4875         fi
4876
4877         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4878                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4879         fi
4880
4881         rm -f $DIR/$tfile
4882         echo "No pages locked after fsync"
4883
4884         reset_async
4885         return 0
4886 }
4887 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
4888
4889 test_118h() {
4890         remote_ost_nodsh && skip "remote OST with nodsh" && return
4891
4892         reset_async
4893
4894         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
4895         set_nodes_failloc "$(osts_nodes)" 0x20e
4896         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
4897         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4898         RC=$?
4899
4900         set_nodes_failloc "$(osts_nodes)" 0
4901         if [[ $RC -eq 0 ]]; then
4902                 error "Must return error due to dropped pages, rc=$RC"
4903         fi
4904
4905         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
4906         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
4907         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writebac)
4908         if [[ $LOCKED -ne 0 ]]; then
4909                 error "Locked pages remain in cache, locked=$LOCKED"
4910         fi
4911
4912         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4913                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4914         fi
4915
4916         rm -f $DIR/$tfile
4917         echo "No pages locked after fsync"
4918
4919         return 0
4920 }
4921 run_test 118h "Verify timeout in handling recoverables errors  =========="
4922
4923 test_118i() {
4924         remote_ost_nodsh && skip "remote OST with nodsh" && return
4925
4926         reset_async
4927
4928         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
4929         set_nodes_failloc "$(osts_nodes)" 0x20e
4930
4931         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
4932         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
4933         PID=$!
4934         sleep 5
4935         set_nodes_failloc "$(osts_nodes)" 0
4936
4937         wait $PID
4938         RC=$?
4939         if [[ $RC -ne 0 ]]; then
4940                 error "got error, but should be not, rc=$RC"
4941         fi
4942
4943         LOCKED=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c locked)
4944         DIRTY=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c dirty)
4945         WRITEBACK=$(lctl get_param -n "llite.*.dump_page_cache" | grep -c writeback)
4946         if [[ $LOCKED -ne 0 ]]; then
4947                 error "Locked pages remain in cache, locked=$LOCKED"
4948         fi
4949
4950         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4951                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4952         fi
4953
4954         rm -f $DIR/$tfile
4955         echo "No pages locked after fsync"
4956
4957         return 0
4958 }
4959 run_test 118i "Fix error before timeout in recoverable error  =========="
4960
4961 test_118j() {
4962         remote_ost_nodsh && skip "remote OST with nodsh" && return
4963
4964         reset_async
4965
4966         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
4967         set_nodes_failloc "$(osts_nodes)" 0x220
4968
4969         # return -EIO from OST
4970         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4971         RC=$?
4972         set_nodes_failloc "$(osts_nodes)" 0x0
4973         if [[ $RC -eq 0 ]]; then
4974                 error "Must return error due to dropped pages, rc=$RC"
4975         fi
4976
4977         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
4978         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
4979         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
4980         if [[ $LOCKED -ne 0 ]]; then
4981                 error "Locked pages remain in cache, locked=$LOCKED"
4982         fi
4983
4984         # in recoverable error on OST we want resend and stay until it finished
4985         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
4986                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
4987         fi
4988
4989         rm -f $DIR/$tfile
4990         echo "No pages locked after fsync"
4991
4992         return 0
4993 }
4994 run_test 118j "Simulate unrecoverable OST side error =========="
4995
4996 test_118k()
4997 {
4998         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
4999
5000         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
5001         set_nodes_failloc "$(osts_nodes)" 0x20e
5002         mkdir -p $DIR/$tdir
5003
5004         for ((i=0;i<10;i++)); do
5005                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
5006                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
5007                 SLEEPPID=$!
5008                 sleep 0.500s
5009                 kill $SLEEPPID
5010                 wait $SLEEPPID
5011         done
5012
5013         set_nodes_failloc "$(osts_nodes)" 0
5014         rm -rf $DIR/$tdir
5015 }
5016 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
5017
5018 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
5019
5020 test_119a() # bug 11737
5021 {
5022         BSIZE=$((512 * 1024))
5023         directio write $DIR/$tfile 0 1 $BSIZE
5024         # We ask to read two blocks, which is more than a file size.
5025         # directio will indicate an error when requested and actual
5026         # sizes aren't equeal (a normal situation in this case) and
5027         # print actual read amount.
5028         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
5029         if [ "$NOB" != "$BSIZE" ]; then
5030                 error "read $NOB bytes instead of $BSIZE"
5031         fi
5032         rm -f $DIR/$tfile
5033 }
5034 run_test 119a "Short directIO read must return actual read amount"
5035
5036 test_119b() # bug 11737
5037 {
5038         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5039
5040         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
5041         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
5042         sync
5043         multiop $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
5044                 error "direct read failed"
5045         rm -f $DIR/$tfile
5046 }
5047 run_test 119b "Sparse directIO read must return actual read amount"
5048
5049 test_119c() # bug 13099
5050 {
5051         BSIZE=1048576
5052         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
5053         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
5054         rm -f $DIR/$tfile
5055 }
5056 run_test 119c "Testing for direct read hitting hole"
5057
5058 test_119d() # bug 15950
5059 {
5060         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
5061         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
5062         BSIZE=1048576
5063         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
5064         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
5065         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
5066         lctl set_param fail_loc=0x40d
5067         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
5068         pid_dio=$!
5069         sleep 1
5070         cat $DIR/$tfile > /dev/null &
5071         lctl set_param fail_loc=0
5072         pid_reads=$!
5073         wait $pid_dio
5074         log "the DIO writes have completed, now wait for the reads (should not block very long)"
5075         sleep 2
5076         [ -n "`ps h -p $pid_reads -o comm`" ] && \
5077                 error "the read rpcs have not completed in 2s"
5078         rm -f $DIR/$tfile
5079         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
5080 }
5081 run_test 119d "The DIO path should try to send a new rpc once one is completed"
5082
5083 test_120a() {
5084         mkdir -p $DIR/$tdir
5085         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
5086                skip "no early lock cancel on server" && return 0
5087         lru_resize_disable mdc
5088         lru_resize_disable osc
5089         cancel_lru_locks mdc
5090         stat $DIR/$tdir > /dev/null
5091         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5092         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5093         mkdir $DIR/$tdir/d1
5094         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5095         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5096         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
5097         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
5098         lru_resize_enable mdc
5099         lru_resize_enable osc
5100 }
5101 run_test 120a "Early Lock Cancel: mkdir test ==================="
5102
5103 test_120b() {
5104         mkdir -p $DIR/$tdir
5105         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
5106                skip "no early lock cancel on server" && return 0
5107         lru_resize_disable mdc
5108         lru_resize_disable osc
5109         cancel_lru_locks mdc
5110         stat $DIR/$tdir > /dev/null
5111         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5112         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5113         touch $DIR/$tdir/f1
5114         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5115         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5116         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
5117         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
5118         lru_resize_enable mdc
5119         lru_resize_enable osc
5120 }
5121 run_test 120b "Early Lock Cancel: create test =================="
5122
5123 test_120c() {
5124         mkdir -p $DIR/$tdir
5125         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
5126                skip "no early lock cancel on server" && return 0
5127         lru_resize_disable mdc
5128         lru_resize_disable osc
5129         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
5130         touch $DIR/$tdir/d1/f1
5131         cancel_lru_locks mdc
5132         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
5133         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5134         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5135         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
5136         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5137         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5138         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
5139         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
5140         lru_resize_enable mdc
5141         lru_resize_enable osc
5142 }
5143 run_test 120c "Early Lock Cancel: link test ===================="
5144
5145 test_120d() {
5146         mkdir -p $DIR/$tdir
5147         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
5148                skip "no early lock cancel on server" && return 0
5149         lru_resize_disable mdc
5150         lru_resize_disable osc
5151         touch $DIR/$tdir
5152         cancel_lru_locks mdc
5153         stat $DIR/$tdir > /dev/null
5154         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5155         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5156         chmod a+x $DIR/$tdir
5157         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5158         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5159         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
5160         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
5161         lru_resize_enable mdc
5162         lru_resize_enable osc
5163 }
5164 run_test 120d "Early Lock Cancel: setattr test ================="
5165
5166 test_120e() {
5167         mkdir -p $DIR/$tdir
5168         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
5169                skip "no early lock cancel on server" && return 0
5170         lru_resize_disable mdc
5171         lru_resize_disable osc
5172         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
5173         cancel_lru_locks mdc
5174         cancel_lru_locks osc
5175         dd if=$DIR/$tdir/f1 of=/dev/null
5176         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
5177         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5178         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5179         unlink $DIR/$tdir/f1
5180         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5181         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5182         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
5183         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
5184         lru_resize_enable mdc
5185         lru_resize_enable osc
5186 }
5187 run_test 120e "Early Lock Cancel: unlink test =================="
5188
5189 test_120f() {
5190         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
5191                skip "no early lock cancel on server" && return 0
5192         mkdir -p $DIR/$tdir
5193         lru_resize_disable mdc
5194         lru_resize_disable osc
5195         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
5196         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
5197         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
5198         cancel_lru_locks mdc
5199         cancel_lru_locks osc
5200         dd if=$DIR/$tdir/d1/f1 of=/dev/null
5201         dd if=$DIR/$tdir/d2/f2 of=/dev/null
5202         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
5203         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5204         blk1=`lctl get_param -n ldlm/services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5205         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
5206         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5207         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5208         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
5209         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
5210         lru_resize_enable mdc
5211         lru_resize_enable osc
5212 }
5213 run_test 120f "Early Lock Cancel: rename test =================="
5214
5215 test_120g() {
5216         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
5217                skip "no early lock cancel on server" && return 0
5218         lru_resize_disable mdc
5219         lru_resize_disable osc
5220         count=10000
5221         echo create $count files
5222         mkdir -p $DIR/$tdir
5223         cancel_lru_locks mdc
5224         cancel_lru_locks osc
5225         t0=`date +%s`
5226
5227         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5228         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5229         createmany -o $DIR/$tdir/f $count
5230         sync
5231         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5232         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5233         t1=`date +%s`
5234         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
5235         echo rm $count files
5236         rm -r $DIR/$tdir
5237         sync
5238         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
5239         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
5240         t2=`date +%s`
5241         echo total: $count removes in $((t2-t1))
5242         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
5243         sleep 2
5244         # wait for commitment of removal
5245         lru_resize_enable mdc
5246         lru_resize_enable osc
5247 }
5248 run_test 120g "Early Lock Cancel: performance test ============="
5249
5250 test_121() { #bug 10589
5251         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
5252         lctl set_param fail_loc=0x310
5253         cancel_lru_locks osc > /dev/null
5254         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
5255         lctl set_param fail_loc=0
5256         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
5257 }
5258 run_test 121 "read cancel race ================================="
5259
5260 test_122() { #bug 11544
5261         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
5262         lctl set_param fail_loc=0x508
5263         dd if=/dev/zero of=$DIR/$tfile count=1
5264         sync
5265         lctl set_param fail_loc=0
5266 }
5267 run_test 122 "fail client bulk callback (shouldn't LBUG) ======="
5268
5269 test_123a() { # was test 123, statahead(bug 11401)
5270         SLOWOK=0
5271         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
5272                 log "testing on UP system. Performance may be not as good as expected."
5273                 SLOWOK=1
5274         fi
5275
5276         rm -rf $DIR/$tdir
5277         mkdir -p $DIR/$tdir
5278         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5279         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
5280         MULT=10
5281         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
5282                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
5283
5284                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
5285                 lctl set_param -n llite.*.statahead_max 0
5286                 lctl get_param llite.*.statahead_max
5287                 cancel_lru_locks mdc
5288                 cancel_lru_locks osc
5289                 stime=`date +%s`
5290                 time ls -l $DIR/$tdir | wc -l
5291                 etime=`date +%s`
5292                 delta=$((etime - stime))
5293                 log "ls $i files without statahead: $delta sec"
5294                 lctl set_param llite.*.statahead_max=$max
5295
5296                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
5297                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
5298                 cancel_lru_locks mdc
5299                 cancel_lru_locks osc
5300                 stime=`date +%s`
5301                 time ls -l $DIR/$tdir | wc -l
5302                 etime=`date +%s`
5303                 delta_sa=$((etime - stime))
5304                 log "ls $i files with statahead: $delta_sa sec"
5305                 lctl get_param -n llite.*.statahead_stats
5306                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
5307
5308                 if [ $swrong -lt $ewrong ]; then
5309                         log "statahead was stopped, maybe too many locks held!"
5310                 fi
5311
5312                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
5313
5314                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
5315                         if [  $SLOWOK -eq 0 ]; then
5316                                 error "ls $i files is slower with statahead!"
5317                                 debugsave
5318
5319                                 lctl set_param debug=-1
5320                                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
5321                                 lctl set_param -n llite.*.statahead_max 0
5322                                 lctl get_param llite.*.statahead_max
5323                                 cancel_lru_locks mdc
5324                                 cancel_lru_locks osc
5325                                 $LCTL clear
5326                                 stime=`date +%s`
5327                                 time ls -l $DIR/$tdir | wc -l
5328                                 etime=`date +%s`
5329                                 error "ls $i files (again) without statahead: $((etime - stime)) sec"
5330
5331                                 lctl set_param debug=-1
5332                                 lctl set_param llite.*.statahead_max=$max
5333                                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
5334                                 cancel_lru_locks mdc
5335                                 cancel_lru_locks osc
5336                                 $LCTL clear
5337                                 stime=`date +%s`
5338                                 time ls -l $DIR/$tdir | wc -l
5339                                 etime=`date +%s`
5340                                 error "ls $i files (again) with statahead: $((etime - stime)) sec"
5341                                 lctl get_param -n llite.*.statahead_stats
5342
5343                                 debugrestore
5344                         else
5345                                 log "ls $i files is slower with statahead!"
5346                         fi
5347                         break
5348                 fi
5349
5350                 [ $delta -gt 20 ] && break
5351                 [ $delta -gt 8 ] && MULT=$((50 / delta))
5352                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
5353         done
5354         log "ls done"
5355
5356         stime=`date +%s`
5357         rm -r $DIR/$tdir
5358         sync
5359         etime=`date +%s`
5360         delta=$((etime - stime))
5361         log "rm -r $DIR/$tdir/: $delta seconds"
5362         log "rm done"
5363         lctl get_param -n llite.*.statahead_stats
5364 }
5365 run_test 123a "verify statahead work"
5366
5367 test_123b () { # statahead(bug 15027)
5368         mkdir -p $DIR/$tdir
5369         createmany -o $DIR/$tdir/$tfile-%d 1000
5370
5371         cancel_lru_locks mdc
5372         cancel_lru_locks osc
5373
5374 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
5375         lctl set_param fail_loc=0x80000803
5376         ls -lR $DIR/$tdir > /dev/null
5377         log "ls done"
5378         lctl set_param fail_loc=0x0
5379         lctl get_param -n llite.*.statahead_stats
5380         rm -r $DIR/$tdir
5381         sync
5382
5383 }
5384 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
5385
5386 test_124a() {
5387         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
5388                skip "no lru resize on server" && return 0
5389         local NR=2000
5390         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
5391
5392         log "create $NR files at $DIR/$tdir"
5393         createmany -o $DIR/$tdir/f $NR ||
5394                 error "failed to create $NR files in $DIR/$tdir"
5395
5396         cancel_lru_locks mdc
5397         ls -l $DIR/$tdir > /dev/null
5398
5399         local NSDIR=""
5400         local LRU_SIZE=0
5401         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
5402                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5403                 LRU_SIZE=$(lctl get_param -n $PARAM)
5404                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
5405                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
5406                         log "NS=$(basename $NSDIR)"
5407                         break
5408                 fi
5409         done
5410
5411         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
5412                 skip "Not enough cached locks created!"
5413                 return 0
5414         fi
5415         log "LRU=$LRU_SIZE"
5416
5417         local SLEEP=30
5418
5419         # We know that lru resize allows one client to hold $LIMIT locks
5420         # for 10h. After that locks begin to be killed by client.
5421         local MAX_HRS=10
5422         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
5423
5424         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
5425         # killing locks. Some time was spent for creating locks. This means
5426         # that up to the moment of sleep finish we must have killed some of
5427         # them (10-100 locks). This depends on how fast ther were created.
5428         # Many of them were touched in almost the same moment and thus will
5429         # be killed in groups.
5430         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
5431
5432         # Use $LRU_SIZE_B here to take into account real number of locks
5433         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
5434         local LRU_SIZE_B=$LRU_SIZE
5435         log "LVF=$LVF"
5436         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
5437         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
5438
5439         # Let's make sure that we really have some margin. Client checks
5440         # cached locks every 10 sec.
5441         SLEEP=$((SLEEP+20))
5442         log "Sleep ${SLEEP} sec"
5443         local SEC=0
5444         while ((SEC<$SLEEP)); do
5445                 echo -n "..."
5446                 sleep 5
5447                 SEC=$((SEC+5))
5448                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
5449                 echo -n "$LRU_SIZE"
5450         done
5451         echo ""
5452         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
5453         local LRU_SIZE_A=`lctl get_param -n $NSDIR/lru_size`
5454
5455         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
5456                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
5457                 unlinkmany $DIR/$tdir/f $NR
5458                 return
5459         }
5460
5461         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
5462         log "unlink $NR files at $DIR/$tdir"
5463         unlinkmany $DIR/$tdir/f $NR
5464 }
5465 run_test 124a "lru resize ======================================="
5466
5467 test_124b() {
5468         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
5469                skip "no lru resize on server" && return 0
5470
5471         # even for cmd no matter what metadata namespace to use for getting
5472         # the limit, we use appropriate.
5473         LIMIT=`lctl get_param -n ldlm.namespaces.*mdc*.pool.limit`
5474
5475         NR=$(($(default_lru_size)*20))
5476         if [ $NR -gt $LIMIT ]; then
5477                 NR=$LIMIT
5478         fi
5479         lru_resize_disable mdc
5480         mkdir -p $DIR/$tdir/disable_lru_resize ||
5481                 error "failed to create $DIR/$tdir/disable_lru_resize"
5482
5483         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
5484         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
5485         cancel_lru_locks mdc
5486         stime=`date +%s`
5487         PID=""
5488         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
5489         PID="$PID $!"
5490         sleep 2
5491         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
5492         PID="$PID $!"
5493         sleep 2
5494         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
5495         PID="$PID $!"
5496         wait $PID
5497         etime=`date +%s`
5498         nolruresize_delta=$((etime-stime))
5499         log "ls -la time: $nolruresize_delta seconds"
5500         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
5501         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
5502
5503         lru_resize_enable mdc
5504         mkdir -p $DIR/$tdir/enable_lru_resize ||
5505                 error "failed to create $DIR/$tdir/enable_lru_resize"
5506
5507         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
5508         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
5509         cancel_lru_locks mdc
5510         stime=`date +%s`
5511         PID=""
5512         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
5513         PID="$PID $!"
5514         sleep 2
5515         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
5516         PID="$PID $!"
5517         sleep 2
5518         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
5519         PID="$PID $!"
5520         wait $PID
5521         etime=`date +%s`
5522         lruresize_delta=$((etime-stime))
5523         log "ls -la time: $lruresize_delta seconds"
5524         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
5525
5526         if [ $lruresize_delta -gt $nolruresize_delta ]; then
5527                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
5528         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
5529                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
5530         else
5531                 log "lru resize performs the same with no lru resize"
5532         fi
5533         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
5534 }
5535 run_test 124b "lru resize (performance test) ======================="
5536
5537 test_125() { # 13358
5538         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5539         mkdir -p $DIR/d125 || error "mkdir failed"
5540         $SETSTRIPE $DIR/d125 -s 65536 -c -1 || error "setstripe failed"
5541         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failes"
5542         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
5543 }
5544 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
5545
5546 test_126() { # bug 12829/13455
5547         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
5548         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
5549         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
5550         rm -f $DIR/$tfile
5551         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
5552 }
5553 run_test 126 "check that the fsgid provided by the client is taken into account"
5554
5555 test_127() { # bug 15521
5556         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
5557         $LCTL set_param osc.*.stats=0
5558         FSIZE=$((2048 * 1024))
5559         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
5560         cancel_lru_locks osc
5561         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
5562
5563         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
5564         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
5565                 echo "got $COUNT $NAME"
5566                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
5567                 eval $NAME=$COUNT || error "Wrong proc format"
5568
5569                 case $NAME in
5570                         read_bytes|write_bytes)
5571                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
5572                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
5573                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
5574                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
5575                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
5576                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
5577                                 error "sumsquare is too small: $SUMSQ"
5578                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
5579                                 error "sumsquare is too big: $SUMSQ"
5580                         ;;
5581                         *) ;;
5582                 esac
5583         done < $DIR/${tfile}.tmp
5584
5585         #check that we actually got some stats
5586         [ "$read_bytes" ] || error "Missing read_bytes stats"
5587         [ "$write_bytes" ] || error "Missing write_bytes stats"
5588         [ "$read_bytes" != 0 ] || error "no read done"
5589         [ "$write_bytes" != 0 ] || error "no write done"
5590 }
5591 run_test 127 "verify the client stats are sane"
5592
5593 test_128() { # bug 15212
5594         touch $DIR/$tfile
5595         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
5596                 find $DIR/$tfile
5597                 find $DIR/$tfile
5598         EOF
5599
5600         result=$(grep error $TMP/$tfile.log)
5601         rm -f $DIR/$tfile
5602         [ -z "$result" ] || error "consecutive find with interactive lfs failed"
5603 }
5604 run_test 128 "interactive lfs for 2 consecutive finds"
5605
5606 test_129() {
5607         [ "$FSTYPE" != "ldiskfs" ] && skip "not needed for FSTYPE=$FSTYPE" && return 0
5608         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5609
5610         local MNTDEV=$(get_mds_mntdev_proc_path)
5611         DEV=$(basename $(do_facet mds lctl get_param -n $MNTDEV))
5612         [ -z "$DEV" ] && error "can't access mds mntdev"
5613         EFBIG=27
5614         LDPROC=/proc/fs/ldiskfs/$DEV/max_dir_size
5615         MAX=16384
5616
5617         do_facet mds "echo $MAX > $LDPROC"
5618
5619         mkdir -p $DIR/$tdir
5620
5621         I=0
5622         J=0
5623         while [ ! $I -gt $MAX ]; do
5624                 multiop $DIR/$tdir/$J Oc
5625                 rc=$?
5626                 if [ $rc -eq $EFBIG ]; then
5627                         do_facet mds "echo 0 >$LDPROC"
5628                         echo "return code $rc received as expected"
5629                         return 0
5630                 elif [ $rc -ne 0 ]; then
5631                         do_facet mds "echo 0 >$LDPROC"
5632                         error_exit "error $rc instead of expected $EFBIG"
5633                 fi
5634                 J=$((J+1))
5635                 I=$(stat -c%s "$DIR/$tdir")
5636         done
5637
5638         error "exceeded dir size limit: $I bytes"
5639         do_facet mds "echo 0 >$LDPROC"
5640 }
5641 run_test 129 "test directory size limit ========================"
5642
5643 OLDIFS="$IFS"
5644 cleanup_130() {
5645         trap 0
5646         IFS="$OLDIFS"
5647 }
5648
5649 test_130a() {
5650         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
5651         [ -n "$filefrag_op" ] && skip "filefrag has no FIEMAP support" && return
5652
5653         trap cleanup_130 EXIT RETURN
5654
5655         local fm_file=$DIR/$tfile
5656         lfs setstripe -s 65536 -c 1 $fm_file ||
5657                 error "setstripe failed on $fm_file"
5658         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
5659                 error "dd failed for $fm_file"
5660
5661         filefrag -ves $fm_file || error "filefrag $fm_file failed"
5662         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
5663
5664         lun=`$GETSTRIPE $fm_file  | grep -A 10 obdidx | awk '{print $1}' | grep -v "obdidx"`
5665
5666         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
5667         IFS=$'\n'
5668         tot_len=0
5669         for line in $filefrag_op
5670         do
5671                 frag_lun=`echo $line | cut -d: -f5`
5672                 ext_len=`echo $line | cut -d: -f4`
5673                 if (( $frag_lun != $lun )); then
5674                         cleanup_130
5675                         error "FIEMAP on 1-stripe file($fm_file) failed"
5676                         return
5677                 fi
5678                 (( tot_len += ext_len ))
5679         done
5680
5681         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
5682                 cleanup_130
5683                 error "FIEMAP on 1-stripe file($fm_file) failed;"
5684                 return
5685         fi
5686
5687         cleanup_130
5688
5689         echo "FIEMAP on single striped file succeeded"
5690 }
5691 run_test 130a "FIEMAP (1-stripe file)"
5692
5693 test_130b() {
5694         [ "$OSTCOUNT" -lt "2" ] && skip_env "FIEMAP on 2-stripe file test" && return
5695
5696         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
5697         [ -n "$filefrag_op" ] && skip "filefrag has no FIEMAP support" && return
5698
5699         trap cleanup_130 EXIT RETURN
5700
5701         local fm_file=$DIR/$tfile
5702         lfs setstripe -s 65536 -c 2 $fm_file ||
5703                 error "setstripe failed on $fm_file"
5704         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
5705                 error "dd failed on $fm_file"
5706
5707         filefrag -ves $fm_file || error "filefrag $fm_file failed"
5708         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
5709
5710         last_lun=`echo $filefrag_op | cut -d: -f5`
5711
5712         IFS=$'\n'
5713         tot_len=0
5714         num_luns=1
5715         for line in $filefrag_op; do
5716                 frag_lun=`echo $line | cut -d: -f5`
5717                 ext_len=`echo $line | cut -d: -f4`
5718                 if (( $frag_lun != $last_lun )); then
5719                         if (( tot_len != 1024 )); then
5720                                 cleanup_130
5721                                 error "FIEMAP $fm_file: len $tot_len for OST $last_lun instead of 256"
5722                                 return
5723                         else
5724                                 (( num_luns += 1 ))
5725                                 tot_len=0
5726                         fi
5727                 fi
5728                 (( tot_len += ext_len ))
5729                 last_lun=$frag_lun
5730         done
5731         if (( num_luns != 2 || tot_len != 1024 )); then
5732                 cleanup_130
5733                 error "FIEMAP $fm_file: wrong number of LUNs or wrong len for OST $last_lun"
5734                 return
5735         fi
5736
5737         cleanup_130
5738
5739         echo "FIEMAP on 2-stripe file succeeded"
5740 }
5741 run_test 130b "FIEMAP (2-stripe file)"
5742
5743 test_130c() {
5744         [ "$OSTCOUNT" -lt "2" ] && skip_env "FIEMAP on 2-stripe hole test" && return
5745
5746         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
5747         [ -n "$filefrag_op" ] && skip "filefrag has no FIEMAP support" && return
5748
5749         trap cleanup_130 EXIT RETURN
5750
5751         local fm_file=$DIR/$tfile
5752         lfs setstripe -s 65536 -c 2 $fm_file ||
5753                 error "setstripe failed on $fm_file"
5754         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
5755                 error "dd failed on $fm_file"
5756
5757         filefrag -ves $fm_file || error "filefrag $fm_file failed"
5758         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
5759
5760         last_lun=`echo $filefrag_op | cut -d: -f5`
5761
5762         IFS=$'\n'
5763         tot_len=0
5764         num_luns=1
5765         for line in $filefrag_op
5766         do
5767                 frag_lun=`echo $line | cut -d: -f5`
5768                 ext_len=`echo $line | cut -d: -f4`
5769                 if (( $frag_lun != $last_lun )); then
5770                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
5771                         if (( logical != 512 )); then
5772                                 cleanup_130
5773                                 error "FIEMAP $fm_file: logical start for LUN $logical instead of 512"
5774                                 return
5775                         fi
5776                         if (( tot_len != 512 )); then
5777                                 cleanup_130
5778                                 error "FIEMAP $fm_file: len $tot_len for OST $last_lun instead of 1024"
5779                                 return
5780                         else
5781                                 (( num_luns += 1 ))
5782                                 tot_len=0
5783                         fi
5784                 fi
5785                 (( tot_len += ext_len ))
5786                 last_lun=$frag_lun
5787         done
5788         if (( num_luns != 2 || tot_len != 512 )); then
5789                 cleanup_130
5790                 error "FIEMAP $fm_file: wrong number of LUNs or wrong len for OST $last_lun"
5791                 return
5792         fi
5793
5794         cleanup_130
5795
5796         echo "FIEMAP on 2-stripe file with hole succeeded"
5797 }
5798 run_test 130c "FIEMAP (2-stripe file with hole)"
5799
5800 test_130d() {
5801         [ "$OSTCOUNT" -lt "3" ] && skip_env "FIEMAP on N-stripe file test" && return
5802
5803         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
5804         [ -n "$filefrag_op" ] && skip "filefrag has no FIEMAP support" && return
5805
5806         trap cleanup_130 EXIT RETURN
5807
5808         local fm_file=$DIR/$tfile
5809         lfs setstripe -s 65536 -c $OSTCOUNT $fm_file ||
5810                 error "setstripe failed on $fm_file"
5811         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
5812                 error "dd failed on $fm_file"
5813
5814         filefrag -ves $fm_file || error "filefrag $fm_file failed"
5815         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
5816
5817         last_lun=`echo $filefrag_op | cut -d: -f5`
5818
5819         IFS=$'\n'
5820         tot_len=0
5821         num_luns=1
5822         for line in $filefrag_op
5823         do
5824                 frag_lun=`echo $line | cut -d: -f5`
5825                 ext_len=`echo $line | cut -d: -f4`
5826                 if (( $frag_lun != $last_lun )); then
5827                         if (( tot_len != 1024 )); then
5828                                 cleanup_130
5829                                 error "FIEMAP $fm_file: len $tot_len for OST $last_lun instead of 1024"
5830                                 return
5831                         else
5832                                 (( num_luns += 1 ))
5833                                 tot_len=0
5834                         fi
5835                 fi
5836                 (( tot_len += ext_len ))
5837                 last_lun=$frag_lun
5838         done
5839         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
5840                 cleanup_130
5841                 error "FIEMAP $fm_file: wrong number of LUNs or wrong len for OST $last_lun"
5842                 return
5843         fi
5844
5845         cleanup_130
5846
5847         echo "FIEMAP on N-stripe file succeeded"
5848 }
5849 run_test 130d "FIEMAP (N-stripe file)"
5850
5851 test_130e() {
5852         [ "$OSTCOUNT" -lt "2" ] && skip "continuation FIEMAP test" && return
5853
5854         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
5855         [ -n "$filefrag_op" ] && skip "filefrag has no FIEMAP support" && return
5856
5857         trap cleanup_130 EXIT RETURN
5858
5859         local fm_file=$DIR/$tfile
5860         lfs setstripe -s 131072 -c 2 $fm_file ||
5861                 error "setstripe failed on $fm_file"
5862         NUM_BLKS=512
5863         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
5864         for ((i = 0; i < $NUM_BLKS; i++)); do
5865                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
5866         done
5867
5868         filefrag -ves $fm_file || error "filefrag $fm_file failed"
5869         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
5870
5871         last_lun=`echo $filefrag_op | cut -d: -f5`
5872
5873         IFS=$'\n'
5874         tot_len=0
5875         num_luns=1
5876         for line in $filefrag_op
5877         do
5878                 frag_lun=`echo $line | cut -d: -f5`
5879                 ext_len=`echo $line | cut -d: -f4`
5880                 if (( $frag_lun != $last_lun )); then
5881                         if (( tot_len != $EXPECTED_LEN )); then
5882                                 cleanup_130
5883                                 error "FIEMAP $fm_file: len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
5884                                 return
5885                         else
5886                                 (( num_luns += 1 ))
5887                                 tot_len=0
5888                         fi
5889                 fi
5890                 (( tot_len += ext_len ))
5891                 last_lun=$frag_lun
5892         done
5893         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
5894                 cleanup_130
5895                 error "FIEMAP $fm_file: wrong number of LUNs or wrong len for OST $last_lun"
5896                 return
5897         fi
5898
5899         cleanup_130
5900
5901         echo "FIEMAP with continuation calls succeeded"
5902 }
5903 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
5904
5905 test_140() { #bug-17379
5906         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
5907         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
5908         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
5909
5910         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
5911         local i=0
5912         while i=`expr $i + 1`; do
5913                 mkdir -p $i || error "Creating dir $i"
5914                 cd $i || error "Changing to $i"
5915                 ln -s ../stat stat || error "Creating stat symlink"
5916                 # Read the symlink until ELOOP present,
5917                 # not LBUGing the system is considered success,
5918                 # we didn't overrun the stack.
5919                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
5920                 [ $ret -ne 0 ] && {
5921                         if [ $ret -eq 40 ]; then
5922                                 break  # -ELOOP
5923                         else
5924                                 error "Open stat symlink"
5925                                 return
5926                         fi
5927                 }
5928         done
5929         i=`expr $i - 1`
5930         echo "The symlink depth = $i"
5931         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 ] || error "Invalid symlink depth"
5932 }
5933 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
5934
5935 test_150() {
5936         local TF="$TMP/$tfile"
5937
5938         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
5939         cp $TF $DIR/$tfile
5940         cancel_lru_locks osc
5941         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
5942         remount_client $MOUNT
5943         df -P $MOUNT
5944         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
5945
5946         $TRUNCATE $TF 6000
5947         $TRUNCATE $DIR/$tfile 6000
5948         cancel_lru_locks osc
5949         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
5950
5951         echo "12345" >>$TF
5952         echo "12345" >>$DIR/$tfile
5953         cancel_lru_locks osc
5954         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
5955
5956         echo "12345" >>$TF
5957         echo "12345" >>$DIR/$tfile
5958         cancel_lru_locks osc
5959         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
5960
5961         rm -f $TF
5962         true
5963 }
5964 run_test 150 "truncate/append tests"
5965
5966 function roc_hit() {
5967     local list=$(comma_list $(osts_nodes))
5968
5969     ACCNUM=$(do_nodes $list $LCTL get_param -n obdfilter.*.stats | \
5970         awk '/'cache_hit'/ {sum+=$2} END {print sum}')
5971     echo $ACCNUM
5972 }
5973
5974 function set_cache() {
5975     local on=1
5976
5977     if [ "$2" == "off" ]; then
5978         on=0;
5979     fi
5980     local list=$(comma_list $(osts_nodes))
5981     do_nodes $list lctl set_param obdfilter.*.${1}_cache_enable $on
5982
5983     cancel_lru_locks osc
5984 }
5985
5986 test_151() {
5987         remote_ost_nodsh && skip "remote OST with nodsh" && return
5988
5989         local list=$(comma_list $(osts_nodes))
5990         if ! do_nodes $list grep -q truncate_inode_pages_range /proc/kallsyms; then  #b=18718
5991                 skip "old RHEL4/SLES9 kernel" && return
5992         fi
5993
5994         local CPAGES=3
5995
5996         # check whether obdfilter is cache capable at all
5997         if ! do_nodes $list $LCTL get_param -n obdfilter.*.read_cache_enable > /dev/null; then
5998                 echo "not cache-capable obdfilter"
5999                 return 0
6000         fi
6001
6002         # make sure cache is enabled on all obdfilters
6003         do_nodes $list $LCTL set_param obdfilter.*.read_cache_enable=1
6004         do_nodes $list $LCTL set_param obdfilter.*.writethrough_cache_enable=1
6005
6006         # pages should be in the case right after write
6007         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
6008         local BEFORE=`roc_hit`
6009         cancel_lru_locks osc
6010         cat $DIR/$tfile >/dev/null
6011         local AFTER=`roc_hit`
6012         if let "AFTER - BEFORE != CPAGES"; then
6013                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
6014         fi
6015
6016         # the following read invalidates the cache
6017         cancel_lru_locks osc
6018         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable 0
6019         cat $DIR/$tfile >/dev/null
6020
6021         # now data shouldn't be found in the cache
6022         BEFORE=`roc_hit`
6023         cancel_lru_locks osc
6024         cat $DIR/$tfile >/dev/null
6025         AFTER=`roc_hit`
6026         if let "AFTER - BEFORE != 0"; then
6027                 error "IN CACHE: before: $BEFORE, after: $AFTER"
6028         fi
6029
6030         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
6031         do_nodes $list $LCTL set_param obdfilter.*.writethrough_cache_enable=1
6032         rm -f $DIR/$tfile
6033 }
6034 run_test 151 "test cache on oss and controls ==============================="
6035
6036 test_152() {
6037         local TF="$TMP/$tfile"
6038
6039         # simulate ENOMEM during write
6040 #define OBD_FAIL_OST_NOMEM      0x226
6041         lctl set_param fail_loc=0x80000226
6042         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
6043         cp $TF $DIR/$tfile
6044         sync || error "sync failed"
6045         lctl set_param fail_loc=0
6046
6047         # discard client's cache
6048         cancel_lru_locks osc
6049
6050         # simulate ENOMEM during read
6051         lctl set_param fail_loc=0x80000226
6052         cmp $TF $DIR/$tfile || error "cmp failed"
6053         lctl set_param fail_loc=0
6054
6055         rm -f $TF
6056 }
6057 run_test 152 "test read/write with enomem ============================"
6058
6059 test_153() {
6060         multiop $DIR/$tfile Ow4096Ycu || error "multiop failed"
6061 }
6062 run_test 153 "test if fdatasync does not crash ======================="
6063
6064 test_154() {
6065         # do directio so as not to populate the page cache
6066         log "creating a 10 Mb file"
6067         multiop $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
6068         log "starting reads"
6069         dd if=$DIR/$tfile of=/dev/null bs=4096 &
6070         log "truncating the file"
6071         multiop $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
6072         log "killing dd"
6073         kill %+ || true # reads might have finished
6074         echo "wait until dd is finished"
6075         wait
6076         log "removing the temporary file"
6077         rm -rf $DIR/$tfile || error "tmp file removal failed"
6078 }
6079 run_test 154 "parallel read and truncate should not deadlock ==="
6080
6081 test_155_load() {
6082     local temp=$TMP/$tfile
6083     local file=$DIR/$tfile
6084     local list=$(comma_list $(osts_nodes))
6085     local big=$(do_nodes $list grep "cache" /proc/cpuinfo | \
6086         awk '{sum+=$4} END{print sum}')
6087
6088     log big is $big K
6089
6090     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
6091         error "dd of=$temp bs=6096 count=1 failed"
6092     cp $temp $file
6093     cancel_lru_locks osc
6094     cmp $temp $file || error "$temp $file differ"
6095
6096     $TRUNCATE $temp 6000
6097     $TRUNCATE $file 6000
6098     cmp $temp $file || error "$temp $file differ (truncate1)"
6099
6100     echo "12345" >>$temp
6101     echo "12345" >>$file
6102     cmp $temp $file || error "$temp $file differ (append1)"
6103
6104     echo "12345" >>$temp
6105     echo "12345" >>$file
6106     cmp $temp $file || error "$temp $file differ (append2)"
6107
6108     dd if=/dev/urandom of=$temp bs=$((big*2)) count=1k || \
6109         error "dd of=$temp bs=$((big*2)) count=1k failed"
6110     cp $temp $file
6111     ls -lh $temp $file
6112     cancel_lru_locks osc
6113     cmp $temp $file || error "$temp $file differ"
6114
6115     rm -f $temp
6116     true
6117 }
6118
6119 test_155a() {
6120     set_cache read on
6121     set_cache writethrough on
6122     test_155_load
6123 }
6124 run_test 155a "Verification of correctness: read cache:on write_cache:on"
6125
6126 test_155b() {
6127     set_cache read on
6128     set_cache writethrough off
6129     test_155_load
6130 }
6131 run_test 155b "Verification of correctness: read cache:on write_cache:off"
6132
6133 test_155c() {
6134     set_cache read off
6135     set_cache writethrough on
6136     test_155_load
6137 }
6138 run_test 155c "Verification of correctness: read cache:off write_cache:on"
6139
6140 test_155d() {
6141     set_cache read off
6142     set_cache writethrough off
6143     test_155_load
6144 }
6145 run_test 155d "Verification of correctness: read cache:off write_cache:off "
6146
6147 test_156() {
6148     local CPAGES=3
6149     local BEFORE
6150     local AFTER
6151     local file="$DIR/$tfile"
6152
6153     log "Turn on read and write cache"
6154     set_cache read on
6155     set_cache writethrough on
6156
6157     log "Write data and read it back."
6158     log "Read should be satisfied from the cache."
6159     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
6160     BEFORE=`roc_hit`
6161     cancel_lru_locks osc
6162     cat $file >/dev/null
6163     AFTER=`roc_hit`
6164     if ! let "AFTER - BEFORE == CPAGES"; then
6165         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
6166     else
6167         log "cache hits:: before: $BEFORE, after: $AFTER"
6168     fi
6169
6170     log "Read again; it should be satisfied from the cache."
6171     BEFORE=$AFTER
6172     cancel_lru_locks osc
6173     cat $file >/dev/null
6174     AFTER=`roc_hit`
6175     if ! let "AFTER - BEFORE == CPAGES"; then
6176         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
6177     else
6178         log "cache hits:: before: $BEFORE, after: $AFTER"
6179     fi
6180
6181
6182     log "Turn off the read cache and turn on the write cache"
6183     set_cache read off
6184     set_cache writethrough on
6185
6186     log "Read again; it should be satisfied from the cache."
6187     BEFORE=`roc_hit`
6188     cancel_lru_locks osc
6189     cat $file >/dev/null
6190     AFTER=`roc_hit`
6191     if ! let "AFTER - BEFORE == CPAGES"; then
6192         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
6193     else
6194         log "cache hits:: before: $BEFORE, after: $AFTER"
6195     fi
6196
6197     log "Read again; it should not be satisfied from the cache."
6198     BEFORE=$AFTER
6199     cancel_lru_locks osc
6200     cat $file >/dev/null
6201     AFTER=`roc_hit`
6202     if ! let "AFTER - BEFORE == 0"; then
6203         error "IN CACHE: before: $BEFORE, after: $AFTER"
6204     else
6205         log "cache hits:: before: $BEFORE, after: $AFTER"
6206     fi
6207
6208     log "Write data and read it back."
6209     log "Read should be satisfied from the cache."
6210     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
6211     BEFORE=`roc_hit`
6212     cancel_lru_locks osc
6213     cat $file >/dev/null
6214     AFTER=`roc_hit`
6215     if ! let "AFTER - BEFORE == CPAGES"; then
6216         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
6217     else
6218         log "cache hits:: before: $BEFORE, after: $AFTER"
6219     fi
6220
6221     log "Read again; it should not be satisfied from the cache."
6222     BEFORE=$AFTER
6223     cancel_lru_locks osc
6224     cat $file >/dev/null
6225     AFTER=`roc_hit`
6226     if ! let "AFTER - BEFORE == 0"; then
6227         error "IN CACHE: before: $BEFORE, after: $AFTER"
6228     else
6229         log "cache hits:: before: $BEFORE, after: $AFTER"
6230     fi
6231
6232
6233     log "Turn off read and write cache"
6234     set_cache read off
6235     set_cache writethrough off
6236
6237     log "Write data and read it back"
6238     log "It should not be satisfied from the cache."
6239     rm -f $file
6240     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
6241     cancel_lru_locks osc
6242     BEFORE=`roc_hit`
6243     cat $file >/dev/null
6244     AFTER=`roc_hit`
6245     if ! let "AFTER - BEFORE == 0"; then
6246         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
6247     else
6248         log "cache hits:: before: $BEFORE, after: $AFTER"
6249     fi
6250
6251
6252     log "Turn on the read cache and turn off the write cache"
6253     set_cache read on
6254     set_cache writethrough off
6255
6256     log "Write data and read it back"
6257     log "It should not be satisfied from the cache."
6258     rm -f $file
6259     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
6260     BEFORE=`roc_hit`
6261     cancel_lru_locks osc
6262     cat $file >/dev/null
6263     AFTER=`roc_hit`
6264     if ! let "AFTER - BEFORE == 0"; then
6265         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
6266     else
6267         log "cache hits:: before: $BEFORE, after: $AFTER"
6268     fi
6269
6270     log "Read again; it should be satisfied from the cache."
6271     BEFORE=`roc_hit`
6272     cancel_lru_locks osc
6273     cat $file >/dev/null
6274     AFTER=`roc_hit`
6275     if ! let "AFTER - BEFORE == CPAGES"; then
6276         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
6277     else
6278         log "cache hits:: before: $BEFORE, after: $AFTER"
6279     fi
6280
6281     rm -f $file
6282 }
6283 run_test 156 "Verification of ROC tunables ========================"
6284
6285 test_170() {
6286         $LCTL clear     # bug 18514
6287         $LCTL debug_daemon start $TMP/${tfile}_log_good
6288         touch $DIR/$tfile
6289         $LCTL debug_daemon stop
6290         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
6291                error "sed failed to read log_good"
6292
6293         $LCTL debug_daemon start $TMP/${tfile}_log_good
6294         rm -rf $DIR/$tfile
6295         $LCTL debug_daemon stop
6296
6297         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
6298                error "lctl df log_bad failed"
6299
6300         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
6301         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
6302
6303         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
6304         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
6305
6306         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
6307                 error "bad_line good_line1 good_line2 are empty"
6308
6309         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
6310         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
6311         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
6312
6313         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
6314         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
6315         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
6316
6317         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
6318                 error "bad_line_new good_line_new are empty"
6319
6320         local expected_good=$((good_line1 + good_line2*2))
6321
6322         rm -f $TMP/${tfile}*
6323         if [ $bad_line -ne $bad_line_new ]; then
6324                 error "expected $bad_line bad lines, but got $bad_line_new"
6325                 return 1
6326         fi
6327
6328         if [ $expected_good -ne $good_line_new ]; then
6329                 error "expected $expected_good good lines, but got $good_line_new"
6330                 return 2
6331         fi
6332         true
6333 }
6334 run_test 170 "test lctl df to handle corrupted log ====================="
6335
6336 test_171() { # bug20592
6337 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
6338         $LCTL set_param fail_loc=0x50e
6339         $LCTL set_param fail_val=3000
6340         multiop_bg_pause $DIR/$tfile Os || true
6341         # cause log dump
6342         sleep 3
6343         if dmesg | grep "recursive fault"; then
6344                 error "caught a recursive fault"
6345         fi
6346         $LCTL set_param fail_loc=0
6347         true
6348 }
6349 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
6350
6351 obdecho_create_test() {
6352         local OBD=$1
6353         local node=$2
6354         local rc=0
6355         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
6356         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" || rc=2; }
6357         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec create 1" || rc=3; }
6358         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 0 w 1" || rc=4; }
6359         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
6360         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec detach" || rc=6; }
6361         return $rc
6362 }
6363
6364 test_180() {
6365         local lustre_version=$(get_lustre_version mds)
6366         if [[ $lustre_version != 1.8* ]]; then
6367                skip bug22316 mds running $lustre_version
6368                return
6369         fi
6370         local rc=0
6371         local rmmod_local=0
6372         local rmmod_remote=0
6373
6374         lsmod | grep -q obdecho || { load_module obdecho/obdecho && rmmod_local=1; }
6375
6376         OBD=`$LCTL  dl | awk ' /-osc-/ { print $4; exit; }'`
6377         [ "x$OBD" != "x" ] && { obdecho_create_test $OBD client || rc=2; }
6378         [ $rmmod_local -eq 1 ] && rmmod obdecho
6379         [ $rc -ne 0 ] && return $rc
6380
6381         do_facet ost "lsmod | grep -q obdecho || { insmod ${LUSTRE}/obdecho/obdecho.ko || modprobe obdecho; }" && rmmod_remote=1
6382
6383         OBD=$(do_facet ost "$LCTL  dl | awk '/obdfilter/ { print; exit; }'" | awk '{print $4;}')
6384         [ "x$OBD" != "x" ] && { obdecho_create_test $OBD ost || rc=3; }
6385         [ $rmmod_remote -eq 1 ] && do_facet ost "rmmod obdecho"
6386         [ $rc -ne 0 ] && return $rc
6387
6388         true
6389 }
6390 run_test 180 "test obdecho ============================================"
6391
6392 test_181() { # bug 22177
6393         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
6394         # create enough files to index the directory
6395         createmany -o $DIR/$tdir/foobar 4000
6396         # print attributes for debug purpose
6397         lsattr -d .
6398         # open dir
6399         multiop_bg_pause $DIR/$tdir D_Sc || return 1
6400         MULTIPID=$!
6401         # remove the files & current working dir
6402         unlinkmany $DIR/$tdir/foobar 4000
6403         rmdir $DIR/$tdir
6404         kill -USR1 $MULTIPID
6405         wait $MULTIPID
6406         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
6407         return 0
6408 }
6409 run_test 181 "Test open-unlinked dir ========================"
6410
6411 POOL=${POOL:-cea1}
6412 TGT_COUNT=$OSTCOUNT
6413 TGTPOOL_FIRST=1
6414 TGTPOOL_MAX=$(($TGT_COUNT - 1))
6415 TGTPOOL_STEP=2
6416 TGTPOOL_LIST=`seq $TGTPOOL_FIRST $TGTPOOL_STEP $TGTPOOL_MAX`
6417 POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
6418 POOL_DIR_NAME=dir_tst
6419 POOL_DIR=$POOL_ROOT/$POOL_DIR_NAME
6420 POOL_FILE=$POOL_ROOT/file_tst
6421
6422 test_pools()
6423 {
6424         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep pools)" ] &&
6425                 skip "missing pools support on server" && return 1
6426         remote_mgs_nodsh && skip "remote MGS with nodsh" && return 1
6427         [ -z "$mdtlov" ] && mdtlov=$(get_mdtlov_proc_path $FSNAME)
6428         return 0
6429 }
6430
6431 check_file_in_pool()
6432 {
6433         file=$1
6434         res=$($GETSTRIPE $file | grep 0x | cut -f2)
6435         for i in $res
6436         do
6437                 found=$(echo :$TGTPOOL_LIST: | tr " " ":"  | grep :$i:)
6438                 if [[ "$found" == "" ]]
6439                 then
6440                         echo "pool list: $TGTPOOL_LIST"
6441                         echo "striping: $res"
6442                         error "$file not allocated in $POOL"
6443                         return 1
6444                 fi
6445         done
6446         return 0
6447 }
6448
6449 export mdtlov=
6450
6451 trap "cleanup_pools $FSNAME" EXIT
6452
6453 test_200a() {
6454         test_pools || return 0
6455
6456         create_pool $FSNAME.$POOL || return $?
6457         [ $($LFS pool_list $FSNAME | grep -c $POOL) -eq 1 ] ||
6458                 error "$POOL not in lfs pool_list"
6459 }
6460 run_test 200a "Create new pool =========================================="
6461
6462 test_200b() {
6463         test_pools || return 0
6464         TGT=$(for i in $TGTPOOL_LIST; do printf "$FSNAME-OST%04x_UUID " $i; done)
6465         do_facet mgs $LCTL pool_add $FSNAME.$POOL \
6466                 $FSNAME-OST[$TGTPOOL_FIRST-$TGTPOOL_MAX/$TGTPOOL_STEP]
6467
6468         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL | sort -u | tr '\n' ' ' " "$TGT" ||
6469                         error "Add to pool failed"
6470 }
6471 run_test 200b "Add targets to a pool ===================================="
6472
6473 test_200c() {
6474         test_pools || return 0
6475         mkdir -p $POOL_DIR
6476         $SETSTRIPE -c 2 -p $POOL $POOL_DIR
6477         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR"
6478         # b-19919 test relative path works well
6479         mkdir -p $POOL_DIR/$POOL_DIR_NAME
6480         cd $POOL_DIR
6481         $SETSTRIPE -c 2 -p $POOL $POOL_DIR_NAME
6482         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/$POOL_DIR_NAME"
6483         $SETSTRIPE -c 2 -p $POOL ./$POOL_DIR_NAME
6484         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/./$POOL_DIR_NAME"
6485         $SETSTRIPE -c 2 -p $POOL ../$POOL_DIR_NAME
6486         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/../$POOL_DIR_NAME"
6487         $SETSTRIPE -c 2 -p $POOL ../$POOL_DIR_NAME/$POOL_DIR_NAME
6488         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/../$POOL_DIR_NAME/$POOL_DIR_NAME"
6489          rm -rf $POOL_DIR_NAME; cd -
6490 }
6491 run_test 200c "Set pool on a directory ================================="
6492
6493 test_200d() {
6494         test_pools || return 0
6495         res=$($GETSTRIPE --pool $POOL_DIR | awk '/^pool:/ {print $2}')
6496         [ "$res" = $POOL ] || error "Pool on $POOL_DIR is $res, not $POOL"
6497 }
6498 run_test 200d "Check pool on a directory ==============================="
6499
6500 test_200e() {
6501         test_pools || return 0
6502         failed=0
6503         for i in $(seq -w 1 $(($TGT_COUNT * 3))); do
6504                 file=$POOL_DIR/file-$i
6505                 touch $file
6506                 check_file_in_pool $file
6507                 if [[ $? != 0 ]]; then
6508                         failed=$(($failed + 1))
6509                 fi
6510         done
6511         [ "$failed" = 0 ] || error "$failed files not allocated in $POOL"
6512 }
6513 run_test 200e "Check files allocation from directory pool =============="
6514
6515 test_200f() {
6516         test_pools || return 0
6517         mkdir -p $POOL_FILE
6518         failed=0
6519         for i in $(seq -w 1 $(($TGT_COUNT * 3))); do
6520                 file=$POOL_FILE/spoo-$i
6521                 $SETSTRIPE -p $POOL $file
6522                 check_file_in_pool $file
6523                 if [[ $? != 0 ]]; then
6524                         failed=$(($failed + 1))
6525                 fi
6526         done
6527         [ "$failed" = 0 ] || error "$failed files not allocated in $POOL"
6528 }
6529 run_test 200f "Create files in a pool ==================================="
6530
6531 test_200g() {
6532         test_pools || return 0
6533
6534         TGT=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$POOL | tr '\n' ' ')
6535         res=$($LFS df --pool $FSNAME.$POOL | awk '{print $1}' | grep "$FSNAME-OST" | tr '\n' ' ')
6536         [ "$res" = "$TGT" ] || error "Pools OSTs '$TGT' is not '$res' that lfs df reports"
6537 }
6538 run_test 200g "lfs df a pool ============================================"
6539
6540 test_201a() {
6541         test_pools || return 0
6542
6543         TGT=$($LCTL get_param -n lov.$FSNAME-*.pools.$POOL | head -1)
6544         do_facet mgs $LCTL pool_remove $FSNAME.$POOL $TGT
6545         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL | grep $TGT" "" ||
6546                 error "$TGT not removed from $FSNAME.$POOL"
6547 }
6548 run_test 201a "Remove a target from a pool ============================="
6549
6550 test_201b() {
6551         test_pools || return 0
6552
6553         for TGT in $($LCTL get_param -n lov.$FSNAME-*.pools.$POOL | sort -u)
6554         do
6555                 do_facet mgs $LCTL pool_remove $FSNAME.$POOL $TGT
6556         done
6557         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL" "" ||
6558                 error "Pool $FSNAME.$POOL cannot be drained"
6559         # striping on an empty/nonexistant pool should fall back to "pool of everything"
6560         touch ${POOL_DIR}/$tfile || error "failed to use fallback striping for empty pool"
6561         # setstripe on an empty pool should fail
6562         $SETSTRIPE -p $POOL ${POOL_FILE}/$tfile 2>/dev/null && \
6563                 error "expected failure when creating file with empty pool"
6564         return 0
6565 }
6566 run_test 201b "Remove all targets from a pool =========================="
6567
6568 test_201c() {
6569         test_pools || return 0
6570
6571         do_facet mgs $LCTL pool_destroy $FSNAME.$POOL
6572         
6573         sleep 2                        
6574     # striping on an empty/nonexistant pool should fall back to "pool of everything"
6575         touch ${POOL_DIR}/$tfile || error "failed to use fallback striping for missing pool"
6576         # setstripe on an empty pool should fail
6577         $SETSTRIPE -p $POOL ${POOL_FILE}/$tfile 2>/dev/null && \
6578                 error "expected failure when creating file with missing pool"
6579
6580         # get param should return err once pool is gone
6581         if wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL 2>/dev/null ||
6582                 echo foo" "foo"; then
6583                 remove_pool_from_list $FSNAME.$POOL
6584                 return 0
6585         fi
6586         error "Pool $FSNAME.$POOL is not destroyed"
6587 }
6588 run_test 201c "Remove a pool ============================================"
6589
6590 cleanup_pools $FSNAME
6591
6592 test_202() {
6593         if [ "$NETTYPE" = "o2ib" ]; then
6594                 skip "unaligned direct I/O is not supported by o2iblnd"
6595                 return
6596         fi
6597         $LFS setstripe -c 2 -s 1048576 $DIR/$tfile
6598         multiop $DIR/$tfile oO_WRONLY:O_APPEND:O_DIRECT:b1048548b130c || \
6599                 error "direct write failed"
6600         multiop $DIR/${tfile}2 oO_WRONLY:O_CREAT:w1048548w130c || \
6601                 error "cached write failed"
6602         cmp $DIR/$tfile $DIR/${tfile}2 || error "cmp failed"
6603         rm -f $DIR/${tfile}2
6604 }
6605 run_test 202 "O_APPEND+O_DIRECT multistripe write ========================"
6606
6607 #
6608 # tests that do cleanup/setup should be run at the end
6609 #
6610
6611 test_900() {
6612         local ls
6613         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
6614         $LCTL set_param fail_loc=0x903
6615         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
6616         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
6617                 echo "clear" > $ls
6618         done
6619         FAIL_ON_ERROR=true cleanup
6620         FAIL_ON_ERROR=true setup
6621 }
6622 run_test 900 "umount should not race with any mgc requeue thread"
6623
6624 log "cleanup: ======================================================"
6625 check_and_cleanup_lustre
6626 if [ "$I_MOUNTED" != "yes" ]; then
6627         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
6628 fi
6629
6630 echo '=========================== finished ==============================='
6631 [ -f "$SANITYLOG" ] && cat $SANITYLOG && grep -q FAIL $SANITYLOG && exit 1 || true
6632 echo "$0: completed"