Whamcloud - gitweb
LU-1431 ptlrpc: Support for over 1MB bulk I/O RPC
[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 cleanup_multiop_pause() {
915         trap 0
916         kill -USR1 $MULTIPID
917 }
918
919 test_24q() {
920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
921         test_mkdir $DIR/R13a
922         test_mkdir $DIR/R13b
923         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
924         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
925         MULTIPID=$!
926
927         trap cleanup_multiop_pause EXIT
928         mrename $DIR/R13a $DIR/R13b
929         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
930         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
931         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
932         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
933         cleanup_multiop_pause
934         wait $MULTIPID || error "multiop close failed"
935 }
936 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
937
938 test_24r() { #bug 3789
939         test_mkdir $DIR/R14a
940         test_mkdir $DIR/R14a/b
941         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
942         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
943         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
944 }
945 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
946
947 test_24s() {
948         test_mkdir $DIR/R15a
949         test_mkdir $DIR/R15a/b
950         test_mkdir $DIR/R15a/b/c
951         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
952         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
953         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
954 }
955 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
956 test_24t() {
957         test_mkdir $DIR/R16a
958         test_mkdir $DIR/R16a/b
959         test_mkdir $DIR/R16a/b/c
960         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
961         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
962         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
963 }
964 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
965
966 test_24u() { # bug12192
967         rm -rf $DIR/$tfile
968         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
969         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
970 }
971 run_test 24u "create stripe file"
972
973 page_size() {
974         getconf PAGE_SIZE
975 }
976
977 simple_cleanup_common() {
978         trap 0
979         rm -rf $DIR/$tdir
980         wait_delete_completed
981 }
982
983 max_pages_per_rpc() {
984         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
985 }
986
987 test_24v() {
988         local NRFILES=100000
989         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
990         [ $FREE_INODES -lt $NRFILES ] && \
991                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
992                 return
993
994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
995         trap simple_cleanup_common EXIT
996
997         mkdir -p $DIR/$tdir
998         createmany -m $DIR/$tdir/$tfile $NRFILES
999
1000         cancel_lru_locks mdc
1001         lctl set_param mdc.*.stats clear
1002
1003         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1004
1005         # LU-5 large readdir
1006         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1007         #               8 bytes for name(filename is mostly 5 in this test) +
1008         #               8 bytes for luda_type
1009         # take into account of overhead in lu_dirpage header and end mark in
1010         # each page, plus one in RPC_NUM calculation.
1011         DIRENT_SIZE=48
1012         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1013         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1014         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1015                                 awk '/^mds_readpage/ {print $2}')
1016         [ $mds_readpage -gt $RPC_NUM ] && \
1017                 error "large readdir doesn't take effect"
1018
1019         simple_cleanup_common
1020 }
1021 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1022
1023 test_24w() { # bug21506
1024         SZ1=234852
1025         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1026         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1027         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1028         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1029         [ "$SZ1" = "$SZ2" ] || \
1030                 error "Error reading at the end of the file $tfile"
1031 }
1032 run_test 24w "Reading a file larger than 4Gb"
1033
1034 test_24x() {
1035         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1037         local MDTIDX=1
1038         local remote_dir=$DIR/$tdir/remote_dir
1039
1040         mkdir -p $DIR/$tdir
1041         $LFS mkdir -i $MDTIDX $remote_dir ||
1042                 error "create remote directory failed"
1043
1044         mkdir -p $DIR/$tdir/src_dir
1045         touch $DIR/$tdir/src_file
1046         mkdir -p $remote_dir/tgt_dir
1047         touch $remote_dir/tgt_file
1048
1049         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1050                 error "rename dir cross MDT works!"
1051
1052         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1053                 error "rename file cross MDT works!"
1054
1055         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1056                 error "ln file cross MDT should not work!"
1057
1058         rm -rf $DIR/$tdir || error "Can not delete directories"
1059 }
1060 run_test 24x "cross rename/link should be failed"
1061
1062 test_24y() {
1063         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1065         local MDTIDX=1
1066         local remote_dir=$DIR/$tdir/remote_dir
1067
1068         mkdir -p $DIR/$tdir
1069         $LFS mkdir -i $MDTIDX $remote_dir ||
1070                    error "create remote directory failed"
1071
1072         mkdir -p $remote_dir/src_dir
1073         touch $remote_dir/src_file
1074         mkdir -p $remote_dir/tgt_dir
1075         touch $remote_dir/tgt_file
1076
1077         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1078                 error "rename subdir in the same remote dir failed!"
1079
1080         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1081                 error "rename files in the same remote dir failed!"
1082
1083         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1084                 error "link files in the same remote dir failed!"
1085
1086         rm -rf $DIR/$tdir || error "Can not delete directories"
1087 }
1088 run_test 24y "rename/link on the same dir should succeed"
1089
1090 test_24z() {
1091         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1092         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1093         local MDTIDX=1
1094         local remote_src=$DIR/$tdir/remote_dir
1095         local remote_tgt=$DIR/$tdir/remote_tgt
1096
1097         mkdir -p $DIR/$tdir
1098         $LFS mkdir -i $MDTIDX $remote_src ||
1099                    error "create remote directory failed"
1100
1101         $LFS mkdir -i $MDTIDX $remote_tgt ||
1102                    error "create remote directory failed"
1103
1104         mrename $remote_src $remote_tgt &&
1105                 error "rename remote dirs should not work!"
1106
1107         rm -rf $DIR/$tdir || error "Can not delete directories"
1108 }
1109 run_test 24z "rename one remote dir to another remote dir should fail"
1110
1111 test_25a() {
1112         echo '== symlink sanity ============================================='
1113
1114         test_mkdir $DIR/d25
1115         ln -s d25 $DIR/s25
1116         touch $DIR/s25/foo || error
1117 }
1118 run_test 25a "create file in symlinked directory ==============="
1119
1120 test_25b() {
1121         [ ! -d $DIR/d25 ] && test_25a
1122         $CHECKSTAT -t file $DIR/s25/foo || error
1123 }
1124 run_test 25b "lookup file in symlinked directory ==============="
1125
1126 test_26a() {
1127         test_mkdir $DIR/d26
1128         test_mkdir $DIR/d26/d26-2
1129         ln -s d26/d26-2 $DIR/s26
1130         touch $DIR/s26/foo || error
1131 }
1132 run_test 26a "multiple component symlink ======================="
1133
1134 test_26b() {
1135         test_mkdir -p $DIR/d26b/d26-2
1136         ln -s d26b/d26-2/foo $DIR/s26-2
1137         touch $DIR/s26-2 || error
1138 }
1139 run_test 26b "multiple component symlink at end of lookup ======"
1140
1141 test_26c() {
1142         test_mkdir $DIR/d26.2
1143         touch $DIR/d26.2/foo
1144         ln -s d26.2 $DIR/s26.2-1
1145         ln -s s26.2-1 $DIR/s26.2-2
1146         ln -s s26.2-2 $DIR/s26.2-3
1147         chmod 0666 $DIR/s26.2-3/foo
1148 }
1149 run_test 26c "chain of symlinks ================================"
1150
1151 # recursive symlinks (bug 439)
1152 test_26d() {
1153         ln -s d26-3/foo $DIR/d26-3
1154 }
1155 run_test 26d "create multiple component recursive symlink ======"
1156
1157 test_26e() {
1158         [ ! -h $DIR/d26-3 ] && test_26d
1159         rm $DIR/d26-3
1160 }
1161 run_test 26e "unlink multiple component recursive symlink ======"
1162
1163 # recursive symlinks (bug 7022)
1164 test_26f() {
1165         test_mkdir -p $DIR/$tdir
1166         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1167         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1168         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1169         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1170         cd $tfile                || error "cd $tfile failed"
1171         ln -s .. dotdot          || error "ln dotdot failed"
1172         ln -s dotdot/lndir lndir || error "ln lndir failed"
1173         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1174         output=`ls $tfile/$tfile/lndir/bar1`
1175         [ "$output" = bar1 ] && error "unexpected output"
1176         rm -r $tfile             || error "rm $tfile failed"
1177         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1178 }
1179 run_test 26f "rm -r of a directory which has recursive symlink ="
1180
1181 test_27a() {
1182         echo '== stripe sanity =============================================='
1183         test_mkdir -p $DIR/d27 || error "mkdir failed"
1184         $GETSTRIPE $DIR/d27
1185         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1186         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1187         pass
1188         log "== test_27a: write to one stripe file ========================="
1189         cp /etc/hosts $DIR/d27/f0 || error
1190 }
1191 run_test 27a "one stripe file =================================="
1192
1193 test_27b() {
1194         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1195         test_mkdir -p $DIR/d27
1196         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1197         $GETSTRIPE -c $DIR/d27/f01
1198         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1199                 error "two-stripe file doesn't have two stripes"
1200 }
1201 run_test 27b "create two stripe file"
1202
1203 test_27c() {
1204         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1205
1206         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1207 }
1208 run_test 27c "write to two stripe file"
1209
1210 test_27d() {
1211         test_mkdir -p $DIR/d27
1212         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1213         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1214         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1215 }
1216 run_test 27d "create file with default settings ================"
1217
1218 test_27e() {
1219         test_mkdir -p $DIR/d27
1220         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1221         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1222         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1223 }
1224 run_test 27e "setstripe existing file (should return error) ======"
1225
1226 test_27f() {
1227         test_mkdir -p $DIR/d27
1228         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1229         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1230         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1231 }
1232 run_test 27f "setstripe with bad stripe size (should return error)"
1233
1234 test_27g() {
1235         test_mkdir -p $DIR/d27
1236         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1237         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1238                 error "$DIR/d27/fnone has object"
1239 }
1240 run_test 27g "$GETSTRIPE with no objects"
1241
1242 test_27i() {
1243         touch $DIR/d27/fsome || error "touch failed"
1244         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1245 }
1246 run_test 27i "$GETSTRIPE with some objects"
1247
1248 test_27j() {
1249         test_mkdir -p $DIR/d27
1250         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1251 }
1252 run_test 27j "setstripe with bad stripe offset (should return error)"
1253
1254 test_27k() { # bug 2844
1255         test_mkdir -p $DIR/d27
1256         FILE=$DIR/d27/f27k
1257         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1258         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1259         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1260         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1261         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1262         dd if=/dev/zero of=$FILE bs=4k count=1
1263         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1264         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1265 }
1266 run_test 27k "limit i_blksize for broken user apps ============="
1267
1268 test_27l() {
1269         test_mkdir -p $DIR/d27
1270         mcreate $DIR/f27l || error "creating file"
1271         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1272                 error "setstripe should have failed" || true
1273 }
1274 run_test 27l "check setstripe permissions (should return error)"
1275
1276 test_27m() {
1277         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1278                 return
1279         if [ $ORIGFREE -gt $MAXFREE ]; then
1280                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1281                 return
1282         fi
1283         trap simple_cleanup_common EXIT
1284         test_mkdir -p $DIR/$tdir
1285         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1286         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1287                 error "dd should fill OST0"
1288         i=2
1289         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1290                 i=`expr $i + 1`
1291                 [ $i -gt 256 ] && break
1292         done
1293         i=`expr $i + 1`
1294         touch $DIR/$tdir/f27m_$i
1295         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1296                 error "OST0 was full but new created file still use it"
1297         i=`expr $i + 1`
1298         touch $DIR/$tdir/f27m_$i
1299         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1300                 error "OST0 was full but new created file still use it"
1301         simple_cleanup_common
1302 }
1303 run_test 27m "create file while OST0 was full =================="
1304
1305 sleep_maxage() {
1306         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1307         sleep $DELAY
1308 }
1309
1310 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1311 # if the OST isn't full anymore.
1312 reset_enospc() {
1313         local OSTIDX=${1:-""}
1314
1315         local list=$(comma_list $(osts_nodes))
1316         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1317
1318         do_nodes $list lctl set_param fail_loc=0
1319         sync    # initiate all OST_DESTROYs from MDS to OST
1320         sleep_maxage
1321 }
1322
1323 exhaust_precreations() {
1324         local OSTIDX=$1
1325         local FAILLOC=$2
1326         local FAILIDX=${3:-$OSTIDX}
1327
1328         test_mkdir -p $DIR/$tdir
1329         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1330         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1331
1332         local OST=$(ostname_from_index $OSTIDX)
1333         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1334                           sed -e 's/_UUID$//;s/^.*-//')
1335
1336         # on the mdt's osc
1337         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1338         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1339         osc.$mdtosc_proc1.prealloc_last_id)
1340         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1341         osc.$mdtosc_proc1.prealloc_next_id)
1342
1343         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1344         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1345
1346         test_mkdir -p $DIR/$tdir/${OST}
1347         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1348 #define OBD_FAIL_OST_ENOSPC              0x215
1349         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1350         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1351         echo "Creating to objid $last_id on ost $OST..."
1352         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1353         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1354         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1355         sleep_maxage
1356 }
1357
1358 exhaust_all_precreations() {
1359         local i
1360         for (( i=0; i < OSTCOUNT; i++ )) ; do
1361                 exhaust_precreations $i $1 -1
1362         done
1363 }
1364
1365 test_27n() {
1366         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1368         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1369         remote_ost_nodsh && skip "remote OST with nodsh" && return
1370
1371         reset_enospc
1372         rm -f $DIR/$tdir/$tfile
1373         exhaust_precreations 0 0x80000215
1374         $SETSTRIPE -c -1 $DIR/$tdir
1375         touch $DIR/$tdir/$tfile || error
1376         $GETSTRIPE $DIR/$tdir/$tfile
1377         reset_enospc
1378 }
1379 run_test 27n "create file with some full OSTs =================="
1380
1381 test_27o() {
1382         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1383         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1384         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1385         remote_ost_nodsh && skip "remote OST with nodsh" && return
1386
1387         reset_enospc
1388         rm -f $DIR/$tdir/$tfile
1389         exhaust_all_precreations 0x215
1390
1391         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1392
1393         reset_enospc
1394         rm -rf $DIR/$tdir/*
1395 }
1396 run_test 27o "create file with all full OSTs (should error) ===="
1397
1398 test_27p() {
1399         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1401         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1402         remote_ost_nodsh && skip "remote OST with nodsh" && return
1403
1404         reset_enospc
1405         rm -f $DIR/$tdir/$tfile
1406         test_mkdir -p $DIR/$tdir
1407
1408         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1409         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1410         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1411
1412         exhaust_precreations 0 0x80000215
1413         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1414         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1415         $GETSTRIPE $DIR/$tdir/$tfile
1416
1417         reset_enospc
1418 }
1419 run_test 27p "append to a truncated file with some full OSTs ==="
1420
1421 test_27q() {
1422         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1424         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1425         remote_ost_nodsh && skip "remote OST with nodsh" && return
1426
1427         reset_enospc
1428         rm -f $DIR/$tdir/$tfile
1429
1430         test_mkdir -p $DIR/$tdir
1431         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1432         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1433         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1434
1435         exhaust_all_precreations 0x215
1436
1437         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1438         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1439
1440         reset_enospc
1441 }
1442 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1443
1444 test_27r() {
1445         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1447         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1448         remote_ost_nodsh && skip "remote OST with nodsh" && return
1449
1450         reset_enospc
1451         rm -f $DIR/$tdir/$tfile
1452         exhaust_precreations 0 0x80000215
1453
1454         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1455
1456         reset_enospc
1457 }
1458 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1459
1460 test_27s() { # bug 10725
1461         test_mkdir -p $DIR/$tdir
1462         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1463         local stripe_count=0
1464         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1465         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1466                 error "stripe width >= 2^32 succeeded" || true
1467
1468 }
1469 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1470
1471 test_27t() { # bug 10864
1472         WDIR=`pwd`
1473         WLFS=`which lfs`
1474         cd $DIR
1475         touch $tfile
1476         $WLFS getstripe $tfile
1477         cd $WDIR
1478 }
1479 run_test 27t "check that utils parse path correctly"
1480
1481 test_27u() { # bug 4900
1482         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1483         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1484
1485 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1486         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1487         test_mkdir -p $DIR/$tdir
1488         createmany -o $DIR/$tdir/t- 1000
1489         do_facet $SINGLEMDS lctl set_param fail_loc=0
1490
1491         TLOG=$DIR/$tfile.getstripe
1492         $GETSTRIPE $DIR/$tdir > $TLOG
1493         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1494         unlinkmany $DIR/$tdir/t- 1000
1495         [ $OBJS -gt 0 ] && \
1496                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1497 }
1498 run_test 27u "skip object creation on OSC w/o objects =========="
1499
1500 test_27v() { # bug 4900
1501         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1503         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1504         remote_ost_nodsh && skip "remote OST with nodsh" && return
1505
1506         exhaust_all_precreations 0x215
1507         reset_enospc
1508
1509         test_mkdir -p $DIR/$tdir
1510         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1511
1512         touch $DIR/$tdir/$tfile
1513         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1514         # all except ost1
1515         for (( i=1; i < OSTCOUNT; i++ )); do
1516                 do_facet ost$i lctl set_param fail_loc=0x705
1517         done
1518         local START=`date +%s`
1519         createmany -o $DIR/$tdir/$tfile 32
1520
1521         local FINISH=`date +%s`
1522         local TIMEOUT=`lctl get_param -n timeout`
1523         local PROCESS=$((FINISH - START))
1524         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1525                error "$FINISH - $START >= $TIMEOUT / 2"
1526         sleep $((TIMEOUT / 2 - PROCESS))
1527         reset_enospc
1528 }
1529 run_test 27v "skip object creation on slow OST ================="
1530
1531 test_27w() { # bug 10997
1532         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1533         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1534         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1535                 error "stripe size $size != 65536" || true
1536         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1537                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1538 }
1539 run_test 27w "check $SETSTRIPE -S option"
1540
1541 test_27wa() {
1542         [ "$OSTCOUNT" -lt "2" ] &&
1543                 skip_env "skipping multiple stripe count/offset test" && return
1544
1545         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1546         for i in $(seq 1 $OSTCOUNT); do
1547                 offset=$((i - 1))
1548                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1549                         error "setstripe -c $i -i $offset failed"
1550                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1551                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1552                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1553                 [ $index -ne $offset ] &&
1554                         error "stripe offset $index != $offset" || true
1555         done
1556 }
1557 run_test 27wa "check $SETSTRIPE -c -i options"
1558
1559 test_27x() {
1560         remote_ost_nodsh && skip "remote OST with nodsh" && return
1561         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1563         OFFSET=$(($OSTCOUNT - 1))
1564         OSTIDX=0
1565         local OST=$(ostname_from_index $OSTIDX)
1566
1567         test_mkdir -p $DIR/$tdir
1568         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1569         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1570         sleep_maxage
1571         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1572         for i in `seq 0 $OFFSET`; do
1573                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1574                 error "OST0 was degraded but new created file still use it"
1575         done
1576         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1577 }
1578 run_test 27x "create files while OST0 is degraded"
1579
1580 test_27y() {
1581         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1582         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1583         remote_ost_nodsh && skip "remote OST with nodsh" && return
1584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1585
1586         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1587         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1588             osc.$mdtosc.prealloc_last_id)
1589         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1590             osc.$mdtosc.prealloc_next_id)
1591         local fcount=$((last_id - next_id))
1592         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1593         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1594
1595         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1596                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1597         local OST_DEACTIVE_IDX=-1
1598         local OSC
1599         local OSTIDX
1600         local OST
1601
1602         for OSC in $MDS_OSCS; do
1603                 OST=$(osc_to_ost $OSC)
1604                 OSTIDX=$(index_from_ostuuid $OST)
1605                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1606                         OST_DEACTIVE_IDX=$OSTIDX
1607                 fi
1608                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1609                         echo $OSC "is Deactivated:"
1610                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1611                 fi
1612         done
1613
1614         OSTIDX=$(index_from_ostuuid $OST)
1615         mkdir -p $DIR/$tdir
1616         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1617
1618         for OSC in $MDS_OSCS; do
1619                 OST=$(osc_to_ost $OSC)
1620                 OSTIDX=$(index_from_ostuuid $OST)
1621                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1622                         echo $OST "is degraded:"
1623                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1624                                                 obdfilter.$OST.degraded=1
1625                 fi
1626         done
1627
1628         sleep_maxage
1629         createmany -o $DIR/$tdir/$tfile $fcount
1630
1631         for OSC in $MDS_OSCS; do
1632                 OST=$(osc_to_ost $OSC)
1633                 OSTIDX=$(index_from_ostuuid $OST)
1634                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1635                         echo $OST "is recovered from degraded:"
1636                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1637                                                 obdfilter.$OST.degraded=0
1638                 else
1639                         do_facet $SINGLEMDS lctl --device %$OSC activate
1640                 fi
1641         done
1642 }
1643 run_test 27y "create files while OST0 is degraded and the rest inactive"
1644
1645 check_seq_oid()
1646 {
1647         log "check file $1"
1648
1649         lmm_count=$($GETSTRIPE -c $1)
1650         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1651         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1652
1653         local old_ifs="$IFS"
1654         IFS=$'[:]'
1655         fid=($($LFS path2fid $1))
1656         IFS="$old_ifs"
1657
1658         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1659         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1660
1661         # compare lmm_seq and lu_fid->f_seq
1662         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1663         # compare lmm_object_id and lu_fid->oid
1664         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1665
1666         # check the trusted.fid attribute of the OST objects of the file
1667         local have_obdidx=false
1668         local stripe_nr=0
1669         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1670                 # skip lines up to and including "obdidx"
1671                 [ -z "$obdidx" ] && break
1672                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1673                 $have_obdidx || continue
1674
1675                 local ost=$((obdidx + 1))
1676                 local dev=$(ostdevname $ost)
1677
1678                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1679                         echo "Currently only works with ldiskfs-based OSTs"
1680                         continue
1681                 fi
1682
1683                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1684
1685                 #don't unmount/remount the OSTs if we don't need to do that
1686                 #local dir=$(facet_mntpt ost$ost)
1687                 #stop ost$dev
1688                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1689                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1690                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1691                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1692                 seq=$(echo $seq | sed -e "s/^0x//g")
1693                 if [ $seq == 0 ]; then
1694                         oid_hex=$(echo $oid)
1695                 else
1696                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1697                 fi
1698                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1699                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1700                            $dev 2>/dev/null" | grep "parent=")
1701
1702                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1703
1704                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1705
1706                 #do_facet ost$ost umount -d $dir
1707                 #start ost$ost $dev $OST_MOUNT_OPTS
1708
1709                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1710                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1711                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1712                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1713                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1714                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1715
1716                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1717                 [ $ff_pseq = $lmm_seq ] ||
1718                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1719                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1720                 [ $ff_poid = $lmm_oid ] ||
1721                         error "FF parent OID $ff_poid != $lmm_oid"
1722                 [ $ff_pstripe = $stripe_nr ] ||
1723                         error "FF stripe $ff_pstripe != $stripe_nr"
1724
1725                 stripe_nr=$((stripe_nr + 1))
1726         done
1727 }
1728
1729 test_27z() {
1730         remote_ost_nodsh && skip "remote OST with nodsh" && return
1731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1732         test_mkdir -p $DIR/$tdir
1733
1734         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1735                 { error "setstripe -c -1 failed"; return 1; }
1736         # We need to send a write to every object to get parent FID info set.
1737         # This _should_ also work for setattr, but does not currently.
1738         # touch $DIR/$tdir/$tfile-1 ||
1739         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1740                 { error "dd $tfile-1 failed"; return 2; }
1741         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1742                 { error "setstripe -c -1 failed"; return 3; }
1743         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1744                 { error "dd $tfile-2 failed"; return 4; }
1745
1746         # make sure write RPCs have been sent to OSTs
1747         sync; sleep 5; sync
1748
1749         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1750         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1751 }
1752 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1753
1754 test_27A() { # b=19102
1755         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1756         local restore_size=$($GETSTRIPE -S $MOUNT)
1757         local restore_count=$($GETSTRIPE -c $MOUNT)
1758         local restore_offset=$($GETSTRIPE -i $MOUNT)
1759         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1760         local default_size=$($GETSTRIPE -S $MOUNT)
1761         local default_count=$($GETSTRIPE -c $MOUNT)
1762         local default_offset=$($GETSTRIPE -i $MOUNT)
1763         local dsize=$((1024 * 1024))
1764         [ $default_size -eq $dsize ] ||
1765                 error "stripe size $default_size != $dsize"
1766         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1767         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1768         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1769 }
1770 run_test 27A "check filesystem-wide default LOV EA values"
1771
1772 # createtest also checks that device nodes are created and
1773 # then visible correctly (#2091)
1774 test_28() { # bug 2091
1775         test_mkdir $DIR/d28
1776         $CREATETEST $DIR/d28/ct || error
1777 }
1778 run_test 28 "create/mknod/mkdir with bad file types ============"
1779
1780 test_29() {
1781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1782         cancel_lru_locks mdc
1783         test_mkdir $DIR/d29
1784         touch $DIR/d29/foo
1785         log 'first d29'
1786         ls -l $DIR/d29
1787
1788         declare -i LOCKCOUNTORIG=0
1789         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1790                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1791         done
1792         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1793
1794         declare -i LOCKUNUSEDCOUNTORIG=0
1795         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1796                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1797         done
1798
1799         log 'second d29'
1800         ls -l $DIR/d29
1801         log 'done'
1802
1803         declare -i LOCKCOUNTCURRENT=0
1804         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1805                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1806         done
1807
1808         declare -i LOCKUNUSEDCOUNTCURRENT=0
1809         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1810                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1811         done
1812
1813         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1814                 lctl set_param -n ldlm.dump_namespaces ""
1815                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1816                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1817                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1818                 return 2
1819         fi
1820         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1821                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1822                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1823                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1824                 return 3
1825         fi
1826 }
1827 run_test 29 "IT_GETATTR regression  ============================"
1828
1829 test_30a() { # was test_30
1830         cp `which ls` $DIR || cp /bin/ls $DIR
1831         $DIR/ls / || error
1832         rm $DIR/ls
1833 }
1834 run_test 30a "execute binary from Lustre (execve) =============="
1835
1836 test_30b() {
1837         cp `which ls` $DIR || cp /bin/ls $DIR
1838         chmod go+rx $DIR/ls
1839         $RUNAS $DIR/ls / || error
1840         rm $DIR/ls
1841 }
1842 run_test 30b "execute binary from Lustre as non-root ==========="
1843
1844 test_30c() { # b=22376
1845         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1846         cp `which ls` $DIR || cp /bin/ls $DIR
1847         chmod a-rw $DIR/ls
1848         cancel_lru_locks mdc
1849         cancel_lru_locks osc
1850         $RUNAS $DIR/ls / || error
1851         rm -f $DIR/ls
1852 }
1853 run_test 30c "execute binary from Lustre without read perms ===="
1854
1855 test_31a() {
1856         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1857         $CHECKSTAT -a $DIR/f31 || error
1858 }
1859 run_test 31a "open-unlink file =================================="
1860
1861 test_31b() {
1862         touch $DIR/f31 || error
1863         ln $DIR/f31 $DIR/f31b || error
1864         $MULTIOP $DIR/f31b Ouc || error
1865         $CHECKSTAT -t file $DIR/f31 || error
1866 }
1867 run_test 31b "unlink file with multiple links while open ======="
1868
1869 test_31c() {
1870         touch $DIR/f31 || error
1871         ln $DIR/f31 $DIR/f31c || error
1872         multiop_bg_pause $DIR/f31 O_uc || return 1
1873         MULTIPID=$!
1874         $MULTIOP $DIR/f31c Ouc
1875         kill -USR1 $MULTIPID
1876         wait $MULTIPID
1877 }
1878 run_test 31c "open-unlink file with multiple links ============="
1879
1880 test_31d() {
1881         opendirunlink $DIR/d31d $DIR/d31d || error
1882         $CHECKSTAT -a $DIR/d31d || error
1883 }
1884 run_test 31d "remove of open directory ========================="
1885
1886 test_31e() { # bug 2904
1887         check_kernel_version 34 || return 0
1888         openfilleddirunlink $DIR/d31e || error
1889 }
1890 run_test 31e "remove of open non-empty directory ==============="
1891
1892 test_31f() { # bug 4554
1893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1894         set -vx
1895         test_mkdir $DIR/d31f
1896         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1897         cp /etc/hosts $DIR/d31f
1898         ls -l $DIR/d31f
1899         $GETSTRIPE $DIR/d31f/hosts
1900         multiop_bg_pause $DIR/d31f D_c || return 1
1901         MULTIPID=$!
1902
1903         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1904         test_mkdir $DIR/d31f
1905         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1906         cp /etc/hosts $DIR/d31f
1907         ls -l $DIR/d31f
1908         $GETSTRIPE $DIR/d31f/hosts
1909         multiop_bg_pause $DIR/d31f D_c || return 1
1910         MULTIPID2=$!
1911
1912         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1913         wait $MULTIPID || error "first opendir $MULTIPID failed"
1914
1915         sleep 6
1916
1917         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1918         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1919         set +vx
1920 }
1921 run_test 31f "remove of open directory with open-unlink file ==="
1922
1923 test_31g() {
1924         echo "-- cross directory link --"
1925         test_mkdir $DIR/d31ga
1926         test_mkdir $DIR/d31gb
1927         touch $DIR/d31ga/f
1928         ln $DIR/d31ga/f $DIR/d31gb/g
1929         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1930         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1931         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1932         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1933 }
1934 run_test 31g "cross directory link==============="
1935
1936 test_31h() {
1937         echo "-- cross directory link --"
1938         test_mkdir $DIR/d31h
1939         test_mkdir $DIR/d31h/dir
1940         touch $DIR/d31h/f
1941         ln $DIR/d31h/f $DIR/d31h/dir/g
1942         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1943         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1944         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1945         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1946 }
1947 run_test 31h "cross directory link under child==============="
1948
1949 test_31i() {
1950         echo "-- cross directory link --"
1951         test_mkdir $DIR/d31i
1952         test_mkdir $DIR/d31i/dir
1953         touch $DIR/d31i/dir/f
1954         ln $DIR/d31i/dir/f $DIR/d31i/g
1955         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1956         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1957         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1958         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1959 }
1960 run_test 31i "cross directory link under parent==============="
1961
1962
1963 test_31j() {
1964         test_mkdir $DIR/d31j
1965         test_mkdir $DIR/d31j/dir1
1966         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1967         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1968         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1969         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1970         return 0
1971 }
1972 run_test 31j "link for directory==============="
1973
1974
1975 test_31k() {
1976         test_mkdir $DIR/d31k
1977         touch $DIR/d31k/s
1978         touch $DIR/d31k/exist
1979         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1980         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1981         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1982         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1983         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1984         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1985         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1986         return 0
1987 }
1988 run_test 31k "link to file: the same, non-existing, dir==============="
1989
1990 test_31m() {
1991         test_mkdir $DIR/d31m
1992         touch $DIR/d31m/s
1993         test_mkdir $DIR/d31m2
1994         touch $DIR/d31m2/exist
1995         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1996         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1997         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1998         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1999         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2000         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2001         return 0
2002 }
2003 run_test 31m "link to file: the same, non-existing, dir==============="
2004
2005 cleanup_test32_mount() {
2006         trap 0
2007         $UMOUNT $DIR/$tdir/ext2-mountpoint
2008 }
2009
2010 test_32a() {
2011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2012         echo "== more mountpoints and symlinks ================="
2013         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2014         trap cleanup_test32_mount EXIT
2015         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2016         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2017         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2018         cleanup_test32_mount
2019 }
2020 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2021
2022 test_32b() {
2023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2024         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2025         trap cleanup_test32_mount EXIT
2026         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2027         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2028         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2029         cleanup_test32_mount
2030 }
2031 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2032
2033 test_32c() {
2034         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2035         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2036         trap cleanup_test32_mount EXIT
2037         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2038         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2039         test_mkdir -p $DIR/$tdir/d2/test_dir
2040         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2041         cleanup_test32_mount
2042 }
2043 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2044
2045 test_32d() {
2046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2047         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2048         trap cleanup_test32_mount EXIT
2049         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2050         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2051         test_mkdir -p $DIR/$tdir/d2/test_dir
2052         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2053         cleanup_test32_mount
2054 }
2055 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2056
2057 test_32e() {
2058         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2059         test_mkdir -p $DIR/d32e/tmp
2060         TMP_DIR=$DIR/d32e/tmp
2061         ln -s $DIR/d32e $TMP_DIR/symlink11
2062         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2063         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2064         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2065 }
2066 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2067
2068 test_32f() {
2069         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2070         test_mkdir -p $DIR/d32f/tmp
2071         TMP_DIR=$DIR/d32f/tmp
2072         ln -s $DIR/d32f $TMP_DIR/symlink11
2073         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2074         ls $DIR/d32f/tmp/symlink11  || error
2075         ls $DIR/d32f/symlink01 || error
2076 }
2077 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2078
2079 test_32g() {
2080         TMP_DIR=$DIR/$tdir/tmp
2081         test_mkdir -p $DIR/$tdir/tmp
2082         test_mkdir $DIR/${tdir}2
2083         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2084         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2085         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2086         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2087         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2088         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2089 }
2090 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2091
2092 test_32h() {
2093         rm -fr $DIR/$tdir $DIR/${tdir}2
2094         TMP_DIR=$DIR/$tdir/tmp
2095         test_mkdir -p $DIR/$tdir/tmp
2096         test_mkdir $DIR/${tdir}2
2097         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2098         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2099         ls $TMP_DIR/symlink12 || error
2100         ls $DIR/$tdir/symlink02  || error
2101 }
2102 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2103
2104 test_32i() {
2105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2106         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2107         trap cleanup_test32_mount EXIT
2108         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2109         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2110         touch $DIR/$tdir/test_file
2111         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2112         cleanup_test32_mount
2113 }
2114 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2115
2116 test_32j() {
2117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2118         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2119         trap cleanup_test32_mount EXIT
2120         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2121         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2122         touch $DIR/$tdir/test_file
2123         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2124         cleanup_test32_mount
2125 }
2126 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2127
2128 test_32k() {
2129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2130         rm -fr $DIR/$tdir
2131         trap cleanup_test32_mount EXIT
2132         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2133         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2134         test_mkdir -p $DIR/$tdir/d2
2135         touch $DIR/$tdir/d2/test_file || error
2136         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2137         cleanup_test32_mount
2138 }
2139 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2140
2141 test_32l() {
2142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2143         rm -fr $DIR/$tdir
2144         trap cleanup_test32_mount EXIT
2145         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2146         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2147         test_mkdir -p $DIR/$tdir/d2
2148         touch $DIR/$tdir/d2/test_file
2149         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2150         cleanup_test32_mount
2151 }
2152 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2153
2154 test_32m() {
2155         rm -fr $DIR/d32m
2156         test_mkdir -p $DIR/d32m/tmp
2157         TMP_DIR=$DIR/d32m/tmp
2158         ln -s $DIR $TMP_DIR/symlink11
2159         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2160         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2161         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2162 }
2163 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2164
2165 test_32n() {
2166         rm -fr $DIR/d32n
2167         test_mkdir -p $DIR/d32n/tmp
2168         TMP_DIR=$DIR/d32n/tmp
2169         ln -s $DIR $TMP_DIR/symlink11
2170         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2171         ls -l $DIR/d32n/tmp/symlink11  || error
2172         ls -l $DIR/d32n/symlink01 || error
2173 }
2174 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2175
2176 test_32o() {
2177         rm -fr $DIR/d32o $DIR/$tfile
2178         touch $DIR/$tfile
2179         test_mkdir -p $DIR/d32o/tmp
2180         TMP_DIR=$DIR/d32o/tmp
2181         ln -s $DIR/$tfile $TMP_DIR/symlink12
2182         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2183         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2184         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2185         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2186         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2187 }
2188 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2189
2190 test_32p() {
2191     log 32p_1
2192         rm -fr $DIR/d32p
2193     log 32p_2
2194         rm -f $DIR/$tfile
2195     log 32p_3
2196         touch $DIR/$tfile
2197     log 32p_4
2198         test_mkdir -p $DIR/d32p/tmp
2199     log 32p_5
2200         TMP_DIR=$DIR/d32p/tmp
2201     log 32p_6
2202         ln -s $DIR/$tfile $TMP_DIR/symlink12
2203     log 32p_7
2204         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2205     log 32p_8
2206         cat $DIR/d32p/tmp/symlink12 || error
2207     log 32p_9
2208         cat $DIR/d32p/symlink02 || error
2209     log 32p_10
2210 }
2211 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2212
2213 cleanup_testdir_mount() {
2214         trap 0
2215         $UMOUNT $DIR/$tdir
2216 }
2217
2218 test_32q() {
2219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2220         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2221         trap cleanup_testdir_mount EXIT
2222         test_mkdir -p $DIR/$tdir
2223         touch $DIR/$tdir/under_the_mount
2224         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2225         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2226         cleanup_testdir_mount
2227 }
2228 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2229
2230 test_32r() {
2231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2232         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2233         trap cleanup_testdir_mount EXIT
2234         test_mkdir -p $DIR/$tdir
2235         touch $DIR/$tdir/under_the_mount
2236         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2237         ls $DIR/$tdir | grep -q under_the_mount && error || true
2238         cleanup_testdir_mount
2239 }
2240 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2241
2242 test_33aa() {
2243         rm -f $DIR/$tfile
2244         touch $DIR/$tfile
2245         chmod 444 $DIR/$tfile
2246         chown $RUNAS_ID $DIR/$tfile
2247         log 33_1
2248         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2249         log 33_2
2250 }
2251 run_test 33aa "write file with mode 444 (should return error) ===="
2252
2253 test_33a() {
2254         rm -fr $DIR/d33
2255         test_mkdir -p $DIR/d33
2256         chown $RUNAS_ID $DIR/d33
2257         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2258         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2259                 error "open RDWR" || true
2260 }
2261 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2262
2263 test_33b() {
2264         rm -fr $DIR/d33
2265         test_mkdir -p $DIR/d33
2266         chown $RUNAS_ID $DIR/d33
2267         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2268 }
2269 run_test 33b "test open file with malformed flags (No panic and return error)"
2270
2271 test_33c() {
2272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2273         local ostnum
2274         local ostname
2275         local write_bytes
2276         local all_zeros
2277
2278         remote_ost_nodsh && skip "remote OST with nodsh" && return
2279         all_zeros=:
2280         rm -fr $DIR/d33
2281         test_mkdir -p $DIR/d33
2282         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2283
2284         sync
2285         for ostnum in $(seq $OSTCOUNT); do
2286                 # test-framework's OST numbering is one-based, while Lustre's
2287                 # is zero-based
2288                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2289                 # Parsing llobdstat's output sucks; we could grep the /proc
2290                 # path, but that's likely to not be as portable as using the
2291                 # llobdstat utility.  So we parse lctl output instead.
2292                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2293                         obdfilter/$ostname/stats |
2294                         awk '/^write_bytes/ {print $7}' )
2295                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2296                 if (( ${write_bytes:-0} > 0 ))
2297                 then
2298                         all_zeros=false
2299                         break;
2300                 fi
2301         done
2302
2303         $all_zeros || return 0
2304
2305         # Write four bytes
2306         echo foo > $DIR/d33/bar
2307         # Really write them
2308         sync
2309
2310         # Total up write_bytes after writing.  We'd better find non-zeros.
2311         for ostnum in $(seq $OSTCOUNT); do
2312                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2313                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2314                         obdfilter/$ostname/stats |
2315                         awk '/^write_bytes/ {print $7}' )
2316                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2317                 if (( ${write_bytes:-0} > 0 ))
2318                 then
2319                         all_zeros=false
2320                         break;
2321                 fi
2322         done
2323
2324         if $all_zeros
2325         then
2326                 for ostnum in $(seq $OSTCOUNT); do
2327                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2328                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2329                         do_facet ost$ostnum lctl get_param -n \
2330                                 obdfilter/$ostname/stats
2331                 done
2332                 error "OST not keeping write_bytes stats (b22312)"
2333         fi
2334 }
2335 run_test 33c "test llobdstat and write_bytes"
2336
2337 test_33d() {
2338         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2340         local MDTIDX=1
2341         local remote_dir=$DIR/$tdir/remote_dir
2342
2343         mkdir -p $DIR/$tdir
2344         $LFS mkdir -i $MDTIDX $remote_dir ||
2345                 error "create remote directory failed"
2346
2347         touch $remote_dir/$tfile
2348         chmod 444 $remote_dir/$tfile
2349         chown $RUNAS_ID $remote_dir/$tfile
2350
2351         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2352
2353         chown $RUNAS_ID $remote_dir
2354         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2355                                         error "create" || true
2356         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2357                                     error "open RDWR" || true
2358         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2359                                     error "create" || true
2360 }
2361 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2362
2363 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2364 test_34a() {
2365         rm -f $DIR/f34
2366         $MCREATE $DIR/f34 || error
2367         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2368         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2369         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2370         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2371 }
2372 run_test 34a "truncate file that has not been opened ==========="
2373
2374 test_34b() {
2375         [ ! -f $DIR/f34 ] && test_34a
2376         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2377         $OPENFILE -f O_RDONLY $DIR/f34
2378         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2379         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2380 }
2381 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2382
2383 test_34c() {
2384         [ ! -f $DIR/f34 ] && test_34a
2385         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2386         $OPENFILE -f O_RDWR $DIR/f34
2387         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2388         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2389 }
2390 run_test 34c "O_RDWR opening file-with-size works =============="
2391
2392 test_34d() {
2393         [ ! -f $DIR/f34 ] && test_34a
2394         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2395         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2396         rm $DIR/f34
2397 }
2398 run_test 34d "write to sparse file ============================="
2399
2400 test_34e() {
2401         rm -f $DIR/f34e
2402         $MCREATE $DIR/f34e || error
2403         $TRUNCATE $DIR/f34e 1000 || error
2404         $CHECKSTAT -s 1000 $DIR/f34e || error
2405         $OPENFILE -f O_RDWR $DIR/f34e
2406         $CHECKSTAT -s 1000 $DIR/f34e || error
2407 }
2408 run_test 34e "create objects, some with size and some without =="
2409
2410 test_34f() { # bug 6242, 6243
2411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2412         SIZE34F=48000
2413         rm -f $DIR/f34f
2414         $MCREATE $DIR/f34f || error
2415         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2416         dd if=$DIR/f34f of=$TMP/f34f
2417         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2418         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2419         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2420         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2421         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2422 }
2423 run_test 34f "read from a file with no objects until EOF ======="
2424
2425 test_34g() {
2426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2427         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2428         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2429         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2430         cancel_lru_locks osc
2431         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2432                 error "wrong size after lock cancel"
2433
2434         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2435         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2436                 error "expanding truncate failed"
2437         cancel_lru_locks osc
2438         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2439                 error "wrong expanded size after lock cancel"
2440 }
2441 run_test 34g "truncate long file ==============================="
2442
2443 test_34h() {
2444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2445         local gid=10
2446         local sz=1000
2447
2448         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2449         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2450         MULTIPID=$!
2451         sleep 2
2452
2453         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2454                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2455                 kill -9 $MULTIPID
2456         fi
2457         wait $MULTIPID
2458         local nsz=`stat -c %s $DIR/$tfile`
2459         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2460 }
2461 run_test 34h "ftruncate file under grouplock should not block"
2462
2463 test_35a() {
2464         cp /bin/sh $DIR/f35a
2465         chmod 444 $DIR/f35a
2466         chown $RUNAS_ID $DIR/f35a
2467         $RUNAS $DIR/f35a && error || true
2468         rm $DIR/f35a
2469 }
2470 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2471
2472 test_36a() {
2473         rm -f $DIR/f36
2474         utime $DIR/f36 || error
2475 }
2476 run_test 36a "MDS utime check (mknod, utime) ==================="
2477
2478 test_36b() {
2479         echo "" > $DIR/f36
2480         utime $DIR/f36 || error
2481 }
2482 run_test 36b "OST utime check (open, utime) ===================="
2483
2484 test_36c() {
2485         rm -f $DIR/d36/f36
2486         test_mkdir $DIR/d36
2487         chown $RUNAS_ID $DIR/d36
2488         $RUNAS utime $DIR/d36/f36 || error
2489 }
2490 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2491
2492 test_36d() {
2493         [ ! -d $DIR/d36 ] && test_36c
2494         echo "" > $DIR/d36/f36
2495         $RUNAS utime $DIR/d36/f36 || error
2496 }
2497 run_test 36d "non-root OST utime check (open, utime) ==========="
2498
2499 test_36e() {
2500         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2501         test_mkdir -p $DIR/$tdir
2502         touch $DIR/$tdir/$tfile
2503         $RUNAS utime $DIR/$tdir/$tfile && \
2504                 error "utime worked, expected failure" || true
2505 }
2506 run_test 36e "utime on non-owned file (should return error) ===="
2507
2508 subr_36fh() {
2509         local fl="$1"
2510         local LANG_SAVE=$LANG
2511         local LC_LANG_SAVE=$LC_LANG
2512         export LANG=C LC_LANG=C # for date language
2513
2514         DATESTR="Dec 20  2000"
2515         test_mkdir -p $DIR/$tdir
2516         lctl set_param fail_loc=$fl
2517         date; date +%s
2518         cp /etc/hosts $DIR/$tdir/$tfile
2519         sync & # write RPC generated with "current" inode timestamp, but delayed
2520         sleep 1
2521         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2522         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2523         cancel_lru_locks osc
2524         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2525         date; date +%s
2526         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2527                 echo "BEFORE: $LS_BEFORE" && \
2528                 echo "AFTER : $LS_AFTER" && \
2529                 echo "WANT  : $DATESTR" && \
2530                 error "$DIR/$tdir/$tfile timestamps changed" || true
2531
2532         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2533 }
2534
2535 test_36f() {
2536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2537         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2538         subr_36fh "0x80000214"
2539 }
2540 run_test 36f "utime on file racing with OST BRW write =========="
2541
2542 test_36g() {
2543         remote_ost_nodsh && skip "remote OST with nodsh" && return
2544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2545         local fmd_max_age
2546         local fmd_before
2547         local fmd_after
2548
2549         test_mkdir -p $DIR/$tdir
2550         fmd_max_age=$(do_facet ost1 \
2551                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2552                 head -n 1")
2553
2554         fmd_before=$(do_facet ost1 \
2555                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2556         touch $DIR/$tdir/$tfile
2557         sleep $((fmd_max_age + 12))
2558         fmd_after=$(do_facet ost1 \
2559                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2560
2561         echo "fmd_before: $fmd_before"
2562         echo "fmd_after: $fmd_after"
2563         [ "$fmd_after" -gt "$fmd_before" ] && \
2564                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2565                 error "fmd didn't expire after ping" || true
2566 }
2567 run_test 36g "filter mod data cache expiry ====================="
2568
2569 test_36h() {
2570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2571         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2572         subr_36fh "0x80000227"
2573 }
2574 run_test 36h "utime on file racing with OST BRW write =========="
2575
2576 # test_37 - duplicate with tests 32q 32r
2577
2578 test_38() {
2579         local file=$DIR/$tfile
2580         touch $file
2581         openfile -f O_DIRECTORY $file
2582         local RC=$?
2583         local ENOTDIR=20
2584         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2585         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2586 }
2587 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2588
2589 test_39() {
2590         touch $DIR/$tfile
2591         touch $DIR/${tfile}2
2592 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2593 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2594 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2595         sleep 2
2596         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2597         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2598                 echo "mtime"
2599                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2600                 echo "atime"
2601                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2602                 echo "ctime"
2603                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2604                 error "O_TRUNC didn't change timestamps"
2605         fi
2606 }
2607 run_test 39 "mtime changed on create ==========================="
2608
2609 test_39b() {
2610         test_mkdir -p $DIR/$tdir
2611         cp -p /etc/passwd $DIR/$tdir/fopen
2612         cp -p /etc/passwd $DIR/$tdir/flink
2613         cp -p /etc/passwd $DIR/$tdir/funlink
2614         cp -p /etc/passwd $DIR/$tdir/frename
2615         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2616
2617         sleep 1
2618         echo "aaaaaa" >> $DIR/$tdir/fopen
2619         echo "aaaaaa" >> $DIR/$tdir/flink
2620         echo "aaaaaa" >> $DIR/$tdir/funlink
2621         echo "aaaaaa" >> $DIR/$tdir/frename
2622
2623         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2624         local link_new=`stat -c %Y $DIR/$tdir/flink`
2625         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2626         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2627
2628         cat $DIR/$tdir/fopen > /dev/null
2629         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2630         rm -f $DIR/$tdir/funlink2
2631         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2632
2633         for (( i=0; i < 2; i++ )) ; do
2634                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2635                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2636                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2637                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2638
2639                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2640                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2641                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2642                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2643
2644                 cancel_lru_locks osc
2645                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2646         done
2647 }
2648 run_test 39b "mtime change on open, link, unlink, rename  ======"
2649
2650 # this should be set to past
2651 TEST_39_MTIME=`date -d "1 year ago" +%s`
2652
2653 # bug 11063
2654 test_39c() {
2655         touch $DIR1/$tfile
2656         sleep 2
2657         local mtime0=`stat -c %Y $DIR1/$tfile`
2658
2659         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2660         local mtime1=`stat -c %Y $DIR1/$tfile`
2661         [ "$mtime1" = $TEST_39_MTIME ] || \
2662                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2663
2664         local d1=`date +%s`
2665         echo hello >> $DIR1/$tfile
2666         local d2=`date +%s`
2667         local mtime2=`stat -c %Y $DIR1/$tfile`
2668         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2669                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2670
2671         mv $DIR1/$tfile $DIR1/$tfile-1
2672
2673         for (( i=0; i < 2; i++ )) ; do
2674                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2675                 [ "$mtime2" = "$mtime3" ] || \
2676                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2677
2678                 cancel_lru_locks osc
2679                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2680         done
2681 }
2682 run_test 39c "mtime change on rename ==========================="
2683
2684 # bug 21114
2685 test_39d() {
2686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2687         touch $DIR1/$tfile
2688
2689         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2690
2691         for (( i=0; i < 2; i++ )) ; do
2692                 local mtime=`stat -c %Y $DIR1/$tfile`
2693                 [ $mtime = $TEST_39_MTIME ] || \
2694                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2695
2696                 cancel_lru_locks osc
2697                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2698         done
2699 }
2700 run_test 39d "create, utime, stat =============================="
2701
2702 # bug 21114
2703 test_39e() {
2704         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2705         touch $DIR1/$tfile
2706         local mtime1=`stat -c %Y $DIR1/$tfile`
2707
2708         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2709
2710         for (( i=0; i < 2; i++ )) ; do
2711                 local mtime2=`stat -c %Y $DIR1/$tfile`
2712                 [ $mtime2 = $TEST_39_MTIME ] || \
2713                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2714
2715                 cancel_lru_locks osc
2716                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2717         done
2718 }
2719 run_test 39e "create, stat, utime, stat ========================"
2720
2721 # bug 21114
2722 test_39f() {
2723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2724         touch $DIR1/$tfile
2725         mtime1=`stat -c %Y $DIR1/$tfile`
2726
2727         sleep 2
2728         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2729
2730         for (( i=0; i < 2; i++ )) ; do
2731                 local mtime2=`stat -c %Y $DIR1/$tfile`
2732                 [ $mtime2 = $TEST_39_MTIME ] || \
2733                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2734
2735                 cancel_lru_locks osc
2736                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2737         done
2738 }
2739 run_test 39f "create, stat, sleep, utime, stat ================="
2740
2741 # bug 11063
2742 test_39g() {
2743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2744         echo hello >> $DIR1/$tfile
2745         local mtime1=`stat -c %Y $DIR1/$tfile`
2746
2747         sleep 2
2748         chmod o+r $DIR1/$tfile
2749
2750         for (( i=0; i < 2; i++ )) ; do
2751                 local mtime2=`stat -c %Y $DIR1/$tfile`
2752                 [ "$mtime1" = "$mtime2" ] || \
2753                         error "lost mtime: $mtime2, should be $mtime1"
2754
2755                 cancel_lru_locks osc
2756                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2757         done
2758 }
2759 run_test 39g "write, chmod, stat ==============================="
2760
2761 # bug 11063
2762 test_39h() {
2763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2764         touch $DIR1/$tfile
2765         sleep 1
2766
2767         local d1=`date`
2768         echo hello >> $DIR1/$tfile
2769         local mtime1=`stat -c %Y $DIR1/$tfile`
2770
2771         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2772         local d2=`date`
2773         if [ "$d1" != "$d2" ]; then
2774                 echo "write and touch not within one second"
2775         else
2776                 for (( i=0; i < 2; i++ )) ; do
2777                         local mtime2=`stat -c %Y $DIR1/$tfile`
2778                         [ "$mtime2" = $TEST_39_MTIME ] || \
2779                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2780
2781                         cancel_lru_locks osc
2782                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2783                 done
2784         fi
2785 }
2786 run_test 39h "write, utime within one second, stat ============="
2787
2788 test_39i() {
2789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2790         touch $DIR1/$tfile
2791         sleep 1
2792
2793         echo hello >> $DIR1/$tfile
2794         local mtime1=`stat -c %Y $DIR1/$tfile`
2795
2796         mv $DIR1/$tfile $DIR1/$tfile-1
2797
2798         for (( i=0; i < 2; i++ )) ; do
2799                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2800
2801                 [ "$mtime1" = "$mtime2" ] || \
2802                         error "lost mtime: $mtime2, should be $mtime1"
2803
2804                 cancel_lru_locks osc
2805                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2806         done
2807 }
2808 run_test 39i "write, rename, stat =============================="
2809
2810 test_39j() {
2811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2812         start_full_debug_logging
2813         touch $DIR1/$tfile
2814         sleep 1
2815
2816         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2817         lctl set_param fail_loc=0x80000412
2818         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2819                 error "multiop failed"
2820         local multipid=$!
2821         local mtime1=`stat -c %Y $DIR1/$tfile`
2822
2823         mv $DIR1/$tfile $DIR1/$tfile-1
2824
2825         kill -USR1 $multipid
2826         wait $multipid || error "multiop close failed"
2827
2828         for (( i=0; i < 2; i++ )) ; do
2829                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2830                 [ "$mtime1" = "$mtime2" ] ||
2831                         error "mtime is lost on close: $mtime2, " \
2832                               "should be $mtime1"
2833
2834                 cancel_lru_locks osc
2835                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2836         done
2837         lctl set_param fail_loc=0
2838         stop_full_debug_logging
2839 }
2840 run_test 39j "write, rename, close, stat ======================="
2841
2842 test_39k() {
2843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2844         touch $DIR1/$tfile
2845         sleep 1
2846
2847         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2848         local multipid=$!
2849         local mtime1=`stat -c %Y $DIR1/$tfile`
2850
2851         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2852
2853         kill -USR1 $multipid
2854         wait $multipid || error "multiop close failed"
2855
2856         for (( i=0; i < 2; i++ )) ; do
2857                 local mtime2=`stat -c %Y $DIR1/$tfile`
2858
2859                 [ "$mtime2" = $TEST_39_MTIME ] || \
2860                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2861
2862                 cancel_lru_locks osc
2863                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2864         done
2865 }
2866 run_test 39k "write, utime, close, stat ========================"
2867
2868 # this should be set to future
2869 TEST_39_ATIME=`date -d "1 year" +%s`
2870
2871 test_39l() {
2872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2873         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2874         local atime_diff=$(do_facet $SINGLEMDS \
2875                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2876         rm -rf $DIR/$tdir
2877         mkdir -p $DIR/$tdir
2878
2879         # test setting directory atime to future
2880         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2881         local atime=$(stat -c %X $DIR/$tdir)
2882         [ "$atime" = $TEST_39_ATIME ] || \
2883                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2884
2885         # test setting directory atime from future to now
2886         local d1=$(date +%s)
2887         ls $DIR/$tdir
2888         local d2=$(date +%s)
2889
2890         cancel_lru_locks mdc
2891         atime=$(stat -c %X $DIR/$tdir)
2892         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2893                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2894
2895         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2896         sleep 3
2897
2898         # test setting directory atime when now > dir atime + atime_diff
2899         d1=$(date +%s)
2900         ls $DIR/$tdir
2901         d2=$(date +%s)
2902         cancel_lru_locks mdc
2903         atime=$(stat -c %X $DIR/$tdir)
2904         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2905                 error "atime is not updated  : $atime, should be $d2"
2906
2907         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2908         sleep 3
2909
2910         # test not setting directory atime when now < dir atime + atime_diff
2911         ls $DIR/$tdir
2912         cancel_lru_locks mdc
2913         atime=$(stat -c %X $DIR/$tdir)
2914         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2915                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2916
2917         do_facet $SINGLEMDS \
2918                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2919 }
2920 run_test 39l "directory atime update ==========================="
2921
2922 test_39m() {
2923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2924         touch $DIR1/$tfile
2925         sleep 2
2926         local far_past_mtime=$(date -d "May 29 1953" +%s)
2927         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2928
2929         touch -m -d @$far_past_mtime $DIR1/$tfile
2930         touch -a -d @$far_past_atime $DIR1/$tfile
2931
2932         for (( i=0; i < 2; i++ )) ; do
2933                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2934                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2935                         error "atime or mtime set incorrectly"
2936
2937                 cancel_lru_locks osc
2938                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2939         done
2940 }
2941 run_test 39m "test atime and mtime before 1970"
2942
2943 test_40() {
2944         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2945         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2946         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2947 }
2948 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2949
2950 test_41() {
2951         # bug 1553
2952         small_write $DIR/f41 18
2953 }
2954 run_test 41 "test small file write + fstat ====================="
2955
2956 count_ost_writes() {
2957         lctl get_param -n osc.*.stats |
2958             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2959 }
2960
2961 # decent default
2962 WRITEBACK_SAVE=500
2963 DIRTY_RATIO_SAVE=40
2964 MAX_DIRTY_RATIO=50
2965 BG_DIRTY_RATIO_SAVE=10
2966 MAX_BG_DIRTY_RATIO=25
2967
2968 start_writeback() {
2969         trap 0
2970         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2971         # dirty_ratio, dirty_background_ratio
2972         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2973                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2974                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2975                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2976         else
2977                 # if file not here, we are a 2.4 kernel
2978                 kill -CONT `pidof kupdated`
2979         fi
2980 }
2981
2982 stop_writeback() {
2983         # setup the trap first, so someone cannot exit the test at the
2984         # exact wrong time and mess up a machine
2985         trap start_writeback EXIT
2986         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2987         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2988                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2989                 sysctl -w vm.dirty_writeback_centisecs=0
2990                 sysctl -w vm.dirty_writeback_centisecs=0
2991                 # save and increase /proc/sys/vm/dirty_ratio
2992                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2993                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2994                 # save and increase /proc/sys/vm/dirty_background_ratio
2995                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2996                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2997         else
2998                 # if file not here, we are a 2.4 kernel
2999                 kill -STOP `pidof kupdated`
3000         fi
3001 }
3002
3003 # ensure that all stripes have some grant before we test client-side cache
3004 setup_test42() {
3005         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3006                 dd if=/dev/zero of=$i bs=4k count=1
3007                 rm $i
3008         done
3009 }
3010
3011 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3012 # file truncation, and file removal.
3013 test_42a() {
3014         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3015         setup_test42
3016         cancel_lru_locks osc
3017         stop_writeback
3018         sync; sleep 1; sync # just to be safe
3019         BEFOREWRITES=`count_ost_writes`
3020         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3021         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3022         AFTERWRITES=`count_ost_writes`
3023         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3024                 error "$BEFOREWRITES < $AFTERWRITES"
3025         start_writeback
3026 }
3027 run_test 42a "ensure that we don't flush on close =============="
3028
3029 test_42b() {
3030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3031         setup_test42
3032         cancel_lru_locks osc
3033         stop_writeback
3034         sync
3035         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3036         BEFOREWRITES=`count_ost_writes`
3037         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3038         AFTERWRITES=`count_ost_writes`
3039         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3040                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3041         fi
3042         BEFOREWRITES=`count_ost_writes`
3043         sync || error "sync: $?"
3044         AFTERWRITES=`count_ost_writes`
3045         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3046                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3047         fi
3048         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3049         start_writeback
3050         return 0
3051 }
3052 run_test 42b "test destroy of file with cached dirty data ======"
3053
3054 # if these tests just want to test the effect of truncation,
3055 # they have to be very careful.  consider:
3056 # - the first open gets a {0,EOF}PR lock
3057 # - the first write conflicts and gets a {0, count-1}PW
3058 # - the rest of the writes are under {count,EOF}PW
3059 # - the open for truncate tries to match a {0,EOF}PR
3060 #   for the filesize and cancels the PWs.
3061 # any number of fixes (don't get {0,EOF} on open, match
3062 # composite locks, do smarter file size management) fix
3063 # this, but for now we want these tests to verify that
3064 # the cancellation with truncate intent works, so we
3065 # start the file with a full-file pw lock to match against
3066 # until the truncate.
3067 trunc_test() {
3068         test=$1
3069         file=$DIR/$test
3070         offset=$2
3071         cancel_lru_locks osc
3072         stop_writeback
3073         # prime the file with 0,EOF PW to match
3074         touch $file
3075         $TRUNCATE $file 0
3076         sync; sync
3077         # now the real test..
3078         dd if=/dev/zero of=$file bs=1024 count=100
3079         BEFOREWRITES=`count_ost_writes`
3080         $TRUNCATE $file $offset
3081         cancel_lru_locks osc
3082         AFTERWRITES=`count_ost_writes`
3083         start_writeback
3084 }
3085
3086 test_42c() {
3087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3088         trunc_test 42c 1024
3089         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3090             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3091         rm $file
3092 }
3093 run_test 42c "test partial truncate of file with cached dirty data"
3094
3095 test_42d() {
3096         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3097         trunc_test 42d 0
3098         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3099             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3100         rm $file
3101 }
3102 run_test 42d "test complete truncate of file with cached dirty data"
3103
3104 test_42e() { # bug22074
3105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3106         local TDIR=$DIR/${tdir}e
3107         local pagesz=$(page_size)
3108         local pages=16 # hardcoded 16 pages, don't change it.
3109         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3110         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3111         local max_dirty_mb
3112         local warmup_files
3113
3114         test_mkdir -p $DIR/${tdir}e
3115         $SETSTRIPE -c 1 $TDIR
3116         createmany -o $TDIR/f $files
3117
3118         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3119
3120         # we assume that with $OSTCOUNT files, at least one of them will
3121         # be allocated on OST0.
3122         warmup_files=$((OSTCOUNT * max_dirty_mb))
3123         createmany -o $TDIR/w $warmup_files
3124
3125         # write a large amount of data into one file and sync, to get good
3126         # avail_grant number from OST.
3127         for ((i=0; i<$warmup_files; i++)); do
3128                 idx=$($GETSTRIPE -i $TDIR/w$i)
3129                 [ $idx -ne 0 ] && continue
3130                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3131                 break
3132         done
3133         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3134         sync
3135         $LCTL get_param $proc_osc0/cur_dirty_bytes
3136         $LCTL get_param $proc_osc0/cur_grant_bytes
3137
3138         # create as much dirty pages as we can while not to trigger the actual
3139         # RPCs directly. but depends on the env, VFS may trigger flush during this
3140         # period, hopefully we are good.
3141         for ((i=0; i<$warmup_files; i++)); do
3142                 idx=$($GETSTRIPE -i $TDIR/w$i)
3143                 [ $idx -ne 0 ] && continue
3144                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3145         done
3146         $LCTL get_param $proc_osc0/cur_dirty_bytes
3147         $LCTL get_param $proc_osc0/cur_grant_bytes
3148
3149         # perform the real test
3150         $LCTL set_param $proc_osc0/rpc_stats 0
3151         for ((;i<$files; i++)); do
3152                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3153                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3154         done
3155         sync
3156         $LCTL get_param $proc_osc0/rpc_stats
3157
3158         local percent=0
3159         local have_ppr=false
3160         $LCTL get_param $proc_osc0/rpc_stats |
3161                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3162                         # skip lines until we are at the RPC histogram data
3163                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3164                         $have_ppr || continue
3165
3166                         # we only want the percent stat for < 16 pages
3167                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3168
3169                         percent=$((percent + WPCT))
3170                         if [ $percent -gt 15 ]; then
3171                                 error "less than 16-pages write RPCs" \
3172                                       "$percent% > 15%"
3173                                 break
3174                         fi
3175                 done
3176         rm -rf $TDIR
3177 }
3178 run_test 42e "verify sub-RPC writes are not done synchronously"
3179
3180 test_43() {
3181         test_mkdir -p $DIR/$tdir
3182         cp -p /bin/ls $DIR/$tdir/$tfile
3183         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3184         pid=$!
3185         # give multiop a chance to open
3186         sleep 1
3187
3188         $DIR/$tdir/$tfile && error || true
3189         kill -USR1 $pid
3190 }
3191 run_test 43 "execution of file opened for write should return -ETXTBSY"
3192
3193 test_43a() {
3194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3195         test_mkdir -p $DIR/$tdir
3196         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3197                         cp -p multiop $DIR/$tdir/multiop
3198         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3199                         return 1
3200         MULTIOP_PID=$!
3201         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3202         kill -USR1 $MULTIOP_PID || return 2
3203         wait $MULTIOP_PID || return 3
3204         rm $TMP/test43.junk
3205 }
3206 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3207
3208 test_43b() {
3209         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3210         test_mkdir -p $DIR/$tdir
3211         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3212                         cp -p multiop $DIR/$tdir/multiop
3213         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3214                         return 1
3215         MULTIOP_PID=$!
3216         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3217         kill -USR1 $MULTIOP_PID || return 2
3218         wait $MULTIOP_PID || return 3
3219         rm $TMP/test43.junk
3220 }
3221 run_test 43b "truncate of file being executed should return -ETXTBSY"
3222
3223 test_43c() {
3224         local testdir="$DIR/$tdir"
3225         test_mkdir -p $DIR/$tdir
3226         cp $SHELL $testdir/
3227         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3228                 ( cd $testdir && md5sum -c)
3229 }
3230 run_test 43c "md5sum of copy into lustre========================"
3231
3232 test_44() {
3233         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3234         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3235         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3236 }
3237 run_test 44 "zero length read from a sparse stripe ============="
3238
3239 test_44a() {
3240     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3241                          awk '{print $2}'`
3242     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3243     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3244     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3245                       awk '{print $2}'`
3246     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3247         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3248     fi
3249
3250     OFFSETS="0 $((stride/2)) $((stride-1))"
3251     for offset in $OFFSETS ; do
3252       for i in `seq 0 $((nstripe-1))`; do
3253         local GLOBALOFFSETS=""
3254         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3255         local myfn=$DIR/d44a-$size
3256         echo "--------writing $myfn at $size"
3257         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3258         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3259         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3260                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3261
3262         for j in `seq 0 $((nstripe-1))`; do
3263             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3264             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3265             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3266         done
3267         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3268                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3269         rm -f $myfn
3270       done
3271     done
3272 }
3273 run_test 44a "test sparse pwrite ==============================="
3274
3275 dirty_osc_total() {
3276         tot=0
3277         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3278                 tot=$(($tot + $d))
3279         done
3280         echo $tot
3281 }
3282 do_dirty_record() {
3283         before=`dirty_osc_total`
3284         echo executing "\"$*\""
3285         eval $*
3286         after=`dirty_osc_total`
3287         echo before $before, after $after
3288 }
3289 test_45() {
3290         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3291         f="$DIR/f45"
3292         # Obtain grants from OST if it supports it
3293         echo blah > ${f}_grant
3294         stop_writeback
3295         sync
3296         do_dirty_record "echo blah > $f"
3297         [ $before -eq $after ] && error "write wasn't cached"
3298         do_dirty_record "> $f"
3299         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3300         do_dirty_record "echo blah > $f"
3301         [ $before -eq $after ] && error "write wasn't cached"
3302         do_dirty_record "sync"
3303         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3304         do_dirty_record "echo blah > $f"
3305         [ $before -eq $after ] && error "write wasn't cached"
3306         do_dirty_record "cancel_lru_locks osc"
3307         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3308         start_writeback
3309 }
3310 run_test 45 "osc io page accounting ============================"
3311
3312 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3313 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3314 # objects offset and an assert hit when an rpc was built with 1023's mapped
3315 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3316 test_46() {
3317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3318         f="$DIR/f46"
3319         stop_writeback
3320         sync
3321         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3322         sync
3323         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3324         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3325         sync
3326         start_writeback
3327 }
3328 run_test 46 "dirtying a previously written page ================"
3329
3330 # test_47 is removed "Device nodes check" is moved to test_28
3331
3332 test_48a() { # bug 2399
3333         check_kernel_version 34 || return 0
3334         test_mkdir -p $DIR/$tdir
3335         cd $DIR/$tdir
3336         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3337         test_mkdir $DIR/$tdir || error "recreate directory failed"
3338         touch foo || error "'touch foo' failed after recreating cwd"
3339         test_mkdir $DIR/$tdir/bar ||
3340                      error "'mkdir foo' failed after recreating cwd"
3341         if check_kernel_version 44; then
3342                 touch .foo || error "'touch .foo' failed after recreating cwd"
3343                 test_mkdir $DIR/$tdir/.bar ||
3344                               error "'mkdir .foo' failed after recreating cwd"
3345         fi
3346         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3347         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3348         cd . || error "'cd .' failed after recreating cwd"
3349         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3350         rmdir . && error "'rmdir .' worked after recreating cwd"
3351         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3352         cd .. || error "'cd ..' failed after recreating cwd"
3353 }
3354 run_test 48a "Access renamed working dir (should return errors)="
3355
3356 test_48b() { # bug 2399
3357         check_kernel_version 34 || return 0
3358         rm -rf $DIR/$tdir
3359         test_mkdir -p $DIR/$tdir
3360         cd $DIR/$tdir
3361         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3362         touch foo && error "'touch foo' worked after removing cwd"
3363         test_mkdir $DIR/$tdir/foo &&
3364                      error "'mkdir foo' worked after removing cwd"
3365         if check_kernel_version 44; then
3366                 touch .foo && error "'touch .foo' worked after removing cwd"
3367                 test_mkdir $DIR/$tdir/.foo &&
3368                               error "'mkdir .foo' worked after removing cwd"
3369         fi
3370         ls . > /dev/null && error "'ls .' worked after removing cwd"
3371         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3372         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3373         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3374         rmdir . && error "'rmdir .' worked after removing cwd"
3375         ln -s . foo && error "'ln -s .' worked after removing cwd"
3376         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3377 }
3378 run_test 48b "Access removed working dir (should return errors)="
3379
3380 test_48c() { # bug 2350
3381         check_kernel_version 36 || return 0
3382         #lctl set_param debug=-1
3383         #set -vx
3384         rm -rf $DIR/$tdir
3385         test_mkdir -p $DIR/$tdir/dir
3386         cd $DIR/$tdir/dir
3387         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3388         $TRACE touch foo && error "touch foo worked after removing cwd"
3389         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3390         if check_kernel_version 44; then
3391                 touch .foo && error "touch .foo worked after removing cwd"
3392                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3393         fi
3394         $TRACE ls . && error "'ls .' worked after removing cwd"
3395         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3396         is_patchless || ( $TRACE cd . &&
3397                         error "'cd .' worked after removing cwd" )
3398         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3399         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3400         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3401         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3402 }
3403 run_test 48c "Access removed working subdir (should return errors)"
3404
3405 test_48d() { # bug 2350
3406         check_kernel_version 36 || return 0
3407         #lctl set_param debug=-1
3408         #set -vx
3409         rm -rf $DIR/$tdir
3410         test_mkdir -p $DIR/$tdir/dir
3411         cd $DIR/$tdir/dir
3412         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3413         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3414         $TRACE touch foo && error "'touch foo' worked after removing parent"
3415         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3416         if check_kernel_version 44; then
3417                 touch .foo && error "'touch .foo' worked after removing parent"
3418                 test_mkdir .foo &&
3419                               error "mkdir .foo worked after removing parent"
3420         fi
3421         $TRACE ls . && error "'ls .' worked after removing parent"
3422         $TRACE ls .. && error "'ls ..' worked after removing parent"
3423         is_patchless || ( $TRACE cd . &&
3424                         error "'cd .' worked after recreate parent" )
3425         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3426         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3427         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3428         is_patchless || ( $TRACE cd .. &&
3429                         error "'cd ..' worked after removing parent" || true )
3430 }
3431 run_test 48d "Access removed parent subdir (should return errors)"
3432
3433 test_48e() { # bug 4134
3434         check_kernel_version 41 || return 0
3435         #lctl set_param debug=-1
3436         #set -vx
3437         rm -rf $DIR/$tdir
3438         test_mkdir -p $DIR/$tdir/dir
3439         cd $DIR/$tdir/dir
3440         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3441         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3442         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3443         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3444         # On a buggy kernel addition of "touch foo" after cd .. will
3445         # produce kernel oops in lookup_hash_it
3446         touch ../foo && error "'cd ..' worked after recreate parent"
3447         cd $DIR
3448         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3449 }
3450 run_test 48e "Access to recreated parent subdir (should return errors)"
3451
3452 test_49() { # LU-1030
3453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3454         # get ost1 size - lustre-OST0000
3455         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3456         # write 800M at maximum
3457         [ $ost1_size -gt 819200 ] && ost1_size=819200
3458
3459         lfs setstripe -c 1 -i 0 $DIR/$tfile
3460         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3461         local dd_pid=$!
3462
3463         # change max_pages_per_rpc while writing the file
3464         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3465         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3466         # loop until dd process exits
3467         while ps ax -opid | grep -wq $dd_pid; do
3468                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3469                 sleep $((RANDOM % 5 + 1))
3470         done
3471         # restore original max_pages_per_rpc
3472         $LCTL set_param $osc1_mppc=$orig_mppc
3473         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3474 }
3475 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3476
3477 test_50() {
3478         # bug 1485
3479         test_mkdir $DIR/$tdir
3480         cd $DIR/$tdir
3481         ls /proc/$$/cwd || error
3482 }
3483 run_test 50 "special situations: /proc symlinks  ==============="
3484
3485 test_51a() {    # was test_51
3486         # bug 1516 - create an empty entry right after ".." then split dir
3487         test_mkdir -p $DIR/$tdir
3488         touch $DIR/$tdir/foo
3489         $MCREATE $DIR/$tdir/bar
3490         rm $DIR/$tdir/foo
3491         createmany -m $DIR/$tdir/longfile 201
3492         FNUM=202
3493         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3494                 $MCREATE $DIR/$tdir/longfile$FNUM
3495                 FNUM=$(($FNUM + 1))
3496                 echo -n "+"
3497         done
3498         echo
3499         ls -l $DIR/$tdir > /dev/null || error
3500 }
3501 run_test 51a "special situations: split htree with empty entry =="
3502
3503 export NUMTEST=70000
3504 test_51b() {
3505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3506         local BASE=$DIR/$tdir
3507
3508         # cleanup the directory
3509         rm -fr $BASE
3510
3511         test_mkdir -p $BASE
3512
3513         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3514         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3515         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3516                 return
3517
3518         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3519                 echo "reduced count to $NUMTEST due to inodes"
3520
3521         # need to check free space for the directories as well
3522         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3523         numfree=$((blkfree / 4))
3524         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3525                 echo "reduced count to $NUMTEST due to blocks"
3526
3527         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3528                 echo "failed" > $BASE/fnum
3529 }
3530 run_test 51b "exceed 64k subdirectory nlink limit"
3531
3532 test_51ba() { # LU-993
3533         local BASE=$DIR/$tdir
3534         # unlink all but 100 subdirectories, then check it still works
3535         local LEFT=100
3536         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3537
3538         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3539         local DELETE=$((NUMTEST - LEFT))
3540
3541         # continue on to run this test even if 51b didn't finish,
3542         # just to delete the many subdirectories created.
3543         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3544
3545         # for ldiskfs the nlink count should be 1, but this is OSD specific
3546         # and so this is listed for informational purposes only
3547         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3548         unlinkmany -d $BASE/d $DELETE
3549         RC=$?
3550
3551         if [ $RC -ne 0 ]; then
3552                 if [ "$NUMPREV" == "failed" ]; then
3553                         skip "previous setup failed"
3554                         return 0
3555                 else
3556                         error "unlink of first $DELETE subdirs failed"
3557                         return $RC
3558                 fi
3559         fi
3560
3561         echo "nlink between: $(stat -c %h $BASE)"
3562         # trim the first line of ls output
3563         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3564         [ $FOUND -ne $LEFT ] &&
3565                 error "can't find subdirs: found only $FOUND/$LEFT"
3566
3567         unlinkmany -d $BASE/d $DELETE $LEFT ||
3568                 error "unlink of second $LEFT subdirs failed"
3569         # regardless of whether the backing filesystem tracks nlink accurately
3570         # or not, the nlink count shouldn't be more than "." and ".." here
3571         local AFTER=$(stat -c %h $BASE)
3572         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3573                 echo "nlink after: $AFTER"
3574 }
3575 run_test 51ba "verify nlink for many subdirectory cleanup"
3576
3577 test_51d() {
3578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3579         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3580         test_mkdir -p $DIR/$tdir
3581         createmany -o $DIR/$tdir/t- 1000
3582         $GETSTRIPE $DIR/$tdir > $TMP/files
3583         for N in `seq 0 $((OSTCOUNT - 1))`; do
3584             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3585             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3586             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3587         done
3588         unlinkmany $DIR/$tdir/t- 1000
3589
3590         NLAST=0
3591         for N in `seq 1 $((OSTCOUNT - 1))`; do
3592             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3593                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3594             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3595                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3596
3597             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3598                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3599             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3600                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3601             NLAST=$N
3602         done
3603 }
3604 run_test 51d "check object distribution ===================="
3605
3606 test_52a() {
3607         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3608         test_mkdir -p $DIR/$tdir
3609         touch $DIR/$tdir/foo
3610         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3611         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3612         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3613         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3614         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3615                                         error "link worked"
3616         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3617         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3618         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3619                                                      error "lsattr"
3620         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3621         cp -r $DIR/$tdir /tmp/
3622         rm -fr $DIR/$tdir || error "cleanup rm failed"
3623 }
3624 run_test 52a "append-only flag test (should return errors) ====="
3625
3626 test_52b() {
3627         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3628         test_mkdir -p $DIR/$tdir
3629         touch $DIR/$tdir/foo
3630         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3631         cat test > $DIR/$tdir/foo && error "cat test worked"
3632         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3633         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3634         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3635                                         error "link worked"
3636         echo foo >> $DIR/$tdir/foo && error "echo worked"
3637         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3638         [ -f $DIR/$tdir/foo ] || error
3639         [ -f $DIR/$tdir/foo_ren ] && error
3640         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3641                                                         error "lsattr"
3642         chattr -i $DIR/$tdir/foo || error "chattr failed"
3643
3644         rm -fr $DIR/$tdir || error
3645 }
3646 run_test 52b "immutable flag test (should return errors) ======="
3647
3648 test_53() {
3649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3650         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3651         remote_ost_nodsh && skip "remote OST with nodsh" && return
3652
3653         local param
3654         local ostname
3655         local mds_last
3656         local ost_last
3657         local ostnum
3658         local node
3659
3660         # only test MDT0000
3661         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3662         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3663                 param=`echo ${value[0]} | cut -d "=" -f1`
3664                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3665                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3666                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3667                 node=$(facet_active_host ost$((ostnum+1)))
3668                 param="obdfilter.$ostname.last_id"
3669                 ost_last=$(do_node $node lctl get_param -n $param | head -n 1)
3670                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3671                 if [ $ost_last != $mds_last ]; then
3672                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3673                 fi
3674         done
3675 }
3676 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3677
3678 test_54a() {
3679         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3680         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3681         $SOCKETSERVER $DIR/socket
3682         $SOCKETCLIENT $DIR/socket || error
3683         $MUNLINK $DIR/socket
3684 }
3685 run_test 54a "unix domain socket test =========================="
3686
3687 test_54b() {
3688         f="$DIR/f54b"
3689         mknod $f c 1 3
3690         chmod 0666 $f
3691         dd if=/dev/zero of=$f bs=`page_size` count=1
3692 }
3693 run_test 54b "char device works in lustre ======================"
3694
3695 find_loop_dev() {
3696         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3697         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3698         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3699
3700         for i in `seq 3 7`; do
3701                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3702                 LOOPDEV=$LOOPBASE$i
3703                 LOOPNUM=$i
3704                 break
3705         done
3706 }
3707
3708 test_54c() {
3709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3710         tfile="$DIR/f54c"
3711         tdir="$DIR/d54c"
3712         loopdev="$DIR/loop54c"
3713
3714         find_loop_dev
3715         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3716         mknod $loopdev b 7 $LOOPNUM
3717         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3718         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3719         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3720         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3721         test_mkdir -p $tdir
3722         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3723         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3724         df $tdir
3725         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3726         $UMOUNT $tdir
3727         losetup -d $loopdev
3728         rm $loopdev
3729 }
3730 run_test 54c "block device works in lustre ====================="
3731
3732 test_54d() {
3733         f="$DIR/f54d"
3734         string="aaaaaa"
3735         mknod $f p
3736         [ "$string" = `echo $string > $f | cat $f` ] || error
3737 }
3738 run_test 54d "fifo device works in lustre ======================"
3739
3740 test_54e() {
3741         check_kernel_version 46 || return 0
3742         f="$DIR/f54e"
3743         string="aaaaaa"
3744         cp -aL /dev/console $f
3745         echo $string > $f || error
3746 }
3747 run_test 54e "console/tty device works in lustre ======================"
3748
3749 #The test_55 used to be iopen test and it was removed by bz#24037.
3750 #run_test 55 "check iopen_connect_dentry() ======================"
3751
3752 test_56a() {    # was test_56
3753         rm -rf $DIR/$tdir
3754         $SETSTRIPE -d $DIR
3755         test_mkdir $DIR/$tdir
3756         test_mkdir $DIR/$tdir/dir
3757         NUMFILES=3
3758         NUMFILESx2=$(($NUMFILES * 2))
3759         for i in `seq 1 $NUMFILES` ; do
3760                 touch $DIR/$tdir/file$i
3761                 touch $DIR/$tdir/dir/file$i
3762         done
3763
3764         # test lfs getstripe with --recursive
3765         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3766         [ $FILENUM -eq $NUMFILESx2 ] ||
3767                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3768         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3769         [ $FILENUM -eq $NUMFILES ] ||
3770                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3771         echo "$GETSTRIPE --recursive passed."
3772
3773         # test lfs getstripe with file instead of dir
3774         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3775         [ $FILENUM  -eq 1 ] || error \
3776                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3777         echo "$GETSTRIPE file1 passed."
3778
3779         #test lfs getstripe with --verbose
3780         [ `$GETSTRIPE --verbose $DIR/$tdir |
3781                         grep -c lmm_magic` -eq $NUMFILES ] ||
3782                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3783         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3784             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3785         echo "$GETSTRIPE --verbose passed."
3786
3787         #test lfs getstripe with --obd
3788         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3789                                         grep -q "unknown obduuid" ||
3790                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3791
3792         [  "$OSTCOUNT" -lt 2 ] &&
3793                 skip_env "skipping other $GETSTRIPE --obd test" && return
3794
3795         OSTIDX=1
3796         OBDUUID=$(ostuuid_from_index $OSTIDX)
3797         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3798         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3799         [ $FOUND -eq $FILENUM ] ||
3800                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3801         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3802                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3803                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3804                 error "$GETSTRIPE --obd: should not show file on other obd"
3805         echo "$GETSTRIPE --obd passed"
3806 }
3807 run_test 56a "check $GETSTRIPE"
3808
3809 NUMFILES=3
3810 NUMDIRS=3
3811 setup_56() {
3812         local LOCAL_NUMFILES="$1"
3813         local LOCAL_NUMDIRS="$2"
3814         local MKDIR_PARAMS="$3"
3815
3816         if [ ! -d "$TDIR" ] ; then
3817                 test_mkdir -p $TDIR
3818                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3819                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3820                         touch $TDIR/file$i
3821                 done
3822                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3823                         test_mkdir $TDIR/dir$i
3824                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3825                                 touch $TDIR/dir$i/file$j
3826                         done
3827                 done
3828         fi
3829 }
3830
3831 setup_56_special() {
3832         LOCAL_NUMFILES=$1
3833         LOCAL_NUMDIRS=$2
3834         setup_56 $1 $2
3835         if [ ! -e "$TDIR/loop1b" ] ; then
3836                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3837                         mknod $TDIR/loop${i}b b 7 $i
3838                         mknod $TDIR/null${i}c c 1 3
3839                         ln -s $TDIR/file1 $TDIR/link${i}l
3840                 done
3841                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3842                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3843                         mknod $TDIR/dir$i/null${i}c c 1 3
3844                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3845                 done
3846         fi
3847 }
3848
3849 test_56g() {
3850         $SETSTRIPE -d $DIR
3851
3852         TDIR=$DIR/${tdir}g
3853         setup_56 $NUMFILES $NUMDIRS
3854
3855         EXPECTED=$(($NUMDIRS + 2))
3856         # test lfs find with -name
3857         for i in $(seq 1 $NUMFILES) ; do
3858                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3859                 [ $NUMS -eq $EXPECTED ] ||
3860                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3861                               "found $NUMS, expected $EXPECTED"
3862         done
3863 }
3864 run_test 56g "check lfs find -name ============================="
3865
3866 test_56h() {
3867         $SETSTRIPE -d $DIR
3868
3869         TDIR=$DIR/${tdir}g
3870         setup_56 $NUMFILES $NUMDIRS
3871
3872         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3873         # test lfs find with ! -name
3874         for i in $(seq 1 $NUMFILES) ; do
3875                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3876                 [ $NUMS -eq $EXPECTED ] ||
3877                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3878                               "found $NUMS, expected $EXPECTED"
3879         done
3880 }
3881 run_test 56h "check lfs find ! -name ============================="
3882
3883 test_56i() {
3884        tdir=${tdir}i
3885        test_mkdir -p $DIR/$tdir
3886        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3887        CMD="$LFIND -ost $UUID $DIR/$tdir"
3888        OUT=$($CMD)
3889        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3890 }
3891 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3892
3893 test_56j() {
3894         TDIR=$DIR/${tdir}g
3895         setup_56_special $NUMFILES $NUMDIRS
3896
3897         EXPECTED=$((NUMDIRS + 1))
3898         CMD="$LFIND -type d $TDIR"
3899         NUMS=$($CMD | wc -l)
3900         [ $NUMS -eq $EXPECTED ] ||
3901                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3902 }
3903 run_test 56j "check lfs find -type d ============================="
3904
3905 test_56k() {
3906         TDIR=$DIR/${tdir}g
3907         setup_56_special $NUMFILES $NUMDIRS
3908
3909         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3910         CMD="$LFIND -type f $TDIR"
3911         NUMS=$($CMD | wc -l)
3912         [ $NUMS -eq $EXPECTED ] ||
3913                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3914 }
3915 run_test 56k "check lfs find -type f ============================="
3916
3917 test_56l() {
3918         TDIR=$DIR/${tdir}g
3919         setup_56_special $NUMFILES $NUMDIRS
3920
3921         EXPECTED=$((NUMDIRS + NUMFILES))
3922         CMD="$LFIND -type b $TDIR"
3923         NUMS=$($CMD | wc -l)
3924         [ $NUMS -eq $EXPECTED ] ||
3925                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3926 }
3927 run_test 56l "check lfs find -type b ============================="
3928
3929 test_56m() {
3930         TDIR=$DIR/${tdir}g
3931         setup_56_special $NUMFILES $NUMDIRS
3932
3933         EXPECTED=$((NUMDIRS + NUMFILES))
3934         CMD="$LFIND -type c $TDIR"
3935         NUMS=$($CMD | wc -l)
3936         [ $NUMS -eq $EXPECTED ] ||
3937                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3938 }
3939 run_test 56m "check lfs find -type c ============================="
3940
3941 test_56n() {
3942         TDIR=$DIR/${tdir}g
3943         setup_56_special $NUMFILES $NUMDIRS
3944
3945         EXPECTED=$((NUMDIRS + NUMFILES))
3946         CMD="$LFIND -type l $TDIR"
3947         NUMS=$($CMD | wc -l)
3948         [ $NUMS -eq $EXPECTED ] ||
3949                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3950 }
3951 run_test 56n "check lfs find -type l ============================="
3952
3953 test_56o() {
3954         TDIR=$DIR/${tdir}o
3955         setup_56 $NUMFILES $NUMDIRS
3956
3957         utime $TDIR/file1 > /dev/null || error "utime (1)"
3958         utime $TDIR/file2 > /dev/null || error "utime (2)"
3959         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3960         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3961         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3962         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3963
3964         EXPECTED=4
3965         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3966         [ $NUMS -eq $EXPECTED ] || \
3967                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3968
3969         EXPECTED=12
3970         CMD="$LFIND -mtime 0 $TDIR"
3971         NUMS=$($CMD | wc -l)
3972         [ $NUMS -eq $EXPECTED ] ||
3973                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3974 }
3975 run_test 56o "check lfs find -mtime for old files =========================="
3976
3977 test_56p() {
3978         [ $RUNAS_ID -eq $UID ] &&
3979                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3980
3981         TDIR=$DIR/${tdir}p
3982         setup_56 $NUMFILES $NUMDIRS
3983
3984         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3985         EXPECTED=$NUMFILES
3986         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3987         NUMS=$($CMD | wc -l)
3988         [ $NUMS -eq $EXPECTED ] || \
3989                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3990
3991         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3992         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3993         NUMS=$($CMD | wc -l)
3994         [ $NUMS -eq $EXPECTED ] || \
3995                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3996 }
3997 run_test 56p "check lfs find -uid and ! -uid ==============================="
3998
3999 test_56q() {
4000         [ $RUNAS_ID -eq $UID ] &&
4001                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4002
4003         TDIR=$DIR/${tdir}q
4004         setup_56 $NUMFILES $NUMDIRS
4005
4006         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4007
4008         EXPECTED=$NUMFILES
4009         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4010         NUMS=$($CMD | wc -l)
4011         [ $NUMS -eq $EXPECTED ] ||
4012                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4013
4014         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4015         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4016         NUMS=$($CMD | wc -l)
4017         [ $NUMS -eq $EXPECTED ] ||
4018                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4019 }
4020 run_test 56q "check lfs find -gid and ! -gid ==============================="
4021
4022 test_56r() {
4023         TDIR=$DIR/${tdir}r
4024         setup_56 $NUMFILES $NUMDIRS
4025
4026         EXPECTED=12
4027         CMD="$LFIND -size 0 -type f $TDIR"
4028         NUMS=$($CMD | wc -l)
4029         [ $NUMS -eq $EXPECTED ] ||
4030                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4031         EXPECTED=0
4032         CMD="$LFIND ! -size 0 -type f $TDIR"
4033         NUMS=$($CMD | wc -l)
4034         [ $NUMS -eq $EXPECTED ] ||
4035                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4036         echo "test" > $TDIR/$tfile
4037         echo "test2" > $TDIR/$tfile.2 && sync
4038         EXPECTED=1
4039         CMD="$LFIND -size 5 -type f $TDIR"
4040         NUMS=$($CMD | wc -l)
4041         [ $NUMS -eq $EXPECTED ] ||
4042                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4043         EXPECTED=1
4044         CMD="$LFIND -size +5 -type f $TDIR"
4045         NUMS=$($CMD | wc -l)
4046         [ $NUMS -eq $EXPECTED ] ||
4047                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4048         EXPECTED=2
4049         CMD="$LFIND -size +0 -type f $TDIR"
4050         NUMS=$($CMD | wc -l)
4051         [ $NUMS -eq $EXPECTED ] ||
4052                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4053         EXPECTED=2
4054         CMD="$LFIND ! -size -5 -type f $TDIR"
4055         NUMS=$($CMD | wc -l)
4056         [ $NUMS -eq $EXPECTED ] ||
4057                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4058         EXPECTED=12
4059         CMD="$LFIND -size -5 -type f $TDIR"
4060         NUMS=$($CMD | wc -l)
4061         [ $NUMS -eq $EXPECTED ] ||
4062                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4063 }
4064 run_test 56r "check lfs find -size works =========================="
4065
4066 test_56s() { # LU-611
4067         TDIR=$DIR/${tdir}s
4068         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4069
4070         if [ $OSTCOUNT -gt 1 ]; then
4071                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4072                 ONESTRIPE=4
4073                 EXTRA=4
4074         else
4075                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4076                 EXTRA=0
4077         fi
4078
4079         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4080         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4081         NUMS=$($CMD | wc -l)
4082         [ $NUMS -eq $EXPECTED ] ||
4083                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4084
4085         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4086         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4087         NUMS=$($CMD | wc -l)
4088         [ $NUMS -eq $EXPECTED ] ||
4089                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4090
4091         EXPECTED=$ONESTRIPE
4092         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4093         NUMS=$($CMD | wc -l)
4094         [ $NUMS -eq $EXPECTED ] ||
4095                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4096
4097         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4098         NUMS=$($CMD | wc -l)
4099         [ $NUMS -eq $EXPECTED ] ||
4100                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4101
4102         EXPECTED=0
4103         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4104         NUMS=$($CMD | wc -l)
4105         [ $NUMS -eq $EXPECTED ] ||
4106                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4107 }
4108 run_test 56s "check lfs find -stripe-count works"
4109
4110 test_56t() { # LU-611
4111         TDIR=$DIR/${tdir}t
4112         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4113
4114         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4115
4116         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4117         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4118         NUMS=$($CMD | wc -l)
4119         [ $NUMS -eq $EXPECTED ] ||
4120                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4121
4122         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4123         NUMS=$($CMD | wc -l)
4124         [ $NUMS -eq $EXPECTED ] ||
4125                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4126
4127         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4128         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4129         NUMS=$($CMD | wc -l)
4130         [ $NUMS -eq $EXPECTED ] ||
4131                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4132
4133         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4134         NUMS=$($CMD | wc -l)
4135         [ $NUMS -eq $EXPECTED ] ||
4136                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4137
4138         EXPECTED=4
4139         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4140         NUMS=$($CMD | wc -l)
4141         [ $NUMS -eq $EXPECTED ] ||
4142                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4143
4144         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4145         NUMS=$($CMD | wc -l)
4146         [ $NUMS -eq $EXPECTED ] ||
4147                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4148
4149         EXPECTED=0
4150         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4151         NUMS=$($CMD | wc -l)
4152         [ $NUMS -eq $EXPECTED ] ||
4153                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4154 }
4155 run_test 56t "check lfs find -stripe-size works"
4156
4157 test_56u() { # LU-611
4158         TDIR=$DIR/${tdir}u
4159         setup_56 $NUMFILES $NUMDIRS "-i 0"
4160
4161         if [ $OSTCOUNT -gt 1 ]; then
4162                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4163                 ONESTRIPE=4
4164         else
4165                 ONESTRIPE=0
4166         fi
4167
4168         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4169         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4170         NUMS=$($CMD | wc -l)
4171         [ $NUMS -eq $EXPECTED ] ||
4172                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4173
4174         EXPECTED=$ONESTRIPE
4175         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4176         NUMS=$($CMD | wc -l)
4177         [ $NUMS -eq $EXPECTED ] ||
4178                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4179
4180         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4181         NUMS=$($CMD | wc -l)
4182         [ $NUMS -eq $EXPECTED ] ||
4183                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4184
4185         EXPECTED=0
4186         # This should produce an error and not return any files
4187         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4188         NUMS=$($CMD 2>/dev/null | wc -l)
4189         [ $NUMS -eq $EXPECTED ] ||
4190                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4191
4192         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4193         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4194         NUMS=$($CMD | wc -l)
4195         [ $NUMS -eq $EXPECTED ] ||
4196                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4197 }
4198 run_test 56u "check lfs find -stripe-index works"
4199
4200 test_56v() {
4201     local MDT_IDX=0
4202
4203     TDIR=$DIR/${tdir}v
4204     rm -rf $TDIR
4205     setup_56 $NUMFILES $NUMDIRS
4206
4207     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4208     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4209
4210     for file in $($LFIND -mdt $UUID $TDIR); do
4211         file_mdt_idx=$($GETSTRIPE -M $file)
4212         [ $file_mdt_idx -eq $MDT_IDX ] ||
4213             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4214     done
4215 }
4216 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4217
4218 # Get and check the actual stripe count of one file.
4219 # Usage: check_stripe_count <file> <expected_stripe_count>
4220 check_stripe_count() {
4221     local file=$1
4222     local expected=$2
4223     local actual
4224
4225     [[ -z "$file" || -z "$expected" ]] &&
4226         error "check_stripe_count: invalid argument!"
4227
4228     local cmd="$GETSTRIPE -c $file"
4229     actual=$($cmd) || error "$cmd failed"
4230     actual=${actual%% *}
4231
4232     if [[ $actual -ne $expected ]]; then
4233         [[ $expected -eq -1 ]] ||
4234             error "$cmd wrong: found $actual, expected $expected"
4235         [[ $actual -eq $OSTCOUNT ]] ||
4236             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4237     fi
4238 }
4239
4240 test_56w() {
4241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4242         TDIR=$DIR/${tdir}w
4243
4244     rm -rf $TDIR || error "remove $TDIR failed"
4245     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4246
4247     local stripe_size
4248     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4249         error "$GETSTRIPE -S -d $TDIR failed"
4250     stripe_size=${stripe_size%% *}
4251
4252     local file_size=$((stripe_size * OSTCOUNT))
4253     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4254     local required_space=$((file_num * file_size))
4255     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4256     [[ $free_space -le $((required_space / 1024)) ]] &&
4257         skip_env "need at least $required_space bytes free space," \
4258                  "have $free_space kbytes" && return
4259
4260     local dd_bs=65536
4261     local dd_count=$((file_size / dd_bs))
4262
4263     # write data into the files
4264     local i
4265     local j
4266     local file
4267     for i in $(seq 1 $NUMFILES); do
4268         file=$TDIR/file$i
4269         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4270             error "write data into $file failed"
4271     done
4272     for i in $(seq 1 $NUMDIRS); do
4273         for j in $(seq 1 $NUMFILES); do
4274             file=$TDIR/dir$i/file$j
4275             yes | dd bs=$dd_bs count=$dd_count of=$file \
4276                 >/dev/null 2>&1 ||
4277                 error "write data into $file failed"
4278         done
4279     done
4280
4281     local expected=-1
4282     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4283
4284     # lfs_migrate file
4285     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4286     echo "$cmd"
4287     eval $cmd || error "$cmd failed"
4288
4289     check_stripe_count $TDIR/file1 $expected
4290
4291     # lfs_migrate dir
4292     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4293     echo "$cmd"
4294     eval $cmd || error "$cmd failed"
4295
4296     for j in $(seq 1 $NUMFILES); do
4297         check_stripe_count $TDIR/dir1/file$j $expected
4298     done
4299
4300     # lfs_migrate works with lfs find
4301     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4302          $LFS_MIGRATE -y -c $expected"
4303     echo "$cmd"
4304     eval $cmd || error "$cmd failed"
4305
4306     for i in $(seq 2 $NUMFILES); do
4307         check_stripe_count $TDIR/file$i $expected
4308     done
4309     for i in $(seq 2 $NUMDIRS); do
4310         for j in $(seq 1 $NUMFILES); do
4311             check_stripe_count $TDIR/dir$i/file$j $expected
4312         done
4313     done
4314 }
4315 run_test 56w "check lfs_migrate -c stripe_count works"
4316
4317 test_57a() {
4318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4319         # note test will not do anything if MDS is not local
4320         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4321                 skip "Only applicable to ldiskfs-based MDTs"
4322                 return
4323         fi
4324
4325         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4326         local MNTDEV="osd*.*MDT*.mntdev"
4327         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4328         [ -z "$DEV" ] && error "can't access $MNTDEV"
4329         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4330                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4331                         error "can't access $DEV"
4332                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4333                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4334                 rm $TMP/t57a.dump
4335         done
4336 }
4337 run_test 57a "verify MDS filesystem created with large inodes =="
4338
4339 test_57b() {
4340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4341         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4342                 skip "Only applicable to ldiskfs-based MDTs"
4343                 return
4344         fi
4345
4346         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4347         local dir=$DIR/d57b
4348
4349         local FILECOUNT=100
4350         local FILE1=$dir/f1
4351         local FILEN=$dir/f$FILECOUNT
4352
4353         rm -rf $dir || error "removing $dir"
4354         test_mkdir -p $dir || error "creating $dir"
4355         local num=$(get_mds_dir $dir)
4356         local mymds=mds$num
4357
4358         echo "mcreating $FILECOUNT files"
4359         createmany -m $dir/f 1 $FILECOUNT || \
4360                 error "creating files in $dir"
4361
4362         # verify that files do not have EAs yet
4363         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4364         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4365
4366         sync
4367         sleep 1
4368         df $dir  #make sure we get new statfs data
4369         local MDSFREE=$(do_facet $mymds \
4370                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4371         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4372         echo "opening files to create objects/EAs"
4373         local FILE
4374         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4375                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4376         done
4377
4378         # verify that files have EAs now
4379         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4380         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4381
4382         sleep 1  #make sure we get new statfs data
4383         df $dir
4384         local MDSFREE2=$(do_facet $mymds \
4385                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4386         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4387         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4388                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4389                         error "MDC before $MDCFREE != after $MDCFREE2"
4390                 else
4391                         echo "MDC before $MDCFREE != after $MDCFREE2"
4392                         echo "unable to confirm if MDS has large inodes"
4393                 fi
4394         fi
4395         rm -rf $dir
4396 }
4397 run_test 57b "default LOV EAs are stored inside large inodes ==="
4398
4399 test_58() {
4400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4401         [ -z "$(which wiretest 2>/dev/null)" ] &&
4402                         skip_env "could not find wiretest" && return
4403         wiretest
4404 }
4405 run_test 58 "verify cross-platform wire constants =============="
4406
4407 test_59() {
4408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4409         echo "touch 130 files"
4410         createmany -o $DIR/f59- 130
4411         echo "rm 130 files"
4412         unlinkmany $DIR/f59- 130
4413         sync
4414         # wait for commitment of removal
4415         wait_delete_completed
4416 }
4417 run_test 59 "verify cancellation of llog records async ========="
4418
4419 TEST60_HEAD="test_60 run $RANDOM"
4420 test_60a() {
4421         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4422         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4423         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4424         log "$TEST60_HEAD - from kernel mode"
4425         do_facet mgs sh run-llog.sh
4426 }
4427 run_test 60a "llog sanity tests run from kernel module =========="
4428
4429 test_60b() { # bug 6411
4430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4431         dmesg > $DIR/$tfile
4432         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4433                                  /llog.test/ {
4434                                          if (marker)
4435                                                  from_marker++
4436                                          from_begin++
4437                                  }
4438                                  END {
4439                                          if (marker)
4440                                                  print from_marker
4441                                          else
4442                                                  print from_begin
4443                                  }"`
4444         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4445 }
4446 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4447
4448 test_60c() {
4449         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4450         echo "create 5000 files"
4451         createmany -o $DIR/f60c- 5000
4452 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4453         lctl set_param fail_loc=0x80000137
4454         unlinkmany $DIR/f60c- 5000
4455         lctl set_param fail_loc=0
4456 }
4457 run_test 60c "unlink file when mds full"
4458
4459 test_60d() {
4460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4461         SAVEPRINTK=$(lctl get_param -n printk)
4462
4463         # verify "lctl mark" is even working"
4464         MESSAGE="test message ID $RANDOM $$"
4465         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4466         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4467
4468         lctl set_param printk=0 || error "set lnet.printk failed"
4469         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4470         MESSAGE="new test message ID $RANDOM $$"
4471         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4472         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4473         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4474
4475         lctl set_param -n printk="$SAVEPRINTK"
4476 }
4477 run_test 60d "test printk console message masking"
4478
4479 test_61() {
4480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4481         f="$DIR/f61"
4482         dd if=/dev/zero of=$f bs=`page_size` count=1
4483         cancel_lru_locks osc
4484         $MULTIOP $f OSMWUc || error
4485         sync
4486 }
4487 run_test 61 "mmap() writes don't make sync hang ================"
4488
4489 # bug 2330 - insufficient obd_match error checking causes LBUG
4490 test_62() {
4491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4492         f="$DIR/f62"
4493         echo foo > $f
4494         cancel_lru_locks osc
4495         lctl set_param fail_loc=0x405
4496         cat $f && error "cat succeeded, expect -EIO"
4497         lctl set_param fail_loc=0
4498 }
4499 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4500 # match every page all of the time.
4501 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4502
4503 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4504 test_63a() {    # was test_63
4505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4506         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4507         lctl set_param -n osc.*.max_dirty_mb 0
4508         for i in `seq 10` ; do
4509                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4510                 sleep 5
4511                 kill $!
4512                 sleep 1
4513         done
4514
4515         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4516         rm -f $DIR/f63 || true
4517 }
4518 run_test 63a "Verify oig_wait interruption does not crash ======="
4519
4520 # bug 2248 - async write errors didn't return to application on sync
4521 # bug 3677 - async write errors left page locked
4522 test_63b() {
4523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4524         debugsave
4525         lctl set_param debug=-1
4526
4527         # ensure we have a grant to do async writes
4528         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4529         rm $DIR/$tfile
4530
4531         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4532         lctl set_param fail_loc=0x80000406
4533         $MULTIOP $DIR/$tfile Owy && \
4534                 error "sync didn't return ENOMEM"
4535         sync; sleep 2; sync     # do a real sync this time to flush page
4536         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4537                 error "locked page left in cache after async error" || true
4538         debugrestore
4539 }
4540 run_test 63b "async write errors should be returned to fsync ==="
4541
4542 test_64a () {
4543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4544         df $DIR
4545         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4546 }
4547 run_test 64a "verify filter grant calculations (in kernel) ====="
4548
4549 test_64b () {
4550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4551         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4552         sh oos.sh $MOUNT
4553 }
4554 run_test 64b "check out-of-space detection on client ==========="
4555
4556 test_64c() {
4557         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4558 }
4559 run_test 64c "verify grant shrink ========================------"
4560
4561 # bug 1414 - set/get directories' stripe info
4562 test_65a() {
4563         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4564         test_mkdir -p $DIR/$tdir
4565         touch $DIR/$tdir/f1
4566         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4567 }
4568 run_test 65a "directory with no stripe info ===================="
4569
4570 test_65b() {
4571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4572         test_mkdir -p $DIR/$tdir
4573         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4574                                                 error "setstripe"
4575         touch $DIR/$tdir/f2
4576         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4577 }
4578 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4579
4580 test_65c() {
4581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4582         if [ $OSTCOUNT -gt 1 ]; then
4583                 test_mkdir -p $DIR/$tdir
4584                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4585                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4586                 touch $DIR/$tdir/f3
4587                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4588         fi
4589 }
4590 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4591
4592 test_65d() {
4593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4594         test_mkdir -p $DIR/$tdir
4595         if [ $STRIPECOUNT -le 0 ]; then
4596                 sc=1
4597         elif [ $STRIPECOUNT -gt 2000 ]; then
4598 #LOV_MAX_STRIPE_COUNT is 2000
4599                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4600         else
4601                 sc=$(($STRIPECOUNT - 1))
4602         fi
4603         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4604         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4605         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4606                                                 error "lverify failed"
4607 }
4608 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4609
4610 test_65e() {
4611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4612         test_mkdir -p $DIR/$tdir
4613
4614         $SETSTRIPE $DIR/$tdir || error "setstripe"
4615         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4616                                         error "no stripe info failed"
4617         touch $DIR/$tdir/f6
4618         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4619 }
4620 run_test 65e "directory setstripe defaults ======================="
4621
4622 test_65f() {
4623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4624         test_mkdir -p $DIR/${tdir}f
4625         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4626 }
4627 run_test 65f "dir setstripe permission (should return error) ==="
4628
4629 test_65g() {
4630         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4631         test_mkdir -p $DIR/$tdir
4632         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4633                                                         error "setstripe"
4634         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4635         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4636                 error "delete default stripe failed"
4637 }
4638 run_test 65g "directory setstripe -d ==========================="
4639
4640 test_65h() {
4641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4642         test_mkdir -p $DIR/$tdir
4643         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4644                                                         error "setstripe"
4645         test_mkdir -p $DIR/$tdir/dd1
4646         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4647                 error "stripe info inherit failed"
4648 }
4649 run_test 65h "directory stripe info inherit ===================="
4650
4651 test_65i() { # bug6367
4652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4653         $SETSTRIPE -S 65536 -c -1 $MOUNT
4654 }
4655 run_test 65i "set non-default striping on root directory (bug 6367)="
4656
4657 test_65ia() { # bug12836
4658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4659         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4660 }
4661 run_test 65ia "getstripe on -1 default directory striping"
4662
4663 test_65ib() { # bug12836
4664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4665         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4666 }
4667 run_test 65ib "getstripe -v on -1 default directory striping"
4668
4669 test_65ic() { # bug12836
4670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4671         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4672 }
4673 run_test 65ic "new find on -1 default directory striping"
4674
4675 test_65j() { # bug6367
4676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4677         sync; sleep 1
4678         # if we aren't already remounting for each test, do so for this test
4679         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4680                 cleanup || error "failed to unmount"
4681                 setup
4682         fi
4683         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4684 }
4685 run_test 65j "set default striping on root directory (bug 6367)="
4686
4687 test_65k() { # bug11679
4688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4689         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4690         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4691
4692     echo "Check OST status: "
4693     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4694               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4695
4696     for OSC in $MDS_OSCS; do
4697         echo $OSC "is activate"
4698         do_facet $SINGLEMDS lctl --device %$OSC activate
4699     done
4700
4701     mkdir -p $DIR/$tdir
4702     for INACTIVE_OSC in $MDS_OSCS; do
4703         echo "Deactivate: " $INACTIVE_OSC
4704         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4705         for STRIPE_OSC in $MDS_OSCS; do
4706             OST=`osc_to_ost $STRIPE_OSC`
4707             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4708                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4709
4710             [ -f $DIR/$tdir/$IDX ] && continue
4711             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4712             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4713             RC=$?
4714             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4715         done
4716         rm -f $DIR/$tdir/*
4717         echo $INACTIVE_OSC "is Activate."
4718         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4719     done
4720 }
4721 run_test 65k "validate manual striping works properly with deactivated OSCs"
4722
4723 test_65l() { # bug 12836
4724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4725         test_mkdir -p $DIR/$tdir/test_dir
4726         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4727         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4728 }
4729 run_test 65l "lfs find on -1 stripe dir ========================"
4730
4731 # bug 2543 - update blocks count on client
4732 test_66() {
4733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4734         COUNT=${COUNT:-8}
4735         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4736         sync; sync_all_data; sync; sync_all_data
4737         cancel_lru_locks osc
4738         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4739         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4740 }
4741 run_test 66 "update inode blocks count on client ==============="
4742
4743 LLOOP=
4744 LLITELOOPLOAD=
4745 cleanup_68() {
4746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4747         trap 0
4748         if [ ! -z "$LLOOP" ]; then
4749                 if swapon -s | grep -q $LLOOP; then
4750                         swapoff $LLOOP || error "swapoff failed"
4751                 fi
4752
4753                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4754                 rm -f $LLOOP
4755                 unset LLOOP
4756         fi
4757         if [ ! -z "$LLITELOOPLOAD" ]; then
4758                 rmmod llite_lloop
4759                 unset LLITELOOPLOAD
4760         fi
4761         rm -f $DIR/f68*
4762 }
4763
4764 meminfo() {
4765         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4766 }
4767
4768 swap_used() {
4769         swapon -s | awk '($1 == "'$1'") { print $4 }'
4770 }
4771
4772 # test case for lloop driver, basic function
4773 test_68a() {
4774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4775         [ "$UID" != 0 ] && skip_env "must run as root" && return
4776         llite_lloop_enabled || \
4777                 { skip_env "llite_lloop module disabled" && return; }
4778
4779         trap cleanup_68 EXIT
4780
4781         if ! module_loaded llite_lloop; then
4782                 if load_module llite/llite_lloop; then
4783                         LLITELOOPLOAD=yes
4784                 else
4785                         skip_env "can't find module llite_lloop"
4786                         return
4787                 fi
4788         fi
4789
4790         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4791         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4792         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4793
4794         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4795         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4796
4797         cleanup_68
4798 }
4799 run_test 68a "lloop driver - basic test ========================"
4800
4801 # excercise swapping to lustre by adding a high priority swapfile entry
4802 # and then consuming memory until it is used.
4803 test_68b() {  # was test_68
4804         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4805         [ "$UID" != 0 ] && skip_env "must run as root" && return
4806         lctl get_param -n devices | grep -q obdfilter && \
4807                 skip "local OST" && return
4808
4809         grep -q llite_lloop /proc/modules
4810         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4811
4812         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4813                 skip "can't reliably test swap with TCP" && return
4814
4815         MEMTOTAL=`meminfo MemTotal`
4816         NR_BLOCKS=$((MEMTOTAL>>8))
4817         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4818
4819         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4820         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4821         mkswap $DIR/f68b
4822
4823         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4824
4825         trap cleanup_68 EXIT
4826
4827         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4828
4829         echo "before: `swapon -s | grep $LLOOP`"
4830         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4831         echo "after: `swapon -s | grep $LLOOP`"
4832         SWAPUSED=`swap_used $LLOOP`
4833
4834         cleanup_68
4835
4836         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4837 }
4838 run_test 68b "support swapping to Lustre ========================"
4839
4840 # bug5265, obdfilter oa2dentry return -ENOENT
4841 # #define OBD_FAIL_OST_ENOENT 0x217
4842 test_69() {
4843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4844         remote_ost_nodsh && skip "remote OST with nodsh" && return
4845
4846         f="$DIR/$tfile"
4847         $SETSTRIPE -c 1 -i 0 $f
4848
4849         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4850
4851         do_facet ost1 lctl set_param fail_loc=0x217
4852         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4853         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4854
4855         do_facet ost1 lctl set_param fail_loc=0
4856         $DIRECTIO write $f 0 2 || error "write error"
4857
4858         cancel_lru_locks osc
4859         $DIRECTIO read $f 0 1 || error "read error"
4860
4861         do_facet ost1 lctl set_param fail_loc=0x217
4862         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4863
4864         do_facet ost1 lctl set_param fail_loc=0
4865         rm -f $f
4866 }
4867 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4868
4869 test_71() {
4870     test_mkdir -p $DIR/$tdir
4871     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4872 }
4873 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4874
4875 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4877         check_kernel_version 43 || return 0
4878         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4879
4880         # Check that testing environment is properly set up. Skip if not
4881         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4882                 skip_env "User $RUNAS_ID does not exist - skipping"
4883                 return 0
4884         }
4885         # We had better clear the $DIR to get enough space for dd
4886         rm -rf $DIR/*
4887         touch $DIR/f72
4888         chmod 777 $DIR/f72
4889         chmod ug+s $DIR/f72
4890         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4891         # See if we are still setuid/sgid
4892         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4893         # Now test that MDS is updated too
4894         cancel_lru_locks mdc
4895         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4896         rm -f $DIR/f72
4897 }
4898 run_test 72a "Test that remove suid works properly (bug5695) ===="
4899
4900 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4901         local perm
4902
4903         [ "$RUNAS_ID" = "$UID" ] && \
4904                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4905         [ "$RUNAS_ID" -eq 0 ] && \
4906                 skip_env "RUNAS_ID = 0 -- skipping" && return
4907
4908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4909         # Check that testing environment is properly set up. Skip if not
4910         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4911                 skip_env "User $RUNAS_ID does not exist - skipping"
4912                 return 0
4913         }
4914         touch $DIR/${tfile}-f{g,u}
4915         test_mkdir $DIR/${tfile}-dg
4916         test_mkdir $DIR/${tfile}-du
4917         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4918         chmod g+s $DIR/${tfile}-{f,d}g
4919         chmod u+s $DIR/${tfile}-{f,d}u
4920         for perm in 777 2777 4777; do
4921                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4922                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4923                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4924                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4925         done
4926         true
4927 }
4928 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4929
4930 # bug 3462 - multiple simultaneous MDC requests
4931 test_73() {
4932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4933         test_mkdir $DIR/d73-1
4934         test_mkdir $DIR/d73-2
4935         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4936         pid1=$!
4937
4938         lctl set_param fail_loc=0x80000129
4939         $MULTIOP $DIR/d73-1/f73-2 Oc &
4940         sleep 1
4941         lctl set_param fail_loc=0
4942
4943         $MULTIOP $DIR/d73-2/f73-3 Oc &
4944         pid3=$!
4945
4946         kill -USR1 $pid1
4947         wait $pid1 || return 1
4948
4949         sleep 25
4950
4951         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4952         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4953         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4954
4955         rm -rf $DIR/d73-*
4956 }
4957 run_test 73 "multiple MDC requests (should not deadlock)"
4958
4959 test_74a() { # bug 6149, 6184
4960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4961         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4962         #
4963         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4964         # will spin in a tight reconnection loop
4965         touch $DIR/f74a
4966         lctl set_param fail_loc=0x8000030e
4967         # get any lock that won't be difficult - lookup works.
4968         ls $DIR/f74a
4969         lctl set_param fail_loc=0
4970         true
4971         rm -f $DIR/f74a
4972 }
4973 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4974
4975 test_74b() { # bug 13310
4976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4977         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4978         #
4979         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4980         # will spin in a tight reconnection loop
4981         lctl set_param fail_loc=0x8000030e
4982         # get a "difficult" lock
4983         touch $DIR/f74b
4984         lctl set_param fail_loc=0
4985         true
4986         rm -f $DIR/f74b
4987 }
4988 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4989
4990 test_74c() {
4991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4992 #define OBD_FAIL_LDLM_NEW_LOCK
4993         lctl set_param fail_loc=0x80000319
4994         touch $DIR/$tfile && error "Touch successful"
4995         true
4996 }
4997 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4998
4999 num_inodes() {
5000         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5001 }
5002
5003 get_inode_slab_tunables() {
5004         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5005 }
5006
5007 set_inode_slab_tunables() {
5008         echo "lustre_inode_cache $1" > /proc/slabinfo
5009 }
5010
5011 test_76() { # Now for bug 20433, added originally in bug 1443
5012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5013         local SLAB_SETTINGS=`get_inode_slab_tunables`
5014         local CPUS=`getconf _NPROCESSORS_ONLN`
5015         # we cannot set limit below 1 which means 1 inode in each
5016         # per-cpu cache is still allowed
5017         set_inode_slab_tunables "1 1 0"
5018         cancel_lru_locks osc
5019         BEFORE_INODES=`num_inodes`
5020         echo "before inodes: $BEFORE_INODES"
5021         local COUNT=1000
5022         [ "$SLOW" = "no" ] && COUNT=100
5023         for i in `seq $COUNT`; do
5024                 touch $DIR/$tfile
5025                 rm -f $DIR/$tfile
5026         done
5027         cancel_lru_locks osc
5028         AFTER_INODES=`num_inodes`
5029         echo "after inodes: $AFTER_INODES"
5030         local wait=0
5031         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5032                 sleep 2
5033                 AFTER_INODES=`num_inodes`
5034                 wait=$((wait+2))
5035                 echo "wait $wait seconds inodes: $AFTER_INODES"
5036                 if [ $wait -gt 30 ]; then
5037                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5038                 fi
5039         done
5040         set_inode_slab_tunables "$SLAB_SETTINGS"
5041 }
5042 run_test 76 "confirm clients recycle inodes properly ===="
5043
5044
5045 export ORIG_CSUM=""
5046 set_checksums()
5047 {
5048         # Note: in sptlrpc modes which enable its own bulk checksum, the
5049         # original crc32_le bulk checksum will be automatically disabled,
5050         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5051         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5052         # In this case set_checksums() will not be no-op, because sptlrpc
5053         # bulk checksum will be enabled all through the test.
5054
5055         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5056         lctl set_param -n osc.*.checksums $1
5057         return 0
5058 }
5059
5060 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5061                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5062 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5063 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5064 set_checksum_type()
5065 {
5066         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5067         log "set checksum type to $1"
5068         return 0
5069 }
5070 F77_TMP=$TMP/f77-temp
5071 F77SZ=8
5072 setup_f77() {
5073         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5074                 error "error writing to $F77_TMP"
5075 }
5076
5077 test_77a() { # bug 10889
5078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5079         $GSS && skip "could not run with gss" && return
5080         [ ! -f $F77_TMP ] && setup_f77
5081         set_checksums 1
5082         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5083         set_checksums 0
5084         rm -f $DIR/$tfile
5085 }
5086 run_test 77a "normal checksum read/write operation ============="
5087
5088 test_77b() { # bug 10889
5089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5090         $GSS && skip "could not run with gss" && return
5091         [ ! -f $F77_TMP ] && setup_f77
5092         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5093         lctl set_param fail_loc=0x80000409
5094         set_checksums 1
5095         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
5096                 error "dd error: $?"
5097         lctl set_param fail_loc=0
5098         set_checksums 0
5099 }
5100 run_test 77b "checksum error on client write ===================="
5101
5102 test_77c() { # bug 10889
5103         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5104         $GSS && skip "could not run with gss" && return
5105         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
5106         set_checksums 1
5107         for algo in $CKSUM_TYPES; do
5108                 cancel_lru_locks osc
5109                 set_checksum_type $algo
5110                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5111                 lctl set_param fail_loc=0x80000408
5112                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
5113                 lctl set_param fail_loc=0
5114         done
5115         set_checksums 0
5116         set_checksum_type $ORIG_CSUM_TYPE
5117         rm -f $DIR/f77b
5118 }
5119 run_test 77c "checksum error on client read ==================="
5120
5121 test_77d() { # bug 10889
5122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5123         $GSS && skip "could not run with gss" && return
5124         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5125         lctl set_param fail_loc=0x80000409
5126         set_checksums 1
5127         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5128                 error "direct write: rc=$?"
5129         lctl set_param fail_loc=0
5130         set_checksums 0
5131 }
5132 run_test 77d "checksum error on OST direct write ==============="
5133
5134 test_77e() { # bug 10889
5135         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5136         $GSS && skip "could not run with gss" && return
5137         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5138         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5139         lctl set_param fail_loc=0x80000408
5140         set_checksums 1
5141         cancel_lru_locks osc
5142         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5143                 error "direct read: rc=$?"
5144         lctl set_param fail_loc=0
5145         set_checksums 0
5146 }
5147 run_test 77e "checksum error on OST direct read ================"
5148
5149 test_77f() { # bug 10889
5150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5151         $GSS && skip "could not run with gss" && return
5152         set_checksums 1
5153         for algo in $CKSUM_TYPES; do
5154                 cancel_lru_locks osc
5155                 set_checksum_type $algo
5156                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5157                 lctl set_param fail_loc=0x409
5158                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5159                         error "direct write succeeded"
5160                 lctl set_param fail_loc=0
5161         done
5162         set_checksum_type $ORIG_CSUM_TYPE
5163         set_checksums 0
5164 }
5165 run_test 77f "repeat checksum error on write (expect error) ===="
5166
5167 test_77g() { # bug 10889
5168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5169         $GSS && skip "could not run with gss" && return
5170         remote_ost_nodsh && skip "remote OST with nodsh" && return
5171
5172         [ ! -f $F77_TMP ] && setup_f77
5173
5174         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5175         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5176         do_facet ost1 lctl set_param fail_loc=0x8000021a
5177         set_checksums 1
5178         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5179                 error "write error: rc=$?"
5180         do_facet ost1 lctl set_param fail_loc=0
5181         set_checksums 0
5182 }
5183 run_test 77g "checksum error on OST write ======================"
5184
5185 test_77h() { # bug 10889
5186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5187         $GSS && skip "could not run with gss" && return
5188         remote_ost_nodsh && skip "remote OST with nodsh" && return
5189
5190         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5191         cancel_lru_locks osc
5192         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5193         do_facet ost1 lctl set_param fail_loc=0x8000021b
5194         set_checksums 1
5195         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5196         do_facet ost1 lctl set_param fail_loc=0
5197         set_checksums 0
5198 }
5199 run_test 77h "checksum error on OST read ======================="
5200
5201 test_77i() { # bug 13805
5202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5203         $GSS && skip "could not run with gss" && return
5204         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5205         lctl set_param fail_loc=0x40b
5206         remount_client $MOUNT
5207         lctl set_param fail_loc=0
5208         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5209                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5210                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5211                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5212         done
5213         remount_client $MOUNT
5214 }
5215 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5216
5217 test_77j() { # bug 13805
5218         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5219         $GSS && skip "could not run with gss" && return
5220         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5221         lctl set_param fail_loc=0x40c
5222         remount_client $MOUNT
5223         lctl set_param fail_loc=0
5224         sleep 2 # wait async osc connect to finish
5225         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5226                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5227                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5228                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5229         done
5230         remount_client $MOUNT
5231 }
5232 run_test 77j "client only supporting ADLER32 ===================="
5233
5234 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5235 rm -f $F77_TMP
5236 unset F77_TMP
5237
5238 test_78() { # bug 10901
5239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5240         remote_ost || { skip_env "local OST" && return; }
5241
5242         NSEQ=5
5243         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5244         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5245         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5246         echo "MemTotal: $MEMTOTAL"
5247 # reserve 256MB of memory for the kernel and other running processes,
5248 # and then take 1/2 of the remaining memory for the read/write buffers.
5249     if [ $MEMTOTAL -gt 512 ] ;then
5250         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5251     else
5252         # for those poor memory-starved high-end clusters...
5253         MEMTOTAL=$((MEMTOTAL / 2))
5254     fi
5255         echo "Mem to use for directio: $MEMTOTAL"
5256         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5257         [ $F78SIZE -gt 512 ] && F78SIZE=512
5258         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5259         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5260         echo "Smallest OST: $SMALLESTOST"
5261         [ $SMALLESTOST -lt 10240 ] && \
5262                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5263
5264         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5265                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5266
5267         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5268         echo "File size: $F78SIZE"
5269         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5270         for i in `seq 1 $NSEQ`
5271         do
5272                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5273                 echo directIO rdwr round $i of $NSEQ
5274                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5275         done
5276
5277         rm -f $DIR/$tfile
5278 }
5279 run_test 78 "handle large O_DIRECT writes correctly ============"
5280
5281 test_79() { # bug 12743
5282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5283         wait_delete_completed
5284
5285         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5286         BKFREE=$(calc_osc_kbytes kbytesfree)
5287         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5288
5289         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5290         DFTOTAL=`echo $STRING | cut -d, -f1`
5291         DFUSED=`echo $STRING  | cut -d, -f2`
5292         DFAVAIL=`echo $STRING | cut -d, -f3`
5293         DFFREE=$(($DFTOTAL - $DFUSED))
5294
5295         ALLOWANCE=$((64 * $OSTCOUNT))
5296
5297         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5298            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5299                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5300         fi
5301         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5302            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5303                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5304         fi
5305         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5306            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5307                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5308         fi
5309 }
5310 run_test 79 "df report consistency check ======================="
5311
5312 test_80() { # bug 10718
5313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5314         # relax strong synchronous semantics for slow backends like ZFS
5315         local soc="obdfilter.*.sync_on_lock_cancel"
5316         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5317         local hosts=
5318         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5319                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5320                           facet_active_host $host; done | sort -u)
5321                 do_nodes $hosts lctl set_param $soc=never
5322         fi
5323
5324         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5325         sync; sleep 1; sync
5326         local BEFORE=`date +%s`
5327         cancel_lru_locks osc
5328         local AFTER=`date +%s`
5329         local DIFF=$((AFTER-BEFORE))
5330         if [ $DIFF -gt 1 ] ; then
5331                 error "elapsed for 1M@1T = $DIFF"
5332         fi
5333
5334         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5335
5336         rm -f $DIR/$tfile
5337 }
5338 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5339
5340 test_81a() { # LU-456
5341         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5342         remote_ost_nodsh && skip "remote OST with nodsh" && return
5343         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5344         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5345         do_facet ost1 lctl set_param fail_loc=0x80000228
5346
5347         # write should trigger a retry and success
5348         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5349         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5350         RC=$?
5351         if [ $RC -ne 0 ] ; then
5352                 error "write should success, but failed for $RC"
5353         fi
5354 }
5355 run_test 81a "OST should retry write when get -ENOSPC ==============="
5356
5357 test_81b() { # LU-456
5358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5359         remote_ost_nodsh && skip "remote OST with nodsh" && return
5360         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5361         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5362         do_facet ost1 lctl set_param fail_loc=0x228
5363
5364         # write should retry several times and return -ENOSPC finally
5365         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5366         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5367         RC=$?
5368         ENOSPC=28
5369         if [ $RC -ne $ENOSPC ] ; then
5370                 error "dd should fail for -ENOSPC, but succeed."
5371         fi
5372 }
5373 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5374
5375 test_82() { # LU-1031
5376         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5377         local gid1=14091995
5378         local gid2=16022000
5379
5380         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5381         local MULTIPID1=$!
5382         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5383         local MULTIPID2=$!
5384         kill -USR1 $MULTIPID2
5385         sleep 2
5386         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5387                 error "First grouplock does not block second one"
5388         else
5389                 echo "Second grouplock blocks first one"
5390         fi
5391         kill -USR1 $MULTIPID1
5392         wait $MULTIPID1
5393         wait $MULTIPID2
5394 }
5395 run_test 82 "Basic grouplock test ==============================="
5396
5397 test_99a() {
5398         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5399             return
5400         test_mkdir -p $DIR/d99cvsroot
5401         chown $RUNAS_ID $DIR/d99cvsroot
5402         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5403         cd $TMP
5404
5405         $RUNAS cvs -d $DIR/d99cvsroot init || error
5406         cd $oldPWD
5407 }
5408 run_test 99a "cvs init ========================================="
5409
5410 test_99b() {
5411         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5412         [ ! -d $DIR/d99cvsroot ] && test_99a
5413         cd /etc/init.d
5414         # some versions of cvs import exit(1) when asked to import links or
5415         # files they can't read.  ignore those files.
5416         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5417                         ! -perm +4 -printf '-I %f\n')
5418         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5419                 d99reposname vtag rtag
5420 }
5421 run_test 99b "cvs import ======================================="
5422
5423 test_99c() {
5424         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5425         [ ! -d $DIR/d99cvsroot ] && test_99b
5426         cd $DIR
5427         test_mkdir -p $DIR/d99reposname
5428         chown $RUNAS_ID $DIR/d99reposname
5429         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5430 }
5431 run_test 99c "cvs checkout ====================================="
5432
5433 test_99d() {
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 touch foo99
5438         $RUNAS cvs add -m 'addmsg' foo99
5439 }
5440 run_test 99d "cvs add =========================================="
5441
5442 test_99e() {
5443         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5444         [ ! -d $DIR/d99cvsroot ] && test_99c
5445         cd $DIR/d99reposname
5446         $RUNAS cvs update
5447 }
5448 run_test 99e "cvs update ======================================="
5449
5450 test_99f() {
5451         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5452         [ ! -d $DIR/d99cvsroot ] && test_99d
5453         cd $DIR/d99reposname
5454         $RUNAS cvs commit -m 'nomsg' foo99
5455     rm -fr $DIR/d99cvsroot
5456 }
5457 run_test 99f "cvs commit ======================================="
5458
5459 test_100() {
5460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5461         [ "$NETTYPE" = tcp ] || \
5462                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5463                         return ; }
5464
5465         remote_ost_nodsh && skip "remote OST with nodsh" && return
5466         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5467         remote_servers || \
5468                 { skip "useless for local single node setup" && return; }
5469
5470         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5471                 [ "$PROT" != "tcp" ] && continue
5472                 RPORT=$(echo $REMOTE | cut -d: -f2)
5473                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5474
5475                 rc=0
5476                 LPORT=`echo $LOCAL | cut -d: -f2`
5477                 if [ $LPORT -ge 1024 ]; then
5478                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5479                         netstat -tna
5480                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5481                 fi
5482         done
5483         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5484 }
5485 run_test 100 "check local port using privileged port ==========="
5486
5487 function get_named_value()
5488 {
5489     local tag
5490
5491     tag=$1
5492     while read ;do
5493         line=$REPLY
5494         case $line in
5495         $tag*)
5496             echo $line | sed "s/^$tag[ ]*//"
5497             break
5498             ;;
5499         esac
5500     done
5501 }
5502
5503 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5504                    awk '/^max_cached_mb/ { print $2 }')
5505
5506 cleanup_101a() {
5507         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5508         trap 0
5509 }
5510
5511 test_101a() {
5512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5513         local s
5514         local discard
5515         local nreads=10000
5516         [ "$CPU" = "UML" ] && nreads=1000
5517         local cache_limit=32
5518
5519         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5520         trap cleanup_101a EXIT
5521         $LCTL set_param -n llite.*.read_ahead_stats 0
5522         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5523
5524         #
5525         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5526         #
5527         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5528         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5529
5530         discard=0
5531         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5532                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5533                         discard=$(($discard + $s))
5534         done
5535         cleanup_101a
5536
5537         if [ $(($discard * 10)) -gt $nreads ] ;then
5538                 $LCTL get_param osc.*-osc*.rpc_stats
5539                 $LCTL get_param llite.*.read_ahead_stats
5540                 error "too many ($discard) discarded pages"
5541         fi
5542         rm -f $DIR/$tfile || true
5543 }
5544 run_test 101a "check read-ahead for random reads ================"
5545
5546 setup_test101bc() {
5547         test_mkdir -p $DIR/$tdir
5548         STRIPE_SIZE=1048576
5549         STRIPE_COUNT=$OSTCOUNT
5550         STRIPE_OFFSET=0
5551
5552         local list=$(comma_list $(osts_nodes))
5553         set_osd_param $list '' read_cache_enable 0
5554         set_osd_param $list '' writethrough_cache_enable 0
5555
5556         trap cleanup_test101bc EXIT
5557         # prepare the read-ahead file
5558         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5559
5560         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5561 }
5562
5563 cleanup_test101bc() {
5564         trap 0
5565         rm -rf $DIR/$tdir
5566         rm -f $DIR/$tfile
5567
5568         local list=$(comma_list $(osts_nodes))
5569         set_osd_param $list '' read_cache_enable 1
5570         set_osd_param $list '' writethrough_cache_enable 1
5571 }
5572
5573 calc_total() {
5574         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5575 }
5576
5577 ra_check_101() {
5578         local READ_SIZE=$1
5579         local STRIPE_SIZE=1048576
5580         local RA_INC=1048576
5581         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5582         local FILE_LENGTH=$((64*100))
5583         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5584                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5585         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5586                         get_named_value 'read but discarded' | \
5587                         cut -d" " -f1 | calc_total`
5588         if [ $DISCARD -gt $discard_limit ]; then
5589                 $LCTL get_param llite.*.read_ahead_stats
5590                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5591         else
5592                 echo "Read-ahead success for size ${READ_SIZE}"
5593         fi
5594 }
5595
5596 test_101b() {
5597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5598         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5599         local STRIPE_SIZE=1048576
5600         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5601         local FILE_LENGTH=$((STRIPE_SIZE*100))
5602         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5603         # prepare the read-ahead file
5604         setup_test101bc
5605         cancel_lru_locks osc
5606         for BIDX in 2 4 8 16 32 64 128 256
5607         do
5608                 local BSIZE=$((BIDX*4096))
5609                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5610                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5611                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5612                 $LCTL set_param -n llite.*.read_ahead_stats 0
5613                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5614                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5615                 cancel_lru_locks osc
5616                 ra_check_101 $BSIZE
5617         done
5618         cleanup_test101bc
5619         true
5620 }
5621 run_test 101b "check stride-io mode read-ahead ================="
5622
5623 test_101c() {
5624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5625         local STRIPE_SIZE=1048576
5626         local FILE_LENGTH=$((STRIPE_SIZE*100))
5627         local nreads=10000
5628         local osc
5629
5630     setup_test101bc
5631
5632     cancel_lru_locks osc
5633     $LCTL set_param osc.*.rpc_stats 0
5634     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5635     for osc in $($LCTL get_param -N osc.*); do
5636         if [ "$osc" == "osc.num_refs" ]; then
5637             continue
5638         fi
5639
5640         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5641         if [ $lines -le 20 ]; then
5642             continue
5643         fi
5644
5645         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5646                                      awk '$1 == "1:" { print $2; exit; }')
5647         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5648                                      awk '$1 == "2:" { print $2; exit; }')
5649         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5650                                      awk '$1 == "4:" { print $2; exit; }')
5651         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5652                                      awk '$1 == "8:" { print $2; exit; }')
5653
5654         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5655         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5656         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5657         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5658         echo "${osc} rpc check passed!"
5659     done
5660     cleanup_test101bc
5661     true
5662 }
5663 run_test 101c "check stripe_size aligned read-ahead ================="
5664
5665 set_read_ahead() {
5666    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5667    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5668 }
5669
5670 test_101d() {
5671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5672         local file=$DIR/$tfile
5673         local size=${FILESIZE_101c:-500}
5674         local ra_MB=${READAHEAD_MB:-40}
5675
5676         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5677         [ $space -gt $((size * 1024)) ] ||
5678                 { skip "Need free space ${size}M, have $space" && return; }
5679
5680     echo Creating ${size}M test file $file
5681     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5682     echo Cancel LRU locks on lustre client to flush the client cache
5683     cancel_lru_locks osc
5684
5685     echo Disable read-ahead
5686     local old_READAHEAD=$(set_read_ahead 0)
5687
5688     echo Reading the test file $file with read-ahead disabled
5689     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5690
5691     echo Cancel LRU locks on lustre client to flush the client cache
5692     cancel_lru_locks osc
5693     echo Enable read-ahead with ${ra_MB}MB
5694     set_read_ahead $ra_MB
5695
5696     echo Reading the test file $file with read-ahead enabled
5697     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5698
5699     echo read-ahead disabled time read $time_ra_OFF
5700     echo read-ahead enabled  time read $time_ra_ON
5701
5702         set_read_ahead $old_READAHEAD
5703         rm -f $file
5704         wait_delete_completed
5705
5706     [ $time_ra_ON -lt $time_ra_OFF ] ||
5707         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5708                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5709 }
5710 run_test 101d "file read with and without read-ahead enabled  ================="
5711
5712 test_101e() {
5713         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5714     local file=$DIR/$tfile
5715     local size=500  #KB
5716     local count=100
5717     local blksize=1024
5718
5719     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5720     local need_space=$((count * size))
5721     [ $space -gt $need_space ] ||
5722         { skip_env "Need free space $need_space, have $space" && return; }
5723
5724     echo Creating $count ${size}K test files
5725     for ((i = 0; i < $count; i++)); do
5726         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5727     done
5728
5729     echo Cancel LRU locks on lustre client to flush the client cache
5730     cancel_lru_locks osc
5731
5732     echo Reset readahead stats
5733     $LCTL set_param -n llite.*.read_ahead_stats 0
5734
5735     for ((i = 0; i < $count; i++)); do
5736         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5737     done
5738
5739     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5740           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5741
5742     for ((i = 0; i < $count; i++)); do
5743         rm -rf ${file}_${i} 2>/dev/null
5744     done
5745
5746     #10000 means 20% reads are missing in readahead
5747     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5748 }
5749 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5750
5751 cleanup_test101f() {
5752     trap 0
5753     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5754     rm -rf $DIR/$tfile 2>/dev/null
5755 }
5756
5757 test_101f() {
5758         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5759     local file=$DIR/$tfile
5760     local nreads=1000
5761
5762     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5763     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5764     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5765     trap cleanup_test101f EXIT
5766
5767     echo Cancel LRU locks on lustre client to flush the client cache
5768     cancel_lru_locks osc
5769
5770     echo Reset readahead stats
5771     $LCTL set_param -n llite.*.read_ahead_stats 0
5772     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5773     # readahead should read in 2M file on second read, so only miss
5774     # 2 pages.
5775     echo Random 4K reads on 2M file for 1000 times
5776     $READS -f $file -s 2097152 -b 4096 -n $nreads
5777
5778     echo checking missing pages
5779     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5780           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5781
5782     [ $miss -lt 3 ] || error "misses too much pages!"
5783     cleanup_test101f
5784 }
5785 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5786
5787 setup_test102() {
5788         test_mkdir -p $DIR/$tdir
5789         chown $RUNAS_ID $DIR/$tdir
5790         STRIPE_SIZE=65536
5791         STRIPE_OFFSET=1
5792         STRIPE_COUNT=$OSTCOUNT
5793         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5794
5795         trap cleanup_test102 EXIT
5796         cd $DIR
5797         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5798         cd $DIR/$tdir
5799         for num in 1 2 3 4; do
5800                 for count in $(seq 1 $STRIPE_COUNT); do
5801                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5802                                 local size=`expr $STRIPE_SIZE \* $num`
5803                                 local file=file"$num-$idx-$count"
5804                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5805                         done
5806                 done
5807         done
5808
5809         cd $DIR
5810         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5811 }
5812
5813 cleanup_test102() {
5814         trap 0
5815         rm -f $TMP/f102.tar
5816         rm -rf $DIR/d0.sanity/d102
5817 }
5818
5819 test_102a() {
5820         local testfile=$DIR/xattr_testfile
5821
5822         touch $testfile
5823
5824         [ "$UID" != 0 ] && skip_env "must run as root" && return
5825         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5826                 skip_env "must have user_xattr" && return
5827
5828         [ -z "$(which setfattr 2>/dev/null)" ] &&
5829                 skip_env "could not find setfattr" && return
5830
5831         echo "set/get xattr..."
5832         setfattr -n trusted.name1 -v value1 $testfile || error
5833         getfattr -n trusted.name1 $testfile 2> /dev/null |
5834           grep "trusted.name1=.value1" ||
5835                 error "$testfile missing trusted.name1=value1"
5836
5837         setfattr -n user.author1 -v author1 $testfile || error
5838         getfattr -n user.author1 $testfile 2> /dev/null |
5839           grep "user.author1=.author1" ||
5840                 error "$testfile missing trusted.author1=author1"
5841
5842         echo "listxattr..."
5843         setfattr -n trusted.name2 -v value2 $testfile ||
5844                 error "$testfile unable to set trusted.name2"
5845         setfattr -n trusted.name3 -v value3 $testfile ||
5846                 error "$testfile unable to set trusted.name3"
5847         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5848             grep "trusted.name" | wc -l) -eq 3 ] ||
5849                 error "$testfile missing 3 trusted.name xattrs"
5850
5851         setfattr -n user.author2 -v author2 $testfile ||
5852                 error "$testfile unable to set user.author2"
5853         setfattr -n user.author3 -v author3 $testfile ||
5854                 error "$testfile unable to set user.author3"
5855         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5856             grep "user.author" | wc -l) -eq 3 ] ||
5857                 error "$testfile missing 3 user.author xattrs"
5858
5859         echo "remove xattr..."
5860         setfattr -x trusted.name1 $testfile ||
5861                 error "$testfile error deleting trusted.name1"
5862         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5863                 error "$testfile did not delete trusted.name1 xattr"
5864
5865         setfattr -x user.author1 $testfile ||
5866                 error "$testfile error deleting user.author1"
5867         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5868                 error "$testfile did not delete trusted.name1 xattr"
5869
5870         # b10667: setting lustre special xattr be silently discarded
5871         echo "set lustre special xattr ..."
5872         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5873                 error "$testfile allowed setting trusted.lov"
5874 }
5875 run_test 102a "user xattr test =================================="
5876
5877 test_102b() {
5878         # b10930: get/set/list trusted.lov xattr
5879         echo "get/set/list trusted.lov xattr ..."
5880         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5881         local testfile=$DIR/$tfile
5882         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5883                 error "setstripe failed"
5884         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5885                 error "getstripe failed"
5886         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5887         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5888
5889         local testfile2=${testfile}2
5890         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5891                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5892
5893         $MCREATE $testfile2
5894         setfattr -n trusted.lov -v $value $testfile2
5895         local stripe_size=$($GETSTRIPE -S $testfile2)
5896         local stripe_count=$($GETSTRIPE -c $testfile2)
5897         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5898         [ $stripe_count -eq $STRIPECOUNT ] ||
5899                 error "stripe count $stripe_count != $STRIPECOUNT"
5900         rm -f $DIR/$tfile
5901 }
5902 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5903
5904 test_102c() {
5905         # b10930: get/set/list lustre.lov xattr
5906         echo "get/set/list lustre.lov xattr ..."
5907         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5908         test_mkdir -p $DIR/$tdir
5909         chown $RUNAS_ID $DIR/$tdir
5910         local testfile=$DIR/$tdir/$tfile
5911         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5912                 error "setstripe failed"
5913         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5914                 error "getstripe failed"
5915         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5916         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5917
5918         local testfile2=${testfile}2
5919         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5920                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5921
5922         $RUNAS $MCREATE $testfile2
5923         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5924         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5925         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5926         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5927         [ $stripe_count -eq $STRIPECOUNT ] ||
5928                 error "stripe count $stripe_count != $STRIPECOUNT"
5929 }
5930 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5931
5932 compare_stripe_info1() {
5933         local stripe_index_all_zero=true
5934
5935         for num in 1 2 3 4; do
5936                 for count in $(seq 1 $STRIPE_COUNT); do
5937                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5938                                 local size=$((STRIPE_SIZE * num))
5939                                 local file=file"$num-$offset-$count"
5940                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5941                                 [ $stripe_size -ne $size ] &&
5942                                     error "$file: size $stripe_size != $size"
5943                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5944                                 # allow fewer stripes to be created, ORI-601
5945                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5946                                     error "$file: count $stripe_count != $count"
5947                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5948                                 [ $stripe_index -ne 0 ] &&
5949                                         stripe_index_all_zero=false
5950                         done
5951                 done
5952         done
5953         $stripe_index_all_zero &&
5954                 error "all files are being extracted starting from OST index 0"
5955         return 0
5956 }
5957
5958 find_lustre_tar() {
5959         [ -n "$(which tar 2>/dev/null)" ] &&
5960                 strings $(which tar) | grep -q "lustre" && echo tar
5961 }
5962
5963 test_102d() {
5964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5965         # b10930: tar test for trusted.lov xattr
5966         TAR=$(find_lustre_tar)
5967         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5968         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5969         setup_test102
5970         test_mkdir -p $DIR/d102d
5971         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5972         cd $DIR/d102d/$tdir
5973         compare_stripe_info1
5974 }
5975 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5976
5977 test_102f() {
5978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5979         # b10930: tar test for trusted.lov xattr
5980         TAR=$(find_lustre_tar)
5981         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5982         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5983         setup_test102
5984         test_mkdir -p $DIR/d102f
5985         cd $DIR
5986         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5987         cd $DIR/d102f/$tdir
5988         compare_stripe_info1
5989 }
5990 run_test 102f "tar copy files, not keep osts ==========="
5991
5992 grow_xattr() {
5993         local xsize=${1:-1024}  # in bytes
5994         local file=$DIR/$tfile
5995
5996         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5997                 skip "must have user_xattr" && return 0
5998         [ -z "$(which setfattr 2>/dev/null)" ] &&
5999                 skip_env "could not find setfattr" && return 0
6000         [ -z "$(which getfattr 2>/dev/null)" ] &&
6001                 skip_env "could not find getfattr" && return 0
6002
6003         touch $file
6004
6005         local value="$(generate_string $xsize)"
6006
6007         local xbig=trusted.big
6008         log "save $xbig on $file"
6009         setfattr -n $xbig -v $value $file ||
6010                 error "saving $xbig on $file failed"
6011
6012         local orig=$(get_xattr_value $xbig $file)
6013         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6014
6015         local xsml=trusted.sml
6016         log "save $xsml on $file"
6017         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6018
6019         local new=$(get_xattr_value $xbig $file)
6020         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6021
6022         log "grow $xsml on $file"
6023         setfattr -n $xsml -v "$value" $file ||
6024                 error "growing $xsml on $file failed"
6025
6026         new=$(get_xattr_value $xbig $file)
6027         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6028         log "$xbig still valid after growing $xsml"
6029
6030         rm -f $file
6031 }
6032
6033 test_102h() { # bug 15777
6034         grow_xattr 1024
6035 }
6036 run_test 102h "grow xattr from inside inode to external block"
6037
6038 test_102ha() {
6039         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6040         grow_xattr $(max_xattr_size)
6041 }
6042 run_test 102ha "grow xattr from inside inode to external inode"
6043
6044 test_102i() { # bug 17038
6045         touch $DIR/$tfile
6046         ln -s $DIR/$tfile $DIR/${tfile}link
6047         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6048         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"
6049         rm -f $DIR/$tfile $DIR/${tfile}link
6050 }
6051 run_test 102i "lgetxattr test on symbolic link ============"
6052
6053 test_102j() {
6054         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6055         TAR=$(find_lustre_tar)
6056         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6057         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6058         setup_test102 "$RUNAS"
6059         test_mkdir -p $DIR/d102j
6060         chown $RUNAS_ID $DIR/d102j
6061         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6062         cd $DIR/d102j/$tdir
6063         compare_stripe_info1 "$RUNAS"
6064 }
6065 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6066
6067 test_102k() {
6068         touch $DIR/$tfile
6069         # b22187 just check that does not crash for regular file.
6070         setfattr -n trusted.lov $DIR/$tfile
6071         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6072         local test_kdir=$DIR/d102k
6073         test_mkdir $test_kdir
6074         local default_size=`$GETSTRIPE -S $test_kdir`
6075         local default_count=`$GETSTRIPE -c $test_kdir`
6076         local default_offset=`$GETSTRIPE -i $test_kdir`
6077         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
6078                 error 'dir setstripe failed'
6079         setfattr -n trusted.lov $test_kdir
6080         local stripe_size=`$GETSTRIPE -S $test_kdir`
6081         local stripe_count=`$GETSTRIPE -c $test_kdir`
6082         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6083         [ $stripe_size -eq $default_size ] ||
6084                 error "stripe size $stripe_size != $default_size"
6085         [ $stripe_count -eq $default_count ] ||
6086                 error "stripe count $stripe_count != $default_count"
6087         [ $stripe_offset -eq $default_offset ] ||
6088                 error "stripe offset $stripe_offset != $default_offset"
6089         rm -rf $DIR/$tfile $test_kdir
6090 }
6091 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6092
6093 test_102l() {
6094         # LU-532 trusted. xattr is invisible to non-root
6095         local testfile=$DIR/$tfile
6096
6097         touch $testfile
6098
6099         echo "listxattr as user..."
6100         chown $RUNAS_ID $testfile
6101         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6102             grep -q "trusted" &&
6103                 error "$testfile trusted xattrs are user visible"
6104
6105         return 0;
6106 }
6107 run_test 102l "listxattr filter test =================================="
6108
6109 cleanup_test102
6110
6111 run_acl_subtest()
6112 {
6113     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6114     return $?
6115 }
6116
6117 test_103 () {
6118     [ "$UID" != 0 ] && skip_env "must run as root" && return
6119     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6120     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
6121     $GSS && skip "could not run under gss" && return
6122
6123     declare -a identity_old
6124
6125         for num in $(seq $MDSCOUNT); do
6126                 switch_identity $num true || identity_old[$num]=$?
6127         done
6128
6129     SAVE_UMASK=`umask`
6130     umask 0022
6131     cd $DIR
6132
6133     echo "performing cp ..."
6134     run_acl_subtest cp || error
6135     echo "performing getfacl-noacl..."
6136     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6137     echo "performing misc..."
6138     run_acl_subtest misc || error  "misc test failed"
6139     echo "performing permissions..."
6140     run_acl_subtest permissions || error "permissions failed"
6141     echo "performing setfacl..."
6142     run_acl_subtest setfacl || error  "setfacl test failed"
6143
6144     # inheritance test got from HP
6145     echo "performing inheritance..."
6146     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6147     chmod +x make-tree || error "chmod +x failed"
6148     run_acl_subtest inheritance || error "inheritance test failed"
6149     rm -f make-tree
6150
6151     echo "LU-974 ignore umask when acl is enabled..."
6152     run_acl_subtest 974 || error "LU-974 test failed"
6153
6154     echo "LU-2561 newly created file is same size as directory..."
6155     run_acl_subtest 2561 || error "LU-2561 test failed"
6156
6157     cd $SAVE_PWD
6158     umask $SAVE_UMASK
6159
6160         for num in $(seq $MDSCOUNT); do
6161                 if [ "${identity_old[$num]}" = 1 ]; then
6162                         switch_identity $num false || identity_old[$num]=$?
6163                 fi
6164         done
6165 }
6166 run_test 103 "acl test ========================================="
6167
6168 test_104a() {
6169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6170         touch $DIR/$tfile
6171         lfs df || error "lfs df failed"
6172         lfs df -ih || error "lfs df -ih failed"
6173         lfs df -h $DIR || error "lfs df -h $DIR failed"
6174         lfs df -i $DIR || error "lfs df -i $DIR failed"
6175         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6176         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6177
6178         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6179         lctl --device %$OSC deactivate
6180         lfs df || error "lfs df with deactivated OSC failed"
6181         lctl --device %$OSC activate
6182         # wait the osc back to normal
6183         wait_osc_import_state client ost FULL
6184
6185         lfs df || error "lfs df with reactivated OSC failed"
6186         rm -f $DIR/$tfile
6187 }
6188 run_test 104a "lfs df [-ih] [path] test ========================="
6189
6190 test_104b() {
6191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6192         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6193         chmod 666 /dev/obd
6194         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6195         if [ $denied_cnt -ne 0 ];
6196         then
6197                     error "lfs check servers test failed"
6198         fi
6199 }
6200 run_test 104b "$RUNAS lfs check servers test ===================="
6201
6202 test_105a() {
6203         # doesn't work on 2.4 kernels
6204         touch $DIR/$tfile
6205         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6206                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6207         else
6208                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6209         fi
6210         rm -f $DIR/$tfile
6211 }
6212 run_test 105a "flock when mounted without -o flock test ========"
6213
6214 test_105b() {
6215         touch $DIR/$tfile
6216         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6217                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6218         else
6219                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6220         fi
6221         rm -f $DIR/$tfile
6222 }
6223 run_test 105b "fcntl when mounted without -o flock test ========"
6224
6225 test_105c() {
6226         touch $DIR/$tfile
6227         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6228                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6229         else
6230                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6231         fi
6232         rm -f $DIR/$tfile
6233 }
6234 run_test 105c "lockf when mounted without -o flock test ========"
6235
6236 test_105d() { # bug 15924
6237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6238         test_mkdir -p $DIR/$tdir
6239         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6240                 skip "mount w/o flock enabled" && return
6241         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6242         $LCTL set_param fail_loc=0x80000315
6243         flocks_test 2 $DIR/$tdir
6244 }
6245 run_test 105d "flock race (should not freeze) ========"
6246
6247 test_105e() { # bug 22660 && 22040
6248         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6249                 skip "mount w/o flock enabled" && return
6250         touch $DIR/$tfile
6251         flocks_test 3 $DIR/$tfile
6252 }
6253 run_test 105e "Two conflicting flocks from same process ======="
6254
6255 test_106() { #bug 10921
6256         test_mkdir -p $DIR/$tdir
6257         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6258         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6259 }
6260 run_test 106 "attempt exec of dir followed by chown of that dir"
6261
6262 test_107() {
6263         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6264         CDIR=`pwd`
6265         cd $DIR
6266
6267         local file=core
6268         rm -f $file
6269
6270         local save_pattern=$(sysctl -n kernel.core_pattern)
6271         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6272         sysctl -w kernel.core_pattern=$file
6273         sysctl -w kernel.core_uses_pid=0
6274
6275         ulimit -c unlimited
6276         sleep 60 &
6277         SLEEPPID=$!
6278
6279         sleep 1
6280
6281         kill -s 11 $SLEEPPID
6282         wait $SLEEPPID
6283         if [ -e $file ]; then
6284                 size=`stat -c%s $file`
6285                 [ $size -eq 0 ] && error "Fail to create core file $file"
6286         else
6287                 error "Fail to create core file $file"
6288         fi
6289         rm -f $file
6290         sysctl -w kernel.core_pattern=$save_pattern
6291         sysctl -w kernel.core_uses_pid=$save_uses_pid
6292         cd $CDIR
6293 }
6294 run_test 107 "Coredump on SIG"
6295
6296 test_110() {
6297         test_mkdir -p $DIR/$tdir
6298         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6299                 error "mkdir with 255 char failed"
6300         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6301                 error "mkdir with 256 char should fail, but did not"
6302         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6303                 error "create with 255 char failed"
6304         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6305                 error "create with 256 char should fail, but did not"
6306
6307         ls -l $DIR/$tdir
6308         rm -rf $DIR/$tdir
6309 }
6310 run_test 110 "filename length checking"
6311
6312 test_115() {
6313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6314         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6315             cut -c11-20)
6316         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6317             return
6318         echo "Starting with $OSTIO_pre threads"
6319
6320         NUMTEST=20000
6321         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6322         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6323         echo "$NUMTEST creates/unlinks"
6324         test_mkdir -p $DIR/$tdir
6325         createmany -o $DIR/$tdir/$tfile $NUMTEST
6326         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6327
6328         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6329             cut -c11-20)
6330
6331         # don't return an error
6332         [ $OSTIO_post == $OSTIO_pre ] && echo \
6333             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6334             echo "This may be fine, depending on what ran before this test" &&
6335             echo "and how fast this system is." && return
6336
6337         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6338 }
6339 run_test 115 "verify dynamic thread creation===================="
6340
6341 free_min_max () {
6342         wait_delete_completed
6343         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6344         echo OST kbytes available: ${AVAIL[@]}
6345         MAXI=0; MAXV=${AVAIL[0]}
6346         MINI=0; MINV=${AVAIL[0]}
6347         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6348             #echo OST $i: ${AVAIL[i]}kb
6349             if [ ${AVAIL[i]} -gt $MAXV ]; then
6350                 MAXV=${AVAIL[i]}; MAXI=$i
6351             fi
6352             if [ ${AVAIL[i]} -lt $MINV ]; then
6353                 MINV=${AVAIL[i]}; MINI=$i
6354             fi
6355         done
6356         echo Min free space: OST $MINI: $MINV
6357         echo Max free space: OST $MAXI: $MAXV
6358 }
6359
6360 test_116a() { # was previously test_116()
6361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6362         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6363
6364         echo -n "Free space priority "
6365         lctl get_param -n lov.*-clilov-*.qos_prio_free
6366         declare -a AVAIL
6367         free_min_max
6368         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6369                 return
6370
6371         # generate uneven OSTs
6372         test_mkdir -p $DIR/$tdir/OST${MINI}
6373         declare -i FILL
6374         FILL=$(($MINV / 4))
6375         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6376         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6377         i=0
6378         while [ $FILL -gt 0 ]; do
6379             i=$(($i + 1))
6380             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6381             FILL=$(($FILL - 2048))
6382             echo -n .
6383         done
6384         FILL=$(($MINV / 4))
6385         sync
6386         sleep_maxage
6387
6388         free_min_max
6389         DIFF=$(($MAXV - $MINV))
6390         DIFF2=$(($DIFF * 100 / $MINV))
6391         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6392         if [ $DIFF2 -gt 20 ]; then
6393             echo "ok"
6394         else
6395             echo "failed - QOS mode won't be used"
6396             error_ignore "QOS imbalance criteria not met"
6397             return
6398         fi
6399
6400         MINI1=$MINI; MINV1=$MINV
6401         MAXI1=$MAXI; MAXV1=$MAXV
6402
6403         # now fill using QOS
6404         echo writing a bunch of files to QOS-assigned OSTs
6405         $SETSTRIPE -c 1 $DIR/$tdir
6406         i=0
6407         while [ $FILL -gt 0 ]; do
6408             i=$(($i + 1))
6409             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6410             FILL=$(($FILL - 200))
6411             echo -n .
6412         done
6413         echo "wrote $i 200k files"
6414         sync
6415         sleep_maxage
6416
6417         echo "Note: free space may not be updated, so measurements might be off"
6418         free_min_max
6419         DIFF2=$(($MAXV - $MINV))
6420         echo "free space delta: orig $DIFF final $DIFF2"
6421         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6422         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6423         echo "Wrote $DIFF to smaller OST $MINI1"
6424         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6425         echo "Wrote $DIFF2 to larger OST $MAXI1"
6426         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6427
6428         # Figure out which files were written where
6429         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6430                awk '/'$MINI1': / {print $2; exit}')
6431         echo $UUID
6432         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6433         echo "$MINC files created on smaller OST $MINI1"
6434         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6435                awk '/'$MAXI1': / {print $2; exit}')
6436         echo $UUID
6437         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6438         echo "$MAXC files created on larger OST $MAXI1"
6439         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6440         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6441
6442         rm -rf $DIR/$tdir
6443 }
6444 run_test 116a "stripe QOS: free space balance ==================="
6445
6446 test_116b() { # LU-2093
6447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6448 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6449         local old_rr
6450         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6451         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6452         mkdir -p $DIR/$tdir
6453         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6454         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6455         do_facet $SINGLEMDS lctl set_param fail_loc=0
6456         rm -rf $DIR/$tdir
6457         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6458 }
6459 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6460
6461 test_117() # bug 10891
6462 {
6463         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6464         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6465         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6466         lctl set_param fail_loc=0x21e
6467         > $DIR/$tfile || error "truncate failed"
6468         lctl set_param fail_loc=0
6469         echo "Truncate succeeded."
6470         rm -f $DIR/$tfile
6471 }
6472 run_test 117 "verify fsfilt_extend =========="
6473
6474 export OLD_RESENDCOUNT=""
6475 set_resend_count () {
6476         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6477         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6478         lctl set_param -n $PROC_RESENDCOUNT $1
6479         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6480 }
6481
6482 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6483
6484 # Reset async IO behavior after error case
6485 reset_async() {
6486         FILE=$DIR/reset_async
6487
6488         # Ensure all OSCs are cleared
6489         $SETSTRIPE -c -1 $FILE
6490         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6491         sync
6492         rm $FILE
6493 }
6494
6495 test_118a() #bug 11710
6496 {
6497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6498         reset_async
6499
6500         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6501         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6502         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6503
6504         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6505                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6506                 return 1;
6507         fi
6508         rm -f $DIR/$tfile
6509 }
6510 run_test 118a "verify O_SYNC works =========="
6511
6512 test_118b()
6513 {
6514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6515         remote_ost_nodsh && skip "remote OST with nodsh" && return
6516
6517         reset_async
6518
6519         #define OBD_FAIL_OST_ENOENT 0x217
6520         set_nodes_failloc "$(osts_nodes)" 0x217
6521         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6522         RC=$?
6523         set_nodes_failloc "$(osts_nodes)" 0
6524         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6525         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6526                     grep -c writeback)
6527
6528         if [[ $RC -eq 0 ]]; then
6529                 error "Must return error due to dropped pages, rc=$RC"
6530                 return 1;
6531         fi
6532
6533         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6534                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6535                 return 1;
6536         fi
6537
6538         echo "Dirty pages not leaked on ENOENT"
6539
6540         # Due to the above error the OSC will issue all RPCs syncronously
6541         # until a subsequent RPC completes successfully without error.
6542         $MULTIOP $DIR/$tfile Ow4096yc
6543         rm -f $DIR/$tfile
6544
6545         return 0
6546 }
6547 run_test 118b "Reclaim dirty pages on fatal error =========="
6548
6549 test_118c()
6550 {
6551         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6552         remote_ost_nodsh && skip "remote OST with nodsh" && return
6553
6554         reset_async
6555
6556         #define OBD_FAIL_OST_EROFS               0x216
6557         set_nodes_failloc "$(osts_nodes)" 0x216
6558
6559         # multiop should block due to fsync until pages are written
6560         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6561         MULTIPID=$!
6562         sleep 1
6563
6564         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6565                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6566         fi
6567
6568         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6569                     grep -c writeback)
6570         if [[ $WRITEBACK -eq 0 ]]; then
6571                 error "No page in writeback, writeback=$WRITEBACK"
6572         fi
6573
6574         set_nodes_failloc "$(osts_nodes)" 0
6575         wait $MULTIPID
6576         RC=$?
6577         if [[ $RC -ne 0 ]]; then
6578                 error "Multiop fsync failed, rc=$RC"
6579         fi
6580
6581         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6582         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6583                     grep -c writeback)
6584         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6585                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6586         fi
6587
6588         rm -f $DIR/$tfile
6589         echo "Dirty pages flushed via fsync on EROFS"
6590         return 0
6591 }
6592 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6593
6594 test_118d()
6595 {
6596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6597         remote_ost_nodsh && skip "remote OST with nodsh" && return
6598
6599         reset_async
6600
6601         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6602         set_nodes_failloc "$(osts_nodes)" 0x214
6603         # multiop should block due to fsync until pages are written
6604         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6605         MULTIPID=$!
6606         sleep 1
6607
6608         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6609                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6610         fi
6611
6612         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6613                     grep -c writeback)
6614         if [[ $WRITEBACK -eq 0 ]]; then
6615                 error "No page in writeback, writeback=$WRITEBACK"
6616         fi
6617
6618         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6619         set_nodes_failloc "$(osts_nodes)" 0
6620
6621         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6622         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6623                     grep -c writeback)
6624         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6625                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6626         fi
6627
6628         rm -f $DIR/$tfile
6629         echo "Dirty pages gaurenteed flushed via fsync"
6630         return 0
6631 }
6632 run_test 118d "Fsync validation inject a delay of the bulk =========="
6633
6634 test_118f() {
6635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6636         reset_async
6637
6638         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6639         lctl set_param fail_loc=0x8000040a
6640
6641         # Should simulate EINVAL error which is fatal
6642         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6643         RC=$?
6644         if [[ $RC -eq 0 ]]; then
6645                 error "Must return error due to dropped pages, rc=$RC"
6646         fi
6647
6648         lctl set_param fail_loc=0x0
6649
6650         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6651         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6652         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6653                     grep -c writeback)
6654         if [[ $LOCKED -ne 0 ]]; then
6655                 error "Locked pages remain in cache, locked=$LOCKED"
6656         fi
6657
6658         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6659                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6660         fi
6661
6662         rm -f $DIR/$tfile
6663         echo "No pages locked after fsync"
6664
6665         reset_async
6666         return 0
6667 }
6668 run_test 118f "Simulate unrecoverable OSC side error =========="
6669
6670 test_118g() {
6671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6672         reset_async
6673
6674         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6675         lctl set_param fail_loc=0x406
6676
6677         # simulate local -ENOMEM
6678         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6679         RC=$?
6680
6681         lctl set_param fail_loc=0
6682         if [[ $RC -eq 0 ]]; then
6683                 error "Must return error due to dropped pages, rc=$RC"
6684         fi
6685
6686         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6687         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6688         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6689                         grep -c writeback)
6690         if [[ $LOCKED -ne 0 ]]; then
6691                 error "Locked pages remain in cache, locked=$LOCKED"
6692         fi
6693
6694         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6695                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6696         fi
6697
6698         rm -f $DIR/$tfile
6699         echo "No pages locked after fsync"
6700
6701         reset_async
6702         return 0
6703 }
6704 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6705
6706 test_118h() {
6707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6708         remote_ost_nodsh && skip "remote OST with nodsh" && return
6709
6710         reset_async
6711
6712         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6713         set_nodes_failloc "$(osts_nodes)" 0x20e
6714         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6715         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6716         RC=$?
6717
6718         set_nodes_failloc "$(osts_nodes)" 0
6719         if [[ $RC -eq 0 ]]; then
6720                 error "Must return error due to dropped pages, rc=$RC"
6721         fi
6722
6723         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6724         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6725         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6726                     grep -c writeback)
6727         if [[ $LOCKED -ne 0 ]]; then
6728                 error "Locked pages remain in cache, locked=$LOCKED"
6729         fi
6730
6731         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6732                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6733         fi
6734
6735         rm -f $DIR/$tfile
6736         echo "No pages locked after fsync"
6737
6738         return 0
6739 }
6740 run_test 118h "Verify timeout in handling recoverables errors  =========="
6741
6742 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6743
6744 test_118i() {
6745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6746         remote_ost_nodsh && skip "remote OST with nodsh" && return
6747
6748         reset_async
6749
6750         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6751         set_nodes_failloc "$(osts_nodes)" 0x20e
6752
6753         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6754         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6755         PID=$!
6756         sleep 5
6757         set_nodes_failloc "$(osts_nodes)" 0
6758
6759         wait $PID
6760         RC=$?
6761         if [[ $RC -ne 0 ]]; then
6762                 error "got error, but should be not, rc=$RC"
6763         fi
6764
6765         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6766         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6767         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6768         if [[ $LOCKED -ne 0 ]]; then
6769                 error "Locked pages remain in cache, locked=$LOCKED"
6770         fi
6771
6772         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6773                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6774         fi
6775
6776         rm -f $DIR/$tfile
6777         echo "No pages locked after fsync"
6778
6779         return 0
6780 }
6781 run_test 118i "Fix error before timeout in recoverable error  =========="
6782
6783 [ "$SLOW" = "no" ] && set_resend_count 4
6784
6785 test_118j() {
6786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6787         remote_ost_nodsh && skip "remote OST with nodsh" && return
6788
6789         reset_async
6790
6791         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6792         set_nodes_failloc "$(osts_nodes)" 0x220
6793
6794         # return -EIO from OST
6795         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6796         RC=$?
6797         set_nodes_failloc "$(osts_nodes)" 0x0
6798         if [[ $RC -eq 0 ]]; then
6799                 error "Must return error due to dropped pages, rc=$RC"
6800         fi
6801
6802         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6803         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6804         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6805         if [[ $LOCKED -ne 0 ]]; then
6806                 error "Locked pages remain in cache, locked=$LOCKED"
6807         fi
6808
6809         # in recoverable error on OST we want resend and stay until it finished
6810         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6811                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6812         fi
6813
6814         rm -f $DIR/$tfile
6815         echo "No pages locked after fsync"
6816
6817         return 0
6818 }
6819 run_test 118j "Simulate unrecoverable OST side error =========="
6820
6821 test_118k()
6822 {
6823         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6824         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6825
6826         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6827         set_nodes_failloc "$(osts_nodes)" 0x20e
6828         test_mkdir -p $DIR/$tdir
6829
6830         for ((i=0;i<10;i++)); do
6831                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6832                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6833                 SLEEPPID=$!
6834                 sleep 0.500s
6835                 kill $SLEEPPID
6836                 wait $SLEEPPID
6837         done
6838
6839         set_nodes_failloc "$(osts_nodes)" 0
6840         rm -rf $DIR/$tdir
6841 }
6842 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6843
6844 test_118l()
6845 {
6846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6847         # LU-646
6848         test_mkdir -p $DIR/$tdir
6849         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6850         rm -rf $DIR/$tdir
6851 }
6852 run_test 118l "fsync dir ========="
6853
6854 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6855
6856 test_119a() # bug 11737
6857 {
6858         BSIZE=$((512 * 1024))
6859         directio write $DIR/$tfile 0 1 $BSIZE
6860         # We ask to read two blocks, which is more than a file size.
6861         # directio will indicate an error when requested and actual
6862         # sizes aren't equeal (a normal situation in this case) and
6863         # print actual read amount.
6864         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6865         if [ "$NOB" != "$BSIZE" ]; then
6866                 error "read $NOB bytes instead of $BSIZE"
6867         fi
6868         rm -f $DIR/$tfile
6869 }
6870 run_test 119a "Short directIO read must return actual read amount"
6871
6872 test_119b() # bug 11737
6873 {
6874         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6875
6876         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6877         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6878         sync
6879         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6880                 error "direct read failed"
6881         rm -f $DIR/$tfile
6882 }
6883 run_test 119b "Sparse directIO read must return actual read amount"
6884
6885 test_119c() # bug 13099
6886 {
6887         BSIZE=1048576
6888         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6889         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6890         rm -f $DIR/$tfile
6891 }
6892 run_test 119c "Testing for direct read hitting hole"
6893
6894 test_119d() # bug 15950
6895 {
6896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6897         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6898         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6899         BSIZE=1048576
6900         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6901         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6902         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6903         lctl set_param fail_loc=0x40d
6904         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6905         pid_dio=$!
6906         sleep 1
6907         cat $DIR/$tfile > /dev/null &
6908         lctl set_param fail_loc=0
6909         pid_reads=$!
6910         wait $pid_dio
6911         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6912         sleep 2
6913         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6914         error "the read rpcs have not completed in 2s"
6915         rm -f $DIR/$tfile
6916         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6917 }
6918 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6919
6920 test_120a() {
6921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6922         test_mkdir -p $DIR/$tdir
6923         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6924                skip "no early lock cancel on server" && return 0
6925         lru_resize_disable mdc
6926         lru_resize_disable osc
6927         cancel_lru_locks mdc
6928         stat $DIR/$tdir > /dev/null
6929         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6930         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6931         test_mkdir $DIR/$tdir/d1
6932         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6933         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6934         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6935         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6936         lru_resize_enable mdc
6937         lru_resize_enable osc
6938 }
6939 run_test 120a "Early Lock Cancel: mkdir test"
6940
6941 test_120b() {
6942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6943         test_mkdir -p $DIR/$tdir
6944         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6945                skip "no early lock cancel on server" && return 0
6946         lru_resize_disable mdc
6947         lru_resize_disable osc
6948         cancel_lru_locks mdc
6949         stat $DIR/$tdir > /dev/null
6950         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6951         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6952         touch $DIR/$tdir/f1
6953         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6954         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6955         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6956         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6957         lru_resize_enable mdc
6958         lru_resize_enable osc
6959 }
6960 run_test 120b "Early Lock Cancel: create test"
6961
6962 test_120c() {
6963         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6964         test_mkdir -p $DIR/$tdir
6965         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6966                skip "no early lock cancel on server" && return 0
6967         lru_resize_disable mdc
6968         lru_resize_disable osc
6969         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6970         touch $DIR/$tdir/d1/f1
6971         cancel_lru_locks mdc
6972         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6973         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6974         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6975         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6976         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6977         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6978         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6979         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6980         lru_resize_enable mdc
6981         lru_resize_enable osc
6982 }
6983 run_test 120c "Early Lock Cancel: link test"
6984
6985 test_120d() {
6986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6987         test_mkdir -p $DIR/$tdir
6988         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6989                skip "no early lock cancel on server" && return 0
6990         lru_resize_disable mdc
6991         lru_resize_disable osc
6992         touch $DIR/$tdir
6993         cancel_lru_locks mdc
6994         stat $DIR/$tdir > /dev/null
6995         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6996         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6997         chmod a+x $DIR/$tdir
6998         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6999         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7000         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7001         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7002         lru_resize_enable mdc
7003         lru_resize_enable osc
7004 }
7005 run_test 120d "Early Lock Cancel: setattr test"
7006
7007 test_120e() {
7008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7009         test_mkdir -p $DIR/$tdir
7010         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7011                skip "no early lock cancel on server" && return 0
7012         lru_resize_disable mdc
7013         lru_resize_disable osc
7014         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7015         cancel_lru_locks mdc
7016         cancel_lru_locks osc
7017         dd if=$DIR/$tdir/f1 of=/dev/null
7018         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7019         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7020               awk '/ldlm_cancel/ {print $2}'`
7021         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7022               awk '/ldlm_bl_callback/ {print $2}'`
7023         unlink $DIR/$tdir/f1
7024         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7025               awk '/ldlm_cancel/ {print $2}'`
7026         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7027               awk '/ldlm_bl_callback/ {print $2}'`
7028         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7029         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7030         lru_resize_enable mdc
7031         lru_resize_enable osc
7032 }
7033 run_test 120e "Early Lock Cancel: unlink test"
7034
7035 test_120f() {
7036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7037         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7038                skip "no early lock cancel on server" && return 0
7039         test_mkdir -p $DIR/$tdir
7040         lru_resize_disable mdc
7041         lru_resize_disable osc
7042         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7043         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7044         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7045         cancel_lru_locks mdc
7046         cancel_lru_locks osc
7047         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7048         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7049         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7050         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7051               awk '/ldlm_cancel/ {print $2}'`
7052         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7053               awk '/ldlm_bl_callback/ {print $2}'`
7054         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7055         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7056               awk '/ldlm_cancel/ {print $2}'`
7057         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7058               awk '/ldlm_bl_callback/ {print $2}'`
7059         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7060         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7061         lru_resize_enable mdc
7062         lru_resize_enable osc
7063 }
7064 run_test 120f "Early Lock Cancel: rename test"
7065
7066 test_120g() {
7067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7068         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7069                skip "no early lock cancel on server" && return 0
7070         lru_resize_disable mdc
7071         lru_resize_disable osc
7072         count=10000
7073         echo create $count files
7074         test_mkdir -p $DIR/$tdir
7075         cancel_lru_locks mdc
7076         cancel_lru_locks osc
7077         t0=`date +%s`
7078
7079         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7080               awk '/ldlm_cancel/ {print $2}'`
7081         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7082               awk '/ldlm_bl_callback/ {print $2}'`
7083         createmany -o $DIR/$tdir/f $count
7084         sync
7085         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7086               awk '/ldlm_cancel/ {print $2}'`
7087         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7088               awk '/ldlm_bl_callback/ {print $2}'`
7089         t1=`date +%s`
7090         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7091         echo rm $count files
7092         rm -r $DIR/$tdir
7093         sync
7094         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7095               awk '/ldlm_cancel/ {print $2}'`
7096         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7097               awk '/ldlm_bl_callback/ {print $2}'`
7098         t2=`date +%s`
7099         echo total: $count removes in $((t2-t1))
7100         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7101         sleep 2
7102         # wait for commitment of removal
7103         lru_resize_enable mdc
7104         lru_resize_enable osc
7105 }
7106 run_test 120g "Early Lock Cancel: performance test"
7107
7108 test_121() { #bug #10589
7109         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7110         rm -rf $DIR/$tfile
7111         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7112 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7113         lctl set_param fail_loc=0x310
7114         cancel_lru_locks osc > /dev/null
7115         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7116         lctl set_param fail_loc=0
7117         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7118 }
7119 run_test 121 "read cancel race ========="
7120
7121 test_123a() { # was test 123, statahead(bug 11401)
7122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7123         SLOWOK=0
7124         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7125             log "testing on UP system. Performance may be not as good as expected."
7126                         SLOWOK=1
7127         fi
7128
7129         rm -rf $DIR/$tdir
7130         test_mkdir -p $DIR/$tdir
7131         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7132         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7133         MULT=10
7134         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7135                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7136
7137                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7138                 lctl set_param -n llite.*.statahead_max 0
7139                 lctl get_param llite.*.statahead_max
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=$((etime - stime))
7146                 log "ls $i files without statahead: $delta sec"
7147                 lctl set_param llite.*.statahead_max=$max
7148
7149                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7150                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7151                 cancel_lru_locks mdc
7152                 cancel_lru_locks osc
7153                 stime=`date +%s`
7154                 time ls -l $DIR/$tdir | wc -l
7155                 etime=`date +%s`
7156                 delta_sa=$((etime - stime))
7157                 log "ls $i files with statahead: $delta_sa sec"
7158                 lctl get_param -n llite.*.statahead_stats
7159                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7160
7161                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7162                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7163
7164                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7165                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7166                     lctl set_param -n llite.*.statahead_max 0
7167                     lctl get_param llite.*.statahead_max
7168                     cancel_lru_locks mdc
7169                     cancel_lru_locks osc
7170                     stime=`date +%s`
7171                     time ls -l $DIR/$tdir | wc -l
7172                     etime=`date +%s`
7173                     delta=$((etime - stime))
7174                     log "ls $i files again without statahead: $delta sec"
7175                     lctl set_param llite.*.statahead_max=$max
7176                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7177                         if [  $SLOWOK -eq 0 ]; then
7178                                 error "ls $i files is slower with statahead!"
7179                         else
7180                                 log "ls $i files is slower with statahead!"
7181                         fi
7182                         break
7183                     fi
7184                 fi
7185
7186                 [ $delta -gt 20 ] && break
7187                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7188                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7189         done
7190         log "ls done"
7191
7192         stime=`date +%s`
7193         rm -r $DIR/$tdir
7194         sync
7195         etime=`date +%s`
7196         delta=$((etime - stime))
7197         log "rm -r $DIR/$tdir/: $delta seconds"
7198         log "rm done"
7199         lctl get_param -n llite.*.statahead_stats
7200 }
7201 run_test 123a "verify statahead work"
7202
7203 test_123b () { # statahead(bug 15027)
7204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7205         test_mkdir -p $DIR/$tdir
7206         createmany -o $DIR/$tdir/$tfile-%d 1000
7207
7208         cancel_lru_locks mdc
7209         cancel_lru_locks osc
7210
7211 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7212         lctl set_param fail_loc=0x80000803
7213         ls -lR $DIR/$tdir > /dev/null
7214         log "ls done"
7215         lctl set_param fail_loc=0x0
7216         lctl get_param -n llite.*.statahead_stats
7217         rm -r $DIR/$tdir
7218         sync
7219
7220 }
7221 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7222
7223 test_124a() {
7224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7225         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7226                skip "no lru resize on server" && return 0
7227         local NR=2000
7228         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7229
7230         log "create $NR files at $DIR/$tdir"
7231         createmany -o $DIR/$tdir/f $NR ||
7232                 error "failed to create $NR files in $DIR/$tdir"
7233
7234         cancel_lru_locks mdc
7235         ls -l $DIR/$tdir > /dev/null
7236
7237         local NSDIR=""
7238         local LRU_SIZE=0
7239         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7240                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7241                 LRU_SIZE=$(lctl get_param -n $PARAM)
7242                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7243                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7244                                                 log "NSDIR=$NSDIR"
7245                         log "NS=$(basename $NSDIR)"
7246                         break
7247                 fi
7248         done
7249
7250         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7251                 skip "Not enough cached locks created!"
7252                 return 0
7253         fi
7254         log "LRU=$LRU_SIZE"
7255
7256         local SLEEP=30
7257
7258         # We know that lru resize allows one client to hold $LIMIT locks
7259         # for 10h. After that locks begin to be killed by client.
7260         local MAX_HRS=10
7261         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7262                 log "LIMIT=$LIMIT"
7263
7264         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7265         # killing locks. Some time was spent for creating locks. This means
7266         # that up to the moment of sleep finish we must have killed some of
7267         # them (10-100 locks). This depends on how fast ther were created.
7268         # Many of them were touched in almost the same moment and thus will
7269         # be killed in groups.
7270         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7271
7272         # Use $LRU_SIZE_B here to take into account real number of locks
7273         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7274         local LRU_SIZE_B=$LRU_SIZE
7275         log "LVF=$LVF"
7276         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7277                 log "OLD_LVF=$OLD_LVF"
7278         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7279
7280         # Let's make sure that we really have some margin. Client checks
7281         # cached locks every 10 sec.
7282         SLEEP=$((SLEEP+20))
7283         log "Sleep ${SLEEP} sec"
7284         local SEC=0
7285         while ((SEC<$SLEEP)); do
7286                 echo -n "..."
7287                 sleep 5
7288                 SEC=$((SEC+5))
7289                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7290                 echo -n "$LRU_SIZE"
7291         done
7292         echo ""
7293         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7294         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7295
7296         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7297                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7298                 unlinkmany $DIR/$tdir/f $NR
7299                 return
7300         }
7301
7302         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7303         log "unlink $NR files at $DIR/$tdir"
7304         unlinkmany $DIR/$tdir/f $NR
7305 }
7306 run_test 124a "lru resize ======================================="
7307
7308 get_max_pool_limit()
7309 {
7310         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7311         local max=0
7312         for l in $limit; do
7313                 if test $l -gt $max; then
7314                         max=$l
7315                 fi
7316         done
7317         echo $max
7318 }
7319
7320 test_124b() {
7321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7322         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7323                skip "no lru resize on server" && return 0
7324
7325         LIMIT=`get_max_pool_limit`
7326
7327         NR=$(($(default_lru_size)*20))
7328         if [ $NR -gt $LIMIT ]; then
7329                 log "Limit lock number by $LIMIT locks"
7330                 NR=$LIMIT
7331         fi
7332         lru_resize_disable mdc
7333         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7334                 error "failed to create $DIR/$tdir/disable_lru_resize"
7335
7336         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7337         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7338         cancel_lru_locks mdc
7339         stime=`date +%s`
7340         PID=""
7341         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7342         PID="$PID $!"
7343         sleep 2
7344         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7345         PID="$PID $!"
7346         sleep 2
7347         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7348         PID="$PID $!"
7349         wait $PID
7350         etime=`date +%s`
7351         nolruresize_delta=$((etime-stime))
7352         log "ls -la time: $nolruresize_delta seconds"
7353         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7354         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7355
7356         lru_resize_enable mdc
7357         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7358                 error "failed to create $DIR/$tdir/enable_lru_resize"
7359
7360         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7361         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7362         cancel_lru_locks mdc
7363         stime=`date +%s`
7364         PID=""
7365         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7366         PID="$PID $!"
7367         sleep 2
7368         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7369         PID="$PID $!"
7370         sleep 2
7371         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7372         PID="$PID $!"
7373         wait $PID
7374         etime=`date +%s`
7375         lruresize_delta=$((etime-stime))
7376         log "ls -la time: $lruresize_delta seconds"
7377         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7378
7379         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7380                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7381         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7382                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7383         else
7384                 log "lru resize performs the same with no lru resize"
7385         fi
7386         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7387 }
7388 run_test 124b "lru resize (performance test) ======================="
7389
7390 test_125() { # 13358
7391         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7392         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7393         test_mkdir -p $DIR/d125 || error "mkdir failed"
7394         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7395         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7396         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7397 }
7398 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7399
7400 test_126() { # bug 12829/13455
7401         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7402         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7403         $GSS && skip "must run as gss disabled" && return
7404
7405         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7406         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7407         rm -f $DIR/$tfile
7408         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7409 }
7410 run_test 126 "check that the fsgid provided by the client is taken into account"
7411
7412 test_127a() { # bug 15521
7413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7414         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7415         $LCTL set_param osc.*.stats=0
7416         FSIZE=$((2048 * 1024))
7417         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7418         cancel_lru_locks osc
7419         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7420
7421         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7422         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7423                 echo "got $COUNT $NAME"
7424                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7425                 eval $NAME=$COUNT || error "Wrong proc format"
7426
7427                 case $NAME in
7428                         read_bytes|write_bytes)
7429                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7430                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7431                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7432                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7433                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7434                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7435                                 error "sumsquare is too small: $SUMSQ"
7436                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7437                                 error "sumsquare is too big: $SUMSQ"
7438                         ;;
7439                         *) ;;
7440                 esac
7441         done < $DIR/${tfile}.tmp
7442
7443         #check that we actually got some stats
7444         [ "$read_bytes" ] || error "Missing read_bytes stats"
7445         [ "$write_bytes" ] || error "Missing write_bytes stats"
7446         [ "$read_bytes" != 0 ] || error "no read done"
7447         [ "$write_bytes" != 0 ] || error "no write done"
7448 }
7449 run_test 127a "verify the client stats are sane"
7450
7451 test_127b() { # bug LU-333
7452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7453         $LCTL set_param llite.*.stats=0
7454         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7455         # perform 2 reads and writes so MAX is different from SUM.
7456         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7457         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7458         cancel_lru_locks osc
7459         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7460         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7461
7462         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7463         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7464                 echo "got $COUNT $NAME"
7465                 eval $NAME=$COUNT || error "Wrong proc format"
7466
7467         case $NAME in
7468                 read_bytes)
7469                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7470                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7471                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7472                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7473                         ;;
7474                 write_bytes)
7475                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7476                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7477                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7478                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7479                         ;;
7480                         *) ;;
7481                 esac
7482         done < $TMP/${tfile}.tmp
7483
7484         #check that we actually got some stats
7485         [ "$read_bytes" ] || error "Missing read_bytes stats"
7486         [ "$write_bytes" ] || error "Missing write_bytes stats"
7487         [ "$read_bytes" != 0 ] || error "no read done"
7488         [ "$write_bytes" != 0 ] || error "no write done"
7489 }
7490 run_test 127b "verify the llite client stats are sane"
7491
7492 test_128() { # bug 15212
7493         touch $DIR/$tfile
7494         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7495                 find $DIR/$tfile
7496                 find $DIR/$tfile
7497         EOF
7498
7499         result=$(grep error $TMP/$tfile.log)
7500         rm -f $DIR/$tfile
7501         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7502 }
7503 run_test 128 "interactive lfs for 2 consecutive find's"
7504
7505 set_dir_limits () {
7506         local mntdev
7507         local canondev
7508         local node
7509
7510         local LDPROC=/proc/fs/ldiskfs
7511         local facets=$(get_facets MDS)
7512
7513         for facet in ${facets//,/ }; do
7514                 canondev=$(ldiskfs_canon \
7515                            *.$(convert_facet2label $facet).mntdev $facet)
7516                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7517                                                 LDPROC=/sys/fs/ldiskfs
7518                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7519         done
7520 }
7521
7522 test_129() {
7523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7524         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7525                 skip "Only applicable to ldiskfs-based MDTs"
7526                 return
7527         fi
7528         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7529
7530         EFBIG=27
7531         MAX=16384
7532
7533         set_dir_limits $MAX
7534         test_mkdir -p $DIR/$tdir
7535
7536         local I=0
7537         local J=0
7538         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7539                 $MULTIOP $DIR/$tdir/$J Oc
7540                 rc=$?
7541                 if [ $rc -eq $EFBIG ]; then
7542                         set_dir_limits 0
7543                         echo "return code $rc received as expected"
7544                         return 0
7545                 elif [ $rc -ne 0 ]; then
7546                         set_dir_limits 0
7547                         error_exit "return code $rc received instead of expected $EFBIG"
7548                 fi
7549                 J=$((J+1))
7550                 I=$(stat -c%s "$DIR/$tdir")
7551         done
7552
7553         set_dir_limits 0
7554         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7555 }
7556 run_test 129 "test directory size limit ========================"
7557
7558 OLDIFS="$IFS"
7559 cleanup_130() {
7560         trap 0
7561         IFS="$OLDIFS"
7562 }
7563
7564 test_130a() {
7565         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7566         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7567                 return
7568
7569         trap cleanup_130 EXIT RETURN
7570
7571         local fm_file=$DIR/$tfile
7572         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7573         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7574                 error "dd failed for $fm_file"
7575
7576         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7577         filefrag -ves $fm_file
7578         RC=$?
7579         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7580                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7581         [ $RC != 0 ] && error "filefrag $fm_file failed"
7582
7583         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7584                       grep -v "ext:" | grep -v "found")
7585         lun=$($GETSTRIPE -i $fm_file)
7586
7587         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7588         IFS=$'\n'
7589         tot_len=0
7590         for line in $filefrag_op
7591         do
7592                 frag_lun=`echo $line | cut -d: -f5`
7593                 ext_len=`echo $line | cut -d: -f4`
7594                 if (( $frag_lun != $lun )); then
7595                         cleanup_130
7596                         error "FIEMAP on 1-stripe file($fm_file) failed"
7597                         return
7598                 fi
7599                 (( tot_len += ext_len ))
7600         done
7601
7602         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7603                 cleanup_130
7604                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7605                 return
7606         fi
7607
7608         cleanup_130
7609
7610         echo "FIEMAP on single striped file succeeded"
7611 }
7612 run_test 130a "FIEMAP (1-stripe file)"
7613
7614 test_130b() {
7615         [ "$OSTCOUNT" -lt "2" ] &&
7616                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7617
7618         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7619         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7620                 return
7621
7622         trap cleanup_130 EXIT RETURN
7623
7624         local fm_file=$DIR/$tfile
7625         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7626         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7627                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7628
7629         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7630                 error "dd failed on $fm_file"
7631
7632         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7633         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7634                       grep -v "ext:" | grep -v "found")
7635
7636         last_lun=$(echo $filefrag_op | cut -d: -f5)
7637
7638         IFS=$'\n'
7639         tot_len=0
7640         num_luns=1
7641         for line in $filefrag_op
7642         do
7643                 frag_lun=`echo $line | cut -d: -f5`
7644                 ext_len=`echo $line | cut -d: -f4`
7645                 if (( $frag_lun != $last_lun )); then
7646                         if (( tot_len != 1024 )); then
7647                                 cleanup_130
7648                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7649                                 return
7650                         else
7651                                 (( num_luns += 1 ))
7652                                 tot_len=0
7653                         fi
7654                 fi
7655                 (( tot_len += ext_len ))
7656                 last_lun=$frag_lun
7657         done
7658         if (( num_luns != 2 || tot_len != 1024 )); then
7659                 cleanup_130
7660                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7661                 return
7662         fi
7663
7664         cleanup_130
7665
7666         echo "FIEMAP on 2-stripe file succeeded"
7667 }
7668 run_test 130b "FIEMAP (2-stripe file)"
7669
7670 test_130c() {
7671         [ "$OSTCOUNT" -lt "2" ] &&
7672                 skip_env "skipping FIEMAP on 2-stripe file" && return
7673
7674         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7675         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7676                 return
7677
7678         trap cleanup_130 EXIT RETURN
7679
7680         local fm_file=$DIR/$tfile
7681         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7682         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7683                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7684
7685         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7686
7687         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7688         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7689
7690         last_lun=`echo $filefrag_op | cut -d: -f5`
7691
7692         IFS=$'\n'
7693         tot_len=0
7694         num_luns=1
7695         for line in $filefrag_op
7696         do
7697                 frag_lun=`echo $line | cut -d: -f5`
7698                 ext_len=`echo $line | cut -d: -f4`
7699                 if (( $frag_lun != $last_lun )); then
7700                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7701                         if (( logical != 512 )); then
7702                                 cleanup_130
7703                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7704                                 return
7705                         fi
7706                         if (( tot_len != 512 )); then
7707                                 cleanup_130
7708                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7709                                 return
7710                         else
7711                                 (( num_luns += 1 ))
7712                                 tot_len=0
7713                         fi
7714                 fi
7715                 (( tot_len += ext_len ))
7716                 last_lun=$frag_lun
7717         done
7718         if (( num_luns != 2 || tot_len != 512 )); then
7719                 cleanup_130
7720                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7721                 return
7722         fi
7723
7724         cleanup_130
7725
7726         echo "FIEMAP on 2-stripe file with hole succeeded"
7727 }
7728 run_test 130c "FIEMAP (2-stripe file with hole)"
7729
7730 test_130d() {
7731         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7732
7733         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7734         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7735
7736         trap cleanup_130 EXIT RETURN
7737
7738         local fm_file=$DIR/$tfile
7739         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7740         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7741                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7742         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7743
7744         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7745         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7746
7747         last_lun=`echo $filefrag_op | cut -d: -f5`
7748
7749         IFS=$'\n'
7750         tot_len=0
7751         num_luns=1
7752         for line in $filefrag_op
7753         do
7754                 frag_lun=`echo $line | cut -d: -f5`
7755                 ext_len=`echo $line | cut -d: -f4`
7756                 if (( $frag_lun != $last_lun )); then
7757                         if (( tot_len != 1024 )); then
7758                                 cleanup_130
7759                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7760                                 return
7761                         else
7762                                 (( num_luns += 1 ))
7763                                 tot_len=0
7764                         fi
7765                 fi
7766                 (( tot_len += ext_len ))
7767                 last_lun=$frag_lun
7768         done
7769         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7770                 cleanup_130
7771                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7772                 return
7773         fi
7774
7775         cleanup_130
7776
7777         echo "FIEMAP on N-stripe file succeeded"
7778 }
7779 run_test 130d "FIEMAP (N-stripe file)"
7780
7781 test_130e() {
7782         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7783
7784         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7785         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7786
7787         trap cleanup_130 EXIT RETURN
7788
7789         local fm_file=$DIR/$tfile
7790         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7791         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7792                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7793
7794         NUM_BLKS=512
7795         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7796         for ((i = 0; i < $NUM_BLKS; i++))
7797         do
7798                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7799         done
7800
7801         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7802         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7803
7804         last_lun=`echo $filefrag_op | cut -d: -f5`
7805
7806         IFS=$'\n'
7807         tot_len=0
7808         num_luns=1
7809         for line in $filefrag_op
7810         do
7811                 frag_lun=`echo $line | cut -d: -f5`
7812                 ext_len=`echo $line | cut -d: -f4`
7813                 if (( $frag_lun != $last_lun )); then
7814                         if (( tot_len != $EXPECTED_LEN )); then
7815                                 cleanup_130
7816                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7817                                 return
7818                         else
7819                                 (( num_luns += 1 ))
7820                                 tot_len=0
7821                         fi
7822                 fi
7823                 (( tot_len += ext_len ))
7824                 last_lun=$frag_lun
7825         done
7826         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7827                 cleanup_130
7828                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7829                 return
7830         fi
7831
7832         cleanup_130
7833
7834         echo "FIEMAP with continuation calls succeeded"
7835 }
7836 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7837
7838 # Test for writev/readv
7839 test_131a() {
7840         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7841         error "writev test failed"
7842         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7843         error "readv failed"
7844         rm -f $DIR/$tfile
7845 }
7846 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7847
7848 test_131b() {
7849         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7850         error "append writev test failed"
7851         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7852         error "append writev test failed"
7853         rm -f $DIR/$tfile
7854 }
7855 run_test 131b "test append writev"
7856
7857 test_131c() {
7858         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7859         error "NOT PASS"
7860 }
7861 run_test 131c "test read/write on file w/o objects"
7862
7863 test_131d() {
7864         rwv -f $DIR/$tfile -w -n 1 1572864
7865         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7866         if [ "$NOB" != 1572864 ]; then
7867                 error "Short read filed: read $NOB bytes instead of 1572864"
7868         fi
7869         rm -f $DIR/$tfile
7870 }
7871 run_test 131d "test short read"
7872
7873 test_131e() {
7874         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7875         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7876         error "read hitting hole failed"
7877         rm -f $DIR/$tfile
7878 }
7879 run_test 131e "test read hitting hole"
7880
7881 get_ost_param() {
7882         local token=$1
7883         local gl_sum=0
7884         for node in $(osts_nodes); do
7885                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7886                 [ x$gl = x"" ] && gl=0
7887                 gl_sum=$((gl_sum + gl))
7888         done
7889         echo $gl
7890 }
7891
7892 som_mode_switch() {
7893         local som=$1
7894         local gl1=$2
7895         local gl2=$3
7896
7897         if [ x$som = x"enabled" ]; then
7898                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7899                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7900                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7901         else
7902                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7903                 MOUNTOPT="$MOUNTOPT,som_preview"
7904                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7905         fi
7906
7907         # do remount to make new mount-conf parameters actual
7908         echo remounting...
7909         sync
7910         stopall
7911         setupall
7912 }
7913
7914 test_132() { #1028, SOM
7915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7916         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7917         local num=$(get_mds_dir $DIR)
7918         local mymds=mds${num}
7919         local MOUNTOPT_SAVE=$MOUNTOPT
7920
7921         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7922         cancel_lru_locks osc
7923
7924         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7925
7926         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7927         stat $DIR/$tfile >/dev/null
7928         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7929         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7930         rm $DIR/$tfile
7931         som_mode_switch $som1 $gl1 $gl2
7932
7933         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7934         cancel_lru_locks osc
7935
7936         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7937         if [ $som1 == $som2 ]; then
7938             error "som is still "$som2
7939             if [ x$som2 = x"enabled" ]; then
7940                 som2="disabled"
7941             else
7942                 som2="enabled"
7943             fi
7944         fi
7945
7946         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7947         stat $DIR/$tfile >/dev/null
7948         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7949         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7950         som_mode_switch $som2 $gl1 $gl2
7951         MOUNTOPT=$MOUNTOPT_SAVE
7952 }
7953 run_test 132 "som avoids glimpse rpc"
7954
7955 check_stats() {
7956         local res
7957         local count
7958         case $1 in
7959         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7960                  ;;
7961         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7962                  ;;
7963         *) error "Wrong argument $1" ;;
7964         esac
7965         echo $res
7966         count=`echo $res | awk '{print $2}'`
7967         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7968         # if the argument $3 is zero, it means any stat increment is ok.
7969         if [ $3 -gt 0 ] ; then
7970                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7971         fi
7972 }
7973
7974 test_133a() {
7975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7976         remote_ost_nodsh && skip "remote OST with nodsh" && return
7977         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7978
7979         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7980                 { skip "MDS doesn't support rename stats"; return; }
7981         local testdir=$DIR/${tdir}/stats_testdir
7982         mkdir -p $DIR/${tdir}
7983
7984         # clear stats.
7985         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7986         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7987
7988         # verify mdt stats first.
7989         mkdir ${testdir} || error "mkdir failed"
7990         check_stats $SINGLEMDS "mkdir" 1
7991         touch ${testdir}/${tfile} || "touch failed"
7992         check_stats $SINGLEMDS "open" 1
7993         check_stats $SINGLEMDS "close" 1
7994         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7995         check_stats $SINGLEMDS "mknod" 1
7996         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7997         check_stats $SINGLEMDS "unlink" 1
7998         rm -f ${testdir}/${tfile} || error "file remove failed"
7999         check_stats $SINGLEMDS "unlink" 2
8000
8001         # remove working dir and check mdt stats again.
8002         rmdir ${testdir} || error "rmdir failed"
8003         check_stats $SINGLEMDS "rmdir" 1
8004
8005         local testdir1=$DIR/${tdir}/stats_testdir1
8006         mkdir -p ${testdir}
8007         mkdir -p ${testdir1}
8008         touch ${testdir1}/test1
8009         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8010         check_stats $SINGLEMDS "crossdir_rename" 1
8011
8012         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8013         check_stats $SINGLEMDS "samedir_rename" 1
8014
8015         rm -rf $DIR/${tdir}
8016 }
8017 run_test 133a "Verifying MDT stats ========================================"
8018
8019 test_133b() {
8020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8021         remote_ost_nodsh && skip "remote OST with nodsh" && return
8022         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8023         local testdir=$DIR/${tdir}/stats_testdir
8024         mkdir -p ${testdir} || error "mkdir failed"
8025         touch ${testdir}/${tfile} || "touch failed"
8026         cancel_lru_locks mdc
8027
8028         # clear stats.
8029         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8030         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8031
8032         # extra mdt stats verification.
8033         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8034         check_stats $SINGLEMDS "setattr" 1
8035         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8036         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8037         then            # LU-1740
8038                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8039                 check_stats $SINGLEMDS "getattr" 1
8040         fi
8041         $LFS df || error "lfs failed"
8042         check_stats $SINGLEMDS "statfs" 1
8043
8044         rm -rf $DIR/${tdir}
8045 }
8046 run_test 133b "Verifying extra MDT stats =================================="
8047
8048 test_133c() {
8049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8050         remote_ost_nodsh && skip "remote OST with nodsh" && return
8051         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8052         local testdir=$DIR/${tdir}/stats_testdir
8053         test_mkdir -p ${testdir} || error "mkdir failed"
8054
8055         # verify obdfilter stats.
8056         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8057         sync
8058         cancel_lru_locks osc
8059         wait_delete_completed
8060
8061         # clear stats.
8062         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8063         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8064
8065         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8066         sync
8067         cancel_lru_locks osc
8068         check_stats ost "write" 1
8069
8070         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8071         check_stats ost "read" 1
8072
8073         > ${testdir}/${tfile} || error "truncate failed"
8074         check_stats ost "punch" 1
8075
8076         rm -f ${testdir}/${tfile} || error "file remove failed"
8077         wait_delete_completed
8078         check_stats ost "destroy" 1
8079
8080         rm -rf $DIR/${tdir}
8081 }
8082 run_test 133c "Verifying OST stats ========================================"
8083
8084 order_2() {
8085     local value=$1
8086     local orig=$value
8087     local order=1
8088
8089     while [ $value -ge 2 ]; do
8090         order=$((order*2))
8091         value=$((value/2))
8092     done
8093
8094     if [ $orig -gt $order ]; then
8095         order=$((order*2))
8096     fi
8097     echo $order
8098 }
8099
8100 size_in_KMGT() {
8101     local value=$1
8102     local size=('K' 'M' 'G' 'T');
8103     local i=0
8104     local size_string=$value
8105
8106     while [ $value -ge 1024 ]; do
8107         if [ $i -gt 3 ]; then
8108             #T is the biggest unit we get here, if that is bigger,
8109             #just return XXXT
8110             size_string=${value}T
8111             break
8112         fi
8113         value=$((value >> 10))
8114         if [ $value -lt 1024 ]; then
8115             size_string=${value}${size[$i]}
8116             break
8117         fi
8118         i=$((i + 1))
8119     done
8120
8121     echo $size_string
8122 }
8123
8124 get_rename_size() {
8125     local size=$1
8126     local context=${2:-.}
8127     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8128                 grep -A1 $context |
8129                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8130     echo $sample
8131 }
8132
8133 test_133d() {
8134         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8135     remote_ost_nodsh && skip "remote OST with nodsh" && return
8136     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8137     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8138         { skip "MDS doesn't support rename stats"; return; }
8139
8140     local testdir1=$DIR/${tdir}/stats_testdir1
8141     local testdir2=$DIR/${tdir}/stats_testdir2
8142
8143     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8144
8145     test_mkdir -p ${testdir1} || error "mkdir failed"
8146     test_mkdir -p ${testdir2} || error "mkdir failed"
8147
8148     createmany -o $testdir1/test 512 || error "createmany failed"
8149
8150         # check samedir rename size
8151         mv ${testdir1}/test0 ${testdir1}/test_0
8152
8153         local testdir1_size=$(ls -l $DIR/${tdir} |
8154                 awk '/stats_testdir1/ {print $5}')
8155         local testdir2_size=$(ls -l $DIR/${tdir} |
8156                 awk '/stats_testdir2/ {print $5}')
8157
8158         testdir1_size=$(order_2 $testdir1_size)
8159         testdir2_size=$(order_2 $testdir2_size)
8160
8161         testdir1_size=$(size_in_KMGT $testdir1_size)
8162         testdir2_size=$(size_in_KMGT $testdir2_size)
8163
8164         echo "source rename dir size: ${testdir1_size}"
8165         echo "target rename dir size: ${testdir2_size}"
8166
8167     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8168     eval $cmd || error "$cmd failed"
8169     local samedir=$($cmd | grep 'same_dir')
8170     local same_sample=$(get_rename_size $testdir1_size)
8171     [ -z "$samedir" ] && error "samedir_rename_size count error"
8172     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8173     echo "Check same dir rename stats success"
8174
8175     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8176
8177     # check crossdir rename size
8178     mv ${testdir1}/test_0 ${testdir2}/test_0
8179
8180         testdir1_size=$(ls -l $DIR/${tdir} |
8181                 awk '/stats_testdir1/ {print $5}')
8182         testdir2_size=$(ls -l $DIR/${tdir} |
8183                 awk '/stats_testdir2/ {print $5}')
8184
8185         testdir1_size=$(order_2 $testdir1_size)
8186         testdir2_size=$(order_2 $testdir2_size)
8187
8188         testdir1_size=$(size_in_KMGT $testdir1_size)
8189         testdir2_size=$(size_in_KMGT $testdir2_size)
8190
8191         echo "source rename dir size: ${testdir1_size}"
8192         echo "target rename dir size: ${testdir2_size}"
8193
8194     eval $cmd || error "$cmd failed"
8195     local crossdir=$($cmd | grep 'crossdir')
8196     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8197     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8198     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8199     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8200     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8201     echo "Check cross dir rename stats success"
8202     rm -rf $DIR/${tdir}
8203 }
8204 run_test 133d "Verifying rename_stats ========================================"
8205
8206 test_133e() {
8207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8208         local testdir=$DIR/${tdir}/stats_testdir
8209         local ctr f0 f1 bs=32768 count=42 sum
8210
8211         remote_ost_nodsh && skip "remote OST with nodsh" && return
8212         mkdir -p ${testdir} || error "mkdir failed"
8213
8214         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8215
8216         for ctr in {write,read}_bytes; do
8217                 sync
8218                 cancel_lru_locks osc
8219
8220                 do_facet ost1 $LCTL set_param -n \
8221                         "obdfilter.*.exports.clear=clear"
8222
8223                 if [ $ctr = write_bytes ]; then
8224                         f0=/dev/zero
8225                         f1=${testdir}/${tfile}
8226                 else
8227                         f0=${testdir}/${tfile}
8228                         f1=/dev/null
8229                 fi
8230
8231                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8232                         error "dd failed"
8233                 sync
8234                 cancel_lru_locks osc
8235
8236                 sum=$(do_facet ost1 $LCTL get_param \
8237                                 "obdfilter.*.exports.*.stats" | \
8238                           awk -v ctr=$ctr '\
8239                                 BEGIN { sum = 0 }
8240                                 $1 == ctr { sum += $7 }
8241                                 END { print sum }')
8242
8243                 if ((sum != bs * count)); then
8244                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8245                 fi
8246         done
8247
8248         rm -rf $DIR/${tdir}
8249 }
8250 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8251
8252 test_140() { #bug-17379
8253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8254         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8255         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8256         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8257
8258         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8259         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8260         local i=0
8261         while i=`expr $i + 1`; do
8262                 test_mkdir -p $i || error "Creating dir $i"
8263                 cd $i || error "Changing to $i"
8264                 ln -s ../stat stat || error "Creating stat symlink"
8265                 # Read the symlink until ELOOP present,
8266                 # not LBUGing the system is considered success,
8267                 # we didn't overrun the stack.
8268                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8269                 [ $ret -ne 0 ] && {
8270                         if [ $ret -eq 40 ]; then
8271                                 break  # -ELOOP
8272                         else
8273                                 error "Open stat symlink"
8274                                 return
8275                         fi
8276                 }
8277         done
8278         i=`expr $i - 1`
8279         echo "The symlink depth = $i"
8280         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8281                                         error "Invalid symlink depth"
8282
8283         # Test recursive symlink
8284         ln -s symlink_self symlink_self
8285         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8286         echo "open symlink_self returns $ret"
8287         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8288 }
8289 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8290
8291 test_150() {
8292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8293         local TF="$TMP/$tfile"
8294
8295         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8296         cp $TF $DIR/$tfile
8297         cancel_lru_locks osc
8298         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8299         remount_client $MOUNT
8300         df -P $MOUNT
8301         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8302
8303         $TRUNCATE $TF 6000
8304         $TRUNCATE $DIR/$tfile 6000
8305         cancel_lru_locks osc
8306         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8307
8308         echo "12345" >>$TF
8309         echo "12345" >>$DIR/$tfile
8310         cancel_lru_locks osc
8311         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8312
8313         echo "12345" >>$TF
8314         echo "12345" >>$DIR/$tfile
8315         cancel_lru_locks osc
8316         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8317
8318         rm -f $TF
8319         true
8320 }
8321 run_test 150 "truncate/append tests"
8322
8323 function roc_hit() {
8324         local list=$(comma_list $(osts_nodes))
8325
8326         echo $(get_osd_param $list '' stats |
8327                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8328 }
8329
8330 function set_cache() {
8331         local on=1
8332
8333         if [ "$2" == "off" ]; then
8334                 on=0;
8335         fi
8336         local list=$(comma_list $(osts_nodes))
8337         set_osd_param $list '' $1_cache_enable $on
8338
8339         cancel_lru_locks osc
8340 }
8341
8342 test_151() {
8343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8344         remote_ost_nodsh && skip "remote OST with nodsh" && return
8345
8346         local CPAGES=3
8347         local list=$(comma_list $(osts_nodes))
8348
8349         # check whether obdfilter is cache capable at all
8350         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8351                 echo "not cache-capable obdfilter"
8352                 return 0
8353         fi
8354
8355         # check cache is enabled on all obdfilters
8356         if get_osd_param $list '' read_cache_enable | grep 0; then
8357                 echo "oss cache is disabled"
8358                 return 0
8359         fi
8360
8361         set_osd_param $list '' writethrough_cache_enable 1
8362
8363         # pages should be in the case right after write
8364         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
8365         local BEFORE=`roc_hit`
8366         cancel_lru_locks osc
8367         cat $DIR/$tfile >/dev/null
8368         local AFTER=`roc_hit`
8369         if ! let "AFTER - BEFORE == CPAGES"; then
8370                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8371         fi
8372
8373         # the following read invalidates the cache
8374         cancel_lru_locks osc
8375         set_osd_param $list '' read_cache_enable 0
8376         cat $DIR/$tfile >/dev/null
8377
8378         # now data shouldn't be found in the cache
8379         BEFORE=`roc_hit`
8380         cancel_lru_locks osc
8381         cat $DIR/$tfile >/dev/null
8382         AFTER=`roc_hit`
8383         if let "AFTER - BEFORE != 0"; then
8384                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8385         fi
8386
8387         set_osd_param $list '' read_cache_enable 1
8388         rm -f $DIR/$tfile
8389 }
8390 run_test 151 "test cache on oss and controls ==============================="
8391
8392 test_152() {
8393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8394         local TF="$TMP/$tfile"
8395
8396         # simulate ENOMEM during write
8397 #define OBD_FAIL_OST_NOMEM      0x226
8398         lctl set_param fail_loc=0x80000226
8399         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8400         cp $TF $DIR/$tfile
8401         sync || error "sync failed"
8402         lctl set_param fail_loc=0
8403
8404         # discard client's cache
8405         cancel_lru_locks osc
8406
8407         # simulate ENOMEM during read
8408         lctl set_param fail_loc=0x80000226
8409         cmp $TF $DIR/$tfile || error "cmp failed"
8410         lctl set_param fail_loc=0
8411
8412         rm -f $TF
8413 }
8414 run_test 152 "test read/write with enomem ============================"
8415
8416 test_153() {
8417         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8418 }
8419 run_test 153 "test if fdatasync does not crash ======================="
8420
8421 test_154() {
8422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8423         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8424                 { skip "Need MDS version at least 2.2.51"; return 0; }
8425
8426         cp /etc/hosts $DIR/$tfile
8427
8428         fid=$($LFS path2fid $DIR/$tfile)
8429         rc=$?
8430         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8431
8432         ffid=$DIR/.lustre/fid/$fid
8433
8434         echo "stat fid $fid"
8435         stat $ffid > /dev/null || error "stat $ffid failed."
8436         echo "touch fid $fid"
8437         touch $ffid || error "touch $ffid failed."
8438         echo "write to fid $fid"
8439         cat /etc/hosts > $ffid || error "write $ffid failed."
8440         echo "read fid $fid"
8441         diff /etc/hosts $ffid || error "read $ffid failed."
8442         echo "append write to fid $fid"
8443         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8444         echo "rename fid $fid"
8445         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
8446         touch $DIR/$tfile.1
8447         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
8448         rm -f $DIR/$tfile.1
8449         echo "truncate fid $fid"
8450         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8451         echo "link fid $fid"
8452         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
8453         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8454                 echo "setfacl fid $fid"
8455                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8456                 echo "getfacl fid $fid"
8457                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8458         fi
8459         echo "unlink fid $fid"
8460         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
8461         echo "mknod fid $fid"
8462         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8463
8464         fid=[0xf00000400:0x1:0x0]
8465         ffid=$DIR/.lustre/fid/$fid
8466
8467         echo "stat non-exist fid $fid"
8468         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8469         echo "write to non-exist fid $fid"
8470         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8471         echo "link new fid $fid"
8472         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8473
8474         test_mkdir -p $DIR/$tdir
8475         touch $DIR/$tdir/$tfile
8476         fid=$($LFS path2fid $DIR/$tdir)
8477         rc=$?
8478         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8479
8480         ffid=$DIR/.lustre/fid/$fid
8481
8482         echo "ls $fid"
8483         ls $ffid > /dev/null || error "ls $ffid failed."
8484         echo "touch $fid/$tfile.1"
8485         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8486
8487         echo "touch $DIR/.lustre/fid/$tfile"
8488         touch $DIR/.lustre/fid/$tfile && \
8489                 error "touch $DIR/.lustre/fid/$tfile should fail."
8490
8491         echo "setxattr to $DIR/.lustre/fid"
8492         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8493                 error "setxattr should fail."
8494
8495         echo "listxattr for $DIR/.lustre/fid"
8496         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8497                 error "listxattr should fail."
8498
8499         echo "delxattr from $DIR/.lustre/fid"
8500         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8501                 error "delxattr should fail."
8502
8503         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8504         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8505                 error "touch invalid fid should fail."
8506
8507         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8508         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8509                 error "touch non-normal fid should fail."
8510
8511         echo "rename $tdir to $DIR/.lustre/fid"
8512         mrename $DIR/$tdir $DIR/.lustre/fid &&
8513                 error "rename to $DIR/.lustre/fid should fail."
8514
8515         echo "rename .lustre to itself"
8516         fid=$($LFS path2fid $DIR)
8517         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8518                 error "rename .lustre to itself should fail."
8519
8520         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8521         fid=$($LFS path2fid $DIR/$tfile-2)
8522         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8523         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8524                 error "create lov data thru .lustre should fail."
8525         echo "cp /etc/passwd $DIR/$tfile-2"
8526         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8527         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8528         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8529                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8530
8531         echo "Open-by-FID succeeded"
8532 }
8533 run_test 154 "Open-by-FID"
8534
8535 test_155_small_load() {
8536     local temp=$TMP/$tfile
8537     local file=$DIR/$tfile
8538
8539     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8540         error "dd of=$temp bs=6096 count=1 failed"
8541     cp $temp $file
8542     cancel_lru_locks osc
8543     cmp $temp $file || error "$temp $file differ"
8544
8545     $TRUNCATE $temp 6000
8546     $TRUNCATE $file 6000
8547     cmp $temp $file || error "$temp $file differ (truncate1)"
8548
8549     echo "12345" >>$temp
8550     echo "12345" >>$file
8551     cmp $temp $file || error "$temp $file differ (append1)"
8552
8553     echo "12345" >>$temp
8554     echo "12345" >>$file
8555     cmp $temp $file || error "$temp $file differ (append2)"
8556
8557     rm -f $temp $file
8558     true
8559 }
8560
8561 test_155_big_load() {
8562     remote_ost_nodsh && skip "remote OST with nodsh" && return
8563     local temp=$TMP/$tfile
8564     local file=$DIR/$tfile
8565
8566     free_min_max
8567     local cache_size=$(do_facet ost$((MAXI+1)) \
8568         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8569     local large_file_size=$((cache_size * 2))
8570
8571     echo "OSS cache size: $cache_size KB"
8572     echo "Large file size: $large_file_size KB"
8573
8574     [ $MAXV -le $large_file_size ] && \
8575         skip_env "max available OST size needs > $large_file_size KB" && \
8576         return 0
8577
8578     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8579
8580     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8581         error "dd of=$temp bs=$large_file_size count=1k failed"
8582     cp $temp $file
8583     ls -lh $temp $file
8584     cancel_lru_locks osc
8585     cmp $temp $file || error "$temp $file differ"
8586
8587     rm -f $temp $file
8588     true
8589 }
8590
8591 test_155a() {
8592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8593         set_cache read on
8594         set_cache writethrough on
8595         test_155_small_load
8596 }
8597 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8598
8599 test_155b() {
8600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8601         set_cache read on
8602         set_cache writethrough off
8603         test_155_small_load
8604 }
8605 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8606
8607 test_155c() {
8608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8609         set_cache read off
8610         set_cache writethrough on
8611         test_155_small_load
8612 }
8613 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8614
8615 test_155d() {
8616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8617         set_cache read off
8618         set_cache writethrough off
8619         test_155_small_load
8620 }
8621 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8622
8623 test_155e() {
8624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8625         set_cache read on
8626         set_cache writethrough on
8627         test_155_big_load
8628 }
8629 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8630
8631 test_155f() {
8632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8633         set_cache read on
8634         set_cache writethrough off
8635         test_155_big_load
8636 }
8637 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8638
8639 test_155g() {
8640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8641         set_cache read off
8642         set_cache writethrough on
8643         test_155_big_load
8644 }
8645 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8646
8647 test_155h() {
8648         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8649         set_cache read off
8650         set_cache writethrough off
8651         test_155_big_load
8652 }
8653 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8654
8655 test_156() {
8656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8657         local CPAGES=3
8658         local BEFORE
8659         local AFTER
8660         local file="$DIR/$tfile"
8661
8662         [ "$(facet_fstype ost1)" = "zfs" ] &&
8663                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8664                 return
8665
8666     log "Turn on read and write cache"
8667     set_cache read on
8668     set_cache writethrough on
8669
8670     log "Write data and read it back."
8671     log "Read should be satisfied from the cache."
8672     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8673     BEFORE=`roc_hit`
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     log "Read again; it should be satisfied from the cache."
8684     BEFORE=$AFTER
8685     cancel_lru_locks osc
8686     cat $file >/dev/null
8687     AFTER=`roc_hit`
8688     if ! let "AFTER - BEFORE == CPAGES"; then
8689         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8690     else
8691         log "cache hits:: before: $BEFORE, after: $AFTER"
8692     fi
8693
8694
8695     log "Turn off the read cache and turn on the write cache"
8696     set_cache read off
8697     set_cache writethrough on
8698
8699     log "Read again; it should be satisfied from the cache."
8700     BEFORE=`roc_hit`
8701     cancel_lru_locks osc
8702     cat $file >/dev/null
8703     AFTER=`roc_hit`
8704     if ! let "AFTER - BEFORE == CPAGES"; then
8705         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8706     else
8707         log "cache hits:: before: $BEFORE, after: $AFTER"
8708     fi
8709
8710     log "Read again; it should not be satisfied from the cache."
8711     BEFORE=$AFTER
8712     cancel_lru_locks osc
8713     cat $file >/dev/null
8714     AFTER=`roc_hit`
8715     if ! let "AFTER - BEFORE == 0"; then
8716         error "IN CACHE: before: $BEFORE, after: $AFTER"
8717     else
8718         log "cache hits:: before: $BEFORE, after: $AFTER"
8719     fi
8720
8721     log "Write data and read it back."
8722     log "Read should be satisfied from the cache."
8723     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8724     BEFORE=`roc_hit`
8725     cancel_lru_locks osc
8726     cat $file >/dev/null
8727     AFTER=`roc_hit`
8728     if ! let "AFTER - BEFORE == CPAGES"; then
8729         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8730     else
8731         log "cache hits:: before: $BEFORE, after: $AFTER"
8732     fi
8733
8734     log "Read again; it should not be satisfied from the cache."
8735     BEFORE=$AFTER
8736     cancel_lru_locks osc
8737     cat $file >/dev/null
8738     AFTER=`roc_hit`
8739     if ! let "AFTER - BEFORE == 0"; then
8740         error "IN CACHE: before: $BEFORE, after: $AFTER"
8741     else
8742         log "cache hits:: before: $BEFORE, after: $AFTER"
8743     fi
8744
8745
8746     log "Turn off read and write cache"
8747     set_cache read off
8748     set_cache writethrough off
8749
8750     log "Write data and read it back"
8751     log "It should not be satisfied from the cache."
8752     rm -f $file
8753     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8754     cancel_lru_locks osc
8755     BEFORE=`roc_hit`
8756     cat $file >/dev/null
8757     AFTER=`roc_hit`
8758     if ! let "AFTER - BEFORE == 0"; then
8759         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8760     else
8761         log "cache hits:: before: $BEFORE, after: $AFTER"
8762     fi
8763
8764
8765     log "Turn on the read cache and turn off the write cache"
8766     set_cache read on
8767     set_cache writethrough off
8768
8769     log "Write data and read it back"
8770     log "It should not be satisfied from the cache."
8771     rm -f $file
8772     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8773     BEFORE=`roc_hit`
8774     cancel_lru_locks osc
8775     cat $file >/dev/null
8776     AFTER=`roc_hit`
8777     if ! let "AFTER - BEFORE == 0"; then
8778         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8779     else
8780         log "cache hits:: before: $BEFORE, after: $AFTER"
8781     fi
8782
8783     log "Read again; it should be satisfied from the cache."
8784     BEFORE=`roc_hit`
8785     cancel_lru_locks osc
8786     cat $file >/dev/null
8787     AFTER=`roc_hit`
8788     if ! let "AFTER - BEFORE == CPAGES"; then
8789         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8790     else
8791         log "cache hits:: before: $BEFORE, after: $AFTER"
8792     fi
8793
8794     rm -f $file
8795 }
8796 run_test 156 "Verification of tunables ============================"
8797
8798 #Changelogs
8799 err17935 () {
8800     if [ $MDSCOUNT -gt 1 ]; then
8801         error_ignore 17935 $*
8802     else
8803         error $*
8804     fi
8805 }
8806
8807 changelog_chmask()
8808 {
8809     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8810            grep -c $1)
8811
8812     if [ $MASK -eq 1 ]; then
8813         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8814     else
8815         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8816     fi
8817 }
8818
8819 test_160() {
8820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8821     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8822     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8823         { skip "Need MDS version at least 2.2.0"; return; }
8824     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8825     echo "Registered as changelog user $USER"
8826     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8827         grep -q $USER || error "User $USER not found in changelog_users"
8828
8829     # change something
8830     test_mkdir -p $DIR/$tdir/pics/2008/zachy
8831     touch $DIR/$tdir/pics/2008/zachy/timestamp
8832     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8833     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8834     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8835     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8836     rm $DIR/$tdir/pics/desktop.jpg
8837
8838     $LFS changelog $MDT0 | tail -5
8839
8840     echo "verifying changelog mask"
8841     changelog_chmask "MKDIR"
8842     changelog_chmask "CLOSE"
8843
8844     test_mkdir -p $DIR/$tdir/pics/zach/sofia
8845     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8846
8847     changelog_chmask "MKDIR"
8848     changelog_chmask "CLOSE"
8849
8850     test_mkdir -p $DIR/$tdir/pics/2008/sofia
8851     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8852
8853     $LFS changelog $MDT0
8854     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8855     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8856     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8857     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8858
8859     # verify contents
8860     echo "verifying target fid"
8861     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8862         tail -1 | awk '{print $6}')
8863     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8864     [ "$fidc" == "t=$fidf" ] || \
8865         err17935 "fid in changelog $fidc != file fid $fidf"
8866     echo "verifying parent fid"
8867     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8868         tail -1 | awk '{print $7}')
8869     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8870     [ "$fidc" == "p=$fidf" ] || \
8871         err17935 "pfid in changelog $fidc != dir fid $fidf"
8872
8873     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8874         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8875     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8876     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8877         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8878     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8879     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8880         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8881
8882     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8883         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8884     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8885     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8886     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8887         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8888
8889     echo "verifying user deregister"
8890     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8891     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8892         grep -q $USER && error "User $USER still found in changelog_users"
8893
8894     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8895         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8896     if [ $USERS -eq 0 ]; then
8897         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8898             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8899         touch $DIR/$tdir/chloe
8900         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8901             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8902         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8903         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8904     else
8905         echo "$USERS other changelog users; can't verify off"
8906     fi
8907 }
8908 run_test 160 "changelog sanity"
8909
8910 test_161() {
8911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8912     test_mkdir -p $DIR/$tdir
8913     cp /etc/hosts $DIR/$tdir/$tfile
8914     test_mkdir $DIR/$tdir/foo1
8915     test_mkdir $DIR/$tdir/foo2
8916     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8917     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8918     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8919     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8920     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8921     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8922         $LFS fid2path $DIR $FID
8923         err17935 "bad link ea"
8924     fi
8925     # middle
8926     rm $DIR/$tdir/foo2/zachary
8927     # last
8928     rm $DIR/$tdir/foo2/thor
8929     # first
8930     rm $DIR/$tdir/$tfile
8931     # rename
8932     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8933     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8934         then
8935         $LFS fid2path $DIR $FID
8936         err17935 "bad link rename"
8937     fi
8938     rm $DIR/$tdir/foo2/maggie
8939
8940     # overflow the EA
8941     local longname=filename_avg_len_is_thirty_two_
8942     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8943         error "failed to hardlink many files"
8944     links=$($LFS fid2path $DIR $FID | wc -l)
8945     echo -n "${links}/1000 links in link EA"
8946     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8947     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8948         error "failed to unlink many hardlinks"
8949 }
8950 run_test 161 "link ea sanity"
8951
8952 check_path() {
8953     local expected=$1
8954     shift
8955     local fid=$2
8956
8957     local path=$(${LFS} fid2path $*)
8958     RC=$?
8959
8960     if [ $RC -ne 0 ]; then
8961         err17935 "path looked up of $expected failed. Error $RC"
8962         return $RC
8963     elif [ "${path}" != "${expected}" ]; then
8964         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8965         return 2
8966     fi
8967     echo "fid $fid resolves to path $path (expected $expected)"
8968 }
8969
8970 test_162() {
8971         # Make changes to filesystem
8972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8973         test_mkdir -p $DIR/$tdir/d2
8974         touch $DIR/$tdir/d2/$tfile
8975         touch $DIR/$tdir/d2/x1
8976         touch $DIR/$tdir/d2/x2
8977         test_mkdir -p $DIR/$tdir/d2/a/b/c
8978         test_mkdir -p $DIR/$tdir/d2/p/q/r
8979         # regular file
8980         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8981         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8982
8983         # softlink
8984         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8985         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8986         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8987
8988         # softlink to wrong file
8989         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8990         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8991         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8992
8993         # hardlink
8994         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8995         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8996         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8997         # fid2path dir/fsname should both work
8998         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8999         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9000
9001         # hardlink count: check that there are 2 links
9002         # Doesnt work with CMD yet: 17935
9003         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9004                 err17935 "expected 2 links"
9005
9006         # hardlink indexing: remove the first link
9007         rm $DIR/$tdir/d2/p/q/r/hlink
9008         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9009
9010         return 0
9011 }
9012 run_test 162 "path lookup sanity"
9013
9014 test_163() {
9015         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9016         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9017         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
9018         copytool $FSNAME &
9019         sleep 1
9020         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
9021         # this proc file is temporary and linux-only
9022         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
9023          error "kernel->userspace send failed"
9024         kill -INT $!
9025 }
9026 run_test 163 "kernel <-> userspace comms"
9027
9028 test_169() {
9029         # do directio so as not to populate the page cache
9030         log "creating a 10 Mb file"
9031         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9032         log "starting reads"
9033         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9034         log "truncating the file"
9035         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9036         log "killing dd"
9037         kill %+ || true # reads might have finished
9038         echo "wait until dd is finished"
9039         wait
9040         log "removing the temporary file"
9041         rm -rf $DIR/$tfile || error "tmp file removal failed"
9042 }
9043 run_test 169 "parallel read and truncate should not deadlock"
9044
9045 test_170() {
9046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9047         $LCTL clear     # bug 18514
9048         $LCTL debug_daemon start $TMP/${tfile}_log_good
9049         touch $DIR/$tfile
9050         $LCTL debug_daemon stop
9051         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9052                error "sed failed to read log_good"
9053
9054         $LCTL debug_daemon start $TMP/${tfile}_log_good
9055         rm -rf $DIR/$tfile
9056         $LCTL debug_daemon stop
9057
9058         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9059                error "lctl df log_bad failed"
9060
9061         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9062         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9063
9064         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9065         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9066
9067         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9068                 error "bad_line good_line1 good_line2 are empty"
9069
9070         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9071         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9072         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9073
9074         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9075         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9076         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9077
9078         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9079                 error "bad_line_new good_line_new are empty"
9080
9081         local expected_good=$((good_line1 + good_line2*2))
9082
9083         rm -f $TMP/${tfile}*
9084         # LU-231, short malformed line may not be counted into bad lines
9085         if [ $bad_line -ne $bad_line_new ] &&
9086                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9087                 error "expected $bad_line bad lines, but got $bad_line_new"
9088                 return 1
9089         fi
9090
9091         if [ $expected_good -ne $good_line_new ]; then
9092                 error "expected $expected_good good lines, but got $good_line_new"
9093                 return 2
9094         fi
9095         true
9096 }
9097 run_test 170 "test lctl df to handle corrupted log ====================="
9098
9099 test_171() { # bug20592
9100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9101 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9102         $LCTL set_param fail_loc=0x50e
9103         $LCTL set_param fail_val=3000
9104         multiop_bg_pause $DIR/$tfile O_s || true
9105         local MULTIPID=$!
9106         kill -USR1 $MULTIPID
9107         # cause log dump
9108         sleep 3
9109         wait $MULTIPID
9110         if dmesg | grep "recursive fault"; then
9111                 error "caught a recursive fault"
9112         fi
9113         $LCTL set_param fail_loc=0
9114         true
9115 }
9116 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
9117
9118 # it would be good to share it with obdfilter-survey/libecho code
9119 setup_obdecho_osc () {
9120         local rc=0
9121         local ost_nid=$1
9122         local obdfilter_name=$2
9123         echo "Creating new osc for $obdfilter_name on $ost_nid"
9124         # make sure we can find loopback nid
9125         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
9126
9127         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
9128                            ${obdfilter_name}_osc_UUID || rc=2; }
9129         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
9130                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
9131         return $rc
9132 }
9133
9134 cleanup_obdecho_osc () {
9135         local obdfilter_name=$1
9136         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
9137         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
9138         return 0
9139 }
9140
9141 obdecho_create_test() {
9142         local OBD=$1
9143         local node=$2
9144         local rc=0
9145         local id
9146         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
9147         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
9148                            rc=2; }
9149         if [ $rc -eq 0 ]; then
9150             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
9151             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
9152         fi
9153         echo "New object id is $id"
9154         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
9155                            rc=4; }
9156         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
9157                                         "cleanup" || rc=5; }
9158         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
9159                                         "detach" || rc=6; }
9160         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
9161         return $rc
9162 }
9163
9164 test_180a() {
9165         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9166         remote_ost_nodsh && skip "remote OST with nodsh" && return
9167         local rc=0
9168         local rmmod_local=0
9169
9170         if ! module_loaded obdecho; then
9171             load_module obdecho/obdecho
9172             rmmod_local=1
9173         fi
9174
9175         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
9176         local host=$(lctl get_param -n osc.$osc.import |
9177                              awk '/current_connection:/ {print $2}' )
9178         local target=$(lctl get_param -n osc.$osc.import |
9179                              awk '/target:/ {print $2}' )
9180         target=${target%_UUID}
9181
9182         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
9183         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
9184         [[ -n $target ]] && cleanup_obdecho_osc $target
9185         [ $rmmod_local -eq 1 ] && rmmod obdecho
9186         return $rc
9187 }
9188 run_test 180a "test obdecho on osc"
9189
9190 test_180b() {
9191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9192         remote_ost_nodsh && skip "remote OST with nodsh" && return
9193         local rc=0
9194         local rmmod_remote=0
9195
9196         do_facet ost1 "lsmod | grep -q obdecho || "                      \
9197                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
9198                       "modprobe obdecho; }" && rmmod_remote=1
9199         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
9200         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
9201         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
9202         return $rc
9203 }
9204 run_test 180b "test obdecho directly on obdfilter"
9205
9206 test_181() { # bug 22177
9207         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9208         # create enough files to index the directory
9209         createmany -o $DIR/$tdir/foobar 4000
9210         # print attributes for debug purpose
9211         lsattr -d .
9212         # open dir
9213         multiop_bg_pause $DIR/$tdir D_Sc || return 1
9214         MULTIPID=$!
9215         # remove the files & current working dir
9216         unlinkmany $DIR/$tdir/foobar 4000
9217         rmdir $DIR/$tdir
9218         kill -USR1 $MULTIPID
9219         wait $MULTIPID
9220         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9221         return 0
9222 }
9223 run_test 181 "Test open-unlinked dir ========================"
9224
9225 test_182() {
9226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9227         # disable MDC RPC lock wouldn't crash client
9228         local fcount=1000
9229         local tcount=4
9230
9231         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9232 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9233         $LCTL set_param fail_loc=0x804
9234
9235         for (( i=0; i < $tcount; i++ )) ; do
9236                 mkdir $DIR/$tdir/$i
9237                 createmany -o $DIR/$tdir/$i/f- $fcount &
9238         done
9239         wait
9240
9241         for (( i=0; i < $tcount; i++ )) ; do
9242                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9243         done
9244         wait
9245
9246         rm -rf $DIR/$tdir
9247
9248         $LCTL set_param fail_loc=0
9249 }
9250 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9251
9252 test_183() { # LU-2275
9253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9254         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9255         echo aaa > $DIR/$tdir/$tfile
9256
9257 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9258         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9259
9260         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9261         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9262
9263         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9264
9265         # Flush negative dentry cache
9266         touch $DIR/$tdir/$tfile
9267
9268         # We are not checking for any leaked references here, they'll
9269         # become evident next time we do cleanup with module unload.
9270         rm -rf $DIR/$tdir
9271 }
9272 run_test 183 "No crash or request leak in case of strange dispositions ========"
9273
9274 test_185() { # LU-2441
9275         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9276         touch $DIR/$tdir/spoo
9277         local mtime1=$(stat -c "%Y" $DIR/$tdir)
9278         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
9279                 error "cannot create/write a volatile file"
9280         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
9281                 error "FID is still valid after close"
9282
9283         multiop_bg_pause $DIR/$tdir vVw4096_c
9284         local multi_pid=$!
9285
9286         local OLD_IFS=$IFS
9287         IFS=":"
9288         local fidv=($fid)
9289         IFS=$OLD_IFS
9290         # assume that the next FID for this client is sequential, since stdout
9291         # is unfortunately eaten by multiop_bg_pause
9292         local n=$((${fidv[1]} + 1))
9293         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
9294         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
9295                 error "FID is missing before close"
9296         kill -USR1 $multi_pid
9297         # 1 second delay, so if mtime change we will see it
9298         sleep 1
9299         local mtime2=$(stat -c "%Y" $DIR/$tdir)
9300         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
9301 }
9302 run_test 185 "Volatile file support"
9303
9304 check_swap_layouts_support()
9305 {
9306         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
9307                 { skip "Does not support layout lock."; return 0; }
9308         return 1
9309 }
9310
9311 # test suite 184 is for LU-2016, LU-2017
9312 test_184a() {
9313         check_swap_layouts_support && return 0
9314
9315         dir0=$DIR/$tdir/$testnum
9316         test_mkdir -p $dir0 || error "creating dir $dir0"
9317         ref1=/etc/passwd
9318         ref2=/etc/group
9319         file1=$dir0/f1
9320         file2=$dir0/f2
9321         $SETSTRIPE -c1 $file1
9322         cp $ref1 $file1
9323         $SETSTRIPE -c2 $file2
9324         cp $ref2 $file2
9325         gen1=$($GETSTRIPE -g $file1)
9326         gen2=$($GETSTRIPE -g $file2)
9327
9328         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
9329         gen=$($GETSTRIPE -g $file1)
9330         [[ $gen1 != $gen ]] ||
9331                 "Layout generation on $file1 does not change"
9332         gen=$($GETSTRIPE -g $file2)
9333         [[ $gen2 != $gen ]] ||
9334                 "Layout generation on $file2 does not change"
9335
9336         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
9337         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
9338 }
9339 run_test 184a "Basic layout swap"
9340
9341 test_184b() {
9342         check_swap_layouts_support && return 0
9343
9344         dir0=$DIR/$tdir/$testnum
9345         mkdir -p $dir0 || error "creating dir $dir0"
9346         file1=$dir0/f1
9347         file2=$dir0/f2
9348         file3=$dir0/f3
9349         dir1=$dir0/d1
9350         dir2=$dir0/d2
9351         mkdir $dir1 $dir2
9352         $SETSTRIPE -c1 $file1
9353         $SETSTRIPE -c2 $file2
9354         $SETSTRIPE -c1 $file3
9355         chown $RUNAS_ID $file3
9356         gen1=$($GETSTRIPE -g $file1)
9357         gen2=$($GETSTRIPE -g $file2)
9358
9359         $LFS swap_layouts $dir1 $dir2 &&
9360                 error "swap of directories layouts should fail"
9361         $LFS swap_layouts $dir1 $file1 &&
9362                 error "swap of directory and file layouts should fail"
9363         $RUNAS $LFS swap_layouts $file1 $file2 &&
9364                 error "swap of file we cannot write should fail"
9365         $LFS swap_layouts $file1 $file3 &&
9366                 error "swap of file with different owner should fail"
9367         /bin/true # to clear error code
9368 }
9369 run_test 184b "Forbidden layout swap (will generate errors)"
9370
9371 test_184c() {
9372         check_swap_layouts_support && return 0
9373
9374         dir0=$DIR/$tdir/$testnum
9375         mkdir -p $dir0 || error "creating dir $dir0"
9376         ref1=$dir0/ref1
9377         ref2=$dir0/ref2
9378         file1=$dir0/file1
9379         file2=$dir0/file2
9380         # create a file large enough for the concurent test
9381         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
9382         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
9383         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
9384
9385         cp $ref2 $file2
9386         dd if=$ref1 of=$file1 bs=64k &
9387         sleep 0.$((RANDOM % 5 + 1))
9388
9389         $LFS swap_layouts $file1 $file2
9390         rc=$?
9391         wait $DD_PID
9392         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
9393         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
9394
9395         # how many bytes copied before swapping layout
9396         local copied=`stat -c %s $file2`
9397         local remaining=`stat -c %s $ref1`
9398         remaining=$((remaining - copied))
9399         echo "Copied $copied bytes before swapping layout..."
9400
9401         cmp -n $copied $file1 $ref2 ||
9402                 error "Content mismatch [0, $copied) of ref2 and file1"
9403         cmp -n $copied $file2 $ref1 ||
9404                 error "Content mismatch [0, $copied) of ref1 and file2"
9405         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
9406                 error "Content mismatch [$copied, EOF) of ref1 and file1"
9407
9408         # clean up
9409         rm -f $ref1 $ref2 $file1 $file2
9410 }
9411 run_test 184c "Concurrent write and layout swap"
9412
9413 # OST pools tests
9414 check_file_in_pool()
9415 {
9416         local file=$1
9417         local pool=$2
9418         local tlist="$3"
9419         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9420         for i in $res
9421         do
9422                 for t in $tlist ; do
9423                         [ "$i" -eq "$t" ] && continue 2
9424                 done
9425
9426                 echo "pool list: $tlist"
9427                 echo "striping: $res"
9428                 error_noexit "$file not allocated in $pool"
9429                 return 1
9430         done
9431         return 0
9432 }
9433
9434 pool_add() {
9435         echo "Creating new pool"
9436         local pool=$1
9437
9438         create_pool $FSNAME.$pool ||
9439                 { error_noexit "No pool created, result code $?"; return 1; }
9440         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9441                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9442 }
9443
9444 pool_add_targets() {
9445         echo "Adding targets to pool"
9446         local pool=$1
9447         local first=$2
9448         local last=$3
9449         local step=${4:-1}
9450
9451         local list=$(seq $first $step $last)
9452
9453         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9454         do_facet mgs $LCTL pool_add \
9455                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9456         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9457                         | sort -u | tr '\n' ' ' " "$t" || { 
9458                 error_noexit "Add to pool failed"
9459                 return 1
9460         }
9461         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9462         local addcount=$(((last - first) / step + 1))
9463         [ $lfscount -eq $addcount ] || {
9464                 error_noexit "lfs pool_list bad ost count" \
9465                                                 "$lfscount != $addcount"
9466                 return 2
9467         }
9468 }
9469
9470 pool_set_dir() {
9471         local pool=$1
9472         local tdir=$2
9473         echo "Setting pool on directory $tdir"
9474
9475         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9476
9477         error_noexit "Cannot set pool $pool to $tdir"
9478         return 1
9479 }
9480
9481 pool_check_dir() {
9482         local pool=$1
9483         local tdir=$2
9484         echo "Checking pool on directory $tdir"
9485
9486         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9487         [ "$res" = "$pool" ] && return 0
9488
9489         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9490         return 1
9491 }
9492
9493 pool_dir_rel_path() {
9494         echo "Testing relative path works well"
9495         local pool=$1
9496         local tdir=$2
9497         local root=$3
9498
9499         mkdir -p $root/$tdir/$tdir
9500         cd $root/$tdir
9501         pool_set_dir $pool $tdir          || return 1
9502         pool_set_dir $pool ./$tdir        || return 2
9503         pool_set_dir $pool ../$tdir       || return 3
9504         pool_set_dir $pool ../$tdir/$tdir || return 4
9505         rm -rf $tdir; cd - > /dev/null
9506 }
9507
9508 pool_alloc_files() {
9509         echo "Checking files allocation from directory pool"
9510         local pool=$1
9511         local tdir=$2
9512         local count=$3
9513         local tlist="$4"
9514
9515         local failed=0
9516         for i in $(seq -w 1 $count)
9517         do
9518                 local file=$tdir/file-$i
9519                 touch $file
9520                 check_file_in_pool $file $pool "$tlist" || \
9521                         failed=$((failed + 1))
9522         done
9523         [ "$failed" = 0 ] && return 0
9524
9525         error_noexit "$failed files not allocated in $pool"
9526         return 1
9527 }
9528
9529 pool_create_files() {
9530         echo "Creating files in pool"
9531         local pool=$1
9532         local tdir=$2
9533         local count=$3
9534         local tlist="$4"
9535
9536         mkdir -p $tdir
9537         local failed=0
9538         for i in $(seq -w 1 $count)
9539         do
9540                 local file=$tdir/spoo-$i
9541                 $SETSTRIPE -p $pool $file
9542                 check_file_in_pool $file $pool "$tlist" || \
9543                         failed=$((failed + 1))
9544         done
9545         [ "$failed" = 0 ] && return 0
9546
9547         error_noexit "$failed files not allocated in $pool"
9548         return 1
9549 }
9550
9551 pool_lfs_df() {
9552         echo "Checking 'lfs df' output"
9553         local pool=$1
9554
9555         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9556                         tr '\n' ' ')
9557         local res=$($LFS df --pool $FSNAME.$pool |
9558                         awk '{print $1}' |
9559                         grep "$FSNAME-OST" |
9560                         tr '\n' ' ')
9561         [ "$res" = "$t" ] && return 0
9562
9563         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9564         return 1
9565 }
9566
9567 pool_file_rel_path() {
9568         echo "Creating files in a pool with relative pathname"
9569         local pool=$1
9570         local tdir=$2
9571
9572         mkdir -p $tdir ||
9573                 { error_noexit "unable to create $tdir"; return 1 ; }
9574         local file="/..$tdir/$tfile-1"
9575         $SETSTRIPE -p $pool $file ||
9576                 { error_noexit "unable to create $file" ; return 2 ; }
9577
9578         cd $tdir
9579         $SETSTRIPE -p $pool $tfile-2 || {
9580                 error_noexit "unable to create $tfile-2 in $tdir"
9581                 return 3
9582         }
9583 }
9584
9585 pool_remove_first_target() {
9586         echo "Removing first target from a pool"
9587         local pool=$1
9588
9589         local pname="lov.$FSNAME-*.pools.$pool"
9590         local t=$($LCTL get_param -n $pname | head -1)
9591         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9592         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9593                 error_noexit "$t not removed from $FSNAME.$pool"
9594                 return 1
9595         }
9596 }
9597
9598 pool_remove_all_targets() {
9599         echo "Removing all targets from pool"
9600         local pool=$1
9601         local file=$2
9602         local pname="lov.$FSNAME-*.pools.$pool"
9603         for t in $($LCTL get_param -n $pname | sort -u)
9604         do
9605                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9606         done
9607         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9608                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9609                 return 1
9610         }
9611         # striping on an empty/nonexistant pool should fall back 
9612         # to "pool of everything"
9613         touch $file || {
9614                 error_noexit "failed to use fallback striping for empty pool"
9615                 return 2
9616         }
9617         # setstripe on an empty pool should fail
9618         $SETSTRIPE -p $pool $file 2>/dev/null && {
9619                 error_noexit "expected failure when creating file" \
9620                                                         "with empty pool"
9621                 return 3
9622         }
9623         return 0
9624 }
9625
9626 pool_remove() {
9627         echo "Destroying pool"
9628         local pool=$1
9629         local file=$2
9630
9631         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9632
9633         sleep 2
9634         # striping on an empty/nonexistant pool should fall back 
9635         # to "pool of everything"
9636         touch $file || {
9637                 error_noexit "failed to use fallback striping for missing pool"
9638                 return 1
9639         }
9640         # setstripe on an empty pool should fail
9641         $SETSTRIPE -p $pool $file 2>/dev/null && {
9642                 error_noexit "expected failure when creating file" \
9643                                                         "with missing pool"
9644                 return 2
9645         }
9646
9647         # get param should return err once pool is gone
9648         if wait_update $HOSTNAME "lctl get_param -n \
9649                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9650         then
9651                 remove_pool_from_list $FSNAME.$pool
9652                 return 0
9653         fi
9654         error_noexit "Pool $FSNAME.$pool is not destroyed"
9655         return 3
9656 }
9657
9658 test_200() {
9659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9660         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9661
9662         local POOL=${POOL:-cea1}
9663         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9664         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9665         # Pool OST targets
9666         local first_ost=0
9667         local last_ost=$(($OSTCOUNT - 1))
9668         local ost_step=2
9669         local ost_list=$(seq $first_ost $ost_step $last_ost)
9670         local ost_range="$first_ost $last_ost $ost_step"
9671         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9672         local file_dir=$POOL_ROOT/file_tst
9673
9674         local rc=0
9675         while : ; do
9676                 # former test_200a test_200b
9677                 pool_add $POOL                          || { rc=$? ; break; }
9678                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9679                 # former test_200c test_200d
9680                 mkdir -p $test_path
9681                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9682                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9683                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9684                                                         || { rc=$? ; break; }
9685                 # former test_200e test_200f
9686                 local files=$((OSTCOUNT*3))
9687                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9688                                                         || { rc=$? ; break; }
9689                 pool_create_files $POOL $file_dir $files "$ost_list" \
9690                                                         || { rc=$? ; break; }
9691                 # former test_200g test_200h
9692                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9693                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9694
9695                 # former test_201a test_201b test_201c
9696                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9697
9698                 local f=$test_path/$tfile
9699                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9700                 pool_remove $POOL $f                    || { rc=$? ; break; }
9701                 break
9702         done
9703
9704         cleanup_pools
9705         return $rc
9706 }
9707 run_test 200 "OST pools"
9708
9709 # usage: default_attr <count | size | offset>
9710 default_attr() {
9711         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9712 }
9713
9714 # usage: check_default_stripe_attr
9715 check_default_stripe_attr() {
9716         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9717         case $1 in
9718         --stripe-count|--count)
9719                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9720         --stripe-size|--size)
9721                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9722         --stripe-index|--index)
9723                 EXPECTED=-1;;
9724         *)
9725                 error "unknown getstripe attr '$1'"
9726         esac
9727
9728         [ $ACTUAL != $EXPECTED ] &&
9729                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9730 }
9731
9732 test_204a() {
9733         test_mkdir -p $DIR/$tdir
9734         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9735
9736         check_default_stripe_attr --stripe-count
9737         check_default_stripe_attr --stripe-size
9738         check_default_stripe_attr --stripe-index
9739
9740         return 0
9741 }
9742 run_test 204a "Print default stripe attributes ================="
9743
9744 test_204b() {
9745         test_mkdir -p $DIR/$tdir
9746         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9747
9748         check_default_stripe_attr --stripe-size
9749         check_default_stripe_attr --stripe-index
9750
9751         return 0
9752 }
9753 run_test 204b "Print default stripe size and offset  ==========="
9754
9755 test_204c() {
9756         test_mkdir -p $DIR/$tdir
9757         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9758
9759         check_default_stripe_attr --stripe-count
9760         check_default_stripe_attr --stripe-index
9761
9762         return 0
9763 }
9764 run_test 204c "Print default stripe count and offset ==========="
9765
9766 test_204d() {
9767         test_mkdir -p $DIR/$tdir
9768         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9769
9770         check_default_stripe_attr --stripe-count
9771         check_default_stripe_attr --stripe-size
9772
9773         return 0
9774 }
9775 run_test 204d "Print default stripe count and size ============="
9776
9777 test_204e() {
9778         test_mkdir -p $DIR/$tdir
9779         $SETSTRIPE -d $DIR/$tdir
9780
9781         check_default_stripe_attr --stripe-count --raw
9782         check_default_stripe_attr --stripe-size --raw
9783         check_default_stripe_attr --stripe-index --raw
9784
9785         return 0
9786 }
9787 run_test 204e "Print raw stripe attributes ================="
9788
9789 test_204f() {
9790         test_mkdir -p $DIR/$tdir
9791         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9792
9793         check_default_stripe_attr --stripe-size --raw
9794         check_default_stripe_attr --stripe-index --raw
9795
9796         return 0
9797 }
9798 run_test 204f "Print raw stripe size and offset  ==========="
9799
9800 test_204g() {
9801         test_mkdir -p $DIR/$tdir
9802         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9803
9804         check_default_stripe_attr --stripe-count --raw
9805         check_default_stripe_attr --stripe-index --raw
9806
9807         return 0
9808 }
9809 run_test 204g "Print raw stripe count and offset ==========="
9810
9811 test_204h() {
9812         test_mkdir -p $DIR/$tdir
9813         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9814
9815         check_default_stripe_attr --stripe-count --raw
9816         check_default_stripe_attr --stripe-size --raw
9817
9818         return 0
9819 }
9820 run_test 204h "Print raw stripe count and size ============="
9821
9822 # Figure out which job scheduler is being used, if any,
9823 # or use a fake one
9824 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9825         JOBENV=SLURM_JOB_ID
9826 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9827         JOBENV=LSB_JOBID
9828 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9829         JOBENV=PBS_JOBID
9830 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9831         JOBENV=LOADL_STEP_ID
9832 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9833         JOBENV=JOB_ID
9834 else
9835         JOBENV=FAKE_JOBID
9836 fi
9837
9838 verify_jobstats() {
9839         local cmd=$1
9840         local target=$2
9841
9842         # clear old jobstats
9843         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9844         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9845
9846         # use a new JobID for this test, or we might see an old one
9847         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9848
9849         JOBVAL=${!JOBENV}
9850         log "Test: $cmd"
9851         log "Using JobID environment variable $JOBENV=$JOBVAL"
9852
9853         if [ $JOBENV = "FAKE_JOBID" ]; then
9854                 FAKE_JOBID=$JOBVAL $cmd
9855         else
9856                 $cmd
9857         fi
9858
9859         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9860                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9861                 do_facet $FACET lctl get_param mdt.*.job_stats |
9862                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9863         fi
9864         if [ "$target" = "ost" -o "$target" = "both" ]; then
9865                 FACET=ost1
9866                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9867                         grep $JOBVAL || error "No job stats found on OST $FACET"
9868         fi
9869 }
9870
9871 test_205() { # Job stats
9872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9873         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9874                 skip "Server doesn't support jobstats" && return 0
9875
9876         local cmd
9877         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9878         if [ $OLD_JOBENV != $JOBENV ]; then
9879                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9880                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9881                         $JOBENV || return 1
9882         fi
9883
9884         # mkdir
9885         cmd="mkdir $DIR/$tfile"
9886         verify_jobstats "$cmd" "mdt"
9887         # rmdir
9888         cmd="rm -fr $DIR/$tfile"
9889         verify_jobstats "$cmd" "mdt"
9890         # mknod
9891         cmd="mknod $DIR/$tfile c 1 3"
9892         verify_jobstats "$cmd" "mdt"
9893         # unlink
9894         cmd="rm -f $DIR/$tfile"
9895         verify_jobstats "$cmd" "mdt"
9896         # open & close
9897         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9898         verify_jobstats "$cmd" "mdt"
9899         # setattr
9900         cmd="touch $DIR/$tfile"
9901         verify_jobstats "$cmd" "both"
9902         # write
9903         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9904         verify_jobstats "$cmd" "ost"
9905         # read
9906         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9907         verify_jobstats "$cmd" "ost"
9908         # truncate
9909         cmd="$TRUNCATE $DIR/$tfile 0"
9910         verify_jobstats "$cmd" "both"
9911         # rename
9912         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9913         verify_jobstats "$cmd" "mdt"
9914
9915         # cleanup
9916         rm -f $DIR/jobstats_test_rename
9917
9918         if [ $OLD_JOBENV != $JOBENV ]; then
9919                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9920                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9921                         $OLD_JOBENV || return 1
9922         fi
9923 }
9924 run_test 205 "Verify job stats"
9925
9926 # LU-1480, LU-1773 and LU-1657
9927 test_206() {
9928         mkdir -p $DIR/$tdir
9929         lfs setstripe -c -1 $DIR/$tdir
9930 #define OBD_FAIL_LOV_INIT 0x1403
9931         $LCTL set_param fail_loc=0xa0001403
9932         $LCTL set_param fail_val=1
9933         touch $DIR/$tdir/$tfile || true
9934 }
9935 run_test 206 "fail lov_init_raid0() doesn't lbug"
9936
9937 test_207a() {
9938         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9939         local fsz=`stat -c %s $DIR/$tfile`
9940         cancel_lru_locks mdc
9941
9942         # do not return layout in getattr intent
9943 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
9944         $LCTL set_param fail_loc=0x170
9945         local sz=`stat -c %s $DIR/$tfile`
9946
9947         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
9948
9949         rm -rf $DIR/$tfile
9950 }
9951 run_test 207a "can refresh layout at glimpse"
9952
9953 test_207b() {
9954         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9955         local cksum=`md5sum $DIR/$tfile`
9956         local fsz=`stat -c %s $DIR/$tfile`
9957         cancel_lru_locks mdc
9958         cancel_lru_locks osc
9959
9960         # do not return layout in getattr intent
9961 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
9962         $LCTL set_param fail_loc=0x171
9963
9964         # it will refresh layout after the file is opened but before read issues
9965         echo checksum is "$cksum"
9966         echo "$cksum" |md5sum -c --quiet || error "file differs"
9967
9968         rm -rf $DIR/$tfile
9969 }
9970 run_test 207b "can refresh layout at open"
9971
9972 test_212() {
9973         size=`date +%s`
9974         size=$((size % 8192 + 1))
9975         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9976         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9977         rm -f $DIR/f212 $DIR/f212.xyz
9978 }
9979 run_test 212 "Sendfile test ============================================"
9980
9981 test_213() {
9982         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9983         cancel_lru_locks osc
9984         lctl set_param fail_loc=0x8000040f
9985         # generate a read lock
9986         cat $DIR/$tfile > /dev/null
9987         # write to the file, it will try to cancel the above read lock.
9988         cat /etc/hosts >> $DIR/$tfile
9989 }
9990 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9991
9992 test_214() { # for bug 20133
9993         test_mkdir -p $DIR/d214p/d214c
9994         for (( i=0; i < 340; i++ )) ; do
9995                 touch $DIR/d214p/d214c/a$i
9996         done
9997
9998         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9999         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
10000         ls $DIR/d214c || error "ls $DIR/d214c failed"
10001         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
10002 }
10003 run_test 214 "hash-indexed directory test - bug 20133"
10004
10005 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
10006 create_lnet_proc_files() {
10007         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
10008         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
10009
10010         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
10011         rm -f "$TMP/lnet_$1.sys_tmp"
10012 }
10013
10014 # counterpart of create_lnet_proc_files
10015 remove_lnet_proc_files() {
10016         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
10017 }
10018
10019 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10020 # 3rd arg as regexp for body
10021 check_lnet_proc_stats() {
10022         local l=$(cat "$TMP/lnet_$1" |wc -l)
10023         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
10024
10025         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
10026 }
10027
10028 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10029 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
10030 # optional and can be regexp for 2nd line (lnet.routes case)
10031 check_lnet_proc_entry() {
10032         local blp=2            # blp stands for 'position of 1st line of body'
10033         [ "$5" = "" ] || blp=3 # lnet.routes case
10034
10035         local l=$(cat "$TMP/lnet_$1" |wc -l)
10036         # subtracting one from $blp because the body can be empty
10037         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
10038
10039         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
10040                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
10041
10042         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
10043                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
10044
10045         # bail out if any unexpected line happened
10046         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
10047         [ "$?" != 0 ] || error "$2 misformatted"
10048 }
10049
10050 test_215() { # for bugs 18102, 21079, 21517
10051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10052         local N='(0|[1-9][0-9]*)'       # non-negative numeric
10053         local P='[1-9][0-9]*'           # positive numeric
10054         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
10055         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
10056         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
10057         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
10058
10059         local L1 # regexp for 1st line
10060         local L2 # regexp for 2nd line (optional)
10061         local BR # regexp for the rest (body)
10062
10063         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
10064         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
10065         create_lnet_proc_files "stats"
10066         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
10067         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
10068         remove_lnet_proc_files "stats"
10069
10070         # /proc/sys/lnet/routes should look like this:
10071         # Routing disabled/enabled
10072         # net hops state router
10073         # where net is a string like tcp0, hops >= 0, state is up/down,
10074         # router is a string like 192.168.1.1@tcp2
10075         L1="^Routing (disabled|enabled)$"
10076         L2="^net +hops +state +router$"
10077         BR="^$NET +$N +(up|down) +$NID$"
10078         create_lnet_proc_files "routes"
10079         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
10080         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
10081         remove_lnet_proc_files "routes"
10082
10083         # /proc/sys/lnet/routers should look like this:
10084         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
10085         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
10086         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
10087         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
10088         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
10089         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
10090         create_lnet_proc_files "routers"
10091         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
10092         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
10093         remove_lnet_proc_files "routers"
10094
10095         # /proc/sys/lnet/peers should look like this:
10096         # nid refs state last max rtr min tx min queue
10097         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
10098         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
10099         # numeric (0 or >0 or <0), queue >= 0.
10100         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
10101         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
10102         create_lnet_proc_files "peers"
10103         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
10104         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
10105         remove_lnet_proc_files "peers"
10106
10107         # /proc/sys/lnet/buffers  should look like this:
10108         # pages count credits min
10109         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
10110         L1="^pages +count +credits +min$"
10111         BR="^ +$N +$N +$I +$I$"
10112         create_lnet_proc_files "buffers"
10113         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
10114         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
10115         remove_lnet_proc_files "buffers"
10116
10117         # /proc/sys/lnet/nis should look like this:
10118         # nid status alive refs peer rtr max tx min
10119         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
10120         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
10121         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
10122         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
10123         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
10124         create_lnet_proc_files "nis"
10125         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
10126         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
10127         remove_lnet_proc_files "nis"
10128
10129         # can we successfully write to /proc/sys/lnet/stats?
10130         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
10131         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
10132 }
10133 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
10134
10135 test_216() { # bug 20317
10136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10137         remote_ost_nodsh && skip "remote OST with nodsh" && return
10138         local node
10139         local p="$TMP/sanityN-$TESTNAME.parameters"
10140         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
10141         for node in $(osts_nodes); do
10142                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
10143                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
10144                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
10145         done
10146         clear_osc_stats
10147
10148         # agressive lockless i/o settings
10149         for node in $(osts_nodes); do
10150                 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'
10151         done
10152         lctl set_param -n osc.*.contention_seconds 60
10153
10154         $DIRECTIO write $DIR/$tfile 0 10 4096
10155         $CHECKSTAT -s 40960 $DIR/$tfile
10156
10157         # disable lockless i/o
10158         for node in $(osts_nodes); do
10159                 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'
10160         done
10161         lctl set_param -n osc.*.contention_seconds 0
10162         clear_osc_stats
10163
10164         dd if=/dev/zero of=$DIR/$tfile count=0
10165         $CHECKSTAT -s 0 $DIR/$tfile
10166
10167         restore_lustre_params <$p
10168         rm -f $p
10169         rm $DIR/$tfile
10170 }
10171 run_test 216 "check lockless direct write works and updates file size and kms correctly"
10172
10173 test_217() { # bug 22430
10174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10175         local node
10176         local nid
10177
10178         for node in $(nodes_list); do
10179                 nid=$(host_nids_address $node $NETTYPE)
10180                 if [[ $nid = *-* ]] ; then
10181                         echo "lctl ping $nid@$NETTYPE"
10182                         lctl ping $nid@$NETTYPE
10183                 else
10184                         echo "skipping $node (no hyphen detected)"
10185                 fi
10186         done
10187 }
10188 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
10189
10190 test_218() {
10191        # do directio so as not to populate the page cache
10192        log "creating a 10 Mb file"
10193        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10194        log "starting reads"
10195        dd if=$DIR/$tfile of=/dev/null bs=4096 &
10196        log "truncating the file"
10197        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10198        log "killing dd"
10199        kill %+ || true # reads might have finished
10200        echo "wait until dd is finished"
10201        wait
10202        log "removing the temporary file"
10203        rm -rf $DIR/$tfile || error "tmp file removal failed"
10204 }
10205 run_test 218 "parallel read and truncate should not deadlock ======================="
10206
10207 test_219() {
10208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10209         # write one partial page
10210         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
10211         # set no grant so vvp_io_commit_write will do sync write
10212         $LCTL set_param fail_loc=0x411
10213         # write a full page at the end of file
10214         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
10215
10216         $LCTL set_param fail_loc=0
10217         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
10218         $LCTL set_param fail_loc=0x411
10219         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
10220 }
10221 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
10222
10223 test_220() { #LU-325
10224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10225         remote_ost_nodsh && skip "remote OST with nodsh" && return
10226         local OSTIDX=0
10227
10228         test_mkdir -p $DIR/$tdir
10229         local OST=$(lfs osts | grep ${OSTIDX}": " | \
10230                 awk '{print $2}' | sed -e 's/_UUID$//')
10231
10232         # on the mdt's osc
10233         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
10234         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
10235                         osc.$mdtosc_proc1.prealloc_last_id)
10236         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
10237                         osc.$mdtosc_proc1.prealloc_next_id)
10238
10239         $LFS df -i
10240
10241         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
10242         #define OBD_FAIL_OST_ENOINO              0x229
10243         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
10244         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
10245         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
10246
10247         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
10248
10249         MDSOBJS=$((last_id - next_id))
10250         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
10251
10252         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
10253         echo "OST still has $count kbytes free"
10254
10255         echo "create $MDSOBJS files @next_id..."
10256         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
10257
10258         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10259                         osc.$mdtosc_proc1.prealloc_last_id)
10260         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10261                         osc.$mdtosc_proc1.prealloc_next_id)
10262
10263         echo "after creation, last_id=$last_id2, next_id=$next_id2"
10264         $LFS df -i
10265
10266         echo "cleanup..."
10267
10268         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
10269         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
10270
10271         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
10272         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
10273         echo "unlink $MDSOBJS files @$next_id..."
10274         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
10275 }
10276 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
10277
10278 test_221() {
10279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10280         dd if=`which date` of=$MOUNT/date oflag=sync
10281         chmod +x $MOUNT/date
10282
10283         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
10284         $LCTL set_param fail_loc=0x80001401
10285
10286         $MOUNT/date > /dev/null
10287         rm -f $MOUNT/date
10288 }
10289 run_test 221 "make sure fault and truncate race to not cause OOM"
10290
10291 test_222a () {
10292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10293        rm -rf $DIR/$tdir
10294        test_mkdir -p $DIR/$tdir
10295        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10296        createmany -o $DIR/$tdir/$tfile 10
10297        cancel_lru_locks mdc
10298        cancel_lru_locks osc
10299        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10300        $LCTL set_param fail_loc=0x31a
10301        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
10302        $LCTL set_param fail_loc=0
10303        rm -r $DIR/$tdir
10304 }
10305 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
10306
10307 test_222b () {
10308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10309        rm -rf $DIR/$tdir
10310        test_mkdir -p $DIR/$tdir
10311        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10312        createmany -o $DIR/$tdir/$tfile 10
10313        cancel_lru_locks mdc
10314        cancel_lru_locks osc
10315        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10316        $LCTL set_param fail_loc=0x31a
10317        rm -r $DIR/$tdir || "AGL for rmdir failed"
10318        $LCTL set_param fail_loc=0
10319 }
10320 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
10321
10322 test_223 () {
10323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10324        rm -rf $DIR/$tdir
10325        test_mkdir -p $DIR/$tdir
10326        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10327        createmany -o $DIR/$tdir/$tfile 10
10328        cancel_lru_locks mdc
10329        cancel_lru_locks osc
10330        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
10331        $LCTL set_param fail_loc=0x31b
10332        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
10333        $LCTL set_param fail_loc=0
10334        rm -r $DIR/$tdir
10335 }
10336 run_test 223 "osc reenqueue if without AGL lock granted ======================="
10337
10338 test_224a() { # LU-1039, MRP-303
10339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10340         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
10341         $LCTL set_param fail_loc=0x508
10342         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
10343         $LCTL set_param fail_loc=0
10344         df $DIR
10345 }
10346 run_test 224a "Don't panic on bulk IO failure"
10347
10348 test_224b() { # LU-1039, MRP-303
10349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10350         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
10351         cancel_lru_locks osc
10352         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
10353         $LCTL set_param fail_loc=0x515
10354         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
10355         $LCTL set_param fail_loc=0
10356         df $DIR
10357 }
10358 run_test 224b "Don't panic on bulk IO failure"
10359
10360 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
10361 test_225a () {
10362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10363        if [ -z ${MDSSURVEY} ]; then
10364               skip_env "mds-survey not found" && return
10365        fi
10366        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10367             { skip "Need MDS version at least 2.2.51"; return; }
10368
10369        local mds=$(facet_host $SINGLEMDS)
10370        local target=$(do_nodes $mds 'lctl dl' | \
10371                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10372
10373        local cmd1="file_count=1000 thrhi=4"
10374        local cmd2="dir_count=2 layer=mdd stripe_count=0"
10375        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10376        local cmd="$cmd1 $cmd2 $cmd3"
10377
10378        rm -f ${TMP}/mds_survey*
10379        echo + $cmd
10380        eval $cmd || error "mds-survey with zero-stripe failed"
10381        cat ${TMP}/mds_survey*
10382        rm -f ${TMP}/mds_survey*
10383 }
10384 run_test 225a "Metadata survey sanity with zero-stripe"
10385
10386 test_225b () {
10387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10388        if [ -z ${MDSSURVEY} ]; then
10389               skip_env "mds-survey not found" && return
10390        fi
10391        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10392             { skip "Need MDS version at least 2.2.51"; return; }
10393
10394        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
10395               skip_env "Need to mount OST to test" && return
10396        fi
10397
10398        local mds=$(facet_host $SINGLEMDS)
10399        local target=$(do_nodes $mds 'lctl dl' | \
10400                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10401
10402        local cmd1="file_count=1000 thrhi=4"
10403        local cmd2="dir_count=2 layer=mdd stripe_count=1"
10404        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10405        local cmd="$cmd1 $cmd2 $cmd3"
10406
10407        rm -f ${TMP}/mds_survey*
10408        echo + $cmd
10409        eval $cmd || error "mds-survey with stripe_count failed"
10410        cat ${TMP}/mds_survey*
10411        rm -f ${TMP}/mds_survey*
10412 }
10413 run_test 225b "Metadata survey sanity with stripe_count = 1"
10414
10415 mcreate_path2fid () {
10416         local mode=$1
10417         local major=$2
10418         local minor=$3
10419         local name=$4
10420         local desc=$5
10421         local path=$DIR/$tdir/$name
10422         local fid
10423         local rc
10424         local fid_path
10425
10426         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10427                 error "cannot create $desc"
10428
10429         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
10430         rc=$?
10431         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10432
10433         fid_path=$($LFS fid2path $MOUNT $fid)
10434         rc=$?
10435         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10436
10437         [ "$path" == "$fid_path" ] ||
10438                 error "fid2path returned $fid_path, expected $path"
10439
10440         echo "pass with $path and $fid"
10441 }
10442
10443 test_226 () {
10444         rm -rf $DIR/$tdir
10445         mkdir -p $DIR/$tdir
10446
10447         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10448         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10449         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10450         mcreate_path2fid 0040666 0 0 dir "directory"
10451         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10452         mcreate_path2fid 0100666 0 0 file "regular file"
10453         mcreate_path2fid 0120666 0 0 link "symbolic link"
10454         mcreate_path2fid 0140666 0 0 sock "socket"
10455 }
10456 run_test 226 "call path2fid and fid2path on files of all type"
10457
10458 # LU-1299 Executing or running ldd on a truncated executable does not
10459 # cause an out-of-memory condition.
10460 test_227() {
10461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10462         dd if=`which date` of=$MOUNT/date bs=1k count=1
10463         chmod +x $MOUNT/date
10464
10465         $MOUNT/date > /dev/null
10466         ldd $MOUNT/date > /dev/null
10467         rm -f $MOUNT/date
10468 }
10469 run_test 227 "running truncated executable does not cause OOM"
10470
10471 # LU-1512 try to reuse idle OI blocks
10472 test_228a() {
10473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10474         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10475                 skip "non-ldiskfs backend" && return
10476
10477         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10478         local myDIR=$DIR/$tdir
10479
10480         mkdir -p $myDIR
10481         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10482         $LCTL set_param fail_loc=0x80001002
10483         createmany -o $myDIR/t- 10000
10484         $LCTL set_param fail_loc=0
10485         # The guard is current the largest FID holder
10486         touch $myDIR/guard
10487         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10488                     tr -d '[')
10489         local IDX=$(($SEQ % 64))
10490
10491         do_facet $SINGLEMDS sync
10492         # Make sure journal flushed.
10493         sleep 6
10494         local blk1=$(do_facet $SINGLEMDS \
10495                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10496                      grep Blockcount | awk '{print $4}')
10497
10498         # Remove old files, some OI blocks will become idle.
10499         unlinkmany $myDIR/t- 10000
10500         # Create new files, idle OI blocks should be reused.
10501         createmany -o $myDIR/t- 2000
10502         do_facet $SINGLEMDS sync
10503         # Make sure journal flushed.
10504         sleep 6
10505         local blk2=$(do_facet $SINGLEMDS \
10506                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10507                      grep Blockcount | awk '{print $4}')
10508
10509         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10510 }
10511 run_test 228a "try to reuse idle OI blocks"
10512
10513 test_228b() {
10514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10515         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10516                 skip "non-ldiskfs backend" && return
10517
10518         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10519         local myDIR=$DIR/$tdir
10520
10521         mkdir -p $myDIR
10522         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10523         $LCTL set_param fail_loc=0x80001002
10524         createmany -o $myDIR/t- 10000
10525         $LCTL set_param fail_loc=0
10526         # The guard is current the largest FID holder
10527         touch $myDIR/guard
10528         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10529                     tr -d '[')
10530         local IDX=$(($SEQ % 64))
10531
10532         do_facet $SINGLEMDS sync
10533         # Make sure journal flushed.
10534         sleep 6
10535         local blk1=$(do_facet $SINGLEMDS \
10536                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10537                      grep Blockcount | awk '{print $4}')
10538
10539         # Remove old files, some OI blocks will become idle.
10540         unlinkmany $myDIR/t- 10000
10541
10542         # stop the MDT
10543         stop $SINGLEMDS || error "Fail to stop MDT."
10544         # remount the MDT
10545         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10546
10547         df $MOUNT || error "Fail to df."
10548         # Create new files, idle OI blocks should be reused.
10549         createmany -o $myDIR/t- 2000
10550         do_facet $SINGLEMDS sync
10551         # Make sure journal flushed.
10552         sleep 6
10553         local blk2=$(do_facet $SINGLEMDS \
10554                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10555                      grep Blockcount | awk '{print $4}')
10556
10557         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10558 }
10559 run_test 228b "idle OI blocks can be reused after MDT restart"
10560
10561 #LU-1881
10562 test_228c() {
10563         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10564         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10565                 skip "non-ldiskfs backend" && return
10566
10567         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10568         local myDIR=$DIR/$tdir
10569
10570         mkdir -p $myDIR
10571         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10572         $LCTL set_param fail_loc=0x80001002
10573         # 20000 files can guarantee there are index nodes in the OI file
10574         createmany -o $myDIR/t- 20000
10575         $LCTL set_param fail_loc=0
10576         # The guard is current the largest FID holder
10577         touch $myDIR/guard
10578         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10579                     tr -d '[')
10580         local IDX=$(($SEQ % 64))
10581
10582         do_facet $SINGLEMDS sync
10583         # Make sure journal flushed.
10584         sleep 6
10585         local blk1=$(do_facet $SINGLEMDS \
10586                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10587                      grep Blockcount | awk '{print $4}')
10588
10589         # Remove old files, some OI blocks will become idle.
10590         unlinkmany $myDIR/t- 20000
10591         rm -f $myDIR/guard
10592         # The OI file should become empty now
10593
10594         # Create new files, idle OI blocks should be reused.
10595         createmany -o $myDIR/t- 2000
10596         do_facet $SINGLEMDS sync
10597         # Make sure journal flushed.
10598         sleep 6
10599         local blk2=$(do_facet $SINGLEMDS \
10600                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10601                      grep Blockcount | awk '{print $4}')
10602
10603         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10604 }
10605 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10606
10607 test_230a() {
10608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10609         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10610         local MDTIDX=1
10611
10612         mkdir -p $DIR/$tdir/test_230_local
10613         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10614         [ $mdt_idx -ne 0 ] &&
10615                 error "create local directory on wrong MDT $mdt_idx"
10616
10617         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10618                         error "create remote directory failed"
10619         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10620         [ $mdt_idx -ne $MDTIDX ] &&
10621                 error "create remote directory on wrong MDT $mdt_idx"
10622
10623         createmany -o $DIR/$tdir/test_230/t- 10 ||
10624                 error "create files on remote directory failed"
10625         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10626         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10627         rm -r $DIR/$tdir || error "unlink remote directory failed"
10628 }
10629 run_test 230a "Create remote directory and files under the remote directory"
10630
10631 test_230b() {
10632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10633         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10634         local MDTIDX=1
10635         local remote_dir=$DIR/$tdir/remote_dir
10636         local rc=0
10637
10638         mkdir -p $DIR/$tdir
10639         $LFS mkdir -i $MDTIDX $remote_dir ||
10640                 error "create remote directory failed"
10641
10642         $LFS mkdir -i 0 $remote_dir/new_dir &&
10643                 error "nested remote directory create succeed!"
10644
10645         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=1
10646         $LFS mkdir -i 0 $remote_dir/new_dir || rc=$?
10647         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=0
10648
10649         [ $rc -ne 0 ] &&
10650            error "create remote directory failed after set enable_remote_dir"
10651
10652         rm -r $DIR/$tdir || error "unlink remote directory failed"
10653 }
10654 run_test 230b "nested remote directory should be failed"
10655
10656 test_231a()
10657 {
10658         # For simplicity this test assumes that max_pages_per_rpc
10659         # is the same across all OSCs
10660         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
10661         local bulk_size=$((max_pages * 4096))
10662
10663         mkdir -p $DIR/$tdir
10664
10665         # clear the OSC stats
10666         $LCTL set_param osc.*.stats=0 &>/dev/null
10667
10668         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
10669         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
10670                 oflag=direct &>/dev/null || error "dd failed"
10671
10672         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
10673         if [ x$nrpcs != "x1" ]; then
10674                 error "found $nrpc ost_write RPCs, not 1 as expected"
10675         fi
10676
10677         # Drop the OSC cache, otherwise we will read from it
10678         cancel_lru_locks osc
10679
10680         # clear the OSC stats
10681         $LCTL set_param osc.*.stats=0 &>/dev/null
10682
10683         # Client reads $bulk_size.
10684         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
10685                 iflag=direct &>/dev/null || error "dd failed"
10686
10687         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
10688         if [ x$nrpcs != "x1" ]; then
10689                 error "found $nrpc ost_read RPCs, not 1 as expected"
10690         fi
10691 }
10692 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
10693
10694 test_231b() {
10695         mkdir -p $DIR/$tdir
10696         local i
10697         for i in {0..1023}; do
10698                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
10699                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
10700                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
10701         done
10702         sync
10703 }
10704 run_test 231b "must not assert on fully utilized OST request buffer"
10705
10706 #
10707 # tests that do cleanup/setup should be run at the end
10708 #
10709
10710 test_900() {
10711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10712         local ls
10713         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
10714         $LCTL set_param fail_loc=0x903
10715         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
10716         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
10717                 echo "clear" > $ls
10718         done
10719         FAIL_ON_ERROR=true cleanup
10720         FAIL_ON_ERROR=true setup
10721 }
10722 run_test 900 "umount should not race with any mgc requeue thread"
10723
10724 complete $SECONDS
10725 check_and_cleanup_lustre
10726 if [ "$I_MOUNTED" != "yes" ]; then
10727         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
10728 fi
10729 exit_status