Whamcloud - gitweb
f16be85455aa06fde492aabe3e84639c00f124b8
[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 SRCDIR=$(cd $(dirname $0); echo $PWD)
21 export PATH=$PATH:/sbin
22
23 TMP=${TMP:-/tmp}
24
25 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
26 CREATETEST=${CREATETEST:-createtest}
27 LFS=${LFS:-lfs}
28 LFIND=${LFIND:-"$LFS find"}
29 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
30 LCTL=${LCTL:-lctl}
31 MCREATE=${MCREATE:-mcreate}
32 OPENFILE=${OPENFILE:-openfile}
33 OPENUNLINK=${OPENUNLINK:-openunlink}
34 export MULTIOP=${MULTIOP:-multiop}
35 READS=${READS:-"reads"}
36 MUNLINK=${MUNLINK:-munlink}
37 SOCKETSERVER=${SOCKETSERVER:-socketserver}
38 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
39 MEMHOG=${MEMHOG:-memhog}
40 DIRECTIO=${DIRECTIO:-directio}
41 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
42 UMOUNT=${UMOUNT:-"umount -d"}
43 STRIPES_PER_OBJ=-1
44 CHECK_GRANT=${CHECK_GRANT:-"yes"}
45 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
46 export PARALLEL=${PARALLEL:-"no"}
47
48 export NAME=${NAME:-local}
49
50 SAVE_PWD=$PWD
51
52 CLEANUP=${CLEANUP:-:}
53 SETUP=${SETUP:-:}
54 TRACE=${TRACE:-""}
55 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
56 . $LUSTRE/tests/test-framework.sh
57 init_test_env $@
58 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
59 init_logging
60
61 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 115 120g 124b"
62
63 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
64 # bug number for skipped test:        LU-2834 LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 18      34h     40      48a     180     184c"
66
67 FAIL_ON_ERROR=false
68
69 cleanup() {
70         echo -n "cln.."
71         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
72         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
73 }
74 setup() {
75         echo -n "mnt.."
76         load_modules
77         setupall || exit 10
78         echo "done"
79 }
80
81 check_kernel_version() {
82         WANT_VER=$1
83         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
84         case $GOT_VER in
85         patchless|patchless_client) return 0;;
86         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
87         esac
88         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
89         return 1
90 }
91
92 if [ "$ONLY" == "cleanup" ]; then
93        sh llmountcleanup.sh
94        exit 0
95 fi
96
97 check_and_setup_lustre
98
99 DIR=${DIR:-$MOUNT}
100 assert_DIR
101
102 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
103     awk '{gsub(/_UUID/,""); print $1}' | head -1)
104 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
105 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
106 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
107 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
108 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
109 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
110
111 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
112 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
113 rm -rf $DIR/[Rdfs][0-9]*
114
115 # $RUNAS_ID may get set incorrectly somewhere else
116 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
117
118 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
119
120 build_test_filter
121
122 if [ "${ONLY}" = "MOUNT" ] ; then
123         echo "Lustre is up, please go on"
124         exit
125 fi
126
127 echo "preparing for tests involving mounts"
128 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
129 touch $EXT2_DEV
130 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
131 echo # add a newline after mke2fs.
132
133 umask 077
134
135 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
136 lctl set_param debug=-1 2> /dev/null || true
137 test_0() {
138         touch $DIR/$tfile
139         $CHECKSTAT -t file $DIR/$tfile || error
140         rm $DIR/$tfile
141         $CHECKSTAT -a $DIR/$tfile || error
142 }
143 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
144
145 test_0b() {
146         chmod 0755 $DIR || error
147         $CHECKSTAT -p 0755 $DIR || error
148 }
149 run_test 0b "chmod 0755 $DIR ============================="
150
151 test_0c() {
152     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
153     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
154 }
155 run_test 0c "check import proc ============================="
156
157 test_1a() {
158         test_mkdir -p $DIR/$tdir
159         test_mkdir -p $DIR/$tdir/d2
160         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
161         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
162 }
163 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
164
165 test_1b() {
166         rmdir $DIR/$tdir/d2
167         rmdir $DIR/$tdir
168         $CHECKSTAT -a $DIR/$tdir || error
169 }
170 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
171
172 test_2a() {
173         test_mkdir $DIR/$tdir
174         touch $DIR/$tdir/$tfile
175         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
176 }
177 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
178
179 test_2b() {
180         rm -r $DIR/$tdir
181         $CHECKSTAT -a $DIR/$tdir || error
182 }
183 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
184
185 test_3a() {
186         test_mkdir -p $DIR/$tdir
187         $CHECKSTAT -t dir $DIR/$tdir || error
188 }
189 run_test 3a "mkdir .../d3 ======================================"
190
191 test_3b() {
192         if [ ! -d $DIR/$tdir ]; then
193                 mkdir $DIR/$tdir
194         fi
195         touch $DIR/$tdir/$tfile
196         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
197 }
198 run_test 3b "touch .../d3/f ===================================="
199
200 test_3c() {
201         rm -r $DIR/$tdir
202         $CHECKSTAT -a $DIR/$tdir || error
203 }
204 run_test 3c "rm -r .../d3 ======================================"
205
206 test_4a() {
207         test_mkdir -p $DIR/$tdir
208         $CHECKSTAT -t dir $DIR/$tdir || error
209 }
210 run_test 4a "mkdir .../d4 ======================================"
211
212 test_4b() {
213         if [ ! -d $DIR/$tdir ]; then
214                 test_mkdir $DIR/$tdir
215         fi
216         test_mkdir $DIR/$tdir/d2
217         mkdir $DIR/$tdir/d2
218         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
219 }
220 run_test 4b "mkdir .../d4/d2 ==================================="
221
222 test_5() {
223         test_mkdir $DIR/$tdir
224         test_mkdir $DIR/$tdir/d2
225         chmod 0707 $DIR/$tdir/d2
226         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error
227 }
228 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
229
230 test_6a() {
231         touch $DIR/$tfile
232         chmod 0666 $DIR/$tfile || error
233         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
234 }
235 run_test 6a "touch .../f6a; chmod .../f6a ======================"
236
237 test_6b() {
238         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
239         if [ ! -f $DIR/$tfile ]; then
240                 touch $DIR/$tfile
241                 chmod 0666 $DIR/$tfile
242         fi
243         $RUNAS chmod 0444 $DIR/$tfile && error
244         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
245 }
246 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
247
248 test_6c() {
249         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
250         touch $DIR/$tfile
251         chown $RUNAS_ID $DIR/$tfile || error
252         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
253 }
254 run_test 6c "touch .../f6c; chown .../f6c ======================"
255
256 test_6d() {
257         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
258         if [ ! -f $DIR/$tfile ]; then
259                 touch $DIR/$tfile
260                 chown $RUNAS_ID $DIR/$tfile
261         fi
262         $RUNAS chown $UID $DIR/$tfile && error
263         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
264 }
265 run_test 6d "$RUNAS chown .../f6c (should return error) =="
266
267 test_6e() {
268         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
269         touch $DIR/$tfile
270         chgrp $RUNAS_ID $DIR/$tfile || error
271         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
272 }
273 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
274
275 test_6f() {
276         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
277         if [ ! -f $DIR/$tfile ]; then
278                 touch $DIR/$tfile
279                 chgrp $RUNAS_ID $DIR/$tfile
280         fi
281         $RUNAS chgrp $UID $DIR/$tfile && error
282         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
283 }
284 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
285
286 test_6g() {
287         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
288         test_mkdir $DIR/$tdir || error
289         chmod 777 $DIR/$tdir || error
290         $RUNAS mkdir $DIR/$tdir/d || error
291         chmod g+s $DIR/$tdir/d || error
292         test_mkdir $DIR/$tdir/d/subdir
293         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir || error
294 }
295 run_test 6g "Is new dir in sgid dir inheriting group?"
296
297 test_6h() { # bug 7331
298         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
299         touch $DIR/$tfile || error "touch failed"
300         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
301         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
302                 error "chown worked"
303         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile || error
304 }
305 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
306
307 test_7a() {
308         test_mkdir $DIR/$tdir
309         $MCREATE $DIR/$tdir/$tfile
310         chmod 0666 $DIR/$tdir/$tfile
311         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
312 }
313 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
314
315 test_7b() {
316         if [ ! -d $DIR/$tdir ]; then
317                 mkdir $DIR/$tdir
318         fi
319         $MCREATE $DIR/$tdir/$tfile
320         echo -n foo > $DIR/$tdir/$tfile
321         [ "`cat $DIR/$tdir/$tfile`" = "foo" ] || error
322         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error
323 }
324 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
325
326 test_8() {
327         test_mkdir $DIR/$tdir
328         touch $DIR/$tdir/$tfile
329         chmod 0666 $DIR/$tdir/$tfile
330         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
331 }
332 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
333
334 test_9() {
335         test_mkdir $DIR/$tdir
336         test_mkdir $DIR/$tdir/d2
337         test_mkdir $DIR/$tdir/d2/d3
338         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error
339 }
340 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
341
342 test_10() {
343         test_mkdir $DIR/$tdir
344         test_mkdir $DIR/$tdir/d2
345         touch $DIR/$tdir/d2/$tfile
346         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile || error
347 }
348 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
349
350 test_11() {
351         test_mkdir $DIR/$tdir
352         test_mkdir $DIR/$tdir/d2
353         chmod 0666 $DIR/$tdir/d2
354         chmod 0705 $DIR/$tdir/d2
355         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 || error
356 }
357 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
358
359 test_12() {
360         test_mkdir $DIR/$tdir
361         touch $DIR/$tdir/$tfile
362         chmod 0666 $DIR/$tdir/$tfile
363         chmod 0654 $DIR/$tdir/$tfile
364         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile || error
365 }
366 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
367
368 test_13() {
369         test_mkdir $DIR/$tdir
370         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
371         >  $DIR/$tdir/$tfile
372         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile || error
373 }
374 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
375
376 test_14() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error
381 }
382 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
383
384 test_15() {
385         test_mkdir $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
388         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 || error
389 }
390 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
391
392 test_16() {
393         test_mkdir $DIR/$tdir
394         touch $DIR/$tdir/$tfile
395         rm -rf $DIR/$tdir/$tfile
396         $CHECKSTAT -a $DIR/$tdir/$tfile || error
397 }
398 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
399
400 test_17a() {
401         test_mkdir -p $DIR/$tdir
402         touch $DIR/$tdir/$tfile
403         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
404         ls -l $DIR/$tdir
405         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist || error
406         $CHECKSTAT -f -t f $DIR/$tdir/l-exist || error
407         rm -f $DIR/$tdir/l-exist
408         $CHECKSTAT -a $DIR/$tdir/l-exist || error
409 }
410 run_test 17a "symlinks: create, remove (real) =================="
411
412 test_17b() {
413         test_mkdir -p $DIR/$tdir
414         ln -s no-such-file $DIR/$tdir/l-dangle
415         ls -l $DIR/$tdir
416         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle || error
417         $CHECKSTAT -fa $DIR/$tdir/l-dangle || error
418         rm -f $DIR/$tdir/l-dangle
419         $CHECKSTAT -a $DIR/$tdir/l-dangle || error
420 }
421 run_test 17b "symlinks: create, remove (dangling) =============="
422
423 test_17c() { # bug 3440 - don't save failed open RPC for replay
424         test_mkdir -p $DIR/$tdir
425         ln -s foo $DIR/$tdir/$tfile
426         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
427 }
428 run_test 17c "symlinks: open dangling (should return error) ===="
429
430 test_17d() {
431         test_mkdir -p $DIR/$tdir
432         ln -s foo $DIR/$tdir/$tfile
433         touch $DIR/$tdir/$tfile || error "creating to new symlink"
434 }
435 run_test 17d "symlinks: create dangling ========================"
436
437 test_17e() {
438         test_mkdir -p $DIR/$tdir
439         local foo=$DIR/$tdir/$tfile
440         ln -s $foo $foo || error "create symlink failed"
441         ls -l $foo || error "ls -l failed"
442         ls $foo && error "ls not failed" || true
443 }
444 run_test 17e "symlinks: create recursive symlink (should return error) ===="
445
446 test_17f() {
447         test_mkdir -p $DIR/d17f
448         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
453         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
454         ls -l  $DIR/d17f
455 }
456 run_test 17f "symlinks: long and very long symlink name ========================"
457
458 # str_repeat(S, N) generate a string that is string S repeated N times
459 str_repeat() {
460         local s=$1
461         local n=$2
462         local ret=''
463         while [ $((n -= 1)) -ge 0 ]; do
464                 ret=$ret$s
465         done
466         echo $ret
467 }
468
469 # Long symlinks and LU-2241
470 test_17g() {
471         test_mkdir -p $DIR/$tdir
472         local TESTS="59 60 61 4094 4095"
473
474         for i in $TESTS; do
475                 local SYMNAME=$(str_repeat 'x' $i)
476                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
477                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
478         done
479 }
480 run_test 17g "symlinks: really long symlink name and inode boundaries"
481
482 test_17h() { #bug 17378
483         remote_mds_nodsh && skip "remote MDS with nodsh" && return
484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
485         local mdt_idx
486         test_mkdir -p $DIR/$tdir
487         if [ $MDSCOUNT -gt 1 ]; then
488                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
489         else
490                 mdt_idx=0
491         fi
492         $SETSTRIPE -c -1 $DIR/$tdir
493 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
494         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
495         touch $DIR/$tdir/$tfile || true
496 }
497 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
498
499 test_17i() { #bug 20018
500         remote_mds_nodsh && skip "remote MDS with nodsh" && return
501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
502         test_mkdir -p $DIR/$tdir
503         local foo=$DIR/$tdir/$tfile
504         local mdt_idx
505         if [ $MDSCOUNT -gt 1 ]; then
506                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
507         else
508                 mdt_idx=0
509         fi
510         ln -s $foo $foo || error "create symlink failed"
511 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
512         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
513         ls -l $foo && error "error not detected"
514         return 0
515 }
516 run_test 17i "don't panic on short symlink"
517
518 test_17k() { #bug 22301
519         rsync --help | grep -q xattr ||
520                 skip_env "$(rsync --version| head -1) does not support xattrs"
521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
522         test_mkdir -p $DIR/$tdir
523         test_mkdir -p $DIR/$tdir.new
524         touch $DIR/$tdir/$tfile
525         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
526         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
527                 error "rsync failed with xattrs enabled"
528 }
529 run_test 17k "symlinks: rsync with xattrs enabled ========================="
530
531 test_17l() { # LU-279
532         mkdir -p $DIR/$tdir
533         touch $DIR/$tdir/$tfile
534         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
535         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
536                 # -h to not follow symlinks. -m '' to list all the xattrs.
537                 # grep to remove first line: '# file: $path'.
538                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
539                 do
540                         lgetxattr_size_check $path $xattr ||
541                                 error "lgetxattr_size_check $path $xattr failed"
542                 done
543         done
544 }
545 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
546
547 # LU-1540
548 test_17m() {
549         local short_sym="0123456789"
550         local WDIR=$DIR/${tdir}m
551         local mds_index
552         local devname
553         local cmd
554         local i
555         local rc=0
556
557         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
558         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
559                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
560
561         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
562                 skip "only for ldiskfs MDT" && return 0
563
564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
565
566         mkdir -p $WDIR
567         long_sym=$short_sym
568         # create a long symlink file
569         for ((i = 0; i < 4; ++i)); do
570                 long_sym=${long_sym}${long_sym}
571         done
572
573         echo "create 512 short and long symlink files under $WDIR"
574         for ((i = 0; i < 256; ++i)); do
575                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
576                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
577         done
578
579         echo "erase them"
580         rm -f $WDIR/*
581         sync
582         wait_delete_completed
583
584         echo "recreate the 512 symlink files with a shorter string"
585         for ((i = 0; i < 512; ++i)); do
586                 # rewrite the symlink file with a shorter string
587                 ln -sf ${long_sym} $WDIR/long-$i
588                 ln -sf ${short_sym} $WDIR/short-$i
589         done
590
591         mds_index=$($LFS getstripe -M $WDIR)
592         mds_index=$((mds_index+1))
593         devname=$(mdsdevname $mds_index)
594         cmd="$E2FSCK -fnvd $devname"
595
596         echo "stop and checking mds${mds_index}: $cmd"
597         # e2fsck should not return error
598         stop mds${mds_index} -f
599         do_facet mds${mds_index} $cmd || rc=$?
600
601         start mds${mds_index} $devname $MDS_MOUNT_OPTS
602         df $MOUNT > /dev/null 2>&1
603         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
604                 "short/long symlink MDT: rc=$rc"
605         return $rc
606 }
607 run_test 17m "run e2fsck against MDT which contains short/long symlink"
608
609 check_fs_consistency_17n() {
610         local mdt_index
611         local devname
612         local cmd
613         local rc=0
614
615         for mdt_index in $(seq 1 $MDSCOUNT); do
616                 devname=$(mdsdevname $mdt_index)
617                 cmd="$E2FSCK -fnvd $devname"
618
619                 echo "stop and checking mds${mdt_index}: $cmd"
620                 # e2fsck should not return error
621                 stop mds${mdt_index}
622                 do_facet mds${mdt_index} $cmd || rc=$?
623
624                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
625                 df $MOUNT > /dev/null 2>&1
626                 [ $rc -ne 0 ] && break
627         done
628         return $rc
629 }
630
631 test_17n() {
632         local i
633
634         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
635         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
636                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
637
638         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
639                 skip "only for ldiskfs MDT" && return 0
640
641         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
642
643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
644
645         mkdir -p $DIR/$tdir
646         for ((i=0; i<10; i++)); do
647                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
648                         error "create remote dir error $i"
649                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
650                         error "create files under remote dir failed $i"
651         done
652
653         check_fs_consistency_17n || error "e2fsck report error"
654
655         for ((i=0;i<10;i++)); do
656                 rm -rf $DIR/$tdir/remote_dir_${i} ||
657                         error "destroy remote dir error $i"
658         done
659
660         check_fs_consistency_17n || error "e2fsck report error"
661 }
662 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
663
664 test_18() {
665         touch $DIR/f
666         ls $DIR || error
667 }
668 run_test 18 "touch .../f ; ls ... =============================="
669
670 test_19a() {
671         touch $DIR/f19
672         ls -l $DIR
673         rm $DIR/f19
674         $CHECKSTAT -a $DIR/f19 || error
675 }
676 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
677
678 test_19b() {
679         ls -l $DIR/f19 && error || true
680 }
681 run_test 19b "ls -l .../f19 (should return error) =============="
682
683 test_19c() {
684         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
685         $RUNAS touch $DIR/f19 && error || true
686 }
687 run_test 19c "$RUNAS touch .../f19 (should return error) =="
688
689 test_19d() {
690         cat $DIR/f19 && error || true
691 }
692 run_test 19d "cat .../f19 (should return error) =============="
693
694 test_20() {
695         touch $DIR/f
696         rm $DIR/f
697         log "1 done"
698         touch $DIR/f
699         rm $DIR/f
700         log "2 done"
701         touch $DIR/f
702         rm $DIR/f
703         log "3 done"
704         $CHECKSTAT -a $DIR/f || error
705 }
706 run_test 20 "touch .../f ; ls -l ... ==========================="
707
708 test_21() {
709         test_mkdir $DIR/d21
710         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
711         ln -s dangle $DIR/d21/link
712         echo foo >> $DIR/d21/link
713         cat $DIR/d21/dangle
714         $CHECKSTAT -t link $DIR/d21/link || error
715         $CHECKSTAT -f -t file $DIR/d21/link || error
716 }
717 run_test 21 "write to dangling link ============================"
718
719 test_22() {
720         WDIR=$DIR/$tdir
721         test_mkdir -p $DIR/$tdir
722         chown $RUNAS_ID:$RUNAS_GID $WDIR
723         (cd $WDIR || error "cd $WDIR failed";
724         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
725         $RUNAS tar xf -)
726         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
727         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
728         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
729 }
730 run_test 22 "unpack tar archive as non-root user ==============="
731
732 # was test_23
733 test_23a() {
734         test_mkdir -p $DIR/$tdir
735         local file=$DIR/$tdir/$tfile
736
737         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
738         openfile -f O_CREAT:O_EXCL $file &&
739                 error "$file recreate succeeded" || true
740 }
741 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
742
743 test_23b() { # bug 18988
744         test_mkdir -p $DIR/$tdir
745         local file=$DIR/$tdir/$tfile
746
747         rm -f $file
748         echo foo > $file || error "write filed"
749         echo bar >> $file || error "append filed"
750         $CHECKSTAT -s 8 $file || error "wrong size"
751         rm $file
752 }
753 run_test 23b "O_APPEND check =========================="
754
755 test_24a() {
756         echo '== rename sanity =============================================='
757         echo '-- same directory rename'
758         test_mkdir $DIR/R1
759         touch $DIR/R1/f
760         mv $DIR/R1/f $DIR/R1/g
761         $CHECKSTAT -t file $DIR/R1/g || error
762 }
763 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
764
765 test_24b() {
766         test_mkdir $DIR/R2
767         touch $DIR/R2/{f,g}
768         mv $DIR/R2/f $DIR/R2/g
769         $CHECKSTAT -a $DIR/R2/f || error
770         $CHECKSTAT -t file $DIR/R2/g || error
771 }
772 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
773
774 test_24c() {
775         test_mkdir $DIR/R3
776         test_mkdir $DIR/R3/f
777         mv $DIR/R3/f $DIR/R3/g
778         $CHECKSTAT -a $DIR/R3/f || error
779         $CHECKSTAT -t dir $DIR/R3/g || error
780 }
781 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
782
783 test_24d() {
784         test_mkdir $DIR/R4
785         test_mkdir $DIR/R4/f
786         test_mkdir $DIR/R4/g
787         mrename $DIR/R4/f $DIR/R4/g
788         $CHECKSTAT -a $DIR/R4/f || error
789         $CHECKSTAT -t dir $DIR/R4/g || error
790 }
791 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
792
793 test_24e() {
794         echo '-- cross directory renames --'
795         test_mkdir $DIR/R5a
796         test_mkdir $DIR/R5b
797         touch $DIR/R5a/f
798         mv $DIR/R5a/f $DIR/R5b/g
799         $CHECKSTAT -a $DIR/R5a/f || error
800         $CHECKSTAT -t file $DIR/R5b/g || error
801 }
802 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
803
804 test_24f() {
805         test_mkdir $DIR/R6a
806         test_mkdir $DIR/R6b
807         touch $DIR/R6a/f $DIR/R6b/g
808         mv $DIR/R6a/f $DIR/R6b/g
809         $CHECKSTAT -a $DIR/R6a/f || error
810         $CHECKSTAT -t file $DIR/R6b/g || error
811 }
812 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
813
814 test_24g() {
815         test_mkdir $DIR/R7a
816         test_mkdir $DIR/R7b
817         test_mkdir $DIR/R7a/d
818         mv $DIR/R7a/d $DIR/R7b/e
819         $CHECKSTAT -a $DIR/R7a/d || error
820         $CHECKSTAT -t dir $DIR/R7b/e || error
821 }
822 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
823
824 test_24h() {
825         test_mkdir $DIR/R8a
826         test_mkdir $DIR/R8b
827         test_mkdir $DIR/R8a/d
828         test_mkdir $DIR/R8b/e
829         mrename $DIR/R8a/d $DIR/R8b/e
830         $CHECKSTAT -a $DIR/R8a/d || error
831         $CHECKSTAT -t dir $DIR/R8b/e || error
832 }
833 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
834
835 test_24i() {
836         echo "-- rename error cases"
837         test_mkdir $DIR/R9
838         test_mkdir $DIR/R9/a
839         touch $DIR/R9/f
840         mrename $DIR/R9/f $DIR/R9/a
841         $CHECKSTAT -t file $DIR/R9/f || error
842         $CHECKSTAT -t dir  $DIR/R9/a || error
843         $CHECKSTAT -a $DIR/R9/a/f || error
844 }
845 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
846
847 test_24j() {
848         test_mkdir $DIR/R10
849         mrename $DIR/R10/f $DIR/R10/g
850         $CHECKSTAT -t dir $DIR/R10 || error
851         $CHECKSTAT -a $DIR/R10/f || error
852         $CHECKSTAT -a $DIR/R10/g || error
853 }
854 run_test 24j "source does not exist ============================"
855
856 test_24k() {
857         test_mkdir $DIR/R11a
858         test_mkdir $DIR/R11a/d
859         touch $DIR/R11a/f
860         mv $DIR/R11a/f $DIR/R11a/d
861         $CHECKSTAT -a $DIR/R11a/f || error
862         $CHECKSTAT -t file $DIR/R11a/d/f || error
863 }
864 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
865
866 # bug 2429 - rename foo foo foo creates invalid file
867 test_24l() {
868         f="$DIR/f24l"
869         $MULTIOP $f OcNs || error
870 }
871 run_test 24l "Renaming a file to itself ========================"
872
873 test_24m() {
874         f="$DIR/f24m"
875         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
876         # on ext3 this does not remove either the source or target files
877         # though the "expected" operation would be to remove the source
878         $CHECKSTAT -t file ${f} || error "${f} missing"
879         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
880 }
881 run_test 24m "Renaming a file to a hard link to itself ========="
882
883 test_24n() {
884     f="$DIR/f24n"
885     # this stats the old file after it was renamed, so it should fail
886     touch ${f}
887     $CHECKSTAT ${f}
888     mv ${f} ${f}.rename
889     $CHECKSTAT ${f}.rename
890     $CHECKSTAT -a ${f}
891 }
892 run_test 24n "Statting the old file after renaming (Posix rename 2)"
893
894 test_24o() {
895         check_kernel_version 37 || return 0
896         test_mkdir -p $DIR/d24o
897         rename_many -s random -v -n 10 $DIR/d24o
898 }
899 run_test 24o "rename of files during htree split ==============="
900
901 test_24p() {
902         test_mkdir $DIR/R12a
903         test_mkdir $DIR/R12b
904         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
905         mrename $DIR/R12a $DIR/R12b
906         $CHECKSTAT -a $DIR/R12a || error
907         $CHECKSTAT -t dir $DIR/R12b || error
908         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
909         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
910 }
911 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
912
913 cleanup_multiop_pause() {
914         trap 0
915         kill -USR1 $MULTIPID
916 }
917
918 test_24q() {
919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
920         test_mkdir $DIR/R13a
921         test_mkdir $DIR/R13b
922         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
923         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
924         MULTIPID=$!
925
926         trap cleanup_multiop_pause EXIT
927         mrename $DIR/R13a $DIR/R13b
928         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
929         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
930         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
931         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
932         cleanup_multiop_pause
933         wait $MULTIPID || error "multiop close failed"
934 }
935 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
936
937 test_24r() { #bug 3789
938         test_mkdir $DIR/R14a
939         test_mkdir $DIR/R14a/b
940         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
941         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
942         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
943 }
944 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
945
946 test_24s() {
947         test_mkdir $DIR/R15a
948         test_mkdir $DIR/R15a/b
949         test_mkdir $DIR/R15a/b/c
950         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
951         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
952         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
953 }
954 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
955 test_24t() {
956         test_mkdir $DIR/R16a
957         test_mkdir $DIR/R16a/b
958         test_mkdir $DIR/R16a/b/c
959         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
960         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
961         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
962 }
963 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
964
965 test_24u() { # bug12192
966         rm -rf $DIR/$tfile
967         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
968         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
969 }
970 run_test 24u "create stripe file"
971
972 page_size() {
973         getconf PAGE_SIZE
974 }
975
976 simple_cleanup_common() {
977         trap 0
978         rm -rf $DIR/$tdir
979         wait_delete_completed
980 }
981
982 max_pages_per_rpc() {
983         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
984 }
985
986 test_24v() {
987         local NRFILES=100000
988         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
989         [ $FREE_INODES -lt $NRFILES ] && \
990                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
991                 return
992
993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
994         trap simple_cleanup_common EXIT
995
996         mkdir -p $DIR/$tdir
997         createmany -m $DIR/$tdir/$tfile $NRFILES
998
999         cancel_lru_locks mdc
1000         lctl set_param mdc.*.stats clear
1001
1002         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1003
1004         # LU-5 large readdir
1005         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1006         #               8 bytes for name(filename is mostly 5 in this test) +
1007         #               8 bytes for luda_type
1008         # take into account of overhead in lu_dirpage header and end mark in
1009         # each page, plus one in RPC_NUM calculation.
1010         DIRENT_SIZE=48
1011         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1012         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1013         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1014                                 awk '/^mds_readpage/ {print $2}')
1015         [ $mds_readpage -gt $RPC_NUM ] && \
1016                 error "large readdir doesn't take effect"
1017
1018         simple_cleanup_common
1019 }
1020 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1021
1022 test_24w() { # bug21506
1023         SZ1=234852
1024         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1025         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1026         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1027         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1028         [ "$SZ1" = "$SZ2" ] || \
1029                 error "Error reading at the end of the file $tfile"
1030 }
1031 run_test 24w "Reading a file larger than 4Gb"
1032
1033 test_24x() {
1034         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1036         local MDTIDX=1
1037         local remote_dir=$DIR/$tdir/remote_dir
1038
1039         mkdir -p $DIR/$tdir
1040         $LFS mkdir -i $MDTIDX $remote_dir ||
1041                 error "create remote directory failed"
1042
1043         mkdir -p $DIR/$tdir/src_dir
1044         touch $DIR/$tdir/src_file
1045         mkdir -p $remote_dir/tgt_dir
1046         touch $remote_dir/tgt_file
1047
1048         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1049                 error "rename dir cross MDT works!"
1050
1051         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1052                 error "rename file cross MDT works!"
1053
1054         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1055                 error "ln file cross MDT should not work!"
1056
1057         rm -rf $DIR/$tdir || error "Can not delete directories"
1058 }
1059 run_test 24x "cross rename/link should be failed"
1060
1061 test_24y() {
1062         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1063         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1064         local MDTIDX=1
1065         local remote_dir=$DIR/$tdir/remote_dir
1066
1067         mkdir -p $DIR/$tdir
1068         $LFS mkdir -i $MDTIDX $remote_dir ||
1069                    error "create remote directory failed"
1070
1071         mkdir -p $remote_dir/src_dir
1072         touch $remote_dir/src_file
1073         mkdir -p $remote_dir/tgt_dir
1074         touch $remote_dir/tgt_file
1075
1076         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1077                 error "rename subdir in the same remote dir failed!"
1078
1079         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1080                 error "rename files in the same remote dir failed!"
1081
1082         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1083                 error "link files in the same remote dir failed!"
1084
1085         rm -rf $DIR/$tdir || error "Can not delete directories"
1086 }
1087 run_test 24y "rename/link on the same dir should succeed"
1088
1089 test_24z() {
1090         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1091         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1092         local MDTIDX=1
1093         local remote_src=$DIR/$tdir/remote_dir
1094         local remote_tgt=$DIR/$tdir/remote_tgt
1095
1096         mkdir -p $DIR/$tdir
1097         $LFS mkdir -i $MDTIDX $remote_src ||
1098                    error "create remote directory failed"
1099
1100         $LFS mkdir -i $MDTIDX $remote_tgt ||
1101                    error "create remote directory failed"
1102
1103         mrename $remote_src $remote_tgt &&
1104                 error "rename remote dirs should not work!"
1105
1106         rm -rf $DIR/$tdir || error "Can not delete directories"
1107 }
1108 run_test 24z "rename one remote dir to another remote dir should fail"
1109
1110 test_25a() {
1111         echo '== symlink sanity ============================================='
1112
1113         test_mkdir $DIR/d25
1114         ln -s d25 $DIR/s25
1115         touch $DIR/s25/foo || error
1116 }
1117 run_test 25a "create file in symlinked directory ==============="
1118
1119 test_25b() {
1120         [ ! -d $DIR/d25 ] && test_25a
1121         $CHECKSTAT -t file $DIR/s25/foo || error
1122 }
1123 run_test 25b "lookup file in symlinked directory ==============="
1124
1125 test_26a() {
1126         test_mkdir $DIR/d26
1127         test_mkdir $DIR/d26/d26-2
1128         ln -s d26/d26-2 $DIR/s26
1129         touch $DIR/s26/foo || error
1130 }
1131 run_test 26a "multiple component symlink ======================="
1132
1133 test_26b() {
1134         test_mkdir -p $DIR/d26b/d26-2
1135         ln -s d26b/d26-2/foo $DIR/s26-2
1136         touch $DIR/s26-2 || error
1137 }
1138 run_test 26b "multiple component symlink at end of lookup ======"
1139
1140 test_26c() {
1141         test_mkdir $DIR/d26.2
1142         touch $DIR/d26.2/foo
1143         ln -s d26.2 $DIR/s26.2-1
1144         ln -s s26.2-1 $DIR/s26.2-2
1145         ln -s s26.2-2 $DIR/s26.2-3
1146         chmod 0666 $DIR/s26.2-3/foo
1147 }
1148 run_test 26c "chain of symlinks ================================"
1149
1150 # recursive symlinks (bug 439)
1151 test_26d() {
1152         ln -s d26-3/foo $DIR/d26-3
1153 }
1154 run_test 26d "create multiple component recursive symlink ======"
1155
1156 test_26e() {
1157         [ ! -h $DIR/d26-3 ] && test_26d
1158         rm $DIR/d26-3
1159 }
1160 run_test 26e "unlink multiple component recursive symlink ======"
1161
1162 # recursive symlinks (bug 7022)
1163 test_26f() {
1164         test_mkdir -p $DIR/$tdir
1165         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1166         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1167         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1168         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1169         cd $tfile                || error "cd $tfile failed"
1170         ln -s .. dotdot          || error "ln dotdot failed"
1171         ln -s dotdot/lndir lndir || error "ln lndir failed"
1172         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1173         output=`ls $tfile/$tfile/lndir/bar1`
1174         [ "$output" = bar1 ] && error "unexpected output"
1175         rm -r $tfile             || error "rm $tfile failed"
1176         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1177 }
1178 run_test 26f "rm -r of a directory which has recursive symlink ="
1179
1180 test_27a() {
1181         echo '== stripe sanity =============================================='
1182         test_mkdir -p $DIR/d27 || error "mkdir failed"
1183         $GETSTRIPE $DIR/d27
1184         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1185         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1186         pass
1187         log "== test_27a: write to one stripe file ========================="
1188         cp /etc/hosts $DIR/d27/f0 || error
1189 }
1190 run_test 27a "one stripe file =================================="
1191
1192 test_27b() {
1193         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1194         test_mkdir -p $DIR/d27
1195         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1196         $GETSTRIPE -c $DIR/d27/f01
1197         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1198                 error "two-stripe file doesn't have two stripes"
1199 }
1200 run_test 27b "create two stripe file"
1201
1202 test_27c() {
1203         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1204
1205         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1206 }
1207 run_test 27c "write to two stripe file"
1208
1209 test_27d() {
1210         test_mkdir -p $DIR/d27
1211         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1212         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1213         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1214 }
1215 run_test 27d "create file with default settings ================"
1216
1217 test_27e() {
1218         test_mkdir -p $DIR/d27
1219         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1220         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1221         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1222 }
1223 run_test 27e "setstripe existing file (should return error) ======"
1224
1225 test_27f() {
1226         test_mkdir -p $DIR/d27
1227         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1228         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1229         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1230 }
1231 run_test 27f "setstripe with bad stripe size (should return error)"
1232
1233 test_27g() {
1234         test_mkdir -p $DIR/d27
1235         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1236         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1237                 error "$DIR/d27/fnone has object"
1238 }
1239 run_test 27g "$GETSTRIPE with no objects"
1240
1241 test_27i() {
1242         touch $DIR/d27/fsome || error "touch failed"
1243         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1244 }
1245 run_test 27i "$GETSTRIPE with some objects"
1246
1247 test_27j() {
1248         test_mkdir -p $DIR/d27
1249         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1250 }
1251 run_test 27j "setstripe with bad stripe offset (should return error)"
1252
1253 test_27k() { # bug 2844
1254         test_mkdir -p $DIR/d27
1255         FILE=$DIR/d27/f27k
1256         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1257         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1258         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1259         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1260         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1261         dd if=/dev/zero of=$FILE bs=4k count=1
1262         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1263         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1264 }
1265 run_test 27k "limit i_blksize for broken user apps ============="
1266
1267 test_27l() {
1268         test_mkdir -p $DIR/d27
1269         mcreate $DIR/f27l || error "creating file"
1270         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1271                 error "setstripe should have failed" || true
1272 }
1273 run_test 27l "check setstripe permissions (should return error)"
1274
1275 test_27m() {
1276         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1277                 return
1278         if [ $ORIGFREE -gt $MAXFREE ]; then
1279                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1280                 return
1281         fi
1282         trap simple_cleanup_common EXIT
1283         test_mkdir -p $DIR/$tdir
1284         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1285         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1286                 error "dd should fill OST0"
1287         i=2
1288         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1289                 i=`expr $i + 1`
1290                 [ $i -gt 256 ] && break
1291         done
1292         i=`expr $i + 1`
1293         touch $DIR/$tdir/f27m_$i
1294         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1295                 error "OST0 was full but new created file still use it"
1296         i=`expr $i + 1`
1297         touch $DIR/$tdir/f27m_$i
1298         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1299                 error "OST0 was full but new created file still use it"
1300         simple_cleanup_common
1301 }
1302 run_test 27m "create file while OST0 was full =================="
1303
1304 sleep_maxage() {
1305         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1306         sleep $DELAY
1307 }
1308
1309 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1310 # if the OST isn't full anymore.
1311 reset_enospc() {
1312         local OSTIDX=${1:-""}
1313
1314         local list=$(comma_list $(osts_nodes))
1315         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1316
1317         do_nodes $list lctl set_param fail_loc=0
1318         sync    # initiate all OST_DESTROYs from MDS to OST
1319         sleep_maxage
1320 }
1321
1322 exhaust_precreations() {
1323         local OSTIDX=$1
1324         local FAILLOC=$2
1325         local FAILIDX=${3:-$OSTIDX}
1326
1327         test_mkdir -p $DIR/$tdir
1328         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1329         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1330
1331         local OST=$(ostname_from_index $OSTIDX)
1332         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1333                           sed -e 's/_UUID$//;s/^.*-//')
1334
1335         # on the mdt's osc
1336         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1337         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1338         osc.$mdtosc_proc1.prealloc_last_id)
1339         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1340         osc.$mdtosc_proc1.prealloc_next_id)
1341
1342         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1343         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1344
1345         test_mkdir -p $DIR/$tdir/${OST}
1346         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1347 #define OBD_FAIL_OST_ENOSPC              0x215
1348         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1349         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1350         echo "Creating to objid $last_id on ost $OST..."
1351         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1352         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1353         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1354         sleep_maxage
1355 }
1356
1357 exhaust_all_precreations() {
1358         local i
1359         for (( i=0; i < OSTCOUNT; i++ )) ; do
1360                 exhaust_precreations $i $1 -1
1361         done
1362 }
1363
1364 test_27n() {
1365         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1367         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1368         remote_ost_nodsh && skip "remote OST with nodsh" && return
1369
1370         reset_enospc
1371         rm -f $DIR/$tdir/$tfile
1372         exhaust_precreations 0 0x80000215
1373         $SETSTRIPE -c -1 $DIR/$tdir
1374         touch $DIR/$tdir/$tfile || error
1375         $GETSTRIPE $DIR/$tdir/$tfile
1376         reset_enospc
1377 }
1378 run_test 27n "create file with some full OSTs =================="
1379
1380 test_27o() {
1381         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1383         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1384         remote_ost_nodsh && skip "remote OST with nodsh" && return
1385
1386         reset_enospc
1387         rm -f $DIR/$tdir/$tfile
1388         exhaust_all_precreations 0x215
1389
1390         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1391
1392         reset_enospc
1393         rm -rf $DIR/$tdir/*
1394 }
1395 run_test 27o "create file with all full OSTs (should error) ===="
1396
1397 test_27p() {
1398         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1400         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1401         remote_ost_nodsh && skip "remote OST with nodsh" && return
1402
1403         reset_enospc
1404         rm -f $DIR/$tdir/$tfile
1405         test_mkdir -p $DIR/$tdir
1406
1407         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1408         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1409         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1410
1411         exhaust_precreations 0 0x80000215
1412         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1413         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1414         $GETSTRIPE $DIR/$tdir/$tfile
1415
1416         reset_enospc
1417 }
1418 run_test 27p "append to a truncated file with some full OSTs ==="
1419
1420 test_27q() {
1421         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1423         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1424         remote_ost_nodsh && skip "remote OST with nodsh" && return
1425
1426         reset_enospc
1427         rm -f $DIR/$tdir/$tfile
1428
1429         test_mkdir -p $DIR/$tdir
1430         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1431         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1432         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1433
1434         exhaust_all_precreations 0x215
1435
1436         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1437         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1438
1439         reset_enospc
1440 }
1441 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1442
1443 test_27r() {
1444         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1446         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1447         remote_ost_nodsh && skip "remote OST with nodsh" && return
1448
1449         reset_enospc
1450         rm -f $DIR/$tdir/$tfile
1451         exhaust_precreations 0 0x80000215
1452
1453         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1454
1455         reset_enospc
1456 }
1457 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1458
1459 test_27s() { # bug 10725
1460         test_mkdir -p $DIR/$tdir
1461         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1462         local stripe_count=0
1463         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1464         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1465                 error "stripe width >= 2^32 succeeded" || true
1466
1467 }
1468 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1469
1470 test_27t() { # bug 10864
1471         WDIR=`pwd`
1472         WLFS=`which lfs`
1473         cd $DIR
1474         touch $tfile
1475         $WLFS getstripe $tfile
1476         cd $WDIR
1477 }
1478 run_test 27t "check that utils parse path correctly"
1479
1480 test_27u() { # bug 4900
1481         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1482         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1483
1484 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1485         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1486         test_mkdir -p $DIR/$tdir
1487         createmany -o $DIR/$tdir/t- 1000
1488         do_facet $SINGLEMDS lctl set_param fail_loc=0
1489
1490         TLOG=$DIR/$tfile.getstripe
1491         $GETSTRIPE $DIR/$tdir > $TLOG
1492         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1493         unlinkmany $DIR/$tdir/t- 1000
1494         [ $OBJS -gt 0 ] && \
1495                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1496 }
1497 run_test 27u "skip object creation on OSC w/o objects =========="
1498
1499 test_27v() { # bug 4900
1500         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1503         remote_ost_nodsh && skip "remote OST with nodsh" && return
1504
1505         exhaust_all_precreations 0x215
1506         reset_enospc
1507
1508         test_mkdir -p $DIR/$tdir
1509         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1510
1511         touch $DIR/$tdir/$tfile
1512         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1513         # all except ost1
1514         for (( i=1; i < OSTCOUNT; i++ )); do
1515                 do_facet ost$i lctl set_param fail_loc=0x705
1516         done
1517         local START=`date +%s`
1518         createmany -o $DIR/$tdir/$tfile 32
1519
1520         local FINISH=`date +%s`
1521         local TIMEOUT=`lctl get_param -n timeout`
1522         local PROCESS=$((FINISH - START))
1523         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1524                error "$FINISH - $START >= $TIMEOUT / 2"
1525         sleep $((TIMEOUT / 2 - PROCESS))
1526         reset_enospc
1527 }
1528 run_test 27v "skip object creation on slow OST ================="
1529
1530 test_27w() { # bug 10997
1531         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1532         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1533         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1534                 error "stripe size $size != 65536" || true
1535         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1536                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1537 }
1538 run_test 27w "check $SETSTRIPE -S option"
1539
1540 test_27wa() {
1541         [ "$OSTCOUNT" -lt "2" ] &&
1542                 skip_env "skipping multiple stripe count/offset test" && return
1543
1544         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1545         for i in $(seq 1 $OSTCOUNT); do
1546                 offset=$((i - 1))
1547                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1548                         error "setstripe -c $i -i $offset failed"
1549                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1550                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1551                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1552                 [ $index -ne $offset ] &&
1553                         error "stripe offset $index != $offset" || true
1554         done
1555 }
1556 run_test 27wa "check $SETSTRIPE -c -i options"
1557
1558 test_27x() {
1559         remote_ost_nodsh && skip "remote OST with nodsh" && return
1560         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1561         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1562         OFFSET=$(($OSTCOUNT - 1))
1563         OSTIDX=0
1564         local OST=$(ostname_from_index $OSTIDX)
1565
1566         test_mkdir -p $DIR/$tdir
1567         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1568         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1569         sleep_maxage
1570         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1571         for i in `seq 0 $OFFSET`; do
1572                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1573                 error "OST0 was degraded but new created file still use it"
1574         done
1575         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1576 }
1577 run_test 27x "create files while OST0 is degraded"
1578
1579 test_27y() {
1580         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1581         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1582         remote_ost_nodsh && skip "remote OST with nodsh" && return
1583         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1584
1585         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1586         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1587             osc.$mdtosc.prealloc_last_id)
1588         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1589             osc.$mdtosc.prealloc_next_id)
1590         local fcount=$((last_id - next_id))
1591         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1592         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1593
1594         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1595                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1596         local OST_DEACTIVE_IDX=-1
1597         local OSC
1598         local OSTIDX
1599         local OST
1600
1601         for OSC in $MDS_OSCS; do
1602                 OST=$(osc_to_ost $OSC)
1603                 OSTIDX=$(index_from_ostuuid $OST)
1604                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1605                         OST_DEACTIVE_IDX=$OSTIDX
1606                 fi
1607                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1608                         echo $OSC "is Deactivated:"
1609                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1610                 fi
1611         done
1612
1613         OSTIDX=$(index_from_ostuuid $OST)
1614         mkdir -p $DIR/$tdir
1615         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1616
1617         for OSC in $MDS_OSCS; do
1618                 OST=$(osc_to_ost $OSC)
1619                 OSTIDX=$(index_from_ostuuid $OST)
1620                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1621                         echo $OST "is degraded:"
1622                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1623                                                 obdfilter.$OST.degraded=1
1624                 fi
1625         done
1626
1627         sleep_maxage
1628         createmany -o $DIR/$tdir/$tfile $fcount
1629
1630         for OSC in $MDS_OSCS; do
1631                 OST=$(osc_to_ost $OSC)
1632                 OSTIDX=$(index_from_ostuuid $OST)
1633                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1634                         echo $OST "is recovered from degraded:"
1635                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1636                                                 obdfilter.$OST.degraded=0
1637                 else
1638                         do_facet $SINGLEMDS lctl --device %$OSC activate
1639                 fi
1640         done
1641
1642         # all osp devices get activated, hence -1 stripe count restored
1643         local stripecnt=0
1644
1645         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1646         # devices get activated.
1647         sleep_maxage
1648         $SETSTRIPE -c -1 $DIR/$tfile
1649         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1650         rm -f $DIR/$tfile
1651         [ $stripecnt -ne $OSTCOUNT ] &&
1652                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1653         return 0
1654 }
1655 run_test 27y "create files while OST0 is degraded and the rest inactive"
1656
1657 check_seq_oid()
1658 {
1659         log "check file $1"
1660
1661         lmm_count=$($GETSTRIPE -c $1)
1662         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1663         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1664
1665         local old_ifs="$IFS"
1666         IFS=$'[:]'
1667         fid=($($LFS path2fid $1))
1668         IFS="$old_ifs"
1669
1670         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1671         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1672
1673         # compare lmm_seq and lu_fid->f_seq
1674         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1675         # compare lmm_object_id and lu_fid->oid
1676         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1677
1678         # check the trusted.fid attribute of the OST objects of the file
1679         local have_obdidx=false
1680         local stripe_nr=0
1681         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1682                 # skip lines up to and including "obdidx"
1683                 [ -z "$obdidx" ] && break
1684                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1685                 $have_obdidx || continue
1686
1687                 local ost=$((obdidx + 1))
1688                 local dev=$(ostdevname $ost)
1689
1690                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1691                         echo "Currently only works with ldiskfs-based OSTs"
1692                         continue
1693                 fi
1694
1695                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1696
1697                 #don't unmount/remount the OSTs if we don't need to do that
1698                 #local dir=$(facet_mntpt ost$ost)
1699                 #stop ost$dev
1700                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1701                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1702                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1703                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1704                 seq=$(echo $seq | sed -e "s/^0x//g")
1705                 if [ $seq == 0 ]; then
1706                         oid_hex=$(echo $oid)
1707                 else
1708                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1709                 fi
1710                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1711                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1712                            $dev 2>/dev/null" | grep "parent=")
1713
1714                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1715
1716                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1717
1718                 #do_facet ost$ost umount -d $dir
1719                 #start ost$ost $dev $OST_MOUNT_OPTS
1720
1721                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1722                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1723                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1724                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1725                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1726                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1727
1728                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1729                 [ $ff_pseq = $lmm_seq ] ||
1730                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1731                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1732                 [ $ff_poid = $lmm_oid ] ||
1733                         error "FF parent OID $ff_poid != $lmm_oid"
1734                 [ $ff_pstripe = $stripe_nr ] ||
1735                         error "FF stripe $ff_pstripe != $stripe_nr"
1736
1737                 stripe_nr=$((stripe_nr + 1))
1738         done
1739 }
1740
1741 test_27z() {
1742         remote_ost_nodsh && skip "remote OST with nodsh" && return
1743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1744         test_mkdir -p $DIR/$tdir
1745
1746         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1747                 { error "setstripe -c -1 failed"; return 1; }
1748         # We need to send a write to every object to get parent FID info set.
1749         # This _should_ also work for setattr, but does not currently.
1750         # touch $DIR/$tdir/$tfile-1 ||
1751         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1752                 { error "dd $tfile-1 failed"; return 2; }
1753         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1754                 { error "setstripe -c -1 failed"; return 3; }
1755         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1756                 { error "dd $tfile-2 failed"; return 4; }
1757
1758         # make sure write RPCs have been sent to OSTs
1759         sync; sleep 5; sync
1760
1761         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1762         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1763 }
1764 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1765
1766 test_27A() { # b=19102
1767         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1768         local restore_size=$($GETSTRIPE -S $MOUNT)
1769         local restore_count=$($GETSTRIPE -c $MOUNT)
1770         local restore_offset=$($GETSTRIPE -i $MOUNT)
1771         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1772         local default_size=$($GETSTRIPE -S $MOUNT)
1773         local default_count=$($GETSTRIPE -c $MOUNT)
1774         local default_offset=$($GETSTRIPE -i $MOUNT)
1775         local dsize=$((1024 * 1024))
1776         [ $default_size -eq $dsize ] ||
1777                 error "stripe size $default_size != $dsize"
1778         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1779         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1780         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1781 }
1782 run_test 27A "check filesystem-wide default LOV EA values"
1783
1784 test_27B() { # LU-2523
1785         test_mkdir -p $DIR/$tdir
1786         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1787         touch $DIR/$tdir/f0
1788         # open f1 with O_LOV_DELAY_CREATE
1789         # rename f0 onto f1
1790         # call setstripe ioctl on open file descriptor for f1
1791         # close
1792         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1793                 $DIR/$tdir/f0
1794
1795         rm -f $DIR/$tdir/f1
1796         # open f1 with O_LOV_DELAY_CREATE
1797         # unlink f1
1798         # call setstripe ioctl on open file descriptor for f1
1799         # close
1800         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1801
1802         # Allow multiop to fail in imitation of NFS's busted semantics.
1803         true
1804 }
1805 run_test 27B "call setstripe on open unlinked file/rename victim"
1806
1807 # createtest also checks that device nodes are created and
1808 # then visible correctly (#2091)
1809 test_28() { # bug 2091
1810         test_mkdir $DIR/d28
1811         $CREATETEST $DIR/d28/ct || error
1812 }
1813 run_test 28 "create/mknod/mkdir with bad file types ============"
1814
1815 test_29() {
1816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1817         cancel_lru_locks mdc
1818         test_mkdir $DIR/d29
1819         touch $DIR/d29/foo
1820         log 'first d29'
1821         ls -l $DIR/d29
1822
1823         declare -i LOCKCOUNTORIG=0
1824         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1825                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1826         done
1827         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1828
1829         declare -i LOCKUNUSEDCOUNTORIG=0
1830         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1831                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1832         done
1833
1834         log 'second d29'
1835         ls -l $DIR/d29
1836         log 'done'
1837
1838         declare -i LOCKCOUNTCURRENT=0
1839         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1840                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1841         done
1842
1843         declare -i LOCKUNUSEDCOUNTCURRENT=0
1844         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1845                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1846         done
1847
1848         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1849                 lctl set_param -n ldlm.dump_namespaces ""
1850                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1851                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1852                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1853                 return 2
1854         fi
1855         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1856                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1857                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1858                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1859                 return 3
1860         fi
1861 }
1862 run_test 29 "IT_GETATTR regression  ============================"
1863
1864 test_30a() { # was test_30
1865         cp `which ls` $DIR || cp /bin/ls $DIR
1866         $DIR/ls / || error
1867         rm $DIR/ls
1868 }
1869 run_test 30a "execute binary from Lustre (execve) =============="
1870
1871 test_30b() {
1872         cp `which ls` $DIR || cp /bin/ls $DIR
1873         chmod go+rx $DIR/ls
1874         $RUNAS $DIR/ls / || error
1875         rm $DIR/ls
1876 }
1877 run_test 30b "execute binary from Lustre as non-root ==========="
1878
1879 test_30c() { # b=22376
1880         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1881         cp `which ls` $DIR || cp /bin/ls $DIR
1882         chmod a-rw $DIR/ls
1883         cancel_lru_locks mdc
1884         cancel_lru_locks osc
1885         $RUNAS $DIR/ls / || error
1886         rm -f $DIR/ls
1887 }
1888 run_test 30c "execute binary from Lustre without read perms ===="
1889
1890 test_31a() {
1891         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1892         $CHECKSTAT -a $DIR/f31 || error
1893 }
1894 run_test 31a "open-unlink file =================================="
1895
1896 test_31b() {
1897         touch $DIR/f31 || error
1898         ln $DIR/f31 $DIR/f31b || error
1899         $MULTIOP $DIR/f31b Ouc || error
1900         $CHECKSTAT -t file $DIR/f31 || error
1901 }
1902 run_test 31b "unlink file with multiple links while open ======="
1903
1904 test_31c() {
1905         touch $DIR/f31 || error
1906         ln $DIR/f31 $DIR/f31c || error
1907         multiop_bg_pause $DIR/f31 O_uc || return 1
1908         MULTIPID=$!
1909         $MULTIOP $DIR/f31c Ouc
1910         kill -USR1 $MULTIPID
1911         wait $MULTIPID
1912 }
1913 run_test 31c "open-unlink file with multiple links ============="
1914
1915 test_31d() {
1916         opendirunlink $DIR/d31d $DIR/d31d || error
1917         $CHECKSTAT -a $DIR/d31d || error
1918 }
1919 run_test 31d "remove of open directory ========================="
1920
1921 test_31e() { # bug 2904
1922         check_kernel_version 34 || return 0
1923         openfilleddirunlink $DIR/d31e || error
1924 }
1925 run_test 31e "remove of open non-empty directory ==============="
1926
1927 test_31f() { # bug 4554
1928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1929         set -vx
1930         test_mkdir $DIR/d31f
1931         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1932         cp /etc/hosts $DIR/d31f
1933         ls -l $DIR/d31f
1934         $GETSTRIPE $DIR/d31f/hosts
1935         multiop_bg_pause $DIR/d31f D_c || return 1
1936         MULTIPID=$!
1937
1938         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1939         test_mkdir $DIR/d31f
1940         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1941         cp /etc/hosts $DIR/d31f
1942         ls -l $DIR/d31f
1943         $GETSTRIPE $DIR/d31f/hosts
1944         multiop_bg_pause $DIR/d31f D_c || return 1
1945         MULTIPID2=$!
1946
1947         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1948         wait $MULTIPID || error "first opendir $MULTIPID failed"
1949
1950         sleep 6
1951
1952         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1953         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1954         set +vx
1955 }
1956 run_test 31f "remove of open directory with open-unlink file ==="
1957
1958 test_31g() {
1959         echo "-- cross directory link --"
1960         test_mkdir $DIR/d31ga
1961         test_mkdir $DIR/d31gb
1962         touch $DIR/d31ga/f
1963         ln $DIR/d31ga/f $DIR/d31gb/g
1964         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1965         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1966         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1967         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1968 }
1969 run_test 31g "cross directory link==============="
1970
1971 test_31h() {
1972         echo "-- cross directory link --"
1973         test_mkdir $DIR/d31h
1974         test_mkdir $DIR/d31h/dir
1975         touch $DIR/d31h/f
1976         ln $DIR/d31h/f $DIR/d31h/dir/g
1977         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1978         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1979         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1980         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1981 }
1982 run_test 31h "cross directory link under child==============="
1983
1984 test_31i() {
1985         echo "-- cross directory link --"
1986         test_mkdir $DIR/d31i
1987         test_mkdir $DIR/d31i/dir
1988         touch $DIR/d31i/dir/f
1989         ln $DIR/d31i/dir/f $DIR/d31i/g
1990         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1991         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1992         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1993         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1994 }
1995 run_test 31i "cross directory link under parent==============="
1996
1997
1998 test_31j() {
1999         test_mkdir $DIR/d31j
2000         test_mkdir $DIR/d31j/dir1
2001         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
2002         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
2003         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
2004         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
2005         return 0
2006 }
2007 run_test 31j "link for directory==============="
2008
2009
2010 test_31k() {
2011         test_mkdir $DIR/d31k
2012         touch $DIR/d31k/s
2013         touch $DIR/d31k/exist
2014         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
2015         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
2016         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
2017         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
2018         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
2019         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
2020         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
2021         return 0
2022 }
2023 run_test 31k "link to file: the same, non-existing, dir==============="
2024
2025 test_31m() {
2026         test_mkdir $DIR/d31m
2027         touch $DIR/d31m/s
2028         test_mkdir $DIR/d31m2
2029         touch $DIR/d31m2/exist
2030         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2031         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2032         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2033         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2034         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2035         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2036         return 0
2037 }
2038 run_test 31m "link to file: the same, non-existing, dir==============="
2039
2040 test_31n() {
2041         [ -e /proc/self/fd/173 ] && echo "skipping, fd 173 is in use" && return
2042         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2043         nlink=$(stat --format=%h $DIR/$tfile)
2044         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2045         exec 173<$DIR/$tfile
2046         trap "exec 173<&-" EXIT
2047         nlink=$(stat --dereference --format=%h /proc/self/fd/173)
2048         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2049         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2050         nlink=$(stat --dereference --format=%h /proc/self/fd/173)
2051         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2052         exec 173<&-
2053 }
2054 run_test 31n "check link count of unlinked file"
2055
2056 cleanup_test32_mount() {
2057         trap 0
2058         $UMOUNT $DIR/$tdir/ext2-mountpoint
2059 }
2060
2061 test_32a() {
2062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2063         echo "== more mountpoints and symlinks ================="
2064         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2065         trap cleanup_test32_mount EXIT
2066         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2067         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2068         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2069         cleanup_test32_mount
2070 }
2071 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2072
2073 test_32b() {
2074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2075         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2076         trap cleanup_test32_mount EXIT
2077         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2078         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2079         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2080         cleanup_test32_mount
2081 }
2082 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2083
2084 test_32c() {
2085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2086         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2087         trap cleanup_test32_mount EXIT
2088         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2089         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2090         test_mkdir -p $DIR/$tdir/d2/test_dir
2091         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2092         cleanup_test32_mount
2093 }
2094 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2095
2096 test_32d() {
2097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2098         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2099         trap cleanup_test32_mount EXIT
2100         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2101         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2102         test_mkdir -p $DIR/$tdir/d2/test_dir
2103         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2104         cleanup_test32_mount
2105 }
2106 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2107
2108 test_32e() {
2109         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2110         test_mkdir -p $DIR/d32e/tmp
2111         TMP_DIR=$DIR/d32e/tmp
2112         ln -s $DIR/d32e $TMP_DIR/symlink11
2113         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2114         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2115         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2116 }
2117 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2118
2119 test_32f() {
2120         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2121         test_mkdir -p $DIR/d32f/tmp
2122         TMP_DIR=$DIR/d32f/tmp
2123         ln -s $DIR/d32f $TMP_DIR/symlink11
2124         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2125         ls $DIR/d32f/tmp/symlink11  || error
2126         ls $DIR/d32f/symlink01 || error
2127 }
2128 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2129
2130 test_32g() {
2131         TMP_DIR=$DIR/$tdir/tmp
2132         test_mkdir -p $DIR/$tdir/tmp
2133         test_mkdir $DIR/${tdir}2
2134         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2135         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2136         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2137         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2138         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2139         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2140 }
2141 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2142
2143 test_32h() {
2144         rm -fr $DIR/$tdir $DIR/${tdir}2
2145         TMP_DIR=$DIR/$tdir/tmp
2146         test_mkdir -p $DIR/$tdir/tmp
2147         test_mkdir $DIR/${tdir}2
2148         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2149         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2150         ls $TMP_DIR/symlink12 || error
2151         ls $DIR/$tdir/symlink02  || error
2152 }
2153 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2154
2155 test_32i() {
2156         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2157         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2158         trap cleanup_test32_mount EXIT
2159         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2160         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2161         touch $DIR/$tdir/test_file
2162         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2163         cleanup_test32_mount
2164 }
2165 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2166
2167 test_32j() {
2168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2169         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2170         trap cleanup_test32_mount EXIT
2171         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2172         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2173         touch $DIR/$tdir/test_file
2174         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2175         cleanup_test32_mount
2176 }
2177 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2178
2179 test_32k() {
2180         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2181         rm -fr $DIR/$tdir
2182         trap cleanup_test32_mount EXIT
2183         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2184         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2185         test_mkdir -p $DIR/$tdir/d2
2186         touch $DIR/$tdir/d2/test_file || error
2187         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2188         cleanup_test32_mount
2189 }
2190 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2191
2192 test_32l() {
2193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2194         rm -fr $DIR/$tdir
2195         trap cleanup_test32_mount EXIT
2196         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2197         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2198         test_mkdir -p $DIR/$tdir/d2
2199         touch $DIR/$tdir/d2/test_file
2200         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2201         cleanup_test32_mount
2202 }
2203 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2204
2205 test_32m() {
2206         rm -fr $DIR/d32m
2207         test_mkdir -p $DIR/d32m/tmp
2208         TMP_DIR=$DIR/d32m/tmp
2209         ln -s $DIR $TMP_DIR/symlink11
2210         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2211         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2212         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2213 }
2214 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2215
2216 test_32n() {
2217         rm -fr $DIR/d32n
2218         test_mkdir -p $DIR/d32n/tmp
2219         TMP_DIR=$DIR/d32n/tmp
2220         ln -s $DIR $TMP_DIR/symlink11
2221         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2222         ls -l $DIR/d32n/tmp/symlink11  || error
2223         ls -l $DIR/d32n/symlink01 || error
2224 }
2225 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2226
2227 test_32o() {
2228         rm -fr $DIR/d32o $DIR/$tfile
2229         touch $DIR/$tfile
2230         test_mkdir -p $DIR/d32o/tmp
2231         TMP_DIR=$DIR/d32o/tmp
2232         ln -s $DIR/$tfile $TMP_DIR/symlink12
2233         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2234         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2235         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2236         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2237         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2238 }
2239 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2240
2241 test_32p() {
2242     log 32p_1
2243         rm -fr $DIR/d32p
2244     log 32p_2
2245         rm -f $DIR/$tfile
2246     log 32p_3
2247         touch $DIR/$tfile
2248     log 32p_4
2249         test_mkdir -p $DIR/d32p/tmp
2250     log 32p_5
2251         TMP_DIR=$DIR/d32p/tmp
2252     log 32p_6
2253         ln -s $DIR/$tfile $TMP_DIR/symlink12
2254     log 32p_7
2255         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2256     log 32p_8
2257         cat $DIR/d32p/tmp/symlink12 || error
2258     log 32p_9
2259         cat $DIR/d32p/symlink02 || error
2260     log 32p_10
2261 }
2262 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2263
2264 cleanup_testdir_mount() {
2265         trap 0
2266         $UMOUNT $DIR/$tdir
2267 }
2268
2269 test_32q() {
2270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2271         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2272         trap cleanup_testdir_mount EXIT
2273         test_mkdir -p $DIR/$tdir
2274         touch $DIR/$tdir/under_the_mount
2275         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2276         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2277         cleanup_testdir_mount
2278 }
2279 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2280
2281 test_32r() {
2282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2283         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2284         trap cleanup_testdir_mount EXIT
2285         test_mkdir -p $DIR/$tdir
2286         touch $DIR/$tdir/under_the_mount
2287         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2288         ls $DIR/$tdir | grep -q under_the_mount && error || true
2289         cleanup_testdir_mount
2290 }
2291 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2292
2293 test_33aa() {
2294         rm -f $DIR/$tfile
2295         touch $DIR/$tfile
2296         chmod 444 $DIR/$tfile
2297         chown $RUNAS_ID $DIR/$tfile
2298         log 33_1
2299         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2300         log 33_2
2301 }
2302 run_test 33aa "write file with mode 444 (should return error) ===="
2303
2304 test_33a() {
2305         rm -fr $DIR/d33
2306         test_mkdir -p $DIR/d33
2307         chown $RUNAS_ID $DIR/d33
2308         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2309         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2310                 error "open RDWR" || true
2311 }
2312 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2313
2314 test_33b() {
2315         rm -fr $DIR/d33
2316         test_mkdir -p $DIR/d33
2317         chown $RUNAS_ID $DIR/d33
2318         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2319 }
2320 run_test 33b "test open file with malformed flags (No panic and return error)"
2321
2322 test_33c() {
2323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2324         local ostnum
2325         local ostname
2326         local write_bytes
2327         local all_zeros
2328
2329         remote_ost_nodsh && skip "remote OST with nodsh" && return
2330         all_zeros=:
2331         rm -fr $DIR/d33
2332         test_mkdir -p $DIR/d33
2333         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2334
2335         sync
2336         for ostnum in $(seq $OSTCOUNT); do
2337                 # test-framework's OST numbering is one-based, while Lustre's
2338                 # is zero-based
2339                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2340                 # Parsing llobdstat's output sucks; we could grep the /proc
2341                 # path, but that's likely to not be as portable as using the
2342                 # llobdstat utility.  So we parse lctl output instead.
2343                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2344                         obdfilter/$ostname/stats |
2345                         awk '/^write_bytes/ {print $7}' )
2346                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2347                 if (( ${write_bytes:-0} > 0 ))
2348                 then
2349                         all_zeros=false
2350                         break;
2351                 fi
2352         done
2353
2354         $all_zeros || return 0
2355
2356         # Write four bytes
2357         echo foo > $DIR/d33/bar
2358         # Really write them
2359         sync
2360
2361         # Total up write_bytes after writing.  We'd better find non-zeros.
2362         for ostnum in $(seq $OSTCOUNT); do
2363                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2364                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2365                         obdfilter/$ostname/stats |
2366                         awk '/^write_bytes/ {print $7}' )
2367                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2368                 if (( ${write_bytes:-0} > 0 ))
2369                 then
2370                         all_zeros=false
2371                         break;
2372                 fi
2373         done
2374
2375         if $all_zeros
2376         then
2377                 for ostnum in $(seq $OSTCOUNT); do
2378                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2379                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2380                         do_facet ost$ostnum lctl get_param -n \
2381                                 obdfilter/$ostname/stats
2382                 done
2383                 error "OST not keeping write_bytes stats (b22312)"
2384         fi
2385 }
2386 run_test 33c "test llobdstat and write_bytes"
2387
2388 test_33d() {
2389         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2391         local MDTIDX=1
2392         local remote_dir=$DIR/$tdir/remote_dir
2393
2394         mkdir -p $DIR/$tdir
2395         $LFS mkdir -i $MDTIDX $remote_dir ||
2396                 error "create remote directory failed"
2397
2398         touch $remote_dir/$tfile
2399         chmod 444 $remote_dir/$tfile
2400         chown $RUNAS_ID $remote_dir/$tfile
2401
2402         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2403
2404         chown $RUNAS_ID $remote_dir
2405         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2406                                         error "create" || true
2407         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2408                                     error "open RDWR" || true
2409         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2410                                     error "create" || true
2411 }
2412 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2413
2414 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2415 test_34a() {
2416         rm -f $DIR/f34
2417         $MCREATE $DIR/f34 || error
2418         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2419         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2420         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2421         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2422 }
2423 run_test 34a "truncate file that has not been opened ==========="
2424
2425 test_34b() {
2426         [ ! -f $DIR/f34 ] && test_34a
2427         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2428         $OPENFILE -f O_RDONLY $DIR/f34
2429         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2430         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2431 }
2432 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2433
2434 test_34c() {
2435         [ ! -f $DIR/f34 ] && test_34a
2436         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2437         $OPENFILE -f O_RDWR $DIR/f34
2438         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2439         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2440 }
2441 run_test 34c "O_RDWR opening file-with-size works =============="
2442
2443 test_34d() {
2444         [ ! -f $DIR/f34 ] && test_34a
2445         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2446         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2447         rm $DIR/f34
2448 }
2449 run_test 34d "write to sparse file ============================="
2450
2451 test_34e() {
2452         rm -f $DIR/f34e
2453         $MCREATE $DIR/f34e || error
2454         $TRUNCATE $DIR/f34e 1000 || error
2455         $CHECKSTAT -s 1000 $DIR/f34e || error
2456         $OPENFILE -f O_RDWR $DIR/f34e
2457         $CHECKSTAT -s 1000 $DIR/f34e || error
2458 }
2459 run_test 34e "create objects, some with size and some without =="
2460
2461 test_34f() { # bug 6242, 6243
2462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2463         SIZE34F=48000
2464         rm -f $DIR/f34f
2465         $MCREATE $DIR/f34f || error
2466         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2467         dd if=$DIR/f34f of=$TMP/f34f
2468         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2469         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2470         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2471         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2472         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2473 }
2474 run_test 34f "read from a file with no objects until EOF ======="
2475
2476 test_34g() {
2477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2478         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2479         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2480         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2481         cancel_lru_locks osc
2482         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2483                 error "wrong size after lock cancel"
2484
2485         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2486         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2487                 error "expanding truncate failed"
2488         cancel_lru_locks osc
2489         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2490                 error "wrong expanded size after lock cancel"
2491 }
2492 run_test 34g "truncate long file ==============================="
2493
2494 test_34h() {
2495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2496         local gid=10
2497         local sz=1000
2498
2499         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2500         sync # Flush the cache so that multiop below does not block on cache
2501              # flush when getting the group lock
2502         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2503         MULTIPID=$!
2504         sleep 2
2505
2506         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2507                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2508                 kill -9 $MULTIPID
2509         fi
2510         wait $MULTIPID
2511         local nsz=`stat -c %s $DIR/$tfile`
2512         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2513 }
2514 run_test 34h "ftruncate file under grouplock should not block"
2515
2516 test_35a() {
2517         cp /bin/sh $DIR/f35a
2518         chmod 444 $DIR/f35a
2519         chown $RUNAS_ID $DIR/f35a
2520         $RUNAS $DIR/f35a && error || true
2521         rm $DIR/f35a
2522 }
2523 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2524
2525 test_36a() {
2526         rm -f $DIR/f36
2527         utime $DIR/f36 || error
2528 }
2529 run_test 36a "MDS utime check (mknod, utime) ==================="
2530
2531 test_36b() {
2532         echo "" > $DIR/f36
2533         utime $DIR/f36 || error
2534 }
2535 run_test 36b "OST utime check (open, utime) ===================="
2536
2537 test_36c() {
2538         rm -f $DIR/d36/f36
2539         test_mkdir $DIR/d36
2540         chown $RUNAS_ID $DIR/d36
2541         $RUNAS utime $DIR/d36/f36 || error
2542 }
2543 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2544
2545 test_36d() {
2546         [ ! -d $DIR/d36 ] && test_36c
2547         echo "" > $DIR/d36/f36
2548         $RUNAS utime $DIR/d36/f36 || error
2549 }
2550 run_test 36d "non-root OST utime check (open, utime) ==========="
2551
2552 test_36e() {
2553         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2554         test_mkdir -p $DIR/$tdir
2555         touch $DIR/$tdir/$tfile
2556         $RUNAS utime $DIR/$tdir/$tfile && \
2557                 error "utime worked, expected failure" || true
2558 }
2559 run_test 36e "utime on non-owned file (should return error) ===="
2560
2561 subr_36fh() {
2562         local fl="$1"
2563         local LANG_SAVE=$LANG
2564         local LC_LANG_SAVE=$LC_LANG
2565         export LANG=C LC_LANG=C # for date language
2566
2567         DATESTR="Dec 20  2000"
2568         test_mkdir -p $DIR/$tdir
2569         lctl set_param fail_loc=$fl
2570         date; date +%s
2571         cp /etc/hosts $DIR/$tdir/$tfile
2572         sync & # write RPC generated with "current" inode timestamp, but delayed
2573         sleep 1
2574         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2575         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2576         cancel_lru_locks osc
2577         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2578         date; date +%s
2579         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2580                 echo "BEFORE: $LS_BEFORE" && \
2581                 echo "AFTER : $LS_AFTER" && \
2582                 echo "WANT  : $DATESTR" && \
2583                 error "$DIR/$tdir/$tfile timestamps changed" || true
2584
2585         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2586 }
2587
2588 test_36f() {
2589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2590         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2591         subr_36fh "0x80000214"
2592 }
2593 run_test 36f "utime on file racing with OST BRW write =========="
2594
2595 test_36g() {
2596         remote_ost_nodsh && skip "remote OST with nodsh" && return
2597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2598         local fmd_max_age
2599         local fmd_before
2600         local fmd_after
2601
2602         test_mkdir -p $DIR/$tdir
2603         fmd_max_age=$(do_facet ost1 \
2604                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2605                 head -n 1")
2606
2607         fmd_before=$(do_facet ost1 \
2608                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2609         touch $DIR/$tdir/$tfile
2610         sleep $((fmd_max_age + 12))
2611         fmd_after=$(do_facet ost1 \
2612                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2613
2614         echo "fmd_before: $fmd_before"
2615         echo "fmd_after: $fmd_after"
2616         [ "$fmd_after" -gt "$fmd_before" ] && \
2617                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2618                 error "fmd didn't expire after ping" || true
2619 }
2620 run_test 36g "filter mod data cache expiry ====================="
2621
2622 test_36h() {
2623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2624         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2625         subr_36fh "0x80000227"
2626 }
2627 run_test 36h "utime on file racing with OST BRW write =========="
2628
2629 # test_37 - duplicate with tests 32q 32r
2630
2631 test_38() {
2632         local file=$DIR/$tfile
2633         touch $file
2634         openfile -f O_DIRECTORY $file
2635         local RC=$?
2636         local ENOTDIR=20
2637         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2638         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2639 }
2640 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2641
2642 test_39() {
2643         touch $DIR/$tfile
2644         touch $DIR/${tfile}2
2645 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2646 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2647 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2648         sleep 2
2649         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2650         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2651                 echo "mtime"
2652                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2653                 echo "atime"
2654                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2655                 echo "ctime"
2656                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2657                 error "O_TRUNC didn't change timestamps"
2658         fi
2659 }
2660 run_test 39 "mtime changed on create ==========================="
2661
2662 test_39b() {
2663         test_mkdir -p $DIR/$tdir
2664         cp -p /etc/passwd $DIR/$tdir/fopen
2665         cp -p /etc/passwd $DIR/$tdir/flink
2666         cp -p /etc/passwd $DIR/$tdir/funlink
2667         cp -p /etc/passwd $DIR/$tdir/frename
2668         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2669
2670         sleep 1
2671         echo "aaaaaa" >> $DIR/$tdir/fopen
2672         echo "aaaaaa" >> $DIR/$tdir/flink
2673         echo "aaaaaa" >> $DIR/$tdir/funlink
2674         echo "aaaaaa" >> $DIR/$tdir/frename
2675
2676         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2677         local link_new=`stat -c %Y $DIR/$tdir/flink`
2678         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2679         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2680
2681         cat $DIR/$tdir/fopen > /dev/null
2682         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2683         rm -f $DIR/$tdir/funlink2
2684         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2685
2686         for (( i=0; i < 2; i++ )) ; do
2687                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2688                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2689                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2690                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2691
2692                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2693                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2694                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2695                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2696
2697                 cancel_lru_locks osc
2698                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2699         done
2700 }
2701 run_test 39b "mtime change on open, link, unlink, rename  ======"
2702
2703 # this should be set to past
2704 TEST_39_MTIME=`date -d "1 year ago" +%s`
2705
2706 # bug 11063
2707 test_39c() {
2708         touch $DIR1/$tfile
2709         sleep 2
2710         local mtime0=`stat -c %Y $DIR1/$tfile`
2711
2712         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2713         local mtime1=`stat -c %Y $DIR1/$tfile`
2714         [ "$mtime1" = $TEST_39_MTIME ] || \
2715                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2716
2717         local d1=`date +%s`
2718         echo hello >> $DIR1/$tfile
2719         local d2=`date +%s`
2720         local mtime2=`stat -c %Y $DIR1/$tfile`
2721         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2722                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2723
2724         mv $DIR1/$tfile $DIR1/$tfile-1
2725
2726         for (( i=0; i < 2; i++ )) ; do
2727                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2728                 [ "$mtime2" = "$mtime3" ] || \
2729                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2730
2731                 cancel_lru_locks osc
2732                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2733         done
2734 }
2735 run_test 39c "mtime change on rename ==========================="
2736
2737 # bug 21114
2738 test_39d() {
2739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2740         touch $DIR1/$tfile
2741
2742         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2743
2744         for (( i=0; i < 2; i++ )) ; do
2745                 local mtime=`stat -c %Y $DIR1/$tfile`
2746                 [ $mtime = $TEST_39_MTIME ] || \
2747                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2748
2749                 cancel_lru_locks osc
2750                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2751         done
2752 }
2753 run_test 39d "create, utime, stat =============================="
2754
2755 # bug 21114
2756 test_39e() {
2757         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2758         touch $DIR1/$tfile
2759         local mtime1=`stat -c %Y $DIR1/$tfile`
2760
2761         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2762
2763         for (( i=0; i < 2; i++ )) ; do
2764                 local mtime2=`stat -c %Y $DIR1/$tfile`
2765                 [ $mtime2 = $TEST_39_MTIME ] || \
2766                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2767
2768                 cancel_lru_locks osc
2769                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2770         done
2771 }
2772 run_test 39e "create, stat, utime, stat ========================"
2773
2774 # bug 21114
2775 test_39f() {
2776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2777         touch $DIR1/$tfile
2778         mtime1=`stat -c %Y $DIR1/$tfile`
2779
2780         sleep 2
2781         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2782
2783         for (( i=0; i < 2; i++ )) ; do
2784                 local mtime2=`stat -c %Y $DIR1/$tfile`
2785                 [ $mtime2 = $TEST_39_MTIME ] || \
2786                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2787
2788                 cancel_lru_locks osc
2789                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2790         done
2791 }
2792 run_test 39f "create, stat, sleep, utime, stat ================="
2793
2794 # bug 11063
2795 test_39g() {
2796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2797         echo hello >> $DIR1/$tfile
2798         local mtime1=`stat -c %Y $DIR1/$tfile`
2799
2800         sleep 2
2801         chmod o+r $DIR1/$tfile
2802
2803         for (( i=0; i < 2; i++ )) ; do
2804                 local mtime2=`stat -c %Y $DIR1/$tfile`
2805                 [ "$mtime1" = "$mtime2" ] || \
2806                         error "lost mtime: $mtime2, should be $mtime1"
2807
2808                 cancel_lru_locks osc
2809                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2810         done
2811 }
2812 run_test 39g "write, chmod, stat ==============================="
2813
2814 # bug 11063
2815 test_39h() {
2816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2817         touch $DIR1/$tfile
2818         sleep 1
2819
2820         local d1=`date`
2821         echo hello >> $DIR1/$tfile
2822         local mtime1=`stat -c %Y $DIR1/$tfile`
2823
2824         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2825         local d2=`date`
2826         if [ "$d1" != "$d2" ]; then
2827                 echo "write and touch not within one second"
2828         else
2829                 for (( i=0; i < 2; i++ )) ; do
2830                         local mtime2=`stat -c %Y $DIR1/$tfile`
2831                         [ "$mtime2" = $TEST_39_MTIME ] || \
2832                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2833
2834                         cancel_lru_locks osc
2835                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2836                 done
2837         fi
2838 }
2839 run_test 39h "write, utime within one second, stat ============="
2840
2841 test_39i() {
2842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2843         touch $DIR1/$tfile
2844         sleep 1
2845
2846         echo hello >> $DIR1/$tfile
2847         local mtime1=`stat -c %Y $DIR1/$tfile`
2848
2849         mv $DIR1/$tfile $DIR1/$tfile-1
2850
2851         for (( i=0; i < 2; i++ )) ; do
2852                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2853
2854                 [ "$mtime1" = "$mtime2" ] || \
2855                         error "lost mtime: $mtime2, should be $mtime1"
2856
2857                 cancel_lru_locks osc
2858                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2859         done
2860 }
2861 run_test 39i "write, rename, stat =============================="
2862
2863 test_39j() {
2864         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2865         start_full_debug_logging
2866         touch $DIR1/$tfile
2867         sleep 1
2868
2869         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2870         lctl set_param fail_loc=0x80000412
2871         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2872                 error "multiop failed"
2873         local multipid=$!
2874         local mtime1=`stat -c %Y $DIR1/$tfile`
2875
2876         mv $DIR1/$tfile $DIR1/$tfile-1
2877
2878         kill -USR1 $multipid
2879         wait $multipid || error "multiop close failed"
2880
2881         for (( i=0; i < 2; i++ )) ; do
2882                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2883                 [ "$mtime1" = "$mtime2" ] ||
2884                         error "mtime is lost on close: $mtime2, " \
2885                               "should be $mtime1"
2886
2887                 cancel_lru_locks osc
2888                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2889         done
2890         lctl set_param fail_loc=0
2891         stop_full_debug_logging
2892 }
2893 run_test 39j "write, rename, close, stat ======================="
2894
2895 test_39k() {
2896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2897         touch $DIR1/$tfile
2898         sleep 1
2899
2900         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2901         local multipid=$!
2902         local mtime1=`stat -c %Y $DIR1/$tfile`
2903
2904         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2905
2906         kill -USR1 $multipid
2907         wait $multipid || error "multiop close failed"
2908
2909         for (( i=0; i < 2; i++ )) ; do
2910                 local mtime2=`stat -c %Y $DIR1/$tfile`
2911
2912                 [ "$mtime2" = $TEST_39_MTIME ] || \
2913                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2914
2915                 cancel_lru_locks osc
2916                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2917         done
2918 }
2919 run_test 39k "write, utime, close, stat ========================"
2920
2921 # this should be set to future
2922 TEST_39_ATIME=`date -d "1 year" +%s`
2923
2924 test_39l() {
2925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2926         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2927         local atime_diff=$(do_facet $SINGLEMDS \
2928                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2929         rm -rf $DIR/$tdir
2930         mkdir -p $DIR/$tdir
2931
2932         # test setting directory atime to future
2933         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2934         local atime=$(stat -c %X $DIR/$tdir)
2935         [ "$atime" = $TEST_39_ATIME ] || \
2936                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2937
2938         # test setting directory atime from future to now
2939         local d1=$(date +%s)
2940         ls $DIR/$tdir
2941         local d2=$(date +%s)
2942
2943         cancel_lru_locks mdc
2944         atime=$(stat -c %X $DIR/$tdir)
2945         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2946                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2947
2948         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2949         sleep 3
2950
2951         # test setting directory atime when now > dir atime + atime_diff
2952         d1=$(date +%s)
2953         ls $DIR/$tdir
2954         d2=$(date +%s)
2955         cancel_lru_locks mdc
2956         atime=$(stat -c %X $DIR/$tdir)
2957         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2958                 error "atime is not updated  : $atime, should be $d2"
2959
2960         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2961         sleep 3
2962
2963         # test not setting directory atime when now < dir atime + atime_diff
2964         ls $DIR/$tdir
2965         cancel_lru_locks mdc
2966         atime=$(stat -c %X $DIR/$tdir)
2967         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2968                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2969
2970         do_facet $SINGLEMDS \
2971                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2972 }
2973 run_test 39l "directory atime update ==========================="
2974
2975 test_39m() {
2976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2977         touch $DIR1/$tfile
2978         sleep 2
2979         local far_past_mtime=$(date -d "May 29 1953" +%s)
2980         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2981
2982         touch -m -d @$far_past_mtime $DIR1/$tfile
2983         touch -a -d @$far_past_atime $DIR1/$tfile
2984
2985         for (( i=0; i < 2; i++ )) ; do
2986                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2987                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2988                         error "atime or mtime set incorrectly"
2989
2990                 cancel_lru_locks osc
2991                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2992         done
2993 }
2994 run_test 39m "test atime and mtime before 1970"
2995
2996 test_40() {
2997         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2998         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2999         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
3000 }
3001 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3002
3003 test_41() {
3004         # bug 1553
3005         small_write $DIR/f41 18
3006 }
3007 run_test 41 "test small file write + fstat ====================="
3008
3009 count_ost_writes() {
3010         lctl get_param -n osc.*.stats |
3011             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
3012 }
3013
3014 # decent default
3015 WRITEBACK_SAVE=500
3016 DIRTY_RATIO_SAVE=40
3017 MAX_DIRTY_RATIO=50
3018 BG_DIRTY_RATIO_SAVE=10
3019 MAX_BG_DIRTY_RATIO=25
3020
3021 start_writeback() {
3022         trap 0
3023         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3024         # dirty_ratio, dirty_background_ratio
3025         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3026                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3027                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3028                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3029         else
3030                 # if file not here, we are a 2.4 kernel
3031                 kill -CONT `pidof kupdated`
3032         fi
3033 }
3034
3035 stop_writeback() {
3036         # setup the trap first, so someone cannot exit the test at the
3037         # exact wrong time and mess up a machine
3038         trap start_writeback EXIT
3039         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3040         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3041                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3042                 sysctl -w vm.dirty_writeback_centisecs=0
3043                 sysctl -w vm.dirty_writeback_centisecs=0
3044                 # save and increase /proc/sys/vm/dirty_ratio
3045                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3046                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3047                 # save and increase /proc/sys/vm/dirty_background_ratio
3048                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3049                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3050         else
3051                 # if file not here, we are a 2.4 kernel
3052                 kill -STOP `pidof kupdated`
3053         fi
3054 }
3055
3056 # ensure that all stripes have some grant before we test client-side cache
3057 setup_test42() {
3058         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3059                 dd if=/dev/zero of=$i bs=4k count=1
3060                 rm $i
3061         done
3062 }
3063
3064 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3065 # file truncation, and file removal.
3066 test_42a() {
3067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3068         setup_test42
3069         cancel_lru_locks osc
3070         stop_writeback
3071         sync; sleep 1; sync # just to be safe
3072         BEFOREWRITES=`count_ost_writes`
3073         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3074         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3075         AFTERWRITES=`count_ost_writes`
3076         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3077                 error "$BEFOREWRITES < $AFTERWRITES"
3078         start_writeback
3079 }
3080 run_test 42a "ensure that we don't flush on close =============="
3081
3082 test_42b() {
3083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3084         setup_test42
3085         cancel_lru_locks osc
3086         stop_writeback
3087         sync
3088         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3089         BEFOREWRITES=`count_ost_writes`
3090         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3091         AFTERWRITES=`count_ost_writes`
3092         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3093                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3094         fi
3095         BEFOREWRITES=`count_ost_writes`
3096         sync || error "sync: $?"
3097         AFTERWRITES=`count_ost_writes`
3098         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3099                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3100         fi
3101         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3102         start_writeback
3103         return 0
3104 }
3105 run_test 42b "test destroy of file with cached dirty data ======"
3106
3107 # if these tests just want to test the effect of truncation,
3108 # they have to be very careful.  consider:
3109 # - the first open gets a {0,EOF}PR lock
3110 # - the first write conflicts and gets a {0, count-1}PW
3111 # - the rest of the writes are under {count,EOF}PW
3112 # - the open for truncate tries to match a {0,EOF}PR
3113 #   for the filesize and cancels the PWs.
3114 # any number of fixes (don't get {0,EOF} on open, match
3115 # composite locks, do smarter file size management) fix
3116 # this, but for now we want these tests to verify that
3117 # the cancellation with truncate intent works, so we
3118 # start the file with a full-file pw lock to match against
3119 # until the truncate.
3120 trunc_test() {
3121         test=$1
3122         file=$DIR/$test
3123         offset=$2
3124         cancel_lru_locks osc
3125         stop_writeback
3126         # prime the file with 0,EOF PW to match
3127         touch $file
3128         $TRUNCATE $file 0
3129         sync; sync
3130         # now the real test..
3131         dd if=/dev/zero of=$file bs=1024 count=100
3132         BEFOREWRITES=`count_ost_writes`
3133         $TRUNCATE $file $offset
3134         cancel_lru_locks osc
3135         AFTERWRITES=`count_ost_writes`
3136         start_writeback
3137 }
3138
3139 test_42c() {
3140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3141         trunc_test 42c 1024
3142         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3143             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3144         rm $file
3145 }
3146 run_test 42c "test partial truncate of file with cached dirty data"
3147
3148 test_42d() {
3149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3150         trunc_test 42d 0
3151         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3152             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3153         rm $file
3154 }
3155 run_test 42d "test complete truncate of file with cached dirty data"
3156
3157 test_42e() { # bug22074
3158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3159         local TDIR=$DIR/${tdir}e
3160         local pagesz=$(page_size)
3161         local pages=16 # hardcoded 16 pages, don't change it.
3162         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3163         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3164         local max_dirty_mb
3165         local warmup_files
3166
3167         test_mkdir -p $DIR/${tdir}e
3168         $SETSTRIPE -c 1 $TDIR
3169         createmany -o $TDIR/f $files
3170
3171         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3172
3173         # we assume that with $OSTCOUNT files, at least one of them will
3174         # be allocated on OST0.
3175         warmup_files=$((OSTCOUNT * max_dirty_mb))
3176         createmany -o $TDIR/w $warmup_files
3177
3178         # write a large amount of data into one file and sync, to get good
3179         # avail_grant number from OST.
3180         for ((i=0; i<$warmup_files; i++)); do
3181                 idx=$($GETSTRIPE -i $TDIR/w$i)
3182                 [ $idx -ne 0 ] && continue
3183                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3184                 break
3185         done
3186         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3187         sync
3188         $LCTL get_param $proc_osc0/cur_dirty_bytes
3189         $LCTL get_param $proc_osc0/cur_grant_bytes
3190
3191         # create as much dirty pages as we can while not to trigger the actual
3192         # RPCs directly. but depends on the env, VFS may trigger flush during this
3193         # period, hopefully we are good.
3194         for ((i=0; i<$warmup_files; i++)); do
3195                 idx=$($GETSTRIPE -i $TDIR/w$i)
3196                 [ $idx -ne 0 ] && continue
3197                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3198         done
3199         $LCTL get_param $proc_osc0/cur_dirty_bytes
3200         $LCTL get_param $proc_osc0/cur_grant_bytes
3201
3202         # perform the real test
3203         $LCTL set_param $proc_osc0/rpc_stats 0
3204         for ((;i<$files; i++)); do
3205                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3206                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3207         done
3208         sync
3209         $LCTL get_param $proc_osc0/rpc_stats
3210
3211         local percent=0
3212         local have_ppr=false
3213         $LCTL get_param $proc_osc0/rpc_stats |
3214                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3215                         # skip lines until we are at the RPC histogram data
3216                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3217                         $have_ppr || continue
3218
3219                         # we only want the percent stat for < 16 pages
3220                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3221
3222                         percent=$((percent + WPCT))
3223                         if [ $percent -gt 15 ]; then
3224                                 error "less than 16-pages write RPCs" \
3225                                       "$percent% > 15%"
3226                                 break
3227                         fi
3228                 done
3229         rm -rf $TDIR
3230 }
3231 run_test 42e "verify sub-RPC writes are not done synchronously"
3232
3233 test_43() {
3234         test_mkdir -p $DIR/$tdir
3235         cp -p /bin/ls $DIR/$tdir/$tfile
3236         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3237         pid=$!
3238         # give multiop a chance to open
3239         sleep 1
3240
3241         $DIR/$tdir/$tfile && error || true
3242         kill -USR1 $pid
3243 }
3244 run_test 43 "execution of file opened for write should return -ETXTBSY"
3245
3246 test_43a() {
3247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3248         test_mkdir -p $DIR/$tdir
3249         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3250                         cp -p multiop $DIR/$tdir/multiop
3251         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3252                         return 1
3253         MULTIOP_PID=$!
3254         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3255         kill -USR1 $MULTIOP_PID || return 2
3256         wait $MULTIOP_PID || return 3
3257         rm $TMP/test43.junk
3258 }
3259 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3260
3261 test_43b() {
3262         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3263         test_mkdir -p $DIR/$tdir
3264         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3265                         cp -p multiop $DIR/$tdir/multiop
3266         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3267                         return 1
3268         MULTIOP_PID=$!
3269         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3270         kill -USR1 $MULTIOP_PID || return 2
3271         wait $MULTIOP_PID || return 3
3272         rm $TMP/test43.junk
3273 }
3274 run_test 43b "truncate of file being executed should return -ETXTBSY"
3275
3276 test_43c() {
3277         local testdir="$DIR/$tdir"
3278         test_mkdir -p $DIR/$tdir
3279         cp $SHELL $testdir/
3280         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3281                 ( cd $testdir && md5sum -c)
3282 }
3283 run_test 43c "md5sum of copy into lustre========================"
3284
3285 test_44() {
3286         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3287         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3288         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3289 }
3290 run_test 44 "zero length read from a sparse stripe ============="
3291
3292 test_44a() {
3293     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3294                          awk '{print $2}'`
3295     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3296     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3297     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3298                       awk '{print $2}'`
3299     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3300         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3301     fi
3302
3303     OFFSETS="0 $((stride/2)) $((stride-1))"
3304     for offset in $OFFSETS ; do
3305       for i in `seq 0 $((nstripe-1))`; do
3306         local GLOBALOFFSETS=""
3307         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3308         local myfn=$DIR/d44a-$size
3309         echo "--------writing $myfn at $size"
3310         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3311         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3312         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3313                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3314
3315         for j in `seq 0 $((nstripe-1))`; do
3316             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3317             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3318             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3319         done
3320         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3321                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3322         rm -f $myfn
3323       done
3324     done
3325 }
3326 run_test 44a "test sparse pwrite ==============================="
3327
3328 dirty_osc_total() {
3329         tot=0
3330         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3331                 tot=$(($tot + $d))
3332         done
3333         echo $tot
3334 }
3335 do_dirty_record() {
3336         before=`dirty_osc_total`
3337         echo executing "\"$*\""
3338         eval $*
3339         after=`dirty_osc_total`
3340         echo before $before, after $after
3341 }
3342 test_45() {
3343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3344         f="$DIR/f45"
3345         # Obtain grants from OST if it supports it
3346         echo blah > ${f}_grant
3347         stop_writeback
3348         sync
3349         do_dirty_record "echo blah > $f"
3350         [ $before -eq $after ] && error "write wasn't cached"
3351         do_dirty_record "> $f"
3352         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3353         do_dirty_record "echo blah > $f"
3354         [ $before -eq $after ] && error "write wasn't cached"
3355         do_dirty_record "sync"
3356         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3357         do_dirty_record "echo blah > $f"
3358         [ $before -eq $after ] && error "write wasn't cached"
3359         do_dirty_record "cancel_lru_locks osc"
3360         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3361         start_writeback
3362 }
3363 run_test 45 "osc io page accounting ============================"
3364
3365 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3366 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3367 # objects offset and an assert hit when an rpc was built with 1023's mapped
3368 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3369 test_46() {
3370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3371         f="$DIR/f46"
3372         stop_writeback
3373         sync
3374         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3375         sync
3376         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3377         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3378         sync
3379         start_writeback
3380 }
3381 run_test 46 "dirtying a previously written page ================"
3382
3383 # test_47 is removed "Device nodes check" is moved to test_28
3384
3385 test_48a() { # bug 2399
3386         check_kernel_version 34 || return 0
3387         test_mkdir -p $DIR/$tdir
3388         cd $DIR/$tdir
3389         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3390         test_mkdir $DIR/$tdir || error "recreate directory failed"
3391         touch foo || error "'touch foo' failed after recreating cwd"
3392         test_mkdir $DIR/$tdir/bar ||
3393                      error "'mkdir foo' failed after recreating cwd"
3394         if check_kernel_version 44; then
3395                 touch .foo || error "'touch .foo' failed after recreating cwd"
3396                 test_mkdir $DIR/$tdir/.bar ||
3397                               error "'mkdir .foo' failed after recreating cwd"
3398         fi
3399         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3400         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3401         cd . || error "'cd .' failed after recreating cwd"
3402         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3403         rmdir . && error "'rmdir .' worked after recreating cwd"
3404         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3405         cd .. || error "'cd ..' failed after recreating cwd"
3406 }
3407 run_test 48a "Access renamed working dir (should return errors)="
3408
3409 test_48b() { # bug 2399
3410         check_kernel_version 34 || return 0
3411         rm -rf $DIR/$tdir
3412         test_mkdir -p $DIR/$tdir
3413         cd $DIR/$tdir
3414         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3415         touch foo && error "'touch foo' worked after removing cwd"
3416         test_mkdir $DIR/$tdir/foo &&
3417                      error "'mkdir foo' worked after removing cwd"
3418         if check_kernel_version 44; then
3419                 touch .foo && error "'touch .foo' worked after removing cwd"
3420                 test_mkdir $DIR/$tdir/.foo &&
3421                               error "'mkdir .foo' worked after removing cwd"
3422         fi
3423         ls . > /dev/null && error "'ls .' worked after removing cwd"
3424         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3425         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3426         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3427         rmdir . && error "'rmdir .' worked after removing cwd"
3428         ln -s . foo && error "'ln -s .' worked after removing cwd"
3429         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3430 }
3431 run_test 48b "Access removed working dir (should return errors)="
3432
3433 test_48c() { # bug 2350
3434         check_kernel_version 36 || return 0
3435         #lctl set_param debug=-1
3436         #set -vx
3437         rm -rf $DIR/$tdir
3438         test_mkdir -p $DIR/$tdir/dir
3439         cd $DIR/$tdir/dir
3440         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3441         $TRACE touch foo && error "touch foo worked after removing cwd"
3442         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3443         if check_kernel_version 44; then
3444                 touch .foo && error "touch .foo worked after removing cwd"
3445                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3446         fi
3447         $TRACE ls . && error "'ls .' worked after removing cwd"
3448         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3449         is_patchless || ( $TRACE cd . &&
3450                         error "'cd .' worked after removing cwd" )
3451         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3452         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3453         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3454         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3455 }
3456 run_test 48c "Access removed working subdir (should return errors)"
3457
3458 test_48d() { # bug 2350
3459         check_kernel_version 36 || return 0
3460         #lctl set_param debug=-1
3461         #set -vx
3462         rm -rf $DIR/$tdir
3463         test_mkdir -p $DIR/$tdir/dir
3464         cd $DIR/$tdir/dir
3465         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3466         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3467         $TRACE touch foo && error "'touch foo' worked after removing parent"
3468         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3469         if check_kernel_version 44; then
3470                 touch .foo && error "'touch .foo' worked after removing parent"
3471                 test_mkdir .foo &&
3472                               error "mkdir .foo worked after removing parent"
3473         fi
3474         $TRACE ls . && error "'ls .' worked after removing parent"
3475         $TRACE ls .. && error "'ls ..' worked after removing parent"
3476         is_patchless || ( $TRACE cd . &&
3477                         error "'cd .' worked after recreate parent" )
3478         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3479         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3480         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3481         is_patchless || ( $TRACE cd .. &&
3482                         error "'cd ..' worked after removing parent" || true )
3483 }
3484 run_test 48d "Access removed parent subdir (should return errors)"
3485
3486 test_48e() { # bug 4134
3487         check_kernel_version 41 || return 0
3488         #lctl set_param debug=-1
3489         #set -vx
3490         rm -rf $DIR/$tdir
3491         test_mkdir -p $DIR/$tdir/dir
3492         cd $DIR/$tdir/dir
3493         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3494         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3495         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3496         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3497         # On a buggy kernel addition of "touch foo" after cd .. will
3498         # produce kernel oops in lookup_hash_it
3499         touch ../foo && error "'cd ..' worked after recreate parent"
3500         cd $DIR
3501         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3502 }
3503 run_test 48e "Access to recreated parent subdir (should return errors)"
3504
3505 test_49() { # LU-1030
3506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3507         # get ost1 size - lustre-OST0000
3508         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3509         # write 800M at maximum
3510         [ $ost1_size -gt 819200 ] && ost1_size=819200
3511
3512         lfs setstripe -c 1 -i 0 $DIR/$tfile
3513         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3514         local dd_pid=$!
3515
3516         # change max_pages_per_rpc while writing the file
3517         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3518         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3519         # loop until dd process exits
3520         while ps ax -opid | grep -wq $dd_pid; do
3521                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3522                 sleep $((RANDOM % 5 + 1))
3523         done
3524         # restore original max_pages_per_rpc
3525         $LCTL set_param $osc1_mppc=$orig_mppc
3526         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3527 }
3528 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3529
3530 test_50() {
3531         # bug 1485
3532         test_mkdir $DIR/$tdir
3533         cd $DIR/$tdir
3534         ls /proc/$$/cwd || error
3535 }
3536 run_test 50 "special situations: /proc symlinks  ==============="
3537
3538 test_51a() {    # was test_51
3539         # bug 1516 - create an empty entry right after ".." then split dir
3540         test_mkdir -p $DIR/$tdir
3541         touch $DIR/$tdir/foo
3542         $MCREATE $DIR/$tdir/bar
3543         rm $DIR/$tdir/foo
3544         createmany -m $DIR/$tdir/longfile 201
3545         FNUM=202
3546         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3547                 $MCREATE $DIR/$tdir/longfile$FNUM
3548                 FNUM=$(($FNUM + 1))
3549                 echo -n "+"
3550         done
3551         echo
3552         ls -l $DIR/$tdir > /dev/null || error
3553 }
3554 run_test 51a "special situations: split htree with empty entry =="
3555
3556 export NUMTEST=70000
3557 test_51b() {
3558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3559         local BASE=$DIR/$tdir
3560
3561         # cleanup the directory
3562         rm -fr $BASE
3563
3564         test_mkdir -p $BASE
3565
3566         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3567         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3568         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3569                 return
3570
3571         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3572                 echo "reduced count to $NUMTEST due to inodes"
3573
3574         # need to check free space for the directories as well
3575         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3576         numfree=$((blkfree / 4))
3577         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3578                 echo "reduced count to $NUMTEST due to blocks"
3579
3580         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3581                 echo "failed" > $BASE/fnum
3582 }
3583 run_test 51b "exceed 64k subdirectory nlink limit"
3584
3585 test_51ba() { # LU-993
3586         local BASE=$DIR/$tdir
3587         # unlink all but 100 subdirectories, then check it still works
3588         local LEFT=100
3589         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3590
3591         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3592         local DELETE=$((NUMTEST - LEFT))
3593
3594         # continue on to run this test even if 51b didn't finish,
3595         # just to delete the many subdirectories created.
3596         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3597
3598         # for ldiskfs the nlink count should be 1, but this is OSD specific
3599         # and so this is listed for informational purposes only
3600         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3601         unlinkmany -d $BASE/d $DELETE
3602         RC=$?
3603
3604         if [ $RC -ne 0 ]; then
3605                 if [ "$NUMPREV" == "failed" ]; then
3606                         skip "previous setup failed"
3607                         return 0
3608                 else
3609                         error "unlink of first $DELETE subdirs failed"
3610                         return $RC
3611                 fi
3612         fi
3613
3614         echo "nlink between: $(stat -c %h $BASE)"
3615         # trim the first line of ls output
3616         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3617         [ $FOUND -ne $LEFT ] &&
3618                 error "can't find subdirs: found only $FOUND/$LEFT"
3619
3620         unlinkmany -d $BASE/d $DELETE $LEFT ||
3621                 error "unlink of second $LEFT subdirs failed"
3622         # regardless of whether the backing filesystem tracks nlink accurately
3623         # or not, the nlink count shouldn't be more than "." and ".." here
3624         local AFTER=$(stat -c %h $BASE)
3625         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3626                 echo "nlink after: $AFTER"
3627 }
3628 run_test 51ba "verify nlink for many subdirectory cleanup"
3629
3630 test_51d() {
3631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3632         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3633         test_mkdir -p $DIR/$tdir
3634         createmany -o $DIR/$tdir/t- 1000
3635         $GETSTRIPE $DIR/$tdir > $TMP/files
3636         for N in `seq 0 $((OSTCOUNT - 1))`; do
3637             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3638             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3639             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3640         done
3641         unlinkmany $DIR/$tdir/t- 1000
3642
3643         NLAST=0
3644         for N in `seq 1 $((OSTCOUNT - 1))`; do
3645             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3646                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3647             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3648                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3649
3650             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3651                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3652             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3653                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3654             NLAST=$N
3655         done
3656 }
3657 run_test 51d "check object distribution ===================="
3658
3659 test_52a() {
3660         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3661         test_mkdir -p $DIR/$tdir
3662         touch $DIR/$tdir/foo
3663         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3664         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3665         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3666         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3667         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3668                                         error "link worked"
3669         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3670         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3671         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3672                                                      error "lsattr"
3673         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3674         cp -r $DIR/$tdir /tmp/
3675         rm -fr $DIR/$tdir || error "cleanup rm failed"
3676 }
3677 run_test 52a "append-only flag test (should return errors) ====="
3678
3679 test_52b() {
3680         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3681         test_mkdir -p $DIR/$tdir
3682         touch $DIR/$tdir/foo
3683         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3684         cat test > $DIR/$tdir/foo && error "cat test worked"
3685         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3686         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3687         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3688                                         error "link worked"
3689         echo foo >> $DIR/$tdir/foo && error "echo worked"
3690         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3691         [ -f $DIR/$tdir/foo ] || error
3692         [ -f $DIR/$tdir/foo_ren ] && error
3693         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3694                                                         error "lsattr"
3695         chattr -i $DIR/$tdir/foo || error "chattr failed"
3696
3697         rm -fr $DIR/$tdir || error
3698 }
3699 run_test 52b "immutable flag test (should return errors) ======="
3700
3701 test_53() {
3702         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3703         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3704         remote_ost_nodsh && skip "remote OST with nodsh" && return
3705
3706         local param
3707         local ostname
3708         local mds_last
3709         local ost_last
3710         local ostnum
3711         local node
3712
3713         # only test MDT0000
3714         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3715         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3716                 param=`echo ${value[0]} | cut -d "=" -f1`
3717                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3718                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3719                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3720                 node=$(facet_active_host ost$((ostnum+1)))
3721                 param="obdfilter.$ostname.last_id"
3722                 ost_last=$(do_node $node lctl get_param -n $param | head -n 1)
3723                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3724                 if [ $ost_last != $mds_last ]; then
3725                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3726                 fi
3727         done
3728 }
3729 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3730
3731 test_54a() {
3732         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3733         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3734         $SOCKETSERVER $DIR/socket
3735         $SOCKETCLIENT $DIR/socket || error
3736         $MUNLINK $DIR/socket
3737 }
3738 run_test 54a "unix domain socket test =========================="
3739
3740 test_54b() {
3741         f="$DIR/f54b"
3742         mknod $f c 1 3
3743         chmod 0666 $f
3744         dd if=/dev/zero of=$f bs=`page_size` count=1
3745 }
3746 run_test 54b "char device works in lustre ======================"
3747
3748 find_loop_dev() {
3749         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3750         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3751         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3752
3753         for i in `seq 3 7`; do
3754                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3755                 LOOPDEV=$LOOPBASE$i
3756                 LOOPNUM=$i
3757                 break
3758         done
3759 }
3760
3761 test_54c() {
3762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3763         tfile="$DIR/f54c"
3764         tdir="$DIR/d54c"
3765         loopdev="$DIR/loop54c"
3766
3767         find_loop_dev
3768         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3769         mknod $loopdev b 7 $LOOPNUM
3770         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3771         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3772         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3773         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3774         test_mkdir -p $tdir
3775         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3776         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3777         df $tdir
3778         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3779         $UMOUNT $tdir
3780         losetup -d $loopdev
3781         rm $loopdev
3782 }
3783 run_test 54c "block device works in lustre ====================="
3784
3785 test_54d() {
3786         f="$DIR/f54d"
3787         string="aaaaaa"
3788         mknod $f p
3789         [ "$string" = `echo $string > $f | cat $f` ] || error
3790 }
3791 run_test 54d "fifo device works in lustre ======================"
3792
3793 test_54e() {
3794         check_kernel_version 46 || return 0
3795         f="$DIR/f54e"
3796         string="aaaaaa"
3797         cp -aL /dev/console $f
3798         echo $string > $f || error
3799 }
3800 run_test 54e "console/tty device works in lustre ======================"
3801
3802 #The test_55 used to be iopen test and it was removed by bz#24037.
3803 #run_test 55 "check iopen_connect_dentry() ======================"
3804
3805 test_56a() {    # was test_56
3806         rm -rf $DIR/$tdir
3807         $SETSTRIPE -d $DIR
3808         test_mkdir $DIR/$tdir
3809         test_mkdir $DIR/$tdir/dir
3810         NUMFILES=3
3811         NUMFILESx2=$(($NUMFILES * 2))
3812         for i in `seq 1 $NUMFILES` ; do
3813                 touch $DIR/$tdir/file$i
3814                 touch $DIR/$tdir/dir/file$i
3815         done
3816
3817         # test lfs getstripe with --recursive
3818         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3819         [ $FILENUM -eq $NUMFILESx2 ] ||
3820                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3821         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3822         [ $FILENUM -eq $NUMFILES ] ||
3823                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3824         echo "$GETSTRIPE --recursive passed."
3825
3826         # test lfs getstripe with file instead of dir
3827         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3828         [ $FILENUM  -eq 1 ] || error \
3829                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3830         echo "$GETSTRIPE file1 passed."
3831
3832         #test lfs getstripe with --verbose
3833         [ `$GETSTRIPE --verbose $DIR/$tdir |
3834                         grep -c lmm_magic` -eq $NUMFILES ] ||
3835                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3836         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3837             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3838         echo "$GETSTRIPE --verbose passed."
3839
3840         #test lfs getstripe with --obd
3841         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3842                                         grep -q "unknown obduuid" ||
3843                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3844
3845         [  "$OSTCOUNT" -lt 2 ] &&
3846                 skip_env "skipping other $GETSTRIPE --obd test" && return
3847
3848         OSTIDX=1
3849         OBDUUID=$(ostuuid_from_index $OSTIDX)
3850         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3851         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3852         [ $FOUND -eq $FILENUM ] ||
3853                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3854         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3855                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3856                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3857                 error "$GETSTRIPE --obd: should not show file on other obd"
3858         echo "$GETSTRIPE --obd passed"
3859 }
3860 run_test 56a "check $GETSTRIPE"
3861
3862 NUMFILES=3
3863 NUMDIRS=3
3864 setup_56() {
3865         local LOCAL_NUMFILES="$1"
3866         local LOCAL_NUMDIRS="$2"
3867         local MKDIR_PARAMS="$3"
3868
3869         if [ ! -d "$TDIR" ] ; then
3870                 test_mkdir -p $TDIR
3871                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3872                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3873                         touch $TDIR/file$i
3874                 done
3875                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3876                         test_mkdir $TDIR/dir$i
3877                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3878                                 touch $TDIR/dir$i/file$j
3879                         done
3880                 done
3881         fi
3882 }
3883
3884 setup_56_special() {
3885         LOCAL_NUMFILES=$1
3886         LOCAL_NUMDIRS=$2
3887         setup_56 $1 $2
3888         if [ ! -e "$TDIR/loop1b" ] ; then
3889                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3890                         mknod $TDIR/loop${i}b b 7 $i
3891                         mknod $TDIR/null${i}c c 1 3
3892                         ln -s $TDIR/file1 $TDIR/link${i}l
3893                 done
3894                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3895                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3896                         mknod $TDIR/dir$i/null${i}c c 1 3
3897                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3898                 done
3899         fi
3900 }
3901
3902 test_56g() {
3903         $SETSTRIPE -d $DIR
3904
3905         TDIR=$DIR/${tdir}g
3906         setup_56 $NUMFILES $NUMDIRS
3907
3908         EXPECTED=$(($NUMDIRS + 2))
3909         # test lfs find with -name
3910         for i in $(seq 1 $NUMFILES) ; do
3911                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3912                 [ $NUMS -eq $EXPECTED ] ||
3913                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3914                               "found $NUMS, expected $EXPECTED"
3915         done
3916 }
3917 run_test 56g "check lfs find -name ============================="
3918
3919 test_56h() {
3920         $SETSTRIPE -d $DIR
3921
3922         TDIR=$DIR/${tdir}g
3923         setup_56 $NUMFILES $NUMDIRS
3924
3925         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3926         # test lfs find with ! -name
3927         for i in $(seq 1 $NUMFILES) ; do
3928                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3929                 [ $NUMS -eq $EXPECTED ] ||
3930                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3931                               "found $NUMS, expected $EXPECTED"
3932         done
3933 }
3934 run_test 56h "check lfs find ! -name ============================="
3935
3936 test_56i() {
3937        tdir=${tdir}i
3938        test_mkdir -p $DIR/$tdir
3939        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3940        CMD="$LFIND -ost $UUID $DIR/$tdir"
3941        OUT=$($CMD)
3942        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3943 }
3944 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3945
3946 test_56j() {
3947         TDIR=$DIR/${tdir}g
3948         setup_56_special $NUMFILES $NUMDIRS
3949
3950         EXPECTED=$((NUMDIRS + 1))
3951         CMD="$LFIND -type d $TDIR"
3952         NUMS=$($CMD | wc -l)
3953         [ $NUMS -eq $EXPECTED ] ||
3954                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3955 }
3956 run_test 56j "check lfs find -type d ============================="
3957
3958 test_56k() {
3959         TDIR=$DIR/${tdir}g
3960         setup_56_special $NUMFILES $NUMDIRS
3961
3962         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3963         CMD="$LFIND -type f $TDIR"
3964         NUMS=$($CMD | wc -l)
3965         [ $NUMS -eq $EXPECTED ] ||
3966                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3967 }
3968 run_test 56k "check lfs find -type f ============================="
3969
3970 test_56l() {
3971         TDIR=$DIR/${tdir}g
3972         setup_56_special $NUMFILES $NUMDIRS
3973
3974         EXPECTED=$((NUMDIRS + NUMFILES))
3975         CMD="$LFIND -type b $TDIR"
3976         NUMS=$($CMD | wc -l)
3977         [ $NUMS -eq $EXPECTED ] ||
3978                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3979 }
3980 run_test 56l "check lfs find -type b ============================="
3981
3982 test_56m() {
3983         TDIR=$DIR/${tdir}g
3984         setup_56_special $NUMFILES $NUMDIRS
3985
3986         EXPECTED=$((NUMDIRS + NUMFILES))
3987         CMD="$LFIND -type c $TDIR"
3988         NUMS=$($CMD | wc -l)
3989         [ $NUMS -eq $EXPECTED ] ||
3990                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3991 }
3992 run_test 56m "check lfs find -type c ============================="
3993
3994 test_56n() {
3995         TDIR=$DIR/${tdir}g
3996         setup_56_special $NUMFILES $NUMDIRS
3997
3998         EXPECTED=$((NUMDIRS + NUMFILES))
3999         CMD="$LFIND -type l $TDIR"
4000         NUMS=$($CMD | wc -l)
4001         [ $NUMS -eq $EXPECTED ] ||
4002                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4003 }
4004 run_test 56n "check lfs find -type l ============================="
4005
4006 test_56o() {
4007         TDIR=$DIR/${tdir}o
4008         setup_56 $NUMFILES $NUMDIRS
4009
4010         utime $TDIR/file1 > /dev/null || error "utime (1)"
4011         utime $TDIR/file2 > /dev/null || error "utime (2)"
4012         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4013         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4014         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4015         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4016
4017         EXPECTED=4
4018         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4019         [ $NUMS -eq $EXPECTED ] || \
4020                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4021
4022         EXPECTED=12
4023         CMD="$LFIND -mtime 0 $TDIR"
4024         NUMS=$($CMD | wc -l)
4025         [ $NUMS -eq $EXPECTED ] ||
4026                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4027 }
4028 run_test 56o "check lfs find -mtime for old files =========================="
4029
4030 test_56p() {
4031         [ $RUNAS_ID -eq $UID ] &&
4032                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4033
4034         TDIR=$DIR/${tdir}p
4035         setup_56 $NUMFILES $NUMDIRS
4036
4037         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4038         EXPECTED=$NUMFILES
4039         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4040         NUMS=$($CMD | wc -l)
4041         [ $NUMS -eq $EXPECTED ] || \
4042                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4043
4044         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4045         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4046         NUMS=$($CMD | wc -l)
4047         [ $NUMS -eq $EXPECTED ] || \
4048                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4049 }
4050 run_test 56p "check lfs find -uid and ! -uid ==============================="
4051
4052 test_56q() {
4053         [ $RUNAS_ID -eq $UID ] &&
4054                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4055
4056         TDIR=$DIR/${tdir}q
4057         setup_56 $NUMFILES $NUMDIRS
4058
4059         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4060
4061         EXPECTED=$NUMFILES
4062         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4063         NUMS=$($CMD | wc -l)
4064         [ $NUMS -eq $EXPECTED ] ||
4065                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4066
4067         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4068         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4069         NUMS=$($CMD | wc -l)
4070         [ $NUMS -eq $EXPECTED ] ||
4071                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4072 }
4073 run_test 56q "check lfs find -gid and ! -gid ==============================="
4074
4075 test_56r() {
4076         TDIR=$DIR/${tdir}r
4077         setup_56 $NUMFILES $NUMDIRS
4078
4079         EXPECTED=12
4080         CMD="$LFIND -size 0 -type f $TDIR"
4081         NUMS=$($CMD | wc -l)
4082         [ $NUMS -eq $EXPECTED ] ||
4083                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4084         EXPECTED=0
4085         CMD="$LFIND ! -size 0 -type f $TDIR"
4086         NUMS=$($CMD | wc -l)
4087         [ $NUMS -eq $EXPECTED ] ||
4088                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4089         echo "test" > $TDIR/$tfile
4090         echo "test2" > $TDIR/$tfile.2 && sync
4091         EXPECTED=1
4092         CMD="$LFIND -size 5 -type f $TDIR"
4093         NUMS=$($CMD | wc -l)
4094         [ $NUMS -eq $EXPECTED ] ||
4095                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4096         EXPECTED=1
4097         CMD="$LFIND -size +5 -type f $TDIR"
4098         NUMS=$($CMD | wc -l)
4099         [ $NUMS -eq $EXPECTED ] ||
4100                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4101         EXPECTED=2
4102         CMD="$LFIND -size +0 -type f $TDIR"
4103         NUMS=$($CMD | wc -l)
4104         [ $NUMS -eq $EXPECTED ] ||
4105                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4106         EXPECTED=2
4107         CMD="$LFIND ! -size -5 -type f $TDIR"
4108         NUMS=$($CMD | wc -l)
4109         [ $NUMS -eq $EXPECTED ] ||
4110                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4111         EXPECTED=12
4112         CMD="$LFIND -size -5 -type f $TDIR"
4113         NUMS=$($CMD | wc -l)
4114         [ $NUMS -eq $EXPECTED ] ||
4115                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4116 }
4117 run_test 56r "check lfs find -size works =========================="
4118
4119 test_56s() { # LU-611
4120         TDIR=$DIR/${tdir}s
4121         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4122
4123         if [ $OSTCOUNT -gt 1 ]; then
4124                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4125                 ONESTRIPE=4
4126                 EXTRA=4
4127         else
4128                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4129                 EXTRA=0
4130         fi
4131
4132         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4133         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4134         NUMS=$($CMD | wc -l)
4135         [ $NUMS -eq $EXPECTED ] ||
4136                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4137
4138         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4139         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4140         NUMS=$($CMD | wc -l)
4141         [ $NUMS -eq $EXPECTED ] ||
4142                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4143
4144         EXPECTED=$ONESTRIPE
4145         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4146         NUMS=$($CMD | wc -l)
4147         [ $NUMS -eq $EXPECTED ] ||
4148                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4149
4150         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4151         NUMS=$($CMD | wc -l)
4152         [ $NUMS -eq $EXPECTED ] ||
4153                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4154
4155         EXPECTED=0
4156         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4157         NUMS=$($CMD | wc -l)
4158         [ $NUMS -eq $EXPECTED ] ||
4159                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4160 }
4161 run_test 56s "check lfs find -stripe-count works"
4162
4163 test_56t() { # LU-611
4164         TDIR=$DIR/${tdir}t
4165         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4166
4167         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4168
4169         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4170         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4171         NUMS=$($CMD | wc -l)
4172         [ $NUMS -eq $EXPECTED ] ||
4173                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4174
4175         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4176         NUMS=$($CMD | wc -l)
4177         [ $NUMS -eq $EXPECTED ] ||
4178                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4179
4180         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4181         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4182         NUMS=$($CMD | wc -l)
4183         [ $NUMS -eq $EXPECTED ] ||
4184                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4185
4186         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4187         NUMS=$($CMD | wc -l)
4188         [ $NUMS -eq $EXPECTED ] ||
4189                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4190
4191         EXPECTED=4
4192         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4193         NUMS=$($CMD | wc -l)
4194         [ $NUMS -eq $EXPECTED ] ||
4195                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4196
4197         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4198         NUMS=$($CMD | wc -l)
4199         [ $NUMS -eq $EXPECTED ] ||
4200                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4201
4202         EXPECTED=0
4203         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4204         NUMS=$($CMD | wc -l)
4205         [ $NUMS -eq $EXPECTED ] ||
4206                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4207 }
4208 run_test 56t "check lfs find -stripe-size works"
4209
4210 test_56u() { # LU-611
4211         TDIR=$DIR/${tdir}u
4212         setup_56 $NUMFILES $NUMDIRS "-i 0"
4213
4214         if [ $OSTCOUNT -gt 1 ]; then
4215                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4216                 ONESTRIPE=4
4217         else
4218                 ONESTRIPE=0
4219         fi
4220
4221         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4222         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4223         NUMS=$($CMD | wc -l)
4224         [ $NUMS -eq $EXPECTED ] ||
4225                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4226
4227         EXPECTED=$ONESTRIPE
4228         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4229         NUMS=$($CMD | wc -l)
4230         [ $NUMS -eq $EXPECTED ] ||
4231                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4232
4233         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4234         NUMS=$($CMD | wc -l)
4235         [ $NUMS -eq $EXPECTED ] ||
4236                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4237
4238         EXPECTED=0
4239         # This should produce an error and not return any files
4240         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4241         NUMS=$($CMD 2>/dev/null | wc -l)
4242         [ $NUMS -eq $EXPECTED ] ||
4243                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4244
4245         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4246         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4247         NUMS=$($CMD | wc -l)
4248         [ $NUMS -eq $EXPECTED ] ||
4249                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4250 }
4251 run_test 56u "check lfs find -stripe-index works"
4252
4253 test_56v() {
4254     local MDT_IDX=0
4255
4256     TDIR=$DIR/${tdir}v
4257     rm -rf $TDIR
4258     setup_56 $NUMFILES $NUMDIRS
4259
4260     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4261     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4262
4263     for file in $($LFIND -mdt $UUID $TDIR); do
4264         file_mdt_idx=$($GETSTRIPE -M $file)
4265         [ $file_mdt_idx -eq $MDT_IDX ] ||
4266             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4267     done
4268 }
4269 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4270
4271 # Get and check the actual stripe count of one file.
4272 # Usage: check_stripe_count <file> <expected_stripe_count>
4273 check_stripe_count() {
4274     local file=$1
4275     local expected=$2
4276     local actual
4277
4278     [[ -z "$file" || -z "$expected" ]] &&
4279         error "check_stripe_count: invalid argument!"
4280
4281     local cmd="$GETSTRIPE -c $file"
4282     actual=$($cmd) || error "$cmd failed"
4283     actual=${actual%% *}
4284
4285     if [[ $actual -ne $expected ]]; then
4286         [[ $expected -eq -1 ]] ||
4287             error "$cmd wrong: found $actual, expected $expected"
4288         [[ $actual -eq $OSTCOUNT ]] ||
4289             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4290     fi
4291 }
4292
4293 test_56w() {
4294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4295         TDIR=$DIR/${tdir}w
4296
4297     rm -rf $TDIR || error "remove $TDIR failed"
4298     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4299
4300     local stripe_size
4301     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4302         error "$GETSTRIPE -S -d $TDIR failed"
4303     stripe_size=${stripe_size%% *}
4304
4305     local file_size=$((stripe_size * OSTCOUNT))
4306     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4307     local required_space=$((file_num * file_size))
4308     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4309     [[ $free_space -le $((required_space / 1024)) ]] &&
4310         skip_env "need at least $required_space bytes free space," \
4311                  "have $free_space kbytes" && return
4312
4313     local dd_bs=65536
4314     local dd_count=$((file_size / dd_bs))
4315
4316     # write data into the files
4317     local i
4318     local j
4319     local file
4320     for i in $(seq 1 $NUMFILES); do
4321         file=$TDIR/file$i
4322         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4323             error "write data into $file failed"
4324     done
4325     for i in $(seq 1 $NUMDIRS); do
4326         for j in $(seq 1 $NUMFILES); do
4327             file=$TDIR/dir$i/file$j
4328             yes | dd bs=$dd_bs count=$dd_count of=$file \
4329                 >/dev/null 2>&1 ||
4330                 error "write data into $file failed"
4331         done
4332     done
4333
4334     local expected=-1
4335     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4336
4337     # lfs_migrate file
4338     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4339     echo "$cmd"
4340     eval $cmd || error "$cmd failed"
4341
4342     check_stripe_count $TDIR/file1 $expected
4343
4344     # lfs_migrate dir
4345     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4346     echo "$cmd"
4347     eval $cmd || error "$cmd failed"
4348
4349     for j in $(seq 1 $NUMFILES); do
4350         check_stripe_count $TDIR/dir1/file$j $expected
4351     done
4352
4353     # lfs_migrate works with lfs find
4354     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4355          $LFS_MIGRATE -y -c $expected"
4356     echo "$cmd"
4357     eval $cmd || error "$cmd failed"
4358
4359     for i in $(seq 2 $NUMFILES); do
4360         check_stripe_count $TDIR/file$i $expected
4361     done
4362     for i in $(seq 2 $NUMDIRS); do
4363         for j in $(seq 1 $NUMFILES); do
4364             check_stripe_count $TDIR/dir$i/file$j $expected
4365         done
4366     done
4367 }
4368 run_test 56w "check lfs_migrate -c stripe_count works"
4369
4370 test_57a() {
4371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4372         # note test will not do anything if MDS is not local
4373         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4374                 skip "Only applicable to ldiskfs-based MDTs"
4375                 return
4376         fi
4377
4378         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4379         local MNTDEV="osd*.*MDT*.mntdev"
4380         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4381         [ -z "$DEV" ] && error "can't access $MNTDEV"
4382         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4383                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4384                         error "can't access $DEV"
4385                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4386                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4387                 rm $TMP/t57a.dump
4388         done
4389 }
4390 run_test 57a "verify MDS filesystem created with large inodes =="
4391
4392 test_57b() {
4393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4394         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4395                 skip "Only applicable to ldiskfs-based MDTs"
4396                 return
4397         fi
4398
4399         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4400         local dir=$DIR/d57b
4401
4402         local FILECOUNT=100
4403         local FILE1=$dir/f1
4404         local FILEN=$dir/f$FILECOUNT
4405
4406         rm -rf $dir || error "removing $dir"
4407         test_mkdir -p $dir || error "creating $dir"
4408         local num=$(get_mds_dir $dir)
4409         local mymds=mds$num
4410
4411         echo "mcreating $FILECOUNT files"
4412         createmany -m $dir/f 1 $FILECOUNT || \
4413                 error "creating files in $dir"
4414
4415         # verify that files do not have EAs yet
4416         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4417         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4418
4419         sync
4420         sleep 1
4421         df $dir  #make sure we get new statfs data
4422         local MDSFREE=$(do_facet $mymds \
4423                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4424         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4425         echo "opening files to create objects/EAs"
4426         local FILE
4427         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4428                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4429         done
4430
4431         # verify that files have EAs now
4432         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4433         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4434
4435         sleep 1  #make sure we get new statfs data
4436         df $dir
4437         local MDSFREE2=$(do_facet $mymds \
4438                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4439         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4440         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4441                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4442                         error "MDC before $MDCFREE != after $MDCFREE2"
4443                 else
4444                         echo "MDC before $MDCFREE != after $MDCFREE2"
4445                         echo "unable to confirm if MDS has large inodes"
4446                 fi
4447         fi
4448         rm -rf $dir
4449 }
4450 run_test 57b "default LOV EAs are stored inside large inodes ==="
4451
4452 test_58() {
4453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4454         [ -z "$(which wiretest 2>/dev/null)" ] &&
4455                         skip_env "could not find wiretest" && return
4456         wiretest
4457 }
4458 run_test 58 "verify cross-platform wire constants =============="
4459
4460 test_59() {
4461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4462         echo "touch 130 files"
4463         createmany -o $DIR/f59- 130
4464         echo "rm 130 files"
4465         unlinkmany $DIR/f59- 130
4466         sync
4467         # wait for commitment of removal
4468         wait_delete_completed
4469 }
4470 run_test 59 "verify cancellation of llog records async ========="
4471
4472 TEST60_HEAD="test_60 run $RANDOM"
4473 test_60a() {
4474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4475         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4476         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4477         log "$TEST60_HEAD - from kernel mode"
4478         do_facet mgs sh run-llog.sh
4479 }
4480 run_test 60a "llog sanity tests run from kernel module =========="
4481
4482 test_60b() { # bug 6411
4483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4484         dmesg > $DIR/$tfile
4485         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4486                                  /llog.test/ {
4487                                          if (marker)
4488                                                  from_marker++
4489                                          from_begin++
4490                                  }
4491                                  END {
4492                                          if (marker)
4493                                                  print from_marker
4494                                          else
4495                                                  print from_begin
4496                                  }"`
4497         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4498 }
4499 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4500
4501 test_60c() {
4502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4503         echo "create 5000 files"
4504         createmany -o $DIR/f60c- 5000
4505 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4506         lctl set_param fail_loc=0x80000137
4507         unlinkmany $DIR/f60c- 5000
4508         lctl set_param fail_loc=0
4509 }
4510 run_test 60c "unlink file when mds full"
4511
4512 test_60d() {
4513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4514         SAVEPRINTK=$(lctl get_param -n printk)
4515
4516         # verify "lctl mark" is even working"
4517         MESSAGE="test message ID $RANDOM $$"
4518         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4519         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4520
4521         lctl set_param printk=0 || error "set lnet.printk failed"
4522         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4523         MESSAGE="new test message ID $RANDOM $$"
4524         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4525         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4526         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4527
4528         lctl set_param -n printk="$SAVEPRINTK"
4529 }
4530 run_test 60d "test printk console message masking"
4531
4532 test_61() {
4533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4534         f="$DIR/f61"
4535         dd if=/dev/zero of=$f bs=`page_size` count=1
4536         cancel_lru_locks osc
4537         $MULTIOP $f OSMWUc || error
4538         sync
4539 }
4540 run_test 61 "mmap() writes don't make sync hang ================"
4541
4542 # bug 2330 - insufficient obd_match error checking causes LBUG
4543 test_62() {
4544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4545         f="$DIR/f62"
4546         echo foo > $f
4547         cancel_lru_locks osc
4548         lctl set_param fail_loc=0x405
4549         cat $f && error "cat succeeded, expect -EIO"
4550         lctl set_param fail_loc=0
4551 }
4552 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4553 # match every page all of the time.
4554 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4555
4556 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4557 test_63a() {    # was test_63
4558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4559         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4560         lctl set_param -n osc.*.max_dirty_mb 0
4561         for i in `seq 10` ; do
4562                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4563                 sleep 5
4564                 kill $!
4565                 sleep 1
4566         done
4567
4568         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4569         rm -f $DIR/f63 || true
4570 }
4571 run_test 63a "Verify oig_wait interruption does not crash ======="
4572
4573 # bug 2248 - async write errors didn't return to application on sync
4574 # bug 3677 - async write errors left page locked
4575 test_63b() {
4576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4577         debugsave
4578         lctl set_param debug=-1
4579
4580         # ensure we have a grant to do async writes
4581         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4582         rm $DIR/$tfile
4583
4584         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4585         lctl set_param fail_loc=0x80000406
4586         $MULTIOP $DIR/$tfile Owy && \
4587                 error "sync didn't return ENOMEM"
4588         sync; sleep 2; sync     # do a real sync this time to flush page
4589         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4590                 error "locked page left in cache after async error" || true
4591         debugrestore
4592 }
4593 run_test 63b "async write errors should be returned to fsync ==="
4594
4595 test_64a () {
4596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4597         df $DIR
4598         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4599 }
4600 run_test 64a "verify filter grant calculations (in kernel) ====="
4601
4602 test_64b () {
4603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4604         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4605         sh oos.sh $MOUNT
4606 }
4607 run_test 64b "check out-of-space detection on client ==========="
4608
4609 test_64c() {
4610         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4611 }
4612 run_test 64c "verify grant shrink ========================------"
4613
4614 # bug 1414 - set/get directories' stripe info
4615 test_65a() {
4616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4617         test_mkdir -p $DIR/$tdir
4618         touch $DIR/$tdir/f1
4619         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4620 }
4621 run_test 65a "directory with no stripe info ===================="
4622
4623 test_65b() {
4624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4625         test_mkdir -p $DIR/$tdir
4626         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4627                                                 error "setstripe"
4628         touch $DIR/$tdir/f2
4629         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4630 }
4631 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4632
4633 test_65c() {
4634         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4635         if [ $OSTCOUNT -gt 1 ]; then
4636                 test_mkdir -p $DIR/$tdir
4637                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4638                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4639                 touch $DIR/$tdir/f3
4640                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4641         fi
4642 }
4643 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4644
4645 test_65d() {
4646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4647         test_mkdir -p $DIR/$tdir
4648         if [ $STRIPECOUNT -le 0 ]; then
4649                 sc=1
4650         elif [ $STRIPECOUNT -gt 2000 ]; then
4651 #LOV_MAX_STRIPE_COUNT is 2000
4652                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4653         else
4654                 sc=$(($STRIPECOUNT - 1))
4655         fi
4656         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4657         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4658         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4659                                                 error "lverify failed"
4660 }
4661 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4662
4663 test_65e() {
4664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4665         test_mkdir -p $DIR/$tdir
4666
4667         $SETSTRIPE $DIR/$tdir || error "setstripe"
4668         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4669                                         error "no stripe info failed"
4670         touch $DIR/$tdir/f6
4671         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4672 }
4673 run_test 65e "directory setstripe defaults ======================="
4674
4675 test_65f() {
4676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4677         test_mkdir -p $DIR/${tdir}f
4678         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4679 }
4680 run_test 65f "dir setstripe permission (should return error) ==="
4681
4682 test_65g() {
4683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4684         test_mkdir -p $DIR/$tdir
4685         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4686                                                         error "setstripe"
4687         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4688         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4689                 error "delete default stripe failed"
4690 }
4691 run_test 65g "directory setstripe -d ==========================="
4692
4693 test_65h() {
4694         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4695         test_mkdir -p $DIR/$tdir
4696         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4697                                                         error "setstripe"
4698         test_mkdir -p $DIR/$tdir/dd1
4699         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4700                 error "stripe info inherit failed"
4701 }
4702 run_test 65h "directory stripe info inherit ===================="
4703
4704 test_65i() { # bug6367
4705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4706         $SETSTRIPE -S 65536 -c -1 $MOUNT
4707 }
4708 run_test 65i "set non-default striping on root directory (bug 6367)="
4709
4710 test_65ia() { # bug12836
4711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4712         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4713 }
4714 run_test 65ia "getstripe on -1 default directory striping"
4715
4716 test_65ib() { # bug12836
4717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4718         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4719 }
4720 run_test 65ib "getstripe -v on -1 default directory striping"
4721
4722 test_65ic() { # bug12836
4723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4724         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4725 }
4726 run_test 65ic "new find on -1 default directory striping"
4727
4728 test_65j() { # bug6367
4729         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4730         sync; sleep 1
4731         # if we aren't already remounting for each test, do so for this test
4732         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4733                 cleanup || error "failed to unmount"
4734                 setup
4735         fi
4736         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4737 }
4738 run_test 65j "set default striping on root directory (bug 6367)="
4739
4740 test_65k() { # bug11679
4741         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4742         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4743         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4744
4745     echo "Check OST status: "
4746     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4747               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4748
4749     for OSC in $MDS_OSCS; do
4750         echo $OSC "is activate"
4751         do_facet $SINGLEMDS lctl --device %$OSC activate
4752     done
4753
4754     mkdir -p $DIR/$tdir
4755     for INACTIVE_OSC in $MDS_OSCS; do
4756         echo "Deactivate: " $INACTIVE_OSC
4757         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4758         for STRIPE_OSC in $MDS_OSCS; do
4759             OST=`osc_to_ost $STRIPE_OSC`
4760             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4761                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4762
4763             [ -f $DIR/$tdir/$IDX ] && continue
4764             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4765             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4766             RC=$?
4767             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4768         done
4769         rm -f $DIR/$tdir/*
4770         echo $INACTIVE_OSC "is Activate."
4771         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4772     done
4773 }
4774 run_test 65k "validate manual striping works properly with deactivated OSCs"
4775
4776 test_65l() { # bug 12836
4777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4778         test_mkdir -p $DIR/$tdir/test_dir
4779         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4780         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4781 }
4782 run_test 65l "lfs find on -1 stripe dir ========================"
4783
4784 # bug 2543 - update blocks count on client
4785 test_66() {
4786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4787         COUNT=${COUNT:-8}
4788         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4789         sync; sync_all_data; sync; sync_all_data
4790         cancel_lru_locks osc
4791         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4792         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4793 }
4794 run_test 66 "update inode blocks count on client ==============="
4795
4796 LLOOP=
4797 LLITELOOPLOAD=
4798 cleanup_68() {
4799         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4800         trap 0
4801         if [ ! -z "$LLOOP" ]; then
4802                 if swapon -s | grep -q $LLOOP; then
4803                         swapoff $LLOOP || error "swapoff failed"
4804                 fi
4805
4806                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4807                 rm -f $LLOOP
4808                 unset LLOOP
4809         fi
4810         if [ ! -z "$LLITELOOPLOAD" ]; then
4811                 rmmod llite_lloop
4812                 unset LLITELOOPLOAD
4813         fi
4814         rm -f $DIR/f68*
4815 }
4816
4817 meminfo() {
4818         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4819 }
4820
4821 swap_used() {
4822         swapon -s | awk '($1 == "'$1'") { print $4 }'
4823 }
4824
4825 # test case for lloop driver, basic function
4826 test_68a() {
4827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4828         [ "$UID" != 0 ] && skip_env "must run as root" && return
4829         llite_lloop_enabled || \
4830                 { skip_env "llite_lloop module disabled" && return; }
4831
4832         trap cleanup_68 EXIT
4833
4834         if ! module_loaded llite_lloop; then
4835                 if load_module llite/llite_lloop; then
4836                         LLITELOOPLOAD=yes
4837                 else
4838                         skip_env "can't find module llite_lloop"
4839                         return
4840                 fi
4841         fi
4842
4843         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4844         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4845         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4846
4847         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4848         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4849
4850         cleanup_68
4851 }
4852 run_test 68a "lloop driver - basic test ========================"
4853
4854 # excercise swapping to lustre by adding a high priority swapfile entry
4855 # and then consuming memory until it is used.
4856 test_68b() {  # was test_68
4857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4858         [ "$UID" != 0 ] && skip_env "must run as root" && return
4859         lctl get_param -n devices | grep -q obdfilter && \
4860                 skip "local OST" && return
4861
4862         grep -q llite_lloop /proc/modules
4863         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4864
4865         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4866                 skip "can't reliably test swap with TCP" && return
4867
4868         MEMTOTAL=`meminfo MemTotal`
4869         NR_BLOCKS=$((MEMTOTAL>>8))
4870         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4871
4872         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4873         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4874         mkswap $DIR/f68b
4875
4876         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4877
4878         trap cleanup_68 EXIT
4879
4880         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4881
4882         echo "before: `swapon -s | grep $LLOOP`"
4883         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4884         echo "after: `swapon -s | grep $LLOOP`"
4885         SWAPUSED=`swap_used $LLOOP`
4886
4887         cleanup_68
4888
4889         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4890 }
4891 run_test 68b "support swapping to Lustre ========================"
4892
4893 # bug5265, obdfilter oa2dentry return -ENOENT
4894 # #define OBD_FAIL_OST_ENOENT 0x217
4895 test_69() {
4896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4897         remote_ost_nodsh && skip "remote OST with nodsh" && return
4898
4899         f="$DIR/$tfile"
4900         $SETSTRIPE -c 1 -i 0 $f
4901
4902         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4903
4904         do_facet ost1 lctl set_param fail_loc=0x217
4905         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4906         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4907
4908         do_facet ost1 lctl set_param fail_loc=0
4909         $DIRECTIO write $f 0 2 || error "write error"
4910
4911         cancel_lru_locks osc
4912         $DIRECTIO read $f 0 1 || error "read error"
4913
4914         do_facet ost1 lctl set_param fail_loc=0x217
4915         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4916
4917         do_facet ost1 lctl set_param fail_loc=0
4918         rm -f $f
4919 }
4920 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4921
4922 test_71() {
4923     test_mkdir -p $DIR/$tdir
4924     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4925 }
4926 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4927
4928 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4930         check_kernel_version 43 || return 0
4931         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4932
4933         # Check that testing environment is properly set up. Skip if not
4934         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4935                 skip_env "User $RUNAS_ID does not exist - skipping"
4936                 return 0
4937         }
4938         # We had better clear the $DIR to get enough space for dd
4939         rm -rf $DIR/*
4940         touch $DIR/f72
4941         chmod 777 $DIR/f72
4942         chmod ug+s $DIR/f72
4943         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4944         # See if we are still setuid/sgid
4945         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4946         # Now test that MDS is updated too
4947         cancel_lru_locks mdc
4948         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4949         rm -f $DIR/f72
4950 }
4951 run_test 72a "Test that remove suid works properly (bug5695) ===="
4952
4953 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4954         local perm
4955
4956         [ "$RUNAS_ID" = "$UID" ] && \
4957                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4958         [ "$RUNAS_ID" -eq 0 ] && \
4959                 skip_env "RUNAS_ID = 0 -- skipping" && return
4960
4961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4962         # Check that testing environment is properly set up. Skip if not
4963         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4964                 skip_env "User $RUNAS_ID does not exist - skipping"
4965                 return 0
4966         }
4967         touch $DIR/${tfile}-f{g,u}
4968         test_mkdir $DIR/${tfile}-dg
4969         test_mkdir $DIR/${tfile}-du
4970         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4971         chmod g+s $DIR/${tfile}-{f,d}g
4972         chmod u+s $DIR/${tfile}-{f,d}u
4973         for perm in 777 2777 4777; do
4974                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4975                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4976                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4977                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4978         done
4979         true
4980 }
4981 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4982
4983 # bug 3462 - multiple simultaneous MDC requests
4984 test_73() {
4985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4986         test_mkdir $DIR/d73-1
4987         test_mkdir $DIR/d73-2
4988         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4989         pid1=$!
4990
4991         lctl set_param fail_loc=0x80000129
4992         $MULTIOP $DIR/d73-1/f73-2 Oc &
4993         sleep 1
4994         lctl set_param fail_loc=0
4995
4996         $MULTIOP $DIR/d73-2/f73-3 Oc &
4997         pid3=$!
4998
4999         kill -USR1 $pid1
5000         wait $pid1 || return 1
5001
5002         sleep 25
5003
5004         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5005         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5006         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5007
5008         rm -rf $DIR/d73-*
5009 }
5010 run_test 73 "multiple MDC requests (should not deadlock)"
5011
5012 test_74a() { # bug 6149, 6184
5013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5014         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5015         #
5016         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5017         # will spin in a tight reconnection loop
5018         touch $DIR/f74a
5019         lctl set_param fail_loc=0x8000030e
5020         # get any lock that won't be difficult - lookup works.
5021         ls $DIR/f74a
5022         lctl set_param fail_loc=0
5023         true
5024         rm -f $DIR/f74a
5025 }
5026 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5027
5028 test_74b() { # bug 13310
5029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5030         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5031         #
5032         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5033         # will spin in a tight reconnection loop
5034         lctl set_param fail_loc=0x8000030e
5035         # get a "difficult" lock
5036         touch $DIR/f74b
5037         lctl set_param fail_loc=0
5038         true
5039         rm -f $DIR/f74b
5040 }
5041 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5042
5043 test_74c() {
5044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5045 #define OBD_FAIL_LDLM_NEW_LOCK
5046         lctl set_param fail_loc=0x80000319
5047         touch $DIR/$tfile && error "Touch successful"
5048         true
5049 }
5050 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5051
5052 num_inodes() {
5053         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5054 }
5055
5056 get_inode_slab_tunables() {
5057         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5058 }
5059
5060 set_inode_slab_tunables() {
5061         echo "lustre_inode_cache $1" > /proc/slabinfo
5062 }
5063
5064 test_76() { # Now for bug 20433, added originally in bug 1443
5065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5066         local SLAB_SETTINGS=`get_inode_slab_tunables`
5067         local CPUS=`getconf _NPROCESSORS_ONLN`
5068         # we cannot set limit below 1 which means 1 inode in each
5069         # per-cpu cache is still allowed
5070         set_inode_slab_tunables "1 1 0"
5071         cancel_lru_locks osc
5072         BEFORE_INODES=`num_inodes`
5073         echo "before inodes: $BEFORE_INODES"
5074         local COUNT=1000
5075         [ "$SLOW" = "no" ] && COUNT=100
5076         for i in `seq $COUNT`; do
5077                 touch $DIR/$tfile
5078                 rm -f $DIR/$tfile
5079         done
5080         cancel_lru_locks osc
5081         AFTER_INODES=`num_inodes`
5082         echo "after inodes: $AFTER_INODES"
5083         local wait=0
5084         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5085                 sleep 2
5086                 AFTER_INODES=`num_inodes`
5087                 wait=$((wait+2))
5088                 echo "wait $wait seconds inodes: $AFTER_INODES"
5089                 if [ $wait -gt 30 ]; then
5090                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5091                 fi
5092         done
5093         set_inode_slab_tunables "$SLAB_SETTINGS"
5094 }
5095 run_test 76 "confirm clients recycle inodes properly ===="
5096
5097
5098 export ORIG_CSUM=""
5099 set_checksums()
5100 {
5101         # Note: in sptlrpc modes which enable its own bulk checksum, the
5102         # original crc32_le bulk checksum will be automatically disabled,
5103         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5104         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5105         # In this case set_checksums() will not be no-op, because sptlrpc
5106         # bulk checksum will be enabled all through the test.
5107
5108         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5109         lctl set_param -n osc.*.checksums $1
5110         return 0
5111 }
5112
5113 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5114                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5115 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5116 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5117 set_checksum_type()
5118 {
5119         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5120         log "set checksum type to $1"
5121         return 0
5122 }
5123 F77_TMP=$TMP/f77-temp
5124 F77SZ=8
5125 setup_f77() {
5126         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5127                 error "error writing to $F77_TMP"
5128 }
5129
5130 test_77a() { # bug 10889
5131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5132         $GSS && skip "could not run with gss" && return
5133         [ ! -f $F77_TMP ] && setup_f77
5134         set_checksums 1
5135         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5136         set_checksums 0
5137         rm -f $DIR/$tfile
5138 }
5139 run_test 77a "normal checksum read/write operation ============="
5140
5141 test_77b() { # bug 10889
5142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5143         $GSS && skip "could not run with gss" && return
5144         [ ! -f $F77_TMP ] && setup_f77
5145         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5146         lctl set_param fail_loc=0x80000409
5147         set_checksums 1
5148         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
5149                 error "dd error: $?"
5150         lctl set_param fail_loc=0
5151         set_checksums 0
5152 }
5153 run_test 77b "checksum error on client write ===================="
5154
5155 test_77c() { # bug 10889
5156         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5157         $GSS && skip "could not run with gss" && return
5158         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
5159         set_checksums 1
5160         for algo in $CKSUM_TYPES; do
5161                 cancel_lru_locks osc
5162                 set_checksum_type $algo
5163                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5164                 lctl set_param fail_loc=0x80000408
5165                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
5166                 lctl set_param fail_loc=0
5167         done
5168         set_checksums 0
5169         set_checksum_type $ORIG_CSUM_TYPE
5170         rm -f $DIR/f77b
5171 }
5172 run_test 77c "checksum error on client read ==================="
5173
5174 test_77d() { # bug 10889
5175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5176         $GSS && skip "could not run with gss" && return
5177         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5178         lctl set_param fail_loc=0x80000409
5179         set_checksums 1
5180         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5181                 error "direct write: rc=$?"
5182         lctl set_param fail_loc=0
5183         set_checksums 0
5184 }
5185 run_test 77d "checksum error on OST direct write ==============="
5186
5187 test_77e() { # bug 10889
5188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5189         $GSS && skip "could not run with gss" && return
5190         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5191         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5192         lctl set_param fail_loc=0x80000408
5193         set_checksums 1
5194         cancel_lru_locks osc
5195         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5196                 error "direct read: rc=$?"
5197         lctl set_param fail_loc=0
5198         set_checksums 0
5199 }
5200 run_test 77e "checksum error on OST direct read ================"
5201
5202 test_77f() { # bug 10889
5203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5204         $GSS && skip "could not run with gss" && return
5205         set_checksums 1
5206         for algo in $CKSUM_TYPES; do
5207                 cancel_lru_locks osc
5208                 set_checksum_type $algo
5209                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5210                 lctl set_param fail_loc=0x409
5211                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5212                         error "direct write succeeded"
5213                 lctl set_param fail_loc=0
5214         done
5215         set_checksum_type $ORIG_CSUM_TYPE
5216         set_checksums 0
5217 }
5218 run_test 77f "repeat checksum error on write (expect error) ===="
5219
5220 test_77g() { # bug 10889
5221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5222         $GSS && skip "could not run with gss" && return
5223         remote_ost_nodsh && skip "remote OST with nodsh" && return
5224
5225         [ ! -f $F77_TMP ] && setup_f77
5226
5227         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5228         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5229         do_facet ost1 lctl set_param fail_loc=0x8000021a
5230         set_checksums 1
5231         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5232                 error "write error: rc=$?"
5233         do_facet ost1 lctl set_param fail_loc=0
5234         set_checksums 0
5235 }
5236 run_test 77g "checksum error on OST write ======================"
5237
5238 test_77h() { # bug 10889
5239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5240         $GSS && skip "could not run with gss" && return
5241         remote_ost_nodsh && skip "remote OST with nodsh" && return
5242
5243         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5244         cancel_lru_locks osc
5245         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5246         do_facet ost1 lctl set_param fail_loc=0x8000021b
5247         set_checksums 1
5248         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5249         do_facet ost1 lctl set_param fail_loc=0
5250         set_checksums 0
5251 }
5252 run_test 77h "checksum error on OST read ======================="
5253
5254 test_77i() { # bug 13805
5255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5256         $GSS && skip "could not run with gss" && return
5257         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5258         lctl set_param fail_loc=0x40b
5259         remount_client $MOUNT
5260         lctl set_param fail_loc=0
5261         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5262                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5263                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5264                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5265         done
5266         remount_client $MOUNT
5267 }
5268 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5269
5270 test_77j() { # bug 13805
5271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5272         $GSS && skip "could not run with gss" && return
5273         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5274         lctl set_param fail_loc=0x40c
5275         remount_client $MOUNT
5276         lctl set_param fail_loc=0
5277         sleep 2 # wait async osc connect to finish
5278         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5279                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5280                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5281                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5282         done
5283         remount_client $MOUNT
5284 }
5285 run_test 77j "client only supporting ADLER32 ===================="
5286
5287 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5288 rm -f $F77_TMP
5289 unset F77_TMP
5290
5291 test_78() { # bug 10901
5292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5293         remote_ost || { skip_env "local OST" && return; }
5294
5295         NSEQ=5
5296         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5297         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5298         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5299         echo "MemTotal: $MEMTOTAL"
5300 # reserve 256MB of memory for the kernel and other running processes,
5301 # and then take 1/2 of the remaining memory for the read/write buffers.
5302     if [ $MEMTOTAL -gt 512 ] ;then
5303         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5304     else
5305         # for those poor memory-starved high-end clusters...
5306         MEMTOTAL=$((MEMTOTAL / 2))
5307     fi
5308         echo "Mem to use for directio: $MEMTOTAL"
5309         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5310         [ $F78SIZE -gt 512 ] && F78SIZE=512
5311         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5312         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5313         echo "Smallest OST: $SMALLESTOST"
5314         [ $SMALLESTOST -lt 10240 ] && \
5315                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5316
5317         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5318                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5319
5320         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5321         echo "File size: $F78SIZE"
5322         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5323         for i in `seq 1 $NSEQ`
5324         do
5325                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5326                 echo directIO rdwr round $i of $NSEQ
5327                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5328         done
5329
5330         rm -f $DIR/$tfile
5331 }
5332 run_test 78 "handle large O_DIRECT writes correctly ============"
5333
5334 test_79() { # bug 12743
5335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5336         wait_delete_completed
5337
5338         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5339         BKFREE=$(calc_osc_kbytes kbytesfree)
5340         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5341
5342         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5343         DFTOTAL=`echo $STRING | cut -d, -f1`
5344         DFUSED=`echo $STRING  | cut -d, -f2`
5345         DFAVAIL=`echo $STRING | cut -d, -f3`
5346         DFFREE=$(($DFTOTAL - $DFUSED))
5347
5348         ALLOWANCE=$((64 * $OSTCOUNT))
5349
5350         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5351            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5352                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5353         fi
5354         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5355            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5356                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5357         fi
5358         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5359            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5360                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5361         fi
5362 }
5363 run_test 79 "df report consistency check ======================="
5364
5365 test_80() { # bug 10718
5366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5367         # relax strong synchronous semantics for slow backends like ZFS
5368         local soc="obdfilter.*.sync_on_lock_cancel"
5369         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5370         local hosts=
5371         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5372                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5373                           facet_active_host $host; done | sort -u)
5374                 do_nodes $hosts lctl set_param $soc=never
5375         fi
5376
5377         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5378         sync; sleep 1; sync
5379         local BEFORE=`date +%s`
5380         cancel_lru_locks osc
5381         local AFTER=`date +%s`
5382         local DIFF=$((AFTER-BEFORE))
5383         if [ $DIFF -gt 1 ] ; then
5384                 error "elapsed for 1M@1T = $DIFF"
5385         fi
5386
5387         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5388
5389         rm -f $DIR/$tfile
5390 }
5391 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5392
5393 test_81a() { # LU-456
5394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5395         remote_ost_nodsh && skip "remote OST with nodsh" && return
5396         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5397         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5398         do_facet ost1 lctl set_param fail_loc=0x80000228
5399
5400         # write should trigger a retry and success
5401         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5402         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5403         RC=$?
5404         if [ $RC -ne 0 ] ; then
5405                 error "write should success, but failed for $RC"
5406         fi
5407 }
5408 run_test 81a "OST should retry write when get -ENOSPC ==============="
5409
5410 test_81b() { # LU-456
5411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5412         remote_ost_nodsh && skip "remote OST with nodsh" && return
5413         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5414         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5415         do_facet ost1 lctl set_param fail_loc=0x228
5416
5417         # write should retry several times and return -ENOSPC finally
5418         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5419         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5420         RC=$?
5421         ENOSPC=28
5422         if [ $RC -ne $ENOSPC ] ; then
5423                 error "dd should fail for -ENOSPC, but succeed."
5424         fi
5425 }
5426 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5427
5428 test_82() { # LU-1031
5429         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5430         local gid1=14091995
5431         local gid2=16022000
5432
5433         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5434         local MULTIPID1=$!
5435         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5436         local MULTIPID2=$!
5437         kill -USR1 $MULTIPID2
5438         sleep 2
5439         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5440                 error "First grouplock does not block second one"
5441         else
5442                 echo "Second grouplock blocks first one"
5443         fi
5444         kill -USR1 $MULTIPID1
5445         wait $MULTIPID1
5446         wait $MULTIPID2
5447 }
5448 run_test 82 "Basic grouplock test ==============================="
5449
5450 test_99a() {
5451         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5452             return
5453         test_mkdir -p $DIR/d99cvsroot
5454         chown $RUNAS_ID $DIR/d99cvsroot
5455         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5456         cd $TMP
5457
5458         $RUNAS cvs -d $DIR/d99cvsroot init || error
5459         cd $oldPWD
5460 }
5461 run_test 99a "cvs init ========================================="
5462
5463 test_99b() {
5464         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5465         [ ! -d $DIR/d99cvsroot ] && test_99a
5466         cd /etc/init.d
5467         # some versions of cvs import exit(1) when asked to import links or
5468         # files they can't read.  ignore those files.
5469         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5470                         ! -perm +4 -printf '-I %f\n')
5471         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5472                 d99reposname vtag rtag
5473 }
5474 run_test 99b "cvs import ======================================="
5475
5476 test_99c() {
5477         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5478         [ ! -d $DIR/d99cvsroot ] && test_99b
5479         cd $DIR
5480         test_mkdir -p $DIR/d99reposname
5481         chown $RUNAS_ID $DIR/d99reposname
5482         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5483 }
5484 run_test 99c "cvs checkout ====================================="
5485
5486 test_99d() {
5487         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5488         [ ! -d $DIR/d99cvsroot ] && test_99c
5489         cd $DIR/d99reposname
5490         $RUNAS touch foo99
5491         $RUNAS cvs add -m 'addmsg' foo99
5492 }
5493 run_test 99d "cvs add =========================================="
5494
5495 test_99e() {
5496         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5497         [ ! -d $DIR/d99cvsroot ] && test_99c
5498         cd $DIR/d99reposname
5499         $RUNAS cvs update
5500 }
5501 run_test 99e "cvs update ======================================="
5502
5503 test_99f() {
5504         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5505         [ ! -d $DIR/d99cvsroot ] && test_99d
5506         cd $DIR/d99reposname
5507         $RUNAS cvs commit -m 'nomsg' foo99
5508     rm -fr $DIR/d99cvsroot
5509 }
5510 run_test 99f "cvs commit ======================================="
5511
5512 test_100() {
5513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5514         [ "$NETTYPE" = tcp ] || \
5515                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5516                         return ; }
5517
5518         remote_ost_nodsh && skip "remote OST with nodsh" && return
5519         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5520         remote_servers || \
5521                 { skip "useless for local single node setup" && return; }
5522
5523         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5524                 [ "$PROT" != "tcp" ] && continue
5525                 RPORT=$(echo $REMOTE | cut -d: -f2)
5526                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5527
5528                 rc=0
5529                 LPORT=`echo $LOCAL | cut -d: -f2`
5530                 if [ $LPORT -ge 1024 ]; then
5531                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5532                         netstat -tna
5533                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5534                 fi
5535         done
5536         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5537 }
5538 run_test 100 "check local port using privileged port ==========="
5539
5540 function get_named_value()
5541 {
5542     local tag
5543
5544     tag=$1
5545     while read ;do
5546         line=$REPLY
5547         case $line in
5548         $tag*)
5549             echo $line | sed "s/^$tag[ ]*//"
5550             break
5551             ;;
5552         esac
5553     done
5554 }
5555
5556 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5557                    awk '/^max_cached_mb/ { print $2 }')
5558
5559 cleanup_101a() {
5560         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5561         trap 0
5562 }
5563
5564 test_101a() {
5565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5566         local s
5567         local discard
5568         local nreads=10000
5569         local cache_limit=32
5570
5571         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5572         trap cleanup_101a EXIT
5573         $LCTL set_param -n llite.*.read_ahead_stats 0
5574         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5575
5576         #
5577         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5578         #
5579         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5580         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5581
5582         discard=0
5583         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5584                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5585                         discard=$(($discard + $s))
5586         done
5587         cleanup_101a
5588
5589         if [ $(($discard * 10)) -gt $nreads ] ;then
5590                 $LCTL get_param osc.*-osc*.rpc_stats
5591                 $LCTL get_param llite.*.read_ahead_stats
5592                 error "too many ($discard) discarded pages"
5593         fi
5594         rm -f $DIR/$tfile || true
5595 }
5596 run_test 101a "check read-ahead for random reads ================"
5597
5598 setup_test101bc() {
5599         test_mkdir -p $DIR/$tdir
5600         STRIPE_SIZE=1048576
5601         STRIPE_COUNT=$OSTCOUNT
5602         STRIPE_OFFSET=0
5603
5604         local list=$(comma_list $(osts_nodes))
5605         set_osd_param $list '' read_cache_enable 0
5606         set_osd_param $list '' writethrough_cache_enable 0
5607
5608         trap cleanup_test101bc EXIT
5609         # prepare the read-ahead file
5610         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5611
5612         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5613 }
5614
5615 cleanup_test101bc() {
5616         trap 0
5617         rm -rf $DIR/$tdir
5618         rm -f $DIR/$tfile
5619
5620         local list=$(comma_list $(osts_nodes))
5621         set_osd_param $list '' read_cache_enable 1
5622         set_osd_param $list '' writethrough_cache_enable 1
5623 }
5624
5625 calc_total() {
5626         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5627 }
5628
5629 ra_check_101() {
5630         local READ_SIZE=$1
5631         local STRIPE_SIZE=1048576
5632         local RA_INC=1048576
5633         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5634         local FILE_LENGTH=$((64*100))
5635         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5636                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5637         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5638                         get_named_value 'read but discarded' | \
5639                         cut -d" " -f1 | calc_total`
5640         if [ $DISCARD -gt $discard_limit ]; then
5641                 $LCTL get_param llite.*.read_ahead_stats
5642                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5643         else
5644                 echo "Read-ahead success for size ${READ_SIZE}"
5645         fi
5646 }
5647
5648 test_101b() {
5649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5650         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5651         local STRIPE_SIZE=1048576
5652         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5653         local FILE_LENGTH=$((STRIPE_SIZE*100))
5654         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5655         # prepare the read-ahead file
5656         setup_test101bc
5657         cancel_lru_locks osc
5658         for BIDX in 2 4 8 16 32 64 128 256
5659         do
5660                 local BSIZE=$((BIDX*4096))
5661                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5662                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5663                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5664                 $LCTL set_param -n llite.*.read_ahead_stats 0
5665                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5666                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5667                 cancel_lru_locks osc
5668                 ra_check_101 $BSIZE
5669         done
5670         cleanup_test101bc
5671         true
5672 }
5673 run_test 101b "check stride-io mode read-ahead ================="
5674
5675 test_101c() {
5676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5677         local STRIPE_SIZE=1048576
5678         local FILE_LENGTH=$((STRIPE_SIZE*100))
5679         local nreads=10000
5680         local osc
5681
5682     setup_test101bc
5683
5684     cancel_lru_locks osc
5685     $LCTL set_param osc.*.rpc_stats 0
5686     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5687     for osc in $($LCTL get_param -N osc.*); do
5688         if [ "$osc" == "osc.num_refs" ]; then
5689             continue
5690         fi
5691
5692         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5693         if [ $lines -le 20 ]; then
5694             continue
5695         fi
5696
5697         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5698                                      awk '$1 == "1:" { print $2; exit; }')
5699         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5700                                      awk '$1 == "2:" { print $2; exit; }')
5701         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5702                                      awk '$1 == "4:" { print $2; exit; }')
5703         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5704                                      awk '$1 == "8:" { print $2; exit; }')
5705
5706         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5707         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5708         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5709         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5710         echo "${osc} rpc check passed!"
5711     done
5712     cleanup_test101bc
5713     true
5714 }
5715 run_test 101c "check stripe_size aligned read-ahead ================="
5716
5717 set_read_ahead() {
5718    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5719    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5720 }
5721
5722 test_101d() {
5723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5724         local file=$DIR/$tfile
5725         local size=${FILESIZE_101c:-500}
5726         local ra_MB=${READAHEAD_MB:-40}
5727
5728         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5729         [ $space -gt $((size * 1024)) ] ||
5730                 { skip "Need free space ${size}M, have $space" && return; }
5731
5732     echo Creating ${size}M test file $file
5733     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5734     echo Cancel LRU locks on lustre client to flush the client cache
5735     cancel_lru_locks osc
5736
5737     echo Disable read-ahead
5738     local old_READAHEAD=$(set_read_ahead 0)
5739
5740     echo Reading the test file $file with read-ahead disabled
5741     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5742
5743     echo Cancel LRU locks on lustre client to flush the client cache
5744     cancel_lru_locks osc
5745     echo Enable read-ahead with ${ra_MB}MB
5746     set_read_ahead $ra_MB
5747
5748     echo Reading the test file $file with read-ahead enabled
5749     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5750
5751     echo read-ahead disabled time read $time_ra_OFF
5752     echo read-ahead enabled  time read $time_ra_ON
5753
5754         set_read_ahead $old_READAHEAD
5755         rm -f $file
5756         wait_delete_completed
5757
5758     [ $time_ra_ON -lt $time_ra_OFF ] ||
5759         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5760                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5761 }
5762 run_test 101d "file read with and without read-ahead enabled  ================="
5763
5764 test_101e() {
5765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5766     local file=$DIR/$tfile
5767     local size=500  #KB
5768     local count=100
5769     local blksize=1024
5770
5771     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5772     local need_space=$((count * size))
5773     [ $space -gt $need_space ] ||
5774         { skip_env "Need free space $need_space, have $space" && return; }
5775
5776     echo Creating $count ${size}K test files
5777     for ((i = 0; i < $count; i++)); do
5778         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5779     done
5780
5781     echo Cancel LRU locks on lustre client to flush the client cache
5782     cancel_lru_locks osc
5783
5784     echo Reset readahead stats
5785     $LCTL set_param -n llite.*.read_ahead_stats 0
5786
5787     for ((i = 0; i < $count; i++)); do
5788         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5789     done
5790
5791     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5792           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5793
5794     for ((i = 0; i < $count; i++)); do
5795         rm -rf ${file}_${i} 2>/dev/null
5796     done
5797
5798     #10000 means 20% reads are missing in readahead
5799     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5800 }
5801 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5802
5803 cleanup_test101f() {
5804     trap 0
5805     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5806     rm -rf $DIR/$tfile 2>/dev/null
5807 }
5808
5809 test_101f() {
5810         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5811     local file=$DIR/$tfile
5812     local nreads=1000
5813
5814     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5815     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5816     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5817     trap cleanup_test101f EXIT
5818
5819     echo Cancel LRU locks on lustre client to flush the client cache
5820     cancel_lru_locks osc
5821
5822     echo Reset readahead stats
5823     $LCTL set_param -n llite.*.read_ahead_stats 0
5824     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5825     # readahead should read in 2M file on second read, so only miss
5826     # 2 pages.
5827     echo Random 4K reads on 2M file for 1000 times
5828     $READS -f $file -s 2097152 -b 4096 -n $nreads
5829
5830     echo checking missing pages
5831     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5832           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5833
5834     [ $miss -lt 3 ] || error "misses too much pages!"
5835     cleanup_test101f
5836 }
5837 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5838
5839 setup_test102() {
5840         test_mkdir -p $DIR/$tdir
5841         chown $RUNAS_ID $DIR/$tdir
5842         STRIPE_SIZE=65536
5843         STRIPE_OFFSET=1
5844         STRIPE_COUNT=$OSTCOUNT
5845         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5846
5847         trap cleanup_test102 EXIT
5848         cd $DIR
5849         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5850         cd $DIR/$tdir
5851         for num in 1 2 3 4; do
5852                 for count in $(seq 1 $STRIPE_COUNT); do
5853                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5854                                 local size=`expr $STRIPE_SIZE \* $num`
5855                                 local file=file"$num-$idx-$count"
5856                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5857                         done
5858                 done
5859         done
5860
5861         cd $DIR
5862         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5863 }
5864
5865 cleanup_test102() {
5866         trap 0
5867         rm -f $TMP/f102.tar
5868         rm -rf $DIR/d0.sanity/d102
5869 }
5870
5871 test_102a() {
5872         local testfile=$DIR/xattr_testfile
5873
5874         touch $testfile
5875
5876         [ "$UID" != 0 ] && skip_env "must run as root" && return
5877         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5878                 skip_env "must have user_xattr" && return
5879
5880         [ -z "$(which setfattr 2>/dev/null)" ] &&
5881                 skip_env "could not find setfattr" && return
5882
5883         echo "set/get xattr..."
5884         setfattr -n trusted.name1 -v value1 $testfile || error
5885         getfattr -n trusted.name1 $testfile 2> /dev/null |
5886           grep "trusted.name1=.value1" ||
5887                 error "$testfile missing trusted.name1=value1"
5888
5889         setfattr -n user.author1 -v author1 $testfile || error
5890         getfattr -n user.author1 $testfile 2> /dev/null |
5891           grep "user.author1=.author1" ||
5892                 error "$testfile missing trusted.author1=author1"
5893
5894         echo "listxattr..."
5895         setfattr -n trusted.name2 -v value2 $testfile ||
5896                 error "$testfile unable to set trusted.name2"
5897         setfattr -n trusted.name3 -v value3 $testfile ||
5898                 error "$testfile unable to set trusted.name3"
5899         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5900             grep "trusted.name" | wc -l) -eq 3 ] ||
5901                 error "$testfile missing 3 trusted.name xattrs"
5902
5903         setfattr -n user.author2 -v author2 $testfile ||
5904                 error "$testfile unable to set user.author2"
5905         setfattr -n user.author3 -v author3 $testfile ||
5906                 error "$testfile unable to set user.author3"
5907         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5908             grep "user.author" | wc -l) -eq 3 ] ||
5909                 error "$testfile missing 3 user.author xattrs"
5910
5911         echo "remove xattr..."
5912         setfattr -x trusted.name1 $testfile ||
5913                 error "$testfile error deleting trusted.name1"
5914         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5915                 error "$testfile did not delete trusted.name1 xattr"
5916
5917         setfattr -x user.author1 $testfile ||
5918                 error "$testfile error deleting user.author1"
5919         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5920                 error "$testfile did not delete trusted.name1 xattr"
5921
5922         # b10667: setting lustre special xattr be silently discarded
5923         echo "set lustre special xattr ..."
5924         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5925                 error "$testfile allowed setting trusted.lov"
5926 }
5927 run_test 102a "user xattr test =================================="
5928
5929 test_102b() {
5930         # b10930: get/set/list trusted.lov xattr
5931         echo "get/set/list trusted.lov xattr ..."
5932         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5933         local testfile=$DIR/$tfile
5934         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5935                 error "setstripe failed"
5936         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5937                 error "getstripe failed"
5938         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5939         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5940
5941         local testfile2=${testfile}2
5942         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5943                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5944
5945         $MCREATE $testfile2
5946         setfattr -n trusted.lov -v $value $testfile2
5947         local stripe_size=$($GETSTRIPE -S $testfile2)
5948         local stripe_count=$($GETSTRIPE -c $testfile2)
5949         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5950         [ $stripe_count -eq $STRIPECOUNT ] ||
5951                 error "stripe count $stripe_count != $STRIPECOUNT"
5952         rm -f $DIR/$tfile
5953 }
5954 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5955
5956 test_102c() {
5957         # b10930: get/set/list lustre.lov xattr
5958         echo "get/set/list lustre.lov xattr ..."
5959         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5960         test_mkdir -p $DIR/$tdir
5961         chown $RUNAS_ID $DIR/$tdir
5962         local testfile=$DIR/$tdir/$tfile
5963         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5964                 error "setstripe failed"
5965         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5966                 error "getstripe failed"
5967         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5968         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5969
5970         local testfile2=${testfile}2
5971         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5972                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5973
5974         $RUNAS $MCREATE $testfile2
5975         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5976         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5977         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5978         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5979         [ $stripe_count -eq $STRIPECOUNT ] ||
5980                 error "stripe count $stripe_count != $STRIPECOUNT"
5981 }
5982 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5983
5984 compare_stripe_info1() {
5985         local stripe_index_all_zero=true
5986
5987         for num in 1 2 3 4; do
5988                 for count in $(seq 1 $STRIPE_COUNT); do
5989                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5990                                 local size=$((STRIPE_SIZE * num))
5991                                 local file=file"$num-$offset-$count"
5992                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5993                                 [ $stripe_size -ne $size ] &&
5994                                     error "$file: size $stripe_size != $size"
5995                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5996                                 # allow fewer stripes to be created, ORI-601
5997                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5998                                     error "$file: count $stripe_count != $count"
5999                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6000                                 [ $stripe_index -ne 0 ] &&
6001                                         stripe_index_all_zero=false
6002                         done
6003                 done
6004         done
6005         $stripe_index_all_zero &&
6006                 error "all files are being extracted starting from OST index 0"
6007         return 0
6008 }
6009
6010 find_lustre_tar() {
6011         [ -n "$(which tar 2>/dev/null)" ] &&
6012                 strings $(which tar) | grep -q "lustre" && echo tar
6013 }
6014
6015 test_102d() {
6016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6017         # b10930: tar test for trusted.lov xattr
6018         TAR=$(find_lustre_tar)
6019         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6020         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6021         setup_test102
6022         test_mkdir -p $DIR/d102d
6023         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6024         cd $DIR/d102d/$tdir
6025         compare_stripe_info1
6026 }
6027 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6028
6029 test_102f() {
6030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6031         # b10930: tar test for trusted.lov xattr
6032         TAR=$(find_lustre_tar)
6033         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6034         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6035         setup_test102
6036         test_mkdir -p $DIR/d102f
6037         cd $DIR
6038         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6039         cd $DIR/d102f/$tdir
6040         compare_stripe_info1
6041 }
6042 run_test 102f "tar copy files, not keep osts ==========="
6043
6044 grow_xattr() {
6045         local xsize=${1:-1024}  # in bytes
6046         local file=$DIR/$tfile
6047
6048         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6049                 skip "must have user_xattr" && return 0
6050         [ -z "$(which setfattr 2>/dev/null)" ] &&
6051                 skip_env "could not find setfattr" && return 0
6052         [ -z "$(which getfattr 2>/dev/null)" ] &&
6053                 skip_env "could not find getfattr" && return 0
6054
6055         touch $file
6056
6057         local value="$(generate_string $xsize)"
6058
6059         local xbig=trusted.big
6060         log "save $xbig on $file"
6061         setfattr -n $xbig -v $value $file ||
6062                 error "saving $xbig on $file failed"
6063
6064         local orig=$(get_xattr_value $xbig $file)
6065         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6066
6067         local xsml=trusted.sml
6068         log "save $xsml on $file"
6069         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6070
6071         local new=$(get_xattr_value $xbig $file)
6072         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6073
6074         log "grow $xsml on $file"
6075         setfattr -n $xsml -v "$value" $file ||
6076                 error "growing $xsml on $file failed"
6077
6078         new=$(get_xattr_value $xbig $file)
6079         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6080         log "$xbig still valid after growing $xsml"
6081
6082         rm -f $file
6083 }
6084
6085 test_102h() { # bug 15777
6086         grow_xattr 1024
6087 }
6088 run_test 102h "grow xattr from inside inode to external block"
6089
6090 test_102ha() {
6091         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6092         grow_xattr $(max_xattr_size)
6093 }
6094 run_test 102ha "grow xattr from inside inode to external inode"
6095
6096 test_102i() { # bug 17038
6097         touch $DIR/$tfile
6098         ln -s $DIR/$tfile $DIR/${tfile}link
6099         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6100         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"
6101         rm -f $DIR/$tfile $DIR/${tfile}link
6102 }
6103 run_test 102i "lgetxattr test on symbolic link ============"
6104
6105 test_102j() {
6106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6107         TAR=$(find_lustre_tar)
6108         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6109         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6110         setup_test102 "$RUNAS"
6111         test_mkdir -p $DIR/d102j
6112         chown $RUNAS_ID $DIR/d102j
6113         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6114         cd $DIR/d102j/$tdir
6115         compare_stripe_info1 "$RUNAS"
6116 }
6117 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6118
6119 test_102k() {
6120         touch $DIR/$tfile
6121         # b22187 just check that does not crash for regular file.
6122         setfattr -n trusted.lov $DIR/$tfile
6123         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6124         local test_kdir=$DIR/d102k
6125         test_mkdir $test_kdir
6126         local default_size=`$GETSTRIPE -S $test_kdir`
6127         local default_count=`$GETSTRIPE -c $test_kdir`
6128         local default_offset=`$GETSTRIPE -i $test_kdir`
6129         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
6130                 error 'dir setstripe failed'
6131         setfattr -n trusted.lov $test_kdir
6132         local stripe_size=`$GETSTRIPE -S $test_kdir`
6133         local stripe_count=`$GETSTRIPE -c $test_kdir`
6134         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6135         [ $stripe_size -eq $default_size ] ||
6136                 error "stripe size $stripe_size != $default_size"
6137         [ $stripe_count -eq $default_count ] ||
6138                 error "stripe count $stripe_count != $default_count"
6139         [ $stripe_offset -eq $default_offset ] ||
6140                 error "stripe offset $stripe_offset != $default_offset"
6141         rm -rf $DIR/$tfile $test_kdir
6142 }
6143 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6144
6145 test_102l() {
6146         # LU-532 trusted. xattr is invisible to non-root
6147         local testfile=$DIR/$tfile
6148
6149         touch $testfile
6150
6151         echo "listxattr as user..."
6152         chown $RUNAS_ID $testfile
6153         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6154             grep -q "trusted" &&
6155                 error "$testfile trusted xattrs are user visible"
6156
6157         return 0;
6158 }
6159 run_test 102l "listxattr filter test =================================="
6160
6161 cleanup_test102
6162
6163 run_acl_subtest()
6164 {
6165     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6166     return $?
6167 }
6168
6169 test_103 () {
6170     [ "$UID" != 0 ] && skip_env "must run as root" && return
6171     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6172     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
6173     $GSS && skip "could not run under gss" && return
6174
6175     declare -a identity_old
6176
6177         for num in $(seq $MDSCOUNT); do
6178                 switch_identity $num true || identity_old[$num]=$?
6179         done
6180
6181     SAVE_UMASK=`umask`
6182     umask 0022
6183     cd $DIR
6184
6185     echo "performing cp ..."
6186     run_acl_subtest cp || error
6187     echo "performing getfacl-noacl..."
6188     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6189     echo "performing misc..."
6190     run_acl_subtest misc || error  "misc test failed"
6191     echo "performing permissions..."
6192     run_acl_subtest permissions || error "permissions failed"
6193     echo "performing setfacl..."
6194     run_acl_subtest setfacl || error  "setfacl test failed"
6195
6196     # inheritance test got from HP
6197     echo "performing inheritance..."
6198     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6199     chmod +x make-tree || error "chmod +x failed"
6200     run_acl_subtest inheritance || error "inheritance test failed"
6201     rm -f make-tree
6202
6203         echo "LU-974 ignore umask when acl is enabled..."
6204         run_acl_subtest 974 || error "LU-974 test failed"
6205         if [ $MDSCOUNT -ge 2 ]; then
6206                 run_acl_subtest 974_remote ||
6207                         error "LU-974 test failed under remote dir"
6208         fi
6209
6210     echo "LU-2561 newly created file is same size as directory..."
6211     run_acl_subtest 2561 || error "LU-2561 test failed"
6212
6213     cd $SAVE_PWD
6214     umask $SAVE_UMASK
6215
6216         for num in $(seq $MDSCOUNT); do
6217                 if [ "${identity_old[$num]}" = 1 ]; then
6218                         switch_identity $num false || identity_old[$num]=$?
6219                 fi
6220         done
6221 }
6222 run_test 103 "acl test ========================================="
6223
6224 test_104a() {
6225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6226         touch $DIR/$tfile
6227         lfs df || error "lfs df failed"
6228         lfs df -ih || error "lfs df -ih failed"
6229         lfs df -h $DIR || error "lfs df -h $DIR failed"
6230         lfs df -i $DIR || error "lfs df -i $DIR failed"
6231         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6232         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6233
6234         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6235         lctl --device %$OSC deactivate
6236         lfs df || error "lfs df with deactivated OSC failed"
6237         lctl --device %$OSC activate
6238         # wait the osc back to normal
6239         wait_osc_import_state client ost FULL
6240
6241         lfs df || error "lfs df with reactivated OSC failed"
6242         rm -f $DIR/$tfile
6243 }
6244 run_test 104a "lfs df [-ih] [path] test ========================="
6245
6246 test_104b() {
6247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6248         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6249         chmod 666 /dev/obd
6250         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6251         if [ $denied_cnt -ne 0 ];
6252         then
6253                     error "lfs check servers test failed"
6254         fi
6255 }
6256 run_test 104b "$RUNAS lfs check servers test ===================="
6257
6258 test_105a() {
6259         # doesn't work on 2.4 kernels
6260         touch $DIR/$tfile
6261         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6262                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6263         else
6264                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6265         fi
6266         rm -f $DIR/$tfile
6267 }
6268 run_test 105a "flock when mounted without -o flock test ========"
6269
6270 test_105b() {
6271         touch $DIR/$tfile
6272         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6273                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6274         else
6275                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6276         fi
6277         rm -f $DIR/$tfile
6278 }
6279 run_test 105b "fcntl when mounted without -o flock test ========"
6280
6281 test_105c() {
6282         touch $DIR/$tfile
6283         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6284                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6285         else
6286                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6287         fi
6288         rm -f $DIR/$tfile
6289 }
6290 run_test 105c "lockf when mounted without -o flock test ========"
6291
6292 test_105d() { # bug 15924
6293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6294         test_mkdir -p $DIR/$tdir
6295         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6296                 skip "mount w/o flock enabled" && return
6297         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6298         $LCTL set_param fail_loc=0x80000315
6299         flocks_test 2 $DIR/$tdir
6300 }
6301 run_test 105d "flock race (should not freeze) ========"
6302
6303 test_105e() { # bug 22660 && 22040
6304         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6305                 skip "mount w/o flock enabled" && return
6306         touch $DIR/$tfile
6307         flocks_test 3 $DIR/$tfile
6308 }
6309 run_test 105e "Two conflicting flocks from same process ======="
6310
6311 test_106() { #bug 10921
6312         test_mkdir -p $DIR/$tdir
6313         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6314         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6315 }
6316 run_test 106 "attempt exec of dir followed by chown of that dir"
6317
6318 test_107() {
6319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6320         CDIR=`pwd`
6321         cd $DIR
6322
6323         local file=core
6324         rm -f $file
6325
6326         local save_pattern=$(sysctl -n kernel.core_pattern)
6327         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6328         sysctl -w kernel.core_pattern=$file
6329         sysctl -w kernel.core_uses_pid=0
6330
6331         ulimit -c unlimited
6332         sleep 60 &
6333         SLEEPPID=$!
6334
6335         sleep 1
6336
6337         kill -s 11 $SLEEPPID
6338         wait $SLEEPPID
6339         if [ -e $file ]; then
6340                 size=`stat -c%s $file`
6341                 [ $size -eq 0 ] && error "Fail to create core file $file"
6342         else
6343                 error "Fail to create core file $file"
6344         fi
6345         rm -f $file
6346         sysctl -w kernel.core_pattern=$save_pattern
6347         sysctl -w kernel.core_uses_pid=$save_uses_pid
6348         cd $CDIR
6349 }
6350 run_test 107 "Coredump on SIG"
6351
6352 test_110() {
6353         test_mkdir -p $DIR/$tdir
6354         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6355                 error "mkdir with 255 char failed"
6356         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6357                 error "mkdir with 256 char should fail, but did not"
6358         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6359                 error "create with 255 char failed"
6360         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6361                 error "create with 256 char should fail, but did not"
6362
6363         ls -l $DIR/$tdir
6364         rm -rf $DIR/$tdir
6365 }
6366 run_test 110 "filename length checking"
6367
6368 test_115() {
6369         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6370         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6371             cut -c11-20)
6372         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6373             return
6374         echo "Starting with $OSTIO_pre threads"
6375
6376         NUMTEST=20000
6377         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6378         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6379         echo "$NUMTEST creates/unlinks"
6380         test_mkdir -p $DIR/$tdir
6381         createmany -o $DIR/$tdir/$tfile $NUMTEST
6382         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6383
6384         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6385             cut -c11-20)
6386
6387         # don't return an error
6388         [ $OSTIO_post == $OSTIO_pre ] && echo \
6389             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6390             echo "This may be fine, depending on what ran before this test" &&
6391             echo "and how fast this system is." && return
6392
6393         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6394 }
6395 run_test 115 "verify dynamic thread creation===================="
6396
6397 free_min_max () {
6398         wait_delete_completed
6399         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6400         echo OST kbytes available: ${AVAIL[@]}
6401         MAXI=0; MAXV=${AVAIL[0]}
6402         MINI=0; MINV=${AVAIL[0]}
6403         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6404             #echo OST $i: ${AVAIL[i]}kb
6405             if [ ${AVAIL[i]} -gt $MAXV ]; then
6406                 MAXV=${AVAIL[i]}; MAXI=$i
6407             fi
6408             if [ ${AVAIL[i]} -lt $MINV ]; then
6409                 MINV=${AVAIL[i]}; MINI=$i
6410             fi
6411         done
6412         echo Min free space: OST $MINI: $MINV
6413         echo Max free space: OST $MAXI: $MAXV
6414 }
6415
6416 test_116a() { # was previously test_116()
6417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6418         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6419
6420         echo -n "Free space priority "
6421         lctl get_param -n lov.*-clilov-*.qos_prio_free
6422         declare -a AVAIL
6423         free_min_max
6424         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6425                 return
6426
6427         # generate uneven OSTs
6428         test_mkdir -p $DIR/$tdir/OST${MINI}
6429         declare -i FILL
6430         FILL=$(($MINV / 4))
6431         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6432         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6433         i=0
6434         while [ $FILL -gt 0 ]; do
6435             i=$(($i + 1))
6436             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6437             FILL=$(($FILL - 2048))
6438             echo -n .
6439         done
6440         FILL=$(($MINV / 4))
6441         sync
6442         sleep_maxage
6443
6444         free_min_max
6445         DIFF=$(($MAXV - $MINV))
6446         DIFF2=$(($DIFF * 100 / $MINV))
6447         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6448         if [ $DIFF2 -gt 20 ]; then
6449             echo "ok"
6450         else
6451             echo "failed - QOS mode won't be used"
6452             error_ignore "QOS imbalance criteria not met"
6453             return
6454         fi
6455
6456         MINI1=$MINI; MINV1=$MINV
6457         MAXI1=$MAXI; MAXV1=$MAXV
6458
6459         # now fill using QOS
6460         echo writing a bunch of files to QOS-assigned OSTs
6461         $SETSTRIPE -c 1 $DIR/$tdir
6462         i=0
6463         while [ $FILL -gt 0 ]; do
6464             i=$(($i + 1))
6465             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6466             FILL=$(($FILL - 200))
6467             echo -n .
6468         done
6469         echo "wrote $i 200k files"
6470         sync
6471         sleep_maxage
6472
6473         echo "Note: free space may not be updated, so measurements might be off"
6474         free_min_max
6475         DIFF2=$(($MAXV - $MINV))
6476         echo "free space delta: orig $DIFF final $DIFF2"
6477         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6478         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6479         echo "Wrote $DIFF to smaller OST $MINI1"
6480         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6481         echo "Wrote $DIFF2 to larger OST $MAXI1"
6482         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6483
6484         # Figure out which files were written where
6485         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6486                awk '/'$MINI1': / {print $2; exit}')
6487         echo $UUID
6488         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6489         echo "$MINC files created on smaller OST $MINI1"
6490         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6491                awk '/'$MAXI1': / {print $2; exit}')
6492         echo $UUID
6493         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6494         echo "$MAXC files created on larger OST $MAXI1"
6495         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6496         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6497
6498         rm -rf $DIR/$tdir
6499 }
6500 run_test 116a "stripe QOS: free space balance ==================="
6501
6502 test_116b() { # LU-2093
6503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6504 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6505         local old_rr
6506         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6507         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6508         mkdir -p $DIR/$tdir
6509         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6510         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6511         do_facet $SINGLEMDS lctl set_param fail_loc=0
6512         rm -rf $DIR/$tdir
6513         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6514 }
6515 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6516
6517 test_117() # bug 10891
6518 {
6519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6520         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6521         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6522         lctl set_param fail_loc=0x21e
6523         > $DIR/$tfile || error "truncate failed"
6524         lctl set_param fail_loc=0
6525         echo "Truncate succeeded."
6526         rm -f $DIR/$tfile
6527 }
6528 run_test 117 "verify fsfilt_extend =========="
6529
6530 NO_SLOW_RESENDCOUNT=4
6531 export OLD_RESENDCOUNT=""
6532 set_resend_count () {
6533         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6534         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6535         lctl set_param -n $PROC_RESENDCOUNT $1
6536         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6537 }
6538
6539 # for reduce test_118* time (b=14842)
6540 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6541
6542 # Reset async IO behavior after error case
6543 reset_async() {
6544         FILE=$DIR/reset_async
6545
6546         # Ensure all OSCs are cleared
6547         $SETSTRIPE -c -1 $FILE
6548         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6549         sync
6550         rm $FILE
6551 }
6552
6553 test_118a() #bug 11710
6554 {
6555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6556         reset_async
6557
6558         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6559         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6560         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6561
6562         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6563                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6564                 return 1;
6565         fi
6566         rm -f $DIR/$tfile
6567 }
6568 run_test 118a "verify O_SYNC works =========="
6569
6570 test_118b()
6571 {
6572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6573         remote_ost_nodsh && skip "remote OST with nodsh" && return
6574
6575         reset_async
6576
6577         #define OBD_FAIL_OST_ENOENT 0x217
6578         set_nodes_failloc "$(osts_nodes)" 0x217
6579         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6580         RC=$?
6581         set_nodes_failloc "$(osts_nodes)" 0
6582         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6583         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6584                     grep -c writeback)
6585
6586         if [[ $RC -eq 0 ]]; then
6587                 error "Must return error due to dropped pages, rc=$RC"
6588                 return 1;
6589         fi
6590
6591         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6592                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6593                 return 1;
6594         fi
6595
6596         echo "Dirty pages not leaked on ENOENT"
6597
6598         # Due to the above error the OSC will issue all RPCs syncronously
6599         # until a subsequent RPC completes successfully without error.
6600         $MULTIOP $DIR/$tfile Ow4096yc
6601         rm -f $DIR/$tfile
6602
6603         return 0
6604 }
6605 run_test 118b "Reclaim dirty pages on fatal error =========="
6606
6607 test_118c()
6608 {
6609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6610
6611         # for 118c, restore the original resend count, LU-1940
6612         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
6613                                 set_resend_count $OLD_RESENDCOUNT
6614         remote_ost_nodsh && skip "remote OST with nodsh" && return
6615
6616         reset_async
6617
6618         #define OBD_FAIL_OST_EROFS               0x216
6619         set_nodes_failloc "$(osts_nodes)" 0x216
6620
6621         # multiop should block due to fsync until pages are written
6622         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6623         MULTIPID=$!
6624         sleep 1
6625
6626         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6627                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6628         fi
6629
6630         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6631                     grep -c writeback)
6632         if [[ $WRITEBACK -eq 0 ]]; then
6633                 error "No page in writeback, writeback=$WRITEBACK"
6634         fi
6635
6636         set_nodes_failloc "$(osts_nodes)" 0
6637         wait $MULTIPID
6638         RC=$?
6639         if [[ $RC -ne 0 ]]; then
6640                 error "Multiop fsync failed, rc=$RC"
6641         fi
6642
6643         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6644         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6645                     grep -c writeback)
6646         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6647                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6648         fi
6649
6650         rm -f $DIR/$tfile
6651         echo "Dirty pages flushed via fsync on EROFS"
6652         return 0
6653 }
6654 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6655
6656 # continue to use small resend count to reduce test_118* time (b=14842)
6657 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6658
6659 test_118d()
6660 {
6661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6662         remote_ost_nodsh && skip "remote OST with nodsh" && return
6663
6664         reset_async
6665
6666         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6667         set_nodes_failloc "$(osts_nodes)" 0x214
6668         # multiop should block due to fsync until pages are written
6669         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6670         MULTIPID=$!
6671         sleep 1
6672
6673         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6674                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6675         fi
6676
6677         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6678                     grep -c writeback)
6679         if [[ $WRITEBACK -eq 0 ]]; then
6680                 error "No page in writeback, writeback=$WRITEBACK"
6681         fi
6682
6683         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6684         set_nodes_failloc "$(osts_nodes)" 0
6685
6686         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6687         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6688                     grep -c writeback)
6689         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6690                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6691         fi
6692
6693         rm -f $DIR/$tfile
6694         echo "Dirty pages gaurenteed flushed via fsync"
6695         return 0
6696 }
6697 run_test 118d "Fsync validation inject a delay of the bulk =========="
6698
6699 test_118f() {
6700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6701         reset_async
6702
6703         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6704         lctl set_param fail_loc=0x8000040a
6705
6706         # Should simulate EINVAL error which is fatal
6707         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6708         RC=$?
6709         if [[ $RC -eq 0 ]]; then
6710                 error "Must return error due to dropped pages, rc=$RC"
6711         fi
6712
6713         lctl set_param fail_loc=0x0
6714
6715         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6716         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6717         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6718                     grep -c writeback)
6719         if [[ $LOCKED -ne 0 ]]; then
6720                 error "Locked pages remain in cache, locked=$LOCKED"
6721         fi
6722
6723         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6724                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6725         fi
6726
6727         rm -f $DIR/$tfile
6728         echo "No pages locked after fsync"
6729
6730         reset_async
6731         return 0
6732 }
6733 run_test 118f "Simulate unrecoverable OSC side error =========="
6734
6735 test_118g() {
6736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6737         reset_async
6738
6739         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6740         lctl set_param fail_loc=0x406
6741
6742         # simulate local -ENOMEM
6743         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6744         RC=$?
6745
6746         lctl set_param fail_loc=0
6747         if [[ $RC -eq 0 ]]; then
6748                 error "Must return error due to dropped pages, rc=$RC"
6749         fi
6750
6751         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6752         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6753         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6754                         grep -c writeback)
6755         if [[ $LOCKED -ne 0 ]]; then
6756                 error "Locked pages remain in cache, locked=$LOCKED"
6757         fi
6758
6759         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6760                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6761         fi
6762
6763         rm -f $DIR/$tfile
6764         echo "No pages locked after fsync"
6765
6766         reset_async
6767         return 0
6768 }
6769 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6770
6771 test_118h() {
6772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6773         remote_ost_nodsh && skip "remote OST with nodsh" && return
6774
6775         reset_async
6776
6777         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6778         set_nodes_failloc "$(osts_nodes)" 0x20e
6779         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6780         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6781         RC=$?
6782
6783         set_nodes_failloc "$(osts_nodes)" 0
6784         if [[ $RC -eq 0 ]]; then
6785                 error "Must return error due to dropped pages, rc=$RC"
6786         fi
6787
6788         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6789         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6790         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6791                     grep -c writeback)
6792         if [[ $LOCKED -ne 0 ]]; then
6793                 error "Locked pages remain in cache, locked=$LOCKED"
6794         fi
6795
6796         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6797                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6798         fi
6799
6800         rm -f $DIR/$tfile
6801         echo "No pages locked after fsync"
6802
6803         return 0
6804 }
6805 run_test 118h "Verify timeout in handling recoverables errors  =========="
6806
6807 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6808
6809 test_118i() {
6810         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6811         remote_ost_nodsh && skip "remote OST with nodsh" && return
6812
6813         reset_async
6814
6815         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6816         set_nodes_failloc "$(osts_nodes)" 0x20e
6817
6818         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6819         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6820         PID=$!
6821         sleep 5
6822         set_nodes_failloc "$(osts_nodes)" 0
6823
6824         wait $PID
6825         RC=$?
6826         if [[ $RC -ne 0 ]]; then
6827                 error "got error, but should be not, rc=$RC"
6828         fi
6829
6830         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6831         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6832         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6833         if [[ $LOCKED -ne 0 ]]; then
6834                 error "Locked pages remain in cache, locked=$LOCKED"
6835         fi
6836
6837         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6838                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6839         fi
6840
6841         rm -f $DIR/$tfile
6842         echo "No pages locked after fsync"
6843
6844         return 0
6845 }
6846 run_test 118i "Fix error before timeout in recoverable error  =========="
6847
6848 [ "$SLOW" = "no" ] && set_resend_count 4
6849
6850 test_118j() {
6851         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6852         remote_ost_nodsh && skip "remote OST with nodsh" && return
6853
6854         reset_async
6855
6856         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6857         set_nodes_failloc "$(osts_nodes)" 0x220
6858
6859         # return -EIO from OST
6860         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6861         RC=$?
6862         set_nodes_failloc "$(osts_nodes)" 0x0
6863         if [[ $RC -eq 0 ]]; then
6864                 error "Must return error due to dropped pages, rc=$RC"
6865         fi
6866
6867         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6868         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6869         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6870         if [[ $LOCKED -ne 0 ]]; then
6871                 error "Locked pages remain in cache, locked=$LOCKED"
6872         fi
6873
6874         # in recoverable error on OST we want resend and stay until it finished
6875         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6876                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6877         fi
6878
6879         rm -f $DIR/$tfile
6880         echo "No pages locked after fsync"
6881
6882         return 0
6883 }
6884 run_test 118j "Simulate unrecoverable OST side error =========="
6885
6886 test_118k()
6887 {
6888         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6889         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6890
6891         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6892         set_nodes_failloc "$(osts_nodes)" 0x20e
6893         test_mkdir -p $DIR/$tdir
6894
6895         for ((i=0;i<10;i++)); do
6896                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6897                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6898                 SLEEPPID=$!
6899                 sleep 0.500s
6900                 kill $SLEEPPID
6901                 wait $SLEEPPID
6902         done
6903
6904         set_nodes_failloc "$(osts_nodes)" 0
6905         rm -rf $DIR/$tdir
6906 }
6907 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6908
6909 test_118l()
6910 {
6911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6912         # LU-646
6913         test_mkdir -p $DIR/$tdir
6914         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6915         rm -rf $DIR/$tdir
6916 }
6917 run_test 118l "fsync dir ========="
6918
6919 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6920
6921 test_119a() # bug 11737
6922 {
6923         BSIZE=$((512 * 1024))
6924         directio write $DIR/$tfile 0 1 $BSIZE
6925         # We ask to read two blocks, which is more than a file size.
6926         # directio will indicate an error when requested and actual
6927         # sizes aren't equeal (a normal situation in this case) and
6928         # print actual read amount.
6929         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6930         if [ "$NOB" != "$BSIZE" ]; then
6931                 error "read $NOB bytes instead of $BSIZE"
6932         fi
6933         rm -f $DIR/$tfile
6934 }
6935 run_test 119a "Short directIO read must return actual read amount"
6936
6937 test_119b() # bug 11737
6938 {
6939         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6940
6941         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6942         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6943         sync
6944         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6945                 error "direct read failed"
6946         rm -f $DIR/$tfile
6947 }
6948 run_test 119b "Sparse directIO read must return actual read amount"
6949
6950 test_119c() # bug 13099
6951 {
6952         BSIZE=1048576
6953         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6954         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6955         rm -f $DIR/$tfile
6956 }
6957 run_test 119c "Testing for direct read hitting hole"
6958
6959 test_119d() # bug 15950
6960 {
6961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6962         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6963         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6964         BSIZE=1048576
6965         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6966         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6967         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6968         lctl set_param fail_loc=0x40d
6969         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6970         pid_dio=$!
6971         sleep 1
6972         cat $DIR/$tfile > /dev/null &
6973         lctl set_param fail_loc=0
6974         pid_reads=$!
6975         wait $pid_dio
6976         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6977         sleep 2
6978         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6979         error "the read rpcs have not completed in 2s"
6980         rm -f $DIR/$tfile
6981         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6982 }
6983 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6984
6985 test_120a() {
6986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6987         test_mkdir -p $DIR/$tdir
6988         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6989                skip "no early lock cancel on server" && return 0
6990         lru_resize_disable mdc
6991         lru_resize_disable osc
6992         cancel_lru_locks mdc
6993         stat $DIR/$tdir > /dev/null
6994         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6995         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6996         test_mkdir $DIR/$tdir/d1
6997         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6998         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6999         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7000         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7001         lru_resize_enable mdc
7002         lru_resize_enable osc
7003 }
7004 run_test 120a "Early Lock Cancel: mkdir test"
7005
7006 test_120b() {
7007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7008         test_mkdir -p $DIR/$tdir
7009         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7010                skip "no early lock cancel on server" && return 0
7011         lru_resize_disable mdc
7012         lru_resize_disable osc
7013         cancel_lru_locks mdc
7014         stat $DIR/$tdir > /dev/null
7015         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7016         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7017         touch $DIR/$tdir/f1
7018         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7019         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7020         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7021         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7022         lru_resize_enable mdc
7023         lru_resize_enable osc
7024 }
7025 run_test 120b "Early Lock Cancel: create test"
7026
7027 test_120c() {
7028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7029         test_mkdir -p $DIR/$tdir
7030         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7031                skip "no early lock cancel on server" && return 0
7032         lru_resize_disable mdc
7033         lru_resize_disable osc
7034         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7035         touch $DIR/$tdir/d1/f1
7036         cancel_lru_locks mdc
7037         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7038         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7039         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7040         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7041         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7042         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | 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 120c "Early Lock Cancel: link test"
7049
7050 test_120d() {
7051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7052         test_mkdir -p $DIR/$tdir
7053         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7054                skip "no early lock cancel on server" && return 0
7055         lru_resize_disable mdc
7056         lru_resize_disable osc
7057         touch $DIR/$tdir
7058         cancel_lru_locks mdc
7059         stat $DIR/$tdir > /dev/null
7060         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7061         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7062         chmod a+x $DIR/$tdir
7063         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7064         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7065         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7066         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7067         lru_resize_enable mdc
7068         lru_resize_enable osc
7069 }
7070 run_test 120d "Early Lock Cancel: setattr test"
7071
7072 test_120e() {
7073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7074         test_mkdir -p $DIR/$tdir
7075         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7076                skip "no early lock cancel on server" && return 0
7077         lru_resize_disable mdc
7078         lru_resize_disable osc
7079         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7080         cancel_lru_locks mdc
7081         cancel_lru_locks osc
7082         dd if=$DIR/$tdir/f1 of=/dev/null
7083         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7084         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7085               awk '/ldlm_cancel/ {print $2}'`
7086         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7087               awk '/ldlm_bl_callback/ {print $2}'`
7088         unlink $DIR/$tdir/f1
7089         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7090               awk '/ldlm_cancel/ {print $2}'`
7091         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7092               awk '/ldlm_bl_callback/ {print $2}'`
7093         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7094         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7095         lru_resize_enable mdc
7096         lru_resize_enable osc
7097 }
7098 run_test 120e "Early Lock Cancel: unlink test"
7099
7100 test_120f() {
7101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7102         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7103                skip "no early lock cancel on server" && return 0
7104         test_mkdir -p $DIR/$tdir
7105         lru_resize_disable mdc
7106         lru_resize_disable osc
7107         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7108         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7109         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7110         cancel_lru_locks mdc
7111         cancel_lru_locks osc
7112         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7113         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7114         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7115         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7116               awk '/ldlm_cancel/ {print $2}'`
7117         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7118               awk '/ldlm_bl_callback/ {print $2}'`
7119         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7120         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7121               awk '/ldlm_cancel/ {print $2}'`
7122         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7123               awk '/ldlm_bl_callback/ {print $2}'`
7124         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7125         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7126         lru_resize_enable mdc
7127         lru_resize_enable osc
7128 }
7129 run_test 120f "Early Lock Cancel: rename test"
7130
7131 test_120g() {
7132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7133         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7134                skip "no early lock cancel on server" && return 0
7135         lru_resize_disable mdc
7136         lru_resize_disable osc
7137         count=10000
7138         echo create $count files
7139         test_mkdir -p $DIR/$tdir
7140         cancel_lru_locks mdc
7141         cancel_lru_locks osc
7142         t0=`date +%s`
7143
7144         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7145               awk '/ldlm_cancel/ {print $2}'`
7146         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7147               awk '/ldlm_bl_callback/ {print $2}'`
7148         createmany -o $DIR/$tdir/f $count
7149         sync
7150         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7151               awk '/ldlm_cancel/ {print $2}'`
7152         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7153               awk '/ldlm_bl_callback/ {print $2}'`
7154         t1=`date +%s`
7155         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7156         echo rm $count files
7157         rm -r $DIR/$tdir
7158         sync
7159         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7160               awk '/ldlm_cancel/ {print $2}'`
7161         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7162               awk '/ldlm_bl_callback/ {print $2}'`
7163         t2=`date +%s`
7164         echo total: $count removes in $((t2-t1))
7165         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7166         sleep 2
7167         # wait for commitment of removal
7168         lru_resize_enable mdc
7169         lru_resize_enable osc
7170 }
7171 run_test 120g "Early Lock Cancel: performance test"
7172
7173 test_121() { #bug #10589
7174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7175         rm -rf $DIR/$tfile
7176         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7177 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7178         lctl set_param fail_loc=0x310
7179         cancel_lru_locks osc > /dev/null
7180         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7181         lctl set_param fail_loc=0
7182         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7183 }
7184 run_test 121 "read cancel race ========="
7185
7186 test_123a() { # was test 123, statahead(bug 11401)
7187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7188         SLOWOK=0
7189         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7190             log "testing on UP system. Performance may be not as good as expected."
7191                         SLOWOK=1
7192         fi
7193
7194         rm -rf $DIR/$tdir
7195         test_mkdir -p $DIR/$tdir
7196         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7197         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7198         MULT=10
7199         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7200                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7201
7202                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7203                 lctl set_param -n llite.*.statahead_max 0
7204                 lctl get_param llite.*.statahead_max
7205                 cancel_lru_locks mdc
7206                 cancel_lru_locks osc
7207                 stime=`date +%s`
7208                 time ls -l $DIR/$tdir | wc -l
7209                 etime=`date +%s`
7210                 delta=$((etime - stime))
7211                 log "ls $i files without statahead: $delta sec"
7212                 lctl set_param llite.*.statahead_max=$max
7213
7214                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7215                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7216                 cancel_lru_locks mdc
7217                 cancel_lru_locks osc
7218                 stime=`date +%s`
7219                 time ls -l $DIR/$tdir | wc -l
7220                 etime=`date +%s`
7221                 delta_sa=$((etime - stime))
7222                 log "ls $i files with statahead: $delta_sa sec"
7223                 lctl get_param -n llite.*.statahead_stats
7224                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7225
7226                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7227                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7228
7229                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7230                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7231                     lctl set_param -n llite.*.statahead_max 0
7232                     lctl get_param llite.*.statahead_max
7233                     cancel_lru_locks mdc
7234                     cancel_lru_locks osc
7235                     stime=`date +%s`
7236                     time ls -l $DIR/$tdir | wc -l
7237                     etime=`date +%s`
7238                     delta=$((etime - stime))
7239                     log "ls $i files again without statahead: $delta sec"
7240                     lctl set_param llite.*.statahead_max=$max
7241                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7242                         if [  $SLOWOK -eq 0 ]; then
7243                                 error "ls $i files is slower with statahead!"
7244                         else
7245                                 log "ls $i files is slower with statahead!"
7246                         fi
7247                         break
7248                     fi
7249                 fi
7250
7251                 [ $delta -gt 20 ] && break
7252                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7253                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7254         done
7255         log "ls done"
7256
7257         stime=`date +%s`
7258         rm -r $DIR/$tdir
7259         sync
7260         etime=`date +%s`
7261         delta=$((etime - stime))
7262         log "rm -r $DIR/$tdir/: $delta seconds"
7263         log "rm done"
7264         lctl get_param -n llite.*.statahead_stats
7265 }
7266 run_test 123a "verify statahead work"
7267
7268 test_123b () { # statahead(bug 15027)
7269         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7270         test_mkdir -p $DIR/$tdir
7271         createmany -o $DIR/$tdir/$tfile-%d 1000
7272
7273         cancel_lru_locks mdc
7274         cancel_lru_locks osc
7275
7276 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7277         lctl set_param fail_loc=0x80000803
7278         ls -lR $DIR/$tdir > /dev/null
7279         log "ls done"
7280         lctl set_param fail_loc=0x0
7281         lctl get_param -n llite.*.statahead_stats
7282         rm -r $DIR/$tdir
7283         sync
7284
7285 }
7286 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7287
7288 test_124a() {
7289         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7290         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7291                skip "no lru resize on server" && return 0
7292         local NR=2000
7293         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7294
7295         log "create $NR files at $DIR/$tdir"
7296         createmany -o $DIR/$tdir/f $NR ||
7297                 error "failed to create $NR files in $DIR/$tdir"
7298
7299         cancel_lru_locks mdc
7300         ls -l $DIR/$tdir > /dev/null
7301
7302         local NSDIR=""
7303         local LRU_SIZE=0
7304         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7305                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7306                 LRU_SIZE=$(lctl get_param -n $PARAM)
7307                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7308                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7309                                                 log "NSDIR=$NSDIR"
7310                         log "NS=$(basename $NSDIR)"
7311                         break
7312                 fi
7313         done
7314
7315         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7316                 skip "Not enough cached locks created!"
7317                 return 0
7318         fi
7319         log "LRU=$LRU_SIZE"
7320
7321         local SLEEP=30
7322
7323         # We know that lru resize allows one client to hold $LIMIT locks
7324         # for 10h. After that locks begin to be killed by client.
7325         local MAX_HRS=10
7326         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7327                 log "LIMIT=$LIMIT"
7328
7329         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7330         # killing locks. Some time was spent for creating locks. This means
7331         # that up to the moment of sleep finish we must have killed some of
7332         # them (10-100 locks). This depends on how fast ther were created.
7333         # Many of them were touched in almost the same moment and thus will
7334         # be killed in groups.
7335         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7336
7337         # Use $LRU_SIZE_B here to take into account real number of locks
7338         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7339         local LRU_SIZE_B=$LRU_SIZE
7340         log "LVF=$LVF"
7341         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7342                 log "OLD_LVF=$OLD_LVF"
7343         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7344
7345         # Let's make sure that we really have some margin. Client checks
7346         # cached locks every 10 sec.
7347         SLEEP=$((SLEEP+20))
7348         log "Sleep ${SLEEP} sec"
7349         local SEC=0
7350         while ((SEC<$SLEEP)); do
7351                 echo -n "..."
7352                 sleep 5
7353                 SEC=$((SEC+5))
7354                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7355                 echo -n "$LRU_SIZE"
7356         done
7357         echo ""
7358         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7359         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7360
7361         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7362                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7363                 unlinkmany $DIR/$tdir/f $NR
7364                 return
7365         }
7366
7367         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7368         log "unlink $NR files at $DIR/$tdir"
7369         unlinkmany $DIR/$tdir/f $NR
7370 }
7371 run_test 124a "lru resize ======================================="
7372
7373 get_max_pool_limit()
7374 {
7375         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7376         local max=0
7377         for l in $limit; do
7378                 if test $l -gt $max; then
7379                         max=$l
7380                 fi
7381         done
7382         echo $max
7383 }
7384
7385 test_124b() {
7386         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7387         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7388                skip "no lru resize on server" && return 0
7389
7390         LIMIT=`get_max_pool_limit`
7391
7392         NR=$(($(default_lru_size)*20))
7393         if [ $NR -gt $LIMIT ]; then
7394                 log "Limit lock number by $LIMIT locks"
7395                 NR=$LIMIT
7396         fi
7397         lru_resize_disable mdc
7398         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7399                 error "failed to create $DIR/$tdir/disable_lru_resize"
7400
7401         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7402         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7403         cancel_lru_locks mdc
7404         stime=`date +%s`
7405         PID=""
7406         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7407         PID="$PID $!"
7408         sleep 2
7409         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7410         PID="$PID $!"
7411         sleep 2
7412         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7413         PID="$PID $!"
7414         wait $PID
7415         etime=`date +%s`
7416         nolruresize_delta=$((etime-stime))
7417         log "ls -la time: $nolruresize_delta seconds"
7418         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7419         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7420
7421         lru_resize_enable mdc
7422         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7423                 error "failed to create $DIR/$tdir/enable_lru_resize"
7424
7425         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7426         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7427         cancel_lru_locks mdc
7428         stime=`date +%s`
7429         PID=""
7430         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7431         PID="$PID $!"
7432         sleep 2
7433         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7434         PID="$PID $!"
7435         sleep 2
7436         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7437         PID="$PID $!"
7438         wait $PID
7439         etime=`date +%s`
7440         lruresize_delta=$((etime-stime))
7441         log "ls -la time: $lruresize_delta seconds"
7442         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7443
7444         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7445                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7446         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7447                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7448         else
7449                 log "lru resize performs the same with no lru resize"
7450         fi
7451         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7452 }
7453 run_test 124b "lru resize (performance test) ======================="
7454
7455 test_125() { # 13358
7456         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7457         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7458         test_mkdir -p $DIR/d125 || error "mkdir failed"
7459         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7460         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7461         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7462 }
7463 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7464
7465 test_126() { # bug 12829/13455
7466         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7467         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7468         $GSS && skip "must run as gss disabled" && return
7469
7470         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7471         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7472         rm -f $DIR/$tfile
7473         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7474 }
7475 run_test 126 "check that the fsgid provided by the client is taken into account"
7476
7477 test_127a() { # bug 15521
7478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7479         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7480         $LCTL set_param osc.*.stats=0
7481         FSIZE=$((2048 * 1024))
7482         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7483         cancel_lru_locks osc
7484         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7485
7486         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7487         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7488                 echo "got $COUNT $NAME"
7489                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7490                 eval $NAME=$COUNT || error "Wrong proc format"
7491
7492                 case $NAME in
7493                         read_bytes|write_bytes)
7494                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7495                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7496                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7497                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7498                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7499                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7500                                 error "sumsquare is too small: $SUMSQ"
7501                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7502                                 error "sumsquare is too big: $SUMSQ"
7503                         ;;
7504                         *) ;;
7505                 esac
7506         done < $DIR/${tfile}.tmp
7507
7508         #check that we actually got some stats
7509         [ "$read_bytes" ] || error "Missing read_bytes stats"
7510         [ "$write_bytes" ] || error "Missing write_bytes stats"
7511         [ "$read_bytes" != 0 ] || error "no read done"
7512         [ "$write_bytes" != 0 ] || error "no write done"
7513 }
7514 run_test 127a "verify the client stats are sane"
7515
7516 test_127b() { # bug LU-333
7517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7518         $LCTL set_param llite.*.stats=0
7519         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7520         # perform 2 reads and writes so MAX is different from SUM.
7521         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7522         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7523         cancel_lru_locks osc
7524         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7525         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7526
7527         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7528         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7529                 echo "got $COUNT $NAME"
7530                 eval $NAME=$COUNT || error "Wrong proc format"
7531
7532         case $NAME in
7533                 read_bytes)
7534                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7535                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7536                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7537                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7538                         ;;
7539                 write_bytes)
7540                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7541                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7542                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7543                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7544                         ;;
7545                         *) ;;
7546                 esac
7547         done < $TMP/${tfile}.tmp
7548
7549         #check that we actually got some stats
7550         [ "$read_bytes" ] || error "Missing read_bytes stats"
7551         [ "$write_bytes" ] || error "Missing write_bytes stats"
7552         [ "$read_bytes" != 0 ] || error "no read done"
7553         [ "$write_bytes" != 0 ] || error "no write done"
7554 }
7555 run_test 127b "verify the llite client stats are sane"
7556
7557 test_128() { # bug 15212
7558         touch $DIR/$tfile
7559         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7560                 find $DIR/$tfile
7561                 find $DIR/$tfile
7562         EOF
7563
7564         result=$(grep error $TMP/$tfile.log)
7565         rm -f $DIR/$tfile
7566         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7567 }
7568 run_test 128 "interactive lfs for 2 consecutive find's"
7569
7570 set_dir_limits () {
7571         local mntdev
7572         local canondev
7573         local node
7574
7575         local LDPROC=/proc/fs/ldiskfs
7576         local facets=$(get_facets MDS)
7577
7578         for facet in ${facets//,/ }; do
7579                 canondev=$(ldiskfs_canon \
7580                            *.$(convert_facet2label $facet).mntdev $facet)
7581                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7582                                                 LDPROC=/sys/fs/ldiskfs
7583                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7584         done
7585 }
7586
7587 test_129() {
7588         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7589         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7590                 skip "Only applicable to ldiskfs-based MDTs"
7591                 return
7592         fi
7593         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7594
7595         EFBIG=27
7596         MAX=16384
7597
7598         set_dir_limits $MAX
7599         test_mkdir -p $DIR/$tdir
7600
7601         local I=0
7602         local J=0
7603         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7604                 $MULTIOP $DIR/$tdir/$J Oc
7605                 rc=$?
7606                 if [ $rc -eq $EFBIG ]; then
7607                         set_dir_limits 0
7608                         echo "return code $rc received as expected"
7609                         return 0
7610                 elif [ $rc -ne 0 ]; then
7611                         set_dir_limits 0
7612                         error_exit "return code $rc received instead of expected $EFBIG"
7613                 fi
7614                 J=$((J+1))
7615                 I=$(stat -c%s "$DIR/$tdir")
7616         done
7617
7618         set_dir_limits 0
7619         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7620 }
7621 run_test 129 "test directory size limit ========================"
7622
7623 OLDIFS="$IFS"
7624 cleanup_130() {
7625         trap 0
7626         IFS="$OLDIFS"
7627 }
7628
7629 test_130a() {
7630         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7631         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7632                 return
7633
7634         trap cleanup_130 EXIT RETURN
7635
7636         local fm_file=$DIR/$tfile
7637         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7638         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7639                 error "dd failed for $fm_file"
7640
7641         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7642         filefrag -ves $fm_file
7643         RC=$?
7644         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7645                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7646         [ $RC != 0 ] && error "filefrag $fm_file failed"
7647
7648         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7649                       grep -v "ext:" | grep -v "found")
7650         lun=$($GETSTRIPE -i $fm_file)
7651
7652         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7653         IFS=$'\n'
7654         tot_len=0
7655         for line in $filefrag_op
7656         do
7657                 frag_lun=`echo $line | cut -d: -f5`
7658                 ext_len=`echo $line | cut -d: -f4`
7659                 if (( $frag_lun != $lun )); then
7660                         cleanup_130
7661                         error "FIEMAP on 1-stripe file($fm_file) failed"
7662                         return
7663                 fi
7664                 (( tot_len += ext_len ))
7665         done
7666
7667         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7668                 cleanup_130
7669                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7670                 return
7671         fi
7672
7673         cleanup_130
7674
7675         echo "FIEMAP on single striped file succeeded"
7676 }
7677 run_test 130a "FIEMAP (1-stripe file)"
7678
7679 test_130b() {
7680         [ "$OSTCOUNT" -lt "2" ] &&
7681                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7682
7683         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7684         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7685                 return
7686
7687         trap cleanup_130 EXIT RETURN
7688
7689         local fm_file=$DIR/$tfile
7690         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7691         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7692                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7693
7694         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7695                 error "dd failed on $fm_file"
7696
7697         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7698         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7699                       grep -v "ext:" | grep -v "found")
7700
7701         last_lun=$(echo $filefrag_op | cut -d: -f5)
7702
7703         IFS=$'\n'
7704         tot_len=0
7705         num_luns=1
7706         for line in $filefrag_op
7707         do
7708                 frag_lun=`echo $line | cut -d: -f5`
7709                 ext_len=`echo $line | cut -d: -f4`
7710                 if (( $frag_lun != $last_lun )); then
7711                         if (( tot_len != 1024 )); then
7712                                 cleanup_130
7713                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7714                                 return
7715                         else
7716                                 (( num_luns += 1 ))
7717                                 tot_len=0
7718                         fi
7719                 fi
7720                 (( tot_len += ext_len ))
7721                 last_lun=$frag_lun
7722         done
7723         if (( num_luns != 2 || tot_len != 1024 )); then
7724                 cleanup_130
7725                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7726                 return
7727         fi
7728
7729         cleanup_130
7730
7731         echo "FIEMAP on 2-stripe file succeeded"
7732 }
7733 run_test 130b "FIEMAP (2-stripe file)"
7734
7735 test_130c() {
7736         [ "$OSTCOUNT" -lt "2" ] &&
7737                 skip_env "skipping FIEMAP on 2-stripe file" && return
7738
7739         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7740         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7741                 return
7742
7743         trap cleanup_130 EXIT RETURN
7744
7745         local fm_file=$DIR/$tfile
7746         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7747         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7748                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7749
7750         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7751
7752         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7753         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7754
7755         last_lun=`echo $filefrag_op | cut -d: -f5`
7756
7757         IFS=$'\n'
7758         tot_len=0
7759         num_luns=1
7760         for line in $filefrag_op
7761         do
7762                 frag_lun=`echo $line | cut -d: -f5`
7763                 ext_len=`echo $line | cut -d: -f4`
7764                 if (( $frag_lun != $last_lun )); then
7765                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7766                         if (( logical != 512 )); then
7767                                 cleanup_130
7768                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7769                                 return
7770                         fi
7771                         if (( tot_len != 512 )); then
7772                                 cleanup_130
7773                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7774                                 return
7775                         else
7776                                 (( num_luns += 1 ))
7777                                 tot_len=0
7778                         fi
7779                 fi
7780                 (( tot_len += ext_len ))
7781                 last_lun=$frag_lun
7782         done
7783         if (( num_luns != 2 || tot_len != 512 )); then
7784                 cleanup_130
7785                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7786                 return
7787         fi
7788
7789         cleanup_130
7790
7791         echo "FIEMAP on 2-stripe file with hole succeeded"
7792 }
7793 run_test 130c "FIEMAP (2-stripe file with hole)"
7794
7795 test_130d() {
7796         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7797
7798         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7799         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7800
7801         trap cleanup_130 EXIT RETURN
7802
7803         local fm_file=$DIR/$tfile
7804         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7805         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7806                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7807         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7808
7809         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7810         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7811
7812         last_lun=`echo $filefrag_op | cut -d: -f5`
7813
7814         IFS=$'\n'
7815         tot_len=0
7816         num_luns=1
7817         for line in $filefrag_op
7818         do
7819                 frag_lun=`echo $line | cut -d: -f5`
7820                 ext_len=`echo $line | cut -d: -f4`
7821                 if (( $frag_lun != $last_lun )); then
7822                         if (( tot_len != 1024 )); then
7823                                 cleanup_130
7824                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7825                                 return
7826                         else
7827                                 (( num_luns += 1 ))
7828                                 tot_len=0
7829                         fi
7830                 fi
7831                 (( tot_len += ext_len ))
7832                 last_lun=$frag_lun
7833         done
7834         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7835                 cleanup_130
7836                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7837                 return
7838         fi
7839
7840         cleanup_130
7841
7842         echo "FIEMAP on N-stripe file succeeded"
7843 }
7844 run_test 130d "FIEMAP (N-stripe file)"
7845
7846 test_130e() {
7847         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7848
7849         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7850         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7851
7852         trap cleanup_130 EXIT RETURN
7853
7854         local fm_file=$DIR/$tfile
7855         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7856         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7857                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7858
7859         NUM_BLKS=512
7860         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7861         for ((i = 0; i < $NUM_BLKS; i++))
7862         do
7863                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7864         done
7865
7866         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7867         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7868
7869         last_lun=`echo $filefrag_op | cut -d: -f5`
7870
7871         IFS=$'\n'
7872         tot_len=0
7873         num_luns=1
7874         for line in $filefrag_op
7875         do
7876                 frag_lun=`echo $line | cut -d: -f5`
7877                 ext_len=`echo $line | cut -d: -f4`
7878                 if (( $frag_lun != $last_lun )); then
7879                         if (( tot_len != $EXPECTED_LEN )); then
7880                                 cleanup_130
7881                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7882                                 return
7883                         else
7884                                 (( num_luns += 1 ))
7885                                 tot_len=0
7886                         fi
7887                 fi
7888                 (( tot_len += ext_len ))
7889                 last_lun=$frag_lun
7890         done
7891         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7892                 cleanup_130
7893                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7894                 return
7895         fi
7896
7897         cleanup_130
7898
7899         echo "FIEMAP with continuation calls succeeded"
7900 }
7901 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7902
7903 # Test for writev/readv
7904 test_131a() {
7905         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7906         error "writev test failed"
7907         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7908         error "readv failed"
7909         rm -f $DIR/$tfile
7910 }
7911 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7912
7913 test_131b() {
7914         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7915         error "append writev test failed"
7916         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7917         error "append writev test failed"
7918         rm -f $DIR/$tfile
7919 }
7920 run_test 131b "test append writev"
7921
7922 test_131c() {
7923         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7924         error "NOT PASS"
7925 }
7926 run_test 131c "test read/write on file w/o objects"
7927
7928 test_131d() {
7929         rwv -f $DIR/$tfile -w -n 1 1572864
7930         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7931         if [ "$NOB" != 1572864 ]; then
7932                 error "Short read filed: read $NOB bytes instead of 1572864"
7933         fi
7934         rm -f $DIR/$tfile
7935 }
7936 run_test 131d "test short read"
7937
7938 test_131e() {
7939         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7940         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7941         error "read hitting hole failed"
7942         rm -f $DIR/$tfile
7943 }
7944 run_test 131e "test read hitting hole"
7945
7946 get_ost_param() {
7947         local token=$1
7948         local gl_sum=0
7949         for node in $(osts_nodes); do
7950                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7951                 [ x$gl = x"" ] && gl=0
7952                 gl_sum=$((gl_sum + gl))
7953         done
7954         echo $gl_sum
7955 }
7956
7957 som_mode_switch() {
7958         local som=$1
7959         local gl1=$2
7960         local gl2=$3
7961
7962         if [ x$som = x"enabled" ]; then
7963                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7964                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7965                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7966         else
7967                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7968                 MOUNTOPT="$MOUNTOPT,som_preview"
7969                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7970         fi
7971
7972         # do remount to make new mount-conf parameters actual
7973         echo remounting...
7974         sync
7975         stopall
7976         setupall
7977 }
7978
7979 test_132() { #1028, SOM
7980         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7981         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7982         local num=$(get_mds_dir $DIR)
7983         local mymds=mds${num}
7984         local MOUNTOPT_SAVE=$MOUNTOPT
7985
7986         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7987         cancel_lru_locks osc
7988
7989         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7990
7991         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7992         stat $DIR/$tfile >/dev/null
7993         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7994         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7995         rm $DIR/$tfile
7996         som_mode_switch $som1 $gl1 $gl2
7997
7998         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7999         cancel_lru_locks osc
8000
8001         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8002         if [ $som1 == $som2 ]; then
8003             error "som is still "$som2
8004             if [ x$som2 = x"enabled" ]; then
8005                 som2="disabled"
8006             else
8007                 som2="enabled"
8008             fi
8009         fi
8010
8011         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8012         stat $DIR/$tfile >/dev/null
8013         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8014         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8015         som_mode_switch $som2 $gl1 $gl2
8016         MOUNTOPT=$MOUNTOPT_SAVE
8017 }
8018 run_test 132 "som avoids glimpse rpc"
8019
8020 check_stats() {
8021         local res
8022         local count
8023         case $1 in
8024         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8025                  ;;
8026         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8027                  ;;
8028         *) error "Wrong argument $1" ;;
8029         esac
8030         echo $res
8031         count=`echo $res | awk '{print $2}'`
8032         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8033         # if the argument $3 is zero, it means any stat increment is ok.
8034         if [ $3 -gt 0 ] ; then
8035                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
8036         fi
8037 }
8038
8039 test_133a() {
8040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8041         remote_ost_nodsh && skip "remote OST with nodsh" && return
8042         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8043
8044         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8045                 { skip "MDS doesn't support rename stats"; return; }
8046         local testdir=$DIR/${tdir}/stats_testdir
8047         mkdir -p $DIR/${tdir}
8048
8049         # clear stats.
8050         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8051         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8052
8053         # verify mdt stats first.
8054         mkdir ${testdir} || error "mkdir failed"
8055         check_stats $SINGLEMDS "mkdir" 1
8056         touch ${testdir}/${tfile} || "touch failed"
8057         check_stats $SINGLEMDS "open" 1
8058         check_stats $SINGLEMDS "close" 1
8059         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8060         check_stats $SINGLEMDS "mknod" 1
8061         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8062         check_stats $SINGLEMDS "unlink" 1
8063         rm -f ${testdir}/${tfile} || error "file remove failed"
8064         check_stats $SINGLEMDS "unlink" 2
8065
8066         # remove working dir and check mdt stats again.
8067         rmdir ${testdir} || error "rmdir failed"
8068         check_stats $SINGLEMDS "rmdir" 1
8069
8070         local testdir1=$DIR/${tdir}/stats_testdir1
8071         mkdir -p ${testdir}
8072         mkdir -p ${testdir1}
8073         touch ${testdir1}/test1
8074         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8075         check_stats $SINGLEMDS "crossdir_rename" 1
8076
8077         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8078         check_stats $SINGLEMDS "samedir_rename" 1
8079
8080         rm -rf $DIR/${tdir}
8081 }
8082 run_test 133a "Verifying MDT stats ========================================"
8083
8084 test_133b() {
8085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8086         remote_ost_nodsh && skip "remote OST with nodsh" && return
8087         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8088         local testdir=$DIR/${tdir}/stats_testdir
8089         mkdir -p ${testdir} || error "mkdir failed"
8090         touch ${testdir}/${tfile} || "touch failed"
8091         cancel_lru_locks mdc
8092
8093         # clear stats.
8094         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8095         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8096
8097         # extra mdt stats verification.
8098         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8099         check_stats $SINGLEMDS "setattr" 1
8100         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8101         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8102         then            # LU-1740
8103                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8104                 check_stats $SINGLEMDS "getattr" 1
8105         fi
8106         $LFS df || error "lfs failed"
8107         check_stats $SINGLEMDS "statfs" 1
8108
8109         rm -rf $DIR/${tdir}
8110 }
8111 run_test 133b "Verifying extra MDT stats =================================="
8112
8113 test_133c() {
8114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8115         remote_ost_nodsh && skip "remote OST with nodsh" && return
8116         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8117         local testdir=$DIR/${tdir}/stats_testdir
8118         test_mkdir -p ${testdir} || error "mkdir failed"
8119
8120         # verify obdfilter stats.
8121         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8122         sync
8123         cancel_lru_locks osc
8124         wait_delete_completed
8125
8126         # clear stats.
8127         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8128         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8129
8130         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8131         sync
8132         cancel_lru_locks osc
8133         check_stats ost "write" 1
8134
8135         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8136         check_stats ost "read" 1
8137
8138         > ${testdir}/${tfile} || error "truncate failed"
8139         check_stats ost "punch" 1
8140
8141         rm -f ${testdir}/${tfile} || error "file remove failed"
8142         wait_delete_completed
8143         check_stats ost "destroy" 1
8144
8145         rm -rf $DIR/${tdir}
8146 }
8147 run_test 133c "Verifying OST stats ========================================"
8148
8149 order_2() {
8150     local value=$1
8151     local orig=$value
8152     local order=1
8153
8154     while [ $value -ge 2 ]; do
8155         order=$((order*2))
8156         value=$((value/2))
8157     done
8158
8159     if [ $orig -gt $order ]; then
8160         order=$((order*2))
8161     fi
8162     echo $order
8163 }
8164
8165 size_in_KMGT() {
8166     local value=$1
8167     local size=('K' 'M' 'G' 'T');
8168     local i=0
8169     local size_string=$value
8170
8171     while [ $value -ge 1024 ]; do
8172         if [ $i -gt 3 ]; then
8173             #T is the biggest unit we get here, if that is bigger,
8174             #just return XXXT
8175             size_string=${value}T
8176             break
8177         fi
8178         value=$((value >> 10))
8179         if [ $value -lt 1024 ]; then
8180             size_string=${value}${size[$i]}
8181             break
8182         fi
8183         i=$((i + 1))
8184     done
8185
8186     echo $size_string
8187 }
8188
8189 get_rename_size() {
8190     local size=$1
8191     local context=${2:-.}
8192     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8193                 grep -A1 $context |
8194                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8195     echo $sample
8196 }
8197
8198 test_133d() {
8199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8200     remote_ost_nodsh && skip "remote OST with nodsh" && return
8201     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8202     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8203         { skip "MDS doesn't support rename stats"; return; }
8204
8205     local testdir1=$DIR/${tdir}/stats_testdir1
8206     local testdir2=$DIR/${tdir}/stats_testdir2
8207
8208     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8209
8210     test_mkdir -p ${testdir1} || error "mkdir failed"
8211     test_mkdir -p ${testdir2} || error "mkdir failed"
8212
8213     createmany -o $testdir1/test 512 || error "createmany failed"
8214
8215         # check samedir rename size
8216         mv ${testdir1}/test0 ${testdir1}/test_0
8217
8218         local testdir1_size=$(ls -l $DIR/${tdir} |
8219                 awk '/stats_testdir1/ {print $5}')
8220         local testdir2_size=$(ls -l $DIR/${tdir} |
8221                 awk '/stats_testdir2/ {print $5}')
8222
8223         testdir1_size=$(order_2 $testdir1_size)
8224         testdir2_size=$(order_2 $testdir2_size)
8225
8226         testdir1_size=$(size_in_KMGT $testdir1_size)
8227         testdir2_size=$(size_in_KMGT $testdir2_size)
8228
8229         echo "source rename dir size: ${testdir1_size}"
8230         echo "target rename dir size: ${testdir2_size}"
8231
8232     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8233     eval $cmd || error "$cmd failed"
8234     local samedir=$($cmd | grep 'same_dir')
8235     local same_sample=$(get_rename_size $testdir1_size)
8236     [ -z "$samedir" ] && error "samedir_rename_size count error"
8237     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8238     echo "Check same dir rename stats success"
8239
8240     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8241
8242     # check crossdir rename size
8243     mv ${testdir1}/test_0 ${testdir2}/test_0
8244
8245         testdir1_size=$(ls -l $DIR/${tdir} |
8246                 awk '/stats_testdir1/ {print $5}')
8247         testdir2_size=$(ls -l $DIR/${tdir} |
8248                 awk '/stats_testdir2/ {print $5}')
8249
8250         testdir1_size=$(order_2 $testdir1_size)
8251         testdir2_size=$(order_2 $testdir2_size)
8252
8253         testdir1_size=$(size_in_KMGT $testdir1_size)
8254         testdir2_size=$(size_in_KMGT $testdir2_size)
8255
8256         echo "source rename dir size: ${testdir1_size}"
8257         echo "target rename dir size: ${testdir2_size}"
8258
8259     eval $cmd || error "$cmd failed"
8260     local crossdir=$($cmd | grep 'crossdir')
8261     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8262     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8263     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8264     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8265     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8266     echo "Check cross dir rename stats success"
8267     rm -rf $DIR/${tdir}
8268 }
8269 run_test 133d "Verifying rename_stats ========================================"
8270
8271 test_133e() {
8272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8273         local testdir=$DIR/${tdir}/stats_testdir
8274         local ctr f0 f1 bs=32768 count=42 sum
8275
8276         remote_ost_nodsh && skip "remote OST with nodsh" && return
8277         mkdir -p ${testdir} || error "mkdir failed"
8278
8279         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8280
8281         for ctr in {write,read}_bytes; do
8282                 sync
8283                 cancel_lru_locks osc
8284
8285                 do_facet ost1 $LCTL set_param -n \
8286                         "obdfilter.*.exports.clear=clear"
8287
8288                 if [ $ctr = write_bytes ]; then
8289                         f0=/dev/zero
8290                         f1=${testdir}/${tfile}
8291                 else
8292                         f0=${testdir}/${tfile}
8293                         f1=/dev/null
8294                 fi
8295
8296                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8297                         error "dd failed"
8298                 sync
8299                 cancel_lru_locks osc
8300
8301                 sum=$(do_facet ost1 $LCTL get_param \
8302                                 "obdfilter.*.exports.*.stats" | \
8303                           awk -v ctr=$ctr '\
8304                                 BEGIN { sum = 0 }
8305                                 $1 == ctr { sum += $7 }
8306                                 END { print sum }')
8307
8308                 if ((sum != bs * count)); then
8309                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8310                 fi
8311         done
8312
8313         rm -rf $DIR/${tdir}
8314 }
8315 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8316
8317 test_133f() {
8318         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8319         local facet
8320
8321         # First without trusting modes.
8322         find $proc_dirs \
8323                 -exec cat '{}' \; &> /dev/null
8324
8325         # Second verifying readability.
8326         find $proc_dirs \
8327                 -type f \
8328                 -readable \
8329                 -exec cat '{}' \; > /dev/null ||
8330                         error "proc file read failed"
8331
8332         for facet in $SINGLEMDS ost1; do
8333                 do_facet $facet find $proc_dirs \
8334                         -not -name req_history \
8335                         -exec cat '{}' \\\; &> /dev/null
8336
8337             do_facet $facet     find $proc_dirs \
8338                         -not -name req_history \
8339                         -type f \
8340                         -readable \
8341                         -exec cat '{}' \\\; > /dev/null ||
8342                                 error "proc file read failed"
8343         done
8344 }
8345 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8346
8347 test_140() { #bug-17379
8348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8349         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8350         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8351         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8352
8353         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8354         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8355         local i=0
8356         while i=`expr $i + 1`; do
8357                 test_mkdir -p $i || error "Creating dir $i"
8358                 cd $i || error "Changing to $i"
8359                 ln -s ../stat stat || error "Creating stat symlink"
8360                 # Read the symlink until ELOOP present,
8361                 # not LBUGing the system is considered success,
8362                 # we didn't overrun the stack.
8363                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8364                 [ $ret -ne 0 ] && {
8365                         if [ $ret -eq 40 ]; then
8366                                 break  # -ELOOP
8367                         else
8368                                 error "Open stat symlink"
8369                                 return
8370                         fi
8371                 }
8372         done
8373         i=`expr $i - 1`
8374         echo "The symlink depth = $i"
8375         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8376                                         error "Invalid symlink depth"
8377
8378         # Test recursive symlink
8379         ln -s symlink_self symlink_self
8380         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8381         echo "open symlink_self returns $ret"
8382         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8383 }
8384 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8385
8386 test_150() {
8387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8388         local TF="$TMP/$tfile"
8389
8390         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8391         cp $TF $DIR/$tfile
8392         cancel_lru_locks osc
8393         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8394         remount_client $MOUNT
8395         df -P $MOUNT
8396         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8397
8398         $TRUNCATE $TF 6000
8399         $TRUNCATE $DIR/$tfile 6000
8400         cancel_lru_locks osc
8401         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8402
8403         echo "12345" >>$TF
8404         echo "12345" >>$DIR/$tfile
8405         cancel_lru_locks osc
8406         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8407
8408         echo "12345" >>$TF
8409         echo "12345" >>$DIR/$tfile
8410         cancel_lru_locks osc
8411         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8412
8413         rm -f $TF
8414         true
8415 }
8416 run_test 150 "truncate/append tests"
8417
8418 function roc_hit() {
8419         local list=$(comma_list $(osts_nodes))
8420
8421         echo $(get_osd_param $list '' stats |
8422                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8423 }
8424
8425 function set_cache() {
8426         local on=1
8427
8428         if [ "$2" == "off" ]; then
8429                 on=0;
8430         fi
8431         local list=$(comma_list $(osts_nodes))
8432         set_osd_param $list '' $1_cache_enable $on
8433
8434         cancel_lru_locks osc
8435 }
8436
8437 test_151() {
8438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8439         remote_ost_nodsh && skip "remote OST with nodsh" && return
8440
8441         local CPAGES=3
8442         local list=$(comma_list $(osts_nodes))
8443
8444         # check whether obdfilter is cache capable at all
8445         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8446                 echo "not cache-capable obdfilter"
8447                 return 0
8448         fi
8449
8450         # check cache is enabled on all obdfilters
8451         if get_osd_param $list '' read_cache_enable | grep 0; then
8452                 echo "oss cache is disabled"
8453                 return 0
8454         fi
8455
8456         set_osd_param $list '' writethrough_cache_enable 1
8457
8458         # pages should be in the case right after write
8459         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
8460         local BEFORE=`roc_hit`
8461         cancel_lru_locks osc
8462         cat $DIR/$tfile >/dev/null
8463         local AFTER=`roc_hit`
8464         if ! let "AFTER - BEFORE == CPAGES"; then
8465                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8466         fi
8467
8468         # the following read invalidates the cache
8469         cancel_lru_locks osc
8470         set_osd_param $list '' read_cache_enable 0
8471         cat $DIR/$tfile >/dev/null
8472
8473         # now data shouldn't be found in the cache
8474         BEFORE=`roc_hit`
8475         cancel_lru_locks osc
8476         cat $DIR/$tfile >/dev/null
8477         AFTER=`roc_hit`
8478         if let "AFTER - BEFORE != 0"; then
8479                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8480         fi
8481
8482         set_osd_param $list '' read_cache_enable 1
8483         rm -f $DIR/$tfile
8484 }
8485 run_test 151 "test cache on oss and controls ==============================="
8486
8487 test_152() {
8488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8489         local TF="$TMP/$tfile"
8490
8491         # simulate ENOMEM during write
8492 #define OBD_FAIL_OST_NOMEM      0x226
8493         lctl set_param fail_loc=0x80000226
8494         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8495         cp $TF $DIR/$tfile
8496         sync || error "sync failed"
8497         lctl set_param fail_loc=0
8498
8499         # discard client's cache
8500         cancel_lru_locks osc
8501
8502         # simulate ENOMEM during read
8503         lctl set_param fail_loc=0x80000226
8504         cmp $TF $DIR/$tfile || error "cmp failed"
8505         lctl set_param fail_loc=0
8506
8507         rm -f $TF
8508 }
8509 run_test 152 "test read/write with enomem ============================"
8510
8511 test_153() {
8512         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8513 }
8514 run_test 153 "test if fdatasync does not crash ======================="
8515
8516 dot_lustre_fid_permission_check() {
8517         local fid=$1
8518         local ffid=$MOUNT/.lustre/fid/$fid
8519         local test_dir=$2
8520
8521         echo "stat fid $fid"
8522         stat $ffid > /dev/null || error "stat $ffid failed."
8523         echo "touch fid $fid"
8524         touch $ffid || error "touch $ffid failed."
8525         echo "write to fid $fid"
8526         cat /etc/hosts > $ffid || error "write $ffid failed."
8527         echo "read fid $fid"
8528         diff /etc/hosts $ffid || error "read $ffid failed."
8529         echo "append write to fid $fid"
8530         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8531         echo "rename fid $fid"
8532         mv $ffid $test_dir/$tfile.1 &&
8533                 error "rename $ffid to $tfile.1 should fail."
8534         touch $test_dir/$tfile.1
8535         mv $test_dir/$tfile.1 $ffid &&
8536                 error "rename $tfile.1 to $ffid should fail."
8537         rm -f $test_dir/$tfile.1
8538         echo "truncate fid $fid"
8539         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8540         echo "link fid $fid"
8541         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
8542         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8543                 echo "setfacl fid $fid"
8544                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8545                 echo "getfacl fid $fid"
8546                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8547         fi
8548         echo "unlink fid $fid"
8549         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
8550         echo "mknod fid $fid"
8551         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8552
8553         fid=[0xf00000400:0x1:0x0]
8554         ffid=$MOUNT/.lustre/fid/$fid
8555
8556         echo "stat non-exist fid $fid"
8557         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8558         echo "write to non-exist fid $fid"
8559         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8560         echo "link new fid $fid"
8561         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
8562
8563         mkdir -p $test_dir/$tdir
8564         touch $test_dir/$tdir/$tfile
8565         fid=$($LFS path2fid $test_dir/$tdir)
8566         rc=$?
8567         [ $rc -ne 0 ] &&
8568                 error "error: could not get fid for $test_dir/$dir/$tfile."
8569
8570         ffid=$MOUNT/.lustre/fid/$fid
8571
8572         echo "ls $fid"
8573         ls $ffid > /dev/null || error "ls $ffid failed."
8574         echo "touch $fid/$tfile.1"
8575         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8576
8577         echo "touch $MOUNT/.lustre/fid/$tfile"
8578         touch $MOUNT/.lustre/fid/$tfile && \
8579                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
8580
8581         echo "setxattr to $MOUNT/.lustre/fid"
8582         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid &&
8583                 error "setxattr should fail."
8584
8585         echo "listxattr for $MOUNT/.lustre/fid"
8586         getfattr -d -m "^trusted" $MOUNT/.lustre/fid &&
8587                 error "listxattr should fail."
8588
8589         echo "delxattr from $MOUNT/.lustre/fid"
8590         setfattr -x trusted.name1 $MOUNT/.lustre/fid &&
8591                 error "delxattr should fail."
8592
8593         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
8594         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
8595                 error "touch invalid fid should fail."
8596
8597         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
8598         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
8599                 error "touch non-normal fid should fail."
8600
8601         echo "rename $tdir to $MOUNT/.lustre/fid"
8602         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
8603                 error "rename to $MOUNT/.lustre/fid should fail."
8604
8605         echo "rename .lustre to itself"
8606         fid=$($LFS path2fid $MOUNT)
8607         mrename $MOUNT/.lustre $MOUNT/.lustre/fid/$fid/.lustre &&
8608                 error "rename .lustre to itself should fail."
8609
8610         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
8611         fid=$($LFS path2fid $test_dir/$tfile-2)
8612         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
8613         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
8614                 error "create lov data thru .lustre should fail."
8615         echo "cp /etc/passwd $test_dir/$tfile-2"
8616         cp /etc/passwd $test_dir/$tfile-2 ||
8617                 error "copy to $test_dir/$tfile-2 failed."
8618         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
8619         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
8620                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
8621
8622         rm -rf $test_dir/tfile.lnk
8623         rm -rf $test_dir/$tfile-2
8624 }
8625
8626 test_154a() {
8627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8628         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8629                 { skip "Need MDS version at least 2.2.51"; return 0; }
8630
8631         cp /etc/hosts $DIR/$tfile
8632
8633         fid=$($LFS path2fid $DIR/$tfile)
8634         rc=$?
8635         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8636
8637         dot_lustre_fid_permission_check "$fid" $DIR ||
8638                 error "dot lustre permission check $fid failed"
8639
8640         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
8641         chmod 777 $MOUNT/.lustre && error ".lustre is not allowed to be chmod"
8642
8643         touch $MOUNT/.lustre/file &&
8644                 error "creation is not allowed under .lustre"
8645
8646         mkdir $MOUNT/.lustre/dir &&
8647                 error "mkdir is not allowed under .lustre"
8648
8649         rm -rf $DIR/$tfile
8650 }
8651 run_test 154a "Open-by-FID"
8652
8653 test_154b() {
8654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8655         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8656                 { skip "Need MDS version at least 2.2.51"; return 0; }
8657
8658         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
8659
8660         local remote_dir=$DIR/$tdir/remote_dir
8661         local MDTIDX=1
8662         local rc=0
8663
8664         mkdir -p $DIR/$tdir
8665         $LFS mkdir -i $MDTIDX $remote_dir ||
8666                 error "create remote directory failed"
8667
8668         cp /etc/hosts $remote_dir/$tfile
8669
8670         fid=$($LFS path2fid $remote_dir/$tfile)
8671         rc=$?
8672         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
8673
8674         dot_lustre_fid_permission_check "$fid" $remote_dir ||
8675                 error "dot lustre permission check $fid failed"
8676         rm -rf $DIR/$tdir
8677 }
8678 run_test 154b "Open-by-FID for remote directory"
8679
8680 test_155_small_load() {
8681     local temp=$TMP/$tfile
8682     local file=$DIR/$tfile
8683
8684     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8685         error "dd of=$temp bs=6096 count=1 failed"
8686     cp $temp $file
8687     cancel_lru_locks osc
8688     cmp $temp $file || error "$temp $file differ"
8689
8690     $TRUNCATE $temp 6000
8691     $TRUNCATE $file 6000
8692     cmp $temp $file || error "$temp $file differ (truncate1)"
8693
8694     echo "12345" >>$temp
8695     echo "12345" >>$file
8696     cmp $temp $file || error "$temp $file differ (append1)"
8697
8698     echo "12345" >>$temp
8699     echo "12345" >>$file
8700     cmp $temp $file || error "$temp $file differ (append2)"
8701
8702     rm -f $temp $file
8703     true
8704 }
8705
8706 test_155_big_load() {
8707     remote_ost_nodsh && skip "remote OST with nodsh" && return
8708     local temp=$TMP/$tfile
8709     local file=$DIR/$tfile
8710
8711     free_min_max
8712     local cache_size=$(do_facet ost$((MAXI+1)) \
8713         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8714     local large_file_size=$((cache_size * 2))
8715
8716     echo "OSS cache size: $cache_size KB"
8717     echo "Large file size: $large_file_size KB"
8718
8719     [ $MAXV -le $large_file_size ] && \
8720         skip_env "max available OST size needs > $large_file_size KB" && \
8721         return 0
8722
8723     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8724
8725     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8726         error "dd of=$temp bs=$large_file_size count=1k failed"
8727     cp $temp $file
8728     ls -lh $temp $file
8729     cancel_lru_locks osc
8730     cmp $temp $file || error "$temp $file differ"
8731
8732     rm -f $temp $file
8733     true
8734 }
8735
8736 test_155a() {
8737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8738         set_cache read on
8739         set_cache writethrough on
8740         test_155_small_load
8741 }
8742 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8743
8744 test_155b() {
8745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8746         set_cache read on
8747         set_cache writethrough off
8748         test_155_small_load
8749 }
8750 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8751
8752 test_155c() {
8753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8754         set_cache read off
8755         set_cache writethrough on
8756         test_155_small_load
8757 }
8758 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8759
8760 test_155d() {
8761         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8762         set_cache read off
8763         set_cache writethrough off
8764         test_155_small_load
8765 }
8766 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8767
8768 test_155e() {
8769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8770         set_cache read on
8771         set_cache writethrough on
8772         test_155_big_load
8773 }
8774 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8775
8776 test_155f() {
8777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8778         set_cache read on
8779         set_cache writethrough off
8780         test_155_big_load
8781 }
8782 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8783
8784 test_155g() {
8785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8786         set_cache read off
8787         set_cache writethrough on
8788         test_155_big_load
8789 }
8790 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8791
8792 test_155h() {
8793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8794         set_cache read off
8795         set_cache writethrough off
8796         test_155_big_load
8797 }
8798 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8799
8800 test_156() {
8801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8802         local CPAGES=3
8803         local BEFORE
8804         local AFTER
8805         local file="$DIR/$tfile"
8806
8807         [ "$(facet_fstype ost1)" = "zfs" ] &&
8808                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8809                 return
8810
8811     log "Turn on read and write cache"
8812     set_cache read on
8813     set_cache writethrough on
8814
8815     log "Write data and read it back."
8816     log "Read should be satisfied from the cache."
8817     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8818     BEFORE=`roc_hit`
8819     cancel_lru_locks osc
8820     cat $file >/dev/null
8821     AFTER=`roc_hit`
8822     if ! let "AFTER - BEFORE == CPAGES"; then
8823         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8824     else
8825         log "cache hits:: before: $BEFORE, after: $AFTER"
8826     fi
8827
8828     log "Read again; it should be satisfied from the cache."
8829     BEFORE=$AFTER
8830     cancel_lru_locks osc
8831     cat $file >/dev/null
8832     AFTER=`roc_hit`
8833     if ! let "AFTER - BEFORE == CPAGES"; then
8834         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8835     else
8836         log "cache hits:: before: $BEFORE, after: $AFTER"
8837     fi
8838
8839
8840     log "Turn off the read cache and turn on the write cache"
8841     set_cache read off
8842     set_cache writethrough on
8843
8844     log "Read again; it should be satisfied from the cache."
8845     BEFORE=`roc_hit`
8846     cancel_lru_locks osc
8847     cat $file >/dev/null
8848     AFTER=`roc_hit`
8849     if ! let "AFTER - BEFORE == CPAGES"; then
8850         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8851     else
8852         log "cache hits:: before: $BEFORE, after: $AFTER"
8853     fi
8854
8855     log "Read again; it should not be satisfied from the cache."
8856     BEFORE=$AFTER
8857     cancel_lru_locks osc
8858     cat $file >/dev/null
8859     AFTER=`roc_hit`
8860     if ! let "AFTER - BEFORE == 0"; then
8861         error "IN CACHE: before: $BEFORE, after: $AFTER"
8862     else
8863         log "cache hits:: before: $BEFORE, after: $AFTER"
8864     fi
8865
8866     log "Write data and read it back."
8867     log "Read should be satisfied from the cache."
8868     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8869     BEFORE=`roc_hit`
8870     cancel_lru_locks osc
8871     cat $file >/dev/null
8872     AFTER=`roc_hit`
8873     if ! let "AFTER - BEFORE == CPAGES"; then
8874         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8875     else
8876         log "cache hits:: before: $BEFORE, after: $AFTER"
8877     fi
8878
8879     log "Read again; it should not be satisfied from the cache."
8880     BEFORE=$AFTER
8881     cancel_lru_locks osc
8882     cat $file >/dev/null
8883     AFTER=`roc_hit`
8884     if ! let "AFTER - BEFORE == 0"; then
8885         error "IN CACHE: before: $BEFORE, after: $AFTER"
8886     else
8887         log "cache hits:: before: $BEFORE, after: $AFTER"
8888     fi
8889
8890
8891     log "Turn off read and write cache"
8892     set_cache read off
8893     set_cache writethrough off
8894
8895     log "Write data and read it back"
8896     log "It should not be satisfied from the cache."
8897     rm -f $file
8898     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8899     cancel_lru_locks osc
8900     BEFORE=`roc_hit`
8901     cat $file >/dev/null
8902     AFTER=`roc_hit`
8903     if ! let "AFTER - BEFORE == 0"; then
8904         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8905     else
8906         log "cache hits:: before: $BEFORE, after: $AFTER"
8907     fi
8908
8909
8910     log "Turn on the read cache and turn off the write cache"
8911     set_cache read on
8912     set_cache writethrough off
8913
8914     log "Write data and read it back"
8915     log "It should not be satisfied from the cache."
8916     rm -f $file
8917     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8918     BEFORE=`roc_hit`
8919     cancel_lru_locks osc
8920     cat $file >/dev/null
8921     AFTER=`roc_hit`
8922     if ! let "AFTER - BEFORE == 0"; then
8923         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8924     else
8925         log "cache hits:: before: $BEFORE, after: $AFTER"
8926     fi
8927
8928     log "Read again; it should be satisfied from the cache."
8929     BEFORE=`roc_hit`
8930     cancel_lru_locks osc
8931     cat $file >/dev/null
8932     AFTER=`roc_hit`
8933     if ! let "AFTER - BEFORE == CPAGES"; then
8934         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8935     else
8936         log "cache hits:: before: $BEFORE, after: $AFTER"
8937     fi
8938
8939     rm -f $file
8940 }
8941 run_test 156 "Verification of tunables ============================"
8942
8943 #Changelogs
8944 err17935 () {
8945     if [ $MDSCOUNT -gt 1 ]; then
8946         error_ignore 17935 $*
8947     else
8948         error $*
8949     fi
8950 }
8951
8952 changelog_chmask()
8953 {
8954     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8955            grep -c $1)
8956
8957     if [ $MASK -eq 1 ]; then
8958         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8959     else
8960         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8961     fi
8962 }
8963
8964 test_160() {
8965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8966     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8967     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8968         { skip "Need MDS version at least 2.2.0"; return; }
8969     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8970     echo "Registered as changelog user $USER"
8971     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8972         grep -q $USER || error "User $USER not found in changelog_users"
8973
8974     # change something
8975     test_mkdir -p $DIR/$tdir/pics/2008/zachy
8976     touch $DIR/$tdir/pics/2008/zachy/timestamp
8977     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8978     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8979     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8980     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8981     rm $DIR/$tdir/pics/desktop.jpg
8982
8983     $LFS changelog $MDT0 | tail -5
8984
8985     echo "verifying changelog mask"
8986     changelog_chmask "MKDIR"
8987     changelog_chmask "CLOSE"
8988
8989     test_mkdir -p $DIR/$tdir/pics/zach/sofia
8990     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8991
8992     changelog_chmask "MKDIR"
8993     changelog_chmask "CLOSE"
8994
8995     test_mkdir -p $DIR/$tdir/pics/2008/sofia
8996     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8997
8998     $LFS changelog $MDT0
8999     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9000     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9001     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9002     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9003
9004     # verify contents
9005     echo "verifying target fid"
9006     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
9007         tail -1 | awk '{print $6}')
9008     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9009     [ "$fidc" == "t=$fidf" ] || \
9010         err17935 "fid in changelog $fidc != file fid $fidf"
9011     echo "verifying parent fid"
9012     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
9013         tail -1 | awk '{print $7}')
9014     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9015     [ "$fidc" == "p=$fidf" ] || \
9016         err17935 "pfid in changelog $fidc != dir fid $fidf"
9017
9018     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
9019         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
9020     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9021     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
9022         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
9023     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9024     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
9025         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
9026
9027     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
9028         awk 'min == "" || $2 < min {min = $2}; END {print min}')
9029     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
9030     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9031     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
9032         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
9033
9034     echo "verifying user deregister"
9035     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9036     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
9037         grep -q $USER && error "User $USER still found in changelog_users"
9038
9039     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
9040         mdd.$MDT0.changelog_users | wc -l) - 2 ))
9041     if [ $USERS -eq 0 ]; then
9042         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
9043             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
9044         touch $DIR/$tdir/chloe
9045         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
9046             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
9047         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
9048         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9049     else
9050         echo "$USERS other changelog users; can't verify off"
9051     fi
9052 }
9053 run_test 160 "changelog sanity"
9054
9055 test_161() {
9056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9057     test_mkdir -p $DIR/$tdir
9058     cp /etc/hosts $DIR/$tdir/$tfile
9059     test_mkdir $DIR/$tdir/foo1
9060     test_mkdir $DIR/$tdir/foo2
9061     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9062     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9063     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9064     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9065     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
9066     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9067         $LFS fid2path $DIR $FID
9068         err17935 "bad link ea"
9069     fi
9070     # middle
9071     rm $DIR/$tdir/foo2/zachary
9072     # last
9073     rm $DIR/$tdir/foo2/thor
9074     # first
9075     rm $DIR/$tdir/$tfile
9076     # rename
9077     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9078     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9079         then
9080         $LFS fid2path $DIR $FID
9081         err17935 "bad link rename"
9082     fi
9083     rm $DIR/$tdir/foo2/maggie
9084
9085     # overflow the EA
9086     local longname=filename_avg_len_is_thirty_two_
9087     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
9088         error "failed to hardlink many files"
9089     links=$($LFS fid2path $DIR $FID | wc -l)
9090     echo -n "${links}/1000 links in link EA"
9091     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9092     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
9093         error "failed to unlink many hardlinks"
9094 }
9095 run_test 161 "link ea sanity"
9096
9097 check_path() {
9098     local expected=$1
9099     shift
9100     local fid=$2
9101
9102     local path=$(${LFS} fid2path $*)
9103     RC=$?
9104
9105     if [ $RC -ne 0 ]; then
9106         err17935 "path looked up of $expected failed. Error $RC"
9107         return $RC
9108     elif [ "${path}" != "${expected}" ]; then
9109         err17935 "path looked up \"${path}\" instead of \"${expected}\""
9110         return 2
9111     fi
9112     echo "fid $fid resolves to path $path (expected $expected)"
9113 }
9114
9115 test_162() {
9116         # Make changes to filesystem
9117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9118         test_mkdir -p $DIR/$tdir/d2
9119         touch $DIR/$tdir/d2/$tfile
9120         touch $DIR/$tdir/d2/x1
9121         touch $DIR/$tdir/d2/x2
9122         test_mkdir -p $DIR/$tdir/d2/a/b/c
9123         test_mkdir -p $DIR/$tdir/d2/p/q/r
9124         # regular file
9125         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
9126         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
9127
9128         # softlink
9129         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
9130         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
9131         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
9132
9133         # softlink to wrong file
9134         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
9135         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
9136         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
9137
9138         # hardlink
9139         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
9140         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
9141         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
9142         # fid2path dir/fsname should both work
9143         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
9144         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9145
9146         # hardlink count: check that there are 2 links
9147         # Doesnt work with CMD yet: 17935
9148         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9149                 err17935 "expected 2 links"
9150
9151         # hardlink indexing: remove the first link
9152         rm $DIR/$tdir/d2/p/q/r/hlink
9153         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9154
9155         return 0
9156 }
9157 run_test 162 "path lookup sanity"
9158
9159 test_163() {
9160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9161         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9162         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
9163         copytool $FSNAME &
9164         sleep 1
9165         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
9166         # this proc file is temporary and linux-only
9167         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
9168          error "kernel->userspace send failed"
9169         kill -INT $!
9170 }
9171 run_test 163 "kernel <-> userspace comms"
9172
9173 test_169() {
9174         # do directio so as not to populate the page cache
9175         log "creating a 10 Mb file"
9176         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9177         log "starting reads"
9178         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9179         log "truncating the file"
9180         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9181         log "killing dd"
9182         kill %+ || true # reads might have finished
9183         echo "wait until dd is finished"
9184         wait
9185         log "removing the temporary file"
9186         rm -rf $DIR/$tfile || error "tmp file removal failed"
9187 }
9188 run_test 169 "parallel read and truncate should not deadlock"
9189
9190 test_170() {
9191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9192         $LCTL clear     # bug 18514
9193         $LCTL debug_daemon start $TMP/${tfile}_log_good
9194         touch $DIR/$tfile
9195         $LCTL debug_daemon stop
9196         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9197                error "sed failed to read log_good"
9198
9199         $LCTL debug_daemon start $TMP/${tfile}_log_good
9200         rm -rf $DIR/$tfile
9201         $LCTL debug_daemon stop
9202
9203         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9204                error "lctl df log_bad failed"
9205
9206         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9207         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9208
9209         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9210         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9211
9212         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9213                 error "bad_line good_line1 good_line2 are empty"
9214
9215         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9216         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9217         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9218
9219         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9220         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9221         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9222
9223         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9224                 error "bad_line_new good_line_new are empty"
9225
9226         local expected_good=$((good_line1 + good_line2*2))
9227
9228         rm -f $TMP/${tfile}*
9229         # LU-231, short malformed line may not be counted into bad lines
9230         if [ $bad_line -ne $bad_line_new ] &&
9231                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9232                 error "expected $bad_line bad lines, but got $bad_line_new"
9233                 return 1
9234         fi
9235
9236         if [ $expected_good -ne $good_line_new ]; then
9237                 error "expected $expected_good good lines, but got $good_line_new"
9238                 return 2
9239         fi
9240         true
9241 }
9242 run_test 170 "test lctl df to handle corrupted log ====================="
9243
9244 test_171() { # bug20592
9245         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9246 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9247         $LCTL set_param fail_loc=0x50e
9248         $LCTL set_param fail_val=3000
9249         multiop_bg_pause $DIR/$tfile O_s || true
9250         local MULTIPID=$!
9251         kill -USR1 $MULTIPID
9252         # cause log dump
9253         sleep 3
9254         wait $MULTIPID
9255         if dmesg | grep "recursive fault"; then
9256                 error "caught a recursive fault"
9257         fi
9258         $LCTL set_param fail_loc=0
9259         true
9260 }
9261 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
9262
9263 # it would be good to share it with obdfilter-survey/libecho code
9264 setup_obdecho_osc () {
9265         local rc=0
9266         local ost_nid=$1
9267         local obdfilter_name=$2
9268         echo "Creating new osc for $obdfilter_name on $ost_nid"
9269         # make sure we can find loopback nid
9270         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
9271
9272         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
9273                            ${obdfilter_name}_osc_UUID || rc=2; }
9274         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
9275                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
9276         return $rc
9277 }
9278
9279 cleanup_obdecho_osc () {
9280         local obdfilter_name=$1
9281         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
9282         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
9283         return 0
9284 }
9285
9286 obdecho_create_test() {
9287         local OBD=$1
9288         local node=$2
9289         local rc=0
9290         local id
9291         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
9292         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
9293                            rc=2; }
9294         if [ $rc -eq 0 ]; then
9295             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
9296             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
9297         fi
9298         echo "New object id is $id"
9299         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
9300                            rc=4; }
9301         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
9302                                         "cleanup" || rc=5; }
9303         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
9304                                         "detach" || rc=6; }
9305         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
9306         return $rc
9307 }
9308
9309 test_180a() {
9310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9311         remote_ost_nodsh && skip "remote OST with nodsh" && return
9312         local rc=0
9313         local rmmod_local=0
9314
9315         if ! module_loaded obdecho; then
9316             load_module obdecho/obdecho
9317             rmmod_local=1
9318         fi
9319
9320         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
9321         local host=$(lctl get_param -n osc.$osc.import |
9322                              awk '/current_connection:/ {print $2}' )
9323         local target=$(lctl get_param -n osc.$osc.import |
9324                              awk '/target:/ {print $2}' )
9325         target=${target%_UUID}
9326
9327         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
9328         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
9329         [[ -n $target ]] && cleanup_obdecho_osc $target
9330         [ $rmmod_local -eq 1 ] && rmmod obdecho
9331         return $rc
9332 }
9333 run_test 180a "test obdecho on osc"
9334
9335 test_180b() {
9336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9337         remote_ost_nodsh && skip "remote OST with nodsh" && return
9338         local rc=0
9339         local rmmod_remote=0
9340
9341         do_facet ost1 "lsmod | grep -q obdecho || "                      \
9342                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
9343                       "modprobe obdecho; }" && rmmod_remote=1
9344         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
9345         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
9346         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
9347         return $rc
9348 }
9349 run_test 180b "test obdecho directly on obdfilter"
9350
9351 test_181() { # bug 22177
9352         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9353         # create enough files to index the directory
9354         createmany -o $DIR/$tdir/foobar 4000
9355         # print attributes for debug purpose
9356         lsattr -d .
9357         # open dir
9358         multiop_bg_pause $DIR/$tdir D_Sc || return 1
9359         MULTIPID=$!
9360         # remove the files & current working dir
9361         unlinkmany $DIR/$tdir/foobar 4000
9362         rmdir $DIR/$tdir
9363         kill -USR1 $MULTIPID
9364         wait $MULTIPID
9365         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9366         return 0
9367 }
9368 run_test 181 "Test open-unlinked dir ========================"
9369
9370 test_182() {
9371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9372         # disable MDC RPC lock wouldn't crash client
9373         local fcount=1000
9374         local tcount=4
9375
9376         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9377 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9378         $LCTL set_param fail_loc=0x804
9379
9380         for (( i=0; i < $tcount; i++ )) ; do
9381                 mkdir $DIR/$tdir/$i
9382                 createmany -o $DIR/$tdir/$i/f- $fcount &
9383         done
9384         wait
9385
9386         for (( i=0; i < $tcount; i++ )) ; do
9387                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9388         done
9389         wait
9390
9391         rm -rf $DIR/$tdir
9392
9393         $LCTL set_param fail_loc=0
9394 }
9395 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9396
9397 test_183() { # LU-2275
9398         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9399         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9400         echo aaa > $DIR/$tdir/$tfile
9401
9402 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9403         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9404
9405         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9406         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9407
9408         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9409
9410         # Flush negative dentry cache
9411         touch $DIR/$tdir/$tfile
9412
9413         # We are not checking for any leaked references here, they'll
9414         # become evident next time we do cleanup with module unload.
9415         rm -rf $DIR/$tdir
9416 }
9417 run_test 183 "No crash or request leak in case of strange dispositions ========"
9418
9419 test_185() { # LU-2441
9420         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9421         touch $DIR/$tdir/spoo
9422         local mtime1=$(stat -c "%Y" $DIR/$tdir)
9423         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
9424                 error "cannot create/write a volatile file"
9425         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
9426                 error "FID is still valid after close"
9427
9428         multiop_bg_pause $DIR/$tdir vVw4096_c
9429         local multi_pid=$!
9430
9431         local OLD_IFS=$IFS
9432         IFS=":"
9433         local fidv=($fid)
9434         IFS=$OLD_IFS
9435         # assume that the next FID for this client is sequential, since stdout
9436         # is unfortunately eaten by multiop_bg_pause
9437         local n=$((${fidv[1]} + 1))
9438         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
9439         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
9440                 error "FID is missing before close"
9441         kill -USR1 $multi_pid
9442         # 1 second delay, so if mtime change we will see it
9443         sleep 1
9444         local mtime2=$(stat -c "%Y" $DIR/$tdir)
9445         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
9446 }
9447 run_test 185 "Volatile file support"
9448
9449 check_swap_layouts_support()
9450 {
9451         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
9452                 { skip "Does not support layout lock."; return 0; }
9453         return 1
9454 }
9455
9456 # test suite 184 is for LU-2016, LU-2017
9457 test_184a() {
9458         check_swap_layouts_support && return 0
9459
9460         dir0=$DIR/$tdir/$testnum
9461         test_mkdir -p $dir0 || error "creating dir $dir0"
9462         ref1=/etc/passwd
9463         ref2=/etc/group
9464         file1=$dir0/f1
9465         file2=$dir0/f2
9466         $SETSTRIPE -c1 $file1
9467         cp $ref1 $file1
9468         $SETSTRIPE -c2 $file2
9469         cp $ref2 $file2
9470         gen1=$($GETSTRIPE -g $file1)
9471         gen2=$($GETSTRIPE -g $file2)
9472
9473         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
9474         gen=$($GETSTRIPE -g $file1)
9475         [[ $gen1 != $gen ]] ||
9476                 "Layout generation on $file1 does not change"
9477         gen=$($GETSTRIPE -g $file2)
9478         [[ $gen2 != $gen ]] ||
9479                 "Layout generation on $file2 does not change"
9480
9481         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
9482         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
9483 }
9484 run_test 184a "Basic layout swap"
9485
9486 test_184b() {
9487         check_swap_layouts_support && return 0
9488
9489         dir0=$DIR/$tdir/$testnum
9490         mkdir -p $dir0 || error "creating dir $dir0"
9491         file1=$dir0/f1
9492         file2=$dir0/f2
9493         file3=$dir0/f3
9494         dir1=$dir0/d1
9495         dir2=$dir0/d2
9496         mkdir $dir1 $dir2
9497         $SETSTRIPE -c1 $file1
9498         $SETSTRIPE -c2 $file2
9499         $SETSTRIPE -c1 $file3
9500         chown $RUNAS_ID $file3
9501         gen1=$($GETSTRIPE -g $file1)
9502         gen2=$($GETSTRIPE -g $file2)
9503
9504         $LFS swap_layouts $dir1 $dir2 &&
9505                 error "swap of directories layouts should fail"
9506         $LFS swap_layouts $dir1 $file1 &&
9507                 error "swap of directory and file layouts should fail"
9508         $RUNAS $LFS swap_layouts $file1 $file2 &&
9509                 error "swap of file we cannot write should fail"
9510         $LFS swap_layouts $file1 $file3 &&
9511                 error "swap of file with different owner should fail"
9512         /bin/true # to clear error code
9513 }
9514 run_test 184b "Forbidden layout swap (will generate errors)"
9515
9516 test_184c() {
9517         check_swap_layouts_support && return 0
9518
9519         local dir0=$DIR/$tdir/$testnum
9520         mkdir -p $dir0 || error "creating dir $dir0"
9521
9522         local ref1=$dir0/ref1
9523         local ref2=$dir0/ref2
9524         local file1=$dir0/file1
9525         local file2=$dir0/file2
9526         # create a file large enough for the concurent test
9527         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
9528         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
9529         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
9530
9531         cp $ref2 $file2
9532         dd if=$ref1 of=$file1 bs=16k &
9533         local DD_PID=$!
9534
9535         sleep 0.$((RANDOM % 5 + 1))
9536
9537         $LFS swap_layouts $file1 $file2
9538         local rc=$?
9539         wait $DD_PID
9540         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
9541         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
9542
9543         # how many bytes copied before swapping layout
9544         local copied=`stat -c %s $file2`
9545         local remaining=`stat -c %s $ref1`
9546         remaining=$((remaining - copied))
9547         echo "Copied $copied bytes before swapping layout..."
9548
9549         cmp -n $copied $file1 $ref2 | grep differ &&
9550                 error "Content mismatch [0, $copied) of ref2 and file1"
9551         cmp -n $copied $file2 $ref1 ||
9552                 error "Content mismatch [0, $copied) of ref1 and file2"
9553         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
9554                 error "Content mismatch [$copied, EOF) of ref1 and file1"
9555
9556         # clean up
9557         rm -f $ref1 $ref2 $file1 $file2
9558 }
9559 run_test 184c "Concurrent write and layout swap"
9560
9561 # OST pools tests
9562 check_file_in_pool()
9563 {
9564         local file=$1
9565         local pool=$2
9566         local tlist="$3"
9567         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9568         for i in $res
9569         do
9570                 for t in $tlist ; do
9571                         [ "$i" -eq "$t" ] && continue 2
9572                 done
9573
9574                 echo "pool list: $tlist"
9575                 echo "striping: $res"
9576                 error_noexit "$file not allocated in $pool"
9577                 return 1
9578         done
9579         return 0
9580 }
9581
9582 pool_add() {
9583         echo "Creating new pool"
9584         local pool=$1
9585
9586         create_pool $FSNAME.$pool ||
9587                 { error_noexit "No pool created, result code $?"; return 1; }
9588         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9589                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9590 }
9591
9592 pool_add_targets() {
9593         echo "Adding targets to pool"
9594         local pool=$1
9595         local first=$2
9596         local last=$3
9597         local step=${4:-1}
9598
9599         local list=$(seq $first $step $last)
9600
9601         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9602         do_facet mgs $LCTL pool_add \
9603                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9604         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9605                         | sort -u | tr '\n' ' ' " "$t" || { 
9606                 error_noexit "Add to pool failed"
9607                 return 1
9608         }
9609         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9610         local addcount=$(((last - first) / step + 1))
9611         [ $lfscount -eq $addcount ] || {
9612                 error_noexit "lfs pool_list bad ost count" \
9613                                                 "$lfscount != $addcount"
9614                 return 2
9615         }
9616 }
9617
9618 pool_set_dir() {
9619         local pool=$1
9620         local tdir=$2
9621         echo "Setting pool on directory $tdir"
9622
9623         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9624
9625         error_noexit "Cannot set pool $pool to $tdir"
9626         return 1
9627 }
9628
9629 pool_check_dir() {
9630         local pool=$1
9631         local tdir=$2
9632         echo "Checking pool on directory $tdir"
9633
9634         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9635         [ "$res" = "$pool" ] && return 0
9636
9637         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9638         return 1
9639 }
9640
9641 pool_dir_rel_path() {
9642         echo "Testing relative path works well"
9643         local pool=$1
9644         local tdir=$2
9645         local root=$3
9646
9647         mkdir -p $root/$tdir/$tdir
9648         cd $root/$tdir
9649         pool_set_dir $pool $tdir          || return 1
9650         pool_set_dir $pool ./$tdir        || return 2
9651         pool_set_dir $pool ../$tdir       || return 3
9652         pool_set_dir $pool ../$tdir/$tdir || return 4
9653         rm -rf $tdir; cd - > /dev/null
9654 }
9655
9656 pool_alloc_files() {
9657         echo "Checking files allocation from directory pool"
9658         local pool=$1
9659         local tdir=$2
9660         local count=$3
9661         local tlist="$4"
9662
9663         local failed=0
9664         for i in $(seq -w 1 $count)
9665         do
9666                 local file=$tdir/file-$i
9667                 touch $file
9668                 check_file_in_pool $file $pool "$tlist" || \
9669                         failed=$((failed + 1))
9670         done
9671         [ "$failed" = 0 ] && return 0
9672
9673         error_noexit "$failed files not allocated in $pool"
9674         return 1
9675 }
9676
9677 pool_create_files() {
9678         echo "Creating files in pool"
9679         local pool=$1
9680         local tdir=$2
9681         local count=$3
9682         local tlist="$4"
9683
9684         mkdir -p $tdir
9685         local failed=0
9686         for i in $(seq -w 1 $count)
9687         do
9688                 local file=$tdir/spoo-$i
9689                 $SETSTRIPE -p $pool $file
9690                 check_file_in_pool $file $pool "$tlist" || \
9691                         failed=$((failed + 1))
9692         done
9693         [ "$failed" = 0 ] && return 0
9694
9695         error_noexit "$failed files not allocated in $pool"
9696         return 1
9697 }
9698
9699 pool_lfs_df() {
9700         echo "Checking 'lfs df' output"
9701         local pool=$1
9702
9703         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9704                         tr '\n' ' ')
9705         local res=$($LFS df --pool $FSNAME.$pool |
9706                         awk '{print $1}' |
9707                         grep "$FSNAME-OST" |
9708                         tr '\n' ' ')
9709         [ "$res" = "$t" ] && return 0
9710
9711         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9712         return 1
9713 }
9714
9715 pool_file_rel_path() {
9716         echo "Creating files in a pool with relative pathname"
9717         local pool=$1
9718         local tdir=$2
9719
9720         mkdir -p $tdir ||
9721                 { error_noexit "unable to create $tdir"; return 1 ; }
9722         local file="/..$tdir/$tfile-1"
9723         $SETSTRIPE -p $pool $file ||
9724                 { error_noexit "unable to create $file" ; return 2 ; }
9725
9726         cd $tdir
9727         $SETSTRIPE -p $pool $tfile-2 || {
9728                 error_noexit "unable to create $tfile-2 in $tdir"
9729                 return 3
9730         }
9731 }
9732
9733 pool_remove_first_target() {
9734         echo "Removing first target from a pool"
9735         local pool=$1
9736
9737         local pname="lov.$FSNAME-*.pools.$pool"
9738         local t=$($LCTL get_param -n $pname | head -1)
9739         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9740         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9741                 error_noexit "$t not removed from $FSNAME.$pool"
9742                 return 1
9743         }
9744 }
9745
9746 pool_remove_all_targets() {
9747         echo "Removing all targets from pool"
9748         local pool=$1
9749         local file=$2
9750         local pname="lov.$FSNAME-*.pools.$pool"
9751         for t in $($LCTL get_param -n $pname | sort -u)
9752         do
9753                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9754         done
9755         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9756                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9757                 return 1
9758         }
9759         # striping on an empty/nonexistant pool should fall back 
9760         # to "pool of everything"
9761         touch $file || {
9762                 error_noexit "failed to use fallback striping for empty pool"
9763                 return 2
9764         }
9765         # setstripe on an empty pool should fail
9766         $SETSTRIPE -p $pool $file 2>/dev/null && {
9767                 error_noexit "expected failure when creating file" \
9768                                                         "with empty pool"
9769                 return 3
9770         }
9771         return 0
9772 }
9773
9774 pool_remove() {
9775         echo "Destroying pool"
9776         local pool=$1
9777         local file=$2
9778
9779         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9780
9781         sleep 2
9782         # striping on an empty/nonexistant pool should fall back 
9783         # to "pool of everything"
9784         touch $file || {
9785                 error_noexit "failed to use fallback striping for missing pool"
9786                 return 1
9787         }
9788         # setstripe on an empty pool should fail
9789         $SETSTRIPE -p $pool $file 2>/dev/null && {
9790                 error_noexit "expected failure when creating file" \
9791                                                         "with missing pool"
9792                 return 2
9793         }
9794
9795         # get param should return err once pool is gone
9796         if wait_update $HOSTNAME "lctl get_param -n \
9797                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9798         then
9799                 remove_pool_from_list $FSNAME.$pool
9800                 return 0
9801         fi
9802         error_noexit "Pool $FSNAME.$pool is not destroyed"
9803         return 3
9804 }
9805
9806 test_200() {
9807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9808         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9809
9810         local POOL=${POOL:-cea1}
9811         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9812         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9813         # Pool OST targets
9814         local first_ost=0
9815         local last_ost=$(($OSTCOUNT - 1))
9816         local ost_step=2
9817         local ost_list=$(seq $first_ost $ost_step $last_ost)
9818         local ost_range="$first_ost $last_ost $ost_step"
9819         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9820         local file_dir=$POOL_ROOT/file_tst
9821
9822         local rc=0
9823         while : ; do
9824                 # former test_200a test_200b
9825                 pool_add $POOL                          || { rc=$? ; break; }
9826                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9827                 # former test_200c test_200d
9828                 mkdir -p $test_path
9829                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9830                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9831                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9832                                                         || { rc=$? ; break; }
9833                 # former test_200e test_200f
9834                 local files=$((OSTCOUNT*3))
9835                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9836                                                         || { rc=$? ; break; }
9837                 pool_create_files $POOL $file_dir $files "$ost_list" \
9838                                                         || { rc=$? ; break; }
9839                 # former test_200g test_200h
9840                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9841                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9842
9843                 # former test_201a test_201b test_201c
9844                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9845
9846                 local f=$test_path/$tfile
9847                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9848                 pool_remove $POOL $f                    || { rc=$? ; break; }
9849                 break
9850         done
9851
9852         cleanup_pools
9853         return $rc
9854 }
9855 run_test 200 "OST pools"
9856
9857 # usage: default_attr <count | size | offset>
9858 default_attr() {
9859         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9860 }
9861
9862 # usage: check_default_stripe_attr
9863 check_default_stripe_attr() {
9864         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9865         case $1 in
9866         --stripe-count|--count)
9867                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9868         --stripe-size|--size)
9869                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9870         --stripe-index|--index)
9871                 EXPECTED=-1;;
9872         *)
9873                 error "unknown getstripe attr '$1'"
9874         esac
9875
9876         [ $ACTUAL != $EXPECTED ] &&
9877                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9878 }
9879
9880 test_204a() {
9881         test_mkdir -p $DIR/$tdir
9882         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9883
9884         check_default_stripe_attr --stripe-count
9885         check_default_stripe_attr --stripe-size
9886         check_default_stripe_attr --stripe-index
9887
9888         return 0
9889 }
9890 run_test 204a "Print default stripe attributes ================="
9891
9892 test_204b() {
9893         test_mkdir -p $DIR/$tdir
9894         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9895
9896         check_default_stripe_attr --stripe-size
9897         check_default_stripe_attr --stripe-index
9898
9899         return 0
9900 }
9901 run_test 204b "Print default stripe size and offset  ==========="
9902
9903 test_204c() {
9904         test_mkdir -p $DIR/$tdir
9905         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9906
9907         check_default_stripe_attr --stripe-count
9908         check_default_stripe_attr --stripe-index
9909
9910         return 0
9911 }
9912 run_test 204c "Print default stripe count and offset ==========="
9913
9914 test_204d() {
9915         test_mkdir -p $DIR/$tdir
9916         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9917
9918         check_default_stripe_attr --stripe-count
9919         check_default_stripe_attr --stripe-size
9920
9921         return 0
9922 }
9923 run_test 204d "Print default stripe count and size ============="
9924
9925 test_204e() {
9926         test_mkdir -p $DIR/$tdir
9927         $SETSTRIPE -d $DIR/$tdir
9928
9929         check_default_stripe_attr --stripe-count --raw
9930         check_default_stripe_attr --stripe-size --raw
9931         check_default_stripe_attr --stripe-index --raw
9932
9933         return 0
9934 }
9935 run_test 204e "Print raw stripe attributes ================="
9936
9937 test_204f() {
9938         test_mkdir -p $DIR/$tdir
9939         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9940
9941         check_default_stripe_attr --stripe-size --raw
9942         check_default_stripe_attr --stripe-index --raw
9943
9944         return 0
9945 }
9946 run_test 204f "Print raw stripe size and offset  ==========="
9947
9948 test_204g() {
9949         test_mkdir -p $DIR/$tdir
9950         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9951
9952         check_default_stripe_attr --stripe-count --raw
9953         check_default_stripe_attr --stripe-index --raw
9954
9955         return 0
9956 }
9957 run_test 204g "Print raw stripe count and offset ==========="
9958
9959 test_204h() {
9960         test_mkdir -p $DIR/$tdir
9961         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9962
9963         check_default_stripe_attr --stripe-count --raw
9964         check_default_stripe_attr --stripe-size --raw
9965
9966         return 0
9967 }
9968 run_test 204h "Print raw stripe count and size ============="
9969
9970 # Figure out which job scheduler is being used, if any,
9971 # or use a fake one
9972 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9973         JOBENV=SLURM_JOB_ID
9974 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9975         JOBENV=LSB_JOBID
9976 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9977         JOBENV=PBS_JOBID
9978 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9979         JOBENV=LOADL_STEP_ID
9980 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9981         JOBENV=JOB_ID
9982 else
9983         JOBENV=FAKE_JOBID
9984 fi
9985
9986 verify_jobstats() {
9987         local cmd=$1
9988         local target=$2
9989
9990         # clear old jobstats
9991         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9992         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9993
9994         # use a new JobID for this test, or we might see an old one
9995         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9996
9997         JOBVAL=${!JOBENV}
9998         log "Test: $cmd"
9999         log "Using JobID environment variable $JOBENV=$JOBVAL"
10000
10001         if [ $JOBENV = "FAKE_JOBID" ]; then
10002                 FAKE_JOBID=$JOBVAL $cmd
10003         else
10004                 $cmd
10005         fi
10006
10007         if [ "$target" = "mdt" -o "$target" = "both" ]; then
10008                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
10009                 do_facet $FACET lctl get_param mdt.*.job_stats |
10010                         grep $JOBVAL || error "No job stats found on MDT $FACET"
10011         fi
10012         if [ "$target" = "ost" -o "$target" = "both" ]; then
10013                 FACET=ost1
10014                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
10015                         grep $JOBVAL || error "No job stats found on OST $FACET"
10016         fi
10017 }
10018
10019 test_205() { # Job stats
10020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10021         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
10022                 skip "Server doesn't support jobstats" && return 0
10023
10024         local cmd
10025         OLD_JOBENV=`$LCTL get_param -n jobid_var`
10026         if [ $OLD_JOBENV != $JOBENV ]; then
10027                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
10028                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
10029                         $JOBENV || return 1
10030         fi
10031
10032         # mkdir
10033         cmd="mkdir $DIR/$tfile"
10034         verify_jobstats "$cmd" "mdt"
10035         # rmdir
10036         cmd="rm -fr $DIR/$tfile"
10037         verify_jobstats "$cmd" "mdt"
10038         # mknod
10039         cmd="mknod $DIR/$tfile c 1 3"
10040         verify_jobstats "$cmd" "mdt"
10041         # unlink
10042         cmd="rm -f $DIR/$tfile"
10043         verify_jobstats "$cmd" "mdt"
10044         # open & close
10045         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
10046         verify_jobstats "$cmd" "mdt"
10047         # setattr
10048         cmd="touch $DIR/$tfile"
10049         verify_jobstats "$cmd" "both"
10050         # write
10051         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
10052         verify_jobstats "$cmd" "ost"
10053         # read
10054         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
10055         verify_jobstats "$cmd" "ost"
10056         # truncate
10057         cmd="$TRUNCATE $DIR/$tfile 0"
10058         verify_jobstats "$cmd" "both"
10059         # rename
10060         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
10061         verify_jobstats "$cmd" "mdt"
10062
10063         # cleanup
10064         rm -f $DIR/jobstats_test_rename
10065
10066         if [ $OLD_JOBENV != $JOBENV ]; then
10067                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
10068                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
10069                         $OLD_JOBENV || return 1
10070         fi
10071 }
10072 run_test 205 "Verify job stats"
10073
10074 # LU-1480, LU-1773 and LU-1657
10075 test_206() {
10076         mkdir -p $DIR/$tdir
10077         lfs setstripe -c -1 $DIR/$tdir
10078 #define OBD_FAIL_LOV_INIT 0x1403
10079         $LCTL set_param fail_loc=0xa0001403
10080         $LCTL set_param fail_val=1
10081         touch $DIR/$tdir/$tfile || true
10082 }
10083 run_test 206 "fail lov_init_raid0() doesn't lbug"
10084
10085 test_207a() {
10086         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10087         local fsz=`stat -c %s $DIR/$tfile`
10088         cancel_lru_locks mdc
10089
10090         # do not return layout in getattr intent
10091 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
10092         $LCTL set_param fail_loc=0x170
10093         local sz=`stat -c %s $DIR/$tfile`
10094
10095         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
10096
10097         rm -rf $DIR/$tfile
10098 }
10099 run_test 207a "can refresh layout at glimpse"
10100
10101 test_207b() {
10102         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10103         local cksum=`md5sum $DIR/$tfile`
10104         local fsz=`stat -c %s $DIR/$tfile`
10105         cancel_lru_locks mdc
10106         cancel_lru_locks osc
10107
10108         # do not return layout in getattr intent
10109 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
10110         $LCTL set_param fail_loc=0x171
10111
10112         # it will refresh layout after the file is opened but before read issues
10113         echo checksum is "$cksum"
10114         echo "$cksum" |md5sum -c --quiet || error "file differs"
10115
10116         rm -rf $DIR/$tfile
10117 }
10118 run_test 207b "can refresh layout at open"
10119
10120 test_212() {
10121         size=`date +%s`
10122         size=$((size % 8192 + 1))
10123         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
10124         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
10125         rm -f $DIR/f212 $DIR/f212.xyz
10126 }
10127 run_test 212 "Sendfile test ============================================"
10128
10129 test_213() {
10130         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
10131         cancel_lru_locks osc
10132         lctl set_param fail_loc=0x8000040f
10133         # generate a read lock
10134         cat $DIR/$tfile > /dev/null
10135         # write to the file, it will try to cancel the above read lock.
10136         cat /etc/hosts >> $DIR/$tfile
10137 }
10138 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
10139
10140 test_214() { # for bug 20133
10141         test_mkdir -p $DIR/d214p/d214c
10142         for (( i=0; i < 340; i++ )) ; do
10143                 touch $DIR/d214p/d214c/a$i
10144         done
10145
10146         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
10147         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
10148         ls $DIR/d214c || error "ls $DIR/d214c failed"
10149         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
10150 }
10151 run_test 214 "hash-indexed directory test - bug 20133"
10152
10153 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
10154 create_lnet_proc_files() {
10155         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
10156         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
10157
10158         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
10159         rm -f "$TMP/lnet_$1.sys_tmp"
10160 }
10161
10162 # counterpart of create_lnet_proc_files
10163 remove_lnet_proc_files() {
10164         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
10165 }
10166
10167 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10168 # 3rd arg as regexp for body
10169 check_lnet_proc_stats() {
10170         local l=$(cat "$TMP/lnet_$1" |wc -l)
10171         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
10172
10173         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
10174 }
10175
10176 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10177 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
10178 # optional and can be regexp for 2nd line (lnet.routes case)
10179 check_lnet_proc_entry() {
10180         local blp=2            # blp stands for 'position of 1st line of body'
10181         [ "$5" = "" ] || blp=3 # lnet.routes case
10182
10183         local l=$(cat "$TMP/lnet_$1" |wc -l)
10184         # subtracting one from $blp because the body can be empty
10185         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
10186
10187         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
10188                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
10189
10190         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
10191                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
10192
10193         # bail out if any unexpected line happened
10194         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
10195         [ "$?" != 0 ] || error "$2 misformatted"
10196 }
10197
10198 test_215() { # for bugs 18102, 21079, 21517
10199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10200         local N='(0|[1-9][0-9]*)'       # non-negative numeric
10201         local P='[1-9][0-9]*'           # positive numeric
10202         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
10203         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
10204         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
10205         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
10206
10207         local L1 # regexp for 1st line
10208         local L2 # regexp for 2nd line (optional)
10209         local BR # regexp for the rest (body)
10210
10211         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
10212         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
10213         create_lnet_proc_files "stats"
10214         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
10215         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
10216         remove_lnet_proc_files "stats"
10217
10218         # /proc/sys/lnet/routes should look like this:
10219         # Routing disabled/enabled
10220         # net hops state router
10221         # where net is a string like tcp0, hops >= 0, state is up/down,
10222         # router is a string like 192.168.1.1@tcp2
10223         L1="^Routing (disabled|enabled)$"
10224         L2="^net +hops +state +router$"
10225         BR="^$NET +$N +(up|down) +$NID$"
10226         create_lnet_proc_files "routes"
10227         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
10228         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
10229         remove_lnet_proc_files "routes"
10230
10231         # /proc/sys/lnet/routers should look like this:
10232         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
10233         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
10234         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
10235         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
10236         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
10237         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
10238         create_lnet_proc_files "routers"
10239         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
10240         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
10241         remove_lnet_proc_files "routers"
10242
10243         # /proc/sys/lnet/peers should look like this:
10244         # nid refs state last max rtr min tx min queue
10245         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
10246         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
10247         # numeric (0 or >0 or <0), queue >= 0.
10248         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
10249         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
10250         create_lnet_proc_files "peers"
10251         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
10252         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
10253         remove_lnet_proc_files "peers"
10254
10255         # /proc/sys/lnet/buffers  should look like this:
10256         # pages count credits min
10257         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
10258         L1="^pages +count +credits +min$"
10259         BR="^ +$N +$N +$I +$I$"
10260         create_lnet_proc_files "buffers"
10261         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
10262         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
10263         remove_lnet_proc_files "buffers"
10264
10265         # /proc/sys/lnet/nis should look like this:
10266         # nid status alive refs peer rtr max tx min
10267         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
10268         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
10269         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
10270         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
10271         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
10272         create_lnet_proc_files "nis"
10273         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
10274         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
10275         remove_lnet_proc_files "nis"
10276
10277         # can we successfully write to /proc/sys/lnet/stats?
10278         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
10279         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
10280 }
10281 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
10282
10283 test_216() { # bug 20317
10284         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10285         remote_ost_nodsh && skip "remote OST with nodsh" && return
10286         local node
10287         local p="$TMP/sanityN-$TESTNAME.parameters"
10288         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
10289         for node in $(osts_nodes); do
10290                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
10291                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
10292                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
10293         done
10294         clear_osc_stats
10295
10296         # agressive lockless i/o settings
10297         for node in $(osts_nodes); do
10298                 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'
10299         done
10300         lctl set_param -n osc.*.contention_seconds 60
10301
10302         $DIRECTIO write $DIR/$tfile 0 10 4096
10303         $CHECKSTAT -s 40960 $DIR/$tfile
10304
10305         # disable lockless i/o
10306         for node in $(osts_nodes); do
10307                 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'
10308         done
10309         lctl set_param -n osc.*.contention_seconds 0
10310         clear_osc_stats
10311
10312         dd if=/dev/zero of=$DIR/$tfile count=0
10313         $CHECKSTAT -s 0 $DIR/$tfile
10314
10315         restore_lustre_params <$p
10316         rm -f $p
10317         rm $DIR/$tfile
10318 }
10319 run_test 216 "check lockless direct write works and updates file size and kms correctly"
10320
10321 test_217() { # bug 22430
10322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10323         local node
10324         local nid
10325
10326         for node in $(nodes_list); do
10327                 nid=$(host_nids_address $node $NETTYPE)
10328                 if [[ $nid = *-* ]] ; then
10329                         echo "lctl ping $nid@$NETTYPE"
10330                         lctl ping $nid@$NETTYPE
10331                 else
10332                         echo "skipping $node (no hyphen detected)"
10333                 fi
10334         done
10335 }
10336 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
10337
10338 test_218() {
10339        # do directio so as not to populate the page cache
10340        log "creating a 10 Mb file"
10341        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10342        log "starting reads"
10343        dd if=$DIR/$tfile of=/dev/null bs=4096 &
10344        log "truncating the file"
10345        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10346        log "killing dd"
10347        kill %+ || true # reads might have finished
10348        echo "wait until dd is finished"
10349        wait
10350        log "removing the temporary file"
10351        rm -rf $DIR/$tfile || error "tmp file removal failed"
10352 }
10353 run_test 218 "parallel read and truncate should not deadlock ======================="
10354
10355 test_219() {
10356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10357         # write one partial page
10358         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
10359         # set no grant so vvp_io_commit_write will do sync write
10360         $LCTL set_param fail_loc=0x411
10361         # write a full page at the end of file
10362         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
10363
10364         $LCTL set_param fail_loc=0
10365         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
10366         $LCTL set_param fail_loc=0x411
10367         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
10368 }
10369 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
10370
10371 test_220() { #LU-325
10372         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10373         remote_ost_nodsh && skip "remote OST with nodsh" && return
10374         local OSTIDX=0
10375
10376         test_mkdir -p $DIR/$tdir
10377         local OST=$(lfs osts | grep ${OSTIDX}": " | \
10378                 awk '{print $2}' | sed -e 's/_UUID$//')
10379
10380         # on the mdt's osc
10381         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
10382         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
10383                         osc.$mdtosc_proc1.prealloc_last_id)
10384         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
10385                         osc.$mdtosc_proc1.prealloc_next_id)
10386
10387         $LFS df -i
10388
10389         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
10390         #define OBD_FAIL_OST_ENOINO              0x229
10391         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
10392         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
10393         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
10394
10395         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
10396
10397         MDSOBJS=$((last_id - next_id))
10398         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
10399
10400         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
10401         echo "OST still has $count kbytes free"
10402
10403         echo "create $MDSOBJS files @next_id..."
10404         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
10405
10406         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10407                         osc.$mdtosc_proc1.prealloc_last_id)
10408         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10409                         osc.$mdtosc_proc1.prealloc_next_id)
10410
10411         echo "after creation, last_id=$last_id2, next_id=$next_id2"
10412         $LFS df -i
10413
10414         echo "cleanup..."
10415
10416         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
10417         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
10418
10419         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
10420         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
10421         echo "unlink $MDSOBJS files @$next_id..."
10422         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
10423 }
10424 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
10425
10426 test_221() {
10427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10428         dd if=`which date` of=$MOUNT/date oflag=sync
10429         chmod +x $MOUNT/date
10430
10431         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
10432         $LCTL set_param fail_loc=0x80001401
10433
10434         $MOUNT/date > /dev/null
10435         rm -f $MOUNT/date
10436 }
10437 run_test 221 "make sure fault and truncate race to not cause OOM"
10438
10439 test_222a () {
10440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10441        rm -rf $DIR/$tdir
10442        test_mkdir -p $DIR/$tdir
10443        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10444        createmany -o $DIR/$tdir/$tfile 10
10445        cancel_lru_locks mdc
10446        cancel_lru_locks osc
10447        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10448        $LCTL set_param fail_loc=0x31a
10449        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
10450        $LCTL set_param fail_loc=0
10451        rm -r $DIR/$tdir
10452 }
10453 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
10454
10455 test_222b () {
10456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10457        rm -rf $DIR/$tdir
10458        test_mkdir -p $DIR/$tdir
10459        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10460        createmany -o $DIR/$tdir/$tfile 10
10461        cancel_lru_locks mdc
10462        cancel_lru_locks osc
10463        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10464        $LCTL set_param fail_loc=0x31a
10465        rm -r $DIR/$tdir || "AGL for rmdir failed"
10466        $LCTL set_param fail_loc=0
10467 }
10468 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
10469
10470 test_223 () {
10471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10472        rm -rf $DIR/$tdir
10473        test_mkdir -p $DIR/$tdir
10474        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10475        createmany -o $DIR/$tdir/$tfile 10
10476        cancel_lru_locks mdc
10477        cancel_lru_locks osc
10478        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
10479        $LCTL set_param fail_loc=0x31b
10480        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
10481        $LCTL set_param fail_loc=0
10482        rm -r $DIR/$tdir
10483 }
10484 run_test 223 "osc reenqueue if without AGL lock granted ======================="
10485
10486 test_224a() { # LU-1039, MRP-303
10487         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10488         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
10489         $LCTL set_param fail_loc=0x508
10490         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
10491         $LCTL set_param fail_loc=0
10492         df $DIR
10493 }
10494 run_test 224a "Don't panic on bulk IO failure"
10495
10496 test_224b() { # LU-1039, MRP-303
10497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10498         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
10499         cancel_lru_locks osc
10500         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
10501         $LCTL set_param fail_loc=0x515
10502         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
10503         $LCTL set_param fail_loc=0
10504         df $DIR
10505 }
10506 run_test 224b "Don't panic on bulk IO failure"
10507
10508 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
10509 test_225a () {
10510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10511        if [ -z ${MDSSURVEY} ]; then
10512               skip_env "mds-survey not found" && return
10513        fi
10514        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10515             { skip "Need MDS version at least 2.2.51"; return; }
10516
10517        local mds=$(facet_host $SINGLEMDS)
10518        local target=$(do_nodes $mds 'lctl dl' | \
10519                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10520
10521        local cmd1="file_count=1000 thrhi=4"
10522        local cmd2="dir_count=2 layer=mdd stripe_count=0"
10523        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10524        local cmd="$cmd1 $cmd2 $cmd3"
10525
10526        rm -f ${TMP}/mds_survey*
10527        echo + $cmd
10528        eval $cmd || error "mds-survey with zero-stripe failed"
10529        cat ${TMP}/mds_survey*
10530        rm -f ${TMP}/mds_survey*
10531 }
10532 run_test 225a "Metadata survey sanity with zero-stripe"
10533
10534 test_225b () {
10535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10536        if [ -z ${MDSSURVEY} ]; then
10537               skip_env "mds-survey not found" && return
10538        fi
10539        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10540             { skip "Need MDS version at least 2.2.51"; return; }
10541
10542        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
10543               skip_env "Need to mount OST to test" && return
10544        fi
10545
10546        local mds=$(facet_host $SINGLEMDS)
10547        local target=$(do_nodes $mds 'lctl dl' | \
10548                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10549
10550        local cmd1="file_count=1000 thrhi=4"
10551        local cmd2="dir_count=2 layer=mdd stripe_count=1"
10552        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10553        local cmd="$cmd1 $cmd2 $cmd3"
10554
10555        rm -f ${TMP}/mds_survey*
10556        echo + $cmd
10557        eval $cmd || error "mds-survey with stripe_count failed"
10558        cat ${TMP}/mds_survey*
10559        rm -f ${TMP}/mds_survey*
10560 }
10561 run_test 225b "Metadata survey sanity with stripe_count = 1"
10562
10563 mcreate_path2fid () {
10564         local mode=$1
10565         local major=$2
10566         local minor=$3
10567         local name=$4
10568         local desc=$5
10569         local path=$DIR/$tdir/$name
10570         local fid
10571         local rc
10572         local fid_path
10573
10574         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10575                 error "cannot create $desc"
10576
10577         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
10578         rc=$?
10579         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10580
10581         fid_path=$($LFS fid2path $MOUNT $fid)
10582         rc=$?
10583         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10584
10585         [ "$path" == "$fid_path" ] ||
10586                 error "fid2path returned $fid_path, expected $path"
10587
10588         echo "pass with $path and $fid"
10589 }
10590
10591 test_226 () {
10592         rm -rf $DIR/$tdir
10593         mkdir -p $DIR/$tdir
10594
10595         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10596         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10597         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10598         mcreate_path2fid 0040666 0 0 dir "directory"
10599         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10600         mcreate_path2fid 0100666 0 0 file "regular file"
10601         mcreate_path2fid 0120666 0 0 link "symbolic link"
10602         mcreate_path2fid 0140666 0 0 sock "socket"
10603 }
10604 run_test 226 "call path2fid and fid2path on files of all type"
10605
10606 # LU-1299 Executing or running ldd on a truncated executable does not
10607 # cause an out-of-memory condition.
10608 test_227() {
10609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10610         dd if=`which date` of=$MOUNT/date bs=1k count=1
10611         chmod +x $MOUNT/date
10612
10613         $MOUNT/date > /dev/null
10614         ldd $MOUNT/date > /dev/null
10615         rm -f $MOUNT/date
10616 }
10617 run_test 227 "running truncated executable does not cause OOM"
10618
10619 # LU-1512 try to reuse idle OI blocks
10620 test_228a() {
10621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10622         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10623                 skip "non-ldiskfs backend" && return
10624
10625         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10626         local myDIR=$DIR/$tdir
10627
10628         mkdir -p $myDIR
10629         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10630         $LCTL set_param fail_loc=0x80001002
10631         createmany -o $myDIR/t- 10000
10632         $LCTL set_param fail_loc=0
10633         # The guard is current the largest FID holder
10634         touch $myDIR/guard
10635         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10636                     tr -d '[')
10637         local IDX=$(($SEQ % 64))
10638
10639         do_facet $SINGLEMDS sync
10640         # Make sure journal flushed.
10641         sleep 6
10642         local blk1=$(do_facet $SINGLEMDS \
10643                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10644                      grep Blockcount | awk '{print $4}')
10645
10646         # Remove old files, some OI blocks will become idle.
10647         unlinkmany $myDIR/t- 10000
10648         # Create new files, idle OI blocks should be reused.
10649         createmany -o $myDIR/t- 2000
10650         do_facet $SINGLEMDS sync
10651         # Make sure journal flushed.
10652         sleep 6
10653         local blk2=$(do_facet $SINGLEMDS \
10654                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10655                      grep Blockcount | awk '{print $4}')
10656
10657         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10658 }
10659 run_test 228a "try to reuse idle OI blocks"
10660
10661 test_228b() {
10662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10663         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10664                 skip "non-ldiskfs backend" && return
10665
10666         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10667         local myDIR=$DIR/$tdir
10668
10669         mkdir -p $myDIR
10670         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10671         $LCTL set_param fail_loc=0x80001002
10672         createmany -o $myDIR/t- 10000
10673         $LCTL set_param fail_loc=0
10674         # The guard is current the largest FID holder
10675         touch $myDIR/guard
10676         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10677                     tr -d '[')
10678         local IDX=$(($SEQ % 64))
10679
10680         do_facet $SINGLEMDS sync
10681         # Make sure journal flushed.
10682         sleep 6
10683         local blk1=$(do_facet $SINGLEMDS \
10684                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10685                      grep Blockcount | awk '{print $4}')
10686
10687         # Remove old files, some OI blocks will become idle.
10688         unlinkmany $myDIR/t- 10000
10689
10690         # stop the MDT
10691         stop $SINGLEMDS || error "Fail to stop MDT."
10692         # remount the MDT
10693         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10694
10695         df $MOUNT || error "Fail to df."
10696         # Create new files, idle OI blocks should be reused.
10697         createmany -o $myDIR/t- 2000
10698         do_facet $SINGLEMDS sync
10699         # Make sure journal flushed.
10700         sleep 6
10701         local blk2=$(do_facet $SINGLEMDS \
10702                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10703                      grep Blockcount | awk '{print $4}')
10704
10705         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10706 }
10707 run_test 228b "idle OI blocks can be reused after MDT restart"
10708
10709 #LU-1881
10710 test_228c() {
10711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10712         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10713                 skip "non-ldiskfs backend" && return
10714
10715         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10716         local myDIR=$DIR/$tdir
10717
10718         mkdir -p $myDIR
10719         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10720         $LCTL set_param fail_loc=0x80001002
10721         # 20000 files can guarantee there are index nodes in the OI file
10722         createmany -o $myDIR/t- 20000
10723         $LCTL set_param fail_loc=0
10724         # The guard is current the largest FID holder
10725         touch $myDIR/guard
10726         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10727                     tr -d '[')
10728         local IDX=$(($SEQ % 64))
10729
10730         do_facet $SINGLEMDS sync
10731         # Make sure journal flushed.
10732         sleep 6
10733         local blk1=$(do_facet $SINGLEMDS \
10734                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10735                      grep Blockcount | awk '{print $4}')
10736
10737         # Remove old files, some OI blocks will become idle.
10738         unlinkmany $myDIR/t- 20000
10739         rm -f $myDIR/guard
10740         # The OI file should become empty now
10741
10742         # Create new files, idle OI blocks should be reused.
10743         createmany -o $myDIR/t- 2000
10744         do_facet $SINGLEMDS sync
10745         # Make sure journal flushed.
10746         sleep 6
10747         local blk2=$(do_facet $SINGLEMDS \
10748                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10749                      grep Blockcount | awk '{print $4}')
10750
10751         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10752 }
10753 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10754
10755 test_230a() {
10756         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10757         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10758         local MDTIDX=1
10759
10760         mkdir -p $DIR/$tdir/test_230_local
10761         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10762         [ $mdt_idx -ne 0 ] &&
10763                 error "create local directory on wrong MDT $mdt_idx"
10764
10765         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10766                         error "create remote directory failed"
10767         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10768         [ $mdt_idx -ne $MDTIDX ] &&
10769                 error "create remote directory on wrong MDT $mdt_idx"
10770
10771         createmany -o $DIR/$tdir/test_230/t- 10 ||
10772                 error "create files on remote directory failed"
10773         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10774         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10775         rm -r $DIR/$tdir || error "unlink remote directory failed"
10776 }
10777 run_test 230a "Create remote directory and files under the remote directory"
10778
10779 test_230b() {
10780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10781         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10782         local MDTIDX=1
10783         local remote_dir=$DIR/$tdir/remote_dir
10784         local rc=0
10785
10786         mkdir -p $DIR/$tdir
10787         $LFS mkdir -i $MDTIDX $remote_dir ||
10788                 error "create remote directory failed"
10789
10790         $LFS mkdir -i 0 $remote_dir/new_dir &&
10791                 error "nested remote directory create succeed!"
10792
10793         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=1
10794         $LFS mkdir -i 0 $remote_dir/new_dir || rc=$?
10795         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=0
10796
10797         [ $rc -ne 0 ] &&
10798            error "create remote directory failed after set enable_remote_dir"
10799
10800         rm -rf $remote_dir || error "first unlink remote directory failed"
10801
10802         $RUNAS -G$RUNAS_GID $LFS mkdir -i $MDTIDX $DIR/$tfile &&
10803                                                         error "chown worked"
10804
10805         do_facet mds$MDTIDX lctl set_param \
10806                                 mdt.*.enable_remote_dir_gid=$RUNAS_GID
10807         $LFS mkdir -i $MDTIDX $remote_dir || rc=$?
10808         do_facet mds$MDTIDX lctl set_param mdt.*.enable_remote_dir_gid=0
10809
10810         [ $rc -ne 0 ] &&
10811            error "create remote dir failed after set enable_remote_dir_gid"
10812
10813         rm -r $DIR/$tdir || error "second unlink remote directory failed"
10814 }
10815 run_test 230b "nested remote directory should be failed"
10816
10817 test_231a()
10818 {
10819         # For simplicity this test assumes that max_pages_per_rpc
10820         # is the same across all OSCs
10821         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
10822         local bulk_size=$((max_pages * 4096))
10823
10824         mkdir -p $DIR/$tdir
10825
10826         # clear the OSC stats
10827         $LCTL set_param osc.*.stats=0 &>/dev/null
10828
10829         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
10830         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
10831                 oflag=direct &>/dev/null || error "dd failed"
10832
10833         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
10834         if [ x$nrpcs != "x1" ]; then
10835                 error "found $nrpc ost_write RPCs, not 1 as expected"
10836         fi
10837
10838         # Drop the OSC cache, otherwise we will read from it
10839         cancel_lru_locks osc
10840
10841         # clear the OSC stats
10842         $LCTL set_param osc.*.stats=0 &>/dev/null
10843
10844         # Client reads $bulk_size.
10845         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
10846                 iflag=direct &>/dev/null || error "dd failed"
10847
10848         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
10849         if [ x$nrpcs != "x1" ]; then
10850                 error "found $nrpc ost_read RPCs, not 1 as expected"
10851         fi
10852 }
10853 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
10854
10855 test_231b() {
10856         mkdir -p $DIR/$tdir
10857         local i
10858         for i in {0..1023}; do
10859                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
10860                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
10861                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
10862         done
10863         sync
10864 }
10865 run_test 231b "must not assert on fully utilized OST request buffer"
10866
10867 #
10868 # tests that do cleanup/setup should be run at the end
10869 #
10870
10871 test_900() {
10872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10873         local ls
10874         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
10875         $LCTL set_param fail_loc=0x903
10876         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
10877         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
10878                 echo "clear" > $ls
10879         done
10880         FAIL_ON_ERROR=true cleanup
10881         FAIL_ON_ERROR=true setup
10882 }
10883 run_test 900 "umount should not race with any mgc requeue thread"
10884
10885 complete $SECONDS
10886 check_and_cleanup_lustre
10887 if [ "$I_MOUNTED" != "yes" ]; then
10888         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
10889 fi
10890 exit_status