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