Whamcloud - gitweb
48c48909b00965dc76567c51c30930705bc1a905
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 4; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                27u   42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19
20 # Tests that fail on uml
21 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
22 #                                    buffer i/o errs             sock spc runas
23 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
24
25 SRCDIR=$(cd $(dirname $0); echo $PWD)
26 export PATH=$PATH:/sbin
27
28 TMP=${TMP:-/tmp}
29
30 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
31 CREATETEST=${CREATETEST:-createtest}
32 LFS=${LFS:-lfs}
33 LFIND=${LFIND:-"$LFS find"}
34 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
35 LCTL=${LCTL:-lctl}
36 MCREATE=${MCREATE:-mcreate}
37 OPENFILE=${OPENFILE:-openfile}
38 OPENUNLINK=${OPENUNLINK:-openunlink}
39 export MULTIOP=${MULTIOP:-multiop}
40 READS=${READS:-"reads"}
41 MUNLINK=${MUNLINK:-munlink}
42 SOCKETSERVER=${SOCKETSERVER:-socketserver}
43 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
44 MEMHOG=${MEMHOG:-memhog}
45 DIRECTIO=${DIRECTIO:-directio}
46 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
47 UMOUNT=${UMOUNT:-"umount -d"}
48 STRIPES_PER_OBJ=-1
49 CHECK_GRANT=${CHECK_GRANT:-"yes"}
50 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
51
52 export NAME=${NAME:-local}
53
54 SAVE_PWD=$PWD
55
56 CLEANUP=${CLEANUP:-:}
57 SETUP=${SETUP:-:}
58 TRACE=${TRACE:-""}
59 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
60 . $LUSTRE/tests/test-framework.sh
61 init_test_env $@
62 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
63 init_logging
64
65 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
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         mkdir $DIR/d1
159         mkdir $DIR/d1/d2
160         mkdir $DIR/d1/d2 && error "we expect EEXIST, but not returned"
161         $CHECKSTAT -t dir $DIR/d1/d2 || error
162 }
163 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
164
165 test_1b() {
166         rmdir $DIR/d1/d2
167         rmdir $DIR/d1
168         $CHECKSTAT -a $DIR/d1 || error
169 }
170 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
171
172 test_2a() {
173         mkdir $DIR/d2
174         touch $DIR/d2/f
175         $CHECKSTAT -t file $DIR/d2/f || error
176 }
177 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
178
179 test_2b() {
180         rm -r $DIR/d2
181         $CHECKSTAT -a $DIR/d2 || error
182 }
183 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
184
185 test_3a() {
186         mkdir $DIR/d3
187         $CHECKSTAT -t dir $DIR/d3 || error
188 }
189 run_test 3a "mkdir .../d3 ======================================"
190
191 test_3b() {
192         if [ ! -d $DIR/d3 ]; then
193                 mkdir $DIR/d3
194         fi
195         touch $DIR/d3/f
196         $CHECKSTAT -t file $DIR/d3/f || error
197 }
198 run_test 3b "touch .../d3/f ===================================="
199
200 test_3c() {
201         rm -r $DIR/d3
202         $CHECKSTAT -a $DIR/d3 || error
203 }
204 run_test 3c "rm -r .../d3 ======================================"
205
206 test_4a() {
207         mkdir $DIR/d4
208         $CHECKSTAT -t dir $DIR/d4 || error
209 }
210 run_test 4a "mkdir .../d4 ======================================"
211
212 test_4b() {
213         if [ ! -d $DIR/d4 ]; then
214                 mkdir $DIR/d4
215         fi
216         mkdir $DIR/d4/d2
217         $CHECKSTAT -t dir $DIR/d4/d2 || error
218 }
219 run_test 4b "mkdir .../d4/d2 ==================================="
220
221 test_5() {
222         mkdir $DIR/d5
223         mkdir $DIR/d5/d2
224         chmod 0707 $DIR/d5/d2
225         $CHECKSTAT -t dir -p 0707 $DIR/d5/d2 || error
226 }
227 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
228
229 test_6a() {
230         touch $DIR/f6a
231         chmod 0666 $DIR/f6a || error
232         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
233 }
234 run_test 6a "touch .../f6a; chmod .../f6a ======================"
235
236 test_6b() {
237         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
238         if [ ! -f $DIR/f6a ]; then
239                 touch $DIR/f6a
240                 chmod 0666 $DIR/f6a
241         fi
242         $RUNAS chmod 0444 $DIR/f6a && error
243         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
244 }
245 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
246
247 test_6c() {
248         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
249         touch $DIR/f6c
250         chown $RUNAS_ID $DIR/f6c || error
251         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
252 }
253 run_test 6c "touch .../f6c; chown .../f6c ======================"
254
255 test_6d() {
256         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
257         if [ ! -f $DIR/f6c ]; then
258                 touch $DIR/f6c
259                 chown $RUNAS_ID $DIR/f6c
260         fi
261         $RUNAS chown $UID $DIR/f6c && error
262         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
263 }
264 run_test 6d "$RUNAS chown .../f6c (should return error) =="
265
266 test_6e() {
267         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
268         touch $DIR/f6e
269         chgrp $RUNAS_ID $DIR/f6e || error
270         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
271 }
272 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
273
274 test_6f() {
275         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
276         if [ ! -f $DIR/f6e ]; then
277                 touch $DIR/f6e
278                 chgrp $RUNAS_ID $DIR/f6e
279         fi
280         $RUNAS chgrp $UID $DIR/f6e && error
281         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
282 }
283 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
284
285 test_6g() {
286         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
287         mkdir $DIR/d6g || error
288         chmod 777 $DIR/d6g || error
289         $RUNAS mkdir $DIR/d6g/d || error
290         chmod g+s $DIR/d6g/d || error
291         mkdir $DIR/d6g/d/subdir
292         $CHECKSTAT -g \#$RUNAS_GID $DIR/d6g/d/subdir || error
293 }
294 run_test 6g "Is new dir in sgid dir inheriting group?"
295
296 test_6h() { # bug 7331
297         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
298         touch $DIR/f6h || error "touch failed"
299         chown $RUNAS_ID:$RUNAS_GID $DIR/f6h || error "initial chown failed"
300         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/f6h && error "chown worked"
301         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/f6h || error
302 }
303 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
304
305 test_7a() {
306         mkdir $DIR/d7
307         $MCREATE $DIR/d7/f
308         chmod 0666 $DIR/d7/f
309         $CHECKSTAT -t file -p 0666 $DIR/d7/f || error
310 }
311 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
312
313 test_7b() {
314         if [ ! -d $DIR/d7 ]; then
315                 mkdir $DIR/d7
316         fi
317         $MCREATE $DIR/d7/f2
318         echo -n foo > $DIR/d7/f2
319         [ "`cat $DIR/d7/f2`" = "foo" ] || error
320         $CHECKSTAT -t file -s 3 $DIR/d7/f2 || error
321 }
322 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
323
324 test_8() {
325         mkdir $DIR/d8
326         touch $DIR/d8/f
327         chmod 0666 $DIR/d8/f
328         $CHECKSTAT -t file -p 0666 $DIR/d8/f || error
329 }
330 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
331
332 test_9() {
333         mkdir $DIR/d9
334         mkdir $DIR/d9/d2
335         mkdir $DIR/d9/d2/d3
336         $CHECKSTAT -t dir $DIR/d9/d2/d3 || error
337 }
338 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
339
340 test_10() {
341         mkdir $DIR/d10
342         mkdir $DIR/d10/d2
343         touch $DIR/d10/d2/f
344         $CHECKSTAT -t file $DIR/d10/d2/f || error
345 }
346 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
347
348 test_11() {
349         mkdir $DIR/d11
350         mkdir $DIR/d11/d2
351         chmod 0666 $DIR/d11/d2
352         chmod 0705 $DIR/d11/d2
353         $CHECKSTAT -t dir -p 0705 $DIR/d11/d2 || error
354 }
355 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
356
357 test_12() {
358         mkdir $DIR/d12
359         touch $DIR/d12/f
360         chmod 0666 $DIR/d12/f
361         chmod 0654 $DIR/d12/f
362         $CHECKSTAT -t file -p 0654 $DIR/d12/f || error
363 }
364 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
365
366 test_13() {
367         mkdir $DIR/d13
368         dd if=/dev/zero of=$DIR/d13/f count=10
369         >  $DIR/d13/f
370         $CHECKSTAT -t file -s 0 $DIR/d13/f || error
371 }
372 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
373
374 test_14() {
375         mkdir $DIR/d14
376         touch $DIR/d14/f
377         rm $DIR/d14/f
378         $CHECKSTAT -a $DIR/d14/f || error
379 }
380 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
381
382 test_15() {
383         mkdir $DIR/d15
384         touch $DIR/d15/f
385         mv $DIR/d15/f $DIR/d15/f2
386         $CHECKSTAT -t file $DIR/d15/f2 || error
387 }
388 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
389
390 test_16() {
391         mkdir $DIR/d16
392         touch $DIR/d16/f
393         rm -rf $DIR/d16/f
394         $CHECKSTAT -a $DIR/d16/f || error
395 }
396 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
397
398 test_17a() {
399         mkdir -p $DIR/d17
400         touch $DIR/d17/f
401         ln -s $DIR/d17/f $DIR/d17/l-exist
402         ls -l $DIR/d17
403         $CHECKSTAT -l $DIR/d17/f $DIR/d17/l-exist || error
404         $CHECKSTAT -f -t f $DIR/d17/l-exist || error
405         rm -f $DIR/d17/l-exist
406         $CHECKSTAT -a $DIR/d17/l-exist || error
407 }
408 run_test 17a "symlinks: create, remove (real) =================="
409
410 test_17b() {
411         mkdir -p $DIR/d17
412         ln -s no-such-file $DIR/d17/l-dangle
413         ls -l $DIR/d17
414         $CHECKSTAT -l no-such-file $DIR/d17/l-dangle || error
415         $CHECKSTAT -fa $DIR/d17/l-dangle || error
416         rm -f $DIR/d17/l-dangle
417         $CHECKSTAT -a $DIR/d17/l-dangle || error
418 }
419 run_test 17b "symlinks: create, remove (dangling) =============="
420
421 test_17c() { # bug 3440 - don't save failed open RPC for replay
422         mkdir -p $DIR/d17
423         ln -s foo $DIR/d17/f17c
424         cat $DIR/d17/f17c && error "opened non-existent symlink" || true
425 }
426 run_test 17c "symlinks: open dangling (should return error) ===="
427
428 test_17d() {
429         mkdir -p $DIR/d17
430         ln -s foo $DIR/d17/f17d
431         touch $DIR/d17/f17d || error "creating to new symlink"
432 }
433 run_test 17d "symlinks: create dangling ========================"
434
435 test_17e() {
436         mkdir -p $DIR/$tdir
437         local foo=$DIR/$tdir/$tfile
438         ln -s $foo $foo || error "create symlink failed"
439         ls -l $foo || error "ls -l failed"
440         ls $foo && error "ls not failed" || true
441 }
442 run_test 17e "symlinks: create recursive symlink (should return error) ===="
443
444 test_17f() {
445         mkdir -p $DIR/d17f
446         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
447         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
448         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
451         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
452         ls -l  $DIR/d17f
453 }
454 run_test 17f "symlinks: long and very long symlink name ========================"
455
456 # str_repeat(S, N) generate a string that is string S repeated N times
457 str_repeat() {
458         local s=$1
459         local n=$2
460         local ret=''
461         while [ $((n -= 1)) -ge 0 ]; do
462                 ret=$ret$s
463         done
464         echo $ret
465 }
466
467 # Long symlinks and LU-2241
468 test_17g() {
469         mkdir -p $DIR/$tdir
470         local TESTS="59 60 61 4094 4095"
471
472         for i in $TESTS; do
473                 local SYMNAME=$(str_repeat 'x' $i)
474                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
475                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
476         done
477 }
478 run_test 17g "symlinks: really long symlink name and inode boundaries"
479
480 test_17h() { #bug 17378
481         remote_mds_nodsh && skip "remote MDS with nodsh" && return
482         mkdir -p $DIR/$tdir
483         $SETSTRIPE -c -1 $DIR/$tdir
484 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
485         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000141
486         touch $DIR/$tdir/$tfile || true
487 }
488 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
489
490 test_17i() { #bug 20018
491         remote_mds_nodsh && skip "remote MDS with nodsh" && return
492         mkdir -p $DIR/$tdir
493         local foo=$DIR/$tdir/$tfile
494         ln -s $foo $foo || error "create symlink failed"
495 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
496         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000143
497         ls -l $foo && error "error not detected"
498         return 0
499 }
500 run_test 17i "don't panic on short symlink"
501
502 test_17k() { #bug 22301
503         rsync --help | grep -q xattr ||
504                 skip_env "$(rsync --version| head -1) does not support xattrs"
505         mkdir -p $DIR/{$tdir,$tdir.new}
506         touch $DIR/$tdir/$tfile
507         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
508         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
509                 error "rsync failed with xattrs enabled"
510 }
511 run_test 17k "symlinks: rsync with xattrs enabled ========================="
512
513 test_17l() { # LU-279
514         mkdir -p $DIR/$tdir
515         touch $DIR/$tdir/$tfile
516         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
517         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
518                 # -h to not follow symlinks. -m '' to list all the xattrs.
519                 # grep to remove first line: '# file: $path'.
520                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
521                 do
522                         lgetxattr_size_check $path $xattr ||
523                                 error "lgetxattr_size_check $path $xattr failed"
524                 done
525         done
526 }
527 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
528
529 # LU-1540
530 test_17m() {
531         local short_sym="0123456789"
532         local WDIR=$DIR/${tdir}m
533         local mds_index
534         local devname
535         local cmd
536         local i
537         local rc=0
538
539         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
540         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
541                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
542
543         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
544                 skip "only for ldiskfs MDT" && return 0
545
546         mkdir -p $WDIR
547         long_sym=$short_sym
548         # create a long symlink file
549         for ((i = 0; i < 4; ++i)); do
550                 long_sym=${long_sym}${long_sym}
551         done
552
553         echo "create 512 short and long symlink files under $WDIR"
554         for ((i = 0; i < 256; ++i)); do
555                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
556                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
557         done
558
559         echo "erase them"
560         rm -f $WDIR/*
561         sync
562         wait_delete_completed
563
564         echo "recreate the 512 symlink files with a shorter string"
565         for ((i = 0; i < 512; ++i)); do
566                 # rewrite the symlink file with a shorter string
567                 ln -sf ${long_sym} $WDIR/long-$i
568                 ln -sf ${short_sym} $WDIR/short-$i
569         done
570
571         mds_index=$($LFS getstripe -M $WDIR)
572         mds_index=$((mds_index+1))
573         devname=$(mdsdevname $mds_index)
574         cmd="$E2FSCK -fnvd $devname"
575
576         echo "stop and checking mds${mds_index}: $cmd"
577         # e2fsck should not return error
578         stop mds${mds_index} -f
579         do_facet mds${mds_index} $cmd || rc=$?
580
581         start mds${mds_index} $devname $MDS_MOUNT_OPTS
582         df $MOUNT > /dev/null 2>&1
583         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
584                 "short/long symlink MDT: rc=$rc"
585         return $rc
586 }
587 run_test 17m "run e2fsck against MDT which contains short/long symlink"
588
589 test_18() {
590         touch $DIR/f
591         ls $DIR || error
592 }
593 run_test 18 "touch .../f ; ls ... =============================="
594
595 test_19a() {
596         touch $DIR/f19
597         ls -l $DIR
598         rm $DIR/f19
599         $CHECKSTAT -a $DIR/f19 || error
600 }
601 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
602
603 test_19b() {
604         ls -l $DIR/f19 && error || true
605 }
606 run_test 19b "ls -l .../f19 (should return error) =============="
607
608 test_19c() {
609         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
610         $RUNAS touch $DIR/f19 && error || true
611 }
612 run_test 19c "$RUNAS touch .../f19 (should return error) =="
613
614 test_19d() {
615         cat $DIR/f19 && error || true
616 }
617 run_test 19d "cat .../f19 (should return error) =============="
618
619 test_20() {
620         touch $DIR/f
621         rm $DIR/f
622         log "1 done"
623         touch $DIR/f
624         rm $DIR/f
625         log "2 done"
626         touch $DIR/f
627         rm $DIR/f
628         log "3 done"
629         $CHECKSTAT -a $DIR/f || error
630 }
631 run_test 20 "touch .../f ; ls -l ... ==========================="
632
633 test_21() {
634         mkdir $DIR/d21
635         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
636         ln -s dangle $DIR/d21/link
637         echo foo >> $DIR/d21/link
638         cat $DIR/d21/dangle
639         $CHECKSTAT -t link $DIR/d21/link || error
640         $CHECKSTAT -f -t file $DIR/d21/link || error
641 }
642 run_test 21 "write to dangling link ============================"
643
644 test_22() {
645         WDIR=$DIR/$tdir
646         mkdir -p $WDIR
647         chown $RUNAS_ID:$RUNAS_GID $WDIR
648         (cd $WDIR || error "cd $WDIR failed";
649         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
650         $RUNAS tar xf -)
651         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
652         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
653         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
654 }
655 run_test 22 "unpack tar archive as non-root user ==============="
656
657 # was test_23
658 test_23a() {
659         mkdir -p $DIR/$tdir
660         local file=$DIR/$tdir/$tfile
661
662         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
663         openfile -f O_CREAT:O_EXCL $file &&
664                 error "$file recreate succeeded" || true
665 }
666 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
667
668 test_23b() { # bug 18988
669         mkdir -p $DIR/$tdir
670         local file=$DIR/$tdir/$tfile
671
672         rm -f $file
673         echo foo > $file || error "write filed"
674         echo bar >> $file || error "append filed"
675         $CHECKSTAT -s 8 $file || error "wrong size"
676         rm $file
677 }
678 run_test 23b "O_APPEND check =========================="
679
680 test_24a() {
681         echo '== rename sanity =============================================='
682         echo '-- same directory rename'
683         mkdir $DIR/R1
684         touch $DIR/R1/f
685         mv $DIR/R1/f $DIR/R1/g
686         $CHECKSTAT -t file $DIR/R1/g || error
687 }
688 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
689
690 test_24b() {
691         mkdir $DIR/R2
692         touch $DIR/R2/{f,g}
693         mv $DIR/R2/f $DIR/R2/g
694         $CHECKSTAT -a $DIR/R2/f || error
695         $CHECKSTAT -t file $DIR/R2/g || error
696 }
697 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
698
699 test_24c() {
700         mkdir $DIR/R3
701         mkdir $DIR/R3/f
702         mv $DIR/R3/f $DIR/R3/g
703         $CHECKSTAT -a $DIR/R3/f || error
704         $CHECKSTAT -t dir $DIR/R3/g || error
705 }
706 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
707
708 test_24d() {
709         mkdir $DIR/R4
710         mkdir $DIR/R4/{f,g}
711         mrename $DIR/R4/f $DIR/R4/g
712         $CHECKSTAT -a $DIR/R4/f || error
713         $CHECKSTAT -t dir $DIR/R4/g || error
714 }
715 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
716
717 test_24e() {
718         echo '-- cross directory renames --'
719         mkdir $DIR/R5{a,b}
720         touch $DIR/R5a/f
721         mv $DIR/R5a/f $DIR/R5b/g
722         $CHECKSTAT -a $DIR/R5a/f || error
723         $CHECKSTAT -t file $DIR/R5b/g || error
724 }
725 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
726
727 test_24f() {
728         mkdir $DIR/R6{a,b}
729         touch $DIR/R6a/f $DIR/R6b/g
730         mv $DIR/R6a/f $DIR/R6b/g
731         $CHECKSTAT -a $DIR/R6a/f || error
732         $CHECKSTAT -t file $DIR/R6b/g || error
733 }
734 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
735
736 test_24g() {
737         mkdir $DIR/R7{a,b}
738         mkdir $DIR/R7a/d
739         mv $DIR/R7a/d $DIR/R7b/e
740         $CHECKSTAT -a $DIR/R7a/d || error
741         $CHECKSTAT -t dir $DIR/R7b/e || error
742 }
743 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
744
745 test_24h() {
746         mkdir $DIR/R8{a,b}
747         mkdir $DIR/R8a/d $DIR/R8b/e
748         mrename $DIR/R8a/d $DIR/R8b/e
749         $CHECKSTAT -a $DIR/R8a/d || error
750         $CHECKSTAT -t dir $DIR/R8b/e || error
751 }
752 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
753
754 test_24i() {
755         echo "-- rename error cases"
756         mkdir $DIR/R9
757         mkdir $DIR/R9/a
758         touch $DIR/R9/f
759         mrename $DIR/R9/f $DIR/R9/a
760         $CHECKSTAT -t file $DIR/R9/f || error
761         $CHECKSTAT -t dir  $DIR/R9/a || error
762         $CHECKSTAT -a $DIR/R9/a/f || error
763 }
764 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
765
766 test_24j() {
767         mkdir $DIR/R10
768         mrename $DIR/R10/f $DIR/R10/g
769         $CHECKSTAT -t dir $DIR/R10 || error
770         $CHECKSTAT -a $DIR/R10/f || error
771         $CHECKSTAT -a $DIR/R10/g || error
772 }
773 run_test 24j "source does not exist ============================"
774
775 test_24k() {
776         mkdir $DIR/R11a $DIR/R11a/d
777         touch $DIR/R11a/f
778         mv $DIR/R11a/f $DIR/R11a/d
779         $CHECKSTAT -a $DIR/R11a/f || error
780         $CHECKSTAT -t file $DIR/R11a/d/f || error
781 }
782 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
783
784 # bug 2429 - rename foo foo foo creates invalid file
785 test_24l() {
786         f="$DIR/f24l"
787         $MULTIOP $f OcNs || error
788 }
789 run_test 24l "Renaming a file to itself ========================"
790
791 test_24m() {
792         f="$DIR/f24m"
793         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
794         # on ext3 this does not remove either the source or target files
795         # though the "expected" operation would be to remove the source
796         $CHECKSTAT -t file ${f} || error "${f} missing"
797         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
798 }
799 run_test 24m "Renaming a file to a hard link to itself ========="
800
801 test_24n() {
802     f="$DIR/f24n"
803     # this stats the old file after it was renamed, so it should fail
804     touch ${f}
805     $CHECKSTAT ${f}
806     mv ${f} ${f}.rename
807     $CHECKSTAT ${f}.rename
808     $CHECKSTAT -a ${f}
809 }
810 run_test 24n "Statting the old file after renaming (Posix rename 2)"
811
812 test_24o() {
813         check_kernel_version 37 || return 0
814         mkdir -p $DIR/d24o
815         rename_many -s random -v -n 10 $DIR/d24o
816 }
817 run_test 24o "rename of files during htree split ==============="
818
819 test_24p() {
820         mkdir $DIR/R12{a,b}
821         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
822         mrename $DIR/R12a $DIR/R12b
823         $CHECKSTAT -a $DIR/R12a || error
824         $CHECKSTAT -t dir $DIR/R12b || error
825         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
826         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
827 }
828 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
829
830 test_24q() {
831         mkdir $DIR/R13{a,b}
832         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
833         multiop_bg_pause $DIR/R13b D_c || return 1
834         MULTIPID=$!
835
836         mrename $DIR/R13a $DIR/R13b
837         $CHECKSTAT -a $DIR/R13a || error
838         $CHECKSTAT -t dir $DIR/R13b || error
839         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
840         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
841         kill -USR1 $MULTIPID
842         wait $MULTIPID || error "multiop close failed"
843 }
844 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
845
846 test_24r() { #bug 3789
847         mkdir $DIR/R14a $DIR/R14a/b
848         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
849         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
850         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
851 }
852 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
853
854 test_24s() {
855         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
856         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
857         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
858         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
859 }
860 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
861 test_24t() {
862         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
863         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
864         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
865         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
866 }
867 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
868
869 test_24u() { # bug12192
870         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
871         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
872 }
873 run_test 24u "create stripe file"
874
875 page_size() {
876         getconf PAGE_SIZE
877 }
878
879 simple_cleanup_common() {
880         trap 0
881         rm -rf $DIR/$tdir
882         wait_delete_completed
883 }
884
885 test_24v() {
886         local NRFILES=100000
887         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
888         [ $FREE_INODES -lt $NRFILES ] && \
889                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
890                 return
891
892         trap simple_cleanup_common EXIT
893
894         mkdir -p $DIR/$tdir
895         createmany -m $DIR/$tdir/$tfile $NRFILES
896
897         cancel_lru_locks mdc
898         lctl set_param mdc.*.stats clear
899
900         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
901
902         # LU-5 large readdir
903         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
904         #               8 bytes for name(filename is mostly 5 in this test) +
905         #               8 bytes for luda_type
906         # take into account of overhead in lu_dirpage header and end mark in
907         # each page, plus one in RPC_NUM calculation.
908         DIRENT_SIZE=48
909         RPC_SIZE=$(($(lctl get_param -n mdc.*.max_pages_per_rpc)*$(page_size)))
910         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
911         mds_readpage=`lctl get_param mdc.*.stats | \
912                                 awk '/^mds_readpage/ {print $2}'`
913         [ $mds_readpage -gt $RPC_NUM ] && \
914                 error "large readdir doesn't take effect"
915
916         simple_cleanup_common
917 }
918 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
919
920 test_24w() { # bug21506
921         SZ1=234852
922         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
923         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
924         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
925         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
926         [ "$SZ1" = "$SZ2" ] || \
927                 error "Error reading at the end of the file $tfile"
928 }
929 run_test 24w "Reading a file larger than 4Gb"
930
931 test_25a() {
932         echo '== symlink sanity ============================================='
933
934         mkdir $DIR/d25
935         ln -s d25 $DIR/s25
936         touch $DIR/s25/foo || error
937 }
938 run_test 25a "create file in symlinked directory ==============="
939
940 test_25b() {
941         [ ! -d $DIR/d25 ] && test_25a
942         $CHECKSTAT -t file $DIR/s25/foo || error
943 }
944 run_test 25b "lookup file in symlinked directory ==============="
945
946 test_26a() {
947         mkdir $DIR/d26
948         mkdir $DIR/d26/d26-2
949         ln -s d26/d26-2 $DIR/s26
950         touch $DIR/s26/foo || error
951 }
952 run_test 26a "multiple component symlink ======================="
953
954 test_26b() {
955         mkdir -p $DIR/d26b/d26-2
956         ln -s d26b/d26-2/foo $DIR/s26-2
957         touch $DIR/s26-2 || error
958 }
959 run_test 26b "multiple component symlink at end of lookup ======"
960
961 test_26c() {
962         mkdir $DIR/d26.2
963         touch $DIR/d26.2/foo
964         ln -s d26.2 $DIR/s26.2-1
965         ln -s s26.2-1 $DIR/s26.2-2
966         ln -s s26.2-2 $DIR/s26.2-3
967         chmod 0666 $DIR/s26.2-3/foo
968 }
969 run_test 26c "chain of symlinks ================================"
970
971 # recursive symlinks (bug 439)
972 test_26d() {
973         ln -s d26-3/foo $DIR/d26-3
974 }
975 run_test 26d "create multiple component recursive symlink ======"
976
977 test_26e() {
978         [ ! -h $DIR/d26-3 ] && test_26d
979         rm $DIR/d26-3
980 }
981 run_test 26e "unlink multiple component recursive symlink ======"
982
983 # recursive symlinks (bug 7022)
984 test_26f() {
985         mkdir -p $DIR/$tdir
986         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
987         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
988         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
989         mkdir $tfile             || error "mkdir $tfile failed"
990         cd $tfile                || error "cd $tfile failed"
991         ln -s .. dotdot          || error "ln dotdot failed"
992         ln -s dotdot/lndir lndir || error "ln lndir failed"
993         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
994         output=`ls $tfile/$tfile/lndir/bar1`
995         [ "$output" = bar1 ] && error "unexpected output"
996         rm -r $tfile             || error "rm $tfile failed"
997         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
998 }
999 run_test 26f "rm -r of a directory which has recursive symlink ="
1000
1001 test_27a() {
1002         echo '== stripe sanity =============================================='
1003         mkdir -p $DIR/d27 || error "mkdir failed"
1004         $GETSTRIPE $DIR/d27
1005         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1006         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1007         pass
1008         log "== test_27a: write to one stripe file ========================="
1009         cp /etc/hosts $DIR/d27/f0 || error
1010 }
1011 run_test 27a "one stripe file =================================="
1012
1013 test_27b() {
1014         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1015         mkdir -p $DIR/d27
1016         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1017         $GETSTRIPE -c $DIR/d27/f01
1018         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1019                 error "two-stripe file doesn't have two stripes"
1020 }
1021 run_test 27b "create two stripe file"
1022
1023 test_27c() {
1024         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1025
1026         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1027 }
1028 run_test 27c "write to two stripe file"
1029
1030 test_27d() {
1031         mkdir -p $DIR/d27
1032         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1033         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1034         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1035 }
1036 run_test 27d "create file with default settings ================"
1037
1038 test_27e() {
1039         mkdir -p $DIR/d27
1040         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1041         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1042         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1043 }
1044 run_test 27e "setstripe existing file (should return error) ======"
1045
1046 test_27f() {
1047         mkdir -p $DIR/d27
1048         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1049         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1050         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1051 }
1052 run_test 27f "setstripe with bad stripe size (should return error)"
1053
1054 test_27g() {
1055         mkdir -p $DIR/d27
1056         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1057         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1058                 error "$DIR/d27/fnone has object"
1059 }
1060 run_test 27g "$GETSTRIPE with no objects"
1061
1062 test_27i() {
1063         touch $DIR/d27/fsome || error "touch failed"
1064         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1065 }
1066 run_test 27i "$GETSTRIPE with some objects"
1067
1068 test_27j() {
1069         mkdir -p $DIR/d27
1070         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1071 }
1072 run_test 27j "setstripe with bad stripe offset (should return error)"
1073
1074 test_27k() { # bug 2844
1075         mkdir -p $DIR/d27
1076         FILE=$DIR/d27/f27k
1077         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1078         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
1079         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1080         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1081         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1082         dd if=/dev/zero of=$FILE bs=4k count=1
1083         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1084         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1085 }
1086 run_test 27k "limit i_blksize for broken user apps ============="
1087
1088 test_27l() {
1089         mkdir -p $DIR/d27
1090         mcreate $DIR/f27l || error "creating file"
1091         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1092                 error "setstripe should have failed" || true
1093 }
1094 run_test 27l "check setstripe permissions (should return error)"
1095
1096 test_27m() {
1097         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1098                 return
1099         if [ $ORIGFREE -gt $MAXFREE ]; then
1100                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1101                 return
1102         fi
1103         trap simple_cleanup_common EXIT
1104         mkdir -p $DIR/$tdir
1105         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1106         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1107                 error "dd should fill OST0"
1108         i=2
1109         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1110                 i=`expr $i + 1`
1111                 [ $i -gt 256 ] && break
1112         done
1113         i=`expr $i + 1`
1114         touch $DIR/$tdir/f27m_$i
1115         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1116                 error "OST0 was full but new created file still use it"
1117         i=`expr $i + 1`
1118         touch $DIR/$tdir/f27m_$i
1119         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1120                 error "OST0 was full but new created file still use it"
1121         simple_cleanup_common
1122 }
1123 run_test 27m "create file while OST0 was full =================="
1124
1125 sleep_maxage() {
1126         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1127         sleep $DELAY
1128 }
1129
1130 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1131 # if the OST isn't full anymore.
1132 reset_enospc() {
1133         local OSTIDX=${1:-""}
1134
1135         local list=$(comma_list $(osts_nodes))
1136         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1137
1138         do_nodes $list lctl set_param fail_loc=0
1139         sync    # initiate all OST_DESTROYs from MDS to OST
1140         sleep_maxage
1141 }
1142
1143 exhaust_precreations() {
1144         local OSTIDX=$1
1145         local FAILLOC=$2
1146         local FAILIDX=${3:-$OSTIDX}
1147
1148         mkdir -p $DIR/$tdir
1149         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1150         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1151
1152         local OST=$(ostname_from_index $OSTIDX)
1153         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1154                           sed -e 's/_UUID$//;s/^.*-//')
1155
1156         # on the mdt's osc
1157         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1158         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1159         osc.$mdtosc_proc1.prealloc_last_id)
1160         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1161         osc.$mdtosc_proc1.prealloc_next_id)
1162
1163         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1164         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1165
1166         mkdir -p $DIR/$tdir/${OST}
1167         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1168 #define OBD_FAIL_OST_ENOSPC              0x215
1169         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1170         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1171         echo "Creating to objid $last_id on ost $OST..."
1172         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1173         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1174         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1175         sleep_maxage
1176 }
1177
1178 exhaust_all_precreations() {
1179         local i
1180         for (( i=0; i < OSTCOUNT; i++ )) ; do
1181                 exhaust_precreations $i $1 -1
1182         done
1183 }
1184
1185 test_27n() {
1186         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1187         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1188         remote_ost_nodsh && skip "remote OST with nodsh" && return
1189
1190         reset_enospc
1191         rm -f $DIR/$tdir/$tfile
1192         exhaust_precreations 0 0x80000215
1193         $SETSTRIPE -c -1 $DIR/$tdir
1194         touch $DIR/$tdir/$tfile || error
1195         $GETSTRIPE $DIR/$tdir/$tfile
1196         reset_enospc
1197 }
1198 run_test 27n "create file with some full OSTs =================="
1199
1200 test_27o() {
1201         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1202         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1203         remote_ost_nodsh && skip "remote OST with nodsh" && return
1204
1205         reset_enospc
1206         rm -f $DIR/$tdir/$tfile
1207         exhaust_all_precreations 0x215
1208
1209         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1210
1211         reset_enospc
1212         rm -rf $DIR/$tdir/*
1213 }
1214 run_test 27o "create file with all full OSTs (should error) ===="
1215
1216 test_27p() {
1217         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1218         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1219         remote_ost_nodsh && skip "remote OST with nodsh" && return
1220
1221         reset_enospc
1222         rm -f $DIR/$tdir/$tfile
1223         mkdir -p $DIR/$tdir
1224
1225         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1226         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1227         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1228
1229         exhaust_precreations 0 0x80000215
1230         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1231         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1232         $GETSTRIPE $DIR/$tdir/$tfile
1233
1234         reset_enospc
1235 }
1236 run_test 27p "append to a truncated file with some full OSTs ==="
1237
1238 test_27q() {
1239         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1240         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1241         remote_ost_nodsh && skip "remote OST with nodsh" && return
1242
1243         reset_enospc
1244         rm -f $DIR/$tdir/$tfile
1245
1246         mkdir -p $DIR/$tdir
1247         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1248         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1249         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1250
1251         exhaust_all_precreations 0x215
1252
1253         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1254         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1255
1256         reset_enospc
1257 }
1258 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1259
1260 test_27r() {
1261         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1262         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1263         remote_ost_nodsh && skip "remote OST with nodsh" && return
1264
1265         reset_enospc
1266         rm -f $DIR/$tdir/$tfile
1267         exhaust_precreations 0 0x80000215
1268
1269         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1270
1271         reset_enospc
1272 }
1273 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1274
1275 test_27s() { # bug 10725
1276         mkdir -p $DIR/$tdir
1277         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1278         local stripe_count=0
1279         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1280         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1281                 error "stripe width >= 2^32 succeeded" || true
1282
1283 }
1284 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1285
1286 test_27t() { # bug 10864
1287         WDIR=`pwd`
1288         WLFS=`which lfs`
1289         cd $DIR
1290         touch $tfile
1291         $WLFS getstripe $tfile
1292         cd $WDIR
1293 }
1294 run_test 27t "check that utils parse path correctly"
1295
1296 test_27u() { # bug 4900
1297         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1298         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1299
1300 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1301         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1302         mkdir -p $DIR/$tdir
1303         createmany -o $DIR/$tdir/t- 1000
1304         do_facet $SINGLEMDS lctl set_param fail_loc=0
1305
1306         TLOG=$DIR/$tfile.getstripe
1307         $GETSTRIPE $DIR/$tdir > $TLOG
1308         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1309         unlinkmany $DIR/$tdir/t- 1000
1310         [ $OBJS -gt 0 ] && \
1311                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1312 }
1313 run_test 27u "skip object creation on OSC w/o objects =========="
1314
1315 test_27v() { # bug 4900
1316         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1317         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1318         remote_ost_nodsh && skip "remote OST with nodsh" && return
1319
1320         exhaust_all_precreations 0x215
1321         reset_enospc
1322
1323         mkdir -p $DIR/$tdir
1324         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1325
1326         touch $DIR/$tdir/$tfile
1327         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1328         # all except ost1
1329         for (( i=1; i < OSTCOUNT; i++ )); do
1330                 do_facet ost$i lctl set_param fail_loc=0x705
1331         done
1332         local START=`date +%s`
1333         createmany -o $DIR/$tdir/$tfile 32
1334
1335         local FINISH=`date +%s`
1336         local TIMEOUT=`lctl get_param -n timeout`
1337         local PROCESS=$((FINISH - START))
1338         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1339                error "$FINISH - $START >= $TIMEOUT / 2"
1340         sleep $((TIMEOUT / 2 - PROCESS))
1341         reset_enospc
1342 }
1343 run_test 27v "skip object creation on slow OST ================="
1344
1345 test_27w() { # bug 10997
1346         mkdir -p $DIR/$tdir || error "mkdir failed"
1347         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1348         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1349                 error "stripe size $size != 65536" || true
1350         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1351                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1352 }
1353 run_test 27w "check $SETSTRIPE -S option"
1354
1355 test_27wa() {
1356         [ "$OSTCOUNT" -lt "2" ] &&
1357                 skip_env "skipping multiple stripe count/offset test" && return
1358
1359         mkdir -p $DIR/$tdir || error "mkdir failed"
1360         for i in $(seq 1 $OSTCOUNT); do
1361                 offset=$((i - 1))
1362                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1363                         error "setstripe -c $i -i $offset failed"
1364                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1365                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1366                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1367                 [ $index -ne $offset ] &&
1368                         error "stripe offset $index != $offset" || true
1369         done
1370 }
1371 run_test 27wa "check $SETSTRIPE -c -i options"
1372
1373 test_27x() {
1374         remote_ost_nodsh && skip "remote OST with nodsh" && return
1375         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1376         OFFSET=$(($OSTCOUNT - 1))
1377         OSTIDX=0
1378         local OST=$(ostname_from_index $OSTIDX)
1379
1380         mkdir -p $DIR/$tdir
1381         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1382         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1383         sleep_maxage
1384         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1385         for i in `seq 0 $OFFSET`; do
1386                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1387                 error "OST0 was degraded but new created file still use it"
1388         done
1389         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1390 }
1391 run_test 27x "create files while OST0 is degraded"
1392
1393 test_27y() {
1394         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1395         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1396         remote_ost_nodsh && skip "remote OST with nodsh" && return
1397
1398         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1399         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1400             osc.$mdtosc.prealloc_last_id)
1401         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1402             osc.$mdtosc.prealloc_next_id)
1403         local fcount=$((last_id - next_id))
1404         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1405         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1406
1407         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1408                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1409         local OST_DEACTIVE_IDX=-1
1410         local OSC
1411         local OSTIDX
1412         local OST
1413
1414         for OSC in $MDS_OSCS; do
1415                 OST=$(osc_to_ost $OSC)
1416                 OSTIDX=$(index_from_ostuuid $OST)
1417                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1418                         OST_DEACTIVE_IDX=$OSTIDX
1419                 fi
1420                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1421                         echo $OSC "is Deactivated:"
1422                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1423                 fi
1424         done
1425
1426         OSTIDX=$(index_from_ostuuid $OST)
1427         mkdir -p $DIR/$tdir
1428         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1429
1430         for OSC in $MDS_OSCS; do
1431                 OST=$(osc_to_ost $OSC)
1432                 OSTIDX=$(index_from_ostuuid $OST)
1433                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1434                         echo $OST "is degraded:"
1435                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1436                                                 obdfilter.$OST.degraded=1
1437                 fi
1438         done
1439
1440         sleep_maxage
1441         createmany -o $DIR/$tdir/$tfile $fcount
1442
1443         for OSC in $MDS_OSCS; do
1444                 OST=$(osc_to_ost $OSC)
1445                 OSTIDX=$(index_from_ostuuid $OST)
1446                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1447                         echo $OST "is recovered from degraded:"
1448                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1449                                                 obdfilter.$OST.degraded=0
1450                 else
1451                         do_facet $SINGLEMDS lctl --device %$OSC activate
1452                 fi
1453         done
1454 }
1455 run_test 27y "create files while OST0 is degraded and the rest inactive"
1456
1457 check_seq_oid()
1458 {
1459         log "check file $1"
1460
1461         lmm_count=$($GETSTRIPE -c $1)
1462         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1463         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1464
1465         local old_ifs="$IFS"
1466         IFS=$'[:]'
1467         fid=($($LFS path2fid $1))
1468         IFS="$old_ifs"
1469
1470         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1471         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1472
1473         # compare lmm_seq and lu_fid->f_seq
1474         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1475         # compare lmm_object_id and lu_fid->oid
1476         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1477
1478         # check the trusted.fid attribute of the OST objects of the file
1479         local have_obdidx=false
1480         local stripe_nr=0
1481         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1482                 # skip lines up to and including "obdidx"
1483                 [ -z "$obdidx" ] && break
1484                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1485                 $have_obdidx || continue
1486
1487                 local ost=$((obdidx + 1))
1488                 local dev=$(ostdevname $ost)
1489
1490                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1491                         echo "Currently only works with ldiskfs-based OSTs"
1492                         continue
1493                 fi
1494
1495                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1496
1497                 #don't unmount/remount the OSTs if we don't need to do that
1498                 #local dir=$(facet_mntpt ost$ost)
1499                 #stop ost$dev
1500                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1501                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1502                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1503                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1504
1505                 local obj_file="O/$seq/d$((oid %32))/$oid"
1506                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1507                            $dev 2>/dev/null" | grep "parent=")
1508
1509                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1510
1511                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1512
1513                 #do_facet ost$ost umount -d $dir
1514                 #start ost$ost $dev $OST_MOUNT_OPTS
1515
1516                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1517                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1518                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1519                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1520                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1521                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1522
1523                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1524                 [ $ff_pseq = $lmm_seq ] ||
1525                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1526                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1527                 [ $ff_poid = $lmm_oid ] ||
1528                         error "FF parent OID $ff_poid != $lmm_oid"
1529                 [ $ff_pstripe = $stripe_nr ] ||
1530                         error "FF stripe $ff_pstripe != $stripe_nr"
1531
1532                 stripe_nr=$((stripe_nr + 1))
1533         done
1534 }
1535
1536 test_27z() {
1537         remote_ost_nodsh && skip "remote OST with nodsh" && return
1538         mkdir -p $DIR/$tdir
1539
1540         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1541                 { error "setstripe -c -1 failed"; return 1; }
1542         # We need to send a write to every object to get parent FID info set.
1543         # This _should_ also work for setattr, but does not currently.
1544         # touch $DIR/$tdir/$tfile-1 ||
1545         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1546                 { error "dd $tfile-1 failed"; return 2; }
1547         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1548                 { error "setstripe -c -1 failed"; return 3; }
1549         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1550                 { error "dd $tfile-2 failed"; return 4; }
1551
1552         # make sure write RPCs have been sent to OSTs
1553         sync; sleep 5; sync
1554
1555         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1556         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1557 }
1558 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1559
1560 test_27A() { # b=19102
1561         local restore_size=$($GETSTRIPE -S $MOUNT)
1562         local restore_count=$($GETSTRIPE -c $MOUNT)
1563         local restore_offset=$($GETSTRIPE -i $MOUNT)
1564         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1565         local default_size=$($GETSTRIPE -S $MOUNT)
1566         local default_count=$($GETSTRIPE -c $MOUNT)
1567         local default_offset=$($GETSTRIPE -i $MOUNT)
1568         local dsize=$((1024 * 1024))
1569         [ $default_size -eq $dsize ] ||
1570                 error "stripe size $default_size != $dsize"
1571         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1572         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1573         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1574 }
1575 run_test 27A "check filesystem-wide default LOV EA values"
1576
1577 # createtest also checks that device nodes are created and
1578 # then visible correctly (#2091)
1579 test_28() { # bug 2091
1580         mkdir $DIR/d28
1581         $CREATETEST $DIR/d28/ct || error
1582 }
1583 run_test 28 "create/mknod/mkdir with bad file types ============"
1584
1585 test_29() {
1586         cancel_lru_locks mdc
1587         mkdir $DIR/d29
1588         touch $DIR/d29/foo
1589         log 'first d29'
1590         ls -l $DIR/d29
1591
1592         declare -i LOCKCOUNTORIG=0
1593         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1594                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1595         done
1596         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1597
1598         declare -i LOCKUNUSEDCOUNTORIG=0
1599         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1600                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1601         done
1602
1603         log 'second d29'
1604         ls -l $DIR/d29
1605         log 'done'
1606
1607         declare -i LOCKCOUNTCURRENT=0
1608         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1609                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1610         done
1611
1612         declare -i LOCKUNUSEDCOUNTCURRENT=0
1613         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1614                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1615         done
1616
1617         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1618                 lctl set_param -n ldlm.dump_namespaces ""
1619                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1620                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1621                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1622                 return 2
1623         fi
1624         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1625                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1626                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1627                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1628                 return 3
1629         fi
1630 }
1631 run_test 29 "IT_GETATTR regression  ============================"
1632
1633 test_30a() { # was test_30
1634         cp `which ls` $DIR || cp /bin/ls $DIR
1635         $DIR/ls / || error
1636         rm $DIR/ls
1637 }
1638 run_test 30a "execute binary from Lustre (execve) =============="
1639
1640 test_30b() {
1641         cp `which ls` $DIR || cp /bin/ls $DIR
1642         chmod go+rx $DIR/ls
1643         $RUNAS $DIR/ls / || error
1644         rm $DIR/ls
1645 }
1646 run_test 30b "execute binary from Lustre as non-root ==========="
1647
1648 test_30c() { # b=22376
1649         cp `which ls` $DIR || cp /bin/ls $DIR
1650         chmod a-rw $DIR/ls
1651         cancel_lru_locks mdc
1652         cancel_lru_locks osc
1653         $RUNAS $DIR/ls / || error
1654         rm -f $DIR/ls
1655 }
1656 run_test 30c "execute binary from Lustre without read perms ===="
1657
1658 test_31a() {
1659         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1660         $CHECKSTAT -a $DIR/f31 || error
1661 }
1662 run_test 31a "open-unlink file =================================="
1663
1664 test_31b() {
1665         touch $DIR/f31 || error
1666         ln $DIR/f31 $DIR/f31b || error
1667         $MULTIOP $DIR/f31b Ouc || error
1668         $CHECKSTAT -t file $DIR/f31 || error
1669 }
1670 run_test 31b "unlink file with multiple links while open ======="
1671
1672 test_31c() {
1673         touch $DIR/f31 || error
1674         ln $DIR/f31 $DIR/f31c || error
1675         multiop_bg_pause $DIR/f31 O_uc || return 1
1676         MULTIPID=$!
1677         $MULTIOP $DIR/f31c Ouc
1678         kill -USR1 $MULTIPID
1679         wait $MULTIPID
1680 }
1681 run_test 31c "open-unlink file with multiple links ============="
1682
1683 test_31d() {
1684         opendirunlink $DIR/d31d $DIR/d31d || error
1685         $CHECKSTAT -a $DIR/d31d || error
1686 }
1687 run_test 31d "remove of open directory ========================="
1688
1689 test_31e() { # bug 2904
1690         check_kernel_version 34 || return 0
1691         openfilleddirunlink $DIR/d31e || error
1692 }
1693 run_test 31e "remove of open non-empty directory ==============="
1694
1695 test_31f() { # bug 4554
1696         set -vx
1697         mkdir $DIR/d31f
1698         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1699         cp /etc/hosts $DIR/d31f
1700         ls -l $DIR/d31f
1701         $GETSTRIPE $DIR/d31f/hosts
1702         multiop_bg_pause $DIR/d31f D_c || return 1
1703         MULTIPID=$!
1704
1705         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1706         mkdir $DIR/d31f
1707         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1708         cp /etc/hosts $DIR/d31f
1709         ls -l $DIR/d31f
1710         $GETSTRIPE $DIR/d31f/hosts
1711         multiop_bg_pause $DIR/d31f D_c || return 1
1712         MULTIPID2=$!
1713
1714         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1715         wait $MULTIPID || error "first opendir $MULTIPID failed"
1716
1717         sleep 6
1718
1719         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1720         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1721         set +vx
1722 }
1723 run_test 31f "remove of open directory with open-unlink file ==="
1724
1725 test_31g() {
1726         echo "-- cross directory link --"
1727         mkdir $DIR/d31g{a,b}
1728         touch $DIR/d31ga/f
1729         ln $DIR/d31ga/f $DIR/d31gb/g
1730         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1731         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1732         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1733         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1734 }
1735 run_test 31g "cross directory link==============="
1736
1737 test_31h() {
1738         echo "-- cross directory link --"
1739         mkdir $DIR/d31h
1740         mkdir $DIR/d31h/dir
1741         touch $DIR/d31h/f
1742         ln $DIR/d31h/f $DIR/d31h/dir/g
1743         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1744         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1745         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1746         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1747 }
1748 run_test 31h "cross directory link under child==============="
1749
1750 test_31i() {
1751         echo "-- cross directory link --"
1752         mkdir $DIR/d31i
1753         mkdir $DIR/d31i/dir
1754         touch $DIR/d31i/dir/f
1755         ln $DIR/d31i/dir/f $DIR/d31i/g
1756         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1757         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1758         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1759         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1760 }
1761 run_test 31i "cross directory link under parent==============="
1762
1763
1764 test_31j() {
1765         mkdir $DIR/d31j
1766         mkdir $DIR/d31j/dir1
1767         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1768         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1769         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1770         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1771         return 0
1772 }
1773 run_test 31j "link for directory==============="
1774
1775
1776 test_31k() {
1777         mkdir $DIR/d31k
1778         touch $DIR/d31k/s
1779         touch $DIR/d31k/exist
1780         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1781         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1782         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1783         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1784         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1785         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1786         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1787         return 0
1788 }
1789 run_test 31k "link to file: the same, non-existing, dir==============="
1790
1791 test_31m() {
1792         mkdir $DIR/d31m
1793         touch $DIR/d31m/s
1794         mkdir $DIR/d31m2
1795         touch $DIR/d31m2/exist
1796         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1797         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1798         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1799         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1800         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1801         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1802         return 0
1803 }
1804 run_test 31m "link to file: the same, non-existing, dir==============="
1805
1806 cleanup_test32_mount() {
1807         trap 0
1808         $UMOUNT $DIR/$tdir/ext2-mountpoint
1809 }
1810
1811 test_32a() {
1812         echo "== more mountpoints and symlinks ================="
1813         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1814         trap cleanup_test32_mount EXIT
1815         mkdir -p $DIR/$tdir/ext2-mountpoint
1816         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1817         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
1818         cleanup_test32_mount
1819 }
1820 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1821
1822 test_32b() {
1823         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1824         trap cleanup_test32_mount EXIT
1825         mkdir -p $DIR/$tdir/ext2-mountpoint
1826         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1827         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
1828         cleanup_test32_mount
1829 }
1830 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1831
1832 test_32c() {
1833         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1834         trap cleanup_test32_mount EXIT
1835         mkdir -p $DIR/$tdir/ext2-mountpoint
1836         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1837         mkdir -p $DIR/$tdir/d2/test_dir
1838         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
1839         cleanup_test32_mount
1840 }
1841 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1842
1843 test_32d() {
1844         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1845         trap cleanup_test32_mount EXIT
1846         mkdir -p $DIR/$tdir/ext2-mountpoint
1847         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1848         mkdir -p $DIR/$tdir/d2/test_dir
1849         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
1850         cleanup_test32_mount
1851 }
1852 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1853
1854 test_32e() {
1855         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1856         mkdir -p $DIR/d32e/tmp
1857         TMP_DIR=$DIR/d32e/tmp
1858         ln -s $DIR/d32e $TMP_DIR/symlink11
1859         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1860         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1861         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1862 }
1863 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1864
1865 test_32f() {
1866         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1867         mkdir -p $DIR/d32f/tmp
1868         TMP_DIR=$DIR/d32f/tmp
1869         ln -s $DIR/d32f $TMP_DIR/symlink11
1870         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1871         ls $DIR/d32f/tmp/symlink11  || error
1872         ls $DIR/d32f/symlink01 || error
1873 }
1874 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
1875
1876 test_32g() {
1877         TMP_DIR=$DIR/$tdir/tmp
1878         mkdir -p $TMP_DIR $DIR/${tdir}2
1879         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1880         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1881         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
1882         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
1883         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
1884         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
1885 }
1886 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1887
1888 test_32h() {
1889         rm -fr $DIR/$tdir $DIR/${tdir}2
1890         TMP_DIR=$DIR/$tdir/tmp
1891         mkdir -p $TMP_DIR $DIR/${tdir}2
1892         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1893         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1894         ls $TMP_DIR/symlink12 || error
1895         ls $DIR/$tdir/symlink02  || error
1896 }
1897 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1898
1899 test_32i() {
1900         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1901         trap cleanup_test32_mount EXIT
1902         mkdir -p $DIR/$tdir/ext2-mountpoint
1903         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1904         touch $DIR/$tdir/test_file
1905         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
1906         cleanup_test32_mount
1907 }
1908 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
1909
1910 test_32j() {
1911         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1912         trap cleanup_test32_mount EXIT
1913         mkdir -p $DIR/$tdir/ext2-mountpoint
1914         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1915         touch $DIR/$tdir/test_file
1916         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
1917         cleanup_test32_mount
1918 }
1919 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
1920
1921 test_32k() {
1922         rm -fr $DIR/$tdir
1923         trap cleanup_test32_mount EXIT
1924         mkdir -p $DIR/$tdir/ext2-mountpoint
1925         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
1926         mkdir -p $DIR/$tdir/d2
1927         touch $DIR/$tdir/d2/test_file || error
1928         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
1929         cleanup_test32_mount
1930 }
1931 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
1932
1933 test_32l() {
1934         rm -fr $DIR/$tdir
1935         trap cleanup_test32_mount EXIT
1936         mkdir -p $DIR/$tdir/ext2-mountpoint
1937         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1938         mkdir -p $DIR/$tdir/d2
1939         touch $DIR/$tdir/d2/test_file
1940         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
1941         cleanup_test32_mount
1942 }
1943 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
1944
1945 test_32m() {
1946         rm -fr $DIR/d32m
1947         mkdir -p $DIR/d32m/tmp
1948         TMP_DIR=$DIR/d32m/tmp
1949         ln -s $DIR $TMP_DIR/symlink11
1950         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1951         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
1952         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
1953 }
1954 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
1955
1956 test_32n() {
1957         rm -fr $DIR/d32n
1958         mkdir -p $DIR/d32n/tmp
1959         TMP_DIR=$DIR/d32n/tmp
1960         ln -s $DIR $TMP_DIR/symlink11
1961         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1962         ls -l $DIR/d32n/tmp/symlink11  || error
1963         ls -l $DIR/d32n/symlink01 || error
1964 }
1965 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
1966
1967 test_32o() {
1968         rm -fr $DIR/d32o $DIR/$tfile
1969         touch $DIR/$tfile
1970         mkdir -p $DIR/d32o/tmp
1971         TMP_DIR=$DIR/d32o/tmp
1972         ln -s $DIR/$tfile $TMP_DIR/symlink12
1973         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1974         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
1975         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
1976         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
1977         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
1978 }
1979 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
1980
1981 test_32p() {
1982     log 32p_1
1983         rm -fr $DIR/d32p
1984     log 32p_2
1985         rm -f $DIR/$tfile
1986     log 32p_3
1987         touch $DIR/$tfile
1988     log 32p_4
1989         mkdir -p $DIR/d32p/tmp
1990     log 32p_5
1991         TMP_DIR=$DIR/d32p/tmp
1992     log 32p_6
1993         ln -s $DIR/$tfile $TMP_DIR/symlink12
1994     log 32p_7
1995         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1996     log 32p_8
1997         cat $DIR/d32p/tmp/symlink12 || error
1998     log 32p_9
1999         cat $DIR/d32p/symlink02 || error
2000     log 32p_10
2001 }
2002 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2003
2004 cleanup_testdir_mount() {
2005         trap 0
2006         $UMOUNT $DIR/$tdir
2007 }
2008
2009 test_32q() {
2010         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2011         trap cleanup_testdir_mount EXIT
2012         mkdir -p $DIR/$tdir
2013         touch $DIR/$tdir/under_the_mount
2014         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2015         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2016         cleanup_testdir_mount
2017 }
2018 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2019
2020 test_32r() {
2021         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2022         trap cleanup_testdir_mount EXIT
2023         mkdir -p $DIR/$tdir
2024         touch $DIR/$tdir/under_the_mount
2025         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2026         ls $DIR/$tdir | grep -q under_the_mount && error || true
2027         cleanup_testdir_mount
2028 }
2029 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2030
2031 test_33() {
2032         rm -f $DIR/$tfile
2033         touch $DIR/$tfile
2034         chmod 444 $DIR/$tfile
2035         chown $RUNAS_ID $DIR/$tfile
2036         log 33_1
2037         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2038         log 33_2
2039 }
2040 run_test 33 "write file with mode 444 (should return error) ===="
2041
2042 test_33a() {
2043         rm -fr $DIR/d33
2044         mkdir -p $DIR/d33
2045         chown $RUNAS_ID $DIR/d33
2046         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2047         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2048                 error "open RDWR" || true
2049 }
2050 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2051
2052 test_33b() {
2053         rm -fr $DIR/d33
2054         mkdir -p $DIR/d33
2055         chown $RUNAS_ID $DIR/d33
2056         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2057 }
2058 run_test 33b "test open file with malformed flags (No panic and return error)"
2059
2060 test_33c() {
2061         local ostnum
2062         local ostname
2063         local write_bytes
2064         local all_zeros
2065
2066         remote_ost_nodsh && skip "remote OST with nodsh" && return
2067         all_zeros=:
2068         rm -fr $DIR/d33
2069         mkdir -p $DIR/d33
2070         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2071
2072         sync
2073         for ostnum in $(seq $OSTCOUNT); do
2074                 # test-framework's OST numbering is one-based, while Lustre's
2075                 # is zero-based
2076                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2077                 # Parsing llobdstat's output sucks; we could grep the /proc
2078                 # path, but that's likely to not be as portable as using the
2079                 # llobdstat utility.  So we parse lctl output instead.
2080                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2081                         obdfilter/$ostname/stats |
2082                         awk '/^write_bytes/ {print $7}' )
2083                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2084                 if (( ${write_bytes:-0} > 0 ))
2085                 then
2086                         all_zeros=false
2087                         break;
2088                 fi
2089         done
2090
2091         $all_zeros || return 0
2092
2093         # Write four bytes
2094         echo foo > $DIR/d33/bar
2095         # Really write them
2096         sync
2097
2098         # Total up write_bytes after writing.  We'd better find non-zeros.
2099         for ostnum in $(seq $OSTCOUNT); do
2100                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2101                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2102                         obdfilter/$ostname/stats |
2103                         awk '/^write_bytes/ {print $7}' )
2104                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2105                 if (( ${write_bytes:-0} > 0 ))
2106                 then
2107                         all_zeros=false
2108                         break;
2109                 fi
2110         done
2111
2112         if $all_zeros
2113         then
2114                 for ostnum in $(seq $OSTCOUNT); do
2115                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2116                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2117                         do_facet ost$ostnum lctl get_param -n \
2118                                 obdfilter/$ostname/stats
2119                 done
2120                 error "OST not keeping write_bytes stats (b22312)"
2121         fi
2122 }
2123 run_test 33c "test llobdstat and write_bytes"
2124
2125 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2126 test_34a() {
2127         rm -f $DIR/f34
2128         $MCREATE $DIR/f34 || error
2129         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2130         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2131         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2132         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2133 }
2134 run_test 34a "truncate file that has not been opened ==========="
2135
2136 test_34b() {
2137         [ ! -f $DIR/f34 ] && test_34a
2138         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2139         $OPENFILE -f O_RDONLY $DIR/f34
2140         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2141         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2142 }
2143 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2144
2145 test_34c() {
2146         [ ! -f $DIR/f34 ] && test_34a
2147         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2148         $OPENFILE -f O_RDWR $DIR/f34
2149         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2150         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2151 }
2152 run_test 34c "O_RDWR opening file-with-size works =============="
2153
2154 test_34d() {
2155         [ ! -f $DIR/f34 ] && test_34a
2156         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2157         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2158         rm $DIR/f34
2159 }
2160 run_test 34d "write to sparse file ============================="
2161
2162 test_34e() {
2163         rm -f $DIR/f34e
2164         $MCREATE $DIR/f34e || error
2165         $TRUNCATE $DIR/f34e 1000 || error
2166         $CHECKSTAT -s 1000 $DIR/f34e || error
2167         $OPENFILE -f O_RDWR $DIR/f34e
2168         $CHECKSTAT -s 1000 $DIR/f34e || error
2169 }
2170 run_test 34e "create objects, some with size and some without =="
2171
2172 test_34f() { # bug 6242, 6243
2173         SIZE34F=48000
2174         rm -f $DIR/f34f
2175         $MCREATE $DIR/f34f || error
2176         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2177         dd if=$DIR/f34f of=$TMP/f34f
2178         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2179         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2180         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2181         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2182         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2183 }
2184 run_test 34f "read from a file with no objects until EOF ======="
2185
2186 test_34g() {
2187         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2188         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2189         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2190         cancel_lru_locks osc
2191         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2192                 error "wrong size after lock cancel"
2193
2194         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2195         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2196                 error "expanding truncate failed"
2197         cancel_lru_locks osc
2198         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2199                 error "wrong expanded size after lock cancel"
2200 }
2201 run_test 34g "truncate long file ==============================="
2202
2203 test_34h() {
2204         local gid=10
2205         local sz=1000
2206
2207         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2208         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2209         MULTIPID=$!
2210         sleep 2
2211
2212         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2213                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2214                 kill -9 $MULTIPID
2215         fi
2216         wait $MULTIPID
2217         local nsz=`stat -c %s $DIR/$tfile`
2218         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2219 }
2220 run_test 34h "ftruncate file under grouplock should not block"
2221
2222 test_35a() {
2223         cp /bin/sh $DIR/f35a
2224         chmod 444 $DIR/f35a
2225         chown $RUNAS_ID $DIR/f35a
2226         $RUNAS $DIR/f35a && error || true
2227         rm $DIR/f35a
2228 }
2229 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2230
2231 test_36a() {
2232         rm -f $DIR/f36
2233         utime $DIR/f36 || error
2234 }
2235 run_test 36a "MDS utime check (mknod, utime) ==================="
2236
2237 test_36b() {
2238         echo "" > $DIR/f36
2239         utime $DIR/f36 || error
2240 }
2241 run_test 36b "OST utime check (open, utime) ===================="
2242
2243 test_36c() {
2244         rm -f $DIR/d36/f36
2245         mkdir $DIR/d36
2246         chown $RUNAS_ID $DIR/d36
2247         $RUNAS utime $DIR/d36/f36 || error
2248 }
2249 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2250
2251 test_36d() {
2252         [ ! -d $DIR/d36 ] && test_36c
2253         echo "" > $DIR/d36/f36
2254         $RUNAS utime $DIR/d36/f36 || error
2255 }
2256 run_test 36d "non-root OST utime check (open, utime) ==========="
2257
2258 test_36e() {
2259         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2260         mkdir -p $DIR/$tdir
2261         touch $DIR/$tdir/$tfile
2262         $RUNAS utime $DIR/$tdir/$tfile && \
2263                 error "utime worked, expected failure" || true
2264 }
2265 run_test 36e "utime on non-owned file (should return error) ===="
2266
2267 subr_36fh() {
2268         local fl="$1"
2269         local LANG_SAVE=$LANG
2270         local LC_LANG_SAVE=$LC_LANG
2271         export LANG=C LC_LANG=C # for date language
2272
2273         DATESTR="Dec 20  2000"
2274         mkdir -p $DIR/$tdir
2275         lctl set_param fail_loc=$fl
2276         date; date +%s
2277         cp /etc/hosts $DIR/$tdir/$tfile
2278         sync & # write RPC generated with "current" inode timestamp, but delayed
2279         sleep 1
2280         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2281         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2282         cancel_lru_locks osc
2283         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2284         date; date +%s
2285         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2286                 echo "BEFORE: $LS_BEFORE" && \
2287                 echo "AFTER : $LS_AFTER" && \
2288                 echo "WANT  : $DATESTR" && \
2289                 error "$DIR/$tdir/$tfile timestamps changed" || true
2290
2291         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2292 }
2293
2294 test_36f() {
2295         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2296         subr_36fh "0x80000214"
2297 }
2298 run_test 36f "utime on file racing with OST BRW write =========="
2299
2300 test_36g() {
2301         remote_ost_nodsh && skip "remote OST with nodsh" && return
2302         local fmd_max_age
2303         local fmd_before
2304         local fmd_after
2305
2306         mkdir -p $DIR/$tdir
2307         fmd_max_age=$(do_facet ost1 \
2308                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2309                 head -n 1")
2310
2311         fmd_before=$(do_facet ost1 \
2312                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2313         touch $DIR/$tdir/$tfile
2314         sleep $((fmd_max_age + 12))
2315         fmd_after=$(do_facet ost1 \
2316                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2317
2318         echo "fmd_before: $fmd_before"
2319         echo "fmd_after: $fmd_after"
2320         [ "$fmd_after" -gt "$fmd_before" ] && \
2321                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2322                 error "fmd didn't expire after ping" || true
2323 }
2324 run_test 36g "filter mod data cache expiry ====================="
2325
2326 test_36h() {
2327         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2328         subr_36fh "0x80000227"
2329 }
2330 run_test 36h "utime on file racing with OST BRW write =========="
2331
2332 # test_37 - duplicate with tests 32q 32r
2333
2334 test_38() {
2335         local file=$DIR/$tfile
2336         touch $file
2337         openfile -f O_DIRECTORY $file
2338         local RC=$?
2339         local ENOTDIR=20
2340         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2341         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2342 }
2343 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2344
2345 test_39() {
2346         touch $DIR/$tfile
2347         touch $DIR/${tfile}2
2348 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2349 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2350 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2351         sleep 2
2352         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2353         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2354                 echo "mtime"
2355                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2356                 echo "atime"
2357                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2358                 echo "ctime"
2359                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2360                 error "O_TRUNC didn't change timestamps"
2361         fi
2362 }
2363 run_test 39 "mtime changed on create ==========================="
2364
2365 test_39b() {
2366         mkdir -p $DIR/$tdir
2367         cp -p /etc/passwd $DIR/$tdir/fopen
2368         cp -p /etc/passwd $DIR/$tdir/flink
2369         cp -p /etc/passwd $DIR/$tdir/funlink
2370         cp -p /etc/passwd $DIR/$tdir/frename
2371         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2372
2373         sleep 1
2374         echo "aaaaaa" >> $DIR/$tdir/fopen
2375         echo "aaaaaa" >> $DIR/$tdir/flink
2376         echo "aaaaaa" >> $DIR/$tdir/funlink
2377         echo "aaaaaa" >> $DIR/$tdir/frename
2378
2379         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2380         local link_new=`stat -c %Y $DIR/$tdir/flink`
2381         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2382         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2383
2384         cat $DIR/$tdir/fopen > /dev/null
2385         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2386         rm -f $DIR/$tdir/funlink2
2387         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2388
2389         for (( i=0; i < 2; i++ )) ; do
2390                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2391                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2392                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2393                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2394
2395                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2396                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2397                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2398                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2399
2400                 cancel_lru_locks osc
2401                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2402         done
2403 }
2404 run_test 39b "mtime change on open, link, unlink, rename  ======"
2405
2406 # this should be set to past
2407 TEST_39_MTIME=`date -d "1 year ago" +%s`
2408
2409 # bug 11063
2410 test_39c() {
2411         touch $DIR1/$tfile
2412         sleep 2
2413         local mtime0=`stat -c %Y $DIR1/$tfile`
2414
2415         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2416         local mtime1=`stat -c %Y $DIR1/$tfile`
2417         [ "$mtime1" = $TEST_39_MTIME ] || \
2418                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2419
2420         local d1=`date +%s`
2421         echo hello >> $DIR1/$tfile
2422         local d2=`date +%s`
2423         local mtime2=`stat -c %Y $DIR1/$tfile`
2424         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2425                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2426
2427         mv $DIR1/$tfile $DIR1/$tfile-1
2428
2429         for (( i=0; i < 2; i++ )) ; do
2430                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2431                 [ "$mtime2" = "$mtime3" ] || \
2432                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2433
2434                 cancel_lru_locks osc
2435                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2436         done
2437 }
2438 run_test 39c "mtime change on rename ==========================="
2439
2440 # bug 21114
2441 test_39d() {
2442         touch $DIR1/$tfile
2443
2444         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2445
2446         for (( i=0; i < 2; i++ )) ; do
2447                 local mtime=`stat -c %Y $DIR1/$tfile`
2448                 [ $mtime = $TEST_39_MTIME ] || \
2449                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2450
2451                 cancel_lru_locks osc
2452                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2453         done
2454 }
2455 run_test 39d "create, utime, stat =============================="
2456
2457 # bug 21114
2458 test_39e() {
2459         touch $DIR1/$tfile
2460         local mtime1=`stat -c %Y $DIR1/$tfile`
2461
2462         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2463
2464         for (( i=0; i < 2; i++ )) ; do
2465                 local mtime2=`stat -c %Y $DIR1/$tfile`
2466                 [ $mtime2 = $TEST_39_MTIME ] || \
2467                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2468
2469                 cancel_lru_locks osc
2470                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2471         done
2472 }
2473 run_test 39e "create, stat, utime, stat ========================"
2474
2475 # bug 21114
2476 test_39f() {
2477         touch $DIR1/$tfile
2478         mtime1=`stat -c %Y $DIR1/$tfile`
2479
2480         sleep 2
2481         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2482
2483         for (( i=0; i < 2; i++ )) ; do
2484                 local mtime2=`stat -c %Y $DIR1/$tfile`
2485                 [ $mtime2 = $TEST_39_MTIME ] || \
2486                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2487
2488                 cancel_lru_locks osc
2489                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2490         done
2491 }
2492 run_test 39f "create, stat, sleep, utime, stat ================="
2493
2494 # bug 11063
2495 test_39g() {
2496         echo hello >> $DIR1/$tfile
2497         local mtime1=`stat -c %Y $DIR1/$tfile`
2498
2499         sleep 2
2500         chmod o+r $DIR1/$tfile
2501
2502         for (( i=0; i < 2; i++ )) ; do
2503                 local mtime2=`stat -c %Y $DIR1/$tfile`
2504                 [ "$mtime1" = "$mtime2" ] || \
2505                         error "lost mtime: $mtime2, should be $mtime1"
2506
2507                 cancel_lru_locks osc
2508                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2509         done
2510 }
2511 run_test 39g "write, chmod, stat ==============================="
2512
2513 # bug 11063
2514 test_39h() {
2515         touch $DIR1/$tfile
2516         sleep 1
2517
2518         local d1=`date`
2519         echo hello >> $DIR1/$tfile
2520         local mtime1=`stat -c %Y $DIR1/$tfile`
2521
2522         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2523         local d2=`date`
2524         if [ "$d1" != "$d2" ]; then
2525                 echo "write and touch not within one second"
2526         else
2527                 for (( i=0; i < 2; i++ )) ; do
2528                         local mtime2=`stat -c %Y $DIR1/$tfile`
2529                         [ "$mtime2" = $TEST_39_MTIME ] || \
2530                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2531
2532                         cancel_lru_locks osc
2533                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2534                 done
2535         fi
2536 }
2537 run_test 39h "write, utime within one second, stat ============="
2538
2539 test_39i() {
2540         touch $DIR1/$tfile
2541         sleep 1
2542
2543         echo hello >> $DIR1/$tfile
2544         local mtime1=`stat -c %Y $DIR1/$tfile`
2545
2546         mv $DIR1/$tfile $DIR1/$tfile-1
2547
2548         for (( i=0; i < 2; i++ )) ; do
2549                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2550
2551                 [ "$mtime1" = "$mtime2" ] || \
2552                         error "lost mtime: $mtime2, should be $mtime1"
2553
2554                 cancel_lru_locks osc
2555                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2556         done
2557 }
2558 run_test 39i "write, rename, stat =============================="
2559
2560 test_39j() {
2561         start_full_debug_logging
2562         touch $DIR1/$tfile
2563         sleep 1
2564
2565         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2566         lctl set_param fail_loc=0x80000412
2567         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2568                 error "multiop failed"
2569         local multipid=$!
2570         local mtime1=`stat -c %Y $DIR1/$tfile`
2571
2572         mv $DIR1/$tfile $DIR1/$tfile-1
2573
2574         kill -USR1 $multipid
2575         wait $multipid || error "multiop close failed"
2576
2577         for (( i=0; i < 2; i++ )) ; do
2578                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2579                 [ "$mtime1" = "$mtime2" ] ||
2580                         error "mtime is lost on close: $mtime2, " \
2581                               "should be $mtime1"
2582
2583                 cancel_lru_locks osc
2584                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2585         done
2586         lctl set_param fail_loc=0
2587         stop_full_debug_logging
2588 }
2589 run_test 39j "write, rename, close, stat ======================="
2590
2591 test_39k() {
2592         touch $DIR1/$tfile
2593         sleep 1
2594
2595         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2596         local multipid=$!
2597         local mtime1=`stat -c %Y $DIR1/$tfile`
2598
2599         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2600
2601         kill -USR1 $multipid
2602         wait $multipid || error "multiop close failed"
2603
2604         for (( i=0; i < 2; i++ )) ; do
2605                 local mtime2=`stat -c %Y $DIR1/$tfile`
2606
2607                 [ "$mtime2" = $TEST_39_MTIME ] || \
2608                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2609
2610                 cancel_lru_locks osc
2611                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2612         done
2613 }
2614 run_test 39k "write, utime, close, stat ========================"
2615
2616 # this should be set to future
2617 TEST_39_ATIME=`date -d "1 year" +%s`
2618
2619 test_39l() {
2620         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2621         local atime_diff=$(do_facet $SINGLEMDS \
2622                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2623         mkdir -p $DIR/$tdir
2624
2625         # test setting directory atime to future
2626         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2627         local atime=$(stat -c %X $DIR/$tdir)
2628         [ "$atime" = $TEST_39_ATIME ] || \
2629                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2630
2631         # test setting directory atime from future to now
2632         local d1=$(date +%s)
2633         ls $DIR/$tdir
2634         local d2=$(date +%s)
2635
2636         cancel_lru_locks mdc
2637         atime=$(stat -c %X $DIR/$tdir)
2638         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2639                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2640
2641         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2642         sleep 3
2643
2644         # test setting directory atime when now > dir atime + atime_diff
2645         d1=$(date +%s)
2646         ls $DIR/$tdir
2647         d2=$(date +%s)
2648         cancel_lru_locks mdc
2649         atime=$(stat -c %X $DIR/$tdir)
2650         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2651                 error "atime is not updated  : $atime, should be $d2"
2652
2653         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2654         sleep 3
2655
2656         # test not setting directory atime when now < dir atime + atime_diff
2657         ls $DIR/$tdir
2658         cancel_lru_locks mdc
2659         atime=$(stat -c %X $DIR/$tdir)
2660         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2661                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2662
2663         do_facet $SINGLEMDS \
2664                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2665 }
2666 run_test 39l "directory atime update ==========================="
2667
2668 test_39m() {
2669         touch $DIR1/$tfile
2670         sleep 2
2671         local far_past_mtime=$(date -d "May 29 1953" +%s)
2672         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2673
2674         touch -m -d @$far_past_mtime $DIR1/$tfile
2675         touch -a -d @$far_past_atime $DIR1/$tfile
2676
2677         for (( i=0; i < 2; i++ )) ; do
2678                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2679                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2680                         error "atime or mtime set incorrectly"
2681
2682                 cancel_lru_locks osc
2683                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2684         done
2685 }
2686 run_test 39m "test atime and mtime before 1970"
2687
2688 test_40() {
2689         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2690         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2691         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2692 }
2693 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2694
2695 test_41() {
2696         # bug 1553
2697         small_write $DIR/f41 18
2698 }
2699 run_test 41 "test small file write + fstat ====================="
2700
2701 count_ost_writes() {
2702         lctl get_param -n osc.*.stats |
2703             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2704 }
2705
2706 # decent default
2707 WRITEBACK_SAVE=500
2708 DIRTY_RATIO_SAVE=40
2709 MAX_DIRTY_RATIO=50
2710 BG_DIRTY_RATIO_SAVE=10
2711 MAX_BG_DIRTY_RATIO=25
2712
2713 start_writeback() {
2714         trap 0
2715         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2716         # dirty_ratio, dirty_background_ratio
2717         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2718                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2719                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2720                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2721         else
2722                 # if file not here, we are a 2.4 kernel
2723                 kill -CONT `pidof kupdated`
2724         fi
2725 }
2726
2727 stop_writeback() {
2728         # setup the trap first, so someone cannot exit the test at the
2729         # exact wrong time and mess up a machine
2730         trap start_writeback EXIT
2731         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2732         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2733                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2734                 sysctl -w vm.dirty_writeback_centisecs=0
2735                 sysctl -w vm.dirty_writeback_centisecs=0
2736                 # save and increase /proc/sys/vm/dirty_ratio
2737                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2738                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2739                 # save and increase /proc/sys/vm/dirty_background_ratio
2740                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2741                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2742         else
2743                 # if file not here, we are a 2.4 kernel
2744                 kill -STOP `pidof kupdated`
2745         fi
2746 }
2747
2748 # ensure that all stripes have some grant before we test client-side cache
2749 setup_test42() {
2750         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2751                 dd if=/dev/zero of=$i bs=4k count=1
2752                 rm $i
2753         done
2754 }
2755
2756 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2757 # file truncation, and file removal.
2758 test_42a() {
2759         setup_test42
2760         cancel_lru_locks osc
2761         stop_writeback
2762         sync; sleep 1; sync # just to be safe
2763         BEFOREWRITES=`count_ost_writes`
2764         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2765         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2766         AFTERWRITES=`count_ost_writes`
2767         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2768                 error "$BEFOREWRITES < $AFTERWRITES"
2769         start_writeback
2770 }
2771 run_test 42a "ensure that we don't flush on close =============="
2772
2773 test_42b() {
2774         setup_test42
2775         cancel_lru_locks osc
2776         stop_writeback
2777         sync
2778         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2779         BEFOREWRITES=`count_ost_writes`
2780         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2781         AFTERWRITES=`count_ost_writes`
2782         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2783                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2784         fi
2785         BEFOREWRITES=`count_ost_writes`
2786         sync || error "sync: $?"
2787         AFTERWRITES=`count_ost_writes`
2788         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2789                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2790         fi
2791         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2792         start_writeback
2793         return 0
2794 }
2795 run_test 42b "test destroy of file with cached dirty data ======"
2796
2797 # if these tests just want to test the effect of truncation,
2798 # they have to be very careful.  consider:
2799 # - the first open gets a {0,EOF}PR lock
2800 # - the first write conflicts and gets a {0, count-1}PW
2801 # - the rest of the writes are under {count,EOF}PW
2802 # - the open for truncate tries to match a {0,EOF}PR
2803 #   for the filesize and cancels the PWs.
2804 # any number of fixes (don't get {0,EOF} on open, match
2805 # composite locks, do smarter file size management) fix
2806 # this, but for now we want these tests to verify that
2807 # the cancellation with truncate intent works, so we
2808 # start the file with a full-file pw lock to match against
2809 # until the truncate.
2810 trunc_test() {
2811         test=$1
2812         file=$DIR/$test
2813         offset=$2
2814         cancel_lru_locks osc
2815         stop_writeback
2816         # prime the file with 0,EOF PW to match
2817         touch $file
2818         $TRUNCATE $file 0
2819         sync; sync
2820         # now the real test..
2821         dd if=/dev/zero of=$file bs=1024 count=100
2822         BEFOREWRITES=`count_ost_writes`
2823         $TRUNCATE $file $offset
2824         cancel_lru_locks osc
2825         AFTERWRITES=`count_ost_writes`
2826         start_writeback
2827 }
2828
2829 test_42c() {
2830         trunc_test 42c 1024
2831         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2832             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2833         rm $file
2834 }
2835 run_test 42c "test partial truncate of file with cached dirty data"
2836
2837 test_42d() {
2838         trunc_test 42d 0
2839         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2840             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2841         rm $file
2842 }
2843 run_test 42d "test complete truncate of file with cached dirty data"
2844
2845 test_42e() { # bug22074
2846         local TDIR=$DIR/${tdir}e
2847         local pagesz=$(page_size)
2848         local pages=16 # hardcoded 16 pages, don't change it.
2849         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
2850         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2851         local max_dirty_mb
2852         local warmup_files
2853
2854         mkdir -p $TDIR
2855         $SETSTRIPE -c 1 $TDIR
2856         createmany -o $TDIR/f $files
2857
2858         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2859
2860         # we assume that with $OSTCOUNT files, at least one of them will
2861         # be allocated on OST0.
2862         warmup_files=$((OSTCOUNT * max_dirty_mb))
2863         createmany -o $TDIR/w $warmup_files
2864
2865         # write a large amount of data into one file and sync, to get good
2866         # avail_grant number from OST.
2867         for ((i=0; i<$warmup_files; i++)); do
2868                 idx=$($GETSTRIPE -i $TDIR/w$i)
2869                 [ $idx -ne 0 ] && continue
2870                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2871                 break
2872         done
2873         [ $i -gt $warmup_files ] && error "OST0 is still cold"
2874         sync
2875         $LCTL get_param $proc_osc0/cur_dirty_bytes
2876         $LCTL get_param $proc_osc0/cur_grant_bytes
2877
2878         # create as much dirty pages as we can while not to trigger the actual
2879         # RPCs directly. but depends on the env, VFS may trigger flush during this
2880         # period, hopefully we are good.
2881         for ((i=0; i<$warmup_files; i++)); do
2882                 idx=$($GETSTRIPE -i $TDIR/w$i)
2883                 [ $idx -ne 0 ] && continue
2884                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2885         done
2886         $LCTL get_param $proc_osc0/cur_dirty_bytes
2887         $LCTL get_param $proc_osc0/cur_grant_bytes
2888
2889         # perform the real test
2890         $LCTL set_param $proc_osc0/rpc_stats 0
2891         for ((;i<$files; i++)); do
2892                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
2893                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2894         done
2895         sync
2896         $LCTL get_param $proc_osc0/rpc_stats
2897
2898         local percent=0
2899         local have_ppr=false
2900         $LCTL get_param $proc_osc0/rpc_stats |
2901                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2902                         # skip lines until we are at the RPC histogram data
2903                         [ "$PPR" == "pages" ] && have_ppr=true && continue
2904                         $have_ppr || continue
2905
2906                         # we only want the percent stat for < 16 pages
2907                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
2908
2909                         percent=$((percent + WPCT))
2910                         if [ $percent -gt 15 ]; then
2911                                 error "less than 16-pages write RPCs" \
2912                                       "$percent% > 15%"
2913                                 break
2914                         fi
2915                 done
2916         rm -rf $TDIR
2917 }
2918 run_test 42e "verify sub-RPC writes are not done synchronously"
2919
2920 test_43() {
2921         mkdir -p $DIR/$tdir
2922         cp -p /bin/ls $DIR/$tdir/$tfile
2923         $MULTIOP $DIR/$tdir/$tfile Ow_c &
2924         pid=$!
2925         # give multiop a chance to open
2926         sleep 1
2927
2928         $DIR/$tdir/$tfile && error || true
2929         kill -USR1 $pid
2930 }
2931 run_test 43 "execution of file opened for write should return -ETXTBSY"
2932
2933 test_43a() {
2934         mkdir -p $DIR/d43
2935         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2936         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2937         MULTIOP_PID=$!
2938         $MULTIOP $DIR/d43/multiop Oc && error "expected error, got success"
2939         kill -USR1 $MULTIOP_PID || return 2
2940         wait $MULTIOP_PID || return 3
2941         rm $TMP/test43.junk
2942 }
2943 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2944
2945 test_43b() {
2946         mkdir -p $DIR/d43
2947         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2948         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2949         MULTIOP_PID=$!
2950         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
2951         kill -USR1 $MULTIOP_PID || return 2
2952         wait $MULTIOP_PID || return 3
2953         rm $TMP/test43.junk
2954 }
2955 run_test 43b "truncate of file being executed should return -ETXTBSY"
2956
2957 test_43c() {
2958         local testdir="$DIR/d43c"
2959         mkdir -p $testdir
2960         cp $SHELL $testdir/
2961         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2962                 ( cd $testdir && md5sum -c)
2963 }
2964 run_test 43c "md5sum of copy into lustre========================"
2965
2966 test_44() {
2967         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2968         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2969         dd if=$DIR/f1 bs=4k count=1 > /dev/null
2970 }
2971 run_test 44 "zero length read from a sparse stripe ============="
2972
2973 test_44a() {
2974     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2975                          awk '{print $2}'`
2976     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2977     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2978     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2979                       awk '{print $2}'`
2980     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2981         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2982     fi
2983
2984     OFFSETS="0 $((stride/2)) $((stride-1))"
2985     for offset in $OFFSETS ; do
2986       for i in `seq 0 $((nstripe-1))`; do
2987         local GLOBALOFFSETS=""
2988         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2989         local myfn=$DIR/d44a-$size
2990         echo "--------writing $myfn at $size"
2991         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
2992         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2993         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2994                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2995
2996         for j in `seq 0 $((nstripe-1))`; do
2997             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2998             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
2999             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3000         done
3001         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3002                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3003         rm -f $myfn
3004       done
3005     done
3006 }
3007 run_test 44a "test sparse pwrite ==============================="
3008
3009 dirty_osc_total() {
3010         tot=0
3011         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3012                 tot=$(($tot + $d))
3013         done
3014         echo $tot
3015 }
3016 do_dirty_record() {
3017         before=`dirty_osc_total`
3018         echo executing "\"$*\""
3019         eval $*
3020         after=`dirty_osc_total`
3021         echo before $before, after $after
3022 }
3023 test_45() {
3024         f="$DIR/f45"
3025         # Obtain grants from OST if it supports it
3026         echo blah > ${f}_grant
3027         stop_writeback
3028         sync
3029         do_dirty_record "echo blah > $f"
3030         [ $before -eq $after ] && error "write wasn't cached"
3031         do_dirty_record "> $f"
3032         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3033         do_dirty_record "echo blah > $f"
3034         [ $before -eq $after ] && error "write wasn't cached"
3035         do_dirty_record "sync"
3036         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3037         do_dirty_record "echo blah > $f"
3038         [ $before -eq $after ] && error "write wasn't cached"
3039         do_dirty_record "cancel_lru_locks osc"
3040         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3041         start_writeback
3042 }
3043 run_test 45 "osc io page accounting ============================"
3044
3045 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3046 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3047 # objects offset and an assert hit when an rpc was built with 1023's mapped
3048 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3049 test_46() {
3050         f="$DIR/f46"
3051         stop_writeback
3052         sync
3053         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3054         sync
3055         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3056         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3057         sync
3058         start_writeback
3059 }
3060 run_test 46 "dirtying a previously written page ================"
3061
3062 # test_47 is removed "Device nodes check" is moved to test_28
3063
3064 test_48a() { # bug 2399
3065         check_kernel_version 34 || return 0
3066         mkdir -p $DIR/d48a
3067         cd $DIR/d48a
3068         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
3069         mkdir $DIR/d48a || error "recreate directory failed"
3070         touch foo || error "'touch foo' failed after recreating cwd"
3071         mkdir bar || error "'mkdir foo' failed after recreating cwd"
3072         if check_kernel_version 44; then
3073                 touch .foo || error "'touch .foo' failed after recreating cwd"
3074                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3075         fi
3076         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3077         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3078         cd . || error "'cd .' failed after recreating cwd"
3079         mkdir . && error "'mkdir .' worked after recreating cwd"
3080         rmdir . && error "'rmdir .' worked after recreating cwd"
3081         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3082         cd .. || error "'cd ..' failed after recreating cwd"
3083 }
3084 run_test 48a "Access renamed working dir (should return errors)="
3085
3086 test_48b() { # bug 2399
3087         check_kernel_version 34 || return 0
3088         mkdir -p $DIR/d48b
3089         cd $DIR/d48b
3090         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
3091         touch foo && error "'touch foo' worked after removing cwd"
3092         mkdir foo && error "'mkdir foo' worked after removing cwd"
3093         if check_kernel_version 44; then
3094                 touch .foo && error "'touch .foo' worked after removing cwd"
3095                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3096         fi
3097         ls . > /dev/null && error "'ls .' worked after removing cwd"
3098         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3099         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3100         mkdir . && error "'mkdir .' worked after removing cwd"
3101         rmdir . && error "'rmdir .' worked after removing cwd"
3102         ln -s . foo && error "'ln -s .' worked after removing cwd"
3103         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3104 }
3105 run_test 48b "Access removed working dir (should return errors)="
3106
3107 test_48c() { # bug 2350
3108         check_kernel_version 36 || return 0
3109         #lctl set_param debug=-1
3110         #set -vx
3111         mkdir -p $DIR/d48c/dir
3112         cd $DIR/d48c/dir
3113         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
3114         $TRACE touch foo && error "'touch foo' worked after removing cwd"
3115         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
3116         if check_kernel_version 44; then
3117                 touch .foo && error "'touch .foo' worked after removing cwd"
3118                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3119         fi
3120         $TRACE ls . && error "'ls .' worked after removing cwd"
3121         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3122         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
3123         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
3124         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3125         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3126         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3127 }
3128 run_test 48c "Access removed working subdir (should return errors)"
3129
3130 test_48d() { # bug 2350
3131         check_kernel_version 36 || return 0
3132         #lctl set_param debug=-1
3133         #set -vx
3134         mkdir -p $DIR/d48d/dir
3135         cd $DIR/d48d/dir
3136         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
3137         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
3138         $TRACE touch foo && error "'touch foo' worked after removing parent"
3139         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
3140         if check_kernel_version 44; then
3141                 touch .foo && error "'touch .foo' worked after removing parent"
3142                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
3143         fi
3144         $TRACE ls . && error "'ls .' worked after removing parent"
3145         $TRACE ls .. && error "'ls ..' worked after removing parent"
3146         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
3147         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
3148         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3149         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3150         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
3151 }
3152 run_test 48d "Access removed parent subdir (should return errors)"
3153
3154 test_48e() { # bug 4134
3155         check_kernel_version 41 || return 0
3156         #lctl set_param debug=-1
3157         #set -vx
3158         mkdir -p $DIR/d48e/dir
3159         cd $DIR/d48e/dir
3160         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3161         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3162         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3163         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3164         # On a buggy kernel addition of "touch foo" after cd .. will
3165         # produce kernel oops in lookup_hash_it
3166         touch ../foo && error "'cd ..' worked after recreate parent"
3167         cd $DIR
3168         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3169 }
3170 run_test 48e "Access to recreated parent subdir (should return errors)"
3171
3172 test_49() { # LU-1030
3173         # get ost1 size - lustre-OST0000
3174         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3175         # write 800M at maximum
3176         [ $ost1_size -gt 819200 ] && ost1_size=819200
3177
3178         lfs setstripe -c 1 -i 0 $DIR/$tfile
3179         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3180         local dd_pid=$!
3181
3182         # change max_pages_per_rpc while writing the file
3183         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3184         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3185         # loop until dd process exits
3186         while ps ax -opid | grep -wq $dd_pid; do
3187                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3188                 sleep $((RANDOM % 5 + 1))
3189         done
3190         # restore original max_pages_per_rpc
3191         $LCTL set_param $osc1_mppc=$orig_mppc
3192         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3193 }
3194 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3195
3196 test_50() {
3197         # bug 1485
3198         mkdir $DIR/d50
3199         cd $DIR/d50
3200         ls /proc/$$/cwd || error
3201 }
3202 run_test 50 "special situations: /proc symlinks  ==============="
3203
3204 test_51a() {    # was test_51
3205         # bug 1516 - create an empty entry right after ".." then split dir
3206         mkdir $DIR/d51
3207         touch $DIR/d51/foo
3208         $MCREATE $DIR/d51/bar
3209         rm $DIR/d51/foo
3210         createmany -m $DIR/d51/longfile 201
3211         FNUM=202
3212         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3213                 $MCREATE $DIR/d51/longfile$FNUM
3214                 FNUM=$(($FNUM + 1))
3215                 echo -n "+"
3216         done
3217         echo
3218         ls -l $DIR/d51 > /dev/null || error
3219 }
3220 run_test 51a "special situations: split htree with empty entry =="
3221
3222 export NUMTEST=70000
3223 test_51b() {
3224         local BASE=$DIR/$tdir
3225         mkdir -p $BASE
3226
3227         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3228         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3229         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3230                 return
3231
3232         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3233                 echo "reduced count to $NUMTEST due to inodes"
3234
3235         # need to check free space for the directories as well
3236         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3237         numfree=$((blkfree / 4))
3238         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3239                 echo "reduced count to $NUMTEST due to blocks"
3240
3241         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3242                 echo "failed" > $BASE/fnum
3243 }
3244 run_test 51b "exceed 64k subdirectory nlink limit"
3245
3246 test_51ba() { # LU-993
3247         local BASE=$DIR/$tdir
3248         # unlink all but 100 subdirectories, then check it still works
3249         local LEFT=100
3250         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3251
3252         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3253         local DELETE=$((NUMTEST - LEFT))
3254
3255         # continue on to run this test even if 51b didn't finish,
3256         # just to delete the many subdirectories created.
3257         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3258
3259         # for ldiskfs the nlink count should be 1, but this is OSD specific
3260         # and so this is listed for informational purposes only
3261         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3262         unlinkmany -d $BASE/d $DELETE
3263         RC=$?
3264
3265         if [ $RC -ne 0 ]; then
3266                 if [ "$NUMPREV" == "failed" ]; then
3267                         skip "previous setup failed"
3268                         return 0
3269                 else
3270                         error "unlink of first $DELETE subdirs failed"
3271                         return $RC
3272                 fi
3273         fi
3274
3275         echo "nlink between: $(stat -c %h $BASE)"
3276         # trim the first line of ls output
3277         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3278         [ $FOUND -ne $LEFT ] &&
3279                 error "can't find subdirs: found only $FOUND/$LEFT"
3280
3281         unlinkmany -d $BASE/d $DELETE $LEFT ||
3282                 error "unlink of second $LEFT subdirs failed"
3283         # regardless of whether the backing filesystem tracks nlink accurately
3284         # or not, the nlink count shouldn't be more than "." and ".." here
3285         local AFTER=$(stat -c %h $BASE)
3286         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3287                 echo "nlink after: $AFTER"
3288 }
3289 run_test 51ba "verify nlink for many subdirectory cleanup"
3290
3291 test_51d() {
3292         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3293         mkdir -p $DIR/d51d
3294         createmany -o $DIR/d51d/t- 1000
3295         $GETSTRIPE $DIR/d51d > $TMP/files
3296         for N in `seq 0 $((OSTCOUNT - 1))`; do
3297             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3298             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3299             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3300         done
3301         unlinkmany $DIR/d51d/t- 1000
3302
3303         NLAST=0
3304         for N in `seq 1 $((OSTCOUNT - 1))`; do
3305             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3306                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3307             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3308                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3309
3310             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3311                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3312             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3313                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3314             NLAST=$N
3315         done
3316 }
3317 run_test 51d "check object distribution ===================="
3318
3319 test_52a() {
3320         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3321         mkdir -p $DIR/d52a
3322         touch $DIR/d52a/foo
3323         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3324         echo bar >> $DIR/d52a/foo || error "append bar failed"
3325         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3326         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3327         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3328         echo foo >> $DIR/d52a/foo || error "append foo failed"
3329         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3330         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3331         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3332         cp -r $DIR/d52a /tmp/
3333         rm -fr $DIR/d52a || error "cleanup rm failed"
3334 }
3335 run_test 52a "append-only flag test (should return errors) ====="
3336
3337 test_52b() {
3338         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3339         mkdir -p $DIR/d52b
3340         touch $DIR/d52b/foo
3341         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3342         cat test > $DIR/d52b/foo && error "cat test worked"
3343         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3344         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3345         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3346         echo foo >> $DIR/d52b/foo && error "echo worked"
3347         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3348         [ -f $DIR/d52b/foo ] || error
3349         [ -f $DIR/d52b/foo_ren ] && error
3350         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3351         chattr -i $DIR/d52b/foo || error "chattr failed"
3352
3353         rm -fr $DIR/d52b || error
3354 }
3355 run_test 52b "immutable flag test (should return errors) ======="
3356
3357 test_53() {
3358         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3359         remote_ost_nodsh && skip "remote OST with nodsh" && return
3360
3361         local param
3362         local ostname
3363         local mds_last
3364         local ost_last
3365         local ostnum
3366
3367         # only test MDT0000
3368         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3369         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3370                 param=`echo ${value[0]} | cut -d "=" -f1`
3371                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3372                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3373                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3374                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3375                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3376                 if [ $ost_last != $mds_last ]; then
3377                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3378                 fi
3379         done
3380 }
3381 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3382
3383 test_54a() {
3384         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3385         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3386         $SOCKETSERVER $DIR/socket
3387         $SOCKETCLIENT $DIR/socket || error
3388         $MUNLINK $DIR/socket
3389 }
3390 run_test 54a "unix domain socket test =========================="
3391
3392 test_54b() {
3393         f="$DIR/f54b"
3394         mknod $f c 1 3
3395         chmod 0666 $f
3396         dd if=/dev/zero of=$f bs=`page_size` count=1
3397 }
3398 run_test 54b "char device works in lustre ======================"
3399
3400 find_loop_dev() {
3401         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3402         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3403         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3404
3405         for i in `seq 3 7`; do
3406                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3407                 LOOPDEV=$LOOPBASE$i
3408                 LOOPNUM=$i
3409                 break
3410         done
3411 }
3412
3413 test_54c() {
3414         tfile="$DIR/f54c"
3415         tdir="$DIR/d54c"
3416         loopdev="$DIR/loop54c"
3417
3418         find_loop_dev
3419         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3420         mknod $loopdev b 7 $LOOPNUM
3421         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3422         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3423         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3424         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3425         mkdir -p $tdir
3426         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3427         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3428         df $tdir
3429         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3430         $UMOUNT $tdir
3431         losetup -d $loopdev
3432         rm $loopdev
3433 }
3434 run_test 54c "block device works in lustre ====================="
3435
3436 test_54d() {
3437         f="$DIR/f54d"
3438         string="aaaaaa"
3439         mknod $f p
3440         [ "$string" = `echo $string > $f | cat $f` ] || error
3441 }
3442 run_test 54d "fifo device works in lustre ======================"
3443
3444 test_54e() {
3445         check_kernel_version 46 || return 0
3446         f="$DIR/f54e"
3447         string="aaaaaa"
3448         cp -aL /dev/console $f
3449         echo $string > $f || error
3450 }
3451 run_test 54e "console/tty device works in lustre ======================"
3452
3453 #The test_55 used to be iopen test and it was removed by bz#24037.
3454 #run_test 55 "check iopen_connect_dentry() ======================"
3455
3456 test_56a() {    # was test_56
3457         rm -rf $DIR/d56
3458         $SETSTRIPE -d $DIR
3459         mkdir $DIR/d56
3460         mkdir $DIR/d56/dir
3461         NUMFILES=3
3462         NUMFILESx2=$(($NUMFILES * 2))
3463         for i in `seq 1 $NUMFILES` ; do
3464                 touch $DIR/d56/file$i
3465                 touch $DIR/d56/dir/file$i
3466         done
3467
3468         # test lfs getstripe with --recursive
3469         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3470         [ $FILENUM -eq $NUMFILESx2 ] ||
3471                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3472         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3473         [ $FILENUM -eq $NUMFILES ] ||
3474                 error "$GETSTRIPE $DIR/d56: found $FILENUM, not $NUMFILES"
3475         echo "$GETSTRIPE --recursive passed."
3476
3477         # test lfs getstripe with file instead of dir
3478         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3479         [ $FILENUM  -eq 1 ] || error \
3480                  "$GETSTRIPE $DIR/d56/file1: found $FILENUM, not 1"
3481         echo "$GETSTRIPE file1 passed."
3482
3483         #test lfs getstripe with --verbose
3484         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||
3485                 error "$GETSTRIPE --verbose $DIR/d56: want $NUMFILES lmm_magic"
3486         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] ||
3487             error "$GETSTRIPE $DIR/d56: showed lmm_magic"
3488         echo "$GETSTRIPE --verbose passed."
3489
3490         #test lfs getstripe with --obd
3491         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" ||
3492                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3493
3494         [  "$OSTCOUNT" -lt 2 ] &&
3495                 skip_env "skipping other $GETSTRIPE --obd test" && return
3496
3497         OSTIDX=1
3498         OBDUUID=$(ostuuid_from_index $OSTIDX)
3499         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3500         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3501         [ $FOUND -eq $FILENUM ] ||
3502                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3503         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 |
3504                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3505                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3506                 error "$GETSTRIPE --obd: should not show file on other obd"
3507         echo "$GETSTRIPE --obd passed"
3508 }
3509 run_test 56a "check $GETSTRIPE"
3510
3511 NUMFILES=3
3512 NUMDIRS=3
3513 setup_56() {
3514         local LOCAL_NUMFILES="$1"
3515         local LOCAL_NUMDIRS="$2"
3516         local MKDIR_PARAMS="$3"
3517
3518         if [ ! -d "$TDIR" ] ; then
3519                 mkdir -p $TDIR
3520                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3521                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3522                         touch $TDIR/file$i
3523                 done
3524                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3525                         mkdir $TDIR/dir$i
3526                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3527                                 touch $TDIR/dir$i/file$j
3528                         done
3529                 done
3530         fi
3531 }
3532
3533 setup_56_special() {
3534         LOCAL_NUMFILES=$1
3535         LOCAL_NUMDIRS=$2
3536         setup_56 $1 $2
3537         if [ ! -e "$TDIR/loop1b" ] ; then
3538                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3539                         mknod $TDIR/loop${i}b b 7 $i
3540                         mknod $TDIR/null${i}c c 1 3
3541                         ln -s $TDIR/file1 $TDIR/link${i}l
3542                 done
3543                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3544                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3545                         mknod $TDIR/dir$i/null${i}c c 1 3
3546                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3547                 done
3548         fi
3549 }
3550
3551 test_56g() {
3552         $SETSTRIPE -d $DIR
3553
3554         TDIR=$DIR/${tdir}g
3555         setup_56 $NUMFILES $NUMDIRS
3556
3557         EXPECTED=$(($NUMDIRS + 2))
3558         # test lfs find with -name
3559         for i in $(seq 1 $NUMFILES) ; do
3560                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3561                 [ $NUMS -eq $EXPECTED ] ||
3562                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3563                               "found $NUMS, expected $EXPECTED"
3564         done
3565 }
3566 run_test 56g "check lfs find -name ============================="
3567
3568 test_56h() {
3569         $SETSTRIPE -d $DIR
3570
3571         TDIR=$DIR/${tdir}g
3572         setup_56 $NUMFILES $NUMDIRS
3573
3574         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3575         # test lfs find with ! -name
3576         for i in $(seq 1 $NUMFILES) ; do
3577                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3578                 [ $NUMS -eq $EXPECTED ] ||
3579                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3580                               "found $NUMS, expected $EXPECTED"
3581         done
3582 }
3583 run_test 56h "check lfs find ! -name ============================="
3584
3585 test_56i() {
3586        tdir=${tdir}i
3587        mkdir -p $DIR/$tdir
3588        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3589        CMD="$LFIND -ost $UUID $DIR/$tdir"
3590        OUT=$($CMD)
3591        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3592 }
3593 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3594
3595 test_56j() {
3596         TDIR=$DIR/${tdir}g
3597         setup_56_special $NUMFILES $NUMDIRS
3598
3599         EXPECTED=$((NUMDIRS + 1))
3600         CMD="$LFIND -type d $TDIR"
3601         NUMS=$($CMD | wc -l)
3602         [ $NUMS -eq $EXPECTED ] ||
3603                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3604 }
3605 run_test 56j "check lfs find -type d ============================="
3606
3607 test_56k() {
3608         TDIR=$DIR/${tdir}g
3609         setup_56_special $NUMFILES $NUMDIRS
3610
3611         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3612         CMD="$LFIND -type f $TDIR"
3613         NUMS=$($CMD | wc -l)
3614         [ $NUMS -eq $EXPECTED ] ||
3615                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3616 }
3617 run_test 56k "check lfs find -type f ============================="
3618
3619 test_56l() {
3620         TDIR=$DIR/${tdir}g
3621         setup_56_special $NUMFILES $NUMDIRS
3622
3623         EXPECTED=$((NUMDIRS + NUMFILES))
3624         CMD="$LFIND -type b $TDIR"
3625         NUMS=$($CMD | wc -l)
3626         [ $NUMS -eq $EXPECTED ] ||
3627                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3628 }
3629 run_test 56l "check lfs find -type b ============================="
3630
3631 test_56m() {
3632         TDIR=$DIR/${tdir}g
3633         setup_56_special $NUMFILES $NUMDIRS
3634
3635         EXPECTED=$((NUMDIRS + NUMFILES))
3636         CMD="$LFIND -type c $TDIR"
3637         NUMS=$($CMD | wc -l)
3638         [ $NUMS -eq $EXPECTED ] ||
3639                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3640 }
3641 run_test 56m "check lfs find -type c ============================="
3642
3643 test_56n() {
3644         TDIR=$DIR/${tdir}g
3645         setup_56_special $NUMFILES $NUMDIRS
3646
3647         EXPECTED=$((NUMDIRS + NUMFILES))
3648         CMD="$LFIND -type l $TDIR"
3649         NUMS=$($CMD | wc -l)
3650         [ $NUMS -eq $EXPECTED ] ||
3651                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3652 }
3653 run_test 56n "check lfs find -type l ============================="
3654
3655 test_56o() {
3656         TDIR=$DIR/${tdir}o
3657         setup_56 $NUMFILES $NUMDIRS
3658
3659         utime $TDIR/file1 > /dev/null || error "utime (1)"
3660         utime $TDIR/file2 > /dev/null || error "utime (2)"
3661         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3662         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3663         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3664         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3665
3666         EXPECTED=4
3667         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3668         [ $NUMS -eq $EXPECTED ] || \
3669                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3670
3671         EXPECTED=12
3672         CMD="$LFIND -mtime 0 $TDIR"
3673         NUMS=$($CMD | wc -l)
3674         [ $NUMS -eq $EXPECTED ] ||
3675                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3676 }
3677 run_test 56o "check lfs find -mtime for old files =========================="
3678
3679 test_56p() {
3680         [ $RUNAS_ID -eq $UID ] &&
3681                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3682
3683         TDIR=$DIR/${tdir}p
3684         setup_56 $NUMFILES $NUMDIRS
3685
3686         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3687         EXPECTED=$NUMFILES
3688         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3689         NUMS=$($CMD | wc -l)
3690         [ $NUMS -eq $EXPECTED ] || \
3691                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3692
3693         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3694         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3695         NUMS=$($CMD | wc -l)
3696         [ $NUMS -eq $EXPECTED ] || \
3697                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3698 }
3699 run_test 56p "check lfs find -uid and ! -uid ==============================="
3700
3701 test_56q() {
3702         [ $RUNAS_ID -eq $UID ] &&
3703                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3704
3705         TDIR=$DIR/${tdir}q
3706         setup_56 $NUMFILES $NUMDIRS
3707
3708         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3709
3710         EXPECTED=$NUMFILES
3711         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3712         NUMS=$($CMD | wc -l)
3713         [ $NUMS -eq $EXPECTED ] ||
3714                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3715
3716         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3717         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
3718         NUMS=$($CMD | wc -l)
3719         [ $NUMS -eq $EXPECTED ] ||
3720                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3721 }
3722 run_test 56q "check lfs find -gid and ! -gid ==============================="
3723
3724 test_56r() {
3725         TDIR=$DIR/${tdir}r
3726         setup_56 $NUMFILES $NUMDIRS
3727
3728         EXPECTED=12
3729         CMD="$LFIND -size 0 -type f $TDIR"
3730         NUMS=$($CMD | wc -l)
3731         [ $NUMS -eq $EXPECTED ] ||
3732                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3733         EXPECTED=0
3734         CMD="$LFIND ! -size 0 -type f $TDIR"
3735         NUMS=$($CMD | wc -l)
3736         [ $NUMS -eq $EXPECTED ] ||
3737                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3738         echo "test" > $TDIR/$tfile
3739         echo "test2" > $TDIR/$tfile.2 && sync
3740         EXPECTED=1
3741         CMD="$LFIND -size 5 -type f $TDIR"
3742         NUMS=$($CMD | wc -l)
3743         [ $NUMS -eq $EXPECTED ] ||
3744                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3745         EXPECTED=1
3746         CMD="$LFIND -size +5 -type f $TDIR"
3747         NUMS=$($CMD | wc -l)
3748         [ $NUMS -eq $EXPECTED ] ||
3749                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3750         EXPECTED=2
3751         CMD="$LFIND -size +0 -type f $TDIR"
3752         NUMS=$($CMD | wc -l)
3753         [ $NUMS -eq $EXPECTED ] ||
3754                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3755         EXPECTED=2
3756         CMD="$LFIND ! -size -5 -type f $TDIR"
3757         NUMS=$($CMD | wc -l)
3758         [ $NUMS -eq $EXPECTED ] ||
3759                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3760         EXPECTED=12
3761         CMD="$LFIND -size -5 -type f $TDIR"
3762         NUMS=$($CMD | wc -l)
3763         [ $NUMS -eq $EXPECTED ] ||
3764                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3765 }
3766 run_test 56r "check lfs find -size works =========================="
3767
3768 test_56s() { # LU-611
3769         TDIR=$DIR/${tdir}s
3770         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3771
3772         if [ $OSTCOUNT -gt 1 ]; then
3773                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3774                 ONESTRIPE=4
3775                 EXTRA=4
3776         else
3777                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3778                 EXTRA=0
3779         fi
3780
3781         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3782         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3783         NUMS=$($CMD | wc -l)
3784         [ $NUMS -eq $EXPECTED ] ||
3785                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3786
3787         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
3788         CMD="$LFIND -stripe-count +0 -type f $TDIR"
3789         NUMS=$($CMD | wc -l)
3790         [ $NUMS -eq $EXPECTED ] ||
3791                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3792
3793         EXPECTED=$ONESTRIPE
3794         CMD="$LFIND -stripe-count 1 -type f $TDIR"
3795         NUMS=$($CMD | wc -l)
3796         [ $NUMS -eq $EXPECTED ] ||
3797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3798
3799         CMD="$LFIND -stripe-count -2 -type f $TDIR"
3800         NUMS=$($CMD | wc -l)
3801         [ $NUMS -eq $EXPECTED ] ||
3802                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3803
3804         EXPECTED=0
3805         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
3806         NUMS=$($CMD | wc -l)
3807         [ $NUMS -eq $EXPECTED ] ||
3808                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3809 }
3810 run_test 56s "check lfs find -stripe-count works"
3811
3812 test_56t() { # LU-611
3813         TDIR=$DIR/${tdir}t
3814         setup_56 $NUMFILES $NUMDIRS "-s 512k"
3815
3816         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
3817
3818         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3819         CMD="$LFIND -stripe-size 512k -type f $TDIR"
3820         NUMS=$($CMD | wc -l)
3821         [ $NUMS -eq $EXPECTED ] ||
3822                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3823
3824         CMD="$LFIND -stripe-size +320k -type f $TDIR"
3825         NUMS=$($CMD | wc -l)
3826         [ $NUMS -eq $EXPECTED ] ||
3827                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3828
3829         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
3830         CMD="$LFIND -stripe-size +200k -type f $TDIR"
3831         NUMS=$($CMD | wc -l)
3832         [ $NUMS -eq $EXPECTED ] ||
3833                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3834
3835         CMD="$LFIND -stripe-size -640k -type f $TDIR"
3836         NUMS=$($CMD | wc -l)
3837         [ $NUMS -eq $EXPECTED ] ||
3838                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3839
3840         EXPECTED=4
3841         CMD="$LFIND -stripe-size 256k -type f $TDIR"
3842         NUMS=$($CMD | wc -l)
3843         [ $NUMS -eq $EXPECTED ] ||
3844                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3845
3846         CMD="$LFIND -stripe-size -320k -type f $TDIR"
3847         NUMS=$($CMD | wc -l)
3848         [ $NUMS -eq $EXPECTED ] ||
3849                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3850
3851         EXPECTED=0
3852         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
3853         NUMS=$($CMD | wc -l)
3854         [ $NUMS -eq $EXPECTED ] ||
3855                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3856 }
3857 run_test 56t "check lfs find -stripe-size works"
3858
3859 test_56u() { # LU-611
3860         TDIR=$DIR/${tdir}u
3861         setup_56 $NUMFILES $NUMDIRS "-i 0"
3862
3863         if [ $OSTCOUNT -gt 1 ]; then
3864                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
3865                 ONESTRIPE=4
3866         else
3867                 ONESTRIPE=0
3868         fi
3869
3870         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3871         CMD="$LFIND -stripe-index 0 -type f $TDIR"
3872         NUMS=$($CMD | wc -l)
3873         [ $NUMS -eq $EXPECTED ] ||
3874                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3875
3876         EXPECTED=$ONESTRIPE
3877         CMD="$LFIND -stripe-index 1 -type f $TDIR"
3878         NUMS=$($CMD | wc -l)
3879         [ $NUMS -eq $EXPECTED ] ||
3880                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3881
3882         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
3883         NUMS=$($CMD | wc -l)
3884         [ $NUMS -eq $EXPECTED ] ||
3885                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3886
3887         EXPECTED=0
3888         # This should produce an error and not return any files
3889         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
3890         NUMS=$($CMD 2>/dev/null | wc -l)
3891         [ $NUMS -eq $EXPECTED ] ||
3892                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3893
3894         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
3895         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
3896         NUMS=$($CMD | wc -l)
3897         [ $NUMS -eq $EXPECTED ] ||
3898                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3899 }
3900 run_test 56u "check lfs find -stripe-index works"
3901
3902 test_56v() {
3903     local MDT_IDX=0
3904
3905     TDIR=$DIR/${tdir}v
3906     rm -rf $TDIR
3907     setup_56 $NUMFILES $NUMDIRS
3908
3909     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
3910     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
3911
3912     for file in $($LFIND -mdt $UUID $TDIR); do
3913         file_mdt_idx=$($GETSTRIPE -M $file)
3914         [ $file_mdt_idx -eq $MDT_IDX ] ||
3915             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
3916     done
3917 }
3918 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
3919
3920 # Get and check the actual stripe count of one file.
3921 # Usage: check_stripe_count <file> <expected_stripe_count>
3922 check_stripe_count() {
3923     local file=$1
3924     local expected=$2
3925     local actual
3926
3927     [[ -z "$file" || -z "$expected" ]] &&
3928         error "check_stripe_count: invalid argument!"
3929
3930     local cmd="$GETSTRIPE -c $file"
3931     actual=$($cmd) || error "$cmd failed"
3932     actual=${actual%% *}
3933
3934     if [[ $actual -ne $expected ]]; then
3935         [[ $expected -eq -1 ]] ||
3936             error "$cmd wrong: found $actual, expected $expected"
3937         [[ $actual -eq $OSTCOUNT ]] ||
3938             error "$cmd wrong: found $actual, expected $OSTCOUNT"
3939     fi
3940 }
3941
3942 test_56w() {
3943     TDIR=$DIR/${tdir}w
3944
3945     rm -rf $TDIR || error "remove $TDIR failed"
3946     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3947
3948     local stripe_size
3949     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
3950         error "$GETSTRIPE -S -d $TDIR failed"
3951     stripe_size=${stripe_size%% *}
3952
3953     local file_size=$((stripe_size * OSTCOUNT))
3954     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
3955     local required_space=$((file_num * file_size))
3956     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
3957     [[ $free_space -le $((required_space / 1024)) ]] &&
3958         skip_env "need at least $required_space bytes free space," \
3959                  "have $free_space kbytes" && return
3960
3961     local dd_bs=65536
3962     local dd_count=$((file_size / dd_bs))
3963
3964     # write data into the files
3965     local i
3966     local j
3967     local file
3968     for i in $(seq 1 $NUMFILES); do
3969         file=$TDIR/file$i
3970         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
3971             error "write data into $file failed"
3972     done
3973     for i in $(seq 1 $NUMDIRS); do
3974         for j in $(seq 1 $NUMFILES); do
3975             file=$TDIR/dir$i/file$j
3976             yes | dd bs=$dd_bs count=$dd_count of=$file \
3977                 >/dev/null 2>&1 ||
3978                 error "write data into $file failed"
3979         done
3980     done
3981
3982     local expected=-1
3983     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
3984
3985     # lfs_migrate file
3986     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
3987     echo "$cmd"
3988     eval $cmd || error "$cmd failed"
3989
3990     check_stripe_count $TDIR/file1 $expected
3991
3992     # lfs_migrate dir
3993     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
3994     echo "$cmd"
3995     eval $cmd || error "$cmd failed"
3996
3997     for j in $(seq 1 $NUMFILES); do
3998         check_stripe_count $TDIR/dir1/file$j $expected
3999     done
4000
4001     # lfs_migrate works with lfs find
4002     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4003          $LFS_MIGRATE -y -c $expected"
4004     echo "$cmd"
4005     eval $cmd || error "$cmd failed"
4006
4007     for i in $(seq 2 $NUMFILES); do
4008         check_stripe_count $TDIR/file$i $expected
4009     done
4010     for i in $(seq 2 $NUMDIRS); do
4011         for j in $(seq 1 $NUMFILES); do
4012             check_stripe_count $TDIR/dir$i/file$j $expected
4013         done
4014     done
4015 }
4016 run_test 56w "check lfs_migrate -c stripe_count works"
4017
4018 test_57a() {
4019         # note test will not do anything if MDS is not local
4020         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4021                 skip "Only applicable to ldiskfs-based MDTs"
4022                 return
4023         fi
4024
4025         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4026         local MNTDEV="osd*.*MDT*.mntdev"
4027         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4028         [ -z "$DEV" ] && error "can't access $MNTDEV"
4029         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4030                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4031                         error "can't access $DEV"
4032                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4033                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4034                 rm $TMP/t57a.dump
4035         done
4036 }
4037 run_test 57a "verify MDS filesystem created with large inodes =="
4038
4039 test_57b() {
4040         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4041                 skip "Only applicable to ldiskfs-based MDTs"
4042                 return
4043         fi
4044
4045         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4046         local dir=$DIR/d57b
4047
4048         local FILECOUNT=100
4049         local FILE1=$dir/f1
4050         local FILEN=$dir/f$FILECOUNT
4051
4052         rm -rf $dir || error "removing $dir"
4053         mkdir -p $dir || error "creating $dir"
4054         local num=$(get_mds_dir $dir)
4055         local mymds=mds$num
4056
4057         echo "mcreating $FILECOUNT files"
4058         createmany -m $dir/f 1 $FILECOUNT || \
4059                 error "creating files in $dir"
4060
4061         # verify that files do not have EAs yet
4062         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4063         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4064
4065         sync
4066         sleep 1
4067         df $dir  #make sure we get new statfs data
4068         local MDSFREE=$(do_facet $mymds \
4069                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4070         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4071         echo "opening files to create objects/EAs"
4072         local FILE
4073         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4074                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4075         done
4076
4077         # verify that files have EAs now
4078         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4079         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4080
4081         sleep 1  #make sure we get new statfs data
4082         df $dir
4083         local MDSFREE2=$(do_facet $mymds \
4084                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4085         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4086         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4087                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4088                         error "MDC before $MDCFREE != after $MDCFREE2"
4089                 else
4090                         echo "MDC before $MDCFREE != after $MDCFREE2"
4091                         echo "unable to confirm if MDS has large inodes"
4092                 fi
4093         fi
4094         rm -rf $dir
4095 }
4096 run_test 57b "default LOV EAs are stored inside large inodes ==="
4097
4098 test_58() {
4099     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
4100     wiretest
4101 }
4102 run_test 58 "verify cross-platform wire constants =============="
4103
4104 test_59() {
4105         echo "touch 130 files"
4106         createmany -o $DIR/f59- 130
4107         echo "rm 130 files"
4108         unlinkmany $DIR/f59- 130
4109         sync
4110         # wait for commitment of removal
4111         wait_delete_completed
4112 }
4113 run_test 59 "verify cancellation of llog records async ========="
4114
4115 TEST60_HEAD="test_60 run $RANDOM"
4116 test_60a() {
4117         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4118         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4119         log "$TEST60_HEAD - from kernel mode"
4120         do_facet mgs sh run-llog.sh
4121 }
4122 run_test 60a "llog sanity tests run from kernel module =========="
4123
4124 test_60b() { # bug 6411
4125         dmesg > $DIR/$tfile
4126         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4127                                  /llog.test/ {
4128                                          if (marker)
4129                                                  from_marker++
4130                                          from_begin++
4131                                  }
4132                                  END {
4133                                          if (marker)
4134                                                  print from_marker
4135                                          else
4136                                                  print from_begin
4137                                  }"`
4138         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4139 }
4140 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4141
4142 test_60c() {
4143         echo "create 5000 files"
4144         createmany -o $DIR/f60c- 5000
4145 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4146         lctl set_param fail_loc=0x80000137
4147         unlinkmany $DIR/f60c- 5000
4148         lctl set_param fail_loc=0
4149 }
4150 run_test 60c "unlink file when mds full"
4151
4152 test_60d() {
4153         SAVEPRINTK=$(lctl get_param -n printk)
4154
4155         # verify "lctl mark" is even working"
4156         MESSAGE="test message ID $RANDOM $$"
4157         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4158         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4159
4160         lctl set_param printk=0 || error "set lnet.printk failed"
4161         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4162         MESSAGE="new test message ID $RANDOM $$"
4163         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4164         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4165         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4166
4167         lctl set_param -n printk="$SAVEPRINTK"
4168 }
4169 run_test 60d "test printk console message masking"
4170
4171 test_61() {
4172         f="$DIR/f61"
4173         dd if=/dev/zero of=$f bs=`page_size` count=1
4174         cancel_lru_locks osc
4175         $MULTIOP $f OSMWUc || error
4176         sync
4177 }
4178 run_test 61 "mmap() writes don't make sync hang ================"
4179
4180 # bug 2330 - insufficient obd_match error checking causes LBUG
4181 test_62() {
4182         f="$DIR/f62"
4183         echo foo > $f
4184         cancel_lru_locks osc
4185         lctl set_param fail_loc=0x405
4186         cat $f && error "cat succeeded, expect -EIO"
4187         lctl set_param fail_loc=0
4188 }
4189 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4190 # match every page all of the time.
4191 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4192
4193 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4194 test_63a() {    # was test_63
4195         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4196         lctl set_param -n osc.*.max_dirty_mb 0
4197         for i in `seq 10` ; do
4198                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4199                 sleep 5
4200                 kill $!
4201                 sleep 1
4202         done
4203
4204         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4205         rm -f $DIR/f63 || true
4206 }
4207 run_test 63a "Verify oig_wait interruption does not crash ======="
4208
4209 # bug 2248 - async write errors didn't return to application on sync
4210 # bug 3677 - async write errors left page locked
4211 test_63b() {
4212         debugsave
4213         lctl set_param debug=-1
4214
4215         # ensure we have a grant to do async writes
4216         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4217         rm $DIR/$tfile
4218
4219         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4220         lctl set_param fail_loc=0x80000406
4221         $MULTIOP $DIR/$tfile Owy && \
4222                 error "sync didn't return ENOMEM"
4223         sync; sleep 2; sync     # do a real sync this time to flush page
4224         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4225                 error "locked page left in cache after async error" || true
4226         debugrestore
4227 }
4228 run_test 63b "async write errors should be returned to fsync ==="
4229
4230 test_64a () {
4231         df $DIR
4232         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4233 }
4234 run_test 64a "verify filter grant calculations (in kernel) ====="
4235
4236 test_64b () {
4237         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4238         sh oos.sh $MOUNT
4239 }
4240 run_test 64b "check out-of-space detection on client ==========="
4241
4242 test_64c() {
4243         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4244 }
4245 run_test 64c "verify grant shrink ========================------"
4246
4247 # bug 1414 - set/get directories' stripe info
4248 test_65a() {
4249         mkdir -p $DIR/d65
4250         touch $DIR/d65/f1
4251         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
4252 }
4253 run_test 65a "directory with no stripe info ===================="
4254
4255 test_65b() {
4256         mkdir -p $DIR/d65
4257         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65||error "setstripe"
4258         touch $DIR/d65/f2
4259         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
4260 }
4261 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4262
4263 test_65c() {
4264         if [ $OSTCOUNT -gt 1 ]; then
4265                 mkdir -p $DIR/d65
4266                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4267                         -c $(($OSTCOUNT - 1)) $DIR/d65 || error "setstripe"
4268                 touch $DIR/d65/f3
4269                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
4270         fi
4271 }
4272 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4273
4274 test_65d() {
4275         mkdir -p $DIR/d65
4276         if [ $STRIPECOUNT -le 0 ]; then
4277                 sc=1
4278         elif [ $STRIPECOUNT -gt 2000 ]; then
4279 #LOV_MAX_STRIPE_COUNT is 2000
4280                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4281         else
4282                 sc=$(($STRIPECOUNT - 1))
4283         fi
4284         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/d65 || error "setstripe"
4285         touch $DIR/d65/f4 $DIR/d65/f5
4286         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
4287 }
4288 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4289
4290 test_65e() {
4291         mkdir -p $DIR/d65
4292
4293         $SETSTRIPE $DIR/d65 || error "setstripe"
4294         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
4295         touch $DIR/d65/f6
4296         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
4297 }
4298 run_test 65e "directory setstripe defaults ======================="
4299
4300 test_65f() {
4301         mkdir -p $DIR/d65f
4302         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
4303 }
4304 run_test 65f "dir setstripe permission (should return error) ==="
4305
4306 test_65g() {
4307         mkdir -p $DIR/d65
4308         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4309         $SETSTRIPE -d $DIR/d65 || error "setstripe"
4310         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
4311                 error "delete default stripe failed"
4312 }
4313 run_test 65g "directory setstripe -d ==========================="
4314
4315 test_65h() {
4316         mkdir -p $DIR/d65
4317         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4318         mkdir -p $DIR/d65/dd1
4319         [ $($GETSTRIPE -c $DIR/d65) == $($GETSTRIPE -c $DIR/d65/dd1) ] ||
4320                 error "stripe info inherit failed"
4321 }
4322 run_test 65h "directory stripe info inherit ===================="
4323
4324 test_65i() { # bug6367
4325         $SETSTRIPE -S 65536 -c -1 $MOUNT
4326 }
4327 run_test 65i "set non-default striping on root directory (bug 6367)="
4328
4329 test_65ia() { # bug12836
4330         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4331 }
4332 run_test 65ia "getstripe on -1 default directory striping"
4333
4334 test_65ib() { # bug12836
4335         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4336 }
4337 run_test 65ib "getstripe -v on -1 default directory striping"
4338
4339 test_65ic() { # bug12836
4340         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4341 }
4342 run_test 65ic "new find on -1 default directory striping"
4343
4344 test_65j() { # bug6367
4345         sync; sleep 1
4346         # if we aren't already remounting for each test, do so for this test
4347         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4348                 cleanup || error "failed to unmount"
4349                 setup
4350         fi
4351         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4352 }
4353 run_test 65j "set default striping on root directory (bug 6367)="
4354
4355 test_65k() { # bug11679
4356     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4357     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4358
4359     echo "Check OST status: "
4360     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4361               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4362
4363     for OSC in $MDS_OSCS; do
4364         echo $OSC "is activate"
4365         do_facet $SINGLEMDS lctl --device %$OSC activate
4366     done
4367
4368     do_facet client mkdir -p $DIR/$tdir
4369     for INACTIVE_OSC in $MDS_OSCS; do
4370         echo "Deactivate: " $INACTIVE_OSC
4371         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4372         for STRIPE_OSC in $MDS_OSCS; do
4373             OST=`osc_to_ost $STRIPE_OSC`
4374             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4375                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4376
4377             [ -f $DIR/$tdir/$IDX ] && continue
4378             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4379             do_facet client $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4380             RC=$?
4381             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4382         done
4383         do_facet client rm -f $DIR/$tdir/*
4384         echo $INACTIVE_OSC "is Activate."
4385         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4386     done
4387 }
4388 run_test 65k "validate manual striping works properly with deactivated OSCs"
4389
4390 test_65l() { # bug 12836
4391         mkdir -p $DIR/$tdir/test_dir
4392         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4393         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4394 }
4395 run_test 65l "lfs find on -1 stripe dir ========================"
4396
4397 # bug 2543 - update blocks count on client
4398 test_66() {
4399         COUNT=${COUNT:-8}
4400         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4401         sync; sync_all_data; sync; sync_all_data
4402         cancel_lru_locks osc
4403         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4404         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4405 }
4406 run_test 66 "update inode blocks count on client ==============="
4407
4408 LLOOP=
4409 LLITELOOPLOAD=
4410 cleanup_68() {
4411         trap 0
4412         if [ ! -z "$LLOOP" ]; then
4413                 if swapon -s | grep -q $LLOOP; then
4414                         swapoff $LLOOP || error "swapoff failed"
4415                 fi
4416
4417                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4418                 rm -f $LLOOP
4419                 unset LLOOP
4420         fi
4421         if [ ! -z "$LLITELOOPLOAD" ]; then
4422                 rmmod llite_lloop
4423                 unset LLITELOOPLOAD
4424         fi
4425         rm -f $DIR/f68*
4426 }
4427
4428 meminfo() {
4429         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4430 }
4431
4432 swap_used() {
4433         swapon -s | awk '($1 == "'$1'") { print $4 }'
4434 }
4435
4436 # test case for lloop driver, basic function
4437 test_68a() {
4438         [ "$UID" != 0 ] && skip_env "must run as root" && return
4439         llite_lloop_enabled || \
4440                 { skip_env "llite_lloop module disabled" && return; }
4441
4442         trap cleanup_68 EXIT
4443
4444         if ! module_loaded llite_lloop; then
4445                 if load_module llite/llite_lloop; then
4446                         LLITELOOPLOAD=yes
4447                 else
4448                         skip_env "can't find module llite_lloop"
4449                         return
4450                 fi
4451         fi
4452
4453         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4454         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4455         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4456
4457         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4458         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4459
4460         cleanup_68
4461 }
4462 run_test 68a "lloop driver - basic test ========================"
4463
4464 # excercise swapping to lustre by adding a high priority swapfile entry
4465 # and then consuming memory until it is used.
4466 test_68b() {  # was test_68
4467         [ "$UID" != 0 ] && skip_env "must run as root" && return
4468         lctl get_param -n devices | grep -q obdfilter && \
4469                 skip "local OST" && return
4470
4471         grep -q llite_lloop /proc/modules
4472         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4473
4474         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4475                 skip "can't reliably test swap with TCP" && return
4476
4477         MEMTOTAL=`meminfo MemTotal`
4478         NR_BLOCKS=$((MEMTOTAL>>8))
4479         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4480
4481         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4482         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4483         mkswap $DIR/f68b
4484
4485         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4486
4487         trap cleanup_68 EXIT
4488
4489         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4490
4491         echo "before: `swapon -s | grep $LLOOP`"
4492         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4493         echo "after: `swapon -s | grep $LLOOP`"
4494         SWAPUSED=`swap_used $LLOOP`
4495
4496         cleanup_68
4497
4498         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4499 }
4500 run_test 68b "support swapping to Lustre ========================"
4501
4502 # bug5265, obdfilter oa2dentry return -ENOENT
4503 # #define OBD_FAIL_OST_ENOENT 0x217
4504 test_69() {
4505         remote_ost_nodsh && skip "remote OST with nodsh" && return
4506
4507         f="$DIR/$tfile"
4508         $SETSTRIPE -c 1 -i 0 $f
4509
4510         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4511
4512         do_facet ost1 lctl set_param fail_loc=0x217
4513         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4514         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4515
4516         do_facet ost1 lctl set_param fail_loc=0
4517         $DIRECTIO write $f 0 2 || error "write error"
4518
4519         cancel_lru_locks osc
4520         $DIRECTIO read $f 0 1 || error "read error"
4521
4522         do_facet ost1 lctl set_param fail_loc=0x217
4523         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4524
4525         do_facet ost1 lctl set_param fail_loc=0
4526         rm -f $f
4527 }
4528 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4529
4530 test_71() {
4531     mkdir -p $DIR/$tdir
4532     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4533 }
4534 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4535
4536 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4537         check_kernel_version 43 || return 0
4538         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4539
4540         # Check that testing environment is properly set up. Skip if not
4541         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4542                 skip_env "User $RUNAS_ID does not exist - skipping"
4543                 return 0
4544         }
4545         # We had better clear the $DIR to get enough space for dd
4546         rm -rf $DIR/*
4547         touch $DIR/f72
4548         chmod 777 $DIR/f72
4549         chmod ug+s $DIR/f72
4550         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4551         # See if we are still setuid/sgid
4552         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4553         # Now test that MDS is updated too
4554         cancel_lru_locks mdc
4555         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4556         rm -f $DIR/f72
4557 }
4558 run_test 72a "Test that remove suid works properly (bug5695) ===="
4559
4560 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4561         local perm
4562
4563         [ "$RUNAS_ID" = "$UID" ] && \
4564                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4565         [ "$RUNAS_ID" -eq 0 ] && \
4566                 skip_env "RUNAS_ID = 0 -- skipping" && return
4567
4568         # Check that testing environment is properly set up. Skip if not
4569         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4570                 skip_env "User $RUNAS_ID does not exist - skipping"
4571                 return 0
4572         }
4573         touch $DIR/${tfile}-f{g,u}
4574         mkdir $DIR/${tfile}-d{g,u}
4575         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4576         chmod g+s $DIR/${tfile}-{f,d}g
4577         chmod u+s $DIR/${tfile}-{f,d}u
4578         for perm in 777 2777 4777; do
4579                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4580                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4581                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4582                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4583         done
4584         true
4585 }
4586 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4587
4588 # bug 3462 - multiple simultaneous MDC requests
4589 test_73() {
4590         mkdir $DIR/d73-1
4591         mkdir $DIR/d73-2
4592         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4593         pid1=$!
4594
4595         lctl set_param fail_loc=0x80000129
4596         $MULTIOP $DIR/d73-1/f73-2 Oc &
4597         sleep 1
4598         lctl set_param fail_loc=0
4599
4600         $MULTIOP $DIR/d73-2/f73-3 Oc &
4601         pid3=$!
4602
4603         kill -USR1 $pid1
4604         wait $pid1 || return 1
4605
4606         sleep 25
4607
4608         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4609         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4610         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4611
4612         rm -rf $DIR/d73-*
4613 }
4614 run_test 73 "multiple MDC requests (should not deadlock)"
4615
4616 test_74a() { # bug 6149, 6184
4617         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4618         #
4619         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4620         # will spin in a tight reconnection loop
4621         touch $DIR/f74a
4622         lctl set_param fail_loc=0x8000030e
4623         # get any lock that won't be difficult - lookup works.
4624         ls $DIR/f74a
4625         lctl set_param fail_loc=0
4626         true
4627         rm -f $DIR/f74a
4628 }
4629 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4630
4631 test_74b() { # bug 13310
4632         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4633         #
4634         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4635         # will spin in a tight reconnection loop
4636         lctl set_param fail_loc=0x8000030e
4637         # get a "difficult" lock
4638         touch $DIR/f74b
4639         lctl set_param fail_loc=0
4640         true
4641         rm -f $DIR/f74b
4642 }
4643 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4644
4645 test_74c() {
4646 #define OBD_FAIL_LDLM_NEW_LOCK
4647         lctl set_param fail_loc=0x80000319
4648         touch $DIR/$tfile && error "Touch successful"
4649         true
4650 }
4651 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4652
4653 num_inodes() {
4654         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4655 }
4656
4657 get_inode_slab_tunables() {
4658         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4659 }
4660
4661 set_inode_slab_tunables() {
4662         echo "lustre_inode_cache $1" > /proc/slabinfo
4663 }
4664
4665 test_76() { # Now for bug 20433, added originally in bug 1443
4666         local SLAB_SETTINGS=`get_inode_slab_tunables`
4667         local CPUS=`getconf _NPROCESSORS_ONLN`
4668         # we cannot set limit below 1 which means 1 inode in each
4669         # per-cpu cache is still allowed
4670         set_inode_slab_tunables "1 1 0"
4671         cancel_lru_locks osc
4672         BEFORE_INODES=`num_inodes`
4673         echo "before inodes: $BEFORE_INODES"
4674         local COUNT=1000
4675         [ "$SLOW" = "no" ] && COUNT=100
4676         for i in `seq $COUNT`; do
4677                 touch $DIR/$tfile
4678                 rm -f $DIR/$tfile
4679         done
4680         cancel_lru_locks osc
4681         AFTER_INODES=`num_inodes`
4682         echo "after inodes: $AFTER_INODES"
4683         local wait=0
4684         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4685                 sleep 2
4686                 AFTER_INODES=`num_inodes`
4687                 wait=$((wait+2))
4688                 echo "wait $wait seconds inodes: $AFTER_INODES"
4689                 if [ $wait -gt 30 ]; then
4690                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4691                 fi
4692         done
4693         set_inode_slab_tunables "$SLAB_SETTINGS"
4694 }
4695 run_test 76 "confirm clients recycle inodes properly ===="
4696
4697
4698 export ORIG_CSUM=""
4699 set_checksums()
4700 {
4701         # Note: in sptlrpc modes which enable its own bulk checksum, the
4702         # original crc32_le bulk checksum will be automatically disabled,
4703         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4704         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4705         # In this case set_checksums() will not be no-op, because sptlrpc
4706         # bulk checksum will be enabled all through the test.
4707
4708         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4709         lctl set_param -n osc.*.checksums $1
4710         return 0
4711 }
4712
4713 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4714                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4715 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4716 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4717 set_checksum_type()
4718 {
4719         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4720         log "set checksum type to $1"
4721         return 0
4722 }
4723 F77_TMP=$TMP/f77-temp
4724 F77SZ=8
4725 setup_f77() {
4726         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4727                 error "error writing to $F77_TMP"
4728 }
4729
4730 test_77a() { # bug 10889
4731         $GSS && skip "could not run with gss" && return
4732         [ ! -f $F77_TMP ] && setup_f77
4733         set_checksums 1
4734         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4735         set_checksums 0
4736         rm -f $DIR/$tfile
4737 }
4738 run_test 77a "normal checksum read/write operation ============="
4739
4740 test_77b() { # bug 10889
4741         $GSS && skip "could not run with gss" && return
4742         [ ! -f $F77_TMP ] && setup_f77
4743         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4744         lctl set_param fail_loc=0x80000409
4745         set_checksums 1
4746         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4747                 error "dd error: $?"
4748         lctl set_param fail_loc=0
4749         set_checksums 0
4750 }
4751 run_test 77b "checksum error on client write ===================="
4752
4753 test_77c() { # bug 10889
4754         $GSS && skip "could not run with gss" && return
4755         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4756         set_checksums 1
4757         for algo in $CKSUM_TYPES; do
4758                 cancel_lru_locks osc
4759                 set_checksum_type $algo
4760                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4761                 lctl set_param fail_loc=0x80000408
4762                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4763                 lctl set_param fail_loc=0
4764         done
4765         set_checksums 0
4766         set_checksum_type $ORIG_CSUM_TYPE
4767         rm -f $DIR/f77b
4768 }
4769 run_test 77c "checksum error on client read ==================="
4770
4771 test_77d() { # bug 10889
4772         $GSS && skip "could not run with gss" && return
4773         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4774         lctl set_param fail_loc=0x80000409
4775         set_checksums 1
4776         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4777                 error "direct write: rc=$?"
4778         lctl set_param fail_loc=0
4779         set_checksums 0
4780 }
4781 run_test 77d "checksum error on OST direct write ==============="
4782
4783 test_77e() { # bug 10889
4784         $GSS && skip "could not run with gss" && return
4785         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4786         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4787         lctl set_param fail_loc=0x80000408
4788         set_checksums 1
4789         cancel_lru_locks osc
4790         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4791                 error "direct read: rc=$?"
4792         lctl set_param fail_loc=0
4793         set_checksums 0
4794 }
4795 run_test 77e "checksum error on OST direct read ================"
4796
4797 test_77f() { # bug 10889
4798         $GSS && skip "could not run with gss" && return
4799         set_checksums 1
4800         for algo in $CKSUM_TYPES; do
4801                 cancel_lru_locks osc
4802                 set_checksum_type $algo
4803                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4804                 lctl set_param fail_loc=0x409
4805                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4806                         error "direct write succeeded"
4807                 lctl set_param fail_loc=0
4808         done
4809         set_checksum_type $ORIG_CSUM_TYPE
4810         set_checksums 0
4811 }
4812 run_test 77f "repeat checksum error on write (expect error) ===="
4813
4814 test_77g() { # bug 10889
4815         $GSS && skip "could not run with gss" && return
4816         remote_ost_nodsh && skip "remote OST with nodsh" && return
4817
4818         [ ! -f $F77_TMP ] && setup_f77
4819
4820         $SETSTRIPE -c 1 -i 0 $DIR/f77g
4821         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4822         do_facet ost1 lctl set_param fail_loc=0x8000021a
4823         set_checksums 1
4824         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4825                 error "write error: rc=$?"
4826         do_facet ost1 lctl set_param fail_loc=0
4827         set_checksums 0
4828 }
4829 run_test 77g "checksum error on OST write ======================"
4830
4831 test_77h() { # bug 10889
4832         $GSS && skip "could not run with gss" && return
4833         remote_ost_nodsh && skip "remote OST with nodsh" && return
4834
4835         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4836         cancel_lru_locks osc
4837         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4838         do_facet ost1 lctl set_param fail_loc=0x8000021b
4839         set_checksums 1
4840         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4841         do_facet ost1 lctl set_param fail_loc=0
4842         set_checksums 0
4843 }
4844 run_test 77h "checksum error on OST read ======================="
4845
4846 test_77i() { # bug 13805
4847         $GSS && skip "could not run with gss" && return
4848         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
4849         lctl set_param fail_loc=0x40b
4850         remount_client $MOUNT
4851         lctl set_param fail_loc=0
4852         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4853                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4854                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4855                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4856         done
4857         remount_client $MOUNT
4858 }
4859 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
4860
4861 test_77j() { # bug 13805
4862         $GSS && skip "could not run with gss" && return
4863         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
4864         lctl set_param fail_loc=0x40c
4865         remount_client $MOUNT
4866         lctl set_param fail_loc=0
4867         sleep 2 # wait async osc connect to finish
4868         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4869                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4870                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4871                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4872         done
4873         remount_client $MOUNT
4874 }
4875 run_test 77j "client only supporting ADLER32 ===================="
4876
4877 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
4878 rm -f $F77_TMP
4879 unset F77_TMP
4880
4881 test_78() { # bug 10901
4882         remote_ost || { skip_env "local OST" && return; }
4883
4884         NSEQ=5
4885         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
4886         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
4887         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
4888         echo "MemTotal: $MEMTOTAL"
4889 # reserve 256MB of memory for the kernel and other running processes,
4890 # and then take 1/2 of the remaining memory for the read/write buffers.
4891     if [ $MEMTOTAL -gt 512 ] ;then
4892         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
4893     else
4894         # for those poor memory-starved high-end clusters...
4895         MEMTOTAL=$((MEMTOTAL / 2))
4896     fi
4897         echo "Mem to use for directio: $MEMTOTAL"
4898         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
4899         [ $F78SIZE -gt 512 ] && F78SIZE=512
4900         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
4901         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
4902         echo "Smallest OST: $SMALLESTOST"
4903         [ $SMALLESTOST -lt 10240 ] && \
4904                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
4905
4906         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
4907                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
4908
4909         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
4910         echo "File size: $F78SIZE"
4911         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
4912         for i in `seq 1 $NSEQ`
4913         do
4914                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
4915                 echo directIO rdwr round $i of $NSEQ
4916                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
4917         done
4918
4919         rm -f $DIR/$tfile
4920 }
4921 run_test 78 "handle large O_DIRECT writes correctly ============"
4922
4923 test_79() { # bug 12743
4924         wait_delete_completed
4925
4926         BKTOTAL=$(calc_osc_kbytes kbytestotal)
4927         BKFREE=$(calc_osc_kbytes kbytesfree)
4928         BKAVAIL=$(calc_osc_kbytes kbytesavail)
4929
4930         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
4931         DFTOTAL=`echo $STRING | cut -d, -f1`
4932         DFUSED=`echo $STRING  | cut -d, -f2`
4933         DFAVAIL=`echo $STRING | cut -d, -f3`
4934         DFFREE=$(($DFTOTAL - $DFUSED))
4935
4936         ALLOWANCE=$((64 * $OSTCOUNT))
4937
4938         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
4939            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
4940                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
4941         fi
4942         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
4943            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
4944                 error "df free($DFFREE) mismatch OST free($BKFREE)"
4945         fi
4946         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
4947            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
4948                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
4949         fi
4950 }
4951 run_test 79 "df report consistency check ======================="
4952
4953 test_80() { # bug 10718
4954         # relax strong synchronous semantics for slow backends like ZFS
4955         local soc="obdfilter.*.sync_on_lock_cancel"
4956         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
4957         local hosts=
4958         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
4959                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
4960                           facet_active_host $host; done | sort -u)
4961                 do_nodes $hosts lctl set_param $soc=never
4962         fi
4963
4964         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
4965         sync; sleep 1; sync
4966         local BEFORE=`date +%s`
4967         cancel_lru_locks osc
4968         local AFTER=`date +%s`
4969         local DIFF=$((AFTER-BEFORE))
4970         if [ $DIFF -gt 1 ] ; then
4971                 error "elapsed for 1M@1T = $DIFF"
4972         fi
4973
4974         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
4975
4976         rm -f $DIR/$tfile
4977 }
4978 run_test 80 "Page eviction is equally fast at high offsets too  ===="
4979
4980 test_81a() { # LU-456
4981         remote_ost_nodsh && skip "remote OST with nodsh" && return
4982         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4983         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
4984         do_facet ost1 lctl set_param fail_loc=0x80000228
4985
4986         # write should trigger a retry and success
4987         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4988         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4989         RC=$?
4990         if [ $RC -ne 0 ] ; then
4991                 error "write should success, but failed for $RC"
4992         fi
4993 }
4994 run_test 81a "OST should retry write when get -ENOSPC ==============="
4995
4996 test_81b() { # LU-456
4997         remote_ost_nodsh && skip "remote OST with nodsh" && return
4998         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4999         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5000         do_facet ost1 lctl set_param fail_loc=0x228
5001
5002         # write should retry several times and return -ENOSPC finally
5003         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5004         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5005         RC=$?
5006         ENOSPC=28
5007         if [ $RC -ne $ENOSPC ] ; then
5008                 error "dd should fail for -ENOSPC, but succeed."
5009         fi
5010 }
5011 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5012
5013 test_82() { # LU-1031
5014         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5015         local gid1=14091995
5016         local gid2=16022000
5017
5018         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5019         local MULTIPID1=$!
5020         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5021         local MULTIPID2=$!
5022         kill -USR1 $MULTIPID2
5023         sleep 2
5024         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5025                 error "First grouplock does not block second one"
5026         else
5027                 echo "Second grouplock blocks first one"
5028         fi
5029         kill -USR1 $MULTIPID1
5030         wait $MULTIPID1
5031         wait $MULTIPID2
5032 }
5033 run_test 82 "Basic grouplock test ==============================="
5034
5035 test_99a() {
5036         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5037             return
5038         mkdir -p $DIR/d99cvsroot
5039         chown $RUNAS_ID $DIR/d99cvsroot
5040         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5041         cd $TMP
5042
5043         $RUNAS cvs -d $DIR/d99cvsroot init || error
5044         cd $oldPWD
5045 }
5046 run_test 99a "cvs init ========================================="
5047
5048 test_99b() {
5049         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5050         [ ! -d $DIR/d99cvsroot ] && test_99a
5051         cd /etc/init.d
5052         # some versions of cvs import exit(1) when asked to import links or
5053         # files they can't read.  ignore those files.
5054         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5055                         ! -perm +4 -printf '-I %f\n')
5056         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5057                 d99reposname vtag rtag
5058 }
5059 run_test 99b "cvs import ======================================="
5060
5061 test_99c() {
5062         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5063         [ ! -d $DIR/d99cvsroot ] && test_99b
5064         cd $DIR
5065         mkdir -p $DIR/d99reposname
5066         chown $RUNAS_ID $DIR/d99reposname
5067         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5068 }
5069 run_test 99c "cvs checkout ====================================="
5070
5071 test_99d() {
5072         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5073         [ ! -d $DIR/d99cvsroot ] && test_99c
5074         cd $DIR/d99reposname
5075         $RUNAS touch foo99
5076         $RUNAS cvs add -m 'addmsg' foo99
5077 }
5078 run_test 99d "cvs add =========================================="
5079
5080 test_99e() {
5081         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5082         [ ! -d $DIR/d99cvsroot ] && test_99c
5083         cd $DIR/d99reposname
5084         $RUNAS cvs update
5085 }
5086 run_test 99e "cvs update ======================================="
5087
5088 test_99f() {
5089         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5090         [ ! -d $DIR/d99cvsroot ] && test_99d
5091         cd $DIR/d99reposname
5092         $RUNAS cvs commit -m 'nomsg' foo99
5093     rm -fr $DIR/d99cvsroot
5094 }
5095 run_test 99f "cvs commit ======================================="
5096
5097 test_100() {
5098         [ "$NETTYPE" = tcp ] || \
5099                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5100                         return ; }
5101
5102         remote_ost_nodsh && skip "remote OST with nodsh" && return
5103         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5104         remote_servers || \
5105                 { skip "useless for local single node setup" && return; }
5106
5107         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5108                 [ "$PROT" != "tcp" ] && continue
5109                 RPORT=$(echo $REMOTE | cut -d: -f2)
5110                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5111
5112                 rc=0
5113                 LPORT=`echo $LOCAL | cut -d: -f2`
5114                 if [ $LPORT -ge 1024 ]; then
5115                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5116                         netstat -tna
5117                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5118                 fi
5119         done
5120         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5121 }
5122 run_test 100 "check local port using privileged port ==========="
5123
5124 function get_named_value()
5125 {
5126     local tag
5127
5128     tag=$1
5129     while read ;do
5130         line=$REPLY
5131         case $line in
5132         $tag*)
5133             echo $line | sed "s/^$tag[ ]*//"
5134             break
5135             ;;
5136         esac
5137     done
5138 }
5139
5140 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5141 cleanup_101a() {
5142         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5143         trap 0
5144 }
5145
5146 test_101a() {
5147         local s
5148         local discard
5149         local nreads=10000
5150         [ "$CPU" = "UML" ] && nreads=1000
5151         local cache_limit=32
5152
5153         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5154         trap cleanup_101a EXIT
5155         $LCTL set_param -n llite.*.read_ahead_stats 0
5156         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5157
5158         #
5159         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5160         #
5161         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5162         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5163
5164         discard=0
5165         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5166                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5167                         discard=$(($discard + $s))
5168         done
5169         cleanup_101a
5170
5171         if [ $(($discard * 10)) -gt $nreads ] ;then
5172                 $LCTL get_param osc.*-osc*.rpc_stats
5173                 $LCTL get_param llite.*.read_ahead_stats
5174                 error "too many ($discard) discarded pages"
5175         fi
5176         rm -f $DIR/$tfile || true
5177 }
5178 run_test 101a "check read-ahead for random reads ================"
5179
5180 setup_test101bc() {
5181         mkdir -p $DIR/$tdir
5182         STRIPE_SIZE=1048576
5183         STRIPE_COUNT=$OSTCOUNT
5184         STRIPE_OFFSET=0
5185
5186         local list=$(comma_list $(osts_nodes))
5187         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
5188         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
5189
5190         trap cleanup_test101bc EXIT
5191         # prepare the read-ahead file
5192         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5193
5194         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5195 }
5196
5197 cleanup_test101bc() {
5198         trap 0
5199         rm -rf $DIR/$tdir
5200         rm -f $DIR/$tfile
5201
5202         local list=$(comma_list $(osts_nodes))
5203         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
5204         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
5205 }
5206
5207 calc_total() {
5208         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5209 }
5210
5211 ra_check_101() {
5212         local READ_SIZE=$1
5213         local STRIPE_SIZE=1048576
5214         local RA_INC=1048576
5215         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5216         local FILE_LENGTH=$((64*100))
5217         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5218                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5219         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5220                         get_named_value 'read but discarded' | \
5221                         cut -d" " -f1 | calc_total`
5222         if [ $DISCARD -gt $discard_limit ]; then
5223                 $LCTL get_param llite.*.read_ahead_stats
5224                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5225         else
5226                 echo "Read-ahead success for size ${READ_SIZE}"
5227         fi
5228 }
5229
5230 test_101b() {
5231         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5232         local STRIPE_SIZE=1048576
5233         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5234         local FILE_LENGTH=$((STRIPE_SIZE*100))
5235         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5236         # prepare the read-ahead file
5237         setup_test101bc
5238         cancel_lru_locks osc
5239         for BIDX in 2 4 8 16 32 64 128 256
5240         do
5241                 local BSIZE=$((BIDX*4096))
5242                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5243                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5244                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5245                 $LCTL set_param -n llite.*.read_ahead_stats 0
5246                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5247                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5248                 cancel_lru_locks osc
5249                 ra_check_101 $BSIZE
5250         done
5251         cleanup_test101bc
5252         true
5253 }
5254 run_test 101b "check stride-io mode read-ahead ================="
5255
5256 test_101c() {
5257     local STRIPE_SIZE=1048576
5258     local FILE_LENGTH=$((STRIPE_SIZE*100))
5259     local nreads=10000
5260     local osc
5261
5262     setup_test101bc
5263
5264     cancel_lru_locks osc
5265     $LCTL set_param osc.*.rpc_stats 0
5266     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5267     for osc in $($LCTL get_param -N osc.*); do
5268         if [ "$osc" == "osc.num_refs" ]; then
5269             continue
5270         fi
5271
5272         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5273         if [ $lines -le 20 ]; then
5274             continue
5275         fi
5276
5277         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5278                                      awk '$1 == "1:" { print $2; exit; }')
5279         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5280                                      awk '$1 == "2:" { print $2; exit; }')
5281         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5282                                      awk '$1 == "4:" { print $2; exit; }')
5283         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5284                                      awk '$1 == "8:" { print $2; exit; }')
5285
5286         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5287         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5288         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5289         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5290         echo "${osc} rpc check passed!"
5291     done
5292     cleanup_test101bc
5293     true
5294 }
5295 run_test 101c "check stripe_size aligned read-ahead ================="
5296
5297 set_read_ahead() {
5298    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5299    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5300 }
5301
5302 test_101d() {
5303     local file=$DIR/$tfile
5304     local size=${FILESIZE_101c:-500}
5305     local ra_MB=${READAHEAD_MB:-40}
5306
5307     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5308     [ $space -gt $((size / 1024)) ] ||
5309         { skip "Need free space ${size}M, have $space" && return; }
5310
5311     echo Creating ${size}M test file $file
5312     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5313     echo Cancel LRU locks on lustre client to flush the client cache
5314     cancel_lru_locks osc
5315
5316     echo Disable read-ahead
5317     local old_READAHEAD=$(set_read_ahead 0)
5318
5319     echo Reading the test file $file with read-ahead disabled
5320     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5321
5322     echo Cancel LRU locks on lustre client to flush the client cache
5323     cancel_lru_locks osc
5324     echo Enable read-ahead with ${ra_MB}MB
5325     set_read_ahead $ra_MB
5326
5327     echo Reading the test file $file with read-ahead enabled
5328     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5329
5330     echo read-ahead disabled time read $time_ra_OFF
5331     echo read-ahead enabled  time read $time_ra_ON
5332
5333         set_read_ahead $old_READAHEAD
5334         rm -f $file
5335         wait_delete_completed
5336
5337     [ $time_ra_ON -lt $time_ra_OFF ] ||
5338         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5339                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5340 }
5341 run_test 101d "file read with and without read-ahead enabled  ================="
5342
5343 test_101e() {
5344     local file=$DIR/$tfile
5345     local size=500  #KB
5346     local count=100
5347     local blksize=1024
5348
5349     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5350     local need_space=$((count * size))
5351     [ $space -gt $need_space ] ||
5352         { skip_env "Need free space $need_space, have $space" && return; }
5353
5354     echo Creating $count ${size}K test files
5355     for ((i = 0; i < $count; i++)); do
5356         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5357     done
5358
5359     echo Cancel LRU locks on lustre client to flush the client cache
5360     cancel_lru_locks osc
5361
5362     echo Reset readahead stats
5363     $LCTL set_param -n llite.*.read_ahead_stats 0
5364
5365     for ((i = 0; i < $count; i++)); do
5366         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5367     done
5368
5369     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5370           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5371
5372     for ((i = 0; i < $count; i++)); do
5373         rm -rf ${file}_${i} 2>/dev/null
5374     done
5375
5376     #10000 means 20% reads are missing in readahead
5377     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5378 }
5379 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5380
5381 cleanup_test101f() {
5382     trap 0
5383     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5384     rm -rf $DIR/$tfile 2>/dev/null
5385 }
5386
5387 test_101f() {
5388     local file=$DIR/$tfile
5389     local nreads=1000
5390
5391     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5392     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5393     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5394     trap cleanup_test101f EXIT
5395
5396     echo Cancel LRU locks on lustre client to flush the client cache
5397     cancel_lru_locks osc
5398
5399     echo Reset readahead stats
5400     $LCTL set_param -n llite.*.read_ahead_stats 0
5401     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5402     # readahead should read in 2M file on second read, so only miss
5403     # 2 pages.
5404     echo Random 4K reads on 2M file for 1000 times
5405     $READS -f $file -s 2097152 -b 4096 -n $nreads
5406
5407     echo checking missing pages
5408     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5409           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5410
5411     [ $miss -lt 3 ] || error "misses too much pages!"
5412     cleanup_test101f
5413 }
5414 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5415
5416 setup_test102() {
5417         mkdir -p $DIR/$tdir
5418         chown $RUNAS_ID $DIR/$tdir
5419         STRIPE_SIZE=65536
5420         STRIPE_OFFSET=1
5421         STRIPE_COUNT=$OSTCOUNT
5422         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5423
5424         trap cleanup_test102 EXIT
5425         cd $DIR
5426         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5427         cd $DIR/$tdir
5428         for num in 1 2 3 4; do
5429                 for count in $(seq 1 $STRIPE_COUNT); do
5430                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5431                                 local size=`expr $STRIPE_SIZE \* $num`
5432                                 local file=file"$num-$idx-$count"
5433                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5434                         done
5435                 done
5436         done
5437
5438         cd $DIR
5439         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5440 }
5441
5442 cleanup_test102() {
5443         trap 0
5444         rm -f $TMP/f102.tar
5445         rm -rf $DIR/d0.sanity/d102
5446 }
5447
5448 test_102a() {
5449         local testfile=$DIR/xattr_testfile
5450
5451         touch $testfile
5452
5453         [ "$UID" != 0 ] && skip_env "must run as root" && return
5454         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5455                 skip_env "must have user_xattr" && return
5456
5457         [ -z "$(which setfattr 2>/dev/null)" ] &&
5458                 skip_env "could not find setfattr" && return
5459
5460         echo "set/get xattr..."
5461         setfattr -n trusted.name1 -v value1 $testfile || error
5462         getfattr -n trusted.name1 $testfile 2> /dev/null |
5463           grep "trusted.name1=.value1" ||
5464                 error "$testfile missing trusted.name1=value1"
5465
5466         setfattr -n user.author1 -v author1 $testfile || error
5467         getfattr -n user.author1 $testfile 2> /dev/null |
5468           grep "user.author1=.author1" ||
5469                 error "$testfile missing trusted.author1=author1"
5470
5471         echo "listxattr..."
5472         setfattr -n trusted.name2 -v value2 $testfile ||
5473                 error "$testfile unable to set trusted.name2"
5474         setfattr -n trusted.name3 -v value3 $testfile ||
5475                 error "$testfile unable to set trusted.name3"
5476         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5477             grep "trusted.name" | wc -l) -eq 3 ] ||
5478                 error "$testfile missing 3 trusted.name xattrs"
5479
5480         setfattr -n user.author2 -v author2 $testfile ||
5481                 error "$testfile unable to set user.author2"
5482         setfattr -n user.author3 -v author3 $testfile ||
5483                 error "$testfile unable to set user.author3"
5484         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5485             grep "user.author" | wc -l) -eq 3 ] ||
5486                 error "$testfile missing 3 user.author xattrs"
5487
5488         echo "remove xattr..."
5489         setfattr -x trusted.name1 $testfile ||
5490                 error "$testfile error deleting trusted.name1"
5491         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5492                 error "$testfile did not delete trusted.name1 xattr"
5493
5494         setfattr -x user.author1 $testfile ||
5495                 error "$testfile error deleting user.author1"
5496         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5497                 error "$testfile did not delete trusted.name1 xattr"
5498
5499         # b10667: setting lustre special xattr be silently discarded
5500         echo "set lustre special xattr ..."
5501         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5502                 error "$testfile allowed setting trusted.lov"
5503 }
5504 run_test 102a "user xattr test =================================="
5505
5506 test_102b() {
5507         # b10930: get/set/list trusted.lov xattr
5508         echo "get/set/list trusted.lov xattr ..."
5509         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5510         local testfile=$DIR/$tfile
5511         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5512                 error "setstripe failed"
5513         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5514                 error "getstripe failed"
5515         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5516         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5517
5518         local testfile2=${testfile}2
5519         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5520                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5521
5522         $MCREATE $testfile2
5523         setfattr -n trusted.lov -v $value $testfile2
5524         local stripe_size=$($GETSTRIPE -S $testfile2)
5525         local stripe_count=$($GETSTRIPE -c $testfile2)
5526         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5527         [ $stripe_count -eq $STRIPECOUNT ] ||
5528                 error "stripe count $stripe_count != $STRIPECOUNT"
5529         rm -f $DIR/$tfile
5530 }
5531 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5532
5533 test_102c() {
5534         # b10930: get/set/list lustre.lov xattr
5535         echo "get/set/list lustre.lov xattr ..."
5536         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5537         mkdir -p $DIR/$tdir
5538         chown $RUNAS_ID $DIR/$tdir
5539         local testfile=$DIR/$tdir/$tfile
5540         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5541                 error "setstripe failed"
5542         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5543                 error "getstripe failed"
5544         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5545         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5546
5547         local testfile2=${testfile}2
5548         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5549                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5550
5551         $RUNAS $MCREATE $testfile2
5552         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5553         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5554         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5555         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5556         [ $stripe_count -eq $STRIPECOUNT ] ||
5557                 error "stripe count $stripe_count != $STRIPECOUNT"
5558 }
5559 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5560
5561 compare_stripe_info1() {
5562         local stripe_index_all_zero=true
5563
5564         for num in 1 2 3 4; do
5565                 for count in $(seq 1 $STRIPE_COUNT); do
5566                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5567                                 local size=$((STRIPE_SIZE * num))
5568                                 local file=file"$num-$offset-$count"
5569                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5570                                 [ $stripe_size -ne $size ] &&
5571                                     error "$file: size $stripe_size != $size"
5572                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5573                                 # allow fewer stripes to be created, ORI-601
5574                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5575                                     error "$file: count $stripe_count != $count"
5576                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5577                                 [ $stripe_index -ne 0 ] &&
5578                                         stripe_index_all_zero=false
5579                         done
5580                 done
5581         done
5582         $stripe_index_all_zero &&
5583                 error "all files are being extracted starting from OST index 0"
5584         return 0
5585 }
5586
5587 find_lustre_tar() {
5588         [ -n "$(which tar 2>/dev/null)" ] &&
5589                 strings $(which tar) | grep -q "lustre" && echo tar
5590 }
5591
5592 test_102d() {
5593         # b10930: tar test for trusted.lov xattr
5594         TAR=$(find_lustre_tar)
5595         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5596         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5597         setup_test102
5598         mkdir -p $DIR/d102d
5599         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5600         cd $DIR/d102d/$tdir
5601         compare_stripe_info1
5602 }
5603 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5604
5605 test_102f() {
5606         # b10930: tar test for trusted.lov xattr
5607         TAR=$(find_lustre_tar)
5608         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5609         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5610         setup_test102
5611         mkdir -p $DIR/d102f
5612         cd $DIR
5613         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5614         cd $DIR/d102f/$tdir
5615         compare_stripe_info1
5616 }
5617 run_test 102f "tar copy files, not keep osts ==========="
5618
5619 grow_xattr() {
5620         local xsize=${1:-1024}  # in bytes
5621         local file=$DIR/$tfile
5622
5623         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5624                 skip "must have user_xattr" && return 0
5625         [ -z "$(which setfattr 2>/dev/null)" ] &&
5626                 skip_env "could not find setfattr" && return 0
5627         [ -z "$(which getfattr 2>/dev/null)" ] &&
5628                 skip_env "could not find getfattr" && return 0
5629
5630         touch $file
5631
5632         local value="$(generate_string $xsize)"
5633
5634         local xbig=trusted.big
5635         log "save $xbig on $file"
5636         setfattr -n $xbig -v $value $file ||
5637                 error "saving $xbig on $file failed"
5638
5639         local orig=$(get_xattr_value $xbig $file)
5640         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5641
5642         local xsml=trusted.sml
5643         log "save $xsml on $file"
5644         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5645
5646         local new=$(get_xattr_value $xbig $file)
5647         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5648
5649         log "grow $xsml on $file"
5650         setfattr -n $xsml -v "$value" $file ||
5651                 error "growing $xsml on $file failed"
5652
5653         new=$(get_xattr_value $xbig $file)
5654         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5655         log "$xbig still valid after growing $xsml"
5656
5657         rm -f $file
5658 }
5659
5660 test_102h() { # bug 15777
5661         grow_xattr 1024
5662 }
5663 run_test 102h "grow xattr from inside inode to external block"
5664
5665 test_102ha() {
5666         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5667         grow_xattr $(max_xattr_size)
5668 }
5669 run_test 102ha "grow xattr from inside inode to external inode"
5670
5671 test_102i() { # bug 17038
5672         touch $DIR/$tfile
5673         ln -s $DIR/$tfile $DIR/${tfile}link
5674         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5675         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"
5676         rm -f $DIR/$tfile $DIR/${tfile}link
5677 }
5678 run_test 102i "lgetxattr test on symbolic link ============"
5679
5680 test_102j() {
5681         TAR=$(find_lustre_tar)
5682         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5683         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5684         setup_test102 "$RUNAS"
5685         mkdir -p $DIR/d102j
5686         chown $RUNAS_ID $DIR/d102j
5687         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5688         cd $DIR/d102j/$tdir
5689         compare_stripe_info1 "$RUNAS"
5690 }
5691 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5692
5693 test_102k() {
5694         touch $DIR/$tfile
5695         # b22187 just check that does not crash for regular file.
5696         setfattr -n trusted.lov $DIR/$tfile
5697         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5698         local test_kdir=$DIR/d102k
5699         mkdir $test_kdir
5700         local default_size=`$GETSTRIPE -S $test_kdir`
5701         local default_count=`$GETSTRIPE -c $test_kdir`
5702         local default_offset=`$GETSTRIPE -i $test_kdir`
5703         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5704                 error 'dir setstripe failed'
5705         setfattr -n trusted.lov $test_kdir
5706         local stripe_size=`$GETSTRIPE -S $test_kdir`
5707         local stripe_count=`$GETSTRIPE -c $test_kdir`
5708         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5709         [ $stripe_size -eq $default_size ] ||
5710                 error "stripe size $stripe_size != $default_size"
5711         [ $stripe_count -eq $default_count ] ||
5712                 error "stripe count $stripe_count != $default_count"
5713         [ $stripe_offset -eq $default_offset ] ||
5714                 error "stripe offset $stripe_offset != $default_offset"
5715         rm -rf $DIR/$tfile $test_kdir
5716 }
5717 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5718
5719 test_102l() {
5720         # LU-532 trusted. xattr is invisible to non-root
5721         local testfile=$DIR/$tfile
5722
5723         touch $testfile
5724
5725         echo "listxattr as user..."
5726         chown $RUNAS_ID $testfile
5727         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5728             grep -q "trusted" &&
5729                 error "$testfile trusted xattrs are user visible"
5730
5731         return 0;
5732 }
5733 run_test 102l "listxattr filter test =================================="
5734
5735 cleanup_test102
5736
5737 run_acl_subtest()
5738 {
5739     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5740     return $?
5741 }
5742
5743 test_103 () {
5744     [ "$UID" != 0 ] && skip_env "must run as root" && return
5745     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5746     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5747     $GSS && skip "could not run under gss" && return
5748
5749     declare -a identity_old
5750
5751         for num in $(seq $MDSCOUNT); do
5752                 switch_identity $num true || identity_old[$num]=$?
5753         done
5754
5755     SAVE_UMASK=`umask`
5756     umask 0022
5757     cd $DIR
5758
5759     echo "performing cp ..."
5760     run_acl_subtest cp || error
5761     echo "performing getfacl-noacl..."
5762     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5763     echo "performing misc..."
5764     run_acl_subtest misc || error  "misc test failed"
5765     echo "performing permissions..."
5766     run_acl_subtest permissions || error "permissions failed"
5767     echo "performing setfacl..."
5768     run_acl_subtest setfacl || error  "setfacl test failed"
5769
5770     # inheritance test got from HP
5771     echo "performing inheritance..."
5772     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5773     chmod +x make-tree || error "chmod +x failed"
5774     run_acl_subtest inheritance || error "inheritance test failed"
5775     rm -f make-tree
5776
5777     echo "LU-974 ignore umask when acl is enabled..."
5778     mkdir $DIR/974
5779     cd $DIR/974
5780     run_acl_subtest 974 || error "LU-974 test failed"
5781     rm -rf $DIR/974
5782
5783     cd $SAVE_PWD
5784     umask $SAVE_UMASK
5785
5786         for num in $(seq $MDSCOUNT); do
5787                 if [ "${identity_old[$num]}" = 1 ]; then
5788                         switch_identity $num false || identity_old[$num]=$?
5789                 fi
5790         done
5791 }
5792 run_test 103 "acl test ========================================="
5793
5794 test_104a() {
5795         touch $DIR/$tfile
5796         lfs df || error "lfs df failed"
5797         lfs df -ih || error "lfs df -ih failed"
5798         lfs df -h $DIR || error "lfs df -h $DIR failed"
5799         lfs df -i $DIR || error "lfs df -i $DIR failed"
5800         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5801         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5802
5803         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
5804         lctl --device %$OSC deactivate
5805         lfs df || error "lfs df with deactivated OSC failed"
5806         lctl --device %$OSC activate
5807         # wait the osc back to normal
5808         wait_osc_import_state client ost FULL
5809
5810         lfs df || error "lfs df with reactivated OSC failed"
5811         rm -f $DIR/$tfile
5812 }
5813 run_test 104a "lfs df [-ih] [path] test ========================="
5814
5815 test_104b() {
5816         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5817         chmod 666 /dev/obd
5818         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5819         if [ $denied_cnt -ne 0 ];
5820         then
5821                     error "lfs check servers test failed"
5822         fi
5823 }
5824 run_test 104b "$RUNAS lfs check servers test ===================="
5825
5826 test_105a() {
5827         # doesn't work on 2.4 kernels
5828         touch $DIR/$tfile
5829         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5830         then
5831                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5832         else
5833                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5834         fi
5835         rm -f $DIR/$tfile
5836 }
5837 run_test 105a "flock when mounted without -o flock test ========"
5838
5839 test_105b() {
5840         touch $DIR/$tfile
5841         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5842         then
5843                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5844         else
5845                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5846         fi
5847         rm -f $DIR/$tfile
5848 }
5849 run_test 105b "fcntl when mounted without -o flock test ========"
5850
5851 test_105c() {
5852         touch $DIR/$tfile
5853         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5854         then
5855                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
5856         else
5857                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
5858         fi
5859         rm -f $DIR/$tfile
5860 }
5861 run_test 105c "lockf when mounted without -o flock test ========"
5862
5863 test_105d() { # bug 15924
5864         mkdir -p $DIR/$tdir
5865         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5866                 skip "mount w/o flock enabled" && return
5867         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
5868         $LCTL set_param fail_loc=0x80000315
5869         flocks_test 2 $DIR/$tdir
5870 }
5871 run_test 105d "flock race (should not freeze) ========"
5872
5873 test_105e() { # bug 22660 && 22040
5874         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5875                 skip "mount w/o flock enabled" && return
5876         touch $DIR/$tfile
5877         flocks_test 3 $DIR/$tfile
5878 }
5879 run_test 105e "Two conflicting flocks from same process ======="
5880
5881 test_106() { #bug 10921
5882         mkdir -p $DIR/$tdir
5883         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
5884         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
5885 }
5886 run_test 106 "attempt exec of dir followed by chown of that dir"
5887
5888 test_107() {
5889         CDIR=`pwd`
5890         cd $DIR
5891
5892         local file=core
5893         rm -f $file
5894
5895         local save_pattern=$(sysctl -n kernel.core_pattern)
5896         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
5897         sysctl -w kernel.core_pattern=$file
5898         sysctl -w kernel.core_uses_pid=0
5899
5900         ulimit -c unlimited
5901         sleep 60 &
5902         SLEEPPID=$!
5903
5904         sleep 1
5905
5906         kill -s 11 $SLEEPPID
5907         wait $SLEEPPID
5908         if [ -e $file ]; then
5909                 size=`stat -c%s $file`
5910                 [ $size -eq 0 ] && error "Fail to create core file $file"
5911         else
5912                 error "Fail to create core file $file"
5913         fi
5914         rm -f $file
5915         sysctl -w kernel.core_pattern=$save_pattern
5916         sysctl -w kernel.core_uses_pid=$save_uses_pid
5917         cd $CDIR
5918 }
5919 run_test 107 "Coredump on SIG"
5920
5921 test_110() {
5922         mkdir -p $DIR/$tdir
5923         mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
5924                 error "mkdir with 255 char failed"
5925         mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
5926                 error "mkdir with 256 char should fail, but did not"
5927         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
5928                 error "create with 255 char failed"
5929         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
5930                 error "create with 256 char should fail, but did not"
5931
5932         ls -l $DIR/$tdir
5933         rm -rf $DIR/$tdir
5934 }
5935 run_test 110 "filename length checking"
5936
5937 test_115() {
5938         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5939             cut -c11-20)
5940         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
5941             return
5942         echo "Starting with $OSTIO_pre threads"
5943
5944         NUMTEST=20000
5945         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5946         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
5947         echo "$NUMTEST creates/unlinks"
5948         mkdir -p $DIR/$tdir
5949         createmany -o $DIR/$tdir/$tfile $NUMTEST
5950         unlinkmany $DIR/$tdir/$tfile $NUMTEST
5951
5952         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5953             cut -c11-20)
5954
5955         # don't return an error
5956         [ $OSTIO_post == $OSTIO_pre ] && echo \
5957             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
5958             echo "This may be fine, depending on what ran before this test" &&
5959             echo "and how fast this system is." && return
5960
5961         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
5962 }
5963 run_test 115 "verify dynamic thread creation===================="
5964
5965 free_min_max () {
5966         wait_delete_completed
5967         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
5968         echo OST kbytes available: ${AVAIL[@]}
5969         MAXI=0; MAXV=${AVAIL[0]}
5970         MINI=0; MINV=${AVAIL[0]}
5971         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
5972             #echo OST $i: ${AVAIL[i]}kb
5973             if [ ${AVAIL[i]} -gt $MAXV ]; then
5974                 MAXV=${AVAIL[i]}; MAXI=$i
5975             fi
5976             if [ ${AVAIL[i]} -lt $MINV ]; then
5977                 MINV=${AVAIL[i]}; MINI=$i
5978             fi
5979         done
5980         echo Min free space: OST $MINI: $MINV
5981         echo Max free space: OST $MAXI: $MAXV
5982 }
5983
5984 test_116a() { # was previously test_116()
5985         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
5986
5987         echo -n "Free space priority "
5988         lctl get_param -n lov.*-clilov-*.qos_prio_free
5989         declare -a AVAIL
5990         free_min_max
5991         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
5992                 return
5993
5994         # generate uneven OSTs
5995         mkdir -p $DIR/$tdir/OST${MINI}
5996         declare -i FILL
5997         FILL=$(($MINV / 4))
5998         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
5999         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6000         i=0
6001         while [ $FILL -gt 0 ]; do
6002             i=$(($i + 1))
6003             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6004             FILL=$(($FILL - 2048))
6005             echo -n .
6006         done
6007         FILL=$(($MINV / 4))
6008         sync
6009         sleep_maxage
6010
6011         free_min_max
6012         DIFF=$(($MAXV - $MINV))
6013         DIFF2=$(($DIFF * 100 / $MINV))
6014         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6015         if [ $DIFF2 -gt 20 ]; then
6016             echo "ok"
6017         else
6018             echo "failed - QOS mode won't be used"
6019             error_ignore "QOS imbalance criteria not met"
6020             return
6021         fi
6022
6023         MINI1=$MINI; MINV1=$MINV
6024         MAXI1=$MAXI; MAXV1=$MAXV
6025
6026         # now fill using QOS
6027         echo writing a bunch of files to QOS-assigned OSTs
6028         $SETSTRIPE -c 1 $DIR/$tdir
6029         i=0
6030         while [ $FILL -gt 0 ]; do
6031             i=$(($i + 1))
6032             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6033             FILL=$(($FILL - 200))
6034             echo -n .
6035         done
6036         echo "wrote $i 200k files"
6037         sync
6038         sleep_maxage
6039
6040         echo "Note: free space may not be updated, so measurements might be off"
6041         free_min_max
6042         DIFF2=$(($MAXV - $MINV))
6043         echo "free space delta: orig $DIFF final $DIFF2"
6044         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6045         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6046         echo "Wrote $DIFF to smaller OST $MINI1"
6047         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6048         echo "Wrote $DIFF2 to larger OST $MAXI1"
6049         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6050
6051         # Figure out which files were written where
6052         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6053                awk '/'$MINI1': / {print $2; exit}')
6054         echo $UUID
6055         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6056         echo "$MINC files created on smaller OST $MINI1"
6057         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6058                awk '/'$MAXI1': / {print $2; exit}')
6059         echo $UUID
6060         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6061         echo "$MAXC files created on larger OST $MAXI1"
6062         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6063         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6064
6065         rm -rf $DIR/$tdir
6066 }
6067 run_test 116a "stripe QOS: free space balance ==================="
6068
6069 test_116b() { # LU-2093
6070 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6071         local old_rr
6072         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6073         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6074         mkdir -p $DIR/$tdir
6075         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6076         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6077         do_facet $SINGLEMDS lctl set_param fail_loc=0
6078         rm -rf $DIR/$tdir
6079         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6080 }
6081 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6082
6083 test_117() # bug 10891
6084 {
6085         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6086         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6087         lctl set_param fail_loc=0x21e
6088         > $DIR/$tfile || error "truncate failed"
6089         lctl set_param fail_loc=0
6090         echo "Truncate succeeded."
6091         rm -f $DIR/$tfile
6092 }
6093 run_test 117 "verify fsfilt_extend =========="
6094
6095 export OLD_RESENDCOUNT=""
6096 set_resend_count () {
6097         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6098         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6099         lctl set_param -n $PROC_RESENDCOUNT $1
6100         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6101 }
6102
6103 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6104
6105 # Reset async IO behavior after error case
6106 reset_async() {
6107         FILE=$DIR/reset_async
6108
6109         # Ensure all OSCs are cleared
6110         $SETSTRIPE -c -1 $FILE
6111         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6112         sync
6113         rm $FILE
6114 }
6115
6116 test_118a() #bug 11710
6117 {
6118         reset_async
6119
6120         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6121         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6122         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6123
6124         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6125                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6126                 return 1;
6127         fi
6128         rm -f $DIR/$tfile
6129 }
6130 run_test 118a "verify O_SYNC works =========="
6131
6132 test_118b()
6133 {
6134         remote_ost_nodsh && skip "remote OST with nodsh" && return
6135
6136         reset_async
6137
6138         #define OBD_FAIL_OST_ENOENT 0x217
6139         set_nodes_failloc "$(osts_nodes)" 0x217
6140         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6141         RC=$?
6142         set_nodes_failloc "$(osts_nodes)" 0
6143         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6144         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6145                     grep -c writeback)
6146
6147         if [[ $RC -eq 0 ]]; then
6148                 error "Must return error due to dropped pages, rc=$RC"
6149                 return 1;
6150         fi
6151
6152         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6153                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6154                 return 1;
6155         fi
6156
6157         echo "Dirty pages not leaked on ENOENT"
6158
6159         # Due to the above error the OSC will issue all RPCs syncronously
6160         # until a subsequent RPC completes successfully without error.
6161         $MULTIOP $DIR/$tfile Ow4096yc
6162         rm -f $DIR/$tfile
6163
6164         return 0
6165 }
6166 run_test 118b "Reclaim dirty pages on fatal error =========="
6167
6168 test_118c()
6169 {
6170         remote_ost_nodsh && skip "remote OST with nodsh" && return
6171
6172         reset_async
6173
6174         #define OBD_FAIL_OST_EROFS               0x216
6175         set_nodes_failloc "$(osts_nodes)" 0x216
6176
6177         # multiop should block due to fsync until pages are written
6178         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6179         MULTIPID=$!
6180         sleep 1
6181
6182         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6183                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6184         fi
6185
6186         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6187                     grep -c writeback)
6188         if [[ $WRITEBACK -eq 0 ]]; then
6189                 error "No page in writeback, writeback=$WRITEBACK"
6190         fi
6191
6192         set_nodes_failloc "$(osts_nodes)" 0
6193         wait $MULTIPID
6194         RC=$?
6195         if [[ $RC -ne 0 ]]; then
6196                 error "Multiop fsync failed, rc=$RC"
6197         fi
6198
6199         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6200         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6201                     grep -c writeback)
6202         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6203                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6204         fi
6205
6206         rm -f $DIR/$tfile
6207         echo "Dirty pages flushed via fsync on EROFS"
6208         return 0
6209 }
6210 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6211
6212 test_118d()
6213 {
6214         remote_ost_nodsh && skip "remote OST with nodsh" && return
6215
6216         reset_async
6217
6218         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6219         set_nodes_failloc "$(osts_nodes)" 0x214
6220         # multiop should block due to fsync until pages are written
6221         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6222         MULTIPID=$!
6223         sleep 1
6224
6225         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6226                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6227         fi
6228
6229         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6230                     grep -c writeback)
6231         if [[ $WRITEBACK -eq 0 ]]; then
6232                 error "No page in writeback, writeback=$WRITEBACK"
6233         fi
6234
6235         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6236         set_nodes_failloc "$(osts_nodes)" 0
6237
6238         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6239         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6240                     grep -c writeback)
6241         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6242                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6243         fi
6244
6245         rm -f $DIR/$tfile
6246         echo "Dirty pages gaurenteed flushed via fsync"
6247         return 0
6248 }
6249 run_test 118d "Fsync validation inject a delay of the bulk =========="
6250
6251 test_118f() {
6252         reset_async
6253
6254         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6255         lctl set_param fail_loc=0x8000040a
6256
6257         # Should simulate EINVAL error which is fatal
6258         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6259         RC=$?
6260         if [[ $RC -eq 0 ]]; then
6261                 error "Must return error due to dropped pages, rc=$RC"
6262         fi
6263
6264         lctl set_param fail_loc=0x0
6265
6266         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6267         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6268         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6269                     grep -c writeback)
6270         if [[ $LOCKED -ne 0 ]]; then
6271                 error "Locked pages remain in cache, locked=$LOCKED"
6272         fi
6273
6274         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6275                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6276         fi
6277
6278         rm -f $DIR/$tfile
6279         echo "No pages locked after fsync"
6280
6281         reset_async
6282         return 0
6283 }
6284 run_test 118f "Simulate unrecoverable OSC side error =========="
6285
6286 test_118g() {
6287         reset_async
6288
6289         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6290         lctl set_param fail_loc=0x406
6291
6292         # simulate local -ENOMEM
6293         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6294         RC=$?
6295
6296         lctl set_param fail_loc=0
6297         if [[ $RC -eq 0 ]]; then
6298                 error "Must return error due to dropped pages, rc=$RC"
6299         fi
6300
6301         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6302         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6303         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6304                         grep -c writeback)
6305         if [[ $LOCKED -ne 0 ]]; then
6306                 error "Locked pages remain in cache, locked=$LOCKED"
6307         fi
6308
6309         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6310                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6311         fi
6312
6313         rm -f $DIR/$tfile
6314         echo "No pages locked after fsync"
6315
6316         reset_async
6317         return 0
6318 }
6319 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6320
6321 test_118h() {
6322         remote_ost_nodsh && skip "remote OST with nodsh" && return
6323
6324         reset_async
6325
6326         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6327         set_nodes_failloc "$(osts_nodes)" 0x20e
6328         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6329         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6330         RC=$?
6331
6332         set_nodes_failloc "$(osts_nodes)" 0
6333         if [[ $RC -eq 0 ]]; then
6334                 error "Must return error due to dropped pages, rc=$RC"
6335         fi
6336
6337         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6338         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6339         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6340                     grep -c writeback)
6341         if [[ $LOCKED -ne 0 ]]; then
6342                 error "Locked pages remain in cache, locked=$LOCKED"
6343         fi
6344
6345         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6346                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6347         fi
6348
6349         rm -f $DIR/$tfile
6350         echo "No pages locked after fsync"
6351
6352         return 0
6353 }
6354 run_test 118h "Verify timeout in handling recoverables errors  =========="
6355
6356 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6357
6358 test_118i() {
6359         remote_ost_nodsh && skip "remote OST with nodsh" && return
6360
6361         reset_async
6362
6363         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6364         set_nodes_failloc "$(osts_nodes)" 0x20e
6365
6366         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6367         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6368         PID=$!
6369         sleep 5
6370         set_nodes_failloc "$(osts_nodes)" 0
6371
6372         wait $PID
6373         RC=$?
6374         if [[ $RC -ne 0 ]]; then
6375                 error "got error, but should be not, rc=$RC"
6376         fi
6377
6378         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6379         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6380         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6381         if [[ $LOCKED -ne 0 ]]; then
6382                 error "Locked pages remain in cache, locked=$LOCKED"
6383         fi
6384
6385         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6386                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6387         fi
6388
6389         rm -f $DIR/$tfile
6390         echo "No pages locked after fsync"
6391
6392         return 0
6393 }
6394 run_test 118i "Fix error before timeout in recoverable error  =========="
6395
6396 [ "$SLOW" = "no" ] && set_resend_count 4
6397
6398 test_118j() {
6399         remote_ost_nodsh && skip "remote OST with nodsh" && return
6400
6401         reset_async
6402
6403         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6404         set_nodes_failloc "$(osts_nodes)" 0x220
6405
6406         # return -EIO from OST
6407         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6408         RC=$?
6409         set_nodes_failloc "$(osts_nodes)" 0x0
6410         if [[ $RC -eq 0 ]]; then
6411                 error "Must return error due to dropped pages, rc=$RC"
6412         fi
6413
6414         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6415         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6416         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6417         if [[ $LOCKED -ne 0 ]]; then
6418                 error "Locked pages remain in cache, locked=$LOCKED"
6419         fi
6420
6421         # in recoverable error on OST we want resend and stay until it finished
6422         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6423                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6424         fi
6425
6426         rm -f $DIR/$tfile
6427         echo "No pages locked after fsync"
6428
6429         return 0
6430 }
6431 run_test 118j "Simulate unrecoverable OST side error =========="
6432
6433 test_118k()
6434 {
6435         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6436
6437         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6438         set_nodes_failloc "$(osts_nodes)" 0x20e
6439         mkdir -p $DIR/$tdir
6440
6441         for ((i=0;i<10;i++)); do
6442                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6443                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6444                 SLEEPPID=$!
6445                 sleep 0.500s
6446                 kill $SLEEPPID
6447                 wait $SLEEPPID
6448         done
6449
6450         set_nodes_failloc "$(osts_nodes)" 0
6451         rm -rf $DIR/$tdir
6452 }
6453 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6454
6455 test_118l()
6456 {
6457         # LU-646
6458         mkdir -p $DIR/$tdir
6459         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6460         rm -rf $DIR/$tdir
6461 }
6462 run_test 118l "fsync dir ========="
6463
6464 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6465
6466 test_119a() # bug 11737
6467 {
6468         BSIZE=$((512 * 1024))
6469         directio write $DIR/$tfile 0 1 $BSIZE
6470         # We ask to read two blocks, which is more than a file size.
6471         # directio will indicate an error when requested and actual
6472         # sizes aren't equeal (a normal situation in this case) and
6473         # print actual read amount.
6474         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6475         if [ "$NOB" != "$BSIZE" ]; then
6476                 error "read $NOB bytes instead of $BSIZE"
6477         fi
6478         rm -f $DIR/$tfile
6479 }
6480 run_test 119a "Short directIO read must return actual read amount"
6481
6482 test_119b() # bug 11737
6483 {
6484         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6485
6486         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6487         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6488         sync
6489         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6490                 error "direct read failed"
6491         rm -f $DIR/$tfile
6492 }
6493 run_test 119b "Sparse directIO read must return actual read amount"
6494
6495 test_119c() # bug 13099
6496 {
6497         BSIZE=1048576
6498         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6499         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6500         rm -f $DIR/$tfile
6501 }
6502 run_test 119c "Testing for direct read hitting hole"
6503
6504 test_119d() # bug 15950
6505 {
6506         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6507         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6508         BSIZE=1048576
6509         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6510         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6511         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6512         lctl set_param fail_loc=0x40d
6513         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6514         pid_dio=$!
6515         sleep 1
6516         cat $DIR/$tfile > /dev/null &
6517         lctl set_param fail_loc=0
6518         pid_reads=$!
6519         wait $pid_dio
6520         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6521         sleep 2
6522         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6523         error "the read rpcs have not completed in 2s"
6524         rm -f $DIR/$tfile
6525         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6526 }
6527 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6528
6529 test_120a() {
6530         mkdir -p $DIR/$tdir
6531         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6532                skip "no early lock cancel on server" && return 0
6533         lru_resize_disable mdc
6534         lru_resize_disable osc
6535         cancel_lru_locks mdc
6536         stat $DIR/$tdir > /dev/null
6537         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6538         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6539         mkdir $DIR/$tdir/d1
6540         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6541         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6542         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6543         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6544         lru_resize_enable mdc
6545         lru_resize_enable osc
6546 }
6547 run_test 120a "Early Lock Cancel: mkdir test"
6548
6549 test_120b() {
6550         mkdir -p $DIR/$tdir
6551         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6552                skip "no early lock cancel on server" && return 0
6553         lru_resize_disable mdc
6554         lru_resize_disable osc
6555         cancel_lru_locks mdc
6556         stat $DIR/$tdir > /dev/null
6557         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6558         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6559         touch $DIR/$tdir/f1
6560         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6561         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6562         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6563         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6564         lru_resize_enable mdc
6565         lru_resize_enable osc
6566 }
6567 run_test 120b "Early Lock Cancel: create test"
6568
6569 test_120c() {
6570         mkdir -p $DIR/$tdir
6571         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6572                skip "no early lock cancel on server" && return 0
6573         lru_resize_disable mdc
6574         lru_resize_disable osc
6575         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6576         touch $DIR/$tdir/d1/f1
6577         cancel_lru_locks mdc
6578         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6579         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6580         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6581         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6582         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6583         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6584         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6585         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6586         lru_resize_enable mdc
6587         lru_resize_enable osc
6588 }
6589 run_test 120c "Early Lock Cancel: link test"
6590
6591 test_120d() {
6592         mkdir -p $DIR/$tdir
6593         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6594                skip "no early lock cancel on server" && return 0
6595         lru_resize_disable mdc
6596         lru_resize_disable osc
6597         touch $DIR/$tdir
6598         cancel_lru_locks mdc
6599         stat $DIR/$tdir > /dev/null
6600         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6601         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6602         chmod a+x $DIR/$tdir
6603         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6604         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6605         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6606         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6607         lru_resize_enable mdc
6608         lru_resize_enable osc
6609 }
6610 run_test 120d "Early Lock Cancel: setattr test"
6611
6612 test_120e() {
6613         mkdir -p $DIR/$tdir
6614         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6615                skip "no early lock cancel on server" && return 0
6616         lru_resize_disable mdc
6617         lru_resize_disable osc
6618         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6619         cancel_lru_locks mdc
6620         cancel_lru_locks osc
6621         dd if=$DIR/$tdir/f1 of=/dev/null
6622         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6623         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6624               awk '/ldlm_cancel/ {print $2}'`
6625         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6626               awk '/ldlm_bl_callback/ {print $2}'`
6627         unlink $DIR/$tdir/f1
6628         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6629               awk '/ldlm_cancel/ {print $2}'`
6630         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6631               awk '/ldlm_bl_callback/ {print $2}'`
6632         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6633         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6634         lru_resize_enable mdc
6635         lru_resize_enable osc
6636 }
6637 run_test 120e "Early Lock Cancel: unlink test"
6638
6639 test_120f() {
6640         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6641                skip "no early lock cancel on server" && return 0
6642         mkdir -p $DIR/$tdir
6643         lru_resize_disable mdc
6644         lru_resize_disable osc
6645         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6646         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6647         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6648         cancel_lru_locks mdc
6649         cancel_lru_locks osc
6650         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6651         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6652         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6653         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6654               awk '/ldlm_cancel/ {print $2}'`
6655         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6656               awk '/ldlm_bl_callback/ {print $2}'`
6657         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6658         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6659               awk '/ldlm_cancel/ {print $2}'`
6660         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6661               awk '/ldlm_bl_callback/ {print $2}'`
6662         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6663         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6664         lru_resize_enable mdc
6665         lru_resize_enable osc
6666 }
6667 run_test 120f "Early Lock Cancel: rename test"
6668
6669 test_120g() {
6670         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6671                skip "no early lock cancel on server" && return 0
6672         lru_resize_disable mdc
6673         lru_resize_disable osc
6674         count=10000
6675         echo create $count files
6676         mkdir -p $DIR/$tdir
6677         cancel_lru_locks mdc
6678         cancel_lru_locks osc
6679         t0=`date +%s`
6680
6681         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6682               awk '/ldlm_cancel/ {print $2}'`
6683         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6684               awk '/ldlm_bl_callback/ {print $2}'`
6685         createmany -o $DIR/$tdir/f $count
6686         sync
6687         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6688               awk '/ldlm_cancel/ {print $2}'`
6689         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6690               awk '/ldlm_bl_callback/ {print $2}'`
6691         t1=`date +%s`
6692         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6693         echo rm $count files
6694         rm -r $DIR/$tdir
6695         sync
6696         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6697               awk '/ldlm_cancel/ {print $2}'`
6698         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6699               awk '/ldlm_bl_callback/ {print $2}'`
6700         t2=`date +%s`
6701         echo total: $count removes in $((t2-t1))
6702         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6703         sleep 2
6704         # wait for commitment of removal
6705         lru_resize_enable mdc
6706         lru_resize_enable osc
6707 }
6708 run_test 120g "Early Lock Cancel: performance test"
6709
6710 test_121() { #bug #10589
6711         rm -rf $DIR/$tfile
6712         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6713 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6714         lctl set_param fail_loc=0x310
6715         cancel_lru_locks osc > /dev/null
6716         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6717         lctl set_param fail_loc=0
6718         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6719 }
6720 run_test 121 "read cancel race ========="
6721
6722 test_123a() { # was test 123, statahead(bug 11401)
6723         SLOWOK=0
6724         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6725             log "testing on UP system. Performance may be not as good as expected."
6726                         SLOWOK=1
6727         fi
6728
6729         rm -rf $DIR/$tdir
6730         mkdir -p $DIR/$tdir
6731         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6732         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6733         MULT=10
6734         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6735                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6736
6737                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6738                 lctl set_param -n llite.*.statahead_max 0
6739                 lctl get_param llite.*.statahead_max
6740                 cancel_lru_locks mdc
6741                 cancel_lru_locks osc
6742                 stime=`date +%s`
6743                 time ls -l $DIR/$tdir | wc -l
6744                 etime=`date +%s`
6745                 delta=$((etime - stime))
6746                 log "ls $i files without statahead: $delta sec"
6747                 lctl set_param llite.*.statahead_max=$max
6748
6749                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6750                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6751                 cancel_lru_locks mdc
6752                 cancel_lru_locks osc
6753                 stime=`date +%s`
6754                 time ls -l $DIR/$tdir | wc -l
6755                 etime=`date +%s`
6756                 delta_sa=$((etime - stime))
6757                 log "ls $i files with statahead: $delta_sa sec"
6758                 lctl get_param -n llite.*.statahead_stats
6759                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6760
6761                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6762                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6763
6764                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6765                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6766                     lctl set_param -n llite.*.statahead_max 0
6767                     lctl get_param llite.*.statahead_max
6768                     cancel_lru_locks mdc
6769                     cancel_lru_locks osc
6770                     stime=`date +%s`
6771                     time ls -l $DIR/$tdir | wc -l
6772                     etime=`date +%s`
6773                     delta=$((etime - stime))
6774                     log "ls $i files again without statahead: $delta sec"
6775                     lctl set_param llite.*.statahead_max=$max
6776                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6777                         if [  $SLOWOK -eq 0 ]; then
6778                                 error "ls $i files is slower with statahead!"
6779                         else
6780                                 log "ls $i files is slower with statahead!"
6781                         fi
6782                         break
6783                     fi
6784                 fi
6785
6786                 [ $delta -gt 20 ] && break
6787                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6788                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6789         done
6790         log "ls done"
6791
6792         stime=`date +%s`
6793         rm -r $DIR/$tdir
6794         sync
6795         etime=`date +%s`
6796         delta=$((etime - stime))
6797         log "rm -r $DIR/$tdir/: $delta seconds"
6798         log "rm done"
6799         lctl get_param -n llite.*.statahead_stats
6800 }
6801 run_test 123a "verify statahead work"
6802
6803 test_123b () { # statahead(bug 15027)
6804         mkdir -p $DIR/$tdir
6805         createmany -o $DIR/$tdir/$tfile-%d 1000
6806
6807         cancel_lru_locks mdc
6808         cancel_lru_locks osc
6809
6810 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6811         lctl set_param fail_loc=0x80000803
6812         ls -lR $DIR/$tdir > /dev/null
6813         log "ls done"
6814         lctl set_param fail_loc=0x0
6815         lctl get_param -n llite.*.statahead_stats
6816         rm -r $DIR/$tdir
6817         sync
6818
6819 }
6820 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6821
6822 test_124a() {
6823         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6824                skip "no lru resize on server" && return 0
6825         local NR=2000
6826         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6827
6828         log "create $NR files at $DIR/$tdir"
6829         createmany -o $DIR/$tdir/f $NR ||
6830                 error "failed to create $NR files in $DIR/$tdir"
6831
6832         cancel_lru_locks mdc
6833         ls -l $DIR/$tdir > /dev/null
6834
6835         local NSDIR=""
6836         local LRU_SIZE=0
6837         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6838                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6839                 LRU_SIZE=$(lctl get_param -n $PARAM)
6840                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6841                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6842                                                 log "NSDIR=$NSDIR"
6843                         log "NS=$(basename $NSDIR)"
6844                         break
6845                 fi
6846         done
6847
6848         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
6849                 skip "Not enough cached locks created!"
6850                 return 0
6851         fi
6852         log "LRU=$LRU_SIZE"
6853
6854         local SLEEP=30
6855
6856         # We know that lru resize allows one client to hold $LIMIT locks
6857         # for 10h. After that locks begin to be killed by client.
6858         local MAX_HRS=10
6859         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
6860                 log "LIMIT=$LIMIT"
6861
6862         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
6863         # killing locks. Some time was spent for creating locks. This means
6864         # that up to the moment of sleep finish we must have killed some of
6865         # them (10-100 locks). This depends on how fast ther were created.
6866         # Many of them were touched in almost the same moment and thus will
6867         # be killed in groups.
6868         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
6869
6870         # Use $LRU_SIZE_B here to take into account real number of locks
6871         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
6872         local LRU_SIZE_B=$LRU_SIZE
6873         log "LVF=$LVF"
6874         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
6875                 log "OLD_LVF=$OLD_LVF"
6876         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
6877
6878         # Let's make sure that we really have some margin. Client checks
6879         # cached locks every 10 sec.
6880         SLEEP=$((SLEEP+20))
6881         log "Sleep ${SLEEP} sec"
6882         local SEC=0
6883         while ((SEC<$SLEEP)); do
6884                 echo -n "..."
6885                 sleep 5
6886                 SEC=$((SEC+5))
6887                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
6888                 echo -n "$LRU_SIZE"
6889         done
6890         echo ""
6891         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
6892         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
6893
6894         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
6895                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
6896                 unlinkmany $DIR/$tdir/f $NR
6897                 return
6898         }
6899
6900         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
6901         log "unlink $NR files at $DIR/$tdir"
6902         unlinkmany $DIR/$tdir/f $NR
6903 }
6904 run_test 124a "lru resize ======================================="
6905
6906 get_max_pool_limit()
6907 {
6908         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
6909         local max=0
6910         for l in $limit; do
6911                 if test $l -gt $max; then
6912                         max=$l
6913                 fi
6914         done
6915         echo $max
6916 }
6917
6918 test_124b() {
6919         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6920                skip "no lru resize on server" && return 0
6921
6922         LIMIT=`get_max_pool_limit`
6923
6924         NR=$(($(default_lru_size)*20))
6925         if [ $NR -gt $LIMIT ]; then
6926                 log "Limit lock number by $LIMIT locks"
6927                 NR=$LIMIT
6928         fi
6929         lru_resize_disable mdc
6930         mkdir -p $DIR/$tdir/disable_lru_resize ||
6931                 error "failed to create $DIR/$tdir/disable_lru_resize"
6932
6933         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
6934         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
6935         cancel_lru_locks mdc
6936         stime=`date +%s`
6937         PID=""
6938         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6939         PID="$PID $!"
6940         sleep 2
6941         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6942         PID="$PID $!"
6943         sleep 2
6944         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6945         PID="$PID $!"
6946         wait $PID
6947         etime=`date +%s`
6948         nolruresize_delta=$((etime-stime))
6949         log "ls -la time: $nolruresize_delta seconds"
6950         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6951         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
6952
6953         lru_resize_enable mdc
6954         mkdir -p $DIR/$tdir/enable_lru_resize ||
6955                 error "failed to create $DIR/$tdir/enable_lru_resize"
6956
6957         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
6958         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
6959         cancel_lru_locks mdc
6960         stime=`date +%s`
6961         PID=""
6962         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6963         PID="$PID $!"
6964         sleep 2
6965         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6966         PID="$PID $!"
6967         sleep 2
6968         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6969         PID="$PID $!"
6970         wait $PID
6971         etime=`date +%s`
6972         lruresize_delta=$((etime-stime))
6973         log "ls -la time: $lruresize_delta seconds"
6974         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6975
6976         if [ $lruresize_delta -gt $nolruresize_delta ]; then
6977                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
6978         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
6979                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
6980         else
6981                 log "lru resize performs the same with no lru resize"
6982         fi
6983         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
6984 }
6985 run_test 124b "lru resize (performance test) ======================="
6986
6987 test_125() { # 13358
6988         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6989         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6990         mkdir -p $DIR/d125 || error "mkdir failed"
6991         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
6992         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
6993         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
6994 }
6995 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
6996
6997 test_126() { # bug 12829/13455
6998         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6999         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7000         $GSS && skip "must run as gss disabled" && return
7001
7002         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7003         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7004         rm -f $DIR/$tfile
7005         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7006 }
7007 run_test 126 "check that the fsgid provided by the client is taken into account"
7008
7009 test_127a() { # bug 15521
7010         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7011         $LCTL set_param osc.*.stats=0
7012         FSIZE=$((2048 * 1024))
7013         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7014         cancel_lru_locks osc
7015         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7016
7017         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7018         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7019                 echo "got $COUNT $NAME"
7020                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7021                 eval $NAME=$COUNT || error "Wrong proc format"
7022
7023                 case $NAME in
7024                         read_bytes|write_bytes)
7025                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7026                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7027                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7028                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7029                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7030                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7031                                 error "sumsquare is too small: $SUMSQ"
7032                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7033                                 error "sumsquare is too big: $SUMSQ"
7034                         ;;
7035                         *) ;;
7036                 esac
7037         done < $DIR/${tfile}.tmp
7038
7039         #check that we actually got some stats
7040         [ "$read_bytes" ] || error "Missing read_bytes stats"
7041         [ "$write_bytes" ] || error "Missing write_bytes stats"
7042         [ "$read_bytes" != 0 ] || error "no read done"
7043         [ "$write_bytes" != 0 ] || error "no write done"
7044 }
7045 run_test 127a "verify the client stats are sane"
7046
7047 test_127b() { # bug LU-333
7048         $LCTL set_param llite.*.stats=0
7049         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7050         # perform 2 reads and writes so MAX is different from SUM.
7051         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7052         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7053         cancel_lru_locks osc
7054         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7055         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7056
7057         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7058         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7059                 echo "got $COUNT $NAME"
7060                 eval $NAME=$COUNT || error "Wrong proc format"
7061
7062         case $NAME in
7063                 read_bytes)
7064                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7065                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7066                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7067                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7068                         ;;
7069                 write_bytes)
7070                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7071                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7072                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7073                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7074                         ;;
7075                         *) ;;
7076                 esac
7077         done < $TMP/${tfile}.tmp
7078
7079         #check that we actually got some stats
7080         [ "$read_bytes" ] || error "Missing read_bytes stats"
7081         [ "$write_bytes" ] || error "Missing write_bytes stats"
7082         [ "$read_bytes" != 0 ] || error "no read done"
7083         [ "$write_bytes" != 0 ] || error "no write done"
7084 }
7085 run_test 127b "verify the llite client stats are sane"
7086
7087 test_128() { # bug 15212
7088         touch $DIR/$tfile
7089         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7090                 find $DIR/$tfile
7091                 find $DIR/$tfile
7092         EOF
7093
7094         result=$(grep error $TMP/$tfile.log)
7095         rm -f $DIR/$tfile
7096         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7097 }
7098 run_test 128 "interactive lfs for 2 consecutive find's"
7099
7100 set_dir_limits () {
7101         local mntdev
7102         local canondev
7103         local node
7104
7105         local LDPROC=/proc/fs/ldiskfs
7106         local facets=$(get_facets MDS)
7107
7108         for facet in ${facets//,/ }; do
7109                 canondev=$(ldiskfs_canon \
7110                            *.$(convert_facet2label $facet).mntdev $facet)
7111                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7112                                                 LDPROC=/sys/fs/ldiskfs
7113                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7114         done
7115 }
7116 test_129() {
7117         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7118                 skip "Only applicable to ldiskfs-based MDTs"
7119                 return
7120         fi
7121         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7122
7123         EFBIG=27
7124         MAX=16384
7125
7126         set_dir_limits $MAX
7127         mkdir -p $DIR/$tdir
7128
7129         local I=0
7130         local J=0
7131         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7132                 $MULTIOP $DIR/$tdir/$J Oc
7133                 rc=$?
7134                 if [ $rc -eq $EFBIG ]; then
7135                         set_dir_limits 0
7136                         echo "return code $rc received as expected"
7137                         return 0
7138                 elif [ $rc -ne 0 ]; then
7139                         set_dir_limits 0
7140                         error_exit "return code $rc received instead of expected $EFBIG"
7141                 fi
7142                 J=$((J+1))
7143                 I=$(stat -c%s "$DIR/$tdir")
7144         done
7145
7146         set_dir_limits 0
7147         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7148 }
7149 run_test 129 "test directory size limit ========================"
7150
7151 OLDIFS="$IFS"
7152 cleanup_130() {
7153         trap 0
7154         IFS="$OLDIFS"
7155 }
7156
7157 test_130a() {
7158         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7159         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7160                 return
7161
7162         trap cleanup_130 EXIT RETURN
7163
7164         local fm_file=$DIR/$tfile
7165         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7166         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7167                 error "dd failed for $fm_file"
7168
7169         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7170         filefrag -ves $fm_file
7171         RC=$?
7172         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7173                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7174         [ $RC != 0 ] && error "filefrag $fm_file failed"
7175
7176         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7177                       grep -v "ext:" | grep -v "found")
7178         lun=$($GETSTRIPE -i $fm_file)
7179
7180         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7181         IFS=$'\n'
7182         tot_len=0
7183         for line in $filefrag_op
7184         do
7185                 frag_lun=`echo $line | cut -d: -f5`
7186                 ext_len=`echo $line | cut -d: -f4`
7187                 if (( $frag_lun != $lun )); then
7188                         cleanup_130
7189                         error "FIEMAP on 1-stripe file($fm_file) failed"
7190                         return
7191                 fi
7192                 (( tot_len += ext_len ))
7193         done
7194
7195         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7196                 cleanup_130
7197                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7198                 return
7199         fi
7200
7201         cleanup_130
7202
7203         echo "FIEMAP on single striped file succeeded"
7204 }
7205 run_test 130a "FIEMAP (1-stripe file)"
7206
7207 test_130b() {
7208         [ "$OSTCOUNT" -lt "2" ] &&
7209                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7210
7211         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7212         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7213                 return
7214
7215         trap cleanup_130 EXIT RETURN
7216
7217         local fm_file=$DIR/$tfile
7218         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7219         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7220                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7221
7222         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7223                 error "dd failed on $fm_file"
7224
7225         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7226         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7227                       grep -v "ext:" | grep -v "found")
7228
7229         last_lun=$(echo $filefrag_op | cut -d: -f5)
7230
7231         IFS=$'\n'
7232         tot_len=0
7233         num_luns=1
7234         for line in $filefrag_op
7235         do
7236                 frag_lun=`echo $line | cut -d: -f5`
7237                 ext_len=`echo $line | cut -d: -f4`
7238                 if (( $frag_lun != $last_lun )); then
7239                         if (( tot_len != 1024 )); then
7240                                 cleanup_130
7241                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7242                                 return
7243                         else
7244                                 (( num_luns += 1 ))
7245                                 tot_len=0
7246                         fi
7247                 fi
7248                 (( tot_len += ext_len ))
7249                 last_lun=$frag_lun
7250         done
7251         if (( num_luns != 2 || tot_len != 1024 )); then
7252                 cleanup_130
7253                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7254                 return
7255         fi
7256
7257         cleanup_130
7258
7259         echo "FIEMAP on 2-stripe file succeeded"
7260 }
7261 run_test 130b "FIEMAP (2-stripe file)"
7262
7263 test_130c() {
7264         [ "$OSTCOUNT" -lt "2" ] &&
7265                 skip_env "skipping FIEMAP on 2-stripe file" && return
7266
7267         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7268         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7269                 return
7270
7271         trap cleanup_130 EXIT RETURN
7272
7273         local fm_file=$DIR/$tfile
7274         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7275         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7276                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7277
7278         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7279
7280         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7281         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7282
7283         last_lun=`echo $filefrag_op | cut -d: -f5`
7284
7285         IFS=$'\n'
7286         tot_len=0
7287         num_luns=1
7288         for line in $filefrag_op
7289         do
7290                 frag_lun=`echo $line | cut -d: -f5`
7291                 ext_len=`echo $line | cut -d: -f4`
7292                 if (( $frag_lun != $last_lun )); then
7293                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7294                         if (( logical != 512 )); then
7295                                 cleanup_130
7296                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7297                                 return
7298                         fi
7299                         if (( tot_len != 512 )); then
7300                                 cleanup_130
7301                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7302                                 return
7303                         else
7304                                 (( num_luns += 1 ))
7305                                 tot_len=0
7306                         fi
7307                 fi
7308                 (( tot_len += ext_len ))
7309                 last_lun=$frag_lun
7310         done
7311         if (( num_luns != 2 || tot_len != 512 )); then
7312                 cleanup_130
7313                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7314                 return
7315         fi
7316
7317         cleanup_130
7318
7319         echo "FIEMAP on 2-stripe file with hole succeeded"
7320 }
7321 run_test 130c "FIEMAP (2-stripe file with hole)"
7322
7323 test_130d() {
7324         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7325
7326         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7327         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7328
7329         trap cleanup_130 EXIT RETURN
7330
7331         local fm_file=$DIR/$tfile
7332         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7333         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7334                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7335         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7336
7337         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7338         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7339
7340         last_lun=`echo $filefrag_op | cut -d: -f5`
7341
7342         IFS=$'\n'
7343         tot_len=0
7344         num_luns=1
7345         for line in $filefrag_op
7346         do
7347                 frag_lun=`echo $line | cut -d: -f5`
7348                 ext_len=`echo $line | cut -d: -f4`
7349                 if (( $frag_lun != $last_lun )); then
7350                         if (( tot_len != 1024 )); then
7351                                 cleanup_130
7352                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7353                                 return
7354                         else
7355                                 (( num_luns += 1 ))
7356                                 tot_len=0
7357                         fi
7358                 fi
7359                 (( tot_len += ext_len ))
7360                 last_lun=$frag_lun
7361         done
7362         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7363                 cleanup_130
7364                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7365                 return
7366         fi
7367
7368         cleanup_130
7369
7370         echo "FIEMAP on N-stripe file succeeded"
7371 }
7372 run_test 130d "FIEMAP (N-stripe file)"
7373
7374 test_130e() {
7375         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7376
7377         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7378         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7379
7380         trap cleanup_130 EXIT RETURN
7381
7382         local fm_file=$DIR/$tfile
7383         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7384         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7385                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7386
7387         NUM_BLKS=512
7388         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7389         for ((i = 0; i < $NUM_BLKS; i++))
7390         do
7391                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7392         done
7393
7394         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7395         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7396
7397         last_lun=`echo $filefrag_op | cut -d: -f5`
7398
7399         IFS=$'\n'
7400         tot_len=0
7401         num_luns=1
7402         for line in $filefrag_op
7403         do
7404                 frag_lun=`echo $line | cut -d: -f5`
7405                 ext_len=`echo $line | cut -d: -f4`
7406                 if (( $frag_lun != $last_lun )); then
7407                         if (( tot_len != $EXPECTED_LEN )); then
7408                                 cleanup_130
7409                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7410                                 return
7411                         else
7412                                 (( num_luns += 1 ))
7413                                 tot_len=0
7414                         fi
7415                 fi
7416                 (( tot_len += ext_len ))
7417                 last_lun=$frag_lun
7418         done
7419         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7420                 cleanup_130
7421                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7422                 return
7423         fi
7424
7425         cleanup_130
7426
7427         echo "FIEMAP with continuation calls succeeded"
7428 }
7429 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7430
7431 # Test for writev/readv
7432 test_131a() {
7433         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7434         error "writev test failed"
7435         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7436         error "readv failed"
7437         rm -f $DIR/$tfile
7438 }
7439 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7440
7441 test_131b() {
7442         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7443         error "append writev test failed"
7444         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7445         error "append writev test failed"
7446         rm -f $DIR/$tfile
7447 }
7448 run_test 131b "test append writev"
7449
7450 test_131c() {
7451         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7452         error "NOT PASS"
7453 }
7454 run_test 131c "test read/write on file w/o objects"
7455
7456 test_131d() {
7457         rwv -f $DIR/$tfile -w -n 1 1572864
7458         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7459         if [ "$NOB" != 1572864 ]; then
7460                 error "Short read filed: read $NOB bytes instead of 1572864"
7461         fi
7462         rm -f $DIR/$tfile
7463 }
7464 run_test 131d "test short read"
7465
7466 test_131e() {
7467         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7468         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7469         error "read hitting hole failed"
7470         rm -f $DIR/$tfile
7471 }
7472 run_test 131e "test read hitting hole"
7473
7474 get_ost_param() {
7475         local token=$1
7476         local gl_sum=0
7477         for node in $(osts_nodes); do
7478                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7479                 [ x$gl = x"" ] && gl=0
7480                 gl_sum=$((gl_sum + gl))
7481         done
7482         echo $gl
7483 }
7484
7485 som_mode_switch() {
7486         local som=$1
7487         local gl1=$2
7488         local gl2=$3
7489
7490         if [ x$som = x"enabled" ]; then
7491                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7492                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7493                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7494         else
7495                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7496                 MOUNTOPT="$MOUNTOPT,som_preview"
7497                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7498         fi
7499
7500         # do remount to make new mount-conf parameters actual
7501         echo remounting...
7502         sync
7503         stopall
7504         setupall
7505 }
7506
7507 test_132() { #1028, SOM
7508         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7509         local num=$(get_mds_dir $DIR)
7510         local mymds=mds${num}
7511         local MOUNTOPT_SAVE=$MOUNTOPT
7512
7513         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7514         cancel_lru_locks osc
7515
7516         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7517
7518         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7519         stat $DIR/$tfile >/dev/null
7520         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7521         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7522         rm $DIR/$tfile
7523         som_mode_switch $som1 $gl1 $gl2
7524
7525         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7526         cancel_lru_locks osc
7527
7528         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7529         if [ $som1 == $som2 ]; then
7530             error "som is still "$som2
7531             if [ x$som2 = x"enabled" ]; then
7532                 som2="disabled"
7533             else
7534                 som2="enabled"
7535             fi
7536         fi
7537
7538         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7539         stat $DIR/$tfile >/dev/null
7540         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7541         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7542         som_mode_switch $som2 $gl1 $gl2
7543         MOUNTOPT=$MOUNTOPT_SAVE
7544 }
7545 run_test 132 "som avoids glimpse rpc"
7546
7547 check_stats() {
7548         local res
7549         local count
7550         case $1 in
7551         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7552                  ;;
7553         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7554                  ;;
7555         *) error "Wrong argument $1" ;;
7556         esac
7557         echo $res
7558         count=`echo $res | awk '{print $2}'`
7559         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7560         # if the argument $3 is zero, it means any stat increment is ok.
7561         if [ $3 -gt 0 ] ; then
7562                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7563         fi
7564 }
7565
7566 test_133a() {
7567         remote_ost_nodsh && skip "remote OST with nodsh" && return
7568         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7569
7570         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7571                 { skip "MDS doesn't support rename stats"; return; }
7572         local testdir=$DIR/${tdir}/stats_testdir
7573         mkdir -p $DIR/${tdir}
7574
7575         # clear stats.
7576         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7577         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7578
7579         # verify mdt stats first.
7580         mkdir ${testdir} || error "mkdir failed"
7581         check_stats $SINGLEMDS "mkdir" 1
7582         touch ${testdir}/${tfile} || "touch failed"
7583         check_stats $SINGLEMDS "open" 1
7584         check_stats $SINGLEMDS "close" 1
7585         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7586         check_stats $SINGLEMDS "mknod" 1
7587         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7588         check_stats $SINGLEMDS "unlink" 1
7589         rm -f ${testdir}/${tfile} || error "file remove failed"
7590         check_stats $SINGLEMDS "unlink" 2
7591
7592         # remove working dir and check mdt stats again.
7593         rmdir ${testdir} || error "rmdir failed"
7594         check_stats $SINGLEMDS "rmdir" 1
7595
7596         local testdir1=$DIR/${tdir}/stats_testdir1
7597         mkdir -p ${testdir}
7598         mkdir -p ${testdir1}
7599         touch ${testdir1}/test1
7600         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7601         check_stats $SINGLEMDS "crossdir_rename" 1
7602
7603         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7604         check_stats $SINGLEMDS "samedir_rename" 1
7605
7606         rm -rf $DIR/${tdir}
7607 }
7608 run_test 133a "Verifying MDT stats ========================================"
7609
7610 test_133b() {
7611         remote_ost_nodsh && skip "remote OST with nodsh" && return
7612         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7613         local testdir=$DIR/${tdir}/stats_testdir
7614         mkdir -p ${testdir} || error "mkdir failed"
7615         touch ${testdir}/${tfile} || "touch failed"
7616         cancel_lru_locks mdc
7617
7618         # clear stats.
7619         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7620         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7621
7622         # extra mdt stats verification.
7623         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7624         check_stats $SINGLEMDS "setattr" 1
7625         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7626         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
7627         then            # LU-1740
7628                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7629                 check_stats $SINGLEMDS "getattr" 1
7630         fi
7631         $LFS df || error "lfs failed"
7632         check_stats $SINGLEMDS "statfs" 1
7633
7634         rm -rf $DIR/${tdir}
7635 }
7636 run_test 133b "Verifying extra MDT stats =================================="
7637
7638 test_133c() {
7639         remote_ost_nodsh && skip "remote OST with nodsh" && return
7640         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7641         local testdir=$DIR/${tdir}/stats_testdir
7642         mkdir -p ${testdir} || error "mkdir failed"
7643
7644         # verify obdfilter stats.
7645         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7646         sync
7647         cancel_lru_locks osc
7648         wait_delete_completed
7649
7650         # clear stats.
7651         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7652         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7653
7654         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7655         sync
7656         cancel_lru_locks osc
7657         check_stats ost "write" 1
7658
7659         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7660         check_stats ost "read" 1
7661
7662         > ${testdir}/${tfile} || error "truncate failed"
7663         check_stats ost "punch" 1
7664
7665         rm -f ${testdir}/${tfile} || error "file remove failed"
7666         wait_delete_completed
7667         check_stats ost "destroy" 1
7668
7669         rm -rf $DIR/${tdir}
7670 }
7671 run_test 133c "Verifying OST stats ========================================"
7672
7673 order_2() {
7674     local value=$1
7675     local orig=$value
7676     local order=1
7677
7678     while [ $value -ge 2 ]; do
7679         order=$((order*2))
7680         value=$((value/2))
7681     done
7682
7683     if [ $orig -gt $order ]; then
7684         order=$((order*2))
7685     fi
7686     echo $order
7687 }
7688
7689 size_in_KMGT() {
7690     local value=$1
7691     local size=('K' 'M' 'G' 'T');
7692     local i=0
7693     local size_string=$value
7694
7695     while [ $value -ge 1024 ]; do
7696         if [ $i -gt 3 ]; then
7697             #T is the biggest unit we get here, if that is bigger,
7698             #just return XXXT
7699             size_string=${value}T
7700             break
7701         fi
7702         value=$((value >> 10))
7703         if [ $value -lt 1024 ]; then
7704             size_string=${value}${size[$i]}
7705             break
7706         fi
7707         i=$((i + 1))
7708     done
7709
7710     echo $size_string
7711 }
7712
7713 get_rename_size() {
7714     local size=$1
7715     local context=${2:-.}
7716     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
7717                 grep -A1 $context |
7718                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7719     echo $sample
7720 }
7721
7722 test_133d() {
7723     remote_ost_nodsh && skip "remote OST with nodsh" && return
7724     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7725     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7726         { skip "MDS doesn't support rename stats"; return; }
7727
7728     local testdir1=$DIR/${tdir}/stats_testdir1
7729     local testdir2=$DIR/${tdir}/stats_testdir2
7730
7731     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7732
7733     mkdir -p ${testdir1} || error "mkdir failed"
7734     mkdir -p ${testdir2} || error "mkdir failed"
7735
7736     createmany -o $testdir1/test 512 || error "createmany failed"
7737
7738         # check samedir rename size
7739         mv ${testdir1}/test0 ${testdir1}/test_0
7740
7741         local testdir1_size=$(ls -l $DIR/${tdir} |
7742                 awk '/stats_testdir1/ {print $5}')
7743         local testdir2_size=$(ls -l $DIR/${tdir} |
7744                 awk '/stats_testdir2/ {print $5}')
7745
7746         testdir1_size=$(order_2 $testdir1_size)
7747         testdir2_size=$(order_2 $testdir2_size)
7748
7749         testdir1_size=$(size_in_KMGT $testdir1_size)
7750         testdir2_size=$(size_in_KMGT $testdir2_size)
7751
7752         echo "source rename dir size: ${testdir1_size}"
7753         echo "target rename dir size: ${testdir2_size}"
7754
7755     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
7756     eval $cmd || error "$cmd failed"
7757     local samedir=$($cmd | grep 'same_dir')
7758     local same_sample=$(get_rename_size $testdir1_size)
7759     [ -z "$samedir" ] && error "samedir_rename_size count error"
7760     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
7761     echo "Check same dir rename stats success"
7762
7763     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7764
7765     # check crossdir rename size
7766     mv ${testdir1}/test_0 ${testdir2}/test_0
7767
7768         testdir1_size=$(ls -l $DIR/${tdir} |
7769                 awk '/stats_testdir1/ {print $5}')
7770         testdir2_size=$(ls -l $DIR/${tdir} |
7771                 awk '/stats_testdir2/ {print $5}')
7772
7773         testdir1_size=$(order_2 $testdir1_size)
7774         testdir2_size=$(order_2 $testdir2_size)
7775
7776         testdir1_size=$(size_in_KMGT $testdir1_size)
7777         testdir2_size=$(size_in_KMGT $testdir2_size)
7778
7779         echo "source rename dir size: ${testdir1_size}"
7780         echo "target rename dir size: ${testdir2_size}"
7781
7782     eval $cmd || error "$cmd failed"
7783     local crossdir=$($cmd | grep 'crossdir')
7784     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
7785     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
7786     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
7787     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
7788     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
7789     echo "Check cross dir rename stats success"
7790     rm -rf $DIR/${tdir}
7791 }
7792 run_test 133d "Verifying rename_stats ========================================"
7793
7794 test_140() { #bug-17379
7795         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7796         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7797         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7798
7799         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7800         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
7801         local i=0
7802         while i=`expr $i + 1`; do
7803                 mkdir -p $i || error "Creating dir $i"
7804                 cd $i || error "Changing to $i"
7805                 ln -s ../stat stat || error "Creating stat symlink"
7806                 # Read the symlink until ELOOP present,
7807                 # not LBUGing the system is considered success,
7808                 # we didn't overrun the stack.
7809                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7810                 [ $ret -ne 0 ] && {
7811                         if [ $ret -eq 40 ]; then
7812                                 break  # -ELOOP
7813                         else
7814                                 error "Open stat symlink"
7815                                 return
7816                         fi
7817                 }
7818         done
7819         i=`expr $i - 1`
7820         echo "The symlink depth = $i"
7821         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
7822                                         error "Invalid symlink depth"
7823
7824         # Test recursive symlink
7825         ln -s symlink_self symlink_self
7826         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
7827         echo "open symlink_self returns $ret"
7828         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
7829 }
7830 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7831
7832 test_150() {
7833         local TF="$TMP/$tfile"
7834
7835         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7836         cp $TF $DIR/$tfile
7837         cancel_lru_locks osc
7838         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7839         remount_client $MOUNT
7840         df -P $MOUNT
7841         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7842
7843         $TRUNCATE $TF 6000
7844         $TRUNCATE $DIR/$tfile 6000
7845         cancel_lru_locks osc
7846         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7847
7848         echo "12345" >>$TF
7849         echo "12345" >>$DIR/$tfile
7850         cancel_lru_locks osc
7851         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
7852
7853         echo "12345" >>$TF
7854         echo "12345" >>$DIR/$tfile
7855         cancel_lru_locks osc
7856         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
7857
7858         rm -f $TF
7859         true
7860 }
7861 run_test 150 "truncate/append tests"
7862
7863 function roc_hit() {
7864         local list=$(comma_list $(osts_nodes))
7865
7866         echo $(get_obdfilter_param $list '' stats |
7867                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
7868 }
7869
7870 function set_cache() {
7871         local on=1
7872
7873         if [ "$2" == "off" ]; then
7874                 on=0;
7875         fi
7876         local list=$(comma_list $(osts_nodes))
7877         set_obdfilter_param $list '' $1_cache_enable $on
7878
7879         cancel_lru_locks osc
7880 }
7881
7882 test_151() {
7883         remote_ost_nodsh && skip "remote OST with nodsh" && return
7884
7885         local CPAGES=3
7886         local list=$(comma_list $(osts_nodes))
7887
7888         # check whether obdfilter is cache capable at all
7889         if ! get_obdfilter_param $list '' read_cache_enable >/dev/null; then
7890                 echo "not cache-capable obdfilter"
7891                 return 0
7892         fi
7893
7894         # check cache is enabled on all obdfilters
7895         if get_obdfilter_param $list '' read_cache_enable | grep 0; then
7896                 echo "oss cache is disabled"
7897                 return 0
7898         fi
7899
7900         set_obdfilter_param $list '' writethrough_cache_enable 1
7901
7902         # pages should be in the case right after write
7903         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
7904         local BEFORE=`roc_hit`
7905         cancel_lru_locks osc
7906         cat $DIR/$tfile >/dev/null
7907         local AFTER=`roc_hit`
7908         if ! let "AFTER - BEFORE == CPAGES"; then
7909                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7910         fi
7911
7912         # the following read invalidates the cache
7913         cancel_lru_locks osc
7914         set_obdfilter_param $list '' read_cache_enable 0
7915         cat $DIR/$tfile >/dev/null
7916
7917         # now data shouldn't be found in the cache
7918         BEFORE=`roc_hit`
7919         cancel_lru_locks osc
7920         cat $DIR/$tfile >/dev/null
7921         AFTER=`roc_hit`
7922         if let "AFTER - BEFORE != 0"; then
7923                 error "IN CACHE: before: $BEFORE, after: $AFTER"
7924         fi
7925
7926         set_obdfilter_param $list '' read_cache_enable 1
7927         rm -f $DIR/$tfile
7928 }
7929 run_test 151 "test cache on oss and controls ==============================="
7930
7931 test_152() {
7932         local TF="$TMP/$tfile"
7933
7934         # simulate ENOMEM during write
7935 #define OBD_FAIL_OST_NOMEM      0x226
7936         lctl set_param fail_loc=0x80000226
7937         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7938         cp $TF $DIR/$tfile
7939         sync || error "sync failed"
7940         lctl set_param fail_loc=0
7941
7942         # discard client's cache
7943         cancel_lru_locks osc
7944
7945         # simulate ENOMEM during read
7946         lctl set_param fail_loc=0x80000226
7947         cmp $TF $DIR/$tfile || error "cmp failed"
7948         lctl set_param fail_loc=0
7949
7950         rm -f $TF
7951 }
7952 run_test 152 "test read/write with enomem ============================"
7953
7954 test_153() {
7955         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
7956 }
7957 run_test 153 "test if fdatasync does not crash ======================="
7958
7959 test_154() {
7960         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
7961                 { skip "Need MDS version at least 2.2.51"; return 0; }
7962
7963         cp /etc/hosts $DIR/$tfile
7964
7965         fid=$($LFS path2fid $DIR/$tfile)
7966         rc=$?
7967         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7968
7969         ffid=$DIR/.lustre/fid/$fid
7970
7971         echo "stat fid $fid"
7972         stat $ffid > /dev/null || error "stat $ffid failed."
7973         echo "touch fid $fid"
7974         touch $ffid || error "touch $ffid failed."
7975         echo "write to fid $fid"
7976         cat /etc/hosts > $ffid || error "write $ffid failed."
7977         echo "read fid $fid"
7978         diff /etc/hosts $ffid || error "read $ffid failed."
7979         echo "append write to fid $fid"
7980         cat /etc/hosts >> $ffid || error "append write $ffid failed."
7981         echo "rename fid $fid"
7982         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
7983         touch $DIR/$tfile.1
7984         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
7985         rm -f $DIR/$tfile.1
7986         echo "truncate fid $fid"
7987         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
7988         echo "link fid $fid"
7989         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
7990         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
7991                 echo "setfacl fid $fid"
7992                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
7993                 echo "getfacl fid $fid"
7994                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
7995         fi
7996         echo "unlink fid $fid"
7997         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
7998         echo "mknod fid $fid"
7999         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8000
8001         fid=[0xf00000400:0x1:0x0]
8002         ffid=$DIR/.lustre/fid/$fid
8003
8004         echo "stat non-exist fid $fid"
8005         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8006         echo "write to non-exist fid $fid"
8007         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8008         echo "link new fid $fid"
8009         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8010
8011         mkdir -p $DIR/$tdir
8012         touch $DIR/$tdir/$tfile
8013         fid=$($LFS path2fid $DIR/$tdir)
8014         rc=$?
8015         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8016
8017         ffid=$DIR/.lustre/fid/$fid
8018
8019         echo "ls $fid"
8020         ls $ffid > /dev/null || error "ls $ffid failed."
8021         echo "touch $fid/$tfile.1"
8022         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8023
8024         echo "touch $DIR/.lustre/fid/$tfile"
8025         touch $DIR/.lustre/fid/$tfile && \
8026                 error "touch $DIR/.lustre/fid/$tfile should fail."
8027
8028         echo "setxattr to $DIR/.lustre/fid"
8029         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8030                 error "setxattr should fail."
8031
8032         echo "listxattr for $DIR/.lustre/fid"
8033         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8034                 error "listxattr should fail."
8035
8036         echo "delxattr from $DIR/.lustre/fid"
8037         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8038                 error "delxattr should fail."
8039
8040         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8041         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8042                 error "touch invalid fid should fail."
8043
8044         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8045         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8046                 error "touch non-normal fid should fail."
8047
8048         echo "rename $tdir to $DIR/.lustre/fid"
8049         mrename $DIR/$tdir $DIR/.lustre/fid &&
8050                 error "rename to $DIR/.lustre/fid should fail."
8051
8052         echo "rename .lustre to itself"
8053         fid=$($LFS path2fid $DIR)
8054         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8055                 error "rename .lustre to itself should fail."
8056
8057         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8058         fid=$($LFS path2fid $DIR/$tfile-2)
8059         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8060         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8061                 error "create lov data thru .lustre should fail."
8062         echo "cp /etc/passwd $DIR/$tfile-2"
8063         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8064         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8065         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8066                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8067
8068         echo "Open-by-FID succeeded"
8069 }
8070 run_test 154 "Open-by-FID"
8071
8072 test_155_small_load() {
8073     local temp=$TMP/$tfile
8074     local file=$DIR/$tfile
8075
8076     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8077         error "dd of=$temp bs=6096 count=1 failed"
8078     cp $temp $file
8079     cancel_lru_locks osc
8080     cmp $temp $file || error "$temp $file differ"
8081
8082     $TRUNCATE $temp 6000
8083     $TRUNCATE $file 6000
8084     cmp $temp $file || error "$temp $file differ (truncate1)"
8085
8086     echo "12345" >>$temp
8087     echo "12345" >>$file
8088     cmp $temp $file || error "$temp $file differ (append1)"
8089
8090     echo "12345" >>$temp
8091     echo "12345" >>$file
8092     cmp $temp $file || error "$temp $file differ (append2)"
8093
8094     rm -f $temp $file
8095     true
8096 }
8097
8098 test_155_big_load() {
8099     remote_ost_nodsh && skip "remote OST with nodsh" && return
8100     local temp=$TMP/$tfile
8101     local file=$DIR/$tfile
8102
8103     free_min_max
8104     local cache_size=$(do_facet ost$((MAXI+1)) \
8105         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8106     local large_file_size=$((cache_size * 2))
8107
8108     echo "OSS cache size: $cache_size KB"
8109     echo "Large file size: $large_file_size KB"
8110
8111     [ $MAXV -le $large_file_size ] && \
8112         skip_env "max available OST size needs > $large_file_size KB" && \
8113         return 0
8114
8115     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8116
8117     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8118         error "dd of=$temp bs=$large_file_size count=1k failed"
8119     cp $temp $file
8120     ls -lh $temp $file
8121     cancel_lru_locks osc
8122     cmp $temp $file || error "$temp $file differ"
8123
8124     rm -f $temp $file
8125     true
8126 }
8127
8128 test_155a() {
8129     set_cache read on
8130     set_cache writethrough on
8131     test_155_small_load
8132 }
8133 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8134
8135 test_155b() {
8136     set_cache read on
8137     set_cache writethrough off
8138     test_155_small_load
8139 }
8140 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8141
8142 test_155c() {
8143     set_cache read off
8144     set_cache writethrough on
8145     test_155_small_load
8146 }
8147 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8148
8149 test_155d() {
8150     set_cache read off
8151     set_cache writethrough off
8152     test_155_small_load
8153 }
8154 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8155
8156 test_155e() {
8157     set_cache read on
8158     set_cache writethrough on
8159     test_155_big_load
8160 }
8161 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8162
8163 test_155f() {
8164     set_cache read on
8165     set_cache writethrough off
8166     test_155_big_load
8167 }
8168 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8169
8170 test_155g() {
8171     set_cache read off
8172     set_cache writethrough on
8173     test_155_big_load
8174 }
8175 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8176
8177 test_155h() {
8178     set_cache read off
8179     set_cache writethrough off
8180     test_155_big_load
8181 }
8182 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8183
8184 test_156() {
8185     local CPAGES=3
8186     local BEFORE
8187     local AFTER
8188     local file="$DIR/$tfile"
8189
8190         [ "$(facet_fstype ost1)" = "zfs" ] &&
8191                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8192                 return
8193
8194     log "Turn on read and write cache"
8195     set_cache read on
8196     set_cache writethrough on
8197
8198     log "Write data and read it back."
8199     log "Read should be satisfied from the cache."
8200     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8201     BEFORE=`roc_hit`
8202     cancel_lru_locks osc
8203     cat $file >/dev/null
8204     AFTER=`roc_hit`
8205     if ! let "AFTER - BEFORE == CPAGES"; then
8206         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8207     else
8208         log "cache hits:: before: $BEFORE, after: $AFTER"
8209     fi
8210
8211     log "Read again; it should be satisfied from the cache."
8212     BEFORE=$AFTER
8213     cancel_lru_locks osc
8214     cat $file >/dev/null
8215     AFTER=`roc_hit`
8216     if ! let "AFTER - BEFORE == CPAGES"; then
8217         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8218     else
8219         log "cache hits:: before: $BEFORE, after: $AFTER"
8220     fi
8221
8222
8223     log "Turn off the read cache and turn on the write cache"
8224     set_cache read off
8225     set_cache writethrough on
8226
8227     log "Read again; it should be satisfied from the cache."
8228     BEFORE=`roc_hit`
8229     cancel_lru_locks osc
8230     cat $file >/dev/null
8231     AFTER=`roc_hit`
8232     if ! let "AFTER - BEFORE == CPAGES"; then
8233         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8234     else
8235         log "cache hits:: before: $BEFORE, after: $AFTER"
8236     fi
8237
8238     log "Read again; it should not be satisfied from the cache."
8239     BEFORE=$AFTER
8240     cancel_lru_locks osc
8241     cat $file >/dev/null
8242     AFTER=`roc_hit`
8243     if ! let "AFTER - BEFORE == 0"; then
8244         error "IN CACHE: before: $BEFORE, after: $AFTER"
8245     else
8246         log "cache hits:: before: $BEFORE, after: $AFTER"
8247     fi
8248
8249     log "Write data and read it back."
8250     log "Read should be satisfied from the cache."
8251     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8252     BEFORE=`roc_hit`
8253     cancel_lru_locks osc
8254     cat $file >/dev/null
8255     AFTER=`roc_hit`
8256     if ! let "AFTER - BEFORE == CPAGES"; then
8257         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8258     else
8259         log "cache hits:: before: $BEFORE, after: $AFTER"
8260     fi
8261
8262     log "Read again; it should not be satisfied from the cache."
8263     BEFORE=$AFTER
8264     cancel_lru_locks osc
8265     cat $file >/dev/null
8266     AFTER=`roc_hit`
8267     if ! let "AFTER - BEFORE == 0"; then
8268         error "IN CACHE: before: $BEFORE, after: $AFTER"
8269     else
8270         log "cache hits:: before: $BEFORE, after: $AFTER"
8271     fi
8272
8273
8274     log "Turn off read and write cache"
8275     set_cache read off
8276     set_cache writethrough off
8277
8278     log "Write data and read it back"
8279     log "It should not be satisfied from the cache."
8280     rm -f $file
8281     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8282     cancel_lru_locks osc
8283     BEFORE=`roc_hit`
8284     cat $file >/dev/null
8285     AFTER=`roc_hit`
8286     if ! let "AFTER - BEFORE == 0"; then
8287         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8288     else
8289         log "cache hits:: before: $BEFORE, after: $AFTER"
8290     fi
8291
8292
8293     log "Turn on the read cache and turn off the write cache"
8294     set_cache read on
8295     set_cache writethrough off
8296
8297     log "Write data and read it back"
8298     log "It should not be satisfied from the cache."
8299     rm -f $file
8300     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8301     BEFORE=`roc_hit`
8302     cancel_lru_locks osc
8303     cat $file >/dev/null
8304     AFTER=`roc_hit`
8305     if ! let "AFTER - BEFORE == 0"; then
8306         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8307     else
8308         log "cache hits:: before: $BEFORE, after: $AFTER"
8309     fi
8310
8311     log "Read again; it should be satisfied from the cache."
8312     BEFORE=`roc_hit`
8313     cancel_lru_locks osc
8314     cat $file >/dev/null
8315     AFTER=`roc_hit`
8316     if ! let "AFTER - BEFORE == CPAGES"; then
8317         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8318     else
8319         log "cache hits:: before: $BEFORE, after: $AFTER"
8320     fi
8321
8322     rm -f $file
8323 }
8324 run_test 156 "Verification of tunables ============================"
8325
8326 #Changelogs
8327 err17935 () {
8328     if [ $MDSCOUNT -gt 1 ]; then
8329         error_ignore 17935 $*
8330     else
8331         error $*
8332     fi
8333 }
8334
8335 changelog_chmask()
8336 {
8337     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8338            grep -c $1)
8339
8340     if [ $MASK -eq 1 ]; then
8341         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8342     else
8343         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8344     fi
8345 }
8346
8347 test_160() {
8348     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8349     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8350         { skip "Need MDS version at least 2.2.0"; return; }
8351     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8352     echo "Registered as changelog user $USER"
8353     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8354         grep -q $USER || error "User $USER not found in changelog_users"
8355
8356     # change something
8357     mkdir -p $DIR/$tdir/pics/2008/zachy
8358     touch $DIR/$tdir/pics/2008/zachy/timestamp
8359     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8360     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8361     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8362     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8363     rm $DIR/$tdir/pics/desktop.jpg
8364
8365     $LFS changelog $MDT0 | tail -5
8366
8367     echo "verifying changelog mask"
8368     changelog_chmask "MKDIR"
8369     changelog_chmask "CLOSE"
8370
8371     mkdir -p $DIR/$tdir/pics/zach/sofia
8372     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8373
8374     changelog_chmask "MKDIR"
8375     changelog_chmask "CLOSE"
8376
8377     mkdir -p $DIR/$tdir/pics/2008/sofia
8378     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8379
8380     $LFS changelog $MDT0
8381     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8382     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8383     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8384     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8385
8386     # verify contents
8387     echo "verifying target fid"
8388     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8389         tail -1 | awk '{print $6}')
8390     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8391     [ "$fidc" == "t=$fidf" ] || \
8392         err17935 "fid in changelog $fidc != file fid $fidf"
8393     echo "verifying parent fid"
8394     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8395         tail -1 | awk '{print $7}')
8396     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8397     [ "$fidc" == "p=$fidf" ] || \
8398         err17935 "pfid in changelog $fidc != dir fid $fidf"
8399
8400     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8401         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8402     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8403     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8404         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8405     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8406     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8407         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8408
8409     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8410         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8411     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8412     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8413     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8414         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8415
8416     echo "verifying user deregister"
8417     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8418     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8419         grep -q $USER && error "User $USER still found in changelog_users"
8420
8421     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8422         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8423     if [ $USERS -eq 0 ]; then
8424         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8425             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8426         touch $DIR/$tdir/chloe
8427         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8428             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8429         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8430         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8431     else
8432         echo "$USERS other changelog users; can't verify off"
8433     fi
8434 }
8435 run_test 160 "changelog sanity"
8436
8437 test_161() {
8438     mkdir -p $DIR/$tdir
8439     cp /etc/hosts $DIR/$tdir/$tfile
8440     mkdir $DIR/$tdir/foo1
8441     mkdir $DIR/$tdir/foo2
8442     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8443     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8444     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8445     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8446     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8447     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8448         $LFS fid2path $DIR $FID
8449         err17935 "bad link ea"
8450     fi
8451     # middle
8452     rm $DIR/$tdir/foo2/zachary
8453     # last
8454     rm $DIR/$tdir/foo2/thor
8455     # first
8456     rm $DIR/$tdir/$tfile
8457     # rename
8458     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8459     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8460         then
8461         $LFS fid2path $DIR $FID
8462         err17935 "bad link rename"
8463     fi
8464     rm $DIR/$tdir/foo2/maggie
8465
8466     # overflow the EA
8467     local longname=filename_avg_len_is_thirty_two_
8468     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8469         error "failed to hardlink many files"
8470     links=$($LFS fid2path $DIR $FID | wc -l)
8471     echo -n "${links}/1000 links in link EA"
8472     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8473     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8474         error "failed to unlink many hardlinks"
8475 }
8476 run_test 161 "link ea sanity"
8477
8478 check_path() {
8479     local expected=$1
8480     shift
8481     local fid=$2
8482
8483     local path=$(${LFS} fid2path $*)
8484     RC=$?
8485
8486     if [ $RC -ne 0 ]; then
8487         err17935 "path looked up of $expected failed. Error $RC"
8488         return $RC
8489     elif [ "${path}" != "${expected}" ]; then
8490         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8491         return 2
8492     fi
8493     echo "fid $fid resolves to path $path (expected $expected)"
8494 }
8495
8496 test_162() {
8497         # Make changes to filesystem
8498         mkdir -p $DIR/$tdir/d2
8499         touch $DIR/$tdir/d2/$tfile
8500         touch $DIR/$tdir/d2/x1
8501         touch $DIR/$tdir/d2/x2
8502         mkdir -p $DIR/$tdir/d2/a/b/c
8503         mkdir -p $DIR/$tdir/d2/p/q/r
8504         # regular file
8505         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8506         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8507
8508         # softlink
8509         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8510         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8511         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8512
8513         # softlink to wrong file
8514         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8515         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8516         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8517
8518         # hardlink
8519         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8520         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8521         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8522         # fid2path dir/fsname should both work
8523         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8524         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8525
8526         # hardlink count: check that there are 2 links
8527         # Doesnt work with CMD yet: 17935
8528         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8529                 err17935 "expected 2 links"
8530
8531         # hardlink indexing: remove the first link
8532         rm $DIR/$tdir/d2/p/q/r/hlink
8533         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8534
8535         return 0
8536 }
8537 run_test 162 "path lookup sanity"
8538
8539 test_163() {
8540         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8541         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8542         copytool $FSNAME &
8543         sleep 1
8544         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8545         # this proc file is temporary and linux-only
8546         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8547          error "kernel->userspace send failed"
8548         kill -INT $!
8549 }
8550 run_test 163 "kernel <-> userspace comms"
8551
8552 test_169() {
8553         # do directio so as not to populate the page cache
8554         log "creating a 10 Mb file"
8555         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8556         log "starting reads"
8557         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8558         log "truncating the file"
8559         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8560         log "killing dd"
8561         kill %+ || true # reads might have finished
8562         echo "wait until dd is finished"
8563         wait
8564         log "removing the temporary file"
8565         rm -rf $DIR/$tfile || error "tmp file removal failed"
8566 }
8567 run_test 169 "parallel read and truncate should not deadlock"
8568
8569 test_170() {
8570         $LCTL clear     # bug 18514
8571         $LCTL debug_daemon start $TMP/${tfile}_log_good
8572         touch $DIR/$tfile
8573         $LCTL debug_daemon stop
8574         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8575                error "sed failed to read log_good"
8576
8577         $LCTL debug_daemon start $TMP/${tfile}_log_good
8578         rm -rf $DIR/$tfile
8579         $LCTL debug_daemon stop
8580
8581         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8582                error "lctl df log_bad failed"
8583
8584         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8585         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8586
8587         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8588         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8589
8590         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8591                 error "bad_line good_line1 good_line2 are empty"
8592
8593         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8594         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8595         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8596
8597         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8598         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8599         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8600
8601         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8602                 error "bad_line_new good_line_new are empty"
8603
8604         local expected_good=$((good_line1 + good_line2*2))
8605
8606         rm -f $TMP/${tfile}*
8607         # LU-231, short malformed line may not be counted into bad lines
8608         if [ $bad_line -ne $bad_line_new ] &&
8609                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8610                 error "expected $bad_line bad lines, but got $bad_line_new"
8611                 return 1
8612         fi
8613
8614         if [ $expected_good -ne $good_line_new ]; then
8615                 error "expected $expected_good good lines, but got $good_line_new"
8616                 return 2
8617         fi
8618         true
8619 }
8620 run_test 170 "test lctl df to handle corrupted log ====================="
8621
8622 test_171() { # bug20592
8623 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8624         $LCTL set_param fail_loc=0x50e
8625         $LCTL set_param fail_val=3000
8626         multiop_bg_pause $DIR/$tfile O_s || true
8627         local MULTIPID=$!
8628         kill -USR1 $MULTIPID
8629         # cause log dump
8630         sleep 3
8631         wait $MULTIPID
8632         if dmesg | grep "recursive fault"; then
8633                 error "caught a recursive fault"
8634         fi
8635         $LCTL set_param fail_loc=0
8636         true
8637 }
8638 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8639
8640 # it would be good to share it with obdfilter-survey/libecho code
8641 setup_obdecho_osc () {
8642         local rc=0
8643         local ost_nid=$1
8644         local obdfilter_name=$2
8645         echo "Creating new osc for $obdfilter_name on $ost_nid"
8646         # make sure we can find loopback nid
8647         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8648
8649         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8650                            ${obdfilter_name}_osc_UUID || rc=2; }
8651         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8652                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8653         return $rc
8654 }
8655
8656 cleanup_obdecho_osc () {
8657         local obdfilter_name=$1
8658         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8659         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8660         return 0
8661 }
8662
8663 obdecho_create_test() {
8664         local OBD=$1
8665         local node=$2
8666         local rc=0
8667         local id
8668         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8669         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8670                            rc=2; }
8671         if [ $rc -eq 0 ]; then
8672             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8673             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8674         fi
8675         echo "New object id is $id"
8676         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8677                            rc=4; }
8678         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8679                                         "cleanup" || rc=5; }
8680         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8681                                         "detach" || rc=6; }
8682         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8683         return $rc
8684 }
8685
8686 test_180a() {
8687         remote_ost_nodsh && skip "remote OST with nodsh" && return
8688         local rc=0
8689         local rmmod_local=0
8690
8691         if ! module_loaded obdecho; then
8692             load_module obdecho/obdecho
8693             rmmod_local=1
8694         fi
8695
8696         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8697         local host=$(lctl get_param -n osc.$osc.import |
8698                              awk '/current_connection:/ {print $2}' )
8699         local target=$(lctl get_param -n osc.$osc.import |
8700                              awk '/target:/ {print $2}' )
8701         target=${target%_UUID}
8702
8703         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8704         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8705         [[ -n $target ]] && cleanup_obdecho_osc $target
8706         [ $rmmod_local -eq 1 ] && rmmod obdecho
8707         return $rc
8708 }
8709 run_test 180a "test obdecho on osc"
8710
8711 test_180b() {
8712         remote_ost_nodsh && skip "remote OST with nodsh" && return
8713         local rc=0
8714         local rmmod_remote=0
8715
8716         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8717                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8718                       "modprobe obdecho; }" && rmmod_remote=1
8719         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8720         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8721         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8722         return $rc
8723 }
8724 run_test 180b "test obdecho directly on obdfilter"
8725
8726 test_181() { # bug 22177
8727         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8728         # create enough files to index the directory
8729         createmany -o $DIR/$tdir/foobar 4000
8730         # print attributes for debug purpose
8731         lsattr -d .
8732         # open dir
8733         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8734         MULTIPID=$!
8735         # remove the files & current working dir
8736         unlinkmany $DIR/$tdir/foobar 4000
8737         rmdir $DIR/$tdir
8738         kill -USR1 $MULTIPID
8739         wait $MULTIPID
8740         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
8741         return 0
8742 }
8743 run_test 181 "Test open-unlinked dir ========================"
8744
8745 test_182() {
8746         # disable MDC RPC lock wouldn't crash client
8747         local fcount=1000
8748         local tcount=4
8749
8750         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8751 #define OBD_FAIL_MDC_RPCS_SEM           0x804
8752         $LCTL set_param fail_loc=0x804
8753
8754         for (( i=0; i < $tcount; i++ )) ; do
8755                 mkdir $DIR/$tdir/$i
8756                 createmany -o $DIR/$tdir/$i/f- $fcount &
8757         done
8758         wait
8759
8760         for (( i=0; i < $tcount; i++ )) ; do
8761                 unlinkmany $DIR/$tdir/$i/f- $fcount &
8762         done
8763         wait
8764
8765         rm -rf $DIR/$tdir
8766
8767         $LCTL set_param fail_loc=0
8768 }
8769 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
8770
8771 test_183() { # LU-2275
8772         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8773         echo aaa > $DIR/$tdir/$tfile
8774
8775 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
8776         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
8777
8778         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
8779         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
8780
8781         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
8782
8783         # Flush negative dentry cache
8784         touch $DIR/$tdir/$tfile
8785
8786         # We are not checking for any leaked references here, they'll
8787         # become evident next time we do cleanup with module unload.
8788         rm -rf $DIR/$tdir
8789 }
8790 run_test 183 "No crash or request leak in case of strange dispositions ========"
8791
8792 # OST pools tests
8793 check_file_in_pool()
8794 {
8795         local file=$1
8796         local pool=$2
8797         local tlist="$3"
8798         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
8799         for i in $res
8800         do
8801                 for t in $tlist ; do
8802                         [ "$i" -eq "$t" ] && continue 2
8803                 done
8804
8805                 echo "pool list: $tlist"
8806                 echo "striping: $res"
8807                 error_noexit "$file not allocated in $pool"
8808                 return 1
8809         done
8810         return 0
8811 }
8812
8813 pool_add() {
8814         echo "Creating new pool"
8815         local pool=$1
8816
8817         create_pool $FSNAME.$pool ||
8818                 { error_noexit "No pool created, result code $?"; return 1; }
8819         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
8820                 { error_noexit "$pool not in lfs pool_list"; return 2; }
8821 }
8822
8823 pool_add_targets() {
8824         echo "Adding targets to pool"
8825         local pool=$1
8826         local first=$2
8827         local last=$3
8828         local step=${4:-1}
8829
8830         local list=$(seq $first $step $last)
8831
8832         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
8833         do_facet mgs $LCTL pool_add \
8834                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
8835         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
8836                         | sort -u | tr '\n' ' ' " "$t" || { 
8837                 error_noexit "Add to pool failed"
8838                 return 1
8839         }
8840         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
8841         local addcount=$(((last - first) / step + 1))
8842         [ $lfscount -eq $addcount ] || {
8843                 error_noexit "lfs pool_list bad ost count" \
8844                                                 "$lfscount != $addcount"
8845                 return 2
8846         }
8847 }
8848
8849 pool_set_dir() {
8850         local pool=$1
8851         local tdir=$2
8852         echo "Setting pool on directory $tdir"
8853
8854         $SETSTRIPE -c 2 -p $pool $tdir && return 0
8855
8856         error_noexit "Cannot set pool $pool to $tdir"
8857         return 1
8858 }
8859
8860 pool_check_dir() {
8861         local pool=$1
8862         local tdir=$2
8863         echo "Checking pool on directory $tdir"
8864
8865         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
8866         [ "$res" = "$pool" ] && return 0
8867
8868         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
8869         return 1
8870 }
8871
8872 pool_dir_rel_path() {
8873         echo "Testing relative path works well"
8874         local pool=$1
8875         local tdir=$2
8876         local root=$3
8877
8878         mkdir -p $root/$tdir/$tdir
8879         cd $root/$tdir
8880         pool_set_dir $pool $tdir          || return 1
8881         pool_set_dir $pool ./$tdir        || return 2
8882         pool_set_dir $pool ../$tdir       || return 3
8883         pool_set_dir $pool ../$tdir/$tdir || return 4
8884         rm -rf $tdir; cd - > /dev/null
8885 }
8886
8887 pool_alloc_files() {
8888         echo "Checking files allocation from directory pool"
8889         local pool=$1
8890         local tdir=$2
8891         local count=$3
8892         local tlist="$4"
8893
8894         local failed=0
8895         for i in $(seq -w 1 $count)
8896         do
8897                 local file=$tdir/file-$i
8898                 touch $file
8899                 check_file_in_pool $file $pool "$tlist" || \
8900                         failed=$((failed + 1))
8901         done
8902         [ "$failed" = 0 ] && return 0
8903
8904         error_noexit "$failed files not allocated in $pool"
8905         return 1
8906 }
8907
8908 pool_create_files() {
8909         echo "Creating files in pool"
8910         local pool=$1
8911         local tdir=$2
8912         local count=$3
8913         local tlist="$4"
8914
8915         mkdir -p $tdir
8916         local failed=0
8917         for i in $(seq -w 1 $count)
8918         do
8919                 local file=$tdir/spoo-$i
8920                 $SETSTRIPE -p $pool $file
8921                 check_file_in_pool $file $pool "$tlist" || \
8922                         failed=$((failed + 1))
8923         done
8924         [ "$failed" = 0 ] && return 0
8925
8926         error_noexit "$failed files not allocated in $pool"
8927         return 1
8928 }
8929
8930 pool_lfs_df() {
8931         echo "Checking 'lfs df' output"
8932         local pool=$1
8933
8934         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
8935                         tr '\n' ' ')
8936         local res=$($LFS df --pool $FSNAME.$pool |
8937                         awk '{print $1}' |
8938                         grep "$FSNAME-OST" |
8939                         tr '\n' ' ')
8940         [ "$res" = "$t" ] && return 0
8941
8942         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
8943         return 1
8944 }
8945
8946 pool_file_rel_path() {
8947         echo "Creating files in a pool with relative pathname"
8948         local pool=$1
8949         local tdir=$2
8950
8951         mkdir -p $tdir ||
8952                 { error_noexit "unable to create $tdir"; return 1 ; }
8953         local file="/..$tdir/$tfile-1"
8954         $SETSTRIPE -p $pool $file ||
8955                 { error_noexit "unable to create $file" ; return 2 ; }
8956
8957         cd $tdir
8958         $SETSTRIPE -p $pool $tfile-2 || {
8959                 error_noexit "unable to create $tfile-2 in $tdir"
8960                 return 3
8961         }
8962 }
8963
8964 pool_remove_first_target() {
8965         echo "Removing first target from a pool"
8966         local pool=$1
8967
8968         local pname="lov.$FSNAME-*.pools.$pool"
8969         local t=$($LCTL get_param -n $pname | head -1)
8970         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8971         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
8972                 error_noexit "$t not removed from $FSNAME.$pool"
8973                 return 1
8974         }
8975 }
8976
8977 pool_remove_all_targets() {
8978         echo "Removing all targets from pool"
8979         local pool=$1
8980         local file=$2
8981         local pname="lov.$FSNAME-*.pools.$pool"
8982         for t in $($LCTL get_param -n $pname | sort -u)
8983         do
8984                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8985         done
8986         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
8987                 error_noexit "Pool $FSNAME.$pool cannot be drained"
8988                 return 1
8989         }
8990         # striping on an empty/nonexistant pool should fall back 
8991         # to "pool of everything"
8992         touch $file || {
8993                 error_noexit "failed to use fallback striping for empty pool"
8994                 return 2
8995         }
8996         # setstripe on an empty pool should fail
8997         $SETSTRIPE -p $pool $file 2>/dev/null && {
8998                 error_noexit "expected failure when creating file" \
8999                                                         "with empty pool"
9000                 return 3
9001         }
9002         return 0
9003 }
9004
9005 pool_remove() {
9006         echo "Destroying pool"
9007         local pool=$1
9008         local file=$2
9009
9010         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9011
9012         sleep 2
9013         # striping on an empty/nonexistant pool should fall back 
9014         # to "pool of everything"
9015         touch $file || {
9016                 error_noexit "failed to use fallback striping for missing pool"
9017                 return 1
9018         }
9019         # setstripe on an empty pool should fail
9020         $SETSTRIPE -p $pool $file 2>/dev/null && {
9021                 error_noexit "expected failure when creating file" \
9022                                                         "with missing pool"
9023                 return 2
9024         }
9025
9026         # get param should return err once pool is gone
9027         if wait_update $HOSTNAME "lctl get_param -n \
9028                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9029         then
9030                 remove_pool_from_list $FSNAME.$pool
9031                 return 0
9032         fi
9033         error_noexit "Pool $FSNAME.$pool is not destroyed"
9034         return 3
9035 }
9036
9037 test_200() {
9038         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9039
9040         local POOL=${POOL:-cea1}
9041         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9042         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9043         # Pool OST targets
9044         local first_ost=0
9045         local last_ost=$(($OSTCOUNT - 1))
9046         local ost_step=2
9047         local ost_list=$(seq $first_ost $ost_step $last_ost)
9048         local ost_range="$first_ost $last_ost $ost_step"
9049         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9050         local file_dir=$POOL_ROOT/file_tst
9051
9052         local rc=0
9053         while : ; do
9054                 # former test_200a test_200b
9055                 pool_add $POOL                          || { rc=$? ; break; }
9056                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9057                 # former test_200c test_200d
9058                 mkdir -p $test_path
9059                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9060                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9061                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9062                                                         || { rc=$? ; break; }
9063                 # former test_200e test_200f
9064                 local files=$((OSTCOUNT*3))
9065                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9066                                                         || { rc=$? ; break; }
9067                 pool_create_files $POOL $file_dir $files "$ost_list" \
9068                                                         || { rc=$? ; break; }
9069                 # former test_200g test_200h
9070                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9071                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9072
9073                 # former test_201a test_201b test_201c
9074                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9075
9076                 local f=$test_path/$tfile
9077                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9078                 pool_remove $POOL $f                    || { rc=$? ; break; }
9079                 break
9080         done
9081
9082         cleanup_pools
9083         return $rc
9084 }
9085 run_test 200 "OST pools"
9086
9087 # usage: default_attr <count | size | offset>
9088 default_attr() {
9089         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9090 }
9091
9092 # usage: check_default_stripe_attr
9093 check_default_stripe_attr() {
9094         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9095         case $1 in
9096         --stripe-count|--count)
9097                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9098         --stripe-size|--size)
9099                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9100         --stripe-index|--index)
9101                 EXPECTED=-1;;
9102         *)
9103                 error "unknown getstripe attr '$1'"
9104         esac
9105
9106         [ $ACTUAL != $EXPECTED ] &&
9107                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9108 }
9109
9110 test_204a() {
9111         mkdir -p $DIR/$tdir
9112         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9113
9114         check_default_stripe_attr --stripe-count
9115         check_default_stripe_attr --stripe-size
9116         check_default_stripe_attr --stripe-index
9117
9118         return 0
9119 }
9120 run_test 204a "Print default stripe attributes ================="
9121
9122 test_204b() {
9123         mkdir -p $DIR/$tdir
9124         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9125
9126         check_default_stripe_attr --stripe-size
9127         check_default_stripe_attr --stripe-index
9128
9129         return 0
9130 }
9131 run_test 204b "Print default stripe size and offset  ==========="
9132
9133 test_204c() {
9134         mkdir -p $DIR/$tdir
9135         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9136
9137         check_default_stripe_attr --stripe-count
9138         check_default_stripe_attr --stripe-index
9139
9140         return 0
9141 }
9142 run_test 204c "Print default stripe count and offset ==========="
9143
9144 test_204d() {
9145         mkdir -p $DIR/$tdir
9146         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9147
9148         check_default_stripe_attr --stripe-count
9149         check_default_stripe_attr --stripe-size
9150
9151         return 0
9152 }
9153 run_test 204d "Print default stripe count and size ============="
9154
9155 test_204e() {
9156         mkdir -p $DIR/$tdir
9157         $SETSTRIPE -d $DIR/$tdir
9158
9159         check_default_stripe_attr --stripe-count --raw
9160         check_default_stripe_attr --stripe-size --raw
9161         check_default_stripe_attr --stripe-index --raw
9162
9163         return 0
9164 }
9165 run_test 204e "Print raw stripe attributes ================="
9166
9167 test_204f() {
9168         mkdir -p $DIR/$tdir
9169         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9170
9171         check_default_stripe_attr --stripe-size --raw
9172         check_default_stripe_attr --stripe-index --raw
9173
9174         return 0
9175 }
9176 run_test 204f "Print raw stripe size and offset  ==========="
9177
9178 test_204g() {
9179         mkdir -p $DIR/$tdir
9180         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9181
9182         check_default_stripe_attr --stripe-count --raw
9183         check_default_stripe_attr --stripe-index --raw
9184
9185         return 0
9186 }
9187 run_test 204g "Print raw stripe count and offset ==========="
9188
9189 test_204h() {
9190         mkdir -p $DIR/$tdir
9191         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9192
9193         check_default_stripe_attr --stripe-count --raw
9194         check_default_stripe_attr --stripe-size --raw
9195
9196         return 0
9197 }
9198 run_test 204h "Print raw stripe count and size ============="
9199
9200 # Figure out which job scheduler is being used, if any,
9201 # or use a fake one
9202 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9203         JOBENV=SLURM_JOB_ID
9204 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9205         JOBENV=LSB_JOBID
9206 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9207         JOBENV=PBS_JOBID
9208 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9209         JOBENV=LOADL_STEP_ID
9210 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9211         JOBENV=JOB_ID
9212 else
9213         JOBENV=FAKE_JOBID
9214 fi
9215
9216 verify_jobstats() {
9217         local cmd=$1
9218         local target=$2
9219
9220         # clear old jobstats
9221         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9222         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9223
9224         # use a new JobID for this test, or we might see an old one
9225         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9226
9227         JOBVAL=${!JOBENV}
9228         log "Test: $cmd"
9229         log "Using JobID environment variable $JOBENV=$JOBVAL"
9230
9231         if [ $JOBENV = "FAKE_JOBID" ]; then
9232                 FAKE_JOBID=$JOBVAL $cmd
9233         else
9234                 $cmd
9235         fi
9236
9237         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9238                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9239                 do_facet $FACET lctl get_param mdt.*.job_stats |
9240                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9241         fi
9242         if [ "$target" = "ost" -o "$target" = "both" ]; then
9243                 FACET=ost1
9244                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9245                         grep $JOBVAL || error "No job stats found on OST $FACET"
9246         fi
9247 }
9248
9249 test_205() { # Job stats
9250         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9251                 skip "Server doesn't support jobstats" && return 0
9252
9253         local cmd
9254         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9255         if [ $OLD_JOBENV != $JOBENV ]; then
9256                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9257                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9258                         $JOBENV || return 1
9259         fi
9260
9261         # mkdir
9262         cmd="mkdir $DIR/$tfile"
9263         verify_jobstats "$cmd" "mdt"
9264         # rmdir
9265         cmd="rm -fr $DIR/$tfile"
9266         verify_jobstats "$cmd" "mdt"
9267         # mknod
9268         cmd="mknod $DIR/$tfile c 1 3"
9269         verify_jobstats "$cmd" "mdt"
9270         # unlink
9271         cmd="rm -f $DIR/$tfile"
9272         verify_jobstats "$cmd" "mdt"
9273         # open & close
9274         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9275         verify_jobstats "$cmd" "mdt"
9276         # setattr
9277         cmd="touch $DIR/$tfile"
9278         verify_jobstats "$cmd" "both"
9279         # write
9280         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9281         verify_jobstats "$cmd" "ost"
9282         # read
9283         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9284         verify_jobstats "$cmd" "ost"
9285         # truncate
9286         cmd="$TRUNCATE $DIR/$tfile 0"
9287         verify_jobstats "$cmd" "both"
9288         # rename
9289         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9290         verify_jobstats "$cmd" "mdt"
9291
9292         # cleanup
9293         rm -f $DIR/jobstats_test_rename
9294
9295         if [ $OLD_JOBENV != $JOBENV ]; then
9296                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9297                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9298                         $OLD_JOBENV || return 1
9299         fi
9300 }
9301 run_test 205 "Verify job stats"
9302
9303 # LU-1480, LU-1773 and LU-1657
9304 test_206() {
9305         mkdir -p $DIR/$tdir
9306         lfs setstripe -c -1 $DIR/$tdir
9307 #define OBD_FAIL_LOV_INIT 0x1403
9308         $LCTL set_param fail_loc=0xa0001403
9309         $LCTL set_param fail_val=1
9310         touch $DIR/$tdir/$tfile || true
9311 }
9312 run_test 206 "fail lov_init_raid0() doesn't lbug"
9313
9314 test_212() {
9315         size=`date +%s`
9316         size=$((size % 8192 + 1))
9317         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9318         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9319         rm -f $DIR/f212 $DIR/f212.xyz
9320 }
9321 run_test 212 "Sendfile test ============================================"
9322
9323 test_213() {
9324         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9325         cancel_lru_locks osc
9326         lctl set_param fail_loc=0x8000040f
9327         # generate a read lock
9328         cat $DIR/$tfile > /dev/null
9329         # write to the file, it will try to cancel the above read lock.
9330         cat /etc/hosts >> $DIR/$tfile
9331 }
9332 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9333
9334 test_214() { # for bug 20133
9335         mkdir -p $DIR/d214p/d214c
9336         for (( i=0; i < 340; i++ )) ; do
9337                 touch $DIR/d214p/d214c/a$i
9338         done
9339
9340         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9341         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9342         ls $DIR/d214c || error "ls $DIR/d214c failed"
9343         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9344 }
9345 run_test 214 "hash-indexed directory test - bug 20133"
9346
9347 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9348 create_lnet_proc_files() {
9349         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9350         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9351
9352         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9353         rm -f "$TMP/lnet_$1.sys_tmp"
9354 }
9355
9356 # counterpart of create_lnet_proc_files
9357 remove_lnet_proc_files() {
9358         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9359 }
9360
9361 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9362 # 3rd arg as regexp for body
9363 check_lnet_proc_stats() {
9364         local l=$(cat "$TMP/lnet_$1" |wc -l)
9365         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9366
9367         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9368 }
9369
9370 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9371 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9372 # optional and can be regexp for 2nd line (lnet.routes case)
9373 check_lnet_proc_entry() {
9374         local blp=2            # blp stands for 'position of 1st line of body'
9375         [ "$5" = "" ] || blp=3 # lnet.routes case
9376
9377         local l=$(cat "$TMP/lnet_$1" |wc -l)
9378         # subtracting one from $blp because the body can be empty
9379         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9380
9381         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9382                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9383
9384         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9385                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9386
9387         # bail out if any unexpected line happened
9388         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9389         [ "$?" != 0 ] || error "$2 misformatted"
9390 }
9391
9392 test_215() { # for bugs 18102, 21079, 21517
9393         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9394         local P='[1-9][0-9]*'           # positive numeric
9395         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9396         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9397         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9398         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9399
9400         local L1 # regexp for 1st line
9401         local L2 # regexp for 2nd line (optional)
9402         local BR # regexp for the rest (body)
9403
9404         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9405         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9406         create_lnet_proc_files "stats"
9407         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9408         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9409         remove_lnet_proc_files "stats"
9410
9411         # /proc/sys/lnet/routes should look like this:
9412         # Routing disabled/enabled
9413         # net hops state router
9414         # where net is a string like tcp0, hops >= 0, state is up/down,
9415         # router is a string like 192.168.1.1@tcp2
9416         L1="^Routing (disabled|enabled)$"
9417         L2="^net +hops +state +router$"
9418         BR="^$NET +$N +(up|down) +$NID$"
9419         create_lnet_proc_files "routes"
9420         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9421         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9422         remove_lnet_proc_files "routes"
9423
9424         # /proc/sys/lnet/routers should look like this:
9425         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9426         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9427         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9428         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9429         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9430         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9431         create_lnet_proc_files "routers"
9432         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9433         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9434         remove_lnet_proc_files "routers"
9435
9436         # /proc/sys/lnet/peers should look like this:
9437         # nid refs state last max rtr min tx min queue
9438         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9439         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9440         # numeric (0 or >0 or <0), queue >= 0.
9441         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9442         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9443         create_lnet_proc_files "peers"
9444         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9445         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9446         remove_lnet_proc_files "peers"
9447
9448         # /proc/sys/lnet/buffers  should look like this:
9449         # pages count credits min
9450         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9451         L1="^pages +count +credits +min$"
9452         BR="^ +$N +$N +$I +$I$"
9453         create_lnet_proc_files "buffers"
9454         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9455         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9456         remove_lnet_proc_files "buffers"
9457
9458         # /proc/sys/lnet/nis should look like this:
9459         # nid status alive refs peer rtr max tx min
9460         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9461         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9462         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9463         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9464         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9465         create_lnet_proc_files "nis"
9466         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9467         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9468         remove_lnet_proc_files "nis"
9469
9470         # can we successfully write to /proc/sys/lnet/stats?
9471         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9472         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9473 }
9474 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9475
9476 test_216() { # bug 20317
9477         remote_ost_nodsh && skip "remote OST with nodsh" && return
9478         local node
9479         local p="$TMP/sanityN-$TESTNAME.parameters"
9480         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9481         for node in $(osts_nodes); do
9482                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9483                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9484                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9485         done
9486         clear_osc_stats
9487
9488         # agressive lockless i/o settings
9489         for node in $(osts_nodes); do
9490                 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'
9491         done
9492         lctl set_param -n osc.*.contention_seconds 60
9493
9494         $DIRECTIO write $DIR/$tfile 0 10 4096
9495         $CHECKSTAT -s 40960 $DIR/$tfile
9496
9497         # disable lockless i/o
9498         for node in $(osts_nodes); do
9499                 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'
9500         done
9501         lctl set_param -n osc.*.contention_seconds 0
9502         clear_osc_stats
9503
9504         dd if=/dev/zero of=$DIR/$tfile count=0
9505         $CHECKSTAT -s 0 $DIR/$tfile
9506
9507         restore_lustre_params <$p
9508         rm -f $p
9509         rm $DIR/$tfile
9510 }
9511 run_test 216 "check lockless direct write works and updates file size and kms correctly"
9512
9513 test_217() { # bug 22430
9514         local node
9515         local nid
9516
9517         for node in $(nodes_list); do
9518                 nid=$(host_nids_address $node $NETTYPE)
9519                 if [[ $nid = *-* ]] ; then
9520                         echo "lctl ping $nid@$NETTYPE"
9521                         lctl ping $nid@$NETTYPE
9522                 else
9523                         echo "skipping $node (no hyphen detected)"
9524                 fi
9525         done
9526 }
9527 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
9528
9529 test_218() {
9530        # do directio so as not to populate the page cache
9531        log "creating a 10 Mb file"
9532        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9533        log "starting reads"
9534        dd if=$DIR/$tfile of=/dev/null bs=4096 &
9535        log "truncating the file"
9536        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9537        log "killing dd"
9538        kill %+ || true # reads might have finished
9539        echo "wait until dd is finished"
9540        wait
9541        log "removing the temporary file"
9542        rm -rf $DIR/$tfile || error "tmp file removal failed"
9543 }
9544 run_test 218 "parallel read and truncate should not deadlock ======================="
9545
9546 test_219() {
9547         # write one partial page
9548         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
9549         # set no grant so vvp_io_commit_write will do sync write
9550         $LCTL set_param fail_loc=0x411
9551         # write a full page at the end of file
9552         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
9553
9554         $LCTL set_param fail_loc=0
9555         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
9556         $LCTL set_param fail_loc=0x411
9557         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
9558 }
9559 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
9560
9561 test_220() { #LU-325
9562         remote_ost_nodsh && skip "remote OST with nodsh" && return
9563         local OSTIDX=0
9564
9565         mkdir -p $DIR/$tdir
9566         local OST=$(lfs osts | grep ${OSTIDX}": " | \
9567                 awk '{print $2}' | sed -e 's/_UUID$//')
9568
9569         # on the mdt's osc
9570         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
9571         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
9572                         osc.$mdtosc_proc1.prealloc_last_id)
9573         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
9574                         osc.$mdtosc_proc1.prealloc_next_id)
9575
9576         $LFS df -i
9577
9578         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
9579         #define OBD_FAIL_OST_ENOINO              0x229
9580         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
9581         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
9582         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
9583
9584         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
9585
9586         MDSOBJS=$((last_id - next_id))
9587         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
9588
9589         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
9590         echo "OST still has $count kbytes free"
9591
9592         echo "create $MDSOBJS files @next_id..."
9593         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
9594
9595         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9596                         osc.$mdtosc_proc1.prealloc_last_id)
9597         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9598                         osc.$mdtosc_proc1.prealloc_next_id)
9599
9600         echo "after creation, last_id=$last_id2, next_id=$next_id2"
9601         $LFS df -i
9602
9603         echo "cleanup..."
9604
9605         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
9606         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
9607
9608         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
9609         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
9610         echo "unlink $MDSOBJS files @$next_id..."
9611         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
9612 }
9613 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
9614
9615 test_221() {
9616         dd if=`which date` of=$MOUNT/date oflag=sync
9617         chmod +x $MOUNT/date
9618
9619         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
9620         $LCTL set_param fail_loc=0x80001401
9621
9622         $MOUNT/date > /dev/null
9623         rm -f $MOUNT/date
9624 }
9625 run_test 221 "make sure fault and truncate race to not cause OOM"
9626
9627 test_222a () {
9628        rm -rf $DIR/$tdir
9629        mkdir -p $DIR/$tdir
9630        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9631        createmany -o $DIR/$tdir/$tfile 10
9632        cancel_lru_locks mdc
9633        cancel_lru_locks osc
9634        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9635        $LCTL set_param fail_loc=0x31a
9636        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
9637        $LCTL set_param fail_loc=0
9638        rm -r $DIR/$tdir
9639 }
9640 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
9641
9642 test_222b () {
9643        rm -rf $DIR/$tdir
9644        mkdir -p $DIR/$tdir
9645        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9646        createmany -o $DIR/$tdir/$tfile 10
9647        cancel_lru_locks mdc
9648        cancel_lru_locks osc
9649        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9650        $LCTL set_param fail_loc=0x31a
9651        rm -r $DIR/$tdir || "AGL for rmdir failed"
9652        $LCTL set_param fail_loc=0
9653 }
9654 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
9655
9656 test_223 () {
9657        rm -rf $DIR/$tdir
9658        mkdir -p $DIR/$tdir
9659        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9660        createmany -o $DIR/$tdir/$tfile 10
9661        cancel_lru_locks mdc
9662        cancel_lru_locks osc
9663        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
9664        $LCTL set_param fail_loc=0x31b
9665        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
9666        $LCTL set_param fail_loc=0
9667        rm -r $DIR/$tdir
9668 }
9669 run_test 223 "osc reenqueue if without AGL lock granted ======================="
9670
9671 test_224a() { # LU-1039, MRP-303
9672         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
9673         $LCTL set_param fail_loc=0x508
9674         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
9675         $LCTL set_param fail_loc=0
9676         df $DIR
9677 }
9678 run_test 224a "Don't panic on bulk IO failure"
9679
9680 test_224b() { # LU-1039, MRP-303
9681         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
9682         cancel_lru_locks osc
9683         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
9684         $LCTL set_param fail_loc=0x515
9685         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
9686         $LCTL set_param fail_loc=0
9687         df $DIR
9688 }
9689 run_test 224b "Don't panic on bulk IO failure"
9690
9691 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
9692 test_225a () {
9693        if [ -z ${MDSSURVEY} ]; then
9694               skip_env "mds-survey not found" && return
9695        fi
9696        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9697             { skip "Need MDS version at least 2.2.51"; return; }
9698
9699        local mds=$(facet_host $SINGLEMDS)
9700        local target=$(do_nodes $mds 'lctl dl' | \
9701                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9702
9703        local cmd1="file_count=1000 thrhi=4"
9704        local cmd2="dir_count=2 layer=mdd stripe_count=0"
9705        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9706        local cmd="$cmd1 $cmd2 $cmd3"
9707
9708        rm -f ${TMP}/mds_survey*
9709        echo + $cmd
9710        eval $cmd || error "mds-survey with zero-stripe failed"
9711        cat ${TMP}/mds_survey*
9712        rm -f ${TMP}/mds_survey*
9713 }
9714 run_test 225a "Metadata survey sanity with zero-stripe"
9715
9716 test_225b () {
9717        if [ -z ${MDSSURVEY} ]; then
9718               skip_env "mds-survey not found" && return
9719        fi
9720        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9721             { skip "Need MDS version at least 2.2.51"; return; }
9722
9723        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
9724               skip_env "Need to mount OST to test" && return
9725        fi
9726
9727        local mds=$(facet_host $SINGLEMDS)
9728        local target=$(do_nodes $mds 'lctl dl' | \
9729                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9730
9731        local cmd1="file_count=1000 thrhi=4"
9732        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9733        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9734        local cmd="$cmd1 $cmd2 $cmd3"
9735
9736        rm -f ${TMP}/mds_survey*
9737        echo + $cmd
9738        eval $cmd || error "mds-survey with stripe_count failed"
9739        cat ${TMP}/mds_survey*
9740        rm -f ${TMP}/mds_survey*
9741 }
9742 run_test 225b "Metadata survey sanity with stripe_count = 1"
9743
9744 mcreate_path2fid () {
9745         local mode=$1
9746         local major=$2
9747         local minor=$3
9748         local name=$4
9749         local desc=$5
9750         local path=$DIR/$tdir/$name
9751         local fid
9752         local rc
9753         local fid_path
9754
9755         $MCREATE --mode=$1 --major=$2 --minor=$3 $path || \
9756                 error "error: cannot create $desc"
9757
9758         fid=$($LFS path2fid $path)
9759         rc=$?
9760         [ $rc -ne 0 ] && error "error: cannot get fid of a $desc"
9761
9762         fid_path=$($LFS fid2path $DIR $fid)
9763         rc=$?
9764         [ $rc -ne 0 ] && error "error: cannot get path of a $desc by fid"
9765
9766         [ "$path" == "$fid_path" ] || \
9767                 error "error: fid2path returned \`$fid_path', expected \`$path'"
9768 }
9769
9770 test_226 () {
9771         rm -rf $DIR/$tdir
9772         mkdir -p $DIR/$tdir
9773
9774         mcreate_path2fid 0010666 0 0 fifo "FIFO"
9775         mcreate_path2fid 0020666 1 3 null "character special file (null)"
9776         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
9777         mcreate_path2fid 0040666 0 0 dir "directory"
9778         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
9779         mcreate_path2fid 0100666 0 0 file "regular file"
9780         mcreate_path2fid 0120666 0 0 link "symbolic link"
9781         mcreate_path2fid 0140666 0 0 sock "socket"
9782 }
9783 run_test 226 "call path2fid and fid2path on files of all type"
9784
9785 # LU-1299 Executing or running ldd on a truncated executable does not
9786 # cause an out-of-memory condition.
9787 test_227() {
9788         dd if=`which date` of=$MOUNT/date bs=1k count=1
9789         chmod +x $MOUNT/date
9790
9791         $MOUNT/date > /dev/null
9792         ldd $MOUNT/date > /dev/null
9793         rm -f $MOUNT/date
9794 }
9795 run_test 227 "running truncated executable does not cause OOM"
9796
9797 # LU-1512 try to reuse idle OI blocks
9798 test_228a() {
9799         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9800                 skip "non-ldiskfs backend" && return
9801
9802         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9803         local myDIR=$DIR/$tdir
9804
9805         mkdir -p $myDIR
9806         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9807         $LCTL set_param fail_loc=0x80001002
9808         createmany -o $myDIR/t- 10000
9809         $LCTL set_param fail_loc=0
9810         # The guard is current the largest FID holder
9811         touch $myDIR/guard
9812         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9813                     tr -d '[')
9814         local IDX=$(($SEQ % 64))
9815
9816         do_facet $SINGLEMDS sync
9817         # Make sure journal flushed.
9818         sleep 6
9819         local blk1=$(do_facet $SINGLEMDS \
9820                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9821                      grep Blockcount | awk '{print $4}')
9822
9823         # Remove old files, some OI blocks will become idle.
9824         unlinkmany $myDIR/t- 10000
9825         # Create new files, idle OI blocks should be reused.
9826         createmany -o $myDIR/t- 2000
9827         do_facet $SINGLEMDS sync
9828         # Make sure journal flushed.
9829         sleep 6
9830         local blk2=$(do_facet $SINGLEMDS \
9831                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9832                      grep Blockcount | awk '{print $4}')
9833
9834         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9835 }
9836 run_test 228a "try to reuse idle OI blocks"
9837
9838 test_228b() {
9839         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9840                 skip "non-ldiskfs backend" && return
9841
9842         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9843         local myDIR=$DIR/$tdir
9844
9845         mkdir -p $myDIR
9846         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9847         $LCTL set_param fail_loc=0x80001002
9848         createmany -o $myDIR/t- 10000
9849         $LCTL set_param fail_loc=0
9850         # The guard is current the largest FID holder
9851         touch $myDIR/guard
9852         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9853                     tr -d '[')
9854         local IDX=$(($SEQ % 64))
9855
9856         do_facet $SINGLEMDS sync
9857         # Make sure journal flushed.
9858         sleep 6
9859         local blk1=$(do_facet $SINGLEMDS \
9860                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9861                      grep Blockcount | awk '{print $4}')
9862
9863         # Remove old files, some OI blocks will become idle.
9864         unlinkmany $myDIR/t- 10000
9865
9866         # stop the MDT
9867         stop $SINGLEMDS || error "Fail to stop MDT."
9868         # remount the MDT
9869         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9870
9871         df $MOUNT || error "Fail to df."
9872         # Create new files, idle OI blocks should be reused.
9873         createmany -o $myDIR/t- 2000
9874         do_facet $SINGLEMDS sync
9875         # Make sure journal flushed.
9876         sleep 6
9877         local blk2=$(do_facet $SINGLEMDS \
9878                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9879                      grep Blockcount | awk '{print $4}')
9880
9881         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9882 }
9883 run_test 228b "idle OI blocks can be reused after MDT restart"
9884
9885 #LU-1881
9886 test_228c() {
9887         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9888                 skip "non-ldiskfs backend" && return
9889
9890         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9891         local myDIR=$DIR/$tdir
9892
9893         mkdir -p $myDIR
9894         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9895         $LCTL set_param fail_loc=0x80001002
9896         # 20000 files can guarantee there are index nodes in the OI file
9897         createmany -o $myDIR/t- 20000
9898         $LCTL set_param fail_loc=0
9899         # The guard is current the largest FID holder
9900         touch $myDIR/guard
9901         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9902                     tr -d '[')
9903         local IDX=$(($SEQ % 64))
9904
9905         do_facet $SINGLEMDS sync
9906         # Make sure journal flushed.
9907         sleep 6
9908         local blk1=$(do_facet $SINGLEMDS \
9909                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9910                      grep Blockcount | awk '{print $4}')
9911
9912         # Remove old files, some OI blocks will become idle.
9913         unlinkmany $myDIR/t- 20000
9914         rm -f $myDIR/guard
9915         # The OI file should become empty now
9916
9917         # Create new files, idle OI blocks should be reused.
9918         createmany -o $myDIR/t- 2000
9919         do_facet $SINGLEMDS sync
9920         # Make sure journal flushed.
9921         sleep 6
9922         local blk2=$(do_facet $SINGLEMDS \
9923                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9924                      grep Blockcount | awk '{print $4}')
9925
9926         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9927 }
9928 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
9929
9930 #
9931 # tests that do cleanup/setup should be run at the end
9932 #
9933
9934 test_900() {
9935         local ls
9936         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
9937         $LCTL set_param fail_loc=0x903
9938         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
9939         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
9940                 echo "clear" > $ls
9941         done
9942         FAIL_ON_ERROR=true cleanup
9943         FAIL_ON_ERROR=true setup
9944 }
9945 run_test 900 "umount should not race with any mgc requeue thread"
9946
9947 complete $SECONDS
9948 check_and_cleanup_lustre
9949 if [ "$I_MOUNTED" != "yes" ]; then
9950         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
9951 fi
9952 exit_status