Whamcloud - gitweb
LU-1445 osp: Use FID to track precreate cache.
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 4; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                27u   42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19
20 # Tests that fail on uml
21 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
22 #                                    buffer i/o errs             sock spc runas
23 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
24
25 SRCDIR=$(cd $(dirname $0); echo $PWD)
26 export PATH=$PATH:/sbin
27
28 TMP=${TMP:-/tmp}
29
30 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
31 CREATETEST=${CREATETEST:-createtest}
32 LFS=${LFS:-lfs}
33 LFIND=${LFIND:-"$LFS find"}
34 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
35 LCTL=${LCTL:-lctl}
36 MCREATE=${MCREATE:-mcreate}
37 OPENFILE=${OPENFILE:-openfile}
38 OPENUNLINK=${OPENUNLINK:-openunlink}
39 export MULTIOP=${MULTIOP:-multiop}
40 READS=${READS:-"reads"}
41 MUNLINK=${MUNLINK:-munlink}
42 SOCKETSERVER=${SOCKETSERVER:-socketserver}
43 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
44 MEMHOG=${MEMHOG:-memhog}
45 DIRECTIO=${DIRECTIO:-directio}
46 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
47 UMOUNT=${UMOUNT:-"umount -d"}
48 STRIPES_PER_OBJ=-1
49 CHECK_GRANT=${CHECK_GRANT:-"yes"}
50 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
51 export PARALLEL=${PARALLEL:-"no"}
52
53 export NAME=${NAME:-local}
54
55 SAVE_PWD=$PWD
56
57 CLEANUP=${CLEANUP:-:}
58 SETUP=${SETUP:-:}
59 TRACE=${TRACE:-""}
60 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
61 . $LUSTRE/tests/test-framework.sh
62 init_test_env $@
63 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
64 init_logging
65
66 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
67
68 FAIL_ON_ERROR=false
69
70 cleanup() {
71         echo -n "cln.."
72         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
73         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
74 }
75 setup() {
76         echo -n "mnt.."
77         load_modules
78         setupall || exit 10
79         echo "done"
80 }
81
82 check_kernel_version() {
83         WANT_VER=$1
84         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
85         case $GOT_VER in
86         patchless|patchless_client) return 0;;
87         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
88         esac
89         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
90         return 1
91 }
92
93 if [ "$ONLY" == "cleanup" ]; then
94        sh llmountcleanup.sh
95        exit 0
96 fi
97
98 check_and_setup_lustre
99
100 DIR=${DIR:-$MOUNT}
101 assert_DIR
102
103 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
104     awk '{gsub(/_UUID/,""); print $1}' | head -1)
105 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
106 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
107 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
108 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
109 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
110 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
111
112 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
113 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
114 rm -rf $DIR/[Rdfs][0-9]*
115
116 # $RUNAS_ID may get set incorrectly somewhere else
117 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
118
119 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
120
121 build_test_filter
122
123 if [ "${ONLY}" = "MOUNT" ] ; then
124         echo "Lustre is up, please go on"
125         exit
126 fi
127
128 echo "preparing for tests involving mounts"
129 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
130 touch $EXT2_DEV
131 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
132 echo # add a newline after mke2fs.
133
134 umask 077
135
136 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
137 lctl set_param debug=-1 2> /dev/null || true
138 test_0() {
139         touch $DIR/$tfile
140         $CHECKSTAT -t file $DIR/$tfile || error
141         rm $DIR/$tfile
142         $CHECKSTAT -a $DIR/$tfile || error
143 }
144 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
145
146 test_0b() {
147         chmod 0755 $DIR || error
148         $CHECKSTAT -p 0755 $DIR || error
149 }
150 run_test 0b "chmod 0755 $DIR ============================="
151
152 test_0c() {
153     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
154     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
155 }
156 run_test 0c "check import proc ============================="
157
158 test_1a() {
159         test_mkdir -p $DIR/$tdir
160         test_mkdir -p $DIR/$tdir/d2
161         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
162         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
163 }
164 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
165
166 test_1b() {
167         rmdir $DIR/$tdir/d2
168         rmdir $DIR/$tdir
169         $CHECKSTAT -a $DIR/$tdir || error
170 }
171 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
172
173 test_2a() {
174         test_mkdir $DIR/$tdir
175         touch $DIR/$tdir/$tfile
176         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
177 }
178 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
179
180 test_2b() {
181         rm -r $DIR/$tdir
182         $CHECKSTAT -a $DIR/$tdir || error
183 }
184 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
185
186 test_3a() {
187         test_mkdir -p $DIR/$tdir
188         $CHECKSTAT -t dir $DIR/$tdir || error
189 }
190 run_test 3a "mkdir .../d3 ======================================"
191
192 test_3b() {
193         if [ ! -d $DIR/$tdir ]; then
194                 mkdir $DIR/$tdir
195         fi
196         touch $DIR/$tdir/$tfile
197         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
198 }
199 run_test 3b "touch .../d3/f ===================================="
200
201 test_3c() {
202         rm -r $DIR/$tdir
203         $CHECKSTAT -a $DIR/$tdir || error
204 }
205 run_test 3c "rm -r .../d3 ======================================"
206
207 test_4a() {
208         test_mkdir -p $DIR/$tdir
209         $CHECKSTAT -t dir $DIR/$tdir || error
210 }
211 run_test 4a "mkdir .../d4 ======================================"
212
213 test_4b() {
214         if [ ! -d $DIR/$tdir ]; then
215                 test_mkdir $DIR/$tdir
216         fi
217         test_mkdir $DIR/$tdir/d2
218         mkdir $DIR/$tdir/d2
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
220 }
221 run_test 4b "mkdir .../d4/d2 ==================================="
222
223 test_5() {
224         test_mkdir $DIR/$tdir
225         test_mkdir $DIR/$tdir/d2
226         chmod 0707 $DIR/$tdir/d2
227         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error
228 }
229 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
230
231 test_6a() {
232         touch $DIR/$tfile
233         chmod 0666 $DIR/$tfile || error
234         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || 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" && return
240         if [ ! -f $DIR/$tfile ]; then
241                 touch $DIR/$tfile
242                 chmod 0666 $DIR/$tfile
243         fi
244         $RUNAS chmod 0444 $DIR/$tfile && error
245         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || 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" && return
251         touch $DIR/$tfile
252         chown $RUNAS_ID $DIR/$tfile || error
253         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || 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" && return
259         if [ ! -f $DIR/$tfile ]; then
260                 touch $DIR/$tfile
261                 chown $RUNAS_ID $DIR/$tfile
262         fi
263         $RUNAS chown $UID $DIR/$tfile && error
264         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || 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" && return
270         touch $DIR/$tfile
271         chgrp $RUNAS_ID $DIR/$tfile || error
272         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || 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" && return
278         if [ ! -f $DIR/$tfile ]; then
279                 touch $DIR/$tfile
280                 chgrp $RUNAS_ID $DIR/$tfile
281         fi
282         $RUNAS chgrp $UID $DIR/$tfile && error
283         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || 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" && return
289         test_mkdir $DIR/$tdir || error
290         chmod 777 $DIR/$tdir || error
291         $RUNAS mkdir $DIR/$tdir/d || error
292         chmod g+s $DIR/$tdir/d || error
293         test_mkdir $DIR/$tdir/d/subdir
294         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/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" && return
300         touch $DIR/$tfile || error "touch failed"
301         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
302         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
303                 error "chown worked"
304         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile || error
305 }
306 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
307
308 test_7a() {
309         test_mkdir $DIR/$tdir
310         $MCREATE $DIR/$tdir/$tfile
311         chmod 0666 $DIR/$tdir/$tfile
312         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
313 }
314 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
315
316 test_7b() {
317         if [ ! -d $DIR/$tdir ]; then
318                 mkdir $DIR/$tdir
319         fi
320         $MCREATE $DIR/$tdir/$tfile
321         echo -n foo > $DIR/$tdir/$tfile
322         [ "`cat $DIR/$tdir/$tfile`" = "foo" ] || error
323         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error
324 }
325 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
326
327 test_8() {
328         test_mkdir $DIR/$tdir
329         touch $DIR/$tdir/$tfile
330         chmod 0666 $DIR/$tdir/$tfile
331         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
332 }
333 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
334
335 test_9() {
336         test_mkdir $DIR/$tdir
337         test_mkdir $DIR/$tdir/d2
338         test_mkdir $DIR/$tdir/d2/d3
339         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error
340 }
341 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
342
343 test_10() {
344         test_mkdir $DIR/$tdir
345         test_mkdir $DIR/$tdir/d2
346         touch $DIR/$tdir/d2/$tfile
347         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile || error
348 }
349 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
350
351 test_11() {
352         test_mkdir $DIR/$tdir
353         test_mkdir $DIR/$tdir/d2
354         chmod 0666 $DIR/$tdir/d2
355         chmod 0705 $DIR/$tdir/d2
356         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 || error
357 }
358 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
359
360 test_12() {
361         test_mkdir $DIR/$tdir
362         touch $DIR/$tdir/$tfile
363         chmod 0666 $DIR/$tdir/$tfile
364         chmod 0654 $DIR/$tdir/$tfile
365         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile || error
366 }
367 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
368
369 test_13() {
370         test_mkdir $DIR/$tdir
371         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
372         >  $DIR/$tdir/$tfile
373         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile || error
374 }
375 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
376
377 test_14() {
378         test_mkdir $DIR/$tdir
379         touch $DIR/$tdir/$tfile
380         rm $DIR/$tdir/$tfile
381         $CHECKSTAT -a $DIR/$tdir/$tfile || error
382 }
383 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
384
385 test_15() {
386         test_mkdir $DIR/$tdir
387         touch $DIR/$tdir/$tfile
388         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
389         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 || error
390 }
391 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
392
393 test_16() {
394         test_mkdir $DIR/$tdir
395         touch $DIR/$tdir/$tfile
396         rm -rf $DIR/$tdir/$tfile
397         $CHECKSTAT -a $DIR/$tdir/$tfile || error
398 }
399 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
400
401 test_17a() {
402         test_mkdir -p $DIR/$tdir
403         touch $DIR/$tdir/$tfile
404         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
405         ls -l $DIR/$tdir
406         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist || error
407         $CHECKSTAT -f -t f $DIR/$tdir/l-exist || error
408         rm -f $DIR/$tdir/l-exist
409         $CHECKSTAT -a $DIR/$tdir/l-exist || error
410 }
411 run_test 17a "symlinks: create, remove (real) =================="
412
413 test_17b() {
414         test_mkdir -p $DIR/$tdir
415         ln -s no-such-file $DIR/$tdir/l-dangle
416         ls -l $DIR/$tdir
417         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle || error
418         $CHECKSTAT -fa $DIR/$tdir/l-dangle || error
419         rm -f $DIR/$tdir/l-dangle
420         $CHECKSTAT -a $DIR/$tdir/l-dangle || error
421 }
422 run_test 17b "symlinks: create, remove (dangling) =============="
423
424 test_17c() { # bug 3440 - don't save failed open RPC for replay
425         test_mkdir -p $DIR/$tdir
426         ln -s foo $DIR/$tdir/$tfile
427         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
428 }
429 run_test 17c "symlinks: open dangling (should return error) ===="
430
431 test_17d() {
432         test_mkdir -p $DIR/$tdir
433         ln -s foo $DIR/$tdir/$tfile
434         touch $DIR/$tdir/$tfile || error "creating to new symlink"
435 }
436 run_test 17d "symlinks: create dangling ========================"
437
438 test_17e() {
439         test_mkdir -p $DIR/$tdir
440         local foo=$DIR/$tdir/$tfile
441         ln -s $foo $foo || error "create symlink failed"
442         ls -l $foo || error "ls -l failed"
443         ls $foo && error "ls not failed" || true
444 }
445 run_test 17e "symlinks: create recursive symlink (should return error) ===="
446
447 test_17f() {
448         test_mkdir -p $DIR/d17f
449         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/d17f/666
455         ls -l  $DIR/d17f
456 }
457 run_test 17f "symlinks: long and very long symlink name ========================"
458
459 # str_repeat(S, N) generate a string that is string S repeated N times
460 str_repeat() {
461         local s=$1
462         local n=$2
463         local ret=''
464         while [ $((n -= 1)) -ge 0 ]; do
465                 ret=$ret$s
466         done
467         echo $ret
468 }
469
470 # Long symlinks and LU-2241
471 test_17g() {
472         test_mkdir -p $DIR/$tdir
473         local TESTS="59 60 61 4094 4095"
474
475         for i in $TESTS; do
476                 local SYMNAME=$(str_repeat 'x' $i)
477                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
478                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
479         done
480 }
481 run_test 17g "symlinks: really long symlink name and inode boundaries"
482
483 test_17h() { #bug 17378
484         remote_mds_nodsh && skip "remote MDS with nodsh" && return
485         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
486         local mdt_idx
487         test_mkdir -p $DIR/$tdir
488         if [ $MDSCOUNT -gt 1 ]; then
489                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
490         else
491                 mdt_idx=0
492         fi
493         $SETSTRIPE -c -1 $DIR/$tdir
494 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
495         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
496         touch $DIR/$tdir/$tfile || true
497 }
498 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
499
500 test_17i() { #bug 20018
501         remote_mds_nodsh && skip "remote MDS with nodsh" && return
502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
503         test_mkdir -p $DIR/$tdir
504         local foo=$DIR/$tdir/$tfile
505         local mdt_idx
506         if [ $MDSCOUNT -gt 1 ]; then
507                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
508         else
509                 mdt_idx=0
510         fi
511         ln -s $foo $foo || error "create symlink failed"
512 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
513         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
514         ls -l $foo && error "error not detected"
515         return 0
516 }
517 run_test 17i "don't panic on short symlink"
518
519 test_17k() { #bug 22301
520         rsync --help | grep -q xattr ||
521                 skip_env "$(rsync --version| head -1) does not support xattrs"
522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
523         test_mkdir -p $DIR/$tdir
524         test_mkdir -p $DIR/$tdir.new
525         touch $DIR/$tdir/$tfile
526         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
527         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
528                 error "rsync failed with xattrs enabled"
529 }
530 run_test 17k "symlinks: rsync with xattrs enabled ========================="
531
532 test_17l() { # LU-279
533         mkdir -p $DIR/$tdir
534         touch $DIR/$tdir/$tfile
535         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
536         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
537                 # -h to not follow symlinks. -m '' to list all the xattrs.
538                 # grep to remove first line: '# file: $path'.
539                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
540                 do
541                         lgetxattr_size_check $path $xattr ||
542                                 error "lgetxattr_size_check $path $xattr failed"
543                 done
544         done
545 }
546 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
547
548 # LU-1540
549 test_17m() {
550         local short_sym="0123456789"
551         local WDIR=$DIR/${tdir}m
552         local mds_index
553         local devname
554         local cmd
555         local i
556         local rc=0
557
558         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
559         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
560                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
561
562         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
563                 skip "only for ldiskfs MDT" && return 0
564
565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
566
567         mkdir -p $WDIR
568         long_sym=$short_sym
569         # create a long symlink file
570         for ((i = 0; i < 4; ++i)); do
571                 long_sym=${long_sym}${long_sym}
572         done
573
574         echo "create 512 short and long symlink files under $WDIR"
575         for ((i = 0; i < 256; ++i)); do
576                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
577                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
578         done
579
580         echo "erase them"
581         rm -f $WDIR/*
582         sync
583         wait_delete_completed
584
585         echo "recreate the 512 symlink files with a shorter string"
586         for ((i = 0; i < 512; ++i)); do
587                 # rewrite the symlink file with a shorter string
588                 ln -sf ${long_sym} $WDIR/long-$i
589                 ln -sf ${short_sym} $WDIR/short-$i
590         done
591
592         mds_index=$($LFS getstripe -M $WDIR)
593         mds_index=$((mds_index+1))
594         devname=$(mdsdevname $mds_index)
595         cmd="$E2FSCK -fnvd $devname"
596
597         echo "stop and checking mds${mds_index}: $cmd"
598         # e2fsck should not return error
599         stop mds${mds_index} -f
600         do_facet mds${mds_index} $cmd || rc=$?
601
602         start mds${mds_index} $devname $MDS_MOUNT_OPTS
603         df $MOUNT > /dev/null 2>&1
604         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
605                 "short/long symlink MDT: rc=$rc"
606         return $rc
607 }
608 run_test 17m "run e2fsck against MDT which contains short/long symlink"
609
610 check_fs_consistency_17n() {
611         local mdt_index
612         local devname
613         local cmd
614         local rc=0
615
616         for mdt_index in $(seq 1 $MDSCOUNT); do
617                 devname=$(mdsdevname $mdt_index)
618                 cmd="$E2FSCK -fnvd $devname"
619
620                 echo "stop and checking mds${mdt_index}: $cmd"
621                 # e2fsck should not return error
622                 stop mds${mdt_index}
623                 do_facet mds${mdt_index} $cmd || rc=$?
624
625                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
626                 df $MOUNT > /dev/null 2>&1
627                 [ $rc -ne 0 ] && break
628         done
629         return $rc
630 }
631
632 test_17n() {
633         local i
634
635         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
636         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
637                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
638
639         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
640                 skip "only for ldiskfs MDT" && return 0
641
642         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
643
644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
645
646         mkdir -p $DIR/$tdir
647         for ((i=0; i<10; i++)); do
648                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
649                         error "create remote dir error $i"
650                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
651                         error "create files under remote dir failed $i"
652         done
653
654         check_fs_consistency_17n || error "e2fsck report error"
655
656         for ((i=0;i<10;i++)); do
657                 rm -rf $DIR/$tdir/remote_dir_${i} ||
658                         error "destroy remote dir error $i"
659         done
660
661         check_fs_consistency_17n || error "e2fsck report error"
662 }
663 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
664
665 test_18() {
666         touch $DIR/f
667         ls $DIR || error
668 }
669 run_test 18 "touch .../f ; ls ... =============================="
670
671 test_19a() {
672         touch $DIR/f19
673         ls -l $DIR
674         rm $DIR/f19
675         $CHECKSTAT -a $DIR/f19 || error
676 }
677 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
678
679 test_19b() {
680         ls -l $DIR/f19 && error || true
681 }
682 run_test 19b "ls -l .../f19 (should return error) =============="
683
684 test_19c() {
685         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
686         $RUNAS touch $DIR/f19 && error || true
687 }
688 run_test 19c "$RUNAS touch .../f19 (should return error) =="
689
690 test_19d() {
691         cat $DIR/f19 && error || true
692 }
693 run_test 19d "cat .../f19 (should return error) =============="
694
695 test_20() {
696         touch $DIR/f
697         rm $DIR/f
698         log "1 done"
699         touch $DIR/f
700         rm $DIR/f
701         log "2 done"
702         touch $DIR/f
703         rm $DIR/f
704         log "3 done"
705         $CHECKSTAT -a $DIR/f || error
706 }
707 run_test 20 "touch .../f ; ls -l ... ==========================="
708
709 test_21() {
710         test_mkdir $DIR/d21
711         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
712         ln -s dangle $DIR/d21/link
713         echo foo >> $DIR/d21/link
714         cat $DIR/d21/dangle
715         $CHECKSTAT -t link $DIR/d21/link || error
716         $CHECKSTAT -f -t file $DIR/d21/link || error
717 }
718 run_test 21 "write to dangling link ============================"
719
720 test_22() {
721         WDIR=$DIR/$tdir
722         test_mkdir -p $DIR/$tdir
723         chown $RUNAS_ID:$RUNAS_GID $WDIR
724         (cd $WDIR || error "cd $WDIR failed";
725         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
726         $RUNAS tar xf -)
727         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
728         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
729         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
730 }
731 run_test 22 "unpack tar archive as non-root user ==============="
732
733 # was test_23
734 test_23a() {
735         test_mkdir -p $DIR/$tdir
736         local file=$DIR/$tdir/$tfile
737
738         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
739         openfile -f O_CREAT:O_EXCL $file &&
740                 error "$file recreate succeeded" || true
741 }
742 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
743
744 test_23b() { # bug 18988
745         test_mkdir -p $DIR/$tdir
746         local file=$DIR/$tdir/$tfile
747
748         rm -f $file
749         echo foo > $file || error "write filed"
750         echo bar >> $file || error "append filed"
751         $CHECKSTAT -s 8 $file || error "wrong size"
752         rm $file
753 }
754 run_test 23b "O_APPEND check =========================="
755
756 test_24a() {
757         echo '== rename sanity =============================================='
758         echo '-- same directory rename'
759         test_mkdir $DIR/R1
760         touch $DIR/R1/f
761         mv $DIR/R1/f $DIR/R1/g
762         $CHECKSTAT -t file $DIR/R1/g || error
763 }
764 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
765
766 test_24b() {
767         test_mkdir $DIR/R2
768         touch $DIR/R2/{f,g}
769         mv $DIR/R2/f $DIR/R2/g
770         $CHECKSTAT -a $DIR/R2/f || error
771         $CHECKSTAT -t file $DIR/R2/g || error
772 }
773 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
774
775 test_24c() {
776         test_mkdir $DIR/R3
777         test_mkdir $DIR/R3/f
778         mv $DIR/R3/f $DIR/R3/g
779         $CHECKSTAT -a $DIR/R3/f || error
780         $CHECKSTAT -t dir $DIR/R3/g || error
781 }
782 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
783
784 test_24d() {
785         test_mkdir $DIR/R4
786         test_mkdir $DIR/R4/f
787         test_mkdir $DIR/R4/g
788         mrename $DIR/R4/f $DIR/R4/g
789         $CHECKSTAT -a $DIR/R4/f || error
790         $CHECKSTAT -t dir $DIR/R4/g || error
791 }
792 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
793
794 test_24e() {
795         echo '-- cross directory renames --'
796         test_mkdir $DIR/R5a
797         test_mkdir $DIR/R5b
798         touch $DIR/R5a/f
799         mv $DIR/R5a/f $DIR/R5b/g
800         $CHECKSTAT -a $DIR/R5a/f || error
801         $CHECKSTAT -t file $DIR/R5b/g || error
802 }
803 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
804
805 test_24f() {
806         test_mkdir $DIR/R6a
807         test_mkdir $DIR/R6b
808         touch $DIR/R6a/f $DIR/R6b/g
809         mv $DIR/R6a/f $DIR/R6b/g
810         $CHECKSTAT -a $DIR/R6a/f || error
811         $CHECKSTAT -t file $DIR/R6b/g || error
812 }
813 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
814
815 test_24g() {
816         test_mkdir $DIR/R7a
817         test_mkdir $DIR/R7b
818         test_mkdir $DIR/R7a/d
819         mv $DIR/R7a/d $DIR/R7b/e
820         $CHECKSTAT -a $DIR/R7a/d || error
821         $CHECKSTAT -t dir $DIR/R7b/e || error
822 }
823 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
824
825 test_24h() {
826         test_mkdir $DIR/R8a
827         test_mkdir $DIR/R8b
828         test_mkdir $DIR/R8a/d
829         test_mkdir $DIR/R8b/e
830         mrename $DIR/R8a/d $DIR/R8b/e
831         $CHECKSTAT -a $DIR/R8a/d || error
832         $CHECKSTAT -t dir $DIR/R8b/e || error
833 }
834 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
835
836 test_24i() {
837         echo "-- rename error cases"
838         test_mkdir $DIR/R9
839         test_mkdir $DIR/R9/a
840         touch $DIR/R9/f
841         mrename $DIR/R9/f $DIR/R9/a
842         $CHECKSTAT -t file $DIR/R9/f || error
843         $CHECKSTAT -t dir  $DIR/R9/a || error
844         $CHECKSTAT -a $DIR/R9/a/f || error
845 }
846 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
847
848 test_24j() {
849         test_mkdir $DIR/R10
850         mrename $DIR/R10/f $DIR/R10/g
851         $CHECKSTAT -t dir $DIR/R10 || error
852         $CHECKSTAT -a $DIR/R10/f || error
853         $CHECKSTAT -a $DIR/R10/g || error
854 }
855 run_test 24j "source does not exist ============================"
856
857 test_24k() {
858         test_mkdir $DIR/R11a
859         test_mkdir $DIR/R11a/d
860         touch $DIR/R11a/f
861         mv $DIR/R11a/f $DIR/R11a/d
862         $CHECKSTAT -a $DIR/R11a/f || error
863         $CHECKSTAT -t file $DIR/R11a/d/f || error
864 }
865 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
866
867 # bug 2429 - rename foo foo foo creates invalid file
868 test_24l() {
869         f="$DIR/f24l"
870         $MULTIOP $f OcNs || error
871 }
872 run_test 24l "Renaming a file to itself ========================"
873
874 test_24m() {
875         f="$DIR/f24m"
876         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
877         # on ext3 this does not remove either the source or target files
878         # though the "expected" operation would be to remove the source
879         $CHECKSTAT -t file ${f} || error "${f} missing"
880         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
881 }
882 run_test 24m "Renaming a file to a hard link to itself ========="
883
884 test_24n() {
885     f="$DIR/f24n"
886     # this stats the old file after it was renamed, so it should fail
887     touch ${f}
888     $CHECKSTAT ${f}
889     mv ${f} ${f}.rename
890     $CHECKSTAT ${f}.rename
891     $CHECKSTAT -a ${f}
892 }
893 run_test 24n "Statting the old file after renaming (Posix rename 2)"
894
895 test_24o() {
896         check_kernel_version 37 || return 0
897         test_mkdir -p $DIR/d24o
898         rename_many -s random -v -n 10 $DIR/d24o
899 }
900 run_test 24o "rename of files during htree split ==============="
901
902 test_24p() {
903         test_mkdir $DIR/R12a
904         test_mkdir $DIR/R12b
905         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
906         mrename $DIR/R12a $DIR/R12b
907         $CHECKSTAT -a $DIR/R12a || error
908         $CHECKSTAT -t dir $DIR/R12b || error
909         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
910         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
911 }
912 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
913
914 test_24q() {
915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
916         test_mkdir $DIR/R13a
917         test_mkdir $DIR/R13b
918         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
919         multiop_bg_pause $DIR/R13b D_c || return 1
920         MULTIPID=$!
921
922         mrename $DIR/R13a $DIR/R13b
923         $CHECKSTAT -a $DIR/R13a || error
924         $CHECKSTAT -t dir $DIR/R13b || error
925         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
926         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
927         kill -USR1 $MULTIPID
928         wait $MULTIPID || error "multiop close failed"
929 }
930 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
931
932 test_24r() { #bug 3789
933         test_mkdir $DIR/R14a
934         test_mkdir $DIR/R14a/b
935         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
936         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
937         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
938 }
939 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
940
941 test_24s() {
942         test_mkdir $DIR/R15a
943         test_mkdir $DIR/R15a/b
944         test_mkdir $DIR/R15a/b/c
945         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
946         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
947         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
948 }
949 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
950 test_24t() {
951         test_mkdir $DIR/R16a
952         test_mkdir $DIR/R16a/b
953         test_mkdir $DIR/R16a/b/c
954         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
955         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
956         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
957 }
958 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
959
960 test_24u() { # bug12192
961         rm -rf $DIR/$tfile
962         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
963         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
964 }
965 run_test 24u "create stripe file"
966
967 page_size() {
968         getconf PAGE_SIZE
969 }
970
971 simple_cleanup_common() {
972         trap 0
973         rm -rf $DIR/$tdir
974         wait_delete_completed
975 }
976
977 max_pages_per_rpc() {
978         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
979 }
980
981 test_24v() {
982         local NRFILES=100000
983         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
984         [ $FREE_INODES -lt $NRFILES ] && \
985                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
986                 return
987
988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
989         trap simple_cleanup_common EXIT
990
991         mkdir -p $DIR/$tdir
992         createmany -m $DIR/$tdir/$tfile $NRFILES
993
994         cancel_lru_locks mdc
995         lctl set_param mdc.*.stats clear
996
997         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
998
999         # LU-5 large readdir
1000         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1001         #               8 bytes for name(filename is mostly 5 in this test) +
1002         #               8 bytes for luda_type
1003         # take into account of overhead in lu_dirpage header and end mark in
1004         # each page, plus one in RPC_NUM calculation.
1005         DIRENT_SIZE=48
1006         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1007         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1008         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1009                                 awk '/^mds_readpage/ {print $2}')
1010         [ $mds_readpage -gt $RPC_NUM ] && \
1011                 error "large readdir doesn't take effect"
1012
1013         simple_cleanup_common
1014 }
1015 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1016
1017 test_24w() { # bug21506
1018         SZ1=234852
1019         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1020         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1021         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1022         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1023         [ "$SZ1" = "$SZ2" ] || \
1024                 error "Error reading at the end of the file $tfile"
1025 }
1026 run_test 24w "Reading a file larger than 4Gb"
1027
1028 test_24x() {
1029         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1031         local MDTIDX=1
1032         local remote_dir=$DIR/$tdir/remote_dir
1033
1034         mkdir -p $DIR/$tdir
1035         $LFS mkdir -i $MDTIDX $remote_dir ||
1036                 error "create remote directory failed"
1037
1038         mkdir -p $DIR/$tdir/src_dir
1039         touch $DIR/$tdir/src_file
1040         mkdir -p $remote_dir/tgt_dir
1041         touch $remote_dir/tgt_file
1042
1043         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1044                 error "rename dir cross MDT works!"
1045
1046         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1047                 error "rename file cross MDT works!"
1048
1049         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1050                 error "ln file cross MDT should not work!"
1051
1052         rm -rf $DIR/$tdir || error "Can not delete directories"
1053 }
1054 run_test 24x "cross rename/link should be failed"
1055
1056 test_24y() {
1057         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1059         local MDTIDX=1
1060         local remote_dir=$DIR/$tdir/remote_dir
1061
1062         mkdir -p $DIR/$tdir
1063         $LFS mkdir -i $MDTIDX $remote_dir ||
1064                    error "create remote directory failed"
1065
1066         mkdir -p $remote_dir/src_dir
1067         touch $remote_dir/src_file
1068         mkdir -p $remote_dir/tgt_dir
1069         touch $remote_dir/tgt_file
1070
1071         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1072                 error "rename subdir in the same remote dir failed!"
1073
1074         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1075                 error "rename files in the same remote dir failed!"
1076
1077         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1078                 error "link files in the same remote dir failed!"
1079
1080         rm -rf $DIR/$tdir || error "Can not delete directories"
1081 }
1082 run_test 24y "rename/link on the same dir should succeed"
1083
1084 test_24z() {
1085         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1086         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1087         local MDTIDX=1
1088         local remote_src=$DIR/$tdir/remote_dir
1089         local remote_tgt=$DIR/$tdir/remote_tgt
1090
1091         mkdir -p $DIR/$tdir
1092         $LFS mkdir -i $MDTIDX $remote_src ||
1093                    error "create remote directory failed"
1094
1095         $LFS mkdir -i $MDTIDX $remote_tgt ||
1096                    error "create remote directory failed"
1097
1098         mrename $remote_src $remote_tgt &&
1099                 error "rename remote dirs should not work!"
1100
1101         rm -rf $DIR/$tdir || error "Can not delete directories"
1102 }
1103 run_test 24z "rename one remote dir to another remote dir should fail"
1104
1105 test_25a() {
1106         echo '== symlink sanity ============================================='
1107
1108         test_mkdir $DIR/d25
1109         ln -s d25 $DIR/s25
1110         touch $DIR/s25/foo || error
1111 }
1112 run_test 25a "create file in symlinked directory ==============="
1113
1114 test_25b() {
1115         [ ! -d $DIR/d25 ] && test_25a
1116         $CHECKSTAT -t file $DIR/s25/foo || error
1117 }
1118 run_test 25b "lookup file in symlinked directory ==============="
1119
1120 test_26a() {
1121         test_mkdir $DIR/d26
1122         test_mkdir $DIR/d26/d26-2
1123         ln -s d26/d26-2 $DIR/s26
1124         touch $DIR/s26/foo || error
1125 }
1126 run_test 26a "multiple component symlink ======================="
1127
1128 test_26b() {
1129         test_mkdir -p $DIR/d26b/d26-2
1130         ln -s d26b/d26-2/foo $DIR/s26-2
1131         touch $DIR/s26-2 || error
1132 }
1133 run_test 26b "multiple component symlink at end of lookup ======"
1134
1135 test_26c() {
1136         test_mkdir $DIR/d26.2
1137         touch $DIR/d26.2/foo
1138         ln -s d26.2 $DIR/s26.2-1
1139         ln -s s26.2-1 $DIR/s26.2-2
1140         ln -s s26.2-2 $DIR/s26.2-3
1141         chmod 0666 $DIR/s26.2-3/foo
1142 }
1143 run_test 26c "chain of symlinks ================================"
1144
1145 # recursive symlinks (bug 439)
1146 test_26d() {
1147         ln -s d26-3/foo $DIR/d26-3
1148 }
1149 run_test 26d "create multiple component recursive symlink ======"
1150
1151 test_26e() {
1152         [ ! -h $DIR/d26-3 ] && test_26d
1153         rm $DIR/d26-3
1154 }
1155 run_test 26e "unlink multiple component recursive symlink ======"
1156
1157 # recursive symlinks (bug 7022)
1158 test_26f() {
1159         test_mkdir -p $DIR/$tdir
1160         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1161         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1162         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1163         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1164         cd $tfile                || error "cd $tfile failed"
1165         ln -s .. dotdot          || error "ln dotdot failed"
1166         ln -s dotdot/lndir lndir || error "ln lndir failed"
1167         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1168         output=`ls $tfile/$tfile/lndir/bar1`
1169         [ "$output" = bar1 ] && error "unexpected output"
1170         rm -r $tfile             || error "rm $tfile failed"
1171         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1172 }
1173 run_test 26f "rm -r of a directory which has recursive symlink ="
1174
1175 test_27a() {
1176         echo '== stripe sanity =============================================='
1177         test_mkdir -p $DIR/d27 || error "mkdir failed"
1178         $GETSTRIPE $DIR/d27
1179         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1180         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1181         pass
1182         log "== test_27a: write to one stripe file ========================="
1183         cp /etc/hosts $DIR/d27/f0 || error
1184 }
1185 run_test 27a "one stripe file =================================="
1186
1187 test_27b() {
1188         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1189         test_mkdir -p $DIR/d27
1190         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1191         $GETSTRIPE -c $DIR/d27/f01
1192         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1193                 error "two-stripe file doesn't have two stripes"
1194 }
1195 run_test 27b "create two stripe file"
1196
1197 test_27c() {
1198         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1199
1200         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1201 }
1202 run_test 27c "write to two stripe file"
1203
1204 test_27d() {
1205         test_mkdir -p $DIR/d27
1206         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1207         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1208         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1209 }
1210 run_test 27d "create file with default settings ================"
1211
1212 test_27e() {
1213         test_mkdir -p $DIR/d27
1214         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1215         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1216         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1217 }
1218 run_test 27e "setstripe existing file (should return error) ======"
1219
1220 test_27f() {
1221         test_mkdir -p $DIR/d27
1222         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1223         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1224         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1225 }
1226 run_test 27f "setstripe with bad stripe size (should return error)"
1227
1228 test_27g() {
1229         test_mkdir -p $DIR/d27
1230         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1231         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1232                 error "$DIR/d27/fnone has object"
1233 }
1234 run_test 27g "$GETSTRIPE with no objects"
1235
1236 test_27i() {
1237         touch $DIR/d27/fsome || error "touch failed"
1238         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1239 }
1240 run_test 27i "$GETSTRIPE with some objects"
1241
1242 test_27j() {
1243         test_mkdir -p $DIR/d27
1244         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1245 }
1246 run_test 27j "setstripe with bad stripe offset (should return error)"
1247
1248 test_27k() { # bug 2844
1249         test_mkdir -p $DIR/d27
1250         FILE=$DIR/d27/f27k
1251         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1252         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1253         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1254         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1255         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1256         dd if=/dev/zero of=$FILE bs=4k count=1
1257         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1258         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1259 }
1260 run_test 27k "limit i_blksize for broken user apps ============="
1261
1262 test_27l() {
1263         test_mkdir -p $DIR/d27
1264         mcreate $DIR/f27l || error "creating file"
1265         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1266                 error "setstripe should have failed" || true
1267 }
1268 run_test 27l "check setstripe permissions (should return error)"
1269
1270 test_27m() {
1271         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1272                 return
1273         if [ $ORIGFREE -gt $MAXFREE ]; then
1274                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1275                 return
1276         fi
1277         trap simple_cleanup_common EXIT
1278         test_mkdir -p $DIR/$tdir
1279         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1280         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1281                 error "dd should fill OST0"
1282         i=2
1283         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1284                 i=`expr $i + 1`
1285                 [ $i -gt 256 ] && break
1286         done
1287         i=`expr $i + 1`
1288         touch $DIR/$tdir/f27m_$i
1289         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1290                 error "OST0 was full but new created file still use it"
1291         i=`expr $i + 1`
1292         touch $DIR/$tdir/f27m_$i
1293         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1294                 error "OST0 was full but new created file still use it"
1295         simple_cleanup_common
1296 }
1297 run_test 27m "create file while OST0 was full =================="
1298
1299 sleep_maxage() {
1300         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1301         sleep $DELAY
1302 }
1303
1304 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1305 # if the OST isn't full anymore.
1306 reset_enospc() {
1307         local OSTIDX=${1:-""}
1308
1309         local list=$(comma_list $(osts_nodes))
1310         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1311
1312         do_nodes $list lctl set_param fail_loc=0
1313         sync    # initiate all OST_DESTROYs from MDS to OST
1314         sleep_maxage
1315 }
1316
1317 exhaust_precreations() {
1318         local OSTIDX=$1
1319         local FAILLOC=$2
1320         local FAILIDX=${3:-$OSTIDX}
1321
1322         test_mkdir -p $DIR/$tdir
1323         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1324         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1325
1326         local OST=$(ostname_from_index $OSTIDX)
1327         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1328                           sed -e 's/_UUID$//;s/^.*-//')
1329
1330         # on the mdt's osc
1331         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1332         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1333         osc.$mdtosc_proc1.prealloc_last_id)
1334         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1335         osc.$mdtosc_proc1.prealloc_next_id)
1336
1337         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1338         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1339
1340         test_mkdir -p $DIR/$tdir/${OST}
1341         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1342 #define OBD_FAIL_OST_ENOSPC              0x215
1343         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1344         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1345         echo "Creating to objid $last_id on ost $OST..."
1346         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1347         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1348         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1349         sleep_maxage
1350 }
1351
1352 exhaust_all_precreations() {
1353         local i
1354         for (( i=0; i < OSTCOUNT; i++ )) ; do
1355                 exhaust_precreations $i $1 -1
1356         done
1357 }
1358
1359 test_27n() {
1360         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1362         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1363         remote_ost_nodsh && skip "remote OST with nodsh" && return
1364
1365         reset_enospc
1366         rm -f $DIR/$tdir/$tfile
1367         exhaust_precreations 0 0x80000215
1368         $SETSTRIPE -c -1 $DIR/$tdir
1369         touch $DIR/$tdir/$tfile || error
1370         $GETSTRIPE $DIR/$tdir/$tfile
1371         reset_enospc
1372 }
1373 run_test 27n "create file with some full OSTs =================="
1374
1375 test_27o() {
1376         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1378         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1379         remote_ost_nodsh && skip "remote OST with nodsh" && return
1380
1381         reset_enospc
1382         rm -f $DIR/$tdir/$tfile
1383         exhaust_all_precreations 0x215
1384
1385         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1386
1387         reset_enospc
1388         rm -rf $DIR/$tdir/*
1389 }
1390 run_test 27o "create file with all full OSTs (should error) ===="
1391
1392 test_27p() {
1393         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1395         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1396         remote_ost_nodsh && skip "remote OST with nodsh" && return
1397
1398         reset_enospc
1399         rm -f $DIR/$tdir/$tfile
1400         test_mkdir -p $DIR/$tdir
1401
1402         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1403         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1404         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1405
1406         exhaust_precreations 0 0x80000215
1407         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1408         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1409         $GETSTRIPE $DIR/$tdir/$tfile
1410
1411         reset_enospc
1412 }
1413 run_test 27p "append to a truncated file with some full OSTs ==="
1414
1415 test_27q() {
1416         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1418         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1419         remote_ost_nodsh && skip "remote OST with nodsh" && return
1420
1421         reset_enospc
1422         rm -f $DIR/$tdir/$tfile
1423
1424         test_mkdir -p $DIR/$tdir
1425         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1426         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1427         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1428
1429         exhaust_all_precreations 0x215
1430
1431         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1432         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1433
1434         reset_enospc
1435 }
1436 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1437
1438 test_27r() {
1439         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1441         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1442         remote_ost_nodsh && skip "remote OST with nodsh" && return
1443
1444         reset_enospc
1445         rm -f $DIR/$tdir/$tfile
1446         exhaust_precreations 0 0x80000215
1447
1448         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1449
1450         reset_enospc
1451 }
1452 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1453
1454 test_27s() { # bug 10725
1455         test_mkdir -p $DIR/$tdir
1456         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1457         local stripe_count=0
1458         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1459         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1460                 error "stripe width >= 2^32 succeeded" || true
1461
1462 }
1463 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1464
1465 test_27t() { # bug 10864
1466         WDIR=`pwd`
1467         WLFS=`which lfs`
1468         cd $DIR
1469         touch $tfile
1470         $WLFS getstripe $tfile
1471         cd $WDIR
1472 }
1473 run_test 27t "check that utils parse path correctly"
1474
1475 test_27u() { # bug 4900
1476         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1477         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1478
1479 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1480         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1481         test_mkdir -p $DIR/$tdir
1482         createmany -o $DIR/$tdir/t- 1000
1483         do_facet $SINGLEMDS lctl set_param fail_loc=0
1484
1485         TLOG=$DIR/$tfile.getstripe
1486         $GETSTRIPE $DIR/$tdir > $TLOG
1487         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1488         unlinkmany $DIR/$tdir/t- 1000
1489         [ $OBJS -gt 0 ] && \
1490                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1491 }
1492 run_test 27u "skip object creation on OSC w/o objects =========="
1493
1494 test_27v() { # bug 4900
1495         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1497         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1498         remote_ost_nodsh && skip "remote OST with nodsh" && return
1499
1500         exhaust_all_precreations 0x215
1501         reset_enospc
1502
1503         test_mkdir -p $DIR/$tdir
1504         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1505
1506         touch $DIR/$tdir/$tfile
1507         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1508         # all except ost1
1509         for (( i=1; i < OSTCOUNT; i++ )); do
1510                 do_facet ost$i lctl set_param fail_loc=0x705
1511         done
1512         local START=`date +%s`
1513         createmany -o $DIR/$tdir/$tfile 32
1514
1515         local FINISH=`date +%s`
1516         local TIMEOUT=`lctl get_param -n timeout`
1517         local PROCESS=$((FINISH - START))
1518         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1519                error "$FINISH - $START >= $TIMEOUT / 2"
1520         sleep $((TIMEOUT / 2 - PROCESS))
1521         reset_enospc
1522 }
1523 run_test 27v "skip object creation on slow OST ================="
1524
1525 test_27w() { # bug 10997
1526         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1527         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1528         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1529                 error "stripe size $size != 65536" || true
1530         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1531                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1532 }
1533 run_test 27w "check $SETSTRIPE -S option"
1534
1535 test_27wa() {
1536         [ "$OSTCOUNT" -lt "2" ] &&
1537                 skip_env "skipping multiple stripe count/offset test" && return
1538
1539         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1540         for i in $(seq 1 $OSTCOUNT); do
1541                 offset=$((i - 1))
1542                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1543                         error "setstripe -c $i -i $offset failed"
1544                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1545                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1546                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1547                 [ $index -ne $offset ] &&
1548                         error "stripe offset $index != $offset" || true
1549         done
1550 }
1551 run_test 27wa "check $SETSTRIPE -c -i options"
1552
1553 test_27x() {
1554         remote_ost_nodsh && skip "remote OST with nodsh" && return
1555         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1557         OFFSET=$(($OSTCOUNT - 1))
1558         OSTIDX=0
1559         local OST=$(ostname_from_index $OSTIDX)
1560
1561         test_mkdir -p $DIR/$tdir
1562         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1563         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1564         sleep_maxage
1565         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1566         for i in `seq 0 $OFFSET`; do
1567                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1568                 error "OST0 was degraded but new created file still use it"
1569         done
1570         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1571 }
1572 run_test 27x "create files while OST0 is degraded"
1573
1574 test_27y() {
1575         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1576         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1577         remote_ost_nodsh && skip "remote OST with nodsh" && return
1578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1579
1580         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1581         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1582             osc.$mdtosc.prealloc_last_id)
1583         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1584             osc.$mdtosc.prealloc_next_id)
1585         local fcount=$((last_id - next_id))
1586         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1587         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1588
1589         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1590                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1591         local OST_DEACTIVE_IDX=-1
1592         local OSC
1593         local OSTIDX
1594         local OST
1595
1596         for OSC in $MDS_OSCS; do
1597                 OST=$(osc_to_ost $OSC)
1598                 OSTIDX=$(index_from_ostuuid $OST)
1599                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1600                         OST_DEACTIVE_IDX=$OSTIDX
1601                 fi
1602                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1603                         echo $OSC "is Deactivated:"
1604                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1605                 fi
1606         done
1607
1608         OSTIDX=$(index_from_ostuuid $OST)
1609         mkdir -p $DIR/$tdir
1610         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1611
1612         for OSC in $MDS_OSCS; do
1613                 OST=$(osc_to_ost $OSC)
1614                 OSTIDX=$(index_from_ostuuid $OST)
1615                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1616                         echo $OST "is degraded:"
1617                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1618                                                 obdfilter.$OST.degraded=1
1619                 fi
1620         done
1621
1622         sleep_maxage
1623         createmany -o $DIR/$tdir/$tfile $fcount
1624
1625         for OSC in $MDS_OSCS; do
1626                 OST=$(osc_to_ost $OSC)
1627                 OSTIDX=$(index_from_ostuuid $OST)
1628                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1629                         echo $OST "is recovered from degraded:"
1630                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1631                                                 obdfilter.$OST.degraded=0
1632                 else
1633                         do_facet $SINGLEMDS lctl --device %$OSC activate
1634                 fi
1635         done
1636 }
1637 run_test 27y "create files while OST0 is degraded and the rest inactive"
1638
1639 check_seq_oid()
1640 {
1641         log "check file $1"
1642
1643         lmm_count=$($GETSTRIPE -c $1)
1644         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1645         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1646
1647         local old_ifs="$IFS"
1648         IFS=$'[:]'
1649         fid=($($LFS path2fid $1))
1650         IFS="$old_ifs"
1651
1652         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1653         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1654
1655         # compare lmm_seq and lu_fid->f_seq
1656         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1657         # compare lmm_object_id and lu_fid->oid
1658         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1659
1660         # check the trusted.fid attribute of the OST objects of the file
1661         local have_obdidx=false
1662         local stripe_nr=0
1663         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1664                 # skip lines up to and including "obdidx"
1665                 [ -z "$obdidx" ] && break
1666                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1667                 $have_obdidx || continue
1668
1669                 local ost=$((obdidx + 1))
1670                 local dev=$(ostdevname $ost)
1671
1672                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1673                         echo "Currently only works with ldiskfs-based OSTs"
1674                         continue
1675                 fi
1676
1677                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1678
1679                 #don't unmount/remount the OSTs if we don't need to do that
1680                 #local dir=$(facet_mntpt ost$ost)
1681                 #stop ost$dev
1682                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1683                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1684                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1685                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1686                 seq=$(echo $seq | sed -e "s/^0x//g")
1687                 if [ $seq == 0 ]; then
1688                         oid_hex=$(echo $oid)
1689                 else
1690                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1691                 fi
1692                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1693                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1694                            $dev 2>/dev/null" | grep "parent=")
1695
1696                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1697
1698                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1699
1700                 #do_facet ost$ost umount -d $dir
1701                 #start ost$ost $dev $OST_MOUNT_OPTS
1702
1703                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1704                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1705                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1706                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1707                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1708                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1709
1710                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1711                 [ $ff_pseq = $lmm_seq ] ||
1712                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1713                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1714                 [ $ff_poid = $lmm_oid ] ||
1715                         error "FF parent OID $ff_poid != $lmm_oid"
1716                 [ $ff_pstripe = $stripe_nr ] ||
1717                         error "FF stripe $ff_pstripe != $stripe_nr"
1718
1719                 stripe_nr=$((stripe_nr + 1))
1720         done
1721 }
1722
1723 test_27z() {
1724         remote_ost_nodsh && skip "remote OST with nodsh" && return
1725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1726         test_mkdir -p $DIR/$tdir
1727
1728         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1729                 { error "setstripe -c -1 failed"; return 1; }
1730         # We need to send a write to every object to get parent FID info set.
1731         # This _should_ also work for setattr, but does not currently.
1732         # touch $DIR/$tdir/$tfile-1 ||
1733         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1734                 { error "dd $tfile-1 failed"; return 2; }
1735         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1736                 { error "setstripe -c -1 failed"; return 3; }
1737         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1738                 { error "dd $tfile-2 failed"; return 4; }
1739
1740         # make sure write RPCs have been sent to OSTs
1741         sync; sleep 5; sync
1742
1743         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1744         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1745 }
1746 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1747
1748 test_27A() { # b=19102
1749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1750         local restore_size=$($GETSTRIPE -S $MOUNT)
1751         local restore_count=$($GETSTRIPE -c $MOUNT)
1752         local restore_offset=$($GETSTRIPE -i $MOUNT)
1753         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1754         local default_size=$($GETSTRIPE -S $MOUNT)
1755         local default_count=$($GETSTRIPE -c $MOUNT)
1756         local default_offset=$($GETSTRIPE -i $MOUNT)
1757         local dsize=$((1024 * 1024))
1758         [ $default_size -eq $dsize ] ||
1759                 error "stripe size $default_size != $dsize"
1760         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1761         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1762         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1763 }
1764 run_test 27A "check filesystem-wide default LOV EA values"
1765
1766 # createtest also checks that device nodes are created and
1767 # then visible correctly (#2091)
1768 test_28() { # bug 2091
1769         test_mkdir $DIR/d28
1770         $CREATETEST $DIR/d28/ct || error
1771 }
1772 run_test 28 "create/mknod/mkdir with bad file types ============"
1773
1774 test_29() {
1775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1776         cancel_lru_locks mdc
1777         test_mkdir $DIR/d29
1778         touch $DIR/d29/foo
1779         log 'first d29'
1780         ls -l $DIR/d29
1781
1782         declare -i LOCKCOUNTORIG=0
1783         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1784                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1785         done
1786         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1787
1788         declare -i LOCKUNUSEDCOUNTORIG=0
1789         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1790                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1791         done
1792
1793         log 'second d29'
1794         ls -l $DIR/d29
1795         log 'done'
1796
1797         declare -i LOCKCOUNTCURRENT=0
1798         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1799                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1800         done
1801
1802         declare -i LOCKUNUSEDCOUNTCURRENT=0
1803         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1804                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1805         done
1806
1807         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1808                 lctl set_param -n ldlm.dump_namespaces ""
1809                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1810                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1811                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1812                 return 2
1813         fi
1814         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1815                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1816                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1817                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1818                 return 3
1819         fi
1820 }
1821 run_test 29 "IT_GETATTR regression  ============================"
1822
1823 test_30a() { # was test_30
1824         cp `which ls` $DIR || cp /bin/ls $DIR
1825         $DIR/ls / || error
1826         rm $DIR/ls
1827 }
1828 run_test 30a "execute binary from Lustre (execve) =============="
1829
1830 test_30b() {
1831         cp `which ls` $DIR || cp /bin/ls $DIR
1832         chmod go+rx $DIR/ls
1833         $RUNAS $DIR/ls / || error
1834         rm $DIR/ls
1835 }
1836 run_test 30b "execute binary from Lustre as non-root ==========="
1837
1838 test_30c() { # b=22376
1839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1840         cp `which ls` $DIR || cp /bin/ls $DIR
1841         chmod a-rw $DIR/ls
1842         cancel_lru_locks mdc
1843         cancel_lru_locks osc
1844         $RUNAS $DIR/ls / || error
1845         rm -f $DIR/ls
1846 }
1847 run_test 30c "execute binary from Lustre without read perms ===="
1848
1849 test_31a() {
1850         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1851         $CHECKSTAT -a $DIR/f31 || error
1852 }
1853 run_test 31a "open-unlink file =================================="
1854
1855 test_31b() {
1856         touch $DIR/f31 || error
1857         ln $DIR/f31 $DIR/f31b || error
1858         $MULTIOP $DIR/f31b Ouc || error
1859         $CHECKSTAT -t file $DIR/f31 || error
1860 }
1861 run_test 31b "unlink file with multiple links while open ======="
1862
1863 test_31c() {
1864         touch $DIR/f31 || error
1865         ln $DIR/f31 $DIR/f31c || error
1866         multiop_bg_pause $DIR/f31 O_uc || return 1
1867         MULTIPID=$!
1868         $MULTIOP $DIR/f31c Ouc
1869         kill -USR1 $MULTIPID
1870         wait $MULTIPID
1871 }
1872 run_test 31c "open-unlink file with multiple links ============="
1873
1874 test_31d() {
1875         opendirunlink $DIR/d31d $DIR/d31d || error
1876         $CHECKSTAT -a $DIR/d31d || error
1877 }
1878 run_test 31d "remove of open directory ========================="
1879
1880 test_31e() { # bug 2904
1881         check_kernel_version 34 || return 0
1882         openfilleddirunlink $DIR/d31e || error
1883 }
1884 run_test 31e "remove of open non-empty directory ==============="
1885
1886 test_31f() { # bug 4554
1887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1888         set -vx
1889         test_mkdir $DIR/d31f
1890         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1891         cp /etc/hosts $DIR/d31f
1892         ls -l $DIR/d31f
1893         $GETSTRIPE $DIR/d31f/hosts
1894         multiop_bg_pause $DIR/d31f D_c || return 1
1895         MULTIPID=$!
1896
1897         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1898         test_mkdir $DIR/d31f
1899         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1900         cp /etc/hosts $DIR/d31f
1901         ls -l $DIR/d31f
1902         $GETSTRIPE $DIR/d31f/hosts
1903         multiop_bg_pause $DIR/d31f D_c || return 1
1904         MULTIPID2=$!
1905
1906         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1907         wait $MULTIPID || error "first opendir $MULTIPID failed"
1908
1909         sleep 6
1910
1911         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1912         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1913         set +vx
1914 }
1915 run_test 31f "remove of open directory with open-unlink file ==="
1916
1917 test_31g() {
1918         echo "-- cross directory link --"
1919         test_mkdir $DIR/d31ga
1920         test_mkdir $DIR/d31gb
1921         touch $DIR/d31ga/f
1922         ln $DIR/d31ga/f $DIR/d31gb/g
1923         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1924         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1925         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1926         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1927 }
1928 run_test 31g "cross directory link==============="
1929
1930 test_31h() {
1931         echo "-- cross directory link --"
1932         test_mkdir $DIR/d31h
1933         test_mkdir $DIR/d31h/dir
1934         touch $DIR/d31h/f
1935         ln $DIR/d31h/f $DIR/d31h/dir/g
1936         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1937         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1938         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1939         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1940 }
1941 run_test 31h "cross directory link under child==============="
1942
1943 test_31i() {
1944         echo "-- cross directory link --"
1945         test_mkdir $DIR/d31i
1946         test_mkdir $DIR/d31i/dir
1947         touch $DIR/d31i/dir/f
1948         ln $DIR/d31i/dir/f $DIR/d31i/g
1949         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1950         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1951         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1952         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1953 }
1954 run_test 31i "cross directory link under parent==============="
1955
1956
1957 test_31j() {
1958         test_mkdir $DIR/d31j
1959         test_mkdir $DIR/d31j/dir1
1960         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1961         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1962         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1963         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1964         return 0
1965 }
1966 run_test 31j "link for directory==============="
1967
1968
1969 test_31k() {
1970         test_mkdir $DIR/d31k
1971         touch $DIR/d31k/s
1972         touch $DIR/d31k/exist
1973         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1974         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1975         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1976         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1977         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1978         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1979         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1980         return 0
1981 }
1982 run_test 31k "link to file: the same, non-existing, dir==============="
1983
1984 test_31m() {
1985         test_mkdir $DIR/d31m
1986         touch $DIR/d31m/s
1987         test_mkdir $DIR/d31m2
1988         touch $DIR/d31m2/exist
1989         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1990         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1991         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1992         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1993         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1994         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1995         return 0
1996 }
1997 run_test 31m "link to file: the same, non-existing, dir==============="
1998
1999 cleanup_test32_mount() {
2000         trap 0
2001         $UMOUNT $DIR/$tdir/ext2-mountpoint
2002 }
2003
2004 test_32a() {
2005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2006         echo "== more mountpoints and symlinks ================="
2007         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2008         trap cleanup_test32_mount EXIT
2009         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2010         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2011         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2012         cleanup_test32_mount
2013 }
2014 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2015
2016 test_32b() {
2017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2018         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2019         trap cleanup_test32_mount EXIT
2020         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2021         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2022         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2023         cleanup_test32_mount
2024 }
2025 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2026
2027 test_32c() {
2028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2029         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2030         trap cleanup_test32_mount EXIT
2031         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2032         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2033         test_mkdir -p $DIR/$tdir/d2/test_dir
2034         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2035         cleanup_test32_mount
2036 }
2037 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2038
2039 test_32d() {
2040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2041         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2042         trap cleanup_test32_mount EXIT
2043         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2044         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2045         test_mkdir -p $DIR/$tdir/d2/test_dir
2046         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2047         cleanup_test32_mount
2048 }
2049 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2050
2051 test_32e() {
2052         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2053         test_mkdir -p $DIR/d32e/tmp
2054         TMP_DIR=$DIR/d32e/tmp
2055         ln -s $DIR/d32e $TMP_DIR/symlink11
2056         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2057         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2058         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2059 }
2060 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2061
2062 test_32f() {
2063         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2064         test_mkdir -p $DIR/d32f/tmp
2065         TMP_DIR=$DIR/d32f/tmp
2066         ln -s $DIR/d32f $TMP_DIR/symlink11
2067         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2068         ls $DIR/d32f/tmp/symlink11  || error
2069         ls $DIR/d32f/symlink01 || error
2070 }
2071 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2072
2073 test_32g() {
2074         TMP_DIR=$DIR/$tdir/tmp
2075         test_mkdir -p $DIR/$tdir/tmp
2076         test_mkdir $DIR/${tdir}2
2077         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2078         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2079         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2080         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2081         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2082         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2083 }
2084 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2085
2086 test_32h() {
2087         rm -fr $DIR/$tdir $DIR/${tdir}2
2088         TMP_DIR=$DIR/$tdir/tmp
2089         test_mkdir -p $DIR/$tdir/tmp
2090         test_mkdir $DIR/${tdir}2
2091         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2092         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2093         ls $TMP_DIR/symlink12 || error
2094         ls $DIR/$tdir/symlink02  || error
2095 }
2096 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2097
2098 test_32i() {
2099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2100         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2101         trap cleanup_test32_mount EXIT
2102         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2103         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2104         touch $DIR/$tdir/test_file
2105         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2106         cleanup_test32_mount
2107 }
2108 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2109
2110 test_32j() {
2111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2112         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2113         trap cleanup_test32_mount EXIT
2114         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2115         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2116         touch $DIR/$tdir/test_file
2117         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2118         cleanup_test32_mount
2119 }
2120 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2121
2122 test_32k() {
2123         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2124         rm -fr $DIR/$tdir
2125         trap cleanup_test32_mount EXIT
2126         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2127         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2128         test_mkdir -p $DIR/$tdir/d2
2129         touch $DIR/$tdir/d2/test_file || error
2130         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2131         cleanup_test32_mount
2132 }
2133 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2134
2135 test_32l() {
2136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2137         rm -fr $DIR/$tdir
2138         trap cleanup_test32_mount EXIT
2139         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2140         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2141         test_mkdir -p $DIR/$tdir/d2
2142         touch $DIR/$tdir/d2/test_file
2143         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2144         cleanup_test32_mount
2145 }
2146 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2147
2148 test_32m() {
2149         rm -fr $DIR/d32m
2150         test_mkdir -p $DIR/d32m/tmp
2151         TMP_DIR=$DIR/d32m/tmp
2152         ln -s $DIR $TMP_DIR/symlink11
2153         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2154         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2155         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2156 }
2157 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2158
2159 test_32n() {
2160         rm -fr $DIR/d32n
2161         test_mkdir -p $DIR/d32n/tmp
2162         TMP_DIR=$DIR/d32n/tmp
2163         ln -s $DIR $TMP_DIR/symlink11
2164         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2165         ls -l $DIR/d32n/tmp/symlink11  || error
2166         ls -l $DIR/d32n/symlink01 || error
2167 }
2168 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2169
2170 test_32o() {
2171         rm -fr $DIR/d32o $DIR/$tfile
2172         touch $DIR/$tfile
2173         test_mkdir -p $DIR/d32o/tmp
2174         TMP_DIR=$DIR/d32o/tmp
2175         ln -s $DIR/$tfile $TMP_DIR/symlink12
2176         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2177         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2178         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2179         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2180         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2181 }
2182 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2183
2184 test_32p() {
2185     log 32p_1
2186         rm -fr $DIR/d32p
2187     log 32p_2
2188         rm -f $DIR/$tfile
2189     log 32p_3
2190         touch $DIR/$tfile
2191     log 32p_4
2192         test_mkdir -p $DIR/d32p/tmp
2193     log 32p_5
2194         TMP_DIR=$DIR/d32p/tmp
2195     log 32p_6
2196         ln -s $DIR/$tfile $TMP_DIR/symlink12
2197     log 32p_7
2198         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2199     log 32p_8
2200         cat $DIR/d32p/tmp/symlink12 || error
2201     log 32p_9
2202         cat $DIR/d32p/symlink02 || error
2203     log 32p_10
2204 }
2205 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2206
2207 cleanup_testdir_mount() {
2208         trap 0
2209         $UMOUNT $DIR/$tdir
2210 }
2211
2212 test_32q() {
2213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2214         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2215         trap cleanup_testdir_mount EXIT
2216         test_mkdir -p $DIR/$tdir
2217         touch $DIR/$tdir/under_the_mount
2218         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2219         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2220         cleanup_testdir_mount
2221 }
2222 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2223
2224 test_32r() {
2225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2226         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2227         trap cleanup_testdir_mount EXIT
2228         test_mkdir -p $DIR/$tdir
2229         touch $DIR/$tdir/under_the_mount
2230         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2231         ls $DIR/$tdir | grep -q under_the_mount && error || true
2232         cleanup_testdir_mount
2233 }
2234 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2235
2236 test_33aa() {
2237         rm -f $DIR/$tfile
2238         touch $DIR/$tfile
2239         chmod 444 $DIR/$tfile
2240         chown $RUNAS_ID $DIR/$tfile
2241         log 33_1
2242         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2243         log 33_2
2244 }
2245 run_test 33aa "write file with mode 444 (should return error) ===="
2246
2247 test_33a() {
2248         rm -fr $DIR/d33
2249         test_mkdir -p $DIR/d33
2250         chown $RUNAS_ID $DIR/d33
2251         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2252         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2253                 error "open RDWR" || true
2254 }
2255 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2256
2257 test_33b() {
2258         rm -fr $DIR/d33
2259         test_mkdir -p $DIR/d33
2260         chown $RUNAS_ID $DIR/d33
2261         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2262 }
2263 run_test 33b "test open file with malformed flags (No panic and return error)"
2264
2265 test_33c() {
2266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2267         local ostnum
2268         local ostname
2269         local write_bytes
2270         local all_zeros
2271
2272         remote_ost_nodsh && skip "remote OST with nodsh" && return
2273         all_zeros=:
2274         rm -fr $DIR/d33
2275         test_mkdir -p $DIR/d33
2276         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2277
2278         sync
2279         for ostnum in $(seq $OSTCOUNT); do
2280                 # test-framework's OST numbering is one-based, while Lustre's
2281                 # is zero-based
2282                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2283                 # Parsing llobdstat's output sucks; we could grep the /proc
2284                 # path, but that's likely to not be as portable as using the
2285                 # llobdstat utility.  So we parse lctl output instead.
2286                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2287                         obdfilter/$ostname/stats |
2288                         awk '/^write_bytes/ {print $7}' )
2289                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2290                 if (( ${write_bytes:-0} > 0 ))
2291                 then
2292                         all_zeros=false
2293                         break;
2294                 fi
2295         done
2296
2297         $all_zeros || return 0
2298
2299         # Write four bytes
2300         echo foo > $DIR/d33/bar
2301         # Really write them
2302         sync
2303
2304         # Total up write_bytes after writing.  We'd better find non-zeros.
2305         for ostnum in $(seq $OSTCOUNT); do
2306                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2307                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2308                         obdfilter/$ostname/stats |
2309                         awk '/^write_bytes/ {print $7}' )
2310                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2311                 if (( ${write_bytes:-0} > 0 ))
2312                 then
2313                         all_zeros=false
2314                         break;
2315                 fi
2316         done
2317
2318         if $all_zeros
2319         then
2320                 for ostnum in $(seq $OSTCOUNT); do
2321                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2322                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2323                         do_facet ost$ostnum lctl get_param -n \
2324                                 obdfilter/$ostname/stats
2325                 done
2326                 error "OST not keeping write_bytes stats (b22312)"
2327         fi
2328 }
2329 run_test 33c "test llobdstat and write_bytes"
2330
2331 test_33d() {
2332         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2334         local MDTIDX=1
2335         local remote_dir=$DIR/$tdir/remote_dir
2336
2337         mkdir -p $DIR/$tdir
2338         $LFS mkdir -i $MDTIDX $remote_dir ||
2339                 error "create remote directory failed"
2340
2341         touch $remote_dir/$tfile
2342         chmod 444 $remote_dir/$tfile
2343         chown $RUNAS_ID $remote_dir/$tfile
2344
2345         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2346
2347         chown $RUNAS_ID $remote_dir
2348         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2349                                         error "create" || true
2350         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2351                                     error "open RDWR" || true
2352         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2353                                     error "create" || true
2354 }
2355 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2356
2357 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2358 test_34a() {
2359         rm -f $DIR/f34
2360         $MCREATE $DIR/f34 || error
2361         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2362         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2363         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2364         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2365 }
2366 run_test 34a "truncate file that has not been opened ==========="
2367
2368 test_34b() {
2369         [ ! -f $DIR/f34 ] && test_34a
2370         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2371         $OPENFILE -f O_RDONLY $DIR/f34
2372         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2373         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2374 }
2375 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2376
2377 test_34c() {
2378         [ ! -f $DIR/f34 ] && test_34a
2379         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2380         $OPENFILE -f O_RDWR $DIR/f34
2381         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2382         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2383 }
2384 run_test 34c "O_RDWR opening file-with-size works =============="
2385
2386 test_34d() {
2387         [ ! -f $DIR/f34 ] && test_34a
2388         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2389         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2390         rm $DIR/f34
2391 }
2392 run_test 34d "write to sparse file ============================="
2393
2394 test_34e() {
2395         rm -f $DIR/f34e
2396         $MCREATE $DIR/f34e || error
2397         $TRUNCATE $DIR/f34e 1000 || error
2398         $CHECKSTAT -s 1000 $DIR/f34e || error
2399         $OPENFILE -f O_RDWR $DIR/f34e
2400         $CHECKSTAT -s 1000 $DIR/f34e || error
2401 }
2402 run_test 34e "create objects, some with size and some without =="
2403
2404 test_34f() { # bug 6242, 6243
2405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2406         SIZE34F=48000
2407         rm -f $DIR/f34f
2408         $MCREATE $DIR/f34f || error
2409         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2410         dd if=$DIR/f34f of=$TMP/f34f
2411         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2412         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2413         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2414         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2415         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2416 }
2417 run_test 34f "read from a file with no objects until EOF ======="
2418
2419 test_34g() {
2420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2421         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2422         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2423         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2424         cancel_lru_locks osc
2425         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2426                 error "wrong size after lock cancel"
2427
2428         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2429         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2430                 error "expanding truncate failed"
2431         cancel_lru_locks osc
2432         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2433                 error "wrong expanded size after lock cancel"
2434 }
2435 run_test 34g "truncate long file ==============================="
2436
2437 test_34h() {
2438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2439         local gid=10
2440         local sz=1000
2441
2442         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2443         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2444         MULTIPID=$!
2445         sleep 2
2446
2447         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2448                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2449                 kill -9 $MULTIPID
2450         fi
2451         wait $MULTIPID
2452         local nsz=`stat -c %s $DIR/$tfile`
2453         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2454 }
2455 run_test 34h "ftruncate file under grouplock should not block"
2456
2457 test_35a() {
2458         cp /bin/sh $DIR/f35a
2459         chmod 444 $DIR/f35a
2460         chown $RUNAS_ID $DIR/f35a
2461         $RUNAS $DIR/f35a && error || true
2462         rm $DIR/f35a
2463 }
2464 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2465
2466 test_36a() {
2467         rm -f $DIR/f36
2468         utime $DIR/f36 || error
2469 }
2470 run_test 36a "MDS utime check (mknod, utime) ==================="
2471
2472 test_36b() {
2473         echo "" > $DIR/f36
2474         utime $DIR/f36 || error
2475 }
2476 run_test 36b "OST utime check (open, utime) ===================="
2477
2478 test_36c() {
2479         rm -f $DIR/d36/f36
2480         test_mkdir $DIR/d36
2481         chown $RUNAS_ID $DIR/d36
2482         $RUNAS utime $DIR/d36/f36 || error
2483 }
2484 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2485
2486 test_36d() {
2487         [ ! -d $DIR/d36 ] && test_36c
2488         echo "" > $DIR/d36/f36
2489         $RUNAS utime $DIR/d36/f36 || error
2490 }
2491 run_test 36d "non-root OST utime check (open, utime) ==========="
2492
2493 test_36e() {
2494         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2495         test_mkdir -p $DIR/$tdir
2496         touch $DIR/$tdir/$tfile
2497         $RUNAS utime $DIR/$tdir/$tfile && \
2498                 error "utime worked, expected failure" || true
2499 }
2500 run_test 36e "utime on non-owned file (should return error) ===="
2501
2502 subr_36fh() {
2503         local fl="$1"
2504         local LANG_SAVE=$LANG
2505         local LC_LANG_SAVE=$LC_LANG
2506         export LANG=C LC_LANG=C # for date language
2507
2508         DATESTR="Dec 20  2000"
2509         test_mkdir -p $DIR/$tdir
2510         lctl set_param fail_loc=$fl
2511         date; date +%s
2512         cp /etc/hosts $DIR/$tdir/$tfile
2513         sync & # write RPC generated with "current" inode timestamp, but delayed
2514         sleep 1
2515         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2516         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2517         cancel_lru_locks osc
2518         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2519         date; date +%s
2520         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2521                 echo "BEFORE: $LS_BEFORE" && \
2522                 echo "AFTER : $LS_AFTER" && \
2523                 echo "WANT  : $DATESTR" && \
2524                 error "$DIR/$tdir/$tfile timestamps changed" || true
2525
2526         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2527 }
2528
2529 test_36f() {
2530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2531         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2532         subr_36fh "0x80000214"
2533 }
2534 run_test 36f "utime on file racing with OST BRW write =========="
2535
2536 test_36g() {
2537         remote_ost_nodsh && skip "remote OST with nodsh" && return
2538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2539         local fmd_max_age
2540         local fmd_before
2541         local fmd_after
2542
2543         test_mkdir -p $DIR/$tdir
2544         fmd_max_age=$(do_facet ost1 \
2545                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2546                 head -n 1")
2547
2548         fmd_before=$(do_facet ost1 \
2549                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2550         touch $DIR/$tdir/$tfile
2551         sleep $((fmd_max_age + 12))
2552         fmd_after=$(do_facet ost1 \
2553                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2554
2555         echo "fmd_before: $fmd_before"
2556         echo "fmd_after: $fmd_after"
2557         [ "$fmd_after" -gt "$fmd_before" ] && \
2558                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2559                 error "fmd didn't expire after ping" || true
2560 }
2561 run_test 36g "filter mod data cache expiry ====================="
2562
2563 test_36h() {
2564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2565         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2566         subr_36fh "0x80000227"
2567 }
2568 run_test 36h "utime on file racing with OST BRW write =========="
2569
2570 # test_37 - duplicate with tests 32q 32r
2571
2572 test_38() {
2573         local file=$DIR/$tfile
2574         touch $file
2575         openfile -f O_DIRECTORY $file
2576         local RC=$?
2577         local ENOTDIR=20
2578         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2579         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2580 }
2581 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2582
2583 test_39() {
2584         touch $DIR/$tfile
2585         touch $DIR/${tfile}2
2586 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2587 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2588 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2589         sleep 2
2590         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2591         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2592                 echo "mtime"
2593                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2594                 echo "atime"
2595                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2596                 echo "ctime"
2597                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2598                 error "O_TRUNC didn't change timestamps"
2599         fi
2600 }
2601 run_test 39 "mtime changed on create ==========================="
2602
2603 test_39b() {
2604         test_mkdir -p $DIR/$tdir
2605         cp -p /etc/passwd $DIR/$tdir/fopen
2606         cp -p /etc/passwd $DIR/$tdir/flink
2607         cp -p /etc/passwd $DIR/$tdir/funlink
2608         cp -p /etc/passwd $DIR/$tdir/frename
2609         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2610
2611         sleep 1
2612         echo "aaaaaa" >> $DIR/$tdir/fopen
2613         echo "aaaaaa" >> $DIR/$tdir/flink
2614         echo "aaaaaa" >> $DIR/$tdir/funlink
2615         echo "aaaaaa" >> $DIR/$tdir/frename
2616
2617         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2618         local link_new=`stat -c %Y $DIR/$tdir/flink`
2619         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2620         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2621
2622         cat $DIR/$tdir/fopen > /dev/null
2623         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2624         rm -f $DIR/$tdir/funlink2
2625         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2626
2627         for (( i=0; i < 2; i++ )) ; do
2628                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2629                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2630                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2631                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2632
2633                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2634                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2635                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2636                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2637
2638                 cancel_lru_locks osc
2639                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2640         done
2641 }
2642 run_test 39b "mtime change on open, link, unlink, rename  ======"
2643
2644 # this should be set to past
2645 TEST_39_MTIME=`date -d "1 year ago" +%s`
2646
2647 # bug 11063
2648 test_39c() {
2649         touch $DIR1/$tfile
2650         sleep 2
2651         local mtime0=`stat -c %Y $DIR1/$tfile`
2652
2653         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2654         local mtime1=`stat -c %Y $DIR1/$tfile`
2655         [ "$mtime1" = $TEST_39_MTIME ] || \
2656                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2657
2658         local d1=`date +%s`
2659         echo hello >> $DIR1/$tfile
2660         local d2=`date +%s`
2661         local mtime2=`stat -c %Y $DIR1/$tfile`
2662         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2663                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2664
2665         mv $DIR1/$tfile $DIR1/$tfile-1
2666
2667         for (( i=0; i < 2; i++ )) ; do
2668                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2669                 [ "$mtime2" = "$mtime3" ] || \
2670                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2671
2672                 cancel_lru_locks osc
2673                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2674         done
2675 }
2676 run_test 39c "mtime change on rename ==========================="
2677
2678 # bug 21114
2679 test_39d() {
2680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2681         touch $DIR1/$tfile
2682
2683         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2684
2685         for (( i=0; i < 2; i++ )) ; do
2686                 local mtime=`stat -c %Y $DIR1/$tfile`
2687                 [ $mtime = $TEST_39_MTIME ] || \
2688                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2689
2690                 cancel_lru_locks osc
2691                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2692         done
2693 }
2694 run_test 39d "create, utime, stat =============================="
2695
2696 # bug 21114
2697 test_39e() {
2698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2699         touch $DIR1/$tfile
2700         local mtime1=`stat -c %Y $DIR1/$tfile`
2701
2702         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2703
2704         for (( i=0; i < 2; i++ )) ; do
2705                 local mtime2=`stat -c %Y $DIR1/$tfile`
2706                 [ $mtime2 = $TEST_39_MTIME ] || \
2707                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2708
2709                 cancel_lru_locks osc
2710                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2711         done
2712 }
2713 run_test 39e "create, stat, utime, stat ========================"
2714
2715 # bug 21114
2716 test_39f() {
2717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2718         touch $DIR1/$tfile
2719         mtime1=`stat -c %Y $DIR1/$tfile`
2720
2721         sleep 2
2722         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2723
2724         for (( i=0; i < 2; i++ )) ; do
2725                 local mtime2=`stat -c %Y $DIR1/$tfile`
2726                 [ $mtime2 = $TEST_39_MTIME ] || \
2727                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2728
2729                 cancel_lru_locks osc
2730                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2731         done
2732 }
2733 run_test 39f "create, stat, sleep, utime, stat ================="
2734
2735 # bug 11063
2736 test_39g() {
2737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2738         echo hello >> $DIR1/$tfile
2739         local mtime1=`stat -c %Y $DIR1/$tfile`
2740
2741         sleep 2
2742         chmod o+r $DIR1/$tfile
2743
2744         for (( i=0; i < 2; i++ )) ; do
2745                 local mtime2=`stat -c %Y $DIR1/$tfile`
2746                 [ "$mtime1" = "$mtime2" ] || \
2747                         error "lost mtime: $mtime2, should be $mtime1"
2748
2749                 cancel_lru_locks osc
2750                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2751         done
2752 }
2753 run_test 39g "write, chmod, stat ==============================="
2754
2755 # bug 11063
2756 test_39h() {
2757         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2758         touch $DIR1/$tfile
2759         sleep 1
2760
2761         local d1=`date`
2762         echo hello >> $DIR1/$tfile
2763         local mtime1=`stat -c %Y $DIR1/$tfile`
2764
2765         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2766         local d2=`date`
2767         if [ "$d1" != "$d2" ]; then
2768                 echo "write and touch not within one second"
2769         else
2770                 for (( i=0; i < 2; i++ )) ; do
2771                         local mtime2=`stat -c %Y $DIR1/$tfile`
2772                         [ "$mtime2" = $TEST_39_MTIME ] || \
2773                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2774
2775                         cancel_lru_locks osc
2776                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2777                 done
2778         fi
2779 }
2780 run_test 39h "write, utime within one second, stat ============="
2781
2782 test_39i() {
2783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2784         touch $DIR1/$tfile
2785         sleep 1
2786
2787         echo hello >> $DIR1/$tfile
2788         local mtime1=`stat -c %Y $DIR1/$tfile`
2789
2790         mv $DIR1/$tfile $DIR1/$tfile-1
2791
2792         for (( i=0; i < 2; i++ )) ; do
2793                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2794
2795                 [ "$mtime1" = "$mtime2" ] || \
2796                         error "lost mtime: $mtime2, should be $mtime1"
2797
2798                 cancel_lru_locks osc
2799                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2800         done
2801 }
2802 run_test 39i "write, rename, stat =============================="
2803
2804 test_39j() {
2805         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2806         start_full_debug_logging
2807         touch $DIR1/$tfile
2808         sleep 1
2809
2810         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2811         lctl set_param fail_loc=0x80000412
2812         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2813                 error "multiop failed"
2814         local multipid=$!
2815         local mtime1=`stat -c %Y $DIR1/$tfile`
2816
2817         mv $DIR1/$tfile $DIR1/$tfile-1
2818
2819         kill -USR1 $multipid
2820         wait $multipid || error "multiop close failed"
2821
2822         for (( i=0; i < 2; i++ )) ; do
2823                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2824                 [ "$mtime1" = "$mtime2" ] ||
2825                         error "mtime is lost on close: $mtime2, " \
2826                               "should be $mtime1"
2827
2828                 cancel_lru_locks osc
2829                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2830         done
2831         lctl set_param fail_loc=0
2832         stop_full_debug_logging
2833 }
2834 run_test 39j "write, rename, close, stat ======================="
2835
2836 test_39k() {
2837         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2838         touch $DIR1/$tfile
2839         sleep 1
2840
2841         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2842         local multipid=$!
2843         local mtime1=`stat -c %Y $DIR1/$tfile`
2844
2845         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2846
2847         kill -USR1 $multipid
2848         wait $multipid || error "multiop close failed"
2849
2850         for (( i=0; i < 2; i++ )) ; do
2851                 local mtime2=`stat -c %Y $DIR1/$tfile`
2852
2853                 [ "$mtime2" = $TEST_39_MTIME ] || \
2854                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2855
2856                 cancel_lru_locks osc
2857                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2858         done
2859 }
2860 run_test 39k "write, utime, close, stat ========================"
2861
2862 # this should be set to future
2863 TEST_39_ATIME=`date -d "1 year" +%s`
2864
2865 test_39l() {
2866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2867         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2868         local atime_diff=$(do_facet $SINGLEMDS \
2869                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2870         rm -rf $DIR/$tdir
2871         mkdir -p $DIR/$tdir
2872
2873         # test setting directory atime to future
2874         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2875         local atime=$(stat -c %X $DIR/$tdir)
2876         [ "$atime" = $TEST_39_ATIME ] || \
2877                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2878
2879         # test setting directory atime from future to now
2880         local d1=$(date +%s)
2881         ls $DIR/$tdir
2882         local d2=$(date +%s)
2883
2884         cancel_lru_locks mdc
2885         atime=$(stat -c %X $DIR/$tdir)
2886         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2887                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2888
2889         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2890         sleep 3
2891
2892         # test setting directory atime when now > dir atime + atime_diff
2893         d1=$(date +%s)
2894         ls $DIR/$tdir
2895         d2=$(date +%s)
2896         cancel_lru_locks mdc
2897         atime=$(stat -c %X $DIR/$tdir)
2898         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2899                 error "atime is not updated  : $atime, should be $d2"
2900
2901         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2902         sleep 3
2903
2904         # test not setting directory atime when now < dir atime + atime_diff
2905         ls $DIR/$tdir
2906         cancel_lru_locks mdc
2907         atime=$(stat -c %X $DIR/$tdir)
2908         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2909                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2910
2911         do_facet $SINGLEMDS \
2912                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2913 }
2914 run_test 39l "directory atime update ==========================="
2915
2916 test_39m() {
2917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2918         touch $DIR1/$tfile
2919         sleep 2
2920         local far_past_mtime=$(date -d "May 29 1953" +%s)
2921         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2922
2923         touch -m -d @$far_past_mtime $DIR1/$tfile
2924         touch -a -d @$far_past_atime $DIR1/$tfile
2925
2926         for (( i=0; i < 2; i++ )) ; do
2927                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2928                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2929                         error "atime or mtime set incorrectly"
2930
2931                 cancel_lru_locks osc
2932                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2933         done
2934 }
2935 run_test 39m "test atime and mtime before 1970"
2936
2937 test_40() {
2938         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2939         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2940         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2941 }
2942 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2943
2944 test_41() {
2945         # bug 1553
2946         small_write $DIR/f41 18
2947 }
2948 run_test 41 "test small file write + fstat ====================="
2949
2950 count_ost_writes() {
2951         lctl get_param -n osc.*.stats |
2952             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2953 }
2954
2955 # decent default
2956 WRITEBACK_SAVE=500
2957 DIRTY_RATIO_SAVE=40
2958 MAX_DIRTY_RATIO=50
2959 BG_DIRTY_RATIO_SAVE=10
2960 MAX_BG_DIRTY_RATIO=25
2961
2962 start_writeback() {
2963         trap 0
2964         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2965         # dirty_ratio, dirty_background_ratio
2966         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2967                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2968                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2969                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2970         else
2971                 # if file not here, we are a 2.4 kernel
2972                 kill -CONT `pidof kupdated`
2973         fi
2974 }
2975
2976 stop_writeback() {
2977         # setup the trap first, so someone cannot exit the test at the
2978         # exact wrong time and mess up a machine
2979         trap start_writeback EXIT
2980         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2981         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2982                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2983                 sysctl -w vm.dirty_writeback_centisecs=0
2984                 sysctl -w vm.dirty_writeback_centisecs=0
2985                 # save and increase /proc/sys/vm/dirty_ratio
2986                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2987                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2988                 # save and increase /proc/sys/vm/dirty_background_ratio
2989                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2990                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2991         else
2992                 # if file not here, we are a 2.4 kernel
2993                 kill -STOP `pidof kupdated`
2994         fi
2995 }
2996
2997 # ensure that all stripes have some grant before we test client-side cache
2998 setup_test42() {
2999         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3000                 dd if=/dev/zero of=$i bs=4k count=1
3001                 rm $i
3002         done
3003 }
3004
3005 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3006 # file truncation, and file removal.
3007 test_42a() {
3008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3009         setup_test42
3010         cancel_lru_locks osc
3011         stop_writeback
3012         sync; sleep 1; sync # just to be safe
3013         BEFOREWRITES=`count_ost_writes`
3014         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3015         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3016         AFTERWRITES=`count_ost_writes`
3017         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3018                 error "$BEFOREWRITES < $AFTERWRITES"
3019         start_writeback
3020 }
3021 run_test 42a "ensure that we don't flush on close =============="
3022
3023 test_42b() {
3024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3025         setup_test42
3026         cancel_lru_locks osc
3027         stop_writeback
3028         sync
3029         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3030         BEFOREWRITES=`count_ost_writes`
3031         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3032         AFTERWRITES=`count_ost_writes`
3033         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3034                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3035         fi
3036         BEFOREWRITES=`count_ost_writes`
3037         sync || error "sync: $?"
3038         AFTERWRITES=`count_ost_writes`
3039         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3040                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3041         fi
3042         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3043         start_writeback
3044         return 0
3045 }
3046 run_test 42b "test destroy of file with cached dirty data ======"
3047
3048 # if these tests just want to test the effect of truncation,
3049 # they have to be very careful.  consider:
3050 # - the first open gets a {0,EOF}PR lock
3051 # - the first write conflicts and gets a {0, count-1}PW
3052 # - the rest of the writes are under {count,EOF}PW
3053 # - the open for truncate tries to match a {0,EOF}PR
3054 #   for the filesize and cancels the PWs.
3055 # any number of fixes (don't get {0,EOF} on open, match
3056 # composite locks, do smarter file size management) fix
3057 # this, but for now we want these tests to verify that
3058 # the cancellation with truncate intent works, so we
3059 # start the file with a full-file pw lock to match against
3060 # until the truncate.
3061 trunc_test() {
3062         test=$1
3063         file=$DIR/$test
3064         offset=$2
3065         cancel_lru_locks osc
3066         stop_writeback
3067         # prime the file with 0,EOF PW to match
3068         touch $file
3069         $TRUNCATE $file 0
3070         sync; sync
3071         # now the real test..
3072         dd if=/dev/zero of=$file bs=1024 count=100
3073         BEFOREWRITES=`count_ost_writes`
3074         $TRUNCATE $file $offset
3075         cancel_lru_locks osc
3076         AFTERWRITES=`count_ost_writes`
3077         start_writeback
3078 }
3079
3080 test_42c() {
3081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3082         trunc_test 42c 1024
3083         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3084             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3085         rm $file
3086 }
3087 run_test 42c "test partial truncate of file with cached dirty data"
3088
3089 test_42d() {
3090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3091         trunc_test 42d 0
3092         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3093             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3094         rm $file
3095 }
3096 run_test 42d "test complete truncate of file with cached dirty data"
3097
3098 test_42e() { # bug22074
3099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3100         local TDIR=$DIR/${tdir}e
3101         local pagesz=$(page_size)
3102         local pages=16 # hardcoded 16 pages, don't change it.
3103         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3104         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3105         local max_dirty_mb
3106         local warmup_files
3107
3108         test_mkdir -p $DIR/${tdir}e
3109         $SETSTRIPE -c 1 $TDIR
3110         createmany -o $TDIR/f $files
3111
3112         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3113
3114         # we assume that with $OSTCOUNT files, at least one of them will
3115         # be allocated on OST0.
3116         warmup_files=$((OSTCOUNT * max_dirty_mb))
3117         createmany -o $TDIR/w $warmup_files
3118
3119         # write a large amount of data into one file and sync, to get good
3120         # avail_grant number from OST.
3121         for ((i=0; i<$warmup_files; i++)); do
3122                 idx=$($GETSTRIPE -i $TDIR/w$i)
3123                 [ $idx -ne 0 ] && continue
3124                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3125                 break
3126         done
3127         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3128         sync
3129         $LCTL get_param $proc_osc0/cur_dirty_bytes
3130         $LCTL get_param $proc_osc0/cur_grant_bytes
3131
3132         # create as much dirty pages as we can while not to trigger the actual
3133         # RPCs directly. but depends on the env, VFS may trigger flush during this
3134         # period, hopefully we are good.
3135         for ((i=0; i<$warmup_files; i++)); do
3136                 idx=$($GETSTRIPE -i $TDIR/w$i)
3137                 [ $idx -ne 0 ] && continue
3138                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3139         done
3140         $LCTL get_param $proc_osc0/cur_dirty_bytes
3141         $LCTL get_param $proc_osc0/cur_grant_bytes
3142
3143         # perform the real test
3144         $LCTL set_param $proc_osc0/rpc_stats 0
3145         for ((;i<$files; i++)); do
3146                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3147                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3148         done
3149         sync
3150         $LCTL get_param $proc_osc0/rpc_stats
3151
3152         local percent=0
3153         local have_ppr=false
3154         $LCTL get_param $proc_osc0/rpc_stats |
3155                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3156                         # skip lines until we are at the RPC histogram data
3157                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3158                         $have_ppr || continue
3159
3160                         # we only want the percent stat for < 16 pages
3161                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3162
3163                         percent=$((percent + WPCT))
3164                         if [ $percent -gt 15 ]; then
3165                                 error "less than 16-pages write RPCs" \
3166                                       "$percent% > 15%"
3167                                 break
3168                         fi
3169                 done
3170         rm -rf $TDIR
3171 }
3172 run_test 42e "verify sub-RPC writes are not done synchronously"
3173
3174 test_43() {
3175         test_mkdir -p $DIR/$tdir
3176         cp -p /bin/ls $DIR/$tdir/$tfile
3177         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3178         pid=$!
3179         # give multiop a chance to open
3180         sleep 1
3181
3182         $DIR/$tdir/$tfile && error || true
3183         kill -USR1 $pid
3184 }
3185 run_test 43 "execution of file opened for write should return -ETXTBSY"
3186
3187 test_43a() {
3188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3189         test_mkdir -p $DIR/$tdir
3190         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3191                         cp -p multiop $DIR/$tdir/multiop
3192         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3193                         return 1
3194         MULTIOP_PID=$!
3195         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3196         kill -USR1 $MULTIOP_PID || return 2
3197         wait $MULTIOP_PID || return 3
3198         rm $TMP/test43.junk
3199 }
3200 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3201
3202 test_43b() {
3203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3204         test_mkdir -p $DIR/$tdir
3205         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3206                         cp -p multiop $DIR/$tdir/multiop
3207         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3208                         return 1
3209         MULTIOP_PID=$!
3210         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3211         kill -USR1 $MULTIOP_PID || return 2
3212         wait $MULTIOP_PID || return 3
3213         rm $TMP/test43.junk
3214 }
3215 run_test 43b "truncate of file being executed should return -ETXTBSY"
3216
3217 test_43c() {
3218         local testdir="$DIR/$tdir"
3219         test_mkdir -p $DIR/$tdir
3220         cp $SHELL $testdir/
3221         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3222                 ( cd $testdir && md5sum -c)
3223 }
3224 run_test 43c "md5sum of copy into lustre========================"
3225
3226 test_44() {
3227         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3228         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3229         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3230 }
3231 run_test 44 "zero length read from a sparse stripe ============="
3232
3233 test_44a() {
3234     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3235                          awk '{print $2}'`
3236     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3237     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3238     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3239                       awk '{print $2}'`
3240     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3241         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3242     fi
3243
3244     OFFSETS="0 $((stride/2)) $((stride-1))"
3245     for offset in $OFFSETS ; do
3246       for i in `seq 0 $((nstripe-1))`; do
3247         local GLOBALOFFSETS=""
3248         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3249         local myfn=$DIR/d44a-$size
3250         echo "--------writing $myfn at $size"
3251         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3252         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3253         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3254                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3255
3256         for j in `seq 0 $((nstripe-1))`; do
3257             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3258             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3259             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3260         done
3261         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3262                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3263         rm -f $myfn
3264       done
3265     done
3266 }
3267 run_test 44a "test sparse pwrite ==============================="
3268
3269 dirty_osc_total() {
3270         tot=0
3271         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3272                 tot=$(($tot + $d))
3273         done
3274         echo $tot
3275 }
3276 do_dirty_record() {
3277         before=`dirty_osc_total`
3278         echo executing "\"$*\""
3279         eval $*
3280         after=`dirty_osc_total`
3281         echo before $before, after $after
3282 }
3283 test_45() {
3284         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3285         f="$DIR/f45"
3286         # Obtain grants from OST if it supports it
3287         echo blah > ${f}_grant
3288         stop_writeback
3289         sync
3290         do_dirty_record "echo blah > $f"
3291         [ $before -eq $after ] && error "write wasn't cached"
3292         do_dirty_record "> $f"
3293         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3294         do_dirty_record "echo blah > $f"
3295         [ $before -eq $after ] && error "write wasn't cached"
3296         do_dirty_record "sync"
3297         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3298         do_dirty_record "echo blah > $f"
3299         [ $before -eq $after ] && error "write wasn't cached"
3300         do_dirty_record "cancel_lru_locks osc"
3301         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3302         start_writeback
3303 }
3304 run_test 45 "osc io page accounting ============================"
3305
3306 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3307 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3308 # objects offset and an assert hit when an rpc was built with 1023's mapped
3309 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3310 test_46() {
3311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3312         f="$DIR/f46"
3313         stop_writeback
3314         sync
3315         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3316         sync
3317         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3318         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3319         sync
3320         start_writeback
3321 }
3322 run_test 46 "dirtying a previously written page ================"
3323
3324 # test_47 is removed "Device nodes check" is moved to test_28
3325
3326 test_48a() { # bug 2399
3327         check_kernel_version 34 || return 0
3328         test_mkdir -p $DIR/$tdir
3329         cd $DIR/$tdir
3330         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3331         test_mkdir $DIR/$tdir || error "recreate directory failed"
3332         touch foo || error "'touch foo' failed after recreating cwd"
3333         test_mkdir $DIR/$tdir/bar ||
3334                      error "'mkdir foo' failed after recreating cwd"
3335         if check_kernel_version 44; then
3336                 touch .foo || error "'touch .foo' failed after recreating cwd"
3337                 test_mkdir $DIR/$tdir/.bar ||
3338                               error "'mkdir .foo' failed after recreating cwd"
3339         fi
3340         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3341         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3342         cd . || error "'cd .' failed after recreating cwd"
3343         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3344         rmdir . && error "'rmdir .' worked after recreating cwd"
3345         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3346         cd .. || error "'cd ..' failed after recreating cwd"
3347 }
3348 run_test 48a "Access renamed working dir (should return errors)="
3349
3350 test_48b() { # bug 2399
3351         check_kernel_version 34 || return 0
3352         rm -rf $DIR/$tdir
3353         test_mkdir -p $DIR/$tdir
3354         cd $DIR/$tdir
3355         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3356         touch foo && error "'touch foo' worked after removing cwd"
3357         test_mkdir $DIR/$tdir/foo &&
3358                      error "'mkdir foo' worked after removing cwd"
3359         if check_kernel_version 44; then
3360                 touch .foo && error "'touch .foo' worked after removing cwd"
3361                 test_mkdir $DIR/$tdir/.foo &&
3362                               error "'mkdir .foo' worked after removing cwd"
3363         fi
3364         ls . > /dev/null && error "'ls .' worked after removing cwd"
3365         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3366         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3367         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3368         rmdir . && error "'rmdir .' worked after removing cwd"
3369         ln -s . foo && error "'ln -s .' worked after removing cwd"
3370         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3371 }
3372 run_test 48b "Access removed working dir (should return errors)="
3373
3374 test_48c() { # bug 2350
3375         check_kernel_version 36 || return 0
3376         #lctl set_param debug=-1
3377         #set -vx
3378         rm -rf $DIR/$tdir
3379         test_mkdir -p $DIR/$tdir/dir
3380         cd $DIR/$tdir/dir
3381         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3382         $TRACE touch foo && error "touch foo worked after removing cwd"
3383         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3384         if check_kernel_version 44; then
3385                 touch .foo && error "touch .foo worked after removing cwd"
3386                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3387         fi
3388         $TRACE ls . && error "'ls .' worked after removing cwd"
3389         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3390         is_patchless || ( $TRACE cd . &&
3391                         error "'cd .' worked after removing cwd" )
3392         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3393         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3394         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3395         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3396 }
3397 run_test 48c "Access removed working subdir (should return errors)"
3398
3399 test_48d() { # bug 2350
3400         check_kernel_version 36 || return 0
3401         #lctl set_param debug=-1
3402         #set -vx
3403         rm -rf $DIR/$tdir
3404         test_mkdir -p $DIR/$tdir/dir
3405         cd $DIR/$tdir/dir
3406         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3407         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3408         $TRACE touch foo && error "'touch foo' worked after removing parent"
3409         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3410         if check_kernel_version 44; then
3411                 touch .foo && error "'touch .foo' worked after removing parent"
3412                 test_mkdir .foo &&
3413                               error "mkdir .foo worked after removing parent"
3414         fi
3415         $TRACE ls . && error "'ls .' worked after removing parent"
3416         $TRACE ls .. && error "'ls ..' worked after removing parent"
3417         is_patchless || ( $TRACE cd . &&
3418                         error "'cd .' worked after recreate parent" )
3419         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3420         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3421         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3422         is_patchless || ( $TRACE cd .. &&
3423                         error "'cd ..' worked after removing parent" || true )
3424 }
3425 run_test 48d "Access removed parent subdir (should return errors)"
3426
3427 test_48e() { # bug 4134
3428         check_kernel_version 41 || return 0
3429         #lctl set_param debug=-1
3430         #set -vx
3431         rm -rf $DIR/$tdir
3432         test_mkdir -p $DIR/$tdir/dir
3433         cd $DIR/$tdir/dir
3434         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3435         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3436         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3437         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3438         # On a buggy kernel addition of "touch foo" after cd .. will
3439         # produce kernel oops in lookup_hash_it
3440         touch ../foo && error "'cd ..' worked after recreate parent"
3441         cd $DIR
3442         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3443 }
3444 run_test 48e "Access to recreated parent subdir (should return errors)"
3445
3446 test_49() { # LU-1030
3447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3448         # get ost1 size - lustre-OST0000
3449         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3450         # write 800M at maximum
3451         [ $ost1_size -gt 819200 ] && ost1_size=819200
3452
3453         lfs setstripe -c 1 -i 0 $DIR/$tfile
3454         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3455         local dd_pid=$!
3456
3457         # change max_pages_per_rpc while writing the file
3458         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3459         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3460         # loop until dd process exits
3461         while ps ax -opid | grep -wq $dd_pid; do
3462                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3463                 sleep $((RANDOM % 5 + 1))
3464         done
3465         # restore original max_pages_per_rpc
3466         $LCTL set_param $osc1_mppc=$orig_mppc
3467         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3468 }
3469 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3470
3471 test_50() {
3472         # bug 1485
3473         test_mkdir $DIR/$tdir
3474         cd $DIR/$tdir
3475         ls /proc/$$/cwd || error
3476 }
3477 run_test 50 "special situations: /proc symlinks  ==============="
3478
3479 test_51a() {    # was test_51
3480         # bug 1516 - create an empty entry right after ".." then split dir
3481         test_mkdir $DIR/$tdir
3482         touch $DIR/$tdir/foo
3483         $MCREATE $DIR/$tdir/bar
3484         rm $DIR/$tdir/foo
3485         createmany -m $DIR/$tdir/longfile 201
3486         FNUM=202
3487         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3488                 $MCREATE $DIR/$tdir/longfile$FNUM
3489                 FNUM=$(($FNUM + 1))
3490                 echo -n "+"
3491         done
3492         echo
3493         ls -l $DIR/$tdir > /dev/null || error
3494 }
3495 run_test 51a "special situations: split htree with empty entry =="
3496
3497 export NUMTEST=70000
3498 test_51b() {
3499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3500         local BASE=$DIR/$tdir
3501
3502         test_mkdir -p $BASE
3503
3504         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3505         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3506         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3507                 return
3508
3509         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3510                 echo "reduced count to $NUMTEST due to inodes"
3511
3512         # need to check free space for the directories as well
3513         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3514         numfree=$((blkfree / 4))
3515         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3516                 echo "reduced count to $NUMTEST due to blocks"
3517
3518         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3519                 echo "failed" > $BASE/fnum
3520 }
3521 run_test 51b "exceed 64k subdirectory nlink limit"
3522
3523 test_51ba() { # LU-993
3524         local BASE=$DIR/$tdir
3525         # unlink all but 100 subdirectories, then check it still works
3526         local LEFT=100
3527         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3528
3529         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3530         local DELETE=$((NUMTEST - LEFT))
3531
3532         # continue on to run this test even if 51b didn't finish,
3533         # just to delete the many subdirectories created.
3534         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3535
3536         # for ldiskfs the nlink count should be 1, but this is OSD specific
3537         # and so this is listed for informational purposes only
3538         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3539         unlinkmany -d $BASE/d $DELETE
3540         RC=$?
3541
3542         if [ $RC -ne 0 ]; then
3543                 if [ "$NUMPREV" == "failed" ]; then
3544                         skip "previous setup failed"
3545                         return 0
3546                 else
3547                         error "unlink of first $DELETE subdirs failed"
3548                         return $RC
3549                 fi
3550         fi
3551
3552         echo "nlink between: $(stat -c %h $BASE)"
3553         # trim the first line of ls output
3554         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3555         [ $FOUND -ne $LEFT ] &&
3556                 error "can't find subdirs: found only $FOUND/$LEFT"
3557
3558         unlinkmany -d $BASE/d $DELETE $LEFT ||
3559                 error "unlink of second $LEFT subdirs failed"
3560         # regardless of whether the backing filesystem tracks nlink accurately
3561         # or not, the nlink count shouldn't be more than "." and ".." here
3562         local AFTER=$(stat -c %h $BASE)
3563         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3564                 echo "nlink after: $AFTER"
3565 }
3566 run_test 51ba "verify nlink for many subdirectory cleanup"
3567
3568 test_51d() {
3569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3570         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3571         test_mkdir -p $DIR/$tdir
3572         createmany -o $DIR/$tdir/t- 1000
3573         $GETSTRIPE $DIR/$tdir > $TMP/files
3574         for N in `seq 0 $((OSTCOUNT - 1))`; do
3575             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3576             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3577             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3578         done
3579         unlinkmany $DIR/$tdir/t- 1000
3580
3581         NLAST=0
3582         for N in `seq 1 $((OSTCOUNT - 1))`; do
3583             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3584                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3585             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3586                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3587
3588             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3589                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3590             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3591                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3592             NLAST=$N
3593         done
3594 }
3595 run_test 51d "check object distribution ===================="
3596
3597 test_52a() {
3598         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3599         test_mkdir -p $DIR/$tdir
3600         touch $DIR/$tdir/foo
3601         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3602         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3603         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3604         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3605         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3606                                         error "link worked"
3607         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3608         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3609         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3610                                                      error "lsattr"
3611         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3612         cp -r $DIR/$tdir /tmp/
3613         rm -fr $DIR/$tdir || error "cleanup rm failed"
3614 }
3615 run_test 52a "append-only flag test (should return errors) ====="
3616
3617 test_52b() {
3618         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3619         test_mkdir -p $DIR/$tdir
3620         touch $DIR/$tdir/foo
3621         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3622         cat test > $DIR/$tdir/foo && error "cat test worked"
3623         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3624         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3625         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3626                                         error "link worked"
3627         echo foo >> $DIR/$tdir/foo && error "echo worked"
3628         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3629         [ -f $DIR/$tdir/foo ] || error
3630         [ -f $DIR/$tdir/foo_ren ] && error
3631         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3632                                                         error "lsattr"
3633         chattr -i $DIR/$tdir/foo || error "chattr failed"
3634
3635         rm -fr $DIR/$tdir || error
3636 }
3637 run_test 52b "immutable flag test (should return errors) ======="
3638
3639 test_53() {
3640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3641         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3642         remote_ost_nodsh && skip "remote OST with nodsh" && return
3643
3644         local param
3645         local ostname
3646         local mds_last
3647         local ost_last
3648         local ostnum
3649         local node
3650
3651         # only test MDT0000
3652         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3653         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3654                 param=`echo ${value[0]} | cut -d "=" -f1`
3655                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3656                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3657                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3658                 node=$(facet_active_host ost$((ostnum+1)))
3659                 param="obdfilter.$ostname.last_id"
3660                 ost_last=$(do_node $node lctl get_param -n $param | head -n 1)
3661                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3662                 if [ $ost_last != $mds_last ]; then
3663                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3664                 fi
3665         done
3666 }
3667 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3668
3669 test_54a() {
3670         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3671         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3672         $SOCKETSERVER $DIR/socket
3673         $SOCKETCLIENT $DIR/socket || error
3674         $MUNLINK $DIR/socket
3675 }
3676 run_test 54a "unix domain socket test =========================="
3677
3678 test_54b() {
3679         f="$DIR/f54b"
3680         mknod $f c 1 3
3681         chmod 0666 $f
3682         dd if=/dev/zero of=$f bs=`page_size` count=1
3683 }
3684 run_test 54b "char device works in lustre ======================"
3685
3686 find_loop_dev() {
3687         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3688         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3689         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3690
3691         for i in `seq 3 7`; do
3692                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3693                 LOOPDEV=$LOOPBASE$i
3694                 LOOPNUM=$i
3695                 break
3696         done
3697 }
3698
3699 test_54c() {
3700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3701         tfile="$DIR/f54c"
3702         tdir="$DIR/d54c"
3703         loopdev="$DIR/loop54c"
3704
3705         find_loop_dev
3706         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3707         mknod $loopdev b 7 $LOOPNUM
3708         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3709         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3710         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3711         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3712         test_mkdir -p $tdir
3713         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3714         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3715         df $tdir
3716         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3717         $UMOUNT $tdir
3718         losetup -d $loopdev
3719         rm $loopdev
3720 }
3721 run_test 54c "block device works in lustre ====================="
3722
3723 test_54d() {
3724         f="$DIR/f54d"
3725         string="aaaaaa"
3726         mknod $f p
3727         [ "$string" = `echo $string > $f | cat $f` ] || error
3728 }
3729 run_test 54d "fifo device works in lustre ======================"
3730
3731 test_54e() {
3732         check_kernel_version 46 || return 0
3733         f="$DIR/f54e"
3734         string="aaaaaa"
3735         cp -aL /dev/console $f
3736         echo $string > $f || error
3737 }
3738 run_test 54e "console/tty device works in lustre ======================"
3739
3740 #The test_55 used to be iopen test and it was removed by bz#24037.
3741 #run_test 55 "check iopen_connect_dentry() ======================"
3742
3743 test_56a() {    # was test_56
3744         rm -rf $DIR/$tdir
3745         $SETSTRIPE -d $DIR
3746         test_mkdir $DIR/$tdir
3747         test_mkdir $DIR/$tdir/dir
3748         NUMFILES=3
3749         NUMFILESx2=$(($NUMFILES * 2))
3750         for i in `seq 1 $NUMFILES` ; do
3751                 touch $DIR/$tdir/file$i
3752                 touch $DIR/$tdir/dir/file$i
3753         done
3754
3755         # test lfs getstripe with --recursive
3756         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3757         [ $FILENUM -eq $NUMFILESx2 ] ||
3758                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3759         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3760         [ $FILENUM -eq $NUMFILES ] ||
3761                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3762         echo "$GETSTRIPE --recursive passed."
3763
3764         # test lfs getstripe with file instead of dir
3765         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3766         [ $FILENUM  -eq 1 ] || error \
3767                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3768         echo "$GETSTRIPE file1 passed."
3769
3770         #test lfs getstripe with --verbose
3771         [ `$GETSTRIPE --verbose $DIR/$tdir |
3772                         grep -c lmm_magic` -eq $NUMFILES ] ||
3773                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3774         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3775             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3776         echo "$GETSTRIPE --verbose passed."
3777
3778         #test lfs getstripe with --obd
3779         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3780                                         grep -q "unknown obduuid" ||
3781                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3782
3783         [  "$OSTCOUNT" -lt 2 ] &&
3784                 skip_env "skipping other $GETSTRIPE --obd test" && return
3785
3786         OSTIDX=1
3787         OBDUUID=$(ostuuid_from_index $OSTIDX)
3788         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3789         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3790         [ $FOUND -eq $FILENUM ] ||
3791                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3792         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3793                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3794                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3795                 error "$GETSTRIPE --obd: should not show file on other obd"
3796         echo "$GETSTRIPE --obd passed"
3797 }
3798 run_test 56a "check $GETSTRIPE"
3799
3800 NUMFILES=3
3801 NUMDIRS=3
3802 setup_56() {
3803         local LOCAL_NUMFILES="$1"
3804         local LOCAL_NUMDIRS="$2"
3805         local MKDIR_PARAMS="$3"
3806
3807         if [ ! -d "$TDIR" ] ; then
3808                 test_mkdir -p $TDIR
3809                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3810                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3811                         touch $TDIR/file$i
3812                 done
3813                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3814                         test_mkdir $TDIR/dir$i
3815                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3816                                 touch $TDIR/dir$i/file$j
3817                         done
3818                 done
3819         fi
3820 }
3821
3822 setup_56_special() {
3823         LOCAL_NUMFILES=$1
3824         LOCAL_NUMDIRS=$2
3825         setup_56 $1 $2
3826         if [ ! -e "$TDIR/loop1b" ] ; then
3827                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3828                         mknod $TDIR/loop${i}b b 7 $i
3829                         mknod $TDIR/null${i}c c 1 3
3830                         ln -s $TDIR/file1 $TDIR/link${i}l
3831                 done
3832                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3833                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3834                         mknod $TDIR/dir$i/null${i}c c 1 3
3835                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3836                 done
3837         fi
3838 }
3839
3840 test_56g() {
3841         $SETSTRIPE -d $DIR
3842
3843         TDIR=$DIR/${tdir}g
3844         setup_56 $NUMFILES $NUMDIRS
3845
3846         EXPECTED=$(($NUMDIRS + 2))
3847         # test lfs find with -name
3848         for i in $(seq 1 $NUMFILES) ; do
3849                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3850                 [ $NUMS -eq $EXPECTED ] ||
3851                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3852                               "found $NUMS, expected $EXPECTED"
3853         done
3854 }
3855 run_test 56g "check lfs find -name ============================="
3856
3857 test_56h() {
3858         $SETSTRIPE -d $DIR
3859
3860         TDIR=$DIR/${tdir}g
3861         setup_56 $NUMFILES $NUMDIRS
3862
3863         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3864         # test lfs find with ! -name
3865         for i in $(seq 1 $NUMFILES) ; do
3866                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3867                 [ $NUMS -eq $EXPECTED ] ||
3868                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3869                               "found $NUMS, expected $EXPECTED"
3870         done
3871 }
3872 run_test 56h "check lfs find ! -name ============================="
3873
3874 test_56i() {
3875        tdir=${tdir}i
3876        test_mkdir -p $DIR/$tdir
3877        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3878        CMD="$LFIND -ost $UUID $DIR/$tdir"
3879        OUT=$($CMD)
3880        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3881 }
3882 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3883
3884 test_56j() {
3885         TDIR=$DIR/${tdir}g
3886         setup_56_special $NUMFILES $NUMDIRS
3887
3888         EXPECTED=$((NUMDIRS + 1))
3889         CMD="$LFIND -type d $TDIR"
3890         NUMS=$($CMD | wc -l)
3891         [ $NUMS -eq $EXPECTED ] ||
3892                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3893 }
3894 run_test 56j "check lfs find -type d ============================="
3895
3896 test_56k() {
3897         TDIR=$DIR/${tdir}g
3898         setup_56_special $NUMFILES $NUMDIRS
3899
3900         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3901         CMD="$LFIND -type f $TDIR"
3902         NUMS=$($CMD | wc -l)
3903         [ $NUMS -eq $EXPECTED ] ||
3904                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3905 }
3906 run_test 56k "check lfs find -type f ============================="
3907
3908 test_56l() {
3909         TDIR=$DIR/${tdir}g
3910         setup_56_special $NUMFILES $NUMDIRS
3911
3912         EXPECTED=$((NUMDIRS + NUMFILES))
3913         CMD="$LFIND -type b $TDIR"
3914         NUMS=$($CMD | wc -l)
3915         [ $NUMS -eq $EXPECTED ] ||
3916                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3917 }
3918 run_test 56l "check lfs find -type b ============================="
3919
3920 test_56m() {
3921         TDIR=$DIR/${tdir}g
3922         setup_56_special $NUMFILES $NUMDIRS
3923
3924         EXPECTED=$((NUMDIRS + NUMFILES))
3925         CMD="$LFIND -type c $TDIR"
3926         NUMS=$($CMD | wc -l)
3927         [ $NUMS -eq $EXPECTED ] ||
3928                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3929 }
3930 run_test 56m "check lfs find -type c ============================="
3931
3932 test_56n() {
3933         TDIR=$DIR/${tdir}g
3934         setup_56_special $NUMFILES $NUMDIRS
3935
3936         EXPECTED=$((NUMDIRS + NUMFILES))
3937         CMD="$LFIND -type l $TDIR"
3938         NUMS=$($CMD | wc -l)
3939         [ $NUMS -eq $EXPECTED ] ||
3940                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3941 }
3942 run_test 56n "check lfs find -type l ============================="
3943
3944 test_56o() {
3945         TDIR=$DIR/${tdir}o
3946         setup_56 $NUMFILES $NUMDIRS
3947
3948         utime $TDIR/file1 > /dev/null || error "utime (1)"
3949         utime $TDIR/file2 > /dev/null || error "utime (2)"
3950         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3951         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3952         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3953         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3954
3955         EXPECTED=4
3956         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3957         [ $NUMS -eq $EXPECTED ] || \
3958                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3959
3960         EXPECTED=12
3961         CMD="$LFIND -mtime 0 $TDIR"
3962         NUMS=$($CMD | wc -l)
3963         [ $NUMS -eq $EXPECTED ] ||
3964                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3965 }
3966 run_test 56o "check lfs find -mtime for old files =========================="
3967
3968 test_56p() {
3969         [ $RUNAS_ID -eq $UID ] &&
3970                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3971
3972         TDIR=$DIR/${tdir}p
3973         setup_56 $NUMFILES $NUMDIRS
3974
3975         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3976         EXPECTED=$NUMFILES
3977         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3978         NUMS=$($CMD | wc -l)
3979         [ $NUMS -eq $EXPECTED ] || \
3980                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3981
3982         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3983         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3984         NUMS=$($CMD | wc -l)
3985         [ $NUMS -eq $EXPECTED ] || \
3986                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3987 }
3988 run_test 56p "check lfs find -uid and ! -uid ==============================="
3989
3990 test_56q() {
3991         [ $RUNAS_ID -eq $UID ] &&
3992                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3993
3994         TDIR=$DIR/${tdir}q
3995         setup_56 $NUMFILES $NUMDIRS
3996
3997         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3998
3999         EXPECTED=$NUMFILES
4000         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4001         NUMS=$($CMD | wc -l)
4002         [ $NUMS -eq $EXPECTED ] ||
4003                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4004
4005         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4006         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4007         NUMS=$($CMD | wc -l)
4008         [ $NUMS -eq $EXPECTED ] ||
4009                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4010 }
4011 run_test 56q "check lfs find -gid and ! -gid ==============================="
4012
4013 test_56r() {
4014         TDIR=$DIR/${tdir}r
4015         setup_56 $NUMFILES $NUMDIRS
4016
4017         EXPECTED=12
4018         CMD="$LFIND -size 0 -type f $TDIR"
4019         NUMS=$($CMD | wc -l)
4020         [ $NUMS -eq $EXPECTED ] ||
4021                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4022         EXPECTED=0
4023         CMD="$LFIND ! -size 0 -type f $TDIR"
4024         NUMS=$($CMD | wc -l)
4025         [ $NUMS -eq $EXPECTED ] ||
4026                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4027         echo "test" > $TDIR/$tfile
4028         echo "test2" > $TDIR/$tfile.2 && sync
4029         EXPECTED=1
4030         CMD="$LFIND -size 5 -type f $TDIR"
4031         NUMS=$($CMD | wc -l)
4032         [ $NUMS -eq $EXPECTED ] ||
4033                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4034         EXPECTED=1
4035         CMD="$LFIND -size +5 -type f $TDIR"
4036         NUMS=$($CMD | wc -l)
4037         [ $NUMS -eq $EXPECTED ] ||
4038                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4039         EXPECTED=2
4040         CMD="$LFIND -size +0 -type f $TDIR"
4041         NUMS=$($CMD | wc -l)
4042         [ $NUMS -eq $EXPECTED ] ||
4043                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4044         EXPECTED=2
4045         CMD="$LFIND ! -size -5 -type f $TDIR"
4046         NUMS=$($CMD | wc -l)
4047         [ $NUMS -eq $EXPECTED ] ||
4048                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4049         EXPECTED=12
4050         CMD="$LFIND -size -5 -type f $TDIR"
4051         NUMS=$($CMD | wc -l)
4052         [ $NUMS -eq $EXPECTED ] ||
4053                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4054 }
4055 run_test 56r "check lfs find -size works =========================="
4056
4057 test_56s() { # LU-611
4058         TDIR=$DIR/${tdir}s
4059         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4060
4061         if [ $OSTCOUNT -gt 1 ]; then
4062                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4063                 ONESTRIPE=4
4064                 EXTRA=4
4065         else
4066                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4067                 EXTRA=0
4068         fi
4069
4070         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4071         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4072         NUMS=$($CMD | wc -l)
4073         [ $NUMS -eq $EXPECTED ] ||
4074                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4075
4076         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4077         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4078         NUMS=$($CMD | wc -l)
4079         [ $NUMS -eq $EXPECTED ] ||
4080                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4081
4082         EXPECTED=$ONESTRIPE
4083         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4084         NUMS=$($CMD | wc -l)
4085         [ $NUMS -eq $EXPECTED ] ||
4086                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4087
4088         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4089         NUMS=$($CMD | wc -l)
4090         [ $NUMS -eq $EXPECTED ] ||
4091                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4092
4093         EXPECTED=0
4094         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4095         NUMS=$($CMD | wc -l)
4096         [ $NUMS -eq $EXPECTED ] ||
4097                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4098 }
4099 run_test 56s "check lfs find -stripe-count works"
4100
4101 test_56t() { # LU-611
4102         TDIR=$DIR/${tdir}t
4103         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4104
4105         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4106
4107         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4108         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4109         NUMS=$($CMD | wc -l)
4110         [ $NUMS -eq $EXPECTED ] ||
4111                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4112
4113         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4114         NUMS=$($CMD | wc -l)
4115         [ $NUMS -eq $EXPECTED ] ||
4116                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4117
4118         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4119         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4120         NUMS=$($CMD | wc -l)
4121         [ $NUMS -eq $EXPECTED ] ||
4122                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4123
4124         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4125         NUMS=$($CMD | wc -l)
4126         [ $NUMS -eq $EXPECTED ] ||
4127                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4128
4129         EXPECTED=4
4130         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4131         NUMS=$($CMD | wc -l)
4132         [ $NUMS -eq $EXPECTED ] ||
4133                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4134
4135         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4136         NUMS=$($CMD | wc -l)
4137         [ $NUMS -eq $EXPECTED ] ||
4138                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4139
4140         EXPECTED=0
4141         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4142         NUMS=$($CMD | wc -l)
4143         [ $NUMS -eq $EXPECTED ] ||
4144                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4145 }
4146 run_test 56t "check lfs find -stripe-size works"
4147
4148 test_56u() { # LU-611
4149         TDIR=$DIR/${tdir}u
4150         setup_56 $NUMFILES $NUMDIRS "-i 0"
4151
4152         if [ $OSTCOUNT -gt 1 ]; then
4153                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4154                 ONESTRIPE=4
4155         else
4156                 ONESTRIPE=0
4157         fi
4158
4159         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4160         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4161         NUMS=$($CMD | wc -l)
4162         [ $NUMS -eq $EXPECTED ] ||
4163                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4164
4165         EXPECTED=$ONESTRIPE
4166         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4167         NUMS=$($CMD | wc -l)
4168         [ $NUMS -eq $EXPECTED ] ||
4169                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4170
4171         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4172         NUMS=$($CMD | wc -l)
4173         [ $NUMS -eq $EXPECTED ] ||
4174                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4175
4176         EXPECTED=0
4177         # This should produce an error and not return any files
4178         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4179         NUMS=$($CMD 2>/dev/null | wc -l)
4180         [ $NUMS -eq $EXPECTED ] ||
4181                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4182
4183         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4184         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4185         NUMS=$($CMD | wc -l)
4186         [ $NUMS -eq $EXPECTED ] ||
4187                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4188 }
4189 run_test 56u "check lfs find -stripe-index works"
4190
4191 test_56v() {
4192     local MDT_IDX=0
4193
4194     TDIR=$DIR/${tdir}v
4195     rm -rf $TDIR
4196     setup_56 $NUMFILES $NUMDIRS
4197
4198     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4199     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4200
4201     for file in $($LFIND -mdt $UUID $TDIR); do
4202         file_mdt_idx=$($GETSTRIPE -M $file)
4203         [ $file_mdt_idx -eq $MDT_IDX ] ||
4204             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4205     done
4206 }
4207 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4208
4209 # Get and check the actual stripe count of one file.
4210 # Usage: check_stripe_count <file> <expected_stripe_count>
4211 check_stripe_count() {
4212     local file=$1
4213     local expected=$2
4214     local actual
4215
4216     [[ -z "$file" || -z "$expected" ]] &&
4217         error "check_stripe_count: invalid argument!"
4218
4219     local cmd="$GETSTRIPE -c $file"
4220     actual=$($cmd) || error "$cmd failed"
4221     actual=${actual%% *}
4222
4223     if [[ $actual -ne $expected ]]; then
4224         [[ $expected -eq -1 ]] ||
4225             error "$cmd wrong: found $actual, expected $expected"
4226         [[ $actual -eq $OSTCOUNT ]] ||
4227             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4228     fi
4229 }
4230
4231 test_56w() {
4232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4233         TDIR=$DIR/${tdir}w
4234
4235     rm -rf $TDIR || error "remove $TDIR failed"
4236     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4237
4238     local stripe_size
4239     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4240         error "$GETSTRIPE -S -d $TDIR failed"
4241     stripe_size=${stripe_size%% *}
4242
4243     local file_size=$((stripe_size * OSTCOUNT))
4244     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4245     local required_space=$((file_num * file_size))
4246     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4247     [[ $free_space -le $((required_space / 1024)) ]] &&
4248         skip_env "need at least $required_space bytes free space," \
4249                  "have $free_space kbytes" && return
4250
4251     local dd_bs=65536
4252     local dd_count=$((file_size / dd_bs))
4253
4254     # write data into the files
4255     local i
4256     local j
4257     local file
4258     for i in $(seq 1 $NUMFILES); do
4259         file=$TDIR/file$i
4260         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4261             error "write data into $file failed"
4262     done
4263     for i in $(seq 1 $NUMDIRS); do
4264         for j in $(seq 1 $NUMFILES); do
4265             file=$TDIR/dir$i/file$j
4266             yes | dd bs=$dd_bs count=$dd_count of=$file \
4267                 >/dev/null 2>&1 ||
4268                 error "write data into $file failed"
4269         done
4270     done
4271
4272     local expected=-1
4273     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4274
4275     # lfs_migrate file
4276     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4277     echo "$cmd"
4278     eval $cmd || error "$cmd failed"
4279
4280     check_stripe_count $TDIR/file1 $expected
4281
4282     # lfs_migrate dir
4283     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4284     echo "$cmd"
4285     eval $cmd || error "$cmd failed"
4286
4287     for j in $(seq 1 $NUMFILES); do
4288         check_stripe_count $TDIR/dir1/file$j $expected
4289     done
4290
4291     # lfs_migrate works with lfs find
4292     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4293          $LFS_MIGRATE -y -c $expected"
4294     echo "$cmd"
4295     eval $cmd || error "$cmd failed"
4296
4297     for i in $(seq 2 $NUMFILES); do
4298         check_stripe_count $TDIR/file$i $expected
4299     done
4300     for i in $(seq 2 $NUMDIRS); do
4301         for j in $(seq 1 $NUMFILES); do
4302             check_stripe_count $TDIR/dir$i/file$j $expected
4303         done
4304     done
4305 }
4306 run_test 56w "check lfs_migrate -c stripe_count works"
4307
4308 test_57a() {
4309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4310         # note test will not do anything if MDS is not local
4311         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4312                 skip "Only applicable to ldiskfs-based MDTs"
4313                 return
4314         fi
4315
4316         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4317         local MNTDEV="osd*.*MDT*.mntdev"
4318         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4319         [ -z "$DEV" ] && error "can't access $MNTDEV"
4320         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4321                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4322                         error "can't access $DEV"
4323                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4324                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4325                 rm $TMP/t57a.dump
4326         done
4327 }
4328 run_test 57a "verify MDS filesystem created with large inodes =="
4329
4330 test_57b() {
4331         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4332         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4333                 skip "Only applicable to ldiskfs-based MDTs"
4334                 return
4335         fi
4336
4337         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4338         local dir=$DIR/d57b
4339
4340         local FILECOUNT=100
4341         local FILE1=$dir/f1
4342         local FILEN=$dir/f$FILECOUNT
4343
4344         rm -rf $dir || error "removing $dir"
4345         test_mkdir -p $dir || error "creating $dir"
4346         local num=$(get_mds_dir $dir)
4347         local mymds=mds$num
4348
4349         echo "mcreating $FILECOUNT files"
4350         createmany -m $dir/f 1 $FILECOUNT || \
4351                 error "creating files in $dir"
4352
4353         # verify that files do not have EAs yet
4354         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4355         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4356
4357         sync
4358         sleep 1
4359         df $dir  #make sure we get new statfs data
4360         local MDSFREE=$(do_facet $mymds \
4361                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4362         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4363         echo "opening files to create objects/EAs"
4364         local FILE
4365         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4366                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4367         done
4368
4369         # verify that files have EAs now
4370         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4371         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4372
4373         sleep 1  #make sure we get new statfs data
4374         df $dir
4375         local MDSFREE2=$(do_facet $mymds \
4376                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4377         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4378         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4379                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4380                         error "MDC before $MDCFREE != after $MDCFREE2"
4381                 else
4382                         echo "MDC before $MDCFREE != after $MDCFREE2"
4383                         echo "unable to confirm if MDS has large inodes"
4384                 fi
4385         fi
4386         rm -rf $dir
4387 }
4388 run_test 57b "default LOV EAs are stored inside large inodes ==="
4389
4390 test_58() {
4391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4392         [ -z "$(which wiretest 2>/dev/null)" ] &&
4393                         skip_env "could not find wiretest" && return
4394         wiretest
4395 }
4396 run_test 58 "verify cross-platform wire constants =============="
4397
4398 test_59() {
4399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4400         echo "touch 130 files"
4401         createmany -o $DIR/f59- 130
4402         echo "rm 130 files"
4403         unlinkmany $DIR/f59- 130
4404         sync
4405         # wait for commitment of removal
4406         wait_delete_completed
4407 }
4408 run_test 59 "verify cancellation of llog records async ========="
4409
4410 TEST60_HEAD="test_60 run $RANDOM"
4411 test_60a() {
4412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4413         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4414         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4415         log "$TEST60_HEAD - from kernel mode"
4416         do_facet mgs sh run-llog.sh
4417 }
4418 run_test 60a "llog sanity tests run from kernel module =========="
4419
4420 test_60b() { # bug 6411
4421         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4422         dmesg > $DIR/$tfile
4423         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4424                                  /llog.test/ {
4425                                          if (marker)
4426                                                  from_marker++
4427                                          from_begin++
4428                                  }
4429                                  END {
4430                                          if (marker)
4431                                                  print from_marker
4432                                          else
4433                                                  print from_begin
4434                                  }"`
4435         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4436 }
4437 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4438
4439 test_60c() {
4440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4441         echo "create 5000 files"
4442         createmany -o $DIR/f60c- 5000
4443 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4444         lctl set_param fail_loc=0x80000137
4445         unlinkmany $DIR/f60c- 5000
4446         lctl set_param fail_loc=0
4447 }
4448 run_test 60c "unlink file when mds full"
4449
4450 test_60d() {
4451         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4452         SAVEPRINTK=$(lctl get_param -n printk)
4453
4454         # verify "lctl mark" is even working"
4455         MESSAGE="test message ID $RANDOM $$"
4456         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4457         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4458
4459         lctl set_param printk=0 || error "set lnet.printk failed"
4460         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4461         MESSAGE="new test message ID $RANDOM $$"
4462         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4463         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4464         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4465
4466         lctl set_param -n printk="$SAVEPRINTK"
4467 }
4468 run_test 60d "test printk console message masking"
4469
4470 test_61() {
4471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4472         f="$DIR/f61"
4473         dd if=/dev/zero of=$f bs=`page_size` count=1
4474         cancel_lru_locks osc
4475         $MULTIOP $f OSMWUc || error
4476         sync
4477 }
4478 run_test 61 "mmap() writes don't make sync hang ================"
4479
4480 # bug 2330 - insufficient obd_match error checking causes LBUG
4481 test_62() {
4482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4483         f="$DIR/f62"
4484         echo foo > $f
4485         cancel_lru_locks osc
4486         lctl set_param fail_loc=0x405
4487         cat $f && error "cat succeeded, expect -EIO"
4488         lctl set_param fail_loc=0
4489 }
4490 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4491 # match every page all of the time.
4492 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4493
4494 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4495 test_63a() {    # was test_63
4496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4497         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4498         lctl set_param -n osc.*.max_dirty_mb 0
4499         for i in `seq 10` ; do
4500                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4501                 sleep 5
4502                 kill $!
4503                 sleep 1
4504         done
4505
4506         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4507         rm -f $DIR/f63 || true
4508 }
4509 run_test 63a "Verify oig_wait interruption does not crash ======="
4510
4511 # bug 2248 - async write errors didn't return to application on sync
4512 # bug 3677 - async write errors left page locked
4513 test_63b() {
4514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4515         debugsave
4516         lctl set_param debug=-1
4517
4518         # ensure we have a grant to do async writes
4519         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4520         rm $DIR/$tfile
4521
4522         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4523         lctl set_param fail_loc=0x80000406
4524         $MULTIOP $DIR/$tfile Owy && \
4525                 error "sync didn't return ENOMEM"
4526         sync; sleep 2; sync     # do a real sync this time to flush page
4527         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4528                 error "locked page left in cache after async error" || true
4529         debugrestore
4530 }
4531 run_test 63b "async write errors should be returned to fsync ==="
4532
4533 test_64a () {
4534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4535         df $DIR
4536         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4537 }
4538 run_test 64a "verify filter grant calculations (in kernel) ====="
4539
4540 test_64b () {
4541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4542         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4543         sh oos.sh $MOUNT
4544 }
4545 run_test 64b "check out-of-space detection on client ==========="
4546
4547 test_64c() {
4548         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4549 }
4550 run_test 64c "verify grant shrink ========================------"
4551
4552 # bug 1414 - set/get directories' stripe info
4553 test_65a() {
4554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4555         test_mkdir -p $DIR/$tdir
4556         touch $DIR/$tdir/f1
4557         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4558 }
4559 run_test 65a "directory with no stripe info ===================="
4560
4561 test_65b() {
4562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4563         test_mkdir -p $DIR/$tdir
4564         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4565                                                 error "setstripe"
4566         touch $DIR/$tdir/f2
4567         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4568 }
4569 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4570
4571 test_65c() {
4572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4573         if [ $OSTCOUNT -gt 1 ]; then
4574                 test_mkdir -p $DIR/$tdir
4575                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4576                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4577                 touch $DIR/$tdir/f3
4578                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4579         fi
4580 }
4581 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4582
4583 test_65d() {
4584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4585         test_mkdir -p $DIR/$tdir
4586         if [ $STRIPECOUNT -le 0 ]; then
4587                 sc=1
4588         elif [ $STRIPECOUNT -gt 2000 ]; then
4589 #LOV_MAX_STRIPE_COUNT is 2000
4590                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4591         else
4592                 sc=$(($STRIPECOUNT - 1))
4593         fi
4594         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4595         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4596         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4597                                                 error "lverify failed"
4598 }
4599 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4600
4601 test_65e() {
4602         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4603         test_mkdir -p $DIR/$tdir
4604
4605         $SETSTRIPE $DIR/$tdir || error "setstripe"
4606         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4607                                         error "no stripe info failed"
4608         touch $DIR/$tdir/f6
4609         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4610 }
4611 run_test 65e "directory setstripe defaults ======================="
4612
4613 test_65f() {
4614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4615         test_mkdir -p $DIR/${tdir}f
4616         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4617 }
4618 run_test 65f "dir setstripe permission (should return error) ==="
4619
4620 test_65g() {
4621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4622         test_mkdir -p $DIR/$tdir
4623         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4624                                                         error "setstripe"
4625         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4626         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4627                 error "delete default stripe failed"
4628 }
4629 run_test 65g "directory setstripe -d ==========================="
4630
4631 test_65h() {
4632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4633         test_mkdir -p $DIR/$tdir
4634         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4635                                                         error "setstripe"
4636         test_mkdir -p $DIR/$tdir/dd1
4637         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4638                 error "stripe info inherit failed"
4639 }
4640 run_test 65h "directory stripe info inherit ===================="
4641
4642 test_65i() { # bug6367
4643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4644         $SETSTRIPE -S 65536 -c -1 $MOUNT
4645 }
4646 run_test 65i "set non-default striping on root directory (bug 6367)="
4647
4648 test_65ia() { # bug12836
4649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4650         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4651 }
4652 run_test 65ia "getstripe on -1 default directory striping"
4653
4654 test_65ib() { # bug12836
4655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4656         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4657 }
4658 run_test 65ib "getstripe -v on -1 default directory striping"
4659
4660 test_65ic() { # bug12836
4661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4662         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4663 }
4664 run_test 65ic "new find on -1 default directory striping"
4665
4666 test_65j() { # bug6367
4667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4668         sync; sleep 1
4669         # if we aren't already remounting for each test, do so for this test
4670         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4671                 cleanup || error "failed to unmount"
4672                 setup
4673         fi
4674         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4675 }
4676 run_test 65j "set default striping on root directory (bug 6367)="
4677
4678 test_65k() { # bug11679
4679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4680         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4681         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4682
4683     echo "Check OST status: "
4684     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4685               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4686
4687     for OSC in $MDS_OSCS; do
4688         echo $OSC "is activate"
4689         do_facet $SINGLEMDS lctl --device %$OSC activate
4690     done
4691
4692     mkdir -p $DIR/$tdir
4693     for INACTIVE_OSC in $MDS_OSCS; do
4694         echo "Deactivate: " $INACTIVE_OSC
4695         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4696         for STRIPE_OSC in $MDS_OSCS; do
4697             OST=`osc_to_ost $STRIPE_OSC`
4698             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4699                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4700
4701             [ -f $DIR/$tdir/$IDX ] && continue
4702             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4703             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4704             RC=$?
4705             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4706         done
4707         rm -f $DIR/$tdir/*
4708         echo $INACTIVE_OSC "is Activate."
4709         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4710     done
4711 }
4712 run_test 65k "validate manual striping works properly with deactivated OSCs"
4713
4714 test_65l() { # bug 12836
4715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4716         test_mkdir -p $DIR/$tdir/test_dir
4717         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4718         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4719 }
4720 run_test 65l "lfs find on -1 stripe dir ========================"
4721
4722 # bug 2543 - update blocks count on client
4723 test_66() {
4724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4725         COUNT=${COUNT:-8}
4726         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4727         sync; sync_all_data; sync; sync_all_data
4728         cancel_lru_locks osc
4729         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4730         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4731 }
4732 run_test 66 "update inode blocks count on client ==============="
4733
4734 LLOOP=
4735 LLITELOOPLOAD=
4736 cleanup_68() {
4737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4738         trap 0
4739         if [ ! -z "$LLOOP" ]; then
4740                 if swapon -s | grep -q $LLOOP; then
4741                         swapoff $LLOOP || error "swapoff failed"
4742                 fi
4743
4744                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4745                 rm -f $LLOOP
4746                 unset LLOOP
4747         fi
4748         if [ ! -z "$LLITELOOPLOAD" ]; then
4749                 rmmod llite_lloop
4750                 unset LLITELOOPLOAD
4751         fi
4752         rm -f $DIR/f68*
4753 }
4754
4755 meminfo() {
4756         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4757 }
4758
4759 swap_used() {
4760         swapon -s | awk '($1 == "'$1'") { print $4 }'
4761 }
4762
4763 # test case for lloop driver, basic function
4764 test_68a() {
4765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4766         [ "$UID" != 0 ] && skip_env "must run as root" && return
4767         llite_lloop_enabled || \
4768                 { skip_env "llite_lloop module disabled" && return; }
4769
4770         trap cleanup_68 EXIT
4771
4772         if ! module_loaded llite_lloop; then
4773                 if load_module llite/llite_lloop; then
4774                         LLITELOOPLOAD=yes
4775                 else
4776                         skip_env "can't find module llite_lloop"
4777                         return
4778                 fi
4779         fi
4780
4781         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4782         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4783         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4784
4785         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4786         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4787
4788         cleanup_68
4789 }
4790 run_test 68a "lloop driver - basic test ========================"
4791
4792 # excercise swapping to lustre by adding a high priority swapfile entry
4793 # and then consuming memory until it is used.
4794 test_68b() {  # was test_68
4795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4796         [ "$UID" != 0 ] && skip_env "must run as root" && return
4797         lctl get_param -n devices | grep -q obdfilter && \
4798                 skip "local OST" && return
4799
4800         grep -q llite_lloop /proc/modules
4801         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4802
4803         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4804                 skip "can't reliably test swap with TCP" && return
4805
4806         MEMTOTAL=`meminfo MemTotal`
4807         NR_BLOCKS=$((MEMTOTAL>>8))
4808         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4809
4810         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4811         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4812         mkswap $DIR/f68b
4813
4814         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4815
4816         trap cleanup_68 EXIT
4817
4818         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4819
4820         echo "before: `swapon -s | grep $LLOOP`"
4821         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4822         echo "after: `swapon -s | grep $LLOOP`"
4823         SWAPUSED=`swap_used $LLOOP`
4824
4825         cleanup_68
4826
4827         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4828 }
4829 run_test 68b "support swapping to Lustre ========================"
4830
4831 # bug5265, obdfilter oa2dentry return -ENOENT
4832 # #define OBD_FAIL_OST_ENOENT 0x217
4833 test_69() {
4834         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4835         remote_ost_nodsh && skip "remote OST with nodsh" && return
4836
4837         f="$DIR/$tfile"
4838         $SETSTRIPE -c 1 -i 0 $f
4839
4840         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4841
4842         do_facet ost1 lctl set_param fail_loc=0x217
4843         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4844         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4845
4846         do_facet ost1 lctl set_param fail_loc=0
4847         $DIRECTIO write $f 0 2 || error "write error"
4848
4849         cancel_lru_locks osc
4850         $DIRECTIO read $f 0 1 || error "read error"
4851
4852         do_facet ost1 lctl set_param fail_loc=0x217
4853         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4854
4855         do_facet ost1 lctl set_param fail_loc=0
4856         rm -f $f
4857 }
4858 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4859
4860 test_71() {
4861     test_mkdir -p $DIR/$tdir
4862     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4863 }
4864 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4865
4866 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4868         check_kernel_version 43 || return 0
4869         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4870
4871         # Check that testing environment is properly set up. Skip if not
4872         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4873                 skip_env "User $RUNAS_ID does not exist - skipping"
4874                 return 0
4875         }
4876         # We had better clear the $DIR to get enough space for dd
4877         rm -rf $DIR/*
4878         touch $DIR/f72
4879         chmod 777 $DIR/f72
4880         chmod ug+s $DIR/f72
4881         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4882         # See if we are still setuid/sgid
4883         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4884         # Now test that MDS is updated too
4885         cancel_lru_locks mdc
4886         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4887         rm -f $DIR/f72
4888 }
4889 run_test 72a "Test that remove suid works properly (bug5695) ===="
4890
4891 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4892         local perm
4893
4894         [ "$RUNAS_ID" = "$UID" ] && \
4895                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4896         [ "$RUNAS_ID" -eq 0 ] && \
4897                 skip_env "RUNAS_ID = 0 -- skipping" && return
4898
4899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4900         # Check that testing environment is properly set up. Skip if not
4901         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4902                 skip_env "User $RUNAS_ID does not exist - skipping"
4903                 return 0
4904         }
4905         touch $DIR/${tfile}-f{g,u}
4906         test_mkdir $DIR/${tfile}-dg
4907         test_mkdir $DIR/${tfile}-du
4908         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4909         chmod g+s $DIR/${tfile}-{f,d}g
4910         chmod u+s $DIR/${tfile}-{f,d}u
4911         for perm in 777 2777 4777; do
4912                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4913                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4914                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4915                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4916         done
4917         true
4918 }
4919 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4920
4921 # bug 3462 - multiple simultaneous MDC requests
4922 test_73() {
4923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4924         test_mkdir $DIR/d73-1
4925         test_mkdir $DIR/d73-2
4926         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4927         pid1=$!
4928
4929         lctl set_param fail_loc=0x80000129
4930         $MULTIOP $DIR/d73-1/f73-2 Oc &
4931         sleep 1
4932         lctl set_param fail_loc=0
4933
4934         $MULTIOP $DIR/d73-2/f73-3 Oc &
4935         pid3=$!
4936
4937         kill -USR1 $pid1
4938         wait $pid1 || return 1
4939
4940         sleep 25
4941
4942         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4943         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4944         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4945
4946         rm -rf $DIR/d73-*
4947 }
4948 run_test 73 "multiple MDC requests (should not deadlock)"
4949
4950 test_74a() { # bug 6149, 6184
4951         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4952         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4953         #
4954         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4955         # will spin in a tight reconnection loop
4956         touch $DIR/f74a
4957         lctl set_param fail_loc=0x8000030e
4958         # get any lock that won't be difficult - lookup works.
4959         ls $DIR/f74a
4960         lctl set_param fail_loc=0
4961         true
4962         rm -f $DIR/f74a
4963 }
4964 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4965
4966 test_74b() { # bug 13310
4967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4968         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4969         #
4970         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4971         # will spin in a tight reconnection loop
4972         lctl set_param fail_loc=0x8000030e
4973         # get a "difficult" lock
4974         touch $DIR/f74b
4975         lctl set_param fail_loc=0
4976         true
4977         rm -f $DIR/f74b
4978 }
4979 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4980
4981 test_74c() {
4982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4983 #define OBD_FAIL_LDLM_NEW_LOCK
4984         lctl set_param fail_loc=0x80000319
4985         touch $DIR/$tfile && error "Touch successful"
4986         true
4987 }
4988 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4989
4990 num_inodes() {
4991         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4992 }
4993
4994 get_inode_slab_tunables() {
4995         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4996 }
4997
4998 set_inode_slab_tunables() {
4999         echo "lustre_inode_cache $1" > /proc/slabinfo
5000 }
5001
5002 test_76() { # Now for bug 20433, added originally in bug 1443
5003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5004         local SLAB_SETTINGS=`get_inode_slab_tunables`
5005         local CPUS=`getconf _NPROCESSORS_ONLN`
5006         # we cannot set limit below 1 which means 1 inode in each
5007         # per-cpu cache is still allowed
5008         set_inode_slab_tunables "1 1 0"
5009         cancel_lru_locks osc
5010         BEFORE_INODES=`num_inodes`
5011         echo "before inodes: $BEFORE_INODES"
5012         local COUNT=1000
5013         [ "$SLOW" = "no" ] && COUNT=100
5014         for i in `seq $COUNT`; do
5015                 touch $DIR/$tfile
5016                 rm -f $DIR/$tfile
5017         done
5018         cancel_lru_locks osc
5019         AFTER_INODES=`num_inodes`
5020         echo "after inodes: $AFTER_INODES"
5021         local wait=0
5022         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5023                 sleep 2
5024                 AFTER_INODES=`num_inodes`
5025                 wait=$((wait+2))
5026                 echo "wait $wait seconds inodes: $AFTER_INODES"
5027                 if [ $wait -gt 30 ]; then
5028                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5029                 fi
5030         done
5031         set_inode_slab_tunables "$SLAB_SETTINGS"
5032 }
5033 run_test 76 "confirm clients recycle inodes properly ===="
5034
5035
5036 export ORIG_CSUM=""
5037 set_checksums()
5038 {
5039         # Note: in sptlrpc modes which enable its own bulk checksum, the
5040         # original crc32_le bulk checksum will be automatically disabled,
5041         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5042         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5043         # In this case set_checksums() will not be no-op, because sptlrpc
5044         # bulk checksum will be enabled all through the test.
5045
5046         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5047         lctl set_param -n osc.*.checksums $1
5048         return 0
5049 }
5050
5051 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5052                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5053 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5054 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5055 set_checksum_type()
5056 {
5057         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5058         log "set checksum type to $1"
5059         return 0
5060 }
5061 F77_TMP=$TMP/f77-temp
5062 F77SZ=8
5063 setup_f77() {
5064         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5065                 error "error writing to $F77_TMP"
5066 }
5067
5068 test_77a() { # bug 10889
5069         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5070         $GSS && skip "could not run with gss" && return
5071         [ ! -f $F77_TMP ] && setup_f77
5072         set_checksums 1
5073         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5074         set_checksums 0
5075         rm -f $DIR/$tfile
5076 }
5077 run_test 77a "normal checksum read/write operation ============="
5078
5079 test_77b() { # bug 10889
5080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5081         $GSS && skip "could not run with gss" && return
5082         [ ! -f $F77_TMP ] && setup_f77
5083         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5084         lctl set_param fail_loc=0x80000409
5085         set_checksums 1
5086         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
5087                 error "dd error: $?"
5088         lctl set_param fail_loc=0
5089         set_checksums 0
5090 }
5091 run_test 77b "checksum error on client write ===================="
5092
5093 test_77c() { # bug 10889
5094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5095         $GSS && skip "could not run with gss" && return
5096         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
5097         set_checksums 1
5098         for algo in $CKSUM_TYPES; do
5099                 cancel_lru_locks osc
5100                 set_checksum_type $algo
5101                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5102                 lctl set_param fail_loc=0x80000408
5103                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
5104                 lctl set_param fail_loc=0
5105         done
5106         set_checksums 0
5107         set_checksum_type $ORIG_CSUM_TYPE
5108         rm -f $DIR/f77b
5109 }
5110 run_test 77c "checksum error on client read ==================="
5111
5112 test_77d() { # bug 10889
5113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5114         $GSS && skip "could not run with gss" && return
5115         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5116         lctl set_param fail_loc=0x80000409
5117         set_checksums 1
5118         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5119                 error "direct write: rc=$?"
5120         lctl set_param fail_loc=0
5121         set_checksums 0
5122 }
5123 run_test 77d "checksum error on OST direct write ==============="
5124
5125 test_77e() { # bug 10889
5126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5127         $GSS && skip "could not run with gss" && return
5128         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5129         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5130         lctl set_param fail_loc=0x80000408
5131         set_checksums 1
5132         cancel_lru_locks osc
5133         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5134                 error "direct read: rc=$?"
5135         lctl set_param fail_loc=0
5136         set_checksums 0
5137 }
5138 run_test 77e "checksum error on OST direct read ================"
5139
5140 test_77f() { # bug 10889
5141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5142         $GSS && skip "could not run with gss" && return
5143         set_checksums 1
5144         for algo in $CKSUM_TYPES; do
5145                 cancel_lru_locks osc
5146                 set_checksum_type $algo
5147                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5148                 lctl set_param fail_loc=0x409
5149                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5150                         error "direct write succeeded"
5151                 lctl set_param fail_loc=0
5152         done
5153         set_checksum_type $ORIG_CSUM_TYPE
5154         set_checksums 0
5155 }
5156 run_test 77f "repeat checksum error on write (expect error) ===="
5157
5158 test_77g() { # bug 10889
5159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5160         $GSS && skip "could not run with gss" && return
5161         remote_ost_nodsh && skip "remote OST with nodsh" && return
5162
5163         [ ! -f $F77_TMP ] && setup_f77
5164
5165         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5166         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5167         do_facet ost1 lctl set_param fail_loc=0x8000021a
5168         set_checksums 1
5169         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5170                 error "write error: rc=$?"
5171         do_facet ost1 lctl set_param fail_loc=0
5172         set_checksums 0
5173 }
5174 run_test 77g "checksum error on OST write ======================"
5175
5176 test_77h() { # bug 10889
5177         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5178         $GSS && skip "could not run with gss" && return
5179         remote_ost_nodsh && skip "remote OST with nodsh" && return
5180
5181         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5182         cancel_lru_locks osc
5183         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5184         do_facet ost1 lctl set_param fail_loc=0x8000021b
5185         set_checksums 1
5186         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5187         do_facet ost1 lctl set_param fail_loc=0
5188         set_checksums 0
5189 }
5190 run_test 77h "checksum error on OST read ======================="
5191
5192 test_77i() { # bug 13805
5193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5194         $GSS && skip "could not run with gss" && return
5195         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5196         lctl set_param fail_loc=0x40b
5197         remount_client $MOUNT
5198         lctl set_param fail_loc=0
5199         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5200                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5201                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5202                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5203         done
5204         remount_client $MOUNT
5205 }
5206 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5207
5208 test_77j() { # bug 13805
5209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5210         $GSS && skip "could not run with gss" && return
5211         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5212         lctl set_param fail_loc=0x40c
5213         remount_client $MOUNT
5214         lctl set_param fail_loc=0
5215         sleep 2 # wait async osc connect to finish
5216         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5217                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5218                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5219                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5220         done
5221         remount_client $MOUNT
5222 }
5223 run_test 77j "client only supporting ADLER32 ===================="
5224
5225 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5226 rm -f $F77_TMP
5227 unset F77_TMP
5228
5229 test_78() { # bug 10901
5230         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5231         remote_ost || { skip_env "local OST" && return; }
5232
5233         NSEQ=5
5234         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5235         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5236         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5237         echo "MemTotal: $MEMTOTAL"
5238 # reserve 256MB of memory for the kernel and other running processes,
5239 # and then take 1/2 of the remaining memory for the read/write buffers.
5240     if [ $MEMTOTAL -gt 512 ] ;then
5241         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5242     else
5243         # for those poor memory-starved high-end clusters...
5244         MEMTOTAL=$((MEMTOTAL / 2))
5245     fi
5246         echo "Mem to use for directio: $MEMTOTAL"
5247         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5248         [ $F78SIZE -gt 512 ] && F78SIZE=512
5249         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5250         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5251         echo "Smallest OST: $SMALLESTOST"
5252         [ $SMALLESTOST -lt 10240 ] && \
5253                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5254
5255         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5256                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5257
5258         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5259         echo "File size: $F78SIZE"
5260         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5261         for i in `seq 1 $NSEQ`
5262         do
5263                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5264                 echo directIO rdwr round $i of $NSEQ
5265                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5266         done
5267
5268         rm -f $DIR/$tfile
5269 }
5270 run_test 78 "handle large O_DIRECT writes correctly ============"
5271
5272 test_79() { # bug 12743
5273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5274         wait_delete_completed
5275
5276         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5277         BKFREE=$(calc_osc_kbytes kbytesfree)
5278         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5279
5280         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5281         DFTOTAL=`echo $STRING | cut -d, -f1`
5282         DFUSED=`echo $STRING  | cut -d, -f2`
5283         DFAVAIL=`echo $STRING | cut -d, -f3`
5284         DFFREE=$(($DFTOTAL - $DFUSED))
5285
5286         ALLOWANCE=$((64 * $OSTCOUNT))
5287
5288         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5289            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5290                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5291         fi
5292         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5293            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5294                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5295         fi
5296         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5297            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5298                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5299         fi
5300 }
5301 run_test 79 "df report consistency check ======================="
5302
5303 test_80() { # bug 10718
5304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5305         # relax strong synchronous semantics for slow backends like ZFS
5306         local soc="obdfilter.*.sync_on_lock_cancel"
5307         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5308         local hosts=
5309         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5310                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5311                           facet_active_host $host; done | sort -u)
5312                 do_nodes $hosts lctl set_param $soc=never
5313         fi
5314
5315         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5316         sync; sleep 1; sync
5317         local BEFORE=`date +%s`
5318         cancel_lru_locks osc
5319         local AFTER=`date +%s`
5320         local DIFF=$((AFTER-BEFORE))
5321         if [ $DIFF -gt 1 ] ; then
5322                 error "elapsed for 1M@1T = $DIFF"
5323         fi
5324
5325         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5326
5327         rm -f $DIR/$tfile
5328 }
5329 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5330
5331 test_81a() { # LU-456
5332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5333         remote_ost_nodsh && skip "remote OST with nodsh" && return
5334         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5335         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5336         do_facet ost1 lctl set_param fail_loc=0x80000228
5337
5338         # write should trigger a retry and success
5339         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5340         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5341         RC=$?
5342         if [ $RC -ne 0 ] ; then
5343                 error "write should success, but failed for $RC"
5344         fi
5345 }
5346 run_test 81a "OST should retry write when get -ENOSPC ==============="
5347
5348 test_81b() { # LU-456
5349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5350         remote_ost_nodsh && skip "remote OST with nodsh" && return
5351         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5352         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5353         do_facet ost1 lctl set_param fail_loc=0x228
5354
5355         # write should retry several times and return -ENOSPC finally
5356         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5357         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5358         RC=$?
5359         ENOSPC=28
5360         if [ $RC -ne $ENOSPC ] ; then
5361                 error "dd should fail for -ENOSPC, but succeed."
5362         fi
5363 }
5364 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5365
5366 test_82() { # LU-1031
5367         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5368         local gid1=14091995
5369         local gid2=16022000
5370
5371         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5372         local MULTIPID1=$!
5373         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5374         local MULTIPID2=$!
5375         kill -USR1 $MULTIPID2
5376         sleep 2
5377         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5378                 error "First grouplock does not block second one"
5379         else
5380                 echo "Second grouplock blocks first one"
5381         fi
5382         kill -USR1 $MULTIPID1
5383         wait $MULTIPID1
5384         wait $MULTIPID2
5385 }
5386 run_test 82 "Basic grouplock test ==============================="
5387
5388 test_99a() {
5389         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5390             return
5391         test_mkdir -p $DIR/d99cvsroot
5392         chown $RUNAS_ID $DIR/d99cvsroot
5393         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5394         cd $TMP
5395
5396         $RUNAS cvs -d $DIR/d99cvsroot init || error
5397         cd $oldPWD
5398 }
5399 run_test 99a "cvs init ========================================="
5400
5401 test_99b() {
5402         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5403         [ ! -d $DIR/d99cvsroot ] && test_99a
5404         cd /etc/init.d
5405         # some versions of cvs import exit(1) when asked to import links or
5406         # files they can't read.  ignore those files.
5407         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5408                         ! -perm +4 -printf '-I %f\n')
5409         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5410                 d99reposname vtag rtag
5411 }
5412 run_test 99b "cvs import ======================================="
5413
5414 test_99c() {
5415         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5416         [ ! -d $DIR/d99cvsroot ] && test_99b
5417         cd $DIR
5418         test_mkdir -p $DIR/d99reposname
5419         chown $RUNAS_ID $DIR/d99reposname
5420         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5421 }
5422 run_test 99c "cvs checkout ====================================="
5423
5424 test_99d() {
5425         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5426         [ ! -d $DIR/d99cvsroot ] && test_99c
5427         cd $DIR/d99reposname
5428         $RUNAS touch foo99
5429         $RUNAS cvs add -m 'addmsg' foo99
5430 }
5431 run_test 99d "cvs add =========================================="
5432
5433 test_99e() {
5434         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5435         [ ! -d $DIR/d99cvsroot ] && test_99c
5436         cd $DIR/d99reposname
5437         $RUNAS cvs update
5438 }
5439 run_test 99e "cvs update ======================================="
5440
5441 test_99f() {
5442         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5443         [ ! -d $DIR/d99cvsroot ] && test_99d
5444         cd $DIR/d99reposname
5445         $RUNAS cvs commit -m 'nomsg' foo99
5446     rm -fr $DIR/d99cvsroot
5447 }
5448 run_test 99f "cvs commit ======================================="
5449
5450 test_100() {
5451         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5452         [ "$NETTYPE" = tcp ] || \
5453                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5454                         return ; }
5455
5456         remote_ost_nodsh && skip "remote OST with nodsh" && return
5457         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5458         remote_servers || \
5459                 { skip "useless for local single node setup" && return; }
5460
5461         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5462                 [ "$PROT" != "tcp" ] && continue
5463                 RPORT=$(echo $REMOTE | cut -d: -f2)
5464                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5465
5466                 rc=0
5467                 LPORT=`echo $LOCAL | cut -d: -f2`
5468                 if [ $LPORT -ge 1024 ]; then
5469                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5470                         netstat -tna
5471                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5472                 fi
5473         done
5474         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5475 }
5476 run_test 100 "check local port using privileged port ==========="
5477
5478 function get_named_value()
5479 {
5480     local tag
5481
5482     tag=$1
5483     while read ;do
5484         line=$REPLY
5485         case $line in
5486         $tag*)
5487             echo $line | sed "s/^$tag[ ]*//"
5488             break
5489             ;;
5490         esac
5491     done
5492 }
5493
5494 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5495 cleanup_101a() {
5496         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5497         trap 0
5498 }
5499
5500 test_101a() {
5501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5502         local s
5503         local discard
5504         local nreads=10000
5505         [ "$CPU" = "UML" ] && nreads=1000
5506         local cache_limit=32
5507
5508         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5509         trap cleanup_101a EXIT
5510         $LCTL set_param -n llite.*.read_ahead_stats 0
5511         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5512
5513         #
5514         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5515         #
5516         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5517         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5518
5519         discard=0
5520         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5521                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5522                         discard=$(($discard + $s))
5523         done
5524         cleanup_101a
5525
5526         if [ $(($discard * 10)) -gt $nreads ] ;then
5527                 $LCTL get_param osc.*-osc*.rpc_stats
5528                 $LCTL get_param llite.*.read_ahead_stats
5529                 error "too many ($discard) discarded pages"
5530         fi
5531         rm -f $DIR/$tfile || true
5532 }
5533 run_test 101a "check read-ahead for random reads ================"
5534
5535 setup_test101bc() {
5536         test_mkdir -p $DIR/$tdir
5537         STRIPE_SIZE=1048576
5538         STRIPE_COUNT=$OSTCOUNT
5539         STRIPE_OFFSET=0
5540
5541         local list=$(comma_list $(osts_nodes))
5542         set_osd_param $list '' read_cache_enable 0
5543         set_osd_param $list '' writethrough_cache_enable 0
5544
5545         trap cleanup_test101bc EXIT
5546         # prepare the read-ahead file
5547         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5548
5549         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5550 }
5551
5552 cleanup_test101bc() {
5553         trap 0
5554         rm -rf $DIR/$tdir
5555         rm -f $DIR/$tfile
5556
5557         local list=$(comma_list $(osts_nodes))
5558         set_osd_param $list '' read_cache_enable 1
5559         set_osd_param $list '' writethrough_cache_enable 1
5560 }
5561
5562 calc_total() {
5563         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5564 }
5565
5566 ra_check_101() {
5567         local READ_SIZE=$1
5568         local STRIPE_SIZE=1048576
5569         local RA_INC=1048576
5570         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5571         local FILE_LENGTH=$((64*100))
5572         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5573                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5574         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5575                         get_named_value 'read but discarded' | \
5576                         cut -d" " -f1 | calc_total`
5577         if [ $DISCARD -gt $discard_limit ]; then
5578                 $LCTL get_param llite.*.read_ahead_stats
5579                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5580         else
5581                 echo "Read-ahead success for size ${READ_SIZE}"
5582         fi
5583 }
5584
5585 test_101b() {
5586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5587         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5588         local STRIPE_SIZE=1048576
5589         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5590         local FILE_LENGTH=$((STRIPE_SIZE*100))
5591         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5592         # prepare the read-ahead file
5593         setup_test101bc
5594         cancel_lru_locks osc
5595         for BIDX in 2 4 8 16 32 64 128 256
5596         do
5597                 local BSIZE=$((BIDX*4096))
5598                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5599                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5600                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5601                 $LCTL set_param -n llite.*.read_ahead_stats 0
5602                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5603                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5604                 cancel_lru_locks osc
5605                 ra_check_101 $BSIZE
5606         done
5607         cleanup_test101bc
5608         true
5609 }
5610 run_test 101b "check stride-io mode read-ahead ================="
5611
5612 test_101c() {
5613         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5614         local STRIPE_SIZE=1048576
5615         local FILE_LENGTH=$((STRIPE_SIZE*100))
5616         local nreads=10000
5617         local osc
5618
5619     setup_test101bc
5620
5621     cancel_lru_locks osc
5622     $LCTL set_param osc.*.rpc_stats 0
5623     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5624     for osc in $($LCTL get_param -N osc.*); do
5625         if [ "$osc" == "osc.num_refs" ]; then
5626             continue
5627         fi
5628
5629         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5630         if [ $lines -le 20 ]; then
5631             continue
5632         fi
5633
5634         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5635                                      awk '$1 == "1:" { print $2; exit; }')
5636         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5637                                      awk '$1 == "2:" { print $2; exit; }')
5638         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5639                                      awk '$1 == "4:" { print $2; exit; }')
5640         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5641                                      awk '$1 == "8:" { print $2; exit; }')
5642
5643         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5644         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5645         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5646         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5647         echo "${osc} rpc check passed!"
5648     done
5649     cleanup_test101bc
5650     true
5651 }
5652 run_test 101c "check stripe_size aligned read-ahead ================="
5653
5654 set_read_ahead() {
5655    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5656    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5657 }
5658
5659 test_101d() {
5660         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5661         local file=$DIR/$tfile
5662         local size=${FILESIZE_101c:-500}
5663         local ra_MB=${READAHEAD_MB:-40}
5664
5665         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5666         [ $space -gt $((size * 1024)) ] ||
5667                 { skip "Need free space ${size}M, have $space" && return; }
5668
5669     echo Creating ${size}M test file $file
5670     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5671     echo Cancel LRU locks on lustre client to flush the client cache
5672     cancel_lru_locks osc
5673
5674     echo Disable read-ahead
5675     local old_READAHEAD=$(set_read_ahead 0)
5676
5677     echo Reading the test file $file with read-ahead disabled
5678     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5679
5680     echo Cancel LRU locks on lustre client to flush the client cache
5681     cancel_lru_locks osc
5682     echo Enable read-ahead with ${ra_MB}MB
5683     set_read_ahead $ra_MB
5684
5685     echo Reading the test file $file with read-ahead enabled
5686     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5687
5688     echo read-ahead disabled time read $time_ra_OFF
5689     echo read-ahead enabled  time read $time_ra_ON
5690
5691         set_read_ahead $old_READAHEAD
5692         rm -f $file
5693         wait_delete_completed
5694
5695     [ $time_ra_ON -lt $time_ra_OFF ] ||
5696         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5697                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5698 }
5699 run_test 101d "file read with and without read-ahead enabled  ================="
5700
5701 test_101e() {
5702         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5703     local file=$DIR/$tfile
5704     local size=500  #KB
5705     local count=100
5706     local blksize=1024
5707
5708     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5709     local need_space=$((count * size))
5710     [ $space -gt $need_space ] ||
5711         { skip_env "Need free space $need_space, have $space" && return; }
5712
5713     echo Creating $count ${size}K test files
5714     for ((i = 0; i < $count; i++)); do
5715         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5716     done
5717
5718     echo Cancel LRU locks on lustre client to flush the client cache
5719     cancel_lru_locks osc
5720
5721     echo Reset readahead stats
5722     $LCTL set_param -n llite.*.read_ahead_stats 0
5723
5724     for ((i = 0; i < $count; i++)); do
5725         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5726     done
5727
5728     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5729           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5730
5731     for ((i = 0; i < $count; i++)); do
5732         rm -rf ${file}_${i} 2>/dev/null
5733     done
5734
5735     #10000 means 20% reads are missing in readahead
5736     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5737 }
5738 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5739
5740 cleanup_test101f() {
5741     trap 0
5742     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5743     rm -rf $DIR/$tfile 2>/dev/null
5744 }
5745
5746 test_101f() {
5747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5748     local file=$DIR/$tfile
5749     local nreads=1000
5750
5751     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5752     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5753     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5754     trap cleanup_test101f EXIT
5755
5756     echo Cancel LRU locks on lustre client to flush the client cache
5757     cancel_lru_locks osc
5758
5759     echo Reset readahead stats
5760     $LCTL set_param -n llite.*.read_ahead_stats 0
5761     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5762     # readahead should read in 2M file on second read, so only miss
5763     # 2 pages.
5764     echo Random 4K reads on 2M file for 1000 times
5765     $READS -f $file -s 2097152 -b 4096 -n $nreads
5766
5767     echo checking missing pages
5768     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5769           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5770
5771     [ $miss -lt 3 ] || error "misses too much pages!"
5772     cleanup_test101f
5773 }
5774 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5775
5776 setup_test102() {
5777         test_mkdir -p $DIR/$tdir
5778         chown $RUNAS_ID $DIR/$tdir
5779         STRIPE_SIZE=65536
5780         STRIPE_OFFSET=1
5781         STRIPE_COUNT=$OSTCOUNT
5782         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5783
5784         trap cleanup_test102 EXIT
5785         cd $DIR
5786         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5787         cd $DIR/$tdir
5788         for num in 1 2 3 4; do
5789                 for count in $(seq 1 $STRIPE_COUNT); do
5790                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5791                                 local size=`expr $STRIPE_SIZE \* $num`
5792                                 local file=file"$num-$idx-$count"
5793                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5794                         done
5795                 done
5796         done
5797
5798         cd $DIR
5799         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5800 }
5801
5802 cleanup_test102() {
5803         trap 0
5804         rm -f $TMP/f102.tar
5805         rm -rf $DIR/d0.sanity/d102
5806 }
5807
5808 test_102a() {
5809         local testfile=$DIR/xattr_testfile
5810
5811         touch $testfile
5812
5813         [ "$UID" != 0 ] && skip_env "must run as root" && return
5814         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5815                 skip_env "must have user_xattr" && return
5816
5817         [ -z "$(which setfattr 2>/dev/null)" ] &&
5818                 skip_env "could not find setfattr" && return
5819
5820         echo "set/get xattr..."
5821         setfattr -n trusted.name1 -v value1 $testfile || error
5822         getfattr -n trusted.name1 $testfile 2> /dev/null |
5823           grep "trusted.name1=.value1" ||
5824                 error "$testfile missing trusted.name1=value1"
5825
5826         setfattr -n user.author1 -v author1 $testfile || error
5827         getfattr -n user.author1 $testfile 2> /dev/null |
5828           grep "user.author1=.author1" ||
5829                 error "$testfile missing trusted.author1=author1"
5830
5831         echo "listxattr..."
5832         setfattr -n trusted.name2 -v value2 $testfile ||
5833                 error "$testfile unable to set trusted.name2"
5834         setfattr -n trusted.name3 -v value3 $testfile ||
5835                 error "$testfile unable to set trusted.name3"
5836         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5837             grep "trusted.name" | wc -l) -eq 3 ] ||
5838                 error "$testfile missing 3 trusted.name xattrs"
5839
5840         setfattr -n user.author2 -v author2 $testfile ||
5841                 error "$testfile unable to set user.author2"
5842         setfattr -n user.author3 -v author3 $testfile ||
5843                 error "$testfile unable to set user.author3"
5844         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5845             grep "user.author" | wc -l) -eq 3 ] ||
5846                 error "$testfile missing 3 user.author xattrs"
5847
5848         echo "remove xattr..."
5849         setfattr -x trusted.name1 $testfile ||
5850                 error "$testfile error deleting trusted.name1"
5851         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5852                 error "$testfile did not delete trusted.name1 xattr"
5853
5854         setfattr -x user.author1 $testfile ||
5855                 error "$testfile error deleting user.author1"
5856         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5857                 error "$testfile did not delete trusted.name1 xattr"
5858
5859         # b10667: setting lustre special xattr be silently discarded
5860         echo "set lustre special xattr ..."
5861         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5862                 error "$testfile allowed setting trusted.lov"
5863 }
5864 run_test 102a "user xattr test =================================="
5865
5866 test_102b() {
5867         # b10930: get/set/list trusted.lov xattr
5868         echo "get/set/list trusted.lov xattr ..."
5869         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5870         local testfile=$DIR/$tfile
5871         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5872                 error "setstripe failed"
5873         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5874                 error "getstripe failed"
5875         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5876         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5877
5878         local testfile2=${testfile}2
5879         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5880                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5881
5882         $MCREATE $testfile2
5883         setfattr -n trusted.lov -v $value $testfile2
5884         local stripe_size=$($GETSTRIPE -S $testfile2)
5885         local stripe_count=$($GETSTRIPE -c $testfile2)
5886         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5887         [ $stripe_count -eq $STRIPECOUNT ] ||
5888                 error "stripe count $stripe_count != $STRIPECOUNT"
5889         rm -f $DIR/$tfile
5890 }
5891 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5892
5893 test_102c() {
5894         # b10930: get/set/list lustre.lov xattr
5895         echo "get/set/list lustre.lov xattr ..."
5896         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5897         test_mkdir -p $DIR/$tdir
5898         chown $RUNAS_ID $DIR/$tdir
5899         local testfile=$DIR/$tdir/$tfile
5900         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5901                 error "setstripe failed"
5902         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5903                 error "getstripe failed"
5904         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5905         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5906
5907         local testfile2=${testfile}2
5908         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5909                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5910
5911         $RUNAS $MCREATE $testfile2
5912         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5913         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5914         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5915         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5916         [ $stripe_count -eq $STRIPECOUNT ] ||
5917                 error "stripe count $stripe_count != $STRIPECOUNT"
5918 }
5919 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5920
5921 compare_stripe_info1() {
5922         local stripe_index_all_zero=true
5923
5924         for num in 1 2 3 4; do
5925                 for count in $(seq 1 $STRIPE_COUNT); do
5926                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5927                                 local size=$((STRIPE_SIZE * num))
5928                                 local file=file"$num-$offset-$count"
5929                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5930                                 [ $stripe_size -ne $size ] &&
5931                                     error "$file: size $stripe_size != $size"
5932                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5933                                 # allow fewer stripes to be created, ORI-601
5934                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5935                                     error "$file: count $stripe_count != $count"
5936                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5937                                 [ $stripe_index -ne 0 ] &&
5938                                         stripe_index_all_zero=false
5939                         done
5940                 done
5941         done
5942         $stripe_index_all_zero &&
5943                 error "all files are being extracted starting from OST index 0"
5944         return 0
5945 }
5946
5947 find_lustre_tar() {
5948         [ -n "$(which tar 2>/dev/null)" ] &&
5949                 strings $(which tar) | grep -q "lustre" && echo tar
5950 }
5951
5952 test_102d() {
5953         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5954         # b10930: tar test for trusted.lov xattr
5955         TAR=$(find_lustre_tar)
5956         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5957         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5958         setup_test102
5959         test_mkdir -p $DIR/d102d
5960         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5961         cd $DIR/d102d/$tdir
5962         compare_stripe_info1
5963 }
5964 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5965
5966 test_102f() {
5967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5968         # b10930: tar test for trusted.lov xattr
5969         TAR=$(find_lustre_tar)
5970         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5971         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5972         setup_test102
5973         test_mkdir -p $DIR/d102f
5974         cd $DIR
5975         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5976         cd $DIR/d102f/$tdir
5977         compare_stripe_info1
5978 }
5979 run_test 102f "tar copy files, not keep osts ==========="
5980
5981 grow_xattr() {
5982         local xsize=${1:-1024}  # in bytes
5983         local file=$DIR/$tfile
5984
5985         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5986                 skip "must have user_xattr" && return 0
5987         [ -z "$(which setfattr 2>/dev/null)" ] &&
5988                 skip_env "could not find setfattr" && return 0
5989         [ -z "$(which getfattr 2>/dev/null)" ] &&
5990                 skip_env "could not find getfattr" && return 0
5991
5992         touch $file
5993
5994         local value="$(generate_string $xsize)"
5995
5996         local xbig=trusted.big
5997         log "save $xbig on $file"
5998         setfattr -n $xbig -v $value $file ||
5999                 error "saving $xbig on $file failed"
6000
6001         local orig=$(get_xattr_value $xbig $file)
6002         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6003
6004         local xsml=trusted.sml
6005         log "save $xsml on $file"
6006         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6007
6008         local new=$(get_xattr_value $xbig $file)
6009         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6010
6011         log "grow $xsml on $file"
6012         setfattr -n $xsml -v "$value" $file ||
6013                 error "growing $xsml on $file failed"
6014
6015         new=$(get_xattr_value $xbig $file)
6016         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6017         log "$xbig still valid after growing $xsml"
6018
6019         rm -f $file
6020 }
6021
6022 test_102h() { # bug 15777
6023         grow_xattr 1024
6024 }
6025 run_test 102h "grow xattr from inside inode to external block"
6026
6027 test_102ha() {
6028         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6029         grow_xattr $(max_xattr_size)
6030 }
6031 run_test 102ha "grow xattr from inside inode to external inode"
6032
6033 test_102i() { # bug 17038
6034         touch $DIR/$tfile
6035         ln -s $DIR/$tfile $DIR/${tfile}link
6036         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6037         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"
6038         rm -f $DIR/$tfile $DIR/${tfile}link
6039 }
6040 run_test 102i "lgetxattr test on symbolic link ============"
6041
6042 test_102j() {
6043         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6044         TAR=$(find_lustre_tar)
6045         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6046         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6047         setup_test102 "$RUNAS"
6048         test_mkdir -p $DIR/d102j
6049         chown $RUNAS_ID $DIR/d102j
6050         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6051         cd $DIR/d102j/$tdir
6052         compare_stripe_info1 "$RUNAS"
6053 }
6054 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6055
6056 test_102k() {
6057         touch $DIR/$tfile
6058         # b22187 just check that does not crash for regular file.
6059         setfattr -n trusted.lov $DIR/$tfile
6060         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6061         local test_kdir=$DIR/d102k
6062         test_mkdir $test_kdir
6063         local default_size=`$GETSTRIPE -S $test_kdir`
6064         local default_count=`$GETSTRIPE -c $test_kdir`
6065         local default_offset=`$GETSTRIPE -i $test_kdir`
6066         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
6067                 error 'dir setstripe failed'
6068         setfattr -n trusted.lov $test_kdir
6069         local stripe_size=`$GETSTRIPE -S $test_kdir`
6070         local stripe_count=`$GETSTRIPE -c $test_kdir`
6071         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6072         [ $stripe_size -eq $default_size ] ||
6073                 error "stripe size $stripe_size != $default_size"
6074         [ $stripe_count -eq $default_count ] ||
6075                 error "stripe count $stripe_count != $default_count"
6076         [ $stripe_offset -eq $default_offset ] ||
6077                 error "stripe offset $stripe_offset != $default_offset"
6078         rm -rf $DIR/$tfile $test_kdir
6079 }
6080 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6081
6082 test_102l() {
6083         # LU-532 trusted. xattr is invisible to non-root
6084         local testfile=$DIR/$tfile
6085
6086         touch $testfile
6087
6088         echo "listxattr as user..."
6089         chown $RUNAS_ID $testfile
6090         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6091             grep -q "trusted" &&
6092                 error "$testfile trusted xattrs are user visible"
6093
6094         return 0;
6095 }
6096 run_test 102l "listxattr filter test =================================="
6097
6098 cleanup_test102
6099
6100 run_acl_subtest()
6101 {
6102     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6103     return $?
6104 }
6105
6106 test_103 () {
6107     [ "$UID" != 0 ] && skip_env "must run as root" && return
6108     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6109     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
6110     $GSS && skip "could not run under gss" && return
6111
6112     declare -a identity_old
6113
6114         for num in $(seq $MDSCOUNT); do
6115                 switch_identity $num true || identity_old[$num]=$?
6116         done
6117
6118     SAVE_UMASK=`umask`
6119     umask 0022
6120     cd $DIR
6121
6122     echo "performing cp ..."
6123     run_acl_subtest cp || error
6124     echo "performing getfacl-noacl..."
6125     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6126     echo "performing misc..."
6127     run_acl_subtest misc || error  "misc test failed"
6128     echo "performing permissions..."
6129     run_acl_subtest permissions || error "permissions failed"
6130     echo "performing setfacl..."
6131     run_acl_subtest setfacl || error  "setfacl test failed"
6132
6133     # inheritance test got from HP
6134     echo "performing inheritance..."
6135     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6136     chmod +x make-tree || error "chmod +x failed"
6137     run_acl_subtest inheritance || error "inheritance test failed"
6138     rm -f make-tree
6139
6140     echo "LU-974 ignore umask when acl is enabled..."
6141     run_acl_subtest 974 || error "LU-974 test failed"
6142
6143     echo "LU-2561 newly created file is same size as directory..."
6144     run_acl_subtest 2561 || error "LU-2561 test failed"
6145
6146     cd $SAVE_PWD
6147     umask $SAVE_UMASK
6148
6149         for num in $(seq $MDSCOUNT); do
6150                 if [ "${identity_old[$num]}" = 1 ]; then
6151                         switch_identity $num false || identity_old[$num]=$?
6152                 fi
6153         done
6154 }
6155 run_test 103 "acl test ========================================="
6156
6157 test_104a() {
6158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6159         touch $DIR/$tfile
6160         lfs df || error "lfs df failed"
6161         lfs df -ih || error "lfs df -ih failed"
6162         lfs df -h $DIR || error "lfs df -h $DIR failed"
6163         lfs df -i $DIR || error "lfs df -i $DIR failed"
6164         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6165         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6166
6167         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6168         lctl --device %$OSC deactivate
6169         lfs df || error "lfs df with deactivated OSC failed"
6170         lctl --device %$OSC activate
6171         # wait the osc back to normal
6172         wait_osc_import_state client ost FULL
6173
6174         lfs df || error "lfs df with reactivated OSC failed"
6175         rm -f $DIR/$tfile
6176 }
6177 run_test 104a "lfs df [-ih] [path] test ========================="
6178
6179 test_104b() {
6180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6181         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6182         chmod 666 /dev/obd
6183         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6184         if [ $denied_cnt -ne 0 ];
6185         then
6186                     error "lfs check servers test failed"
6187         fi
6188 }
6189 run_test 104b "$RUNAS lfs check servers test ===================="
6190
6191 test_105a() {
6192         # doesn't work on 2.4 kernels
6193         touch $DIR/$tfile
6194         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6195                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6196         else
6197                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6198         fi
6199         rm -f $DIR/$tfile
6200 }
6201 run_test 105a "flock when mounted without -o flock test ========"
6202
6203 test_105b() {
6204         touch $DIR/$tfile
6205         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6206                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6207         else
6208                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6209         fi
6210         rm -f $DIR/$tfile
6211 }
6212 run_test 105b "fcntl when mounted without -o flock test ========"
6213
6214 test_105c() {
6215         touch $DIR/$tfile
6216         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6217                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6218         else
6219                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6220         fi
6221         rm -f $DIR/$tfile
6222 }
6223 run_test 105c "lockf when mounted without -o flock test ========"
6224
6225 test_105d() { # bug 15924
6226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6227         test_mkdir -p $DIR/$tdir
6228         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6229                 skip "mount w/o flock enabled" && return
6230         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6231         $LCTL set_param fail_loc=0x80000315
6232         flocks_test 2 $DIR/$tdir
6233 }
6234 run_test 105d "flock race (should not freeze) ========"
6235
6236 test_105e() { # bug 22660 && 22040
6237         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6238                 skip "mount w/o flock enabled" && return
6239         touch $DIR/$tfile
6240         flocks_test 3 $DIR/$tfile
6241 }
6242 run_test 105e "Two conflicting flocks from same process ======="
6243
6244 test_106() { #bug 10921
6245         test_mkdir -p $DIR/$tdir
6246         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6247         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6248 }
6249 run_test 106 "attempt exec of dir followed by chown of that dir"
6250
6251 test_107() {
6252         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6253         CDIR=`pwd`
6254         cd $DIR
6255
6256         local file=core
6257         rm -f $file
6258
6259         local save_pattern=$(sysctl -n kernel.core_pattern)
6260         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6261         sysctl -w kernel.core_pattern=$file
6262         sysctl -w kernel.core_uses_pid=0
6263
6264         ulimit -c unlimited
6265         sleep 60 &
6266         SLEEPPID=$!
6267
6268         sleep 1
6269
6270         kill -s 11 $SLEEPPID
6271         wait $SLEEPPID
6272         if [ -e $file ]; then
6273                 size=`stat -c%s $file`
6274                 [ $size -eq 0 ] && error "Fail to create core file $file"
6275         else
6276                 error "Fail to create core file $file"
6277         fi
6278         rm -f $file
6279         sysctl -w kernel.core_pattern=$save_pattern
6280         sysctl -w kernel.core_uses_pid=$save_uses_pid
6281         cd $CDIR
6282 }
6283 run_test 107 "Coredump on SIG"
6284
6285 test_110() {
6286         test_mkdir -p $DIR/$tdir
6287         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6288                 error "mkdir with 255 char failed"
6289         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6290                 error "mkdir with 256 char should fail, but did not"
6291         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6292                 error "create with 255 char failed"
6293         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6294                 error "create with 256 char should fail, but did not"
6295
6296         ls -l $DIR/$tdir
6297         rm -rf $DIR/$tdir
6298 }
6299 run_test 110 "filename length checking"
6300
6301 test_115() {
6302         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6303         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6304             cut -c11-20)
6305         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6306             return
6307         echo "Starting with $OSTIO_pre threads"
6308
6309         NUMTEST=20000
6310         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6311         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6312         echo "$NUMTEST creates/unlinks"
6313         test_mkdir -p $DIR/$tdir
6314         createmany -o $DIR/$tdir/$tfile $NUMTEST
6315         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6316
6317         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6318             cut -c11-20)
6319
6320         # don't return an error
6321         [ $OSTIO_post == $OSTIO_pre ] && echo \
6322             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6323             echo "This may be fine, depending on what ran before this test" &&
6324             echo "and how fast this system is." && return
6325
6326         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6327 }
6328 run_test 115 "verify dynamic thread creation===================="
6329
6330 free_min_max () {
6331         wait_delete_completed
6332         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6333         echo OST kbytes available: ${AVAIL[@]}
6334         MAXI=0; MAXV=${AVAIL[0]}
6335         MINI=0; MINV=${AVAIL[0]}
6336         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6337             #echo OST $i: ${AVAIL[i]}kb
6338             if [ ${AVAIL[i]} -gt $MAXV ]; then
6339                 MAXV=${AVAIL[i]}; MAXI=$i
6340             fi
6341             if [ ${AVAIL[i]} -lt $MINV ]; then
6342                 MINV=${AVAIL[i]}; MINI=$i
6343             fi
6344         done
6345         echo Min free space: OST $MINI: $MINV
6346         echo Max free space: OST $MAXI: $MAXV
6347 }
6348
6349 test_116a() { # was previously test_116()
6350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6351         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6352
6353         echo -n "Free space priority "
6354         lctl get_param -n lov.*-clilov-*.qos_prio_free
6355         declare -a AVAIL
6356         free_min_max
6357         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6358                 return
6359
6360         # generate uneven OSTs
6361         test_mkdir -p $DIR/$tdir/OST${MINI}
6362         declare -i FILL
6363         FILL=$(($MINV / 4))
6364         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6365         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6366         i=0
6367         while [ $FILL -gt 0 ]; do
6368             i=$(($i + 1))
6369             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6370             FILL=$(($FILL - 2048))
6371             echo -n .
6372         done
6373         FILL=$(($MINV / 4))
6374         sync
6375         sleep_maxage
6376
6377         free_min_max
6378         DIFF=$(($MAXV - $MINV))
6379         DIFF2=$(($DIFF * 100 / $MINV))
6380         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6381         if [ $DIFF2 -gt 20 ]; then
6382             echo "ok"
6383         else
6384             echo "failed - QOS mode won't be used"
6385             error_ignore "QOS imbalance criteria not met"
6386             return
6387         fi
6388
6389         MINI1=$MINI; MINV1=$MINV
6390         MAXI1=$MAXI; MAXV1=$MAXV
6391
6392         # now fill using QOS
6393         echo writing a bunch of files to QOS-assigned OSTs
6394         $SETSTRIPE -c 1 $DIR/$tdir
6395         i=0
6396         while [ $FILL -gt 0 ]; do
6397             i=$(($i + 1))
6398             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6399             FILL=$(($FILL - 200))
6400             echo -n .
6401         done
6402         echo "wrote $i 200k files"
6403         sync
6404         sleep_maxage
6405
6406         echo "Note: free space may not be updated, so measurements might be off"
6407         free_min_max
6408         DIFF2=$(($MAXV - $MINV))
6409         echo "free space delta: orig $DIFF final $DIFF2"
6410         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6411         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6412         echo "Wrote $DIFF to smaller OST $MINI1"
6413         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6414         echo "Wrote $DIFF2 to larger OST $MAXI1"
6415         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6416
6417         # Figure out which files were written where
6418         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6419                awk '/'$MINI1': / {print $2; exit}')
6420         echo $UUID
6421         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6422         echo "$MINC files created on smaller OST $MINI1"
6423         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6424                awk '/'$MAXI1': / {print $2; exit}')
6425         echo $UUID
6426         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6427         echo "$MAXC files created on larger OST $MAXI1"
6428         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6429         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6430
6431         rm -rf $DIR/$tdir
6432 }
6433 run_test 116a "stripe QOS: free space balance ==================="
6434
6435 test_116b() { # LU-2093
6436         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6437 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6438         local old_rr
6439         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6440         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6441         mkdir -p $DIR/$tdir
6442         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6443         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6444         do_facet $SINGLEMDS lctl set_param fail_loc=0
6445         rm -rf $DIR/$tdir
6446         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6447 }
6448 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6449
6450 test_117() # bug 10891
6451 {
6452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6453         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6454         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6455         lctl set_param fail_loc=0x21e
6456         > $DIR/$tfile || error "truncate failed"
6457         lctl set_param fail_loc=0
6458         echo "Truncate succeeded."
6459         rm -f $DIR/$tfile
6460 }
6461 run_test 117 "verify fsfilt_extend =========="
6462
6463 export OLD_RESENDCOUNT=""
6464 set_resend_count () {
6465         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6466         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6467         lctl set_param -n $PROC_RESENDCOUNT $1
6468         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6469 }
6470
6471 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6472
6473 # Reset async IO behavior after error case
6474 reset_async() {
6475         FILE=$DIR/reset_async
6476
6477         # Ensure all OSCs are cleared
6478         $SETSTRIPE -c -1 $FILE
6479         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6480         sync
6481         rm $FILE
6482 }
6483
6484 test_118a() #bug 11710
6485 {
6486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6487         reset_async
6488
6489         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6490         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6491         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6492
6493         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6494                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6495                 return 1;
6496         fi
6497         rm -f $DIR/$tfile
6498 }
6499 run_test 118a "verify O_SYNC works =========="
6500
6501 test_118b()
6502 {
6503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6504         remote_ost_nodsh && skip "remote OST with nodsh" && return
6505
6506         reset_async
6507
6508         #define OBD_FAIL_OST_ENOENT 0x217
6509         set_nodes_failloc "$(osts_nodes)" 0x217
6510         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6511         RC=$?
6512         set_nodes_failloc "$(osts_nodes)" 0
6513         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6514         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6515                     grep -c writeback)
6516
6517         if [[ $RC -eq 0 ]]; then
6518                 error "Must return error due to dropped pages, rc=$RC"
6519                 return 1;
6520         fi
6521
6522         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6523                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6524                 return 1;
6525         fi
6526
6527         echo "Dirty pages not leaked on ENOENT"
6528
6529         # Due to the above error the OSC will issue all RPCs syncronously
6530         # until a subsequent RPC completes successfully without error.
6531         $MULTIOP $DIR/$tfile Ow4096yc
6532         rm -f $DIR/$tfile
6533
6534         return 0
6535 }
6536 run_test 118b "Reclaim dirty pages on fatal error =========="
6537
6538 test_118c()
6539 {
6540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6541         remote_ost_nodsh && skip "remote OST with nodsh" && return
6542
6543         reset_async
6544
6545         #define OBD_FAIL_OST_EROFS               0x216
6546         set_nodes_failloc "$(osts_nodes)" 0x216
6547
6548         # multiop should block due to fsync until pages are written
6549         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6550         MULTIPID=$!
6551         sleep 1
6552
6553         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6554                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6555         fi
6556
6557         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6558                     grep -c writeback)
6559         if [[ $WRITEBACK -eq 0 ]]; then
6560                 error "No page in writeback, writeback=$WRITEBACK"
6561         fi
6562
6563         set_nodes_failloc "$(osts_nodes)" 0
6564         wait $MULTIPID
6565         RC=$?
6566         if [[ $RC -ne 0 ]]; then
6567                 error "Multiop fsync failed, rc=$RC"
6568         fi
6569
6570         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6571         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6572                     grep -c writeback)
6573         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6574                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6575         fi
6576
6577         rm -f $DIR/$tfile
6578         echo "Dirty pages flushed via fsync on EROFS"
6579         return 0
6580 }
6581 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6582
6583 test_118d()
6584 {
6585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6586         remote_ost_nodsh && skip "remote OST with nodsh" && return
6587
6588         reset_async
6589
6590         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6591         set_nodes_failloc "$(osts_nodes)" 0x214
6592         # multiop should block due to fsync until pages are written
6593         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6594         MULTIPID=$!
6595         sleep 1
6596
6597         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6598                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6599         fi
6600
6601         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6602                     grep -c writeback)
6603         if [[ $WRITEBACK -eq 0 ]]; then
6604                 error "No page in writeback, writeback=$WRITEBACK"
6605         fi
6606
6607         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6608         set_nodes_failloc "$(osts_nodes)" 0
6609
6610         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6611         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6612                     grep -c writeback)
6613         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6614                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6615         fi
6616
6617         rm -f $DIR/$tfile
6618         echo "Dirty pages gaurenteed flushed via fsync"
6619         return 0
6620 }
6621 run_test 118d "Fsync validation inject a delay of the bulk =========="
6622
6623 test_118f() {
6624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6625         reset_async
6626
6627         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6628         lctl set_param fail_loc=0x8000040a
6629
6630         # Should simulate EINVAL error which is fatal
6631         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6632         RC=$?
6633         if [[ $RC -eq 0 ]]; then
6634                 error "Must return error due to dropped pages, rc=$RC"
6635         fi
6636
6637         lctl set_param fail_loc=0x0
6638
6639         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6640         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6641         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6642                     grep -c writeback)
6643         if [[ $LOCKED -ne 0 ]]; then
6644                 error "Locked pages remain in cache, locked=$LOCKED"
6645         fi
6646
6647         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6648                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6649         fi
6650
6651         rm -f $DIR/$tfile
6652         echo "No pages locked after fsync"
6653
6654         reset_async
6655         return 0
6656 }
6657 run_test 118f "Simulate unrecoverable OSC side error =========="
6658
6659 test_118g() {
6660         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6661         reset_async
6662
6663         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6664         lctl set_param fail_loc=0x406
6665
6666         # simulate local -ENOMEM
6667         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6668         RC=$?
6669
6670         lctl set_param fail_loc=0
6671         if [[ $RC -eq 0 ]]; then
6672                 error "Must return error due to dropped pages, rc=$RC"
6673         fi
6674
6675         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6676         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6677         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6678                         grep -c writeback)
6679         if [[ $LOCKED -ne 0 ]]; then
6680                 error "Locked pages remain in cache, locked=$LOCKED"
6681         fi
6682
6683         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6684                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6685         fi
6686
6687         rm -f $DIR/$tfile
6688         echo "No pages locked after fsync"
6689
6690         reset_async
6691         return 0
6692 }
6693 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6694
6695 test_118h() {
6696         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6697         remote_ost_nodsh && skip "remote OST with nodsh" && return
6698
6699         reset_async
6700
6701         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6702         set_nodes_failloc "$(osts_nodes)" 0x20e
6703         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6704         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6705         RC=$?
6706
6707         set_nodes_failloc "$(osts_nodes)" 0
6708         if [[ $RC -eq 0 ]]; then
6709                 error "Must return error due to dropped pages, rc=$RC"
6710         fi
6711
6712         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6713         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6714         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6715                     grep -c writeback)
6716         if [[ $LOCKED -ne 0 ]]; then
6717                 error "Locked pages remain in cache, locked=$LOCKED"
6718         fi
6719
6720         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6721                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6722         fi
6723
6724         rm -f $DIR/$tfile
6725         echo "No pages locked after fsync"
6726
6727         return 0
6728 }
6729 run_test 118h "Verify timeout in handling recoverables errors  =========="
6730
6731 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6732
6733 test_118i() {
6734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6735         remote_ost_nodsh && skip "remote OST with nodsh" && return
6736
6737         reset_async
6738
6739         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6740         set_nodes_failloc "$(osts_nodes)" 0x20e
6741
6742         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6743         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6744         PID=$!
6745         sleep 5
6746         set_nodes_failloc "$(osts_nodes)" 0
6747
6748         wait $PID
6749         RC=$?
6750         if [[ $RC -ne 0 ]]; then
6751                 error "got error, but should be not, rc=$RC"
6752         fi
6753
6754         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6755         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6756         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6757         if [[ $LOCKED -ne 0 ]]; then
6758                 error "Locked pages remain in cache, locked=$LOCKED"
6759         fi
6760
6761         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6762                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6763         fi
6764
6765         rm -f $DIR/$tfile
6766         echo "No pages locked after fsync"
6767
6768         return 0
6769 }
6770 run_test 118i "Fix error before timeout in recoverable error  =========="
6771
6772 [ "$SLOW" = "no" ] && set_resend_count 4
6773
6774 test_118j() {
6775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6776         remote_ost_nodsh && skip "remote OST with nodsh" && return
6777
6778         reset_async
6779
6780         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6781         set_nodes_failloc "$(osts_nodes)" 0x220
6782
6783         # return -EIO from OST
6784         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6785         RC=$?
6786         set_nodes_failloc "$(osts_nodes)" 0x0
6787         if [[ $RC -eq 0 ]]; then
6788                 error "Must return error due to dropped pages, rc=$RC"
6789         fi
6790
6791         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6792         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6793         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6794         if [[ $LOCKED -ne 0 ]]; then
6795                 error "Locked pages remain in cache, locked=$LOCKED"
6796         fi
6797
6798         # in recoverable error on OST we want resend and stay until it finished
6799         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6800                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6801         fi
6802
6803         rm -f $DIR/$tfile
6804         echo "No pages locked after fsync"
6805
6806         return 0
6807 }
6808 run_test 118j "Simulate unrecoverable OST side error =========="
6809
6810 test_118k()
6811 {
6812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6813         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6814
6815         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6816         set_nodes_failloc "$(osts_nodes)" 0x20e
6817         test_mkdir -p $DIR/$tdir
6818
6819         for ((i=0;i<10;i++)); do
6820                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6821                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6822                 SLEEPPID=$!
6823                 sleep 0.500s
6824                 kill $SLEEPPID
6825                 wait $SLEEPPID
6826         done
6827
6828         set_nodes_failloc "$(osts_nodes)" 0
6829         rm -rf $DIR/$tdir
6830 }
6831 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6832
6833 test_118l()
6834 {
6835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6836         # LU-646
6837         test_mkdir -p $DIR/$tdir
6838         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6839         rm -rf $DIR/$tdir
6840 }
6841 run_test 118l "fsync dir ========="
6842
6843 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6844
6845 test_119a() # bug 11737
6846 {
6847         BSIZE=$((512 * 1024))
6848         directio write $DIR/$tfile 0 1 $BSIZE
6849         # We ask to read two blocks, which is more than a file size.
6850         # directio will indicate an error when requested and actual
6851         # sizes aren't equeal (a normal situation in this case) and
6852         # print actual read amount.
6853         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6854         if [ "$NOB" != "$BSIZE" ]; then
6855                 error "read $NOB bytes instead of $BSIZE"
6856         fi
6857         rm -f $DIR/$tfile
6858 }
6859 run_test 119a "Short directIO read must return actual read amount"
6860
6861 test_119b() # bug 11737
6862 {
6863         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6864
6865         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6866         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6867         sync
6868         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6869                 error "direct read failed"
6870         rm -f $DIR/$tfile
6871 }
6872 run_test 119b "Sparse directIO read must return actual read amount"
6873
6874 test_119c() # bug 13099
6875 {
6876         BSIZE=1048576
6877         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6878         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6879         rm -f $DIR/$tfile
6880 }
6881 run_test 119c "Testing for direct read hitting hole"
6882
6883 test_119d() # bug 15950
6884 {
6885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6886         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6887         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6888         BSIZE=1048576
6889         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6890         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6891         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6892         lctl set_param fail_loc=0x40d
6893         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6894         pid_dio=$!
6895         sleep 1
6896         cat $DIR/$tfile > /dev/null &
6897         lctl set_param fail_loc=0
6898         pid_reads=$!
6899         wait $pid_dio
6900         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6901         sleep 2
6902         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6903         error "the read rpcs have not completed in 2s"
6904         rm -f $DIR/$tfile
6905         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6906 }
6907 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6908
6909 test_120a() {
6910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6911         test_mkdir -p $DIR/$tdir
6912         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6913                skip "no early lock cancel on server" && return 0
6914         lru_resize_disable mdc
6915         lru_resize_disable osc
6916         cancel_lru_locks mdc
6917         stat $DIR/$tdir > /dev/null
6918         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6919         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6920         test_mkdir $DIR/$tdir/d1
6921         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6922         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6923         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6924         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6925         lru_resize_enable mdc
6926         lru_resize_enable osc
6927 }
6928 run_test 120a "Early Lock Cancel: mkdir test"
6929
6930 test_120b() {
6931         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6932         test_mkdir -p $DIR/$tdir
6933         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6934                skip "no early lock cancel on server" && return 0
6935         lru_resize_disable mdc
6936         lru_resize_disable osc
6937         cancel_lru_locks mdc
6938         stat $DIR/$tdir > /dev/null
6939         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6940         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6941         touch $DIR/$tdir/f1
6942         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6943         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6944         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6945         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6946         lru_resize_enable mdc
6947         lru_resize_enable osc
6948 }
6949 run_test 120b "Early Lock Cancel: create test"
6950
6951 test_120c() {
6952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6953         test_mkdir -p $DIR/$tdir
6954         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6955                skip "no early lock cancel on server" && return 0
6956         lru_resize_disable mdc
6957         lru_resize_disable osc
6958         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6959         touch $DIR/$tdir/d1/f1
6960         cancel_lru_locks mdc
6961         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6962         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6963         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6964         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6965         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6966         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6967         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6968         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6969         lru_resize_enable mdc
6970         lru_resize_enable osc
6971 }
6972 run_test 120c "Early Lock Cancel: link test"
6973
6974 test_120d() {
6975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6976         test_mkdir -p $DIR/$tdir
6977         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6978                skip "no early lock cancel on server" && return 0
6979         lru_resize_disable mdc
6980         lru_resize_disable osc
6981         touch $DIR/$tdir
6982         cancel_lru_locks mdc
6983         stat $DIR/$tdir > /dev/null
6984         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6985         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6986         chmod a+x $DIR/$tdir
6987         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6988         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6989         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6990         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6991         lru_resize_enable mdc
6992         lru_resize_enable osc
6993 }
6994 run_test 120d "Early Lock Cancel: setattr test"
6995
6996 test_120e() {
6997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6998         test_mkdir -p $DIR/$tdir
6999         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7000                skip "no early lock cancel on server" && return 0
7001         lru_resize_disable mdc
7002         lru_resize_disable osc
7003         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7004         cancel_lru_locks mdc
7005         cancel_lru_locks osc
7006         dd if=$DIR/$tdir/f1 of=/dev/null
7007         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7008         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7009               awk '/ldlm_cancel/ {print $2}'`
7010         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7011               awk '/ldlm_bl_callback/ {print $2}'`
7012         unlink $DIR/$tdir/f1
7013         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7014               awk '/ldlm_cancel/ {print $2}'`
7015         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7016               awk '/ldlm_bl_callback/ {print $2}'`
7017         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7018         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7019         lru_resize_enable mdc
7020         lru_resize_enable osc
7021 }
7022 run_test 120e "Early Lock Cancel: unlink test"
7023
7024 test_120f() {
7025         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7026         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7027                skip "no early lock cancel on server" && return 0
7028         test_mkdir -p $DIR/$tdir
7029         lru_resize_disable mdc
7030         lru_resize_disable osc
7031         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7032         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7033         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7034         cancel_lru_locks mdc
7035         cancel_lru_locks osc
7036         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7037         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7038         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7039         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7040               awk '/ldlm_cancel/ {print $2}'`
7041         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7042               awk '/ldlm_bl_callback/ {print $2}'`
7043         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7044         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7045               awk '/ldlm_cancel/ {print $2}'`
7046         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7047               awk '/ldlm_bl_callback/ {print $2}'`
7048         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7049         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7050         lru_resize_enable mdc
7051         lru_resize_enable osc
7052 }
7053 run_test 120f "Early Lock Cancel: rename test"
7054
7055 test_120g() {
7056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7057         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7058                skip "no early lock cancel on server" && return 0
7059         lru_resize_disable mdc
7060         lru_resize_disable osc
7061         count=10000
7062         echo create $count files
7063         test_mkdir -p $DIR/$tdir
7064         cancel_lru_locks mdc
7065         cancel_lru_locks osc
7066         t0=`date +%s`
7067
7068         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7069               awk '/ldlm_cancel/ {print $2}'`
7070         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7071               awk '/ldlm_bl_callback/ {print $2}'`
7072         createmany -o $DIR/$tdir/f $count
7073         sync
7074         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7075               awk '/ldlm_cancel/ {print $2}'`
7076         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7077               awk '/ldlm_bl_callback/ {print $2}'`
7078         t1=`date +%s`
7079         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7080         echo rm $count files
7081         rm -r $DIR/$tdir
7082         sync
7083         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7084               awk '/ldlm_cancel/ {print $2}'`
7085         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7086               awk '/ldlm_bl_callback/ {print $2}'`
7087         t2=`date +%s`
7088         echo total: $count removes in $((t2-t1))
7089         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7090         sleep 2
7091         # wait for commitment of removal
7092         lru_resize_enable mdc
7093         lru_resize_enable osc
7094 }
7095 run_test 120g "Early Lock Cancel: performance test"
7096
7097 test_121() { #bug #10589
7098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7099         rm -rf $DIR/$tfile
7100         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7101 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7102         lctl set_param fail_loc=0x310
7103         cancel_lru_locks osc > /dev/null
7104         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7105         lctl set_param fail_loc=0
7106         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7107 }
7108 run_test 121 "read cancel race ========="
7109
7110 test_123a() { # was test 123, statahead(bug 11401)
7111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7112         SLOWOK=0
7113         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7114             log "testing on UP system. Performance may be not as good as expected."
7115                         SLOWOK=1
7116         fi
7117
7118         rm -rf $DIR/$tdir
7119         test_mkdir -p $DIR/$tdir
7120         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7121         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7122         MULT=10
7123         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7124                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7125
7126                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7127                 lctl set_param -n llite.*.statahead_max 0
7128                 lctl get_param llite.*.statahead_max
7129                 cancel_lru_locks mdc
7130                 cancel_lru_locks osc
7131                 stime=`date +%s`
7132                 time ls -l $DIR/$tdir | wc -l
7133                 etime=`date +%s`
7134                 delta=$((etime - stime))
7135                 log "ls $i files without statahead: $delta sec"
7136                 lctl set_param llite.*.statahead_max=$max
7137
7138                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7139                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7140                 cancel_lru_locks mdc
7141                 cancel_lru_locks osc
7142                 stime=`date +%s`
7143                 time ls -l $DIR/$tdir | wc -l
7144                 etime=`date +%s`
7145                 delta_sa=$((etime - stime))
7146                 log "ls $i files with statahead: $delta_sa sec"
7147                 lctl get_param -n llite.*.statahead_stats
7148                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7149
7150                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7151                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7152
7153                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7154                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7155                     lctl set_param -n llite.*.statahead_max 0
7156                     lctl get_param llite.*.statahead_max
7157                     cancel_lru_locks mdc
7158                     cancel_lru_locks osc
7159                     stime=`date +%s`
7160                     time ls -l $DIR/$tdir | wc -l
7161                     etime=`date +%s`
7162                     delta=$((etime - stime))
7163                     log "ls $i files again without statahead: $delta sec"
7164                     lctl set_param llite.*.statahead_max=$max
7165                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7166                         if [  $SLOWOK -eq 0 ]; then
7167                                 error "ls $i files is slower with statahead!"
7168                         else
7169                                 log "ls $i files is slower with statahead!"
7170                         fi
7171                         break
7172                     fi
7173                 fi
7174
7175                 [ $delta -gt 20 ] && break
7176                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7177                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7178         done
7179         log "ls done"
7180
7181         stime=`date +%s`
7182         rm -r $DIR/$tdir
7183         sync
7184         etime=`date +%s`
7185         delta=$((etime - stime))
7186         log "rm -r $DIR/$tdir/: $delta seconds"
7187         log "rm done"
7188         lctl get_param -n llite.*.statahead_stats
7189 }
7190 run_test 123a "verify statahead work"
7191
7192 test_123b () { # statahead(bug 15027)
7193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7194         test_mkdir -p $DIR/$tdir
7195         createmany -o $DIR/$tdir/$tfile-%d 1000
7196
7197         cancel_lru_locks mdc
7198         cancel_lru_locks osc
7199
7200 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7201         lctl set_param fail_loc=0x80000803
7202         ls -lR $DIR/$tdir > /dev/null
7203         log "ls done"
7204         lctl set_param fail_loc=0x0
7205         lctl get_param -n llite.*.statahead_stats
7206         rm -r $DIR/$tdir
7207         sync
7208
7209 }
7210 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7211
7212 test_124a() {
7213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7214         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7215                skip "no lru resize on server" && return 0
7216         local NR=2000
7217         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7218
7219         log "create $NR files at $DIR/$tdir"
7220         createmany -o $DIR/$tdir/f $NR ||
7221                 error "failed to create $NR files in $DIR/$tdir"
7222
7223         cancel_lru_locks mdc
7224         ls -l $DIR/$tdir > /dev/null
7225
7226         local NSDIR=""
7227         local LRU_SIZE=0
7228         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7229                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7230                 LRU_SIZE=$(lctl get_param -n $PARAM)
7231                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7232                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7233                                                 log "NSDIR=$NSDIR"
7234                         log "NS=$(basename $NSDIR)"
7235                         break
7236                 fi
7237         done
7238
7239         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7240                 skip "Not enough cached locks created!"
7241                 return 0
7242         fi
7243         log "LRU=$LRU_SIZE"
7244
7245         local SLEEP=30
7246
7247         # We know that lru resize allows one client to hold $LIMIT locks
7248         # for 10h. After that locks begin to be killed by client.
7249         local MAX_HRS=10
7250         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7251                 log "LIMIT=$LIMIT"
7252
7253         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7254         # killing locks. Some time was spent for creating locks. This means
7255         # that up to the moment of sleep finish we must have killed some of
7256         # them (10-100 locks). This depends on how fast ther were created.
7257         # Many of them were touched in almost the same moment and thus will
7258         # be killed in groups.
7259         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7260
7261         # Use $LRU_SIZE_B here to take into account real number of locks
7262         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7263         local LRU_SIZE_B=$LRU_SIZE
7264         log "LVF=$LVF"
7265         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7266                 log "OLD_LVF=$OLD_LVF"
7267         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7268
7269         # Let's make sure that we really have some margin. Client checks
7270         # cached locks every 10 sec.
7271         SLEEP=$((SLEEP+20))
7272         log "Sleep ${SLEEP} sec"
7273         local SEC=0
7274         while ((SEC<$SLEEP)); do
7275                 echo -n "..."
7276                 sleep 5
7277                 SEC=$((SEC+5))
7278                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7279                 echo -n "$LRU_SIZE"
7280         done
7281         echo ""
7282         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7283         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7284
7285         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7286                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7287                 unlinkmany $DIR/$tdir/f $NR
7288                 return
7289         }
7290
7291         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7292         log "unlink $NR files at $DIR/$tdir"
7293         unlinkmany $DIR/$tdir/f $NR
7294 }
7295 run_test 124a "lru resize ======================================="
7296
7297 get_max_pool_limit()
7298 {
7299         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7300         local max=0
7301         for l in $limit; do
7302                 if test $l -gt $max; then
7303                         max=$l
7304                 fi
7305         done
7306         echo $max
7307 }
7308
7309 test_124b() {
7310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7311         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7312                skip "no lru resize on server" && return 0
7313
7314         LIMIT=`get_max_pool_limit`
7315
7316         NR=$(($(default_lru_size)*20))
7317         if [ $NR -gt $LIMIT ]; then
7318                 log "Limit lock number by $LIMIT locks"
7319                 NR=$LIMIT
7320         fi
7321         lru_resize_disable mdc
7322         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7323                 error "failed to create $DIR/$tdir/disable_lru_resize"
7324
7325         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7326         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7327         cancel_lru_locks mdc
7328         stime=`date +%s`
7329         PID=""
7330         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7331         PID="$PID $!"
7332         sleep 2
7333         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7334         PID="$PID $!"
7335         sleep 2
7336         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7337         PID="$PID $!"
7338         wait $PID
7339         etime=`date +%s`
7340         nolruresize_delta=$((etime-stime))
7341         log "ls -la time: $nolruresize_delta seconds"
7342         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7343         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7344
7345         lru_resize_enable mdc
7346         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7347                 error "failed to create $DIR/$tdir/enable_lru_resize"
7348
7349         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7350         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7351         cancel_lru_locks mdc
7352         stime=`date +%s`
7353         PID=""
7354         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7355         PID="$PID $!"
7356         sleep 2
7357         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7358         PID="$PID $!"
7359         sleep 2
7360         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7361         PID="$PID $!"
7362         wait $PID
7363         etime=`date +%s`
7364         lruresize_delta=$((etime-stime))
7365         log "ls -la time: $lruresize_delta seconds"
7366         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7367
7368         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7369                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7370         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7371                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7372         else
7373                 log "lru resize performs the same with no lru resize"
7374         fi
7375         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7376 }
7377 run_test 124b "lru resize (performance test) ======================="
7378
7379 test_125() { # 13358
7380         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7381         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7382         test_mkdir -p $DIR/d125 || error "mkdir failed"
7383         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7384         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7385         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7386 }
7387 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7388
7389 test_126() { # bug 12829/13455
7390         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7391         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7392         $GSS && skip "must run as gss disabled" && return
7393
7394         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7395         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7396         rm -f $DIR/$tfile
7397         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7398 }
7399 run_test 126 "check that the fsgid provided by the client is taken into account"
7400
7401 test_127a() { # bug 15521
7402         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7403         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7404         $LCTL set_param osc.*.stats=0
7405         FSIZE=$((2048 * 1024))
7406         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7407         cancel_lru_locks osc
7408         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7409
7410         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7411         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7412                 echo "got $COUNT $NAME"
7413                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7414                 eval $NAME=$COUNT || error "Wrong proc format"
7415
7416                 case $NAME in
7417                         read_bytes|write_bytes)
7418                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7419                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7420                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7421                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7422                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7423                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7424                                 error "sumsquare is too small: $SUMSQ"
7425                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7426                                 error "sumsquare is too big: $SUMSQ"
7427                         ;;
7428                         *) ;;
7429                 esac
7430         done < $DIR/${tfile}.tmp
7431
7432         #check that we actually got some stats
7433         [ "$read_bytes" ] || error "Missing read_bytes stats"
7434         [ "$write_bytes" ] || error "Missing write_bytes stats"
7435         [ "$read_bytes" != 0 ] || error "no read done"
7436         [ "$write_bytes" != 0 ] || error "no write done"
7437 }
7438 run_test 127a "verify the client stats are sane"
7439
7440 test_127b() { # bug LU-333
7441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7442         $LCTL set_param llite.*.stats=0
7443         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7444         # perform 2 reads and writes so MAX is different from SUM.
7445         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7446         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7447         cancel_lru_locks osc
7448         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7449         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7450
7451         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7452         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7453                 echo "got $COUNT $NAME"
7454                 eval $NAME=$COUNT || error "Wrong proc format"
7455
7456         case $NAME in
7457                 read_bytes)
7458                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7459                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7460                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7461                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7462                         ;;
7463                 write_bytes)
7464                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7465                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7466                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7467                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7468                         ;;
7469                         *) ;;
7470                 esac
7471         done < $TMP/${tfile}.tmp
7472
7473         #check that we actually got some stats
7474         [ "$read_bytes" ] || error "Missing read_bytes stats"
7475         [ "$write_bytes" ] || error "Missing write_bytes stats"
7476         [ "$read_bytes" != 0 ] || error "no read done"
7477         [ "$write_bytes" != 0 ] || error "no write done"
7478 }
7479 run_test 127b "verify the llite client stats are sane"
7480
7481 test_128() { # bug 15212
7482         touch $DIR/$tfile
7483         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7484                 find $DIR/$tfile
7485                 find $DIR/$tfile
7486         EOF
7487
7488         result=$(grep error $TMP/$tfile.log)
7489         rm -f $DIR/$tfile
7490         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7491 }
7492 run_test 128 "interactive lfs for 2 consecutive find's"
7493
7494 set_dir_limits () {
7495         local mntdev
7496         local canondev
7497         local node
7498
7499         local LDPROC=/proc/fs/ldiskfs
7500         local facets=$(get_facets MDS)
7501
7502         for facet in ${facets//,/ }; do
7503                 canondev=$(ldiskfs_canon \
7504                            *.$(convert_facet2label $facet).mntdev $facet)
7505                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7506                                                 LDPROC=/sys/fs/ldiskfs
7507                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7508         done
7509 }
7510
7511 test_129() {
7512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7513         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7514                 skip "Only applicable to ldiskfs-based MDTs"
7515                 return
7516         fi
7517         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7518
7519         EFBIG=27
7520         MAX=16384
7521
7522         set_dir_limits $MAX
7523         test_mkdir -p $DIR/$tdir
7524
7525         local I=0
7526         local J=0
7527         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7528                 $MULTIOP $DIR/$tdir/$J Oc
7529                 rc=$?
7530                 if [ $rc -eq $EFBIG ]; then
7531                         set_dir_limits 0
7532                         echo "return code $rc received as expected"
7533                         return 0
7534                 elif [ $rc -ne 0 ]; then
7535                         set_dir_limits 0
7536                         error_exit "return code $rc received instead of expected $EFBIG"
7537                 fi
7538                 J=$((J+1))
7539                 I=$(stat -c%s "$DIR/$tdir")
7540         done
7541
7542         set_dir_limits 0
7543         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7544 }
7545 run_test 129 "test directory size limit ========================"
7546
7547 OLDIFS="$IFS"
7548 cleanup_130() {
7549         trap 0
7550         IFS="$OLDIFS"
7551 }
7552
7553 test_130a() {
7554         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7555         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7556                 return
7557
7558         trap cleanup_130 EXIT RETURN
7559
7560         local fm_file=$DIR/$tfile
7561         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7562         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7563                 error "dd failed for $fm_file"
7564
7565         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7566         filefrag -ves $fm_file
7567         RC=$?
7568         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7569                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7570         [ $RC != 0 ] && error "filefrag $fm_file failed"
7571
7572         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7573                       grep -v "ext:" | grep -v "found")
7574         lun=$($GETSTRIPE -i $fm_file)
7575
7576         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7577         IFS=$'\n'
7578         tot_len=0
7579         for line in $filefrag_op
7580         do
7581                 frag_lun=`echo $line | cut -d: -f5`
7582                 ext_len=`echo $line | cut -d: -f4`
7583                 if (( $frag_lun != $lun )); then
7584                         cleanup_130
7585                         error "FIEMAP on 1-stripe file($fm_file) failed"
7586                         return
7587                 fi
7588                 (( tot_len += ext_len ))
7589         done
7590
7591         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7592                 cleanup_130
7593                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7594                 return
7595         fi
7596
7597         cleanup_130
7598
7599         echo "FIEMAP on single striped file succeeded"
7600 }
7601 run_test 130a "FIEMAP (1-stripe file)"
7602
7603 test_130b() {
7604         [ "$OSTCOUNT" -lt "2" ] &&
7605                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7606
7607         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7608         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7609                 return
7610
7611         trap cleanup_130 EXIT RETURN
7612
7613         local fm_file=$DIR/$tfile
7614         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7615         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7616                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7617
7618         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7619                 error "dd failed on $fm_file"
7620
7621         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7622         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7623                       grep -v "ext:" | grep -v "found")
7624
7625         last_lun=$(echo $filefrag_op | cut -d: -f5)
7626
7627         IFS=$'\n'
7628         tot_len=0
7629         num_luns=1
7630         for line in $filefrag_op
7631         do
7632                 frag_lun=`echo $line | cut -d: -f5`
7633                 ext_len=`echo $line | cut -d: -f4`
7634                 if (( $frag_lun != $last_lun )); then
7635                         if (( tot_len != 1024 )); then
7636                                 cleanup_130
7637                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7638                                 return
7639                         else
7640                                 (( num_luns += 1 ))
7641                                 tot_len=0
7642                         fi
7643                 fi
7644                 (( tot_len += ext_len ))
7645                 last_lun=$frag_lun
7646         done
7647         if (( num_luns != 2 || tot_len != 1024 )); then
7648                 cleanup_130
7649                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7650                 return
7651         fi
7652
7653         cleanup_130
7654
7655         echo "FIEMAP on 2-stripe file succeeded"
7656 }
7657 run_test 130b "FIEMAP (2-stripe file)"
7658
7659 test_130c() {
7660         [ "$OSTCOUNT" -lt "2" ] &&
7661                 skip_env "skipping FIEMAP on 2-stripe file" && return
7662
7663         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7664         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7665                 return
7666
7667         trap cleanup_130 EXIT RETURN
7668
7669         local fm_file=$DIR/$tfile
7670         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7671         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7672                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7673
7674         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7675
7676         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7677         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7678
7679         last_lun=`echo $filefrag_op | cut -d: -f5`
7680
7681         IFS=$'\n'
7682         tot_len=0
7683         num_luns=1
7684         for line in $filefrag_op
7685         do
7686                 frag_lun=`echo $line | cut -d: -f5`
7687                 ext_len=`echo $line | cut -d: -f4`
7688                 if (( $frag_lun != $last_lun )); then
7689                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7690                         if (( logical != 512 )); then
7691                                 cleanup_130
7692                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7693                                 return
7694                         fi
7695                         if (( tot_len != 512 )); then
7696                                 cleanup_130
7697                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7698                                 return
7699                         else
7700                                 (( num_luns += 1 ))
7701                                 tot_len=0
7702                         fi
7703                 fi
7704                 (( tot_len += ext_len ))
7705                 last_lun=$frag_lun
7706         done
7707         if (( num_luns != 2 || tot_len != 512 )); then
7708                 cleanup_130
7709                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7710                 return
7711         fi
7712
7713         cleanup_130
7714
7715         echo "FIEMAP on 2-stripe file with hole succeeded"
7716 }
7717 run_test 130c "FIEMAP (2-stripe file with hole)"
7718
7719 test_130d() {
7720         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7721
7722         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7723         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7724
7725         trap cleanup_130 EXIT RETURN
7726
7727         local fm_file=$DIR/$tfile
7728         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7729         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7730                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7731         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7732
7733         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7734         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7735
7736         last_lun=`echo $filefrag_op | cut -d: -f5`
7737
7738         IFS=$'\n'
7739         tot_len=0
7740         num_luns=1
7741         for line in $filefrag_op
7742         do
7743                 frag_lun=`echo $line | cut -d: -f5`
7744                 ext_len=`echo $line | cut -d: -f4`
7745                 if (( $frag_lun != $last_lun )); then
7746                         if (( tot_len != 1024 )); then
7747                                 cleanup_130
7748                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7749                                 return
7750                         else
7751                                 (( num_luns += 1 ))
7752                                 tot_len=0
7753                         fi
7754                 fi
7755                 (( tot_len += ext_len ))
7756                 last_lun=$frag_lun
7757         done
7758         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7759                 cleanup_130
7760                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7761                 return
7762         fi
7763
7764         cleanup_130
7765
7766         echo "FIEMAP on N-stripe file succeeded"
7767 }
7768 run_test 130d "FIEMAP (N-stripe file)"
7769
7770 test_130e() {
7771         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7772
7773         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7774         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7775
7776         trap cleanup_130 EXIT RETURN
7777
7778         local fm_file=$DIR/$tfile
7779         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7780         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7781                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7782
7783         NUM_BLKS=512
7784         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7785         for ((i = 0; i < $NUM_BLKS; i++))
7786         do
7787                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7788         done
7789
7790         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7791         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7792
7793         last_lun=`echo $filefrag_op | cut -d: -f5`
7794
7795         IFS=$'\n'
7796         tot_len=0
7797         num_luns=1
7798         for line in $filefrag_op
7799         do
7800                 frag_lun=`echo $line | cut -d: -f5`
7801                 ext_len=`echo $line | cut -d: -f4`
7802                 if (( $frag_lun != $last_lun )); then
7803                         if (( tot_len != $EXPECTED_LEN )); then
7804                                 cleanup_130
7805                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7806                                 return
7807                         else
7808                                 (( num_luns += 1 ))
7809                                 tot_len=0
7810                         fi
7811                 fi
7812                 (( tot_len += ext_len ))
7813                 last_lun=$frag_lun
7814         done
7815         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7816                 cleanup_130
7817                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7818                 return
7819         fi
7820
7821         cleanup_130
7822
7823         echo "FIEMAP with continuation calls succeeded"
7824 }
7825 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7826
7827 # Test for writev/readv
7828 test_131a() {
7829         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7830         error "writev test failed"
7831         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7832         error "readv failed"
7833         rm -f $DIR/$tfile
7834 }
7835 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7836
7837 test_131b() {
7838         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7839         error "append writev test failed"
7840         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7841         error "append writev test failed"
7842         rm -f $DIR/$tfile
7843 }
7844 run_test 131b "test append writev"
7845
7846 test_131c() {
7847         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7848         error "NOT PASS"
7849 }
7850 run_test 131c "test read/write on file w/o objects"
7851
7852 test_131d() {
7853         rwv -f $DIR/$tfile -w -n 1 1572864
7854         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7855         if [ "$NOB" != 1572864 ]; then
7856                 error "Short read filed: read $NOB bytes instead of 1572864"
7857         fi
7858         rm -f $DIR/$tfile
7859 }
7860 run_test 131d "test short read"
7861
7862 test_131e() {
7863         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7864         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7865         error "read hitting hole failed"
7866         rm -f $DIR/$tfile
7867 }
7868 run_test 131e "test read hitting hole"
7869
7870 get_ost_param() {
7871         local token=$1
7872         local gl_sum=0
7873         for node in $(osts_nodes); do
7874                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7875                 [ x$gl = x"" ] && gl=0
7876                 gl_sum=$((gl_sum + gl))
7877         done
7878         echo $gl
7879 }
7880
7881 som_mode_switch() {
7882         local som=$1
7883         local gl1=$2
7884         local gl2=$3
7885
7886         if [ x$som = x"enabled" ]; then
7887                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7888                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7889                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7890         else
7891                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7892                 MOUNTOPT="$MOUNTOPT,som_preview"
7893                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7894         fi
7895
7896         # do remount to make new mount-conf parameters actual
7897         echo remounting...
7898         sync
7899         stopall
7900         setupall
7901 }
7902
7903 test_132() { #1028, SOM
7904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7905         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7906         local num=$(get_mds_dir $DIR)
7907         local mymds=mds${num}
7908         local MOUNTOPT_SAVE=$MOUNTOPT
7909
7910         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7911         cancel_lru_locks osc
7912
7913         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7914
7915         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7916         stat $DIR/$tfile >/dev/null
7917         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7918         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7919         rm $DIR/$tfile
7920         som_mode_switch $som1 $gl1 $gl2
7921
7922         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7923         cancel_lru_locks osc
7924
7925         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7926         if [ $som1 == $som2 ]; then
7927             error "som is still "$som2
7928             if [ x$som2 = x"enabled" ]; then
7929                 som2="disabled"
7930             else
7931                 som2="enabled"
7932             fi
7933         fi
7934
7935         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7936         stat $DIR/$tfile >/dev/null
7937         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7938         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7939         som_mode_switch $som2 $gl1 $gl2
7940         MOUNTOPT=$MOUNTOPT_SAVE
7941 }
7942 run_test 132 "som avoids glimpse rpc"
7943
7944 check_stats() {
7945         local res
7946         local count
7947         case $1 in
7948         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7949                  ;;
7950         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7951                  ;;
7952         *) error "Wrong argument $1" ;;
7953         esac
7954         echo $res
7955         count=`echo $res | awk '{print $2}'`
7956         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7957         # if the argument $3 is zero, it means any stat increment is ok.
7958         if [ $3 -gt 0 ] ; then
7959                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7960         fi
7961 }
7962
7963 test_133a() {
7964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7965         remote_ost_nodsh && skip "remote OST with nodsh" && return
7966         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7967
7968         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7969                 { skip "MDS doesn't support rename stats"; return; }
7970         local testdir=$DIR/${tdir}/stats_testdir
7971         mkdir -p $DIR/${tdir}
7972
7973         # clear stats.
7974         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7975         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7976
7977         # verify mdt stats first.
7978         mkdir ${testdir} || error "mkdir failed"
7979         check_stats $SINGLEMDS "mkdir" 1
7980         touch ${testdir}/${tfile} || "touch failed"
7981         check_stats $SINGLEMDS "open" 1
7982         check_stats $SINGLEMDS "close" 1
7983         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7984         check_stats $SINGLEMDS "mknod" 1
7985         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7986         check_stats $SINGLEMDS "unlink" 1
7987         rm -f ${testdir}/${tfile} || error "file remove failed"
7988         check_stats $SINGLEMDS "unlink" 2
7989
7990         # remove working dir and check mdt stats again.
7991         rmdir ${testdir} || error "rmdir failed"
7992         check_stats $SINGLEMDS "rmdir" 1
7993
7994         local testdir1=$DIR/${tdir}/stats_testdir1
7995         mkdir -p ${testdir}
7996         mkdir -p ${testdir1}
7997         touch ${testdir1}/test1
7998         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7999         check_stats $SINGLEMDS "crossdir_rename" 1
8000
8001         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8002         check_stats $SINGLEMDS "samedir_rename" 1
8003
8004         rm -rf $DIR/${tdir}
8005 }
8006 run_test 133a "Verifying MDT stats ========================================"
8007
8008 test_133b() {
8009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8010         remote_ost_nodsh && skip "remote OST with nodsh" && return
8011         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8012         local testdir=$DIR/${tdir}/stats_testdir
8013         mkdir -p ${testdir} || error "mkdir failed"
8014         touch ${testdir}/${tfile} || "touch failed"
8015         cancel_lru_locks mdc
8016
8017         # clear stats.
8018         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8019         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8020
8021         # extra mdt stats verification.
8022         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8023         check_stats $SINGLEMDS "setattr" 1
8024         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8025         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8026         then            # LU-1740
8027                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8028                 check_stats $SINGLEMDS "getattr" 1
8029         fi
8030         $LFS df || error "lfs failed"
8031         check_stats $SINGLEMDS "statfs" 1
8032
8033         rm -rf $DIR/${tdir}
8034 }
8035 run_test 133b "Verifying extra MDT stats =================================="
8036
8037 test_133c() {
8038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8039         remote_ost_nodsh && skip "remote OST with nodsh" && return
8040         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8041         local testdir=$DIR/${tdir}/stats_testdir
8042         test_mkdir -p ${testdir} || error "mkdir failed"
8043
8044         # verify obdfilter stats.
8045         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8046         sync
8047         cancel_lru_locks osc
8048         wait_delete_completed
8049
8050         # clear stats.
8051         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8052         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8053
8054         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8055         sync
8056         cancel_lru_locks osc
8057         check_stats ost "write" 1
8058
8059         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8060         check_stats ost "read" 1
8061
8062         > ${testdir}/${tfile} || error "truncate failed"
8063         check_stats ost "punch" 1
8064
8065         rm -f ${testdir}/${tfile} || error "file remove failed"
8066         wait_delete_completed
8067         check_stats ost "destroy" 1
8068
8069         rm -rf $DIR/${tdir}
8070 }
8071 run_test 133c "Verifying OST stats ========================================"
8072
8073 order_2() {
8074     local value=$1
8075     local orig=$value
8076     local order=1
8077
8078     while [ $value -ge 2 ]; do
8079         order=$((order*2))
8080         value=$((value/2))
8081     done
8082
8083     if [ $orig -gt $order ]; then
8084         order=$((order*2))
8085     fi
8086     echo $order
8087 }
8088
8089 size_in_KMGT() {
8090     local value=$1
8091     local size=('K' 'M' 'G' 'T');
8092     local i=0
8093     local size_string=$value
8094
8095     while [ $value -ge 1024 ]; do
8096         if [ $i -gt 3 ]; then
8097             #T is the biggest unit we get here, if that is bigger,
8098             #just return XXXT
8099             size_string=${value}T
8100             break
8101         fi
8102         value=$((value >> 10))
8103         if [ $value -lt 1024 ]; then
8104             size_string=${value}${size[$i]}
8105             break
8106         fi
8107         i=$((i + 1))
8108     done
8109
8110     echo $size_string
8111 }
8112
8113 get_rename_size() {
8114     local size=$1
8115     local context=${2:-.}
8116     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8117                 grep -A1 $context |
8118                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8119     echo $sample
8120 }
8121
8122 test_133d() {
8123         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8124     remote_ost_nodsh && skip "remote OST with nodsh" && return
8125     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8126     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8127         { skip "MDS doesn't support rename stats"; return; }
8128
8129     local testdir1=$DIR/${tdir}/stats_testdir1
8130     local testdir2=$DIR/${tdir}/stats_testdir2
8131
8132     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8133
8134     test_mkdir -p ${testdir1} || error "mkdir failed"
8135     test_mkdir -p ${testdir2} || error "mkdir failed"
8136
8137     createmany -o $testdir1/test 512 || error "createmany failed"
8138
8139         # check samedir rename size
8140         mv ${testdir1}/test0 ${testdir1}/test_0
8141
8142         local testdir1_size=$(ls -l $DIR/${tdir} |
8143                 awk '/stats_testdir1/ {print $5}')
8144         local testdir2_size=$(ls -l $DIR/${tdir} |
8145                 awk '/stats_testdir2/ {print $5}')
8146
8147         testdir1_size=$(order_2 $testdir1_size)
8148         testdir2_size=$(order_2 $testdir2_size)
8149
8150         testdir1_size=$(size_in_KMGT $testdir1_size)
8151         testdir2_size=$(size_in_KMGT $testdir2_size)
8152
8153         echo "source rename dir size: ${testdir1_size}"
8154         echo "target rename dir size: ${testdir2_size}"
8155
8156     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8157     eval $cmd || error "$cmd failed"
8158     local samedir=$($cmd | grep 'same_dir')
8159     local same_sample=$(get_rename_size $testdir1_size)
8160     [ -z "$samedir" ] && error "samedir_rename_size count error"
8161     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8162     echo "Check same dir rename stats success"
8163
8164     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8165
8166     # check crossdir rename size
8167     mv ${testdir1}/test_0 ${testdir2}/test_0
8168
8169         testdir1_size=$(ls -l $DIR/${tdir} |
8170                 awk '/stats_testdir1/ {print $5}')
8171         testdir2_size=$(ls -l $DIR/${tdir} |
8172                 awk '/stats_testdir2/ {print $5}')
8173
8174         testdir1_size=$(order_2 $testdir1_size)
8175         testdir2_size=$(order_2 $testdir2_size)
8176
8177         testdir1_size=$(size_in_KMGT $testdir1_size)
8178         testdir2_size=$(size_in_KMGT $testdir2_size)
8179
8180         echo "source rename dir size: ${testdir1_size}"
8181         echo "target rename dir size: ${testdir2_size}"
8182
8183     eval $cmd || error "$cmd failed"
8184     local crossdir=$($cmd | grep 'crossdir')
8185     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8186     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8187     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8188     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8189     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8190     echo "Check cross dir rename stats success"
8191     rm -rf $DIR/${tdir}
8192 }
8193 run_test 133d "Verifying rename_stats ========================================"
8194
8195 test_133e() {
8196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8197         local testdir=$DIR/${tdir}/stats_testdir
8198         local ctr f0 f1 bs=32768 count=42 sum
8199
8200         remote_ost_nodsh && skip "remote OST with nodsh" && return
8201         mkdir -p ${testdir} || error "mkdir failed"
8202
8203         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8204
8205         for ctr in {write,read}_bytes; do
8206                 sync
8207                 cancel_lru_locks osc
8208
8209                 do_facet ost1 $LCTL set_param -n \
8210                         "obdfilter.*.exports.clear=clear"
8211
8212                 if [ $ctr = write_bytes ]; then
8213                         f0=/dev/zero
8214                         f1=${testdir}/${tfile}
8215                 else
8216                         f0=${testdir}/${tfile}
8217                         f1=/dev/null
8218                 fi
8219
8220                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8221                         error "dd failed"
8222                 sync
8223                 cancel_lru_locks osc
8224
8225                 sum=$(do_facet ost1 $LCTL get_param \
8226                                 "obdfilter.*.exports.*.stats" | \
8227                           awk -v ctr=$ctr '\
8228                                 BEGIN { sum = 0 }
8229                                 $1 == ctr { sum += $7 }
8230                                 END { print sum }')
8231
8232                 if ((sum != bs * count)); then
8233                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8234                 fi
8235         done
8236
8237         rm -rf $DIR/${tdir}
8238 }
8239 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8240
8241 test_140() { #bug-17379
8242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8243         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8244         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8245         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8246
8247         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8248         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8249         local i=0
8250         while i=`expr $i + 1`; do
8251                 test_mkdir -p $i || error "Creating dir $i"
8252                 cd $i || error "Changing to $i"
8253                 ln -s ../stat stat || error "Creating stat symlink"
8254                 # Read the symlink until ELOOP present,
8255                 # not LBUGing the system is considered success,
8256                 # we didn't overrun the stack.
8257                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8258                 [ $ret -ne 0 ] && {
8259                         if [ $ret -eq 40 ]; then
8260                                 break  # -ELOOP
8261                         else
8262                                 error "Open stat symlink"
8263                                 return
8264                         fi
8265                 }
8266         done
8267         i=`expr $i - 1`
8268         echo "The symlink depth = $i"
8269         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8270                                         error "Invalid symlink depth"
8271
8272         # Test recursive symlink
8273         ln -s symlink_self symlink_self
8274         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8275         echo "open symlink_self returns $ret"
8276         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8277 }
8278 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8279
8280 test_150() {
8281         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8282         local TF="$TMP/$tfile"
8283
8284         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8285         cp $TF $DIR/$tfile
8286         cancel_lru_locks osc
8287         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8288         remount_client $MOUNT
8289         df -P $MOUNT
8290         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8291
8292         $TRUNCATE $TF 6000
8293         $TRUNCATE $DIR/$tfile 6000
8294         cancel_lru_locks osc
8295         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8296
8297         echo "12345" >>$TF
8298         echo "12345" >>$DIR/$tfile
8299         cancel_lru_locks osc
8300         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8301
8302         echo "12345" >>$TF
8303         echo "12345" >>$DIR/$tfile
8304         cancel_lru_locks osc
8305         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8306
8307         rm -f $TF
8308         true
8309 }
8310 run_test 150 "truncate/append tests"
8311
8312 function roc_hit() {
8313         local list=$(comma_list $(osts_nodes))
8314
8315         echo $(get_osd_param $list '' stats |
8316                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8317 }
8318
8319 function set_cache() {
8320         local on=1
8321
8322         if [ "$2" == "off" ]; then
8323                 on=0;
8324         fi
8325         local list=$(comma_list $(osts_nodes))
8326         set_osd_param $list '' $1_cache_enable $on
8327
8328         cancel_lru_locks osc
8329 }
8330
8331 test_151() {
8332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8333         remote_ost_nodsh && skip "remote OST with nodsh" && return
8334
8335         local CPAGES=3
8336         local list=$(comma_list $(osts_nodes))
8337
8338         # check whether obdfilter is cache capable at all
8339         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8340                 echo "not cache-capable obdfilter"
8341                 return 0
8342         fi
8343
8344         # check cache is enabled on all obdfilters
8345         if get_osd_param $list '' read_cache_enable | grep 0; then
8346                 echo "oss cache is disabled"
8347                 return 0
8348         fi
8349
8350         set_osd_param $list '' writethrough_cache_enable 1
8351
8352         # pages should be in the case right after write
8353         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
8354         local BEFORE=`roc_hit`
8355         cancel_lru_locks osc
8356         cat $DIR/$tfile >/dev/null
8357         local AFTER=`roc_hit`
8358         if ! let "AFTER - BEFORE == CPAGES"; then
8359                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8360         fi
8361
8362         # the following read invalidates the cache
8363         cancel_lru_locks osc
8364         set_osd_param $list '' read_cache_enable 0
8365         cat $DIR/$tfile >/dev/null
8366
8367         # now data shouldn't be found in the cache
8368         BEFORE=`roc_hit`
8369         cancel_lru_locks osc
8370         cat $DIR/$tfile >/dev/null
8371         AFTER=`roc_hit`
8372         if let "AFTER - BEFORE != 0"; then
8373                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8374         fi
8375
8376         set_osd_param $list '' read_cache_enable 1
8377         rm -f $DIR/$tfile
8378 }
8379 run_test 151 "test cache on oss and controls ==============================="
8380
8381 test_152() {
8382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8383         local TF="$TMP/$tfile"
8384
8385         # simulate ENOMEM during write
8386 #define OBD_FAIL_OST_NOMEM      0x226
8387         lctl set_param fail_loc=0x80000226
8388         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8389         cp $TF $DIR/$tfile
8390         sync || error "sync failed"
8391         lctl set_param fail_loc=0
8392
8393         # discard client's cache
8394         cancel_lru_locks osc
8395
8396         # simulate ENOMEM during read
8397         lctl set_param fail_loc=0x80000226
8398         cmp $TF $DIR/$tfile || error "cmp failed"
8399         lctl set_param fail_loc=0
8400
8401         rm -f $TF
8402 }
8403 run_test 152 "test read/write with enomem ============================"
8404
8405 test_153() {
8406         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8407 }
8408 run_test 153 "test if fdatasync does not crash ======================="
8409
8410 test_154() {
8411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8412         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8413                 { skip "Need MDS version at least 2.2.51"; return 0; }
8414
8415         cp /etc/hosts $DIR/$tfile
8416
8417         fid=$($LFS path2fid $DIR/$tfile)
8418         rc=$?
8419         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8420
8421         ffid=$DIR/.lustre/fid/$fid
8422
8423         echo "stat fid $fid"
8424         stat $ffid > /dev/null || error "stat $ffid failed."
8425         echo "touch fid $fid"
8426         touch $ffid || error "touch $ffid failed."
8427         echo "write to fid $fid"
8428         cat /etc/hosts > $ffid || error "write $ffid failed."
8429         echo "read fid $fid"
8430         diff /etc/hosts $ffid || error "read $ffid failed."
8431         echo "append write to fid $fid"
8432         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8433         echo "rename fid $fid"
8434         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
8435         touch $DIR/$tfile.1
8436         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
8437         rm -f $DIR/$tfile.1
8438         echo "truncate fid $fid"
8439         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8440         echo "link fid $fid"
8441         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
8442         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8443                 echo "setfacl fid $fid"
8444                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8445                 echo "getfacl fid $fid"
8446                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8447         fi
8448         echo "unlink fid $fid"
8449         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
8450         echo "mknod fid $fid"
8451         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8452
8453         fid=[0xf00000400:0x1:0x0]
8454         ffid=$DIR/.lustre/fid/$fid
8455
8456         echo "stat non-exist fid $fid"
8457         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8458         echo "write to non-exist fid $fid"
8459         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8460         echo "link new fid $fid"
8461         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8462
8463         test_mkdir -p $DIR/$tdir
8464         touch $DIR/$tdir/$tfile
8465         fid=$($LFS path2fid $DIR/$tdir)
8466         rc=$?
8467         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8468
8469         ffid=$DIR/.lustre/fid/$fid
8470
8471         echo "ls $fid"
8472         ls $ffid > /dev/null || error "ls $ffid failed."
8473         echo "touch $fid/$tfile.1"
8474         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8475
8476         echo "touch $DIR/.lustre/fid/$tfile"
8477         touch $DIR/.lustre/fid/$tfile && \
8478                 error "touch $DIR/.lustre/fid/$tfile should fail."
8479
8480         echo "setxattr to $DIR/.lustre/fid"
8481         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8482                 error "setxattr should fail."
8483
8484         echo "listxattr for $DIR/.lustre/fid"
8485         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8486                 error "listxattr should fail."
8487
8488         echo "delxattr from $DIR/.lustre/fid"
8489         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8490                 error "delxattr should fail."
8491
8492         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8493         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8494                 error "touch invalid fid should fail."
8495
8496         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8497         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8498                 error "touch non-normal fid should fail."
8499
8500         echo "rename $tdir to $DIR/.lustre/fid"
8501         mrename $DIR/$tdir $DIR/.lustre/fid &&
8502                 error "rename to $DIR/.lustre/fid should fail."
8503
8504         echo "rename .lustre to itself"
8505         fid=$($LFS path2fid $DIR)
8506         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8507                 error "rename .lustre to itself should fail."
8508
8509         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8510         fid=$($LFS path2fid $DIR/$tfile-2)
8511         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8512         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8513                 error "create lov data thru .lustre should fail."
8514         echo "cp /etc/passwd $DIR/$tfile-2"
8515         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8516         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8517         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8518                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8519
8520         echo "Open-by-FID succeeded"
8521 }
8522 run_test 154 "Open-by-FID"
8523
8524 test_155_small_load() {
8525     local temp=$TMP/$tfile
8526     local file=$DIR/$tfile
8527
8528     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8529         error "dd of=$temp bs=6096 count=1 failed"
8530     cp $temp $file
8531     cancel_lru_locks osc
8532     cmp $temp $file || error "$temp $file differ"
8533
8534     $TRUNCATE $temp 6000
8535     $TRUNCATE $file 6000
8536     cmp $temp $file || error "$temp $file differ (truncate1)"
8537
8538     echo "12345" >>$temp
8539     echo "12345" >>$file
8540     cmp $temp $file || error "$temp $file differ (append1)"
8541
8542     echo "12345" >>$temp
8543     echo "12345" >>$file
8544     cmp $temp $file || error "$temp $file differ (append2)"
8545
8546     rm -f $temp $file
8547     true
8548 }
8549
8550 test_155_big_load() {
8551     remote_ost_nodsh && skip "remote OST with nodsh" && return
8552     local temp=$TMP/$tfile
8553     local file=$DIR/$tfile
8554
8555     free_min_max
8556     local cache_size=$(do_facet ost$((MAXI+1)) \
8557         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8558     local large_file_size=$((cache_size * 2))
8559
8560     echo "OSS cache size: $cache_size KB"
8561     echo "Large file size: $large_file_size KB"
8562
8563     [ $MAXV -le $large_file_size ] && \
8564         skip_env "max available OST size needs > $large_file_size KB" && \
8565         return 0
8566
8567     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8568
8569     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8570         error "dd of=$temp bs=$large_file_size count=1k failed"
8571     cp $temp $file
8572     ls -lh $temp $file
8573     cancel_lru_locks osc
8574     cmp $temp $file || error "$temp $file differ"
8575
8576     rm -f $temp $file
8577     true
8578 }
8579
8580 test_155a() {
8581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8582         set_cache read on
8583         set_cache writethrough on
8584         test_155_small_load
8585 }
8586 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8587
8588 test_155b() {
8589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8590         set_cache read on
8591         set_cache writethrough off
8592         test_155_small_load
8593 }
8594 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8595
8596 test_155c() {
8597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8598         set_cache read off
8599         set_cache writethrough on
8600         test_155_small_load
8601 }
8602 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8603
8604 test_155d() {
8605         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8606         set_cache read off
8607         set_cache writethrough off
8608         test_155_small_load
8609 }
8610 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8611
8612 test_155e() {
8613         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8614         set_cache read on
8615         set_cache writethrough on
8616         test_155_big_load
8617 }
8618 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8619
8620 test_155f() {
8621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8622         set_cache read on
8623         set_cache writethrough off
8624         test_155_big_load
8625 }
8626 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8627
8628 test_155g() {
8629         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8630         set_cache read off
8631         set_cache writethrough on
8632         test_155_big_load
8633 }
8634 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8635
8636 test_155h() {
8637         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8638         set_cache read off
8639         set_cache writethrough off
8640         test_155_big_load
8641 }
8642 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8643
8644 test_156() {
8645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8646         local CPAGES=3
8647         local BEFORE
8648         local AFTER
8649         local file="$DIR/$tfile"
8650
8651         [ "$(facet_fstype ost1)" = "zfs" ] &&
8652                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8653                 return
8654
8655     log "Turn on read and write cache"
8656     set_cache read on
8657     set_cache writethrough on
8658
8659     log "Write data and read it back."
8660     log "Read should be satisfied from the cache."
8661     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8662     BEFORE=`roc_hit`
8663     cancel_lru_locks osc
8664     cat $file >/dev/null
8665     AFTER=`roc_hit`
8666     if ! let "AFTER - BEFORE == CPAGES"; then
8667         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8668     else
8669         log "cache hits:: before: $BEFORE, after: $AFTER"
8670     fi
8671
8672     log "Read again; it should be satisfied from the cache."
8673     BEFORE=$AFTER
8674     cancel_lru_locks osc
8675     cat $file >/dev/null
8676     AFTER=`roc_hit`
8677     if ! let "AFTER - BEFORE == CPAGES"; then
8678         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8679     else
8680         log "cache hits:: before: $BEFORE, after: $AFTER"
8681     fi
8682
8683
8684     log "Turn off the read cache and turn on the write cache"
8685     set_cache read off
8686     set_cache writethrough on
8687
8688     log "Read again; it should be satisfied from the cache."
8689     BEFORE=`roc_hit`
8690     cancel_lru_locks osc
8691     cat $file >/dev/null
8692     AFTER=`roc_hit`
8693     if ! let "AFTER - BEFORE == CPAGES"; then
8694         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8695     else
8696         log "cache hits:: before: $BEFORE, after: $AFTER"
8697     fi
8698
8699     log "Read again; it should not be satisfied from the cache."
8700     BEFORE=$AFTER
8701     cancel_lru_locks osc
8702     cat $file >/dev/null
8703     AFTER=`roc_hit`
8704     if ! let "AFTER - BEFORE == 0"; then
8705         error "IN CACHE: before: $BEFORE, after: $AFTER"
8706     else
8707         log "cache hits:: before: $BEFORE, after: $AFTER"
8708     fi
8709
8710     log "Write data and read it back."
8711     log "Read should be satisfied from the cache."
8712     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8713     BEFORE=`roc_hit`
8714     cancel_lru_locks osc
8715     cat $file >/dev/null
8716     AFTER=`roc_hit`
8717     if ! let "AFTER - BEFORE == CPAGES"; then
8718         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8719     else
8720         log "cache hits:: before: $BEFORE, after: $AFTER"
8721     fi
8722
8723     log "Read again; it should not be satisfied from the cache."
8724     BEFORE=$AFTER
8725     cancel_lru_locks osc
8726     cat $file >/dev/null
8727     AFTER=`roc_hit`
8728     if ! let "AFTER - BEFORE == 0"; then
8729         error "IN CACHE: before: $BEFORE, after: $AFTER"
8730     else
8731         log "cache hits:: before: $BEFORE, after: $AFTER"
8732     fi
8733
8734
8735     log "Turn off read and write cache"
8736     set_cache read off
8737     set_cache writethrough off
8738
8739     log "Write data and read it back"
8740     log "It should not be satisfied from the cache."
8741     rm -f $file
8742     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8743     cancel_lru_locks osc
8744     BEFORE=`roc_hit`
8745     cat $file >/dev/null
8746     AFTER=`roc_hit`
8747     if ! let "AFTER - BEFORE == 0"; then
8748         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8749     else
8750         log "cache hits:: before: $BEFORE, after: $AFTER"
8751     fi
8752
8753
8754     log "Turn on the read cache and turn off the write cache"
8755     set_cache read on
8756     set_cache writethrough off
8757
8758     log "Write data and read it back"
8759     log "It should not be satisfied from the cache."
8760     rm -f $file
8761     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8762     BEFORE=`roc_hit`
8763     cancel_lru_locks osc
8764     cat $file >/dev/null
8765     AFTER=`roc_hit`
8766     if ! let "AFTER - BEFORE == 0"; then
8767         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8768     else
8769         log "cache hits:: before: $BEFORE, after: $AFTER"
8770     fi
8771
8772     log "Read again; it should be satisfied from the cache."
8773     BEFORE=`roc_hit`
8774     cancel_lru_locks osc
8775     cat $file >/dev/null
8776     AFTER=`roc_hit`
8777     if ! let "AFTER - BEFORE == CPAGES"; then
8778         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8779     else
8780         log "cache hits:: before: $BEFORE, after: $AFTER"
8781     fi
8782
8783     rm -f $file
8784 }
8785 run_test 156 "Verification of tunables ============================"
8786
8787 #Changelogs
8788 err17935 () {
8789     if [ $MDSCOUNT -gt 1 ]; then
8790         error_ignore 17935 $*
8791     else
8792         error $*
8793     fi
8794 }
8795
8796 changelog_chmask()
8797 {
8798     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8799            grep -c $1)
8800
8801     if [ $MASK -eq 1 ]; then
8802         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8803     else
8804         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8805     fi
8806 }
8807
8808 test_160() {
8809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8810     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8811     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8812         { skip "Need MDS version at least 2.2.0"; return; }
8813     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8814     echo "Registered as changelog user $USER"
8815     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8816         grep -q $USER || error "User $USER not found in changelog_users"
8817
8818     # change something
8819     test_mkdir -p $DIR/$tdir/pics/2008/zachy
8820     touch $DIR/$tdir/pics/2008/zachy/timestamp
8821     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8822     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8823     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8824     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8825     rm $DIR/$tdir/pics/desktop.jpg
8826
8827     $LFS changelog $MDT0 | tail -5
8828
8829     echo "verifying changelog mask"
8830     changelog_chmask "MKDIR"
8831     changelog_chmask "CLOSE"
8832
8833     test_mkdir -p $DIR/$tdir/pics/zach/sofia
8834     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8835
8836     changelog_chmask "MKDIR"
8837     changelog_chmask "CLOSE"
8838
8839     test_mkdir -p $DIR/$tdir/pics/2008/sofia
8840     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8841
8842     $LFS changelog $MDT0
8843     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8844     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8845     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8846     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8847
8848     # verify contents
8849     echo "verifying target fid"
8850     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8851         tail -1 | awk '{print $6}')
8852     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8853     [ "$fidc" == "t=$fidf" ] || \
8854         err17935 "fid in changelog $fidc != file fid $fidf"
8855     echo "verifying parent fid"
8856     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8857         tail -1 | awk '{print $7}')
8858     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8859     [ "$fidc" == "p=$fidf" ] || \
8860         err17935 "pfid in changelog $fidc != dir fid $fidf"
8861
8862     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8863         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8864     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8865     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8866         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8867     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8868     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8869         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8870
8871     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8872         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8873     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8874     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8875     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8876         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8877
8878     echo "verifying user deregister"
8879     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8880     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8881         grep -q $USER && error "User $USER still found in changelog_users"
8882
8883     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8884         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8885     if [ $USERS -eq 0 ]; then
8886         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8887             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8888         touch $DIR/$tdir/chloe
8889         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8890             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8891         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8892         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8893     else
8894         echo "$USERS other changelog users; can't verify off"
8895     fi
8896 }
8897 run_test 160 "changelog sanity"
8898
8899 test_161() {
8900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8901     test_mkdir -p $DIR/$tdir
8902     cp /etc/hosts $DIR/$tdir/$tfile
8903     test_mkdir $DIR/$tdir/foo1
8904     test_mkdir $DIR/$tdir/foo2
8905     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8906     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8907     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8908     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8909     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8910     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8911         $LFS fid2path $DIR $FID
8912         err17935 "bad link ea"
8913     fi
8914     # middle
8915     rm $DIR/$tdir/foo2/zachary
8916     # last
8917     rm $DIR/$tdir/foo2/thor
8918     # first
8919     rm $DIR/$tdir/$tfile
8920     # rename
8921     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8922     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8923         then
8924         $LFS fid2path $DIR $FID
8925         err17935 "bad link rename"
8926     fi
8927     rm $DIR/$tdir/foo2/maggie
8928
8929     # overflow the EA
8930     local longname=filename_avg_len_is_thirty_two_
8931     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8932         error "failed to hardlink many files"
8933     links=$($LFS fid2path $DIR $FID | wc -l)
8934     echo -n "${links}/1000 links in link EA"
8935     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8936     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8937         error "failed to unlink many hardlinks"
8938 }
8939 run_test 161 "link ea sanity"
8940
8941 check_path() {
8942     local expected=$1
8943     shift
8944     local fid=$2
8945
8946     local path=$(${LFS} fid2path $*)
8947     RC=$?
8948
8949     if [ $RC -ne 0 ]; then
8950         err17935 "path looked up of $expected failed. Error $RC"
8951         return $RC
8952     elif [ "${path}" != "${expected}" ]; then
8953         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8954         return 2
8955     fi
8956     echo "fid $fid resolves to path $path (expected $expected)"
8957 }
8958
8959 test_162() {
8960         # Make changes to filesystem
8961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8962         test_mkdir -p $DIR/$tdir/d2
8963         touch $DIR/$tdir/d2/$tfile
8964         touch $DIR/$tdir/d2/x1
8965         touch $DIR/$tdir/d2/x2
8966         test_mkdir -p $DIR/$tdir/d2/a/b/c
8967         test_mkdir -p $DIR/$tdir/d2/p/q/r
8968         # regular file
8969         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8970         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8971
8972         # softlink
8973         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8974         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8975         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8976
8977         # softlink to wrong file
8978         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8979         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8980         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8981
8982         # hardlink
8983         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8984         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8985         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8986         # fid2path dir/fsname should both work
8987         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8988         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8989
8990         # hardlink count: check that there are 2 links
8991         # Doesnt work with CMD yet: 17935
8992         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8993                 err17935 "expected 2 links"
8994
8995         # hardlink indexing: remove the first link
8996         rm $DIR/$tdir/d2/p/q/r/hlink
8997         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8998
8999         return 0
9000 }
9001 run_test 162 "path lookup sanity"
9002
9003 test_163() {
9004         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9005         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9006         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
9007         copytool $FSNAME &
9008         sleep 1
9009         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
9010         # this proc file is temporary and linux-only
9011         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
9012          error "kernel->userspace send failed"
9013         kill -INT $!
9014 }
9015 run_test 163 "kernel <-> userspace comms"
9016
9017 test_169() {
9018         # do directio so as not to populate the page cache
9019         log "creating a 10 Mb file"
9020         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9021         log "starting reads"
9022         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9023         log "truncating the file"
9024         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9025         log "killing dd"
9026         kill %+ || true # reads might have finished
9027         echo "wait until dd is finished"
9028         wait
9029         log "removing the temporary file"
9030         rm -rf $DIR/$tfile || error "tmp file removal failed"
9031 }
9032 run_test 169 "parallel read and truncate should not deadlock"
9033
9034 test_170() {
9035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9036         $LCTL clear     # bug 18514
9037         $LCTL debug_daemon start $TMP/${tfile}_log_good
9038         touch $DIR/$tfile
9039         $LCTL debug_daemon stop
9040         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9041                error "sed failed to read log_good"
9042
9043         $LCTL debug_daemon start $TMP/${tfile}_log_good
9044         rm -rf $DIR/$tfile
9045         $LCTL debug_daemon stop
9046
9047         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9048                error "lctl df log_bad failed"
9049
9050         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9051         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9052
9053         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9054         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9055
9056         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9057                 error "bad_line good_line1 good_line2 are empty"
9058
9059         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9060         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9061         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9062
9063         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9064         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9065         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9066
9067         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9068                 error "bad_line_new good_line_new are empty"
9069
9070         local expected_good=$((good_line1 + good_line2*2))
9071
9072         rm -f $TMP/${tfile}*
9073         # LU-231, short malformed line may not be counted into bad lines
9074         if [ $bad_line -ne $bad_line_new ] &&
9075                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9076                 error "expected $bad_line bad lines, but got $bad_line_new"
9077                 return 1
9078         fi
9079
9080         if [ $expected_good -ne $good_line_new ]; then
9081                 error "expected $expected_good good lines, but got $good_line_new"
9082                 return 2
9083         fi
9084         true
9085 }
9086 run_test 170 "test lctl df to handle corrupted log ====================="
9087
9088 test_171() { # bug20592
9089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9090 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9091         $LCTL set_param fail_loc=0x50e
9092         $LCTL set_param fail_val=3000
9093         multiop_bg_pause $DIR/$tfile O_s || true
9094         local MULTIPID=$!
9095         kill -USR1 $MULTIPID
9096         # cause log dump
9097         sleep 3
9098         wait $MULTIPID
9099         if dmesg | grep "recursive fault"; then
9100                 error "caught a recursive fault"
9101         fi
9102         $LCTL set_param fail_loc=0
9103         true
9104 }
9105 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
9106
9107 # it would be good to share it with obdfilter-survey/libecho code
9108 setup_obdecho_osc () {
9109         local rc=0
9110         local ost_nid=$1
9111         local obdfilter_name=$2
9112         echo "Creating new osc for $obdfilter_name on $ost_nid"
9113         # make sure we can find loopback nid
9114         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
9115
9116         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
9117                            ${obdfilter_name}_osc_UUID || rc=2; }
9118         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
9119                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
9120         return $rc
9121 }
9122
9123 cleanup_obdecho_osc () {
9124         local obdfilter_name=$1
9125         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
9126         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
9127         return 0
9128 }
9129
9130 obdecho_create_test() {
9131         local OBD=$1
9132         local node=$2
9133         local rc=0
9134         local id
9135         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
9136         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
9137                            rc=2; }
9138         if [ $rc -eq 0 ]; then
9139             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
9140             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
9141         fi
9142         echo "New object id is $id"
9143         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
9144                            rc=4; }
9145         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
9146                                         "cleanup" || rc=5; }
9147         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
9148                                         "detach" || rc=6; }
9149         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
9150         return $rc
9151 }
9152
9153 test_180a() {
9154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9155         remote_ost_nodsh && skip "remote OST with nodsh" && return
9156         local rc=0
9157         local rmmod_local=0
9158
9159         if ! module_loaded obdecho; then
9160             load_module obdecho/obdecho
9161             rmmod_local=1
9162         fi
9163
9164         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
9165         local host=$(lctl get_param -n osc.$osc.import |
9166                              awk '/current_connection:/ {print $2}' )
9167         local target=$(lctl get_param -n osc.$osc.import |
9168                              awk '/target:/ {print $2}' )
9169         target=${target%_UUID}
9170
9171         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
9172         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
9173         [[ -n $target ]] && cleanup_obdecho_osc $target
9174         [ $rmmod_local -eq 1 ] && rmmod obdecho
9175         return $rc
9176 }
9177 run_test 180a "test obdecho on osc"
9178
9179 test_180b() {
9180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9181         remote_ost_nodsh && skip "remote OST with nodsh" && return
9182         local rc=0
9183         local rmmod_remote=0
9184
9185         do_facet ost1 "lsmod | grep -q obdecho || "                      \
9186                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
9187                       "modprobe obdecho; }" && rmmod_remote=1
9188         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
9189         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
9190         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
9191         return $rc
9192 }
9193 run_test 180b "test obdecho directly on obdfilter"
9194
9195 test_181() { # bug 22177
9196         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9197         # create enough files to index the directory
9198         createmany -o $DIR/$tdir/foobar 4000
9199         # print attributes for debug purpose
9200         lsattr -d .
9201         # open dir
9202         multiop_bg_pause $DIR/$tdir D_Sc || return 1
9203         MULTIPID=$!
9204         # remove the files & current working dir
9205         unlinkmany $DIR/$tdir/foobar 4000
9206         rmdir $DIR/$tdir
9207         kill -USR1 $MULTIPID
9208         wait $MULTIPID
9209         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9210         return 0
9211 }
9212 run_test 181 "Test open-unlinked dir ========================"
9213
9214 test_182() {
9215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9216         # disable MDC RPC lock wouldn't crash client
9217         local fcount=1000
9218         local tcount=4
9219
9220         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9221 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9222         $LCTL set_param fail_loc=0x804
9223
9224         for (( i=0; i < $tcount; i++ )) ; do
9225                 mkdir $DIR/$tdir/$i
9226                 createmany -o $DIR/$tdir/$i/f- $fcount &
9227         done
9228         wait
9229
9230         for (( i=0; i < $tcount; i++ )) ; do
9231                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9232         done
9233         wait
9234
9235         rm -rf $DIR/$tdir
9236
9237         $LCTL set_param fail_loc=0
9238 }
9239 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9240
9241 test_183() { # LU-2275
9242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9243         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9244         echo aaa > $DIR/$tdir/$tfile
9245
9246 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9247         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9248
9249         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9250         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9251
9252         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9253
9254         # Flush negative dentry cache
9255         touch $DIR/$tdir/$tfile
9256
9257         # We are not checking for any leaked references here, they'll
9258         # become evident next time we do cleanup with module unload.
9259         rm -rf $DIR/$tdir
9260 }
9261 run_test 183 "No crash or request leak in case of strange dispositions ========"
9262
9263 # OST pools tests
9264 check_file_in_pool()
9265 {
9266         local file=$1
9267         local pool=$2
9268         local tlist="$3"
9269         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9270         for i in $res
9271         do
9272                 for t in $tlist ; do
9273                         [ "$i" -eq "$t" ] && continue 2
9274                 done
9275
9276                 echo "pool list: $tlist"
9277                 echo "striping: $res"
9278                 error_noexit "$file not allocated in $pool"
9279                 return 1
9280         done
9281         return 0
9282 }
9283
9284 pool_add() {
9285         echo "Creating new pool"
9286         local pool=$1
9287
9288         create_pool $FSNAME.$pool ||
9289                 { error_noexit "No pool created, result code $?"; return 1; }
9290         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9291                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9292 }
9293
9294 pool_add_targets() {
9295         echo "Adding targets to pool"
9296         local pool=$1
9297         local first=$2
9298         local last=$3
9299         local step=${4:-1}
9300
9301         local list=$(seq $first $step $last)
9302
9303         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9304         do_facet mgs $LCTL pool_add \
9305                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9306         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9307                         | sort -u | tr '\n' ' ' " "$t" || { 
9308                 error_noexit "Add to pool failed"
9309                 return 1
9310         }
9311         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9312         local addcount=$(((last - first) / step + 1))
9313         [ $lfscount -eq $addcount ] || {
9314                 error_noexit "lfs pool_list bad ost count" \
9315                                                 "$lfscount != $addcount"
9316                 return 2
9317         }
9318 }
9319
9320 pool_set_dir() {
9321         local pool=$1
9322         local tdir=$2
9323         echo "Setting pool on directory $tdir"
9324
9325         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9326
9327         error_noexit "Cannot set pool $pool to $tdir"
9328         return 1
9329 }
9330
9331 pool_check_dir() {
9332         local pool=$1
9333         local tdir=$2
9334         echo "Checking pool on directory $tdir"
9335
9336         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9337         [ "$res" = "$pool" ] && return 0
9338
9339         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9340         return 1
9341 }
9342
9343 pool_dir_rel_path() {
9344         echo "Testing relative path works well"
9345         local pool=$1
9346         local tdir=$2
9347         local root=$3
9348
9349         mkdir -p $root/$tdir/$tdir
9350         cd $root/$tdir
9351         pool_set_dir $pool $tdir          || return 1
9352         pool_set_dir $pool ./$tdir        || return 2
9353         pool_set_dir $pool ../$tdir       || return 3
9354         pool_set_dir $pool ../$tdir/$tdir || return 4
9355         rm -rf $tdir; cd - > /dev/null
9356 }
9357
9358 pool_alloc_files() {
9359         echo "Checking files allocation from directory pool"
9360         local pool=$1
9361         local tdir=$2
9362         local count=$3
9363         local tlist="$4"
9364
9365         local failed=0
9366         for i in $(seq -w 1 $count)
9367         do
9368                 local file=$tdir/file-$i
9369                 touch $file
9370                 check_file_in_pool $file $pool "$tlist" || \
9371                         failed=$((failed + 1))
9372         done
9373         [ "$failed" = 0 ] && return 0
9374
9375         error_noexit "$failed files not allocated in $pool"
9376         return 1
9377 }
9378
9379 pool_create_files() {
9380         echo "Creating files in pool"
9381         local pool=$1
9382         local tdir=$2
9383         local count=$3
9384         local tlist="$4"
9385
9386         mkdir -p $tdir
9387         local failed=0
9388         for i in $(seq -w 1 $count)
9389         do
9390                 local file=$tdir/spoo-$i
9391                 $SETSTRIPE -p $pool $file
9392                 check_file_in_pool $file $pool "$tlist" || \
9393                         failed=$((failed + 1))
9394         done
9395         [ "$failed" = 0 ] && return 0
9396
9397         error_noexit "$failed files not allocated in $pool"
9398         return 1
9399 }
9400
9401 pool_lfs_df() {
9402         echo "Checking 'lfs df' output"
9403         local pool=$1
9404
9405         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9406                         tr '\n' ' ')
9407         local res=$($LFS df --pool $FSNAME.$pool |
9408                         awk '{print $1}' |
9409                         grep "$FSNAME-OST" |
9410                         tr '\n' ' ')
9411         [ "$res" = "$t" ] && return 0
9412
9413         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9414         return 1
9415 }
9416
9417 pool_file_rel_path() {
9418         echo "Creating files in a pool with relative pathname"
9419         local pool=$1
9420         local tdir=$2
9421
9422         mkdir -p $tdir ||
9423                 { error_noexit "unable to create $tdir"; return 1 ; }
9424         local file="/..$tdir/$tfile-1"
9425         $SETSTRIPE -p $pool $file ||
9426                 { error_noexit "unable to create $file" ; return 2 ; }
9427
9428         cd $tdir
9429         $SETSTRIPE -p $pool $tfile-2 || {
9430                 error_noexit "unable to create $tfile-2 in $tdir"
9431                 return 3
9432         }
9433 }
9434
9435 pool_remove_first_target() {
9436         echo "Removing first target from a pool"
9437         local pool=$1
9438
9439         local pname="lov.$FSNAME-*.pools.$pool"
9440         local t=$($LCTL get_param -n $pname | head -1)
9441         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9442         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9443                 error_noexit "$t not removed from $FSNAME.$pool"
9444                 return 1
9445         }
9446 }
9447
9448 pool_remove_all_targets() {
9449         echo "Removing all targets from pool"
9450         local pool=$1
9451         local file=$2
9452         local pname="lov.$FSNAME-*.pools.$pool"
9453         for t in $($LCTL get_param -n $pname | sort -u)
9454         do
9455                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9456         done
9457         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9458                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9459                 return 1
9460         }
9461         # striping on an empty/nonexistant pool should fall back 
9462         # to "pool of everything"
9463         touch $file || {
9464                 error_noexit "failed to use fallback striping for empty pool"
9465                 return 2
9466         }
9467         # setstripe on an empty pool should fail
9468         $SETSTRIPE -p $pool $file 2>/dev/null && {
9469                 error_noexit "expected failure when creating file" \
9470                                                         "with empty pool"
9471                 return 3
9472         }
9473         return 0
9474 }
9475
9476 pool_remove() {
9477         echo "Destroying pool"
9478         local pool=$1
9479         local file=$2
9480
9481         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9482
9483         sleep 2
9484         # striping on an empty/nonexistant pool should fall back 
9485         # to "pool of everything"
9486         touch $file || {
9487                 error_noexit "failed to use fallback striping for missing pool"
9488                 return 1
9489         }
9490         # setstripe on an empty pool should fail
9491         $SETSTRIPE -p $pool $file 2>/dev/null && {
9492                 error_noexit "expected failure when creating file" \
9493                                                         "with missing pool"
9494                 return 2
9495         }
9496
9497         # get param should return err once pool is gone
9498         if wait_update $HOSTNAME "lctl get_param -n \
9499                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9500         then
9501                 remove_pool_from_list $FSNAME.$pool
9502                 return 0
9503         fi
9504         error_noexit "Pool $FSNAME.$pool is not destroyed"
9505         return 3
9506 }
9507
9508 test_200() {
9509         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9510         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9511
9512         local POOL=${POOL:-cea1}
9513         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9514         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9515         # Pool OST targets
9516         local first_ost=0
9517         local last_ost=$(($OSTCOUNT - 1))
9518         local ost_step=2
9519         local ost_list=$(seq $first_ost $ost_step $last_ost)
9520         local ost_range="$first_ost $last_ost $ost_step"
9521         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9522         local file_dir=$POOL_ROOT/file_tst
9523
9524         local rc=0
9525         while : ; do
9526                 # former test_200a test_200b
9527                 pool_add $POOL                          || { rc=$? ; break; }
9528                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9529                 # former test_200c test_200d
9530                 mkdir -p $test_path
9531                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9532                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9533                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9534                                                         || { rc=$? ; break; }
9535                 # former test_200e test_200f
9536                 local files=$((OSTCOUNT*3))
9537                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9538                                                         || { rc=$? ; break; }
9539                 pool_create_files $POOL $file_dir $files "$ost_list" \
9540                                                         || { rc=$? ; break; }
9541                 # former test_200g test_200h
9542                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9543                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9544
9545                 # former test_201a test_201b test_201c
9546                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9547
9548                 local f=$test_path/$tfile
9549                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9550                 pool_remove $POOL $f                    || { rc=$? ; break; }
9551                 break
9552         done
9553
9554         cleanup_pools
9555         return $rc
9556 }
9557 run_test 200 "OST pools"
9558
9559 # usage: default_attr <count | size | offset>
9560 default_attr() {
9561         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9562 }
9563
9564 # usage: check_default_stripe_attr
9565 check_default_stripe_attr() {
9566         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9567         case $1 in
9568         --stripe-count|--count)
9569                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9570         --stripe-size|--size)
9571                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9572         --stripe-index|--index)
9573                 EXPECTED=-1;;
9574         *)
9575                 error "unknown getstripe attr '$1'"
9576         esac
9577
9578         [ $ACTUAL != $EXPECTED ] &&
9579                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9580 }
9581
9582 test_204a() {
9583         test_mkdir -p $DIR/$tdir
9584         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9585
9586         check_default_stripe_attr --stripe-count
9587         check_default_stripe_attr --stripe-size
9588         check_default_stripe_attr --stripe-index
9589
9590         return 0
9591 }
9592 run_test 204a "Print default stripe attributes ================="
9593
9594 test_204b() {
9595         test_mkdir -p $DIR/$tdir
9596         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9597
9598         check_default_stripe_attr --stripe-size
9599         check_default_stripe_attr --stripe-index
9600
9601         return 0
9602 }
9603 run_test 204b "Print default stripe size and offset  ==========="
9604
9605 test_204c() {
9606         test_mkdir -p $DIR/$tdir
9607         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9608
9609         check_default_stripe_attr --stripe-count
9610         check_default_stripe_attr --stripe-index
9611
9612         return 0
9613 }
9614 run_test 204c "Print default stripe count and offset ==========="
9615
9616 test_204d() {
9617         test_mkdir -p $DIR/$tdir
9618         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9619
9620         check_default_stripe_attr --stripe-count
9621         check_default_stripe_attr --stripe-size
9622
9623         return 0
9624 }
9625 run_test 204d "Print default stripe count and size ============="
9626
9627 test_204e() {
9628         test_mkdir -p $DIR/$tdir
9629         $SETSTRIPE -d $DIR/$tdir
9630
9631         check_default_stripe_attr --stripe-count --raw
9632         check_default_stripe_attr --stripe-size --raw
9633         check_default_stripe_attr --stripe-index --raw
9634
9635         return 0
9636 }
9637 run_test 204e "Print raw stripe attributes ================="
9638
9639 test_204f() {
9640         test_mkdir -p $DIR/$tdir
9641         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9642
9643         check_default_stripe_attr --stripe-size --raw
9644         check_default_stripe_attr --stripe-index --raw
9645
9646         return 0
9647 }
9648 run_test 204f "Print raw stripe size and offset  ==========="
9649
9650 test_204g() {
9651         test_mkdir -p $DIR/$tdir
9652         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9653
9654         check_default_stripe_attr --stripe-count --raw
9655         check_default_stripe_attr --stripe-index --raw
9656
9657         return 0
9658 }
9659 run_test 204g "Print raw stripe count and offset ==========="
9660
9661 test_204h() {
9662         test_mkdir -p $DIR/$tdir
9663         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9664
9665         check_default_stripe_attr --stripe-count --raw
9666         check_default_stripe_attr --stripe-size --raw
9667
9668         return 0
9669 }
9670 run_test 204h "Print raw stripe count and size ============="
9671
9672 # Figure out which job scheduler is being used, if any,
9673 # or use a fake one
9674 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9675         JOBENV=SLURM_JOB_ID
9676 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9677         JOBENV=LSB_JOBID
9678 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9679         JOBENV=PBS_JOBID
9680 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9681         JOBENV=LOADL_STEP_ID
9682 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9683         JOBENV=JOB_ID
9684 else
9685         JOBENV=FAKE_JOBID
9686 fi
9687
9688 verify_jobstats() {
9689         local cmd=$1
9690         local target=$2
9691
9692         # clear old jobstats
9693         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9694         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9695
9696         # use a new JobID for this test, or we might see an old one
9697         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9698
9699         JOBVAL=${!JOBENV}
9700         log "Test: $cmd"
9701         log "Using JobID environment variable $JOBENV=$JOBVAL"
9702
9703         if [ $JOBENV = "FAKE_JOBID" ]; then
9704                 FAKE_JOBID=$JOBVAL $cmd
9705         else
9706                 $cmd
9707         fi
9708
9709         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9710                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9711                 do_facet $FACET lctl get_param mdt.*.job_stats |
9712                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9713         fi
9714         if [ "$target" = "ost" -o "$target" = "both" ]; then
9715                 FACET=ost1
9716                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9717                         grep $JOBVAL || error "No job stats found on OST $FACET"
9718         fi
9719 }
9720
9721 test_205() { # Job stats
9722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9723         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9724                 skip "Server doesn't support jobstats" && return 0
9725
9726         local cmd
9727         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9728         if [ $OLD_JOBENV != $JOBENV ]; then
9729                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9730                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9731                         $JOBENV || return 1
9732         fi
9733
9734         # mkdir
9735         cmd="mkdir $DIR/$tfile"
9736         verify_jobstats "$cmd" "mdt"
9737         # rmdir
9738         cmd="rm -fr $DIR/$tfile"
9739         verify_jobstats "$cmd" "mdt"
9740         # mknod
9741         cmd="mknod $DIR/$tfile c 1 3"
9742         verify_jobstats "$cmd" "mdt"
9743         # unlink
9744         cmd="rm -f $DIR/$tfile"
9745         verify_jobstats "$cmd" "mdt"
9746         # open & close
9747         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9748         verify_jobstats "$cmd" "mdt"
9749         # setattr
9750         cmd="touch $DIR/$tfile"
9751         verify_jobstats "$cmd" "both"
9752         # write
9753         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9754         verify_jobstats "$cmd" "ost"
9755         # read
9756         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9757         verify_jobstats "$cmd" "ost"
9758         # truncate
9759         cmd="$TRUNCATE $DIR/$tfile 0"
9760         verify_jobstats "$cmd" "both"
9761         # rename
9762         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9763         verify_jobstats "$cmd" "mdt"
9764
9765         # cleanup
9766         rm -f $DIR/jobstats_test_rename
9767
9768         if [ $OLD_JOBENV != $JOBENV ]; then
9769                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9770                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9771                         $OLD_JOBENV || return 1
9772         fi
9773 }
9774 run_test 205 "Verify job stats"
9775
9776 # LU-1480, LU-1773 and LU-1657
9777 test_206() {
9778         mkdir -p $DIR/$tdir
9779         lfs setstripe -c -1 $DIR/$tdir
9780 #define OBD_FAIL_LOV_INIT 0x1403
9781         $LCTL set_param fail_loc=0xa0001403
9782         $LCTL set_param fail_val=1
9783         touch $DIR/$tdir/$tfile || true
9784 }
9785 run_test 206 "fail lov_init_raid0() doesn't lbug"
9786
9787 test_207a() {
9788         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9789         local fsz=`stat -c %s $DIR/$tfile`
9790         cancel_lru_locks mdc
9791
9792         # do not return layout in getattr intent
9793 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
9794         $LCTL set_param fail_loc=0x170
9795         local sz=`stat -c %s $DIR/$tfile`
9796
9797         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
9798
9799         rm -rf $DIR/$tfile
9800 }
9801 run_test 207a "can refresh layout at glimpse"
9802
9803 test_207b() {
9804         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9805         local cksum=`md5sum $DIR/$tfile`
9806         local fsz=`stat -c %s $DIR/$tfile`
9807         cancel_lru_locks mdc
9808         cancel_lru_locks osc
9809
9810         # do not return layout in getattr intent
9811 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
9812         $LCTL set_param fail_loc=0x171
9813
9814         # it will refresh layout after the file is opened but before read issues
9815         echo checksum is "$cksum"
9816         echo "$cksum" |md5sum -c --quiet || error "file differs"
9817
9818         rm -rf $DIR/$tfile
9819 }
9820 run_test 207b "can refresh layout at open"
9821
9822 test_212() {
9823         size=`date +%s`
9824         size=$((size % 8192 + 1))
9825         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9826         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9827         rm -f $DIR/f212 $DIR/f212.xyz
9828 }
9829 run_test 212 "Sendfile test ============================================"
9830
9831 test_213() {
9832         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9833         cancel_lru_locks osc
9834         lctl set_param fail_loc=0x8000040f
9835         # generate a read lock
9836         cat $DIR/$tfile > /dev/null
9837         # write to the file, it will try to cancel the above read lock.
9838         cat /etc/hosts >> $DIR/$tfile
9839 }
9840 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9841
9842 test_214() { # for bug 20133
9843         test_mkdir -p $DIR/d214p/d214c
9844         for (( i=0; i < 340; i++ )) ; do
9845                 touch $DIR/d214p/d214c/a$i
9846         done
9847
9848         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9849         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9850         ls $DIR/d214c || error "ls $DIR/d214c failed"
9851         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9852 }
9853 run_test 214 "hash-indexed directory test - bug 20133"
9854
9855 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9856 create_lnet_proc_files() {
9857         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9858         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9859
9860         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9861         rm -f "$TMP/lnet_$1.sys_tmp"
9862 }
9863
9864 # counterpart of create_lnet_proc_files
9865 remove_lnet_proc_files() {
9866         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9867 }
9868
9869 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9870 # 3rd arg as regexp for body
9871 check_lnet_proc_stats() {
9872         local l=$(cat "$TMP/lnet_$1" |wc -l)
9873         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9874
9875         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9876 }
9877
9878 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9879 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9880 # optional and can be regexp for 2nd line (lnet.routes case)
9881 check_lnet_proc_entry() {
9882         local blp=2            # blp stands for 'position of 1st line of body'
9883         [ "$5" = "" ] || blp=3 # lnet.routes case
9884
9885         local l=$(cat "$TMP/lnet_$1" |wc -l)
9886         # subtracting one from $blp because the body can be empty
9887         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9888
9889         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9890                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9891
9892         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9893                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9894
9895         # bail out if any unexpected line happened
9896         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9897         [ "$?" != 0 ] || error "$2 misformatted"
9898 }
9899
9900 test_215() { # for bugs 18102, 21079, 21517
9901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9902         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9903         local P='[1-9][0-9]*'           # positive numeric
9904         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9905         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9906         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9907         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9908
9909         local L1 # regexp for 1st line
9910         local L2 # regexp for 2nd line (optional)
9911         local BR # regexp for the rest (body)
9912
9913         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9914         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9915         create_lnet_proc_files "stats"
9916         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9917         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9918         remove_lnet_proc_files "stats"
9919
9920         # /proc/sys/lnet/routes should look like this:
9921         # Routing disabled/enabled
9922         # net hops state router
9923         # where net is a string like tcp0, hops >= 0, state is up/down,
9924         # router is a string like 192.168.1.1@tcp2
9925         L1="^Routing (disabled|enabled)$"
9926         L2="^net +hops +state +router$"
9927         BR="^$NET +$N +(up|down) +$NID$"
9928         create_lnet_proc_files "routes"
9929         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9930         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9931         remove_lnet_proc_files "routes"
9932
9933         # /proc/sys/lnet/routers should look like this:
9934         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9935         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9936         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9937         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9938         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9939         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9940         create_lnet_proc_files "routers"
9941         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9942         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9943         remove_lnet_proc_files "routers"
9944
9945         # /proc/sys/lnet/peers should look like this:
9946         # nid refs state last max rtr min tx min queue
9947         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9948         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9949         # numeric (0 or >0 or <0), queue >= 0.
9950         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9951         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9952         create_lnet_proc_files "peers"
9953         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9954         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9955         remove_lnet_proc_files "peers"
9956
9957         # /proc/sys/lnet/buffers  should look like this:
9958         # pages count credits min
9959         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9960         L1="^pages +count +credits +min$"
9961         BR="^ +$N +$N +$I +$I$"
9962         create_lnet_proc_files "buffers"
9963         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9964         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9965         remove_lnet_proc_files "buffers"
9966
9967         # /proc/sys/lnet/nis should look like this:
9968         # nid status alive refs peer rtr max tx min
9969         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9970         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9971         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9972         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9973         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9974         create_lnet_proc_files "nis"
9975         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9976         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9977         remove_lnet_proc_files "nis"
9978
9979         # can we successfully write to /proc/sys/lnet/stats?
9980         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9981         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9982 }
9983 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9984
9985 test_216() { # bug 20317
9986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9987         remote_ost_nodsh && skip "remote OST with nodsh" && return
9988         local node
9989         local p="$TMP/sanityN-$TESTNAME.parameters"
9990         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9991         for node in $(osts_nodes); do
9992                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9993                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9994                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9995         done
9996         clear_osc_stats
9997
9998         # agressive lockless i/o settings
9999         for node in $(osts_nodes); do
10000                 do_node $node 'lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes 2000000; lctl set_param -n ldlm.namespaces.filter-*.contended_locks 0; lctl set_param -n ldlm.namespaces.filter-*.contention_seconds 60'
10001         done
10002         lctl set_param -n osc.*.contention_seconds 60
10003
10004         $DIRECTIO write $DIR/$tfile 0 10 4096
10005         $CHECKSTAT -s 40960 $DIR/$tfile
10006
10007         # disable lockless i/o
10008         for node in $(osts_nodes); do
10009                 do_node $node 'lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes 0; lctl set_param -n ldlm.namespaces.filter-*.contended_locks 32; lctl set_param -n ldlm.namespaces.filter-*.contention_seconds 0'
10010         done
10011         lctl set_param -n osc.*.contention_seconds 0
10012         clear_osc_stats
10013
10014         dd if=/dev/zero of=$DIR/$tfile count=0
10015         $CHECKSTAT -s 0 $DIR/$tfile
10016
10017         restore_lustre_params <$p
10018         rm -f $p
10019         rm $DIR/$tfile
10020 }
10021 run_test 216 "check lockless direct write works and updates file size and kms correctly"
10022
10023 test_217() { # bug 22430
10024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10025         local node
10026         local nid
10027
10028         for node in $(nodes_list); do
10029                 nid=$(host_nids_address $node $NETTYPE)
10030                 if [[ $nid = *-* ]] ; then
10031                         echo "lctl ping $nid@$NETTYPE"
10032                         lctl ping $nid@$NETTYPE
10033                 else
10034                         echo "skipping $node (no hyphen detected)"
10035                 fi
10036         done
10037 }
10038 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
10039
10040 test_218() {
10041        # do directio so as not to populate the page cache
10042        log "creating a 10 Mb file"
10043        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10044        log "starting reads"
10045        dd if=$DIR/$tfile of=/dev/null bs=4096 &
10046        log "truncating the file"
10047        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10048        log "killing dd"
10049        kill %+ || true # reads might have finished
10050        echo "wait until dd is finished"
10051        wait
10052        log "removing the temporary file"
10053        rm -rf $DIR/$tfile || error "tmp file removal failed"
10054 }
10055 run_test 218 "parallel read and truncate should not deadlock ======================="
10056
10057 test_219() {
10058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10059         # write one partial page
10060         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
10061         # set no grant so vvp_io_commit_write will do sync write
10062         $LCTL set_param fail_loc=0x411
10063         # write a full page at the end of file
10064         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
10065
10066         $LCTL set_param fail_loc=0
10067         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
10068         $LCTL set_param fail_loc=0x411
10069         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
10070 }
10071 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
10072
10073 test_220() { #LU-325
10074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10075         remote_ost_nodsh && skip "remote OST with nodsh" && return
10076         local OSTIDX=0
10077
10078         test_mkdir -p $DIR/$tdir
10079         local OST=$(lfs osts | grep ${OSTIDX}": " | \
10080                 awk '{print $2}' | sed -e 's/_UUID$//')
10081
10082         # on the mdt's osc
10083         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
10084         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
10085                         osc.$mdtosc_proc1.prealloc_last_id)
10086         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
10087                         osc.$mdtosc_proc1.prealloc_next_id)
10088
10089         $LFS df -i
10090
10091         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
10092         #define OBD_FAIL_OST_ENOINO              0x229
10093         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
10094         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
10095         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
10096
10097         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
10098
10099         MDSOBJS=$((last_id - next_id))
10100         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
10101
10102         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
10103         echo "OST still has $count kbytes free"
10104
10105         echo "create $MDSOBJS files @next_id..."
10106         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
10107
10108         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10109                         osc.$mdtosc_proc1.prealloc_last_id)
10110         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10111                         osc.$mdtosc_proc1.prealloc_next_id)
10112
10113         echo "after creation, last_id=$last_id2, next_id=$next_id2"
10114         $LFS df -i
10115
10116         echo "cleanup..."
10117
10118         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
10119         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
10120
10121         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
10122         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
10123         echo "unlink $MDSOBJS files @$next_id..."
10124         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
10125 }
10126 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
10127
10128 test_221() {
10129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10130         dd if=`which date` of=$MOUNT/date oflag=sync
10131         chmod +x $MOUNT/date
10132
10133         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
10134         $LCTL set_param fail_loc=0x80001401
10135
10136         $MOUNT/date > /dev/null
10137         rm -f $MOUNT/date
10138 }
10139 run_test 221 "make sure fault and truncate race to not cause OOM"
10140
10141 test_222a () {
10142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10143        rm -rf $DIR/$tdir
10144        test_mkdir -p $DIR/$tdir
10145        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10146        createmany -o $DIR/$tdir/$tfile 10
10147        cancel_lru_locks mdc
10148        cancel_lru_locks osc
10149        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10150        $LCTL set_param fail_loc=0x31a
10151        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
10152        $LCTL set_param fail_loc=0
10153        rm -r $DIR/$tdir
10154 }
10155 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
10156
10157 test_222b () {
10158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10159        rm -rf $DIR/$tdir
10160        test_mkdir -p $DIR/$tdir
10161        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10162        createmany -o $DIR/$tdir/$tfile 10
10163        cancel_lru_locks mdc
10164        cancel_lru_locks osc
10165        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10166        $LCTL set_param fail_loc=0x31a
10167        rm -r $DIR/$tdir || "AGL for rmdir failed"
10168        $LCTL set_param fail_loc=0
10169 }
10170 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
10171
10172 test_223 () {
10173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10174        rm -rf $DIR/$tdir
10175        test_mkdir -p $DIR/$tdir
10176        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10177        createmany -o $DIR/$tdir/$tfile 10
10178        cancel_lru_locks mdc
10179        cancel_lru_locks osc
10180        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
10181        $LCTL set_param fail_loc=0x31b
10182        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
10183        $LCTL set_param fail_loc=0
10184        rm -r $DIR/$tdir
10185 }
10186 run_test 223 "osc reenqueue if without AGL lock granted ======================="
10187
10188 test_224a() { # LU-1039, MRP-303
10189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10190         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
10191         $LCTL set_param fail_loc=0x508
10192         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
10193         $LCTL set_param fail_loc=0
10194         df $DIR
10195 }
10196 run_test 224a "Don't panic on bulk IO failure"
10197
10198 test_224b() { # LU-1039, MRP-303
10199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10200         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
10201         cancel_lru_locks osc
10202         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
10203         $LCTL set_param fail_loc=0x515
10204         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
10205         $LCTL set_param fail_loc=0
10206         df $DIR
10207 }
10208 run_test 224b "Don't panic on bulk IO failure"
10209
10210 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
10211 test_225a () {
10212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10213        if [ -z ${MDSSURVEY} ]; then
10214               skip_env "mds-survey not found" && return
10215        fi
10216        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10217             { skip "Need MDS version at least 2.2.51"; return; }
10218
10219        local mds=$(facet_host $SINGLEMDS)
10220        local target=$(do_nodes $mds 'lctl dl' | \
10221                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10222
10223        local cmd1="file_count=1000 thrhi=4"
10224        local cmd2="dir_count=2 layer=mdd stripe_count=0"
10225        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10226        local cmd="$cmd1 $cmd2 $cmd3"
10227
10228        rm -f ${TMP}/mds_survey*
10229        echo + $cmd
10230        eval $cmd || error "mds-survey with zero-stripe failed"
10231        cat ${TMP}/mds_survey*
10232        rm -f ${TMP}/mds_survey*
10233 }
10234 run_test 225a "Metadata survey sanity with zero-stripe"
10235
10236 test_225b () {
10237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10238        if [ -z ${MDSSURVEY} ]; then
10239               skip_env "mds-survey not found" && return
10240        fi
10241        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10242             { skip "Need MDS version at least 2.2.51"; return; }
10243
10244        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
10245               skip_env "Need to mount OST to test" && return
10246        fi
10247
10248        local mds=$(facet_host $SINGLEMDS)
10249        local target=$(do_nodes $mds 'lctl dl' | \
10250                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10251
10252        local cmd1="file_count=1000 thrhi=4"
10253        local cmd2="dir_count=2 layer=mdd stripe_count=1"
10254        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10255        local cmd="$cmd1 $cmd2 $cmd3"
10256
10257        rm -f ${TMP}/mds_survey*
10258        echo + $cmd
10259        eval $cmd || error "mds-survey with stripe_count failed"
10260        cat ${TMP}/mds_survey*
10261        rm -f ${TMP}/mds_survey*
10262 }
10263 run_test 225b "Metadata survey sanity with stripe_count = 1"
10264
10265 mcreate_path2fid () {
10266         local mode=$1
10267         local major=$2
10268         local minor=$3
10269         local name=$4
10270         local desc=$5
10271         local path=$DIR/$tdir/$name
10272         local fid
10273         local rc
10274         local fid_path
10275
10276         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10277                 error "cannot create $desc"
10278
10279         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
10280         rc=$?
10281         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10282
10283         fid_path=$($LFS fid2path $MOUNT $fid)
10284         rc=$?
10285         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10286
10287         [ "$path" == "$fid_path" ] ||
10288                 error "fid2path returned $fid_path, expected $path"
10289
10290         echo "pass with $path and $fid"
10291 }
10292
10293 test_226 () {
10294         rm -rf $DIR/$tdir
10295         mkdir -p $DIR/$tdir
10296
10297         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10298         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10299         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10300         mcreate_path2fid 0040666 0 0 dir "directory"
10301         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10302         mcreate_path2fid 0100666 0 0 file "regular file"
10303         mcreate_path2fid 0120666 0 0 link "symbolic link"
10304         mcreate_path2fid 0140666 0 0 sock "socket"
10305 }
10306 run_test 226 "call path2fid and fid2path on files of all type"
10307
10308 # LU-1299 Executing or running ldd on a truncated executable does not
10309 # cause an out-of-memory condition.
10310 test_227() {
10311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10312         dd if=`which date` of=$MOUNT/date bs=1k count=1
10313         chmod +x $MOUNT/date
10314
10315         $MOUNT/date > /dev/null
10316         ldd $MOUNT/date > /dev/null
10317         rm -f $MOUNT/date
10318 }
10319 run_test 227 "running truncated executable does not cause OOM"
10320
10321 # LU-1512 try to reuse idle OI blocks
10322 test_228a() {
10323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10324         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10325                 skip "non-ldiskfs backend" && return
10326
10327         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10328         local myDIR=$DIR/$tdir
10329
10330         mkdir -p $myDIR
10331         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10332         $LCTL set_param fail_loc=0x80001002
10333         createmany -o $myDIR/t- 10000
10334         $LCTL set_param fail_loc=0
10335         # The guard is current the largest FID holder
10336         touch $myDIR/guard
10337         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10338                     tr -d '[')
10339         local IDX=$(($SEQ % 64))
10340
10341         do_facet $SINGLEMDS sync
10342         # Make sure journal flushed.
10343         sleep 6
10344         local blk1=$(do_facet $SINGLEMDS \
10345                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10346                      grep Blockcount | awk '{print $4}')
10347
10348         # Remove old files, some OI blocks will become idle.
10349         unlinkmany $myDIR/t- 10000
10350         # Create new files, idle OI blocks should be reused.
10351         createmany -o $myDIR/t- 2000
10352         do_facet $SINGLEMDS sync
10353         # Make sure journal flushed.
10354         sleep 6
10355         local blk2=$(do_facet $SINGLEMDS \
10356                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10357                      grep Blockcount | awk '{print $4}')
10358
10359         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10360 }
10361 run_test 228a "try to reuse idle OI blocks"
10362
10363 test_228b() {
10364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10365         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10366                 skip "non-ldiskfs backend" && return
10367
10368         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10369         local myDIR=$DIR/$tdir
10370
10371         mkdir -p $myDIR
10372         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10373         $LCTL set_param fail_loc=0x80001002
10374         createmany -o $myDIR/t- 10000
10375         $LCTL set_param fail_loc=0
10376         # The guard is current the largest FID holder
10377         touch $myDIR/guard
10378         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10379                     tr -d '[')
10380         local IDX=$(($SEQ % 64))
10381
10382         do_facet $SINGLEMDS sync
10383         # Make sure journal flushed.
10384         sleep 6
10385         local blk1=$(do_facet $SINGLEMDS \
10386                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10387                      grep Blockcount | awk '{print $4}')
10388
10389         # Remove old files, some OI blocks will become idle.
10390         unlinkmany $myDIR/t- 10000
10391
10392         # stop the MDT
10393         stop $SINGLEMDS || error "Fail to stop MDT."
10394         # remount the MDT
10395         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10396
10397         df $MOUNT || error "Fail to df."
10398         # Create new files, idle OI blocks should be reused.
10399         createmany -o $myDIR/t- 2000
10400         do_facet $SINGLEMDS sync
10401         # Make sure journal flushed.
10402         sleep 6
10403         local blk2=$(do_facet $SINGLEMDS \
10404                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10405                      grep Blockcount | awk '{print $4}')
10406
10407         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10408 }
10409 run_test 228b "idle OI blocks can be reused after MDT restart"
10410
10411 #LU-1881
10412 test_228c() {
10413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10414         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10415                 skip "non-ldiskfs backend" && return
10416
10417         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10418         local myDIR=$DIR/$tdir
10419
10420         mkdir -p $myDIR
10421         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10422         $LCTL set_param fail_loc=0x80001002
10423         # 20000 files can guarantee there are index nodes in the OI file
10424         createmany -o $myDIR/t- 20000
10425         $LCTL set_param fail_loc=0
10426         # The guard is current the largest FID holder
10427         touch $myDIR/guard
10428         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10429                     tr -d '[')
10430         local IDX=$(($SEQ % 64))
10431
10432         do_facet $SINGLEMDS sync
10433         # Make sure journal flushed.
10434         sleep 6
10435         local blk1=$(do_facet $SINGLEMDS \
10436                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10437                      grep Blockcount | awk '{print $4}')
10438
10439         # Remove old files, some OI blocks will become idle.
10440         unlinkmany $myDIR/t- 20000
10441         rm -f $myDIR/guard
10442         # The OI file should become empty now
10443
10444         # Create new files, idle OI blocks should be reused.
10445         createmany -o $myDIR/t- 2000
10446         do_facet $SINGLEMDS sync
10447         # Make sure journal flushed.
10448         sleep 6
10449         local blk2=$(do_facet $SINGLEMDS \
10450                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10451                      grep Blockcount | awk '{print $4}')
10452
10453         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10454 }
10455 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10456
10457 test_230a() {
10458         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10459         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10460         local MDTIDX=1
10461
10462         mkdir -p $DIR/$tdir/test_230_local
10463         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10464         [ $mdt_idx -ne 0 ] &&
10465                 error "create local directory on wrong MDT $mdt_idx"
10466
10467         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10468                         error "create remote directory failed"
10469         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10470         [ $mdt_idx -ne $MDTIDX ] &&
10471                 error "create remote directory on wrong MDT $mdt_idx"
10472
10473         createmany -o $DIR/$tdir/test_230/t- 10 ||
10474                 error "create files on remote directory failed"
10475         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10476         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10477         rm -r $DIR/$tdir || error "unlink remote directory failed"
10478 }
10479 run_test 230a "Create remote directory and files under the remote directory"
10480
10481 test_230b() {
10482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10483         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10484         local MDTIDX=1
10485         local remote_dir=$DIR/$tdir/remote_dir
10486
10487         mkdir -p $DIR/$tdir
10488         $LFS mkdir -i $MDTIDX $remote_dir ||
10489                 error "create remote directory failed"
10490
10491         $LFS mkdir -i 0 $remote_dir/new_dir &&
10492                 error "nested remote directory create succeed!"
10493
10494         rm -r $DIR/$tdir || error "unlink remote directory failed"
10495 }
10496 run_test 230b "nested remote directory should be failed"
10497
10498 #
10499 # tests that do cleanup/setup should be run at the end
10500 #
10501
10502 test_900() {
10503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10504         local ls
10505         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
10506         $LCTL set_param fail_loc=0x903
10507         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
10508         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
10509                 echo "clear" > $ls
10510         done
10511         FAIL_ON_ERROR=true cleanup
10512         FAIL_ON_ERROR=true setup
10513 }
10514 run_test 900 "umount should not race with any mgc requeue thread"
10515
10516 complete $SECONDS
10517 check_and_cleanup_lustre
10518 if [ "$I_MOUNTED" != "yes" ]; then
10519         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
10520 fi
10521 exit_status