Whamcloud - gitweb
LU-1187 tests: Add DNE test cases in sanity.
[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 check_fs_consistency_17n() {
590         local mdt_index
591         local devname
592         local cmd
593         local rc=0
594
595         for mdt_index in $(seq 1 $MDSCOUNT); do
596                 devname=$(mdsdevname $mdt_index)
597                 cmd="$E2FSCK -fnvd $devname"
598
599                 echo "stop and checking mds${mdt_index}: $cmd"
600                 # e2fsck should not return error
601                 stop mds${mdt_index}
602                 do_facet mds${mdt_index} $cmd || rc=$?
603
604                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
605                 df $MOUNT > /dev/null 2>&1
606                 [ $rc -ne 0 ] && break
607         done
608         return $rc
609 }
610
611 test_17n() {
612         local i
613
614         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
615         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
616                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
617
618         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
619                 skip "only for ldiskfs MDT" && return 0
620
621         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
622
623         mkdir -p $DIR/$tdir
624         for ((i=0; i<10; i++)); do
625                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
626                         error "create remote dir error $i"
627                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
628                         error "create files under remote dir failed $i"
629         done
630
631         check_fs_consistency_17n || error "e2fsck report error"
632
633         for ((i=0;i<10;i++)); do
634                 rm -rf $DIR/$tdir/remote_dir_${i} ||
635                         error "destroy remote dir error $i"
636         done
637
638         check_fs_consistency_17n || error "e2fsck report error"
639 }
640 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
641
642 test_18() {
643         touch $DIR/f
644         ls $DIR || error
645 }
646 run_test 18 "touch .../f ; ls ... =============================="
647
648 test_19a() {
649         touch $DIR/f19
650         ls -l $DIR
651         rm $DIR/f19
652         $CHECKSTAT -a $DIR/f19 || error
653 }
654 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
655
656 test_19b() {
657         ls -l $DIR/f19 && error || true
658 }
659 run_test 19b "ls -l .../f19 (should return error) =============="
660
661 test_19c() {
662         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
663         $RUNAS touch $DIR/f19 && error || true
664 }
665 run_test 19c "$RUNAS touch .../f19 (should return error) =="
666
667 test_19d() {
668         cat $DIR/f19 && error || true
669 }
670 run_test 19d "cat .../f19 (should return error) =============="
671
672 test_20() {
673         touch $DIR/f
674         rm $DIR/f
675         log "1 done"
676         touch $DIR/f
677         rm $DIR/f
678         log "2 done"
679         touch $DIR/f
680         rm $DIR/f
681         log "3 done"
682         $CHECKSTAT -a $DIR/f || error
683 }
684 run_test 20 "touch .../f ; ls -l ... ==========================="
685
686 test_21() {
687         mkdir $DIR/d21
688         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
689         ln -s dangle $DIR/d21/link
690         echo foo >> $DIR/d21/link
691         cat $DIR/d21/dangle
692         $CHECKSTAT -t link $DIR/d21/link || error
693         $CHECKSTAT -f -t file $DIR/d21/link || error
694 }
695 run_test 21 "write to dangling link ============================"
696
697 test_22() {
698         WDIR=$DIR/$tdir
699         mkdir -p $WDIR
700         chown $RUNAS_ID:$RUNAS_GID $WDIR
701         (cd $WDIR || error "cd $WDIR failed";
702         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
703         $RUNAS tar xf -)
704         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
705         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
706         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
707 }
708 run_test 22 "unpack tar archive as non-root user ==============="
709
710 # was test_23
711 test_23a() {
712         mkdir -p $DIR/$tdir
713         local file=$DIR/$tdir/$tfile
714
715         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
716         openfile -f O_CREAT:O_EXCL $file &&
717                 error "$file recreate succeeded" || true
718 }
719 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
720
721 test_23b() { # bug 18988
722         mkdir -p $DIR/$tdir
723         local file=$DIR/$tdir/$tfile
724
725         rm -f $file
726         echo foo > $file || error "write filed"
727         echo bar >> $file || error "append filed"
728         $CHECKSTAT -s 8 $file || error "wrong size"
729         rm $file
730 }
731 run_test 23b "O_APPEND check =========================="
732
733 test_24a() {
734         echo '== rename sanity =============================================='
735         echo '-- same directory rename'
736         mkdir $DIR/R1
737         touch $DIR/R1/f
738         mv $DIR/R1/f $DIR/R1/g
739         $CHECKSTAT -t file $DIR/R1/g || error
740 }
741 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
742
743 test_24b() {
744         mkdir $DIR/R2
745         touch $DIR/R2/{f,g}
746         mv $DIR/R2/f $DIR/R2/g
747         $CHECKSTAT -a $DIR/R2/f || error
748         $CHECKSTAT -t file $DIR/R2/g || error
749 }
750 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
751
752 test_24c() {
753         mkdir $DIR/R3
754         mkdir $DIR/R3/f
755         mv $DIR/R3/f $DIR/R3/g
756         $CHECKSTAT -a $DIR/R3/f || error
757         $CHECKSTAT -t dir $DIR/R3/g || error
758 }
759 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
760
761 test_24d() {
762         mkdir $DIR/R4
763         mkdir $DIR/R4/{f,g}
764         mrename $DIR/R4/f $DIR/R4/g
765         $CHECKSTAT -a $DIR/R4/f || error
766         $CHECKSTAT -t dir $DIR/R4/g || error
767 }
768 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
769
770 test_24e() {
771         echo '-- cross directory renames --'
772         mkdir $DIR/R5{a,b}
773         touch $DIR/R5a/f
774         mv $DIR/R5a/f $DIR/R5b/g
775         $CHECKSTAT -a $DIR/R5a/f || error
776         $CHECKSTAT -t file $DIR/R5b/g || error
777 }
778 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
779
780 test_24f() {
781         mkdir $DIR/R6{a,b}
782         touch $DIR/R6a/f $DIR/R6b/g
783         mv $DIR/R6a/f $DIR/R6b/g
784         $CHECKSTAT -a $DIR/R6a/f || error
785         $CHECKSTAT -t file $DIR/R6b/g || error
786 }
787 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
788
789 test_24g() {
790         mkdir $DIR/R7{a,b}
791         mkdir $DIR/R7a/d
792         mv $DIR/R7a/d $DIR/R7b/e
793         $CHECKSTAT -a $DIR/R7a/d || error
794         $CHECKSTAT -t dir $DIR/R7b/e || error
795 }
796 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
797
798 test_24h() {
799         mkdir $DIR/R8{a,b}
800         mkdir $DIR/R8a/d $DIR/R8b/e
801         mrename $DIR/R8a/d $DIR/R8b/e
802         $CHECKSTAT -a $DIR/R8a/d || error
803         $CHECKSTAT -t dir $DIR/R8b/e || error
804 }
805 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
806
807 test_24i() {
808         echo "-- rename error cases"
809         mkdir $DIR/R9
810         mkdir $DIR/R9/a
811         touch $DIR/R9/f
812         mrename $DIR/R9/f $DIR/R9/a
813         $CHECKSTAT -t file $DIR/R9/f || error
814         $CHECKSTAT -t dir  $DIR/R9/a || error
815         $CHECKSTAT -a $DIR/R9/a/f || error
816 }
817 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
818
819 test_24j() {
820         mkdir $DIR/R10
821         mrename $DIR/R10/f $DIR/R10/g
822         $CHECKSTAT -t dir $DIR/R10 || error
823         $CHECKSTAT -a $DIR/R10/f || error
824         $CHECKSTAT -a $DIR/R10/g || error
825 }
826 run_test 24j "source does not exist ============================"
827
828 test_24k() {
829         mkdir $DIR/R11a $DIR/R11a/d
830         touch $DIR/R11a/f
831         mv $DIR/R11a/f $DIR/R11a/d
832         $CHECKSTAT -a $DIR/R11a/f || error
833         $CHECKSTAT -t file $DIR/R11a/d/f || error
834 }
835 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
836
837 # bug 2429 - rename foo foo foo creates invalid file
838 test_24l() {
839         f="$DIR/f24l"
840         $MULTIOP $f OcNs || error
841 }
842 run_test 24l "Renaming a file to itself ========================"
843
844 test_24m() {
845         f="$DIR/f24m"
846         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
847         # on ext3 this does not remove either the source or target files
848         # though the "expected" operation would be to remove the source
849         $CHECKSTAT -t file ${f} || error "${f} missing"
850         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
851 }
852 run_test 24m "Renaming a file to a hard link to itself ========="
853
854 test_24n() {
855     f="$DIR/f24n"
856     # this stats the old file after it was renamed, so it should fail
857     touch ${f}
858     $CHECKSTAT ${f}
859     mv ${f} ${f}.rename
860     $CHECKSTAT ${f}.rename
861     $CHECKSTAT -a ${f}
862 }
863 run_test 24n "Statting the old file after renaming (Posix rename 2)"
864
865 test_24o() {
866         check_kernel_version 37 || return 0
867         mkdir -p $DIR/d24o
868         rename_many -s random -v -n 10 $DIR/d24o
869 }
870 run_test 24o "rename of files during htree split ==============="
871
872 test_24p() {
873         mkdir $DIR/R12{a,b}
874         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
875         mrename $DIR/R12a $DIR/R12b
876         $CHECKSTAT -a $DIR/R12a || error
877         $CHECKSTAT -t dir $DIR/R12b || error
878         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
879         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
880 }
881 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
882
883 test_24q() {
884         mkdir $DIR/R13{a,b}
885         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
886         multiop_bg_pause $DIR/R13b D_c || return 1
887         MULTIPID=$!
888
889         mrename $DIR/R13a $DIR/R13b
890         $CHECKSTAT -a $DIR/R13a || error
891         $CHECKSTAT -t dir $DIR/R13b || error
892         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
893         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
894         kill -USR1 $MULTIPID
895         wait $MULTIPID || error "multiop close failed"
896 }
897 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
898
899 test_24r() { #bug 3789
900         mkdir $DIR/R14a $DIR/R14a/b
901         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
902         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
903         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
904 }
905 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
906
907 test_24s() {
908         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
909         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
910         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
911         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
912 }
913 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
914 test_24t() {
915         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
916         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
917         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
918         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
919 }
920 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
921
922 test_24u() { # bug12192
923         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
924         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
925 }
926 run_test 24u "create stripe file"
927
928 page_size() {
929         getconf PAGE_SIZE
930 }
931
932 simple_cleanup_common() {
933         trap 0
934         rm -rf $DIR/$tdir
935         wait_delete_completed
936 }
937
938 test_24v() {
939         local NRFILES=100000
940         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
941         [ $FREE_INODES -lt $NRFILES ] && \
942                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
943                 return
944
945         trap simple_cleanup_common EXIT
946
947         mkdir -p $DIR/$tdir
948         createmany -m $DIR/$tdir/$tfile $NRFILES
949
950         cancel_lru_locks mdc
951         lctl set_param mdc.*.stats clear
952
953         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
954
955         # LU-5 large readdir
956         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
957         #               8 bytes for name(filename is mostly 5 in this test) +
958         #               8 bytes for luda_type
959         # take into account of overhead in lu_dirpage header and end mark in
960         # each page, plus one in RPC_NUM calculation.
961         DIRENT_SIZE=48
962         RPC_SIZE=$(($(lctl get_param -n mdc.*.max_pages_per_rpc)*$(page_size)))
963         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
964         mds_readpage=`lctl get_param mdc.*.stats | \
965                                 awk '/^mds_readpage/ {print $2}'`
966         [ $mds_readpage -gt $RPC_NUM ] && \
967                 error "large readdir doesn't take effect"
968
969         simple_cleanup_common
970 }
971 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
972
973 test_24w() { # bug21506
974         SZ1=234852
975         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
976         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
977         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
978         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
979         [ "$SZ1" = "$SZ2" ] || \
980                 error "Error reading at the end of the file $tfile"
981 }
982 run_test 24w "Reading a file larger than 4Gb"
983
984 test_24x() {
985         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
986         local MDTIDX=1
987         local remote_dir=$DIR/$tdir/remote_dir
988
989         mkdir -p $DIR/$tdir
990         $LFS mkdir -i $MDTIDX $remote_dir ||
991                 error "create remote directory failed"
992
993         mkdir -p $DIR/$tdir/src_dir
994         touch $DIR/$tdir/src_file
995         mkdir -p $remote_dir/tgt_dir
996         touch $remote_dir/tgt_file
997
998         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
999                 error "rename dir cross MDT works!"
1000
1001         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1002                 error "rename file cross MDT works!"
1003
1004         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1005                 error "ln file cross MDT should not work!"
1006
1007         rm -rf $DIR/$tdir || error "Can not delete directories"
1008 }
1009 run_test 24x "cross rename/link should be failed"
1010
1011 test_24y() {
1012         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1013         local MDTIDX=1
1014         local remote_dir=$DIR/$tdir/remote_dir
1015
1016         mkdir -p $DIR/$tdir
1017         $LFS mkdir -i $MDTIDX $remote_dir ||
1018                    error "create remote directory failed"
1019
1020         mkdir -p $remote_dir/src_dir
1021         touch $remote_dir/src_file
1022         mkdir -p $remote_dir/tgt_dir
1023         touch $remote_dir/tgt_file
1024
1025         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1026                 error "rename subdir in the same remote dir failed!"
1027
1028         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1029                 error "rename files in the same remote dir failed!"
1030
1031         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1032                 error "link files in the same remote dir failed!"
1033
1034         rm -rf $DIR/$tdir || error "Can not delete directories"
1035 }
1036 run_test 24y "rename/link on the same dir should succeed"
1037
1038 test_24z() {
1039         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1040         local MDTIDX=1
1041         local remote_src=$DIR/$tdir/remote_dir
1042         local remote_tgt=$DIR/$tdir/remote_tgt
1043
1044         mkdir -p $DIR/$tdir
1045         $LFS mkdir -i $MDTIDX $remote_src ||
1046                    error "create remote directory failed"
1047
1048         $LFS mkdir -i $MDTIDX $remote_tgt ||
1049                    error "create remote directory failed"
1050
1051         mrename $remote_src $remote_tgt &&
1052                 error "rename remote dirs should not work!"
1053
1054         rm -rf $DIR/$tdir || error "Can not delete directories"
1055 }
1056 run_test 24z "rename one remote dir to another remote dir should fail"
1057
1058 test_25a() {
1059         echo '== symlink sanity ============================================='
1060
1061         mkdir $DIR/d25
1062         ln -s d25 $DIR/s25
1063         touch $DIR/s25/foo || error
1064 }
1065 run_test 25a "create file in symlinked directory ==============="
1066
1067 test_25b() {
1068         [ ! -d $DIR/d25 ] && test_25a
1069         $CHECKSTAT -t file $DIR/s25/foo || error
1070 }
1071 run_test 25b "lookup file in symlinked directory ==============="
1072
1073 test_26a() {
1074         mkdir $DIR/d26
1075         mkdir $DIR/d26/d26-2
1076         ln -s d26/d26-2 $DIR/s26
1077         touch $DIR/s26/foo || error
1078 }
1079 run_test 26a "multiple component symlink ======================="
1080
1081 test_26b() {
1082         mkdir -p $DIR/d26b/d26-2
1083         ln -s d26b/d26-2/foo $DIR/s26-2
1084         touch $DIR/s26-2 || error
1085 }
1086 run_test 26b "multiple component symlink at end of lookup ======"
1087
1088 test_26c() {
1089         mkdir $DIR/d26.2
1090         touch $DIR/d26.2/foo
1091         ln -s d26.2 $DIR/s26.2-1
1092         ln -s s26.2-1 $DIR/s26.2-2
1093         ln -s s26.2-2 $DIR/s26.2-3
1094         chmod 0666 $DIR/s26.2-3/foo
1095 }
1096 run_test 26c "chain of symlinks ================================"
1097
1098 # recursive symlinks (bug 439)
1099 test_26d() {
1100         ln -s d26-3/foo $DIR/d26-3
1101 }
1102 run_test 26d "create multiple component recursive symlink ======"
1103
1104 test_26e() {
1105         [ ! -h $DIR/d26-3 ] && test_26d
1106         rm $DIR/d26-3
1107 }
1108 run_test 26e "unlink multiple component recursive symlink ======"
1109
1110 # recursive symlinks (bug 7022)
1111 test_26f() {
1112         mkdir -p $DIR/$tdir
1113         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
1114         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1115         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1116         mkdir $tfile             || error "mkdir $tfile failed"
1117         cd $tfile                || error "cd $tfile failed"
1118         ln -s .. dotdot          || error "ln dotdot failed"
1119         ln -s dotdot/lndir lndir || error "ln lndir failed"
1120         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1121         output=`ls $tfile/$tfile/lndir/bar1`
1122         [ "$output" = bar1 ] && error "unexpected output"
1123         rm -r $tfile             || error "rm $tfile failed"
1124         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1125 }
1126 run_test 26f "rm -r of a directory which has recursive symlink ="
1127
1128 test_27a() {
1129         echo '== stripe sanity =============================================='
1130         mkdir -p $DIR/d27 || error "mkdir failed"
1131         $GETSTRIPE $DIR/d27
1132         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1133         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1134         pass
1135         log "== test_27a: write to one stripe file ========================="
1136         cp /etc/hosts $DIR/d27/f0 || error
1137 }
1138 run_test 27a "one stripe file =================================="
1139
1140 test_27b() {
1141         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1142         mkdir -p $DIR/d27
1143         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1144         $GETSTRIPE -c $DIR/d27/f01
1145         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1146                 error "two-stripe file doesn't have two stripes"
1147 }
1148 run_test 27b "create two stripe file"
1149
1150 test_27c() {
1151         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1152
1153         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1154 }
1155 run_test 27c "write to two stripe file"
1156
1157 test_27d() {
1158         mkdir -p $DIR/d27
1159         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1160         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1161         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1162 }
1163 run_test 27d "create file with default settings ================"
1164
1165 test_27e() {
1166         mkdir -p $DIR/d27
1167         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1168         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1169         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1170 }
1171 run_test 27e "setstripe existing file (should return error) ======"
1172
1173 test_27f() {
1174         mkdir -p $DIR/d27
1175         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1176         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1177         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1178 }
1179 run_test 27f "setstripe with bad stripe size (should return error)"
1180
1181 test_27g() {
1182         mkdir -p $DIR/d27
1183         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1184         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1185                 error "$DIR/d27/fnone has object"
1186 }
1187 run_test 27g "$GETSTRIPE with no objects"
1188
1189 test_27i() {
1190         touch $DIR/d27/fsome || error "touch failed"
1191         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1192 }
1193 run_test 27i "$GETSTRIPE with some objects"
1194
1195 test_27j() {
1196         mkdir -p $DIR/d27
1197         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1198 }
1199 run_test 27j "setstripe with bad stripe offset (should return error)"
1200
1201 test_27k() { # bug 2844
1202         mkdir -p $DIR/d27
1203         FILE=$DIR/d27/f27k
1204         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1205         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
1206         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1207         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1208         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1209         dd if=/dev/zero of=$FILE bs=4k count=1
1210         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1211         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1212 }
1213 run_test 27k "limit i_blksize for broken user apps ============="
1214
1215 test_27l() {
1216         mkdir -p $DIR/d27
1217         mcreate $DIR/f27l || error "creating file"
1218         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1219                 error "setstripe should have failed" || true
1220 }
1221 run_test 27l "check setstripe permissions (should return error)"
1222
1223 test_27m() {
1224         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1225                 return
1226         if [ $ORIGFREE -gt $MAXFREE ]; then
1227                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1228                 return
1229         fi
1230         trap simple_cleanup_common EXIT
1231         mkdir -p $DIR/$tdir
1232         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1233         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1234                 error "dd should fill OST0"
1235         i=2
1236         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1237                 i=`expr $i + 1`
1238                 [ $i -gt 256 ] && break
1239         done
1240         i=`expr $i + 1`
1241         touch $DIR/$tdir/f27m_$i
1242         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1243                 error "OST0 was full but new created file still use it"
1244         i=`expr $i + 1`
1245         touch $DIR/$tdir/f27m_$i
1246         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1247                 error "OST0 was full but new created file still use it"
1248         simple_cleanup_common
1249 }
1250 run_test 27m "create file while OST0 was full =================="
1251
1252 sleep_maxage() {
1253         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1254         sleep $DELAY
1255 }
1256
1257 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1258 # if the OST isn't full anymore.
1259 reset_enospc() {
1260         local OSTIDX=${1:-""}
1261
1262         local list=$(comma_list $(osts_nodes))
1263         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1264
1265         do_nodes $list lctl set_param fail_loc=0
1266         sync    # initiate all OST_DESTROYs from MDS to OST
1267         sleep_maxage
1268 }
1269
1270 exhaust_precreations() {
1271         local OSTIDX=$1
1272         local FAILLOC=$2
1273         local FAILIDX=${3:-$OSTIDX}
1274
1275         mkdir -p $DIR/$tdir
1276         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1277         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1278
1279         local OST=$(ostname_from_index $OSTIDX)
1280         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1281                           sed -e 's/_UUID$//;s/^.*-//')
1282
1283         # on the mdt's osc
1284         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1285         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1286         osc.$mdtosc_proc1.prealloc_last_id)
1287         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1288         osc.$mdtosc_proc1.prealloc_next_id)
1289
1290         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1291         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1292
1293         mkdir -p $DIR/$tdir/${OST}
1294         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1295 #define OBD_FAIL_OST_ENOSPC              0x215
1296         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1297         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1298         echo "Creating to objid $last_id on ost $OST..."
1299         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1300         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1301         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1302         sleep_maxage
1303 }
1304
1305 exhaust_all_precreations() {
1306         local i
1307         for (( i=0; i < OSTCOUNT; i++ )) ; do
1308                 exhaust_precreations $i $1 -1
1309         done
1310 }
1311
1312 test_27n() {
1313         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1314         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1315         remote_ost_nodsh && skip "remote OST with nodsh" && return
1316
1317         reset_enospc
1318         rm -f $DIR/$tdir/$tfile
1319         exhaust_precreations 0 0x80000215
1320         $SETSTRIPE -c -1 $DIR/$tdir
1321         touch $DIR/$tdir/$tfile || error
1322         $GETSTRIPE $DIR/$tdir/$tfile
1323         reset_enospc
1324 }
1325 run_test 27n "create file with some full OSTs =================="
1326
1327 test_27o() {
1328         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1329         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1330         remote_ost_nodsh && skip "remote OST with nodsh" && return
1331
1332         reset_enospc
1333         rm -f $DIR/$tdir/$tfile
1334         exhaust_all_precreations 0x215
1335
1336         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1337
1338         reset_enospc
1339         rm -rf $DIR/$tdir/*
1340 }
1341 run_test 27o "create file with all full OSTs (should error) ===="
1342
1343 test_27p() {
1344         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1345         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1346         remote_ost_nodsh && skip "remote OST with nodsh" && return
1347
1348         reset_enospc
1349         rm -f $DIR/$tdir/$tfile
1350         mkdir -p $DIR/$tdir
1351
1352         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1353         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1354         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1355
1356         exhaust_precreations 0 0x80000215
1357         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1358         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1359         $GETSTRIPE $DIR/$tdir/$tfile
1360
1361         reset_enospc
1362 }
1363 run_test 27p "append to a truncated file with some full OSTs ==="
1364
1365 test_27q() {
1366         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1367         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1368         remote_ost_nodsh && skip "remote OST with nodsh" && return
1369
1370         reset_enospc
1371         rm -f $DIR/$tdir/$tfile
1372
1373         mkdir -p $DIR/$tdir
1374         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1375         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1376         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1377
1378         exhaust_all_precreations 0x215
1379
1380         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1381         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1382
1383         reset_enospc
1384 }
1385 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1386
1387 test_27r() {
1388         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1389         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1390         remote_ost_nodsh && skip "remote OST with nodsh" && return
1391
1392         reset_enospc
1393         rm -f $DIR/$tdir/$tfile
1394         exhaust_precreations 0 0x80000215
1395
1396         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1397
1398         reset_enospc
1399 }
1400 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1401
1402 test_27s() { # bug 10725
1403         mkdir -p $DIR/$tdir
1404         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1405         local stripe_count=0
1406         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1407         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1408                 error "stripe width >= 2^32 succeeded" || true
1409
1410 }
1411 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1412
1413 test_27t() { # bug 10864
1414         WDIR=`pwd`
1415         WLFS=`which lfs`
1416         cd $DIR
1417         touch $tfile
1418         $WLFS getstripe $tfile
1419         cd $WDIR
1420 }
1421 run_test 27t "check that utils parse path correctly"
1422
1423 test_27u() { # bug 4900
1424         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1425         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1426
1427 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1428         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1429         mkdir -p $DIR/$tdir
1430         createmany -o $DIR/$tdir/t- 1000
1431         do_facet $SINGLEMDS lctl set_param fail_loc=0
1432
1433         TLOG=$DIR/$tfile.getstripe
1434         $GETSTRIPE $DIR/$tdir > $TLOG
1435         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1436         unlinkmany $DIR/$tdir/t- 1000
1437         [ $OBJS -gt 0 ] && \
1438                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1439 }
1440 run_test 27u "skip object creation on OSC w/o objects =========="
1441
1442 test_27v() { # bug 4900
1443         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1444         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1445         remote_ost_nodsh && skip "remote OST with nodsh" && return
1446
1447         exhaust_all_precreations 0x215
1448         reset_enospc
1449
1450         mkdir -p $DIR/$tdir
1451         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1452
1453         touch $DIR/$tdir/$tfile
1454         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1455         # all except ost1
1456         for (( i=1; i < OSTCOUNT; i++ )); do
1457                 do_facet ost$i lctl set_param fail_loc=0x705
1458         done
1459         local START=`date +%s`
1460         createmany -o $DIR/$tdir/$tfile 32
1461
1462         local FINISH=`date +%s`
1463         local TIMEOUT=`lctl get_param -n timeout`
1464         local PROCESS=$((FINISH - START))
1465         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1466                error "$FINISH - $START >= $TIMEOUT / 2"
1467         sleep $((TIMEOUT / 2 - PROCESS))
1468         reset_enospc
1469 }
1470 run_test 27v "skip object creation on slow OST ================="
1471
1472 test_27w() { # bug 10997
1473         mkdir -p $DIR/$tdir || error "mkdir failed"
1474         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1475         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1476                 error "stripe size $size != 65536" || true
1477         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1478                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1479 }
1480 run_test 27w "check $SETSTRIPE -S option"
1481
1482 test_27wa() {
1483         [ "$OSTCOUNT" -lt "2" ] &&
1484                 skip_env "skipping multiple stripe count/offset test" && return
1485
1486         mkdir -p $DIR/$tdir || error "mkdir failed"
1487         for i in $(seq 1 $OSTCOUNT); do
1488                 offset=$((i - 1))
1489                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1490                         error "setstripe -c $i -i $offset failed"
1491                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1492                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1493                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1494                 [ $index -ne $offset ] &&
1495                         error "stripe offset $index != $offset" || true
1496         done
1497 }
1498 run_test 27wa "check $SETSTRIPE -c -i options"
1499
1500 test_27x() {
1501         remote_ost_nodsh && skip "remote OST with nodsh" && return
1502         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1503         OFFSET=$(($OSTCOUNT - 1))
1504         OSTIDX=0
1505         local OST=$(ostname_from_index $OSTIDX)
1506
1507         mkdir -p $DIR/$tdir
1508         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1509         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1510         sleep_maxage
1511         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1512         for i in `seq 0 $OFFSET`; do
1513                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1514                 error "OST0 was degraded but new created file still use it"
1515         done
1516         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1517 }
1518 run_test 27x "create files while OST0 is degraded"
1519
1520 test_27y() {
1521         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1522         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1523         remote_ost_nodsh && skip "remote OST with nodsh" && return
1524
1525         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1526         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1527             osc.$mdtosc.prealloc_last_id)
1528         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1529             osc.$mdtosc.prealloc_next_id)
1530         local fcount=$((last_id - next_id))
1531         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1532         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1533
1534         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1535                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1536         local OST_DEACTIVE_IDX=-1
1537         local OSC
1538         local OSTIDX
1539         local OST
1540
1541         for OSC in $MDS_OSCS; do
1542                 OST=$(osc_to_ost $OSC)
1543                 OSTIDX=$(index_from_ostuuid $OST)
1544                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1545                         OST_DEACTIVE_IDX=$OSTIDX
1546                 fi
1547                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1548                         echo $OSC "is Deactivated:"
1549                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1550                 fi
1551         done
1552
1553         OSTIDX=$(index_from_ostuuid $OST)
1554         mkdir -p $DIR/$tdir
1555         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1556
1557         for OSC in $MDS_OSCS; do
1558                 OST=$(osc_to_ost $OSC)
1559                 OSTIDX=$(index_from_ostuuid $OST)
1560                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1561                         echo $OST "is degraded:"
1562                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1563                                                 obdfilter.$OST.degraded=1
1564                 fi
1565         done
1566
1567         sleep_maxage
1568         createmany -o $DIR/$tdir/$tfile $fcount
1569
1570         for OSC in $MDS_OSCS; do
1571                 OST=$(osc_to_ost $OSC)
1572                 OSTIDX=$(index_from_ostuuid $OST)
1573                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1574                         echo $OST "is recovered from degraded:"
1575                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1576                                                 obdfilter.$OST.degraded=0
1577                 else
1578                         do_facet $SINGLEMDS lctl --device %$OSC activate
1579                 fi
1580         done
1581 }
1582 run_test 27y "create files while OST0 is degraded and the rest inactive"
1583
1584 check_seq_oid()
1585 {
1586         log "check file $1"
1587
1588         lmm_count=$($GETSTRIPE -c $1)
1589         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1590         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1591
1592         local old_ifs="$IFS"
1593         IFS=$'[:]'
1594         fid=($($LFS path2fid $1))
1595         IFS="$old_ifs"
1596
1597         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1598         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1599
1600         # compare lmm_seq and lu_fid->f_seq
1601         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1602         # compare lmm_object_id and lu_fid->oid
1603         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1604
1605         # check the trusted.fid attribute of the OST objects of the file
1606         local have_obdidx=false
1607         local stripe_nr=0
1608         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1609                 # skip lines up to and including "obdidx"
1610                 [ -z "$obdidx" ] && break
1611                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1612                 $have_obdidx || continue
1613
1614                 local ost=$((obdidx + 1))
1615                 local dev=$(ostdevname $ost)
1616
1617                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1618                         echo "Currently only works with ldiskfs-based OSTs"
1619                         continue
1620                 fi
1621
1622                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1623
1624                 #don't unmount/remount the OSTs if we don't need to do that
1625                 #local dir=$(facet_mntpt ost$ost)
1626                 #stop ost$dev
1627                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1628                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1629                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1630                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1631
1632                 local obj_file="O/$seq/d$((oid %32))/$oid"
1633                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1634                            $dev 2>/dev/null" | grep "parent=")
1635
1636                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1637
1638                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1639
1640                 #do_facet ost$ost umount -d $dir
1641                 #start ost$ost $dev $OST_MOUNT_OPTS
1642
1643                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1644                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1645                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1646                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1647                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1648                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1649
1650                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1651                 [ $ff_pseq = $lmm_seq ] ||
1652                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1653                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1654                 [ $ff_poid = $lmm_oid ] ||
1655                         error "FF parent OID $ff_poid != $lmm_oid"
1656                 [ $ff_pstripe = $stripe_nr ] ||
1657                         error "FF stripe $ff_pstripe != $stripe_nr"
1658
1659                 stripe_nr=$((stripe_nr + 1))
1660         done
1661 }
1662
1663 test_27z() {
1664         remote_ost_nodsh && skip "remote OST with nodsh" && return
1665         mkdir -p $DIR/$tdir
1666
1667         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1668                 { error "setstripe -c -1 failed"; return 1; }
1669         # We need to send a write to every object to get parent FID info set.
1670         # This _should_ also work for setattr, but does not currently.
1671         # touch $DIR/$tdir/$tfile-1 ||
1672         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1673                 { error "dd $tfile-1 failed"; return 2; }
1674         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1675                 { error "setstripe -c -1 failed"; return 3; }
1676         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1677                 { error "dd $tfile-2 failed"; return 4; }
1678
1679         # make sure write RPCs have been sent to OSTs
1680         sync; sleep 5; sync
1681
1682         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1683         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1684 }
1685 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1686
1687 test_27A() { # b=19102
1688         local restore_size=$($GETSTRIPE -S $MOUNT)
1689         local restore_count=$($GETSTRIPE -c $MOUNT)
1690         local restore_offset=$($GETSTRIPE -i $MOUNT)
1691         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1692         local default_size=$($GETSTRIPE -S $MOUNT)
1693         local default_count=$($GETSTRIPE -c $MOUNT)
1694         local default_offset=$($GETSTRIPE -i $MOUNT)
1695         local dsize=$((1024 * 1024))
1696         [ $default_size -eq $dsize ] ||
1697                 error "stripe size $default_size != $dsize"
1698         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1699         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1700         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1701 }
1702 run_test 27A "check filesystem-wide default LOV EA values"
1703
1704 # createtest also checks that device nodes are created and
1705 # then visible correctly (#2091)
1706 test_28() { # bug 2091
1707         mkdir $DIR/d28
1708         $CREATETEST $DIR/d28/ct || error
1709 }
1710 run_test 28 "create/mknod/mkdir with bad file types ============"
1711
1712 test_29() {
1713         cancel_lru_locks mdc
1714         mkdir $DIR/d29
1715         touch $DIR/d29/foo
1716         log 'first d29'
1717         ls -l $DIR/d29
1718
1719         declare -i LOCKCOUNTORIG=0
1720         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1721                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1722         done
1723         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1724
1725         declare -i LOCKUNUSEDCOUNTORIG=0
1726         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1727                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1728         done
1729
1730         log 'second d29'
1731         ls -l $DIR/d29
1732         log 'done'
1733
1734         declare -i LOCKCOUNTCURRENT=0
1735         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1736                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1737         done
1738
1739         declare -i LOCKUNUSEDCOUNTCURRENT=0
1740         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1741                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1742         done
1743
1744         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1745                 lctl set_param -n ldlm.dump_namespaces ""
1746                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1747                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1748                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1749                 return 2
1750         fi
1751         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1752                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1753                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1754                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1755                 return 3
1756         fi
1757 }
1758 run_test 29 "IT_GETATTR regression  ============================"
1759
1760 test_30a() { # was test_30
1761         cp `which ls` $DIR || cp /bin/ls $DIR
1762         $DIR/ls / || error
1763         rm $DIR/ls
1764 }
1765 run_test 30a "execute binary from Lustre (execve) =============="
1766
1767 test_30b() {
1768         cp `which ls` $DIR || cp /bin/ls $DIR
1769         chmod go+rx $DIR/ls
1770         $RUNAS $DIR/ls / || error
1771         rm $DIR/ls
1772 }
1773 run_test 30b "execute binary from Lustre as non-root ==========="
1774
1775 test_30c() { # b=22376
1776         cp `which ls` $DIR || cp /bin/ls $DIR
1777         chmod a-rw $DIR/ls
1778         cancel_lru_locks mdc
1779         cancel_lru_locks osc
1780         $RUNAS $DIR/ls / || error
1781         rm -f $DIR/ls
1782 }
1783 run_test 30c "execute binary from Lustre without read perms ===="
1784
1785 test_31a() {
1786         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1787         $CHECKSTAT -a $DIR/f31 || error
1788 }
1789 run_test 31a "open-unlink file =================================="
1790
1791 test_31b() {
1792         touch $DIR/f31 || error
1793         ln $DIR/f31 $DIR/f31b || error
1794         $MULTIOP $DIR/f31b Ouc || error
1795         $CHECKSTAT -t file $DIR/f31 || error
1796 }
1797 run_test 31b "unlink file with multiple links while open ======="
1798
1799 test_31c() {
1800         touch $DIR/f31 || error
1801         ln $DIR/f31 $DIR/f31c || error
1802         multiop_bg_pause $DIR/f31 O_uc || return 1
1803         MULTIPID=$!
1804         $MULTIOP $DIR/f31c Ouc
1805         kill -USR1 $MULTIPID
1806         wait $MULTIPID
1807 }
1808 run_test 31c "open-unlink file with multiple links ============="
1809
1810 test_31d() {
1811         opendirunlink $DIR/d31d $DIR/d31d || error
1812         $CHECKSTAT -a $DIR/d31d || error
1813 }
1814 run_test 31d "remove of open directory ========================="
1815
1816 test_31e() { # bug 2904
1817         check_kernel_version 34 || return 0
1818         openfilleddirunlink $DIR/d31e || error
1819 }
1820 run_test 31e "remove of open non-empty directory ==============="
1821
1822 test_31f() { # bug 4554
1823         set -vx
1824         mkdir $DIR/d31f
1825         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1826         cp /etc/hosts $DIR/d31f
1827         ls -l $DIR/d31f
1828         $GETSTRIPE $DIR/d31f/hosts
1829         multiop_bg_pause $DIR/d31f D_c || return 1
1830         MULTIPID=$!
1831
1832         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1833         mkdir $DIR/d31f
1834         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1835         cp /etc/hosts $DIR/d31f
1836         ls -l $DIR/d31f
1837         $GETSTRIPE $DIR/d31f/hosts
1838         multiop_bg_pause $DIR/d31f D_c || return 1
1839         MULTIPID2=$!
1840
1841         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1842         wait $MULTIPID || error "first opendir $MULTIPID failed"
1843
1844         sleep 6
1845
1846         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1847         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1848         set +vx
1849 }
1850 run_test 31f "remove of open directory with open-unlink file ==="
1851
1852 test_31g() {
1853         echo "-- cross directory link --"
1854         mkdir $DIR/d31g{a,b}
1855         touch $DIR/d31ga/f
1856         ln $DIR/d31ga/f $DIR/d31gb/g
1857         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1858         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1859         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1860         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1861 }
1862 run_test 31g "cross directory link==============="
1863
1864 test_31h() {
1865         echo "-- cross directory link --"
1866         mkdir $DIR/d31h
1867         mkdir $DIR/d31h/dir
1868         touch $DIR/d31h/f
1869         ln $DIR/d31h/f $DIR/d31h/dir/g
1870         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1871         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1872         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1873         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1874 }
1875 run_test 31h "cross directory link under child==============="
1876
1877 test_31i() {
1878         echo "-- cross directory link --"
1879         mkdir $DIR/d31i
1880         mkdir $DIR/d31i/dir
1881         touch $DIR/d31i/dir/f
1882         ln $DIR/d31i/dir/f $DIR/d31i/g
1883         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1884         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1885         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1886         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1887 }
1888 run_test 31i "cross directory link under parent==============="
1889
1890
1891 test_31j() {
1892         mkdir $DIR/d31j
1893         mkdir $DIR/d31j/dir1
1894         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1895         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1896         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1897         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1898         return 0
1899 }
1900 run_test 31j "link for directory==============="
1901
1902
1903 test_31k() {
1904         mkdir $DIR/d31k
1905         touch $DIR/d31k/s
1906         touch $DIR/d31k/exist
1907         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1908         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1909         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1910         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1911         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1912         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1913         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1914         return 0
1915 }
1916 run_test 31k "link to file: the same, non-existing, dir==============="
1917
1918 test_31m() {
1919         mkdir $DIR/d31m
1920         touch $DIR/d31m/s
1921         mkdir $DIR/d31m2
1922         touch $DIR/d31m2/exist
1923         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1924         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1925         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1926         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1927         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1928         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1929         return 0
1930 }
1931 run_test 31m "link to file: the same, non-existing, dir==============="
1932
1933 cleanup_test32_mount() {
1934         trap 0
1935         $UMOUNT $DIR/$tdir/ext2-mountpoint
1936 }
1937
1938 test_32a() {
1939         echo "== more mountpoints and symlinks ================="
1940         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1941         trap cleanup_test32_mount EXIT
1942         mkdir -p $DIR/$tdir/ext2-mountpoint
1943         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1944         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
1945         cleanup_test32_mount
1946 }
1947 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1948
1949 test_32b() {
1950         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1951         trap cleanup_test32_mount EXIT
1952         mkdir -p $DIR/$tdir/ext2-mountpoint
1953         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1954         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
1955         cleanup_test32_mount
1956 }
1957 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1958
1959 test_32c() {
1960         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1961         trap cleanup_test32_mount EXIT
1962         mkdir -p $DIR/$tdir/ext2-mountpoint
1963         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1964         mkdir -p $DIR/$tdir/d2/test_dir
1965         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
1966         cleanup_test32_mount
1967 }
1968 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1969
1970 test_32d() {
1971         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1972         trap cleanup_test32_mount EXIT
1973         mkdir -p $DIR/$tdir/ext2-mountpoint
1974         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1975         mkdir -p $DIR/$tdir/d2/test_dir
1976         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
1977         cleanup_test32_mount
1978 }
1979 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1980
1981 test_32e() {
1982         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1983         mkdir -p $DIR/d32e/tmp
1984         TMP_DIR=$DIR/d32e/tmp
1985         ln -s $DIR/d32e $TMP_DIR/symlink11
1986         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1987         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1988         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1989 }
1990 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1991
1992 test_32f() {
1993         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1994         mkdir -p $DIR/d32f/tmp
1995         TMP_DIR=$DIR/d32f/tmp
1996         ln -s $DIR/d32f $TMP_DIR/symlink11
1997         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1998         ls $DIR/d32f/tmp/symlink11  || error
1999         ls $DIR/d32f/symlink01 || error
2000 }
2001 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2002
2003 test_32g() {
2004         TMP_DIR=$DIR/$tdir/tmp
2005         mkdir -p $TMP_DIR $DIR/${tdir}2
2006         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2007         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2008         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2009         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2010         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2011         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2012 }
2013 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2014
2015 test_32h() {
2016         rm -fr $DIR/$tdir $DIR/${tdir}2
2017         TMP_DIR=$DIR/$tdir/tmp
2018         mkdir -p $TMP_DIR $DIR/${tdir}2
2019         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2020         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2021         ls $TMP_DIR/symlink12 || error
2022         ls $DIR/$tdir/symlink02  || error
2023 }
2024 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2025
2026 test_32i() {
2027         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2028         trap cleanup_test32_mount EXIT
2029         mkdir -p $DIR/$tdir/ext2-mountpoint
2030         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2031         touch $DIR/$tdir/test_file
2032         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2033         cleanup_test32_mount
2034 }
2035 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2036
2037 test_32j() {
2038         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2039         trap cleanup_test32_mount EXIT
2040         mkdir -p $DIR/$tdir/ext2-mountpoint
2041         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2042         touch $DIR/$tdir/test_file
2043         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2044         cleanup_test32_mount
2045 }
2046 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2047
2048 test_32k() {
2049         rm -fr $DIR/$tdir
2050         trap cleanup_test32_mount EXIT
2051         mkdir -p $DIR/$tdir/ext2-mountpoint
2052         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2053         mkdir -p $DIR/$tdir/d2
2054         touch $DIR/$tdir/d2/test_file || error
2055         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2056         cleanup_test32_mount
2057 }
2058 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2059
2060 test_32l() {
2061         rm -fr $DIR/$tdir
2062         trap cleanup_test32_mount EXIT
2063         mkdir -p $DIR/$tdir/ext2-mountpoint
2064         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2065         mkdir -p $DIR/$tdir/d2
2066         touch $DIR/$tdir/d2/test_file
2067         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2068         cleanup_test32_mount
2069 }
2070 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2071
2072 test_32m() {
2073         rm -fr $DIR/d32m
2074         mkdir -p $DIR/d32m/tmp
2075         TMP_DIR=$DIR/d32m/tmp
2076         ln -s $DIR $TMP_DIR/symlink11
2077         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2078         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2079         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2080 }
2081 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2082
2083 test_32n() {
2084         rm -fr $DIR/d32n
2085         mkdir -p $DIR/d32n/tmp
2086         TMP_DIR=$DIR/d32n/tmp
2087         ln -s $DIR $TMP_DIR/symlink11
2088         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2089         ls -l $DIR/d32n/tmp/symlink11  || error
2090         ls -l $DIR/d32n/symlink01 || error
2091 }
2092 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2093
2094 test_32o() {
2095         rm -fr $DIR/d32o $DIR/$tfile
2096         touch $DIR/$tfile
2097         mkdir -p $DIR/d32o/tmp
2098         TMP_DIR=$DIR/d32o/tmp
2099         ln -s $DIR/$tfile $TMP_DIR/symlink12
2100         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2101         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2102         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2103         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2104         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2105 }
2106 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2107
2108 test_32p() {
2109     log 32p_1
2110         rm -fr $DIR/d32p
2111     log 32p_2
2112         rm -f $DIR/$tfile
2113     log 32p_3
2114         touch $DIR/$tfile
2115     log 32p_4
2116         mkdir -p $DIR/d32p/tmp
2117     log 32p_5
2118         TMP_DIR=$DIR/d32p/tmp
2119     log 32p_6
2120         ln -s $DIR/$tfile $TMP_DIR/symlink12
2121     log 32p_7
2122         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2123     log 32p_8
2124         cat $DIR/d32p/tmp/symlink12 || error
2125     log 32p_9
2126         cat $DIR/d32p/symlink02 || error
2127     log 32p_10
2128 }
2129 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2130
2131 cleanup_testdir_mount() {
2132         trap 0
2133         $UMOUNT $DIR/$tdir
2134 }
2135
2136 test_32q() {
2137         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2138         trap cleanup_testdir_mount EXIT
2139         mkdir -p $DIR/$tdir
2140         touch $DIR/$tdir/under_the_mount
2141         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2142         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2143         cleanup_testdir_mount
2144 }
2145 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2146
2147 test_32r() {
2148         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2149         trap cleanup_testdir_mount EXIT
2150         mkdir -p $DIR/$tdir
2151         touch $DIR/$tdir/under_the_mount
2152         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2153         ls $DIR/$tdir | grep -q under_the_mount && error || true
2154         cleanup_testdir_mount
2155 }
2156 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2157
2158 test_33aa() {
2159         rm -f $DIR/$tfile
2160         touch $DIR/$tfile
2161         chmod 444 $DIR/$tfile
2162         chown $RUNAS_ID $DIR/$tfile
2163         log 33_1
2164         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2165         log 33_2
2166 }
2167 run_test 33aa "write file with mode 444 (should return error) ===="
2168
2169 test_33a() {
2170         rm -fr $DIR/d33
2171         mkdir -p $DIR/d33
2172         chown $RUNAS_ID $DIR/d33
2173         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2174         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2175                 error "open RDWR" || true
2176 }
2177 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2178
2179 test_33b() {
2180         rm -fr $DIR/d33
2181         mkdir -p $DIR/d33
2182         chown $RUNAS_ID $DIR/d33
2183         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2184 }
2185 run_test 33b "test open file with malformed flags (No panic and return error)"
2186
2187 test_33c() {
2188         local ostnum
2189         local ostname
2190         local write_bytes
2191         local all_zeros
2192
2193         remote_ost_nodsh && skip "remote OST with nodsh" && return
2194         all_zeros=:
2195         rm -fr $DIR/d33
2196         mkdir -p $DIR/d33
2197         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2198
2199         sync
2200         for ostnum in $(seq $OSTCOUNT); do
2201                 # test-framework's OST numbering is one-based, while Lustre's
2202                 # is zero-based
2203                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2204                 # Parsing llobdstat's output sucks; we could grep the /proc
2205                 # path, but that's likely to not be as portable as using the
2206                 # llobdstat utility.  So we parse lctl output instead.
2207                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2208                         obdfilter/$ostname/stats |
2209                         awk '/^write_bytes/ {print $7}' )
2210                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2211                 if (( ${write_bytes:-0} > 0 ))
2212                 then
2213                         all_zeros=false
2214                         break;
2215                 fi
2216         done
2217
2218         $all_zeros || return 0
2219
2220         # Write four bytes
2221         echo foo > $DIR/d33/bar
2222         # Really write them
2223         sync
2224
2225         # Total up write_bytes after writing.  We'd better find non-zeros.
2226         for ostnum in $(seq $OSTCOUNT); do
2227                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2228                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2229                         obdfilter/$ostname/stats |
2230                         awk '/^write_bytes/ {print $7}' )
2231                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2232                 if (( ${write_bytes:-0} > 0 ))
2233                 then
2234                         all_zeros=false
2235                         break;
2236                 fi
2237         done
2238
2239         if $all_zeros
2240         then
2241                 for ostnum in $(seq $OSTCOUNT); do
2242                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2243                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2244                         do_facet ost$ostnum lctl get_param -n \
2245                                 obdfilter/$ostname/stats
2246                 done
2247                 error "OST not keeping write_bytes stats (b22312)"
2248         fi
2249 }
2250 run_test 33c "test llobdstat and write_bytes"
2251
2252 test_33d() {
2253         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2254         local MDTIDX=1
2255         local remote_dir=$DIR/$tdir/remote_dir
2256
2257         mkdir -p $DIR/$tdir
2258         $LFS mkdir -i $MDTIDX $remote_dir ||
2259                 error "create remote directory failed"
2260
2261         touch $remote_dir/$tfile
2262         chmod 444 $remote_dir/$tfile
2263         chown $RUNAS_ID $remote_dir/$tfile
2264
2265         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2266
2267         chown $RUNAS_ID $remote_dir
2268         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2269                                         error "create" || true
2270         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2271                                     error "open RDWR" || true
2272         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2273                                     error "create" || true
2274 }
2275 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2276
2277 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2278 test_34a() {
2279         rm -f $DIR/f34
2280         $MCREATE $DIR/f34 || error
2281         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2282         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2283         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2284         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2285 }
2286 run_test 34a "truncate file that has not been opened ==========="
2287
2288 test_34b() {
2289         [ ! -f $DIR/f34 ] && test_34a
2290         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2291         $OPENFILE -f O_RDONLY $DIR/f34
2292         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2293         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2294 }
2295 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2296
2297 test_34c() {
2298         [ ! -f $DIR/f34 ] && test_34a
2299         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2300         $OPENFILE -f O_RDWR $DIR/f34
2301         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2302         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2303 }
2304 run_test 34c "O_RDWR opening file-with-size works =============="
2305
2306 test_34d() {
2307         [ ! -f $DIR/f34 ] && test_34a
2308         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2309         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2310         rm $DIR/f34
2311 }
2312 run_test 34d "write to sparse file ============================="
2313
2314 test_34e() {
2315         rm -f $DIR/f34e
2316         $MCREATE $DIR/f34e || error
2317         $TRUNCATE $DIR/f34e 1000 || error
2318         $CHECKSTAT -s 1000 $DIR/f34e || error
2319         $OPENFILE -f O_RDWR $DIR/f34e
2320         $CHECKSTAT -s 1000 $DIR/f34e || error
2321 }
2322 run_test 34e "create objects, some with size and some without =="
2323
2324 test_34f() { # bug 6242, 6243
2325         SIZE34F=48000
2326         rm -f $DIR/f34f
2327         $MCREATE $DIR/f34f || error
2328         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2329         dd if=$DIR/f34f of=$TMP/f34f
2330         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2331         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2332         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2333         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2334         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2335 }
2336 run_test 34f "read from a file with no objects until EOF ======="
2337
2338 test_34g() {
2339         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2340         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2341         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2342         cancel_lru_locks osc
2343         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2344                 error "wrong size after lock cancel"
2345
2346         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2347         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2348                 error "expanding truncate failed"
2349         cancel_lru_locks osc
2350         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2351                 error "wrong expanded size after lock cancel"
2352 }
2353 run_test 34g "truncate long file ==============================="
2354
2355 test_34h() {
2356         local gid=10
2357         local sz=1000
2358
2359         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2360         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2361         MULTIPID=$!
2362         sleep 2
2363
2364         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2365                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2366                 kill -9 $MULTIPID
2367         fi
2368         wait $MULTIPID
2369         local nsz=`stat -c %s $DIR/$tfile`
2370         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2371 }
2372 run_test 34h "ftruncate file under grouplock should not block"
2373
2374 test_35a() {
2375         cp /bin/sh $DIR/f35a
2376         chmod 444 $DIR/f35a
2377         chown $RUNAS_ID $DIR/f35a
2378         $RUNAS $DIR/f35a && error || true
2379         rm $DIR/f35a
2380 }
2381 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2382
2383 test_36a() {
2384         rm -f $DIR/f36
2385         utime $DIR/f36 || error
2386 }
2387 run_test 36a "MDS utime check (mknod, utime) ==================="
2388
2389 test_36b() {
2390         echo "" > $DIR/f36
2391         utime $DIR/f36 || error
2392 }
2393 run_test 36b "OST utime check (open, utime) ===================="
2394
2395 test_36c() {
2396         rm -f $DIR/d36/f36
2397         mkdir $DIR/d36
2398         chown $RUNAS_ID $DIR/d36
2399         $RUNAS utime $DIR/d36/f36 || error
2400 }
2401 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2402
2403 test_36d() {
2404         [ ! -d $DIR/d36 ] && test_36c
2405         echo "" > $DIR/d36/f36
2406         $RUNAS utime $DIR/d36/f36 || error
2407 }
2408 run_test 36d "non-root OST utime check (open, utime) ==========="
2409
2410 test_36e() {
2411         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2412         mkdir -p $DIR/$tdir
2413         touch $DIR/$tdir/$tfile
2414         $RUNAS utime $DIR/$tdir/$tfile && \
2415                 error "utime worked, expected failure" || true
2416 }
2417 run_test 36e "utime on non-owned file (should return error) ===="
2418
2419 subr_36fh() {
2420         local fl="$1"
2421         local LANG_SAVE=$LANG
2422         local LC_LANG_SAVE=$LC_LANG
2423         export LANG=C LC_LANG=C # for date language
2424
2425         DATESTR="Dec 20  2000"
2426         mkdir -p $DIR/$tdir
2427         lctl set_param fail_loc=$fl
2428         date; date +%s
2429         cp /etc/hosts $DIR/$tdir/$tfile
2430         sync & # write RPC generated with "current" inode timestamp, but delayed
2431         sleep 1
2432         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2433         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2434         cancel_lru_locks osc
2435         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2436         date; date +%s
2437         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2438                 echo "BEFORE: $LS_BEFORE" && \
2439                 echo "AFTER : $LS_AFTER" && \
2440                 echo "WANT  : $DATESTR" && \
2441                 error "$DIR/$tdir/$tfile timestamps changed" || true
2442
2443         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2444 }
2445
2446 test_36f() {
2447         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2448         subr_36fh "0x80000214"
2449 }
2450 run_test 36f "utime on file racing with OST BRW write =========="
2451
2452 test_36g() {
2453         remote_ost_nodsh && skip "remote OST with nodsh" && return
2454         local fmd_max_age
2455         local fmd_before
2456         local fmd_after
2457
2458         mkdir -p $DIR/$tdir
2459         fmd_max_age=$(do_facet ost1 \
2460                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2461                 head -n 1")
2462
2463         fmd_before=$(do_facet ost1 \
2464                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2465         touch $DIR/$tdir/$tfile
2466         sleep $((fmd_max_age + 12))
2467         fmd_after=$(do_facet ost1 \
2468                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2469
2470         echo "fmd_before: $fmd_before"
2471         echo "fmd_after: $fmd_after"
2472         [ "$fmd_after" -gt "$fmd_before" ] && \
2473                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2474                 error "fmd didn't expire after ping" || true
2475 }
2476 run_test 36g "filter mod data cache expiry ====================="
2477
2478 test_36h() {
2479         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2480         subr_36fh "0x80000227"
2481 }
2482 run_test 36h "utime on file racing with OST BRW write =========="
2483
2484 # test_37 - duplicate with tests 32q 32r
2485
2486 test_38() {
2487         local file=$DIR/$tfile
2488         touch $file
2489         openfile -f O_DIRECTORY $file
2490         local RC=$?
2491         local ENOTDIR=20
2492         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2493         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2494 }
2495 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2496
2497 test_39() {
2498         touch $DIR/$tfile
2499         touch $DIR/${tfile}2
2500 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2501 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2502 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2503         sleep 2
2504         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2505         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2506                 echo "mtime"
2507                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2508                 echo "atime"
2509                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2510                 echo "ctime"
2511                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2512                 error "O_TRUNC didn't change timestamps"
2513         fi
2514 }
2515 run_test 39 "mtime changed on create ==========================="
2516
2517 test_39b() {
2518         mkdir -p $DIR/$tdir
2519         cp -p /etc/passwd $DIR/$tdir/fopen
2520         cp -p /etc/passwd $DIR/$tdir/flink
2521         cp -p /etc/passwd $DIR/$tdir/funlink
2522         cp -p /etc/passwd $DIR/$tdir/frename
2523         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2524
2525         sleep 1
2526         echo "aaaaaa" >> $DIR/$tdir/fopen
2527         echo "aaaaaa" >> $DIR/$tdir/flink
2528         echo "aaaaaa" >> $DIR/$tdir/funlink
2529         echo "aaaaaa" >> $DIR/$tdir/frename
2530
2531         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2532         local link_new=`stat -c %Y $DIR/$tdir/flink`
2533         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2534         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2535
2536         cat $DIR/$tdir/fopen > /dev/null
2537         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2538         rm -f $DIR/$tdir/funlink2
2539         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2540
2541         for (( i=0; i < 2; i++ )) ; do
2542                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2543                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2544                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2545                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2546
2547                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2548                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2549                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2550                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2551
2552                 cancel_lru_locks osc
2553                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2554         done
2555 }
2556 run_test 39b "mtime change on open, link, unlink, rename  ======"
2557
2558 # this should be set to past
2559 TEST_39_MTIME=`date -d "1 year ago" +%s`
2560
2561 # bug 11063
2562 test_39c() {
2563         touch $DIR1/$tfile
2564         sleep 2
2565         local mtime0=`stat -c %Y $DIR1/$tfile`
2566
2567         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2568         local mtime1=`stat -c %Y $DIR1/$tfile`
2569         [ "$mtime1" = $TEST_39_MTIME ] || \
2570                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2571
2572         local d1=`date +%s`
2573         echo hello >> $DIR1/$tfile
2574         local d2=`date +%s`
2575         local mtime2=`stat -c %Y $DIR1/$tfile`
2576         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2577                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2578
2579         mv $DIR1/$tfile $DIR1/$tfile-1
2580
2581         for (( i=0; i < 2; i++ )) ; do
2582                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2583                 [ "$mtime2" = "$mtime3" ] || \
2584                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2585
2586                 cancel_lru_locks osc
2587                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2588         done
2589 }
2590 run_test 39c "mtime change on rename ==========================="
2591
2592 # bug 21114
2593 test_39d() {
2594         touch $DIR1/$tfile
2595
2596         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2597
2598         for (( i=0; i < 2; i++ )) ; do
2599                 local mtime=`stat -c %Y $DIR1/$tfile`
2600                 [ $mtime = $TEST_39_MTIME ] || \
2601                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2602
2603                 cancel_lru_locks osc
2604                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2605         done
2606 }
2607 run_test 39d "create, utime, stat =============================="
2608
2609 # bug 21114
2610 test_39e() {
2611         touch $DIR1/$tfile
2612         local mtime1=`stat -c %Y $DIR1/$tfile`
2613
2614         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2615
2616         for (( i=0; i < 2; i++ )) ; do
2617                 local mtime2=`stat -c %Y $DIR1/$tfile`
2618                 [ $mtime2 = $TEST_39_MTIME ] || \
2619                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2620
2621                 cancel_lru_locks osc
2622                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2623         done
2624 }
2625 run_test 39e "create, stat, utime, stat ========================"
2626
2627 # bug 21114
2628 test_39f() {
2629         touch $DIR1/$tfile
2630         mtime1=`stat -c %Y $DIR1/$tfile`
2631
2632         sleep 2
2633         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2634
2635         for (( i=0; i < 2; i++ )) ; do
2636                 local mtime2=`stat -c %Y $DIR1/$tfile`
2637                 [ $mtime2 = $TEST_39_MTIME ] || \
2638                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2639
2640                 cancel_lru_locks osc
2641                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2642         done
2643 }
2644 run_test 39f "create, stat, sleep, utime, stat ================="
2645
2646 # bug 11063
2647 test_39g() {
2648         echo hello >> $DIR1/$tfile
2649         local mtime1=`stat -c %Y $DIR1/$tfile`
2650
2651         sleep 2
2652         chmod o+r $DIR1/$tfile
2653
2654         for (( i=0; i < 2; i++ )) ; do
2655                 local mtime2=`stat -c %Y $DIR1/$tfile`
2656                 [ "$mtime1" = "$mtime2" ] || \
2657                         error "lost mtime: $mtime2, should be $mtime1"
2658
2659                 cancel_lru_locks osc
2660                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2661         done
2662 }
2663 run_test 39g "write, chmod, stat ==============================="
2664
2665 # bug 11063
2666 test_39h() {
2667         touch $DIR1/$tfile
2668         sleep 1
2669
2670         local d1=`date`
2671         echo hello >> $DIR1/$tfile
2672         local mtime1=`stat -c %Y $DIR1/$tfile`
2673
2674         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2675         local d2=`date`
2676         if [ "$d1" != "$d2" ]; then
2677                 echo "write and touch not within one second"
2678         else
2679                 for (( i=0; i < 2; i++ )) ; do
2680                         local mtime2=`stat -c %Y $DIR1/$tfile`
2681                         [ "$mtime2" = $TEST_39_MTIME ] || \
2682                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2683
2684                         cancel_lru_locks osc
2685                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2686                 done
2687         fi
2688 }
2689 run_test 39h "write, utime within one second, stat ============="
2690
2691 test_39i() {
2692         touch $DIR1/$tfile
2693         sleep 1
2694
2695         echo hello >> $DIR1/$tfile
2696         local mtime1=`stat -c %Y $DIR1/$tfile`
2697
2698         mv $DIR1/$tfile $DIR1/$tfile-1
2699
2700         for (( i=0; i < 2; i++ )) ; do
2701                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2702
2703                 [ "$mtime1" = "$mtime2" ] || \
2704                         error "lost mtime: $mtime2, should be $mtime1"
2705
2706                 cancel_lru_locks osc
2707                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2708         done
2709 }
2710 run_test 39i "write, rename, stat =============================="
2711
2712 test_39j() {
2713         start_full_debug_logging
2714         touch $DIR1/$tfile
2715         sleep 1
2716
2717         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2718         lctl set_param fail_loc=0x80000412
2719         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2720                 error "multiop failed"
2721         local multipid=$!
2722         local mtime1=`stat -c %Y $DIR1/$tfile`
2723
2724         mv $DIR1/$tfile $DIR1/$tfile-1
2725
2726         kill -USR1 $multipid
2727         wait $multipid || error "multiop close failed"
2728
2729         for (( i=0; i < 2; i++ )) ; do
2730                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2731                 [ "$mtime1" = "$mtime2" ] ||
2732                         error "mtime is lost on close: $mtime2, " \
2733                               "should be $mtime1"
2734
2735                 cancel_lru_locks osc
2736                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2737         done
2738         lctl set_param fail_loc=0
2739         stop_full_debug_logging
2740 }
2741 run_test 39j "write, rename, close, stat ======================="
2742
2743 test_39k() {
2744         touch $DIR1/$tfile
2745         sleep 1
2746
2747         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2748         local multipid=$!
2749         local mtime1=`stat -c %Y $DIR1/$tfile`
2750
2751         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2752
2753         kill -USR1 $multipid
2754         wait $multipid || error "multiop close failed"
2755
2756         for (( i=0; i < 2; i++ )) ; do
2757                 local mtime2=`stat -c %Y $DIR1/$tfile`
2758
2759                 [ "$mtime2" = $TEST_39_MTIME ] || \
2760                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2761
2762                 cancel_lru_locks osc
2763                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2764         done
2765 }
2766 run_test 39k "write, utime, close, stat ========================"
2767
2768 # this should be set to future
2769 TEST_39_ATIME=`date -d "1 year" +%s`
2770
2771 test_39l() {
2772         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2773         local atime_diff=$(do_facet $SINGLEMDS \
2774                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2775         mkdir -p $DIR/$tdir
2776
2777         # test setting directory atime to future
2778         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2779         local atime=$(stat -c %X $DIR/$tdir)
2780         [ "$atime" = $TEST_39_ATIME ] || \
2781                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2782
2783         # test setting directory atime from future to now
2784         local d1=$(date +%s)
2785         ls $DIR/$tdir
2786         local d2=$(date +%s)
2787
2788         cancel_lru_locks mdc
2789         atime=$(stat -c %X $DIR/$tdir)
2790         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2791                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2792
2793         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2794         sleep 3
2795
2796         # test setting directory atime when now > dir atime + atime_diff
2797         d1=$(date +%s)
2798         ls $DIR/$tdir
2799         d2=$(date +%s)
2800         cancel_lru_locks mdc
2801         atime=$(stat -c %X $DIR/$tdir)
2802         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2803                 error "atime is not updated  : $atime, should be $d2"
2804
2805         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2806         sleep 3
2807
2808         # test not setting directory atime when now < dir atime + atime_diff
2809         ls $DIR/$tdir
2810         cancel_lru_locks mdc
2811         atime=$(stat -c %X $DIR/$tdir)
2812         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2813                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2814
2815         do_facet $SINGLEMDS \
2816                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2817 }
2818 run_test 39l "directory atime update ==========================="
2819
2820 test_39m() {
2821         touch $DIR1/$tfile
2822         sleep 2
2823         local far_past_mtime=$(date -d "May 29 1953" +%s)
2824         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2825
2826         touch -m -d @$far_past_mtime $DIR1/$tfile
2827         touch -a -d @$far_past_atime $DIR1/$tfile
2828
2829         for (( i=0; i < 2; i++ )) ; do
2830                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2831                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2832                         error "atime or mtime set incorrectly"
2833
2834                 cancel_lru_locks osc
2835                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2836         done
2837 }
2838 run_test 39m "test atime and mtime before 1970"
2839
2840 test_40() {
2841         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2842         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2843         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2844 }
2845 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2846
2847 test_41() {
2848         # bug 1553
2849         small_write $DIR/f41 18
2850 }
2851 run_test 41 "test small file write + fstat ====================="
2852
2853 count_ost_writes() {
2854         lctl get_param -n osc.*.stats |
2855             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2856 }
2857
2858 # decent default
2859 WRITEBACK_SAVE=500
2860 DIRTY_RATIO_SAVE=40
2861 MAX_DIRTY_RATIO=50
2862 BG_DIRTY_RATIO_SAVE=10
2863 MAX_BG_DIRTY_RATIO=25
2864
2865 start_writeback() {
2866         trap 0
2867         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2868         # dirty_ratio, dirty_background_ratio
2869         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2870                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2871                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2872                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2873         else
2874                 # if file not here, we are a 2.4 kernel
2875                 kill -CONT `pidof kupdated`
2876         fi
2877 }
2878
2879 stop_writeback() {
2880         # setup the trap first, so someone cannot exit the test at the
2881         # exact wrong time and mess up a machine
2882         trap start_writeback EXIT
2883         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2884         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2885                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2886                 sysctl -w vm.dirty_writeback_centisecs=0
2887                 sysctl -w vm.dirty_writeback_centisecs=0
2888                 # save and increase /proc/sys/vm/dirty_ratio
2889                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2890                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2891                 # save and increase /proc/sys/vm/dirty_background_ratio
2892                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2893                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2894         else
2895                 # if file not here, we are a 2.4 kernel
2896                 kill -STOP `pidof kupdated`
2897         fi
2898 }
2899
2900 # ensure that all stripes have some grant before we test client-side cache
2901 setup_test42() {
2902         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2903                 dd if=/dev/zero of=$i bs=4k count=1
2904                 rm $i
2905         done
2906 }
2907
2908 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2909 # file truncation, and file removal.
2910 test_42a() {
2911         setup_test42
2912         cancel_lru_locks osc
2913         stop_writeback
2914         sync; sleep 1; sync # just to be safe
2915         BEFOREWRITES=`count_ost_writes`
2916         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2917         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2918         AFTERWRITES=`count_ost_writes`
2919         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2920                 error "$BEFOREWRITES < $AFTERWRITES"
2921         start_writeback
2922 }
2923 run_test 42a "ensure that we don't flush on close =============="
2924
2925 test_42b() {
2926         setup_test42
2927         cancel_lru_locks osc
2928         stop_writeback
2929         sync
2930         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2931         BEFOREWRITES=`count_ost_writes`
2932         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2933         AFTERWRITES=`count_ost_writes`
2934         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2935                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2936         fi
2937         BEFOREWRITES=`count_ost_writes`
2938         sync || error "sync: $?"
2939         AFTERWRITES=`count_ost_writes`
2940         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2941                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2942         fi
2943         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2944         start_writeback
2945         return 0
2946 }
2947 run_test 42b "test destroy of file with cached dirty data ======"
2948
2949 # if these tests just want to test the effect of truncation,
2950 # they have to be very careful.  consider:
2951 # - the first open gets a {0,EOF}PR lock
2952 # - the first write conflicts and gets a {0, count-1}PW
2953 # - the rest of the writes are under {count,EOF}PW
2954 # - the open for truncate tries to match a {0,EOF}PR
2955 #   for the filesize and cancels the PWs.
2956 # any number of fixes (don't get {0,EOF} on open, match
2957 # composite locks, do smarter file size management) fix
2958 # this, but for now we want these tests to verify that
2959 # the cancellation with truncate intent works, so we
2960 # start the file with a full-file pw lock to match against
2961 # until the truncate.
2962 trunc_test() {
2963         test=$1
2964         file=$DIR/$test
2965         offset=$2
2966         cancel_lru_locks osc
2967         stop_writeback
2968         # prime the file with 0,EOF PW to match
2969         touch $file
2970         $TRUNCATE $file 0
2971         sync; sync
2972         # now the real test..
2973         dd if=/dev/zero of=$file bs=1024 count=100
2974         BEFOREWRITES=`count_ost_writes`
2975         $TRUNCATE $file $offset
2976         cancel_lru_locks osc
2977         AFTERWRITES=`count_ost_writes`
2978         start_writeback
2979 }
2980
2981 test_42c() {
2982         trunc_test 42c 1024
2983         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2984             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2985         rm $file
2986 }
2987 run_test 42c "test partial truncate of file with cached dirty data"
2988
2989 test_42d() {
2990         trunc_test 42d 0
2991         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2992             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2993         rm $file
2994 }
2995 run_test 42d "test complete truncate of file with cached dirty data"
2996
2997 test_42e() { # bug22074
2998         local TDIR=$DIR/${tdir}e
2999         local pagesz=$(page_size)
3000         local pages=16 # hardcoded 16 pages, don't change it.
3001         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3002         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3003         local max_dirty_mb
3004         local warmup_files
3005
3006         mkdir -p $TDIR
3007         $SETSTRIPE -c 1 $TDIR
3008         createmany -o $TDIR/f $files
3009
3010         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3011
3012         # we assume that with $OSTCOUNT files, at least one of them will
3013         # be allocated on OST0.
3014         warmup_files=$((OSTCOUNT * max_dirty_mb))
3015         createmany -o $TDIR/w $warmup_files
3016
3017         # write a large amount of data into one file and sync, to get good
3018         # avail_grant number from OST.
3019         for ((i=0; i<$warmup_files; i++)); do
3020                 idx=$($GETSTRIPE -i $TDIR/w$i)
3021                 [ $idx -ne 0 ] && continue
3022                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3023                 break
3024         done
3025         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3026         sync
3027         $LCTL get_param $proc_osc0/cur_dirty_bytes
3028         $LCTL get_param $proc_osc0/cur_grant_bytes
3029
3030         # create as much dirty pages as we can while not to trigger the actual
3031         # RPCs directly. but depends on the env, VFS may trigger flush during this
3032         # period, hopefully we are good.
3033         for ((i=0; i<$warmup_files; i++)); do
3034                 idx=$($GETSTRIPE -i $TDIR/w$i)
3035                 [ $idx -ne 0 ] && continue
3036                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3037         done
3038         $LCTL get_param $proc_osc0/cur_dirty_bytes
3039         $LCTL get_param $proc_osc0/cur_grant_bytes
3040
3041         # perform the real test
3042         $LCTL set_param $proc_osc0/rpc_stats 0
3043         for ((;i<$files; i++)); do
3044                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3045                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3046         done
3047         sync
3048         $LCTL get_param $proc_osc0/rpc_stats
3049
3050         local percent=0
3051         local have_ppr=false
3052         $LCTL get_param $proc_osc0/rpc_stats |
3053                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3054                         # skip lines until we are at the RPC histogram data
3055                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3056                         $have_ppr || continue
3057
3058                         # we only want the percent stat for < 16 pages
3059                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3060
3061                         percent=$((percent + WPCT))
3062                         if [ $percent -gt 15 ]; then
3063                                 error "less than 16-pages write RPCs" \
3064                                       "$percent% > 15%"
3065                                 break
3066                         fi
3067                 done
3068         rm -rf $TDIR
3069 }
3070 run_test 42e "verify sub-RPC writes are not done synchronously"
3071
3072 test_43() {
3073         mkdir -p $DIR/$tdir
3074         cp -p /bin/ls $DIR/$tdir/$tfile
3075         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3076         pid=$!
3077         # give multiop a chance to open
3078         sleep 1
3079
3080         $DIR/$tdir/$tfile && error || true
3081         kill -USR1 $pid
3082 }
3083 run_test 43 "execution of file opened for write should return -ETXTBSY"
3084
3085 test_43a() {
3086         mkdir -p $DIR/d43
3087         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
3088         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
3089         MULTIOP_PID=$!
3090         $MULTIOP $DIR/d43/multiop Oc && error "expected error, got success"
3091         kill -USR1 $MULTIOP_PID || return 2
3092         wait $MULTIOP_PID || return 3
3093         rm $TMP/test43.junk
3094 }
3095 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3096
3097 test_43b() {
3098         mkdir -p $DIR/d43
3099         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
3100         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
3101         MULTIOP_PID=$!
3102         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
3103         kill -USR1 $MULTIOP_PID || return 2
3104         wait $MULTIOP_PID || return 3
3105         rm $TMP/test43.junk
3106 }
3107 run_test 43b "truncate of file being executed should return -ETXTBSY"
3108
3109 test_43c() {
3110         local testdir="$DIR/d43c"
3111         mkdir -p $testdir
3112         cp $SHELL $testdir/
3113         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3114                 ( cd $testdir && md5sum -c)
3115 }
3116 run_test 43c "md5sum of copy into lustre========================"
3117
3118 test_44() {
3119         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3120         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3121         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3122 }
3123 run_test 44 "zero length read from a sparse stripe ============="
3124
3125 test_44a() {
3126     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3127                          awk '{print $2}'`
3128     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3129     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3130     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3131                       awk '{print $2}'`
3132     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3133         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3134     fi
3135
3136     OFFSETS="0 $((stride/2)) $((stride-1))"
3137     for offset in $OFFSETS ; do
3138       for i in `seq 0 $((nstripe-1))`; do
3139         local GLOBALOFFSETS=""
3140         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3141         local myfn=$DIR/d44a-$size
3142         echo "--------writing $myfn at $size"
3143         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3144         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3145         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3146                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3147
3148         for j in `seq 0 $((nstripe-1))`; do
3149             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3150             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3151             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3152         done
3153         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3154                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3155         rm -f $myfn
3156       done
3157     done
3158 }
3159 run_test 44a "test sparse pwrite ==============================="
3160
3161 dirty_osc_total() {
3162         tot=0
3163         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3164                 tot=$(($tot + $d))
3165         done
3166         echo $tot
3167 }
3168 do_dirty_record() {
3169         before=`dirty_osc_total`
3170         echo executing "\"$*\""
3171         eval $*
3172         after=`dirty_osc_total`
3173         echo before $before, after $after
3174 }
3175 test_45() {
3176         f="$DIR/f45"
3177         # Obtain grants from OST if it supports it
3178         echo blah > ${f}_grant
3179         stop_writeback
3180         sync
3181         do_dirty_record "echo blah > $f"
3182         [ $before -eq $after ] && error "write wasn't cached"
3183         do_dirty_record "> $f"
3184         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3185         do_dirty_record "echo blah > $f"
3186         [ $before -eq $after ] && error "write wasn't cached"
3187         do_dirty_record "sync"
3188         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3189         do_dirty_record "echo blah > $f"
3190         [ $before -eq $after ] && error "write wasn't cached"
3191         do_dirty_record "cancel_lru_locks osc"
3192         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3193         start_writeback
3194 }
3195 run_test 45 "osc io page accounting ============================"
3196
3197 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3198 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3199 # objects offset and an assert hit when an rpc was built with 1023's mapped
3200 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3201 test_46() {
3202         f="$DIR/f46"
3203         stop_writeback
3204         sync
3205         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3206         sync
3207         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3208         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3209         sync
3210         start_writeback
3211 }
3212 run_test 46 "dirtying a previously written page ================"
3213
3214 # test_47 is removed "Device nodes check" is moved to test_28
3215
3216 test_48a() { # bug 2399
3217         check_kernel_version 34 || return 0
3218         mkdir -p $DIR/d48a
3219         cd $DIR/d48a
3220         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
3221         mkdir $DIR/d48a || error "recreate directory failed"
3222         touch foo || error "'touch foo' failed after recreating cwd"
3223         mkdir bar || error "'mkdir foo' failed after recreating cwd"
3224         if check_kernel_version 44; then
3225                 touch .foo || error "'touch .foo' failed after recreating cwd"
3226                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3227         fi
3228         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3229         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3230         cd . || error "'cd .' failed after recreating cwd"
3231         mkdir . && error "'mkdir .' worked after recreating cwd"
3232         rmdir . && error "'rmdir .' worked after recreating cwd"
3233         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3234         cd .. || error "'cd ..' failed after recreating cwd"
3235 }
3236 run_test 48a "Access renamed working dir (should return errors)="
3237
3238 test_48b() { # bug 2399
3239         check_kernel_version 34 || return 0
3240         mkdir -p $DIR/d48b
3241         cd $DIR/d48b
3242         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
3243         touch foo && error "'touch foo' worked after removing cwd"
3244         mkdir foo && error "'mkdir foo' worked after removing cwd"
3245         if check_kernel_version 44; then
3246                 touch .foo && error "'touch .foo' worked after removing cwd"
3247                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3248         fi
3249         ls . > /dev/null && error "'ls .' worked after removing cwd"
3250         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3251         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3252         mkdir . && error "'mkdir .' worked after removing cwd"
3253         rmdir . && error "'rmdir .' worked after removing cwd"
3254         ln -s . foo && error "'ln -s .' worked after removing cwd"
3255         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3256 }
3257 run_test 48b "Access removed working dir (should return errors)="
3258
3259 test_48c() { # bug 2350
3260         check_kernel_version 36 || return 0
3261         #lctl set_param debug=-1
3262         #set -vx
3263         mkdir -p $DIR/d48c/dir
3264         cd $DIR/d48c/dir
3265         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
3266         $TRACE touch foo && error "'touch foo' worked after removing cwd"
3267         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
3268         if check_kernel_version 44; then
3269                 touch .foo && error "'touch .foo' worked after removing cwd"
3270                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3271         fi
3272         $TRACE ls . && error "'ls .' worked after removing cwd"
3273         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3274         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
3275         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
3276         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3277         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3278         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3279 }
3280 run_test 48c "Access removed working subdir (should return errors)"
3281
3282 test_48d() { # bug 2350
3283         check_kernel_version 36 || return 0
3284         #lctl set_param debug=-1
3285         #set -vx
3286         mkdir -p $DIR/d48d/dir
3287         cd $DIR/d48d/dir
3288         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
3289         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
3290         $TRACE touch foo && error "'touch foo' worked after removing parent"
3291         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
3292         if check_kernel_version 44; then
3293                 touch .foo && error "'touch .foo' worked after removing parent"
3294                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
3295         fi
3296         $TRACE ls . && error "'ls .' worked after removing parent"
3297         $TRACE ls .. && error "'ls ..' worked after removing parent"
3298         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
3299         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
3300         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3301         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3302         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
3303 }
3304 run_test 48d "Access removed parent subdir (should return errors)"
3305
3306 test_48e() { # bug 4134
3307         check_kernel_version 41 || return 0
3308         #lctl set_param debug=-1
3309         #set -vx
3310         mkdir -p $DIR/d48e/dir
3311         cd $DIR/d48e/dir
3312         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3313         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3314         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3315         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3316         # On a buggy kernel addition of "touch foo" after cd .. will
3317         # produce kernel oops in lookup_hash_it
3318         touch ../foo && error "'cd ..' worked after recreate parent"
3319         cd $DIR
3320         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3321 }
3322 run_test 48e "Access to recreated parent subdir (should return errors)"
3323
3324 test_49() { # LU-1030
3325         # get ost1 size - lustre-OST0000
3326         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3327         # write 800M at maximum
3328         [ $ost1_size -gt 819200 ] && ost1_size=819200
3329
3330         lfs setstripe -c 1 -i 0 $DIR/$tfile
3331         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3332         local dd_pid=$!
3333
3334         # change max_pages_per_rpc while writing the file
3335         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3336         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3337         # loop until dd process exits
3338         while ps ax -opid | grep -wq $dd_pid; do
3339                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3340                 sleep $((RANDOM % 5 + 1))
3341         done
3342         # restore original max_pages_per_rpc
3343         $LCTL set_param $osc1_mppc=$orig_mppc
3344         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3345 }
3346 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3347
3348 test_50() {
3349         # bug 1485
3350         mkdir $DIR/d50
3351         cd $DIR/d50
3352         ls /proc/$$/cwd || error
3353 }
3354 run_test 50 "special situations: /proc symlinks  ==============="
3355
3356 test_51a() {    # was test_51
3357         # bug 1516 - create an empty entry right after ".." then split dir
3358         mkdir $DIR/d51
3359         touch $DIR/d51/foo
3360         $MCREATE $DIR/d51/bar
3361         rm $DIR/d51/foo
3362         createmany -m $DIR/d51/longfile 201
3363         FNUM=202
3364         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3365                 $MCREATE $DIR/d51/longfile$FNUM
3366                 FNUM=$(($FNUM + 1))
3367                 echo -n "+"
3368         done
3369         echo
3370         ls -l $DIR/d51 > /dev/null || error
3371 }
3372 run_test 51a "special situations: split htree with empty entry =="
3373
3374 export NUMTEST=70000
3375 test_51b() {
3376         local BASE=$DIR/$tdir
3377         mkdir -p $BASE
3378
3379         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3380         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3381         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3382                 return
3383
3384         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3385                 echo "reduced count to $NUMTEST due to inodes"
3386
3387         # need to check free space for the directories as well
3388         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3389         numfree=$((blkfree / 4))
3390         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3391                 echo "reduced count to $NUMTEST due to blocks"
3392
3393         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3394                 echo "failed" > $BASE/fnum
3395 }
3396 run_test 51b "exceed 64k subdirectory nlink limit"
3397
3398 test_51ba() { # LU-993
3399         local BASE=$DIR/$tdir
3400         # unlink all but 100 subdirectories, then check it still works
3401         local LEFT=100
3402         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3403
3404         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3405         local DELETE=$((NUMTEST - LEFT))
3406
3407         # continue on to run this test even if 51b didn't finish,
3408         # just to delete the many subdirectories created.
3409         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3410
3411         # for ldiskfs the nlink count should be 1, but this is OSD specific
3412         # and so this is listed for informational purposes only
3413         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3414         unlinkmany -d $BASE/d $DELETE
3415         RC=$?
3416
3417         if [ $RC -ne 0 ]; then
3418                 if [ "$NUMPREV" == "failed" ]; then
3419                         skip "previous setup failed"
3420                         return 0
3421                 else
3422                         error "unlink of first $DELETE subdirs failed"
3423                         return $RC
3424                 fi
3425         fi
3426
3427         echo "nlink between: $(stat -c %h $BASE)"
3428         # trim the first line of ls output
3429         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3430         [ $FOUND -ne $LEFT ] &&
3431                 error "can't find subdirs: found only $FOUND/$LEFT"
3432
3433         unlinkmany -d $BASE/d $DELETE $LEFT ||
3434                 error "unlink of second $LEFT subdirs failed"
3435         # regardless of whether the backing filesystem tracks nlink accurately
3436         # or not, the nlink count shouldn't be more than "." and ".." here
3437         local AFTER=$(stat -c %h $BASE)
3438         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3439                 echo "nlink after: $AFTER"
3440 }
3441 run_test 51ba "verify nlink for many subdirectory cleanup"
3442
3443 test_51d() {
3444         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3445         mkdir -p $DIR/d51d
3446         createmany -o $DIR/d51d/t- 1000
3447         $GETSTRIPE $DIR/d51d > $TMP/files
3448         for N in `seq 0 $((OSTCOUNT - 1))`; do
3449             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3450             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3451             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3452         done
3453         unlinkmany $DIR/d51d/t- 1000
3454
3455         NLAST=0
3456         for N in `seq 1 $((OSTCOUNT - 1))`; do
3457             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3458                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3459             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3460                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3461
3462             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3463                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3464             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3465                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3466             NLAST=$N
3467         done
3468 }
3469 run_test 51d "check object distribution ===================="
3470
3471 test_52a() {
3472         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3473         mkdir -p $DIR/d52a
3474         touch $DIR/d52a/foo
3475         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3476         echo bar >> $DIR/d52a/foo || error "append bar failed"
3477         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3478         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3479         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3480         echo foo >> $DIR/d52a/foo || error "append foo failed"
3481         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3482         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3483         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3484         cp -r $DIR/d52a /tmp/
3485         rm -fr $DIR/d52a || error "cleanup rm failed"
3486 }
3487 run_test 52a "append-only flag test (should return errors) ====="
3488
3489 test_52b() {
3490         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3491         mkdir -p $DIR/d52b
3492         touch $DIR/d52b/foo
3493         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3494         cat test > $DIR/d52b/foo && error "cat test worked"
3495         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3496         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3497         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3498         echo foo >> $DIR/d52b/foo && error "echo worked"
3499         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3500         [ -f $DIR/d52b/foo ] || error
3501         [ -f $DIR/d52b/foo_ren ] && error
3502         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3503         chattr -i $DIR/d52b/foo || error "chattr failed"
3504
3505         rm -fr $DIR/d52b || error
3506 }
3507 run_test 52b "immutable flag test (should return errors) ======="
3508
3509 test_53() {
3510         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3511         remote_ost_nodsh && skip "remote OST with nodsh" && return
3512
3513         local param
3514         local ostname
3515         local mds_last
3516         local ost_last
3517         local ostnum
3518
3519         # only test MDT0000
3520         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3521         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3522                 param=`echo ${value[0]} | cut -d "=" -f1`
3523                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3524                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3525                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3526                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3527                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3528                 if [ $ost_last != $mds_last ]; then
3529                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3530                 fi
3531         done
3532 }
3533 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3534
3535 test_54a() {
3536         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3537         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3538         $SOCKETSERVER $DIR/socket
3539         $SOCKETCLIENT $DIR/socket || error
3540         $MUNLINK $DIR/socket
3541 }
3542 run_test 54a "unix domain socket test =========================="
3543
3544 test_54b() {
3545         f="$DIR/f54b"
3546         mknod $f c 1 3
3547         chmod 0666 $f
3548         dd if=/dev/zero of=$f bs=`page_size` count=1
3549 }
3550 run_test 54b "char device works in lustre ======================"
3551
3552 find_loop_dev() {
3553         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3554         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3555         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3556
3557         for i in `seq 3 7`; do
3558                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3559                 LOOPDEV=$LOOPBASE$i
3560                 LOOPNUM=$i
3561                 break
3562         done
3563 }
3564
3565 test_54c() {
3566         tfile="$DIR/f54c"
3567         tdir="$DIR/d54c"
3568         loopdev="$DIR/loop54c"
3569
3570         find_loop_dev
3571         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3572         mknod $loopdev b 7 $LOOPNUM
3573         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3574         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3575         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3576         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3577         mkdir -p $tdir
3578         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3579         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3580         df $tdir
3581         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3582         $UMOUNT $tdir
3583         losetup -d $loopdev
3584         rm $loopdev
3585 }
3586 run_test 54c "block device works in lustre ====================="
3587
3588 test_54d() {
3589         f="$DIR/f54d"
3590         string="aaaaaa"
3591         mknod $f p
3592         [ "$string" = `echo $string > $f | cat $f` ] || error
3593 }
3594 run_test 54d "fifo device works in lustre ======================"
3595
3596 test_54e() {
3597         check_kernel_version 46 || return 0
3598         f="$DIR/f54e"
3599         string="aaaaaa"
3600         cp -aL /dev/console $f
3601         echo $string > $f || error
3602 }
3603 run_test 54e "console/tty device works in lustre ======================"
3604
3605 #The test_55 used to be iopen test and it was removed by bz#24037.
3606 #run_test 55 "check iopen_connect_dentry() ======================"
3607
3608 test_56a() {    # was test_56
3609         rm -rf $DIR/d56
3610         $SETSTRIPE -d $DIR
3611         mkdir $DIR/d56
3612         mkdir $DIR/d56/dir
3613         NUMFILES=3
3614         NUMFILESx2=$(($NUMFILES * 2))
3615         for i in `seq 1 $NUMFILES` ; do
3616                 touch $DIR/d56/file$i
3617                 touch $DIR/d56/dir/file$i
3618         done
3619
3620         # test lfs getstripe with --recursive
3621         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3622         [ $FILENUM -eq $NUMFILESx2 ] ||
3623                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3624         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3625         [ $FILENUM -eq $NUMFILES ] ||
3626                 error "$GETSTRIPE $DIR/d56: found $FILENUM, not $NUMFILES"
3627         echo "$GETSTRIPE --recursive passed."
3628
3629         # test lfs getstripe with file instead of dir
3630         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3631         [ $FILENUM  -eq 1 ] || error \
3632                  "$GETSTRIPE $DIR/d56/file1: found $FILENUM, not 1"
3633         echo "$GETSTRIPE file1 passed."
3634
3635         #test lfs getstripe with --verbose
3636         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||
3637                 error "$GETSTRIPE --verbose $DIR/d56: want $NUMFILES lmm_magic"
3638         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] ||
3639             error "$GETSTRIPE $DIR/d56: showed lmm_magic"
3640         echo "$GETSTRIPE --verbose passed."
3641
3642         #test lfs getstripe with --obd
3643         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" ||
3644                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3645
3646         [  "$OSTCOUNT" -lt 2 ] &&
3647                 skip_env "skipping other $GETSTRIPE --obd test" && return
3648
3649         OSTIDX=1
3650         OBDUUID=$(ostuuid_from_index $OSTIDX)
3651         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3652         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3653         [ $FOUND -eq $FILENUM ] ||
3654                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3655         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 |
3656                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3657                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3658                 error "$GETSTRIPE --obd: should not show file on other obd"
3659         echo "$GETSTRIPE --obd passed"
3660 }
3661 run_test 56a "check $GETSTRIPE"
3662
3663 NUMFILES=3
3664 NUMDIRS=3
3665 setup_56() {
3666         local LOCAL_NUMFILES="$1"
3667         local LOCAL_NUMDIRS="$2"
3668         local MKDIR_PARAMS="$3"
3669
3670         if [ ! -d "$TDIR" ] ; then
3671                 mkdir -p $TDIR
3672                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3673                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3674                         touch $TDIR/file$i
3675                 done
3676                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3677                         mkdir $TDIR/dir$i
3678                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3679                                 touch $TDIR/dir$i/file$j
3680                         done
3681                 done
3682         fi
3683 }
3684
3685 setup_56_special() {
3686         LOCAL_NUMFILES=$1
3687         LOCAL_NUMDIRS=$2
3688         setup_56 $1 $2
3689         if [ ! -e "$TDIR/loop1b" ] ; then
3690                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3691                         mknod $TDIR/loop${i}b b 7 $i
3692                         mknod $TDIR/null${i}c c 1 3
3693                         ln -s $TDIR/file1 $TDIR/link${i}l
3694                 done
3695                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3696                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3697                         mknod $TDIR/dir$i/null${i}c c 1 3
3698                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3699                 done
3700         fi
3701 }
3702
3703 test_56g() {
3704         $SETSTRIPE -d $DIR
3705
3706         TDIR=$DIR/${tdir}g
3707         setup_56 $NUMFILES $NUMDIRS
3708
3709         EXPECTED=$(($NUMDIRS + 2))
3710         # test lfs find with -name
3711         for i in $(seq 1 $NUMFILES) ; do
3712                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3713                 [ $NUMS -eq $EXPECTED ] ||
3714                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3715                               "found $NUMS, expected $EXPECTED"
3716         done
3717 }
3718 run_test 56g "check lfs find -name ============================="
3719
3720 test_56h() {
3721         $SETSTRIPE -d $DIR
3722
3723         TDIR=$DIR/${tdir}g
3724         setup_56 $NUMFILES $NUMDIRS
3725
3726         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3727         # test lfs find with ! -name
3728         for i in $(seq 1 $NUMFILES) ; do
3729                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3730                 [ $NUMS -eq $EXPECTED ] ||
3731                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3732                               "found $NUMS, expected $EXPECTED"
3733         done
3734 }
3735 run_test 56h "check lfs find ! -name ============================="
3736
3737 test_56i() {
3738        tdir=${tdir}i
3739        mkdir -p $DIR/$tdir
3740        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3741        CMD="$LFIND -ost $UUID $DIR/$tdir"
3742        OUT=$($CMD)
3743        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3744 }
3745 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3746
3747 test_56j() {
3748         TDIR=$DIR/${tdir}g
3749         setup_56_special $NUMFILES $NUMDIRS
3750
3751         EXPECTED=$((NUMDIRS + 1))
3752         CMD="$LFIND -type d $TDIR"
3753         NUMS=$($CMD | wc -l)
3754         [ $NUMS -eq $EXPECTED ] ||
3755                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3756 }
3757 run_test 56j "check lfs find -type d ============================="
3758
3759 test_56k() {
3760         TDIR=$DIR/${tdir}g
3761         setup_56_special $NUMFILES $NUMDIRS
3762
3763         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3764         CMD="$LFIND -type f $TDIR"
3765         NUMS=$($CMD | wc -l)
3766         [ $NUMS -eq $EXPECTED ] ||
3767                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3768 }
3769 run_test 56k "check lfs find -type f ============================="
3770
3771 test_56l() {
3772         TDIR=$DIR/${tdir}g
3773         setup_56_special $NUMFILES $NUMDIRS
3774
3775         EXPECTED=$((NUMDIRS + NUMFILES))
3776         CMD="$LFIND -type b $TDIR"
3777         NUMS=$($CMD | wc -l)
3778         [ $NUMS -eq $EXPECTED ] ||
3779                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3780 }
3781 run_test 56l "check lfs find -type b ============================="
3782
3783 test_56m() {
3784         TDIR=$DIR/${tdir}g
3785         setup_56_special $NUMFILES $NUMDIRS
3786
3787         EXPECTED=$((NUMDIRS + NUMFILES))
3788         CMD="$LFIND -type c $TDIR"
3789         NUMS=$($CMD | wc -l)
3790         [ $NUMS -eq $EXPECTED ] ||
3791                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3792 }
3793 run_test 56m "check lfs find -type c ============================="
3794
3795 test_56n() {
3796         TDIR=$DIR/${tdir}g
3797         setup_56_special $NUMFILES $NUMDIRS
3798
3799         EXPECTED=$((NUMDIRS + NUMFILES))
3800         CMD="$LFIND -type l $TDIR"
3801         NUMS=$($CMD | wc -l)
3802         [ $NUMS -eq $EXPECTED ] ||
3803                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3804 }
3805 run_test 56n "check lfs find -type l ============================="
3806
3807 test_56o() {
3808         TDIR=$DIR/${tdir}o
3809         setup_56 $NUMFILES $NUMDIRS
3810
3811         utime $TDIR/file1 > /dev/null || error "utime (1)"
3812         utime $TDIR/file2 > /dev/null || error "utime (2)"
3813         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3814         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3815         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3816         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3817
3818         EXPECTED=4
3819         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3820         [ $NUMS -eq $EXPECTED ] || \
3821                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3822
3823         EXPECTED=12
3824         CMD="$LFIND -mtime 0 $TDIR"
3825         NUMS=$($CMD | wc -l)
3826         [ $NUMS -eq $EXPECTED ] ||
3827                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3828 }
3829 run_test 56o "check lfs find -mtime for old files =========================="
3830
3831 test_56p() {
3832         [ $RUNAS_ID -eq $UID ] &&
3833                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3834
3835         TDIR=$DIR/${tdir}p
3836         setup_56 $NUMFILES $NUMDIRS
3837
3838         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3839         EXPECTED=$NUMFILES
3840         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3841         NUMS=$($CMD | wc -l)
3842         [ $NUMS -eq $EXPECTED ] || \
3843                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3844
3845         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3846         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3847         NUMS=$($CMD | wc -l)
3848         [ $NUMS -eq $EXPECTED ] || \
3849                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3850 }
3851 run_test 56p "check lfs find -uid and ! -uid ==============================="
3852
3853 test_56q() {
3854         [ $RUNAS_ID -eq $UID ] &&
3855                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3856
3857         TDIR=$DIR/${tdir}q
3858         setup_56 $NUMFILES $NUMDIRS
3859
3860         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3861
3862         EXPECTED=$NUMFILES
3863         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3864         NUMS=$($CMD | wc -l)
3865         [ $NUMS -eq $EXPECTED ] ||
3866                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3867
3868         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3869         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
3870         NUMS=$($CMD | wc -l)
3871         [ $NUMS -eq $EXPECTED ] ||
3872                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3873 }
3874 run_test 56q "check lfs find -gid and ! -gid ==============================="
3875
3876 test_56r() {
3877         TDIR=$DIR/${tdir}r
3878         setup_56 $NUMFILES $NUMDIRS
3879
3880         EXPECTED=12
3881         CMD="$LFIND -size 0 -type f $TDIR"
3882         NUMS=$($CMD | wc -l)
3883         [ $NUMS -eq $EXPECTED ] ||
3884                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3885         EXPECTED=0
3886         CMD="$LFIND ! -size 0 -type f $TDIR"
3887         NUMS=$($CMD | wc -l)
3888         [ $NUMS -eq $EXPECTED ] ||
3889                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3890         echo "test" > $TDIR/$tfile
3891         echo "test2" > $TDIR/$tfile.2 && sync
3892         EXPECTED=1
3893         CMD="$LFIND -size 5 -type f $TDIR"
3894         NUMS=$($CMD | wc -l)
3895         [ $NUMS -eq $EXPECTED ] ||
3896                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3897         EXPECTED=1
3898         CMD="$LFIND -size +5 -type f $TDIR"
3899         NUMS=$($CMD | wc -l)
3900         [ $NUMS -eq $EXPECTED ] ||
3901                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3902         EXPECTED=2
3903         CMD="$LFIND -size +0 -type f $TDIR"
3904         NUMS=$($CMD | wc -l)
3905         [ $NUMS -eq $EXPECTED ] ||
3906                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3907         EXPECTED=2
3908         CMD="$LFIND ! -size -5 -type f $TDIR"
3909         NUMS=$($CMD | wc -l)
3910         [ $NUMS -eq $EXPECTED ] ||
3911                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3912         EXPECTED=12
3913         CMD="$LFIND -size -5 -type f $TDIR"
3914         NUMS=$($CMD | wc -l)
3915         [ $NUMS -eq $EXPECTED ] ||
3916                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3917 }
3918 run_test 56r "check lfs find -size works =========================="
3919
3920 test_56s() { # LU-611
3921         TDIR=$DIR/${tdir}s
3922         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3923
3924         if [ $OSTCOUNT -gt 1 ]; then
3925                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3926                 ONESTRIPE=4
3927                 EXTRA=4
3928         else
3929                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3930                 EXTRA=0
3931         fi
3932
3933         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3934         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3935         NUMS=$($CMD | wc -l)
3936         [ $NUMS -eq $EXPECTED ] ||
3937                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3938
3939         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
3940         CMD="$LFIND -stripe-count +0 -type f $TDIR"
3941         NUMS=$($CMD | wc -l)
3942         [ $NUMS -eq $EXPECTED ] ||
3943                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3944
3945         EXPECTED=$ONESTRIPE
3946         CMD="$LFIND -stripe-count 1 -type f $TDIR"
3947         NUMS=$($CMD | wc -l)
3948         [ $NUMS -eq $EXPECTED ] ||
3949                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3950
3951         CMD="$LFIND -stripe-count -2 -type f $TDIR"
3952         NUMS=$($CMD | wc -l)
3953         [ $NUMS -eq $EXPECTED ] ||
3954                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3955
3956         EXPECTED=0
3957         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
3958         NUMS=$($CMD | wc -l)
3959         [ $NUMS -eq $EXPECTED ] ||
3960                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3961 }
3962 run_test 56s "check lfs find -stripe-count works"
3963
3964 test_56t() { # LU-611
3965         TDIR=$DIR/${tdir}t
3966         setup_56 $NUMFILES $NUMDIRS "-s 512k"
3967
3968         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
3969
3970         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3971         CMD="$LFIND -stripe-size 512k -type f $TDIR"
3972         NUMS=$($CMD | wc -l)
3973         [ $NUMS -eq $EXPECTED ] ||
3974                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3975
3976         CMD="$LFIND -stripe-size +320k -type f $TDIR"
3977         NUMS=$($CMD | wc -l)
3978         [ $NUMS -eq $EXPECTED ] ||
3979                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3980
3981         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
3982         CMD="$LFIND -stripe-size +200k -type f $TDIR"
3983         NUMS=$($CMD | wc -l)
3984         [ $NUMS -eq $EXPECTED ] ||
3985                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3986
3987         CMD="$LFIND -stripe-size -640k -type f $TDIR"
3988         NUMS=$($CMD | wc -l)
3989         [ $NUMS -eq $EXPECTED ] ||
3990                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3991
3992         EXPECTED=4
3993         CMD="$LFIND -stripe-size 256k -type f $TDIR"
3994         NUMS=$($CMD | wc -l)
3995         [ $NUMS -eq $EXPECTED ] ||
3996                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3997
3998         CMD="$LFIND -stripe-size -320k -type f $TDIR"
3999         NUMS=$($CMD | wc -l)
4000         [ $NUMS -eq $EXPECTED ] ||
4001                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4002
4003         EXPECTED=0
4004         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4005         NUMS=$($CMD | wc -l)
4006         [ $NUMS -eq $EXPECTED ] ||
4007                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4008 }
4009 run_test 56t "check lfs find -stripe-size works"
4010
4011 test_56u() { # LU-611
4012         TDIR=$DIR/${tdir}u
4013         setup_56 $NUMFILES $NUMDIRS "-i 0"
4014
4015         if [ $OSTCOUNT -gt 1 ]; then
4016                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4017                 ONESTRIPE=4
4018         else
4019                 ONESTRIPE=0
4020         fi
4021
4022         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4023         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4024         NUMS=$($CMD | wc -l)
4025         [ $NUMS -eq $EXPECTED ] ||
4026                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4027
4028         EXPECTED=$ONESTRIPE
4029         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4030         NUMS=$($CMD | wc -l)
4031         [ $NUMS -eq $EXPECTED ] ||
4032                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4033
4034         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4035         NUMS=$($CMD | wc -l)
4036         [ $NUMS -eq $EXPECTED ] ||
4037                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4038
4039         EXPECTED=0
4040         # This should produce an error and not return any files
4041         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4042         NUMS=$($CMD 2>/dev/null | wc -l)
4043         [ $NUMS -eq $EXPECTED ] ||
4044                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4045
4046         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4047         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4048         NUMS=$($CMD | wc -l)
4049         [ $NUMS -eq $EXPECTED ] ||
4050                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4051 }
4052 run_test 56u "check lfs find -stripe-index works"
4053
4054 test_56v() {
4055     local MDT_IDX=0
4056
4057     TDIR=$DIR/${tdir}v
4058     rm -rf $TDIR
4059     setup_56 $NUMFILES $NUMDIRS
4060
4061     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4062     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4063
4064     for file in $($LFIND -mdt $UUID $TDIR); do
4065         file_mdt_idx=$($GETSTRIPE -M $file)
4066         [ $file_mdt_idx -eq $MDT_IDX ] ||
4067             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4068     done
4069 }
4070 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4071
4072 # Get and check the actual stripe count of one file.
4073 # Usage: check_stripe_count <file> <expected_stripe_count>
4074 check_stripe_count() {
4075     local file=$1
4076     local expected=$2
4077     local actual
4078
4079     [[ -z "$file" || -z "$expected" ]] &&
4080         error "check_stripe_count: invalid argument!"
4081
4082     local cmd="$GETSTRIPE -c $file"
4083     actual=$($cmd) || error "$cmd failed"
4084     actual=${actual%% *}
4085
4086     if [[ $actual -ne $expected ]]; then
4087         [[ $expected -eq -1 ]] ||
4088             error "$cmd wrong: found $actual, expected $expected"
4089         [[ $actual -eq $OSTCOUNT ]] ||
4090             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4091     fi
4092 }
4093
4094 test_56w() {
4095     TDIR=$DIR/${tdir}w
4096
4097     rm -rf $TDIR || error "remove $TDIR failed"
4098     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4099
4100     local stripe_size
4101     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4102         error "$GETSTRIPE -S -d $TDIR failed"
4103     stripe_size=${stripe_size%% *}
4104
4105     local file_size=$((stripe_size * OSTCOUNT))
4106     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4107     local required_space=$((file_num * file_size))
4108     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4109     [[ $free_space -le $((required_space / 1024)) ]] &&
4110         skip_env "need at least $required_space bytes free space," \
4111                  "have $free_space kbytes" && return
4112
4113     local dd_bs=65536
4114     local dd_count=$((file_size / dd_bs))
4115
4116     # write data into the files
4117     local i
4118     local j
4119     local file
4120     for i in $(seq 1 $NUMFILES); do
4121         file=$TDIR/file$i
4122         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4123             error "write data into $file failed"
4124     done
4125     for i in $(seq 1 $NUMDIRS); do
4126         for j in $(seq 1 $NUMFILES); do
4127             file=$TDIR/dir$i/file$j
4128             yes | dd bs=$dd_bs count=$dd_count of=$file \
4129                 >/dev/null 2>&1 ||
4130                 error "write data into $file failed"
4131         done
4132     done
4133
4134     local expected=-1
4135     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4136
4137     # lfs_migrate file
4138     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4139     echo "$cmd"
4140     eval $cmd || error "$cmd failed"
4141
4142     check_stripe_count $TDIR/file1 $expected
4143
4144     # lfs_migrate dir
4145     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4146     echo "$cmd"
4147     eval $cmd || error "$cmd failed"
4148
4149     for j in $(seq 1 $NUMFILES); do
4150         check_stripe_count $TDIR/dir1/file$j $expected
4151     done
4152
4153     # lfs_migrate works with lfs find
4154     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4155          $LFS_MIGRATE -y -c $expected"
4156     echo "$cmd"
4157     eval $cmd || error "$cmd failed"
4158
4159     for i in $(seq 2 $NUMFILES); do
4160         check_stripe_count $TDIR/file$i $expected
4161     done
4162     for i in $(seq 2 $NUMDIRS); do
4163         for j in $(seq 1 $NUMFILES); do
4164             check_stripe_count $TDIR/dir$i/file$j $expected
4165         done
4166     done
4167 }
4168 run_test 56w "check lfs_migrate -c stripe_count works"
4169
4170 test_57a() {
4171         # note test will not do anything if MDS is not local
4172         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4173                 skip "Only applicable to ldiskfs-based MDTs"
4174                 return
4175         fi
4176
4177         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4178         local MNTDEV="osd*.*MDT*.mntdev"
4179         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4180         [ -z "$DEV" ] && error "can't access $MNTDEV"
4181         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4182                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4183                         error "can't access $DEV"
4184                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4185                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4186                 rm $TMP/t57a.dump
4187         done
4188 }
4189 run_test 57a "verify MDS filesystem created with large inodes =="
4190
4191 test_57b() {
4192         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4193                 skip "Only applicable to ldiskfs-based MDTs"
4194                 return
4195         fi
4196
4197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4198         local dir=$DIR/d57b
4199
4200         local FILECOUNT=100
4201         local FILE1=$dir/f1
4202         local FILEN=$dir/f$FILECOUNT
4203
4204         rm -rf $dir || error "removing $dir"
4205         mkdir -p $dir || error "creating $dir"
4206         local num=$(get_mds_dir $dir)
4207         local mymds=mds$num
4208
4209         echo "mcreating $FILECOUNT files"
4210         createmany -m $dir/f 1 $FILECOUNT || \
4211                 error "creating files in $dir"
4212
4213         # verify that files do not have EAs yet
4214         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4215         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4216
4217         sync
4218         sleep 1
4219         df $dir  #make sure we get new statfs data
4220         local MDSFREE=$(do_facet $mymds \
4221                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4222         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4223         echo "opening files to create objects/EAs"
4224         local FILE
4225         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4226                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4227         done
4228
4229         # verify that files have EAs now
4230         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4231         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4232
4233         sleep 1  #make sure we get new statfs data
4234         df $dir
4235         local MDSFREE2=$(do_facet $mymds \
4236                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4237         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4238         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4239                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4240                         error "MDC before $MDCFREE != after $MDCFREE2"
4241                 else
4242                         echo "MDC before $MDCFREE != after $MDCFREE2"
4243                         echo "unable to confirm if MDS has large inodes"
4244                 fi
4245         fi
4246         rm -rf $dir
4247 }
4248 run_test 57b "default LOV EAs are stored inside large inodes ==="
4249
4250 test_58() {
4251     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
4252     wiretest
4253 }
4254 run_test 58 "verify cross-platform wire constants =============="
4255
4256 test_59() {
4257         echo "touch 130 files"
4258         createmany -o $DIR/f59- 130
4259         echo "rm 130 files"
4260         unlinkmany $DIR/f59- 130
4261         sync
4262         # wait for commitment of removal
4263         wait_delete_completed
4264 }
4265 run_test 59 "verify cancellation of llog records async ========="
4266
4267 TEST60_HEAD="test_60 run $RANDOM"
4268 test_60a() {
4269         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4270         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4271         log "$TEST60_HEAD - from kernel mode"
4272         do_facet mgs sh run-llog.sh
4273 }
4274 run_test 60a "llog sanity tests run from kernel module =========="
4275
4276 test_60b() { # bug 6411
4277         dmesg > $DIR/$tfile
4278         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4279                                  /llog.test/ {
4280                                          if (marker)
4281                                                  from_marker++
4282                                          from_begin++
4283                                  }
4284                                  END {
4285                                          if (marker)
4286                                                  print from_marker
4287                                          else
4288                                                  print from_begin
4289                                  }"`
4290         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4291 }
4292 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4293
4294 test_60c() {
4295         echo "create 5000 files"
4296         createmany -o $DIR/f60c- 5000
4297 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4298         lctl set_param fail_loc=0x80000137
4299         unlinkmany $DIR/f60c- 5000
4300         lctl set_param fail_loc=0
4301 }
4302 run_test 60c "unlink file when mds full"
4303
4304 test_60d() {
4305         SAVEPRINTK=$(lctl get_param -n printk)
4306
4307         # verify "lctl mark" is even working"
4308         MESSAGE="test message ID $RANDOM $$"
4309         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4310         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4311
4312         lctl set_param printk=0 || error "set lnet.printk failed"
4313         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4314         MESSAGE="new test message ID $RANDOM $$"
4315         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4316         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4317         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4318
4319         lctl set_param -n printk="$SAVEPRINTK"
4320 }
4321 run_test 60d "test printk console message masking"
4322
4323 test_61() {
4324         f="$DIR/f61"
4325         dd if=/dev/zero of=$f bs=`page_size` count=1
4326         cancel_lru_locks osc
4327         $MULTIOP $f OSMWUc || error
4328         sync
4329 }
4330 run_test 61 "mmap() writes don't make sync hang ================"
4331
4332 # bug 2330 - insufficient obd_match error checking causes LBUG
4333 test_62() {
4334         f="$DIR/f62"
4335         echo foo > $f
4336         cancel_lru_locks osc
4337         lctl set_param fail_loc=0x405
4338         cat $f && error "cat succeeded, expect -EIO"
4339         lctl set_param fail_loc=0
4340 }
4341 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4342 # match every page all of the time.
4343 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4344
4345 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4346 test_63a() {    # was test_63
4347         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4348         lctl set_param -n osc.*.max_dirty_mb 0
4349         for i in `seq 10` ; do
4350                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4351                 sleep 5
4352                 kill $!
4353                 sleep 1
4354         done
4355
4356         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4357         rm -f $DIR/f63 || true
4358 }
4359 run_test 63a "Verify oig_wait interruption does not crash ======="
4360
4361 # bug 2248 - async write errors didn't return to application on sync
4362 # bug 3677 - async write errors left page locked
4363 test_63b() {
4364         debugsave
4365         lctl set_param debug=-1
4366
4367         # ensure we have a grant to do async writes
4368         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4369         rm $DIR/$tfile
4370
4371         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4372         lctl set_param fail_loc=0x80000406
4373         $MULTIOP $DIR/$tfile Owy && \
4374                 error "sync didn't return ENOMEM"
4375         sync; sleep 2; sync     # do a real sync this time to flush page
4376         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4377                 error "locked page left in cache after async error" || true
4378         debugrestore
4379 }
4380 run_test 63b "async write errors should be returned to fsync ==="
4381
4382 test_64a () {
4383         df $DIR
4384         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4385 }
4386 run_test 64a "verify filter grant calculations (in kernel) ====="
4387
4388 test_64b () {
4389         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4390         sh oos.sh $MOUNT
4391 }
4392 run_test 64b "check out-of-space detection on client ==========="
4393
4394 test_64c() {
4395         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4396 }
4397 run_test 64c "verify grant shrink ========================------"
4398
4399 # bug 1414 - set/get directories' stripe info
4400 test_65a() {
4401         mkdir -p $DIR/d65
4402         touch $DIR/d65/f1
4403         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
4404 }
4405 run_test 65a "directory with no stripe info ===================="
4406
4407 test_65b() {
4408         mkdir -p $DIR/d65
4409         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65||error "setstripe"
4410         touch $DIR/d65/f2
4411         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
4412 }
4413 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4414
4415 test_65c() {
4416         if [ $OSTCOUNT -gt 1 ]; then
4417                 mkdir -p $DIR/d65
4418                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4419                         -c $(($OSTCOUNT - 1)) $DIR/d65 || error "setstripe"
4420                 touch $DIR/d65/f3
4421                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
4422         fi
4423 }
4424 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4425
4426 test_65d() {
4427         mkdir -p $DIR/d65
4428         if [ $STRIPECOUNT -le 0 ]; then
4429                 sc=1
4430         elif [ $STRIPECOUNT -gt 2000 ]; then
4431 #LOV_MAX_STRIPE_COUNT is 2000
4432                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4433         else
4434                 sc=$(($STRIPECOUNT - 1))
4435         fi
4436         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/d65 || error "setstripe"
4437         touch $DIR/d65/f4 $DIR/d65/f5
4438         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
4439 }
4440 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4441
4442 test_65e() {
4443         mkdir -p $DIR/d65
4444
4445         $SETSTRIPE $DIR/d65 || error "setstripe"
4446         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
4447         touch $DIR/d65/f6
4448         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
4449 }
4450 run_test 65e "directory setstripe defaults ======================="
4451
4452 test_65f() {
4453         mkdir -p $DIR/d65f
4454         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
4455 }
4456 run_test 65f "dir setstripe permission (should return error) ==="
4457
4458 test_65g() {
4459         mkdir -p $DIR/d65
4460         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4461         $SETSTRIPE -d $DIR/d65 || error "setstripe"
4462         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
4463                 error "delete default stripe failed"
4464 }
4465 run_test 65g "directory setstripe -d ==========================="
4466
4467 test_65h() {
4468         mkdir -p $DIR/d65
4469         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4470         mkdir -p $DIR/d65/dd1
4471         [ $($GETSTRIPE -c $DIR/d65) == $($GETSTRIPE -c $DIR/d65/dd1) ] ||
4472                 error "stripe info inherit failed"
4473 }
4474 run_test 65h "directory stripe info inherit ===================="
4475
4476 test_65i() { # bug6367
4477         $SETSTRIPE -S 65536 -c -1 $MOUNT
4478 }
4479 run_test 65i "set non-default striping on root directory (bug 6367)="
4480
4481 test_65ia() { # bug12836
4482         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4483 }
4484 run_test 65ia "getstripe on -1 default directory striping"
4485
4486 test_65ib() { # bug12836
4487         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4488 }
4489 run_test 65ib "getstripe -v on -1 default directory striping"
4490
4491 test_65ic() { # bug12836
4492         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4493 }
4494 run_test 65ic "new find on -1 default directory striping"
4495
4496 test_65j() { # bug6367
4497         sync; sleep 1
4498         # if we aren't already remounting for each test, do so for this test
4499         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4500                 cleanup || error "failed to unmount"
4501                 setup
4502         fi
4503         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4504 }
4505 run_test 65j "set default striping on root directory (bug 6367)="
4506
4507 test_65k() { # bug11679
4508     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4509     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4510
4511     echo "Check OST status: "
4512     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4513               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4514
4515     for OSC in $MDS_OSCS; do
4516         echo $OSC "is activate"
4517         do_facet $SINGLEMDS lctl --device %$OSC activate
4518     done
4519
4520     do_facet client mkdir -p $DIR/$tdir
4521     for INACTIVE_OSC in $MDS_OSCS; do
4522         echo "Deactivate: " $INACTIVE_OSC
4523         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4524         for STRIPE_OSC in $MDS_OSCS; do
4525             OST=`osc_to_ost $STRIPE_OSC`
4526             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4527                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4528
4529             [ -f $DIR/$tdir/$IDX ] && continue
4530             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4531             do_facet client $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4532             RC=$?
4533             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4534         done
4535         do_facet client rm -f $DIR/$tdir/*
4536         echo $INACTIVE_OSC "is Activate."
4537         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4538     done
4539 }
4540 run_test 65k "validate manual striping works properly with deactivated OSCs"
4541
4542 test_65l() { # bug 12836
4543         mkdir -p $DIR/$tdir/test_dir
4544         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4545         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4546 }
4547 run_test 65l "lfs find on -1 stripe dir ========================"
4548
4549 # bug 2543 - update blocks count on client
4550 test_66() {
4551         COUNT=${COUNT:-8}
4552         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4553         sync; sync_all_data; sync; sync_all_data
4554         cancel_lru_locks osc
4555         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4556         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4557 }
4558 run_test 66 "update inode blocks count on client ==============="
4559
4560 LLOOP=
4561 LLITELOOPLOAD=
4562 cleanup_68() {
4563         trap 0
4564         if [ ! -z "$LLOOP" ]; then
4565                 if swapon -s | grep -q $LLOOP; then
4566                         swapoff $LLOOP || error "swapoff failed"
4567                 fi
4568
4569                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4570                 rm -f $LLOOP
4571                 unset LLOOP
4572         fi
4573         if [ ! -z "$LLITELOOPLOAD" ]; then
4574                 rmmod llite_lloop
4575                 unset LLITELOOPLOAD
4576         fi
4577         rm -f $DIR/f68*
4578 }
4579
4580 meminfo() {
4581         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4582 }
4583
4584 swap_used() {
4585         swapon -s | awk '($1 == "'$1'") { print $4 }'
4586 }
4587
4588 # test case for lloop driver, basic function
4589 test_68a() {
4590         [ "$UID" != 0 ] && skip_env "must run as root" && return
4591         llite_lloop_enabled || \
4592                 { skip_env "llite_lloop module disabled" && return; }
4593
4594         trap cleanup_68 EXIT
4595
4596         if ! module_loaded llite_lloop; then
4597                 if load_module llite/llite_lloop; then
4598                         LLITELOOPLOAD=yes
4599                 else
4600                         skip_env "can't find module llite_lloop"
4601                         return
4602                 fi
4603         fi
4604
4605         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4606         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4607         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4608
4609         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4610         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4611
4612         cleanup_68
4613 }
4614 run_test 68a "lloop driver - basic test ========================"
4615
4616 # excercise swapping to lustre by adding a high priority swapfile entry
4617 # and then consuming memory until it is used.
4618 test_68b() {  # was test_68
4619         [ "$UID" != 0 ] && skip_env "must run as root" && return
4620         lctl get_param -n devices | grep -q obdfilter && \
4621                 skip "local OST" && return
4622
4623         grep -q llite_lloop /proc/modules
4624         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4625
4626         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4627                 skip "can't reliably test swap with TCP" && return
4628
4629         MEMTOTAL=`meminfo MemTotal`
4630         NR_BLOCKS=$((MEMTOTAL>>8))
4631         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4632
4633         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4634         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4635         mkswap $DIR/f68b
4636
4637         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4638
4639         trap cleanup_68 EXIT
4640
4641         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4642
4643         echo "before: `swapon -s | grep $LLOOP`"
4644         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4645         echo "after: `swapon -s | grep $LLOOP`"
4646         SWAPUSED=`swap_used $LLOOP`
4647
4648         cleanup_68
4649
4650         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4651 }
4652 run_test 68b "support swapping to Lustre ========================"
4653
4654 # bug5265, obdfilter oa2dentry return -ENOENT
4655 # #define OBD_FAIL_OST_ENOENT 0x217
4656 test_69() {
4657         remote_ost_nodsh && skip "remote OST with nodsh" && return
4658
4659         f="$DIR/$tfile"
4660         $SETSTRIPE -c 1 -i 0 $f
4661
4662         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4663
4664         do_facet ost1 lctl set_param fail_loc=0x217
4665         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4666         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4667
4668         do_facet ost1 lctl set_param fail_loc=0
4669         $DIRECTIO write $f 0 2 || error "write error"
4670
4671         cancel_lru_locks osc
4672         $DIRECTIO read $f 0 1 || error "read error"
4673
4674         do_facet ost1 lctl set_param fail_loc=0x217
4675         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4676
4677         do_facet ost1 lctl set_param fail_loc=0
4678         rm -f $f
4679 }
4680 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4681
4682 test_71() {
4683     mkdir -p $DIR/$tdir
4684     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4685 }
4686 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4687
4688 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4689         check_kernel_version 43 || return 0
4690         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4691
4692         # Check that testing environment is properly set up. Skip if not
4693         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4694                 skip_env "User $RUNAS_ID does not exist - skipping"
4695                 return 0
4696         }
4697         # We had better clear the $DIR to get enough space for dd
4698         rm -rf $DIR/*
4699         touch $DIR/f72
4700         chmod 777 $DIR/f72
4701         chmod ug+s $DIR/f72
4702         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4703         # See if we are still setuid/sgid
4704         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4705         # Now test that MDS is updated too
4706         cancel_lru_locks mdc
4707         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4708         rm -f $DIR/f72
4709 }
4710 run_test 72a "Test that remove suid works properly (bug5695) ===="
4711
4712 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4713         local perm
4714
4715         [ "$RUNAS_ID" = "$UID" ] && \
4716                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4717         [ "$RUNAS_ID" -eq 0 ] && \
4718                 skip_env "RUNAS_ID = 0 -- skipping" && return
4719
4720         # Check that testing environment is properly set up. Skip if not
4721         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4722                 skip_env "User $RUNAS_ID does not exist - skipping"
4723                 return 0
4724         }
4725         touch $DIR/${tfile}-f{g,u}
4726         mkdir $DIR/${tfile}-d{g,u}
4727         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4728         chmod g+s $DIR/${tfile}-{f,d}g
4729         chmod u+s $DIR/${tfile}-{f,d}u
4730         for perm in 777 2777 4777; do
4731                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4732                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4733                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4734                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4735         done
4736         true
4737 }
4738 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4739
4740 # bug 3462 - multiple simultaneous MDC requests
4741 test_73() {
4742         mkdir $DIR/d73-1
4743         mkdir $DIR/d73-2
4744         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4745         pid1=$!
4746
4747         lctl set_param fail_loc=0x80000129
4748         $MULTIOP $DIR/d73-1/f73-2 Oc &
4749         sleep 1
4750         lctl set_param fail_loc=0
4751
4752         $MULTIOP $DIR/d73-2/f73-3 Oc &
4753         pid3=$!
4754
4755         kill -USR1 $pid1
4756         wait $pid1 || return 1
4757
4758         sleep 25
4759
4760         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4761         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4762         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4763
4764         rm -rf $DIR/d73-*
4765 }
4766 run_test 73 "multiple MDC requests (should not deadlock)"
4767
4768 test_74a() { # bug 6149, 6184
4769         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4770         #
4771         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4772         # will spin in a tight reconnection loop
4773         touch $DIR/f74a
4774         lctl set_param fail_loc=0x8000030e
4775         # get any lock that won't be difficult - lookup works.
4776         ls $DIR/f74a
4777         lctl set_param fail_loc=0
4778         true
4779         rm -f $DIR/f74a
4780 }
4781 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4782
4783 test_74b() { # bug 13310
4784         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4785         #
4786         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4787         # will spin in a tight reconnection loop
4788         lctl set_param fail_loc=0x8000030e
4789         # get a "difficult" lock
4790         touch $DIR/f74b
4791         lctl set_param fail_loc=0
4792         true
4793         rm -f $DIR/f74b
4794 }
4795 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4796
4797 test_74c() {
4798 #define OBD_FAIL_LDLM_NEW_LOCK
4799         lctl set_param fail_loc=0x80000319
4800         touch $DIR/$tfile && error "Touch successful"
4801         true
4802 }
4803 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4804
4805 num_inodes() {
4806         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4807 }
4808
4809 get_inode_slab_tunables() {
4810         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4811 }
4812
4813 set_inode_slab_tunables() {
4814         echo "lustre_inode_cache $1" > /proc/slabinfo
4815 }
4816
4817 test_76() { # Now for bug 20433, added originally in bug 1443
4818         local SLAB_SETTINGS=`get_inode_slab_tunables`
4819         local CPUS=`getconf _NPROCESSORS_ONLN`
4820         # we cannot set limit below 1 which means 1 inode in each
4821         # per-cpu cache is still allowed
4822         set_inode_slab_tunables "1 1 0"
4823         cancel_lru_locks osc
4824         BEFORE_INODES=`num_inodes`
4825         echo "before inodes: $BEFORE_INODES"
4826         local COUNT=1000
4827         [ "$SLOW" = "no" ] && COUNT=100
4828         for i in `seq $COUNT`; do
4829                 touch $DIR/$tfile
4830                 rm -f $DIR/$tfile
4831         done
4832         cancel_lru_locks osc
4833         AFTER_INODES=`num_inodes`
4834         echo "after inodes: $AFTER_INODES"
4835         local wait=0
4836         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4837                 sleep 2
4838                 AFTER_INODES=`num_inodes`
4839                 wait=$((wait+2))
4840                 echo "wait $wait seconds inodes: $AFTER_INODES"
4841                 if [ $wait -gt 30 ]; then
4842                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4843                 fi
4844         done
4845         set_inode_slab_tunables "$SLAB_SETTINGS"
4846 }
4847 run_test 76 "confirm clients recycle inodes properly ===="
4848
4849
4850 export ORIG_CSUM=""
4851 set_checksums()
4852 {
4853         # Note: in sptlrpc modes which enable its own bulk checksum, the
4854         # original crc32_le bulk checksum will be automatically disabled,
4855         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4856         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4857         # In this case set_checksums() will not be no-op, because sptlrpc
4858         # bulk checksum will be enabled all through the test.
4859
4860         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4861         lctl set_param -n osc.*.checksums $1
4862         return 0
4863 }
4864
4865 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4866                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4867 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4868 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4869 set_checksum_type()
4870 {
4871         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4872         log "set checksum type to $1"
4873         return 0
4874 }
4875 F77_TMP=$TMP/f77-temp
4876 F77SZ=8
4877 setup_f77() {
4878         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4879                 error "error writing to $F77_TMP"
4880 }
4881
4882 test_77a() { # bug 10889
4883         $GSS && skip "could not run with gss" && return
4884         [ ! -f $F77_TMP ] && setup_f77
4885         set_checksums 1
4886         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4887         set_checksums 0
4888         rm -f $DIR/$tfile
4889 }
4890 run_test 77a "normal checksum read/write operation ============="
4891
4892 test_77b() { # bug 10889
4893         $GSS && skip "could not run with gss" && return
4894         [ ! -f $F77_TMP ] && setup_f77
4895         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4896         lctl set_param fail_loc=0x80000409
4897         set_checksums 1
4898         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4899                 error "dd error: $?"
4900         lctl set_param fail_loc=0
4901         set_checksums 0
4902 }
4903 run_test 77b "checksum error on client write ===================="
4904
4905 test_77c() { # bug 10889
4906         $GSS && skip "could not run with gss" && return
4907         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4908         set_checksums 1
4909         for algo in $CKSUM_TYPES; do
4910                 cancel_lru_locks osc
4911                 set_checksum_type $algo
4912                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4913                 lctl set_param fail_loc=0x80000408
4914                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4915                 lctl set_param fail_loc=0
4916         done
4917         set_checksums 0
4918         set_checksum_type $ORIG_CSUM_TYPE
4919         rm -f $DIR/f77b
4920 }
4921 run_test 77c "checksum error on client read ==================="
4922
4923 test_77d() { # bug 10889
4924         $GSS && skip "could not run with gss" && return
4925         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4926         lctl set_param fail_loc=0x80000409
4927         set_checksums 1
4928         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4929                 error "direct write: rc=$?"
4930         lctl set_param fail_loc=0
4931         set_checksums 0
4932 }
4933 run_test 77d "checksum error on OST direct write ==============="
4934
4935 test_77e() { # bug 10889
4936         $GSS && skip "could not run with gss" && return
4937         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4938         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4939         lctl set_param fail_loc=0x80000408
4940         set_checksums 1
4941         cancel_lru_locks osc
4942         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4943                 error "direct read: rc=$?"
4944         lctl set_param fail_loc=0
4945         set_checksums 0
4946 }
4947 run_test 77e "checksum error on OST direct read ================"
4948
4949 test_77f() { # bug 10889
4950         $GSS && skip "could not run with gss" && return
4951         set_checksums 1
4952         for algo in $CKSUM_TYPES; do
4953                 cancel_lru_locks osc
4954                 set_checksum_type $algo
4955                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4956                 lctl set_param fail_loc=0x409
4957                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4958                         error "direct write succeeded"
4959                 lctl set_param fail_loc=0
4960         done
4961         set_checksum_type $ORIG_CSUM_TYPE
4962         set_checksums 0
4963 }
4964 run_test 77f "repeat checksum error on write (expect error) ===="
4965
4966 test_77g() { # bug 10889
4967         $GSS && skip "could not run with gss" && return
4968         remote_ost_nodsh && skip "remote OST with nodsh" && return
4969
4970         [ ! -f $F77_TMP ] && setup_f77
4971
4972         $SETSTRIPE -c 1 -i 0 $DIR/f77g
4973         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4974         do_facet ost1 lctl set_param fail_loc=0x8000021a
4975         set_checksums 1
4976         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4977                 error "write error: rc=$?"
4978         do_facet ost1 lctl set_param fail_loc=0
4979         set_checksums 0
4980 }
4981 run_test 77g "checksum error on OST write ======================"
4982
4983 test_77h() { # bug 10889
4984         $GSS && skip "could not run with gss" && return
4985         remote_ost_nodsh && skip "remote OST with nodsh" && return
4986
4987         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4988         cancel_lru_locks osc
4989         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4990         do_facet ost1 lctl set_param fail_loc=0x8000021b
4991         set_checksums 1
4992         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4993         do_facet ost1 lctl set_param fail_loc=0
4994         set_checksums 0
4995 }
4996 run_test 77h "checksum error on OST read ======================="
4997
4998 test_77i() { # bug 13805
4999         $GSS && skip "could not run with gss" && return
5000         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5001         lctl set_param fail_loc=0x40b
5002         remount_client $MOUNT
5003         lctl set_param fail_loc=0
5004         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5005                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5006                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5007                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5008         done
5009         remount_client $MOUNT
5010 }
5011 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5012
5013 test_77j() { # bug 13805
5014         $GSS && skip "could not run with gss" && return
5015         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5016         lctl set_param fail_loc=0x40c
5017         remount_client $MOUNT
5018         lctl set_param fail_loc=0
5019         sleep 2 # wait async osc connect to finish
5020         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5021                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5022                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5023                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5024         done
5025         remount_client $MOUNT
5026 }
5027 run_test 77j "client only supporting ADLER32 ===================="
5028
5029 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5030 rm -f $F77_TMP
5031 unset F77_TMP
5032
5033 test_78() { # bug 10901
5034         remote_ost || { skip_env "local OST" && return; }
5035
5036         NSEQ=5
5037         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5038         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5039         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5040         echo "MemTotal: $MEMTOTAL"
5041 # reserve 256MB of memory for the kernel and other running processes,
5042 # and then take 1/2 of the remaining memory for the read/write buffers.
5043     if [ $MEMTOTAL -gt 512 ] ;then
5044         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5045     else
5046         # for those poor memory-starved high-end clusters...
5047         MEMTOTAL=$((MEMTOTAL / 2))
5048     fi
5049         echo "Mem to use for directio: $MEMTOTAL"
5050         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5051         [ $F78SIZE -gt 512 ] && F78SIZE=512
5052         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5053         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5054         echo "Smallest OST: $SMALLESTOST"
5055         [ $SMALLESTOST -lt 10240 ] && \
5056                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5057
5058         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5059                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5060
5061         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5062         echo "File size: $F78SIZE"
5063         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5064         for i in `seq 1 $NSEQ`
5065         do
5066                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5067                 echo directIO rdwr round $i of $NSEQ
5068                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5069         done
5070
5071         rm -f $DIR/$tfile
5072 }
5073 run_test 78 "handle large O_DIRECT writes correctly ============"
5074
5075 test_79() { # bug 12743
5076         wait_delete_completed
5077
5078         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5079         BKFREE=$(calc_osc_kbytes kbytesfree)
5080         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5081
5082         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5083         DFTOTAL=`echo $STRING | cut -d, -f1`
5084         DFUSED=`echo $STRING  | cut -d, -f2`
5085         DFAVAIL=`echo $STRING | cut -d, -f3`
5086         DFFREE=$(($DFTOTAL - $DFUSED))
5087
5088         ALLOWANCE=$((64 * $OSTCOUNT))
5089
5090         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5091            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5092                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5093         fi
5094         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5095            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5096                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5097         fi
5098         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5099            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5100                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5101         fi
5102 }
5103 run_test 79 "df report consistency check ======================="
5104
5105 test_80() { # bug 10718
5106         # relax strong synchronous semantics for slow backends like ZFS
5107         local soc="obdfilter.*.sync_on_lock_cancel"
5108         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5109         local hosts=
5110         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5111                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5112                           facet_active_host $host; done | sort -u)
5113                 do_nodes $hosts lctl set_param $soc=never
5114         fi
5115
5116         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5117         sync; sleep 1; sync
5118         local BEFORE=`date +%s`
5119         cancel_lru_locks osc
5120         local AFTER=`date +%s`
5121         local DIFF=$((AFTER-BEFORE))
5122         if [ $DIFF -gt 1 ] ; then
5123                 error "elapsed for 1M@1T = $DIFF"
5124         fi
5125
5126         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5127
5128         rm -f $DIR/$tfile
5129 }
5130 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5131
5132 test_81a() { # LU-456
5133         remote_ost_nodsh && skip "remote OST with nodsh" && return
5134         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5135         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5136         do_facet ost1 lctl set_param fail_loc=0x80000228
5137
5138         # write should trigger a retry and success
5139         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5140         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5141         RC=$?
5142         if [ $RC -ne 0 ] ; then
5143                 error "write should success, but failed for $RC"
5144         fi
5145 }
5146 run_test 81a "OST should retry write when get -ENOSPC ==============="
5147
5148 test_81b() { # LU-456
5149         remote_ost_nodsh && skip "remote OST with nodsh" && return
5150         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5151         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5152         do_facet ost1 lctl set_param fail_loc=0x228
5153
5154         # write should retry several times and return -ENOSPC finally
5155         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5156         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5157         RC=$?
5158         ENOSPC=28
5159         if [ $RC -ne $ENOSPC ] ; then
5160                 error "dd should fail for -ENOSPC, but succeed."
5161         fi
5162 }
5163 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5164
5165 test_82() { # LU-1031
5166         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5167         local gid1=14091995
5168         local gid2=16022000
5169
5170         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5171         local MULTIPID1=$!
5172         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5173         local MULTIPID2=$!
5174         kill -USR1 $MULTIPID2
5175         sleep 2
5176         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5177                 error "First grouplock does not block second one"
5178         else
5179                 echo "Second grouplock blocks first one"
5180         fi
5181         kill -USR1 $MULTIPID1
5182         wait $MULTIPID1
5183         wait $MULTIPID2
5184 }
5185 run_test 82 "Basic grouplock test ==============================="
5186
5187 test_99a() {
5188         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5189             return
5190         mkdir -p $DIR/d99cvsroot
5191         chown $RUNAS_ID $DIR/d99cvsroot
5192         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5193         cd $TMP
5194
5195         $RUNAS cvs -d $DIR/d99cvsroot init || error
5196         cd $oldPWD
5197 }
5198 run_test 99a "cvs init ========================================="
5199
5200 test_99b() {
5201         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5202         [ ! -d $DIR/d99cvsroot ] && test_99a
5203         cd /etc/init.d
5204         # some versions of cvs import exit(1) when asked to import links or
5205         # files they can't read.  ignore those files.
5206         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5207                         ! -perm +4 -printf '-I %f\n')
5208         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5209                 d99reposname vtag rtag
5210 }
5211 run_test 99b "cvs import ======================================="
5212
5213 test_99c() {
5214         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5215         [ ! -d $DIR/d99cvsroot ] && test_99b
5216         cd $DIR
5217         mkdir -p $DIR/d99reposname
5218         chown $RUNAS_ID $DIR/d99reposname
5219         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5220 }
5221 run_test 99c "cvs checkout ====================================="
5222
5223 test_99d() {
5224         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5225         [ ! -d $DIR/d99cvsroot ] && test_99c
5226         cd $DIR/d99reposname
5227         $RUNAS touch foo99
5228         $RUNAS cvs add -m 'addmsg' foo99
5229 }
5230 run_test 99d "cvs add =========================================="
5231
5232 test_99e() {
5233         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5234         [ ! -d $DIR/d99cvsroot ] && test_99c
5235         cd $DIR/d99reposname
5236         $RUNAS cvs update
5237 }
5238 run_test 99e "cvs update ======================================="
5239
5240 test_99f() {
5241         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5242         [ ! -d $DIR/d99cvsroot ] && test_99d
5243         cd $DIR/d99reposname
5244         $RUNAS cvs commit -m 'nomsg' foo99
5245     rm -fr $DIR/d99cvsroot
5246 }
5247 run_test 99f "cvs commit ======================================="
5248
5249 test_100() {
5250         [ "$NETTYPE" = tcp ] || \
5251                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5252                         return ; }
5253
5254         remote_ost_nodsh && skip "remote OST with nodsh" && return
5255         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5256         remote_servers || \
5257                 { skip "useless for local single node setup" && return; }
5258
5259         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5260                 [ "$PROT" != "tcp" ] && continue
5261                 RPORT=$(echo $REMOTE | cut -d: -f2)
5262                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5263
5264                 rc=0
5265                 LPORT=`echo $LOCAL | cut -d: -f2`
5266                 if [ $LPORT -ge 1024 ]; then
5267                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5268                         netstat -tna
5269                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5270                 fi
5271         done
5272         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5273 }
5274 run_test 100 "check local port using privileged port ==========="
5275
5276 function get_named_value()
5277 {
5278     local tag
5279
5280     tag=$1
5281     while read ;do
5282         line=$REPLY
5283         case $line in
5284         $tag*)
5285             echo $line | sed "s/^$tag[ ]*//"
5286             break
5287             ;;
5288         esac
5289     done
5290 }
5291
5292 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5293 cleanup_101a() {
5294         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5295         trap 0
5296 }
5297
5298 test_101a() {
5299         local s
5300         local discard
5301         local nreads=10000
5302         [ "$CPU" = "UML" ] && nreads=1000
5303         local cache_limit=32
5304
5305         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5306         trap cleanup_101a EXIT
5307         $LCTL set_param -n llite.*.read_ahead_stats 0
5308         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5309
5310         #
5311         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5312         #
5313         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5314         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5315
5316         discard=0
5317         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5318                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5319                         discard=$(($discard + $s))
5320         done
5321         cleanup_101a
5322
5323         if [ $(($discard * 10)) -gt $nreads ] ;then
5324                 $LCTL get_param osc.*-osc*.rpc_stats
5325                 $LCTL get_param llite.*.read_ahead_stats
5326                 error "too many ($discard) discarded pages"
5327         fi
5328         rm -f $DIR/$tfile || true
5329 }
5330 run_test 101a "check read-ahead for random reads ================"
5331
5332 setup_test101bc() {
5333         mkdir -p $DIR/$tdir
5334         STRIPE_SIZE=1048576
5335         STRIPE_COUNT=$OSTCOUNT
5336         STRIPE_OFFSET=0
5337
5338         local list=$(comma_list $(osts_nodes))
5339         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
5340         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
5341
5342         trap cleanup_test101bc EXIT
5343         # prepare the read-ahead file
5344         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5345
5346         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5347 }
5348
5349 cleanup_test101bc() {
5350         trap 0
5351         rm -rf $DIR/$tdir
5352         rm -f $DIR/$tfile
5353
5354         local list=$(comma_list $(osts_nodes))
5355         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
5356         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
5357 }
5358
5359 calc_total() {
5360         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5361 }
5362
5363 ra_check_101() {
5364         local READ_SIZE=$1
5365         local STRIPE_SIZE=1048576
5366         local RA_INC=1048576
5367         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5368         local FILE_LENGTH=$((64*100))
5369         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5370                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5371         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5372                         get_named_value 'read but discarded' | \
5373                         cut -d" " -f1 | calc_total`
5374         if [ $DISCARD -gt $discard_limit ]; then
5375                 $LCTL get_param llite.*.read_ahead_stats
5376                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5377         else
5378                 echo "Read-ahead success for size ${READ_SIZE}"
5379         fi
5380 }
5381
5382 test_101b() {
5383         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5384         local STRIPE_SIZE=1048576
5385         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5386         local FILE_LENGTH=$((STRIPE_SIZE*100))
5387         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5388         # prepare the read-ahead file
5389         setup_test101bc
5390         cancel_lru_locks osc
5391         for BIDX in 2 4 8 16 32 64 128 256
5392         do
5393                 local BSIZE=$((BIDX*4096))
5394                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5395                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5396                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5397                 $LCTL set_param -n llite.*.read_ahead_stats 0
5398                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5399                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5400                 cancel_lru_locks osc
5401                 ra_check_101 $BSIZE
5402         done
5403         cleanup_test101bc
5404         true
5405 }
5406 run_test 101b "check stride-io mode read-ahead ================="
5407
5408 test_101c() {
5409     local STRIPE_SIZE=1048576
5410     local FILE_LENGTH=$((STRIPE_SIZE*100))
5411     local nreads=10000
5412     local osc
5413
5414     setup_test101bc
5415
5416     cancel_lru_locks osc
5417     $LCTL set_param osc.*.rpc_stats 0
5418     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5419     for osc in $($LCTL get_param -N osc.*); do
5420         if [ "$osc" == "osc.num_refs" ]; then
5421             continue
5422         fi
5423
5424         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5425         if [ $lines -le 20 ]; then
5426             continue
5427         fi
5428
5429         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5430                                      awk '$1 == "1:" { print $2; exit; }')
5431         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5432                                      awk '$1 == "2:" { print $2; exit; }')
5433         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5434                                      awk '$1 == "4:" { print $2; exit; }')
5435         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5436                                      awk '$1 == "8:" { print $2; exit; }')
5437
5438         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5439         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5440         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5441         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5442         echo "${osc} rpc check passed!"
5443     done
5444     cleanup_test101bc
5445     true
5446 }
5447 run_test 101c "check stripe_size aligned read-ahead ================="
5448
5449 set_read_ahead() {
5450    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5451    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5452 }
5453
5454 test_101d() {
5455     local file=$DIR/$tfile
5456     local size=${FILESIZE_101c:-500}
5457     local ra_MB=${READAHEAD_MB:-40}
5458
5459     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5460     [ $space -gt $((size / 1024)) ] ||
5461         { skip "Need free space ${size}M, have $space" && return; }
5462
5463     echo Creating ${size}M test file $file
5464     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5465     echo Cancel LRU locks on lustre client to flush the client cache
5466     cancel_lru_locks osc
5467
5468     echo Disable read-ahead
5469     local old_READAHEAD=$(set_read_ahead 0)
5470
5471     echo Reading the test file $file with read-ahead disabled
5472     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5473
5474     echo Cancel LRU locks on lustre client to flush the client cache
5475     cancel_lru_locks osc
5476     echo Enable read-ahead with ${ra_MB}MB
5477     set_read_ahead $ra_MB
5478
5479     echo Reading the test file $file with read-ahead enabled
5480     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5481
5482     echo read-ahead disabled time read $time_ra_OFF
5483     echo read-ahead enabled  time read $time_ra_ON
5484
5485         set_read_ahead $old_READAHEAD
5486         rm -f $file
5487         wait_delete_completed
5488
5489     [ $time_ra_ON -lt $time_ra_OFF ] ||
5490         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5491                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5492 }
5493 run_test 101d "file read with and without read-ahead enabled  ================="
5494
5495 test_101e() {
5496     local file=$DIR/$tfile
5497     local size=500  #KB
5498     local count=100
5499     local blksize=1024
5500
5501     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5502     local need_space=$((count * size))
5503     [ $space -gt $need_space ] ||
5504         { skip_env "Need free space $need_space, have $space" && return; }
5505
5506     echo Creating $count ${size}K test files
5507     for ((i = 0; i < $count; i++)); do
5508         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5509     done
5510
5511     echo Cancel LRU locks on lustre client to flush the client cache
5512     cancel_lru_locks osc
5513
5514     echo Reset readahead stats
5515     $LCTL set_param -n llite.*.read_ahead_stats 0
5516
5517     for ((i = 0; i < $count; i++)); do
5518         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5519     done
5520
5521     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5522           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5523
5524     for ((i = 0; i < $count; i++)); do
5525         rm -rf ${file}_${i} 2>/dev/null
5526     done
5527
5528     #10000 means 20% reads are missing in readahead
5529     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5530 }
5531 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5532
5533 cleanup_test101f() {
5534     trap 0
5535     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5536     rm -rf $DIR/$tfile 2>/dev/null
5537 }
5538
5539 test_101f() {
5540     local file=$DIR/$tfile
5541     local nreads=1000
5542
5543     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5544     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5545     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5546     trap cleanup_test101f EXIT
5547
5548     echo Cancel LRU locks on lustre client to flush the client cache
5549     cancel_lru_locks osc
5550
5551     echo Reset readahead stats
5552     $LCTL set_param -n llite.*.read_ahead_stats 0
5553     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5554     # readahead should read in 2M file on second read, so only miss
5555     # 2 pages.
5556     echo Random 4K reads on 2M file for 1000 times
5557     $READS -f $file -s 2097152 -b 4096 -n $nreads
5558
5559     echo checking missing pages
5560     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5561           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5562
5563     [ $miss -lt 3 ] || error "misses too much pages!"
5564     cleanup_test101f
5565 }
5566 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5567
5568 setup_test102() {
5569         mkdir -p $DIR/$tdir
5570         chown $RUNAS_ID $DIR/$tdir
5571         STRIPE_SIZE=65536
5572         STRIPE_OFFSET=1
5573         STRIPE_COUNT=$OSTCOUNT
5574         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5575
5576         trap cleanup_test102 EXIT
5577         cd $DIR
5578         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5579         cd $DIR/$tdir
5580         for num in 1 2 3 4; do
5581                 for count in $(seq 1 $STRIPE_COUNT); do
5582                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5583                                 local size=`expr $STRIPE_SIZE \* $num`
5584                                 local file=file"$num-$idx-$count"
5585                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5586                         done
5587                 done
5588         done
5589
5590         cd $DIR
5591         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5592 }
5593
5594 cleanup_test102() {
5595         trap 0
5596         rm -f $TMP/f102.tar
5597         rm -rf $DIR/d0.sanity/d102
5598 }
5599
5600 test_102a() {
5601         local testfile=$DIR/xattr_testfile
5602
5603         touch $testfile
5604
5605         [ "$UID" != 0 ] && skip_env "must run as root" && return
5606         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5607                 skip_env "must have user_xattr" && return
5608
5609         [ -z "$(which setfattr 2>/dev/null)" ] &&
5610                 skip_env "could not find setfattr" && return
5611
5612         echo "set/get xattr..."
5613         setfattr -n trusted.name1 -v value1 $testfile || error
5614         getfattr -n trusted.name1 $testfile 2> /dev/null |
5615           grep "trusted.name1=.value1" ||
5616                 error "$testfile missing trusted.name1=value1"
5617
5618         setfattr -n user.author1 -v author1 $testfile || error
5619         getfattr -n user.author1 $testfile 2> /dev/null |
5620           grep "user.author1=.author1" ||
5621                 error "$testfile missing trusted.author1=author1"
5622
5623         echo "listxattr..."
5624         setfattr -n trusted.name2 -v value2 $testfile ||
5625                 error "$testfile unable to set trusted.name2"
5626         setfattr -n trusted.name3 -v value3 $testfile ||
5627                 error "$testfile unable to set trusted.name3"
5628         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5629             grep "trusted.name" | wc -l) -eq 3 ] ||
5630                 error "$testfile missing 3 trusted.name xattrs"
5631
5632         setfattr -n user.author2 -v author2 $testfile ||
5633                 error "$testfile unable to set user.author2"
5634         setfattr -n user.author3 -v author3 $testfile ||
5635                 error "$testfile unable to set user.author3"
5636         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5637             grep "user.author" | wc -l) -eq 3 ] ||
5638                 error "$testfile missing 3 user.author xattrs"
5639
5640         echo "remove xattr..."
5641         setfattr -x trusted.name1 $testfile ||
5642                 error "$testfile error deleting trusted.name1"
5643         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5644                 error "$testfile did not delete trusted.name1 xattr"
5645
5646         setfattr -x user.author1 $testfile ||
5647                 error "$testfile error deleting user.author1"
5648         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5649                 error "$testfile did not delete trusted.name1 xattr"
5650
5651         # b10667: setting lustre special xattr be silently discarded
5652         echo "set lustre special xattr ..."
5653         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5654                 error "$testfile allowed setting trusted.lov"
5655 }
5656 run_test 102a "user xattr test =================================="
5657
5658 test_102b() {
5659         # b10930: get/set/list trusted.lov xattr
5660         echo "get/set/list trusted.lov xattr ..."
5661         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5662         local testfile=$DIR/$tfile
5663         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5664                 error "setstripe failed"
5665         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5666                 error "getstripe failed"
5667         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5668         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5669
5670         local testfile2=${testfile}2
5671         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5672                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5673
5674         $MCREATE $testfile2
5675         setfattr -n trusted.lov -v $value $testfile2
5676         local stripe_size=$($GETSTRIPE -S $testfile2)
5677         local stripe_count=$($GETSTRIPE -c $testfile2)
5678         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5679         [ $stripe_count -eq $STRIPECOUNT ] ||
5680                 error "stripe count $stripe_count != $STRIPECOUNT"
5681         rm -f $DIR/$tfile
5682 }
5683 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5684
5685 test_102c() {
5686         # b10930: get/set/list lustre.lov xattr
5687         echo "get/set/list lustre.lov xattr ..."
5688         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5689         mkdir -p $DIR/$tdir
5690         chown $RUNAS_ID $DIR/$tdir
5691         local testfile=$DIR/$tdir/$tfile
5692         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5693                 error "setstripe failed"
5694         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5695                 error "getstripe failed"
5696         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5697         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5698
5699         local testfile2=${testfile}2
5700         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5701                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5702
5703         $RUNAS $MCREATE $testfile2
5704         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5705         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5706         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5707         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5708         [ $stripe_count -eq $STRIPECOUNT ] ||
5709                 error "stripe count $stripe_count != $STRIPECOUNT"
5710 }
5711 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5712
5713 compare_stripe_info1() {
5714         local stripe_index_all_zero=true
5715
5716         for num in 1 2 3 4; do
5717                 for count in $(seq 1 $STRIPE_COUNT); do
5718                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5719                                 local size=$((STRIPE_SIZE * num))
5720                                 local file=file"$num-$offset-$count"
5721                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5722                                 [ $stripe_size -ne $size ] &&
5723                                     error "$file: size $stripe_size != $size"
5724                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5725                                 # allow fewer stripes to be created, ORI-601
5726                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5727                                     error "$file: count $stripe_count != $count"
5728                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5729                                 [ $stripe_index -ne 0 ] &&
5730                                         stripe_index_all_zero=false
5731                         done
5732                 done
5733         done
5734         $stripe_index_all_zero &&
5735                 error "all files are being extracted starting from OST index 0"
5736         return 0
5737 }
5738
5739 find_lustre_tar() {
5740         [ -n "$(which tar 2>/dev/null)" ] &&
5741                 strings $(which tar) | grep -q "lustre" && echo tar
5742 }
5743
5744 test_102d() {
5745         # b10930: tar test for trusted.lov xattr
5746         TAR=$(find_lustre_tar)
5747         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5748         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5749         setup_test102
5750         mkdir -p $DIR/d102d
5751         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5752         cd $DIR/d102d/$tdir
5753         compare_stripe_info1
5754 }
5755 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5756
5757 test_102f() {
5758         # b10930: tar test for trusted.lov xattr
5759         TAR=$(find_lustre_tar)
5760         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5761         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5762         setup_test102
5763         mkdir -p $DIR/d102f
5764         cd $DIR
5765         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5766         cd $DIR/d102f/$tdir
5767         compare_stripe_info1
5768 }
5769 run_test 102f "tar copy files, not keep osts ==========="
5770
5771 grow_xattr() {
5772         local xsize=${1:-1024}  # in bytes
5773         local file=$DIR/$tfile
5774
5775         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5776                 skip "must have user_xattr" && return 0
5777         [ -z "$(which setfattr 2>/dev/null)" ] &&
5778                 skip_env "could not find setfattr" && return 0
5779         [ -z "$(which getfattr 2>/dev/null)" ] &&
5780                 skip_env "could not find getfattr" && return 0
5781
5782         touch $file
5783
5784         local value="$(generate_string $xsize)"
5785
5786         local xbig=trusted.big
5787         log "save $xbig on $file"
5788         setfattr -n $xbig -v $value $file ||
5789                 error "saving $xbig on $file failed"
5790
5791         local orig=$(get_xattr_value $xbig $file)
5792         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5793
5794         local xsml=trusted.sml
5795         log "save $xsml on $file"
5796         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5797
5798         local new=$(get_xattr_value $xbig $file)
5799         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5800
5801         log "grow $xsml on $file"
5802         setfattr -n $xsml -v "$value" $file ||
5803                 error "growing $xsml on $file failed"
5804
5805         new=$(get_xattr_value $xbig $file)
5806         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5807         log "$xbig still valid after growing $xsml"
5808
5809         rm -f $file
5810 }
5811
5812 test_102h() { # bug 15777
5813         grow_xattr 1024
5814 }
5815 run_test 102h "grow xattr from inside inode to external block"
5816
5817 test_102ha() {
5818         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5819         grow_xattr $(max_xattr_size)
5820 }
5821 run_test 102ha "grow xattr from inside inode to external inode"
5822
5823 test_102i() { # bug 17038
5824         touch $DIR/$tfile
5825         ln -s $DIR/$tfile $DIR/${tfile}link
5826         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5827         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"
5828         rm -f $DIR/$tfile $DIR/${tfile}link
5829 }
5830 run_test 102i "lgetxattr test on symbolic link ============"
5831
5832 test_102j() {
5833         TAR=$(find_lustre_tar)
5834         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5835         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5836         setup_test102 "$RUNAS"
5837         mkdir -p $DIR/d102j
5838         chown $RUNAS_ID $DIR/d102j
5839         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5840         cd $DIR/d102j/$tdir
5841         compare_stripe_info1 "$RUNAS"
5842 }
5843 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5844
5845 test_102k() {
5846         touch $DIR/$tfile
5847         # b22187 just check that does not crash for regular file.
5848         setfattr -n trusted.lov $DIR/$tfile
5849         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5850         local test_kdir=$DIR/d102k
5851         mkdir $test_kdir
5852         local default_size=`$GETSTRIPE -S $test_kdir`
5853         local default_count=`$GETSTRIPE -c $test_kdir`
5854         local default_offset=`$GETSTRIPE -i $test_kdir`
5855         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5856                 error 'dir setstripe failed'
5857         setfattr -n trusted.lov $test_kdir
5858         local stripe_size=`$GETSTRIPE -S $test_kdir`
5859         local stripe_count=`$GETSTRIPE -c $test_kdir`
5860         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5861         [ $stripe_size -eq $default_size ] ||
5862                 error "stripe size $stripe_size != $default_size"
5863         [ $stripe_count -eq $default_count ] ||
5864                 error "stripe count $stripe_count != $default_count"
5865         [ $stripe_offset -eq $default_offset ] ||
5866                 error "stripe offset $stripe_offset != $default_offset"
5867         rm -rf $DIR/$tfile $test_kdir
5868 }
5869 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5870
5871 test_102l() {
5872         # LU-532 trusted. xattr is invisible to non-root
5873         local testfile=$DIR/$tfile
5874
5875         touch $testfile
5876
5877         echo "listxattr as user..."
5878         chown $RUNAS_ID $testfile
5879         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5880             grep -q "trusted" &&
5881                 error "$testfile trusted xattrs are user visible"
5882
5883         return 0;
5884 }
5885 run_test 102l "listxattr filter test =================================="
5886
5887 cleanup_test102
5888
5889 run_acl_subtest()
5890 {
5891     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5892     return $?
5893 }
5894
5895 test_103 () {
5896     [ "$UID" != 0 ] && skip_env "must run as root" && return
5897     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5898     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5899     $GSS && skip "could not run under gss" && return
5900
5901     declare -a identity_old
5902
5903         for num in $(seq $MDSCOUNT); do
5904                 switch_identity $num true || identity_old[$num]=$?
5905         done
5906
5907     SAVE_UMASK=`umask`
5908     umask 0022
5909     cd $DIR
5910
5911     echo "performing cp ..."
5912     run_acl_subtest cp || error
5913     echo "performing getfacl-noacl..."
5914     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5915     echo "performing misc..."
5916     run_acl_subtest misc || error  "misc test failed"
5917     echo "performing permissions..."
5918     run_acl_subtest permissions || error "permissions failed"
5919     echo "performing setfacl..."
5920     run_acl_subtest setfacl || error  "setfacl test failed"
5921
5922     # inheritance test got from HP
5923     echo "performing inheritance..."
5924     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5925     chmod +x make-tree || error "chmod +x failed"
5926     run_acl_subtest inheritance || error "inheritance test failed"
5927     rm -f make-tree
5928
5929     echo "LU-974 ignore umask when acl is enabled..."
5930     mkdir $DIR/974
5931     cd $DIR/974
5932     run_acl_subtest 974 || error "LU-974 test failed"
5933     rm -rf $DIR/974
5934
5935     cd $SAVE_PWD
5936     umask $SAVE_UMASK
5937
5938         for num in $(seq $MDSCOUNT); do
5939                 if [ "${identity_old[$num]}" = 1 ]; then
5940                         switch_identity $num false || identity_old[$num]=$?
5941                 fi
5942         done
5943 }
5944 run_test 103 "acl test ========================================="
5945
5946 test_104a() {
5947         touch $DIR/$tfile
5948         lfs df || error "lfs df failed"
5949         lfs df -ih || error "lfs df -ih failed"
5950         lfs df -h $DIR || error "lfs df -h $DIR failed"
5951         lfs df -i $DIR || error "lfs df -i $DIR failed"
5952         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5953         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5954
5955         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
5956         lctl --device %$OSC deactivate
5957         lfs df || error "lfs df with deactivated OSC failed"
5958         lctl --device %$OSC activate
5959         # wait the osc back to normal
5960         wait_osc_import_state client ost FULL
5961
5962         lfs df || error "lfs df with reactivated OSC failed"
5963         rm -f $DIR/$tfile
5964 }
5965 run_test 104a "lfs df [-ih] [path] test ========================="
5966
5967 test_104b() {
5968         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5969         chmod 666 /dev/obd
5970         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5971         if [ $denied_cnt -ne 0 ];
5972         then
5973                     error "lfs check servers test failed"
5974         fi
5975 }
5976 run_test 104b "$RUNAS lfs check servers test ===================="
5977
5978 test_105a() {
5979         # doesn't work on 2.4 kernels
5980         touch $DIR/$tfile
5981         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5982         then
5983                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5984         else
5985                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5986         fi
5987         rm -f $DIR/$tfile
5988 }
5989 run_test 105a "flock when mounted without -o flock test ========"
5990
5991 test_105b() {
5992         touch $DIR/$tfile
5993         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5994         then
5995                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5996         else
5997                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5998         fi
5999         rm -f $DIR/$tfile
6000 }
6001 run_test 105b "fcntl when mounted without -o flock test ========"
6002
6003 test_105c() {
6004         touch $DIR/$tfile
6005         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
6006         then
6007                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6008         else
6009                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6010         fi
6011         rm -f $DIR/$tfile
6012 }
6013 run_test 105c "lockf when mounted without -o flock test ========"
6014
6015 test_105d() { # bug 15924
6016         mkdir -p $DIR/$tdir
6017         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
6018                 skip "mount w/o flock enabled" && return
6019         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6020         $LCTL set_param fail_loc=0x80000315
6021         flocks_test 2 $DIR/$tdir
6022 }
6023 run_test 105d "flock race (should not freeze) ========"
6024
6025 test_105e() { # bug 22660 && 22040
6026         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
6027                 skip "mount w/o flock enabled" && return
6028         touch $DIR/$tfile
6029         flocks_test 3 $DIR/$tfile
6030 }
6031 run_test 105e "Two conflicting flocks from same process ======="
6032
6033 test_106() { #bug 10921
6034         mkdir -p $DIR/$tdir
6035         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6036         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6037 }
6038 run_test 106 "attempt exec of dir followed by chown of that dir"
6039
6040 test_107() {
6041         CDIR=`pwd`
6042         cd $DIR
6043
6044         local file=core
6045         rm -f $file
6046
6047         local save_pattern=$(sysctl -n kernel.core_pattern)
6048         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6049         sysctl -w kernel.core_pattern=$file
6050         sysctl -w kernel.core_uses_pid=0
6051
6052         ulimit -c unlimited
6053         sleep 60 &
6054         SLEEPPID=$!
6055
6056         sleep 1
6057
6058         kill -s 11 $SLEEPPID
6059         wait $SLEEPPID
6060         if [ -e $file ]; then
6061                 size=`stat -c%s $file`
6062                 [ $size -eq 0 ] && error "Fail to create core file $file"
6063         else
6064                 error "Fail to create core file $file"
6065         fi
6066         rm -f $file
6067         sysctl -w kernel.core_pattern=$save_pattern
6068         sysctl -w kernel.core_uses_pid=$save_uses_pid
6069         cd $CDIR
6070 }
6071 run_test 107 "Coredump on SIG"
6072
6073 test_110() {
6074         mkdir -p $DIR/$tdir
6075         mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6076                 error "mkdir with 255 char failed"
6077         mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6078                 error "mkdir with 256 char should fail, but did not"
6079         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6080                 error "create with 255 char failed"
6081         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6082                 error "create with 256 char should fail, but did not"
6083
6084         ls -l $DIR/$tdir
6085         rm -rf $DIR/$tdir
6086 }
6087 run_test 110 "filename length checking"
6088
6089 test_115() {
6090         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6091             cut -c11-20)
6092         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6093             return
6094         echo "Starting with $OSTIO_pre threads"
6095
6096         NUMTEST=20000
6097         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6098         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6099         echo "$NUMTEST creates/unlinks"
6100         mkdir -p $DIR/$tdir
6101         createmany -o $DIR/$tdir/$tfile $NUMTEST
6102         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6103
6104         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6105             cut -c11-20)
6106
6107         # don't return an error
6108         [ $OSTIO_post == $OSTIO_pre ] && echo \
6109             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6110             echo "This may be fine, depending on what ran before this test" &&
6111             echo "and how fast this system is." && return
6112
6113         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6114 }
6115 run_test 115 "verify dynamic thread creation===================="
6116
6117 free_min_max () {
6118         wait_delete_completed
6119         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6120         echo OST kbytes available: ${AVAIL[@]}
6121         MAXI=0; MAXV=${AVAIL[0]}
6122         MINI=0; MINV=${AVAIL[0]}
6123         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6124             #echo OST $i: ${AVAIL[i]}kb
6125             if [ ${AVAIL[i]} -gt $MAXV ]; then
6126                 MAXV=${AVAIL[i]}; MAXI=$i
6127             fi
6128             if [ ${AVAIL[i]} -lt $MINV ]; then
6129                 MINV=${AVAIL[i]}; MINI=$i
6130             fi
6131         done
6132         echo Min free space: OST $MINI: $MINV
6133         echo Max free space: OST $MAXI: $MAXV
6134 }
6135
6136 test_116a() { # was previously test_116()
6137         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6138
6139         echo -n "Free space priority "
6140         lctl get_param -n lov.*-clilov-*.qos_prio_free
6141         declare -a AVAIL
6142         free_min_max
6143         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6144                 return
6145
6146         # generate uneven OSTs
6147         mkdir -p $DIR/$tdir/OST${MINI}
6148         declare -i FILL
6149         FILL=$(($MINV / 4))
6150         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6151         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6152         i=0
6153         while [ $FILL -gt 0 ]; do
6154             i=$(($i + 1))
6155             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6156             FILL=$(($FILL - 2048))
6157             echo -n .
6158         done
6159         FILL=$(($MINV / 4))
6160         sync
6161         sleep_maxage
6162
6163         free_min_max
6164         DIFF=$(($MAXV - $MINV))
6165         DIFF2=$(($DIFF * 100 / $MINV))
6166         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6167         if [ $DIFF2 -gt 20 ]; then
6168             echo "ok"
6169         else
6170             echo "failed - QOS mode won't be used"
6171             error_ignore "QOS imbalance criteria not met"
6172             return
6173         fi
6174
6175         MINI1=$MINI; MINV1=$MINV
6176         MAXI1=$MAXI; MAXV1=$MAXV
6177
6178         # now fill using QOS
6179         echo writing a bunch of files to QOS-assigned OSTs
6180         $SETSTRIPE -c 1 $DIR/$tdir
6181         i=0
6182         while [ $FILL -gt 0 ]; do
6183             i=$(($i + 1))
6184             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6185             FILL=$(($FILL - 200))
6186             echo -n .
6187         done
6188         echo "wrote $i 200k files"
6189         sync
6190         sleep_maxage
6191
6192         echo "Note: free space may not be updated, so measurements might be off"
6193         free_min_max
6194         DIFF2=$(($MAXV - $MINV))
6195         echo "free space delta: orig $DIFF final $DIFF2"
6196         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6197         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6198         echo "Wrote $DIFF to smaller OST $MINI1"
6199         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6200         echo "Wrote $DIFF2 to larger OST $MAXI1"
6201         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6202
6203         # Figure out which files were written where
6204         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6205                awk '/'$MINI1': / {print $2; exit}')
6206         echo $UUID
6207         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6208         echo "$MINC files created on smaller OST $MINI1"
6209         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6210                awk '/'$MAXI1': / {print $2; exit}')
6211         echo $UUID
6212         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6213         echo "$MAXC files created on larger OST $MAXI1"
6214         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6215         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6216
6217         rm -rf $DIR/$tdir
6218 }
6219 run_test 116a "stripe QOS: free space balance ==================="
6220
6221 test_116b() { # LU-2093
6222 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6223         local old_rr
6224         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6225         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6226         mkdir -p $DIR/$tdir
6227         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6228         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6229         do_facet $SINGLEMDS lctl set_param fail_loc=0
6230         rm -rf $DIR/$tdir
6231         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6232 }
6233 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6234
6235 test_117() # bug 10891
6236 {
6237         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6238         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6239         lctl set_param fail_loc=0x21e
6240         > $DIR/$tfile || error "truncate failed"
6241         lctl set_param fail_loc=0
6242         echo "Truncate succeeded."
6243         rm -f $DIR/$tfile
6244 }
6245 run_test 117 "verify fsfilt_extend =========="
6246
6247 export OLD_RESENDCOUNT=""
6248 set_resend_count () {
6249         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6250         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6251         lctl set_param -n $PROC_RESENDCOUNT $1
6252         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6253 }
6254
6255 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6256
6257 # Reset async IO behavior after error case
6258 reset_async() {
6259         FILE=$DIR/reset_async
6260
6261         # Ensure all OSCs are cleared
6262         $SETSTRIPE -c -1 $FILE
6263         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6264         sync
6265         rm $FILE
6266 }
6267
6268 test_118a() #bug 11710
6269 {
6270         reset_async
6271
6272         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6273         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6274         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6275
6276         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6277                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6278                 return 1;
6279         fi
6280         rm -f $DIR/$tfile
6281 }
6282 run_test 118a "verify O_SYNC works =========="
6283
6284 test_118b()
6285 {
6286         remote_ost_nodsh && skip "remote OST with nodsh" && return
6287
6288         reset_async
6289
6290         #define OBD_FAIL_OST_ENOENT 0x217
6291         set_nodes_failloc "$(osts_nodes)" 0x217
6292         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6293         RC=$?
6294         set_nodes_failloc "$(osts_nodes)" 0
6295         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6296         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6297                     grep -c writeback)
6298
6299         if [[ $RC -eq 0 ]]; then
6300                 error "Must return error due to dropped pages, rc=$RC"
6301                 return 1;
6302         fi
6303
6304         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6305                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6306                 return 1;
6307         fi
6308
6309         echo "Dirty pages not leaked on ENOENT"
6310
6311         # Due to the above error the OSC will issue all RPCs syncronously
6312         # until a subsequent RPC completes successfully without error.
6313         $MULTIOP $DIR/$tfile Ow4096yc
6314         rm -f $DIR/$tfile
6315
6316         return 0
6317 }
6318 run_test 118b "Reclaim dirty pages on fatal error =========="
6319
6320 test_118c()
6321 {
6322         remote_ost_nodsh && skip "remote OST with nodsh" && return
6323
6324         reset_async
6325
6326         #define OBD_FAIL_OST_EROFS               0x216
6327         set_nodes_failloc "$(osts_nodes)" 0x216
6328
6329         # multiop should block due to fsync until pages are written
6330         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6331         MULTIPID=$!
6332         sleep 1
6333
6334         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6335                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6336         fi
6337
6338         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6339                     grep -c writeback)
6340         if [[ $WRITEBACK -eq 0 ]]; then
6341                 error "No page in writeback, writeback=$WRITEBACK"
6342         fi
6343
6344         set_nodes_failloc "$(osts_nodes)" 0
6345         wait $MULTIPID
6346         RC=$?
6347         if [[ $RC -ne 0 ]]; then
6348                 error "Multiop fsync failed, rc=$RC"
6349         fi
6350
6351         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6352         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6353                     grep -c writeback)
6354         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6355                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6356         fi
6357
6358         rm -f $DIR/$tfile
6359         echo "Dirty pages flushed via fsync on EROFS"
6360         return 0
6361 }
6362 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6363
6364 test_118d()
6365 {
6366         remote_ost_nodsh && skip "remote OST with nodsh" && return
6367
6368         reset_async
6369
6370         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6371         set_nodes_failloc "$(osts_nodes)" 0x214
6372         # multiop should block due to fsync until pages are written
6373         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6374         MULTIPID=$!
6375         sleep 1
6376
6377         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6378                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6379         fi
6380
6381         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6382                     grep -c writeback)
6383         if [[ $WRITEBACK -eq 0 ]]; then
6384                 error "No page in writeback, writeback=$WRITEBACK"
6385         fi
6386
6387         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6388         set_nodes_failloc "$(osts_nodes)" 0
6389
6390         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6391         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6392                     grep -c writeback)
6393         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6394                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6395         fi
6396
6397         rm -f $DIR/$tfile
6398         echo "Dirty pages gaurenteed flushed via fsync"
6399         return 0
6400 }
6401 run_test 118d "Fsync validation inject a delay of the bulk =========="
6402
6403 test_118f() {
6404         reset_async
6405
6406         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6407         lctl set_param fail_loc=0x8000040a
6408
6409         # Should simulate EINVAL error which is fatal
6410         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6411         RC=$?
6412         if [[ $RC -eq 0 ]]; then
6413                 error "Must return error due to dropped pages, rc=$RC"
6414         fi
6415
6416         lctl set_param fail_loc=0x0
6417
6418         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6419         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6420         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6421                     grep -c writeback)
6422         if [[ $LOCKED -ne 0 ]]; then
6423                 error "Locked pages remain in cache, locked=$LOCKED"
6424         fi
6425
6426         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6427                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6428         fi
6429
6430         rm -f $DIR/$tfile
6431         echo "No pages locked after fsync"
6432
6433         reset_async
6434         return 0
6435 }
6436 run_test 118f "Simulate unrecoverable OSC side error =========="
6437
6438 test_118g() {
6439         reset_async
6440
6441         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6442         lctl set_param fail_loc=0x406
6443
6444         # simulate local -ENOMEM
6445         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6446         RC=$?
6447
6448         lctl set_param fail_loc=0
6449         if [[ $RC -eq 0 ]]; then
6450                 error "Must return error due to dropped pages, rc=$RC"
6451         fi
6452
6453         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6454         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6455         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6456                         grep -c writeback)
6457         if [[ $LOCKED -ne 0 ]]; then
6458                 error "Locked pages remain in cache, locked=$LOCKED"
6459         fi
6460
6461         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6462                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6463         fi
6464
6465         rm -f $DIR/$tfile
6466         echo "No pages locked after fsync"
6467
6468         reset_async
6469         return 0
6470 }
6471 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6472
6473 test_118h() {
6474         remote_ost_nodsh && skip "remote OST with nodsh" && return
6475
6476         reset_async
6477
6478         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6479         set_nodes_failloc "$(osts_nodes)" 0x20e
6480         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6481         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6482         RC=$?
6483
6484         set_nodes_failloc "$(osts_nodes)" 0
6485         if [[ $RC -eq 0 ]]; then
6486                 error "Must return error due to dropped pages, rc=$RC"
6487         fi
6488
6489         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6490         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6491         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6492                     grep -c writeback)
6493         if [[ $LOCKED -ne 0 ]]; then
6494                 error "Locked pages remain in cache, locked=$LOCKED"
6495         fi
6496
6497         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6498                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6499         fi
6500
6501         rm -f $DIR/$tfile
6502         echo "No pages locked after fsync"
6503
6504         return 0
6505 }
6506 run_test 118h "Verify timeout in handling recoverables errors  =========="
6507
6508 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6509
6510 test_118i() {
6511         remote_ost_nodsh && skip "remote OST with nodsh" && return
6512
6513         reset_async
6514
6515         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6516         set_nodes_failloc "$(osts_nodes)" 0x20e
6517
6518         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6519         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6520         PID=$!
6521         sleep 5
6522         set_nodes_failloc "$(osts_nodes)" 0
6523
6524         wait $PID
6525         RC=$?
6526         if [[ $RC -ne 0 ]]; then
6527                 error "got error, but should be not, rc=$RC"
6528         fi
6529
6530         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6531         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6532         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6533         if [[ $LOCKED -ne 0 ]]; then
6534                 error "Locked pages remain in cache, locked=$LOCKED"
6535         fi
6536
6537         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6538                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6539         fi
6540
6541         rm -f $DIR/$tfile
6542         echo "No pages locked after fsync"
6543
6544         return 0
6545 }
6546 run_test 118i "Fix error before timeout in recoverable error  =========="
6547
6548 [ "$SLOW" = "no" ] && set_resend_count 4
6549
6550 test_118j() {
6551         remote_ost_nodsh && skip "remote OST with nodsh" && return
6552
6553         reset_async
6554
6555         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6556         set_nodes_failloc "$(osts_nodes)" 0x220
6557
6558         # return -EIO from OST
6559         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6560         RC=$?
6561         set_nodes_failloc "$(osts_nodes)" 0x0
6562         if [[ $RC -eq 0 ]]; then
6563                 error "Must return error due to dropped pages, rc=$RC"
6564         fi
6565
6566         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6567         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6568         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6569         if [[ $LOCKED -ne 0 ]]; then
6570                 error "Locked pages remain in cache, locked=$LOCKED"
6571         fi
6572
6573         # in recoverable error on OST we want resend and stay until it finished
6574         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6575                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6576         fi
6577
6578         rm -f $DIR/$tfile
6579         echo "No pages locked after fsync"
6580
6581         return 0
6582 }
6583 run_test 118j "Simulate unrecoverable OST side error =========="
6584
6585 test_118k()
6586 {
6587         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6588
6589         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6590         set_nodes_failloc "$(osts_nodes)" 0x20e
6591         mkdir -p $DIR/$tdir
6592
6593         for ((i=0;i<10;i++)); do
6594                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6595                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6596                 SLEEPPID=$!
6597                 sleep 0.500s
6598                 kill $SLEEPPID
6599                 wait $SLEEPPID
6600         done
6601
6602         set_nodes_failloc "$(osts_nodes)" 0
6603         rm -rf $DIR/$tdir
6604 }
6605 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6606
6607 test_118l()
6608 {
6609         # LU-646
6610         mkdir -p $DIR/$tdir
6611         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6612         rm -rf $DIR/$tdir
6613 }
6614 run_test 118l "fsync dir ========="
6615
6616 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6617
6618 test_119a() # bug 11737
6619 {
6620         BSIZE=$((512 * 1024))
6621         directio write $DIR/$tfile 0 1 $BSIZE
6622         # We ask to read two blocks, which is more than a file size.
6623         # directio will indicate an error when requested and actual
6624         # sizes aren't equeal (a normal situation in this case) and
6625         # print actual read amount.
6626         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6627         if [ "$NOB" != "$BSIZE" ]; then
6628                 error "read $NOB bytes instead of $BSIZE"
6629         fi
6630         rm -f $DIR/$tfile
6631 }
6632 run_test 119a "Short directIO read must return actual read amount"
6633
6634 test_119b() # bug 11737
6635 {
6636         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6637
6638         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6639         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6640         sync
6641         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6642                 error "direct read failed"
6643         rm -f $DIR/$tfile
6644 }
6645 run_test 119b "Sparse directIO read must return actual read amount"
6646
6647 test_119c() # bug 13099
6648 {
6649         BSIZE=1048576
6650         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6651         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6652         rm -f $DIR/$tfile
6653 }
6654 run_test 119c "Testing for direct read hitting hole"
6655
6656 test_119d() # bug 15950
6657 {
6658         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6659         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6660         BSIZE=1048576
6661         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6662         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6663         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6664         lctl set_param fail_loc=0x40d
6665         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6666         pid_dio=$!
6667         sleep 1
6668         cat $DIR/$tfile > /dev/null &
6669         lctl set_param fail_loc=0
6670         pid_reads=$!
6671         wait $pid_dio
6672         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6673         sleep 2
6674         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6675         error "the read rpcs have not completed in 2s"
6676         rm -f $DIR/$tfile
6677         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6678 }
6679 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6680
6681 test_120a() {
6682         mkdir -p $DIR/$tdir
6683         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6684                skip "no early lock cancel on server" && return 0
6685         lru_resize_disable mdc
6686         lru_resize_disable osc
6687         cancel_lru_locks mdc
6688         stat $DIR/$tdir > /dev/null
6689         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6690         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6691         mkdir $DIR/$tdir/d1
6692         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6693         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6694         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6695         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6696         lru_resize_enable mdc
6697         lru_resize_enable osc
6698 }
6699 run_test 120a "Early Lock Cancel: mkdir test"
6700
6701 test_120b() {
6702         mkdir -p $DIR/$tdir
6703         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6704                skip "no early lock cancel on server" && return 0
6705         lru_resize_disable mdc
6706         lru_resize_disable osc
6707         cancel_lru_locks mdc
6708         stat $DIR/$tdir > /dev/null
6709         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6710         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6711         touch $DIR/$tdir/f1
6712         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6713         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6714         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6715         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6716         lru_resize_enable mdc
6717         lru_resize_enable osc
6718 }
6719 run_test 120b "Early Lock Cancel: create test"
6720
6721 test_120c() {
6722         mkdir -p $DIR/$tdir
6723         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6724                skip "no early lock cancel on server" && return 0
6725         lru_resize_disable mdc
6726         lru_resize_disable osc
6727         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6728         touch $DIR/$tdir/d1/f1
6729         cancel_lru_locks mdc
6730         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6731         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6732         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6733         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6734         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6735         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6736         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6737         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6738         lru_resize_enable mdc
6739         lru_resize_enable osc
6740 }
6741 run_test 120c "Early Lock Cancel: link test"
6742
6743 test_120d() {
6744         mkdir -p $DIR/$tdir
6745         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6746                skip "no early lock cancel on server" && return 0
6747         lru_resize_disable mdc
6748         lru_resize_disable osc
6749         touch $DIR/$tdir
6750         cancel_lru_locks mdc
6751         stat $DIR/$tdir > /dev/null
6752         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6753         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6754         chmod a+x $DIR/$tdir
6755         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6756         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6757         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6758         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6759         lru_resize_enable mdc
6760         lru_resize_enable osc
6761 }
6762 run_test 120d "Early Lock Cancel: setattr test"
6763
6764 test_120e() {
6765         mkdir -p $DIR/$tdir
6766         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6767                skip "no early lock cancel on server" && return 0
6768         lru_resize_disable mdc
6769         lru_resize_disable osc
6770         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6771         cancel_lru_locks mdc
6772         cancel_lru_locks osc
6773         dd if=$DIR/$tdir/f1 of=/dev/null
6774         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6775         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6776               awk '/ldlm_cancel/ {print $2}'`
6777         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6778               awk '/ldlm_bl_callback/ {print $2}'`
6779         unlink $DIR/$tdir/f1
6780         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6781               awk '/ldlm_cancel/ {print $2}'`
6782         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6783               awk '/ldlm_bl_callback/ {print $2}'`
6784         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6785         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6786         lru_resize_enable mdc
6787         lru_resize_enable osc
6788 }
6789 run_test 120e "Early Lock Cancel: unlink test"
6790
6791 test_120f() {
6792         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6793                skip "no early lock cancel on server" && return 0
6794         mkdir -p $DIR/$tdir
6795         lru_resize_disable mdc
6796         lru_resize_disable osc
6797         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6798         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6799         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6800         cancel_lru_locks mdc
6801         cancel_lru_locks osc
6802         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6803         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6804         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6805         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6806               awk '/ldlm_cancel/ {print $2}'`
6807         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6808               awk '/ldlm_bl_callback/ {print $2}'`
6809         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6810         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6811               awk '/ldlm_cancel/ {print $2}'`
6812         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6813               awk '/ldlm_bl_callback/ {print $2}'`
6814         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6815         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6816         lru_resize_enable mdc
6817         lru_resize_enable osc
6818 }
6819 run_test 120f "Early Lock Cancel: rename test"
6820
6821 test_120g() {
6822         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6823                skip "no early lock cancel on server" && return 0
6824         lru_resize_disable mdc
6825         lru_resize_disable osc
6826         count=10000
6827         echo create $count files
6828         mkdir -p $DIR/$tdir
6829         cancel_lru_locks mdc
6830         cancel_lru_locks osc
6831         t0=`date +%s`
6832
6833         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6834               awk '/ldlm_cancel/ {print $2}'`
6835         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6836               awk '/ldlm_bl_callback/ {print $2}'`
6837         createmany -o $DIR/$tdir/f $count
6838         sync
6839         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6840               awk '/ldlm_cancel/ {print $2}'`
6841         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6842               awk '/ldlm_bl_callback/ {print $2}'`
6843         t1=`date +%s`
6844         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6845         echo rm $count files
6846         rm -r $DIR/$tdir
6847         sync
6848         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6849               awk '/ldlm_cancel/ {print $2}'`
6850         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6851               awk '/ldlm_bl_callback/ {print $2}'`
6852         t2=`date +%s`
6853         echo total: $count removes in $((t2-t1))
6854         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6855         sleep 2
6856         # wait for commitment of removal
6857         lru_resize_enable mdc
6858         lru_resize_enable osc
6859 }
6860 run_test 120g "Early Lock Cancel: performance test"
6861
6862 test_121() { #bug #10589
6863         rm -rf $DIR/$tfile
6864         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6865 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6866         lctl set_param fail_loc=0x310
6867         cancel_lru_locks osc > /dev/null
6868         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6869         lctl set_param fail_loc=0
6870         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6871 }
6872 run_test 121 "read cancel race ========="
6873
6874 test_123a() { # was test 123, statahead(bug 11401)
6875         SLOWOK=0
6876         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6877             log "testing on UP system. Performance may be not as good as expected."
6878                         SLOWOK=1
6879         fi
6880
6881         rm -rf $DIR/$tdir
6882         mkdir -p $DIR/$tdir
6883         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6884         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6885         MULT=10
6886         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6887                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6888
6889                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6890                 lctl set_param -n llite.*.statahead_max 0
6891                 lctl get_param llite.*.statahead_max
6892                 cancel_lru_locks mdc
6893                 cancel_lru_locks osc
6894                 stime=`date +%s`
6895                 time ls -l $DIR/$tdir | wc -l
6896                 etime=`date +%s`
6897                 delta=$((etime - stime))
6898                 log "ls $i files without statahead: $delta sec"
6899                 lctl set_param llite.*.statahead_max=$max
6900
6901                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6902                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6903                 cancel_lru_locks mdc
6904                 cancel_lru_locks osc
6905                 stime=`date +%s`
6906                 time ls -l $DIR/$tdir | wc -l
6907                 etime=`date +%s`
6908                 delta_sa=$((etime - stime))
6909                 log "ls $i files with statahead: $delta_sa sec"
6910                 lctl get_param -n llite.*.statahead_stats
6911                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6912
6913                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6914                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6915
6916                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6917                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6918                     lctl set_param -n llite.*.statahead_max 0
6919                     lctl get_param llite.*.statahead_max
6920                     cancel_lru_locks mdc
6921                     cancel_lru_locks osc
6922                     stime=`date +%s`
6923                     time ls -l $DIR/$tdir | wc -l
6924                     etime=`date +%s`
6925                     delta=$((etime - stime))
6926                     log "ls $i files again without statahead: $delta sec"
6927                     lctl set_param llite.*.statahead_max=$max
6928                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6929                         if [  $SLOWOK -eq 0 ]; then
6930                                 error "ls $i files is slower with statahead!"
6931                         else
6932                                 log "ls $i files is slower with statahead!"
6933                         fi
6934                         break
6935                     fi
6936                 fi
6937
6938                 [ $delta -gt 20 ] && break
6939                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6940                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6941         done
6942         log "ls done"
6943
6944         stime=`date +%s`
6945         rm -r $DIR/$tdir
6946         sync
6947         etime=`date +%s`
6948         delta=$((etime - stime))
6949         log "rm -r $DIR/$tdir/: $delta seconds"
6950         log "rm done"
6951         lctl get_param -n llite.*.statahead_stats
6952 }
6953 run_test 123a "verify statahead work"
6954
6955 test_123b () { # statahead(bug 15027)
6956         mkdir -p $DIR/$tdir
6957         createmany -o $DIR/$tdir/$tfile-%d 1000
6958
6959         cancel_lru_locks mdc
6960         cancel_lru_locks osc
6961
6962 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6963         lctl set_param fail_loc=0x80000803
6964         ls -lR $DIR/$tdir > /dev/null
6965         log "ls done"
6966         lctl set_param fail_loc=0x0
6967         lctl get_param -n llite.*.statahead_stats
6968         rm -r $DIR/$tdir
6969         sync
6970
6971 }
6972 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6973
6974 test_124a() {
6975         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6976                skip "no lru resize on server" && return 0
6977         local NR=2000
6978         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6979
6980         log "create $NR files at $DIR/$tdir"
6981         createmany -o $DIR/$tdir/f $NR ||
6982                 error "failed to create $NR files in $DIR/$tdir"
6983
6984         cancel_lru_locks mdc
6985         ls -l $DIR/$tdir > /dev/null
6986
6987         local NSDIR=""
6988         local LRU_SIZE=0
6989         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6990                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6991                 LRU_SIZE=$(lctl get_param -n $PARAM)
6992                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6993                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6994                                                 log "NSDIR=$NSDIR"
6995                         log "NS=$(basename $NSDIR)"
6996                         break
6997                 fi
6998         done
6999
7000         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7001                 skip "Not enough cached locks created!"
7002                 return 0
7003         fi
7004         log "LRU=$LRU_SIZE"
7005
7006         local SLEEP=30
7007
7008         # We know that lru resize allows one client to hold $LIMIT locks
7009         # for 10h. After that locks begin to be killed by client.
7010         local MAX_HRS=10
7011         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7012                 log "LIMIT=$LIMIT"
7013
7014         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7015         # killing locks. Some time was spent for creating locks. This means
7016         # that up to the moment of sleep finish we must have killed some of
7017         # them (10-100 locks). This depends on how fast ther were created.
7018         # Many of them were touched in almost the same moment and thus will
7019         # be killed in groups.
7020         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7021
7022         # Use $LRU_SIZE_B here to take into account real number of locks
7023         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7024         local LRU_SIZE_B=$LRU_SIZE
7025         log "LVF=$LVF"
7026         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7027                 log "OLD_LVF=$OLD_LVF"
7028         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7029
7030         # Let's make sure that we really have some margin. Client checks
7031         # cached locks every 10 sec.
7032         SLEEP=$((SLEEP+20))
7033         log "Sleep ${SLEEP} sec"
7034         local SEC=0
7035         while ((SEC<$SLEEP)); do
7036                 echo -n "..."
7037                 sleep 5
7038                 SEC=$((SEC+5))
7039                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7040                 echo -n "$LRU_SIZE"
7041         done
7042         echo ""
7043         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7044         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7045
7046         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7047                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7048                 unlinkmany $DIR/$tdir/f $NR
7049                 return
7050         }
7051
7052         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7053         log "unlink $NR files at $DIR/$tdir"
7054         unlinkmany $DIR/$tdir/f $NR
7055 }
7056 run_test 124a "lru resize ======================================="
7057
7058 get_max_pool_limit()
7059 {
7060         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7061         local max=0
7062         for l in $limit; do
7063                 if test $l -gt $max; then
7064                         max=$l
7065                 fi
7066         done
7067         echo $max
7068 }
7069
7070 test_124b() {
7071         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7072                skip "no lru resize on server" && return 0
7073
7074         LIMIT=`get_max_pool_limit`
7075
7076         NR=$(($(default_lru_size)*20))
7077         if [ $NR -gt $LIMIT ]; then
7078                 log "Limit lock number by $LIMIT locks"
7079                 NR=$LIMIT
7080         fi
7081         lru_resize_disable mdc
7082         mkdir -p $DIR/$tdir/disable_lru_resize ||
7083                 error "failed to create $DIR/$tdir/disable_lru_resize"
7084
7085         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7086         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7087         cancel_lru_locks mdc
7088         stime=`date +%s`
7089         PID=""
7090         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7091         PID="$PID $!"
7092         sleep 2
7093         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7094         PID="$PID $!"
7095         sleep 2
7096         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7097         PID="$PID $!"
7098         wait $PID
7099         etime=`date +%s`
7100         nolruresize_delta=$((etime-stime))
7101         log "ls -la time: $nolruresize_delta seconds"
7102         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7103         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7104
7105         lru_resize_enable mdc
7106         mkdir -p $DIR/$tdir/enable_lru_resize ||
7107                 error "failed to create $DIR/$tdir/enable_lru_resize"
7108
7109         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7110         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7111         cancel_lru_locks mdc
7112         stime=`date +%s`
7113         PID=""
7114         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7115         PID="$PID $!"
7116         sleep 2
7117         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7118         PID="$PID $!"
7119         sleep 2
7120         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7121         PID="$PID $!"
7122         wait $PID
7123         etime=`date +%s`
7124         lruresize_delta=$((etime-stime))
7125         log "ls -la time: $lruresize_delta seconds"
7126         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7127
7128         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7129                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7130         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7131                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7132         else
7133                 log "lru resize performs the same with no lru resize"
7134         fi
7135         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7136 }
7137 run_test 124b "lru resize (performance test) ======================="
7138
7139 test_125() { # 13358
7140         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7141         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7142         mkdir -p $DIR/d125 || error "mkdir failed"
7143         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7144         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7145         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7146 }
7147 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7148
7149 test_126() { # bug 12829/13455
7150         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7151         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7152         $GSS && skip "must run as gss disabled" && return
7153
7154         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7155         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7156         rm -f $DIR/$tfile
7157         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7158 }
7159 run_test 126 "check that the fsgid provided by the client is taken into account"
7160
7161 test_127a() { # bug 15521
7162         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7163         $LCTL set_param osc.*.stats=0
7164         FSIZE=$((2048 * 1024))
7165         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7166         cancel_lru_locks osc
7167         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7168
7169         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7170         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7171                 echo "got $COUNT $NAME"
7172                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7173                 eval $NAME=$COUNT || error "Wrong proc format"
7174
7175                 case $NAME in
7176                         read_bytes|write_bytes)
7177                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7178                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7179                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7180                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7181                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7182                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7183                                 error "sumsquare is too small: $SUMSQ"
7184                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7185                                 error "sumsquare is too big: $SUMSQ"
7186                         ;;
7187                         *) ;;
7188                 esac
7189         done < $DIR/${tfile}.tmp
7190
7191         #check that we actually got some stats
7192         [ "$read_bytes" ] || error "Missing read_bytes stats"
7193         [ "$write_bytes" ] || error "Missing write_bytes stats"
7194         [ "$read_bytes" != 0 ] || error "no read done"
7195         [ "$write_bytes" != 0 ] || error "no write done"
7196 }
7197 run_test 127a "verify the client stats are sane"
7198
7199 test_127b() { # bug LU-333
7200         $LCTL set_param llite.*.stats=0
7201         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7202         # perform 2 reads and writes so MAX is different from SUM.
7203         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7204         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7205         cancel_lru_locks osc
7206         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7207         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7208
7209         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7210         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7211                 echo "got $COUNT $NAME"
7212                 eval $NAME=$COUNT || error "Wrong proc format"
7213
7214         case $NAME in
7215                 read_bytes)
7216                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7217                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7218                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7219                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7220                         ;;
7221                 write_bytes)
7222                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7223                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7224                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7225                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7226                         ;;
7227                         *) ;;
7228                 esac
7229         done < $TMP/${tfile}.tmp
7230
7231         #check that we actually got some stats
7232         [ "$read_bytes" ] || error "Missing read_bytes stats"
7233         [ "$write_bytes" ] || error "Missing write_bytes stats"
7234         [ "$read_bytes" != 0 ] || error "no read done"
7235         [ "$write_bytes" != 0 ] || error "no write done"
7236 }
7237 run_test 127b "verify the llite client stats are sane"
7238
7239 test_128() { # bug 15212
7240         touch $DIR/$tfile
7241         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7242                 find $DIR/$tfile
7243                 find $DIR/$tfile
7244         EOF
7245
7246         result=$(grep error $TMP/$tfile.log)
7247         rm -f $DIR/$tfile
7248         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7249 }
7250 run_test 128 "interactive lfs for 2 consecutive find's"
7251
7252 set_dir_limits () {
7253         local mntdev
7254         local canondev
7255         local node
7256
7257         local LDPROC=/proc/fs/ldiskfs
7258         local facets=$(get_facets MDS)
7259
7260         for facet in ${facets//,/ }; do
7261                 canondev=$(ldiskfs_canon \
7262                            *.$(convert_facet2label $facet).mntdev $facet)
7263                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7264                                                 LDPROC=/sys/fs/ldiskfs
7265                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7266         done
7267 }
7268 test_129() {
7269         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7270                 skip "Only applicable to ldiskfs-based MDTs"
7271                 return
7272         fi
7273         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7274
7275         EFBIG=27
7276         MAX=16384
7277
7278         set_dir_limits $MAX
7279         mkdir -p $DIR/$tdir
7280
7281         local I=0
7282         local J=0
7283         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7284                 $MULTIOP $DIR/$tdir/$J Oc
7285                 rc=$?
7286                 if [ $rc -eq $EFBIG ]; then
7287                         set_dir_limits 0
7288                         echo "return code $rc received as expected"
7289                         return 0
7290                 elif [ $rc -ne 0 ]; then
7291                         set_dir_limits 0
7292                         error_exit "return code $rc received instead of expected $EFBIG"
7293                 fi
7294                 J=$((J+1))
7295                 I=$(stat -c%s "$DIR/$tdir")
7296         done
7297
7298         set_dir_limits 0
7299         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7300 }
7301 run_test 129 "test directory size limit ========================"
7302
7303 OLDIFS="$IFS"
7304 cleanup_130() {
7305         trap 0
7306         IFS="$OLDIFS"
7307 }
7308
7309 test_130a() {
7310         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7311         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7312                 return
7313
7314         trap cleanup_130 EXIT RETURN
7315
7316         local fm_file=$DIR/$tfile
7317         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7318         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7319                 error "dd failed for $fm_file"
7320
7321         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7322         filefrag -ves $fm_file
7323         RC=$?
7324         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7325                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7326         [ $RC != 0 ] && error "filefrag $fm_file failed"
7327
7328         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7329                       grep -v "ext:" | grep -v "found")
7330         lun=$($GETSTRIPE -i $fm_file)
7331
7332         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7333         IFS=$'\n'
7334         tot_len=0
7335         for line in $filefrag_op
7336         do
7337                 frag_lun=`echo $line | cut -d: -f5`
7338                 ext_len=`echo $line | cut -d: -f4`
7339                 if (( $frag_lun != $lun )); then
7340                         cleanup_130
7341                         error "FIEMAP on 1-stripe file($fm_file) failed"
7342                         return
7343                 fi
7344                 (( tot_len += ext_len ))
7345         done
7346
7347         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7348                 cleanup_130
7349                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7350                 return
7351         fi
7352
7353         cleanup_130
7354
7355         echo "FIEMAP on single striped file succeeded"
7356 }
7357 run_test 130a "FIEMAP (1-stripe file)"
7358
7359 test_130b() {
7360         [ "$OSTCOUNT" -lt "2" ] &&
7361                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7362
7363         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7364         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7365                 return
7366
7367         trap cleanup_130 EXIT RETURN
7368
7369         local fm_file=$DIR/$tfile
7370         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7371         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7372                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7373
7374         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7375                 error "dd failed on $fm_file"
7376
7377         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7378         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7379                       grep -v "ext:" | grep -v "found")
7380
7381         last_lun=$(echo $filefrag_op | cut -d: -f5)
7382
7383         IFS=$'\n'
7384         tot_len=0
7385         num_luns=1
7386         for line in $filefrag_op
7387         do
7388                 frag_lun=`echo $line | cut -d: -f5`
7389                 ext_len=`echo $line | cut -d: -f4`
7390                 if (( $frag_lun != $last_lun )); then
7391                         if (( tot_len != 1024 )); then
7392                                 cleanup_130
7393                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7394                                 return
7395                         else
7396                                 (( num_luns += 1 ))
7397                                 tot_len=0
7398                         fi
7399                 fi
7400                 (( tot_len += ext_len ))
7401                 last_lun=$frag_lun
7402         done
7403         if (( num_luns != 2 || tot_len != 1024 )); then
7404                 cleanup_130
7405                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7406                 return
7407         fi
7408
7409         cleanup_130
7410
7411         echo "FIEMAP on 2-stripe file succeeded"
7412 }
7413 run_test 130b "FIEMAP (2-stripe file)"
7414
7415 test_130c() {
7416         [ "$OSTCOUNT" -lt "2" ] &&
7417                 skip_env "skipping FIEMAP on 2-stripe file" && return
7418
7419         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7420         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7421                 return
7422
7423         trap cleanup_130 EXIT RETURN
7424
7425         local fm_file=$DIR/$tfile
7426         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7427         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7428                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7429
7430         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7431
7432         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7433         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7434
7435         last_lun=`echo $filefrag_op | cut -d: -f5`
7436
7437         IFS=$'\n'
7438         tot_len=0
7439         num_luns=1
7440         for line in $filefrag_op
7441         do
7442                 frag_lun=`echo $line | cut -d: -f5`
7443                 ext_len=`echo $line | cut -d: -f4`
7444                 if (( $frag_lun != $last_lun )); then
7445                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7446                         if (( logical != 512 )); then
7447                                 cleanup_130
7448                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7449                                 return
7450                         fi
7451                         if (( tot_len != 512 )); then
7452                                 cleanup_130
7453                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7454                                 return
7455                         else
7456                                 (( num_luns += 1 ))
7457                                 tot_len=0
7458                         fi
7459                 fi
7460                 (( tot_len += ext_len ))
7461                 last_lun=$frag_lun
7462         done
7463         if (( num_luns != 2 || tot_len != 512 )); then
7464                 cleanup_130
7465                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7466                 return
7467         fi
7468
7469         cleanup_130
7470
7471         echo "FIEMAP on 2-stripe file with hole succeeded"
7472 }
7473 run_test 130c "FIEMAP (2-stripe file with hole)"
7474
7475 test_130d() {
7476         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7477
7478         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7479         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7480
7481         trap cleanup_130 EXIT RETURN
7482
7483         local fm_file=$DIR/$tfile
7484         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7485         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7486                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7487         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7488
7489         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7490         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7491
7492         last_lun=`echo $filefrag_op | cut -d: -f5`
7493
7494         IFS=$'\n'
7495         tot_len=0
7496         num_luns=1
7497         for line in $filefrag_op
7498         do
7499                 frag_lun=`echo $line | cut -d: -f5`
7500                 ext_len=`echo $line | cut -d: -f4`
7501                 if (( $frag_lun != $last_lun )); then
7502                         if (( tot_len != 1024 )); then
7503                                 cleanup_130
7504                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7505                                 return
7506                         else
7507                                 (( num_luns += 1 ))
7508                                 tot_len=0
7509                         fi
7510                 fi
7511                 (( tot_len += ext_len ))
7512                 last_lun=$frag_lun
7513         done
7514         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7515                 cleanup_130
7516                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7517                 return
7518         fi
7519
7520         cleanup_130
7521
7522         echo "FIEMAP on N-stripe file succeeded"
7523 }
7524 run_test 130d "FIEMAP (N-stripe file)"
7525
7526 test_130e() {
7527         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7528
7529         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7530         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7531
7532         trap cleanup_130 EXIT RETURN
7533
7534         local fm_file=$DIR/$tfile
7535         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7536         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7537                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7538
7539         NUM_BLKS=512
7540         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7541         for ((i = 0; i < $NUM_BLKS; i++))
7542         do
7543                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7544         done
7545
7546         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7547         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7548
7549         last_lun=`echo $filefrag_op | cut -d: -f5`
7550
7551         IFS=$'\n'
7552         tot_len=0
7553         num_luns=1
7554         for line in $filefrag_op
7555         do
7556                 frag_lun=`echo $line | cut -d: -f5`
7557                 ext_len=`echo $line | cut -d: -f4`
7558                 if (( $frag_lun != $last_lun )); then
7559                         if (( tot_len != $EXPECTED_LEN )); then
7560                                 cleanup_130
7561                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7562                                 return
7563                         else
7564                                 (( num_luns += 1 ))
7565                                 tot_len=0
7566                         fi
7567                 fi
7568                 (( tot_len += ext_len ))
7569                 last_lun=$frag_lun
7570         done
7571         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7572                 cleanup_130
7573                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7574                 return
7575         fi
7576
7577         cleanup_130
7578
7579         echo "FIEMAP with continuation calls succeeded"
7580 }
7581 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7582
7583 # Test for writev/readv
7584 test_131a() {
7585         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7586         error "writev test failed"
7587         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7588         error "readv failed"
7589         rm -f $DIR/$tfile
7590 }
7591 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7592
7593 test_131b() {
7594         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7595         error "append writev test failed"
7596         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7597         error "append writev test failed"
7598         rm -f $DIR/$tfile
7599 }
7600 run_test 131b "test append writev"
7601
7602 test_131c() {
7603         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7604         error "NOT PASS"
7605 }
7606 run_test 131c "test read/write on file w/o objects"
7607
7608 test_131d() {
7609         rwv -f $DIR/$tfile -w -n 1 1572864
7610         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7611         if [ "$NOB" != 1572864 ]; then
7612                 error "Short read filed: read $NOB bytes instead of 1572864"
7613         fi
7614         rm -f $DIR/$tfile
7615 }
7616 run_test 131d "test short read"
7617
7618 test_131e() {
7619         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7620         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7621         error "read hitting hole failed"
7622         rm -f $DIR/$tfile
7623 }
7624 run_test 131e "test read hitting hole"
7625
7626 get_ost_param() {
7627         local token=$1
7628         local gl_sum=0
7629         for node in $(osts_nodes); do
7630                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7631                 [ x$gl = x"" ] && gl=0
7632                 gl_sum=$((gl_sum + gl))
7633         done
7634         echo $gl
7635 }
7636
7637 som_mode_switch() {
7638         local som=$1
7639         local gl1=$2
7640         local gl2=$3
7641
7642         if [ x$som = x"enabled" ]; then
7643                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7644                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7645                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7646         else
7647                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7648                 MOUNTOPT="$MOUNTOPT,som_preview"
7649                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7650         fi
7651
7652         # do remount to make new mount-conf parameters actual
7653         echo remounting...
7654         sync
7655         stopall
7656         setupall
7657 }
7658
7659 test_132() { #1028, SOM
7660         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7661         local num=$(get_mds_dir $DIR)
7662         local mymds=mds${num}
7663         local MOUNTOPT_SAVE=$MOUNTOPT
7664
7665         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7666         cancel_lru_locks osc
7667
7668         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7669
7670         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7671         stat $DIR/$tfile >/dev/null
7672         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7673         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7674         rm $DIR/$tfile
7675         som_mode_switch $som1 $gl1 $gl2
7676
7677         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7678         cancel_lru_locks osc
7679
7680         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7681         if [ $som1 == $som2 ]; then
7682             error "som is still "$som2
7683             if [ x$som2 = x"enabled" ]; then
7684                 som2="disabled"
7685             else
7686                 som2="enabled"
7687             fi
7688         fi
7689
7690         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7691         stat $DIR/$tfile >/dev/null
7692         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7693         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7694         som_mode_switch $som2 $gl1 $gl2
7695         MOUNTOPT=$MOUNTOPT_SAVE
7696 }
7697 run_test 132 "som avoids glimpse rpc"
7698
7699 check_stats() {
7700         local res
7701         local count
7702         case $1 in
7703         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7704                  ;;
7705         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7706                  ;;
7707         *) error "Wrong argument $1" ;;
7708         esac
7709         echo $res
7710         count=`echo $res | awk '{print $2}'`
7711         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7712         # if the argument $3 is zero, it means any stat increment is ok.
7713         if [ $3 -gt 0 ] ; then
7714                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7715         fi
7716 }
7717
7718 test_133a() {
7719         remote_ost_nodsh && skip "remote OST with nodsh" && return
7720         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7721
7722         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7723                 { skip "MDS doesn't support rename stats"; return; }
7724         local testdir=$DIR/${tdir}/stats_testdir
7725         mkdir -p $DIR/${tdir}
7726
7727         # clear stats.
7728         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7729         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7730
7731         # verify mdt stats first.
7732         mkdir ${testdir} || error "mkdir failed"
7733         check_stats $SINGLEMDS "mkdir" 1
7734         touch ${testdir}/${tfile} || "touch failed"
7735         check_stats $SINGLEMDS "open" 1
7736         check_stats $SINGLEMDS "close" 1
7737         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7738         check_stats $SINGLEMDS "mknod" 1
7739         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7740         check_stats $SINGLEMDS "unlink" 1
7741         rm -f ${testdir}/${tfile} || error "file remove failed"
7742         check_stats $SINGLEMDS "unlink" 2
7743
7744         # remove working dir and check mdt stats again.
7745         rmdir ${testdir} || error "rmdir failed"
7746         check_stats $SINGLEMDS "rmdir" 1
7747
7748         local testdir1=$DIR/${tdir}/stats_testdir1
7749         mkdir -p ${testdir}
7750         mkdir -p ${testdir1}
7751         touch ${testdir1}/test1
7752         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7753         check_stats $SINGLEMDS "crossdir_rename" 1
7754
7755         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7756         check_stats $SINGLEMDS "samedir_rename" 1
7757
7758         rm -rf $DIR/${tdir}
7759 }
7760 run_test 133a "Verifying MDT stats ========================================"
7761
7762 test_133b() {
7763         remote_ost_nodsh && skip "remote OST with nodsh" && return
7764         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7765         local testdir=$DIR/${tdir}/stats_testdir
7766         mkdir -p ${testdir} || error "mkdir failed"
7767         touch ${testdir}/${tfile} || "touch failed"
7768         cancel_lru_locks mdc
7769
7770         # clear stats.
7771         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7772         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7773
7774         # extra mdt stats verification.
7775         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7776         check_stats $SINGLEMDS "setattr" 1
7777         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7778         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
7779         then            # LU-1740
7780                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7781                 check_stats $SINGLEMDS "getattr" 1
7782         fi
7783         $LFS df || error "lfs failed"
7784         check_stats $SINGLEMDS "statfs" 1
7785
7786         rm -rf $DIR/${tdir}
7787 }
7788 run_test 133b "Verifying extra MDT stats =================================="
7789
7790 test_133c() {
7791         remote_ost_nodsh && skip "remote OST with nodsh" && return
7792         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7793         local testdir=$DIR/${tdir}/stats_testdir
7794         mkdir -p ${testdir} || error "mkdir failed"
7795
7796         # verify obdfilter stats.
7797         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7798         sync
7799         cancel_lru_locks osc
7800         wait_delete_completed
7801
7802         # clear stats.
7803         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7804         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7805
7806         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7807         sync
7808         cancel_lru_locks osc
7809         check_stats ost "write" 1
7810
7811         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7812         check_stats ost "read" 1
7813
7814         > ${testdir}/${tfile} || error "truncate failed"
7815         check_stats ost "punch" 1
7816
7817         rm -f ${testdir}/${tfile} || error "file remove failed"
7818         wait_delete_completed
7819         check_stats ost "destroy" 1
7820
7821         rm -rf $DIR/${tdir}
7822 }
7823 run_test 133c "Verifying OST stats ========================================"
7824
7825 order_2() {
7826     local value=$1
7827     local orig=$value
7828     local order=1
7829
7830     while [ $value -ge 2 ]; do
7831         order=$((order*2))
7832         value=$((value/2))
7833     done
7834
7835     if [ $orig -gt $order ]; then
7836         order=$((order*2))
7837     fi
7838     echo $order
7839 }
7840
7841 size_in_KMGT() {
7842     local value=$1
7843     local size=('K' 'M' 'G' 'T');
7844     local i=0
7845     local size_string=$value
7846
7847     while [ $value -ge 1024 ]; do
7848         if [ $i -gt 3 ]; then
7849             #T is the biggest unit we get here, if that is bigger,
7850             #just return XXXT
7851             size_string=${value}T
7852             break
7853         fi
7854         value=$((value >> 10))
7855         if [ $value -lt 1024 ]; then
7856             size_string=${value}${size[$i]}
7857             break
7858         fi
7859         i=$((i + 1))
7860     done
7861
7862     echo $size_string
7863 }
7864
7865 get_rename_size() {
7866     local size=$1
7867     local context=${2:-.}
7868     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
7869                 grep -A1 $context |
7870                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7871     echo $sample
7872 }
7873
7874 test_133d() {
7875     remote_ost_nodsh && skip "remote OST with nodsh" && return
7876     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7877     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7878         { skip "MDS doesn't support rename stats"; return; }
7879
7880     local testdir1=$DIR/${tdir}/stats_testdir1
7881     local testdir2=$DIR/${tdir}/stats_testdir2
7882
7883     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7884
7885     mkdir -p ${testdir1} || error "mkdir failed"
7886     mkdir -p ${testdir2} || error "mkdir failed"
7887
7888     createmany -o $testdir1/test 512 || error "createmany failed"
7889
7890         # check samedir rename size
7891         mv ${testdir1}/test0 ${testdir1}/test_0
7892
7893         local testdir1_size=$(ls -l $DIR/${tdir} |
7894                 awk '/stats_testdir1/ {print $5}')
7895         local testdir2_size=$(ls -l $DIR/${tdir} |
7896                 awk '/stats_testdir2/ {print $5}')
7897
7898         testdir1_size=$(order_2 $testdir1_size)
7899         testdir2_size=$(order_2 $testdir2_size)
7900
7901         testdir1_size=$(size_in_KMGT $testdir1_size)
7902         testdir2_size=$(size_in_KMGT $testdir2_size)
7903
7904         echo "source rename dir size: ${testdir1_size}"
7905         echo "target rename dir size: ${testdir2_size}"
7906
7907     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
7908     eval $cmd || error "$cmd failed"
7909     local samedir=$($cmd | grep 'same_dir')
7910     local same_sample=$(get_rename_size $testdir1_size)
7911     [ -z "$samedir" ] && error "samedir_rename_size count error"
7912     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
7913     echo "Check same dir rename stats success"
7914
7915     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7916
7917     # check crossdir rename size
7918     mv ${testdir1}/test_0 ${testdir2}/test_0
7919
7920         testdir1_size=$(ls -l $DIR/${tdir} |
7921                 awk '/stats_testdir1/ {print $5}')
7922         testdir2_size=$(ls -l $DIR/${tdir} |
7923                 awk '/stats_testdir2/ {print $5}')
7924
7925         testdir1_size=$(order_2 $testdir1_size)
7926         testdir2_size=$(order_2 $testdir2_size)
7927
7928         testdir1_size=$(size_in_KMGT $testdir1_size)
7929         testdir2_size=$(size_in_KMGT $testdir2_size)
7930
7931         echo "source rename dir size: ${testdir1_size}"
7932         echo "target rename dir size: ${testdir2_size}"
7933
7934     eval $cmd || error "$cmd failed"
7935     local crossdir=$($cmd | grep 'crossdir')
7936     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
7937     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
7938     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
7939     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
7940     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
7941     echo "Check cross dir rename stats success"
7942     rm -rf $DIR/${tdir}
7943 }
7944 run_test 133d "Verifying rename_stats ========================================"
7945
7946 test_140() { #bug-17379
7947         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7948         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7949         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7950
7951         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7952         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
7953         local i=0
7954         while i=`expr $i + 1`; do
7955                 mkdir -p $i || error "Creating dir $i"
7956                 cd $i || error "Changing to $i"
7957                 ln -s ../stat stat || error "Creating stat symlink"
7958                 # Read the symlink until ELOOP present,
7959                 # not LBUGing the system is considered success,
7960                 # we didn't overrun the stack.
7961                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7962                 [ $ret -ne 0 ] && {
7963                         if [ $ret -eq 40 ]; then
7964                                 break  # -ELOOP
7965                         else
7966                                 error "Open stat symlink"
7967                                 return
7968                         fi
7969                 }
7970         done
7971         i=`expr $i - 1`
7972         echo "The symlink depth = $i"
7973         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
7974                                         error "Invalid symlink depth"
7975
7976         # Test recursive symlink
7977         ln -s symlink_self symlink_self
7978         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
7979         echo "open symlink_self returns $ret"
7980         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
7981 }
7982 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7983
7984 test_150() {
7985         local TF="$TMP/$tfile"
7986
7987         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7988         cp $TF $DIR/$tfile
7989         cancel_lru_locks osc
7990         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7991         remount_client $MOUNT
7992         df -P $MOUNT
7993         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7994
7995         $TRUNCATE $TF 6000
7996         $TRUNCATE $DIR/$tfile 6000
7997         cancel_lru_locks osc
7998         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7999
8000         echo "12345" >>$TF
8001         echo "12345" >>$DIR/$tfile
8002         cancel_lru_locks osc
8003         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8004
8005         echo "12345" >>$TF
8006         echo "12345" >>$DIR/$tfile
8007         cancel_lru_locks osc
8008         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8009
8010         rm -f $TF
8011         true
8012 }
8013 run_test 150 "truncate/append tests"
8014
8015 function roc_hit() {
8016         local list=$(comma_list $(osts_nodes))
8017
8018         echo $(get_obdfilter_param $list '' stats |
8019                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8020 }
8021
8022 function set_cache() {
8023         local on=1
8024
8025         if [ "$2" == "off" ]; then
8026                 on=0;
8027         fi
8028         local list=$(comma_list $(osts_nodes))
8029         set_obdfilter_param $list '' $1_cache_enable $on
8030
8031         cancel_lru_locks osc
8032 }
8033
8034 test_151() {
8035         remote_ost_nodsh && skip "remote OST with nodsh" && return
8036
8037         local CPAGES=3
8038         local list=$(comma_list $(osts_nodes))
8039
8040         # check whether obdfilter is cache capable at all
8041         if ! get_obdfilter_param $list '' read_cache_enable >/dev/null; then
8042                 echo "not cache-capable obdfilter"
8043                 return 0
8044         fi
8045
8046         # check cache is enabled on all obdfilters
8047         if get_obdfilter_param $list '' read_cache_enable | grep 0; then
8048                 echo "oss cache is disabled"
8049                 return 0
8050         fi
8051
8052         set_obdfilter_param $list '' writethrough_cache_enable 1
8053
8054         # pages should be in the case right after write
8055         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
8056         local BEFORE=`roc_hit`
8057         cancel_lru_locks osc
8058         cat $DIR/$tfile >/dev/null
8059         local AFTER=`roc_hit`
8060         if ! let "AFTER - BEFORE == CPAGES"; then
8061                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8062         fi
8063
8064         # the following read invalidates the cache
8065         cancel_lru_locks osc
8066         set_obdfilter_param $list '' read_cache_enable 0
8067         cat $DIR/$tfile >/dev/null
8068
8069         # now data shouldn't be found in the cache
8070         BEFORE=`roc_hit`
8071         cancel_lru_locks osc
8072         cat $DIR/$tfile >/dev/null
8073         AFTER=`roc_hit`
8074         if let "AFTER - BEFORE != 0"; then
8075                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8076         fi
8077
8078         set_obdfilter_param $list '' read_cache_enable 1
8079         rm -f $DIR/$tfile
8080 }
8081 run_test 151 "test cache on oss and controls ==============================="
8082
8083 test_152() {
8084         local TF="$TMP/$tfile"
8085
8086         # simulate ENOMEM during write
8087 #define OBD_FAIL_OST_NOMEM      0x226
8088         lctl set_param fail_loc=0x80000226
8089         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8090         cp $TF $DIR/$tfile
8091         sync || error "sync failed"
8092         lctl set_param fail_loc=0
8093
8094         # discard client's cache
8095         cancel_lru_locks osc
8096
8097         # simulate ENOMEM during read
8098         lctl set_param fail_loc=0x80000226
8099         cmp $TF $DIR/$tfile || error "cmp failed"
8100         lctl set_param fail_loc=0
8101
8102         rm -f $TF
8103 }
8104 run_test 152 "test read/write with enomem ============================"
8105
8106 test_153() {
8107         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8108 }
8109 run_test 153 "test if fdatasync does not crash ======================="
8110
8111 test_154() {
8112         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8113                 { skip "Need MDS version at least 2.2.51"; return 0; }
8114
8115         cp /etc/hosts $DIR/$tfile
8116
8117         fid=$($LFS path2fid $DIR/$tfile)
8118         rc=$?
8119         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8120
8121         ffid=$DIR/.lustre/fid/$fid
8122
8123         echo "stat fid $fid"
8124         stat $ffid > /dev/null || error "stat $ffid failed."
8125         echo "touch fid $fid"
8126         touch $ffid || error "touch $ffid failed."
8127         echo "write to fid $fid"
8128         cat /etc/hosts > $ffid || error "write $ffid failed."
8129         echo "read fid $fid"
8130         diff /etc/hosts $ffid || error "read $ffid failed."
8131         echo "append write to fid $fid"
8132         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8133         echo "rename fid $fid"
8134         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
8135         touch $DIR/$tfile.1
8136         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
8137         rm -f $DIR/$tfile.1
8138         echo "truncate fid $fid"
8139         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8140         echo "link fid $fid"
8141         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
8142         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8143                 echo "setfacl fid $fid"
8144                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8145                 echo "getfacl fid $fid"
8146                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8147         fi
8148         echo "unlink fid $fid"
8149         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
8150         echo "mknod fid $fid"
8151         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8152
8153         fid=[0xf00000400:0x1:0x0]
8154         ffid=$DIR/.lustre/fid/$fid
8155
8156         echo "stat non-exist fid $fid"
8157         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8158         echo "write to non-exist fid $fid"
8159         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8160         echo "link new fid $fid"
8161         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8162
8163         mkdir -p $DIR/$tdir
8164         touch $DIR/$tdir/$tfile
8165         fid=$($LFS path2fid $DIR/$tdir)
8166         rc=$?
8167         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8168
8169         ffid=$DIR/.lustre/fid/$fid
8170
8171         echo "ls $fid"
8172         ls $ffid > /dev/null || error "ls $ffid failed."
8173         echo "touch $fid/$tfile.1"
8174         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8175
8176         echo "touch $DIR/.lustre/fid/$tfile"
8177         touch $DIR/.lustre/fid/$tfile && \
8178                 error "touch $DIR/.lustre/fid/$tfile should fail."
8179
8180         echo "setxattr to $DIR/.lustre/fid"
8181         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8182                 error "setxattr should fail."
8183
8184         echo "listxattr for $DIR/.lustre/fid"
8185         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8186                 error "listxattr should fail."
8187
8188         echo "delxattr from $DIR/.lustre/fid"
8189         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8190                 error "delxattr should fail."
8191
8192         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8193         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8194                 error "touch invalid fid should fail."
8195
8196         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8197         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8198                 error "touch non-normal fid should fail."
8199
8200         echo "rename $tdir to $DIR/.lustre/fid"
8201         mrename $DIR/$tdir $DIR/.lustre/fid &&
8202                 error "rename to $DIR/.lustre/fid should fail."
8203
8204         echo "rename .lustre to itself"
8205         fid=$($LFS path2fid $DIR)
8206         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8207                 error "rename .lustre to itself should fail."
8208
8209         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8210         fid=$($LFS path2fid $DIR/$tfile-2)
8211         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8212         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8213                 error "create lov data thru .lustre should fail."
8214         echo "cp /etc/passwd $DIR/$tfile-2"
8215         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8216         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8217         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8218                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8219
8220         echo "Open-by-FID succeeded"
8221 }
8222 run_test 154 "Open-by-FID"
8223
8224 test_155_small_load() {
8225     local temp=$TMP/$tfile
8226     local file=$DIR/$tfile
8227
8228     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8229         error "dd of=$temp bs=6096 count=1 failed"
8230     cp $temp $file
8231     cancel_lru_locks osc
8232     cmp $temp $file || error "$temp $file differ"
8233
8234     $TRUNCATE $temp 6000
8235     $TRUNCATE $file 6000
8236     cmp $temp $file || error "$temp $file differ (truncate1)"
8237
8238     echo "12345" >>$temp
8239     echo "12345" >>$file
8240     cmp $temp $file || error "$temp $file differ (append1)"
8241
8242     echo "12345" >>$temp
8243     echo "12345" >>$file
8244     cmp $temp $file || error "$temp $file differ (append2)"
8245
8246     rm -f $temp $file
8247     true
8248 }
8249
8250 test_155_big_load() {
8251     remote_ost_nodsh && skip "remote OST with nodsh" && return
8252     local temp=$TMP/$tfile
8253     local file=$DIR/$tfile
8254
8255     free_min_max
8256     local cache_size=$(do_facet ost$((MAXI+1)) \
8257         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8258     local large_file_size=$((cache_size * 2))
8259
8260     echo "OSS cache size: $cache_size KB"
8261     echo "Large file size: $large_file_size KB"
8262
8263     [ $MAXV -le $large_file_size ] && \
8264         skip_env "max available OST size needs > $large_file_size KB" && \
8265         return 0
8266
8267     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8268
8269     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8270         error "dd of=$temp bs=$large_file_size count=1k failed"
8271     cp $temp $file
8272     ls -lh $temp $file
8273     cancel_lru_locks osc
8274     cmp $temp $file || error "$temp $file differ"
8275
8276     rm -f $temp $file
8277     true
8278 }
8279
8280 test_155a() {
8281     set_cache read on
8282     set_cache writethrough on
8283     test_155_small_load
8284 }
8285 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8286
8287 test_155b() {
8288     set_cache read on
8289     set_cache writethrough off
8290     test_155_small_load
8291 }
8292 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8293
8294 test_155c() {
8295     set_cache read off
8296     set_cache writethrough on
8297     test_155_small_load
8298 }
8299 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8300
8301 test_155d() {
8302     set_cache read off
8303     set_cache writethrough off
8304     test_155_small_load
8305 }
8306 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8307
8308 test_155e() {
8309     set_cache read on
8310     set_cache writethrough on
8311     test_155_big_load
8312 }
8313 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8314
8315 test_155f() {
8316     set_cache read on
8317     set_cache writethrough off
8318     test_155_big_load
8319 }
8320 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8321
8322 test_155g() {
8323     set_cache read off
8324     set_cache writethrough on
8325     test_155_big_load
8326 }
8327 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8328
8329 test_155h() {
8330     set_cache read off
8331     set_cache writethrough off
8332     test_155_big_load
8333 }
8334 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8335
8336 test_156() {
8337     local CPAGES=3
8338     local BEFORE
8339     local AFTER
8340     local file="$DIR/$tfile"
8341
8342         [ "$(facet_fstype ost1)" = "zfs" ] &&
8343                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8344                 return
8345
8346     log "Turn on read and write cache"
8347     set_cache read on
8348     set_cache writethrough on
8349
8350     log "Write data and read it back."
8351     log "Read should be satisfied from the cache."
8352     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8353     BEFORE=`roc_hit`
8354     cancel_lru_locks osc
8355     cat $file >/dev/null
8356     AFTER=`roc_hit`
8357     if ! let "AFTER - BEFORE == CPAGES"; then
8358         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8359     else
8360         log "cache hits:: before: $BEFORE, after: $AFTER"
8361     fi
8362
8363     log "Read again; it should be satisfied from the cache."
8364     BEFORE=$AFTER
8365     cancel_lru_locks osc
8366     cat $file >/dev/null
8367     AFTER=`roc_hit`
8368     if ! let "AFTER - BEFORE == CPAGES"; then
8369         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8370     else
8371         log "cache hits:: before: $BEFORE, after: $AFTER"
8372     fi
8373
8374
8375     log "Turn off the read cache and turn on the write cache"
8376     set_cache read off
8377     set_cache writethrough on
8378
8379     log "Read again; it should be satisfied from the cache."
8380     BEFORE=`roc_hit`
8381     cancel_lru_locks osc
8382     cat $file >/dev/null
8383     AFTER=`roc_hit`
8384     if ! let "AFTER - BEFORE == CPAGES"; then
8385         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8386     else
8387         log "cache hits:: before: $BEFORE, after: $AFTER"
8388     fi
8389
8390     log "Read again; it should not be satisfied from the cache."
8391     BEFORE=$AFTER
8392     cancel_lru_locks osc
8393     cat $file >/dev/null
8394     AFTER=`roc_hit`
8395     if ! let "AFTER - BEFORE == 0"; then
8396         error "IN CACHE: before: $BEFORE, after: $AFTER"
8397     else
8398         log "cache hits:: before: $BEFORE, after: $AFTER"
8399     fi
8400
8401     log "Write data and read it back."
8402     log "Read should be satisfied from the cache."
8403     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8404     BEFORE=`roc_hit`
8405     cancel_lru_locks osc
8406     cat $file >/dev/null
8407     AFTER=`roc_hit`
8408     if ! let "AFTER - BEFORE == CPAGES"; then
8409         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8410     else
8411         log "cache hits:: before: $BEFORE, after: $AFTER"
8412     fi
8413
8414     log "Read again; it should not be satisfied from the cache."
8415     BEFORE=$AFTER
8416     cancel_lru_locks osc
8417     cat $file >/dev/null
8418     AFTER=`roc_hit`
8419     if ! let "AFTER - BEFORE == 0"; then
8420         error "IN CACHE: before: $BEFORE, after: $AFTER"
8421     else
8422         log "cache hits:: before: $BEFORE, after: $AFTER"
8423     fi
8424
8425
8426     log "Turn off read and write cache"
8427     set_cache read off
8428     set_cache writethrough off
8429
8430     log "Write data and read it back"
8431     log "It should not be satisfied from the cache."
8432     rm -f $file
8433     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8434     cancel_lru_locks osc
8435     BEFORE=`roc_hit`
8436     cat $file >/dev/null
8437     AFTER=`roc_hit`
8438     if ! let "AFTER - BEFORE == 0"; then
8439         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8440     else
8441         log "cache hits:: before: $BEFORE, after: $AFTER"
8442     fi
8443
8444
8445     log "Turn on the read cache and turn off the write cache"
8446     set_cache read on
8447     set_cache writethrough off
8448
8449     log "Write data and read it back"
8450     log "It should not be satisfied from the cache."
8451     rm -f $file
8452     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8453     BEFORE=`roc_hit`
8454     cancel_lru_locks osc
8455     cat $file >/dev/null
8456     AFTER=`roc_hit`
8457     if ! let "AFTER - BEFORE == 0"; then
8458         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8459     else
8460         log "cache hits:: before: $BEFORE, after: $AFTER"
8461     fi
8462
8463     log "Read again; it should be satisfied from the cache."
8464     BEFORE=`roc_hit`
8465     cancel_lru_locks osc
8466     cat $file >/dev/null
8467     AFTER=`roc_hit`
8468     if ! let "AFTER - BEFORE == CPAGES"; then
8469         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8470     else
8471         log "cache hits:: before: $BEFORE, after: $AFTER"
8472     fi
8473
8474     rm -f $file
8475 }
8476 run_test 156 "Verification of tunables ============================"
8477
8478 #Changelogs
8479 err17935 () {
8480     if [ $MDSCOUNT -gt 1 ]; then
8481         error_ignore 17935 $*
8482     else
8483         error $*
8484     fi
8485 }
8486
8487 changelog_chmask()
8488 {
8489     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8490            grep -c $1)
8491
8492     if [ $MASK -eq 1 ]; then
8493         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8494     else
8495         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8496     fi
8497 }
8498
8499 test_160() {
8500     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8501     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8502         { skip "Need MDS version at least 2.2.0"; return; }
8503     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8504     echo "Registered as changelog user $USER"
8505     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8506         grep -q $USER || error "User $USER not found in changelog_users"
8507
8508     # change something
8509     mkdir -p $DIR/$tdir/pics/2008/zachy
8510     touch $DIR/$tdir/pics/2008/zachy/timestamp
8511     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8512     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8513     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8514     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8515     rm $DIR/$tdir/pics/desktop.jpg
8516
8517     $LFS changelog $MDT0 | tail -5
8518
8519     echo "verifying changelog mask"
8520     changelog_chmask "MKDIR"
8521     changelog_chmask "CLOSE"
8522
8523     mkdir -p $DIR/$tdir/pics/zach/sofia
8524     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8525
8526     changelog_chmask "MKDIR"
8527     changelog_chmask "CLOSE"
8528
8529     mkdir -p $DIR/$tdir/pics/2008/sofia
8530     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8531
8532     $LFS changelog $MDT0
8533     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8534     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8535     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8536     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8537
8538     # verify contents
8539     echo "verifying target fid"
8540     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8541         tail -1 | awk '{print $6}')
8542     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8543     [ "$fidc" == "t=$fidf" ] || \
8544         err17935 "fid in changelog $fidc != file fid $fidf"
8545     echo "verifying parent fid"
8546     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8547         tail -1 | awk '{print $7}')
8548     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8549     [ "$fidc" == "p=$fidf" ] || \
8550         err17935 "pfid in changelog $fidc != dir fid $fidf"
8551
8552     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8553         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8554     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8555     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8556         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8557     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8558     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8559         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8560
8561     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8562         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8563     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8564     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8565     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8566         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8567
8568     echo "verifying user deregister"
8569     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8570     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8571         grep -q $USER && error "User $USER still found in changelog_users"
8572
8573     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8574         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8575     if [ $USERS -eq 0 ]; then
8576         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8577             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8578         touch $DIR/$tdir/chloe
8579         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8580             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8581         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8582         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8583     else
8584         echo "$USERS other changelog users; can't verify off"
8585     fi
8586 }
8587 run_test 160 "changelog sanity"
8588
8589 test_161() {
8590     mkdir -p $DIR/$tdir
8591     cp /etc/hosts $DIR/$tdir/$tfile
8592     mkdir $DIR/$tdir/foo1
8593     mkdir $DIR/$tdir/foo2
8594     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8595     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8596     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8597     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8598     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8599     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8600         $LFS fid2path $DIR $FID
8601         err17935 "bad link ea"
8602     fi
8603     # middle
8604     rm $DIR/$tdir/foo2/zachary
8605     # last
8606     rm $DIR/$tdir/foo2/thor
8607     # first
8608     rm $DIR/$tdir/$tfile
8609     # rename
8610     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8611     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8612         then
8613         $LFS fid2path $DIR $FID
8614         err17935 "bad link rename"
8615     fi
8616     rm $DIR/$tdir/foo2/maggie
8617
8618     # overflow the EA
8619     local longname=filename_avg_len_is_thirty_two_
8620     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8621         error "failed to hardlink many files"
8622     links=$($LFS fid2path $DIR $FID | wc -l)
8623     echo -n "${links}/1000 links in link EA"
8624     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8625     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8626         error "failed to unlink many hardlinks"
8627 }
8628 run_test 161 "link ea sanity"
8629
8630 check_path() {
8631     local expected=$1
8632     shift
8633     local fid=$2
8634
8635     local path=$(${LFS} fid2path $*)
8636     RC=$?
8637
8638     if [ $RC -ne 0 ]; then
8639         err17935 "path looked up of $expected failed. Error $RC"
8640         return $RC
8641     elif [ "${path}" != "${expected}" ]; then
8642         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8643         return 2
8644     fi
8645     echo "fid $fid resolves to path $path (expected $expected)"
8646 }
8647
8648 test_162() {
8649         # Make changes to filesystem
8650         mkdir -p $DIR/$tdir/d2
8651         touch $DIR/$tdir/d2/$tfile
8652         touch $DIR/$tdir/d2/x1
8653         touch $DIR/$tdir/d2/x2
8654         mkdir -p $DIR/$tdir/d2/a/b/c
8655         mkdir -p $DIR/$tdir/d2/p/q/r
8656         # regular file
8657         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8658         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8659
8660         # softlink
8661         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8662         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8663         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8664
8665         # softlink to wrong file
8666         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8667         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8668         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8669
8670         # hardlink
8671         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8672         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8673         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8674         # fid2path dir/fsname should both work
8675         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8676         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8677
8678         # hardlink count: check that there are 2 links
8679         # Doesnt work with CMD yet: 17935
8680         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8681                 err17935 "expected 2 links"
8682
8683         # hardlink indexing: remove the first link
8684         rm $DIR/$tdir/d2/p/q/r/hlink
8685         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8686
8687         return 0
8688 }
8689 run_test 162 "path lookup sanity"
8690
8691 test_163() {
8692         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8693         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8694         copytool $FSNAME &
8695         sleep 1
8696         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8697         # this proc file is temporary and linux-only
8698         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8699          error "kernel->userspace send failed"
8700         kill -INT $!
8701 }
8702 run_test 163 "kernel <-> userspace comms"
8703
8704 test_169() {
8705         # do directio so as not to populate the page cache
8706         log "creating a 10 Mb file"
8707         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8708         log "starting reads"
8709         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8710         log "truncating the file"
8711         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8712         log "killing dd"
8713         kill %+ || true # reads might have finished
8714         echo "wait until dd is finished"
8715         wait
8716         log "removing the temporary file"
8717         rm -rf $DIR/$tfile || error "tmp file removal failed"
8718 }
8719 run_test 169 "parallel read and truncate should not deadlock"
8720
8721 test_170() {
8722         $LCTL clear     # bug 18514
8723         $LCTL debug_daemon start $TMP/${tfile}_log_good
8724         touch $DIR/$tfile
8725         $LCTL debug_daemon stop
8726         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8727                error "sed failed to read log_good"
8728
8729         $LCTL debug_daemon start $TMP/${tfile}_log_good
8730         rm -rf $DIR/$tfile
8731         $LCTL debug_daemon stop
8732
8733         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8734                error "lctl df log_bad failed"
8735
8736         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8737         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8738
8739         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8740         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8741
8742         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8743                 error "bad_line good_line1 good_line2 are empty"
8744
8745         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8746         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8747         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8748
8749         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8750         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8751         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8752
8753         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8754                 error "bad_line_new good_line_new are empty"
8755
8756         local expected_good=$((good_line1 + good_line2*2))
8757
8758         rm -f $TMP/${tfile}*
8759         # LU-231, short malformed line may not be counted into bad lines
8760         if [ $bad_line -ne $bad_line_new ] &&
8761                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8762                 error "expected $bad_line bad lines, but got $bad_line_new"
8763                 return 1
8764         fi
8765
8766         if [ $expected_good -ne $good_line_new ]; then
8767                 error "expected $expected_good good lines, but got $good_line_new"
8768                 return 2
8769         fi
8770         true
8771 }
8772 run_test 170 "test lctl df to handle corrupted log ====================="
8773
8774 test_171() { # bug20592
8775 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8776         $LCTL set_param fail_loc=0x50e
8777         $LCTL set_param fail_val=3000
8778         multiop_bg_pause $DIR/$tfile O_s || true
8779         local MULTIPID=$!
8780         kill -USR1 $MULTIPID
8781         # cause log dump
8782         sleep 3
8783         wait $MULTIPID
8784         if dmesg | grep "recursive fault"; then
8785                 error "caught a recursive fault"
8786         fi
8787         $LCTL set_param fail_loc=0
8788         true
8789 }
8790 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8791
8792 # it would be good to share it with obdfilter-survey/libecho code
8793 setup_obdecho_osc () {
8794         local rc=0
8795         local ost_nid=$1
8796         local obdfilter_name=$2
8797         echo "Creating new osc for $obdfilter_name on $ost_nid"
8798         # make sure we can find loopback nid
8799         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8800
8801         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8802                            ${obdfilter_name}_osc_UUID || rc=2; }
8803         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8804                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8805         return $rc
8806 }
8807
8808 cleanup_obdecho_osc () {
8809         local obdfilter_name=$1
8810         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8811         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8812         return 0
8813 }
8814
8815 obdecho_create_test() {
8816         local OBD=$1
8817         local node=$2
8818         local rc=0
8819         local id
8820         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8821         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8822                            rc=2; }
8823         if [ $rc -eq 0 ]; then
8824             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8825             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8826         fi
8827         echo "New object id is $id"
8828         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8829                            rc=4; }
8830         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8831                                         "cleanup" || rc=5; }
8832         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8833                                         "detach" || rc=6; }
8834         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8835         return $rc
8836 }
8837
8838 test_180a() {
8839         remote_ost_nodsh && skip "remote OST with nodsh" && return
8840         local rc=0
8841         local rmmod_local=0
8842
8843         if ! module_loaded obdecho; then
8844             load_module obdecho/obdecho
8845             rmmod_local=1
8846         fi
8847
8848         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8849         local host=$(lctl get_param -n osc.$osc.import |
8850                              awk '/current_connection:/ {print $2}' )
8851         local target=$(lctl get_param -n osc.$osc.import |
8852                              awk '/target:/ {print $2}' )
8853         target=${target%_UUID}
8854
8855         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8856         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8857         [[ -n $target ]] && cleanup_obdecho_osc $target
8858         [ $rmmod_local -eq 1 ] && rmmod obdecho
8859         return $rc
8860 }
8861 run_test 180a "test obdecho on osc"
8862
8863 test_180b() {
8864         remote_ost_nodsh && skip "remote OST with nodsh" && return
8865         local rc=0
8866         local rmmod_remote=0
8867
8868         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8869                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8870                       "modprobe obdecho; }" && rmmod_remote=1
8871         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8872         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8873         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8874         return $rc
8875 }
8876 run_test 180b "test obdecho directly on obdfilter"
8877
8878 test_181() { # bug 22177
8879         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8880         # create enough files to index the directory
8881         createmany -o $DIR/$tdir/foobar 4000
8882         # print attributes for debug purpose
8883         lsattr -d .
8884         # open dir
8885         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8886         MULTIPID=$!
8887         # remove the files & current working dir
8888         unlinkmany $DIR/$tdir/foobar 4000
8889         rmdir $DIR/$tdir
8890         kill -USR1 $MULTIPID
8891         wait $MULTIPID
8892         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
8893         return 0
8894 }
8895 run_test 181 "Test open-unlinked dir ========================"
8896
8897 test_182() {
8898         # disable MDC RPC lock wouldn't crash client
8899         local fcount=1000
8900         local tcount=4
8901
8902         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8903 #define OBD_FAIL_MDC_RPCS_SEM           0x804
8904         $LCTL set_param fail_loc=0x804
8905
8906         for (( i=0; i < $tcount; i++ )) ; do
8907                 mkdir $DIR/$tdir/$i
8908                 createmany -o $DIR/$tdir/$i/f- $fcount &
8909         done
8910         wait
8911
8912         for (( i=0; i < $tcount; i++ )) ; do
8913                 unlinkmany $DIR/$tdir/$i/f- $fcount &
8914         done
8915         wait
8916
8917         rm -rf $DIR/$tdir
8918
8919         $LCTL set_param fail_loc=0
8920 }
8921 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
8922
8923 test_183() { # LU-2275
8924         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8925         echo aaa > $DIR/$tdir/$tfile
8926
8927 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
8928         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
8929
8930         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
8931         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
8932
8933         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
8934
8935         # Flush negative dentry cache
8936         touch $DIR/$tdir/$tfile
8937
8938         # We are not checking for any leaked references here, they'll
8939         # become evident next time we do cleanup with module unload.
8940         rm -rf $DIR/$tdir
8941 }
8942 run_test 183 "No crash or request leak in case of strange dispositions ========"
8943
8944 # OST pools tests
8945 check_file_in_pool()
8946 {
8947         local file=$1
8948         local pool=$2
8949         local tlist="$3"
8950         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
8951         for i in $res
8952         do
8953                 for t in $tlist ; do
8954                         [ "$i" -eq "$t" ] && continue 2
8955                 done
8956
8957                 echo "pool list: $tlist"
8958                 echo "striping: $res"
8959                 error_noexit "$file not allocated in $pool"
8960                 return 1
8961         done
8962         return 0
8963 }
8964
8965 pool_add() {
8966         echo "Creating new pool"
8967         local pool=$1
8968
8969         create_pool $FSNAME.$pool ||
8970                 { error_noexit "No pool created, result code $?"; return 1; }
8971         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
8972                 { error_noexit "$pool not in lfs pool_list"; return 2; }
8973 }
8974
8975 pool_add_targets() {
8976         echo "Adding targets to pool"
8977         local pool=$1
8978         local first=$2
8979         local last=$3
8980         local step=${4:-1}
8981
8982         local list=$(seq $first $step $last)
8983
8984         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
8985         do_facet mgs $LCTL pool_add \
8986                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
8987         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
8988                         | sort -u | tr '\n' ' ' " "$t" || { 
8989                 error_noexit "Add to pool failed"
8990                 return 1
8991         }
8992         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
8993         local addcount=$(((last - first) / step + 1))
8994         [ $lfscount -eq $addcount ] || {
8995                 error_noexit "lfs pool_list bad ost count" \
8996                                                 "$lfscount != $addcount"
8997                 return 2
8998         }
8999 }
9000
9001 pool_set_dir() {
9002         local pool=$1
9003         local tdir=$2
9004         echo "Setting pool on directory $tdir"
9005
9006         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9007
9008         error_noexit "Cannot set pool $pool to $tdir"
9009         return 1
9010 }
9011
9012 pool_check_dir() {
9013         local pool=$1
9014         local tdir=$2
9015         echo "Checking pool on directory $tdir"
9016
9017         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9018         [ "$res" = "$pool" ] && return 0
9019
9020         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9021         return 1
9022 }
9023
9024 pool_dir_rel_path() {
9025         echo "Testing relative path works well"
9026         local pool=$1
9027         local tdir=$2
9028         local root=$3
9029
9030         mkdir -p $root/$tdir/$tdir
9031         cd $root/$tdir
9032         pool_set_dir $pool $tdir          || return 1
9033         pool_set_dir $pool ./$tdir        || return 2
9034         pool_set_dir $pool ../$tdir       || return 3
9035         pool_set_dir $pool ../$tdir/$tdir || return 4
9036         rm -rf $tdir; cd - > /dev/null
9037 }
9038
9039 pool_alloc_files() {
9040         echo "Checking files allocation from directory pool"
9041         local pool=$1
9042         local tdir=$2
9043         local count=$3
9044         local tlist="$4"
9045
9046         local failed=0
9047         for i in $(seq -w 1 $count)
9048         do
9049                 local file=$tdir/file-$i
9050                 touch $file
9051                 check_file_in_pool $file $pool "$tlist" || \
9052                         failed=$((failed + 1))
9053         done
9054         [ "$failed" = 0 ] && return 0
9055
9056         error_noexit "$failed files not allocated in $pool"
9057         return 1
9058 }
9059
9060 pool_create_files() {
9061         echo "Creating files in pool"
9062         local pool=$1
9063         local tdir=$2
9064         local count=$3
9065         local tlist="$4"
9066
9067         mkdir -p $tdir
9068         local failed=0
9069         for i in $(seq -w 1 $count)
9070         do
9071                 local file=$tdir/spoo-$i
9072                 $SETSTRIPE -p $pool $file
9073                 check_file_in_pool $file $pool "$tlist" || \
9074                         failed=$((failed + 1))
9075         done
9076         [ "$failed" = 0 ] && return 0
9077
9078         error_noexit "$failed files not allocated in $pool"
9079         return 1
9080 }
9081
9082 pool_lfs_df() {
9083         echo "Checking 'lfs df' output"
9084         local pool=$1
9085
9086         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9087                         tr '\n' ' ')
9088         local res=$($LFS df --pool $FSNAME.$pool |
9089                         awk '{print $1}' |
9090                         grep "$FSNAME-OST" |
9091                         tr '\n' ' ')
9092         [ "$res" = "$t" ] && return 0
9093
9094         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9095         return 1
9096 }
9097
9098 pool_file_rel_path() {
9099         echo "Creating files in a pool with relative pathname"
9100         local pool=$1
9101         local tdir=$2
9102
9103         mkdir -p $tdir ||
9104                 { error_noexit "unable to create $tdir"; return 1 ; }
9105         local file="/..$tdir/$tfile-1"
9106         $SETSTRIPE -p $pool $file ||
9107                 { error_noexit "unable to create $file" ; return 2 ; }
9108
9109         cd $tdir
9110         $SETSTRIPE -p $pool $tfile-2 || {
9111                 error_noexit "unable to create $tfile-2 in $tdir"
9112                 return 3
9113         }
9114 }
9115
9116 pool_remove_first_target() {
9117         echo "Removing first target from a pool"
9118         local pool=$1
9119
9120         local pname="lov.$FSNAME-*.pools.$pool"
9121         local t=$($LCTL get_param -n $pname | head -1)
9122         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9123         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9124                 error_noexit "$t not removed from $FSNAME.$pool"
9125                 return 1
9126         }
9127 }
9128
9129 pool_remove_all_targets() {
9130         echo "Removing all targets from pool"
9131         local pool=$1
9132         local file=$2
9133         local pname="lov.$FSNAME-*.pools.$pool"
9134         for t in $($LCTL get_param -n $pname | sort -u)
9135         do
9136                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9137         done
9138         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9139                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9140                 return 1
9141         }
9142         # striping on an empty/nonexistant pool should fall back 
9143         # to "pool of everything"
9144         touch $file || {
9145                 error_noexit "failed to use fallback striping for empty pool"
9146                 return 2
9147         }
9148         # setstripe on an empty pool should fail
9149         $SETSTRIPE -p $pool $file 2>/dev/null && {
9150                 error_noexit "expected failure when creating file" \
9151                                                         "with empty pool"
9152                 return 3
9153         }
9154         return 0
9155 }
9156
9157 pool_remove() {
9158         echo "Destroying pool"
9159         local pool=$1
9160         local file=$2
9161
9162         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9163
9164         sleep 2
9165         # striping on an empty/nonexistant pool should fall back 
9166         # to "pool of everything"
9167         touch $file || {
9168                 error_noexit "failed to use fallback striping for missing pool"
9169                 return 1
9170         }
9171         # setstripe on an empty pool should fail
9172         $SETSTRIPE -p $pool $file 2>/dev/null && {
9173                 error_noexit "expected failure when creating file" \
9174                                                         "with missing pool"
9175                 return 2
9176         }
9177
9178         # get param should return err once pool is gone
9179         if wait_update $HOSTNAME "lctl get_param -n \
9180                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9181         then
9182                 remove_pool_from_list $FSNAME.$pool
9183                 return 0
9184         fi
9185         error_noexit "Pool $FSNAME.$pool is not destroyed"
9186         return 3
9187 }
9188
9189 test_200() {
9190         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9191
9192         local POOL=${POOL:-cea1}
9193         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9194         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9195         # Pool OST targets
9196         local first_ost=0
9197         local last_ost=$(($OSTCOUNT - 1))
9198         local ost_step=2
9199         local ost_list=$(seq $first_ost $ost_step $last_ost)
9200         local ost_range="$first_ost $last_ost $ost_step"
9201         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9202         local file_dir=$POOL_ROOT/file_tst
9203
9204         local rc=0
9205         while : ; do
9206                 # former test_200a test_200b
9207                 pool_add $POOL                          || { rc=$? ; break; }
9208                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9209                 # former test_200c test_200d
9210                 mkdir -p $test_path
9211                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9212                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9213                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9214                                                         || { rc=$? ; break; }
9215                 # former test_200e test_200f
9216                 local files=$((OSTCOUNT*3))
9217                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9218                                                         || { rc=$? ; break; }
9219                 pool_create_files $POOL $file_dir $files "$ost_list" \
9220                                                         || { rc=$? ; break; }
9221                 # former test_200g test_200h
9222                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9223                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9224
9225                 # former test_201a test_201b test_201c
9226                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9227
9228                 local f=$test_path/$tfile
9229                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9230                 pool_remove $POOL $f                    || { rc=$? ; break; }
9231                 break
9232         done
9233
9234         cleanup_pools
9235         return $rc
9236 }
9237 run_test 200 "OST pools"
9238
9239 # usage: default_attr <count | size | offset>
9240 default_attr() {
9241         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9242 }
9243
9244 # usage: check_default_stripe_attr
9245 check_default_stripe_attr() {
9246         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9247         case $1 in
9248         --stripe-count|--count)
9249                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9250         --stripe-size|--size)
9251                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9252         --stripe-index|--index)
9253                 EXPECTED=-1;;
9254         *)
9255                 error "unknown getstripe attr '$1'"
9256         esac
9257
9258         [ $ACTUAL != $EXPECTED ] &&
9259                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9260 }
9261
9262 test_204a() {
9263         mkdir -p $DIR/$tdir
9264         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9265
9266         check_default_stripe_attr --stripe-count
9267         check_default_stripe_attr --stripe-size
9268         check_default_stripe_attr --stripe-index
9269
9270         return 0
9271 }
9272 run_test 204a "Print default stripe attributes ================="
9273
9274 test_204b() {
9275         mkdir -p $DIR/$tdir
9276         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9277
9278         check_default_stripe_attr --stripe-size
9279         check_default_stripe_attr --stripe-index
9280
9281         return 0
9282 }
9283 run_test 204b "Print default stripe size and offset  ==========="
9284
9285 test_204c() {
9286         mkdir -p $DIR/$tdir
9287         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9288
9289         check_default_stripe_attr --stripe-count
9290         check_default_stripe_attr --stripe-index
9291
9292         return 0
9293 }
9294 run_test 204c "Print default stripe count and offset ==========="
9295
9296 test_204d() {
9297         mkdir -p $DIR/$tdir
9298         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9299
9300         check_default_stripe_attr --stripe-count
9301         check_default_stripe_attr --stripe-size
9302
9303         return 0
9304 }
9305 run_test 204d "Print default stripe count and size ============="
9306
9307 test_204e() {
9308         mkdir -p $DIR/$tdir
9309         $SETSTRIPE -d $DIR/$tdir
9310
9311         check_default_stripe_attr --stripe-count --raw
9312         check_default_stripe_attr --stripe-size --raw
9313         check_default_stripe_attr --stripe-index --raw
9314
9315         return 0
9316 }
9317 run_test 204e "Print raw stripe attributes ================="
9318
9319 test_204f() {
9320         mkdir -p $DIR/$tdir
9321         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9322
9323         check_default_stripe_attr --stripe-size --raw
9324         check_default_stripe_attr --stripe-index --raw
9325
9326         return 0
9327 }
9328 run_test 204f "Print raw stripe size and offset  ==========="
9329
9330 test_204g() {
9331         mkdir -p $DIR/$tdir
9332         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9333
9334         check_default_stripe_attr --stripe-count --raw
9335         check_default_stripe_attr --stripe-index --raw
9336
9337         return 0
9338 }
9339 run_test 204g "Print raw stripe count and offset ==========="
9340
9341 test_204h() {
9342         mkdir -p $DIR/$tdir
9343         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9344
9345         check_default_stripe_attr --stripe-count --raw
9346         check_default_stripe_attr --stripe-size --raw
9347
9348         return 0
9349 }
9350 run_test 204h "Print raw stripe count and size ============="
9351
9352 # Figure out which job scheduler is being used, if any,
9353 # or use a fake one
9354 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9355         JOBENV=SLURM_JOB_ID
9356 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9357         JOBENV=LSB_JOBID
9358 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9359         JOBENV=PBS_JOBID
9360 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9361         JOBENV=LOADL_STEP_ID
9362 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9363         JOBENV=JOB_ID
9364 else
9365         JOBENV=FAKE_JOBID
9366 fi
9367
9368 verify_jobstats() {
9369         local cmd=$1
9370         local target=$2
9371
9372         # clear old jobstats
9373         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9374         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9375
9376         # use a new JobID for this test, or we might see an old one
9377         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9378
9379         JOBVAL=${!JOBENV}
9380         log "Test: $cmd"
9381         log "Using JobID environment variable $JOBENV=$JOBVAL"
9382
9383         if [ $JOBENV = "FAKE_JOBID" ]; then
9384                 FAKE_JOBID=$JOBVAL $cmd
9385         else
9386                 $cmd
9387         fi
9388
9389         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9390                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9391                 do_facet $FACET lctl get_param mdt.*.job_stats |
9392                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9393         fi
9394         if [ "$target" = "ost" -o "$target" = "both" ]; then
9395                 FACET=ost1
9396                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9397                         grep $JOBVAL || error "No job stats found on OST $FACET"
9398         fi
9399 }
9400
9401 test_205() { # Job stats
9402         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9403                 skip "Server doesn't support jobstats" && return 0
9404
9405         local cmd
9406         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9407         if [ $OLD_JOBENV != $JOBENV ]; then
9408                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9409                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9410                         $JOBENV || return 1
9411         fi
9412
9413         # mkdir
9414         cmd="mkdir $DIR/$tfile"
9415         verify_jobstats "$cmd" "mdt"
9416         # rmdir
9417         cmd="rm -fr $DIR/$tfile"
9418         verify_jobstats "$cmd" "mdt"
9419         # mknod
9420         cmd="mknod $DIR/$tfile c 1 3"
9421         verify_jobstats "$cmd" "mdt"
9422         # unlink
9423         cmd="rm -f $DIR/$tfile"
9424         verify_jobstats "$cmd" "mdt"
9425         # open & close
9426         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9427         verify_jobstats "$cmd" "mdt"
9428         # setattr
9429         cmd="touch $DIR/$tfile"
9430         verify_jobstats "$cmd" "both"
9431         # write
9432         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9433         verify_jobstats "$cmd" "ost"
9434         # read
9435         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9436         verify_jobstats "$cmd" "ost"
9437         # truncate
9438         cmd="$TRUNCATE $DIR/$tfile 0"
9439         verify_jobstats "$cmd" "both"
9440         # rename
9441         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9442         verify_jobstats "$cmd" "mdt"
9443
9444         # cleanup
9445         rm -f $DIR/jobstats_test_rename
9446
9447         if [ $OLD_JOBENV != $JOBENV ]; then
9448                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9449                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9450                         $OLD_JOBENV || return 1
9451         fi
9452 }
9453 run_test 205 "Verify job stats"
9454
9455 # LU-1480, LU-1773 and LU-1657
9456 test_206() {
9457         mkdir -p $DIR/$tdir
9458         lfs setstripe -c -1 $DIR/$tdir
9459 #define OBD_FAIL_LOV_INIT 0x1403
9460         $LCTL set_param fail_loc=0xa0001403
9461         $LCTL set_param fail_val=1
9462         touch $DIR/$tdir/$tfile || true
9463 }
9464 run_test 206 "fail lov_init_raid0() doesn't lbug"
9465
9466 test_212() {
9467         size=`date +%s`
9468         size=$((size % 8192 + 1))
9469         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9470         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9471         rm -f $DIR/f212 $DIR/f212.xyz
9472 }
9473 run_test 212 "Sendfile test ============================================"
9474
9475 test_213() {
9476         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9477         cancel_lru_locks osc
9478         lctl set_param fail_loc=0x8000040f
9479         # generate a read lock
9480         cat $DIR/$tfile > /dev/null
9481         # write to the file, it will try to cancel the above read lock.
9482         cat /etc/hosts >> $DIR/$tfile
9483 }
9484 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9485
9486 test_214() { # for bug 20133
9487         mkdir -p $DIR/d214p/d214c
9488         for (( i=0; i < 340; i++ )) ; do
9489                 touch $DIR/d214p/d214c/a$i
9490         done
9491
9492         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9493         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9494         ls $DIR/d214c || error "ls $DIR/d214c failed"
9495         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9496 }
9497 run_test 214 "hash-indexed directory test - bug 20133"
9498
9499 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9500 create_lnet_proc_files() {
9501         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9502         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9503
9504         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9505         rm -f "$TMP/lnet_$1.sys_tmp"
9506 }
9507
9508 # counterpart of create_lnet_proc_files
9509 remove_lnet_proc_files() {
9510         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9511 }
9512
9513 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9514 # 3rd arg as regexp for body
9515 check_lnet_proc_stats() {
9516         local l=$(cat "$TMP/lnet_$1" |wc -l)
9517         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9518
9519         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9520 }
9521
9522 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9523 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9524 # optional and can be regexp for 2nd line (lnet.routes case)
9525 check_lnet_proc_entry() {
9526         local blp=2            # blp stands for 'position of 1st line of body'
9527         [ "$5" = "" ] || blp=3 # lnet.routes case
9528
9529         local l=$(cat "$TMP/lnet_$1" |wc -l)
9530         # subtracting one from $blp because the body can be empty
9531         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9532
9533         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9534                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9535
9536         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9537                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9538
9539         # bail out if any unexpected line happened
9540         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9541         [ "$?" != 0 ] || error "$2 misformatted"
9542 }
9543
9544 test_215() { # for bugs 18102, 21079, 21517
9545         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9546         local P='[1-9][0-9]*'           # positive numeric
9547         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9548         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9549         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9550         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9551
9552         local L1 # regexp for 1st line
9553         local L2 # regexp for 2nd line (optional)
9554         local BR # regexp for the rest (body)
9555
9556         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9557         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9558         create_lnet_proc_files "stats"
9559         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9560         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9561         remove_lnet_proc_files "stats"
9562
9563         # /proc/sys/lnet/routes should look like this:
9564         # Routing disabled/enabled
9565         # net hops state router
9566         # where net is a string like tcp0, hops >= 0, state is up/down,
9567         # router is a string like 192.168.1.1@tcp2
9568         L1="^Routing (disabled|enabled)$"
9569         L2="^net +hops +state +router$"
9570         BR="^$NET +$N +(up|down) +$NID$"
9571         create_lnet_proc_files "routes"
9572         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9573         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9574         remove_lnet_proc_files "routes"
9575
9576         # /proc/sys/lnet/routers should look like this:
9577         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9578         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9579         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9580         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9581         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9582         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9583         create_lnet_proc_files "routers"
9584         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9585         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9586         remove_lnet_proc_files "routers"
9587
9588         # /proc/sys/lnet/peers should look like this:
9589         # nid refs state last max rtr min tx min queue
9590         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9591         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9592         # numeric (0 or >0 or <0), queue >= 0.
9593         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9594         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9595         create_lnet_proc_files "peers"
9596         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9597         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9598         remove_lnet_proc_files "peers"
9599
9600         # /proc/sys/lnet/buffers  should look like this:
9601         # pages count credits min
9602         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9603         L1="^pages +count +credits +min$"
9604         BR="^ +$N +$N +$I +$I$"
9605         create_lnet_proc_files "buffers"
9606         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9607         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9608         remove_lnet_proc_files "buffers"
9609
9610         # /proc/sys/lnet/nis should look like this:
9611         # nid status alive refs peer rtr max tx min
9612         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9613         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9614         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9615         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9616         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9617         create_lnet_proc_files "nis"
9618         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9619         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9620         remove_lnet_proc_files "nis"
9621
9622         # can we successfully write to /proc/sys/lnet/stats?
9623         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9624         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9625 }
9626 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9627
9628 test_216() { # bug 20317
9629         remote_ost_nodsh && skip "remote OST with nodsh" && return
9630         local node
9631         local p="$TMP/sanityN-$TESTNAME.parameters"
9632         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9633         for node in $(osts_nodes); do
9634                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9635                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9636                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9637         done
9638         clear_osc_stats
9639
9640         # agressive lockless i/o settings
9641         for node in $(osts_nodes); do
9642                 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'
9643         done
9644         lctl set_param -n osc.*.contention_seconds 60
9645
9646         $DIRECTIO write $DIR/$tfile 0 10 4096
9647         $CHECKSTAT -s 40960 $DIR/$tfile
9648
9649         # disable lockless i/o
9650         for node in $(osts_nodes); do
9651                 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'
9652         done
9653         lctl set_param -n osc.*.contention_seconds 0
9654         clear_osc_stats
9655
9656         dd if=/dev/zero of=$DIR/$tfile count=0
9657         $CHECKSTAT -s 0 $DIR/$tfile
9658
9659         restore_lustre_params <$p
9660         rm -f $p
9661         rm $DIR/$tfile
9662 }
9663 run_test 216 "check lockless direct write works and updates file size and kms correctly"
9664
9665 test_217() { # bug 22430
9666         local node
9667         local nid
9668
9669         for node in $(nodes_list); do
9670                 nid=$(host_nids_address $node $NETTYPE)
9671                 if [[ $nid = *-* ]] ; then
9672                         echo "lctl ping $nid@$NETTYPE"
9673                         lctl ping $nid@$NETTYPE
9674                 else
9675                         echo "skipping $node (no hyphen detected)"
9676                 fi
9677         done
9678 }
9679 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
9680
9681 test_218() {
9682        # do directio so as not to populate the page cache
9683        log "creating a 10 Mb file"
9684        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9685        log "starting reads"
9686        dd if=$DIR/$tfile of=/dev/null bs=4096 &
9687        log "truncating the file"
9688        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9689        log "killing dd"
9690        kill %+ || true # reads might have finished
9691        echo "wait until dd is finished"
9692        wait
9693        log "removing the temporary file"
9694        rm -rf $DIR/$tfile || error "tmp file removal failed"
9695 }
9696 run_test 218 "parallel read and truncate should not deadlock ======================="
9697
9698 test_219() {
9699         # write one partial page
9700         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
9701         # set no grant so vvp_io_commit_write will do sync write
9702         $LCTL set_param fail_loc=0x411
9703         # write a full page at the end of file
9704         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
9705
9706         $LCTL set_param fail_loc=0
9707         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
9708         $LCTL set_param fail_loc=0x411
9709         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
9710 }
9711 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
9712
9713 test_220() { #LU-325
9714         remote_ost_nodsh && skip "remote OST with nodsh" && return
9715         local OSTIDX=0
9716
9717         mkdir -p $DIR/$tdir
9718         local OST=$(lfs osts | grep ${OSTIDX}": " | \
9719                 awk '{print $2}' | sed -e 's/_UUID$//')
9720
9721         # on the mdt's osc
9722         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
9723         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
9724                         osc.$mdtosc_proc1.prealloc_last_id)
9725         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
9726                         osc.$mdtosc_proc1.prealloc_next_id)
9727
9728         $LFS df -i
9729
9730         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
9731         #define OBD_FAIL_OST_ENOINO              0x229
9732         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
9733         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
9734         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
9735
9736         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
9737
9738         MDSOBJS=$((last_id - next_id))
9739         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
9740
9741         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
9742         echo "OST still has $count kbytes free"
9743
9744         echo "create $MDSOBJS files @next_id..."
9745         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
9746
9747         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9748                         osc.$mdtosc_proc1.prealloc_last_id)
9749         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9750                         osc.$mdtosc_proc1.prealloc_next_id)
9751
9752         echo "after creation, last_id=$last_id2, next_id=$next_id2"
9753         $LFS df -i
9754
9755         echo "cleanup..."
9756
9757         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
9758         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
9759
9760         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
9761         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
9762         echo "unlink $MDSOBJS files @$next_id..."
9763         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
9764 }
9765 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
9766
9767 test_221() {
9768         dd if=`which date` of=$MOUNT/date oflag=sync
9769         chmod +x $MOUNT/date
9770
9771         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
9772         $LCTL set_param fail_loc=0x80001401
9773
9774         $MOUNT/date > /dev/null
9775         rm -f $MOUNT/date
9776 }
9777 run_test 221 "make sure fault and truncate race to not cause OOM"
9778
9779 test_222a () {
9780        rm -rf $DIR/$tdir
9781        mkdir -p $DIR/$tdir
9782        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9783        createmany -o $DIR/$tdir/$tfile 10
9784        cancel_lru_locks mdc
9785        cancel_lru_locks osc
9786        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9787        $LCTL set_param fail_loc=0x31a
9788        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
9789        $LCTL set_param fail_loc=0
9790        rm -r $DIR/$tdir
9791 }
9792 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
9793
9794 test_222b () {
9795        rm -rf $DIR/$tdir
9796        mkdir -p $DIR/$tdir
9797        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9798        createmany -o $DIR/$tdir/$tfile 10
9799        cancel_lru_locks mdc
9800        cancel_lru_locks osc
9801        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9802        $LCTL set_param fail_loc=0x31a
9803        rm -r $DIR/$tdir || "AGL for rmdir failed"
9804        $LCTL set_param fail_loc=0
9805 }
9806 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
9807
9808 test_223 () {
9809        rm -rf $DIR/$tdir
9810        mkdir -p $DIR/$tdir
9811        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9812        createmany -o $DIR/$tdir/$tfile 10
9813        cancel_lru_locks mdc
9814        cancel_lru_locks osc
9815        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
9816        $LCTL set_param fail_loc=0x31b
9817        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
9818        $LCTL set_param fail_loc=0
9819        rm -r $DIR/$tdir
9820 }
9821 run_test 223 "osc reenqueue if without AGL lock granted ======================="
9822
9823 test_224a() { # LU-1039, MRP-303
9824         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
9825         $LCTL set_param fail_loc=0x508
9826         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
9827         $LCTL set_param fail_loc=0
9828         df $DIR
9829 }
9830 run_test 224a "Don't panic on bulk IO failure"
9831
9832 test_224b() { # LU-1039, MRP-303
9833         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
9834         cancel_lru_locks osc
9835         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
9836         $LCTL set_param fail_loc=0x515
9837         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
9838         $LCTL set_param fail_loc=0
9839         df $DIR
9840 }
9841 run_test 224b "Don't panic on bulk IO failure"
9842
9843 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
9844 test_225a () {
9845        if [ -z ${MDSSURVEY} ]; then
9846               skip_env "mds-survey not found" && return
9847        fi
9848        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9849             { skip "Need MDS version at least 2.2.51"; return; }
9850
9851        local mds=$(facet_host $SINGLEMDS)
9852        local target=$(do_nodes $mds 'lctl dl' | \
9853                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9854
9855        local cmd1="file_count=1000 thrhi=4"
9856        local cmd2="dir_count=2 layer=mdd stripe_count=0"
9857        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9858        local cmd="$cmd1 $cmd2 $cmd3"
9859
9860        rm -f ${TMP}/mds_survey*
9861        echo + $cmd
9862        eval $cmd || error "mds-survey with zero-stripe failed"
9863        cat ${TMP}/mds_survey*
9864        rm -f ${TMP}/mds_survey*
9865 }
9866 run_test 225a "Metadata survey sanity with zero-stripe"
9867
9868 test_225b () {
9869        if [ -z ${MDSSURVEY} ]; then
9870               skip_env "mds-survey not found" && return
9871        fi
9872        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9873             { skip "Need MDS version at least 2.2.51"; return; }
9874
9875        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
9876               skip_env "Need to mount OST to test" && return
9877        fi
9878
9879        local mds=$(facet_host $SINGLEMDS)
9880        local target=$(do_nodes $mds 'lctl dl' | \
9881                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9882
9883        local cmd1="file_count=1000 thrhi=4"
9884        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9885        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9886        local cmd="$cmd1 $cmd2 $cmd3"
9887
9888        rm -f ${TMP}/mds_survey*
9889        echo + $cmd
9890        eval $cmd || error "mds-survey with stripe_count failed"
9891        cat ${TMP}/mds_survey*
9892        rm -f ${TMP}/mds_survey*
9893 }
9894 run_test 225b "Metadata survey sanity with stripe_count = 1"
9895
9896 mcreate_path2fid () {
9897         local mode=$1
9898         local major=$2
9899         local minor=$3
9900         local name=$4
9901         local desc=$5
9902         local path=$DIR/$tdir/$name
9903         local fid
9904         local rc
9905         local fid_path
9906
9907         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
9908                 error "cannot create $desc"
9909
9910         fid=$($LFS path2fid $path)
9911         rc=$?
9912         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
9913
9914         fid_path=$($LFS fid2path $DIR "$fid")
9915         rc=$?
9916         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
9917
9918         [ "$path" == "$fid_path" ] ||
9919                 error "fid2path returned $fid_path, expected $path"
9920
9921         echo "pass with $path and $fid"
9922 }
9923
9924 test_226 () {
9925         rm -rf $DIR/$tdir
9926         mkdir -p $DIR/$tdir
9927
9928         mcreate_path2fid 0010666 0 0 fifo "FIFO"
9929         mcreate_path2fid 0020666 1 3 null "character special file (null)"
9930         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
9931         mcreate_path2fid 0040666 0 0 dir "directory"
9932         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
9933         mcreate_path2fid 0100666 0 0 file "regular file"
9934         mcreate_path2fid 0120666 0 0 link "symbolic link"
9935         mcreate_path2fid 0140666 0 0 sock "socket"
9936 }
9937 run_test 226 "call path2fid and fid2path on files of all type"
9938
9939 # LU-1299 Executing or running ldd on a truncated executable does not
9940 # cause an out-of-memory condition.
9941 test_227() {
9942         dd if=`which date` of=$MOUNT/date bs=1k count=1
9943         chmod +x $MOUNT/date
9944
9945         $MOUNT/date > /dev/null
9946         ldd $MOUNT/date > /dev/null
9947         rm -f $MOUNT/date
9948 }
9949 run_test 227 "running truncated executable does not cause OOM"
9950
9951 # LU-1512 try to reuse idle OI blocks
9952 test_228a() {
9953         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9954                 skip "non-ldiskfs backend" && return
9955
9956         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9957         local myDIR=$DIR/$tdir
9958
9959         mkdir -p $myDIR
9960         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9961         $LCTL set_param fail_loc=0x80001002
9962         createmany -o $myDIR/t- 10000
9963         $LCTL set_param fail_loc=0
9964         # The guard is current the largest FID holder
9965         touch $myDIR/guard
9966         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9967                     tr -d '[')
9968         local IDX=$(($SEQ % 64))
9969
9970         do_facet $SINGLEMDS sync
9971         # Make sure journal flushed.
9972         sleep 6
9973         local blk1=$(do_facet $SINGLEMDS \
9974                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9975                      grep Blockcount | awk '{print $4}')
9976
9977         # Remove old files, some OI blocks will become idle.
9978         unlinkmany $myDIR/t- 10000
9979         # Create new files, idle OI blocks should be reused.
9980         createmany -o $myDIR/t- 2000
9981         do_facet $SINGLEMDS sync
9982         # Make sure journal flushed.
9983         sleep 6
9984         local blk2=$(do_facet $SINGLEMDS \
9985                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9986                      grep Blockcount | awk '{print $4}')
9987
9988         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9989 }
9990 run_test 228a "try to reuse idle OI blocks"
9991
9992 test_228b() {
9993         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9994                 skip "non-ldiskfs backend" && return
9995
9996         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9997         local myDIR=$DIR/$tdir
9998
9999         mkdir -p $myDIR
10000         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10001         $LCTL set_param fail_loc=0x80001002
10002         createmany -o $myDIR/t- 10000
10003         $LCTL set_param fail_loc=0
10004         # The guard is current the largest FID holder
10005         touch $myDIR/guard
10006         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10007                     tr -d '[')
10008         local IDX=$(($SEQ % 64))
10009
10010         do_facet $SINGLEMDS sync
10011         # Make sure journal flushed.
10012         sleep 6
10013         local blk1=$(do_facet $SINGLEMDS \
10014                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10015                      grep Blockcount | awk '{print $4}')
10016
10017         # Remove old files, some OI blocks will become idle.
10018         unlinkmany $myDIR/t- 10000
10019
10020         # stop the MDT
10021         stop $SINGLEMDS || error "Fail to stop MDT."
10022         # remount the MDT
10023         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10024
10025         df $MOUNT || error "Fail to df."
10026         # Create new files, idle OI blocks should be reused.
10027         createmany -o $myDIR/t- 2000
10028         do_facet $SINGLEMDS sync
10029         # Make sure journal flushed.
10030         sleep 6
10031         local blk2=$(do_facet $SINGLEMDS \
10032                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10033                      grep Blockcount | awk '{print $4}')
10034
10035         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10036 }
10037 run_test 228b "idle OI blocks can be reused after MDT restart"
10038
10039 #LU-1881
10040 test_228c() {
10041         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10042                 skip "non-ldiskfs backend" && return
10043
10044         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10045         local myDIR=$DIR/$tdir
10046
10047         mkdir -p $myDIR
10048         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10049         $LCTL set_param fail_loc=0x80001002
10050         # 20000 files can guarantee there are index nodes in the OI file
10051         createmany -o $myDIR/t- 20000
10052         $LCTL set_param fail_loc=0
10053         # The guard is current the largest FID holder
10054         touch $myDIR/guard
10055         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10056                     tr -d '[')
10057         local IDX=$(($SEQ % 64))
10058
10059         do_facet $SINGLEMDS sync
10060         # Make sure journal flushed.
10061         sleep 6
10062         local blk1=$(do_facet $SINGLEMDS \
10063                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10064                      grep Blockcount | awk '{print $4}')
10065
10066         # Remove old files, some OI blocks will become idle.
10067         unlinkmany $myDIR/t- 20000
10068         rm -f $myDIR/guard
10069         # The OI file should become empty now
10070
10071         # Create new files, idle OI blocks should be reused.
10072         createmany -o $myDIR/t- 2000
10073         do_facet $SINGLEMDS sync
10074         # Make sure journal flushed.
10075         sleep 6
10076         local blk2=$(do_facet $SINGLEMDS \
10077                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10078                      grep Blockcount | awk '{print $4}')
10079
10080         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10081 }
10082 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10083
10084 test_230a() {
10085         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10086         local MDTIDX=1
10087
10088         mkdir -p $DIR/$tdir/test_230_local
10089         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10090         [ $mdt_idx -ne 0 ] &&
10091                 error "create local directory on wrong MDT $mdt_idx"
10092
10093         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10094                         error "create remote directory failed"
10095         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10096         [ $mdt_idx -ne $MDTIDX ] &&
10097                 error "create remote directory on wrong MDT $mdt_idx"
10098
10099         createmany -o $DIR/$tdir/test_230/t- 10 ||
10100                 error "create files on remote directory failed"
10101         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10102         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10103         rm -r $DIR/$tdir || error "unlink remote directory failed"
10104 }
10105 run_test 230a "Create remote directory and files under the remote directory"
10106
10107 test_230b() {
10108         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10109         local MDTIDX=1
10110         local remote_dir=$DIR/$tdir/remote_dir
10111
10112         mkdir -p $DIR/$tdir
10113         $LFS mkdir -i $MDTIDX $remote_dir ||
10114                 error "create remote directory failed"
10115
10116         $LFS mkdir -i 0 $remote_dir/new_dir &&
10117                 error "nested remote directory create succeed!"
10118
10119         rm -r $DIR/$tdir || error "unlink remote directory failed"
10120 }
10121 run_test 230b "nested remote directory should be failed"
10122
10123 #
10124 # tests that do cleanup/setup should be run at the end
10125 #
10126
10127 test_900() {
10128         local ls
10129         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
10130         $LCTL set_param fail_loc=0x903
10131         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
10132         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
10133                 echo "clear" > $ls
10134         done
10135         FAIL_ON_ERROR=true cleanup
10136         FAIL_ON_ERROR=true setup
10137 }
10138 run_test 900 "umount should not race with any mgc requeue thread"
10139
10140 complete $SECONDS
10141 check_and_cleanup_lustre
10142 if [ "$I_MOUNTED" != "yes" ]; then
10143         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
10144 fi
10145 exit_status