Whamcloud - gitweb
0d03287e0e0457fae5b21deae87c48176f3b439d
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 4; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                27u   42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19
20 # Tests that fail on uml
21 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
22 #                                    buffer i/o errs             sock spc runas
23 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
24
25 SRCDIR=$(cd $(dirname $0); echo $PWD)
26 export PATH=$PATH:/sbin
27
28 TMP=${TMP:-/tmp}
29
30 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
31 CREATETEST=${CREATETEST:-createtest}
32 LFS=${LFS:-lfs}
33 LFIND=${LFIND:-"$LFS find"}
34 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
35 LCTL=${LCTL:-lctl}
36 MCREATE=${MCREATE:-mcreate}
37 OPENFILE=${OPENFILE:-openfile}
38 OPENUNLINK=${OPENUNLINK:-openunlink}
39 export MULTIOP=${MULTIOP:-multiop}
40 READS=${READS:-"reads"}
41 MUNLINK=${MUNLINK:-munlink}
42 SOCKETSERVER=${SOCKETSERVER:-socketserver}
43 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
44 MEMHOG=${MEMHOG:-memhog}
45 DIRECTIO=${DIRECTIO:-directio}
46 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
47 UMOUNT=${UMOUNT:-"umount -d"}
48 STRIPES_PER_OBJ=-1
49 CHECK_GRANT=${CHECK_GRANT:-"yes"}
50 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
51 export PARALLEL=${PARALLEL:-"no"}
52
53 export NAME=${NAME:-local}
54
55 SAVE_PWD=$PWD
56
57 CLEANUP=${CLEANUP:-:}
58 SETUP=${SETUP:-:}
59 TRACE=${TRACE:-""}
60 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
61 . $LUSTRE/tests/test-framework.sh
62 init_test_env $@
63 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
64 init_logging
65
66 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
67
68 FAIL_ON_ERROR=false
69
70 cleanup() {
71         echo -n "cln.."
72         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
73         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
74 }
75 setup() {
76         echo -n "mnt.."
77         load_modules
78         setupall || exit 10
79         echo "done"
80 }
81
82 check_kernel_version() {
83         WANT_VER=$1
84         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
85         case $GOT_VER in
86         patchless|patchless_client) return 0;;
87         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
88         esac
89         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
90         return 1
91 }
92
93 if [ "$ONLY" == "cleanup" ]; then
94        sh llmountcleanup.sh
95        exit 0
96 fi
97
98 check_and_setup_lustre
99
100 DIR=${DIR:-$MOUNT}
101 assert_DIR
102
103 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
104     awk '{gsub(/_UUID/,""); print $1}' | head -1)
105 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
106 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
107 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
108 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
109 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
110 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
111
112 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
113 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
114 rm -rf $DIR/[Rdfs][0-9]*
115
116 # $RUNAS_ID may get set incorrectly somewhere else
117 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
118
119 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
120
121 build_test_filter
122
123 if [ "${ONLY}" = "MOUNT" ] ; then
124         echo "Lustre is up, please go on"
125         exit
126 fi
127
128 echo "preparing for tests involving mounts"
129 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
130 touch $EXT2_DEV
131 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
132 echo # add a newline after mke2fs.
133
134 umask 077
135
136 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
137 lctl set_param debug=-1 2> /dev/null || true
138 test_0() {
139         touch $DIR/$tfile
140         $CHECKSTAT -t file $DIR/$tfile || error
141         rm $DIR/$tfile
142         $CHECKSTAT -a $DIR/$tfile || error
143 }
144 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
145
146 test_0b() {
147         chmod 0755 $DIR || error
148         $CHECKSTAT -p 0755 $DIR || error
149 }
150 run_test 0b "chmod 0755 $DIR ============================="
151
152 test_0c() {
153     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
154     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
155 }
156 run_test 0c "check import proc ============================="
157
158 test_1a() {
159         test_mkdir -p $DIR/$tdir
160         test_mkdir -p $DIR/$tdir/d2
161         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
162         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
163 }
164 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
165
166 test_1b() {
167         rmdir $DIR/$tdir/d2
168         rmdir $DIR/$tdir
169         $CHECKSTAT -a $DIR/$tdir || error
170 }
171 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
172
173 test_2a() {
174         test_mkdir $DIR/$tdir
175         touch $DIR/$tdir/$tfile
176         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
177 }
178 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
179
180 test_2b() {
181         rm -r $DIR/$tdir
182         $CHECKSTAT -a $DIR/$tdir || error
183 }
184 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
185
186 test_3a() {
187         test_mkdir -p $DIR/$tdir
188         $CHECKSTAT -t dir $DIR/$tdir || error
189 }
190 run_test 3a "mkdir .../d3 ======================================"
191
192 test_3b() {
193         if [ ! -d $DIR/$tdir ]; then
194                 mkdir $DIR/$tdir
195         fi
196         touch $DIR/$tdir/$tfile
197         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
198 }
199 run_test 3b "touch .../d3/f ===================================="
200
201 test_3c() {
202         rm -r $DIR/$tdir
203         $CHECKSTAT -a $DIR/$tdir || error
204 }
205 run_test 3c "rm -r .../d3 ======================================"
206
207 test_4a() {
208         test_mkdir -p $DIR/$tdir
209         $CHECKSTAT -t dir $DIR/$tdir || error
210 }
211 run_test 4a "mkdir .../d4 ======================================"
212
213 test_4b() {
214         if [ ! -d $DIR/$tdir ]; then
215                 test_mkdir $DIR/$tdir
216         fi
217         test_mkdir $DIR/$tdir/d2
218         mkdir $DIR/$tdir/d2
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
220 }
221 run_test 4b "mkdir .../d4/d2 ==================================="
222
223 test_5() {
224         test_mkdir $DIR/$tdir
225         test_mkdir $DIR/$tdir/d2
226         chmod 0707 $DIR/$tdir/d2
227         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error
228 }
229 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
230
231 test_6a() {
232         touch $DIR/$tfile
233         chmod 0666 $DIR/$tfile || error
234         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
235 }
236 run_test 6a "touch .../f6a; chmod .../f6a ======================"
237
238 test_6b() {
239         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
240         if [ ! -f $DIR/$tfile ]; then
241                 touch $DIR/$tfile
242                 chmod 0666 $DIR/$tfile
243         fi
244         $RUNAS chmod 0444 $DIR/$tfile && error
245         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
246 }
247 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
248
249 test_6c() {
250         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
251         touch $DIR/$tfile
252         chown $RUNAS_ID $DIR/$tfile || error
253         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
254 }
255 run_test 6c "touch .../f6c; chown .../f6c ======================"
256
257 test_6d() {
258         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
259         if [ ! -f $DIR/$tfile ]; then
260                 touch $DIR/$tfile
261                 chown $RUNAS_ID $DIR/$tfile
262         fi
263         $RUNAS chown $UID $DIR/$tfile && error
264         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
265 }
266 run_test 6d "$RUNAS chown .../f6c (should return error) =="
267
268 test_6e() {
269         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
270         touch $DIR/$tfile
271         chgrp $RUNAS_ID $DIR/$tfile || error
272         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
273 }
274 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
275
276 test_6f() {
277         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
278         if [ ! -f $DIR/$tfile ]; then
279                 touch $DIR/$tfile
280                 chgrp $RUNAS_ID $DIR/$tfile
281         fi
282         $RUNAS chgrp $UID $DIR/$tfile && error
283         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
284 }
285 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
286
287 test_6g() {
288         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
289         test_mkdir $DIR/$tdir || error
290         chmod 777 $DIR/$tdir || error
291         $RUNAS mkdir $DIR/$tdir/d || error
292         chmod g+s $DIR/$tdir/d || error
293         test_mkdir $DIR/$tdir/d/subdir
294         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir || error
295 }
296 run_test 6g "Is new dir in sgid dir inheriting group?"
297
298 test_6h() { # bug 7331
299         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
300         touch $DIR/$tfile || error "touch failed"
301         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
302         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
303                 error "chown worked"
304         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile || error
305 }
306 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
307
308 test_7a() {
309         test_mkdir $DIR/$tdir
310         $MCREATE $DIR/$tdir/$tfile
311         chmod 0666 $DIR/$tdir/$tfile
312         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
313 }
314 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
315
316 test_7b() {
317         if [ ! -d $DIR/$tdir ]; then
318                 mkdir $DIR/$tdir
319         fi
320         $MCREATE $DIR/$tdir/$tfile
321         echo -n foo > $DIR/$tdir/$tfile
322         [ "`cat $DIR/$tdir/$tfile`" = "foo" ] || error
323         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error
324 }
325 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
326
327 test_8() {
328         test_mkdir $DIR/$tdir
329         touch $DIR/$tdir/$tfile
330         chmod 0666 $DIR/$tdir/$tfile
331         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
332 }
333 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
334
335 test_9() {
336         test_mkdir $DIR/$tdir
337         test_mkdir $DIR/$tdir/d2
338         test_mkdir $DIR/$tdir/d2/d3
339         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error
340 }
341 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
342
343 test_10() {
344         test_mkdir $DIR/$tdir
345         test_mkdir $DIR/$tdir/d2
346         touch $DIR/$tdir/d2/$tfile
347         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile || error
348 }
349 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
350
351 test_11() {
352         test_mkdir $DIR/$tdir
353         test_mkdir $DIR/$tdir/d2
354         chmod 0666 $DIR/$tdir/d2
355         chmod 0705 $DIR/$tdir/d2
356         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 || error
357 }
358 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
359
360 test_12() {
361         test_mkdir $DIR/$tdir
362         touch $DIR/$tdir/$tfile
363         chmod 0666 $DIR/$tdir/$tfile
364         chmod 0654 $DIR/$tdir/$tfile
365         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile || error
366 }
367 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
368
369 test_13() {
370         test_mkdir $DIR/$tdir
371         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
372         >  $DIR/$tdir/$tfile
373         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile || error
374 }
375 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
376
377 test_14() {
378         test_mkdir $DIR/$tdir
379         touch $DIR/$tdir/$tfile
380         rm $DIR/$tdir/$tfile
381         $CHECKSTAT -a $DIR/$tdir/$tfile || error
382 }
383 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
384
385 test_15() {
386         test_mkdir $DIR/$tdir
387         touch $DIR/$tdir/$tfile
388         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
389         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 || error
390 }
391 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
392
393 test_16() {
394         test_mkdir $DIR/$tdir
395         touch $DIR/$tdir/$tfile
396         rm -rf $DIR/$tdir/$tfile
397         $CHECKSTAT -a $DIR/$tdir/$tfile || error
398 }
399 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
400
401 test_17a() {
402         test_mkdir -p $DIR/$tdir
403         touch $DIR/$tdir/$tfile
404         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
405         ls -l $DIR/$tdir
406         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist || error
407         $CHECKSTAT -f -t f $DIR/$tdir/l-exist || error
408         rm -f $DIR/$tdir/l-exist
409         $CHECKSTAT -a $DIR/$tdir/l-exist || error
410 }
411 run_test 17a "symlinks: create, remove (real) =================="
412
413 test_17b() {
414         test_mkdir -p $DIR/$tdir
415         ln -s no-such-file $DIR/$tdir/l-dangle
416         ls -l $DIR/$tdir
417         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle || error
418         $CHECKSTAT -fa $DIR/$tdir/l-dangle || error
419         rm -f $DIR/$tdir/l-dangle
420         $CHECKSTAT -a $DIR/$tdir/l-dangle || error
421 }
422 run_test 17b "symlinks: create, remove (dangling) =============="
423
424 test_17c() { # bug 3440 - don't save failed open RPC for replay
425         test_mkdir -p $DIR/$tdir
426         ln -s foo $DIR/$tdir/$tfile
427         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
428 }
429 run_test 17c "symlinks: open dangling (should return error) ===="
430
431 test_17d() {
432         test_mkdir -p $DIR/$tdir
433         ln -s foo $DIR/$tdir/$tfile
434         touch $DIR/$tdir/$tfile || error "creating to new symlink"
435 }
436 run_test 17d "symlinks: create dangling ========================"
437
438 test_17e() {
439         test_mkdir -p $DIR/$tdir
440         local foo=$DIR/$tdir/$tfile
441         ln -s $foo $foo || error "create symlink failed"
442         ls -l $foo || error "ls -l failed"
443         ls $foo && error "ls not failed" || true
444 }
445 run_test 17e "symlinks: create recursive symlink (should return error) ===="
446
447 test_17f() {
448         test_mkdir -p $DIR/d17f
449         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/d17f/666
455         ls -l  $DIR/d17f
456 }
457 run_test 17f "symlinks: long and very long symlink name ========================"
458
459 # str_repeat(S, N) generate a string that is string S repeated N times
460 str_repeat() {
461         local s=$1
462         local n=$2
463         local ret=''
464         while [ $((n -= 1)) -ge 0 ]; do
465                 ret=$ret$s
466         done
467         echo $ret
468 }
469
470 # Long symlinks and LU-2241
471 test_17g() {
472         test_mkdir -p $DIR/$tdir
473         local TESTS="59 60 61 4094 4095"
474
475         for i in $TESTS; do
476                 local SYMNAME=$(str_repeat 'x' $i)
477                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
478                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
479         done
480 }
481 run_test 17g "symlinks: really long symlink name and inode boundaries"
482
483 test_17h() { #bug 17378
484         remote_mds_nodsh && skip "remote MDS with nodsh" && return
485         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
486         local mdt_idx
487         test_mkdir -p $DIR/$tdir
488         if [ $MDSCOUNT -gt 1 ]; then
489                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
490         else
491                 mdt_idx=0
492         fi
493         $SETSTRIPE -c -1 $DIR/$tdir
494 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
495         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
496         touch $DIR/$tdir/$tfile || true
497 }
498 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
499
500 test_17i() { #bug 20018
501         remote_mds_nodsh && skip "remote MDS with nodsh" && return
502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
503         test_mkdir -p $DIR/$tdir
504         local foo=$DIR/$tdir/$tfile
505         local mdt_idx
506         if [ $MDSCOUNT -gt 1 ]; then
507                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
508         else
509                 mdt_idx=0
510         fi
511         ln -s $foo $foo || error "create symlink failed"
512 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
513         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
514         ls -l $foo && error "error not detected"
515         return 0
516 }
517 run_test 17i "don't panic on short symlink"
518
519 test_17k() { #bug 22301
520         rsync --help | grep -q xattr ||
521                 skip_env "$(rsync --version| head -1) does not support xattrs"
522         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
523         test_mkdir -p $DIR/$tdir
524         test_mkdir -p $DIR/$tdir.new
525         touch $DIR/$tdir/$tfile
526         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
527         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
528                 error "rsync failed with xattrs enabled"
529 }
530 run_test 17k "symlinks: rsync with xattrs enabled ========================="
531
532 test_17l() { # LU-279
533         mkdir -p $DIR/$tdir
534         touch $DIR/$tdir/$tfile
535         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
536         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
537                 # -h to not follow symlinks. -m '' to list all the xattrs.
538                 # grep to remove first line: '# file: $path'.
539                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
540                 do
541                         lgetxattr_size_check $path $xattr ||
542                                 error "lgetxattr_size_check $path $xattr failed"
543                 done
544         done
545 }
546 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
547
548 # LU-1540
549 test_17m() {
550         local short_sym="0123456789"
551         local WDIR=$DIR/${tdir}m
552         local mds_index
553         local devname
554         local cmd
555         local i
556         local rc=0
557
558         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
559         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
560                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
561
562         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
563                 skip "only for ldiskfs MDT" && return 0
564
565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
566
567         mkdir -p $WDIR
568         long_sym=$short_sym
569         # create a long symlink file
570         for ((i = 0; i < 4; ++i)); do
571                 long_sym=${long_sym}${long_sym}
572         done
573
574         echo "create 512 short and long symlink files under $WDIR"
575         for ((i = 0; i < 256; ++i)); do
576                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
577                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
578         done
579
580         echo "erase them"
581         rm -f $WDIR/*
582         sync
583         wait_delete_completed
584
585         echo "recreate the 512 symlink files with a shorter string"
586         for ((i = 0; i < 512; ++i)); do
587                 # rewrite the symlink file with a shorter string
588                 ln -sf ${long_sym} $WDIR/long-$i
589                 ln -sf ${short_sym} $WDIR/short-$i
590         done
591
592         mds_index=$($LFS getstripe -M $WDIR)
593         mds_index=$((mds_index+1))
594         devname=$(mdsdevname $mds_index)
595         cmd="$E2FSCK -fnvd $devname"
596
597         echo "stop and checking mds${mds_index}: $cmd"
598         # e2fsck should not return error
599         stop mds${mds_index} -f
600         do_facet mds${mds_index} $cmd || rc=$?
601
602         start mds${mds_index} $devname $MDS_MOUNT_OPTS
603         df $MOUNT > /dev/null 2>&1
604         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
605                 "short/long symlink MDT: rc=$rc"
606         return $rc
607 }
608 run_test 17m "run e2fsck against MDT which contains short/long symlink"
609
610 check_fs_consistency_17n() {
611         local mdt_index
612         local devname
613         local cmd
614         local rc=0
615
616         for mdt_index in $(seq 1 $MDSCOUNT); do
617                 devname=$(mdsdevname $mdt_index)
618                 cmd="$E2FSCK -fnvd $devname"
619
620                 echo "stop and checking mds${mdt_index}: $cmd"
621                 # e2fsck should not return error
622                 stop mds${mdt_index}
623                 do_facet mds${mdt_index} $cmd || rc=$?
624
625                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
626                 df $MOUNT > /dev/null 2>&1
627                 [ $rc -ne 0 ] && break
628         done
629         return $rc
630 }
631
632 test_17n() {
633         local i
634
635         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
636         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
637                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
638
639         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
640                 skip "only for ldiskfs MDT" && return 0
641
642         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
643
644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
645
646         mkdir -p $DIR/$tdir
647         for ((i=0; i<10; i++)); do
648                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
649                         error "create remote dir error $i"
650                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
651                         error "create files under remote dir failed $i"
652         done
653
654         check_fs_consistency_17n || error "e2fsck report error"
655
656         for ((i=0;i<10;i++)); do
657                 rm -rf $DIR/$tdir/remote_dir_${i} ||
658                         error "destroy remote dir error $i"
659         done
660
661         check_fs_consistency_17n || error "e2fsck report error"
662 }
663 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
664
665 test_18() {
666         touch $DIR/f
667         ls $DIR || error
668 }
669 run_test 18 "touch .../f ; ls ... =============================="
670
671 test_19a() {
672         touch $DIR/f19
673         ls -l $DIR
674         rm $DIR/f19
675         $CHECKSTAT -a $DIR/f19 || error
676 }
677 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
678
679 test_19b() {
680         ls -l $DIR/f19 && error || true
681 }
682 run_test 19b "ls -l .../f19 (should return error) =============="
683
684 test_19c() {
685         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
686         $RUNAS touch $DIR/f19 && error || true
687 }
688 run_test 19c "$RUNAS touch .../f19 (should return error) =="
689
690 test_19d() {
691         cat $DIR/f19 && error || true
692 }
693 run_test 19d "cat .../f19 (should return error) =============="
694
695 test_20() {
696         touch $DIR/f
697         rm $DIR/f
698         log "1 done"
699         touch $DIR/f
700         rm $DIR/f
701         log "2 done"
702         touch $DIR/f
703         rm $DIR/f
704         log "3 done"
705         $CHECKSTAT -a $DIR/f || error
706 }
707 run_test 20 "touch .../f ; ls -l ... ==========================="
708
709 test_21() {
710         test_mkdir $DIR/d21
711         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
712         ln -s dangle $DIR/d21/link
713         echo foo >> $DIR/d21/link
714         cat $DIR/d21/dangle
715         $CHECKSTAT -t link $DIR/d21/link || error
716         $CHECKSTAT -f -t file $DIR/d21/link || error
717 }
718 run_test 21 "write to dangling link ============================"
719
720 test_22() {
721         WDIR=$DIR/$tdir
722         test_mkdir -p $DIR/$tdir
723         chown $RUNAS_ID:$RUNAS_GID $WDIR
724         (cd $WDIR || error "cd $WDIR failed";
725         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
726         $RUNAS tar xf -)
727         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
728         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
729         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
730 }
731 run_test 22 "unpack tar archive as non-root user ==============="
732
733 # was test_23
734 test_23a() {
735         test_mkdir -p $DIR/$tdir
736         local file=$DIR/$tdir/$tfile
737
738         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
739         openfile -f O_CREAT:O_EXCL $file &&
740                 error "$file recreate succeeded" || true
741 }
742 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
743
744 test_23b() { # bug 18988
745         test_mkdir -p $DIR/$tdir
746         local file=$DIR/$tdir/$tfile
747
748         rm -f $file
749         echo foo > $file || error "write filed"
750         echo bar >> $file || error "append filed"
751         $CHECKSTAT -s 8 $file || error "wrong size"
752         rm $file
753 }
754 run_test 23b "O_APPEND check =========================="
755
756 test_24a() {
757         echo '== rename sanity =============================================='
758         echo '-- same directory rename'
759         test_mkdir $DIR/R1
760         touch $DIR/R1/f
761         mv $DIR/R1/f $DIR/R1/g
762         $CHECKSTAT -t file $DIR/R1/g || error
763 }
764 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
765
766 test_24b() {
767         test_mkdir $DIR/R2
768         touch $DIR/R2/{f,g}
769         mv $DIR/R2/f $DIR/R2/g
770         $CHECKSTAT -a $DIR/R2/f || error
771         $CHECKSTAT -t file $DIR/R2/g || error
772 }
773 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
774
775 test_24c() {
776         test_mkdir $DIR/R3
777         test_mkdir $DIR/R3/f
778         mv $DIR/R3/f $DIR/R3/g
779         $CHECKSTAT -a $DIR/R3/f || error
780         $CHECKSTAT -t dir $DIR/R3/g || error
781 }
782 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
783
784 test_24d() {
785         test_mkdir $DIR/R4
786         test_mkdir $DIR/R4/f
787         test_mkdir $DIR/R4/g
788         mrename $DIR/R4/f $DIR/R4/g
789         $CHECKSTAT -a $DIR/R4/f || error
790         $CHECKSTAT -t dir $DIR/R4/g || error
791 }
792 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
793
794 test_24e() {
795         echo '-- cross directory renames --'
796         test_mkdir $DIR/R5a
797         test_mkdir $DIR/R5b
798         touch $DIR/R5a/f
799         mv $DIR/R5a/f $DIR/R5b/g
800         $CHECKSTAT -a $DIR/R5a/f || error
801         $CHECKSTAT -t file $DIR/R5b/g || error
802 }
803 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
804
805 test_24f() {
806         test_mkdir $DIR/R6a
807         test_mkdir $DIR/R6b
808         touch $DIR/R6a/f $DIR/R6b/g
809         mv $DIR/R6a/f $DIR/R6b/g
810         $CHECKSTAT -a $DIR/R6a/f || error
811         $CHECKSTAT -t file $DIR/R6b/g || error
812 }
813 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
814
815 test_24g() {
816         test_mkdir $DIR/R7a
817         test_mkdir $DIR/R7b
818         test_mkdir $DIR/R7a/d
819         mv $DIR/R7a/d $DIR/R7b/e
820         $CHECKSTAT -a $DIR/R7a/d || error
821         $CHECKSTAT -t dir $DIR/R7b/e || error
822 }
823 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
824
825 test_24h() {
826         test_mkdir $DIR/R8a
827         test_mkdir $DIR/R8b
828         test_mkdir $DIR/R8a/d
829         test_mkdir $DIR/R8b/e
830         mrename $DIR/R8a/d $DIR/R8b/e
831         $CHECKSTAT -a $DIR/R8a/d || error
832         $CHECKSTAT -t dir $DIR/R8b/e || error
833 }
834 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
835
836 test_24i() {
837         echo "-- rename error cases"
838         test_mkdir $DIR/R9
839         test_mkdir $DIR/R9/a
840         touch $DIR/R9/f
841         mrename $DIR/R9/f $DIR/R9/a
842         $CHECKSTAT -t file $DIR/R9/f || error
843         $CHECKSTAT -t dir  $DIR/R9/a || error
844         $CHECKSTAT -a $DIR/R9/a/f || error
845 }
846 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
847
848 test_24j() {
849         test_mkdir $DIR/R10
850         mrename $DIR/R10/f $DIR/R10/g
851         $CHECKSTAT -t dir $DIR/R10 || error
852         $CHECKSTAT -a $DIR/R10/f || error
853         $CHECKSTAT -a $DIR/R10/g || error
854 }
855 run_test 24j "source does not exist ============================"
856
857 test_24k() {
858         test_mkdir $DIR/R11a
859         test_mkdir $DIR/R11a/d
860         touch $DIR/R11a/f
861         mv $DIR/R11a/f $DIR/R11a/d
862         $CHECKSTAT -a $DIR/R11a/f || error
863         $CHECKSTAT -t file $DIR/R11a/d/f || error
864 }
865 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
866
867 # bug 2429 - rename foo foo foo creates invalid file
868 test_24l() {
869         f="$DIR/f24l"
870         $MULTIOP $f OcNs || error
871 }
872 run_test 24l "Renaming a file to itself ========================"
873
874 test_24m() {
875         f="$DIR/f24m"
876         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
877         # on ext3 this does not remove either the source or target files
878         # though the "expected" operation would be to remove the source
879         $CHECKSTAT -t file ${f} || error "${f} missing"
880         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
881 }
882 run_test 24m "Renaming a file to a hard link to itself ========="
883
884 test_24n() {
885     f="$DIR/f24n"
886     # this stats the old file after it was renamed, so it should fail
887     touch ${f}
888     $CHECKSTAT ${f}
889     mv ${f} ${f}.rename
890     $CHECKSTAT ${f}.rename
891     $CHECKSTAT -a ${f}
892 }
893 run_test 24n "Statting the old file after renaming (Posix rename 2)"
894
895 test_24o() {
896         check_kernel_version 37 || return 0
897         test_mkdir -p $DIR/d24o
898         rename_many -s random -v -n 10 $DIR/d24o
899 }
900 run_test 24o "rename of files during htree split ==============="
901
902 test_24p() {
903         test_mkdir $DIR/R12a
904         test_mkdir $DIR/R12b
905         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
906         mrename $DIR/R12a $DIR/R12b
907         $CHECKSTAT -a $DIR/R12a || error
908         $CHECKSTAT -t dir $DIR/R12b || error
909         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
910         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
911 }
912 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
913
914 test_24q() {
915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
916         test_mkdir $DIR/R13a
917         test_mkdir $DIR/R13b
918         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
919         multiop_bg_pause $DIR/R13b D_c || return 1
920         MULTIPID=$!
921
922         mrename $DIR/R13a $DIR/R13b
923         $CHECKSTAT -a $DIR/R13a || error
924         $CHECKSTAT -t dir $DIR/R13b || error
925         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
926         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
927         kill -USR1 $MULTIPID
928         wait $MULTIPID || error "multiop close failed"
929 }
930 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
931
932 test_24r() { #bug 3789
933         test_mkdir $DIR/R14a
934         test_mkdir $DIR/R14a/b
935         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
936         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
937         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
938 }
939 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
940
941 test_24s() {
942         test_mkdir $DIR/R15a
943         test_mkdir $DIR/R15a/b
944         test_mkdir $DIR/R15a/b/c
945         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
946         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
947         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
948 }
949 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
950 test_24t() {
951         test_mkdir $DIR/R16a
952         test_mkdir $DIR/R16a/b
953         test_mkdir $DIR/R16a/b/c
954         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
955         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
956         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
957 }
958 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
959
960 test_24u() { # bug12192
961         rm -rf $DIR/$tfile
962         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
963         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
964 }
965 run_test 24u "create stripe file"
966
967 page_size() {
968         getconf PAGE_SIZE
969 }
970
971 simple_cleanup_common() {
972         trap 0
973         rm -rf $DIR/$tdir
974         wait_delete_completed
975 }
976
977 max_pages_per_rpc() {
978         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
979 }
980
981 test_24v() {
982         local NRFILES=100000
983         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
984         [ $FREE_INODES -lt $NRFILES ] && \
985                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
986                 return
987
988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
989         trap simple_cleanup_common EXIT
990
991         mkdir -p $DIR/$tdir
992         createmany -m $DIR/$tdir/$tfile $NRFILES
993
994         cancel_lru_locks mdc
995         lctl set_param mdc.*.stats clear
996
997         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
998
999         # LU-5 large readdir
1000         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1001         #               8 bytes for name(filename is mostly 5 in this test) +
1002         #               8 bytes for luda_type
1003         # take into account of overhead in lu_dirpage header and end mark in
1004         # each page, plus one in RPC_NUM calculation.
1005         DIRENT_SIZE=48
1006         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1007         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1008         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1009                                 awk '/^mds_readpage/ {print $2}')
1010         [ $mds_readpage -gt $RPC_NUM ] && \
1011                 error "large readdir doesn't take effect"
1012
1013         simple_cleanup_common
1014 }
1015 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1016
1017 test_24w() { # bug21506
1018         SZ1=234852
1019         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1020         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1021         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1022         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1023         [ "$SZ1" = "$SZ2" ] || \
1024                 error "Error reading at the end of the file $tfile"
1025 }
1026 run_test 24w "Reading a file larger than 4Gb"
1027
1028 test_24x() {
1029         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1031         local MDTIDX=1
1032         local remote_dir=$DIR/$tdir/remote_dir
1033
1034         mkdir -p $DIR/$tdir
1035         $LFS mkdir -i $MDTIDX $remote_dir ||
1036                 error "create remote directory failed"
1037
1038         mkdir -p $DIR/$tdir/src_dir
1039         touch $DIR/$tdir/src_file
1040         mkdir -p $remote_dir/tgt_dir
1041         touch $remote_dir/tgt_file
1042
1043         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1044                 error "rename dir cross MDT works!"
1045
1046         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1047                 error "rename file cross MDT works!"
1048
1049         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1050                 error "ln file cross MDT should not work!"
1051
1052         rm -rf $DIR/$tdir || error "Can not delete directories"
1053 }
1054 run_test 24x "cross rename/link should be failed"
1055
1056 test_24y() {
1057         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1059         local MDTIDX=1
1060         local remote_dir=$DIR/$tdir/remote_dir
1061
1062         mkdir -p $DIR/$tdir
1063         $LFS mkdir -i $MDTIDX $remote_dir ||
1064                    error "create remote directory failed"
1065
1066         mkdir -p $remote_dir/src_dir
1067         touch $remote_dir/src_file
1068         mkdir -p $remote_dir/tgt_dir
1069         touch $remote_dir/tgt_file
1070
1071         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1072                 error "rename subdir in the same remote dir failed!"
1073
1074         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1075                 error "rename files in the same remote dir failed!"
1076
1077         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1078                 error "link files in the same remote dir failed!"
1079
1080         rm -rf $DIR/$tdir || error "Can not delete directories"
1081 }
1082 run_test 24y "rename/link on the same dir should succeed"
1083
1084 test_24z() {
1085         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1086         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1087         local MDTIDX=1
1088         local remote_src=$DIR/$tdir/remote_dir
1089         local remote_tgt=$DIR/$tdir/remote_tgt
1090
1091         mkdir -p $DIR/$tdir
1092         $LFS mkdir -i $MDTIDX $remote_src ||
1093                    error "create remote directory failed"
1094
1095         $LFS mkdir -i $MDTIDX $remote_tgt ||
1096                    error "create remote directory failed"
1097
1098         mrename $remote_src $remote_tgt &&
1099                 error "rename remote dirs should not work!"
1100
1101         rm -rf $DIR/$tdir || error "Can not delete directories"
1102 }
1103 run_test 24z "rename one remote dir to another remote dir should fail"
1104
1105 test_25a() {
1106         echo '== symlink sanity ============================================='
1107
1108         test_mkdir $DIR/d25
1109         ln -s d25 $DIR/s25
1110         touch $DIR/s25/foo || error
1111 }
1112 run_test 25a "create file in symlinked directory ==============="
1113
1114 test_25b() {
1115         [ ! -d $DIR/d25 ] && test_25a
1116         $CHECKSTAT -t file $DIR/s25/foo || error
1117 }
1118 run_test 25b "lookup file in symlinked directory ==============="
1119
1120 test_26a() {
1121         test_mkdir $DIR/d26
1122         test_mkdir $DIR/d26/d26-2
1123         ln -s d26/d26-2 $DIR/s26
1124         touch $DIR/s26/foo || error
1125 }
1126 run_test 26a "multiple component symlink ======================="
1127
1128 test_26b() {
1129         test_mkdir -p $DIR/d26b/d26-2
1130         ln -s d26b/d26-2/foo $DIR/s26-2
1131         touch $DIR/s26-2 || error
1132 }
1133 run_test 26b "multiple component symlink at end of lookup ======"
1134
1135 test_26c() {
1136         test_mkdir $DIR/d26.2
1137         touch $DIR/d26.2/foo
1138         ln -s d26.2 $DIR/s26.2-1
1139         ln -s s26.2-1 $DIR/s26.2-2
1140         ln -s s26.2-2 $DIR/s26.2-3
1141         chmod 0666 $DIR/s26.2-3/foo
1142 }
1143 run_test 26c "chain of symlinks ================================"
1144
1145 # recursive symlinks (bug 439)
1146 test_26d() {
1147         ln -s d26-3/foo $DIR/d26-3
1148 }
1149 run_test 26d "create multiple component recursive symlink ======"
1150
1151 test_26e() {
1152         [ ! -h $DIR/d26-3 ] && test_26d
1153         rm $DIR/d26-3
1154 }
1155 run_test 26e "unlink multiple component recursive symlink ======"
1156
1157 # recursive symlinks (bug 7022)
1158 test_26f() {
1159         test_mkdir -p $DIR/$tdir
1160         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1161         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1162         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1163         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1164         cd $tfile                || error "cd $tfile failed"
1165         ln -s .. dotdot          || error "ln dotdot failed"
1166         ln -s dotdot/lndir lndir || error "ln lndir failed"
1167         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1168         output=`ls $tfile/$tfile/lndir/bar1`
1169         [ "$output" = bar1 ] && error "unexpected output"
1170         rm -r $tfile             || error "rm $tfile failed"
1171         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1172 }
1173 run_test 26f "rm -r of a directory which has recursive symlink ="
1174
1175 test_27a() {
1176         echo '== stripe sanity =============================================='
1177         test_mkdir -p $DIR/d27 || error "mkdir failed"
1178         $GETSTRIPE $DIR/d27
1179         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1180         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1181         pass
1182         log "== test_27a: write to one stripe file ========================="
1183         cp /etc/hosts $DIR/d27/f0 || error
1184 }
1185 run_test 27a "one stripe file =================================="
1186
1187 test_27b() {
1188         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1189         test_mkdir -p $DIR/d27
1190         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1191         $GETSTRIPE -c $DIR/d27/f01
1192         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1193                 error "two-stripe file doesn't have two stripes"
1194 }
1195 run_test 27b "create two stripe file"
1196
1197 test_27c() {
1198         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1199
1200         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1201 }
1202 run_test 27c "write to two stripe file"
1203
1204 test_27d() {
1205         test_mkdir -p $DIR/d27
1206         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1207         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1208         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1209 }
1210 run_test 27d "create file with default settings ================"
1211
1212 test_27e() {
1213         test_mkdir -p $DIR/d27
1214         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1215         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1216         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1217 }
1218 run_test 27e "setstripe existing file (should return error) ======"
1219
1220 test_27f() {
1221         test_mkdir -p $DIR/d27
1222         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1223         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1224         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1225 }
1226 run_test 27f "setstripe with bad stripe size (should return error)"
1227
1228 test_27g() {
1229         test_mkdir -p $DIR/d27
1230         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1231         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1232                 error "$DIR/d27/fnone has object"
1233 }
1234 run_test 27g "$GETSTRIPE with no objects"
1235
1236 test_27i() {
1237         touch $DIR/d27/fsome || error "touch failed"
1238         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1239 }
1240 run_test 27i "$GETSTRIPE with some objects"
1241
1242 test_27j() {
1243         test_mkdir -p $DIR/d27
1244         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1245 }
1246 run_test 27j "setstripe with bad stripe offset (should return error)"
1247
1248 test_27k() { # bug 2844
1249         test_mkdir -p $DIR/d27
1250         FILE=$DIR/d27/f27k
1251         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1252         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1253         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1254         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1255         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1256         dd if=/dev/zero of=$FILE bs=4k count=1
1257         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1258         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1259 }
1260 run_test 27k "limit i_blksize for broken user apps ============="
1261
1262 test_27l() {
1263         test_mkdir -p $DIR/d27
1264         mcreate $DIR/f27l || error "creating file"
1265         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1266                 error "setstripe should have failed" || true
1267 }
1268 run_test 27l "check setstripe permissions (should return error)"
1269
1270 test_27m() {
1271         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1272                 return
1273         if [ $ORIGFREE -gt $MAXFREE ]; then
1274                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1275                 return
1276         fi
1277         trap simple_cleanup_common EXIT
1278         test_mkdir -p $DIR/$tdir
1279         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1280         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1281                 error "dd should fill OST0"
1282         i=2
1283         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1284                 i=`expr $i + 1`
1285                 [ $i -gt 256 ] && break
1286         done
1287         i=`expr $i + 1`
1288         touch $DIR/$tdir/f27m_$i
1289         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1290                 error "OST0 was full but new created file still use it"
1291         i=`expr $i + 1`
1292         touch $DIR/$tdir/f27m_$i
1293         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1294                 error "OST0 was full but new created file still use it"
1295         simple_cleanup_common
1296 }
1297 run_test 27m "create file while OST0 was full =================="
1298
1299 sleep_maxage() {
1300         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1301         sleep $DELAY
1302 }
1303
1304 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1305 # if the OST isn't full anymore.
1306 reset_enospc() {
1307         local OSTIDX=${1:-""}
1308
1309         local list=$(comma_list $(osts_nodes))
1310         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1311
1312         do_nodes $list lctl set_param fail_loc=0
1313         sync    # initiate all OST_DESTROYs from MDS to OST
1314         sleep_maxage
1315 }
1316
1317 exhaust_precreations() {
1318         local OSTIDX=$1
1319         local FAILLOC=$2
1320         local FAILIDX=${3:-$OSTIDX}
1321
1322         test_mkdir -p $DIR/$tdir
1323         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1324         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1325
1326         local OST=$(ostname_from_index $OSTIDX)
1327         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1328                           sed -e 's/_UUID$//;s/^.*-//')
1329
1330         # on the mdt's osc
1331         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1332         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1333         osc.$mdtosc_proc1.prealloc_last_id)
1334         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1335         osc.$mdtosc_proc1.prealloc_next_id)
1336
1337         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1338         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1339
1340         test_mkdir -p $DIR/$tdir/${OST}
1341         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1342 #define OBD_FAIL_OST_ENOSPC              0x215
1343         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1344         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1345         echo "Creating to objid $last_id on ost $OST..."
1346         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1347         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1348         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1349         sleep_maxage
1350 }
1351
1352 exhaust_all_precreations() {
1353         local i
1354         for (( i=0; i < OSTCOUNT; i++ )) ; do
1355                 exhaust_precreations $i $1 -1
1356         done
1357 }
1358
1359 test_27n() {
1360         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1362         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1363         remote_ost_nodsh && skip "remote OST with nodsh" && return
1364
1365         reset_enospc
1366         rm -f $DIR/$tdir/$tfile
1367         exhaust_precreations 0 0x80000215
1368         $SETSTRIPE -c -1 $DIR/$tdir
1369         touch $DIR/$tdir/$tfile || error
1370         $GETSTRIPE $DIR/$tdir/$tfile
1371         reset_enospc
1372 }
1373 run_test 27n "create file with some full OSTs =================="
1374
1375 test_27o() {
1376         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1378         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1379         remote_ost_nodsh && skip "remote OST with nodsh" && return
1380
1381         reset_enospc
1382         rm -f $DIR/$tdir/$tfile
1383         exhaust_all_precreations 0x215
1384
1385         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1386
1387         reset_enospc
1388         rm -rf $DIR/$tdir/*
1389 }
1390 run_test 27o "create file with all full OSTs (should error) ===="
1391
1392 test_27p() {
1393         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1395         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1396         remote_ost_nodsh && skip "remote OST with nodsh" && return
1397
1398         reset_enospc
1399         rm -f $DIR/$tdir/$tfile
1400         test_mkdir -p $DIR/$tdir
1401
1402         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1403         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1404         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1405
1406         exhaust_precreations 0 0x80000215
1407         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1408         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1409         $GETSTRIPE $DIR/$tdir/$tfile
1410
1411         reset_enospc
1412 }
1413 run_test 27p "append to a truncated file with some full OSTs ==="
1414
1415 test_27q() {
1416         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1418         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1419         remote_ost_nodsh && skip "remote OST with nodsh" && return
1420
1421         reset_enospc
1422         rm -f $DIR/$tdir/$tfile
1423
1424         test_mkdir -p $DIR/$tdir
1425         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1426         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1427         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1428
1429         exhaust_all_precreations 0x215
1430
1431         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1432         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1433
1434         reset_enospc
1435 }
1436 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1437
1438 test_27r() {
1439         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1441         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1442         remote_ost_nodsh && skip "remote OST with nodsh" && return
1443
1444         reset_enospc
1445         rm -f $DIR/$tdir/$tfile
1446         exhaust_precreations 0 0x80000215
1447
1448         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1449
1450         reset_enospc
1451 }
1452 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1453
1454 test_27s() { # bug 10725
1455         test_mkdir -p $DIR/$tdir
1456         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1457         local stripe_count=0
1458         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1459         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1460                 error "stripe width >= 2^32 succeeded" || true
1461
1462 }
1463 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1464
1465 test_27t() { # bug 10864
1466         WDIR=`pwd`
1467         WLFS=`which lfs`
1468         cd $DIR
1469         touch $tfile
1470         $WLFS getstripe $tfile
1471         cd $WDIR
1472 }
1473 run_test 27t "check that utils parse path correctly"
1474
1475 test_27u() { # bug 4900
1476         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1477         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1478
1479 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1480         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1481         test_mkdir -p $DIR/$tdir
1482         createmany -o $DIR/$tdir/t- 1000
1483         do_facet $SINGLEMDS lctl set_param fail_loc=0
1484
1485         TLOG=$DIR/$tfile.getstripe
1486         $GETSTRIPE $DIR/$tdir > $TLOG
1487         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1488         unlinkmany $DIR/$tdir/t- 1000
1489         [ $OBJS -gt 0 ] && \
1490                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1491 }
1492 run_test 27u "skip object creation on OSC w/o objects =========="
1493
1494 test_27v() { # bug 4900
1495         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1497         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1498         remote_ost_nodsh && skip "remote OST with nodsh" && return
1499
1500         exhaust_all_precreations 0x215
1501         reset_enospc
1502
1503         test_mkdir -p $DIR/$tdir
1504         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1505
1506         touch $DIR/$tdir/$tfile
1507         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1508         # all except ost1
1509         for (( i=1; i < OSTCOUNT; i++ )); do
1510                 do_facet ost$i lctl set_param fail_loc=0x705
1511         done
1512         local START=`date +%s`
1513         createmany -o $DIR/$tdir/$tfile 32
1514
1515         local FINISH=`date +%s`
1516         local TIMEOUT=`lctl get_param -n timeout`
1517         local PROCESS=$((FINISH - START))
1518         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1519                error "$FINISH - $START >= $TIMEOUT / 2"
1520         sleep $((TIMEOUT / 2 - PROCESS))
1521         reset_enospc
1522 }
1523 run_test 27v "skip object creation on slow OST ================="
1524
1525 test_27w() { # bug 10997
1526         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1527         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1528         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1529                 error "stripe size $size != 65536" || true
1530         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1531                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1532 }
1533 run_test 27w "check $SETSTRIPE -S option"
1534
1535 test_27wa() {
1536         [ "$OSTCOUNT" -lt "2" ] &&
1537                 skip_env "skipping multiple stripe count/offset test" && return
1538
1539         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1540         for i in $(seq 1 $OSTCOUNT); do
1541                 offset=$((i - 1))
1542                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1543                         error "setstripe -c $i -i $offset failed"
1544                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1545                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1546                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1547                 [ $index -ne $offset ] &&
1548                         error "stripe offset $index != $offset" || true
1549         done
1550 }
1551 run_test 27wa "check $SETSTRIPE -c -i options"
1552
1553 test_27x() {
1554         remote_ost_nodsh && skip "remote OST with nodsh" && return
1555         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1557         OFFSET=$(($OSTCOUNT - 1))
1558         OSTIDX=0
1559         local OST=$(ostname_from_index $OSTIDX)
1560
1561         test_mkdir -p $DIR/$tdir
1562         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1563         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1564         sleep_maxage
1565         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1566         for i in `seq 0 $OFFSET`; do
1567                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1568                 error "OST0 was degraded but new created file still use it"
1569         done
1570         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1571 }
1572 run_test 27x "create files while OST0 is degraded"
1573
1574 test_27y() {
1575         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1576         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1577         remote_ost_nodsh && skip "remote OST with nodsh" && return
1578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1579
1580         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1581         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1582             osc.$mdtosc.prealloc_last_id)
1583         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1584             osc.$mdtosc.prealloc_next_id)
1585         local fcount=$((last_id - next_id))
1586         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1587         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1588
1589         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1590                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1591         local OST_DEACTIVE_IDX=-1
1592         local OSC
1593         local OSTIDX
1594         local OST
1595
1596         for OSC in $MDS_OSCS; do
1597                 OST=$(osc_to_ost $OSC)
1598                 OSTIDX=$(index_from_ostuuid $OST)
1599                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1600                         OST_DEACTIVE_IDX=$OSTIDX
1601                 fi
1602                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1603                         echo $OSC "is Deactivated:"
1604                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1605                 fi
1606         done
1607
1608         OSTIDX=$(index_from_ostuuid $OST)
1609         mkdir -p $DIR/$tdir
1610         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1611
1612         for OSC in $MDS_OSCS; do
1613                 OST=$(osc_to_ost $OSC)
1614                 OSTIDX=$(index_from_ostuuid $OST)
1615                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1616                         echo $OST "is degraded:"
1617                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1618                                                 obdfilter.$OST.degraded=1
1619                 fi
1620         done
1621
1622         sleep_maxage
1623         createmany -o $DIR/$tdir/$tfile $fcount
1624
1625         for OSC in $MDS_OSCS; do
1626                 OST=$(osc_to_ost $OSC)
1627                 OSTIDX=$(index_from_ostuuid $OST)
1628                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1629                         echo $OST "is recovered from degraded:"
1630                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1631                                                 obdfilter.$OST.degraded=0
1632                 else
1633                         do_facet $SINGLEMDS lctl --device %$OSC activate
1634                 fi
1635         done
1636 }
1637 run_test 27y "create files while OST0 is degraded and the rest inactive"
1638
1639 check_seq_oid()
1640 {
1641         log "check file $1"
1642
1643         lmm_count=$($GETSTRIPE -c $1)
1644         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1645         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1646
1647         local old_ifs="$IFS"
1648         IFS=$'[:]'
1649         fid=($($LFS path2fid $1))
1650         IFS="$old_ifs"
1651
1652         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1653         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1654
1655         # compare lmm_seq and lu_fid->f_seq
1656         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1657         # compare lmm_object_id and lu_fid->oid
1658         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1659
1660         # check the trusted.fid attribute of the OST objects of the file
1661         local have_obdidx=false
1662         local stripe_nr=0
1663         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1664                 # skip lines up to and including "obdidx"
1665                 [ -z "$obdidx" ] && break
1666                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1667                 $have_obdidx || continue
1668
1669                 local ost=$((obdidx + 1))
1670                 local dev=$(ostdevname $ost)
1671
1672                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1673                         echo "Currently only works with ldiskfs-based OSTs"
1674                         continue
1675                 fi
1676
1677                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1678
1679                 #don't unmount/remount the OSTs if we don't need to do that
1680                 #local dir=$(facet_mntpt ost$ost)
1681                 #stop ost$dev
1682                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1683                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1684                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1685                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1686
1687                 local obj_file="O/$seq/d$((oid %32))/$oid"
1688                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1689                            $dev 2>/dev/null" | grep "parent=")
1690
1691                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1692
1693                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1694
1695                 #do_facet ost$ost umount -d $dir
1696                 #start ost$ost $dev $OST_MOUNT_OPTS
1697
1698                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1699                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1700                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1701                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1702                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1703                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1704
1705                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1706                 [ $ff_pseq = $lmm_seq ] ||
1707                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1708                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1709                 [ $ff_poid = $lmm_oid ] ||
1710                         error "FF parent OID $ff_poid != $lmm_oid"
1711                 [ $ff_pstripe = $stripe_nr ] ||
1712                         error "FF stripe $ff_pstripe != $stripe_nr"
1713
1714                 stripe_nr=$((stripe_nr + 1))
1715         done
1716 }
1717
1718 test_27z() {
1719         remote_ost_nodsh && skip "remote OST with nodsh" && return
1720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1721         test_mkdir -p $DIR/$tdir
1722
1723         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1724                 { error "setstripe -c -1 failed"; return 1; }
1725         # We need to send a write to every object to get parent FID info set.
1726         # This _should_ also work for setattr, but does not currently.
1727         # touch $DIR/$tdir/$tfile-1 ||
1728         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1729                 { error "dd $tfile-1 failed"; return 2; }
1730         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1731                 { error "setstripe -c -1 failed"; return 3; }
1732         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1733                 { error "dd $tfile-2 failed"; return 4; }
1734
1735         # make sure write RPCs have been sent to OSTs
1736         sync; sleep 5; sync
1737
1738         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1739         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1740 }
1741 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1742
1743 test_27A() { # b=19102
1744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1745         local restore_size=$($GETSTRIPE -S $MOUNT)
1746         local restore_count=$($GETSTRIPE -c $MOUNT)
1747         local restore_offset=$($GETSTRIPE -i $MOUNT)
1748         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1749         local default_size=$($GETSTRIPE -S $MOUNT)
1750         local default_count=$($GETSTRIPE -c $MOUNT)
1751         local default_offset=$($GETSTRIPE -i $MOUNT)
1752         local dsize=$((1024 * 1024))
1753         [ $default_size -eq $dsize ] ||
1754                 error "stripe size $default_size != $dsize"
1755         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1756         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1757         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1758 }
1759 run_test 27A "check filesystem-wide default LOV EA values"
1760
1761 # createtest also checks that device nodes are created and
1762 # then visible correctly (#2091)
1763 test_28() { # bug 2091
1764         test_mkdir $DIR/d28
1765         $CREATETEST $DIR/d28/ct || error
1766 }
1767 run_test 28 "create/mknod/mkdir with bad file types ============"
1768
1769 test_29() {
1770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1771         cancel_lru_locks mdc
1772         test_mkdir $DIR/d29
1773         touch $DIR/d29/foo
1774         log 'first d29'
1775         ls -l $DIR/d29
1776
1777         declare -i LOCKCOUNTORIG=0
1778         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1779                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1780         done
1781         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1782
1783         declare -i LOCKUNUSEDCOUNTORIG=0
1784         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1785                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1786         done
1787
1788         log 'second d29'
1789         ls -l $DIR/d29
1790         log 'done'
1791
1792         declare -i LOCKCOUNTCURRENT=0
1793         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1794                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1795         done
1796
1797         declare -i LOCKUNUSEDCOUNTCURRENT=0
1798         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1799                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1800         done
1801
1802         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1803                 lctl set_param -n ldlm.dump_namespaces ""
1804                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1805                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1806                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1807                 return 2
1808         fi
1809         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1810                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1811                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1812                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1813                 return 3
1814         fi
1815 }
1816 run_test 29 "IT_GETATTR regression  ============================"
1817
1818 test_30a() { # was test_30
1819         cp `which ls` $DIR || cp /bin/ls $DIR
1820         $DIR/ls / || error
1821         rm $DIR/ls
1822 }
1823 run_test 30a "execute binary from Lustre (execve) =============="
1824
1825 test_30b() {
1826         cp `which ls` $DIR || cp /bin/ls $DIR
1827         chmod go+rx $DIR/ls
1828         $RUNAS $DIR/ls / || error
1829         rm $DIR/ls
1830 }
1831 run_test 30b "execute binary from Lustre as non-root ==========="
1832
1833 test_30c() { # b=22376
1834         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1835         cp `which ls` $DIR || cp /bin/ls $DIR
1836         chmod a-rw $DIR/ls
1837         cancel_lru_locks mdc
1838         cancel_lru_locks osc
1839         $RUNAS $DIR/ls / || error
1840         rm -f $DIR/ls
1841 }
1842 run_test 30c "execute binary from Lustre without read perms ===="
1843
1844 test_31a() {
1845         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1846         $CHECKSTAT -a $DIR/f31 || error
1847 }
1848 run_test 31a "open-unlink file =================================="
1849
1850 test_31b() {
1851         touch $DIR/f31 || error
1852         ln $DIR/f31 $DIR/f31b || error
1853         $MULTIOP $DIR/f31b Ouc || error
1854         $CHECKSTAT -t file $DIR/f31 || error
1855 }
1856 run_test 31b "unlink file with multiple links while open ======="
1857
1858 test_31c() {
1859         touch $DIR/f31 || error
1860         ln $DIR/f31 $DIR/f31c || error
1861         multiop_bg_pause $DIR/f31 O_uc || return 1
1862         MULTIPID=$!
1863         $MULTIOP $DIR/f31c Ouc
1864         kill -USR1 $MULTIPID
1865         wait $MULTIPID
1866 }
1867 run_test 31c "open-unlink file with multiple links ============="
1868
1869 test_31d() {
1870         opendirunlink $DIR/d31d $DIR/d31d || error
1871         $CHECKSTAT -a $DIR/d31d || error
1872 }
1873 run_test 31d "remove of open directory ========================="
1874
1875 test_31e() { # bug 2904
1876         check_kernel_version 34 || return 0
1877         openfilleddirunlink $DIR/d31e || error
1878 }
1879 run_test 31e "remove of open non-empty directory ==============="
1880
1881 test_31f() { # bug 4554
1882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1883         set -vx
1884         test_mkdir $DIR/d31f
1885         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1886         cp /etc/hosts $DIR/d31f
1887         ls -l $DIR/d31f
1888         $GETSTRIPE $DIR/d31f/hosts
1889         multiop_bg_pause $DIR/d31f D_c || return 1
1890         MULTIPID=$!
1891
1892         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1893         test_mkdir $DIR/d31f
1894         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1895         cp /etc/hosts $DIR/d31f
1896         ls -l $DIR/d31f
1897         $GETSTRIPE $DIR/d31f/hosts
1898         multiop_bg_pause $DIR/d31f D_c || return 1
1899         MULTIPID2=$!
1900
1901         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1902         wait $MULTIPID || error "first opendir $MULTIPID failed"
1903
1904         sleep 6
1905
1906         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1907         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1908         set +vx
1909 }
1910 run_test 31f "remove of open directory with open-unlink file ==="
1911
1912 test_31g() {
1913         echo "-- cross directory link --"
1914         test_mkdir $DIR/d31ga
1915         test_mkdir $DIR/d31gb
1916         touch $DIR/d31ga/f
1917         ln $DIR/d31ga/f $DIR/d31gb/g
1918         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1919         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1920         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1921         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1922 }
1923 run_test 31g "cross directory link==============="
1924
1925 test_31h() {
1926         echo "-- cross directory link --"
1927         test_mkdir $DIR/d31h
1928         test_mkdir $DIR/d31h/dir
1929         touch $DIR/d31h/f
1930         ln $DIR/d31h/f $DIR/d31h/dir/g
1931         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1932         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1933         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1934         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1935 }
1936 run_test 31h "cross directory link under child==============="
1937
1938 test_31i() {
1939         echo "-- cross directory link --"
1940         test_mkdir $DIR/d31i
1941         test_mkdir $DIR/d31i/dir
1942         touch $DIR/d31i/dir/f
1943         ln $DIR/d31i/dir/f $DIR/d31i/g
1944         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1945         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1946         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1947         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1948 }
1949 run_test 31i "cross directory link under parent==============="
1950
1951
1952 test_31j() {
1953         test_mkdir $DIR/d31j
1954         test_mkdir $DIR/d31j/dir1
1955         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1956         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1957         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1958         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1959         return 0
1960 }
1961 run_test 31j "link for directory==============="
1962
1963
1964 test_31k() {
1965         test_mkdir $DIR/d31k
1966         touch $DIR/d31k/s
1967         touch $DIR/d31k/exist
1968         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1969         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1970         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1971         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1972         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1973         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1974         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1975         return 0
1976 }
1977 run_test 31k "link to file: the same, non-existing, dir==============="
1978
1979 test_31m() {
1980         test_mkdir $DIR/d31m
1981         touch $DIR/d31m/s
1982         test_mkdir $DIR/d31m2
1983         touch $DIR/d31m2/exist
1984         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1985         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1986         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1987         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1988         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1989         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1990         return 0
1991 }
1992 run_test 31m "link to file: the same, non-existing, dir==============="
1993
1994 cleanup_test32_mount() {
1995         trap 0
1996         $UMOUNT $DIR/$tdir/ext2-mountpoint
1997 }
1998
1999 test_32a() {
2000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2001         echo "== more mountpoints and symlinks ================="
2002         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2003         trap cleanup_test32_mount EXIT
2004         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2005         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2006         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2007         cleanup_test32_mount
2008 }
2009 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2010
2011 test_32b() {
2012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
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         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2018         cleanup_test32_mount
2019 }
2020 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2021
2022 test_32c() {
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         test_mkdir -p $DIR/$tdir/d2/test_dir
2029         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2030         cleanup_test32_mount
2031 }
2032 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2033
2034 test_32d() {
2035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2036         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2037         trap cleanup_test32_mount EXIT
2038         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2039         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2040         test_mkdir -p $DIR/$tdir/d2/test_dir
2041         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2042         cleanup_test32_mount
2043 }
2044 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2045
2046 test_32e() {
2047         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2048         test_mkdir -p $DIR/d32e/tmp
2049         TMP_DIR=$DIR/d32e/tmp
2050         ln -s $DIR/d32e $TMP_DIR/symlink11
2051         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2052         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2053         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2054 }
2055 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2056
2057 test_32f() {
2058         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2059         test_mkdir -p $DIR/d32f/tmp
2060         TMP_DIR=$DIR/d32f/tmp
2061         ln -s $DIR/d32f $TMP_DIR/symlink11
2062         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2063         ls $DIR/d32f/tmp/symlink11  || error
2064         ls $DIR/d32f/symlink01 || error
2065 }
2066 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2067
2068 test_32g() {
2069         TMP_DIR=$DIR/$tdir/tmp
2070         test_mkdir -p $DIR/$tdir/tmp
2071         test_mkdir $DIR/${tdir}2
2072         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2073         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2074         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2075         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2076         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2077         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2078 }
2079 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2080
2081 test_32h() {
2082         rm -fr $DIR/$tdir $DIR/${tdir}2
2083         TMP_DIR=$DIR/$tdir/tmp
2084         test_mkdir -p $DIR/$tdir/tmp
2085         test_mkdir $DIR/${tdir}2
2086         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2087         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2088         ls $TMP_DIR/symlink12 || error
2089         ls $DIR/$tdir/symlink02  || error
2090 }
2091 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2092
2093 test_32i() {
2094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2095         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2096         trap cleanup_test32_mount EXIT
2097         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2098         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2099         touch $DIR/$tdir/test_file
2100         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2101         cleanup_test32_mount
2102 }
2103 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2104
2105 test_32j() {
2106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2107         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2108         trap cleanup_test32_mount EXIT
2109         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2110         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2111         touch $DIR/$tdir/test_file
2112         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2113         cleanup_test32_mount
2114 }
2115 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2116
2117 test_32k() {
2118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2119         rm -fr $DIR/$tdir
2120         trap cleanup_test32_mount EXIT
2121         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2122         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2123         test_mkdir -p $DIR/$tdir/d2
2124         touch $DIR/$tdir/d2/test_file || error
2125         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2126         cleanup_test32_mount
2127 }
2128 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2129
2130 test_32l() {
2131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2132         rm -fr $DIR/$tdir
2133         trap cleanup_test32_mount EXIT
2134         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2135         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2136         test_mkdir -p $DIR/$tdir/d2
2137         touch $DIR/$tdir/d2/test_file
2138         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2139         cleanup_test32_mount
2140 }
2141 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2142
2143 test_32m() {
2144         rm -fr $DIR/d32m
2145         test_mkdir -p $DIR/d32m/tmp
2146         TMP_DIR=$DIR/d32m/tmp
2147         ln -s $DIR $TMP_DIR/symlink11
2148         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2149         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2150         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2151 }
2152 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2153
2154 test_32n() {
2155         rm -fr $DIR/d32n
2156         test_mkdir -p $DIR/d32n/tmp
2157         TMP_DIR=$DIR/d32n/tmp
2158         ln -s $DIR $TMP_DIR/symlink11
2159         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2160         ls -l $DIR/d32n/tmp/symlink11  || error
2161         ls -l $DIR/d32n/symlink01 || error
2162 }
2163 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2164
2165 test_32o() {
2166         rm -fr $DIR/d32o $DIR/$tfile
2167         touch $DIR/$tfile
2168         test_mkdir -p $DIR/d32o/tmp
2169         TMP_DIR=$DIR/d32o/tmp
2170         ln -s $DIR/$tfile $TMP_DIR/symlink12
2171         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2172         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2173         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2174         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2175         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2176 }
2177 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2178
2179 test_32p() {
2180     log 32p_1
2181         rm -fr $DIR/d32p
2182     log 32p_2
2183         rm -f $DIR/$tfile
2184     log 32p_3
2185         touch $DIR/$tfile
2186     log 32p_4
2187         test_mkdir -p $DIR/d32p/tmp
2188     log 32p_5
2189         TMP_DIR=$DIR/d32p/tmp
2190     log 32p_6
2191         ln -s $DIR/$tfile $TMP_DIR/symlink12
2192     log 32p_7
2193         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2194     log 32p_8
2195         cat $DIR/d32p/tmp/symlink12 || error
2196     log 32p_9
2197         cat $DIR/d32p/symlink02 || error
2198     log 32p_10
2199 }
2200 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2201
2202 cleanup_testdir_mount() {
2203         trap 0
2204         $UMOUNT $DIR/$tdir
2205 }
2206
2207 test_32q() {
2208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2209         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2210         trap cleanup_testdir_mount EXIT
2211         test_mkdir -p $DIR/$tdir
2212         touch $DIR/$tdir/under_the_mount
2213         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2214         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2215         cleanup_testdir_mount
2216 }
2217 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2218
2219 test_32r() {
2220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2221         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2222         trap cleanup_testdir_mount EXIT
2223         test_mkdir -p $DIR/$tdir
2224         touch $DIR/$tdir/under_the_mount
2225         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2226         ls $DIR/$tdir | grep -q under_the_mount && error || true
2227         cleanup_testdir_mount
2228 }
2229 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2230
2231 test_33aa() {
2232         rm -f $DIR/$tfile
2233         touch $DIR/$tfile
2234         chmod 444 $DIR/$tfile
2235         chown $RUNAS_ID $DIR/$tfile
2236         log 33_1
2237         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2238         log 33_2
2239 }
2240 run_test 33aa "write file with mode 444 (should return error) ===="
2241
2242 test_33a() {
2243         rm -fr $DIR/d33
2244         test_mkdir -p $DIR/d33
2245         chown $RUNAS_ID $DIR/d33
2246         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2247         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2248                 error "open RDWR" || true
2249 }
2250 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2251
2252 test_33b() {
2253         rm -fr $DIR/d33
2254         test_mkdir -p $DIR/d33
2255         chown $RUNAS_ID $DIR/d33
2256         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2257 }
2258 run_test 33b "test open file with malformed flags (No panic and return error)"
2259
2260 test_33c() {
2261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2262         local ostnum
2263         local ostname
2264         local write_bytes
2265         local all_zeros
2266
2267         remote_ost_nodsh && skip "remote OST with nodsh" && return
2268         all_zeros=:
2269         rm -fr $DIR/d33
2270         test_mkdir -p $DIR/d33
2271         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2272
2273         sync
2274         for ostnum in $(seq $OSTCOUNT); do
2275                 # test-framework's OST numbering is one-based, while Lustre's
2276                 # is zero-based
2277                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2278                 # Parsing llobdstat's output sucks; we could grep the /proc
2279                 # path, but that's likely to not be as portable as using the
2280                 # llobdstat utility.  So we parse lctl output instead.
2281                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2282                         obdfilter/$ostname/stats |
2283                         awk '/^write_bytes/ {print $7}' )
2284                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2285                 if (( ${write_bytes:-0} > 0 ))
2286                 then
2287                         all_zeros=false
2288                         break;
2289                 fi
2290         done
2291
2292         $all_zeros || return 0
2293
2294         # Write four bytes
2295         echo foo > $DIR/d33/bar
2296         # Really write them
2297         sync
2298
2299         # Total up write_bytes after writing.  We'd better find non-zeros.
2300         for ostnum in $(seq $OSTCOUNT); do
2301                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2302                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2303                         obdfilter/$ostname/stats |
2304                         awk '/^write_bytes/ {print $7}' )
2305                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2306                 if (( ${write_bytes:-0} > 0 ))
2307                 then
2308                         all_zeros=false
2309                         break;
2310                 fi
2311         done
2312
2313         if $all_zeros
2314         then
2315                 for ostnum in $(seq $OSTCOUNT); do
2316                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2317                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2318                         do_facet ost$ostnum lctl get_param -n \
2319                                 obdfilter/$ostname/stats
2320                 done
2321                 error "OST not keeping write_bytes stats (b22312)"
2322         fi
2323 }
2324 run_test 33c "test llobdstat and write_bytes"
2325
2326 test_33d() {
2327         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2328         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2329         local MDTIDX=1
2330         local remote_dir=$DIR/$tdir/remote_dir
2331
2332         mkdir -p $DIR/$tdir
2333         $LFS mkdir -i $MDTIDX $remote_dir ||
2334                 error "create remote directory failed"
2335
2336         touch $remote_dir/$tfile
2337         chmod 444 $remote_dir/$tfile
2338         chown $RUNAS_ID $remote_dir/$tfile
2339
2340         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2341
2342         chown $RUNAS_ID $remote_dir
2343         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2344                                         error "create" || true
2345         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2346                                     error "open RDWR" || true
2347         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2348                                     error "create" || true
2349 }
2350 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2351
2352 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2353 test_34a() {
2354         rm -f $DIR/f34
2355         $MCREATE $DIR/f34 || error
2356         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2357         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2358         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2359         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2360 }
2361 run_test 34a "truncate file that has not been opened ==========="
2362
2363 test_34b() {
2364         [ ! -f $DIR/f34 ] && test_34a
2365         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2366         $OPENFILE -f O_RDONLY $DIR/f34
2367         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2368         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2369 }
2370 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2371
2372 test_34c() {
2373         [ ! -f $DIR/f34 ] && test_34a
2374         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2375         $OPENFILE -f O_RDWR $DIR/f34
2376         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2377         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2378 }
2379 run_test 34c "O_RDWR opening file-with-size works =============="
2380
2381 test_34d() {
2382         [ ! -f $DIR/f34 ] && test_34a
2383         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2384         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2385         rm $DIR/f34
2386 }
2387 run_test 34d "write to sparse file ============================="
2388
2389 test_34e() {
2390         rm -f $DIR/f34e
2391         $MCREATE $DIR/f34e || error
2392         $TRUNCATE $DIR/f34e 1000 || error
2393         $CHECKSTAT -s 1000 $DIR/f34e || error
2394         $OPENFILE -f O_RDWR $DIR/f34e
2395         $CHECKSTAT -s 1000 $DIR/f34e || error
2396 }
2397 run_test 34e "create objects, some with size and some without =="
2398
2399 test_34f() { # bug 6242, 6243
2400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2401         SIZE34F=48000
2402         rm -f $DIR/f34f
2403         $MCREATE $DIR/f34f || error
2404         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2405         dd if=$DIR/f34f of=$TMP/f34f
2406         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2407         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2408         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2409         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2410         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2411 }
2412 run_test 34f "read from a file with no objects until EOF ======="
2413
2414 test_34g() {
2415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2416         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2417         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2418         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2419         cancel_lru_locks osc
2420         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2421                 error "wrong size after lock cancel"
2422
2423         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2424         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2425                 error "expanding truncate failed"
2426         cancel_lru_locks osc
2427         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2428                 error "wrong expanded size after lock cancel"
2429 }
2430 run_test 34g "truncate long file ==============================="
2431
2432 test_34h() {
2433         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2434         local gid=10
2435         local sz=1000
2436
2437         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2438         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2439         MULTIPID=$!
2440         sleep 2
2441
2442         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2443                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2444                 kill -9 $MULTIPID
2445         fi
2446         wait $MULTIPID
2447         local nsz=`stat -c %s $DIR/$tfile`
2448         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2449 }
2450 run_test 34h "ftruncate file under grouplock should not block"
2451
2452 test_35a() {
2453         cp /bin/sh $DIR/f35a
2454         chmod 444 $DIR/f35a
2455         chown $RUNAS_ID $DIR/f35a
2456         $RUNAS $DIR/f35a && error || true
2457         rm $DIR/f35a
2458 }
2459 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2460
2461 test_36a() {
2462         rm -f $DIR/f36
2463         utime $DIR/f36 || error
2464 }
2465 run_test 36a "MDS utime check (mknod, utime) ==================="
2466
2467 test_36b() {
2468         echo "" > $DIR/f36
2469         utime $DIR/f36 || error
2470 }
2471 run_test 36b "OST utime check (open, utime) ===================="
2472
2473 test_36c() {
2474         rm -f $DIR/d36/f36
2475         test_mkdir $DIR/d36
2476         chown $RUNAS_ID $DIR/d36
2477         $RUNAS utime $DIR/d36/f36 || error
2478 }
2479 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2480
2481 test_36d() {
2482         [ ! -d $DIR/d36 ] && test_36c
2483         echo "" > $DIR/d36/f36
2484         $RUNAS utime $DIR/d36/f36 || error
2485 }
2486 run_test 36d "non-root OST utime check (open, utime) ==========="
2487
2488 test_36e() {
2489         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2490         test_mkdir -p $DIR/$tdir
2491         touch $DIR/$tdir/$tfile
2492         $RUNAS utime $DIR/$tdir/$tfile && \
2493                 error "utime worked, expected failure" || true
2494 }
2495 run_test 36e "utime on non-owned file (should return error) ===="
2496
2497 subr_36fh() {
2498         local fl="$1"
2499         local LANG_SAVE=$LANG
2500         local LC_LANG_SAVE=$LC_LANG
2501         export LANG=C LC_LANG=C # for date language
2502
2503         DATESTR="Dec 20  2000"
2504         test_mkdir -p $DIR/$tdir
2505         lctl set_param fail_loc=$fl
2506         date; date +%s
2507         cp /etc/hosts $DIR/$tdir/$tfile
2508         sync & # write RPC generated with "current" inode timestamp, but delayed
2509         sleep 1
2510         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2511         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2512         cancel_lru_locks osc
2513         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2514         date; date +%s
2515         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2516                 echo "BEFORE: $LS_BEFORE" && \
2517                 echo "AFTER : $LS_AFTER" && \
2518                 echo "WANT  : $DATESTR" && \
2519                 error "$DIR/$tdir/$tfile timestamps changed" || true
2520
2521         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2522 }
2523
2524 test_36f() {
2525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2526         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2527         subr_36fh "0x80000214"
2528 }
2529 run_test 36f "utime on file racing with OST BRW write =========="
2530
2531 test_36g() {
2532         remote_ost_nodsh && skip "remote OST with nodsh" && return
2533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2534         local fmd_max_age
2535         local fmd_before
2536         local fmd_after
2537
2538         test_mkdir -p $DIR/$tdir
2539         fmd_max_age=$(do_facet ost1 \
2540                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2541                 head -n 1")
2542
2543         fmd_before=$(do_facet ost1 \
2544                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2545         touch $DIR/$tdir/$tfile
2546         sleep $((fmd_max_age + 12))
2547         fmd_after=$(do_facet ost1 \
2548                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2549
2550         echo "fmd_before: $fmd_before"
2551         echo "fmd_after: $fmd_after"
2552         [ "$fmd_after" -gt "$fmd_before" ] && \
2553                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2554                 error "fmd didn't expire after ping" || true
2555 }
2556 run_test 36g "filter mod data cache expiry ====================="
2557
2558 test_36h() {
2559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2560         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2561         subr_36fh "0x80000227"
2562 }
2563 run_test 36h "utime on file racing with OST BRW write =========="
2564
2565 # test_37 - duplicate with tests 32q 32r
2566
2567 test_38() {
2568         local file=$DIR/$tfile
2569         touch $file
2570         openfile -f O_DIRECTORY $file
2571         local RC=$?
2572         local ENOTDIR=20
2573         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2574         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2575 }
2576 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2577
2578 test_39() {
2579         touch $DIR/$tfile
2580         touch $DIR/${tfile}2
2581 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2582 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2583 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2584         sleep 2
2585         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2586         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2587                 echo "mtime"
2588                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2589                 echo "atime"
2590                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2591                 echo "ctime"
2592                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2593                 error "O_TRUNC didn't change timestamps"
2594         fi
2595 }
2596 run_test 39 "mtime changed on create ==========================="
2597
2598 test_39b() {
2599         test_mkdir -p $DIR/$tdir
2600         cp -p /etc/passwd $DIR/$tdir/fopen
2601         cp -p /etc/passwd $DIR/$tdir/flink
2602         cp -p /etc/passwd $DIR/$tdir/funlink
2603         cp -p /etc/passwd $DIR/$tdir/frename
2604         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2605
2606         sleep 1
2607         echo "aaaaaa" >> $DIR/$tdir/fopen
2608         echo "aaaaaa" >> $DIR/$tdir/flink
2609         echo "aaaaaa" >> $DIR/$tdir/funlink
2610         echo "aaaaaa" >> $DIR/$tdir/frename
2611
2612         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2613         local link_new=`stat -c %Y $DIR/$tdir/flink`
2614         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2615         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2616
2617         cat $DIR/$tdir/fopen > /dev/null
2618         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2619         rm -f $DIR/$tdir/funlink2
2620         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2621
2622         for (( i=0; i < 2; i++ )) ; do
2623                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2624                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2625                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2626                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2627
2628                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2629                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2630                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2631                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2632
2633                 cancel_lru_locks osc
2634                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2635         done
2636 }
2637 run_test 39b "mtime change on open, link, unlink, rename  ======"
2638
2639 # this should be set to past
2640 TEST_39_MTIME=`date -d "1 year ago" +%s`
2641
2642 # bug 11063
2643 test_39c() {
2644         touch $DIR1/$tfile
2645         sleep 2
2646         local mtime0=`stat -c %Y $DIR1/$tfile`
2647
2648         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2649         local mtime1=`stat -c %Y $DIR1/$tfile`
2650         [ "$mtime1" = $TEST_39_MTIME ] || \
2651                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2652
2653         local d1=`date +%s`
2654         echo hello >> $DIR1/$tfile
2655         local d2=`date +%s`
2656         local mtime2=`stat -c %Y $DIR1/$tfile`
2657         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2658                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2659
2660         mv $DIR1/$tfile $DIR1/$tfile-1
2661
2662         for (( i=0; i < 2; i++ )) ; do
2663                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2664                 [ "$mtime2" = "$mtime3" ] || \
2665                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2666
2667                 cancel_lru_locks osc
2668                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2669         done
2670 }
2671 run_test 39c "mtime change on rename ==========================="
2672
2673 # bug 21114
2674 test_39d() {
2675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2676         touch $DIR1/$tfile
2677
2678         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2679
2680         for (( i=0; i < 2; i++ )) ; do
2681                 local mtime=`stat -c %Y $DIR1/$tfile`
2682                 [ $mtime = $TEST_39_MTIME ] || \
2683                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2684
2685                 cancel_lru_locks osc
2686                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2687         done
2688 }
2689 run_test 39d "create, utime, stat =============================="
2690
2691 # bug 21114
2692 test_39e() {
2693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2694         touch $DIR1/$tfile
2695         local mtime1=`stat -c %Y $DIR1/$tfile`
2696
2697         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2698
2699         for (( i=0; i < 2; i++ )) ; do
2700                 local mtime2=`stat -c %Y $DIR1/$tfile`
2701                 [ $mtime2 = $TEST_39_MTIME ] || \
2702                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2703
2704                 cancel_lru_locks osc
2705                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2706         done
2707 }
2708 run_test 39e "create, stat, utime, stat ========================"
2709
2710 # bug 21114
2711 test_39f() {
2712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2713         touch $DIR1/$tfile
2714         mtime1=`stat -c %Y $DIR1/$tfile`
2715
2716         sleep 2
2717         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2718
2719         for (( i=0; i < 2; i++ )) ; do
2720                 local mtime2=`stat -c %Y $DIR1/$tfile`
2721                 [ $mtime2 = $TEST_39_MTIME ] || \
2722                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2723
2724                 cancel_lru_locks osc
2725                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2726         done
2727 }
2728 run_test 39f "create, stat, sleep, utime, stat ================="
2729
2730 # bug 11063
2731 test_39g() {
2732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2733         echo hello >> $DIR1/$tfile
2734         local mtime1=`stat -c %Y $DIR1/$tfile`
2735
2736         sleep 2
2737         chmod o+r $DIR1/$tfile
2738
2739         for (( i=0; i < 2; i++ )) ; do
2740                 local mtime2=`stat -c %Y $DIR1/$tfile`
2741                 [ "$mtime1" = "$mtime2" ] || \
2742                         error "lost mtime: $mtime2, should be $mtime1"
2743
2744                 cancel_lru_locks osc
2745                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2746         done
2747 }
2748 run_test 39g "write, chmod, stat ==============================="
2749
2750 # bug 11063
2751 test_39h() {
2752         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2753         touch $DIR1/$tfile
2754         sleep 1
2755
2756         local d1=`date`
2757         echo hello >> $DIR1/$tfile
2758         local mtime1=`stat -c %Y $DIR1/$tfile`
2759
2760         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2761         local d2=`date`
2762         if [ "$d1" != "$d2" ]; then
2763                 echo "write and touch not within one second"
2764         else
2765                 for (( i=0; i < 2; i++ )) ; do
2766                         local mtime2=`stat -c %Y $DIR1/$tfile`
2767                         [ "$mtime2" = $TEST_39_MTIME ] || \
2768                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2769
2770                         cancel_lru_locks osc
2771                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2772                 done
2773         fi
2774 }
2775 run_test 39h "write, utime within one second, stat ============="
2776
2777 test_39i() {
2778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2779         touch $DIR1/$tfile
2780         sleep 1
2781
2782         echo hello >> $DIR1/$tfile
2783         local mtime1=`stat -c %Y $DIR1/$tfile`
2784
2785         mv $DIR1/$tfile $DIR1/$tfile-1
2786
2787         for (( i=0; i < 2; i++ )) ; do
2788                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2789
2790                 [ "$mtime1" = "$mtime2" ] || \
2791                         error "lost mtime: $mtime2, should be $mtime1"
2792
2793                 cancel_lru_locks osc
2794                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2795         done
2796 }
2797 run_test 39i "write, rename, stat =============================="
2798
2799 test_39j() {
2800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2801         start_full_debug_logging
2802         touch $DIR1/$tfile
2803         sleep 1
2804
2805         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2806         lctl set_param fail_loc=0x80000412
2807         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2808                 error "multiop failed"
2809         local multipid=$!
2810         local mtime1=`stat -c %Y $DIR1/$tfile`
2811
2812         mv $DIR1/$tfile $DIR1/$tfile-1
2813
2814         kill -USR1 $multipid
2815         wait $multipid || error "multiop close failed"
2816
2817         for (( i=0; i < 2; i++ )) ; do
2818                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2819                 [ "$mtime1" = "$mtime2" ] ||
2820                         error "mtime is lost on close: $mtime2, " \
2821                               "should be $mtime1"
2822
2823                 cancel_lru_locks osc
2824                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2825         done
2826         lctl set_param fail_loc=0
2827         stop_full_debug_logging
2828 }
2829 run_test 39j "write, rename, close, stat ======================="
2830
2831 test_39k() {
2832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2833         touch $DIR1/$tfile
2834         sleep 1
2835
2836         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2837         local multipid=$!
2838         local mtime1=`stat -c %Y $DIR1/$tfile`
2839
2840         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2841
2842         kill -USR1 $multipid
2843         wait $multipid || error "multiop close failed"
2844
2845         for (( i=0; i < 2; i++ )) ; do
2846                 local mtime2=`stat -c %Y $DIR1/$tfile`
2847
2848                 [ "$mtime2" = $TEST_39_MTIME ] || \
2849                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2850
2851                 cancel_lru_locks osc
2852                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2853         done
2854 }
2855 run_test 39k "write, utime, close, stat ========================"
2856
2857 # this should be set to future
2858 TEST_39_ATIME=`date -d "1 year" +%s`
2859
2860 test_39l() {
2861         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2862         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2863         local atime_diff=$(do_facet $SINGLEMDS \
2864                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2865         rm -rf $DIR/$tdir
2866         mkdir -p $DIR/$tdir
2867
2868         # test setting directory atime to future
2869         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2870         local atime=$(stat -c %X $DIR/$tdir)
2871         [ "$atime" = $TEST_39_ATIME ] || \
2872                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2873
2874         # test setting directory atime from future to now
2875         local d1=$(date +%s)
2876         ls $DIR/$tdir
2877         local d2=$(date +%s)
2878
2879         cancel_lru_locks mdc
2880         atime=$(stat -c %X $DIR/$tdir)
2881         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2882                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2883
2884         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2885         sleep 3
2886
2887         # test setting directory atime when now > dir atime + atime_diff
2888         d1=$(date +%s)
2889         ls $DIR/$tdir
2890         d2=$(date +%s)
2891         cancel_lru_locks mdc
2892         atime=$(stat -c %X $DIR/$tdir)
2893         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2894                 error "atime is not updated  : $atime, should be $d2"
2895
2896         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2897         sleep 3
2898
2899         # test not setting directory atime when now < dir atime + atime_diff
2900         ls $DIR/$tdir
2901         cancel_lru_locks mdc
2902         atime=$(stat -c %X $DIR/$tdir)
2903         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2904                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2905
2906         do_facet $SINGLEMDS \
2907                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2908 }
2909 run_test 39l "directory atime update ==========================="
2910
2911 test_39m() {
2912         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2913         touch $DIR1/$tfile
2914         sleep 2
2915         local far_past_mtime=$(date -d "May 29 1953" +%s)
2916         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2917
2918         touch -m -d @$far_past_mtime $DIR1/$tfile
2919         touch -a -d @$far_past_atime $DIR1/$tfile
2920
2921         for (( i=0; i < 2; i++ )) ; do
2922                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2923                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2924                         error "atime or mtime set incorrectly"
2925
2926                 cancel_lru_locks osc
2927                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2928         done
2929 }
2930 run_test 39m "test atime and mtime before 1970"
2931
2932 test_40() {
2933         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2934         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2935         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2936 }
2937 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2938
2939 test_41() {
2940         # bug 1553
2941         small_write $DIR/f41 18
2942 }
2943 run_test 41 "test small file write + fstat ====================="
2944
2945 count_ost_writes() {
2946         lctl get_param -n osc.*.stats |
2947             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2948 }
2949
2950 # decent default
2951 WRITEBACK_SAVE=500
2952 DIRTY_RATIO_SAVE=40
2953 MAX_DIRTY_RATIO=50
2954 BG_DIRTY_RATIO_SAVE=10
2955 MAX_BG_DIRTY_RATIO=25
2956
2957 start_writeback() {
2958         trap 0
2959         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2960         # dirty_ratio, dirty_background_ratio
2961         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2962                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2963                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2964                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2965         else
2966                 # if file not here, we are a 2.4 kernel
2967                 kill -CONT `pidof kupdated`
2968         fi
2969 }
2970
2971 stop_writeback() {
2972         # setup the trap first, so someone cannot exit the test at the
2973         # exact wrong time and mess up a machine
2974         trap start_writeback EXIT
2975         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2976         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2977                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2978                 sysctl -w vm.dirty_writeback_centisecs=0
2979                 sysctl -w vm.dirty_writeback_centisecs=0
2980                 # save and increase /proc/sys/vm/dirty_ratio
2981                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2982                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2983                 # save and increase /proc/sys/vm/dirty_background_ratio
2984                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2985                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2986         else
2987                 # if file not here, we are a 2.4 kernel
2988                 kill -STOP `pidof kupdated`
2989         fi
2990 }
2991
2992 # ensure that all stripes have some grant before we test client-side cache
2993 setup_test42() {
2994         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2995                 dd if=/dev/zero of=$i bs=4k count=1
2996                 rm $i
2997         done
2998 }
2999
3000 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3001 # file truncation, and file removal.
3002 test_42a() {
3003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3004         setup_test42
3005         cancel_lru_locks osc
3006         stop_writeback
3007         sync; sleep 1; sync # just to be safe
3008         BEFOREWRITES=`count_ost_writes`
3009         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3010         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3011         AFTERWRITES=`count_ost_writes`
3012         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3013                 error "$BEFOREWRITES < $AFTERWRITES"
3014         start_writeback
3015 }
3016 run_test 42a "ensure that we don't flush on close =============="
3017
3018 test_42b() {
3019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3020         setup_test42
3021         cancel_lru_locks osc
3022         stop_writeback
3023         sync
3024         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3025         BEFOREWRITES=`count_ost_writes`
3026         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3027         AFTERWRITES=`count_ost_writes`
3028         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3029                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3030         fi
3031         BEFOREWRITES=`count_ost_writes`
3032         sync || error "sync: $?"
3033         AFTERWRITES=`count_ost_writes`
3034         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3035                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3036         fi
3037         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3038         start_writeback
3039         return 0
3040 }
3041 run_test 42b "test destroy of file with cached dirty data ======"
3042
3043 # if these tests just want to test the effect of truncation,
3044 # they have to be very careful.  consider:
3045 # - the first open gets a {0,EOF}PR lock
3046 # - the first write conflicts and gets a {0, count-1}PW
3047 # - the rest of the writes are under {count,EOF}PW
3048 # - the open for truncate tries to match a {0,EOF}PR
3049 #   for the filesize and cancels the PWs.
3050 # any number of fixes (don't get {0,EOF} on open, match
3051 # composite locks, do smarter file size management) fix
3052 # this, but for now we want these tests to verify that
3053 # the cancellation with truncate intent works, so we
3054 # start the file with a full-file pw lock to match against
3055 # until the truncate.
3056 trunc_test() {
3057         test=$1
3058         file=$DIR/$test
3059         offset=$2
3060         cancel_lru_locks osc
3061         stop_writeback
3062         # prime the file with 0,EOF PW to match
3063         touch $file
3064         $TRUNCATE $file 0
3065         sync; sync
3066         # now the real test..
3067         dd if=/dev/zero of=$file bs=1024 count=100
3068         BEFOREWRITES=`count_ost_writes`
3069         $TRUNCATE $file $offset
3070         cancel_lru_locks osc
3071         AFTERWRITES=`count_ost_writes`
3072         start_writeback
3073 }
3074
3075 test_42c() {
3076         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3077         trunc_test 42c 1024
3078         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3079             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3080         rm $file
3081 }
3082 run_test 42c "test partial truncate of file with cached dirty data"
3083
3084 test_42d() {
3085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3086         trunc_test 42d 0
3087         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3088             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3089         rm $file
3090 }
3091 run_test 42d "test complete truncate of file with cached dirty data"
3092
3093 test_42e() { # bug22074
3094         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3095         local TDIR=$DIR/${tdir}e
3096         local pagesz=$(page_size)
3097         local pages=16 # hardcoded 16 pages, don't change it.
3098         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3099         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3100         local max_dirty_mb
3101         local warmup_files
3102
3103         test_mkdir -p $DIR/${tdir}e
3104         $SETSTRIPE -c 1 $TDIR
3105         createmany -o $TDIR/f $files
3106
3107         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3108
3109         # we assume that with $OSTCOUNT files, at least one of them will
3110         # be allocated on OST0.
3111         warmup_files=$((OSTCOUNT * max_dirty_mb))
3112         createmany -o $TDIR/w $warmup_files
3113
3114         # write a large amount of data into one file and sync, to get good
3115         # avail_grant number from OST.
3116         for ((i=0; i<$warmup_files; i++)); do
3117                 idx=$($GETSTRIPE -i $TDIR/w$i)
3118                 [ $idx -ne 0 ] && continue
3119                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3120                 break
3121         done
3122         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3123         sync
3124         $LCTL get_param $proc_osc0/cur_dirty_bytes
3125         $LCTL get_param $proc_osc0/cur_grant_bytes
3126
3127         # create as much dirty pages as we can while not to trigger the actual
3128         # RPCs directly. but depends on the env, VFS may trigger flush during this
3129         # period, hopefully we are good.
3130         for ((i=0; i<$warmup_files; i++)); do
3131                 idx=$($GETSTRIPE -i $TDIR/w$i)
3132                 [ $idx -ne 0 ] && continue
3133                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3134         done
3135         $LCTL get_param $proc_osc0/cur_dirty_bytes
3136         $LCTL get_param $proc_osc0/cur_grant_bytes
3137
3138         # perform the real test
3139         $LCTL set_param $proc_osc0/rpc_stats 0
3140         for ((;i<$files; i++)); do
3141                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3142                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3143         done
3144         sync
3145         $LCTL get_param $proc_osc0/rpc_stats
3146
3147         local percent=0
3148         local have_ppr=false
3149         $LCTL get_param $proc_osc0/rpc_stats |
3150                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3151                         # skip lines until we are at the RPC histogram data
3152                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3153                         $have_ppr || continue
3154
3155                         # we only want the percent stat for < 16 pages
3156                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3157
3158                         percent=$((percent + WPCT))
3159                         if [ $percent -gt 15 ]; then
3160                                 error "less than 16-pages write RPCs" \
3161                                       "$percent% > 15%"
3162                                 break
3163                         fi
3164                 done
3165         rm -rf $TDIR
3166 }
3167 run_test 42e "verify sub-RPC writes are not done synchronously"
3168
3169 test_43() {
3170         test_mkdir -p $DIR/$tdir
3171         cp -p /bin/ls $DIR/$tdir/$tfile
3172         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3173         pid=$!
3174         # give multiop a chance to open
3175         sleep 1
3176
3177         $DIR/$tdir/$tfile && error || true
3178         kill -USR1 $pid
3179 }
3180 run_test 43 "execution of file opened for write should return -ETXTBSY"
3181
3182 test_43a() {
3183         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3184         test_mkdir -p $DIR/$tdir
3185         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3186                         cp -p multiop $DIR/$tdir/multiop
3187         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3188                         return 1
3189         MULTIOP_PID=$!
3190         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3191         kill -USR1 $MULTIOP_PID || return 2
3192         wait $MULTIOP_PID || return 3
3193         rm $TMP/test43.junk
3194 }
3195 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3196
3197 test_43b() {
3198         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3199         test_mkdir -p $DIR/$tdir
3200         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3201                         cp -p multiop $DIR/$tdir/multiop
3202         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3203                         return 1
3204         MULTIOP_PID=$!
3205         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3206         kill -USR1 $MULTIOP_PID || return 2
3207         wait $MULTIOP_PID || return 3
3208         rm $TMP/test43.junk
3209 }
3210 run_test 43b "truncate of file being executed should return -ETXTBSY"
3211
3212 test_43c() {
3213         local testdir="$DIR/$tdir"
3214         test_mkdir -p $DIR/$tdir
3215         cp $SHELL $testdir/
3216         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3217                 ( cd $testdir && md5sum -c)
3218 }
3219 run_test 43c "md5sum of copy into lustre========================"
3220
3221 test_44() {
3222         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3223         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3224         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3225 }
3226 run_test 44 "zero length read from a sparse stripe ============="
3227
3228 test_44a() {
3229     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3230                          awk '{print $2}'`
3231     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3232     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3233     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3234                       awk '{print $2}'`
3235     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3236         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3237     fi
3238
3239     OFFSETS="0 $((stride/2)) $((stride-1))"
3240     for offset in $OFFSETS ; do
3241       for i in `seq 0 $((nstripe-1))`; do
3242         local GLOBALOFFSETS=""
3243         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3244         local myfn=$DIR/d44a-$size
3245         echo "--------writing $myfn at $size"
3246         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3247         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3248         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3249                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3250
3251         for j in `seq 0 $((nstripe-1))`; do
3252             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3253             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3254             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3255         done
3256         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3257                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3258         rm -f $myfn
3259       done
3260     done
3261 }
3262 run_test 44a "test sparse pwrite ==============================="
3263
3264 dirty_osc_total() {
3265         tot=0
3266         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3267                 tot=$(($tot + $d))
3268         done
3269         echo $tot
3270 }
3271 do_dirty_record() {
3272         before=`dirty_osc_total`
3273         echo executing "\"$*\""
3274         eval $*
3275         after=`dirty_osc_total`
3276         echo before $before, after $after
3277 }
3278 test_45() {
3279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3280         f="$DIR/f45"
3281         # Obtain grants from OST if it supports it
3282         echo blah > ${f}_grant
3283         stop_writeback
3284         sync
3285         do_dirty_record "echo blah > $f"
3286         [ $before -eq $after ] && error "write wasn't cached"
3287         do_dirty_record "> $f"
3288         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3289         do_dirty_record "echo blah > $f"
3290         [ $before -eq $after ] && error "write wasn't cached"
3291         do_dirty_record "sync"
3292         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3293         do_dirty_record "echo blah > $f"
3294         [ $before -eq $after ] && error "write wasn't cached"
3295         do_dirty_record "cancel_lru_locks osc"
3296         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3297         start_writeback
3298 }
3299 run_test 45 "osc io page accounting ============================"
3300
3301 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3302 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3303 # objects offset and an assert hit when an rpc was built with 1023's mapped
3304 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3305 test_46() {
3306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3307         f="$DIR/f46"
3308         stop_writeback
3309         sync
3310         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3311         sync
3312         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3313         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3314         sync
3315         start_writeback
3316 }
3317 run_test 46 "dirtying a previously written page ================"
3318
3319 # test_47 is removed "Device nodes check" is moved to test_28
3320
3321 test_48a() { # bug 2399
3322         check_kernel_version 34 || return 0
3323         test_mkdir -p $DIR/$tdir
3324         cd $DIR/$tdir
3325         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3326         test_mkdir $DIR/$tdir || error "recreate directory failed"
3327         touch foo || error "'touch foo' failed after recreating cwd"
3328         test_mkdir $DIR/$tdir/bar ||
3329                      error "'mkdir foo' failed after recreating cwd"
3330         if check_kernel_version 44; then
3331                 touch .foo || error "'touch .foo' failed after recreating cwd"
3332                 test_mkdir $DIR/$tdir/.bar ||
3333                               error "'mkdir .foo' failed after recreating cwd"
3334         fi
3335         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3336         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3337         cd . || error "'cd .' failed after recreating cwd"
3338         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3339         rmdir . && error "'rmdir .' worked after recreating cwd"
3340         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3341         cd .. || error "'cd ..' failed after recreating cwd"
3342 }
3343 run_test 48a "Access renamed working dir (should return errors)="
3344
3345 test_48b() { # bug 2399
3346         check_kernel_version 34 || return 0
3347         rm -rf $DIR/$tdir
3348         test_mkdir -p $DIR/$tdir
3349         cd $DIR/$tdir
3350         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3351         touch foo && error "'touch foo' worked after removing cwd"
3352         test_mkdir $DIR/$tdir/foo &&
3353                      error "'mkdir foo' worked after removing cwd"
3354         if check_kernel_version 44; then
3355                 touch .foo && error "'touch .foo' worked after removing cwd"
3356                 test_mkdir $DIR/$tdir/.foo &&
3357                               error "'mkdir .foo' worked after removing cwd"
3358         fi
3359         ls . > /dev/null && error "'ls .' worked after removing cwd"
3360         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3361         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3362         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3363         rmdir . && error "'rmdir .' worked after removing cwd"
3364         ln -s . foo && error "'ln -s .' worked after removing cwd"
3365         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3366 }
3367 run_test 48b "Access removed working dir (should return errors)="
3368
3369 test_48c() { # bug 2350
3370         check_kernel_version 36 || return 0
3371         #lctl set_param debug=-1
3372         #set -vx
3373         rm -rf $DIR/$tdir
3374         test_mkdir -p $DIR/$tdir/dir
3375         cd $DIR/$tdir/dir
3376         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3377         $TRACE touch foo && error "touch foo worked after removing cwd"
3378         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3379         if check_kernel_version 44; then
3380                 touch .foo && error "touch .foo worked after removing cwd"
3381                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3382         fi
3383         $TRACE ls . && error "'ls .' worked after removing cwd"
3384         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3385         is_patchless || ( $TRACE cd . &&
3386                         error "'cd .' worked after removing cwd" )
3387         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3388         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3389         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3390         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3391 }
3392 run_test 48c "Access removed working subdir (should return errors)"
3393
3394 test_48d() { # bug 2350
3395         check_kernel_version 36 || return 0
3396         #lctl set_param debug=-1
3397         #set -vx
3398         rm -rf $DIR/$tdir
3399         test_mkdir -p $DIR/$tdir/dir
3400         cd $DIR/$tdir/dir
3401         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3402         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3403         $TRACE touch foo && error "'touch foo' worked after removing parent"
3404         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3405         if check_kernel_version 44; then
3406                 touch .foo && error "'touch .foo' worked after removing parent"
3407                 test_mkdir .foo &&
3408                               error "mkdir .foo worked after removing parent"
3409         fi
3410         $TRACE ls . && error "'ls .' worked after removing parent"
3411         $TRACE ls .. && error "'ls ..' worked after removing parent"
3412         is_patchless || ( $TRACE cd . &&
3413                         error "'cd .' worked after recreate parent" )
3414         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3415         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3416         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3417         is_patchless || ( $TRACE cd .. &&
3418                         error "'cd ..' worked after removing parent" || true )
3419 }
3420 run_test 48d "Access removed parent subdir (should return errors)"
3421
3422 test_48e() { # bug 4134
3423         check_kernel_version 41 || return 0
3424         #lctl set_param debug=-1
3425         #set -vx
3426         rm -rf $DIR/$tdir
3427         test_mkdir -p $DIR/$tdir/dir
3428         cd $DIR/$tdir/dir
3429         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3430         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3431         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3432         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3433         # On a buggy kernel addition of "touch foo" after cd .. will
3434         # produce kernel oops in lookup_hash_it
3435         touch ../foo && error "'cd ..' worked after recreate parent"
3436         cd $DIR
3437         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3438 }
3439 run_test 48e "Access to recreated parent subdir (should return errors)"
3440
3441 test_49() { # LU-1030
3442         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3443         # get ost1 size - lustre-OST0000
3444         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3445         # write 800M at maximum
3446         [ $ost1_size -gt 819200 ] && ost1_size=819200
3447
3448         lfs setstripe -c 1 -i 0 $DIR/$tfile
3449         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3450         local dd_pid=$!
3451
3452         # change max_pages_per_rpc while writing the file
3453         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3454         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3455         # loop until dd process exits
3456         while ps ax -opid | grep -wq $dd_pid; do
3457                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3458                 sleep $((RANDOM % 5 + 1))
3459         done
3460         # restore original max_pages_per_rpc
3461         $LCTL set_param $osc1_mppc=$orig_mppc
3462         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3463 }
3464 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3465
3466 test_50() {
3467         # bug 1485
3468         test_mkdir $DIR/$tdir
3469         cd $DIR/$tdir
3470         ls /proc/$$/cwd || error
3471 }
3472 run_test 50 "special situations: /proc symlinks  ==============="
3473
3474 test_51a() {    # was test_51
3475         # bug 1516 - create an empty entry right after ".." then split dir
3476         test_mkdir $DIR/$tdir
3477         touch $DIR/$tdir/foo
3478         $MCREATE $DIR/$tdir/bar
3479         rm $DIR/$tdir/foo
3480         createmany -m $DIR/$tdir/longfile 201
3481         FNUM=202
3482         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3483                 $MCREATE $DIR/$tdir/longfile$FNUM
3484                 FNUM=$(($FNUM + 1))
3485                 echo -n "+"
3486         done
3487         echo
3488         ls -l $DIR/$tdir > /dev/null || error
3489 }
3490 run_test 51a "special situations: split htree with empty entry =="
3491
3492 export NUMTEST=70000
3493 test_51b() {
3494         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3495         local BASE=$DIR/$tdir
3496
3497         test_mkdir -p $BASE
3498
3499         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3500         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3501         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3502                 return
3503
3504         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3505                 echo "reduced count to $NUMTEST due to inodes"
3506
3507         # need to check free space for the directories as well
3508         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3509         numfree=$((blkfree / 4))
3510         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3511                 echo "reduced count to $NUMTEST due to blocks"
3512
3513         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3514                 echo "failed" > $BASE/fnum
3515 }
3516 run_test 51b "exceed 64k subdirectory nlink limit"
3517
3518 test_51ba() { # LU-993
3519         local BASE=$DIR/$tdir
3520         # unlink all but 100 subdirectories, then check it still works
3521         local LEFT=100
3522         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3523
3524         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3525         local DELETE=$((NUMTEST - LEFT))
3526
3527         # continue on to run this test even if 51b didn't finish,
3528         # just to delete the many subdirectories created.
3529         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3530
3531         # for ldiskfs the nlink count should be 1, but this is OSD specific
3532         # and so this is listed for informational purposes only
3533         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3534         unlinkmany -d $BASE/d $DELETE
3535         RC=$?
3536
3537         if [ $RC -ne 0 ]; then
3538                 if [ "$NUMPREV" == "failed" ]; then
3539                         skip "previous setup failed"
3540                         return 0
3541                 else
3542                         error "unlink of first $DELETE subdirs failed"
3543                         return $RC
3544                 fi
3545         fi
3546
3547         echo "nlink between: $(stat -c %h $BASE)"
3548         # trim the first line of ls output
3549         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3550         [ $FOUND -ne $LEFT ] &&
3551                 error "can't find subdirs: found only $FOUND/$LEFT"
3552
3553         unlinkmany -d $BASE/d $DELETE $LEFT ||
3554                 error "unlink of second $LEFT subdirs failed"
3555         # regardless of whether the backing filesystem tracks nlink accurately
3556         # or not, the nlink count shouldn't be more than "." and ".." here
3557         local AFTER=$(stat -c %h $BASE)
3558         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3559                 echo "nlink after: $AFTER"
3560 }
3561 run_test 51ba "verify nlink for many subdirectory cleanup"
3562
3563 test_51d() {
3564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3565         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3566         test_mkdir -p $DIR/$tdir
3567         createmany -o $DIR/$tdir/t- 1000
3568         $GETSTRIPE $DIR/$tdir > $TMP/files
3569         for N in `seq 0 $((OSTCOUNT - 1))`; do
3570             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3571             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3572             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3573         done
3574         unlinkmany $DIR/$tdir/t- 1000
3575
3576         NLAST=0
3577         for N in `seq 1 $((OSTCOUNT - 1))`; do
3578             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3579                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3580             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3581                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3582
3583             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3584                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3585             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3586                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3587             NLAST=$N
3588         done
3589 }
3590 run_test 51d "check object distribution ===================="
3591
3592 test_52a() {
3593         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3594         test_mkdir -p $DIR/$tdir
3595         touch $DIR/$tdir/foo
3596         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3597         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3598         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3599         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3600         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3601                                         error "link worked"
3602         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3603         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3604         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3605                                                      error "lsattr"
3606         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3607         cp -r $DIR/$tdir /tmp/
3608         rm -fr $DIR/$tdir || error "cleanup rm failed"
3609 }
3610 run_test 52a "append-only flag test (should return errors) ====="
3611
3612 test_52b() {
3613         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3614         test_mkdir -p $DIR/$tdir
3615         touch $DIR/$tdir/foo
3616         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3617         cat test > $DIR/$tdir/foo && error "cat test worked"
3618         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3619         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3620         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3621                                         error "link worked"
3622         echo foo >> $DIR/$tdir/foo && error "echo worked"
3623         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3624         [ -f $DIR/$tdir/foo ] || error
3625         [ -f $DIR/$tdir/foo_ren ] && error
3626         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3627                                                         error "lsattr"
3628         chattr -i $DIR/$tdir/foo || error "chattr failed"
3629
3630         rm -fr $DIR/$tdir || error
3631 }
3632 run_test 52b "immutable flag test (should return errors) ======="
3633
3634 test_53() {
3635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3636         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3637         remote_ost_nodsh && skip "remote OST with nodsh" && return
3638
3639         local param
3640         local ostname
3641         local mds_last
3642         local ost_last
3643         local ostnum
3644         local node
3645
3646         # only test MDT0000
3647         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3648         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3649                 param=`echo ${value[0]} | cut -d "=" -f1`
3650                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3651                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3652                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3653                 node=$(facet_active_host ost$((ostnum+1)))
3654                 param="obdfilter.$ostname.last_id"
3655                 ost_last=$(do_node $node lctl get_param -n $param | head -n 1)
3656                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3657                 if [ $ost_last != $mds_last ]; then
3658                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3659                 fi
3660         done
3661 }
3662 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3663
3664 test_54a() {
3665         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3666         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3667         $SOCKETSERVER $DIR/socket
3668         $SOCKETCLIENT $DIR/socket || error
3669         $MUNLINK $DIR/socket
3670 }
3671 run_test 54a "unix domain socket test =========================="
3672
3673 test_54b() {
3674         f="$DIR/f54b"
3675         mknod $f c 1 3
3676         chmod 0666 $f
3677         dd if=/dev/zero of=$f bs=`page_size` count=1
3678 }
3679 run_test 54b "char device works in lustre ======================"
3680
3681 find_loop_dev() {
3682         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3683         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3684         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3685
3686         for i in `seq 3 7`; do
3687                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3688                 LOOPDEV=$LOOPBASE$i
3689                 LOOPNUM=$i
3690                 break
3691         done
3692 }
3693
3694 test_54c() {
3695         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3696         tfile="$DIR/f54c"
3697         tdir="$DIR/d54c"
3698         loopdev="$DIR/loop54c"
3699
3700         find_loop_dev
3701         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3702         mknod $loopdev b 7 $LOOPNUM
3703         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3704         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3705         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3706         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3707         test_mkdir -p $tdir
3708         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3709         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3710         df $tdir
3711         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3712         $UMOUNT $tdir
3713         losetup -d $loopdev
3714         rm $loopdev
3715 }
3716 run_test 54c "block device works in lustre ====================="
3717
3718 test_54d() {
3719         f="$DIR/f54d"
3720         string="aaaaaa"
3721         mknod $f p
3722         [ "$string" = `echo $string > $f | cat $f` ] || error
3723 }
3724 run_test 54d "fifo device works in lustre ======================"
3725
3726 test_54e() {
3727         check_kernel_version 46 || return 0
3728         f="$DIR/f54e"
3729         string="aaaaaa"
3730         cp -aL /dev/console $f
3731         echo $string > $f || error
3732 }
3733 run_test 54e "console/tty device works in lustre ======================"
3734
3735 #The test_55 used to be iopen test and it was removed by bz#24037.
3736 #run_test 55 "check iopen_connect_dentry() ======================"
3737
3738 test_56a() {    # was test_56
3739         rm -rf $DIR/$tdir
3740         $SETSTRIPE -d $DIR
3741         test_mkdir $DIR/$tdir
3742         test_mkdir $DIR/$tdir/dir
3743         NUMFILES=3
3744         NUMFILESx2=$(($NUMFILES * 2))
3745         for i in `seq 1 $NUMFILES` ; do
3746                 touch $DIR/$tdir/file$i
3747                 touch $DIR/$tdir/dir/file$i
3748         done
3749
3750         # test lfs getstripe with --recursive
3751         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3752         [ $FILENUM -eq $NUMFILESx2 ] ||
3753                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3754         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3755         [ $FILENUM -eq $NUMFILES ] ||
3756                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3757         echo "$GETSTRIPE --recursive passed."
3758
3759         # test lfs getstripe with file instead of dir
3760         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3761         [ $FILENUM  -eq 1 ] || error \
3762                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3763         echo "$GETSTRIPE file1 passed."
3764
3765         #test lfs getstripe with --verbose
3766         [ `$GETSTRIPE --verbose $DIR/$tdir |
3767                         grep -c lmm_magic` -eq $NUMFILES ] ||
3768                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3769         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3770             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3771         echo "$GETSTRIPE --verbose passed."
3772
3773         #test lfs getstripe with --obd
3774         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3775                                         grep -q "unknown obduuid" ||
3776                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3777
3778         [  "$OSTCOUNT" -lt 2 ] &&
3779                 skip_env "skipping other $GETSTRIPE --obd test" && return
3780
3781         OSTIDX=1
3782         OBDUUID=$(ostuuid_from_index $OSTIDX)
3783         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3784         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3785         [ $FOUND -eq $FILENUM ] ||
3786                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3787         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3788                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3789                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3790                 error "$GETSTRIPE --obd: should not show file on other obd"
3791         echo "$GETSTRIPE --obd passed"
3792 }
3793 run_test 56a "check $GETSTRIPE"
3794
3795 NUMFILES=3
3796 NUMDIRS=3
3797 setup_56() {
3798         local LOCAL_NUMFILES="$1"
3799         local LOCAL_NUMDIRS="$2"
3800         local MKDIR_PARAMS="$3"
3801
3802         if [ ! -d "$TDIR" ] ; then
3803                 test_mkdir -p $TDIR
3804                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3805                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3806                         touch $TDIR/file$i
3807                 done
3808                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3809                         test_mkdir $TDIR/dir$i
3810                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3811                                 touch $TDIR/dir$i/file$j
3812                         done
3813                 done
3814         fi
3815 }
3816
3817 setup_56_special() {
3818         LOCAL_NUMFILES=$1
3819         LOCAL_NUMDIRS=$2
3820         setup_56 $1 $2
3821         if [ ! -e "$TDIR/loop1b" ] ; then
3822                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3823                         mknod $TDIR/loop${i}b b 7 $i
3824                         mknod $TDIR/null${i}c c 1 3
3825                         ln -s $TDIR/file1 $TDIR/link${i}l
3826                 done
3827                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3828                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3829                         mknod $TDIR/dir$i/null${i}c c 1 3
3830                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3831                 done
3832         fi
3833 }
3834
3835 test_56g() {
3836         $SETSTRIPE -d $DIR
3837
3838         TDIR=$DIR/${tdir}g
3839         setup_56 $NUMFILES $NUMDIRS
3840
3841         EXPECTED=$(($NUMDIRS + 2))
3842         # test lfs find with -name
3843         for i in $(seq 1 $NUMFILES) ; do
3844                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3845                 [ $NUMS -eq $EXPECTED ] ||
3846                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3847                               "found $NUMS, expected $EXPECTED"
3848         done
3849 }
3850 run_test 56g "check lfs find -name ============================="
3851
3852 test_56h() {
3853         $SETSTRIPE -d $DIR
3854
3855         TDIR=$DIR/${tdir}g
3856         setup_56 $NUMFILES $NUMDIRS
3857
3858         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3859         # test lfs find with ! -name
3860         for i in $(seq 1 $NUMFILES) ; do
3861                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3862                 [ $NUMS -eq $EXPECTED ] ||
3863                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3864                               "found $NUMS, expected $EXPECTED"
3865         done
3866 }
3867 run_test 56h "check lfs find ! -name ============================="
3868
3869 test_56i() {
3870        tdir=${tdir}i
3871        test_mkdir -p $DIR/$tdir
3872        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3873        CMD="$LFIND -ost $UUID $DIR/$tdir"
3874        OUT=$($CMD)
3875        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3876 }
3877 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3878
3879 test_56j() {
3880         TDIR=$DIR/${tdir}g
3881         setup_56_special $NUMFILES $NUMDIRS
3882
3883         EXPECTED=$((NUMDIRS + 1))
3884         CMD="$LFIND -type d $TDIR"
3885         NUMS=$($CMD | wc -l)
3886         [ $NUMS -eq $EXPECTED ] ||
3887                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3888 }
3889 run_test 56j "check lfs find -type d ============================="
3890
3891 test_56k() {
3892         TDIR=$DIR/${tdir}g
3893         setup_56_special $NUMFILES $NUMDIRS
3894
3895         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3896         CMD="$LFIND -type f $TDIR"
3897         NUMS=$($CMD | wc -l)
3898         [ $NUMS -eq $EXPECTED ] ||
3899                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3900 }
3901 run_test 56k "check lfs find -type f ============================="
3902
3903 test_56l() {
3904         TDIR=$DIR/${tdir}g
3905         setup_56_special $NUMFILES $NUMDIRS
3906
3907         EXPECTED=$((NUMDIRS + NUMFILES))
3908         CMD="$LFIND -type b $TDIR"
3909         NUMS=$($CMD | wc -l)
3910         [ $NUMS -eq $EXPECTED ] ||
3911                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3912 }
3913 run_test 56l "check lfs find -type b ============================="
3914
3915 test_56m() {
3916         TDIR=$DIR/${tdir}g
3917         setup_56_special $NUMFILES $NUMDIRS
3918
3919         EXPECTED=$((NUMDIRS + NUMFILES))
3920         CMD="$LFIND -type c $TDIR"
3921         NUMS=$($CMD | wc -l)
3922         [ $NUMS -eq $EXPECTED ] ||
3923                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3924 }
3925 run_test 56m "check lfs find -type c ============================="
3926
3927 test_56n() {
3928         TDIR=$DIR/${tdir}g
3929         setup_56_special $NUMFILES $NUMDIRS
3930
3931         EXPECTED=$((NUMDIRS + NUMFILES))
3932         CMD="$LFIND -type l $TDIR"
3933         NUMS=$($CMD | wc -l)
3934         [ $NUMS -eq $EXPECTED ] ||
3935                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3936 }
3937 run_test 56n "check lfs find -type l ============================="
3938
3939 test_56o() {
3940         TDIR=$DIR/${tdir}o
3941         setup_56 $NUMFILES $NUMDIRS
3942
3943         utime $TDIR/file1 > /dev/null || error "utime (1)"
3944         utime $TDIR/file2 > /dev/null || error "utime (2)"
3945         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3946         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3947         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3948         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3949
3950         EXPECTED=4
3951         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3952         [ $NUMS -eq $EXPECTED ] || \
3953                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3954
3955         EXPECTED=12
3956         CMD="$LFIND -mtime 0 $TDIR"
3957         NUMS=$($CMD | wc -l)
3958         [ $NUMS -eq $EXPECTED ] ||
3959                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3960 }
3961 run_test 56o "check lfs find -mtime for old files =========================="
3962
3963 test_56p() {
3964         [ $RUNAS_ID -eq $UID ] &&
3965                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3966
3967         TDIR=$DIR/${tdir}p
3968         setup_56 $NUMFILES $NUMDIRS
3969
3970         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3971         EXPECTED=$NUMFILES
3972         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3973         NUMS=$($CMD | wc -l)
3974         [ $NUMS -eq $EXPECTED ] || \
3975                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3976
3977         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3978         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3979         NUMS=$($CMD | wc -l)
3980         [ $NUMS -eq $EXPECTED ] || \
3981                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3982 }
3983 run_test 56p "check lfs find -uid and ! -uid ==============================="
3984
3985 test_56q() {
3986         [ $RUNAS_ID -eq $UID ] &&
3987                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3988
3989         TDIR=$DIR/${tdir}q
3990         setup_56 $NUMFILES $NUMDIRS
3991
3992         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3993
3994         EXPECTED=$NUMFILES
3995         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3996         NUMS=$($CMD | wc -l)
3997         [ $NUMS -eq $EXPECTED ] ||
3998                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3999
4000         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4001         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4002         NUMS=$($CMD | wc -l)
4003         [ $NUMS -eq $EXPECTED ] ||
4004                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4005 }
4006 run_test 56q "check lfs find -gid and ! -gid ==============================="
4007
4008 test_56r() {
4009         TDIR=$DIR/${tdir}r
4010         setup_56 $NUMFILES $NUMDIRS
4011
4012         EXPECTED=12
4013         CMD="$LFIND -size 0 -type f $TDIR"
4014         NUMS=$($CMD | wc -l)
4015         [ $NUMS -eq $EXPECTED ] ||
4016                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4017         EXPECTED=0
4018         CMD="$LFIND ! -size 0 -type f $TDIR"
4019         NUMS=$($CMD | wc -l)
4020         [ $NUMS -eq $EXPECTED ] ||
4021                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4022         echo "test" > $TDIR/$tfile
4023         echo "test2" > $TDIR/$tfile.2 && sync
4024         EXPECTED=1
4025         CMD="$LFIND -size 5 -type f $TDIR"
4026         NUMS=$($CMD | wc -l)
4027         [ $NUMS -eq $EXPECTED ] ||
4028                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4029         EXPECTED=1
4030         CMD="$LFIND -size +5 -type f $TDIR"
4031         NUMS=$($CMD | wc -l)
4032         [ $NUMS -eq $EXPECTED ] ||
4033                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4034         EXPECTED=2
4035         CMD="$LFIND -size +0 -type f $TDIR"
4036         NUMS=$($CMD | wc -l)
4037         [ $NUMS -eq $EXPECTED ] ||
4038                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4039         EXPECTED=2
4040         CMD="$LFIND ! -size -5 -type f $TDIR"
4041         NUMS=$($CMD | wc -l)
4042         [ $NUMS -eq $EXPECTED ] ||
4043                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4044         EXPECTED=12
4045         CMD="$LFIND -size -5 -type f $TDIR"
4046         NUMS=$($CMD | wc -l)
4047         [ $NUMS -eq $EXPECTED ] ||
4048                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4049 }
4050 run_test 56r "check lfs find -size works =========================="
4051
4052 test_56s() { # LU-611
4053         TDIR=$DIR/${tdir}s
4054         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4055
4056         if [ $OSTCOUNT -gt 1 ]; then
4057                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4058                 ONESTRIPE=4
4059                 EXTRA=4
4060         else
4061                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4062                 EXTRA=0
4063         fi
4064
4065         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4066         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4067         NUMS=$($CMD | wc -l)
4068         [ $NUMS -eq $EXPECTED ] ||
4069                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4070
4071         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4072         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4073         NUMS=$($CMD | wc -l)
4074         [ $NUMS -eq $EXPECTED ] ||
4075                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4076
4077         EXPECTED=$ONESTRIPE
4078         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4079         NUMS=$($CMD | wc -l)
4080         [ $NUMS -eq $EXPECTED ] ||
4081                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4082
4083         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4084         NUMS=$($CMD | wc -l)
4085         [ $NUMS -eq $EXPECTED ] ||
4086                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4087
4088         EXPECTED=0
4089         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4090         NUMS=$($CMD | wc -l)
4091         [ $NUMS -eq $EXPECTED ] ||
4092                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4093 }
4094 run_test 56s "check lfs find -stripe-count works"
4095
4096 test_56t() { # LU-611
4097         TDIR=$DIR/${tdir}t
4098         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4099
4100         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4101
4102         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4103         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4104         NUMS=$($CMD | wc -l)
4105         [ $NUMS -eq $EXPECTED ] ||
4106                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4107
4108         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4109         NUMS=$($CMD | wc -l)
4110         [ $NUMS -eq $EXPECTED ] ||
4111                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4112
4113         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4114         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4115         NUMS=$($CMD | wc -l)
4116         [ $NUMS -eq $EXPECTED ] ||
4117                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4118
4119         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4120         NUMS=$($CMD | wc -l)
4121         [ $NUMS -eq $EXPECTED ] ||
4122                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4123
4124         EXPECTED=4
4125         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4126         NUMS=$($CMD | wc -l)
4127         [ $NUMS -eq $EXPECTED ] ||
4128                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4129
4130         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4131         NUMS=$($CMD | wc -l)
4132         [ $NUMS -eq $EXPECTED ] ||
4133                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4134
4135         EXPECTED=0
4136         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4137         NUMS=$($CMD | wc -l)
4138         [ $NUMS -eq $EXPECTED ] ||
4139                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4140 }
4141 run_test 56t "check lfs find -stripe-size works"
4142
4143 test_56u() { # LU-611
4144         TDIR=$DIR/${tdir}u
4145         setup_56 $NUMFILES $NUMDIRS "-i 0"
4146
4147         if [ $OSTCOUNT -gt 1 ]; then
4148                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4149                 ONESTRIPE=4
4150         else
4151                 ONESTRIPE=0
4152         fi
4153
4154         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4155         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4156         NUMS=$($CMD | wc -l)
4157         [ $NUMS -eq $EXPECTED ] ||
4158                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4159
4160         EXPECTED=$ONESTRIPE
4161         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4162         NUMS=$($CMD | wc -l)
4163         [ $NUMS -eq $EXPECTED ] ||
4164                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4165
4166         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4167         NUMS=$($CMD | wc -l)
4168         [ $NUMS -eq $EXPECTED ] ||
4169                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4170
4171         EXPECTED=0
4172         # This should produce an error and not return any files
4173         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4174         NUMS=$($CMD 2>/dev/null | wc -l)
4175         [ $NUMS -eq $EXPECTED ] ||
4176                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4177
4178         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4179         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4180         NUMS=$($CMD | wc -l)
4181         [ $NUMS -eq $EXPECTED ] ||
4182                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4183 }
4184 run_test 56u "check lfs find -stripe-index works"
4185
4186 test_56v() {
4187     local MDT_IDX=0
4188
4189     TDIR=$DIR/${tdir}v
4190     rm -rf $TDIR
4191     setup_56 $NUMFILES $NUMDIRS
4192
4193     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4194     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4195
4196     for file in $($LFIND -mdt $UUID $TDIR); do
4197         file_mdt_idx=$($GETSTRIPE -M $file)
4198         [ $file_mdt_idx -eq $MDT_IDX ] ||
4199             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4200     done
4201 }
4202 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4203
4204 # Get and check the actual stripe count of one file.
4205 # Usage: check_stripe_count <file> <expected_stripe_count>
4206 check_stripe_count() {
4207     local file=$1
4208     local expected=$2
4209     local actual
4210
4211     [[ -z "$file" || -z "$expected" ]] &&
4212         error "check_stripe_count: invalid argument!"
4213
4214     local cmd="$GETSTRIPE -c $file"
4215     actual=$($cmd) || error "$cmd failed"
4216     actual=${actual%% *}
4217
4218     if [[ $actual -ne $expected ]]; then
4219         [[ $expected -eq -1 ]] ||
4220             error "$cmd wrong: found $actual, expected $expected"
4221         [[ $actual -eq $OSTCOUNT ]] ||
4222             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4223     fi
4224 }
4225
4226 test_56w() {
4227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4228         TDIR=$DIR/${tdir}w
4229
4230     rm -rf $TDIR || error "remove $TDIR failed"
4231     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4232
4233     local stripe_size
4234     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4235         error "$GETSTRIPE -S -d $TDIR failed"
4236     stripe_size=${stripe_size%% *}
4237
4238     local file_size=$((stripe_size * OSTCOUNT))
4239     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4240     local required_space=$((file_num * file_size))
4241     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4242     [[ $free_space -le $((required_space / 1024)) ]] &&
4243         skip_env "need at least $required_space bytes free space," \
4244                  "have $free_space kbytes" && return
4245
4246     local dd_bs=65536
4247     local dd_count=$((file_size / dd_bs))
4248
4249     # write data into the files
4250     local i
4251     local j
4252     local file
4253     for i in $(seq 1 $NUMFILES); do
4254         file=$TDIR/file$i
4255         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4256             error "write data into $file failed"
4257     done
4258     for i in $(seq 1 $NUMDIRS); do
4259         for j in $(seq 1 $NUMFILES); do
4260             file=$TDIR/dir$i/file$j
4261             yes | dd bs=$dd_bs count=$dd_count of=$file \
4262                 >/dev/null 2>&1 ||
4263                 error "write data into $file failed"
4264         done
4265     done
4266
4267     local expected=-1
4268     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4269
4270     # lfs_migrate file
4271     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4272     echo "$cmd"
4273     eval $cmd || error "$cmd failed"
4274
4275     check_stripe_count $TDIR/file1 $expected
4276
4277     # lfs_migrate dir
4278     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4279     echo "$cmd"
4280     eval $cmd || error "$cmd failed"
4281
4282     for j in $(seq 1 $NUMFILES); do
4283         check_stripe_count $TDIR/dir1/file$j $expected
4284     done
4285
4286     # lfs_migrate works with lfs find
4287     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4288          $LFS_MIGRATE -y -c $expected"
4289     echo "$cmd"
4290     eval $cmd || error "$cmd failed"
4291
4292     for i in $(seq 2 $NUMFILES); do
4293         check_stripe_count $TDIR/file$i $expected
4294     done
4295     for i in $(seq 2 $NUMDIRS); do
4296         for j in $(seq 1 $NUMFILES); do
4297             check_stripe_count $TDIR/dir$i/file$j $expected
4298         done
4299     done
4300 }
4301 run_test 56w "check lfs_migrate -c stripe_count works"
4302
4303 test_57a() {
4304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4305         # note test will not do anything if MDS is not local
4306         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4307                 skip "Only applicable to ldiskfs-based MDTs"
4308                 return
4309         fi
4310
4311         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4312         local MNTDEV="osd*.*MDT*.mntdev"
4313         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4314         [ -z "$DEV" ] && error "can't access $MNTDEV"
4315         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4316                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4317                         error "can't access $DEV"
4318                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4319                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4320                 rm $TMP/t57a.dump
4321         done
4322 }
4323 run_test 57a "verify MDS filesystem created with large inodes =="
4324
4325 test_57b() {
4326         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4327         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4328                 skip "Only applicable to ldiskfs-based MDTs"
4329                 return
4330         fi
4331
4332         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4333         local dir=$DIR/d57b
4334
4335         local FILECOUNT=100
4336         local FILE1=$dir/f1
4337         local FILEN=$dir/f$FILECOUNT
4338
4339         rm -rf $dir || error "removing $dir"
4340         test_mkdir -p $dir || error "creating $dir"
4341         local num=$(get_mds_dir $dir)
4342         local mymds=mds$num
4343
4344         echo "mcreating $FILECOUNT files"
4345         createmany -m $dir/f 1 $FILECOUNT || \
4346                 error "creating files in $dir"
4347
4348         # verify that files do not have EAs yet
4349         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4350         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4351
4352         sync
4353         sleep 1
4354         df $dir  #make sure we get new statfs data
4355         local MDSFREE=$(do_facet $mymds \
4356                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4357         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4358         echo "opening files to create objects/EAs"
4359         local FILE
4360         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4361                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4362         done
4363
4364         # verify that files have EAs now
4365         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4366         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4367
4368         sleep 1  #make sure we get new statfs data
4369         df $dir
4370         local MDSFREE2=$(do_facet $mymds \
4371                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4372         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4373         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4374                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4375                         error "MDC before $MDCFREE != after $MDCFREE2"
4376                 else
4377                         echo "MDC before $MDCFREE != after $MDCFREE2"
4378                         echo "unable to confirm if MDS has large inodes"
4379                 fi
4380         fi
4381         rm -rf $dir
4382 }
4383 run_test 57b "default LOV EAs are stored inside large inodes ==="
4384
4385 test_58() {
4386         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4387         [ -z "$(which wiretest 2>/dev/null)" ] &&
4388                         skip_env "could not find wiretest" && return
4389         wiretest
4390 }
4391 run_test 58 "verify cross-platform wire constants =============="
4392
4393 test_59() {
4394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4395         echo "touch 130 files"
4396         createmany -o $DIR/f59- 130
4397         echo "rm 130 files"
4398         unlinkmany $DIR/f59- 130
4399         sync
4400         # wait for commitment of removal
4401         wait_delete_completed
4402 }
4403 run_test 59 "verify cancellation of llog records async ========="
4404
4405 TEST60_HEAD="test_60 run $RANDOM"
4406 test_60a() {
4407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4408         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4409         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4410         log "$TEST60_HEAD - from kernel mode"
4411         do_facet mgs sh run-llog.sh
4412 }
4413 run_test 60a "llog sanity tests run from kernel module =========="
4414
4415 test_60b() { # bug 6411
4416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4417         dmesg > $DIR/$tfile
4418         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4419                                  /llog.test/ {
4420                                          if (marker)
4421                                                  from_marker++
4422                                          from_begin++
4423                                  }
4424                                  END {
4425                                          if (marker)
4426                                                  print from_marker
4427                                          else
4428                                                  print from_begin
4429                                  }"`
4430         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4431 }
4432 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4433
4434 test_60c() {
4435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4436         echo "create 5000 files"
4437         createmany -o $DIR/f60c- 5000
4438 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4439         lctl set_param fail_loc=0x80000137
4440         unlinkmany $DIR/f60c- 5000
4441         lctl set_param fail_loc=0
4442 }
4443 run_test 60c "unlink file when mds full"
4444
4445 test_60d() {
4446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4447         SAVEPRINTK=$(lctl get_param -n printk)
4448
4449         # verify "lctl mark" is even working"
4450         MESSAGE="test message ID $RANDOM $$"
4451         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4452         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4453
4454         lctl set_param printk=0 || error "set lnet.printk failed"
4455         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4456         MESSAGE="new test message ID $RANDOM $$"
4457         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4458         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4459         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4460
4461         lctl set_param -n printk="$SAVEPRINTK"
4462 }
4463 run_test 60d "test printk console message masking"
4464
4465 test_61() {
4466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4467         f="$DIR/f61"
4468         dd if=/dev/zero of=$f bs=`page_size` count=1
4469         cancel_lru_locks osc
4470         $MULTIOP $f OSMWUc || error
4471         sync
4472 }
4473 run_test 61 "mmap() writes don't make sync hang ================"
4474
4475 # bug 2330 - insufficient obd_match error checking causes LBUG
4476 test_62() {
4477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4478         f="$DIR/f62"
4479         echo foo > $f
4480         cancel_lru_locks osc
4481         lctl set_param fail_loc=0x405
4482         cat $f && error "cat succeeded, expect -EIO"
4483         lctl set_param fail_loc=0
4484 }
4485 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4486 # match every page all of the time.
4487 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4488
4489 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4490 test_63a() {    # was test_63
4491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4492         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4493         lctl set_param -n osc.*.max_dirty_mb 0
4494         for i in `seq 10` ; do
4495                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4496                 sleep 5
4497                 kill $!
4498                 sleep 1
4499         done
4500
4501         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4502         rm -f $DIR/f63 || true
4503 }
4504 run_test 63a "Verify oig_wait interruption does not crash ======="
4505
4506 # bug 2248 - async write errors didn't return to application on sync
4507 # bug 3677 - async write errors left page locked
4508 test_63b() {
4509         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4510         debugsave
4511         lctl set_param debug=-1
4512
4513         # ensure we have a grant to do async writes
4514         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4515         rm $DIR/$tfile
4516
4517         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4518         lctl set_param fail_loc=0x80000406
4519         $MULTIOP $DIR/$tfile Owy && \
4520                 error "sync didn't return ENOMEM"
4521         sync; sleep 2; sync     # do a real sync this time to flush page
4522         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4523                 error "locked page left in cache after async error" || true
4524         debugrestore
4525 }
4526 run_test 63b "async write errors should be returned to fsync ==="
4527
4528 test_64a () {
4529         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4530         df $DIR
4531         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4532 }
4533 run_test 64a "verify filter grant calculations (in kernel) ====="
4534
4535 test_64b () {
4536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4537         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4538         sh oos.sh $MOUNT
4539 }
4540 run_test 64b "check out-of-space detection on client ==========="
4541
4542 test_64c() {
4543         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4544 }
4545 run_test 64c "verify grant shrink ========================------"
4546
4547 # bug 1414 - set/get directories' stripe info
4548 test_65a() {
4549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4550         test_mkdir -p $DIR/$tdir
4551         touch $DIR/$tdir/f1
4552         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4553 }
4554 run_test 65a "directory with no stripe info ===================="
4555
4556 test_65b() {
4557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4558         test_mkdir -p $DIR/$tdir
4559         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4560                                                 error "setstripe"
4561         touch $DIR/$tdir/f2
4562         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4563 }
4564 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4565
4566 test_65c() {
4567         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4568         if [ $OSTCOUNT -gt 1 ]; then
4569                 test_mkdir -p $DIR/$tdir
4570                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4571                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4572                 touch $DIR/$tdir/f3
4573                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4574         fi
4575 }
4576 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4577
4578 test_65d() {
4579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4580         test_mkdir -p $DIR/$tdir
4581         if [ $STRIPECOUNT -le 0 ]; then
4582                 sc=1
4583         elif [ $STRIPECOUNT -gt 2000 ]; then
4584 #LOV_MAX_STRIPE_COUNT is 2000
4585                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4586         else
4587                 sc=$(($STRIPECOUNT - 1))
4588         fi
4589         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4590         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4591         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4592                                                 error "lverify failed"
4593 }
4594 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4595
4596 test_65e() {
4597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4598         test_mkdir -p $DIR/$tdir
4599
4600         $SETSTRIPE $DIR/$tdir || error "setstripe"
4601         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4602                                         error "no stripe info failed"
4603         touch $DIR/$tdir/f6
4604         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4605 }
4606 run_test 65e "directory setstripe defaults ======================="
4607
4608 test_65f() {
4609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4610         test_mkdir -p $DIR/${tdir}f
4611         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4612 }
4613 run_test 65f "dir setstripe permission (should return error) ==="
4614
4615 test_65g() {
4616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4617         test_mkdir -p $DIR/$tdir
4618         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4619                                                         error "setstripe"
4620         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4621         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4622                 error "delete default stripe failed"
4623 }
4624 run_test 65g "directory setstripe -d ==========================="
4625
4626 test_65h() {
4627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4628         test_mkdir -p $DIR/$tdir
4629         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4630                                                         error "setstripe"
4631         test_mkdir -p $DIR/$tdir/dd1
4632         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4633                 error "stripe info inherit failed"
4634 }
4635 run_test 65h "directory stripe info inherit ===================="
4636
4637 test_65i() { # bug6367
4638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4639         $SETSTRIPE -S 65536 -c -1 $MOUNT
4640 }
4641 run_test 65i "set non-default striping on root directory (bug 6367)="
4642
4643 test_65ia() { # bug12836
4644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4645         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4646 }
4647 run_test 65ia "getstripe on -1 default directory striping"
4648
4649 test_65ib() { # bug12836
4650         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4651         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4652 }
4653 run_test 65ib "getstripe -v on -1 default directory striping"
4654
4655 test_65ic() { # bug12836
4656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4657         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4658 }
4659 run_test 65ic "new find on -1 default directory striping"
4660
4661 test_65j() { # bug6367
4662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4663         sync; sleep 1
4664         # if we aren't already remounting for each test, do so for this test
4665         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4666                 cleanup || error "failed to unmount"
4667                 setup
4668         fi
4669         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4670 }
4671 run_test 65j "set default striping on root directory (bug 6367)="
4672
4673 test_65k() { # bug11679
4674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4675         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4676         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4677
4678     echo "Check OST status: "
4679     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4680               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4681
4682     for OSC in $MDS_OSCS; do
4683         echo $OSC "is activate"
4684         do_facet $SINGLEMDS lctl --device %$OSC activate
4685     done
4686
4687     mkdir -p $DIR/$tdir
4688     for INACTIVE_OSC in $MDS_OSCS; do
4689         echo "Deactivate: " $INACTIVE_OSC
4690         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4691         for STRIPE_OSC in $MDS_OSCS; do
4692             OST=`osc_to_ost $STRIPE_OSC`
4693             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4694                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4695
4696             [ -f $DIR/$tdir/$IDX ] && continue
4697             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4698             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4699             RC=$?
4700             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4701         done
4702         rm -f $DIR/$tdir/*
4703         echo $INACTIVE_OSC "is Activate."
4704         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4705     done
4706 }
4707 run_test 65k "validate manual striping works properly with deactivated OSCs"
4708
4709 test_65l() { # bug 12836
4710         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4711         test_mkdir -p $DIR/$tdir/test_dir
4712         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4713         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4714 }
4715 run_test 65l "lfs find on -1 stripe dir ========================"
4716
4717 # bug 2543 - update blocks count on client
4718 test_66() {
4719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4720         COUNT=${COUNT:-8}
4721         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4722         sync; sync_all_data; sync; sync_all_data
4723         cancel_lru_locks osc
4724         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4725         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4726 }
4727 run_test 66 "update inode blocks count on client ==============="
4728
4729 LLOOP=
4730 LLITELOOPLOAD=
4731 cleanup_68() {
4732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4733         trap 0
4734         if [ ! -z "$LLOOP" ]; then
4735                 if swapon -s | grep -q $LLOOP; then
4736                         swapoff $LLOOP || error "swapoff failed"
4737                 fi
4738
4739                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4740                 rm -f $LLOOP
4741                 unset LLOOP
4742         fi
4743         if [ ! -z "$LLITELOOPLOAD" ]; then
4744                 rmmod llite_lloop
4745                 unset LLITELOOPLOAD
4746         fi
4747         rm -f $DIR/f68*
4748 }
4749
4750 meminfo() {
4751         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4752 }
4753
4754 swap_used() {
4755         swapon -s | awk '($1 == "'$1'") { print $4 }'
4756 }
4757
4758 # test case for lloop driver, basic function
4759 test_68a() {
4760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4761         [ "$UID" != 0 ] && skip_env "must run as root" && return
4762         llite_lloop_enabled || \
4763                 { skip_env "llite_lloop module disabled" && return; }
4764
4765         trap cleanup_68 EXIT
4766
4767         if ! module_loaded llite_lloop; then
4768                 if load_module llite/llite_lloop; then
4769                         LLITELOOPLOAD=yes
4770                 else
4771                         skip_env "can't find module llite_lloop"
4772                         return
4773                 fi
4774         fi
4775
4776         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4777         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4778         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4779
4780         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4781         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4782
4783         cleanup_68
4784 }
4785 run_test 68a "lloop driver - basic test ========================"
4786
4787 # excercise swapping to lustre by adding a high priority swapfile entry
4788 # and then consuming memory until it is used.
4789 test_68b() {  # was test_68
4790         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4791         [ "$UID" != 0 ] && skip_env "must run as root" && return
4792         lctl get_param -n devices | grep -q obdfilter && \
4793                 skip "local OST" && return
4794
4795         grep -q llite_lloop /proc/modules
4796         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4797
4798         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4799                 skip "can't reliably test swap with TCP" && return
4800
4801         MEMTOTAL=`meminfo MemTotal`
4802         NR_BLOCKS=$((MEMTOTAL>>8))
4803         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4804
4805         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4806         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4807         mkswap $DIR/f68b
4808
4809         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4810
4811         trap cleanup_68 EXIT
4812
4813         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4814
4815         echo "before: `swapon -s | grep $LLOOP`"
4816         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4817         echo "after: `swapon -s | grep $LLOOP`"
4818         SWAPUSED=`swap_used $LLOOP`
4819
4820         cleanup_68
4821
4822         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4823 }
4824 run_test 68b "support swapping to Lustre ========================"
4825
4826 # bug5265, obdfilter oa2dentry return -ENOENT
4827 # #define OBD_FAIL_OST_ENOENT 0x217
4828 test_69() {
4829         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4830         remote_ost_nodsh && skip "remote OST with nodsh" && return
4831
4832         f="$DIR/$tfile"
4833         $SETSTRIPE -c 1 -i 0 $f
4834
4835         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4836
4837         do_facet ost1 lctl set_param fail_loc=0x217
4838         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4839         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4840
4841         do_facet ost1 lctl set_param fail_loc=0
4842         $DIRECTIO write $f 0 2 || error "write error"
4843
4844         cancel_lru_locks osc
4845         $DIRECTIO read $f 0 1 || error "read error"
4846
4847         do_facet ost1 lctl set_param fail_loc=0x217
4848         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4849
4850         do_facet ost1 lctl set_param fail_loc=0
4851         rm -f $f
4852 }
4853 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4854
4855 test_71() {
4856     test_mkdir -p $DIR/$tdir
4857     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4858 }
4859 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4860
4861 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4862         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4863         check_kernel_version 43 || return 0
4864         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4865
4866         # Check that testing environment is properly set up. Skip if not
4867         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4868                 skip_env "User $RUNAS_ID does not exist - skipping"
4869                 return 0
4870         }
4871         # We had better clear the $DIR to get enough space for dd
4872         rm -rf $DIR/*
4873         touch $DIR/f72
4874         chmod 777 $DIR/f72
4875         chmod ug+s $DIR/f72
4876         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4877         # See if we are still setuid/sgid
4878         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4879         # Now test that MDS is updated too
4880         cancel_lru_locks mdc
4881         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4882         rm -f $DIR/f72
4883 }
4884 run_test 72a "Test that remove suid works properly (bug5695) ===="
4885
4886 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4887         local perm
4888
4889         [ "$RUNAS_ID" = "$UID" ] && \
4890                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4891         [ "$RUNAS_ID" -eq 0 ] && \
4892                 skip_env "RUNAS_ID = 0 -- skipping" && return
4893
4894         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4895         # Check that testing environment is properly set up. Skip if not
4896         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4897                 skip_env "User $RUNAS_ID does not exist - skipping"
4898                 return 0
4899         }
4900         touch $DIR/${tfile}-f{g,u}
4901         test_mkdir $DIR/${tfile}-dg
4902         test_mkdir $DIR/${tfile}-du
4903         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4904         chmod g+s $DIR/${tfile}-{f,d}g
4905         chmod u+s $DIR/${tfile}-{f,d}u
4906         for perm in 777 2777 4777; do
4907                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4908                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4909                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4910                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4911         done
4912         true
4913 }
4914 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4915
4916 # bug 3462 - multiple simultaneous MDC requests
4917 test_73() {
4918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4919         test_mkdir $DIR/d73-1
4920         test_mkdir $DIR/d73-2
4921         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4922         pid1=$!
4923
4924         lctl set_param fail_loc=0x80000129
4925         $MULTIOP $DIR/d73-1/f73-2 Oc &
4926         sleep 1
4927         lctl set_param fail_loc=0
4928
4929         $MULTIOP $DIR/d73-2/f73-3 Oc &
4930         pid3=$!
4931
4932         kill -USR1 $pid1
4933         wait $pid1 || return 1
4934
4935         sleep 25
4936
4937         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4938         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4939         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4940
4941         rm -rf $DIR/d73-*
4942 }
4943 run_test 73 "multiple MDC requests (should not deadlock)"
4944
4945 test_74a() { # bug 6149, 6184
4946         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4947         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4948         #
4949         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4950         # will spin in a tight reconnection loop
4951         touch $DIR/f74a
4952         lctl set_param fail_loc=0x8000030e
4953         # get any lock that won't be difficult - lookup works.
4954         ls $DIR/f74a
4955         lctl set_param fail_loc=0
4956         true
4957         rm -f $DIR/f74a
4958 }
4959 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4960
4961 test_74b() { # bug 13310
4962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4963         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4964         #
4965         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4966         # will spin in a tight reconnection loop
4967         lctl set_param fail_loc=0x8000030e
4968         # get a "difficult" lock
4969         touch $DIR/f74b
4970         lctl set_param fail_loc=0
4971         true
4972         rm -f $DIR/f74b
4973 }
4974 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4975
4976 test_74c() {
4977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4978 #define OBD_FAIL_LDLM_NEW_LOCK
4979         lctl set_param fail_loc=0x80000319
4980         touch $DIR/$tfile && error "Touch successful"
4981         true
4982 }
4983 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4984
4985 num_inodes() {
4986         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4987 }
4988
4989 get_inode_slab_tunables() {
4990         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4991 }
4992
4993 set_inode_slab_tunables() {
4994         echo "lustre_inode_cache $1" > /proc/slabinfo
4995 }
4996
4997 test_76() { # Now for bug 20433, added originally in bug 1443
4998         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4999         local SLAB_SETTINGS=`get_inode_slab_tunables`
5000         local CPUS=`getconf _NPROCESSORS_ONLN`
5001         # we cannot set limit below 1 which means 1 inode in each
5002         # per-cpu cache is still allowed
5003         set_inode_slab_tunables "1 1 0"
5004         cancel_lru_locks osc
5005         BEFORE_INODES=`num_inodes`
5006         echo "before inodes: $BEFORE_INODES"
5007         local COUNT=1000
5008         [ "$SLOW" = "no" ] && COUNT=100
5009         for i in `seq $COUNT`; do
5010                 touch $DIR/$tfile
5011                 rm -f $DIR/$tfile
5012         done
5013         cancel_lru_locks osc
5014         AFTER_INODES=`num_inodes`
5015         echo "after inodes: $AFTER_INODES"
5016         local wait=0
5017         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5018                 sleep 2
5019                 AFTER_INODES=`num_inodes`
5020                 wait=$((wait+2))
5021                 echo "wait $wait seconds inodes: $AFTER_INODES"
5022                 if [ $wait -gt 30 ]; then
5023                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5024                 fi
5025         done
5026         set_inode_slab_tunables "$SLAB_SETTINGS"
5027 }
5028 run_test 76 "confirm clients recycle inodes properly ===="
5029
5030
5031 export ORIG_CSUM=""
5032 set_checksums()
5033 {
5034         # Note: in sptlrpc modes which enable its own bulk checksum, the
5035         # original crc32_le bulk checksum will be automatically disabled,
5036         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5037         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5038         # In this case set_checksums() will not be no-op, because sptlrpc
5039         # bulk checksum will be enabled all through the test.
5040
5041         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5042         lctl set_param -n osc.*.checksums $1
5043         return 0
5044 }
5045
5046 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5047                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5048 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5049 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5050 set_checksum_type()
5051 {
5052         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5053         log "set checksum type to $1"
5054         return 0
5055 }
5056 F77_TMP=$TMP/f77-temp
5057 F77SZ=8
5058 setup_f77() {
5059         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5060                 error "error writing to $F77_TMP"
5061 }
5062
5063 test_77a() { # bug 10889
5064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5065         $GSS && skip "could not run with gss" && return
5066         [ ! -f $F77_TMP ] && setup_f77
5067         set_checksums 1
5068         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5069         set_checksums 0
5070         rm -f $DIR/$tfile
5071 }
5072 run_test 77a "normal checksum read/write operation ============="
5073
5074 test_77b() { # bug 10889
5075         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5076         $GSS && skip "could not run with gss" && return
5077         [ ! -f $F77_TMP ] && setup_f77
5078         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5079         lctl set_param fail_loc=0x80000409
5080         set_checksums 1
5081         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
5082                 error "dd error: $?"
5083         lctl set_param fail_loc=0
5084         set_checksums 0
5085 }
5086 run_test 77b "checksum error on client write ===================="
5087
5088 test_77c() { # bug 10889
5089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5090         $GSS && skip "could not run with gss" && return
5091         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
5092         set_checksums 1
5093         for algo in $CKSUM_TYPES; do
5094                 cancel_lru_locks osc
5095                 set_checksum_type $algo
5096                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5097                 lctl set_param fail_loc=0x80000408
5098                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
5099                 lctl set_param fail_loc=0
5100         done
5101         set_checksums 0
5102         set_checksum_type $ORIG_CSUM_TYPE
5103         rm -f $DIR/f77b
5104 }
5105 run_test 77c "checksum error on client read ==================="
5106
5107 test_77d() { # bug 10889
5108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5109         $GSS && skip "could not run with gss" && return
5110         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5111         lctl set_param fail_loc=0x80000409
5112         set_checksums 1
5113         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5114                 error "direct write: rc=$?"
5115         lctl set_param fail_loc=0
5116         set_checksums 0
5117 }
5118 run_test 77d "checksum error on OST direct write ==============="
5119
5120 test_77e() { # bug 10889
5121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5122         $GSS && skip "could not run with gss" && return
5123         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5124         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5125         lctl set_param fail_loc=0x80000408
5126         set_checksums 1
5127         cancel_lru_locks osc
5128         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5129                 error "direct read: rc=$?"
5130         lctl set_param fail_loc=0
5131         set_checksums 0
5132 }
5133 run_test 77e "checksum error on OST direct read ================"
5134
5135 test_77f() { # bug 10889
5136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5137         $GSS && skip "could not run with gss" && return
5138         set_checksums 1
5139         for algo in $CKSUM_TYPES; do
5140                 cancel_lru_locks osc
5141                 set_checksum_type $algo
5142                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5143                 lctl set_param fail_loc=0x409
5144                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5145                         error "direct write succeeded"
5146                 lctl set_param fail_loc=0
5147         done
5148         set_checksum_type $ORIG_CSUM_TYPE
5149         set_checksums 0
5150 }
5151 run_test 77f "repeat checksum error on write (expect error) ===="
5152
5153 test_77g() { # bug 10889
5154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5155         $GSS && skip "could not run with gss" && return
5156         remote_ost_nodsh && skip "remote OST with nodsh" && return
5157
5158         [ ! -f $F77_TMP ] && setup_f77
5159
5160         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5161         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5162         do_facet ost1 lctl set_param fail_loc=0x8000021a
5163         set_checksums 1
5164         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5165                 error "write error: rc=$?"
5166         do_facet ost1 lctl set_param fail_loc=0
5167         set_checksums 0
5168 }
5169 run_test 77g "checksum error on OST write ======================"
5170
5171 test_77h() { # bug 10889
5172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5173         $GSS && skip "could not run with gss" && return
5174         remote_ost_nodsh && skip "remote OST with nodsh" && return
5175
5176         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5177         cancel_lru_locks osc
5178         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5179         do_facet ost1 lctl set_param fail_loc=0x8000021b
5180         set_checksums 1
5181         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5182         do_facet ost1 lctl set_param fail_loc=0
5183         set_checksums 0
5184 }
5185 run_test 77h "checksum error on OST read ======================="
5186
5187 test_77i() { # bug 13805
5188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5189         $GSS && skip "could not run with gss" && return
5190         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5191         lctl set_param fail_loc=0x40b
5192         remount_client $MOUNT
5193         lctl set_param fail_loc=0
5194         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5195                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5196                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5197                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5198         done
5199         remount_client $MOUNT
5200 }
5201 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5202
5203 test_77j() { # bug 13805
5204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5205         $GSS && skip "could not run with gss" && return
5206         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5207         lctl set_param fail_loc=0x40c
5208         remount_client $MOUNT
5209         lctl set_param fail_loc=0
5210         sleep 2 # wait async osc connect to finish
5211         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5212                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5213                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5214                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5215         done
5216         remount_client $MOUNT
5217 }
5218 run_test 77j "client only supporting ADLER32 ===================="
5219
5220 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5221 rm -f $F77_TMP
5222 unset F77_TMP
5223
5224 test_78() { # bug 10901
5225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5226         remote_ost || { skip_env "local OST" && return; }
5227
5228         NSEQ=5
5229         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5230         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5231         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5232         echo "MemTotal: $MEMTOTAL"
5233 # reserve 256MB of memory for the kernel and other running processes,
5234 # and then take 1/2 of the remaining memory for the read/write buffers.
5235     if [ $MEMTOTAL -gt 512 ] ;then
5236         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5237     else
5238         # for those poor memory-starved high-end clusters...
5239         MEMTOTAL=$((MEMTOTAL / 2))
5240     fi
5241         echo "Mem to use for directio: $MEMTOTAL"
5242         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5243         [ $F78SIZE -gt 512 ] && F78SIZE=512
5244         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5245         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5246         echo "Smallest OST: $SMALLESTOST"
5247         [ $SMALLESTOST -lt 10240 ] && \
5248                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5249
5250         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5251                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5252
5253         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5254         echo "File size: $F78SIZE"
5255         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5256         for i in `seq 1 $NSEQ`
5257         do
5258                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5259                 echo directIO rdwr round $i of $NSEQ
5260                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5261         done
5262
5263         rm -f $DIR/$tfile
5264 }
5265 run_test 78 "handle large O_DIRECT writes correctly ============"
5266
5267 test_79() { # bug 12743
5268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5269         wait_delete_completed
5270
5271         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5272         BKFREE=$(calc_osc_kbytes kbytesfree)
5273         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5274
5275         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5276         DFTOTAL=`echo $STRING | cut -d, -f1`
5277         DFUSED=`echo $STRING  | cut -d, -f2`
5278         DFAVAIL=`echo $STRING | cut -d, -f3`
5279         DFFREE=$(($DFTOTAL - $DFUSED))
5280
5281         ALLOWANCE=$((64 * $OSTCOUNT))
5282
5283         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5284            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5285                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5286         fi
5287         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5288            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5289                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5290         fi
5291         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5292            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5293                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5294         fi
5295 }
5296 run_test 79 "df report consistency check ======================="
5297
5298 test_80() { # bug 10718
5299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5300         # relax strong synchronous semantics for slow backends like ZFS
5301         local soc="obdfilter.*.sync_on_lock_cancel"
5302         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5303         local hosts=
5304         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5305                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5306                           facet_active_host $host; done | sort -u)
5307                 do_nodes $hosts lctl set_param $soc=never
5308         fi
5309
5310         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5311         sync; sleep 1; sync
5312         local BEFORE=`date +%s`
5313         cancel_lru_locks osc
5314         local AFTER=`date +%s`
5315         local DIFF=$((AFTER-BEFORE))
5316         if [ $DIFF -gt 1 ] ; then
5317                 error "elapsed for 1M@1T = $DIFF"
5318         fi
5319
5320         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5321
5322         rm -f $DIR/$tfile
5323 }
5324 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5325
5326 test_81a() { # LU-456
5327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5328         remote_ost_nodsh && skip "remote OST with nodsh" && return
5329         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5330         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5331         do_facet ost1 lctl set_param fail_loc=0x80000228
5332
5333         # write should trigger a retry and success
5334         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5335         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5336         RC=$?
5337         if [ $RC -ne 0 ] ; then
5338                 error "write should success, but failed for $RC"
5339         fi
5340 }
5341 run_test 81a "OST should retry write when get -ENOSPC ==============="
5342
5343 test_81b() { # LU-456
5344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5345         remote_ost_nodsh && skip "remote OST with nodsh" && return
5346         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5347         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5348         do_facet ost1 lctl set_param fail_loc=0x228
5349
5350         # write should retry several times and return -ENOSPC finally
5351         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5352         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5353         RC=$?
5354         ENOSPC=28
5355         if [ $RC -ne $ENOSPC ] ; then
5356                 error "dd should fail for -ENOSPC, but succeed."
5357         fi
5358 }
5359 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5360
5361 test_82() { # LU-1031
5362         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5363         local gid1=14091995
5364         local gid2=16022000
5365
5366         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5367         local MULTIPID1=$!
5368         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5369         local MULTIPID2=$!
5370         kill -USR1 $MULTIPID2
5371         sleep 2
5372         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5373                 error "First grouplock does not block second one"
5374         else
5375                 echo "Second grouplock blocks first one"
5376         fi
5377         kill -USR1 $MULTIPID1
5378         wait $MULTIPID1
5379         wait $MULTIPID2
5380 }
5381 run_test 82 "Basic grouplock test ==============================="
5382
5383 test_99a() {
5384         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5385             return
5386         test_mkdir -p $DIR/d99cvsroot
5387         chown $RUNAS_ID $DIR/d99cvsroot
5388         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5389         cd $TMP
5390
5391         $RUNAS cvs -d $DIR/d99cvsroot init || error
5392         cd $oldPWD
5393 }
5394 run_test 99a "cvs init ========================================="
5395
5396 test_99b() {
5397         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5398         [ ! -d $DIR/d99cvsroot ] && test_99a
5399         cd /etc/init.d
5400         # some versions of cvs import exit(1) when asked to import links or
5401         # files they can't read.  ignore those files.
5402         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5403                         ! -perm +4 -printf '-I %f\n')
5404         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5405                 d99reposname vtag rtag
5406 }
5407 run_test 99b "cvs import ======================================="
5408
5409 test_99c() {
5410         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5411         [ ! -d $DIR/d99cvsroot ] && test_99b
5412         cd $DIR
5413         test_mkdir -p $DIR/d99reposname
5414         chown $RUNAS_ID $DIR/d99reposname
5415         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5416 }
5417 run_test 99c "cvs checkout ====================================="
5418
5419 test_99d() {
5420         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5421         [ ! -d $DIR/d99cvsroot ] && test_99c
5422         cd $DIR/d99reposname
5423         $RUNAS touch foo99
5424         $RUNAS cvs add -m 'addmsg' foo99
5425 }
5426 run_test 99d "cvs add =========================================="
5427
5428 test_99e() {
5429         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5430         [ ! -d $DIR/d99cvsroot ] && test_99c
5431         cd $DIR/d99reposname
5432         $RUNAS cvs update
5433 }
5434 run_test 99e "cvs update ======================================="
5435
5436 test_99f() {
5437         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5438         [ ! -d $DIR/d99cvsroot ] && test_99d
5439         cd $DIR/d99reposname
5440         $RUNAS cvs commit -m 'nomsg' foo99
5441     rm -fr $DIR/d99cvsroot
5442 }
5443 run_test 99f "cvs commit ======================================="
5444
5445 test_100() {
5446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5447         [ "$NETTYPE" = tcp ] || \
5448                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5449                         return ; }
5450
5451         remote_ost_nodsh && skip "remote OST with nodsh" && return
5452         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5453         remote_servers || \
5454                 { skip "useless for local single node setup" && return; }
5455
5456         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5457                 [ "$PROT" != "tcp" ] && continue
5458                 RPORT=$(echo $REMOTE | cut -d: -f2)
5459                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5460
5461                 rc=0
5462                 LPORT=`echo $LOCAL | cut -d: -f2`
5463                 if [ $LPORT -ge 1024 ]; then
5464                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5465                         netstat -tna
5466                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5467                 fi
5468         done
5469         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5470 }
5471 run_test 100 "check local port using privileged port ==========="
5472
5473 function get_named_value()
5474 {
5475     local tag
5476
5477     tag=$1
5478     while read ;do
5479         line=$REPLY
5480         case $line in
5481         $tag*)
5482             echo $line | sed "s/^$tag[ ]*//"
5483             break
5484             ;;
5485         esac
5486     done
5487 }
5488
5489 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5490 cleanup_101a() {
5491         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5492         trap 0
5493 }
5494
5495 test_101a() {
5496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5497         local s
5498         local discard
5499         local nreads=10000
5500         [ "$CPU" = "UML" ] && nreads=1000
5501         local cache_limit=32
5502
5503         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5504         trap cleanup_101a EXIT
5505         $LCTL set_param -n llite.*.read_ahead_stats 0
5506         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5507
5508         #
5509         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5510         #
5511         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5512         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5513
5514         discard=0
5515         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5516                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5517                         discard=$(($discard + $s))
5518         done
5519         cleanup_101a
5520
5521         if [ $(($discard * 10)) -gt $nreads ] ;then
5522                 $LCTL get_param osc.*-osc*.rpc_stats
5523                 $LCTL get_param llite.*.read_ahead_stats
5524                 error "too many ($discard) discarded pages"
5525         fi
5526         rm -f $DIR/$tfile || true
5527 }
5528 run_test 101a "check read-ahead for random reads ================"
5529
5530 setup_test101bc() {
5531         test_mkdir -p $DIR/$tdir
5532         STRIPE_SIZE=1048576
5533         STRIPE_COUNT=$OSTCOUNT
5534         STRIPE_OFFSET=0
5535
5536         local list=$(comma_list $(osts_nodes))
5537         set_osd_param $list '' read_cache_enable 0
5538         set_osd_param $list '' writethrough_cache_enable 0
5539
5540         trap cleanup_test101bc EXIT
5541         # prepare the read-ahead file
5542         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5543
5544         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5545 }
5546
5547 cleanup_test101bc() {
5548         trap 0
5549         rm -rf $DIR/$tdir
5550         rm -f $DIR/$tfile
5551
5552         local list=$(comma_list $(osts_nodes))
5553         set_osd_param $list '' read_cache_enable 1
5554         set_osd_param $list '' writethrough_cache_enable 1
5555 }
5556
5557 calc_total() {
5558         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5559 }
5560
5561 ra_check_101() {
5562         local READ_SIZE=$1
5563         local STRIPE_SIZE=1048576
5564         local RA_INC=1048576
5565         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5566         local FILE_LENGTH=$((64*100))
5567         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5568                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5569         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5570                         get_named_value 'read but discarded' | \
5571                         cut -d" " -f1 | calc_total`
5572         if [ $DISCARD -gt $discard_limit ]; then
5573                 $LCTL get_param llite.*.read_ahead_stats
5574                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5575         else
5576                 echo "Read-ahead success for size ${READ_SIZE}"
5577         fi
5578 }
5579
5580 test_101b() {
5581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5582         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5583         local STRIPE_SIZE=1048576
5584         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5585         local FILE_LENGTH=$((STRIPE_SIZE*100))
5586         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5587         # prepare the read-ahead file
5588         setup_test101bc
5589         cancel_lru_locks osc
5590         for BIDX in 2 4 8 16 32 64 128 256
5591         do
5592                 local BSIZE=$((BIDX*4096))
5593                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5594                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5595                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5596                 $LCTL set_param -n llite.*.read_ahead_stats 0
5597                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5598                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5599                 cancel_lru_locks osc
5600                 ra_check_101 $BSIZE
5601         done
5602         cleanup_test101bc
5603         true
5604 }
5605 run_test 101b "check stride-io mode read-ahead ================="
5606
5607 test_101c() {
5608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5609         local STRIPE_SIZE=1048576
5610         local FILE_LENGTH=$((STRIPE_SIZE*100))
5611         local nreads=10000
5612         local osc
5613
5614     setup_test101bc
5615
5616     cancel_lru_locks osc
5617     $LCTL set_param osc.*.rpc_stats 0
5618     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5619     for osc in $($LCTL get_param -N osc.*); do
5620         if [ "$osc" == "osc.num_refs" ]; then
5621             continue
5622         fi
5623
5624         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5625         if [ $lines -le 20 ]; then
5626             continue
5627         fi
5628
5629         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5630                                      awk '$1 == "1:" { print $2; exit; }')
5631         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5632                                      awk '$1 == "2:" { print $2; exit; }')
5633         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5634                                      awk '$1 == "4:" { print $2; exit; }')
5635         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5636                                      awk '$1 == "8:" { print $2; exit; }')
5637
5638         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5639         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5640         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5641         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5642         echo "${osc} rpc check passed!"
5643     done
5644     cleanup_test101bc
5645     true
5646 }
5647 run_test 101c "check stripe_size aligned read-ahead ================="
5648
5649 set_read_ahead() {
5650    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5651    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5652 }
5653
5654 test_101d() {
5655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5656         local file=$DIR/$tfile
5657         local size=${FILESIZE_101c:-500}
5658         local ra_MB=${READAHEAD_MB:-40}
5659
5660         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5661         [ $space -gt $((size * 1024)) ] ||
5662                 { skip "Need free space ${size}M, have $space" && return; }
5663
5664     echo Creating ${size}M test file $file
5665     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5666     echo Cancel LRU locks on lustre client to flush the client cache
5667     cancel_lru_locks osc
5668
5669     echo Disable read-ahead
5670     local old_READAHEAD=$(set_read_ahead 0)
5671
5672     echo Reading the test file $file with read-ahead disabled
5673     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5674
5675     echo Cancel LRU locks on lustre client to flush the client cache
5676     cancel_lru_locks osc
5677     echo Enable read-ahead with ${ra_MB}MB
5678     set_read_ahead $ra_MB
5679
5680     echo Reading the test file $file with read-ahead enabled
5681     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5682
5683     echo read-ahead disabled time read $time_ra_OFF
5684     echo read-ahead enabled  time read $time_ra_ON
5685
5686         set_read_ahead $old_READAHEAD
5687         rm -f $file
5688         wait_delete_completed
5689
5690     [ $time_ra_ON -lt $time_ra_OFF ] ||
5691         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5692                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5693 }
5694 run_test 101d "file read with and without read-ahead enabled  ================="
5695
5696 test_101e() {
5697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5698     local file=$DIR/$tfile
5699     local size=500  #KB
5700     local count=100
5701     local blksize=1024
5702
5703     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5704     local need_space=$((count * size))
5705     [ $space -gt $need_space ] ||
5706         { skip_env "Need free space $need_space, have $space" && return; }
5707
5708     echo Creating $count ${size}K test files
5709     for ((i = 0; i < $count; i++)); do
5710         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5711     done
5712
5713     echo Cancel LRU locks on lustre client to flush the client cache
5714     cancel_lru_locks osc
5715
5716     echo Reset readahead stats
5717     $LCTL set_param -n llite.*.read_ahead_stats 0
5718
5719     for ((i = 0; i < $count; i++)); do
5720         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5721     done
5722
5723     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5724           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5725
5726     for ((i = 0; i < $count; i++)); do
5727         rm -rf ${file}_${i} 2>/dev/null
5728     done
5729
5730     #10000 means 20% reads are missing in readahead
5731     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5732 }
5733 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5734
5735 cleanup_test101f() {
5736     trap 0
5737     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5738     rm -rf $DIR/$tfile 2>/dev/null
5739 }
5740
5741 test_101f() {
5742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5743     local file=$DIR/$tfile
5744     local nreads=1000
5745
5746     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5747     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5748     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5749     trap cleanup_test101f EXIT
5750
5751     echo Cancel LRU locks on lustre client to flush the client cache
5752     cancel_lru_locks osc
5753
5754     echo Reset readahead stats
5755     $LCTL set_param -n llite.*.read_ahead_stats 0
5756     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5757     # readahead should read in 2M file on second read, so only miss
5758     # 2 pages.
5759     echo Random 4K reads on 2M file for 1000 times
5760     $READS -f $file -s 2097152 -b 4096 -n $nreads
5761
5762     echo checking missing pages
5763     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5764           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5765
5766     [ $miss -lt 3 ] || error "misses too much pages!"
5767     cleanup_test101f
5768 }
5769 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5770
5771 setup_test102() {
5772         test_mkdir -p $DIR/$tdir
5773         chown $RUNAS_ID $DIR/$tdir
5774         STRIPE_SIZE=65536
5775         STRIPE_OFFSET=1
5776         STRIPE_COUNT=$OSTCOUNT
5777         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5778
5779         trap cleanup_test102 EXIT
5780         cd $DIR
5781         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5782         cd $DIR/$tdir
5783         for num in 1 2 3 4; do
5784                 for count in $(seq 1 $STRIPE_COUNT); do
5785                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5786                                 local size=`expr $STRIPE_SIZE \* $num`
5787                                 local file=file"$num-$idx-$count"
5788                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5789                         done
5790                 done
5791         done
5792
5793         cd $DIR
5794         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5795 }
5796
5797 cleanup_test102() {
5798         trap 0
5799         rm -f $TMP/f102.tar
5800         rm -rf $DIR/d0.sanity/d102
5801 }
5802
5803 test_102a() {
5804         local testfile=$DIR/xattr_testfile
5805
5806         touch $testfile
5807
5808         [ "$UID" != 0 ] && skip_env "must run as root" && return
5809         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5810                 skip_env "must have user_xattr" && return
5811
5812         [ -z "$(which setfattr 2>/dev/null)" ] &&
5813                 skip_env "could not find setfattr" && return
5814
5815         echo "set/get xattr..."
5816         setfattr -n trusted.name1 -v value1 $testfile || error
5817         getfattr -n trusted.name1 $testfile 2> /dev/null |
5818           grep "trusted.name1=.value1" ||
5819                 error "$testfile missing trusted.name1=value1"
5820
5821         setfattr -n user.author1 -v author1 $testfile || error
5822         getfattr -n user.author1 $testfile 2> /dev/null |
5823           grep "user.author1=.author1" ||
5824                 error "$testfile missing trusted.author1=author1"
5825
5826         echo "listxattr..."
5827         setfattr -n trusted.name2 -v value2 $testfile ||
5828                 error "$testfile unable to set trusted.name2"
5829         setfattr -n trusted.name3 -v value3 $testfile ||
5830                 error "$testfile unable to set trusted.name3"
5831         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5832             grep "trusted.name" | wc -l) -eq 3 ] ||
5833                 error "$testfile missing 3 trusted.name xattrs"
5834
5835         setfattr -n user.author2 -v author2 $testfile ||
5836                 error "$testfile unable to set user.author2"
5837         setfattr -n user.author3 -v author3 $testfile ||
5838                 error "$testfile unable to set user.author3"
5839         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5840             grep "user.author" | wc -l) -eq 3 ] ||
5841                 error "$testfile missing 3 user.author xattrs"
5842
5843         echo "remove xattr..."
5844         setfattr -x trusted.name1 $testfile ||
5845                 error "$testfile error deleting trusted.name1"
5846         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5847                 error "$testfile did not delete trusted.name1 xattr"
5848
5849         setfattr -x user.author1 $testfile ||
5850                 error "$testfile error deleting user.author1"
5851         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5852                 error "$testfile did not delete trusted.name1 xattr"
5853
5854         # b10667: setting lustre special xattr be silently discarded
5855         echo "set lustre special xattr ..."
5856         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5857                 error "$testfile allowed setting trusted.lov"
5858 }
5859 run_test 102a "user xattr test =================================="
5860
5861 test_102b() {
5862         # b10930: get/set/list trusted.lov xattr
5863         echo "get/set/list trusted.lov xattr ..."
5864         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5865         local testfile=$DIR/$tfile
5866         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5867                 error "setstripe failed"
5868         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5869                 error "getstripe failed"
5870         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5871         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5872
5873         local testfile2=${testfile}2
5874         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5875                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5876
5877         $MCREATE $testfile2
5878         setfattr -n trusted.lov -v $value $testfile2
5879         local stripe_size=$($GETSTRIPE -S $testfile2)
5880         local stripe_count=$($GETSTRIPE -c $testfile2)
5881         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5882         [ $stripe_count -eq $STRIPECOUNT ] ||
5883                 error "stripe count $stripe_count != $STRIPECOUNT"
5884         rm -f $DIR/$tfile
5885 }
5886 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5887
5888 test_102c() {
5889         # b10930: get/set/list lustre.lov xattr
5890         echo "get/set/list lustre.lov xattr ..."
5891         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5892         test_mkdir -p $DIR/$tdir
5893         chown $RUNAS_ID $DIR/$tdir
5894         local testfile=$DIR/$tdir/$tfile
5895         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5896                 error "setstripe failed"
5897         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5898                 error "getstripe failed"
5899         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5900         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5901
5902         local testfile2=${testfile}2
5903         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5904                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5905
5906         $RUNAS $MCREATE $testfile2
5907         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5908         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5909         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5910         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5911         [ $stripe_count -eq $STRIPECOUNT ] ||
5912                 error "stripe count $stripe_count != $STRIPECOUNT"
5913 }
5914 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5915
5916 compare_stripe_info1() {
5917         local stripe_index_all_zero=true
5918
5919         for num in 1 2 3 4; do
5920                 for count in $(seq 1 $STRIPE_COUNT); do
5921                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5922                                 local size=$((STRIPE_SIZE * num))
5923                                 local file=file"$num-$offset-$count"
5924                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5925                                 [ $stripe_size -ne $size ] &&
5926                                     error "$file: size $stripe_size != $size"
5927                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5928                                 # allow fewer stripes to be created, ORI-601
5929                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5930                                     error "$file: count $stripe_count != $count"
5931                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5932                                 [ $stripe_index -ne 0 ] &&
5933                                         stripe_index_all_zero=false
5934                         done
5935                 done
5936         done
5937         $stripe_index_all_zero &&
5938                 error "all files are being extracted starting from OST index 0"
5939         return 0
5940 }
5941
5942 find_lustre_tar() {
5943         [ -n "$(which tar 2>/dev/null)" ] &&
5944                 strings $(which tar) | grep -q "lustre" && echo tar
5945 }
5946
5947 test_102d() {
5948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5949         # b10930: tar test for trusted.lov xattr
5950         TAR=$(find_lustre_tar)
5951         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5952         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5953         setup_test102
5954         test_mkdir -p $DIR/d102d
5955         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5956         cd $DIR/d102d/$tdir
5957         compare_stripe_info1
5958 }
5959 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5960
5961 test_102f() {
5962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5963         # b10930: tar test for trusted.lov xattr
5964         TAR=$(find_lustre_tar)
5965         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5966         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5967         setup_test102
5968         test_mkdir -p $DIR/d102f
5969         cd $DIR
5970         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5971         cd $DIR/d102f/$tdir
5972         compare_stripe_info1
5973 }
5974 run_test 102f "tar copy files, not keep osts ==========="
5975
5976 grow_xattr() {
5977         local xsize=${1:-1024}  # in bytes
5978         local file=$DIR/$tfile
5979
5980         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5981                 skip "must have user_xattr" && return 0
5982         [ -z "$(which setfattr 2>/dev/null)" ] &&
5983                 skip_env "could not find setfattr" && return 0
5984         [ -z "$(which getfattr 2>/dev/null)" ] &&
5985                 skip_env "could not find getfattr" && return 0
5986
5987         touch $file
5988
5989         local value="$(generate_string $xsize)"
5990
5991         local xbig=trusted.big
5992         log "save $xbig on $file"
5993         setfattr -n $xbig -v $value $file ||
5994                 error "saving $xbig on $file failed"
5995
5996         local orig=$(get_xattr_value $xbig $file)
5997         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5998
5999         local xsml=trusted.sml
6000         log "save $xsml on $file"
6001         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6002
6003         local new=$(get_xattr_value $xbig $file)
6004         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6005
6006         log "grow $xsml on $file"
6007         setfattr -n $xsml -v "$value" $file ||
6008                 error "growing $xsml on $file failed"
6009
6010         new=$(get_xattr_value $xbig $file)
6011         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6012         log "$xbig still valid after growing $xsml"
6013
6014         rm -f $file
6015 }
6016
6017 test_102h() { # bug 15777
6018         grow_xattr 1024
6019 }
6020 run_test 102h "grow xattr from inside inode to external block"
6021
6022 test_102ha() {
6023         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6024         grow_xattr $(max_xattr_size)
6025 }
6026 run_test 102ha "grow xattr from inside inode to external inode"
6027
6028 test_102i() { # bug 17038
6029         touch $DIR/$tfile
6030         ln -s $DIR/$tfile $DIR/${tfile}link
6031         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6032         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"
6033         rm -f $DIR/$tfile $DIR/${tfile}link
6034 }
6035 run_test 102i "lgetxattr test on symbolic link ============"
6036
6037 test_102j() {
6038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6039         TAR=$(find_lustre_tar)
6040         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6041         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6042         setup_test102 "$RUNAS"
6043         test_mkdir -p $DIR/d102j
6044         chown $RUNAS_ID $DIR/d102j
6045         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6046         cd $DIR/d102j/$tdir
6047         compare_stripe_info1 "$RUNAS"
6048 }
6049 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6050
6051 test_102k() {
6052         touch $DIR/$tfile
6053         # b22187 just check that does not crash for regular file.
6054         setfattr -n trusted.lov $DIR/$tfile
6055         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6056         local test_kdir=$DIR/d102k
6057         test_mkdir $test_kdir
6058         local default_size=`$GETSTRIPE -S $test_kdir`
6059         local default_count=`$GETSTRIPE -c $test_kdir`
6060         local default_offset=`$GETSTRIPE -i $test_kdir`
6061         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
6062                 error 'dir setstripe failed'
6063         setfattr -n trusted.lov $test_kdir
6064         local stripe_size=`$GETSTRIPE -S $test_kdir`
6065         local stripe_count=`$GETSTRIPE -c $test_kdir`
6066         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6067         [ $stripe_size -eq $default_size ] ||
6068                 error "stripe size $stripe_size != $default_size"
6069         [ $stripe_count -eq $default_count ] ||
6070                 error "stripe count $stripe_count != $default_count"
6071         [ $stripe_offset -eq $default_offset ] ||
6072                 error "stripe offset $stripe_offset != $default_offset"
6073         rm -rf $DIR/$tfile $test_kdir
6074 }
6075 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6076
6077 test_102l() {
6078         # LU-532 trusted. xattr is invisible to non-root
6079         local testfile=$DIR/$tfile
6080
6081         touch $testfile
6082
6083         echo "listxattr as user..."
6084         chown $RUNAS_ID $testfile
6085         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6086             grep -q "trusted" &&
6087                 error "$testfile trusted xattrs are user visible"
6088
6089         return 0;
6090 }
6091 run_test 102l "listxattr filter test =================================="
6092
6093 cleanup_test102
6094
6095 run_acl_subtest()
6096 {
6097     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6098     return $?
6099 }
6100
6101 test_103 () {
6102     [ "$UID" != 0 ] && skip_env "must run as root" && return
6103     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6104     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
6105     $GSS && skip "could not run under gss" && return
6106
6107     declare -a identity_old
6108
6109         for num in $(seq $MDSCOUNT); do
6110                 switch_identity $num true || identity_old[$num]=$?
6111         done
6112
6113     SAVE_UMASK=`umask`
6114     umask 0022
6115     cd $DIR
6116
6117     echo "performing cp ..."
6118     run_acl_subtest cp || error
6119     echo "performing getfacl-noacl..."
6120     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6121     echo "performing misc..."
6122     run_acl_subtest misc || error  "misc test failed"
6123     echo "performing permissions..."
6124     run_acl_subtest permissions || error "permissions failed"
6125     echo "performing setfacl..."
6126     run_acl_subtest setfacl || error  "setfacl test failed"
6127
6128     # inheritance test got from HP
6129     echo "performing inheritance..."
6130     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6131     chmod +x make-tree || error "chmod +x failed"
6132     run_acl_subtest inheritance || error "inheritance test failed"
6133     rm -f make-tree
6134
6135     echo "LU-974 ignore umask when acl is enabled..."
6136     run_acl_subtest 974 || error "LU-974 test failed"
6137
6138     echo "LU-2561 newly created file is same size as directory..."
6139     run_acl_subtest 2561 || error "LU-2561 test failed"
6140
6141     cd $SAVE_PWD
6142     umask $SAVE_UMASK
6143
6144         for num in $(seq $MDSCOUNT); do
6145                 if [ "${identity_old[$num]}" = 1 ]; then
6146                         switch_identity $num false || identity_old[$num]=$?
6147                 fi
6148         done
6149 }
6150 run_test 103 "acl test ========================================="
6151
6152 test_104a() {
6153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6154         touch $DIR/$tfile
6155         lfs df || error "lfs df failed"
6156         lfs df -ih || error "lfs df -ih failed"
6157         lfs df -h $DIR || error "lfs df -h $DIR failed"
6158         lfs df -i $DIR || error "lfs df -i $DIR failed"
6159         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6160         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6161
6162         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6163         lctl --device %$OSC deactivate
6164         lfs df || error "lfs df with deactivated OSC failed"
6165         lctl --device %$OSC activate
6166         # wait the osc back to normal
6167         wait_osc_import_state client ost FULL
6168
6169         lfs df || error "lfs df with reactivated OSC failed"
6170         rm -f $DIR/$tfile
6171 }
6172 run_test 104a "lfs df [-ih] [path] test ========================="
6173
6174 test_104b() {
6175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6176         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6177         chmod 666 /dev/obd
6178         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6179         if [ $denied_cnt -ne 0 ];
6180         then
6181                     error "lfs check servers test failed"
6182         fi
6183 }
6184 run_test 104b "$RUNAS lfs check servers test ===================="
6185
6186 test_105a() {
6187         # doesn't work on 2.4 kernels
6188         touch $DIR/$tfile
6189         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6190                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6191         else
6192                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6193         fi
6194         rm -f $DIR/$tfile
6195 }
6196 run_test 105a "flock when mounted without -o flock test ========"
6197
6198 test_105b() {
6199         touch $DIR/$tfile
6200         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6201                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6202         else
6203                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6204         fi
6205         rm -f $DIR/$tfile
6206 }
6207 run_test 105b "fcntl when mounted without -o flock test ========"
6208
6209 test_105c() {
6210         touch $DIR/$tfile
6211         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6212                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6213         else
6214                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6215         fi
6216         rm -f $DIR/$tfile
6217 }
6218 run_test 105c "lockf when mounted without -o flock test ========"
6219
6220 test_105d() { # bug 15924
6221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6222         test_mkdir -p $DIR/$tdir
6223         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6224                 skip "mount w/o flock enabled" && return
6225         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6226         $LCTL set_param fail_loc=0x80000315
6227         flocks_test 2 $DIR/$tdir
6228 }
6229 run_test 105d "flock race (should not freeze) ========"
6230
6231 test_105e() { # bug 22660 && 22040
6232         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6233                 skip "mount w/o flock enabled" && return
6234         touch $DIR/$tfile
6235         flocks_test 3 $DIR/$tfile
6236 }
6237 run_test 105e "Two conflicting flocks from same process ======="
6238
6239 test_106() { #bug 10921
6240         test_mkdir -p $DIR/$tdir
6241         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6242         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6243 }
6244 run_test 106 "attempt exec of dir followed by chown of that dir"
6245
6246 test_107() {
6247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6248         CDIR=`pwd`
6249         cd $DIR
6250
6251         local file=core
6252         rm -f $file
6253
6254         local save_pattern=$(sysctl -n kernel.core_pattern)
6255         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6256         sysctl -w kernel.core_pattern=$file
6257         sysctl -w kernel.core_uses_pid=0
6258
6259         ulimit -c unlimited
6260         sleep 60 &
6261         SLEEPPID=$!
6262
6263         sleep 1
6264
6265         kill -s 11 $SLEEPPID
6266         wait $SLEEPPID
6267         if [ -e $file ]; then
6268                 size=`stat -c%s $file`
6269                 [ $size -eq 0 ] && error "Fail to create core file $file"
6270         else
6271                 error "Fail to create core file $file"
6272         fi
6273         rm -f $file
6274         sysctl -w kernel.core_pattern=$save_pattern
6275         sysctl -w kernel.core_uses_pid=$save_uses_pid
6276         cd $CDIR
6277 }
6278 run_test 107 "Coredump on SIG"
6279
6280 test_110() {
6281         test_mkdir -p $DIR/$tdir
6282         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6283                 error "mkdir with 255 char failed"
6284         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6285                 error "mkdir with 256 char should fail, but did not"
6286         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6287                 error "create with 255 char failed"
6288         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6289                 error "create with 256 char should fail, but did not"
6290
6291         ls -l $DIR/$tdir
6292         rm -rf $DIR/$tdir
6293 }
6294 run_test 110 "filename length checking"
6295
6296 test_115() {
6297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6298         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6299             cut -c11-20)
6300         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6301             return
6302         echo "Starting with $OSTIO_pre threads"
6303
6304         NUMTEST=20000
6305         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6306         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6307         echo "$NUMTEST creates/unlinks"
6308         test_mkdir -p $DIR/$tdir
6309         createmany -o $DIR/$tdir/$tfile $NUMTEST
6310         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6311
6312         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6313             cut -c11-20)
6314
6315         # don't return an error
6316         [ $OSTIO_post == $OSTIO_pre ] && echo \
6317             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6318             echo "This may be fine, depending on what ran before this test" &&
6319             echo "and how fast this system is." && return
6320
6321         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6322 }
6323 run_test 115 "verify dynamic thread creation===================="
6324
6325 free_min_max () {
6326         wait_delete_completed
6327         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6328         echo OST kbytes available: ${AVAIL[@]}
6329         MAXI=0; MAXV=${AVAIL[0]}
6330         MINI=0; MINV=${AVAIL[0]}
6331         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6332             #echo OST $i: ${AVAIL[i]}kb
6333             if [ ${AVAIL[i]} -gt $MAXV ]; then
6334                 MAXV=${AVAIL[i]}; MAXI=$i
6335             fi
6336             if [ ${AVAIL[i]} -lt $MINV ]; then
6337                 MINV=${AVAIL[i]}; MINI=$i
6338             fi
6339         done
6340         echo Min free space: OST $MINI: $MINV
6341         echo Max free space: OST $MAXI: $MAXV
6342 }
6343
6344 test_116a() { # was previously test_116()
6345         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6346         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6347
6348         echo -n "Free space priority "
6349         lctl get_param -n lov.*-clilov-*.qos_prio_free
6350         declare -a AVAIL
6351         free_min_max
6352         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6353                 return
6354
6355         # generate uneven OSTs
6356         test_mkdir -p $DIR/$tdir/OST${MINI}
6357         declare -i FILL
6358         FILL=$(($MINV / 4))
6359         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6360         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6361         i=0
6362         while [ $FILL -gt 0 ]; do
6363             i=$(($i + 1))
6364             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6365             FILL=$(($FILL - 2048))
6366             echo -n .
6367         done
6368         FILL=$(($MINV / 4))
6369         sync
6370         sleep_maxage
6371
6372         free_min_max
6373         DIFF=$(($MAXV - $MINV))
6374         DIFF2=$(($DIFF * 100 / $MINV))
6375         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6376         if [ $DIFF2 -gt 20 ]; then
6377             echo "ok"
6378         else
6379             echo "failed - QOS mode won't be used"
6380             error_ignore "QOS imbalance criteria not met"
6381             return
6382         fi
6383
6384         MINI1=$MINI; MINV1=$MINV
6385         MAXI1=$MAXI; MAXV1=$MAXV
6386
6387         # now fill using QOS
6388         echo writing a bunch of files to QOS-assigned OSTs
6389         $SETSTRIPE -c 1 $DIR/$tdir
6390         i=0
6391         while [ $FILL -gt 0 ]; do
6392             i=$(($i + 1))
6393             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6394             FILL=$(($FILL - 200))
6395             echo -n .
6396         done
6397         echo "wrote $i 200k files"
6398         sync
6399         sleep_maxage
6400
6401         echo "Note: free space may not be updated, so measurements might be off"
6402         free_min_max
6403         DIFF2=$(($MAXV - $MINV))
6404         echo "free space delta: orig $DIFF final $DIFF2"
6405         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6406         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6407         echo "Wrote $DIFF to smaller OST $MINI1"
6408         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6409         echo "Wrote $DIFF2 to larger OST $MAXI1"
6410         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6411
6412         # Figure out which files were written where
6413         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6414                awk '/'$MINI1': / {print $2; exit}')
6415         echo $UUID
6416         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6417         echo "$MINC files created on smaller OST $MINI1"
6418         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6419                awk '/'$MAXI1': / {print $2; exit}')
6420         echo $UUID
6421         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6422         echo "$MAXC files created on larger OST $MAXI1"
6423         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6424         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6425
6426         rm -rf $DIR/$tdir
6427 }
6428 run_test 116a "stripe QOS: free space balance ==================="
6429
6430 test_116b() { # LU-2093
6431         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6432 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6433         local old_rr
6434         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6435         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6436         mkdir -p $DIR/$tdir
6437         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6438         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6439         do_facet $SINGLEMDS lctl set_param fail_loc=0
6440         rm -rf $DIR/$tdir
6441         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6442 }
6443 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6444
6445 test_117() # bug 10891
6446 {
6447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6448         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6449         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6450         lctl set_param fail_loc=0x21e
6451         > $DIR/$tfile || error "truncate failed"
6452         lctl set_param fail_loc=0
6453         echo "Truncate succeeded."
6454         rm -f $DIR/$tfile
6455 }
6456 run_test 117 "verify fsfilt_extend =========="
6457
6458 export OLD_RESENDCOUNT=""
6459 set_resend_count () {
6460         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6461         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6462         lctl set_param -n $PROC_RESENDCOUNT $1
6463         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6464 }
6465
6466 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6467
6468 # Reset async IO behavior after error case
6469 reset_async() {
6470         FILE=$DIR/reset_async
6471
6472         # Ensure all OSCs are cleared
6473         $SETSTRIPE -c -1 $FILE
6474         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6475         sync
6476         rm $FILE
6477 }
6478
6479 test_118a() #bug 11710
6480 {
6481         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6482         reset_async
6483
6484         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6485         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6486         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6487
6488         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6489                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6490                 return 1;
6491         fi
6492         rm -f $DIR/$tfile
6493 }
6494 run_test 118a "verify O_SYNC works =========="
6495
6496 test_118b()
6497 {
6498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6499         remote_ost_nodsh && skip "remote OST with nodsh" && return
6500
6501         reset_async
6502
6503         #define OBD_FAIL_OST_ENOENT 0x217
6504         set_nodes_failloc "$(osts_nodes)" 0x217
6505         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6506         RC=$?
6507         set_nodes_failloc "$(osts_nodes)" 0
6508         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6509         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6510                     grep -c writeback)
6511
6512         if [[ $RC -eq 0 ]]; then
6513                 error "Must return error due to dropped pages, rc=$RC"
6514                 return 1;
6515         fi
6516
6517         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6518                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6519                 return 1;
6520         fi
6521
6522         echo "Dirty pages not leaked on ENOENT"
6523
6524         # Due to the above error the OSC will issue all RPCs syncronously
6525         # until a subsequent RPC completes successfully without error.
6526         $MULTIOP $DIR/$tfile Ow4096yc
6527         rm -f $DIR/$tfile
6528
6529         return 0
6530 }
6531 run_test 118b "Reclaim dirty pages on fatal error =========="
6532
6533 test_118c()
6534 {
6535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6536         remote_ost_nodsh && skip "remote OST with nodsh" && return
6537
6538         reset_async
6539
6540         #define OBD_FAIL_OST_EROFS               0x216
6541         set_nodes_failloc "$(osts_nodes)" 0x216
6542
6543         # multiop should block due to fsync until pages are written
6544         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6545         MULTIPID=$!
6546         sleep 1
6547
6548         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6549                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6550         fi
6551
6552         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6553                     grep -c writeback)
6554         if [[ $WRITEBACK -eq 0 ]]; then
6555                 error "No page in writeback, writeback=$WRITEBACK"
6556         fi
6557
6558         set_nodes_failloc "$(osts_nodes)" 0
6559         wait $MULTIPID
6560         RC=$?
6561         if [[ $RC -ne 0 ]]; then
6562                 error "Multiop fsync failed, rc=$RC"
6563         fi
6564
6565         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6566         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6567                     grep -c writeback)
6568         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6569                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6570         fi
6571
6572         rm -f $DIR/$tfile
6573         echo "Dirty pages flushed via fsync on EROFS"
6574         return 0
6575 }
6576 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6577
6578 test_118d()
6579 {
6580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6581         remote_ost_nodsh && skip "remote OST with nodsh" && return
6582
6583         reset_async
6584
6585         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6586         set_nodes_failloc "$(osts_nodes)" 0x214
6587         # multiop should block due to fsync until pages are written
6588         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6589         MULTIPID=$!
6590         sleep 1
6591
6592         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6593                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6594         fi
6595
6596         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6597                     grep -c writeback)
6598         if [[ $WRITEBACK -eq 0 ]]; then
6599                 error "No page in writeback, writeback=$WRITEBACK"
6600         fi
6601
6602         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6603         set_nodes_failloc "$(osts_nodes)" 0
6604
6605         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6606         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6607                     grep -c writeback)
6608         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6609                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6610         fi
6611
6612         rm -f $DIR/$tfile
6613         echo "Dirty pages gaurenteed flushed via fsync"
6614         return 0
6615 }
6616 run_test 118d "Fsync validation inject a delay of the bulk =========="
6617
6618 test_118f() {
6619         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6620         reset_async
6621
6622         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6623         lctl set_param fail_loc=0x8000040a
6624
6625         # Should simulate EINVAL error which is fatal
6626         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6627         RC=$?
6628         if [[ $RC -eq 0 ]]; then
6629                 error "Must return error due to dropped pages, rc=$RC"
6630         fi
6631
6632         lctl set_param fail_loc=0x0
6633
6634         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6635         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6636         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6637                     grep -c writeback)
6638         if [[ $LOCKED -ne 0 ]]; then
6639                 error "Locked pages remain in cache, locked=$LOCKED"
6640         fi
6641
6642         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6643                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6644         fi
6645
6646         rm -f $DIR/$tfile
6647         echo "No pages locked after fsync"
6648
6649         reset_async
6650         return 0
6651 }
6652 run_test 118f "Simulate unrecoverable OSC side error =========="
6653
6654 test_118g() {
6655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6656         reset_async
6657
6658         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6659         lctl set_param fail_loc=0x406
6660
6661         # simulate local -ENOMEM
6662         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6663         RC=$?
6664
6665         lctl set_param fail_loc=0
6666         if [[ $RC -eq 0 ]]; then
6667                 error "Must return error due to dropped pages, rc=$RC"
6668         fi
6669
6670         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6671         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6672         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6673                         grep -c writeback)
6674         if [[ $LOCKED -ne 0 ]]; then
6675                 error "Locked pages remain in cache, locked=$LOCKED"
6676         fi
6677
6678         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6679                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6680         fi
6681
6682         rm -f $DIR/$tfile
6683         echo "No pages locked after fsync"
6684
6685         reset_async
6686         return 0
6687 }
6688 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6689
6690 test_118h() {
6691         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6692         remote_ost_nodsh && skip "remote OST with nodsh" && return
6693
6694         reset_async
6695
6696         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6697         set_nodes_failloc "$(osts_nodes)" 0x20e
6698         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6699         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6700         RC=$?
6701
6702         set_nodes_failloc "$(osts_nodes)" 0
6703         if [[ $RC -eq 0 ]]; then
6704                 error "Must return error due to dropped pages, rc=$RC"
6705         fi
6706
6707         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6708         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6709         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6710                     grep -c writeback)
6711         if [[ $LOCKED -ne 0 ]]; then
6712                 error "Locked pages remain in cache, locked=$LOCKED"
6713         fi
6714
6715         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6716                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6717         fi
6718
6719         rm -f $DIR/$tfile
6720         echo "No pages locked after fsync"
6721
6722         return 0
6723 }
6724 run_test 118h "Verify timeout in handling recoverables errors  =========="
6725
6726 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6727
6728 test_118i() {
6729         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6730         remote_ost_nodsh && skip "remote OST with nodsh" && return
6731
6732         reset_async
6733
6734         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6735         set_nodes_failloc "$(osts_nodes)" 0x20e
6736
6737         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6738         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6739         PID=$!
6740         sleep 5
6741         set_nodes_failloc "$(osts_nodes)" 0
6742
6743         wait $PID
6744         RC=$?
6745         if [[ $RC -ne 0 ]]; then
6746                 error "got error, but should be not, rc=$RC"
6747         fi
6748
6749         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6750         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6751         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6752         if [[ $LOCKED -ne 0 ]]; then
6753                 error "Locked pages remain in cache, locked=$LOCKED"
6754         fi
6755
6756         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6757                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6758         fi
6759
6760         rm -f $DIR/$tfile
6761         echo "No pages locked after fsync"
6762
6763         return 0
6764 }
6765 run_test 118i "Fix error before timeout in recoverable error  =========="
6766
6767 [ "$SLOW" = "no" ] && set_resend_count 4
6768
6769 test_118j() {
6770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6771         remote_ost_nodsh && skip "remote OST with nodsh" && return
6772
6773         reset_async
6774
6775         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6776         set_nodes_failloc "$(osts_nodes)" 0x220
6777
6778         # return -EIO from OST
6779         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6780         RC=$?
6781         set_nodes_failloc "$(osts_nodes)" 0x0
6782         if [[ $RC -eq 0 ]]; then
6783                 error "Must return error due to dropped pages, rc=$RC"
6784         fi
6785
6786         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6787         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6788         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6789         if [[ $LOCKED -ne 0 ]]; then
6790                 error "Locked pages remain in cache, locked=$LOCKED"
6791         fi
6792
6793         # in recoverable error on OST we want resend and stay until it finished
6794         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6795                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6796         fi
6797
6798         rm -f $DIR/$tfile
6799         echo "No pages locked after fsync"
6800
6801         return 0
6802 }
6803 run_test 118j "Simulate unrecoverable OST side error =========="
6804
6805 test_118k()
6806 {
6807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6808         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6809
6810         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6811         set_nodes_failloc "$(osts_nodes)" 0x20e
6812         test_mkdir -p $DIR/$tdir
6813
6814         for ((i=0;i<10;i++)); do
6815                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6816                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6817                 SLEEPPID=$!
6818                 sleep 0.500s
6819                 kill $SLEEPPID
6820                 wait $SLEEPPID
6821         done
6822
6823         set_nodes_failloc "$(osts_nodes)" 0
6824         rm -rf $DIR/$tdir
6825 }
6826 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6827
6828 test_118l()
6829 {
6830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6831         # LU-646
6832         test_mkdir -p $DIR/$tdir
6833         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6834         rm -rf $DIR/$tdir
6835 }
6836 run_test 118l "fsync dir ========="
6837
6838 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6839
6840 test_119a() # bug 11737
6841 {
6842         BSIZE=$((512 * 1024))
6843         directio write $DIR/$tfile 0 1 $BSIZE
6844         # We ask to read two blocks, which is more than a file size.
6845         # directio will indicate an error when requested and actual
6846         # sizes aren't equeal (a normal situation in this case) and
6847         # print actual read amount.
6848         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6849         if [ "$NOB" != "$BSIZE" ]; then
6850                 error "read $NOB bytes instead of $BSIZE"
6851         fi
6852         rm -f $DIR/$tfile
6853 }
6854 run_test 119a "Short directIO read must return actual read amount"
6855
6856 test_119b() # bug 11737
6857 {
6858         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6859
6860         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6861         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6862         sync
6863         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6864                 error "direct read failed"
6865         rm -f $DIR/$tfile
6866 }
6867 run_test 119b "Sparse directIO read must return actual read amount"
6868
6869 test_119c() # bug 13099
6870 {
6871         BSIZE=1048576
6872         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6873         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6874         rm -f $DIR/$tfile
6875 }
6876 run_test 119c "Testing for direct read hitting hole"
6877
6878 test_119d() # bug 15950
6879 {
6880         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6881         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6882         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6883         BSIZE=1048576
6884         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6885         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6886         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6887         lctl set_param fail_loc=0x40d
6888         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6889         pid_dio=$!
6890         sleep 1
6891         cat $DIR/$tfile > /dev/null &
6892         lctl set_param fail_loc=0
6893         pid_reads=$!
6894         wait $pid_dio
6895         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6896         sleep 2
6897         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6898         error "the read rpcs have not completed in 2s"
6899         rm -f $DIR/$tfile
6900         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6901 }
6902 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6903
6904 test_120a() {
6905         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6906         test_mkdir -p $DIR/$tdir
6907         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6908                skip "no early lock cancel on server" && return 0
6909         lru_resize_disable mdc
6910         lru_resize_disable osc
6911         cancel_lru_locks mdc
6912         stat $DIR/$tdir > /dev/null
6913         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6914         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6915         test_mkdir $DIR/$tdir/d1
6916         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6917         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6918         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6919         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6920         lru_resize_enable mdc
6921         lru_resize_enable osc
6922 }
6923 run_test 120a "Early Lock Cancel: mkdir test"
6924
6925 test_120b() {
6926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6927         test_mkdir -p $DIR/$tdir
6928         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6929                skip "no early lock cancel on server" && return 0
6930         lru_resize_disable mdc
6931         lru_resize_disable osc
6932         cancel_lru_locks mdc
6933         stat $DIR/$tdir > /dev/null
6934         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6935         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6936         touch $DIR/$tdir/f1
6937         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6938         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6939         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6940         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6941         lru_resize_enable mdc
6942         lru_resize_enable osc
6943 }
6944 run_test 120b "Early Lock Cancel: create test"
6945
6946 test_120c() {
6947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6948         test_mkdir -p $DIR/$tdir
6949         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6950                skip "no early lock cancel on server" && return 0
6951         lru_resize_disable mdc
6952         lru_resize_disable osc
6953         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6954         touch $DIR/$tdir/d1/f1
6955         cancel_lru_locks mdc
6956         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6957         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6958         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6959         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6960         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6961         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6962         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6963         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6964         lru_resize_enable mdc
6965         lru_resize_enable osc
6966 }
6967 run_test 120c "Early Lock Cancel: link test"
6968
6969 test_120d() {
6970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6971         test_mkdir -p $DIR/$tdir
6972         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6973                skip "no early lock cancel on server" && return 0
6974         lru_resize_disable mdc
6975         lru_resize_disable osc
6976         touch $DIR/$tdir
6977         cancel_lru_locks mdc
6978         stat $DIR/$tdir > /dev/null
6979         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6980         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6981         chmod a+x $DIR/$tdir
6982         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6983         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6984         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6985         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6986         lru_resize_enable mdc
6987         lru_resize_enable osc
6988 }
6989 run_test 120d "Early Lock Cancel: setattr test"
6990
6991 test_120e() {
6992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6993         test_mkdir -p $DIR/$tdir
6994         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6995                skip "no early lock cancel on server" && return 0
6996         lru_resize_disable mdc
6997         lru_resize_disable osc
6998         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6999         cancel_lru_locks mdc
7000         cancel_lru_locks osc
7001         dd if=$DIR/$tdir/f1 of=/dev/null
7002         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7003         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7004               awk '/ldlm_cancel/ {print $2}'`
7005         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7006               awk '/ldlm_bl_callback/ {print $2}'`
7007         unlink $DIR/$tdir/f1
7008         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7009               awk '/ldlm_cancel/ {print $2}'`
7010         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7011               awk '/ldlm_bl_callback/ {print $2}'`
7012         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7013         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7014         lru_resize_enable mdc
7015         lru_resize_enable osc
7016 }
7017 run_test 120e "Early Lock Cancel: unlink test"
7018
7019 test_120f() {
7020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7021         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7022                skip "no early lock cancel on server" && return 0
7023         test_mkdir -p $DIR/$tdir
7024         lru_resize_disable mdc
7025         lru_resize_disable osc
7026         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7027         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7028         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7029         cancel_lru_locks mdc
7030         cancel_lru_locks osc
7031         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7032         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7033         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7034         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7035               awk '/ldlm_cancel/ {print $2}'`
7036         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7037               awk '/ldlm_bl_callback/ {print $2}'`
7038         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7039         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7040               awk '/ldlm_cancel/ {print $2}'`
7041         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7042               awk '/ldlm_bl_callback/ {print $2}'`
7043         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7044         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7045         lru_resize_enable mdc
7046         lru_resize_enable osc
7047 }
7048 run_test 120f "Early Lock Cancel: rename test"
7049
7050 test_120g() {
7051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7052         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7053                skip "no early lock cancel on server" && return 0
7054         lru_resize_disable mdc
7055         lru_resize_disable osc
7056         count=10000
7057         echo create $count files
7058         test_mkdir -p $DIR/$tdir
7059         cancel_lru_locks mdc
7060         cancel_lru_locks osc
7061         t0=`date +%s`
7062
7063         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7064               awk '/ldlm_cancel/ {print $2}'`
7065         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7066               awk '/ldlm_bl_callback/ {print $2}'`
7067         createmany -o $DIR/$tdir/f $count
7068         sync
7069         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7070               awk '/ldlm_cancel/ {print $2}'`
7071         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7072               awk '/ldlm_bl_callback/ {print $2}'`
7073         t1=`date +%s`
7074         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7075         echo rm $count files
7076         rm -r $DIR/$tdir
7077         sync
7078         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7079               awk '/ldlm_cancel/ {print $2}'`
7080         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7081               awk '/ldlm_bl_callback/ {print $2}'`
7082         t2=`date +%s`
7083         echo total: $count removes in $((t2-t1))
7084         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7085         sleep 2
7086         # wait for commitment of removal
7087         lru_resize_enable mdc
7088         lru_resize_enable osc
7089 }
7090 run_test 120g "Early Lock Cancel: performance test"
7091
7092 test_121() { #bug #10589
7093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7094         rm -rf $DIR/$tfile
7095         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7096 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7097         lctl set_param fail_loc=0x310
7098         cancel_lru_locks osc > /dev/null
7099         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7100         lctl set_param fail_loc=0
7101         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7102 }
7103 run_test 121 "read cancel race ========="
7104
7105 test_123a() { # was test 123, statahead(bug 11401)
7106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7107         SLOWOK=0
7108         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7109             log "testing on UP system. Performance may be not as good as expected."
7110                         SLOWOK=1
7111         fi
7112
7113         rm -rf $DIR/$tdir
7114         test_mkdir -p $DIR/$tdir
7115         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7116         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7117         MULT=10
7118         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7119                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7120
7121                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7122                 lctl set_param -n llite.*.statahead_max 0
7123                 lctl get_param llite.*.statahead_max
7124                 cancel_lru_locks mdc
7125                 cancel_lru_locks osc
7126                 stime=`date +%s`
7127                 time ls -l $DIR/$tdir | wc -l
7128                 etime=`date +%s`
7129                 delta=$((etime - stime))
7130                 log "ls $i files without statahead: $delta sec"
7131                 lctl set_param llite.*.statahead_max=$max
7132
7133                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7134                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7135                 cancel_lru_locks mdc
7136                 cancel_lru_locks osc
7137                 stime=`date +%s`
7138                 time ls -l $DIR/$tdir | wc -l
7139                 etime=`date +%s`
7140                 delta_sa=$((etime - stime))
7141                 log "ls $i files with statahead: $delta_sa sec"
7142                 lctl get_param -n llite.*.statahead_stats
7143                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7144
7145                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7146                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7147
7148                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7149                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7150                     lctl set_param -n llite.*.statahead_max 0
7151                     lctl get_param llite.*.statahead_max
7152                     cancel_lru_locks mdc
7153                     cancel_lru_locks osc
7154                     stime=`date +%s`
7155                     time ls -l $DIR/$tdir | wc -l
7156                     etime=`date +%s`
7157                     delta=$((etime - stime))
7158                     log "ls $i files again without statahead: $delta sec"
7159                     lctl set_param llite.*.statahead_max=$max
7160                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7161                         if [  $SLOWOK -eq 0 ]; then
7162                                 error "ls $i files is slower with statahead!"
7163                         else
7164                                 log "ls $i files is slower with statahead!"
7165                         fi
7166                         break
7167                     fi
7168                 fi
7169
7170                 [ $delta -gt 20 ] && break
7171                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7172                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7173         done
7174         log "ls done"
7175
7176         stime=`date +%s`
7177         rm -r $DIR/$tdir
7178         sync
7179         etime=`date +%s`
7180         delta=$((etime - stime))
7181         log "rm -r $DIR/$tdir/: $delta seconds"
7182         log "rm done"
7183         lctl get_param -n llite.*.statahead_stats
7184 }
7185 run_test 123a "verify statahead work"
7186
7187 test_123b () { # statahead(bug 15027)
7188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7189         test_mkdir -p $DIR/$tdir
7190         createmany -o $DIR/$tdir/$tfile-%d 1000
7191
7192         cancel_lru_locks mdc
7193         cancel_lru_locks osc
7194
7195 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7196         lctl set_param fail_loc=0x80000803
7197         ls -lR $DIR/$tdir > /dev/null
7198         log "ls done"
7199         lctl set_param fail_loc=0x0
7200         lctl get_param -n llite.*.statahead_stats
7201         rm -r $DIR/$tdir
7202         sync
7203
7204 }
7205 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7206
7207 test_124a() {
7208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7209         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7210                skip "no lru resize on server" && return 0
7211         local NR=2000
7212         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7213
7214         log "create $NR files at $DIR/$tdir"
7215         createmany -o $DIR/$tdir/f $NR ||
7216                 error "failed to create $NR files in $DIR/$tdir"
7217
7218         cancel_lru_locks mdc
7219         ls -l $DIR/$tdir > /dev/null
7220
7221         local NSDIR=""
7222         local LRU_SIZE=0
7223         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7224                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7225                 LRU_SIZE=$(lctl get_param -n $PARAM)
7226                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7227                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7228                                                 log "NSDIR=$NSDIR"
7229                         log "NS=$(basename $NSDIR)"
7230                         break
7231                 fi
7232         done
7233
7234         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7235                 skip "Not enough cached locks created!"
7236                 return 0
7237         fi
7238         log "LRU=$LRU_SIZE"
7239
7240         local SLEEP=30
7241
7242         # We know that lru resize allows one client to hold $LIMIT locks
7243         # for 10h. After that locks begin to be killed by client.
7244         local MAX_HRS=10
7245         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7246                 log "LIMIT=$LIMIT"
7247
7248         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7249         # killing locks. Some time was spent for creating locks. This means
7250         # that up to the moment of sleep finish we must have killed some of
7251         # them (10-100 locks). This depends on how fast ther were created.
7252         # Many of them were touched in almost the same moment and thus will
7253         # be killed in groups.
7254         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7255
7256         # Use $LRU_SIZE_B here to take into account real number of locks
7257         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7258         local LRU_SIZE_B=$LRU_SIZE
7259         log "LVF=$LVF"
7260         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7261                 log "OLD_LVF=$OLD_LVF"
7262         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7263
7264         # Let's make sure that we really have some margin. Client checks
7265         # cached locks every 10 sec.
7266         SLEEP=$((SLEEP+20))
7267         log "Sleep ${SLEEP} sec"
7268         local SEC=0
7269         while ((SEC<$SLEEP)); do
7270                 echo -n "..."
7271                 sleep 5
7272                 SEC=$((SEC+5))
7273                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7274                 echo -n "$LRU_SIZE"
7275         done
7276         echo ""
7277         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7278         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7279
7280         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7281                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7282                 unlinkmany $DIR/$tdir/f $NR
7283                 return
7284         }
7285
7286         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7287         log "unlink $NR files at $DIR/$tdir"
7288         unlinkmany $DIR/$tdir/f $NR
7289 }
7290 run_test 124a "lru resize ======================================="
7291
7292 get_max_pool_limit()
7293 {
7294         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7295         local max=0
7296         for l in $limit; do
7297                 if test $l -gt $max; then
7298                         max=$l
7299                 fi
7300         done
7301         echo $max
7302 }
7303
7304 test_124b() {
7305         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7306         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7307                skip "no lru resize on server" && return 0
7308
7309         LIMIT=`get_max_pool_limit`
7310
7311         NR=$(($(default_lru_size)*20))
7312         if [ $NR -gt $LIMIT ]; then
7313                 log "Limit lock number by $LIMIT locks"
7314                 NR=$LIMIT
7315         fi
7316         lru_resize_disable mdc
7317         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7318                 error "failed to create $DIR/$tdir/disable_lru_resize"
7319
7320         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7321         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7322         cancel_lru_locks mdc
7323         stime=`date +%s`
7324         PID=""
7325         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7326         PID="$PID $!"
7327         sleep 2
7328         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7329         PID="$PID $!"
7330         sleep 2
7331         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7332         PID="$PID $!"
7333         wait $PID
7334         etime=`date +%s`
7335         nolruresize_delta=$((etime-stime))
7336         log "ls -la time: $nolruresize_delta seconds"
7337         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7338         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7339
7340         lru_resize_enable mdc
7341         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7342                 error "failed to create $DIR/$tdir/enable_lru_resize"
7343
7344         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7345         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7346         cancel_lru_locks mdc
7347         stime=`date +%s`
7348         PID=""
7349         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7350         PID="$PID $!"
7351         sleep 2
7352         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7353         PID="$PID $!"
7354         sleep 2
7355         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7356         PID="$PID $!"
7357         wait $PID
7358         etime=`date +%s`
7359         lruresize_delta=$((etime-stime))
7360         log "ls -la time: $lruresize_delta seconds"
7361         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7362
7363         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7364                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7365         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7366                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7367         else
7368                 log "lru resize performs the same with no lru resize"
7369         fi
7370         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7371 }
7372 run_test 124b "lru resize (performance test) ======================="
7373
7374 test_125() { # 13358
7375         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7376         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7377         test_mkdir -p $DIR/d125 || error "mkdir failed"
7378         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7379         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7380         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7381 }
7382 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7383
7384 test_126() { # bug 12829/13455
7385         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7386         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7387         $GSS && skip "must run as gss disabled" && return
7388
7389         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7390         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7391         rm -f $DIR/$tfile
7392         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7393 }
7394 run_test 126 "check that the fsgid provided by the client is taken into account"
7395
7396 test_127a() { # bug 15521
7397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7398         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7399         $LCTL set_param osc.*.stats=0
7400         FSIZE=$((2048 * 1024))
7401         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7402         cancel_lru_locks osc
7403         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7404
7405         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7406         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7407                 echo "got $COUNT $NAME"
7408                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7409                 eval $NAME=$COUNT || error "Wrong proc format"
7410
7411                 case $NAME in
7412                         read_bytes|write_bytes)
7413                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7414                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7415                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7416                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7417                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7418                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7419                                 error "sumsquare is too small: $SUMSQ"
7420                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7421                                 error "sumsquare is too big: $SUMSQ"
7422                         ;;
7423                         *) ;;
7424                 esac
7425         done < $DIR/${tfile}.tmp
7426
7427         #check that we actually got some stats
7428         [ "$read_bytes" ] || error "Missing read_bytes stats"
7429         [ "$write_bytes" ] || error "Missing write_bytes stats"
7430         [ "$read_bytes" != 0 ] || error "no read done"
7431         [ "$write_bytes" != 0 ] || error "no write done"
7432 }
7433 run_test 127a "verify the client stats are sane"
7434
7435 test_127b() { # bug LU-333
7436         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7437         $LCTL set_param llite.*.stats=0
7438         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7439         # perform 2 reads and writes so MAX is different from SUM.
7440         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7441         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7442         cancel_lru_locks osc
7443         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7444         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7445
7446         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7447         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7448                 echo "got $COUNT $NAME"
7449                 eval $NAME=$COUNT || error "Wrong proc format"
7450
7451         case $NAME in
7452                 read_bytes)
7453                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7454                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7455                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7456                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7457                         ;;
7458                 write_bytes)
7459                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7460                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7461                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7462                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7463                         ;;
7464                         *) ;;
7465                 esac
7466         done < $TMP/${tfile}.tmp
7467
7468         #check that we actually got some stats
7469         [ "$read_bytes" ] || error "Missing read_bytes stats"
7470         [ "$write_bytes" ] || error "Missing write_bytes stats"
7471         [ "$read_bytes" != 0 ] || error "no read done"
7472         [ "$write_bytes" != 0 ] || error "no write done"
7473 }
7474 run_test 127b "verify the llite client stats are sane"
7475
7476 test_128() { # bug 15212
7477         touch $DIR/$tfile
7478         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7479                 find $DIR/$tfile
7480                 find $DIR/$tfile
7481         EOF
7482
7483         result=$(grep error $TMP/$tfile.log)
7484         rm -f $DIR/$tfile
7485         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7486 }
7487 run_test 128 "interactive lfs for 2 consecutive find's"
7488
7489 set_dir_limits () {
7490         local mntdev
7491         local canondev
7492         local node
7493
7494         local LDPROC=/proc/fs/ldiskfs
7495         local facets=$(get_facets MDS)
7496
7497         for facet in ${facets//,/ }; do
7498                 canondev=$(ldiskfs_canon \
7499                            *.$(convert_facet2label $facet).mntdev $facet)
7500                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7501                                                 LDPROC=/sys/fs/ldiskfs
7502                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7503         done
7504 }
7505
7506 test_129() {
7507         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7508         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7509                 skip "Only applicable to ldiskfs-based MDTs"
7510                 return
7511         fi
7512         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7513
7514         EFBIG=27
7515         MAX=16384
7516
7517         set_dir_limits $MAX
7518         test_mkdir -p $DIR/$tdir
7519
7520         local I=0
7521         local J=0
7522         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7523                 $MULTIOP $DIR/$tdir/$J Oc
7524                 rc=$?
7525                 if [ $rc -eq $EFBIG ]; then
7526                         set_dir_limits 0
7527                         echo "return code $rc received as expected"
7528                         return 0
7529                 elif [ $rc -ne 0 ]; then
7530                         set_dir_limits 0
7531                         error_exit "return code $rc received instead of expected $EFBIG"
7532                 fi
7533                 J=$((J+1))
7534                 I=$(stat -c%s "$DIR/$tdir")
7535         done
7536
7537         set_dir_limits 0
7538         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7539 }
7540 run_test 129 "test directory size limit ========================"
7541
7542 OLDIFS="$IFS"
7543 cleanup_130() {
7544         trap 0
7545         IFS="$OLDIFS"
7546 }
7547
7548 test_130a() {
7549         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7550         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7551                 return
7552
7553         trap cleanup_130 EXIT RETURN
7554
7555         local fm_file=$DIR/$tfile
7556         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7557         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7558                 error "dd failed for $fm_file"
7559
7560         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7561         filefrag -ves $fm_file
7562         RC=$?
7563         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7564                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7565         [ $RC != 0 ] && error "filefrag $fm_file failed"
7566
7567         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7568                       grep -v "ext:" | grep -v "found")
7569         lun=$($GETSTRIPE -i $fm_file)
7570
7571         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7572         IFS=$'\n'
7573         tot_len=0
7574         for line in $filefrag_op
7575         do
7576                 frag_lun=`echo $line | cut -d: -f5`
7577                 ext_len=`echo $line | cut -d: -f4`
7578                 if (( $frag_lun != $lun )); then
7579                         cleanup_130
7580                         error "FIEMAP on 1-stripe file($fm_file) failed"
7581                         return
7582                 fi
7583                 (( tot_len += ext_len ))
7584         done
7585
7586         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7587                 cleanup_130
7588                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7589                 return
7590         fi
7591
7592         cleanup_130
7593
7594         echo "FIEMAP on single striped file succeeded"
7595 }
7596 run_test 130a "FIEMAP (1-stripe file)"
7597
7598 test_130b() {
7599         [ "$OSTCOUNT" -lt "2" ] &&
7600                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7601
7602         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7603         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7604                 return
7605
7606         trap cleanup_130 EXIT RETURN
7607
7608         local fm_file=$DIR/$tfile
7609         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7610         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7611                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7612
7613         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7614                 error "dd failed on $fm_file"
7615
7616         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7617         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7618                       grep -v "ext:" | grep -v "found")
7619
7620         last_lun=$(echo $filefrag_op | cut -d: -f5)
7621
7622         IFS=$'\n'
7623         tot_len=0
7624         num_luns=1
7625         for line in $filefrag_op
7626         do
7627                 frag_lun=`echo $line | cut -d: -f5`
7628                 ext_len=`echo $line | cut -d: -f4`
7629                 if (( $frag_lun != $last_lun )); then
7630                         if (( tot_len != 1024 )); then
7631                                 cleanup_130
7632                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7633                                 return
7634                         else
7635                                 (( num_luns += 1 ))
7636                                 tot_len=0
7637                         fi
7638                 fi
7639                 (( tot_len += ext_len ))
7640                 last_lun=$frag_lun
7641         done
7642         if (( num_luns != 2 || tot_len != 1024 )); then
7643                 cleanup_130
7644                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7645                 return
7646         fi
7647
7648         cleanup_130
7649
7650         echo "FIEMAP on 2-stripe file succeeded"
7651 }
7652 run_test 130b "FIEMAP (2-stripe file)"
7653
7654 test_130c() {
7655         [ "$OSTCOUNT" -lt "2" ] &&
7656                 skip_env "skipping FIEMAP on 2-stripe file" && return
7657
7658         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7659         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7660                 return
7661
7662         trap cleanup_130 EXIT RETURN
7663
7664         local fm_file=$DIR/$tfile
7665         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7666         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7667                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7668
7669         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7670
7671         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7672         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7673
7674         last_lun=`echo $filefrag_op | cut -d: -f5`
7675
7676         IFS=$'\n'
7677         tot_len=0
7678         num_luns=1
7679         for line in $filefrag_op
7680         do
7681                 frag_lun=`echo $line | cut -d: -f5`
7682                 ext_len=`echo $line | cut -d: -f4`
7683                 if (( $frag_lun != $last_lun )); then
7684                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7685                         if (( logical != 512 )); then
7686                                 cleanup_130
7687                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7688                                 return
7689                         fi
7690                         if (( tot_len != 512 )); then
7691                                 cleanup_130
7692                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7693                                 return
7694                         else
7695                                 (( num_luns += 1 ))
7696                                 tot_len=0
7697                         fi
7698                 fi
7699                 (( tot_len += ext_len ))
7700                 last_lun=$frag_lun
7701         done
7702         if (( num_luns != 2 || tot_len != 512 )); then
7703                 cleanup_130
7704                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7705                 return
7706         fi
7707
7708         cleanup_130
7709
7710         echo "FIEMAP on 2-stripe file with hole succeeded"
7711 }
7712 run_test 130c "FIEMAP (2-stripe file with hole)"
7713
7714 test_130d() {
7715         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7716
7717         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7718         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7719
7720         trap cleanup_130 EXIT RETURN
7721
7722         local fm_file=$DIR/$tfile
7723         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7724         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7725                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7726         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7727
7728         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7729         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7730
7731         last_lun=`echo $filefrag_op | cut -d: -f5`
7732
7733         IFS=$'\n'
7734         tot_len=0
7735         num_luns=1
7736         for line in $filefrag_op
7737         do
7738                 frag_lun=`echo $line | cut -d: -f5`
7739                 ext_len=`echo $line | cut -d: -f4`
7740                 if (( $frag_lun != $last_lun )); then
7741                         if (( tot_len != 1024 )); then
7742                                 cleanup_130
7743                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7744                                 return
7745                         else
7746                                 (( num_luns += 1 ))
7747                                 tot_len=0
7748                         fi
7749                 fi
7750                 (( tot_len += ext_len ))
7751                 last_lun=$frag_lun
7752         done
7753         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7754                 cleanup_130
7755                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7756                 return
7757         fi
7758
7759         cleanup_130
7760
7761         echo "FIEMAP on N-stripe file succeeded"
7762 }
7763 run_test 130d "FIEMAP (N-stripe file)"
7764
7765 test_130e() {
7766         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7767
7768         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7769         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7770
7771         trap cleanup_130 EXIT RETURN
7772
7773         local fm_file=$DIR/$tfile
7774         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7775         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7776                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7777
7778         NUM_BLKS=512
7779         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7780         for ((i = 0; i < $NUM_BLKS; i++))
7781         do
7782                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7783         done
7784
7785         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7786         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7787
7788         last_lun=`echo $filefrag_op | cut -d: -f5`
7789
7790         IFS=$'\n'
7791         tot_len=0
7792         num_luns=1
7793         for line in $filefrag_op
7794         do
7795                 frag_lun=`echo $line | cut -d: -f5`
7796                 ext_len=`echo $line | cut -d: -f4`
7797                 if (( $frag_lun != $last_lun )); then
7798                         if (( tot_len != $EXPECTED_LEN )); then
7799                                 cleanup_130
7800                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7801                                 return
7802                         else
7803                                 (( num_luns += 1 ))
7804                                 tot_len=0
7805                         fi
7806                 fi
7807                 (( tot_len += ext_len ))
7808                 last_lun=$frag_lun
7809         done
7810         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7811                 cleanup_130
7812                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7813                 return
7814         fi
7815
7816         cleanup_130
7817
7818         echo "FIEMAP with continuation calls succeeded"
7819 }
7820 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7821
7822 # Test for writev/readv
7823 test_131a() {
7824         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7825         error "writev test failed"
7826         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7827         error "readv failed"
7828         rm -f $DIR/$tfile
7829 }
7830 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7831
7832 test_131b() {
7833         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7834         error "append writev test failed"
7835         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7836         error "append writev test failed"
7837         rm -f $DIR/$tfile
7838 }
7839 run_test 131b "test append writev"
7840
7841 test_131c() {
7842         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7843         error "NOT PASS"
7844 }
7845 run_test 131c "test read/write on file w/o objects"
7846
7847 test_131d() {
7848         rwv -f $DIR/$tfile -w -n 1 1572864
7849         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7850         if [ "$NOB" != 1572864 ]; then
7851                 error "Short read filed: read $NOB bytes instead of 1572864"
7852         fi
7853         rm -f $DIR/$tfile
7854 }
7855 run_test 131d "test short read"
7856
7857 test_131e() {
7858         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7859         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7860         error "read hitting hole failed"
7861         rm -f $DIR/$tfile
7862 }
7863 run_test 131e "test read hitting hole"
7864
7865 get_ost_param() {
7866         local token=$1
7867         local gl_sum=0
7868         for node in $(osts_nodes); do
7869                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7870                 [ x$gl = x"" ] && gl=0
7871                 gl_sum=$((gl_sum + gl))
7872         done
7873         echo $gl
7874 }
7875
7876 som_mode_switch() {
7877         local som=$1
7878         local gl1=$2
7879         local gl2=$3
7880
7881         if [ x$som = x"enabled" ]; then
7882                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7883                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7884                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7885         else
7886                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7887                 MOUNTOPT="$MOUNTOPT,som_preview"
7888                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7889         fi
7890
7891         # do remount to make new mount-conf parameters actual
7892         echo remounting...
7893         sync
7894         stopall
7895         setupall
7896 }
7897
7898 test_132() { #1028, SOM
7899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7900         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7901         local num=$(get_mds_dir $DIR)
7902         local mymds=mds${num}
7903         local MOUNTOPT_SAVE=$MOUNTOPT
7904
7905         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7906         cancel_lru_locks osc
7907
7908         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7909
7910         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7911         stat $DIR/$tfile >/dev/null
7912         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7913         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7914         rm $DIR/$tfile
7915         som_mode_switch $som1 $gl1 $gl2
7916
7917         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7918         cancel_lru_locks osc
7919
7920         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7921         if [ $som1 == $som2 ]; then
7922             error "som is still "$som2
7923             if [ x$som2 = x"enabled" ]; then
7924                 som2="disabled"
7925             else
7926                 som2="enabled"
7927             fi
7928         fi
7929
7930         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7931         stat $DIR/$tfile >/dev/null
7932         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7933         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7934         som_mode_switch $som2 $gl1 $gl2
7935         MOUNTOPT=$MOUNTOPT_SAVE
7936 }
7937 run_test 132 "som avoids glimpse rpc"
7938
7939 check_stats() {
7940         local res
7941         local count
7942         case $1 in
7943         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7944                  ;;
7945         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7946                  ;;
7947         *) error "Wrong argument $1" ;;
7948         esac
7949         echo $res
7950         count=`echo $res | awk '{print $2}'`
7951         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7952         # if the argument $3 is zero, it means any stat increment is ok.
7953         if [ $3 -gt 0 ] ; then
7954                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7955         fi
7956 }
7957
7958 test_133a() {
7959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7960         remote_ost_nodsh && skip "remote OST with nodsh" && return
7961         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7962
7963         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7964                 { skip "MDS doesn't support rename stats"; return; }
7965         local testdir=$DIR/${tdir}/stats_testdir
7966         mkdir -p $DIR/${tdir}
7967
7968         # clear stats.
7969         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7970         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7971
7972         # verify mdt stats first.
7973         mkdir ${testdir} || error "mkdir failed"
7974         check_stats $SINGLEMDS "mkdir" 1
7975         touch ${testdir}/${tfile} || "touch failed"
7976         check_stats $SINGLEMDS "open" 1
7977         check_stats $SINGLEMDS "close" 1
7978         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7979         check_stats $SINGLEMDS "mknod" 1
7980         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7981         check_stats $SINGLEMDS "unlink" 1
7982         rm -f ${testdir}/${tfile} || error "file remove failed"
7983         check_stats $SINGLEMDS "unlink" 2
7984
7985         # remove working dir and check mdt stats again.
7986         rmdir ${testdir} || error "rmdir failed"
7987         check_stats $SINGLEMDS "rmdir" 1
7988
7989         local testdir1=$DIR/${tdir}/stats_testdir1
7990         mkdir -p ${testdir}
7991         mkdir -p ${testdir1}
7992         touch ${testdir1}/test1
7993         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7994         check_stats $SINGLEMDS "crossdir_rename" 1
7995
7996         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7997         check_stats $SINGLEMDS "samedir_rename" 1
7998
7999         rm -rf $DIR/${tdir}
8000 }
8001 run_test 133a "Verifying MDT stats ========================================"
8002
8003 test_133b() {
8004         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8005         remote_ost_nodsh && skip "remote OST with nodsh" && return
8006         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8007         local testdir=$DIR/${tdir}/stats_testdir
8008         mkdir -p ${testdir} || error "mkdir failed"
8009         touch ${testdir}/${tfile} || "touch failed"
8010         cancel_lru_locks mdc
8011
8012         # clear stats.
8013         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8014         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8015
8016         # extra mdt stats verification.
8017         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8018         check_stats $SINGLEMDS "setattr" 1
8019         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8020         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8021         then            # LU-1740
8022                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8023                 check_stats $SINGLEMDS "getattr" 1
8024         fi
8025         $LFS df || error "lfs failed"
8026         check_stats $SINGLEMDS "statfs" 1
8027
8028         rm -rf $DIR/${tdir}
8029 }
8030 run_test 133b "Verifying extra MDT stats =================================="
8031
8032 test_133c() {
8033         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8034         remote_ost_nodsh && skip "remote OST with nodsh" && return
8035         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8036         local testdir=$DIR/${tdir}/stats_testdir
8037         test_mkdir -p ${testdir} || error "mkdir failed"
8038
8039         # verify obdfilter stats.
8040         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8041         sync
8042         cancel_lru_locks osc
8043         wait_delete_completed
8044
8045         # clear stats.
8046         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8047         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8048
8049         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8050         sync
8051         cancel_lru_locks osc
8052         check_stats ost "write" 1
8053
8054         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8055         check_stats ost "read" 1
8056
8057         > ${testdir}/${tfile} || error "truncate failed"
8058         check_stats ost "punch" 1
8059
8060         rm -f ${testdir}/${tfile} || error "file remove failed"
8061         wait_delete_completed
8062         check_stats ost "destroy" 1
8063
8064         rm -rf $DIR/${tdir}
8065 }
8066 run_test 133c "Verifying OST stats ========================================"
8067
8068 order_2() {
8069     local value=$1
8070     local orig=$value
8071     local order=1
8072
8073     while [ $value -ge 2 ]; do
8074         order=$((order*2))
8075         value=$((value/2))
8076     done
8077
8078     if [ $orig -gt $order ]; then
8079         order=$((order*2))
8080     fi
8081     echo $order
8082 }
8083
8084 size_in_KMGT() {
8085     local value=$1
8086     local size=('K' 'M' 'G' 'T');
8087     local i=0
8088     local size_string=$value
8089
8090     while [ $value -ge 1024 ]; do
8091         if [ $i -gt 3 ]; then
8092             #T is the biggest unit we get here, if that is bigger,
8093             #just return XXXT
8094             size_string=${value}T
8095             break
8096         fi
8097         value=$((value >> 10))
8098         if [ $value -lt 1024 ]; then
8099             size_string=${value}${size[$i]}
8100             break
8101         fi
8102         i=$((i + 1))
8103     done
8104
8105     echo $size_string
8106 }
8107
8108 get_rename_size() {
8109     local size=$1
8110     local context=${2:-.}
8111     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8112                 grep -A1 $context |
8113                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8114     echo $sample
8115 }
8116
8117 test_133d() {
8118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8119     remote_ost_nodsh && skip "remote OST with nodsh" && return
8120     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8121     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8122         { skip "MDS doesn't support rename stats"; return; }
8123
8124     local testdir1=$DIR/${tdir}/stats_testdir1
8125     local testdir2=$DIR/${tdir}/stats_testdir2
8126
8127     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8128
8129     test_mkdir -p ${testdir1} || error "mkdir failed"
8130     test_mkdir -p ${testdir2} || error "mkdir failed"
8131
8132     createmany -o $testdir1/test 512 || error "createmany failed"
8133
8134         # check samedir rename size
8135         mv ${testdir1}/test0 ${testdir1}/test_0
8136
8137         local testdir1_size=$(ls -l $DIR/${tdir} |
8138                 awk '/stats_testdir1/ {print $5}')
8139         local testdir2_size=$(ls -l $DIR/${tdir} |
8140                 awk '/stats_testdir2/ {print $5}')
8141
8142         testdir1_size=$(order_2 $testdir1_size)
8143         testdir2_size=$(order_2 $testdir2_size)
8144
8145         testdir1_size=$(size_in_KMGT $testdir1_size)
8146         testdir2_size=$(size_in_KMGT $testdir2_size)
8147
8148         echo "source rename dir size: ${testdir1_size}"
8149         echo "target rename dir size: ${testdir2_size}"
8150
8151     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8152     eval $cmd || error "$cmd failed"
8153     local samedir=$($cmd | grep 'same_dir')
8154     local same_sample=$(get_rename_size $testdir1_size)
8155     [ -z "$samedir" ] && error "samedir_rename_size count error"
8156     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8157     echo "Check same dir rename stats success"
8158
8159     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8160
8161     # check crossdir rename size
8162     mv ${testdir1}/test_0 ${testdir2}/test_0
8163
8164         testdir1_size=$(ls -l $DIR/${tdir} |
8165                 awk '/stats_testdir1/ {print $5}')
8166         testdir2_size=$(ls -l $DIR/${tdir} |
8167                 awk '/stats_testdir2/ {print $5}')
8168
8169         testdir1_size=$(order_2 $testdir1_size)
8170         testdir2_size=$(order_2 $testdir2_size)
8171
8172         testdir1_size=$(size_in_KMGT $testdir1_size)
8173         testdir2_size=$(size_in_KMGT $testdir2_size)
8174
8175         echo "source rename dir size: ${testdir1_size}"
8176         echo "target rename dir size: ${testdir2_size}"
8177
8178     eval $cmd || error "$cmd failed"
8179     local crossdir=$($cmd | grep 'crossdir')
8180     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8181     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8182     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8183     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8184     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8185     echo "Check cross dir rename stats success"
8186     rm -rf $DIR/${tdir}
8187 }
8188 run_test 133d "Verifying rename_stats ========================================"
8189
8190 test_133e() {
8191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8192         local testdir=$DIR/${tdir}/stats_testdir
8193         local ctr f0 f1 bs=32768 count=42 sum
8194
8195         remote_ost_nodsh && skip "remote OST with nodsh" && return
8196         mkdir -p ${testdir} || error "mkdir failed"
8197
8198         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8199
8200         for ctr in {write,read}_bytes; do
8201                 sync
8202                 cancel_lru_locks osc
8203
8204                 do_facet ost1 $LCTL set_param -n \
8205                         "obdfilter.*.exports.clear=clear"
8206
8207                 if [ $ctr = write_bytes ]; then
8208                         f0=/dev/zero
8209                         f1=${testdir}/${tfile}
8210                 else
8211                         f0=${testdir}/${tfile}
8212                         f1=/dev/null
8213                 fi
8214
8215                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8216                         error "dd failed"
8217                 sync
8218                 cancel_lru_locks osc
8219
8220                 sum=$(do_facet ost1 $LCTL get_param \
8221                                 "obdfilter.*.exports.*.stats" | \
8222                           awk -v ctr=$ctr '\
8223                                 BEGIN { sum = 0 }
8224                                 $1 == ctr { sum += $7 }
8225                                 END { print sum }')
8226
8227                 if ((sum != bs * count)); then
8228                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8229                 fi
8230         done
8231
8232         rm -rf $DIR/${tdir}
8233 }
8234 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8235
8236 test_140() { #bug-17379
8237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8238         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8239         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8240         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8241
8242         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8243         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8244         local i=0
8245         while i=`expr $i + 1`; do
8246                 test_mkdir -p $i || error "Creating dir $i"
8247                 cd $i || error "Changing to $i"
8248                 ln -s ../stat stat || error "Creating stat symlink"
8249                 # Read the symlink until ELOOP present,
8250                 # not LBUGing the system is considered success,
8251                 # we didn't overrun the stack.
8252                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8253                 [ $ret -ne 0 ] && {
8254                         if [ $ret -eq 40 ]; then
8255                                 break  # -ELOOP
8256                         else
8257                                 error "Open stat symlink"
8258                                 return
8259                         fi
8260                 }
8261         done
8262         i=`expr $i - 1`
8263         echo "The symlink depth = $i"
8264         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8265                                         error "Invalid symlink depth"
8266
8267         # Test recursive symlink
8268         ln -s symlink_self symlink_self
8269         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8270         echo "open symlink_self returns $ret"
8271         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8272 }
8273 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8274
8275 test_150() {
8276         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8277         local TF="$TMP/$tfile"
8278
8279         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8280         cp $TF $DIR/$tfile
8281         cancel_lru_locks osc
8282         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8283         remount_client $MOUNT
8284         df -P $MOUNT
8285         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8286
8287         $TRUNCATE $TF 6000
8288         $TRUNCATE $DIR/$tfile 6000
8289         cancel_lru_locks osc
8290         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8291
8292         echo "12345" >>$TF
8293         echo "12345" >>$DIR/$tfile
8294         cancel_lru_locks osc
8295         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8296
8297         echo "12345" >>$TF
8298         echo "12345" >>$DIR/$tfile
8299         cancel_lru_locks osc
8300         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8301
8302         rm -f $TF
8303         true
8304 }
8305 run_test 150 "truncate/append tests"
8306
8307 function roc_hit() {
8308         local list=$(comma_list $(osts_nodes))
8309
8310         echo $(get_osd_param $list '' stats |
8311                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8312 }
8313
8314 function set_cache() {
8315         local on=1
8316
8317         if [ "$2" == "off" ]; then
8318                 on=0;
8319         fi
8320         local list=$(comma_list $(osts_nodes))
8321         set_osd_param $list '' $1_cache_enable $on
8322
8323         cancel_lru_locks osc
8324 }
8325
8326 test_151() {
8327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8328         remote_ost_nodsh && skip "remote OST with nodsh" && return
8329
8330         local CPAGES=3
8331         local list=$(comma_list $(osts_nodes))
8332
8333         # check whether obdfilter is cache capable at all
8334         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8335                 echo "not cache-capable obdfilter"
8336                 return 0
8337         fi
8338
8339         # check cache is enabled on all obdfilters
8340         if get_osd_param $list '' read_cache_enable | grep 0; then
8341                 echo "oss cache is disabled"
8342                 return 0
8343         fi
8344
8345         set_osd_param $list '' writethrough_cache_enable 1
8346
8347         # pages should be in the case right after write
8348         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
8349         local BEFORE=`roc_hit`
8350         cancel_lru_locks osc
8351         cat $DIR/$tfile >/dev/null
8352         local AFTER=`roc_hit`
8353         if ! let "AFTER - BEFORE == CPAGES"; then
8354                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8355         fi
8356
8357         # the following read invalidates the cache
8358         cancel_lru_locks osc
8359         set_osd_param $list '' read_cache_enable 0
8360         cat $DIR/$tfile >/dev/null
8361
8362         # now data shouldn't be found in the cache
8363         BEFORE=`roc_hit`
8364         cancel_lru_locks osc
8365         cat $DIR/$tfile >/dev/null
8366         AFTER=`roc_hit`
8367         if let "AFTER - BEFORE != 0"; then
8368                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8369         fi
8370
8371         set_osd_param $list '' read_cache_enable 1
8372         rm -f $DIR/$tfile
8373 }
8374 run_test 151 "test cache on oss and controls ==============================="
8375
8376 test_152() {
8377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8378         local TF="$TMP/$tfile"
8379
8380         # simulate ENOMEM during write
8381 #define OBD_FAIL_OST_NOMEM      0x226
8382         lctl set_param fail_loc=0x80000226
8383         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8384         cp $TF $DIR/$tfile
8385         sync || error "sync failed"
8386         lctl set_param fail_loc=0
8387
8388         # discard client's cache
8389         cancel_lru_locks osc
8390
8391         # simulate ENOMEM during read
8392         lctl set_param fail_loc=0x80000226
8393         cmp $TF $DIR/$tfile || error "cmp failed"
8394         lctl set_param fail_loc=0
8395
8396         rm -f $TF
8397 }
8398 run_test 152 "test read/write with enomem ============================"
8399
8400 test_153() {
8401         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8402 }
8403 run_test 153 "test if fdatasync does not crash ======================="
8404
8405 test_154() {
8406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8407         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8408                 { skip "Need MDS version at least 2.2.51"; return 0; }
8409
8410         cp /etc/hosts $DIR/$tfile
8411
8412         fid=$($LFS path2fid $DIR/$tfile)
8413         rc=$?
8414         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8415
8416         ffid=$DIR/.lustre/fid/$fid
8417
8418         echo "stat fid $fid"
8419         stat $ffid > /dev/null || error "stat $ffid failed."
8420         echo "touch fid $fid"
8421         touch $ffid || error "touch $ffid failed."
8422         echo "write to fid $fid"
8423         cat /etc/hosts > $ffid || error "write $ffid failed."
8424         echo "read fid $fid"
8425         diff /etc/hosts $ffid || error "read $ffid failed."
8426         echo "append write to fid $fid"
8427         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8428         echo "rename fid $fid"
8429         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
8430         touch $DIR/$tfile.1
8431         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
8432         rm -f $DIR/$tfile.1
8433         echo "truncate fid $fid"
8434         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8435         echo "link fid $fid"
8436         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
8437         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8438                 echo "setfacl fid $fid"
8439                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8440                 echo "getfacl fid $fid"
8441                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8442         fi
8443         echo "unlink fid $fid"
8444         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
8445         echo "mknod fid $fid"
8446         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8447
8448         fid=[0xf00000400:0x1:0x0]
8449         ffid=$DIR/.lustre/fid/$fid
8450
8451         echo "stat non-exist fid $fid"
8452         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8453         echo "write to non-exist fid $fid"
8454         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8455         echo "link new fid $fid"
8456         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8457
8458         test_mkdir -p $DIR/$tdir
8459         touch $DIR/$tdir/$tfile
8460         fid=$($LFS path2fid $DIR/$tdir)
8461         rc=$?
8462         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8463
8464         ffid=$DIR/.lustre/fid/$fid
8465
8466         echo "ls $fid"
8467         ls $ffid > /dev/null || error "ls $ffid failed."
8468         echo "touch $fid/$tfile.1"
8469         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8470
8471         echo "touch $DIR/.lustre/fid/$tfile"
8472         touch $DIR/.lustre/fid/$tfile && \
8473                 error "touch $DIR/.lustre/fid/$tfile should fail."
8474
8475         echo "setxattr to $DIR/.lustre/fid"
8476         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8477                 error "setxattr should fail."
8478
8479         echo "listxattr for $DIR/.lustre/fid"
8480         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8481                 error "listxattr should fail."
8482
8483         echo "delxattr from $DIR/.lustre/fid"
8484         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8485                 error "delxattr should fail."
8486
8487         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8488         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8489                 error "touch invalid fid should fail."
8490
8491         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8492         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8493                 error "touch non-normal fid should fail."
8494
8495         echo "rename $tdir to $DIR/.lustre/fid"
8496         mrename $DIR/$tdir $DIR/.lustre/fid &&
8497                 error "rename to $DIR/.lustre/fid should fail."
8498
8499         echo "rename .lustre to itself"
8500         fid=$($LFS path2fid $DIR)
8501         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8502                 error "rename .lustre to itself should fail."
8503
8504         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8505         fid=$($LFS path2fid $DIR/$tfile-2)
8506         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8507         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8508                 error "create lov data thru .lustre should fail."
8509         echo "cp /etc/passwd $DIR/$tfile-2"
8510         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8511         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8512         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8513                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8514
8515         echo "Open-by-FID succeeded"
8516 }
8517 run_test 154 "Open-by-FID"
8518
8519 test_155_small_load() {
8520     local temp=$TMP/$tfile
8521     local file=$DIR/$tfile
8522
8523     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8524         error "dd of=$temp bs=6096 count=1 failed"
8525     cp $temp $file
8526     cancel_lru_locks osc
8527     cmp $temp $file || error "$temp $file differ"
8528
8529     $TRUNCATE $temp 6000
8530     $TRUNCATE $file 6000
8531     cmp $temp $file || error "$temp $file differ (truncate1)"
8532
8533     echo "12345" >>$temp
8534     echo "12345" >>$file
8535     cmp $temp $file || error "$temp $file differ (append1)"
8536
8537     echo "12345" >>$temp
8538     echo "12345" >>$file
8539     cmp $temp $file || error "$temp $file differ (append2)"
8540
8541     rm -f $temp $file
8542     true
8543 }
8544
8545 test_155_big_load() {
8546     remote_ost_nodsh && skip "remote OST with nodsh" && return
8547     local temp=$TMP/$tfile
8548     local file=$DIR/$tfile
8549
8550     free_min_max
8551     local cache_size=$(do_facet ost$((MAXI+1)) \
8552         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8553     local large_file_size=$((cache_size * 2))
8554
8555     echo "OSS cache size: $cache_size KB"
8556     echo "Large file size: $large_file_size KB"
8557
8558     [ $MAXV -le $large_file_size ] && \
8559         skip_env "max available OST size needs > $large_file_size KB" && \
8560         return 0
8561
8562     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8563
8564     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8565         error "dd of=$temp bs=$large_file_size count=1k failed"
8566     cp $temp $file
8567     ls -lh $temp $file
8568     cancel_lru_locks osc
8569     cmp $temp $file || error "$temp $file differ"
8570
8571     rm -f $temp $file
8572     true
8573 }
8574
8575 test_155a() {
8576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8577         set_cache read on
8578         set_cache writethrough on
8579         test_155_small_load
8580 }
8581 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8582
8583 test_155b() {
8584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8585         set_cache read on
8586         set_cache writethrough off
8587         test_155_small_load
8588 }
8589 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8590
8591 test_155c() {
8592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8593         set_cache read off
8594         set_cache writethrough on
8595         test_155_small_load
8596 }
8597 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8598
8599 test_155d() {
8600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8601         set_cache read off
8602         set_cache writethrough off
8603         test_155_small_load
8604 }
8605 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8606
8607 test_155e() {
8608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8609         set_cache read on
8610         set_cache writethrough on
8611         test_155_big_load
8612 }
8613 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8614
8615 test_155f() {
8616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8617         set_cache read on
8618         set_cache writethrough off
8619         test_155_big_load
8620 }
8621 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8622
8623 test_155g() {
8624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8625         set_cache read off
8626         set_cache writethrough on
8627         test_155_big_load
8628 }
8629 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8630
8631 test_155h() {
8632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8633         set_cache read off
8634         set_cache writethrough off
8635         test_155_big_load
8636 }
8637 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8638
8639 test_156() {
8640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8641         local CPAGES=3
8642         local BEFORE
8643         local AFTER
8644         local file="$DIR/$tfile"
8645
8646         [ "$(facet_fstype ost1)" = "zfs" ] &&
8647                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8648                 return
8649
8650     log "Turn on read and write cache"
8651     set_cache read on
8652     set_cache writethrough on
8653
8654     log "Write data and read it back."
8655     log "Read should be satisfied from the cache."
8656     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8657     BEFORE=`roc_hit`
8658     cancel_lru_locks osc
8659     cat $file >/dev/null
8660     AFTER=`roc_hit`
8661     if ! let "AFTER - BEFORE == CPAGES"; then
8662         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8663     else
8664         log "cache hits:: before: $BEFORE, after: $AFTER"
8665     fi
8666
8667     log "Read again; it should be satisfied from the cache."
8668     BEFORE=$AFTER
8669     cancel_lru_locks osc
8670     cat $file >/dev/null
8671     AFTER=`roc_hit`
8672     if ! let "AFTER - BEFORE == CPAGES"; then
8673         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8674     else
8675         log "cache hits:: before: $BEFORE, after: $AFTER"
8676     fi
8677
8678
8679     log "Turn off the read cache and turn on the write cache"
8680     set_cache read off
8681     set_cache writethrough on
8682
8683     log "Read again; it should be satisfied from the cache."
8684     BEFORE=`roc_hit`
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     log "Read again; it should not be satisfied from the cache."
8695     BEFORE=$AFTER
8696     cancel_lru_locks osc
8697     cat $file >/dev/null
8698     AFTER=`roc_hit`
8699     if ! let "AFTER - BEFORE == 0"; then
8700         error "IN CACHE: before: $BEFORE, after: $AFTER"
8701     else
8702         log "cache hits:: before: $BEFORE, after: $AFTER"
8703     fi
8704
8705     log "Write data and read it back."
8706     log "Read should be satisfied from the cache."
8707     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8708     BEFORE=`roc_hit`
8709     cancel_lru_locks osc
8710     cat $file >/dev/null
8711     AFTER=`roc_hit`
8712     if ! let "AFTER - BEFORE == CPAGES"; then
8713         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8714     else
8715         log "cache hits:: before: $BEFORE, after: $AFTER"
8716     fi
8717
8718     log "Read again; it should not be satisfied from the cache."
8719     BEFORE=$AFTER
8720     cancel_lru_locks osc
8721     cat $file >/dev/null
8722     AFTER=`roc_hit`
8723     if ! let "AFTER - BEFORE == 0"; then
8724         error "IN CACHE: before: $BEFORE, after: $AFTER"
8725     else
8726         log "cache hits:: before: $BEFORE, after: $AFTER"
8727     fi
8728
8729
8730     log "Turn off read and write cache"
8731     set_cache read off
8732     set_cache writethrough off
8733
8734     log "Write data and read it back"
8735     log "It should not be satisfied from the cache."
8736     rm -f $file
8737     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8738     cancel_lru_locks osc
8739     BEFORE=`roc_hit`
8740     cat $file >/dev/null
8741     AFTER=`roc_hit`
8742     if ! let "AFTER - BEFORE == 0"; then
8743         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8744     else
8745         log "cache hits:: before: $BEFORE, after: $AFTER"
8746     fi
8747
8748
8749     log "Turn on the read cache and turn off the write cache"
8750     set_cache read on
8751     set_cache writethrough off
8752
8753     log "Write data and read it back"
8754     log "It should not be satisfied from the cache."
8755     rm -f $file
8756     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8757     BEFORE=`roc_hit`
8758     cancel_lru_locks osc
8759     cat $file >/dev/null
8760     AFTER=`roc_hit`
8761     if ! let "AFTER - BEFORE == 0"; then
8762         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8763     else
8764         log "cache hits:: before: $BEFORE, after: $AFTER"
8765     fi
8766
8767     log "Read again; it should be satisfied from the cache."
8768     BEFORE=`roc_hit`
8769     cancel_lru_locks osc
8770     cat $file >/dev/null
8771     AFTER=`roc_hit`
8772     if ! let "AFTER - BEFORE == CPAGES"; then
8773         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8774     else
8775         log "cache hits:: before: $BEFORE, after: $AFTER"
8776     fi
8777
8778     rm -f $file
8779 }
8780 run_test 156 "Verification of tunables ============================"
8781
8782 #Changelogs
8783 err17935 () {
8784     if [ $MDSCOUNT -gt 1 ]; then
8785         error_ignore 17935 $*
8786     else
8787         error $*
8788     fi
8789 }
8790
8791 changelog_chmask()
8792 {
8793     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8794            grep -c $1)
8795
8796     if [ $MASK -eq 1 ]; then
8797         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8798     else
8799         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8800     fi
8801 }
8802
8803 test_160() {
8804         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8805     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8806     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8807         { skip "Need MDS version at least 2.2.0"; return; }
8808     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8809     echo "Registered as changelog user $USER"
8810     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8811         grep -q $USER || error "User $USER not found in changelog_users"
8812
8813     # change something
8814     test_mkdir -p $DIR/$tdir/pics/2008/zachy
8815     touch $DIR/$tdir/pics/2008/zachy/timestamp
8816     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8817     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8818     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8819     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8820     rm $DIR/$tdir/pics/desktop.jpg
8821
8822     $LFS changelog $MDT0 | tail -5
8823
8824     echo "verifying changelog mask"
8825     changelog_chmask "MKDIR"
8826     changelog_chmask "CLOSE"
8827
8828     test_mkdir -p $DIR/$tdir/pics/zach/sofia
8829     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8830
8831     changelog_chmask "MKDIR"
8832     changelog_chmask "CLOSE"
8833
8834     test_mkdir -p $DIR/$tdir/pics/2008/sofia
8835     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8836
8837     $LFS changelog $MDT0
8838     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8839     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8840     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8841     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8842
8843     # verify contents
8844     echo "verifying target fid"
8845     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8846         tail -1 | awk '{print $6}')
8847     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8848     [ "$fidc" == "t=$fidf" ] || \
8849         err17935 "fid in changelog $fidc != file fid $fidf"
8850     echo "verifying parent fid"
8851     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8852         tail -1 | awk '{print $7}')
8853     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8854     [ "$fidc" == "p=$fidf" ] || \
8855         err17935 "pfid in changelog $fidc != dir fid $fidf"
8856
8857     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8858         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8859     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8860     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8861         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8862     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8863     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8864         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8865
8866     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8867         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8868     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8869     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8870     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8871         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8872
8873     echo "verifying user deregister"
8874     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8875     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8876         grep -q $USER && error "User $USER still found in changelog_users"
8877
8878     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8879         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8880     if [ $USERS -eq 0 ]; then
8881         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8882             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8883         touch $DIR/$tdir/chloe
8884         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8885             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8886         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8887         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8888     else
8889         echo "$USERS other changelog users; can't verify off"
8890     fi
8891 }
8892 run_test 160 "changelog sanity"
8893
8894 test_161() {
8895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8896     test_mkdir -p $DIR/$tdir
8897     cp /etc/hosts $DIR/$tdir/$tfile
8898     test_mkdir $DIR/$tdir/foo1
8899     test_mkdir $DIR/$tdir/foo2
8900     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8901     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8902     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8903     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8904     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8905     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8906         $LFS fid2path $DIR $FID
8907         err17935 "bad link ea"
8908     fi
8909     # middle
8910     rm $DIR/$tdir/foo2/zachary
8911     # last
8912     rm $DIR/$tdir/foo2/thor
8913     # first
8914     rm $DIR/$tdir/$tfile
8915     # rename
8916     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8917     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8918         then
8919         $LFS fid2path $DIR $FID
8920         err17935 "bad link rename"
8921     fi
8922     rm $DIR/$tdir/foo2/maggie
8923
8924     # overflow the EA
8925     local longname=filename_avg_len_is_thirty_two_
8926     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8927         error "failed to hardlink many files"
8928     links=$($LFS fid2path $DIR $FID | wc -l)
8929     echo -n "${links}/1000 links in link EA"
8930     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8931     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8932         error "failed to unlink many hardlinks"
8933 }
8934 run_test 161 "link ea sanity"
8935
8936 check_path() {
8937     local expected=$1
8938     shift
8939     local fid=$2
8940
8941     local path=$(${LFS} fid2path $*)
8942     RC=$?
8943
8944     if [ $RC -ne 0 ]; then
8945         err17935 "path looked up of $expected failed. Error $RC"
8946         return $RC
8947     elif [ "${path}" != "${expected}" ]; then
8948         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8949         return 2
8950     fi
8951     echo "fid $fid resolves to path $path (expected $expected)"
8952 }
8953
8954 test_162() {
8955         # Make changes to filesystem
8956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8957         test_mkdir -p $DIR/$tdir/d2
8958         touch $DIR/$tdir/d2/$tfile
8959         touch $DIR/$tdir/d2/x1
8960         touch $DIR/$tdir/d2/x2
8961         test_mkdir -p $DIR/$tdir/d2/a/b/c
8962         test_mkdir -p $DIR/$tdir/d2/p/q/r
8963         # regular file
8964         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8965         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8966
8967         # softlink
8968         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8969         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8970         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8971
8972         # softlink to wrong file
8973         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8974         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8975         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8976
8977         # hardlink
8978         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8979         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8980         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8981         # fid2path dir/fsname should both work
8982         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8983         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8984
8985         # hardlink count: check that there are 2 links
8986         # Doesnt work with CMD yet: 17935
8987         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8988                 err17935 "expected 2 links"
8989
8990         # hardlink indexing: remove the first link
8991         rm $DIR/$tdir/d2/p/q/r/hlink
8992         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8993
8994         return 0
8995 }
8996 run_test 162 "path lookup sanity"
8997
8998 test_163() {
8999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9000         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9001         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
9002         copytool $FSNAME &
9003         sleep 1
9004         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
9005         # this proc file is temporary and linux-only
9006         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
9007          error "kernel->userspace send failed"
9008         kill -INT $!
9009 }
9010 run_test 163 "kernel <-> userspace comms"
9011
9012 test_169() {
9013         # do directio so as not to populate the page cache
9014         log "creating a 10 Mb file"
9015         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9016         log "starting reads"
9017         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9018         log "truncating the file"
9019         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9020         log "killing dd"
9021         kill %+ || true # reads might have finished
9022         echo "wait until dd is finished"
9023         wait
9024         log "removing the temporary file"
9025         rm -rf $DIR/$tfile || error "tmp file removal failed"
9026 }
9027 run_test 169 "parallel read and truncate should not deadlock"
9028
9029 test_170() {
9030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9031         $LCTL clear     # bug 18514
9032         $LCTL debug_daemon start $TMP/${tfile}_log_good
9033         touch $DIR/$tfile
9034         $LCTL debug_daemon stop
9035         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9036                error "sed failed to read log_good"
9037
9038         $LCTL debug_daemon start $TMP/${tfile}_log_good
9039         rm -rf $DIR/$tfile
9040         $LCTL debug_daemon stop
9041
9042         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9043                error "lctl df log_bad failed"
9044
9045         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9046         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9047
9048         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9049         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9050
9051         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9052                 error "bad_line good_line1 good_line2 are empty"
9053
9054         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9055         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9056         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9057
9058         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9059         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9060         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9061
9062         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9063                 error "bad_line_new good_line_new are empty"
9064
9065         local expected_good=$((good_line1 + good_line2*2))
9066
9067         rm -f $TMP/${tfile}*
9068         # LU-231, short malformed line may not be counted into bad lines
9069         if [ $bad_line -ne $bad_line_new ] &&
9070                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9071                 error "expected $bad_line bad lines, but got $bad_line_new"
9072                 return 1
9073         fi
9074
9075         if [ $expected_good -ne $good_line_new ]; then
9076                 error "expected $expected_good good lines, but got $good_line_new"
9077                 return 2
9078         fi
9079         true
9080 }
9081 run_test 170 "test lctl df to handle corrupted log ====================="
9082
9083 test_171() { # bug20592
9084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9085 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9086         $LCTL set_param fail_loc=0x50e
9087         $LCTL set_param fail_val=3000
9088         multiop_bg_pause $DIR/$tfile O_s || true
9089         local MULTIPID=$!
9090         kill -USR1 $MULTIPID
9091         # cause log dump
9092         sleep 3
9093         wait $MULTIPID
9094         if dmesg | grep "recursive fault"; then
9095                 error "caught a recursive fault"
9096         fi
9097         $LCTL set_param fail_loc=0
9098         true
9099 }
9100 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
9101
9102 # it would be good to share it with obdfilter-survey/libecho code
9103 setup_obdecho_osc () {
9104         local rc=0
9105         local ost_nid=$1
9106         local obdfilter_name=$2
9107         echo "Creating new osc for $obdfilter_name on $ost_nid"
9108         # make sure we can find loopback nid
9109         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
9110
9111         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
9112                            ${obdfilter_name}_osc_UUID || rc=2; }
9113         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
9114                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
9115         return $rc
9116 }
9117
9118 cleanup_obdecho_osc () {
9119         local obdfilter_name=$1
9120         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
9121         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
9122         return 0
9123 }
9124
9125 obdecho_create_test() {
9126         local OBD=$1
9127         local node=$2
9128         local rc=0
9129         local id
9130         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
9131         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
9132                            rc=2; }
9133         if [ $rc -eq 0 ]; then
9134             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
9135             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
9136         fi
9137         echo "New object id is $id"
9138         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
9139                            rc=4; }
9140         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
9141                                         "cleanup" || rc=5; }
9142         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
9143                                         "detach" || rc=6; }
9144         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
9145         return $rc
9146 }
9147
9148 test_180a() {
9149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9150         remote_ost_nodsh && skip "remote OST with nodsh" && return
9151         local rc=0
9152         local rmmod_local=0
9153
9154         if ! module_loaded obdecho; then
9155             load_module obdecho/obdecho
9156             rmmod_local=1
9157         fi
9158
9159         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
9160         local host=$(lctl get_param -n osc.$osc.import |
9161                              awk '/current_connection:/ {print $2}' )
9162         local target=$(lctl get_param -n osc.$osc.import |
9163                              awk '/target:/ {print $2}' )
9164         target=${target%_UUID}
9165
9166         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
9167         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
9168         [[ -n $target ]] && cleanup_obdecho_osc $target
9169         [ $rmmod_local -eq 1 ] && rmmod obdecho
9170         return $rc
9171 }
9172 run_test 180a "test obdecho on osc"
9173
9174 test_180b() {
9175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9176         remote_ost_nodsh && skip "remote OST with nodsh" && return
9177         local rc=0
9178         local rmmod_remote=0
9179
9180         do_facet ost1 "lsmod | grep -q obdecho || "                      \
9181                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
9182                       "modprobe obdecho; }" && rmmod_remote=1
9183         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
9184         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
9185         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
9186         return $rc
9187 }
9188 run_test 180b "test obdecho directly on obdfilter"
9189
9190 test_181() { # bug 22177
9191         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9192         # create enough files to index the directory
9193         createmany -o $DIR/$tdir/foobar 4000
9194         # print attributes for debug purpose
9195         lsattr -d .
9196         # open dir
9197         multiop_bg_pause $DIR/$tdir D_Sc || return 1
9198         MULTIPID=$!
9199         # remove the files & current working dir
9200         unlinkmany $DIR/$tdir/foobar 4000
9201         rmdir $DIR/$tdir
9202         kill -USR1 $MULTIPID
9203         wait $MULTIPID
9204         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9205         return 0
9206 }
9207 run_test 181 "Test open-unlinked dir ========================"
9208
9209 test_182() {
9210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9211         # disable MDC RPC lock wouldn't crash client
9212         local fcount=1000
9213         local tcount=4
9214
9215         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9216 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9217         $LCTL set_param fail_loc=0x804
9218
9219         for (( i=0; i < $tcount; i++ )) ; do
9220                 mkdir $DIR/$tdir/$i
9221                 createmany -o $DIR/$tdir/$i/f- $fcount &
9222         done
9223         wait
9224
9225         for (( i=0; i < $tcount; i++ )) ; do
9226                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9227         done
9228         wait
9229
9230         rm -rf $DIR/$tdir
9231
9232         $LCTL set_param fail_loc=0
9233 }
9234 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9235
9236 test_183() { # LU-2275
9237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9238         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9239         echo aaa > $DIR/$tdir/$tfile
9240
9241 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9242         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9243
9244         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9245         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9246
9247         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9248
9249         # Flush negative dentry cache
9250         touch $DIR/$tdir/$tfile
9251
9252         # We are not checking for any leaked references here, they'll
9253         # become evident next time we do cleanup with module unload.
9254         rm -rf $DIR/$tdir
9255 }
9256 run_test 183 "No crash or request leak in case of strange dispositions ========"
9257
9258 # OST pools tests
9259 check_file_in_pool()
9260 {
9261         local file=$1
9262         local pool=$2
9263         local tlist="$3"
9264         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9265         for i in $res
9266         do
9267                 for t in $tlist ; do
9268                         [ "$i" -eq "$t" ] && continue 2
9269                 done
9270
9271                 echo "pool list: $tlist"
9272                 echo "striping: $res"
9273                 error_noexit "$file not allocated in $pool"
9274                 return 1
9275         done
9276         return 0
9277 }
9278
9279 pool_add() {
9280         echo "Creating new pool"
9281         local pool=$1
9282
9283         create_pool $FSNAME.$pool ||
9284                 { error_noexit "No pool created, result code $?"; return 1; }
9285         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9286                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9287 }
9288
9289 pool_add_targets() {
9290         echo "Adding targets to pool"
9291         local pool=$1
9292         local first=$2
9293         local last=$3
9294         local step=${4:-1}
9295
9296         local list=$(seq $first $step $last)
9297
9298         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9299         do_facet mgs $LCTL pool_add \
9300                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9301         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9302                         | sort -u | tr '\n' ' ' " "$t" || { 
9303                 error_noexit "Add to pool failed"
9304                 return 1
9305         }
9306         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9307         local addcount=$(((last - first) / step + 1))
9308         [ $lfscount -eq $addcount ] || {
9309                 error_noexit "lfs pool_list bad ost count" \
9310                                                 "$lfscount != $addcount"
9311                 return 2
9312         }
9313 }
9314
9315 pool_set_dir() {
9316         local pool=$1
9317         local tdir=$2
9318         echo "Setting pool on directory $tdir"
9319
9320         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9321
9322         error_noexit "Cannot set pool $pool to $tdir"
9323         return 1
9324 }
9325
9326 pool_check_dir() {
9327         local pool=$1
9328         local tdir=$2
9329         echo "Checking pool on directory $tdir"
9330
9331         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9332         [ "$res" = "$pool" ] && return 0
9333
9334         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9335         return 1
9336 }
9337
9338 pool_dir_rel_path() {
9339         echo "Testing relative path works well"
9340         local pool=$1
9341         local tdir=$2
9342         local root=$3
9343
9344         mkdir -p $root/$tdir/$tdir
9345         cd $root/$tdir
9346         pool_set_dir $pool $tdir          || return 1
9347         pool_set_dir $pool ./$tdir        || return 2
9348         pool_set_dir $pool ../$tdir       || return 3
9349         pool_set_dir $pool ../$tdir/$tdir || return 4
9350         rm -rf $tdir; cd - > /dev/null
9351 }
9352
9353 pool_alloc_files() {
9354         echo "Checking files allocation from directory pool"
9355         local pool=$1
9356         local tdir=$2
9357         local count=$3
9358         local tlist="$4"
9359
9360         local failed=0
9361         for i in $(seq -w 1 $count)
9362         do
9363                 local file=$tdir/file-$i
9364                 touch $file
9365                 check_file_in_pool $file $pool "$tlist" || \
9366                         failed=$((failed + 1))
9367         done
9368         [ "$failed" = 0 ] && return 0
9369
9370         error_noexit "$failed files not allocated in $pool"
9371         return 1
9372 }
9373
9374 pool_create_files() {
9375         echo "Creating files in pool"
9376         local pool=$1
9377         local tdir=$2
9378         local count=$3
9379         local tlist="$4"
9380
9381         mkdir -p $tdir
9382         local failed=0
9383         for i in $(seq -w 1 $count)
9384         do
9385                 local file=$tdir/spoo-$i
9386                 $SETSTRIPE -p $pool $file
9387                 check_file_in_pool $file $pool "$tlist" || \
9388                         failed=$((failed + 1))
9389         done
9390         [ "$failed" = 0 ] && return 0
9391
9392         error_noexit "$failed files not allocated in $pool"
9393         return 1
9394 }
9395
9396 pool_lfs_df() {
9397         echo "Checking 'lfs df' output"
9398         local pool=$1
9399
9400         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9401                         tr '\n' ' ')
9402         local res=$($LFS df --pool $FSNAME.$pool |
9403                         awk '{print $1}' |
9404                         grep "$FSNAME-OST" |
9405                         tr '\n' ' ')
9406         [ "$res" = "$t" ] && return 0
9407
9408         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9409         return 1
9410 }
9411
9412 pool_file_rel_path() {
9413         echo "Creating files in a pool with relative pathname"
9414         local pool=$1
9415         local tdir=$2
9416
9417         mkdir -p $tdir ||
9418                 { error_noexit "unable to create $tdir"; return 1 ; }
9419         local file="/..$tdir/$tfile-1"
9420         $SETSTRIPE -p $pool $file ||
9421                 { error_noexit "unable to create $file" ; return 2 ; }
9422
9423         cd $tdir
9424         $SETSTRIPE -p $pool $tfile-2 || {
9425                 error_noexit "unable to create $tfile-2 in $tdir"
9426                 return 3
9427         }
9428 }
9429
9430 pool_remove_first_target() {
9431         echo "Removing first target from a pool"
9432         local pool=$1
9433
9434         local pname="lov.$FSNAME-*.pools.$pool"
9435         local t=$($LCTL get_param -n $pname | head -1)
9436         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9437         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9438                 error_noexit "$t not removed from $FSNAME.$pool"
9439                 return 1
9440         }
9441 }
9442
9443 pool_remove_all_targets() {
9444         echo "Removing all targets from pool"
9445         local pool=$1
9446         local file=$2
9447         local pname="lov.$FSNAME-*.pools.$pool"
9448         for t in $($LCTL get_param -n $pname | sort -u)
9449         do
9450                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9451         done
9452         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9453                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9454                 return 1
9455         }
9456         # striping on an empty/nonexistant pool should fall back 
9457         # to "pool of everything"
9458         touch $file || {
9459                 error_noexit "failed to use fallback striping for empty pool"
9460                 return 2
9461         }
9462         # setstripe on an empty pool should fail
9463         $SETSTRIPE -p $pool $file 2>/dev/null && {
9464                 error_noexit "expected failure when creating file" \
9465                                                         "with empty pool"
9466                 return 3
9467         }
9468         return 0
9469 }
9470
9471 pool_remove() {
9472         echo "Destroying pool"
9473         local pool=$1
9474         local file=$2
9475
9476         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9477
9478         sleep 2
9479         # striping on an empty/nonexistant pool should fall back 
9480         # to "pool of everything"
9481         touch $file || {
9482                 error_noexit "failed to use fallback striping for missing pool"
9483                 return 1
9484         }
9485         # setstripe on an empty pool should fail
9486         $SETSTRIPE -p $pool $file 2>/dev/null && {
9487                 error_noexit "expected failure when creating file" \
9488                                                         "with missing pool"
9489                 return 2
9490         }
9491
9492         # get param should return err once pool is gone
9493         if wait_update $HOSTNAME "lctl get_param -n \
9494                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9495         then
9496                 remove_pool_from_list $FSNAME.$pool
9497                 return 0
9498         fi
9499         error_noexit "Pool $FSNAME.$pool is not destroyed"
9500         return 3
9501 }
9502
9503 test_200() {
9504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9505         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9506
9507         local POOL=${POOL:-cea1}
9508         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9509         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9510         # Pool OST targets
9511         local first_ost=0
9512         local last_ost=$(($OSTCOUNT - 1))
9513         local ost_step=2
9514         local ost_list=$(seq $first_ost $ost_step $last_ost)
9515         local ost_range="$first_ost $last_ost $ost_step"
9516         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9517         local file_dir=$POOL_ROOT/file_tst
9518
9519         local rc=0
9520         while : ; do
9521                 # former test_200a test_200b
9522                 pool_add $POOL                          || { rc=$? ; break; }
9523                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9524                 # former test_200c test_200d
9525                 mkdir -p $test_path
9526                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9527                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9528                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9529                                                         || { rc=$? ; break; }
9530                 # former test_200e test_200f
9531                 local files=$((OSTCOUNT*3))
9532                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9533                                                         || { rc=$? ; break; }
9534                 pool_create_files $POOL $file_dir $files "$ost_list" \
9535                                                         || { rc=$? ; break; }
9536                 # former test_200g test_200h
9537                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9538                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9539
9540                 # former test_201a test_201b test_201c
9541                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9542
9543                 local f=$test_path/$tfile
9544                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9545                 pool_remove $POOL $f                    || { rc=$? ; break; }
9546                 break
9547         done
9548
9549         cleanup_pools
9550         return $rc
9551 }
9552 run_test 200 "OST pools"
9553
9554 # usage: default_attr <count | size | offset>
9555 default_attr() {
9556         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9557 }
9558
9559 # usage: check_default_stripe_attr
9560 check_default_stripe_attr() {
9561         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9562         case $1 in
9563         --stripe-count|--count)
9564                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9565         --stripe-size|--size)
9566                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9567         --stripe-index|--index)
9568                 EXPECTED=-1;;
9569         *)
9570                 error "unknown getstripe attr '$1'"
9571         esac
9572
9573         [ $ACTUAL != $EXPECTED ] &&
9574                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9575 }
9576
9577 test_204a() {
9578         test_mkdir -p $DIR/$tdir
9579         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9580
9581         check_default_stripe_attr --stripe-count
9582         check_default_stripe_attr --stripe-size
9583         check_default_stripe_attr --stripe-index
9584
9585         return 0
9586 }
9587 run_test 204a "Print default stripe attributes ================="
9588
9589 test_204b() {
9590         test_mkdir -p $DIR/$tdir
9591         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9592
9593         check_default_stripe_attr --stripe-size
9594         check_default_stripe_attr --stripe-index
9595
9596         return 0
9597 }
9598 run_test 204b "Print default stripe size and offset  ==========="
9599
9600 test_204c() {
9601         test_mkdir -p $DIR/$tdir
9602         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9603
9604         check_default_stripe_attr --stripe-count
9605         check_default_stripe_attr --stripe-index
9606
9607         return 0
9608 }
9609 run_test 204c "Print default stripe count and offset ==========="
9610
9611 test_204d() {
9612         test_mkdir -p $DIR/$tdir
9613         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9614
9615         check_default_stripe_attr --stripe-count
9616         check_default_stripe_attr --stripe-size
9617
9618         return 0
9619 }
9620 run_test 204d "Print default stripe count and size ============="
9621
9622 test_204e() {
9623         test_mkdir -p $DIR/$tdir
9624         $SETSTRIPE -d $DIR/$tdir
9625
9626         check_default_stripe_attr --stripe-count --raw
9627         check_default_stripe_attr --stripe-size --raw
9628         check_default_stripe_attr --stripe-index --raw
9629
9630         return 0
9631 }
9632 run_test 204e "Print raw stripe attributes ================="
9633
9634 test_204f() {
9635         test_mkdir -p $DIR/$tdir
9636         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9637
9638         check_default_stripe_attr --stripe-size --raw
9639         check_default_stripe_attr --stripe-index --raw
9640
9641         return 0
9642 }
9643 run_test 204f "Print raw stripe size and offset  ==========="
9644
9645 test_204g() {
9646         test_mkdir -p $DIR/$tdir
9647         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9648
9649         check_default_stripe_attr --stripe-count --raw
9650         check_default_stripe_attr --stripe-index --raw
9651
9652         return 0
9653 }
9654 run_test 204g "Print raw stripe count and offset ==========="
9655
9656 test_204h() {
9657         test_mkdir -p $DIR/$tdir
9658         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9659
9660         check_default_stripe_attr --stripe-count --raw
9661         check_default_stripe_attr --stripe-size --raw
9662
9663         return 0
9664 }
9665 run_test 204h "Print raw stripe count and size ============="
9666
9667 # Figure out which job scheduler is being used, if any,
9668 # or use a fake one
9669 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9670         JOBENV=SLURM_JOB_ID
9671 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9672         JOBENV=LSB_JOBID
9673 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9674         JOBENV=PBS_JOBID
9675 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9676         JOBENV=LOADL_STEP_ID
9677 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9678         JOBENV=JOB_ID
9679 else
9680         JOBENV=FAKE_JOBID
9681 fi
9682
9683 verify_jobstats() {
9684         local cmd=$1
9685         local target=$2
9686
9687         # clear old jobstats
9688         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9689         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9690
9691         # use a new JobID for this test, or we might see an old one
9692         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9693
9694         JOBVAL=${!JOBENV}
9695         log "Test: $cmd"
9696         log "Using JobID environment variable $JOBENV=$JOBVAL"
9697
9698         if [ $JOBENV = "FAKE_JOBID" ]; then
9699                 FAKE_JOBID=$JOBVAL $cmd
9700         else
9701                 $cmd
9702         fi
9703
9704         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9705                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9706                 do_facet $FACET lctl get_param mdt.*.job_stats |
9707                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9708         fi
9709         if [ "$target" = "ost" -o "$target" = "both" ]; then
9710                 FACET=ost1
9711                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9712                         grep $JOBVAL || error "No job stats found on OST $FACET"
9713         fi
9714 }
9715
9716 test_205() { # Job stats
9717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9718         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9719                 skip "Server doesn't support jobstats" && return 0
9720
9721         local cmd
9722         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9723         if [ $OLD_JOBENV != $JOBENV ]; then
9724                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9725                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9726                         $JOBENV || return 1
9727         fi
9728
9729         # mkdir
9730         cmd="mkdir $DIR/$tfile"
9731         verify_jobstats "$cmd" "mdt"
9732         # rmdir
9733         cmd="rm -fr $DIR/$tfile"
9734         verify_jobstats "$cmd" "mdt"
9735         # mknod
9736         cmd="mknod $DIR/$tfile c 1 3"
9737         verify_jobstats "$cmd" "mdt"
9738         # unlink
9739         cmd="rm -f $DIR/$tfile"
9740         verify_jobstats "$cmd" "mdt"
9741         # open & close
9742         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9743         verify_jobstats "$cmd" "mdt"
9744         # setattr
9745         cmd="touch $DIR/$tfile"
9746         verify_jobstats "$cmd" "both"
9747         # write
9748         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9749         verify_jobstats "$cmd" "ost"
9750         # read
9751         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9752         verify_jobstats "$cmd" "ost"
9753         # truncate
9754         cmd="$TRUNCATE $DIR/$tfile 0"
9755         verify_jobstats "$cmd" "both"
9756         # rename
9757         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9758         verify_jobstats "$cmd" "mdt"
9759
9760         # cleanup
9761         rm -f $DIR/jobstats_test_rename
9762
9763         if [ $OLD_JOBENV != $JOBENV ]; then
9764                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9765                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9766                         $OLD_JOBENV || return 1
9767         fi
9768 }
9769 run_test 205 "Verify job stats"
9770
9771 # LU-1480, LU-1773 and LU-1657
9772 test_206() {
9773         mkdir -p $DIR/$tdir
9774         lfs setstripe -c -1 $DIR/$tdir
9775 #define OBD_FAIL_LOV_INIT 0x1403
9776         $LCTL set_param fail_loc=0xa0001403
9777         $LCTL set_param fail_val=1
9778         touch $DIR/$tdir/$tfile || true
9779 }
9780 run_test 206 "fail lov_init_raid0() doesn't lbug"
9781
9782 test_207a() {
9783         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9784         local fsz=`stat -c %s $DIR/$tfile`
9785         cancel_lru_locks mdc
9786
9787         # do not return layout in getattr intent
9788 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
9789         $LCTL set_param fail_loc=0x170
9790         local sz=`stat -c %s $DIR/$tfile`
9791
9792         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
9793
9794         rm -rf $DIR/$tfile
9795 }
9796 run_test 207a "can refresh layout at glimpse"
9797
9798 test_207b() {
9799         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9800         local cksum=`md5sum $DIR/$tfile`
9801         local fsz=`stat -c %s $DIR/$tfile`
9802         cancel_lru_locks mdc
9803         cancel_lru_locks osc
9804
9805         # do not return layout in getattr intent
9806 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
9807         $LCTL set_param fail_loc=0x171
9808
9809         # it will refresh layout after the file is opened but before read issues
9810         echo checksum is "$cksum"
9811         echo "$cksum" |md5sum -c --quiet || error "file differs"
9812
9813         rm -rf $DIR/$tfile
9814 }
9815 run_test 207b "can refresh layout at open"
9816
9817 test_212() {
9818         size=`date +%s`
9819         size=$((size % 8192 + 1))
9820         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9821         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9822         rm -f $DIR/f212 $DIR/f212.xyz
9823 }
9824 run_test 212 "Sendfile test ============================================"
9825
9826 test_213() {
9827         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9828         cancel_lru_locks osc
9829         lctl set_param fail_loc=0x8000040f
9830         # generate a read lock
9831         cat $DIR/$tfile > /dev/null
9832         # write to the file, it will try to cancel the above read lock.
9833         cat /etc/hosts >> $DIR/$tfile
9834 }
9835 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9836
9837 test_214() { # for bug 20133
9838         test_mkdir -p $DIR/d214p/d214c
9839         for (( i=0; i < 340; i++ )) ; do
9840                 touch $DIR/d214p/d214c/a$i
9841         done
9842
9843         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9844         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9845         ls $DIR/d214c || error "ls $DIR/d214c failed"
9846         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9847 }
9848 run_test 214 "hash-indexed directory test - bug 20133"
9849
9850 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9851 create_lnet_proc_files() {
9852         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9853         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9854
9855         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9856         rm -f "$TMP/lnet_$1.sys_tmp"
9857 }
9858
9859 # counterpart of create_lnet_proc_files
9860 remove_lnet_proc_files() {
9861         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9862 }
9863
9864 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9865 # 3rd arg as regexp for body
9866 check_lnet_proc_stats() {
9867         local l=$(cat "$TMP/lnet_$1" |wc -l)
9868         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9869
9870         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9871 }
9872
9873 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9874 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9875 # optional and can be regexp for 2nd line (lnet.routes case)
9876 check_lnet_proc_entry() {
9877         local blp=2            # blp stands for 'position of 1st line of body'
9878         [ "$5" = "" ] || blp=3 # lnet.routes case
9879
9880         local l=$(cat "$TMP/lnet_$1" |wc -l)
9881         # subtracting one from $blp because the body can be empty
9882         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9883
9884         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9885                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9886
9887         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9888                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9889
9890         # bail out if any unexpected line happened
9891         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9892         [ "$?" != 0 ] || error "$2 misformatted"
9893 }
9894
9895 test_215() { # for bugs 18102, 21079, 21517
9896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9897         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9898         local P='[1-9][0-9]*'           # positive numeric
9899         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9900         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9901         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9902         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9903
9904         local L1 # regexp for 1st line
9905         local L2 # regexp for 2nd line (optional)
9906         local BR # regexp for the rest (body)
9907
9908         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9909         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9910         create_lnet_proc_files "stats"
9911         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9912         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9913         remove_lnet_proc_files "stats"
9914
9915         # /proc/sys/lnet/routes should look like this:
9916         # Routing disabled/enabled
9917         # net hops state router
9918         # where net is a string like tcp0, hops >= 0, state is up/down,
9919         # router is a string like 192.168.1.1@tcp2
9920         L1="^Routing (disabled|enabled)$"
9921         L2="^net +hops +state +router$"
9922         BR="^$NET +$N +(up|down) +$NID$"
9923         create_lnet_proc_files "routes"
9924         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9925         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9926         remove_lnet_proc_files "routes"
9927
9928         # /proc/sys/lnet/routers should look like this:
9929         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9930         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9931         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9932         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9933         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9934         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9935         create_lnet_proc_files "routers"
9936         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9937         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9938         remove_lnet_proc_files "routers"
9939
9940         # /proc/sys/lnet/peers should look like this:
9941         # nid refs state last max rtr min tx min queue
9942         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9943         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9944         # numeric (0 or >0 or <0), queue >= 0.
9945         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9946         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9947         create_lnet_proc_files "peers"
9948         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9949         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9950         remove_lnet_proc_files "peers"
9951
9952         # /proc/sys/lnet/buffers  should look like this:
9953         # pages count credits min
9954         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9955         L1="^pages +count +credits +min$"
9956         BR="^ +$N +$N +$I +$I$"
9957         create_lnet_proc_files "buffers"
9958         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9959         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9960         remove_lnet_proc_files "buffers"
9961
9962         # /proc/sys/lnet/nis should look like this:
9963         # nid status alive refs peer rtr max tx min
9964         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9965         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9966         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9967         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9968         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9969         create_lnet_proc_files "nis"
9970         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9971         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9972         remove_lnet_proc_files "nis"
9973
9974         # can we successfully write to /proc/sys/lnet/stats?
9975         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9976         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9977 }
9978 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9979
9980 test_216() { # bug 20317
9981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9982         remote_ost_nodsh && skip "remote OST with nodsh" && return
9983         local node
9984         local p="$TMP/sanityN-$TESTNAME.parameters"
9985         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9986         for node in $(osts_nodes); do
9987                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9988                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9989                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9990         done
9991         clear_osc_stats
9992
9993         # agressive lockless i/o settings
9994         for node in $(osts_nodes); do
9995                 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'
9996         done
9997         lctl set_param -n osc.*.contention_seconds 60
9998
9999         $DIRECTIO write $DIR/$tfile 0 10 4096
10000         $CHECKSTAT -s 40960 $DIR/$tfile
10001
10002         # disable lockless i/o
10003         for node in $(osts_nodes); do
10004                 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'
10005         done
10006         lctl set_param -n osc.*.contention_seconds 0
10007         clear_osc_stats
10008
10009         dd if=/dev/zero of=$DIR/$tfile count=0
10010         $CHECKSTAT -s 0 $DIR/$tfile
10011
10012         restore_lustre_params <$p
10013         rm -f $p
10014         rm $DIR/$tfile
10015 }
10016 run_test 216 "check lockless direct write works and updates file size and kms correctly"
10017
10018 test_217() { # bug 22430
10019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10020         local node
10021         local nid
10022
10023         for node in $(nodes_list); do
10024                 nid=$(host_nids_address $node $NETTYPE)
10025                 if [[ $nid = *-* ]] ; then
10026                         echo "lctl ping $nid@$NETTYPE"
10027                         lctl ping $nid@$NETTYPE
10028                 else
10029                         echo "skipping $node (no hyphen detected)"
10030                 fi
10031         done
10032 }
10033 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
10034
10035 test_218() {
10036        # do directio so as not to populate the page cache
10037        log "creating a 10 Mb file"
10038        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10039        log "starting reads"
10040        dd if=$DIR/$tfile of=/dev/null bs=4096 &
10041        log "truncating the file"
10042        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10043        log "killing dd"
10044        kill %+ || true # reads might have finished
10045        echo "wait until dd is finished"
10046        wait
10047        log "removing the temporary file"
10048        rm -rf $DIR/$tfile || error "tmp file removal failed"
10049 }
10050 run_test 218 "parallel read and truncate should not deadlock ======================="
10051
10052 test_219() {
10053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10054         # write one partial page
10055         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
10056         # set no grant so vvp_io_commit_write will do sync write
10057         $LCTL set_param fail_loc=0x411
10058         # write a full page at the end of file
10059         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
10060
10061         $LCTL set_param fail_loc=0
10062         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
10063         $LCTL set_param fail_loc=0x411
10064         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
10065 }
10066 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
10067
10068 test_220() { #LU-325
10069         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10070         remote_ost_nodsh && skip "remote OST with nodsh" && return
10071         local OSTIDX=0
10072
10073         test_mkdir -p $DIR/$tdir
10074         local OST=$(lfs osts | grep ${OSTIDX}": " | \
10075                 awk '{print $2}' | sed -e 's/_UUID$//')
10076
10077         # on the mdt's osc
10078         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
10079         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
10080                         osc.$mdtosc_proc1.prealloc_last_id)
10081         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
10082                         osc.$mdtosc_proc1.prealloc_next_id)
10083
10084         $LFS df -i
10085
10086         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
10087         #define OBD_FAIL_OST_ENOINO              0x229
10088         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
10089         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
10090         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
10091
10092         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
10093
10094         MDSOBJS=$((last_id - next_id))
10095         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
10096
10097         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
10098         echo "OST still has $count kbytes free"
10099
10100         echo "create $MDSOBJS files @next_id..."
10101         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
10102
10103         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10104                         osc.$mdtosc_proc1.prealloc_last_id)
10105         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10106                         osc.$mdtosc_proc1.prealloc_next_id)
10107
10108         echo "after creation, last_id=$last_id2, next_id=$next_id2"
10109         $LFS df -i
10110
10111         echo "cleanup..."
10112
10113         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
10114         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
10115
10116         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
10117         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
10118         echo "unlink $MDSOBJS files @$next_id..."
10119         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
10120 }
10121 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
10122
10123 test_221() {
10124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10125         dd if=`which date` of=$MOUNT/date oflag=sync
10126         chmod +x $MOUNT/date
10127
10128         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
10129         $LCTL set_param fail_loc=0x80001401
10130
10131         $MOUNT/date > /dev/null
10132         rm -f $MOUNT/date
10133 }
10134 run_test 221 "make sure fault and truncate race to not cause OOM"
10135
10136 test_222a () {
10137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10138        rm -rf $DIR/$tdir
10139        test_mkdir -p $DIR/$tdir
10140        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10141        createmany -o $DIR/$tdir/$tfile 10
10142        cancel_lru_locks mdc
10143        cancel_lru_locks osc
10144        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10145        $LCTL set_param fail_loc=0x31a
10146        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
10147        $LCTL set_param fail_loc=0
10148        rm -r $DIR/$tdir
10149 }
10150 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
10151
10152 test_222b () {
10153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10154        rm -rf $DIR/$tdir
10155        test_mkdir -p $DIR/$tdir
10156        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10157        createmany -o $DIR/$tdir/$tfile 10
10158        cancel_lru_locks mdc
10159        cancel_lru_locks osc
10160        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10161        $LCTL set_param fail_loc=0x31a
10162        rm -r $DIR/$tdir || "AGL for rmdir failed"
10163        $LCTL set_param fail_loc=0
10164 }
10165 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
10166
10167 test_223 () {
10168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10169        rm -rf $DIR/$tdir
10170        test_mkdir -p $DIR/$tdir
10171        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10172        createmany -o $DIR/$tdir/$tfile 10
10173        cancel_lru_locks mdc
10174        cancel_lru_locks osc
10175        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
10176        $LCTL set_param fail_loc=0x31b
10177        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
10178        $LCTL set_param fail_loc=0
10179        rm -r $DIR/$tdir
10180 }
10181 run_test 223 "osc reenqueue if without AGL lock granted ======================="
10182
10183 test_224a() { # LU-1039, MRP-303
10184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10185         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
10186         $LCTL set_param fail_loc=0x508
10187         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
10188         $LCTL set_param fail_loc=0
10189         df $DIR
10190 }
10191 run_test 224a "Don't panic on bulk IO failure"
10192
10193 test_224b() { # LU-1039, MRP-303
10194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10195         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
10196         cancel_lru_locks osc
10197         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
10198         $LCTL set_param fail_loc=0x515
10199         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
10200         $LCTL set_param fail_loc=0
10201         df $DIR
10202 }
10203 run_test 224b "Don't panic on bulk IO failure"
10204
10205 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
10206 test_225a () {
10207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10208        if [ -z ${MDSSURVEY} ]; then
10209               skip_env "mds-survey not found" && return
10210        fi
10211        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10212             { skip "Need MDS version at least 2.2.51"; return; }
10213
10214        local mds=$(facet_host $SINGLEMDS)
10215        local target=$(do_nodes $mds 'lctl dl' | \
10216                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10217
10218        local cmd1="file_count=1000 thrhi=4"
10219        local cmd2="dir_count=2 layer=mdd stripe_count=0"
10220        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10221        local cmd="$cmd1 $cmd2 $cmd3"
10222
10223        rm -f ${TMP}/mds_survey*
10224        echo + $cmd
10225        eval $cmd || error "mds-survey with zero-stripe failed"
10226        cat ${TMP}/mds_survey*
10227        rm -f ${TMP}/mds_survey*
10228 }
10229 run_test 225a "Metadata survey sanity with zero-stripe"
10230
10231 test_225b () {
10232         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10233        if [ -z ${MDSSURVEY} ]; then
10234               skip_env "mds-survey not found" && return
10235        fi
10236        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10237             { skip "Need MDS version at least 2.2.51"; return; }
10238
10239        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
10240               skip_env "Need to mount OST to test" && return
10241        fi
10242
10243        local mds=$(facet_host $SINGLEMDS)
10244        local target=$(do_nodes $mds 'lctl dl' | \
10245                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10246
10247        local cmd1="file_count=1000 thrhi=4"
10248        local cmd2="dir_count=2 layer=mdd stripe_count=1"
10249        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10250        local cmd="$cmd1 $cmd2 $cmd3"
10251
10252        rm -f ${TMP}/mds_survey*
10253        echo + $cmd
10254        eval $cmd || error "mds-survey with stripe_count failed"
10255        cat ${TMP}/mds_survey*
10256        rm -f ${TMP}/mds_survey*
10257 }
10258 run_test 225b "Metadata survey sanity with stripe_count = 1"
10259
10260 mcreate_path2fid () {
10261         local mode=$1
10262         local major=$2
10263         local minor=$3
10264         local name=$4
10265         local desc=$5
10266         local path=$DIR/$tdir/$name
10267         local fid
10268         local rc
10269         local fid_path
10270
10271         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10272                 error "cannot create $desc"
10273
10274         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
10275         rc=$?
10276         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10277
10278         fid_path=$($LFS fid2path $MOUNT $fid)
10279         rc=$?
10280         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10281
10282         [ "$path" == "$fid_path" ] ||
10283                 error "fid2path returned $fid_path, expected $path"
10284
10285         echo "pass with $path and $fid"
10286 }
10287
10288 test_226 () {
10289         rm -rf $DIR/$tdir
10290         mkdir -p $DIR/$tdir
10291
10292         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10293         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10294         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10295         mcreate_path2fid 0040666 0 0 dir "directory"
10296         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10297         mcreate_path2fid 0100666 0 0 file "regular file"
10298         mcreate_path2fid 0120666 0 0 link "symbolic link"
10299         mcreate_path2fid 0140666 0 0 sock "socket"
10300 }
10301 run_test 226 "call path2fid and fid2path on files of all type"
10302
10303 # LU-1299 Executing or running ldd on a truncated executable does not
10304 # cause an out-of-memory condition.
10305 test_227() {
10306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10307         dd if=`which date` of=$MOUNT/date bs=1k count=1
10308         chmod +x $MOUNT/date
10309
10310         $MOUNT/date > /dev/null
10311         ldd $MOUNT/date > /dev/null
10312         rm -f $MOUNT/date
10313 }
10314 run_test 227 "running truncated executable does not cause OOM"
10315
10316 # LU-1512 try to reuse idle OI blocks
10317 test_228a() {
10318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10319         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10320                 skip "non-ldiskfs backend" && return
10321
10322         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10323         local myDIR=$DIR/$tdir
10324
10325         mkdir -p $myDIR
10326         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10327         $LCTL set_param fail_loc=0x80001002
10328         createmany -o $myDIR/t- 10000
10329         $LCTL set_param fail_loc=0
10330         # The guard is current the largest FID holder
10331         touch $myDIR/guard
10332         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10333                     tr -d '[')
10334         local IDX=$(($SEQ % 64))
10335
10336         do_facet $SINGLEMDS sync
10337         # Make sure journal flushed.
10338         sleep 6
10339         local blk1=$(do_facet $SINGLEMDS \
10340                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10341                      grep Blockcount | awk '{print $4}')
10342
10343         # Remove old files, some OI blocks will become idle.
10344         unlinkmany $myDIR/t- 10000
10345         # Create new files, idle OI blocks should be reused.
10346         createmany -o $myDIR/t- 2000
10347         do_facet $SINGLEMDS sync
10348         # Make sure journal flushed.
10349         sleep 6
10350         local blk2=$(do_facet $SINGLEMDS \
10351                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10352                      grep Blockcount | awk '{print $4}')
10353
10354         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10355 }
10356 run_test 228a "try to reuse idle OI blocks"
10357
10358 test_228b() {
10359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10360         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10361                 skip "non-ldiskfs backend" && return
10362
10363         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10364         local myDIR=$DIR/$tdir
10365
10366         mkdir -p $myDIR
10367         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10368         $LCTL set_param fail_loc=0x80001002
10369         createmany -o $myDIR/t- 10000
10370         $LCTL set_param fail_loc=0
10371         # The guard is current the largest FID holder
10372         touch $myDIR/guard
10373         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10374                     tr -d '[')
10375         local IDX=$(($SEQ % 64))
10376
10377         do_facet $SINGLEMDS sync
10378         # Make sure journal flushed.
10379         sleep 6
10380         local blk1=$(do_facet $SINGLEMDS \
10381                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10382                      grep Blockcount | awk '{print $4}')
10383
10384         # Remove old files, some OI blocks will become idle.
10385         unlinkmany $myDIR/t- 10000
10386
10387         # stop the MDT
10388         stop $SINGLEMDS || error "Fail to stop MDT."
10389         # remount the MDT
10390         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10391
10392         df $MOUNT || error "Fail to df."
10393         # Create new files, idle OI blocks should be reused.
10394         createmany -o $myDIR/t- 2000
10395         do_facet $SINGLEMDS sync
10396         # Make sure journal flushed.
10397         sleep 6
10398         local blk2=$(do_facet $SINGLEMDS \
10399                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10400                      grep Blockcount | awk '{print $4}')
10401
10402         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10403 }
10404 run_test 228b "idle OI blocks can be reused after MDT restart"
10405
10406 #LU-1881
10407 test_228c() {
10408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10409         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10410                 skip "non-ldiskfs backend" && return
10411
10412         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10413         local myDIR=$DIR/$tdir
10414
10415         mkdir -p $myDIR
10416         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10417         $LCTL set_param fail_loc=0x80001002
10418         # 20000 files can guarantee there are index nodes in the OI file
10419         createmany -o $myDIR/t- 20000
10420         $LCTL set_param fail_loc=0
10421         # The guard is current the largest FID holder
10422         touch $myDIR/guard
10423         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10424                     tr -d '[')
10425         local IDX=$(($SEQ % 64))
10426
10427         do_facet $SINGLEMDS sync
10428         # Make sure journal flushed.
10429         sleep 6
10430         local blk1=$(do_facet $SINGLEMDS \
10431                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10432                      grep Blockcount | awk '{print $4}')
10433
10434         # Remove old files, some OI blocks will become idle.
10435         unlinkmany $myDIR/t- 20000
10436         rm -f $myDIR/guard
10437         # The OI file should become empty now
10438
10439         # Create new files, idle OI blocks should be reused.
10440         createmany -o $myDIR/t- 2000
10441         do_facet $SINGLEMDS sync
10442         # Make sure journal flushed.
10443         sleep 6
10444         local blk2=$(do_facet $SINGLEMDS \
10445                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10446                      grep Blockcount | awk '{print $4}')
10447
10448         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10449 }
10450 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10451
10452 test_230a() {
10453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10454         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10455         local MDTIDX=1
10456
10457         mkdir -p $DIR/$tdir/test_230_local
10458         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10459         [ $mdt_idx -ne 0 ] &&
10460                 error "create local directory on wrong MDT $mdt_idx"
10461
10462         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10463                         error "create remote directory failed"
10464         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10465         [ $mdt_idx -ne $MDTIDX ] &&
10466                 error "create remote directory on wrong MDT $mdt_idx"
10467
10468         createmany -o $DIR/$tdir/test_230/t- 10 ||
10469                 error "create files on remote directory failed"
10470         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10471         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10472         rm -r $DIR/$tdir || error "unlink remote directory failed"
10473 }
10474 run_test 230a "Create remote directory and files under the remote directory"
10475
10476 test_230b() {
10477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10478         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10479         local MDTIDX=1
10480         local remote_dir=$DIR/$tdir/remote_dir
10481
10482         mkdir -p $DIR/$tdir
10483         $LFS mkdir -i $MDTIDX $remote_dir ||
10484                 error "create remote directory failed"
10485
10486         $LFS mkdir -i 0 $remote_dir/new_dir &&
10487                 error "nested remote directory create succeed!"
10488
10489         rm -r $DIR/$tdir || error "unlink remote directory failed"
10490 }
10491 run_test 230b "nested remote directory should be failed"
10492
10493 #
10494 # tests that do cleanup/setup should be run at the end
10495 #
10496
10497 test_900() {
10498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10499         local ls
10500         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
10501         $LCTL set_param fail_loc=0x903
10502         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
10503         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
10504                 echo "clear" > $ls
10505         done
10506         FAIL_ON_ERROR=true cleanup
10507         FAIL_ON_ERROR=true setup
10508 }
10509 run_test 900 "umount should not race with any mgc requeue thread"
10510
10511 complete $SECONDS
10512 check_and_cleanup_lustre
10513 if [ "$I_MOUNTED" != "yes" ]; then
10514         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
10515 fi
10516 exit_status