Whamcloud - gitweb
LU-2020 sanity: test 140 should allow 40 consecutive symlink
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
11 ALWAYS_EXCEPT="                27u   42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
12 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
13
14 # with LOD/OSP landing
15 # bug number for skipped tests: LU-2036
16 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
17
18
19 # Tests that fail on uml
20 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
21 #                                    buffer i/o errs             sock spc runas
22 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
23
24 SRCDIR=$(cd $(dirname $0); echo $PWD)
25 export PATH=$PATH:/sbin
26
27 TMP=${TMP:-/tmp}
28
29 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
30 CREATETEST=${CREATETEST:-createtest}
31 LFS=${LFS:-lfs}
32 LFIND=${LFIND:-"$LFS find"}
33 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
34 LCTL=${LCTL:-lctl}
35 MCREATE=${MCREATE:-mcreate}
36 OPENFILE=${OPENFILE:-openfile}
37 OPENUNLINK=${OPENUNLINK:-openunlink}
38 export MULTIOP=${MULTIOP:-multiop}
39 READS=${READS:-"reads"}
40 MUNLINK=${MUNLINK:-munlink}
41 SOCKETSERVER=${SOCKETSERVER:-socketserver}
42 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
43 MEMHOG=${MEMHOG:-memhog}
44 DIRECTIO=${DIRECTIO:-directio}
45 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
46 UMOUNT=${UMOUNT:-"umount -d"}
47 STRIPES_PER_OBJ=-1
48 CHECK_GRANT=${CHECK_GRANT:-"yes"}
49 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
50
51 export NAME=${NAME:-local}
52
53 SAVE_PWD=$PWD
54
55 CLEANUP=${CLEANUP:-:}
56 SETUP=${SETUP:-:}
57 TRACE=${TRACE:-""}
58 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
59 . $LUSTRE/tests/test-framework.sh
60 init_test_env $@
61 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
62 init_logging
63
64 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
65
66 FAIL_ON_ERROR=false
67
68 cleanup() {
69         echo -n "cln.."
70         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
71         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
72 }
73 setup() {
74         echo -n "mnt.."
75         load_modules
76         setupall || exit 10
77         echo "done"
78 }
79
80 check_kernel_version() {
81         WANT_VER=$1
82         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
83         case $GOT_VER in
84         patchless|patchless_client) return 0;;
85         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
86         esac
87         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
88         return 1
89 }
90
91 if [ "$ONLY" == "cleanup" ]; then
92        sh llmountcleanup.sh
93        exit 0
94 fi
95
96 check_and_setup_lustre
97
98 DIR=${DIR:-$MOUNT}
99 assert_DIR
100
101 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
102     awk '{gsub(/_UUID/,""); print $1}' | head -1)
103 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
104 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
105 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
106 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
107 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
108 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
109
110 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
111 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
112 rm -rf $DIR/[Rdfs][0-9]*
113
114 # $RUNAS_ID may get set incorrectly somewhere else
115 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
116
117 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
118
119 build_test_filter
120
121 if [ "${ONLY}" = "MOUNT" ] ; then
122         echo "Lustre is up, please go on"
123         exit
124 fi
125
126 echo "preparing for tests involving mounts"
127 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
128 touch $EXT2_DEV
129 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
130 echo # add a newline after mke2fs.
131
132 umask 077
133
134 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
135 lctl set_param debug=-1 2> /dev/null || true
136 test_0() {
137         touch $DIR/$tfile
138         $CHECKSTAT -t file $DIR/$tfile || error
139         rm $DIR/$tfile
140         $CHECKSTAT -a $DIR/$tfile || error
141 }
142 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
143
144 test_0b() {
145         chmod 0755 $DIR || error
146         $CHECKSTAT -p 0755 $DIR || error
147 }
148 run_test 0b "chmod 0755 $DIR ============================="
149
150 test_0c() {
151     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
152     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
153 }
154 run_test 0c "check import proc ============================="
155
156 test_1a() {
157         mkdir $DIR/d1
158         mkdir $DIR/d1/d2
159         mkdir $DIR/d1/d2 && error "we expect EEXIST, but not returned"
160         $CHECKSTAT -t dir $DIR/d1/d2 || error
161 }
162 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
163
164 test_1b() {
165         rmdir $DIR/d1/d2
166         rmdir $DIR/d1
167         $CHECKSTAT -a $DIR/d1 || error
168 }
169 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
170
171 test_2a() {
172         mkdir $DIR/d2
173         touch $DIR/d2/f
174         $CHECKSTAT -t file $DIR/d2/f || error
175 }
176 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
177
178 test_2b() {
179         rm -r $DIR/d2
180         $CHECKSTAT -a $DIR/d2 || error
181 }
182 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
183
184 test_3a() {
185         mkdir $DIR/d3
186         $CHECKSTAT -t dir $DIR/d3 || error
187 }
188 run_test 3a "mkdir .../d3 ======================================"
189
190 test_3b() {
191         if [ ! -d $DIR/d3 ]; then
192                 mkdir $DIR/d3
193         fi
194         touch $DIR/d3/f
195         $CHECKSTAT -t file $DIR/d3/f || error
196 }
197 run_test 3b "touch .../d3/f ===================================="
198
199 test_3c() {
200         rm -r $DIR/d3
201         $CHECKSTAT -a $DIR/d3 || error
202 }
203 run_test 3c "rm -r .../d3 ======================================"
204
205 test_4a() {
206         mkdir $DIR/d4
207         $CHECKSTAT -t dir $DIR/d4 || error
208 }
209 run_test 4a "mkdir .../d4 ======================================"
210
211 test_4b() {
212         if [ ! -d $DIR/d4 ]; then
213                 mkdir $DIR/d4
214         fi
215         mkdir $DIR/d4/d2
216         $CHECKSTAT -t dir $DIR/d4/d2 || error
217 }
218 run_test 4b "mkdir .../d4/d2 ==================================="
219
220 test_5() {
221         mkdir $DIR/d5
222         mkdir $DIR/d5/d2
223         chmod 0707 $DIR/d5/d2
224         $CHECKSTAT -t dir -p 0707 $DIR/d5/d2 || error
225 }
226 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
227
228 test_6a() {
229         touch $DIR/f6a
230         chmod 0666 $DIR/f6a || error
231         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
232 }
233 run_test 6a "touch .../f6a; chmod .../f6a ======================"
234
235 test_6b() {
236         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
237         if [ ! -f $DIR/f6a ]; then
238                 touch $DIR/f6a
239                 chmod 0666 $DIR/f6a
240         fi
241         $RUNAS chmod 0444 $DIR/f6a && error
242         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
243 }
244 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
245
246 test_6c() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
248         touch $DIR/f6c
249         chown $RUNAS_ID $DIR/f6c || error
250         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
251 }
252 run_test 6c "touch .../f6c; chown .../f6c ======================"
253
254 test_6d() {
255         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
256         if [ ! -f $DIR/f6c ]; then
257                 touch $DIR/f6c
258                 chown $RUNAS_ID $DIR/f6c
259         fi
260         $RUNAS chown $UID $DIR/f6c && error
261         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
262 }
263 run_test 6d "$RUNAS chown .../f6c (should return error) =="
264
265 test_6e() {
266         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
267         touch $DIR/f6e
268         chgrp $RUNAS_ID $DIR/f6e || error
269         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
270 }
271 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
272
273 test_6f() {
274         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
275         if [ ! -f $DIR/f6e ]; then
276                 touch $DIR/f6e
277                 chgrp $RUNAS_ID $DIR/f6e
278         fi
279         $RUNAS chgrp $UID $DIR/f6e && error
280         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
281 }
282 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
283
284 test_6g() {
285         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
286         mkdir $DIR/d6g || error
287         chmod 777 $DIR/d6g || error
288         $RUNAS mkdir $DIR/d6g/d || error
289         chmod g+s $DIR/d6g/d || error
290         mkdir $DIR/d6g/d/subdir
291         $CHECKSTAT -g \#$RUNAS_GID $DIR/d6g/d/subdir || error
292 }
293 run_test 6g "Is new dir in sgid dir inheriting group?"
294
295 test_6h() { # bug 7331
296         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
297         touch $DIR/f6h || error "touch failed"
298         chown $RUNAS_ID:$RUNAS_GID $DIR/f6h || error "initial chown failed"
299         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/f6h && error "chown worked"
300         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/f6h || error
301 }
302 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
303
304 test_7a() {
305         mkdir $DIR/d7
306         $MCREATE $DIR/d7/f
307         chmod 0666 $DIR/d7/f
308         $CHECKSTAT -t file -p 0666 $DIR/d7/f || error
309 }
310 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
311
312 test_7b() {
313         if [ ! -d $DIR/d7 ]; then
314                 mkdir $DIR/d7
315         fi
316         $MCREATE $DIR/d7/f2
317         echo -n foo > $DIR/d7/f2
318         [ "`cat $DIR/d7/f2`" = "foo" ] || error
319         $CHECKSTAT -t file -s 3 $DIR/d7/f2 || error
320 }
321 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
322
323 test_8() {
324         mkdir $DIR/d8
325         touch $DIR/d8/f
326         chmod 0666 $DIR/d8/f
327         $CHECKSTAT -t file -p 0666 $DIR/d8/f || error
328 }
329 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
330
331 test_9() {
332         mkdir $DIR/d9
333         mkdir $DIR/d9/d2
334         mkdir $DIR/d9/d2/d3
335         $CHECKSTAT -t dir $DIR/d9/d2/d3 || error
336 }
337 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
338
339 test_10() {
340         mkdir $DIR/d10
341         mkdir $DIR/d10/d2
342         touch $DIR/d10/d2/f
343         $CHECKSTAT -t file $DIR/d10/d2/f || error
344 }
345 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
346
347 test_11() {
348         mkdir $DIR/d11
349         mkdir $DIR/d11/d2
350         chmod 0666 $DIR/d11/d2
351         chmod 0705 $DIR/d11/d2
352         $CHECKSTAT -t dir -p 0705 $DIR/d11/d2 || error
353 }
354 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
355
356 test_12() {
357         mkdir $DIR/d12
358         touch $DIR/d12/f
359         chmod 0666 $DIR/d12/f
360         chmod 0654 $DIR/d12/f
361         $CHECKSTAT -t file -p 0654 $DIR/d12/f || error
362 }
363 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
364
365 test_13() {
366         mkdir $DIR/d13
367         dd if=/dev/zero of=$DIR/d13/f count=10
368         >  $DIR/d13/f
369         $CHECKSTAT -t file -s 0 $DIR/d13/f || error
370 }
371 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
372
373 test_14() {
374         mkdir $DIR/d14
375         touch $DIR/d14/f
376         rm $DIR/d14/f
377         $CHECKSTAT -a $DIR/d14/f || error
378 }
379 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
380
381 test_15() {
382         mkdir $DIR/d15
383         touch $DIR/d15/f
384         mv $DIR/d15/f $DIR/d15/f2
385         $CHECKSTAT -t file $DIR/d15/f2 || error
386 }
387 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
388
389 test_16() {
390         mkdir $DIR/d16
391         touch $DIR/d16/f
392         rm -rf $DIR/d16/f
393         $CHECKSTAT -a $DIR/d16/f || error
394 }
395 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
396
397 test_17a() {
398         mkdir -p $DIR/d17
399         touch $DIR/d17/f
400         ln -s $DIR/d17/f $DIR/d17/l-exist
401         ls -l $DIR/d17
402         $CHECKSTAT -l $DIR/d17/f $DIR/d17/l-exist || error
403         $CHECKSTAT -f -t f $DIR/d17/l-exist || error
404         rm -f $DIR/d17/l-exist
405         $CHECKSTAT -a $DIR/d17/l-exist || error
406 }
407 run_test 17a "symlinks: create, remove (real) =================="
408
409 test_17b() {
410         mkdir -p $DIR/d17
411         ln -s no-such-file $DIR/d17/l-dangle
412         ls -l $DIR/d17
413         $CHECKSTAT -l no-such-file $DIR/d17/l-dangle || error
414         $CHECKSTAT -fa $DIR/d17/l-dangle || error
415         rm -f $DIR/d17/l-dangle
416         $CHECKSTAT -a $DIR/d17/l-dangle || error
417 }
418 run_test 17b "symlinks: create, remove (dangling) =============="
419
420 test_17c() { # bug 3440 - don't save failed open RPC for replay
421         mkdir -p $DIR/d17
422         ln -s foo $DIR/d17/f17c
423         cat $DIR/d17/f17c && error "opened non-existent symlink" || true
424 }
425 run_test 17c "symlinks: open dangling (should return error) ===="
426
427 test_17d() {
428         mkdir -p $DIR/d17
429         ln -s foo $DIR/d17/f17d
430         touch $DIR/d17/f17d || error "creating to new symlink"
431 }
432 run_test 17d "symlinks: create dangling ========================"
433
434 test_17e() {
435         mkdir -p $DIR/$tdir
436         local foo=$DIR/$tdir/$tfile
437         ln -s $foo $foo || error "create symlink failed"
438         ls -l $foo || error "ls -l failed"
439         ls $foo && error "ls not failed" || true
440 }
441 run_test 17e "symlinks: create recursive symlink (should return error) ===="
442
443 test_17f() {
444         mkdir -p $DIR/d17f
445         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
446         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
447         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
448         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
450         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
451         ls -l  $DIR/d17f
452 }
453 run_test 17f "symlinks: long and very long symlink name ========================"
454
455 # str_repeat(S, N) generate a string that is string S repeated N times
456 str_repeat() {
457         local s=$1
458         local n=$2
459         local ret=''
460         while [ $((n -= 1)) -ge 0 ]; do
461                 ret=$ret$s
462         done
463         echo $ret
464 }
465
466 # Long symlinks and LU-2241
467 test_17g() {
468         mkdir -p $DIR/$tdir
469         local TESTS="59 60 61 4094 4095"
470
471         for i in $TESTS; do
472                 local SYMNAME=$(str_repeat 'x' $i)
473                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
474                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
475         done
476 }
477 run_test 17g "symlinks: really long symlink name and inode boundaries"
478
479 test_17h() { #bug 17378
480         remote_mds_nodsh && skip "remote MDS with nodsh" && return
481         mkdir -p $DIR/$tdir
482         $SETSTRIPE -c -1 $DIR/$tdir
483 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
484         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000141
485         touch $DIR/$tdir/$tfile || true
486 }
487 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
488
489 test_17i() { #bug 20018
490         remote_mds_nodsh && skip "remote MDS with nodsh" && return
491         mkdir -p $DIR/$tdir
492         local foo=$DIR/$tdir/$tfile
493         ln -s $foo $foo || error "create symlink failed"
494 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
495         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000143
496         ls -l $foo && error "error not detected"
497         return 0
498 }
499 run_test 17i "don't panic on short symlink"
500
501 test_17k() { #bug 22301
502         rsync --help | grep -q xattr ||
503                 skip_env "$(rsync --version| head -1) does not support xattrs"
504         mkdir -p $DIR/{$tdir,$tdir.new}
505         touch $DIR/$tdir/$tfile
506         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
507         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
508                 error "rsync failed with xattrs enabled"
509 }
510 run_test 17k "symlinks: rsync with xattrs enabled ========================="
511
512 # LU-1540
513 test_17m() {
514         local short_sym="0123456789"
515         local WDIR=$DIR/${tdir}m
516         local mds_index
517         local devname
518         local cmd
519         local i
520         local rc=0
521
522         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
523         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
524                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
525
526         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
527                 skip "only for ldiskfs MDT" && return 0
528
529         mkdir -p $WDIR
530         long_sym=$short_sym
531         # create a long symlink file
532         for ((i = 0; i < 4; ++i)); do
533                 long_sym=${long_sym}${long_sym}
534         done
535
536         echo "create 512 short and long symlink files under $WDIR"
537         for ((i = 0; i < 256; ++i)); do
538                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
539                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
540         done
541
542         echo "erase them"
543         rm -f $WDIR/*
544         sync
545         sleep 2
546
547         echo "recreate the 512 symlink files with a shorter string"
548         for ((i = 0; i < 512; ++i)); do
549                 # rewrite the symlink file with a shorter string
550                 ln -sf ${long_sym} $WDIR/long-$i
551                 ln -sf ${short_sym} $WDIR/short-$i
552         done
553
554         mds_index=$($LFS getstripe -M $WDIR)
555         mds_index=$((mds_index+1))
556         devname=$(mdsdevname $mds_index)
557         cmd="$E2FSCK -fnvd $devname"
558
559         echo "stop and checking mds${mds_index}: $cmd"
560         # e2fsck should not return error
561         stop mds${mds_index} -f
562         do_facet mds${mds_index} $cmd || rc=$?
563
564         start mds${mds_index} $devname $MDS_MOUNT_OPTS
565         df $MOUNT > /dev/null 2>&1
566         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
567                 "short/long symlink MDT: rc=$rc"
568         return $rc
569 }
570 run_test 17m "run e2fsck against MDT which contains short/long symlink"
571
572 test_18() {
573         touch $DIR/f
574         ls $DIR || error
575 }
576 run_test 18 "touch .../f ; ls ... =============================="
577
578 test_19a() {
579         touch $DIR/f19
580         ls -l $DIR
581         rm $DIR/f19
582         $CHECKSTAT -a $DIR/f19 || error
583 }
584 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
585
586 test_19b() {
587         ls -l $DIR/f19 && error || true
588 }
589 run_test 19b "ls -l .../f19 (should return error) =============="
590
591 test_19c() {
592         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
593         $RUNAS touch $DIR/f19 && error || true
594 }
595 run_test 19c "$RUNAS touch .../f19 (should return error) =="
596
597 test_19d() {
598         cat $DIR/f19 && error || true
599 }
600 run_test 19d "cat .../f19 (should return error) =============="
601
602 test_20() {
603         touch $DIR/f
604         rm $DIR/f
605         log "1 done"
606         touch $DIR/f
607         rm $DIR/f
608         log "2 done"
609         touch $DIR/f
610         rm $DIR/f
611         log "3 done"
612         $CHECKSTAT -a $DIR/f || error
613 }
614 run_test 20 "touch .../f ; ls -l ... ==========================="
615
616 test_21() {
617         mkdir $DIR/d21
618         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
619         ln -s dangle $DIR/d21/link
620         echo foo >> $DIR/d21/link
621         cat $DIR/d21/dangle
622         $CHECKSTAT -t link $DIR/d21/link || error
623         $CHECKSTAT -f -t file $DIR/d21/link || error
624 }
625 run_test 21 "write to dangling link ============================"
626
627 test_22() {
628         WDIR=$DIR/$tdir
629         mkdir -p $WDIR
630         chown $RUNAS_ID:$RUNAS_GID $WDIR
631         (cd $WDIR || error "cd $WDIR failed";
632         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
633         $RUNAS tar xf -)
634         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
635         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
636         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
637 }
638 run_test 22 "unpack tar archive as non-root user ==============="
639
640 # was test_23
641 test_23a() {
642         mkdir -p $DIR/$tdir
643         local file=$DIR/$tdir/$tfile
644
645         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
646         openfile -f O_CREAT:O_EXCL $file &&
647                 error "$file recreate succeeded" || true
648 }
649 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
650
651 test_23b() { # bug 18988
652         mkdir -p $DIR/$tdir
653         local file=$DIR/$tdir/$tfile
654
655         rm -f $file
656         echo foo > $file || error "write filed"
657         echo bar >> $file || error "append filed"
658         $CHECKSTAT -s 8 $file || error "wrong size"
659         rm $file
660 }
661 run_test 23b "O_APPEND check =========================="
662
663 test_24a() {
664         echo '== rename sanity =============================================='
665         echo '-- same directory rename'
666         mkdir $DIR/R1
667         touch $DIR/R1/f
668         mv $DIR/R1/f $DIR/R1/g
669         $CHECKSTAT -t file $DIR/R1/g || error
670 }
671 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
672
673 test_24b() {
674         mkdir $DIR/R2
675         touch $DIR/R2/{f,g}
676         mv $DIR/R2/f $DIR/R2/g
677         $CHECKSTAT -a $DIR/R2/f || error
678         $CHECKSTAT -t file $DIR/R2/g || error
679 }
680 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
681
682 test_24c() {
683         mkdir $DIR/R3
684         mkdir $DIR/R3/f
685         mv $DIR/R3/f $DIR/R3/g
686         $CHECKSTAT -a $DIR/R3/f || error
687         $CHECKSTAT -t dir $DIR/R3/g || error
688 }
689 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
690
691 test_24d() {
692         mkdir $DIR/R4
693         mkdir $DIR/R4/{f,g}
694         mrename $DIR/R4/f $DIR/R4/g
695         $CHECKSTAT -a $DIR/R4/f || error
696         $CHECKSTAT -t dir $DIR/R4/g || error
697 }
698 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
699
700 test_24e() {
701         echo '-- cross directory renames --'
702         mkdir $DIR/R5{a,b}
703         touch $DIR/R5a/f
704         mv $DIR/R5a/f $DIR/R5b/g
705         $CHECKSTAT -a $DIR/R5a/f || error
706         $CHECKSTAT -t file $DIR/R5b/g || error
707 }
708 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
709
710 test_24f() {
711         mkdir $DIR/R6{a,b}
712         touch $DIR/R6a/f $DIR/R6b/g
713         mv $DIR/R6a/f $DIR/R6b/g
714         $CHECKSTAT -a $DIR/R6a/f || error
715         $CHECKSTAT -t file $DIR/R6b/g || error
716 }
717 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
718
719 test_24g() {
720         mkdir $DIR/R7{a,b}
721         mkdir $DIR/R7a/d
722         mv $DIR/R7a/d $DIR/R7b/e
723         $CHECKSTAT -a $DIR/R7a/d || error
724         $CHECKSTAT -t dir $DIR/R7b/e || error
725 }
726 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
727
728 test_24h() {
729         mkdir $DIR/R8{a,b}
730         mkdir $DIR/R8a/d $DIR/R8b/e
731         mrename $DIR/R8a/d $DIR/R8b/e
732         $CHECKSTAT -a $DIR/R8a/d || error
733         $CHECKSTAT -t dir $DIR/R8b/e || error
734 }
735 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
736
737 test_24i() {
738         echo "-- rename error cases"
739         mkdir $DIR/R9
740         mkdir $DIR/R9/a
741         touch $DIR/R9/f
742         mrename $DIR/R9/f $DIR/R9/a
743         $CHECKSTAT -t file $DIR/R9/f || error
744         $CHECKSTAT -t dir  $DIR/R9/a || error
745         $CHECKSTAT -a $DIR/R9/a/f || error
746 }
747 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
748
749 test_24j() {
750         mkdir $DIR/R10
751         mrename $DIR/R10/f $DIR/R10/g
752         $CHECKSTAT -t dir $DIR/R10 || error
753         $CHECKSTAT -a $DIR/R10/f || error
754         $CHECKSTAT -a $DIR/R10/g || error
755 }
756 run_test 24j "source does not exist ============================"
757
758 test_24k() {
759         mkdir $DIR/R11a $DIR/R11a/d
760         touch $DIR/R11a/f
761         mv $DIR/R11a/f $DIR/R11a/d
762         $CHECKSTAT -a $DIR/R11a/f || error
763         $CHECKSTAT -t file $DIR/R11a/d/f || error
764 }
765 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
766
767 # bug 2429 - rename foo foo foo creates invalid file
768 test_24l() {
769         f="$DIR/f24l"
770         $MULTIOP $f OcNs || error
771 }
772 run_test 24l "Renaming a file to itself ========================"
773
774 test_24m() {
775         f="$DIR/f24m"
776         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
777         # on ext3 this does not remove either the source or target files
778         # though the "expected" operation would be to remove the source
779         $CHECKSTAT -t file ${f} || error "${f} missing"
780         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
781 }
782 run_test 24m "Renaming a file to a hard link to itself ========="
783
784 test_24n() {
785     f="$DIR/f24n"
786     # this stats the old file after it was renamed, so it should fail
787     touch ${f}
788     $CHECKSTAT ${f}
789     mv ${f} ${f}.rename
790     $CHECKSTAT ${f}.rename
791     $CHECKSTAT -a ${f}
792 }
793 run_test 24n "Statting the old file after renaming (Posix rename 2)"
794
795 test_24o() {
796         check_kernel_version 37 || return 0
797         mkdir -p $DIR/d24o
798         rename_many -s random -v -n 10 $DIR/d24o
799 }
800 run_test 24o "rename of files during htree split ==============="
801
802 test_24p() {
803         mkdir $DIR/R12{a,b}
804         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
805         mrename $DIR/R12a $DIR/R12b
806         $CHECKSTAT -a $DIR/R12a || error
807         $CHECKSTAT -t dir $DIR/R12b || error
808         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
809         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
810 }
811 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
812
813 test_24q() {
814         mkdir $DIR/R13{a,b}
815         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
816         multiop_bg_pause $DIR/R13b D_c || return 1
817         MULTIPID=$!
818
819         mrename $DIR/R13a $DIR/R13b
820         $CHECKSTAT -a $DIR/R13a || error
821         $CHECKSTAT -t dir $DIR/R13b || error
822         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
823         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
824         kill -USR1 $MULTIPID
825         wait $MULTIPID || error "multiop close failed"
826 }
827 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
828
829 test_24r() { #bug 3789
830         mkdir $DIR/R14a $DIR/R14a/b
831         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
832         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
833         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
834 }
835 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
836
837 test_24s() {
838         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
839         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
840         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
841         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
842 }
843 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
844 test_24t() {
845         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
846         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
847         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
848         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
849 }
850 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
851
852 test_24u() { # bug12192
853         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
854         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
855 }
856 run_test 24u "create stripe file"
857
858 page_size() {
859         getconf PAGE_SIZE
860 }
861
862 test_24v() {
863         local NRFILES=100000
864         local FREE_INODES=`lfs df -i|grep "filesystem summary" | awk '{print $5}'`
865         [ $FREE_INODES -lt $NRFILES ] && \
866                 skip "not enough free inodes $FREE_INODES required $NRFILES" && \
867                 return
868
869         mkdir -p $DIR/d24v
870         createmany -m $DIR/d24v/$tfile $NRFILES
871
872         cancel_lru_locks mdc
873         lctl set_param mdc.*.stats clear
874
875         ls $DIR/d24v >/dev/null || error "error in listing large dir"
876
877         # LU-5 large readdir
878         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
879         #               8 bytes for name(filename is mostly 5 in this test) +
880         #               8 bytes for luda_type
881         # take into account of overhead in lu_dirpage header and end mark in
882         # each page, plus one in RPC_NUM calculation.
883         DIRENT_SIZE=48
884         RPC_SIZE=$(($(lctl get_param -n mdc.*.max_pages_per_rpc)*$(page_size)))
885         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
886         mds_readpage=`lctl get_param mdc.*.stats | \
887                                 awk '/^mds_readpage/ {print $2}'`
888         [ $mds_readpage -gt $RPC_NUM ] && \
889                 error "large readdir doesn't take effect"
890
891         rm $DIR/d24v -rf
892 }
893 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
894
895 test_24w() { # bug21506
896         SZ1=234852
897         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
898         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
899         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
900         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
901         [ "$SZ1" = "$SZ2" ] || \
902                 error "Error reading at the end of the file $tfile"
903 }
904 run_test 24w "Reading a file larger than 4Gb"
905
906 test_25a() {
907         echo '== symlink sanity ============================================='
908
909         mkdir $DIR/d25
910         ln -s d25 $DIR/s25
911         touch $DIR/s25/foo || error
912 }
913 run_test 25a "create file in symlinked directory ==============="
914
915 test_25b() {
916         [ ! -d $DIR/d25 ] && test_25a
917         $CHECKSTAT -t file $DIR/s25/foo || error
918 }
919 run_test 25b "lookup file in symlinked directory ==============="
920
921 test_26a() {
922         mkdir $DIR/d26
923         mkdir $DIR/d26/d26-2
924         ln -s d26/d26-2 $DIR/s26
925         touch $DIR/s26/foo || error
926 }
927 run_test 26a "multiple component symlink ======================="
928
929 test_26b() {
930         mkdir -p $DIR/d26b/d26-2
931         ln -s d26b/d26-2/foo $DIR/s26-2
932         touch $DIR/s26-2 || error
933 }
934 run_test 26b "multiple component symlink at end of lookup ======"
935
936 test_26c() {
937         mkdir $DIR/d26.2
938         touch $DIR/d26.2/foo
939         ln -s d26.2 $DIR/s26.2-1
940         ln -s s26.2-1 $DIR/s26.2-2
941         ln -s s26.2-2 $DIR/s26.2-3
942         chmod 0666 $DIR/s26.2-3/foo
943 }
944 run_test 26c "chain of symlinks ================================"
945
946 # recursive symlinks (bug 439)
947 test_26d() {
948         ln -s d26-3/foo $DIR/d26-3
949 }
950 run_test 26d "create multiple component recursive symlink ======"
951
952 test_26e() {
953         [ ! -h $DIR/d26-3 ] && test_26d
954         rm $DIR/d26-3
955 }
956 run_test 26e "unlink multiple component recursive symlink ======"
957
958 # recursive symlinks (bug 7022)
959 test_26f() {
960         mkdir -p $DIR/$tdir
961         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
962         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
963         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
964         mkdir $tfile             || error "mkdir $tfile failed"
965         cd $tfile                || error "cd $tfile failed"
966         ln -s .. dotdot          || error "ln dotdot failed"
967         ln -s dotdot/lndir lndir || error "ln lndir failed"
968         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
969         output=`ls $tfile/$tfile/lndir/bar1`
970         [ "$output" = bar1 ] && error "unexpected output"
971         rm -r $tfile             || error "rm $tfile failed"
972         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
973 }
974 run_test 26f "rm -r of a directory which has recursive symlink ="
975
976 test_27a() {
977         echo '== stripe sanity =============================================='
978         mkdir -p $DIR/d27 || error "mkdir failed"
979         $GETSTRIPE $DIR/d27
980         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
981         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
982         pass
983         log "== test_27a: write to one stripe file ========================="
984         cp /etc/hosts $DIR/d27/f0 || error
985 }
986 run_test 27a "one stripe file =================================="
987
988 test_27b() {
989         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
990         mkdir -p $DIR/d27
991         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
992         $GETSTRIPE -c $DIR/d27/f01
993         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
994                 error "two-stripe file doesn't have two stripes"
995 }
996 run_test 27b "create two stripe file"
997
998 test_27c() {
999         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1000
1001         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1002 }
1003 run_test 27c "write to two stripe file"
1004
1005 test_27d() {
1006         mkdir -p $DIR/d27
1007         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1008         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1009         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1010 }
1011 run_test 27d "create file with default settings ================"
1012
1013 test_27e() {
1014         mkdir -p $DIR/d27
1015         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1016         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1017         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1018 }
1019 run_test 27e "setstripe existing file (should return error) ======"
1020
1021 test_27f() {
1022         mkdir -p $DIR/d27
1023         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1024         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1025         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1026 }
1027 run_test 27f "setstripe with bad stripe size (should return error)"
1028
1029 test_27g() {
1030         mkdir -p $DIR/d27
1031         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1032         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1033                 error "$DIR/d27/fnone has object"
1034 }
1035 run_test 27g "$GETSTRIPE with no objects"
1036
1037 test_27i() {
1038         touch $DIR/d27/fsome || error "touch failed"
1039         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1040 }
1041 run_test 27i "$GETSTRIPE with some objects"
1042
1043 test_27j() {
1044         mkdir -p $DIR/d27
1045         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1046 }
1047 run_test 27j "setstripe with bad stripe offset (should return error)"
1048
1049 test_27k() { # bug 2844
1050         mkdir -p $DIR/d27
1051         FILE=$DIR/d27/f27k
1052         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1053         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
1054         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1055         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1056         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1057         dd if=/dev/zero of=$FILE bs=4k count=1
1058         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1059         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1060 }
1061 run_test 27k "limit i_blksize for broken user apps ============="
1062
1063 test_27l() {
1064         mkdir -p $DIR/d27
1065         mcreate $DIR/f27l || error "creating file"
1066         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1067                 error "setstripe should have failed" || true
1068 }
1069 run_test 27l "check setstripe permissions (should return error)"
1070
1071 test_27m() {
1072         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1073         if [ $ORIGFREE -gt $MAXFREE ]; then
1074                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1075                 return
1076         fi
1077         mkdir -p $DIR/d27
1078         $SETSTRIPE -i 0 -c 1 $DIR/d27/f27m_1
1079         dd if=/dev/zero of=$DIR/d27/f27m_1 bs=1024 count=$MAXFREE &&
1080                 error "dd should fill OST0"
1081         i=2
1082         while $SETSTRIPE -i 0 -c 1 $DIR/d27/f27m_$i; do
1083                 i=`expr $i + 1`
1084                 [ $i -gt 256 ] && break
1085         done
1086         i=`expr $i + 1`
1087         touch $DIR/d27/f27m_$i
1088         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1089                 error "OST0 was full but new created file still use it"
1090         i=`expr $i + 1`
1091         touch $DIR/d27/f27m_$i
1092         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1093                 error "OST0 was full but new created file still use it"
1094         rm -r $DIR/d27
1095         sleep 15
1096 }
1097 run_test 27m "create file while OST0 was full =================="
1098
1099 sleep_maxage() {
1100         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1101         sleep $DELAY
1102 }
1103
1104 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1105 # if the OST isn't full anymore.
1106 reset_enospc() {
1107         local OSTIDX=${1:-""}
1108
1109         local list=$(comma_list $(osts_nodes))
1110         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1111
1112         do_nodes $list lctl set_param fail_loc=0
1113         sync    # initiate all OST_DESTROYs from MDS to OST
1114         sleep_maxage
1115 }
1116
1117 exhaust_precreations() {
1118         local OSTIDX=$1
1119         local FAILLOC=$2
1120         local FAILIDX=${3:-$OSTIDX}
1121
1122         mkdir -p $DIR/$tdir
1123         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1124         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1125
1126         local OST=$(ostname_from_index $OSTIDX)
1127         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1128                           sed -e 's/_UUID$//;s/^.*-//')
1129
1130         # on the mdt's osc
1131         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1132         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1133         osc.$mdtosc_proc1.prealloc_last_id)
1134         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1135         osc.$mdtosc_proc1.prealloc_next_id)
1136
1137         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1138         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1139
1140         mkdir -p $DIR/$tdir/${OST}
1141         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1142 #define OBD_FAIL_OST_ENOSPC              0x215
1143         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1144         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1145         echo "Creating to objid $last_id on ost $OST..."
1146         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1147         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1148         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1149         sleep_maxage
1150 }
1151
1152 exhaust_all_precreations() {
1153         local i
1154         for (( i=0; i < OSTCOUNT; i++ )) ; do
1155                 exhaust_precreations $i $1 -1
1156         done
1157 }
1158
1159 test_27n() {
1160         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1161         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1162         remote_ost_nodsh && skip "remote OST with nodsh" && return
1163
1164         reset_enospc
1165         rm -f $DIR/$tdir/$tfile
1166         exhaust_precreations 0 0x80000215
1167         $SETSTRIPE -c -1 $DIR/$tdir
1168         touch $DIR/$tdir/$tfile || error
1169         $GETSTRIPE $DIR/$tdir/$tfile
1170         reset_enospc
1171 }
1172 run_test 27n "create file with some full OSTs =================="
1173
1174 test_27o() {
1175         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1176         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1177         remote_ost_nodsh && skip "remote OST with nodsh" && return
1178
1179         reset_enospc
1180         rm -f $DIR/$tdir/$tfile
1181         exhaust_all_precreations 0x215
1182
1183         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1184
1185         reset_enospc
1186         rm -rf $DIR/$tdir/*
1187 }
1188 run_test 27o "create file with all full OSTs (should error) ===="
1189
1190 test_27p() {
1191         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1192         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1193         remote_ost_nodsh && skip "remote OST with nodsh" && return
1194
1195         reset_enospc
1196         rm -f $DIR/$tdir/$tfile
1197         mkdir -p $DIR/$tdir
1198
1199         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1200         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1201         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1202
1203         exhaust_precreations 0 0x80000215
1204         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1205         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1206         $GETSTRIPE $DIR/$tdir/$tfile
1207
1208         reset_enospc
1209 }
1210 run_test 27p "append to a truncated file with some full OSTs ==="
1211
1212 test_27q() {
1213         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1214         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1215         remote_ost_nodsh && skip "remote OST with nodsh" && return
1216
1217         reset_enospc
1218         rm -f $DIR/$tdir/$tfile
1219
1220         mkdir -p $DIR/$tdir
1221         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1222         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1223         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1224
1225         exhaust_all_precreations 0x215
1226
1227         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1228         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1229
1230         reset_enospc
1231 }
1232 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1233
1234 test_27r() {
1235         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1236         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1237         remote_ost_nodsh && skip "remote OST with nodsh" && return
1238
1239         reset_enospc
1240         rm -f $DIR/$tdir/$tfile
1241         exhaust_precreations 0 0x80000215
1242
1243         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1244
1245         reset_enospc
1246 }
1247 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1248
1249 test_27s() { # bug 10725
1250         mkdir -p $DIR/$tdir
1251         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1252         local stripe_count=0
1253         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1254         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1255                 error "stripe width >= 2^32 succeeded" || true
1256
1257 }
1258 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1259
1260 test_27t() { # bug 10864
1261         WDIR=`pwd`
1262         WLFS=`which lfs`
1263         cd $DIR
1264         touch $tfile
1265         $WLFS getstripe $tfile
1266         cd $WDIR
1267 }
1268 run_test 27t "check that utils parse path correctly"
1269
1270 test_27u() { # bug 4900
1271         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1272         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1273
1274 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1275         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1276         mkdir -p $DIR/$tdir
1277         createmany -o $DIR/$tdir/t- 1000
1278         do_facet $SINGLEMDS lctl set_param fail_loc=0
1279
1280         TLOG=$DIR/$tfile.getstripe
1281         $GETSTRIPE $DIR/$tdir > $TLOG
1282         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1283         unlinkmany $DIR/$tdir/t- 1000
1284         [ $OBJS -gt 0 ] && \
1285                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1286 }
1287 run_test 27u "skip object creation on OSC w/o objects =========="
1288
1289 test_27v() { # bug 4900
1290         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1291         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1292         remote_ost_nodsh && skip "remote OST with nodsh" && return
1293
1294         exhaust_all_precreations 0x215
1295         reset_enospc
1296
1297         mkdir -p $DIR/$tdir
1298         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1299
1300         touch $DIR/$tdir/$tfile
1301         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1302         # all except ost1
1303         for (( i=1; i < OSTCOUNT; i++ )); do
1304                 do_facet ost$i lctl set_param fail_loc=0x705
1305         done
1306         local START=`date +%s`
1307         createmany -o $DIR/$tdir/$tfile 32
1308
1309         local FINISH=`date +%s`
1310         local TIMEOUT=`lctl get_param -n timeout`
1311         local PROCESS=$((FINISH - START))
1312         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1313                error "$FINISH - $START >= $TIMEOUT / 2"
1314         sleep $((TIMEOUT / 2 - PROCESS))
1315         reset_enospc
1316 }
1317 run_test 27v "skip object creation on slow OST ================="
1318
1319 test_27w() { # bug 10997
1320         mkdir -p $DIR/$tdir || error "mkdir failed"
1321         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1322         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1323                 error "stripe size $size != 65536" || true
1324         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1325                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1326 }
1327 run_test 27w "check $SETSTRIPE -S option"
1328
1329 test_27wa() {
1330         [ "$OSTCOUNT" -lt "2" ] &&
1331                 skip_env "skipping multiple stripe count/offset test" && return
1332
1333         mkdir -p $DIR/$tdir || error "mkdir failed"
1334         for i in $(seq 1 $OSTCOUNT); do
1335                 offset=$((i - 1))
1336                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1337                         error "setstripe -c $i -i $offset failed"
1338                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1339                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1340                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1341                 [ $index -ne $offset ] &&
1342                         error "stripe offset $index != $offset" || true
1343         done
1344 }
1345 run_test 27wa "check $SETSTRIPE -c -i options"
1346
1347 test_27x() {
1348         remote_ost_nodsh && skip "remote OST with nodsh" && return
1349         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1350         OFFSET=$(($OSTCOUNT - 1))
1351         OSTIDX=0
1352         local OST=$(ostname_from_index $OSTIDX)
1353
1354         mkdir -p $DIR/$tdir
1355         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1356         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1357         sleep_maxage
1358         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1359         for i in `seq 0 $OFFSET`; do
1360                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1361                 error "OST0 was degraded but new created file still use it"
1362         done
1363         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1364 }
1365 run_test 27x "create files while OST0 is degraded"
1366
1367 test_27y() {
1368         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1369         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1370         remote_ost_nodsh && skip "remote OST with nodsh" && return
1371
1372         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1373         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1374             osc.$mdtosc.prealloc_last_id)
1375         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1376             osc.$mdtosc.prealloc_next_id)
1377         local fcount=$((last_id - next_id))
1378         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1379         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1380
1381         MDS_OSCS=`do_facet $SINGLEMDS lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
1382         OFFSET=$(($OSTCOUNT-1))
1383         OST=-1
1384         for OSC in $MDS_OSCS; do
1385                 if [ $OST == -1 ]; then {
1386                         OST=`osc_to_ost $OSC`
1387                 } else {
1388                         echo $OSC "is Deactivate:"
1389                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1390                 } fi
1391         done
1392
1393         OSTIDX=$(index_from_ostuuid $OST)
1394         mkdir -p $DIR/$tdir
1395         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1396
1397         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 1
1398         sleep_maxage
1399         createmany -o $DIR/$tdir/$tfile $fcount
1400         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 0
1401
1402         for i in `seq 0 $OFFSET`; do
1403                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "$OSTIDX"` ] || \
1404                       error "files created on deactivated OSTs instead of degraded OST"
1405         done
1406         for OSC in $MDS_OSCS; do
1407                 [ `osc_to_ost $OSC` != $OST  ] && {
1408                         echo $OSC "is activate"
1409                         do_facet $SINGLEMDS lctl --device %$OSC activate
1410                 }
1411         done
1412 }
1413 run_test 27y "create files while OST0 is degraded and the rest inactive"
1414
1415 check_seq_oid()
1416 {
1417         log "check file $1"
1418
1419         lmm_count=$($GETSTRIPE -c $1)
1420         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1421         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1422
1423         local old_ifs="$IFS"
1424         IFS=$'[:]'
1425         fid=($($LFS path2fid $1))
1426         IFS="$old_ifs"
1427
1428         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1429         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1430
1431         # compare lmm_seq and lu_fid->f_seq
1432         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1433         # compare lmm_object_id and lu_fid->oid
1434         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1435
1436         # check the trusted.fid attribute of the OST objects of the file
1437         local have_obdidx=false
1438         local stripe_nr=0
1439         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1440                 # skip lines up to and including "obdidx"
1441                 [ -z "$obdidx" ] && break
1442                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1443                 $have_obdidx || continue
1444
1445                 local ost=$((obdidx + 1))
1446                 local dev=$(ostdevname $ost)
1447
1448                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1449                         echo "Currently only works with ldiskfs-based OSTs"
1450                         continue
1451                 fi
1452
1453                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1454
1455                 #don't unmount/remount the OSTs if we don't need to do that
1456                 #local dir=$(facet_mntpt ost$ost)
1457                 #stop ost$dev
1458                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1459                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1460                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1461                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1462
1463                 local obj_file="O/$seq/d$((oid %32))/$oid"
1464                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1465                            $dev 2>/dev/null" | grep "parent=")
1466
1467                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1468
1469                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1470
1471                 #do_facet ost$ost umount -d $dir
1472                 #start ost$ost $dev $OST_MOUNT_OPTS
1473
1474                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1475                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1476                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1477                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1478                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1479                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1480
1481                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1482                 [ $ff_pseq = $lmm_seq ] ||
1483                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1484                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1485                 [ $ff_poid = $lmm_oid ] ||
1486                         error "FF parent OID $ff_poid != $lmm_oid"
1487                 [ $ff_pstripe = $stripe_nr ] ||
1488                         error "FF stripe $ff_pstripe != $stripe_nr"
1489
1490                 stripe_nr=$((stripe_nr + 1))
1491         done
1492 }
1493
1494 test_27z() {
1495         remote_ost_nodsh && skip "remote OST with nodsh" && return
1496         mkdir -p $DIR/$tdir
1497
1498         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1499                 { error "setstripe -c -1 failed"; return 1; }
1500         # We need to send a write to every object to get parent FID info set.
1501         # This _should_ also work for setattr, but does not currently.
1502         # touch $DIR/$tdir/$tfile-1 ||
1503         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1504                 { error "dd $tfile-1 failed"; return 2; }
1505         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1506                 { error "setstripe -c -1 failed"; return 3; }
1507         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1508                 { error "dd $tfile-2 failed"; return 4; }
1509
1510         # make sure write RPCs have been sent to OSTs
1511         sync; sleep 5; sync
1512
1513         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1514         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1515 }
1516 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1517
1518 test_27A() { # b=19102
1519         local restore_size=$($GETSTRIPE -S $MOUNT)
1520         local restore_count=$($GETSTRIPE -c $MOUNT)
1521         local restore_offset=$($GETSTRIPE -i $MOUNT)
1522         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1523         local default_size=$($GETSTRIPE -S $MOUNT)
1524         local default_count=$($GETSTRIPE -c $MOUNT)
1525         local default_offset=$($GETSTRIPE -i $MOUNT)
1526         local dsize=$((1024 * 1024))
1527         [ $default_size -eq $dsize ] ||
1528                 error "stripe size $default_size != $dsize"
1529         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1530         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1531         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1532 }
1533 run_test 27A "check filesystem-wide default LOV EA values"
1534
1535 # createtest also checks that device nodes are created and
1536 # then visible correctly (#2091)
1537 test_28() { # bug 2091
1538         mkdir $DIR/d28
1539         $CREATETEST $DIR/d28/ct || error
1540 }
1541 run_test 28 "create/mknod/mkdir with bad file types ============"
1542
1543 test_29() {
1544         cancel_lru_locks mdc
1545         mkdir $DIR/d29
1546         touch $DIR/d29/foo
1547         log 'first d29'
1548         ls -l $DIR/d29
1549
1550         declare -i LOCKCOUNTORIG=0
1551         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1552                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1553         done
1554         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1555
1556         declare -i LOCKUNUSEDCOUNTORIG=0
1557         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1558                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1559         done
1560
1561         log 'second d29'
1562         ls -l $DIR/d29
1563         log 'done'
1564
1565         declare -i LOCKCOUNTCURRENT=0
1566         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1567                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1568         done
1569
1570         declare -i LOCKUNUSEDCOUNTCURRENT=0
1571         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1572                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1573         done
1574
1575         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1576                 lctl set_param -n ldlm.dump_namespaces ""
1577                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1578                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1579                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1580                 return 2
1581         fi
1582         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1583                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1584                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1585                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1586                 return 3
1587         fi
1588 }
1589 run_test 29 "IT_GETATTR regression  ============================"
1590
1591 test_30a() { # was test_30
1592         cp `which ls` $DIR || cp /bin/ls $DIR
1593         $DIR/ls / || error
1594         rm $DIR/ls
1595 }
1596 run_test 30a "execute binary from Lustre (execve) =============="
1597
1598 test_30b() {
1599         cp `which ls` $DIR || cp /bin/ls $DIR
1600         chmod go+rx $DIR/ls
1601         $RUNAS $DIR/ls / || error
1602         rm $DIR/ls
1603 }
1604 run_test 30b "execute binary from Lustre as non-root ==========="
1605
1606 test_30c() { # b=22376
1607         cp `which ls` $DIR || cp /bin/ls $DIR
1608         chmod a-rw $DIR/ls
1609         cancel_lru_locks mdc
1610         cancel_lru_locks osc
1611         $RUNAS $DIR/ls / || error
1612         rm -f $DIR/ls
1613 }
1614 run_test 30c "execute binary from Lustre without read perms ===="
1615
1616 test_31a() {
1617         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1618         $CHECKSTAT -a $DIR/f31 || error
1619 }
1620 run_test 31a "open-unlink file =================================="
1621
1622 test_31b() {
1623         touch $DIR/f31 || error
1624         ln $DIR/f31 $DIR/f31b || error
1625         $MULTIOP $DIR/f31b Ouc || error
1626         $CHECKSTAT -t file $DIR/f31 || error
1627 }
1628 run_test 31b "unlink file with multiple links while open ======="
1629
1630 test_31c() {
1631         touch $DIR/f31 || error
1632         ln $DIR/f31 $DIR/f31c || error
1633         multiop_bg_pause $DIR/f31 O_uc || return 1
1634         MULTIPID=$!
1635         $MULTIOP $DIR/f31c Ouc
1636         kill -USR1 $MULTIPID
1637         wait $MULTIPID
1638 }
1639 run_test 31c "open-unlink file with multiple links ============="
1640
1641 test_31d() {
1642         opendirunlink $DIR/d31d $DIR/d31d || error
1643         $CHECKSTAT -a $DIR/d31d || error
1644 }
1645 run_test 31d "remove of open directory ========================="
1646
1647 test_31e() { # bug 2904
1648         check_kernel_version 34 || return 0
1649         openfilleddirunlink $DIR/d31e || error
1650 }
1651 run_test 31e "remove of open non-empty directory ==============="
1652
1653 test_31f() { # bug 4554
1654         set -vx
1655         mkdir $DIR/d31f
1656         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1657         cp /etc/hosts $DIR/d31f
1658         ls -l $DIR/d31f
1659         $GETSTRIPE $DIR/d31f/hosts
1660         multiop_bg_pause $DIR/d31f D_c || return 1
1661         MULTIPID=$!
1662
1663         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1664         mkdir $DIR/d31f
1665         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1666         cp /etc/hosts $DIR/d31f
1667         ls -l $DIR/d31f
1668         $GETSTRIPE $DIR/d31f/hosts
1669         multiop_bg_pause $DIR/d31f D_c || return 1
1670         MULTIPID2=$!
1671
1672         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1673         wait $MULTIPID || error "first opendir $MULTIPID failed"
1674
1675         sleep 6
1676
1677         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1678         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1679         set +vx
1680 }
1681 run_test 31f "remove of open directory with open-unlink file ==="
1682
1683 test_31g() {
1684         echo "-- cross directory link --"
1685         mkdir $DIR/d31g{a,b}
1686         touch $DIR/d31ga/f
1687         ln $DIR/d31ga/f $DIR/d31gb/g
1688         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1689         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1690         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1691         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1692 }
1693 run_test 31g "cross directory link==============="
1694
1695 test_31h() {
1696         echo "-- cross directory link --"
1697         mkdir $DIR/d31h
1698         mkdir $DIR/d31h/dir
1699         touch $DIR/d31h/f
1700         ln $DIR/d31h/f $DIR/d31h/dir/g
1701         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1702         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1703         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1704         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1705 }
1706 run_test 31h "cross directory link under child==============="
1707
1708 test_31i() {
1709         echo "-- cross directory link --"
1710         mkdir $DIR/d31i
1711         mkdir $DIR/d31i/dir
1712         touch $DIR/d31i/dir/f
1713         ln $DIR/d31i/dir/f $DIR/d31i/g
1714         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1715         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1716         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1717         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1718 }
1719 run_test 31i "cross directory link under parent==============="
1720
1721
1722 test_31j() {
1723         mkdir $DIR/d31j
1724         mkdir $DIR/d31j/dir1
1725         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1726         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1727         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1728         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1729         return 0
1730 }
1731 run_test 31j "link for directory==============="
1732
1733
1734 test_31k() {
1735         mkdir $DIR/d31k
1736         touch $DIR/d31k/s
1737         touch $DIR/d31k/exist
1738         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1739         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1740         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1741         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1742         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1743         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1744         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1745         return 0
1746 }
1747 run_test 31k "link to file: the same, non-existing, dir==============="
1748
1749 test_31m() {
1750         mkdir $DIR/d31m
1751         touch $DIR/d31m/s
1752         mkdir $DIR/d31m2
1753         touch $DIR/d31m2/exist
1754         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1755         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1756         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1757         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1758         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1759         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1760         return 0
1761 }
1762 run_test 31m "link to file: the same, non-existing, dir==============="
1763
1764 test_32a() {
1765         echo "== more mountpoints and symlinks ================="
1766         [ -e $DIR/d32a ] && rm -fr $DIR/d32a
1767         mkdir -p $DIR/d32a/ext2-mountpoint
1768         mount -t ext2 -o loop $EXT2_DEV $DIR/d32a/ext2-mountpoint || error
1769         $CHECKSTAT -t dir $DIR/d32a/ext2-mountpoint/.. || error
1770         $UMOUNT $DIR/d32a/ext2-mountpoint || error
1771 }
1772 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1773
1774 test_32b() {
1775         [ -e $DIR/d32b ] && rm -fr $DIR/d32b
1776         mkdir -p $DIR/d32b/ext2-mountpoint
1777         mount -t ext2 -o loop $EXT2_DEV $DIR/d32b/ext2-mountpoint || error
1778         ls -al $DIR/d32b/ext2-mountpoint/.. || error
1779         $UMOUNT $DIR/d32b/ext2-mountpoint || error
1780 }
1781 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1782
1783 test_32c() {
1784         [ -e $DIR/d32c ] && rm -fr $DIR/d32c
1785         mkdir -p $DIR/d32c/ext2-mountpoint
1786         mount -t ext2 -o loop $EXT2_DEV $DIR/d32c/ext2-mountpoint || error
1787         mkdir -p $DIR/d32c/d2/test_dir
1788         $CHECKSTAT -t dir $DIR/d32c/ext2-mountpoint/../d2/test_dir || error
1789         $UMOUNT $DIR/d32c/ext2-mountpoint || error
1790 }
1791 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1792
1793 test_32d() {
1794         [ -e $DIR/d32d ] && rm -fr $DIR/d32d
1795         mkdir -p $DIR/d32d/ext2-mountpoint
1796         mount -t ext2 -o loop $EXT2_DEV $DIR/d32d/ext2-mountpoint || error
1797         mkdir -p $DIR/d32d/d2/test_dir
1798         ls -al $DIR/d32d/ext2-mountpoint/../d2/test_dir || error
1799         $UMOUNT $DIR/d32d/ext2-mountpoint || error
1800 }
1801 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1802
1803 test_32e() {
1804         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1805         mkdir -p $DIR/d32e/tmp
1806         TMP_DIR=$DIR/d32e/tmp
1807         ln -s $DIR/d32e $TMP_DIR/symlink11
1808         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1809         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1810         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1811 }
1812 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1813
1814 test_32f() {
1815         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1816         mkdir -p $DIR/d32f/tmp
1817         TMP_DIR=$DIR/d32f/tmp
1818         ln -s $DIR/d32f $TMP_DIR/symlink11
1819         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1820         ls $DIR/d32f/tmp/symlink11  || error
1821         ls $DIR/d32f/symlink01 || error
1822 }
1823 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
1824
1825 test_32g() {
1826         TMP_DIR=$DIR/$tdir/tmp
1827         mkdir -p $TMP_DIR $DIR/${tdir}2
1828         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1829         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1830         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
1831         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
1832         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
1833         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
1834 }
1835 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1836
1837 test_32h() {
1838         rm -fr $DIR/$tdir $DIR/${tdir}2
1839         TMP_DIR=$DIR/$tdir/tmp
1840         mkdir -p $TMP_DIR $DIR/${tdir}2
1841         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1842         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1843         ls $TMP_DIR/symlink12 || error
1844         ls $DIR/$tdir/symlink02  || error
1845 }
1846 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1847
1848 test_32i() {
1849         [ -e $DIR/d32i ] && rm -fr $DIR/d32i
1850         mkdir -p $DIR/d32i/ext2-mountpoint
1851         mount -t ext2 -o loop $EXT2_DEV $DIR/d32i/ext2-mountpoint || error
1852         touch $DIR/d32i/test_file
1853         $CHECKSTAT -t file $DIR/d32i/ext2-mountpoint/../test_file || error
1854         $UMOUNT $DIR/d32i/ext2-mountpoint || error
1855 }
1856 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
1857
1858 test_32j() {
1859         [ -e $DIR/d32j ] && rm -fr $DIR/d32j
1860         mkdir -p $DIR/d32j/ext2-mountpoint
1861         mount -t ext2 -o loop $EXT2_DEV $DIR/d32j/ext2-mountpoint || error
1862         touch $DIR/d32j/test_file
1863         cat $DIR/d32j/ext2-mountpoint/../test_file || error
1864         $UMOUNT $DIR/d32j/ext2-mountpoint || error
1865 }
1866 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
1867
1868 test_32k() {
1869         rm -fr $DIR/d32k
1870         mkdir -p $DIR/d32k/ext2-mountpoint
1871         mount -t ext2 -o loop $EXT2_DEV $DIR/d32k/ext2-mountpoint
1872         mkdir -p $DIR/d32k/d2
1873         touch $DIR/d32k/d2/test_file || error
1874         $CHECKSTAT -t file $DIR/d32k/ext2-mountpoint/../d2/test_file || error
1875         $UMOUNT $DIR/d32k/ext2-mountpoint || error
1876 }
1877 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
1878
1879 test_32l() {
1880         rm -fr $DIR/d32l
1881         mkdir -p $DIR/d32l/ext2-mountpoint
1882         mount -t ext2 -o loop $EXT2_DEV $DIR/d32l/ext2-mountpoint || error
1883         mkdir -p $DIR/d32l/d2
1884         touch $DIR/d32l/d2/test_file
1885         cat  $DIR/d32l/ext2-mountpoint/../d2/test_file || error
1886         $UMOUNT $DIR/d32l/ext2-mountpoint || error
1887 }
1888 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
1889
1890 test_32m() {
1891         rm -fr $DIR/d32m
1892         mkdir -p $DIR/d32m/tmp
1893         TMP_DIR=$DIR/d32m/tmp
1894         ln -s $DIR $TMP_DIR/symlink11
1895         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1896         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
1897         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
1898 }
1899 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
1900
1901 test_32n() {
1902         rm -fr $DIR/d32n
1903         mkdir -p $DIR/d32n/tmp
1904         TMP_DIR=$DIR/d32n/tmp
1905         ln -s $DIR $TMP_DIR/symlink11
1906         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1907         ls -l $DIR/d32n/tmp/symlink11  || error
1908         ls -l $DIR/d32n/symlink01 || error
1909 }
1910 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
1911
1912 test_32o() {
1913         rm -fr $DIR/d32o $DIR/$tfile
1914         touch $DIR/$tfile
1915         mkdir -p $DIR/d32o/tmp
1916         TMP_DIR=$DIR/d32o/tmp
1917         ln -s $DIR/$tfile $TMP_DIR/symlink12
1918         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1919         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
1920         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
1921         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
1922         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
1923 }
1924 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
1925
1926 test_32p() {
1927     log 32p_1
1928         rm -fr $DIR/d32p
1929     log 32p_2
1930         rm -f $DIR/$tfile
1931     log 32p_3
1932         touch $DIR/$tfile
1933     log 32p_4
1934         mkdir -p $DIR/d32p/tmp
1935     log 32p_5
1936         TMP_DIR=$DIR/d32p/tmp
1937     log 32p_6
1938         ln -s $DIR/$tfile $TMP_DIR/symlink12
1939     log 32p_7
1940         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1941     log 32p_8
1942         cat $DIR/d32p/tmp/symlink12 || error
1943     log 32p_9
1944         cat $DIR/d32p/symlink02 || error
1945     log 32p_10
1946 }
1947 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
1948
1949 test_32q() {
1950         [ -e $DIR/d32q ] && rm -fr $DIR/d32q
1951         mkdir -p $DIR/d32q
1952         touch $DIR/d32q/under_the_mount
1953         mount -t ext2 -o loop $EXT2_DEV $DIR/d32q
1954         ls $DIR/d32q/under_the_mount && error || true
1955         $UMOUNT $DIR/d32q || error
1956 }
1957 run_test 32q "stat follows mountpoints in Lustre (should return error)"
1958
1959 test_32r() {
1960         [ -e $DIR/d32r ] && rm -fr $DIR/d32r
1961         mkdir -p $DIR/d32r
1962         touch $DIR/d32r/under_the_mount
1963         mount -t ext2 -o loop $EXT2_DEV $DIR/d32r
1964         ls $DIR/d32r | grep -q under_the_mount && error || true
1965         $UMOUNT $DIR/d32r || error
1966 }
1967 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
1968
1969 test_33() {
1970         rm -f $DIR/$tfile
1971         touch $DIR/$tfile
1972         chmod 444 $DIR/$tfile
1973         chown $RUNAS_ID $DIR/$tfile
1974         log 33_1
1975         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
1976         log 33_2
1977 }
1978 run_test 33 "write file with mode 444 (should return error) ===="
1979
1980 test_33a() {
1981         rm -fr $DIR/d33
1982         mkdir -p $DIR/d33
1983         chown $RUNAS_ID $DIR/d33
1984         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
1985         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
1986                 error "open RDWR" || true
1987 }
1988 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
1989
1990 test_33b() {
1991         rm -fr $DIR/d33
1992         mkdir -p $DIR/d33
1993         chown $RUNAS_ID $DIR/d33
1994         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
1995 }
1996 run_test 33b "test open file with malformed flags (No panic and return error)"
1997
1998 test_33c() {
1999         local ostnum
2000         local ostname
2001         local write_bytes
2002         local all_zeros
2003
2004         remote_ost_nodsh && skip "remote OST with nodsh" && return
2005         all_zeros=:
2006         rm -fr $DIR/d33
2007         mkdir -p $DIR/d33
2008         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2009
2010         sync
2011         for ostnum in $(seq $OSTCOUNT); do
2012                 # test-framework's OST numbering is one-based, while Lustre's
2013                 # is zero-based
2014                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2015                 # Parsing llobdstat's output sucks; we could grep the /proc
2016                 # path, but that's likely to not be as portable as using the
2017                 # llobdstat utility.  So we parse lctl output instead.
2018                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2019                         obdfilter/$ostname/stats |
2020                         awk '/^write_bytes/ {print $7}' )
2021                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2022                 if (( ${write_bytes:-0} > 0 ))
2023                 then
2024                         all_zeros=false
2025                         break;
2026                 fi
2027         done
2028
2029         $all_zeros || return 0
2030
2031         # Write four bytes
2032         echo foo > $DIR/d33/bar
2033         # Really write them
2034         sync
2035
2036         # Total up write_bytes after writing.  We'd better find non-zeros.
2037         for ostnum in $(seq $OSTCOUNT); do
2038                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2039                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2040                         obdfilter/$ostname/stats |
2041                         awk '/^write_bytes/ {print $7}' )
2042                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2043                 if (( ${write_bytes:-0} > 0 ))
2044                 then
2045                         all_zeros=false
2046                         break;
2047                 fi
2048         done
2049
2050         if $all_zeros
2051         then
2052                 for ostnum in $(seq $OSTCOUNT); do
2053                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2054                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2055                         do_facet ost$ostnum lctl get_param -n \
2056                                 obdfilter/$ostname/stats
2057                 done
2058                 error "OST not keeping write_bytes stats (b22312)"
2059         fi
2060 }
2061 run_test 33c "test llobdstat and write_bytes"
2062
2063 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2064 test_34a() {
2065         rm -f $DIR/f34
2066         $MCREATE $DIR/f34 || error
2067         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2068         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2069         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2070         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2071 }
2072 run_test 34a "truncate file that has not been opened ==========="
2073
2074 test_34b() {
2075         [ ! -f $DIR/f34 ] && test_34a
2076         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2077         $OPENFILE -f O_RDONLY $DIR/f34
2078         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2079         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2080 }
2081 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2082
2083 test_34c() {
2084         [ ! -f $DIR/f34 ] && test_34a
2085         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2086         $OPENFILE -f O_RDWR $DIR/f34
2087         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2088         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2089 }
2090 run_test 34c "O_RDWR opening file-with-size works =============="
2091
2092 test_34d() {
2093         [ ! -f $DIR/f34 ] && test_34a
2094         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2095         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2096         rm $DIR/f34
2097 }
2098 run_test 34d "write to sparse file ============================="
2099
2100 test_34e() {
2101         rm -f $DIR/f34e
2102         $MCREATE $DIR/f34e || error
2103         $TRUNCATE $DIR/f34e 1000 || error
2104         $CHECKSTAT -s 1000 $DIR/f34e || error
2105         $OPENFILE -f O_RDWR $DIR/f34e
2106         $CHECKSTAT -s 1000 $DIR/f34e || error
2107 }
2108 run_test 34e "create objects, some with size and some without =="
2109
2110 test_34f() { # bug 6242, 6243
2111         SIZE34F=48000
2112         rm -f $DIR/f34f
2113         $MCREATE $DIR/f34f || error
2114         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2115         dd if=$DIR/f34f of=$TMP/f34f
2116         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2117         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2118         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2119         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2120         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2121 }
2122 run_test 34f "read from a file with no objects until EOF ======="
2123
2124 test_34g() {
2125         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2126         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2127         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2128         cancel_lru_locks osc
2129         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2130                 error "wrong size after lock cancel"
2131
2132         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2133         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2134                 error "expanding truncate failed"
2135         cancel_lru_locks osc
2136         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2137                 error "wrong expanded size after lock cancel"
2138 }
2139 run_test 34g "truncate long file ==============================="
2140
2141 test_34h() {
2142         local gid=10
2143         local sz=1000
2144
2145         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2146         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2147         MULTIPID=$!
2148         sleep 2
2149
2150         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2151                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2152                 kill -9 $MULTIPID
2153         fi
2154         wait $MULTIPID
2155         local nsz=`stat -c %s $DIR/$tfile`
2156         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2157 }
2158 run_test 34h "ftruncate file under grouplock should not block"
2159
2160 test_35a() {
2161         cp /bin/sh $DIR/f35a
2162         chmod 444 $DIR/f35a
2163         chown $RUNAS_ID $DIR/f35a
2164         $RUNAS $DIR/f35a && error || true
2165         rm $DIR/f35a
2166 }
2167 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2168
2169 test_36a() {
2170         rm -f $DIR/f36
2171         utime $DIR/f36 || error
2172 }
2173 run_test 36a "MDS utime check (mknod, utime) ==================="
2174
2175 test_36b() {
2176         echo "" > $DIR/f36
2177         utime $DIR/f36 || error
2178 }
2179 run_test 36b "OST utime check (open, utime) ===================="
2180
2181 test_36c() {
2182         rm -f $DIR/d36/f36
2183         mkdir $DIR/d36
2184         chown $RUNAS_ID $DIR/d36
2185         $RUNAS utime $DIR/d36/f36 || error
2186 }
2187 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2188
2189 test_36d() {
2190         [ ! -d $DIR/d36 ] && test_36c
2191         echo "" > $DIR/d36/f36
2192         $RUNAS utime $DIR/d36/f36 || error
2193 }
2194 run_test 36d "non-root OST utime check (open, utime) ==========="
2195
2196 test_36e() {
2197         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2198         mkdir -p $DIR/$tdir
2199         touch $DIR/$tdir/$tfile
2200         $RUNAS utime $DIR/$tdir/$tfile && \
2201                 error "utime worked, expected failure" || true
2202 }
2203 run_test 36e "utime on non-owned file (should return error) ===="
2204
2205 subr_36fh() {
2206         local fl="$1"
2207         local LANG_SAVE=$LANG
2208         local LC_LANG_SAVE=$LC_LANG
2209         export LANG=C LC_LANG=C # for date language
2210
2211         DATESTR="Dec 20  2000"
2212         mkdir -p $DIR/$tdir
2213         lctl set_param fail_loc=$fl
2214         date; date +%s
2215         cp /etc/hosts $DIR/$tdir/$tfile
2216         sync & # write RPC generated with "current" inode timestamp, but delayed
2217         sleep 1
2218         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2219         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2220         cancel_lru_locks osc
2221         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2222         date; date +%s
2223         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2224                 echo "BEFORE: $LS_BEFORE" && \
2225                 echo "AFTER : $LS_AFTER" && \
2226                 echo "WANT  : $DATESTR" && \
2227                 error "$DIR/$tdir/$tfile timestamps changed" || true
2228
2229         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2230 }
2231
2232 test_36f() {
2233         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2234         subr_36fh "0x80000214"
2235 }
2236 run_test 36f "utime on file racing with OST BRW write =========="
2237
2238 test_36g() {
2239         remote_ost_nodsh && skip "remote OST with nodsh" && return
2240         local fmd_max_age
2241         local fmd_before
2242         local fmd_after
2243
2244         mkdir -p $DIR/$tdir
2245         fmd_max_age=$(do_facet ost1 \
2246                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2247                 head -n 1")
2248
2249         fmd_before=$(do_facet ost1 \
2250                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2251         touch $DIR/$tdir/$tfile
2252         sleep $((fmd_max_age + 12))
2253         fmd_after=$(do_facet ost1 \
2254                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2255
2256         echo "fmd_before: $fmd_before"
2257         echo "fmd_after: $fmd_after"
2258         [ "$fmd_after" -gt "$fmd_before" ] && \
2259                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2260                 error "fmd didn't expire after ping" || true
2261 }
2262 run_test 36g "filter mod data cache expiry ====================="
2263
2264 test_36h() {
2265         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2266         subr_36fh "0x80000227"
2267 }
2268 run_test 36h "utime on file racing with OST BRW write =========="
2269
2270 test_37() {
2271         mkdir -p $DIR/$tdir
2272         echo f > $DIR/$tdir/fbugfile
2273         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2274         ls $DIR/$tdir | grep "\<fbugfile\>" && error
2275         $UMOUNT $DIR/$tdir || error
2276         rm -f $DIR/$tdir/fbugfile || error
2277 }
2278 run_test 37 "ls a mounted file system to check old content ====="
2279
2280 test_38() {
2281         local file=$DIR/$tfile
2282         touch $file
2283         openfile -f O_DIRECTORY $file
2284         local RC=$?
2285         local ENOTDIR=20
2286         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2287         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2288 }
2289 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2290
2291 test_39() {
2292         touch $DIR/$tfile
2293         touch $DIR/${tfile}2
2294 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2295 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2296 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2297         sleep 2
2298         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2299         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2300                 echo "mtime"
2301                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2302                 echo "atime"
2303                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2304                 echo "ctime"
2305                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2306                 error "O_TRUNC didn't change timestamps"
2307         fi
2308 }
2309 run_test 39 "mtime changed on create ==========================="
2310
2311 test_39b() {
2312         mkdir -p $DIR/$tdir
2313         cp -p /etc/passwd $DIR/$tdir/fopen
2314         cp -p /etc/passwd $DIR/$tdir/flink
2315         cp -p /etc/passwd $DIR/$tdir/funlink
2316         cp -p /etc/passwd $DIR/$tdir/frename
2317         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2318
2319         sleep 1
2320         echo "aaaaaa" >> $DIR/$tdir/fopen
2321         echo "aaaaaa" >> $DIR/$tdir/flink
2322         echo "aaaaaa" >> $DIR/$tdir/funlink
2323         echo "aaaaaa" >> $DIR/$tdir/frename
2324
2325         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2326         local link_new=`stat -c %Y $DIR/$tdir/flink`
2327         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2328         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2329
2330         cat $DIR/$tdir/fopen > /dev/null
2331         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2332         rm -f $DIR/$tdir/funlink2
2333         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2334
2335         for (( i=0; i < 2; i++ )) ; do
2336                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2337                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2338                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2339                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2340
2341                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2342                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2343                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2344                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2345
2346                 cancel_lru_locks osc
2347                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2348         done
2349 }
2350 run_test 39b "mtime change on open, link, unlink, rename  ======"
2351
2352 # this should be set to past
2353 TEST_39_MTIME=`date -d "1 year ago" +%s`
2354
2355 # bug 11063
2356 test_39c() {
2357         touch $DIR1/$tfile
2358         sleep 2
2359         local mtime0=`stat -c %Y $DIR1/$tfile`
2360
2361         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2362         local mtime1=`stat -c %Y $DIR1/$tfile`
2363         [ "$mtime1" = $TEST_39_MTIME ] || \
2364                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2365
2366         local d1=`date +%s`
2367         echo hello >> $DIR1/$tfile
2368         local d2=`date +%s`
2369         local mtime2=`stat -c %Y $DIR1/$tfile`
2370         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2371                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2372
2373         mv $DIR1/$tfile $DIR1/$tfile-1
2374
2375         for (( i=0; i < 2; i++ )) ; do
2376                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2377                 [ "$mtime2" = "$mtime3" ] || \
2378                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2379
2380                 cancel_lru_locks osc
2381                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2382         done
2383 }
2384 run_test 39c "mtime change on rename ==========================="
2385
2386 # bug 21114
2387 test_39d() {
2388         touch $DIR1/$tfile
2389
2390         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2391
2392         for (( i=0; i < 2; i++ )) ; do
2393                 local mtime=`stat -c %Y $DIR1/$tfile`
2394                 [ $mtime = $TEST_39_MTIME ] || \
2395                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2396
2397                 cancel_lru_locks osc
2398                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2399         done
2400 }
2401 run_test 39d "create, utime, stat =============================="
2402
2403 # bug 21114
2404 test_39e() {
2405         touch $DIR1/$tfile
2406         local mtime1=`stat -c %Y $DIR1/$tfile`
2407
2408         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2409
2410         for (( i=0; i < 2; i++ )) ; do
2411                 local mtime2=`stat -c %Y $DIR1/$tfile`
2412                 [ $mtime2 = $TEST_39_MTIME ] || \
2413                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2414
2415                 cancel_lru_locks osc
2416                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2417         done
2418 }
2419 run_test 39e "create, stat, utime, stat ========================"
2420
2421 # bug 21114
2422 test_39f() {
2423         touch $DIR1/$tfile
2424         mtime1=`stat -c %Y $DIR1/$tfile`
2425
2426         sleep 2
2427         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2428
2429         for (( i=0; i < 2; i++ )) ; do
2430                 local mtime2=`stat -c %Y $DIR1/$tfile`
2431                 [ $mtime2 = $TEST_39_MTIME ] || \
2432                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2433
2434                 cancel_lru_locks osc
2435                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2436         done
2437 }
2438 run_test 39f "create, stat, sleep, utime, stat ================="
2439
2440 # bug 11063
2441 test_39g() {
2442         echo hello >> $DIR1/$tfile
2443         local mtime1=`stat -c %Y $DIR1/$tfile`
2444
2445         sleep 2
2446         chmod o+r $DIR1/$tfile
2447
2448         for (( i=0; i < 2; i++ )) ; do
2449                 local mtime2=`stat -c %Y $DIR1/$tfile`
2450                 [ "$mtime1" = "$mtime2" ] || \
2451                         error "lost mtime: $mtime2, should be $mtime1"
2452
2453                 cancel_lru_locks osc
2454                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2455         done
2456 }
2457 run_test 39g "write, chmod, stat ==============================="
2458
2459 # bug 11063
2460 test_39h() {
2461         touch $DIR1/$tfile
2462         sleep 1
2463
2464         local d1=`date`
2465         echo hello >> $DIR1/$tfile
2466         local mtime1=`stat -c %Y $DIR1/$tfile`
2467
2468         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2469         local d2=`date`
2470         if [ "$d1" != "$d2" ]; then
2471                 echo "write and touch not within one second"
2472         else
2473                 for (( i=0; i < 2; i++ )) ; do
2474                         local mtime2=`stat -c %Y $DIR1/$tfile`
2475                         [ "$mtime2" = $TEST_39_MTIME ] || \
2476                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2477
2478                         cancel_lru_locks osc
2479                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2480                 done
2481         fi
2482 }
2483 run_test 39h "write, utime within one second, stat ============="
2484
2485 test_39i() {
2486         touch $DIR1/$tfile
2487         sleep 1
2488
2489         echo hello >> $DIR1/$tfile
2490         local mtime1=`stat -c %Y $DIR1/$tfile`
2491
2492         mv $DIR1/$tfile $DIR1/$tfile-1
2493
2494         for (( i=0; i < 2; i++ )) ; do
2495                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2496
2497                 [ "$mtime1" = "$mtime2" ] || \
2498                         error "lost mtime: $mtime2, should be $mtime1"
2499
2500                 cancel_lru_locks osc
2501                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2502         done
2503 }
2504 run_test 39i "write, rename, stat =============================="
2505
2506 test_39j() {
2507         start_full_debug_logging
2508         touch $DIR1/$tfile
2509         sleep 1
2510
2511         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2512         lctl set_param fail_loc=0x80000412
2513         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2514                 error "multiop failed"
2515         local multipid=$!
2516         local mtime1=`stat -c %Y $DIR1/$tfile`
2517
2518         mv $DIR1/$tfile $DIR1/$tfile-1
2519
2520         kill -USR1 $multipid
2521         wait $multipid || error "multiop close failed"
2522
2523         for (( i=0; i < 2; i++ )) ; do
2524                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2525                 [ "$mtime1" = "$mtime2" ] ||
2526                         error "mtime is lost on close: $mtime2, " \
2527                               "should be $mtime1"
2528
2529                 cancel_lru_locks osc
2530                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2531         done
2532         lctl set_param fail_loc=0
2533         stop_full_debug_logging
2534 }
2535 run_test 39j "write, rename, close, stat ======================="
2536
2537 test_39k() {
2538         touch $DIR1/$tfile
2539         sleep 1
2540
2541         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2542         local multipid=$!
2543         local mtime1=`stat -c %Y $DIR1/$tfile`
2544
2545         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2546
2547         kill -USR1 $multipid
2548         wait $multipid || error "multiop close failed"
2549
2550         for (( i=0; i < 2; i++ )) ; do
2551                 local mtime2=`stat -c %Y $DIR1/$tfile`
2552
2553                 [ "$mtime2" = $TEST_39_MTIME ] || \
2554                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2555
2556                 cancel_lru_locks osc
2557                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2558         done
2559 }
2560 run_test 39k "write, utime, close, stat ========================"
2561
2562 # this should be set to future
2563 TEST_39_ATIME=`date -d "1 year" +%s`
2564
2565 is_sles11()                                             # LU-1783
2566 {
2567         if [ -r /etc/SuSE-release ]
2568         then
2569                 local vers=`grep VERSION /etc/SuSE-release | awk '{print $3}'`
2570                 local patchlev=`grep PATCHLEVEL /etc/SuSE-release \
2571                         | awk '{print $3}'`
2572                 if [ $vers -eq 11 ] && [ $patchlev -eq 1 ]
2573                 then
2574                         return 0
2575                 fi
2576         fi
2577         return 1
2578 }
2579
2580 test_39l() {
2581         is_sles11 && skip "SLES 11 SP1" && return       # LU-1783
2582         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2583         local atime_diff=$(do_facet $SINGLEMDS lctl get_param -n mdd.*.atime_diff)
2584
2585         mkdir -p $DIR/$tdir
2586
2587         # test setting directory atime to future
2588         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2589         local atime=$(stat -c %X $DIR/$tdir)
2590         [ "$atime" = $TEST_39_ATIME ] || \
2591                 error "atime is not set to future: $atime, should be $TEST_39_ATIME"
2592
2593         # test setting directory atime from future to now
2594         local d1=$(date +%s)
2595         ls $DIR/$tdir
2596         local d2=$(date +%s)
2597
2598         cancel_lru_locks mdc
2599         atime=$(stat -c %X $DIR/$tdir)
2600         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2601                 error "atime is not updated from future: $atime, should be $d1<atime<$d2"
2602
2603         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=2
2604         sleep 3
2605
2606         # test setting directory atime when now > dir atime + atime_diff
2607         d1=$(date +%s)
2608         ls $DIR/$tdir
2609         d2=$(date +%s)
2610         cancel_lru_locks mdc
2611         atime=$(stat -c %X $DIR/$tdir)
2612         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2613                 error "atime is not updated  : $atime, should be $d2"
2614
2615         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=60
2616         sleep 3
2617
2618         # test not setting directory atime when now < dir atime + atime_diff
2619         ls $DIR/$tdir
2620         cancel_lru_locks mdc
2621         atime=$(stat -c %X $DIR/$tdir)
2622         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2623                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2624
2625         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=$atime_diff
2626 }
2627 run_test 39l "directory atime update ==========================="
2628
2629 test_39m() {
2630         touch $DIR1/$tfile
2631         sleep 2
2632         local far_past_mtime=$(date -d "May 29 1953" +%s)
2633         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2634
2635         touch -m -d @$far_past_mtime $DIR1/$tfile
2636         touch -a -d @$far_past_atime $DIR1/$tfile
2637
2638         for (( i=0; i < 2; i++ )) ; do
2639                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2640                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2641                         error "atime or mtime set incorrectly"
2642
2643                 cancel_lru_locks osc
2644                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2645         done
2646 }
2647 run_test 39m "test atime and mtime before 1970"
2648
2649 test_40() {
2650         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2651         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2652         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2653 }
2654 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2655
2656 test_41() {
2657         # bug 1553
2658         small_write $DIR/f41 18
2659 }
2660 run_test 41 "test small file write + fstat ====================="
2661
2662 count_ost_writes() {
2663         lctl get_param -n osc.*.stats |
2664             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2665 }
2666
2667 # decent default
2668 WRITEBACK_SAVE=500
2669 DIRTY_RATIO_SAVE=40
2670 MAX_DIRTY_RATIO=50
2671 BG_DIRTY_RATIO_SAVE=10
2672 MAX_BG_DIRTY_RATIO=25
2673
2674 start_writeback() {
2675         trap 0
2676         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2677         # dirty_ratio, dirty_background_ratio
2678         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2679                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2680                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2681                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2682         else
2683                 # if file not here, we are a 2.4 kernel
2684                 kill -CONT `pidof kupdated`
2685         fi
2686 }
2687
2688 stop_writeback() {
2689         # setup the trap first, so someone cannot exit the test at the
2690         # exact wrong time and mess up a machine
2691         trap start_writeback EXIT
2692         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2693         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2694                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2695                 sysctl -w vm.dirty_writeback_centisecs=0
2696                 sysctl -w vm.dirty_writeback_centisecs=0
2697                 # save and increase /proc/sys/vm/dirty_ratio
2698                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2699                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2700                 # save and increase /proc/sys/vm/dirty_background_ratio
2701                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2702                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2703         else
2704                 # if file not here, we are a 2.4 kernel
2705                 kill -STOP `pidof kupdated`
2706         fi
2707 }
2708
2709 # ensure that all stripes have some grant before we test client-side cache
2710 setup_test42() {
2711         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2712                 dd if=/dev/zero of=$i bs=4k count=1
2713                 rm $i
2714         done
2715 }
2716
2717 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2718 # file truncation, and file removal.
2719 test_42a() {
2720         setup_test42
2721         cancel_lru_locks osc
2722         stop_writeback
2723         sync; sleep 1; sync # just to be safe
2724         BEFOREWRITES=`count_ost_writes`
2725         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2726         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2727         AFTERWRITES=`count_ost_writes`
2728         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2729                 error "$BEFOREWRITES < $AFTERWRITES"
2730         start_writeback
2731 }
2732 run_test 42a "ensure that we don't flush on close =============="
2733
2734 test_42b() {
2735         setup_test42
2736         cancel_lru_locks osc
2737         stop_writeback
2738         sync
2739         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2740         BEFOREWRITES=`count_ost_writes`
2741         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2742         AFTERWRITES=`count_ost_writes`
2743         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2744                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2745         fi
2746         BEFOREWRITES=`count_ost_writes`
2747         sync || error "sync: $?"
2748         AFTERWRITES=`count_ost_writes`
2749         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2750                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2751         fi
2752         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2753         start_writeback
2754         return 0
2755 }
2756 run_test 42b "test destroy of file with cached dirty data ======"
2757
2758 # if these tests just want to test the effect of truncation,
2759 # they have to be very careful.  consider:
2760 # - the first open gets a {0,EOF}PR lock
2761 # - the first write conflicts and gets a {0, count-1}PW
2762 # - the rest of the writes are under {count,EOF}PW
2763 # - the open for truncate tries to match a {0,EOF}PR
2764 #   for the filesize and cancels the PWs.
2765 # any number of fixes (don't get {0,EOF} on open, match
2766 # composite locks, do smarter file size management) fix
2767 # this, but for now we want these tests to verify that
2768 # the cancellation with truncate intent works, so we
2769 # start the file with a full-file pw lock to match against
2770 # until the truncate.
2771 trunc_test() {
2772         test=$1
2773         file=$DIR/$test
2774         offset=$2
2775         cancel_lru_locks osc
2776         stop_writeback
2777         # prime the file with 0,EOF PW to match
2778         touch $file
2779         $TRUNCATE $file 0
2780         sync; sync
2781         # now the real test..
2782         dd if=/dev/zero of=$file bs=1024 count=100
2783         BEFOREWRITES=`count_ost_writes`
2784         $TRUNCATE $file $offset
2785         cancel_lru_locks osc
2786         AFTERWRITES=`count_ost_writes`
2787         start_writeback
2788 }
2789
2790 test_42c() {
2791         trunc_test 42c 1024
2792         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2793             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2794         rm $file
2795 }
2796 run_test 42c "test partial truncate of file with cached dirty data"
2797
2798 test_42d() {
2799         trunc_test 42d 0
2800         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2801             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2802         rm $file
2803 }
2804 run_test 42d "test complete truncate of file with cached dirty data"
2805
2806 test_42e() { # bug22074
2807         local TDIR=$DIR/${tdir}e
2808         local pagesz=$(page_size)
2809         local pages=16 # hardcoded 16 pages, don't change it.
2810         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
2811         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2812         local max_dirty_mb
2813         local warmup_files
2814
2815         mkdir -p $TDIR
2816         $SETSTRIPE -c 1 $TDIR
2817         createmany -o $TDIR/f $files
2818
2819         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2820
2821         # we assume that with $OSTCOUNT files, at least one of them will
2822         # be allocated on OST0.
2823         warmup_files=$((OSTCOUNT * max_dirty_mb))
2824         createmany -o $TDIR/w $warmup_files
2825
2826         # write a large amount of data into one file and sync, to get good
2827         # avail_grant number from OST.
2828         for ((i=0; i<$warmup_files; i++)); do
2829                 idx=$($GETSTRIPE -i $TDIR/w$i)
2830                 [ $idx -ne 0 ] && continue
2831                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2832                 break
2833         done
2834         [ $i -gt $warmup_files ] && error "OST0 is still cold"
2835         sync
2836         $LCTL get_param $proc_osc0/cur_dirty_bytes
2837         $LCTL get_param $proc_osc0/cur_grant_bytes
2838
2839         # create as much dirty pages as we can while not to trigger the actual
2840         # RPCs directly. but depends on the env, VFS may trigger flush during this
2841         # period, hopefully we are good.
2842         for ((i=0; i<$warmup_files; i++)); do
2843                 idx=$($GETSTRIPE -i $TDIR/w$i)
2844                 [ $idx -ne 0 ] && continue
2845                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2846         done
2847         $LCTL get_param $proc_osc0/cur_dirty_bytes
2848         $LCTL get_param $proc_osc0/cur_grant_bytes
2849
2850         # perform the real test
2851         $LCTL set_param $proc_osc0/rpc_stats 0
2852         for ((;i<$files; i++)); do
2853                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
2854                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2855         done
2856         sync
2857         $LCTL get_param $proc_osc0/rpc_stats
2858
2859         local percent=0
2860         local have_ppr=false
2861         $LCTL get_param $proc_osc0/rpc_stats |
2862                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2863                         # skip lines until we are at the RPC histogram data
2864                         [ "$PPR" == "pages" ] && have_ppr=true && continue
2865                         $have_ppr || continue
2866
2867                         # we only want the percent stat for < 16 pages
2868                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
2869
2870                         percent=$((percent + WPCT))
2871                         if [ $percent -gt 15 ]; then
2872                                 error "less than 16-pages write RPCs" \
2873                                       "$percent% > 15%"
2874                                 break
2875                         fi
2876                 done
2877         rm -rf $TDIR
2878 }
2879 run_test 42e "verify sub-RPC writes are not done synchronously"
2880
2881 test_43() {
2882         mkdir -p $DIR/$tdir
2883         cp -p /bin/ls $DIR/$tdir/$tfile
2884         $MULTIOP $DIR/$tdir/$tfile Ow_c &
2885         pid=$!
2886         # give multiop a chance to open
2887         sleep 1
2888
2889         $DIR/$tdir/$tfile && error || true
2890         kill -USR1 $pid
2891 }
2892 run_test 43 "execution of file opened for write should return -ETXTBSY"
2893
2894 test_43a() {
2895         mkdir -p $DIR/d43
2896         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2897         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2898         MULTIOP_PID=$!
2899         $MULTIOP $DIR/d43/multiop Oc && error "expected error, got success"
2900         kill -USR1 $MULTIOP_PID || return 2
2901         wait $MULTIOP_PID || return 3
2902         rm $TMP/test43.junk
2903 }
2904 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2905
2906 test_43b() {
2907         mkdir -p $DIR/d43
2908         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2909         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2910         MULTIOP_PID=$!
2911         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
2912         kill -USR1 $MULTIOP_PID || return 2
2913         wait $MULTIOP_PID || return 3
2914         rm $TMP/test43.junk
2915 }
2916 run_test 43b "truncate of file being executed should return -ETXTBSY"
2917
2918 test_43c() {
2919         local testdir="$DIR/d43c"
2920         mkdir -p $testdir
2921         cp $SHELL $testdir/
2922         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2923                 ( cd $testdir && md5sum -c)
2924 }
2925 run_test 43c "md5sum of copy into lustre========================"
2926
2927 test_44() {
2928         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2929         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2930         dd if=$DIR/f1 bs=4k count=1 > /dev/null
2931 }
2932 run_test 44 "zero length read from a sparse stripe ============="
2933
2934 test_44a() {
2935     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2936                          awk '{print $2}'`
2937     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2938     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2939     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2940                       awk '{print $2}'`
2941     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2942         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2943     fi
2944
2945     OFFSETS="0 $((stride/2)) $((stride-1))"
2946     for offset in $OFFSETS ; do
2947       for i in `seq 0 $((nstripe-1))`; do
2948         local GLOBALOFFSETS=""
2949         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2950         local myfn=$DIR/d44a-$size
2951         echo "--------writing $myfn at $size"
2952         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
2953         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2954         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2955                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2956
2957         for j in `seq 0 $((nstripe-1))`; do
2958             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2959             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
2960             GLOBALOFFSETS="$GLOBALOFFSETS $size"
2961         done
2962         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2963                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2964         rm -f $myfn
2965       done
2966     done
2967 }
2968 run_test 44a "test sparse pwrite ==============================="
2969
2970 dirty_osc_total() {
2971         tot=0
2972         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
2973                 tot=$(($tot + $d))
2974         done
2975         echo $tot
2976 }
2977 do_dirty_record() {
2978         before=`dirty_osc_total`
2979         echo executing "\"$*\""
2980         eval $*
2981         after=`dirty_osc_total`
2982         echo before $before, after $after
2983 }
2984 test_45() {
2985         f="$DIR/f45"
2986         # Obtain grants from OST if it supports it
2987         echo blah > ${f}_grant
2988         stop_writeback
2989         sync
2990         do_dirty_record "echo blah > $f"
2991         [ $before -eq $after ] && error "write wasn't cached"
2992         do_dirty_record "> $f"
2993         [ $before -gt $after ] || error "truncate didn't lower dirty count"
2994         do_dirty_record "echo blah > $f"
2995         [ $before -eq $after ] && error "write wasn't cached"
2996         do_dirty_record "sync"
2997         [ $before -gt $after ] || error "writeback didn't lower dirty count"
2998         do_dirty_record "echo blah > $f"
2999         [ $before -eq $after ] && error "write wasn't cached"
3000         do_dirty_record "cancel_lru_locks osc"
3001         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3002         start_writeback
3003 }
3004 run_test 45 "osc io page accounting ============================"
3005
3006 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3007 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3008 # objects offset and an assert hit when an rpc was built with 1023's mapped
3009 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3010 test_46() {
3011         f="$DIR/f46"
3012         stop_writeback
3013         sync
3014         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3015         sync
3016         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3017         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3018         sync
3019         start_writeback
3020 }
3021 run_test 46 "dirtying a previously written page ================"
3022
3023 # test_47 is removed "Device nodes check" is moved to test_28
3024
3025 test_48a() { # bug 2399
3026         check_kernel_version 34 || return 0
3027         mkdir -p $DIR/d48a
3028         cd $DIR/d48a
3029         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
3030         mkdir $DIR/d48a || error "recreate directory failed"
3031         touch foo || error "'touch foo' failed after recreating cwd"
3032         mkdir bar || error "'mkdir foo' failed after recreating cwd"
3033         if check_kernel_version 44; then
3034                 touch .foo || error "'touch .foo' failed after recreating cwd"
3035                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3036         fi
3037         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3038         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3039         cd . || error "'cd .' failed after recreating cwd"
3040         mkdir . && error "'mkdir .' worked after recreating cwd"
3041         rmdir . && error "'rmdir .' worked after recreating cwd"
3042         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3043         cd .. || error "'cd ..' failed after recreating cwd"
3044 }
3045 run_test 48a "Access renamed working dir (should return errors)="
3046
3047 test_48b() { # bug 2399
3048         check_kernel_version 34 || return 0
3049         mkdir -p $DIR/d48b
3050         cd $DIR/d48b
3051         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
3052         touch foo && error "'touch foo' worked after removing cwd"
3053         mkdir foo && error "'mkdir foo' worked after removing cwd"
3054         if check_kernel_version 44; then
3055                 touch .foo && error "'touch .foo' worked after removing cwd"
3056                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3057         fi
3058         ls . > /dev/null && error "'ls .' worked after removing cwd"
3059         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3060         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3061         mkdir . && error "'mkdir .' worked after removing cwd"
3062         rmdir . && error "'rmdir .' worked after removing cwd"
3063         ln -s . foo && error "'ln -s .' worked after removing cwd"
3064         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3065 }
3066 run_test 48b "Access removed working dir (should return errors)="
3067
3068 test_48c() { # bug 2350
3069         check_kernel_version 36 || return 0
3070         #lctl set_param debug=-1
3071         #set -vx
3072         mkdir -p $DIR/d48c/dir
3073         cd $DIR/d48c/dir
3074         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
3075         $TRACE touch foo && error "'touch foo' worked after removing cwd"
3076         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
3077         if check_kernel_version 44; then
3078                 touch .foo && error "'touch .foo' worked after removing cwd"
3079                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3080         fi
3081         $TRACE ls . && error "'ls .' worked after removing cwd"
3082         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3083         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
3084         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
3085         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3086         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3087         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3088 }
3089 run_test 48c "Access removed working subdir (should return errors)"
3090
3091 test_48d() { # bug 2350
3092         check_kernel_version 36 || return 0
3093         #lctl set_param debug=-1
3094         #set -vx
3095         mkdir -p $DIR/d48d/dir
3096         cd $DIR/d48d/dir
3097         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
3098         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
3099         $TRACE touch foo && error "'touch foo' worked after removing parent"
3100         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
3101         if check_kernel_version 44; then
3102                 touch .foo && error "'touch .foo' worked after removing parent"
3103                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
3104         fi
3105         $TRACE ls . && error "'ls .' worked after removing parent"
3106         $TRACE ls .. && error "'ls ..' worked after removing parent"
3107         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
3108         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
3109         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3110         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3111         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
3112 }
3113 run_test 48d "Access removed parent subdir (should return errors)"
3114
3115 test_48e() { # bug 4134
3116         check_kernel_version 41 || return 0
3117         #lctl set_param debug=-1
3118         #set -vx
3119         mkdir -p $DIR/d48e/dir
3120         cd $DIR/d48e/dir
3121         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3122         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3123         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3124         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3125         # On a buggy kernel addition of "touch foo" after cd .. will
3126         # produce kernel oops in lookup_hash_it
3127         touch ../foo && error "'cd ..' worked after recreate parent"
3128         cd $DIR
3129         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3130 }
3131 run_test 48e "Access to recreated parent subdir (should return errors)"
3132
3133 test_49() { # LU-1030
3134         # get ost1 size - lustre-OST0000
3135         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3136         # write 800M at maximum
3137         [ $ost1_size -gt 819200 ] && ost1_size=819200
3138
3139         lfs setstripe -c 1 -i 0 $DIR/$tfile
3140         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3141         local dd_pid=$!
3142
3143         # change max_pages_per_rpc while writing the file
3144         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3145         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3146         # loop until dd process exits
3147         while ps ax -opid | grep -wq $dd_pid; do
3148                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3149                 sleep $((RANDOM % 5 + 1))
3150         done
3151         # restore original max_pages_per_rpc
3152         $LCTL set_param $osc1_mppc=$orig_mppc
3153         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3154 }
3155 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3156
3157 test_50() {
3158         # bug 1485
3159         mkdir $DIR/d50
3160         cd $DIR/d50
3161         ls /proc/$$/cwd || error
3162 }
3163 run_test 50 "special situations: /proc symlinks  ==============="
3164
3165 test_51a() {    # was test_51
3166         # bug 1516 - create an empty entry right after ".." then split dir
3167         mkdir $DIR/d51
3168         touch $DIR/d51/foo
3169         $MCREATE $DIR/d51/bar
3170         rm $DIR/d51/foo
3171         createmany -m $DIR/d51/longfile 201
3172         FNUM=202
3173         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3174                 $MCREATE $DIR/d51/longfile$FNUM
3175                 FNUM=$(($FNUM + 1))
3176                 echo -n "+"
3177         done
3178         echo
3179         ls -l $DIR/d51 > /dev/null || error
3180 }
3181 run_test 51a "special situations: split htree with empty entry =="
3182
3183 export NUMTEST=70000
3184 test_51b() {
3185         local BASE=$DIR/$tdir
3186         mkdir -p $BASE
3187
3188         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3189         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3190         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3191                 return
3192
3193         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3194                 echo "reduced count to $NUMTEST due to inodes"
3195
3196         # need to check free space for the directories as well
3197         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3198         numfree=$((blkfree / 4))
3199         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3200                 echo "reduced count to $NUMTEST due to blocks"
3201
3202         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3203                 echo "failed" > $BASE/fnum
3204 }
3205 run_test 51b "exceed 64k subdirectory nlink limit"
3206
3207 test_51ba() { # LU-993
3208         local BASE=$DIR/$tdir
3209         # unlink all but 100 subdirectories, then check it still works
3210         local LEFT=100
3211         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3212
3213         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3214         local DELETE=$((NUMTEST - LEFT))
3215
3216         # continue on to run this test even if 51b didn't finish,
3217         # just to delete the many subdirectories created.
3218         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3219
3220         # for ldiskfs the nlink count should be 1, but this is OSD specific
3221         # and so this is listed for informational purposes only
3222         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3223         unlinkmany -d $BASE/d $DELETE
3224         RC=$?
3225
3226         if [ $RC -ne 0 ]; then
3227                 if [ "$NUMPREV" == "failed" ]; then
3228                         skip "previous setup failed"
3229                         return 0
3230                 else
3231                         error "unlink of first $DELETE subdirs failed"
3232                         return $RC
3233                 fi
3234         fi
3235
3236         echo "nlink between: $(stat -c %h $BASE)"
3237         # trim the first line of ls output
3238         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3239         [ $FOUND -ne $LEFT ] &&
3240                 error "can't find subdirs: found only $FOUND/$LEFT"
3241
3242         unlinkmany -d $BASE/d $DELETE $LEFT ||
3243                 error "unlink of second $LEFT subdirs failed"
3244         # regardless of whether the backing filesystem tracks nlink accurately
3245         # or not, the nlink count shouldn't be more than "." and ".." here
3246         local AFTER=$(stat -c %h $BASE)
3247         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3248                 echo "nlink after: $AFTER"
3249 }
3250 run_test 51ba "verify nlink for many subdirectory cleanup"
3251
3252 test_51bb() {
3253         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3254
3255         local ndirs=${TEST51BB_NDIRS:-10}
3256         local nfiles=${TEST51BB_NFILES:-100}
3257
3258         local numfree=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3259
3260         [ $numfree -lt $(( ndirs * nfiles)) ] && \
3261                 nfiles=$(( numfree / ndirs - 10 ))
3262
3263         local dir=$DIR/d51bb
3264         mkdir -p $dir
3265         local savePOLICY=$(lctl get_param -n lmv.*.placement)
3266         lctl set_param -n lmv.*.placement=CHAR
3267
3268         lfs df -i $dir
3269         local IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3270         OLDUSED=($IUSED)
3271
3272         declare -a dirs
3273         for ((i=0; i < $ndirs; i++)); do
3274                 dirs[i]=$dir/$RANDOM
3275                 echo Creating directory ${dirs[i]}
3276                 mkdir -p ${dirs[i]}
3277                 ls $dir
3278                 echo Creating $nfiles in dir ${dirs[i]} ...
3279                 echo "createmany -o ${dirs[i]}/$tfile- $nfiles"
3280                 createmany -o ${dirs[i]}/$tfile- $nfiles
3281         done
3282         ls $dir
3283
3284         sleep 1
3285
3286         IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3287         NEWUSED=($IUSED)
3288
3289         local rc=0
3290         for ((i=0; i<${#NEWUSED[@]}; i++)); do
3291                 echo "mds $i: inodes count OLD ${OLDUSED[$i]} NEW ${NEWUSED[$i]}"
3292                 [ ${OLDUSED[$i]} -lt ${NEWUSED[$i]} ] || rc=$((rc + 1))
3293         done
3294
3295         lctl set_param -n lmv.*.placement=$savePOLICY
3296
3297         [ $rc -ne $MDSCOUNT ] || \
3298                 error "Objects/inodes are not distributed over all mds servers"
3299 }
3300 run_test 51bb "mkdir createmany CMD $MDSCOUNT  ===================="
3301
3302 test_51d() {
3303         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3304         mkdir -p $DIR/d51d
3305         createmany -o $DIR/d51d/t- 1000
3306         $GETSTRIPE $DIR/d51d > $TMP/files
3307         for N in `seq 0 $((OSTCOUNT - 1))`; do
3308             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3309             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3310             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3311         done
3312         unlinkmany $DIR/d51d/t- 1000
3313
3314         NLAST=0
3315         for N in `seq 1 $((OSTCOUNT - 1))`; do
3316             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3317                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3318             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3319                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3320
3321             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3322                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3323             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3324                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3325             NLAST=$N
3326         done
3327 }
3328 run_test 51d "check object distribution ===================="
3329
3330 test_52a() {
3331         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3332         mkdir -p $DIR/d52a
3333         touch $DIR/d52a/foo
3334         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3335         echo bar >> $DIR/d52a/foo || error "append bar failed"
3336         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3337         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3338         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3339         echo foo >> $DIR/d52a/foo || error "append foo failed"
3340         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3341         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3342         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3343         cp -r $DIR/d52a /tmp/
3344         rm -fr $DIR/d52a || error "cleanup rm failed"
3345 }
3346 run_test 52a "append-only flag test (should return errors) ====="
3347
3348 test_52b() {
3349         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3350         mkdir -p $DIR/d52b
3351         touch $DIR/d52b/foo
3352         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3353         cat test > $DIR/d52b/foo && error "cat test worked"
3354         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3355         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3356         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3357         echo foo >> $DIR/d52b/foo && error "echo worked"
3358         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3359         [ -f $DIR/d52b/foo ] || error
3360         [ -f $DIR/d52b/foo_ren ] && error
3361         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3362         chattr -i $DIR/d52b/foo || error "chattr failed"
3363
3364         rm -fr $DIR/d52b || error
3365 }
3366 run_test 52b "immutable flag test (should return errors) ======="
3367
3368 test_53() {
3369         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3370         remote_ost_nodsh && skip "remote OST with nodsh" && return
3371
3372         local param
3373         local ostname
3374         local mds_last
3375         local ost_last
3376         local ostnum
3377
3378         # only test MDT0000
3379         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3380         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3381                 param=`echo ${value[0]} | cut -d "=" -f1`
3382                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3383                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3384                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3385                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3386                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3387                 if [ $ost_last != $mds_last ]; then
3388                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3389                 fi
3390         done
3391 }
3392 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3393
3394 test_54a() {
3395         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3396         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3397         $SOCKETSERVER $DIR/socket
3398         $SOCKETCLIENT $DIR/socket || error
3399         $MUNLINK $DIR/socket
3400 }
3401 run_test 54a "unix domain socket test =========================="
3402
3403 test_54b() {
3404         f="$DIR/f54b"
3405         mknod $f c 1 3
3406         chmod 0666 $f
3407         dd if=/dev/zero of=$f bs=`page_size` count=1
3408 }
3409 run_test 54b "char device works in lustre ======================"
3410
3411 find_loop_dev() {
3412         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3413         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3414         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3415
3416         for i in `seq 3 7`; do
3417                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3418                 LOOPDEV=$LOOPBASE$i
3419                 LOOPNUM=$i
3420                 break
3421         done
3422 }
3423
3424 test_54c() {
3425         tfile="$DIR/f54c"
3426         tdir="$DIR/d54c"
3427         loopdev="$DIR/loop54c"
3428
3429         find_loop_dev
3430         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3431         mknod $loopdev b 7 $LOOPNUM
3432         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3433         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3434         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3435         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3436         mkdir -p $tdir
3437         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3438         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3439         df $tdir
3440         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3441         $UMOUNT $tdir
3442         losetup -d $loopdev
3443         rm $loopdev
3444 }
3445 run_test 54c "block device works in lustre ====================="
3446
3447 test_54d() {
3448         f="$DIR/f54d"
3449         string="aaaaaa"
3450         mknod $f p
3451         [ "$string" = `echo $string > $f | cat $f` ] || error
3452 }
3453 run_test 54d "fifo device works in lustre ======================"
3454
3455 test_54e() {
3456         check_kernel_version 46 || return 0
3457         f="$DIR/f54e"
3458         string="aaaaaa"
3459         cp -aL /dev/console $f
3460         echo $string > $f || error
3461 }
3462 run_test 54e "console/tty device works in lustre ======================"
3463
3464 #The test_55 used to be iopen test and it was removed by bz#24037.
3465 #run_test 55 "check iopen_connect_dentry() ======================"
3466
3467 test_56a() {    # was test_56
3468         rm -rf $DIR/d56
3469         $SETSTRIPE -d $DIR
3470         mkdir $DIR/d56
3471         mkdir $DIR/d56/dir
3472         NUMFILES=3
3473         NUMFILESx2=$(($NUMFILES * 2))
3474         for i in `seq 1 $NUMFILES` ; do
3475                 touch $DIR/d56/file$i
3476                 touch $DIR/d56/dir/file$i
3477         done
3478
3479         # test lfs getstripe with --recursive
3480         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3481         [ $FILENUM -eq $NUMFILESx2 ] ||
3482                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3483         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3484         [ $FILENUM -eq $NUMFILES ] ||
3485                 error "$GETSTRIPE $DIR/d56: found $FILENUM, not $NUMFILES"
3486         echo "$GETSTRIPE --recursive passed."
3487
3488         # test lfs getstripe with file instead of dir
3489         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3490         [ $FILENUM  -eq 1 ] || error \
3491                  "$GETSTRIPE $DIR/d56/file1: found $FILENUM, not 1"
3492         echo "$GETSTRIPE file1 passed."
3493
3494         #test lfs getstripe with --verbose
3495         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||
3496                 error "$GETSTRIPE --verbose $DIR/d56: want $NUMFILES lmm_magic"
3497         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] ||
3498             error "$GETSTRIPE $DIR/d56: showed lmm_magic"
3499         echo "$GETSTRIPE --verbose passed."
3500
3501         #test lfs getstripe with --obd
3502         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" ||
3503                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3504
3505         [  "$OSTCOUNT" -lt 2 ] &&
3506                 skip_env "skipping other $GETSTRIPE --obd test" && return
3507
3508         OSTIDX=1
3509         OBDUUID=$(ostuuid_from_index $OSTIDX)
3510         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3511         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3512         [ $FOUND -eq $FILENUM ] ||
3513                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3514         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 |
3515                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3516                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3517                 error "$GETSTRIPE --obd: should not show file on other obd"
3518         echo "$GETSTRIPE --obd passed"
3519 }
3520 run_test 56a "check $GETSTRIPE"
3521
3522 NUMFILES=3
3523 NUMDIRS=3
3524 setup_56() {
3525         local LOCAL_NUMFILES="$1"
3526         local LOCAL_NUMDIRS="$2"
3527         local MKDIR_PARAMS="$3"
3528
3529         if [ ! -d "$TDIR" ] ; then
3530                 mkdir -p $TDIR
3531                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3532                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3533                         touch $TDIR/file$i
3534                 done
3535                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3536                         mkdir $TDIR/dir$i
3537                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3538                                 touch $TDIR/dir$i/file$j
3539                         done
3540                 done
3541         fi
3542 }
3543
3544 setup_56_special() {
3545         LOCAL_NUMFILES=$1
3546         LOCAL_NUMDIRS=$2
3547         setup_56 $1 $2
3548         if [ ! -e "$TDIR/loop1b" ] ; then
3549                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3550                         mknod $TDIR/loop${i}b b 7 $i
3551                         mknod $TDIR/null${i}c c 1 3
3552                         ln -s $TDIR/file1 $TDIR/link${i}l
3553                 done
3554                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3555                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3556                         mknod $TDIR/dir$i/null${i}c c 1 3
3557                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3558                 done
3559         fi
3560 }
3561
3562 test_56g() {
3563         $SETSTRIPE -d $DIR
3564
3565         TDIR=$DIR/${tdir}g
3566         setup_56 $NUMFILES $NUMDIRS
3567
3568         EXPECTED=$(($NUMDIRS + 2))
3569         # test lfs find with -name
3570         for i in $(seq 1 $NUMFILES) ; do
3571                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3572                 [ $NUMS -eq $EXPECTED ] ||
3573                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3574                               "found $NUMS, expected $EXPECTED"
3575         done
3576 }
3577 run_test 56g "check lfs find -name ============================="
3578
3579 test_56h() {
3580         $SETSTRIPE -d $DIR
3581
3582         TDIR=$DIR/${tdir}g
3583         setup_56 $NUMFILES $NUMDIRS
3584
3585         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3586         # test lfs find with ! -name
3587         for i in $(seq 1 $NUMFILES) ; do
3588                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3589                 [ $NUMS -eq $EXPECTED ] ||
3590                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3591                               "found $NUMS, expected $EXPECTED"
3592         done
3593 }
3594 run_test 56h "check lfs find ! -name ============================="
3595
3596 test_56i() {
3597        tdir=${tdir}i
3598        mkdir -p $DIR/$tdir
3599        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3600        CMD="$LFIND -ost $UUID $DIR/$tdir"
3601        OUT=$($CMD)
3602        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3603 }
3604 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3605
3606 test_56j() {
3607         TDIR=$DIR/${tdir}g
3608         setup_56_special $NUMFILES $NUMDIRS
3609
3610         EXPECTED=$((NUMDIRS + 1))
3611         CMD="$LFIND -type d $TDIR"
3612         NUMS=$($CMD | wc -l)
3613         [ $NUMS -eq $EXPECTED ] ||
3614                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3615 }
3616 run_test 56j "check lfs find -type d ============================="
3617
3618 test_56k() {
3619         TDIR=$DIR/${tdir}g
3620         setup_56_special $NUMFILES $NUMDIRS
3621
3622         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3623         CMD="$LFIND -type f $TDIR"
3624         NUMS=$($CMD | wc -l)
3625         [ $NUMS -eq $EXPECTED ] ||
3626                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3627 }
3628 run_test 56k "check lfs find -type f ============================="
3629
3630 test_56l() {
3631         TDIR=$DIR/${tdir}g
3632         setup_56_special $NUMFILES $NUMDIRS
3633
3634         EXPECTED=$((NUMDIRS + NUMFILES))
3635         CMD="$LFIND -type b $TDIR"
3636         NUMS=$($CMD | wc -l)
3637         [ $NUMS -eq $EXPECTED ] ||
3638                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3639 }
3640 run_test 56l "check lfs find -type b ============================="
3641
3642 test_56m() {
3643         TDIR=$DIR/${tdir}g
3644         setup_56_special $NUMFILES $NUMDIRS
3645
3646         EXPECTED=$((NUMDIRS + NUMFILES))
3647         CMD="$LFIND -type c $TDIR"
3648         NUMS=$($CMD | wc -l)
3649         [ $NUMS -eq $EXPECTED ] ||
3650                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3651 }
3652 run_test 56m "check lfs find -type c ============================="
3653
3654 test_56n() {
3655         TDIR=$DIR/${tdir}g
3656         setup_56_special $NUMFILES $NUMDIRS
3657
3658         EXPECTED=$((NUMDIRS + NUMFILES))
3659         CMD="$LFIND -type l $TDIR"
3660         NUMS=$($CMD | wc -l)
3661         [ $NUMS -eq $EXPECTED ] ||
3662                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3663 }
3664 run_test 56n "check lfs find -type l ============================="
3665
3666 test_56o() {
3667         TDIR=$DIR/${tdir}o
3668         setup_56 $NUMFILES $NUMDIRS
3669
3670         utime $TDIR/file1 > /dev/null || error "utime (1)"
3671         utime $TDIR/file2 > /dev/null || error "utime (2)"
3672         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3673         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3674         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3675         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3676
3677         EXPECTED=4
3678         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3679         [ $NUMS -eq $EXPECTED ] || \
3680                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3681
3682         EXPECTED=12
3683         CMD="$LFIND -mtime 0 $TDIR"
3684         NUMS=$($CMD | wc -l)
3685         [ $NUMS -eq $EXPECTED ] ||
3686                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3687 }
3688 run_test 56o "check lfs find -mtime for old files =========================="
3689
3690 test_56p() {
3691         [ $RUNAS_ID -eq $UID ] &&
3692                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3693
3694         TDIR=$DIR/${tdir}p
3695         setup_56 $NUMFILES $NUMDIRS
3696
3697         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3698         EXPECTED=$NUMFILES
3699         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3700         NUMS=$($CMD | wc -l)
3701         [ $NUMS -eq $EXPECTED ] || \
3702                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3703
3704         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
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 run_test 56p "check lfs find -uid and ! -uid ==============================="
3711
3712 test_56q() {
3713         [ $RUNAS_ID -eq $UID ] &&
3714                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3715
3716         TDIR=$DIR/${tdir}q
3717         setup_56 $NUMFILES $NUMDIRS
3718
3719         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3720
3721         EXPECTED=$NUMFILES
3722         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3723         NUMS=$($CMD | wc -l)
3724         [ $NUMS -eq $EXPECTED ] ||
3725                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3726
3727         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
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 run_test 56q "check lfs find -gid and ! -gid ==============================="
3734
3735 test_56r() {
3736         TDIR=$DIR/${tdir}r
3737         setup_56 $NUMFILES $NUMDIRS
3738
3739         EXPECTED=12
3740         CMD="$LFIND -size 0 -type f $TDIR"
3741         NUMS=$($CMD | wc -l)
3742         [ $NUMS -eq $EXPECTED ] ||
3743                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3744         EXPECTED=0
3745         CMD="$LFIND ! -size 0 -type f $TDIR"
3746         NUMS=$($CMD | wc -l)
3747         [ $NUMS -eq $EXPECTED ] ||
3748                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3749         echo "test" > $TDIR/$tfile
3750         echo "test2" > $TDIR/$tfile.2 && sync
3751         EXPECTED=1
3752         CMD="$LFIND -size 5 -type f $TDIR"
3753         NUMS=$($CMD | wc -l)
3754         [ $NUMS -eq $EXPECTED ] ||
3755                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3756         EXPECTED=1
3757         CMD="$LFIND -size +5 -type f $TDIR"
3758         NUMS=$($CMD | wc -l)
3759         [ $NUMS -eq $EXPECTED ] ||
3760                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3761         EXPECTED=2
3762         CMD="$LFIND -size +0 -type f $TDIR"
3763         NUMS=$($CMD | wc -l)
3764         [ $NUMS -eq $EXPECTED ] ||
3765                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3766         EXPECTED=2
3767         CMD="$LFIND ! -size -5 -type f $TDIR"
3768         NUMS=$($CMD | wc -l)
3769         [ $NUMS -eq $EXPECTED ] ||
3770                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3771         EXPECTED=12
3772         CMD="$LFIND -size -5 -type f $TDIR"
3773         NUMS=$($CMD | wc -l)
3774         [ $NUMS -eq $EXPECTED ] ||
3775                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3776 }
3777 run_test 56r "check lfs find -size works =========================="
3778
3779 test_56s() { # LU-611
3780         TDIR=$DIR/${tdir}s
3781         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3782
3783         if [ $OSTCOUNT -gt 1 ]; then
3784                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3785                 ONESTRIPE=4
3786                 EXTRA=4
3787         else
3788                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3789                 EXTRA=0
3790         fi
3791
3792         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3793         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3794         NUMS=$($CMD | wc -l)
3795         [ $NUMS -eq $EXPECTED ] ||
3796                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3797
3798         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
3799         CMD="$LFIND -stripe-count +0 -type f $TDIR"
3800         NUMS=$($CMD | wc -l)
3801         [ $NUMS -eq $EXPECTED ] ||
3802                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3803
3804         EXPECTED=$ONESTRIPE
3805         CMD="$LFIND -stripe-count 1 -type f $TDIR"
3806         NUMS=$($CMD | wc -l)
3807         [ $NUMS -eq $EXPECTED ] ||
3808                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3809
3810         CMD="$LFIND -stripe-count -2 -type f $TDIR"
3811         NUMS=$($CMD | wc -l)
3812         [ $NUMS -eq $EXPECTED ] ||
3813                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3814
3815         EXPECTED=0
3816         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
3817         NUMS=$($CMD | wc -l)
3818         [ $NUMS -eq $EXPECTED ] ||
3819                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3820 }
3821 run_test 56s "check lfs find -stripe-count works"
3822
3823 test_56t() { # LU-611
3824         TDIR=$DIR/${tdir}t
3825         setup_56 $NUMFILES $NUMDIRS "-s 512k"
3826
3827         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
3828
3829         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3830         CMD="$LFIND -stripe-size 512k -type f $TDIR"
3831         NUMS=$($CMD | wc -l)
3832         [ $NUMS -eq $EXPECTED ] ||
3833                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3834
3835         CMD="$LFIND -stripe-size +320k -type f $TDIR"
3836         NUMS=$($CMD | wc -l)
3837         [ $NUMS -eq $EXPECTED ] ||
3838                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3839
3840         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
3841         CMD="$LFIND -stripe-size +200k -type f $TDIR"
3842         NUMS=$($CMD | wc -l)
3843         [ $NUMS -eq $EXPECTED ] ||
3844                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3845
3846         CMD="$LFIND -stripe-size -640k -type f $TDIR"
3847         NUMS=$($CMD | wc -l)
3848         [ $NUMS -eq $EXPECTED ] ||
3849                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3850
3851         EXPECTED=4
3852         CMD="$LFIND -stripe-size 256k -type f $TDIR"
3853         NUMS=$($CMD | wc -l)
3854         [ $NUMS -eq $EXPECTED ] ||
3855                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3856
3857         CMD="$LFIND -stripe-size -320k -type f $TDIR"
3858         NUMS=$($CMD | wc -l)
3859         [ $NUMS -eq $EXPECTED ] ||
3860                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3861
3862         EXPECTED=0
3863         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
3864         NUMS=$($CMD | wc -l)
3865         [ $NUMS -eq $EXPECTED ] ||
3866                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3867 }
3868 run_test 56t "check lfs find -stripe-size works"
3869
3870 test_56u() { # LU-611
3871         TDIR=$DIR/${tdir}u
3872         setup_56 $NUMFILES $NUMDIRS "-i 0"
3873
3874         if [ $OSTCOUNT -gt 1 ]; then
3875                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
3876                 ONESTRIPE=4
3877         else
3878                 ONESTRIPE=0
3879         fi
3880
3881         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3882         CMD="$LFIND -stripe-index 0 -type f $TDIR"
3883         NUMS=$($CMD | wc -l)
3884         [ $NUMS -eq $EXPECTED ] ||
3885                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3886
3887         EXPECTED=$ONESTRIPE
3888         CMD="$LFIND -stripe-index 1 -type f $TDIR"
3889         NUMS=$($CMD | wc -l)
3890         [ $NUMS -eq $EXPECTED ] ||
3891                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3892
3893         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
3894         NUMS=$($CMD | wc -l)
3895         [ $NUMS -eq $EXPECTED ] ||
3896                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3897
3898         EXPECTED=0
3899         # This should produce an error and not return any files
3900         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
3901         NUMS=$($CMD 2>/dev/null | wc -l)
3902         [ $NUMS -eq $EXPECTED ] ||
3903                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3904
3905         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
3906         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
3907         NUMS=$($CMD | wc -l)
3908         [ $NUMS -eq $EXPECTED ] ||
3909                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3910 }
3911 run_test 56u "check lfs find -stripe-index works"
3912
3913 test_56v() {
3914     local MDT_IDX=0
3915
3916     TDIR=$DIR/${tdir}v
3917     rm -rf $TDIR
3918     setup_56 $NUMFILES $NUMDIRS
3919
3920     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
3921     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
3922
3923     for file in $($LFIND -mdt $UUID $TDIR); do
3924         file_mdt_idx=$($GETSTRIPE -M $file)
3925         [ $file_mdt_idx -eq $MDT_IDX ] ||
3926             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
3927     done
3928 }
3929 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
3930
3931 # Get and check the actual stripe count of one file.
3932 # Usage: check_stripe_count <file> <expected_stripe_count>
3933 check_stripe_count() {
3934     local file=$1
3935     local expected=$2
3936     local actual
3937
3938     [[ -z "$file" || -z "$expected" ]] &&
3939         error "check_stripe_count: invalid argument!"
3940
3941     local cmd="$GETSTRIPE -c $file"
3942     actual=$($cmd) || error "$cmd failed"
3943     actual=${actual%% *}
3944
3945     if [[ $actual -ne $expected ]]; then
3946         [[ $expected -eq -1 ]] ||
3947             error "$cmd wrong: found $actual, expected $expected"
3948         [[ $actual -eq $OSTCOUNT ]] ||
3949             error "$cmd wrong: found $actual, expected $OSTCOUNT"
3950     fi
3951 }
3952
3953 test_56w() {
3954     TDIR=$DIR/${tdir}w
3955
3956     rm -rf $TDIR || error "remove $TDIR failed"
3957     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3958
3959     local stripe_size
3960     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
3961         error "$GETSTRIPE -S -d $TDIR failed"
3962     stripe_size=${stripe_size%% *}
3963
3964     local file_size=$((stripe_size * OSTCOUNT))
3965     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
3966     local required_space=$((file_num * file_size))
3967     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
3968     [[ $free_space -le $((required_space / 1024)) ]] &&
3969         skip_env "need at least $required_space bytes free space," \
3970                  "have $free_space kbytes" && return
3971
3972     local dd_bs=65536
3973     local dd_count=$((file_size / dd_bs))
3974
3975     # write data into the files
3976     local i
3977     local j
3978     local file
3979     for i in $(seq 1 $NUMFILES); do
3980         file=$TDIR/file$i
3981         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
3982             error "write data into $file failed"
3983     done
3984     for i in $(seq 1 $NUMDIRS); do
3985         for j in $(seq 1 $NUMFILES); do
3986             file=$TDIR/dir$i/file$j
3987             yes | dd bs=$dd_bs count=$dd_count of=$file \
3988                 >/dev/null 2>&1 ||
3989                 error "write data into $file failed"
3990         done
3991     done
3992
3993     local expected=-1
3994     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
3995
3996     # lfs_migrate file
3997     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
3998     echo "$cmd"
3999     eval $cmd || error "$cmd failed"
4000
4001     check_stripe_count $TDIR/file1 $expected
4002
4003     # lfs_migrate dir
4004     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4005     echo "$cmd"
4006     eval $cmd || error "$cmd failed"
4007
4008     for j in $(seq 1 $NUMFILES); do
4009         check_stripe_count $TDIR/dir1/file$j $expected
4010     done
4011
4012     # lfs_migrate works with lfs find
4013     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4014          $LFS_MIGRATE -y -c $expected"
4015     echo "$cmd"
4016     eval $cmd || error "$cmd failed"
4017
4018     for i in $(seq 2 $NUMFILES); do
4019         check_stripe_count $TDIR/file$i $expected
4020     done
4021     for i in $(seq 2 $NUMDIRS); do
4022         for j in $(seq 1 $NUMFILES); do
4023             check_stripe_count $TDIR/dir$i/file$j $expected
4024         done
4025     done
4026 }
4027 run_test 56w "check lfs_migrate -c stripe_count works"
4028
4029 test_57a() {
4030         # note test will not do anything if MDS is not local
4031         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4032                 skip "Only applicable to ldiskfs-based MDTs"
4033                 return
4034         fi
4035
4036         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4037         local MNTDEV="osd*.*MDT*.mntdev"
4038         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4039         [ -z "$DEV" ] && error "can't access $MNTDEV"
4040         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4041                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4042                         error "can't access $DEV"
4043                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4044                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4045                 rm $TMP/t57a.dump
4046         done
4047 }
4048 run_test 57a "verify MDS filesystem created with large inodes =="
4049
4050 test_57b() {
4051         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4052                 skip "Only applicable to ldiskfs-based MDTs"
4053                 return
4054         fi
4055
4056         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4057         local dir=$DIR/d57b
4058
4059         local FILECOUNT=100
4060         local FILE1=$dir/f1
4061         local FILEN=$dir/f$FILECOUNT
4062
4063         rm -rf $dir || error "removing $dir"
4064         mkdir -p $dir || error "creating $dir"
4065         local num=$(get_mds_dir $dir)
4066         local mymds=mds$num
4067
4068         echo "mcreating $FILECOUNT files"
4069         createmany -m $dir/f 1 $FILECOUNT || \
4070                 error "creating files in $dir"
4071
4072         # verify that files do not have EAs yet
4073         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4074         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4075
4076         sync
4077         sleep 1
4078         df $dir  #make sure we get new statfs data
4079         local MDSFREE=$(do_facet $mymds \
4080                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4081         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4082         echo "opening files to create objects/EAs"
4083         local FILE
4084         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4085                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4086         done
4087
4088         # verify that files have EAs now
4089         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4090         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4091
4092         sleep 1  #make sure we get new statfs data
4093         df $dir
4094         local MDSFREE2=$(do_facet $mymds \
4095                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4096         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4097         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4098                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4099                         error "MDC before $MDCFREE != after $MDCFREE2"
4100                 else
4101                         echo "MDC before $MDCFREE != after $MDCFREE2"
4102                         echo "unable to confirm if MDS has large inodes"
4103                 fi
4104         fi
4105         rm -rf $dir
4106 }
4107 run_test 57b "default LOV EAs are stored inside large inodes ==="
4108
4109 test_58() {
4110     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
4111     wiretest
4112 }
4113 run_test 58 "verify cross-platform wire constants =============="
4114
4115 test_59() {
4116         echo "touch 130 files"
4117         createmany -o $DIR/f59- 130
4118         echo "rm 130 files"
4119         unlinkmany $DIR/f59- 130
4120         sync
4121         sleep 2
4122         # wait for commitment of removal
4123 }
4124 run_test 59 "verify cancellation of llog records async ========="
4125
4126 TEST60_HEAD="test_60 run $RANDOM"
4127 test_60a() {
4128         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4129         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4130         log "$TEST60_HEAD - from kernel mode"
4131         do_facet mgs sh run-llog.sh
4132 }
4133 run_test 60a "llog sanity tests run from kernel module =========="
4134
4135 test_60b() { # bug 6411
4136         dmesg > $DIR/$tfile
4137         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4138                                  /llog.test/ {
4139                                          if (marker)
4140                                                  from_marker++
4141                                          from_begin++
4142                                  }
4143                                  END {
4144                                          if (marker)
4145                                                  print from_marker
4146                                          else
4147                                                  print from_begin
4148                                  }"`
4149         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4150 }
4151 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4152
4153 test_60c() {
4154         echo "create 5000 files"
4155         createmany -o $DIR/f60c- 5000
4156 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4157         lctl set_param fail_loc=0x80000137
4158         unlinkmany $DIR/f60c- 5000
4159         lctl set_param fail_loc=0
4160 }
4161 run_test 60c "unlink file when mds full"
4162
4163 test_60d() {
4164         SAVEPRINTK=$(lctl get_param -n printk)
4165
4166         # verify "lctl mark" is even working"
4167         MESSAGE="test message ID $RANDOM $$"
4168         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4169         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4170
4171         lctl set_param printk=0 || error "set lnet.printk failed"
4172         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4173         MESSAGE="new test message ID $RANDOM $$"
4174         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4175         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4176         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4177
4178         lctl set_param -n printk="$SAVEPRINTK"
4179 }
4180 run_test 60d "test printk console message masking"
4181
4182 test_61() {
4183         f="$DIR/f61"
4184         dd if=/dev/zero of=$f bs=`page_size` count=1
4185         cancel_lru_locks osc
4186         $MULTIOP $f OSMWUc || error
4187         sync
4188 }
4189 run_test 61 "mmap() writes don't make sync hang ================"
4190
4191 # bug 2330 - insufficient obd_match error checking causes LBUG
4192 test_62() {
4193         f="$DIR/f62"
4194         echo foo > $f
4195         cancel_lru_locks osc
4196         lctl set_param fail_loc=0x405
4197         cat $f && error "cat succeeded, expect -EIO"
4198         lctl set_param fail_loc=0
4199 }
4200 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4201 # match every page all of the time.
4202 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4203
4204 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4205 test_63a() {    # was test_63
4206         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4207         lctl set_param -n osc.*.max_dirty_mb 0
4208         for i in `seq 10` ; do
4209                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4210                 sleep 5
4211                 kill $!
4212                 sleep 1
4213         done
4214
4215         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4216         rm -f $DIR/f63 || true
4217 }
4218 run_test 63a "Verify oig_wait interruption does not crash ======="
4219
4220 # bug 2248 - async write errors didn't return to application on sync
4221 # bug 3677 - async write errors left page locked
4222 test_63b() {
4223         debugsave
4224         lctl set_param debug=-1
4225
4226         # ensure we have a grant to do async writes
4227         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4228         rm $DIR/$tfile
4229
4230         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4231         lctl set_param fail_loc=0x80000406
4232         $MULTIOP $DIR/$tfile Owy && \
4233                 error "sync didn't return ENOMEM"
4234         sync; sleep 2; sync     # do a real sync this time to flush page
4235         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4236                 error "locked page left in cache after async error" || true
4237         debugrestore
4238 }
4239 run_test 63b "async write errors should be returned to fsync ==="
4240
4241 test_64a () {
4242         df $DIR
4243         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4244 }
4245 run_test 64a "verify filter grant calculations (in kernel) ====="
4246
4247 test_64b () {
4248         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4249         sh oos.sh $MOUNT
4250 }
4251 run_test 64b "check out-of-space detection on client ==========="
4252
4253 # bug 1414 - set/get directories' stripe info
4254 test_65a() {
4255         mkdir -p $DIR/d65
4256         touch $DIR/d65/f1
4257         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
4258 }
4259 run_test 65a "directory with no stripe info ===================="
4260
4261 test_65b() {
4262         mkdir -p $DIR/d65
4263         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65||error "setstripe"
4264         touch $DIR/d65/f2
4265         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
4266 }
4267 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4268
4269 test_65c() {
4270         if [ $OSTCOUNT -gt 1 ]; then
4271                 mkdir -p $DIR/d65
4272                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4273                         -c $(($OSTCOUNT - 1)) $DIR/d65 || error "setstripe"
4274                 touch $DIR/d65/f3
4275                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
4276         fi
4277 }
4278 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4279
4280 test_65d() {
4281         mkdir -p $DIR/d65
4282         if [ $STRIPECOUNT -le 0 ]; then
4283                 sc=1
4284         elif [ $STRIPECOUNT -gt 2000 ]; then
4285 #LOV_MAX_STRIPE_COUNT is 2000
4286                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4287         else
4288                 sc=$(($STRIPECOUNT - 1))
4289         fi
4290         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/d65 || error "setstripe"
4291         touch $DIR/d65/f4 $DIR/d65/f5
4292         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
4293 }
4294 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4295
4296 test_65e() {
4297         mkdir -p $DIR/d65
4298
4299         $SETSTRIPE $DIR/d65 || error "setstripe"
4300         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
4301         touch $DIR/d65/f6
4302         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
4303 }
4304 run_test 65e "directory setstripe defaults ======================="
4305
4306 test_65f() {
4307         mkdir -p $DIR/d65f
4308         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
4309 }
4310 run_test 65f "dir setstripe permission (should return error) ==="
4311
4312 test_65g() {
4313         mkdir -p $DIR/d65
4314         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4315         $SETSTRIPE -d $DIR/d65 || error "setstripe"
4316         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
4317                 error "delete default stripe failed"
4318 }
4319 run_test 65g "directory setstripe -d ==========================="
4320
4321 test_65h() {
4322         mkdir -p $DIR/d65
4323         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4324         mkdir -p $DIR/d65/dd1
4325         [ $($GETSTRIPE -c $DIR/d65) == $($GETSTRIPE -c $DIR/d65/dd1) ] ||
4326                 error "stripe info inherit failed"
4327 }
4328 run_test 65h "directory stripe info inherit ===================="
4329
4330 test_65i() { # bug6367
4331         $SETSTRIPE -S 65536 -c -1 $MOUNT
4332 }
4333 run_test 65i "set non-default striping on root directory (bug 6367)="
4334
4335 test_65ia() { # bug12836
4336         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4337 }
4338 run_test 65ia "getstripe on -1 default directory striping"
4339
4340 test_65ib() { # bug12836
4341         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4342 }
4343 run_test 65ib "getstripe -v on -1 default directory striping"
4344
4345 test_65ic() { # bug12836
4346         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4347 }
4348 run_test 65ic "new find on -1 default directory striping"
4349
4350 test_65j() { # bug6367
4351         sync; sleep 1
4352         # if we aren't already remounting for each test, do so for this test
4353         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4354                 cleanup || error "failed to unmount"
4355                 setup
4356         fi
4357         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4358 }
4359 run_test 65j "set default striping on root directory (bug 6367)="
4360
4361 test_65k() { # bug11679
4362     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4363     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4364
4365     echo "Check OST status: "
4366     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4367               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4368
4369     for OSC in $MDS_OSCS; do
4370         echo $OSC "is activate"
4371         do_facet $SINGLEMDS lctl --device %$OSC activate
4372     done
4373
4374     do_facet client mkdir -p $DIR/$tdir
4375     for INACTIVE_OSC in $MDS_OSCS; do
4376         echo "Deactivate: " $INACTIVE_OSC
4377         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4378         for STRIPE_OSC in $MDS_OSCS; do
4379             OST=`osc_to_ost $STRIPE_OSC`
4380             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4381                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4382
4383             [ -f $DIR/$tdir/$IDX ] && continue
4384             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4385             do_facet client $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4386             RC=$?
4387             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4388         done
4389         do_facet client rm -f $DIR/$tdir/*
4390         echo $INACTIVE_OSC "is Activate."
4391         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4392     done
4393 }
4394 run_test 65k "validate manual striping works properly with deactivated OSCs"
4395
4396 test_65l() { # bug 12836
4397         mkdir -p $DIR/$tdir/test_dir
4398         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4399         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4400 }
4401 run_test 65l "lfs find on -1 stripe dir ========================"
4402
4403 # bug 2543 - update blocks count on client
4404 test_66() {
4405         COUNT=${COUNT:-8}
4406         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4407         sync; sync_all_data; sync; sync_all_data
4408         cancel_lru_locks osc
4409         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4410         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4411 }
4412 run_test 66 "update inode blocks count on client ==============="
4413
4414 LLOOP=
4415 LLITELOOPLOAD=
4416 cleanup_68() {
4417         trap 0
4418         if [ ! -z "$LLOOP" ]; then
4419                 if swapon -s | grep -q $LLOOP; then
4420                         swapoff $LLOOP || error "swapoff failed"
4421                 fi
4422
4423                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4424                 rm -f $LLOOP
4425                 unset LLOOP
4426         fi
4427         if [ ! -z "$LLITELOOPLOAD" ]; then
4428                 rmmod llite_lloop
4429                 unset LLITELOOPLOAD
4430         fi
4431         rm -f $DIR/f68*
4432 }
4433
4434 meminfo() {
4435         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4436 }
4437
4438 swap_used() {
4439         swapon -s | awk '($1 == "'$1'") { print $4 }'
4440 }
4441
4442 # test case for lloop driver, basic function
4443 test_68a() {
4444         [ "$UID" != 0 ] && skip_env "must run as root" && return
4445         llite_lloop_enabled || \
4446                 { skip_env "llite_lloop module disabled" && return; }
4447
4448         trap cleanup_68 EXIT
4449
4450         if ! module_loaded llite_lloop; then
4451                 if load_module llite/llite_lloop; then
4452                         LLITELOOPLOAD=yes
4453                 else
4454                         skip_env "can't find module llite_lloop"
4455                         return
4456                 fi
4457         fi
4458
4459         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4460         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4461         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4462
4463         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4464         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4465
4466         cleanup_68
4467 }
4468 run_test 68a "lloop driver - basic test ========================"
4469
4470 # excercise swapping to lustre by adding a high priority swapfile entry
4471 # and then consuming memory until it is used.
4472 test_68b() {  # was test_68
4473         [ "$UID" != 0 ] && skip_env "must run as root" && return
4474         lctl get_param -n devices | grep -q obdfilter && \
4475                 skip "local OST" && return
4476
4477         grep -q llite_lloop /proc/modules
4478         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4479
4480         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4481                 skip "can't reliably test swap with TCP" && return
4482
4483         MEMTOTAL=`meminfo MemTotal`
4484         NR_BLOCKS=$((MEMTOTAL>>8))
4485         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4486
4487         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4488         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4489         mkswap $DIR/f68b
4490
4491         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4492
4493         trap cleanup_68 EXIT
4494
4495         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4496
4497         echo "before: `swapon -s | grep $LLOOP`"
4498         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4499         echo "after: `swapon -s | grep $LLOOP`"
4500         SWAPUSED=`swap_used $LLOOP`
4501
4502         cleanup_68
4503
4504         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4505 }
4506 run_test 68b "support swapping to Lustre ========================"
4507
4508 # bug5265, obdfilter oa2dentry return -ENOENT
4509 # #define OBD_FAIL_OST_ENOENT 0x217
4510 test_69() {
4511         remote_ost_nodsh && skip "remote OST with nodsh" && return
4512
4513         f="$DIR/$tfile"
4514         $SETSTRIPE -c 1 -i 0 $f
4515
4516         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4517
4518         do_facet ost1 lctl set_param fail_loc=0x217
4519         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4520         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4521
4522         do_facet ost1 lctl set_param fail_loc=0
4523         $DIRECTIO write $f 0 2 || error "write error"
4524
4525         cancel_lru_locks osc
4526         $DIRECTIO read $f 0 1 || error "read error"
4527
4528         do_facet ost1 lctl set_param fail_loc=0x217
4529         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4530
4531         do_facet ost1 lctl set_param fail_loc=0
4532         rm -f $f
4533 }
4534 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4535
4536 test_71() {
4537     mkdir -p $DIR/$tdir
4538     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4539 }
4540 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4541
4542 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4543         check_kernel_version 43 || return 0
4544         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4545
4546         # Check that testing environment is properly set up. Skip if not
4547         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4548                 skip_env "User $RUNAS_ID does not exist - skipping"
4549                 return 0
4550         }
4551         # We had better clear the $DIR to get enough space for dd
4552         rm -rf $DIR/*
4553         touch $DIR/f72
4554         chmod 777 $DIR/f72
4555         chmod ug+s $DIR/f72
4556         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4557         # See if we are still setuid/sgid
4558         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4559         # Now test that MDS is updated too
4560         cancel_lru_locks mdc
4561         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4562         true
4563         rm -f $DIR/f72
4564 }
4565 run_test 72a "Test that remove suid works properly (bug5695) ===="
4566
4567 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4568         local perm
4569
4570         [ "$RUNAS_ID" = "$UID" ] && \
4571                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4572         [ "$RUNAS_ID" -eq 0 ] && \
4573                 skip_env "RUNAS_ID = 0 -- skipping" && return
4574
4575         # Check that testing environment is properly set up. Skip if not
4576         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4577                 skip_env "User $RUNAS_ID does not exist - skipping"
4578                 return 0
4579         }
4580         touch $DIR/${tfile}-f{g,u}
4581         mkdir $DIR/${tfile}-d{g,u}
4582         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4583         chmod g+s $DIR/${tfile}-{f,d}g
4584         chmod u+s $DIR/${tfile}-{f,d}u
4585         for perm in 777 2777 4777; do
4586                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4587                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4588                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4589                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4590         done
4591         true
4592 }
4593 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4594
4595 # bug 3462 - multiple simultaneous MDC requests
4596 test_73() {
4597         mkdir $DIR/d73-1
4598         mkdir $DIR/d73-2
4599         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4600         pid1=$!
4601
4602         lctl set_param fail_loc=0x80000129
4603         $MULTIOP $DIR/d73-1/f73-2 Oc &
4604         sleep 1
4605         lctl set_param fail_loc=0
4606
4607         $MULTIOP $DIR/d73-2/f73-3 Oc &
4608         pid3=$!
4609
4610         kill -USR1 $pid1
4611         wait $pid1 || return 1
4612
4613         sleep 25
4614
4615         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4616         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4617         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4618
4619         rm -rf $DIR/d73-*
4620 }
4621 run_test 73 "multiple MDC requests (should not deadlock)"
4622
4623 test_74a() { # bug 6149, 6184
4624         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4625         #
4626         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4627         # will spin in a tight reconnection loop
4628         touch $DIR/f74a
4629         lctl set_param fail_loc=0x8000030e
4630         # get any lock that won't be difficult - lookup works.
4631         ls $DIR/f74a
4632         lctl set_param fail_loc=0
4633         true
4634         rm -f $DIR/f74a
4635 }
4636 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4637
4638 test_74b() { # bug 13310
4639         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4640         #
4641         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4642         # will spin in a tight reconnection loop
4643         lctl set_param fail_loc=0x8000030e
4644         # get a "difficult" lock
4645         touch $DIR/f74b
4646         lctl set_param fail_loc=0
4647         true
4648         rm -f $DIR/f74b
4649 }
4650 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4651
4652 test_74c() {
4653 #define OBD_FAIL_LDLM_NEW_LOCK
4654         lctl set_param fail_loc=0x80000319
4655         touch $DIR/$tfile && error "Touch successful"
4656         true
4657 }
4658 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4659
4660 num_inodes() {
4661         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4662 }
4663
4664 get_inode_slab_tunables() {
4665         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4666 }
4667
4668 set_inode_slab_tunables() {
4669         echo "lustre_inode_cache $1" > /proc/slabinfo
4670 }
4671
4672 test_76() { # Now for bug 20433, added originally in bug 1443
4673         local SLAB_SETTINGS=`get_inode_slab_tunables`
4674         local CPUS=`getconf _NPROCESSORS_ONLN`
4675         # we cannot set limit below 1 which means 1 inode in each
4676         # per-cpu cache is still allowed
4677         set_inode_slab_tunables "1 1 0"
4678         cancel_lru_locks osc
4679         BEFORE_INODES=`num_inodes`
4680         echo "before inodes: $BEFORE_INODES"
4681         local COUNT=1000
4682         [ "$SLOW" = "no" ] && COUNT=100
4683         for i in `seq $COUNT`; do
4684                 touch $DIR/$tfile
4685                 rm -f $DIR/$tfile
4686         done
4687         cancel_lru_locks osc
4688         AFTER_INODES=`num_inodes`
4689         echo "after inodes: $AFTER_INODES"
4690         local wait=0
4691         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4692                 sleep 2
4693                 AFTER_INODES=`num_inodes`
4694                 wait=$((wait+2))
4695                 echo "wait $wait seconds inodes: $AFTER_INODES"
4696                 if [ $wait -gt 30 ]; then
4697                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4698                 fi
4699         done
4700         set_inode_slab_tunables "$SLAB_SETTINGS"
4701 }
4702 run_test 76 "confirm clients recycle inodes properly ===="
4703
4704
4705 export ORIG_CSUM=""
4706 set_checksums()
4707 {
4708         # Note: in sptlrpc modes which enable its own bulk checksum, the
4709         # original crc32_le bulk checksum will be automatically disabled,
4710         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4711         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4712         # In this case set_checksums() will not be no-op, because sptlrpc
4713         # bulk checksum will be enabled all through the test.
4714
4715         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4716         lctl set_param -n osc.*.checksums $1
4717         return 0
4718 }
4719
4720 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4721                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4722 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4723 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4724 set_checksum_type()
4725 {
4726         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4727         log "set checksum type to $1"
4728         return 0
4729 }
4730 F77_TMP=$TMP/f77-temp
4731 F77SZ=8
4732 setup_f77() {
4733         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4734                 error "error writing to $F77_TMP"
4735 }
4736
4737 test_77a() { # bug 10889
4738         $GSS && skip "could not run with gss" && return
4739         [ ! -f $F77_TMP ] && setup_f77
4740         set_checksums 1
4741         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4742         set_checksums 0
4743         rm -f $DIR/$tfile
4744 }
4745 run_test 77a "normal checksum read/write operation ============="
4746
4747 test_77b() { # bug 10889
4748         $GSS && skip "could not run with gss" && return
4749         [ ! -f $F77_TMP ] && setup_f77
4750         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4751         lctl set_param fail_loc=0x80000409
4752         set_checksums 1
4753         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4754                 error "dd error: $?"
4755         lctl set_param fail_loc=0
4756         set_checksums 0
4757 }
4758 run_test 77b "checksum error on client write ===================="
4759
4760 test_77c() { # bug 10889
4761         $GSS && skip "could not run with gss" && return
4762         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4763         set_checksums 1
4764         for algo in $CKSUM_TYPES; do
4765                 cancel_lru_locks osc
4766                 set_checksum_type $algo
4767                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4768                 lctl set_param fail_loc=0x80000408
4769                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4770                 lctl set_param fail_loc=0
4771         done
4772         set_checksums 0
4773         set_checksum_type $ORIG_CSUM_TYPE
4774         rm -f $DIR/f77b
4775 }
4776 run_test 77c "checksum error on client read ==================="
4777
4778 test_77d() { # bug 10889
4779         $GSS && skip "could not run with gss" && return
4780         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4781         lctl set_param fail_loc=0x80000409
4782         set_checksums 1
4783         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4784                 error "direct write: rc=$?"
4785         lctl set_param fail_loc=0
4786         set_checksums 0
4787 }
4788 run_test 77d "checksum error on OST direct write ==============="
4789
4790 test_77e() { # bug 10889
4791         $GSS && skip "could not run with gss" && return
4792         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4793         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4794         lctl set_param fail_loc=0x80000408
4795         set_checksums 1
4796         cancel_lru_locks osc
4797         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4798                 error "direct read: rc=$?"
4799         lctl set_param fail_loc=0
4800         set_checksums 0
4801 }
4802 run_test 77e "checksum error on OST direct read ================"
4803
4804 test_77f() { # bug 10889
4805         $GSS && skip "could not run with gss" && return
4806         set_checksums 1
4807         for algo in $CKSUM_TYPES; do
4808                 cancel_lru_locks osc
4809                 set_checksum_type $algo
4810                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4811                 lctl set_param fail_loc=0x409
4812                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4813                         error "direct write succeeded"
4814                 lctl set_param fail_loc=0
4815         done
4816         set_checksum_type $ORIG_CSUM_TYPE
4817         set_checksums 0
4818 }
4819 run_test 77f "repeat checksum error on write (expect error) ===="
4820
4821 test_77g() { # bug 10889
4822         $GSS && skip "could not run with gss" && return
4823         remote_ost_nodsh && skip "remote OST with nodsh" && return
4824
4825         [ ! -f $F77_TMP ] && setup_f77
4826
4827         $SETSTRIPE -c 1 -i 0 $DIR/f77g
4828         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4829         do_facet ost1 lctl set_param fail_loc=0x8000021a
4830         set_checksums 1
4831         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4832                 error "write error: rc=$?"
4833         do_facet ost1 lctl set_param fail_loc=0
4834         set_checksums 0
4835 }
4836 run_test 77g "checksum error on OST write ======================"
4837
4838 test_77h() { # bug 10889
4839         $GSS && skip "could not run with gss" && return
4840         remote_ost_nodsh && skip "remote OST with nodsh" && return
4841
4842         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4843         cancel_lru_locks osc
4844         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4845         do_facet ost1 lctl set_param fail_loc=0x8000021b
4846         set_checksums 1
4847         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4848         do_facet ost1 lctl set_param fail_loc=0
4849         set_checksums 0
4850 }
4851 run_test 77h "checksum error on OST read ======================="
4852
4853 test_77i() { # bug 13805
4854         $GSS && skip "could not run with gss" && return
4855         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
4856         lctl set_param fail_loc=0x40b
4857         remount_client $MOUNT
4858         lctl set_param fail_loc=0
4859         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4860                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4861                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4862                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4863         done
4864         remount_client $MOUNT
4865 }
4866 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
4867
4868 test_77j() { # bug 13805
4869         $GSS && skip "could not run with gss" && return
4870         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
4871         lctl set_param fail_loc=0x40c
4872         remount_client $MOUNT
4873         lctl set_param fail_loc=0
4874         sleep 2 # wait async osc connect to finish
4875         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4876                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4877                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4878                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4879         done
4880         remount_client $MOUNT
4881 }
4882 run_test 77j "client only supporting ADLER32 ===================="
4883
4884 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
4885 rm -f $F77_TMP
4886 unset F77_TMP
4887
4888 test_78() { # bug 10901
4889         remote_ost || { skip_env "local OST" && return; }
4890
4891         NSEQ=5
4892         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
4893         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
4894         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
4895         echo "MemTotal: $MEMTOTAL"
4896 # reserve 256MB of memory for the kernel and other running processes,
4897 # and then take 1/2 of the remaining memory for the read/write buffers.
4898     if [ $MEMTOTAL -gt 512 ] ;then
4899         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
4900     else
4901         # for those poor memory-starved high-end clusters...
4902         MEMTOTAL=$((MEMTOTAL / 2))
4903     fi
4904         echo "Mem to use for directio: $MEMTOTAL"
4905         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
4906         [ $F78SIZE -gt 512 ] && F78SIZE=512
4907         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
4908         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
4909         echo "Smallest OST: $SMALLESTOST"
4910         [ $SMALLESTOST -lt 10240 ] && \
4911                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
4912
4913         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
4914                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
4915
4916         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
4917         echo "File size: $F78SIZE"
4918         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
4919         for i in `seq 1 $NSEQ`
4920         do
4921                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
4922                 echo directIO rdwr round $i of $NSEQ
4923                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
4924         done
4925
4926         rm -f $DIR/$tfile
4927 }
4928 run_test 78 "handle large O_DIRECT writes correctly ============"
4929
4930 test_79() { # bug 12743
4931         wait_delete_completed
4932
4933         BKTOTAL=$(calc_osc_kbytes kbytestotal)
4934         BKFREE=$(calc_osc_kbytes kbytesfree)
4935         BKAVAIL=$(calc_osc_kbytes kbytesavail)
4936
4937         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
4938         DFTOTAL=`echo $STRING | cut -d, -f1`
4939         DFUSED=`echo $STRING  | cut -d, -f2`
4940         DFAVAIL=`echo $STRING | cut -d, -f3`
4941         DFFREE=$(($DFTOTAL - $DFUSED))
4942
4943         ALLOWANCE=$((64 * $OSTCOUNT))
4944
4945         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
4946            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
4947                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
4948         fi
4949         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
4950            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
4951                 error "df free($DFFREE) mismatch OST free($BKFREE)"
4952         fi
4953         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
4954            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
4955                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
4956         fi
4957 }
4958 run_test 79 "df report consistency check ======================="
4959
4960 test_80() { # bug 10718
4961         # relax strong synchronous semantics for slow backends like ZFS
4962         local soc="obdfilter.*.sync_on_lock_cancel"
4963         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
4964         local hosts=
4965         if [ "$soc_old" != "never" -a "$FSTYPE" != "ldiskfs" ]; then
4966                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
4967                           facet_active_host $host; done | sort -u)
4968                 do_nodes $hosts lctl set_param $soc=never
4969         fi
4970
4971         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
4972         sync; sleep 1; sync
4973         local BEFORE=`date +%s`
4974         cancel_lru_locks osc
4975         local AFTER=`date +%s`
4976         local DIFF=$((AFTER-BEFORE))
4977         if [ $DIFF -gt 1 ] ; then
4978                 error "elapsed for 1M@1T = $DIFF"
4979         fi
4980
4981         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
4982
4983         true
4984         rm -f $DIR/$tfile
4985 }
4986 run_test 80 "Page eviction is equally fast at high offsets too  ===="
4987
4988 test_81a() { # LU-456
4989         remote_ost_nodsh && skip "remote OST with nodsh" && return
4990         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4991         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
4992         do_facet ost1 lctl set_param fail_loc=0x80000228
4993
4994         # write should trigger a retry and success
4995         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4996         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4997         RC=$?
4998         if [ $RC -ne 0 ] ; then
4999                 error "write should success, but failed for $RC"
5000         fi
5001 }
5002 run_test 81a "OST should retry write when get -ENOSPC ==============="
5003
5004 test_81b() { # LU-456
5005         remote_ost_nodsh && skip "remote OST with nodsh" && return
5006         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5007         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5008         do_facet ost1 lctl set_param fail_loc=0x228
5009
5010         # write should retry several times and return -ENOSPC finally
5011         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5012         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5013         RC=$?
5014         ENOSPC=28
5015         if [ $RC -ne $ENOSPC ] ; then
5016                 error "dd should fail for -ENOSPC, but succeed."
5017         fi
5018 }
5019 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5020
5021 test_82() { # LU-1031
5022         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5023         local gid1=14091995
5024         local gid2=16022000
5025
5026         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5027         local MULTIPID1=$!
5028         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5029         local MULTIPID2=$!
5030         kill -USR1 $MULTIPID2
5031         sleep 2
5032         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5033                 error "First grouplock does not block second one"
5034         else
5035                 echo "Second grouplock blocks first one"
5036         fi
5037         kill -USR1 $MULTIPID1
5038         wait $MULTIPID1
5039         wait $MULTIPID2
5040 }
5041 run_test 82 "Basic grouplock test ==============================="
5042
5043 test_99a() {
5044         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5045             return
5046         mkdir -p $DIR/d99cvsroot
5047         chown $RUNAS_ID $DIR/d99cvsroot
5048         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5049         cd $TMP
5050
5051         $RUNAS cvs -d $DIR/d99cvsroot init || error
5052         cd $oldPWD
5053 }
5054 run_test 99a "cvs init ========================================="
5055
5056 test_99b() {
5057         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5058         [ ! -d $DIR/d99cvsroot ] && test_99a
5059         cd /etc/init.d
5060         # some versions of cvs import exit(1) when asked to import links or
5061         # files they can't read.  ignore those files.
5062         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5063                         ! -perm +4 -printf '-I %f\n')
5064         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5065                 d99reposname vtag rtag
5066 }
5067 run_test 99b "cvs import ======================================="
5068
5069 test_99c() {
5070         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5071         [ ! -d $DIR/d99cvsroot ] && test_99b
5072         cd $DIR
5073         mkdir -p $DIR/d99reposname
5074         chown $RUNAS_ID $DIR/d99reposname
5075         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5076 }
5077 run_test 99c "cvs checkout ====================================="
5078
5079 test_99d() {
5080         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5081         [ ! -d $DIR/d99cvsroot ] && test_99c
5082         cd $DIR/d99reposname
5083         $RUNAS touch foo99
5084         $RUNAS cvs add -m 'addmsg' foo99
5085 }
5086 run_test 99d "cvs add =========================================="
5087
5088 test_99e() {
5089         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5090         [ ! -d $DIR/d99cvsroot ] && test_99c
5091         cd $DIR/d99reposname
5092         $RUNAS cvs update
5093 }
5094 run_test 99e "cvs update ======================================="
5095
5096 test_99f() {
5097         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5098         [ ! -d $DIR/d99cvsroot ] && test_99d
5099         cd $DIR/d99reposname
5100         $RUNAS cvs commit -m 'nomsg' foo99
5101     rm -fr $DIR/d99cvsroot
5102 }
5103 run_test 99f "cvs commit ======================================="
5104
5105 test_100() {
5106         [ "$NETTYPE" = tcp ] || \
5107                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5108                         return ; }
5109
5110         remote_ost_nodsh && skip "remote OST with nodsh" && return
5111         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5112         remote_servers || \
5113                 { skip "useless for local single node setup" && return; }
5114
5115         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5116                 [ "$PROT" != "tcp" ] && continue
5117                 RPORT=$(echo $REMOTE | cut -d: -f2)
5118                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5119
5120                 rc=0
5121                 LPORT=`echo $LOCAL | cut -d: -f2`
5122                 if [ $LPORT -ge 1024 ]; then
5123                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5124                         netstat -tna
5125                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5126                 fi
5127         done
5128         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5129 }
5130 run_test 100 "check local port using privileged port ==========="
5131
5132 function get_named_value()
5133 {
5134     local tag
5135
5136     tag=$1
5137     while read ;do
5138         line=$REPLY
5139         case $line in
5140         $tag*)
5141             echo $line | sed "s/^$tag[ ]*//"
5142             break
5143             ;;
5144         esac
5145     done
5146 }
5147
5148 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5149 cleanup_101a() {
5150         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5151         trap 0
5152 }
5153
5154 test_101a() {
5155         local s
5156         local discard
5157         local nreads=10000
5158         [ "$CPU" = "UML" ] && nreads=1000
5159         local cache_limit=32
5160
5161         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5162         trap cleanup_101a EXIT
5163         $LCTL set_param -n llite.*.read_ahead_stats 0
5164         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5165
5166         #
5167         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5168         #
5169         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5170         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5171
5172         discard=0
5173         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5174                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5175                         discard=$(($discard + $s))
5176         done
5177         cleanup_101a
5178
5179         if [ $(($discard * 10)) -gt $nreads ] ;then
5180                 $LCTL get_param osc.*-osc*.rpc_stats
5181                 $LCTL get_param llite.*.read_ahead_stats
5182                 error "too many ($discard) discarded pages"
5183         fi
5184         rm -f $DIR/$tfile || true
5185 }
5186 run_test 101a "check read-ahead for random reads ================"
5187
5188 setup_test101bc() {
5189         mkdir -p $DIR/$tdir
5190         STRIPE_SIZE=1048576
5191         STRIPE_COUNT=$OSTCOUNT
5192         STRIPE_OFFSET=0
5193
5194         local list=$(comma_list $(osts_nodes))
5195         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
5196         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
5197
5198         trap cleanup_test101bc EXIT
5199         # prepare the read-ahead file
5200         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5201
5202         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5203 }
5204
5205 cleanup_test101bc() {
5206         trap 0
5207         rm -rf $DIR/$tdir
5208         rm -f $DIR/$tfile
5209
5210         local list=$(comma_list $(osts_nodes))
5211         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
5212         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
5213 }
5214
5215 calc_total() {
5216         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5217 }
5218
5219 ra_check_101() {
5220         local READ_SIZE=$1
5221         local STRIPE_SIZE=1048576
5222         local RA_INC=1048576
5223         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5224         local FILE_LENGTH=$((64*100))
5225         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5226                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5227         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5228                         get_named_value 'read but discarded' | \
5229                         cut -d" " -f1 | calc_total`
5230         if [ $DISCARD -gt $discard_limit ]; then
5231                 $LCTL get_param llite.*.read_ahead_stats
5232                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5233         else
5234                 echo "Read-ahead success for size ${READ_SIZE}"
5235         fi
5236 }
5237
5238 test_101b() {
5239         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5240         local STRIPE_SIZE=1048576
5241         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5242         local FILE_LENGTH=$((STRIPE_SIZE*100))
5243         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5244         # prepare the read-ahead file
5245         setup_test101bc
5246         cancel_lru_locks osc
5247         for BIDX in 2 4 8 16 32 64 128 256
5248         do
5249                 local BSIZE=$((BIDX*4096))
5250                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5251                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5252                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5253                 $LCTL set_param -n llite.*.read_ahead_stats 0
5254                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5255                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5256                 cancel_lru_locks osc
5257                 ra_check_101 $BSIZE
5258         done
5259         cleanup_test101bc
5260         true
5261 }
5262 run_test 101b "check stride-io mode read-ahead ================="
5263
5264 test_101c() {
5265     local STRIPE_SIZE=1048576
5266     local FILE_LENGTH=$((STRIPE_SIZE*100))
5267     local nreads=10000
5268     local osc
5269
5270     setup_test101bc
5271
5272     cancel_lru_locks osc
5273     $LCTL set_param osc.*.rpc_stats 0
5274     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5275     for osc in $($LCTL get_param -N osc.*); do
5276         if [ "$osc" == "osc.num_refs" ]; then
5277             continue
5278         fi
5279
5280         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5281         if [ $lines -le 20 ]; then
5282             continue
5283         fi
5284
5285         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5286                                      awk '$1 == "1:" { print $2; exit; }')
5287         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5288                                      awk '$1 == "2:" { print $2; exit; }')
5289         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5290                                      awk '$1 == "4:" { print $2; exit; }')
5291         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5292                                      awk '$1 == "8:" { print $2; exit; }')
5293
5294         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5295         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5296         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5297         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5298         echo "${osc} rpc check passed!"
5299     done
5300     cleanup_test101bc
5301     true
5302 }
5303 run_test 101c "check stripe_size aligned read-ahead ================="
5304
5305 set_read_ahead() {
5306    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5307    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5308 }
5309
5310 test_101d() {
5311     local file=$DIR/$tfile
5312     local size=${FILESIZE_101c:-500}
5313     local ra_MB=${READAHEAD_MB:-40}
5314
5315     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5316     [ $space -gt $((size / 1024)) ] ||
5317         { skip "Need free space ${size}M, have $space" && return; }
5318
5319     echo Creating ${size}M test file $file
5320     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5321     echo Cancel LRU locks on lustre client to flush the client cache
5322     cancel_lru_locks osc
5323
5324     echo Disable read-ahead
5325     local old_READAHEAD=$(set_read_ahead 0)
5326
5327     echo Reading the test file $file with read-ahead disabled
5328     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5329
5330     echo Cancel LRU locks on lustre client to flush the client cache
5331     cancel_lru_locks osc
5332     echo Enable read-ahead with ${ra_MB}MB
5333     set_read_ahead $ra_MB
5334
5335     echo Reading the test file $file with read-ahead enabled
5336     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5337
5338     echo read-ahead disabled time read $time_ra_OFF
5339     echo read-ahead enabled  time read $time_ra_ON
5340
5341         set_read_ahead $old_READAHEAD
5342         rm -f $file
5343         wait_delete_completed
5344
5345     [ $time_ra_ON -lt $time_ra_OFF ] ||
5346         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5347                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5348 }
5349 run_test 101d "file read with and without read-ahead enabled  ================="
5350
5351 test_101e() {
5352     local file=$DIR/$tfile
5353     local size=500  #KB
5354     local count=100
5355     local blksize=1024
5356
5357     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5358     local need_space=$((count * size))
5359     [ $space -gt $need_space ] ||
5360         { skip_env "Need free space $need_space, have $space" && return; }
5361
5362     echo Creating $count ${size}K test files
5363     for ((i = 0; i < $count; i++)); do
5364         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5365     done
5366
5367     echo Cancel LRU locks on lustre client to flush the client cache
5368     cancel_lru_locks osc
5369
5370     echo Reset readahead stats
5371     $LCTL set_param -n llite.*.read_ahead_stats 0
5372
5373     for ((i = 0; i < $count; i++)); do
5374         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5375     done
5376
5377     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5378           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5379
5380     for ((i = 0; i < $count; i++)); do
5381         rm -rf ${file}_${i} 2>/dev/null
5382     done
5383
5384     #10000 means 20% reads are missing in readahead
5385     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5386 }
5387 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5388
5389 cleanup_test101f() {
5390     trap 0
5391     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5392     rm -rf $DIR/$tfile 2>/dev/null
5393 }
5394
5395 test_101f() {
5396     local file=$DIR/$tfile
5397     local nreads=1000
5398
5399     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5400     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5401     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5402     trap cleanup_test101f EXIT
5403
5404     echo Cancel LRU locks on lustre client to flush the client cache
5405     cancel_lru_locks osc
5406
5407     echo Reset readahead stats
5408     $LCTL set_param -n llite.*.read_ahead_stats 0
5409     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5410     # readahead should read in 2M file on second read, so only miss
5411     # 2 pages.
5412     echo Random 4K reads on 2M file for 1000 times
5413     $READS -f $file -s 2097152 -b 4096 -n $nreads
5414
5415     echo checking missing pages
5416     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5417           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5418
5419     [ $miss -lt 3 ] || error "misses too much pages!"
5420     cleanup_test101f
5421 }
5422 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5423
5424 setup_test102() {
5425         mkdir -p $DIR/$tdir
5426         chown $RUNAS_ID $DIR/$tdir
5427         STRIPE_SIZE=65536
5428         STRIPE_OFFSET=1
5429         STRIPE_COUNT=$OSTCOUNT
5430         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5431
5432         trap cleanup_test102 EXIT
5433         cd $DIR
5434         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5435         cd $DIR/$tdir
5436         for num in 1 2 3 4; do
5437                 for count in $(seq 1 $STRIPE_COUNT); do
5438                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5439                                 local size=`expr $STRIPE_SIZE \* $num`
5440                                 local file=file"$num-$idx-$count"
5441                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5442                         done
5443                 done
5444         done
5445
5446         cd $DIR
5447         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5448 }
5449
5450 cleanup_test102() {
5451         trap 0
5452         rm -f $TMP/f102.tar
5453         rm -rf $DIR/d0.sanity/d102
5454 }
5455
5456 test_102a() {
5457         local testfile=$DIR/xattr_testfile
5458
5459         touch $testfile
5460
5461         [ "$UID" != 0 ] && skip_env "must run as root" && return
5462         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5463                 skip_env "must have user_xattr" && return
5464
5465         [ -z "$(which setfattr 2>/dev/null)" ] &&
5466                 skip_env "could not find setfattr" && return
5467
5468         echo "set/get xattr..."
5469         setfattr -n trusted.name1 -v value1 $testfile || error
5470         getfattr -n trusted.name1 $testfile 2> /dev/null |
5471           grep "trusted.name1=.value1" ||
5472                 error "$testfile missing trusted.name1=value1"
5473
5474         setfattr -n user.author1 -v author1 $testfile || error
5475         getfattr -n user.author1 $testfile 2> /dev/null |
5476           grep "user.author1=.author1" ||
5477                 error "$testfile missing trusted.author1=author1"
5478
5479         echo "listxattr..."
5480         setfattr -n trusted.name2 -v value2 $testfile ||
5481                 error "$testfile unable to set trusted.name2"
5482         setfattr -n trusted.name3 -v value3 $testfile ||
5483                 error "$testfile unable to set trusted.name3"
5484         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5485             grep "trusted.name" | wc -l) -eq 3 ] ||
5486                 error "$testfile missing 3 trusted.name xattrs"
5487
5488         setfattr -n user.author2 -v author2 $testfile ||
5489                 error "$testfile unable to set user.author2"
5490         setfattr -n user.author3 -v author3 $testfile ||
5491                 error "$testfile unable to set user.author3"
5492         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5493             grep "user.author" | wc -l) -eq 3 ] ||
5494                 error "$testfile missing 3 user.author xattrs"
5495
5496         echo "remove xattr..."
5497         setfattr -x trusted.name1 $testfile ||
5498                 error "$testfile error deleting trusted.name1"
5499         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5500                 error "$testfile did not delete trusted.name1 xattr"
5501
5502         setfattr -x user.author1 $testfile ||
5503                 error "$testfile error deleting user.author1"
5504         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5505                 error "$testfile did not delete trusted.name1 xattr"
5506
5507         # b10667: setting lustre special xattr be silently discarded
5508         echo "set lustre special xattr ..."
5509         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5510                 error "$testfile allowed setting trusted.lov"
5511 }
5512 run_test 102a "user xattr test =================================="
5513
5514 test_102b() {
5515         # b10930: get/set/list trusted.lov xattr
5516         echo "get/set/list trusted.lov xattr ..."
5517         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5518         local testfile=$DIR/$tfile
5519         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5520                 error "setstripe failed"
5521         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5522                 error "getstripe failed"
5523         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5524         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5525
5526         local testfile2=${testfile}2
5527         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5528                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5529
5530         $MCREATE $testfile2
5531         setfattr -n trusted.lov -v $value $testfile2
5532         local stripe_size=$($GETSTRIPE -S $testfile2)
5533         local stripe_count=$($GETSTRIPE -c $testfile2)
5534         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5535         [ $stripe_count -eq $STRIPECOUNT ] ||
5536                 error "stripe count $stripe_count != $STRIPECOUNT"
5537         rm -f $DIR/$tfile
5538 }
5539 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5540
5541 test_102c() {
5542         # b10930: get/set/list lustre.lov xattr
5543         echo "get/set/list lustre.lov xattr ..."
5544         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5545         mkdir -p $DIR/$tdir
5546         chown $RUNAS_ID $DIR/$tdir
5547         local testfile=$DIR/$tdir/$tfile
5548         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5549                 error "setstripe failed"
5550         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5551                 error "getstripe failed"
5552         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5553         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5554
5555         local testfile2=${testfile}2
5556         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5557                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5558
5559         $RUNAS $MCREATE $testfile2
5560         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5561         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5562         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5563         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5564         [ $stripe_count -eq $STRIPECOUNT ] ||
5565                 error "stripe count $stripe_count != $STRIPECOUNT"
5566 }
5567 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5568
5569 compare_stripe_info1() {
5570         local stripe_index_all_zero=true
5571
5572         for num in 1 2 3 4; do
5573                 for count in $(seq 1 $STRIPE_COUNT); do
5574                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5575                                 local size=$((STRIPE_SIZE * num))
5576                                 local file=file"$num-$offset-$count"
5577                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5578                                 [ $stripe_size -ne $size ] &&
5579                                     error "$file: size $stripe_size != $size"
5580                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5581                                 # allow fewer stripes to be created, ORI-601
5582                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5583                                     error "$file: count $stripe_count != $count"
5584                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5585                                 [ $stripe_index -ne 0 ] &&
5586                                         stripe_index_all_zero=false
5587                         done
5588                 done
5589         done
5590         $stripe_index_all_zero &&
5591                 error "all files are being extracted starting from OST index 0"
5592         return 0
5593 }
5594
5595 find_lustre_tar() {
5596         [ -n "$(which tar 2>/dev/null)" ] &&
5597                 strings $(which tar) | grep -q "lustre" && echo tar
5598 }
5599
5600 test_102d() {
5601         # b10930: tar test for trusted.lov xattr
5602         TAR=$(find_lustre_tar)
5603         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5604         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5605         setup_test102
5606         mkdir -p $DIR/d102d
5607         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5608         cd $DIR/d102d/$tdir
5609         compare_stripe_info1
5610 }
5611 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5612
5613 test_102f() {
5614         # b10930: tar test for trusted.lov xattr
5615         TAR=$(find_lustre_tar)
5616         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5617         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5618         setup_test102
5619         mkdir -p $DIR/d102f
5620         cd $DIR
5621         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5622         cd $DIR/d102f/$tdir
5623         compare_stripe_info1
5624 }
5625 run_test 102f "tar copy files, not keep osts ==========="
5626
5627 grow_xattr() {
5628         local xsize=${1:-1024}  # in bytes
5629         local file=$DIR/$tfile
5630
5631         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5632                 skip "must have user_xattr" && return 0
5633         [ -z "$(which setfattr 2>/dev/null)" ] &&
5634                 skip_env "could not find setfattr" && return 0
5635         [ -z "$(which getfattr 2>/dev/null)" ] &&
5636                 skip_env "could not find getfattr" && return 0
5637
5638         touch $file
5639
5640         local value="$(generate_string $xsize)"
5641
5642         local xbig=trusted.big
5643         log "save $xbig on $file"
5644         setfattr -n $xbig -v $value $file ||
5645                 error "saving $xbig on $file failed"
5646
5647         local orig=$(get_xattr_value $xbig $file)
5648         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5649
5650         local xsml=trusted.sml
5651         log "save $xsml on $file"
5652         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5653
5654         local new=$(get_xattr_value $xbig $file)
5655         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5656
5657         log "grow $xsml on $file"
5658         setfattr -n $xsml -v "$value" $file ||
5659                 error "growing $xsml on $file failed"
5660
5661         new=$(get_xattr_value $xbig $file)
5662         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5663         log "$xbig still valid after growing $xsml"
5664
5665         rm -f $file
5666 }
5667
5668 test_102h() { # bug 15777
5669         grow_xattr 1024
5670 }
5671 run_test 102h "grow xattr from inside inode to external block"
5672
5673 test_102ha() {
5674         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5675         grow_xattr $(max_xattr_size)
5676 }
5677 run_test 102ha "grow xattr from inside inode to external inode"
5678
5679 test_102i() { # bug 17038
5680         touch $DIR/$tfile
5681         ln -s $DIR/$tfile $DIR/${tfile}link
5682         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5683         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"
5684         rm -f $DIR/$tfile $DIR/${tfile}link
5685 }
5686 run_test 102i "lgetxattr test on symbolic link ============"
5687
5688 test_102j() {
5689         TAR=$(find_lustre_tar)
5690         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5691         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5692         setup_test102 "$RUNAS"
5693         mkdir -p $DIR/d102j
5694         chown $RUNAS_ID $DIR/d102j
5695         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5696         cd $DIR/d102j/$tdir
5697         compare_stripe_info1 "$RUNAS"
5698 }
5699 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5700
5701 test_102k() {
5702         touch $DIR/$tfile
5703         # b22187 just check that does not crash for regular file.
5704         setfattr -n trusted.lov $DIR/$tfile
5705         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5706         local test_kdir=$DIR/d102k
5707         mkdir $test_kdir
5708         local default_size=`$GETSTRIPE -S $test_kdir`
5709         local default_count=`$GETSTRIPE -c $test_kdir`
5710         local default_offset=`$GETSTRIPE -i $test_kdir`
5711         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5712                 error 'dir setstripe failed'
5713         setfattr -n trusted.lov $test_kdir
5714         local stripe_size=`$GETSTRIPE -S $test_kdir`
5715         local stripe_count=`$GETSTRIPE -c $test_kdir`
5716         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5717         [ $stripe_size -eq $default_size ] ||
5718                 error "stripe size $stripe_size != $default_size"
5719         [ $stripe_count -eq $default_count ] ||
5720                 error "stripe count $stripe_count != $default_count"
5721         [ $stripe_offset -eq $default_offset ] ||
5722                 error "stripe offset $stripe_offset != $default_offset"
5723         rm -rf $DIR/$tfile $test_kdir
5724 }
5725 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5726
5727 test_102l() {
5728         # LU-532 trusted. xattr is invisible to non-root
5729         local testfile=$DIR/$tfile
5730
5731         touch $testfile
5732
5733         echo "listxattr as user..."
5734         chown $RUNAS_ID $testfile
5735         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5736             grep -q "trusted" &&
5737                 error "$testfile trusted xattrs are user visible"
5738
5739         return 0;
5740 }
5741 run_test 102l "listxattr filter test =================================="
5742
5743 cleanup_test102
5744
5745 run_acl_subtest()
5746 {
5747     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5748     return $?
5749 }
5750
5751 test_103 () {
5752     [ "$UID" != 0 ] && skip_env "must run as root" && return
5753     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5754     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5755     $GSS && skip "could not run under gss" && return
5756
5757     declare -a identity_old
5758
5759     for num in `seq $MDSCOUNT`; do
5760         switch_identity $num true || identity_old[$num]=$?
5761     done
5762
5763     SAVE_UMASK=`umask`
5764     umask 0022
5765     cd $DIR
5766
5767     echo "performing cp ..."
5768     run_acl_subtest cp || error
5769     echo "performing getfacl-noacl..."
5770     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5771     echo "performing misc..."
5772     run_acl_subtest misc || error  "misc test failed"
5773     echo "performing permissions..."
5774     run_acl_subtest permissions || error "permissions failed"
5775     echo "performing setfacl..."
5776     run_acl_subtest setfacl || error  "setfacl test failed"
5777
5778     # inheritance test got from HP
5779     echo "performing inheritance..."
5780     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5781     chmod +x make-tree || error "chmod +x failed"
5782     run_acl_subtest inheritance || error "inheritance test failed"
5783     rm -f make-tree
5784
5785     cd $SAVE_PWD
5786     umask $SAVE_UMASK
5787
5788     for num in `seq $MDSCOUNT`; do
5789         if [ "${identity_old[$num]}" = 1 ]; then
5790             switch_identity $num false || identity_old[$num]=$?
5791         fi
5792     done
5793 }
5794 run_test 103 "acl test ========================================="
5795
5796 test_104a() {
5797         touch $DIR/$tfile
5798         lfs df || error "lfs df failed"
5799         lfs df -ih || error "lfs df -ih failed"
5800         lfs df -h $DIR || error "lfs df -h $DIR failed"
5801         lfs df -i $DIR || error "lfs df -i $DIR failed"
5802         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5803         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5804
5805         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
5806         lctl --device %$OSC deactivate
5807         lfs df || error "lfs df with deactivated OSC failed"
5808         lctl --device %$OSC activate
5809         # wait the osc back to normal
5810         wait_osc_import_state client ost FULL
5811
5812         lfs df || error "lfs df with reactivated OSC failed"
5813         rm -f $DIR/$tfile
5814 }
5815 run_test 104a "lfs df [-ih] [path] test ========================="
5816
5817 test_104b() {
5818         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5819         chmod 666 /dev/obd
5820         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5821         if [ $denied_cnt -ne 0 ];
5822         then
5823                     error "lfs check servers test failed"
5824         fi
5825 }
5826 run_test 104b "$RUNAS lfs check servers test ===================="
5827
5828 test_105a() {
5829         # doesn't work on 2.4 kernels
5830         touch $DIR/$tfile
5831         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5832         then
5833                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5834         else
5835                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5836         fi
5837         rm -f $DIR/$tfile
5838 }
5839 run_test 105a "flock when mounted without -o flock test ========"
5840
5841 test_105b() {
5842         touch $DIR/$tfile
5843         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5844         then
5845                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5846         else
5847                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5848         fi
5849         rm -f $DIR/$tfile
5850 }
5851 run_test 105b "fcntl when mounted without -o flock test ========"
5852
5853 test_105c() {
5854         touch $DIR/$tfile
5855         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5856         then
5857                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
5858         else
5859                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
5860         fi
5861         rm -f $DIR/$tfile
5862 }
5863 run_test 105c "lockf when mounted without -o flock test ========"
5864
5865 test_105d() { # bug 15924
5866         mkdir -p $DIR/$tdir
5867         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5868                 skip "mount w/o flock enabled" && return
5869         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
5870         $LCTL set_param fail_loc=0x80000315
5871         flocks_test 2 $DIR/$tdir
5872 }
5873 run_test 105d "flock race (should not freeze) ========"
5874
5875 test_105e() { # bug 22660 && 22040
5876         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5877                 skip "mount w/o flock enabled" && return
5878         touch $DIR/$tfile
5879         flocks_test 3 $DIR/$tfile
5880 }
5881 run_test 105e "Two conflicting flocks from same process ======="
5882
5883 test_106() { #bug 10921
5884         mkdir -p $DIR/$tdir
5885         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
5886         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
5887 }
5888 run_test 106 "attempt exec of dir followed by chown of that dir"
5889
5890 test_107() {
5891         CDIR=`pwd`
5892         cd $DIR
5893
5894         local file=core
5895         rm -f $file
5896
5897         local save_pattern=$(sysctl -n kernel.core_pattern)
5898         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
5899         sysctl -w kernel.core_pattern=$file
5900         sysctl -w kernel.core_uses_pid=0
5901
5902         ulimit -c unlimited
5903         sleep 60 &
5904         SLEEPPID=$!
5905
5906         sleep 1
5907
5908         kill -s 11 $SLEEPPID
5909         wait $SLEEPPID
5910         if [ -e $file ]; then
5911                 size=`stat -c%s $file`
5912                 [ $size -eq 0 ] && error "Fail to create core file $file"
5913         else
5914                 error "Fail to create core file $file"
5915         fi
5916         rm -f $file
5917         sysctl -w kernel.core_pattern=$save_pattern
5918         sysctl -w kernel.core_uses_pid=$save_uses_pid
5919         cd $CDIR
5920 }
5921 run_test 107 "Coredump on SIG"
5922
5923 test_110() {
5924         mkdir -p $DIR/d110
5925         mkdir $DIR/d110/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || error "mkdir with 255 char fail"
5926         mkdir $DIR/d110/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && error "mkdir with 256 char should fail, but not"
5927         touch $DIR/d110/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx || error "create with 255 char fail"
5928         touch $DIR/d110/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy && error ""create with 256 char should fail, but not
5929
5930         ls -l $DIR/d110
5931     rm -fr $DIR/d110
5932 }
5933 run_test 110 "filename length checking"
5934
5935 test_115() {
5936         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5937             cut -c11-20)
5938         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
5939             return
5940         echo "Starting with $OSTIO_pre threads"
5941
5942         NUMTEST=20000
5943         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5944         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
5945         echo "$NUMTEST creates/unlinks"
5946         mkdir -p $DIR/$tdir
5947         createmany -o $DIR/$tdir/$tfile $NUMTEST
5948         unlinkmany $DIR/$tdir/$tfile $NUMTEST
5949
5950         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5951             cut -c11-20)
5952
5953         # don't return an error
5954         [ $OSTIO_post == $OSTIO_pre ] && echo \
5955             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
5956             echo "This may be fine, depending on what ran before this test" &&
5957             echo "and how fast this system is." && return
5958
5959         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
5960 }
5961 run_test 115 "verify dynamic thread creation===================="
5962
5963 free_min_max () {
5964         wait_delete_completed
5965         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
5966         echo OST kbytes available: ${AVAIL[@]}
5967         MAXI=0; MAXV=${AVAIL[0]}
5968         MINI=0; MINV=${AVAIL[0]}
5969         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
5970             #echo OST $i: ${AVAIL[i]}kb
5971             if [ ${AVAIL[i]} -gt $MAXV ]; then
5972                 MAXV=${AVAIL[i]}; MAXI=$i
5973             fi
5974             if [ ${AVAIL[i]} -lt $MINV ]; then
5975                 MINV=${AVAIL[i]}; MINI=$i
5976             fi
5977         done
5978         echo Min free space: OST $MINI: $MINV
5979         echo Max free space: OST $MAXI: $MAXV
5980 }
5981
5982 test_116a() { # was previously test_116()
5983         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
5984
5985         echo -n "Free space priority "
5986         lctl get_param -n lov.*-clilov-*.qos_prio_free
5987         declare -a AVAIL
5988         free_min_max
5989         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
5990                 return
5991
5992         # generate uneven OSTs
5993         mkdir -p $DIR/$tdir/OST${MINI}
5994         declare -i FILL
5995         FILL=$(($MINV / 4))
5996         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
5997         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
5998         i=0
5999         while [ $FILL -gt 0 ]; do
6000             i=$(($i + 1))
6001             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6002             FILL=$(($FILL - 2048))
6003             echo -n .
6004         done
6005         FILL=$(($MINV / 4))
6006         sync
6007         sleep_maxage
6008
6009         free_min_max
6010         DIFF=$(($MAXV - $MINV))
6011         DIFF2=$(($DIFF * 100 / $MINV))
6012         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6013         if [ $DIFF2 -gt 20 ]; then
6014             echo "ok"
6015         else
6016             echo "failed - QOS mode won't be used"
6017             error_ignore "QOS imbalance criteria not met"
6018             return
6019         fi
6020
6021         MINI1=$MINI; MINV1=$MINV
6022         MAXI1=$MAXI; MAXV1=$MAXV
6023
6024         # now fill using QOS
6025         echo writing a bunch of files to QOS-assigned OSTs
6026         $SETSTRIPE -c 1 $DIR/$tdir
6027         i=0
6028         while [ $FILL -gt 0 ]; do
6029             i=$(($i + 1))
6030             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6031             FILL=$(($FILL - 200))
6032             echo -n .
6033         done
6034         echo "wrote $i 200k files"
6035         sync
6036         sleep_maxage
6037
6038         echo "Note: free space may not be updated, so measurements might be off"
6039         free_min_max
6040         DIFF2=$(($MAXV - $MINV))
6041         echo "free space delta: orig $DIFF final $DIFF2"
6042         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6043         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6044         echo "Wrote $DIFF to smaller OST $MINI1"
6045         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6046         echo "Wrote $DIFF2 to larger OST $MAXI1"
6047         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6048
6049         # Figure out which files were written where
6050         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6051                awk '/'$MINI1': / {print $2; exit}')
6052         echo $UUID
6053         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6054         echo "$MINC files created on smaller OST $MINI1"
6055         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6056                awk '/'$MAXI1': / {print $2; exit}')
6057         echo $UUID
6058         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6059         echo "$MAXC files created on larger OST $MAXI1"
6060         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6061         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6062
6063         rm -rf $DIR/$tdir
6064 }
6065 run_test 116a "stripe QOS: free space balance ==================="
6066
6067 test_116b() { # LU-2093
6068 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6069         local old_rr
6070         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6071         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6072         mkdir -p $DIR/$tdir
6073         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6074         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6075         do_facet $SINGLEMDS lctl set_param fail_loc=0
6076         rm -rf $DIR/$tdir
6077         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6078 }
6079 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6080
6081 test_117() # bug 10891
6082 {
6083         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6084         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6085         lctl set_param fail_loc=0x21e
6086         > $DIR/$tfile || error "truncate failed"
6087         lctl set_param fail_loc=0
6088         echo "Truncate succeeded."
6089         rm -f $DIR/$tfile
6090 }
6091 run_test 117 "verify fsfilt_extend =========="
6092
6093 export OLD_RESENDCOUNT=""
6094 set_resend_count () {
6095         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6096         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6097         lctl set_param -n $PROC_RESENDCOUNT $1
6098         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6099 }
6100
6101 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6102
6103 # Reset async IO behavior after error case
6104 reset_async() {
6105         FILE=$DIR/reset_async
6106
6107         # Ensure all OSCs are cleared
6108         $SETSTRIPE -c -1 $FILE
6109         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6110         sync
6111         rm $FILE
6112 }
6113
6114 test_118a() #bug 11710
6115 {
6116         reset_async
6117
6118         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6119         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6120         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6121
6122         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6123                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6124                 return 1;
6125         fi
6126         rm -f $DIR/$tfile
6127 }
6128 run_test 118a "verify O_SYNC works =========="
6129
6130 test_118b()
6131 {
6132         remote_ost_nodsh && skip "remote OST with nodsh" && return
6133
6134         reset_async
6135
6136         #define OBD_FAIL_OST_ENOENT 0x217
6137         set_nodes_failloc "$(osts_nodes)" 0x217
6138         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6139         RC=$?
6140         set_nodes_failloc "$(osts_nodes)" 0
6141         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6142         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6143                     grep -c writeback)
6144
6145         if [[ $RC -eq 0 ]]; then
6146                 error "Must return error due to dropped pages, rc=$RC"
6147                 return 1;
6148         fi
6149
6150         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6151                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6152                 return 1;
6153         fi
6154
6155         echo "Dirty pages not leaked on ENOENT"
6156
6157         # Due to the above error the OSC will issue all RPCs syncronously
6158         # until a subsequent RPC completes successfully without error.
6159         $MULTIOP $DIR/$tfile Ow4096yc
6160         rm -f $DIR/$tfile
6161
6162         return 0
6163 }
6164 run_test 118b "Reclaim dirty pages on fatal error =========="
6165
6166 test_118c()
6167 {
6168         remote_ost_nodsh && skip "remote OST with nodsh" && return
6169
6170         reset_async
6171
6172         #define OBD_FAIL_OST_EROFS               0x216
6173         set_nodes_failloc "$(osts_nodes)" 0x216
6174
6175         # multiop should block due to fsync until pages are written
6176         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6177         MULTIPID=$!
6178         sleep 1
6179
6180         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6181                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6182         fi
6183
6184         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6185                     grep -c writeback)
6186         if [[ $WRITEBACK -eq 0 ]]; then
6187                 error "No page in writeback, writeback=$WRITEBACK"
6188         fi
6189
6190         set_nodes_failloc "$(osts_nodes)" 0
6191         wait $MULTIPID
6192         RC=$?
6193         if [[ $RC -ne 0 ]]; then
6194                 error "Multiop fsync failed, rc=$RC"
6195         fi
6196
6197         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6198         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6199                     grep -c writeback)
6200         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6201                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6202         fi
6203
6204         rm -f $DIR/$tfile
6205         echo "Dirty pages flushed via fsync on EROFS"
6206         return 0
6207 }
6208 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6209
6210 test_118d()
6211 {
6212         remote_ost_nodsh && skip "remote OST with nodsh" && return
6213
6214         reset_async
6215
6216         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6217         set_nodes_failloc "$(osts_nodes)" 0x214
6218         # multiop should block due to fsync until pages are written
6219         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6220         MULTIPID=$!
6221         sleep 1
6222
6223         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6224                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6225         fi
6226
6227         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6228                     grep -c writeback)
6229         if [[ $WRITEBACK -eq 0 ]]; then
6230                 error "No page in writeback, writeback=$WRITEBACK"
6231         fi
6232
6233         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6234         set_nodes_failloc "$(osts_nodes)" 0
6235
6236         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6237         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6238                     grep -c writeback)
6239         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6240                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6241         fi
6242
6243         rm -f $DIR/$tfile
6244         echo "Dirty pages gaurenteed flushed via fsync"
6245         return 0
6246 }
6247 run_test 118d "Fsync validation inject a delay of the bulk =========="
6248
6249 test_118f() {
6250         reset_async
6251
6252         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6253         lctl set_param fail_loc=0x8000040a
6254
6255         # Should simulate EINVAL error which is fatal
6256         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6257         RC=$?
6258         if [[ $RC -eq 0 ]]; then
6259                 error "Must return error due to dropped pages, rc=$RC"
6260         fi
6261
6262         lctl set_param fail_loc=0x0
6263
6264         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6265         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6266         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6267                     grep -c writeback)
6268         if [[ $LOCKED -ne 0 ]]; then
6269                 error "Locked pages remain in cache, locked=$LOCKED"
6270         fi
6271
6272         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6273                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6274         fi
6275
6276         rm -f $DIR/$tfile
6277         echo "No pages locked after fsync"
6278
6279         reset_async
6280         return 0
6281 }
6282 run_test 118f "Simulate unrecoverable OSC side error =========="
6283
6284 test_118g() {
6285         reset_async
6286
6287         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6288         lctl set_param fail_loc=0x406
6289
6290         # simulate local -ENOMEM
6291         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6292         RC=$?
6293
6294         lctl set_param fail_loc=0
6295         if [[ $RC -eq 0 ]]; then
6296                 error "Must return error due to dropped pages, rc=$RC"
6297         fi
6298
6299         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6300         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6301         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6302                         grep -c writeback)
6303         if [[ $LOCKED -ne 0 ]]; then
6304                 error "Locked pages remain in cache, locked=$LOCKED"
6305         fi
6306
6307         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6308                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6309         fi
6310
6311         rm -f $DIR/$tfile
6312         echo "No pages locked after fsync"
6313
6314         reset_async
6315         return 0
6316 }
6317 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6318
6319 test_118h() {
6320         remote_ost_nodsh && skip "remote OST with nodsh" && return
6321
6322         reset_async
6323
6324         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6325         set_nodes_failloc "$(osts_nodes)" 0x20e
6326         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6327         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6328         RC=$?
6329
6330         set_nodes_failloc "$(osts_nodes)" 0
6331         if [[ $RC -eq 0 ]]; then
6332                 error "Must return error due to dropped pages, rc=$RC"
6333         fi
6334
6335         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6336         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6337         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6338                     grep -c writeback)
6339         if [[ $LOCKED -ne 0 ]]; then
6340                 error "Locked pages remain in cache, locked=$LOCKED"
6341         fi
6342
6343         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6344                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6345         fi
6346
6347         rm -f $DIR/$tfile
6348         echo "No pages locked after fsync"
6349
6350         return 0
6351 }
6352 run_test 118h "Verify timeout in handling recoverables errors  =========="
6353
6354 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6355
6356 test_118i() {
6357         remote_ost_nodsh && skip "remote OST with nodsh" && return
6358
6359         reset_async
6360
6361         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6362         set_nodes_failloc "$(osts_nodes)" 0x20e
6363
6364         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6365         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6366         PID=$!
6367         sleep 5
6368         set_nodes_failloc "$(osts_nodes)" 0
6369
6370         wait $PID
6371         RC=$?
6372         if [[ $RC -ne 0 ]]; then
6373                 error "got error, but should be not, rc=$RC"
6374         fi
6375
6376         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6377         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6378         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6379         if [[ $LOCKED -ne 0 ]]; then
6380                 error "Locked pages remain in cache, locked=$LOCKED"
6381         fi
6382
6383         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6384                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6385         fi
6386
6387         rm -f $DIR/$tfile
6388         echo "No pages locked after fsync"
6389
6390         return 0
6391 }
6392 run_test 118i "Fix error before timeout in recoverable error  =========="
6393
6394 [ "$SLOW" = "no" ] && set_resend_count 4
6395
6396 test_118j() {
6397         remote_ost_nodsh && skip "remote OST with nodsh" && return
6398
6399         reset_async
6400
6401         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6402         set_nodes_failloc "$(osts_nodes)" 0x220
6403
6404         # return -EIO from OST
6405         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6406         RC=$?
6407         set_nodes_failloc "$(osts_nodes)" 0x0
6408         if [[ $RC -eq 0 ]]; then
6409                 error "Must return error due to dropped pages, rc=$RC"
6410         fi
6411
6412         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6413         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6414         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6415         if [[ $LOCKED -ne 0 ]]; then
6416                 error "Locked pages remain in cache, locked=$LOCKED"
6417         fi
6418
6419         # in recoverable error on OST we want resend and stay until it finished
6420         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6421                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6422         fi
6423
6424         rm -f $DIR/$tfile
6425         echo "No pages locked after fsync"
6426
6427         return 0
6428 }
6429 run_test 118j "Simulate unrecoverable OST side error =========="
6430
6431 test_118k()
6432 {
6433         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6434
6435         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6436         set_nodes_failloc "$(osts_nodes)" 0x20e
6437         mkdir -p $DIR/$tdir
6438
6439         for ((i=0;i<10;i++)); do
6440                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6441                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6442                 SLEEPPID=$!
6443                 sleep 0.500s
6444                 kill $SLEEPPID
6445                 wait $SLEEPPID
6446         done
6447
6448         set_nodes_failloc "$(osts_nodes)" 0
6449         rm -rf $DIR/$tdir
6450 }
6451 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6452
6453 test_118l()
6454 {
6455         # LU-646
6456         mkdir -p $DIR/$tdir
6457         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6458         rm -rf $DIR/$tdir
6459 }
6460 run_test 118l "fsync dir ========="
6461
6462 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6463
6464 test_119a() # bug 11737
6465 {
6466         BSIZE=$((512 * 1024))
6467         directio write $DIR/$tfile 0 1 $BSIZE
6468         # We ask to read two blocks, which is more than a file size.
6469         # directio will indicate an error when requested and actual
6470         # sizes aren't equeal (a normal situation in this case) and
6471         # print actual read amount.
6472         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6473         if [ "$NOB" != "$BSIZE" ]; then
6474                 error "read $NOB bytes instead of $BSIZE"
6475         fi
6476         rm -f $DIR/$tfile
6477 }
6478 run_test 119a "Short directIO read must return actual read amount"
6479
6480 test_119b() # bug 11737
6481 {
6482         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6483
6484         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6485         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6486         sync
6487         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6488                 error "direct read failed"
6489         rm -f $DIR/$tfile
6490 }
6491 run_test 119b "Sparse directIO read must return actual read amount"
6492
6493 test_119c() # bug 13099
6494 {
6495         BSIZE=1048576
6496         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6497         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6498         rm -f $DIR/$tfile
6499 }
6500 run_test 119c "Testing for direct read hitting hole"
6501
6502 test_119d() # bug 15950
6503 {
6504         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6505         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6506         BSIZE=1048576
6507         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6508         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6509         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6510         lctl set_param fail_loc=0x40d
6511         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6512         pid_dio=$!
6513         sleep 1
6514         cat $DIR/$tfile > /dev/null &
6515         lctl set_param fail_loc=0
6516         pid_reads=$!
6517         wait $pid_dio
6518         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6519         sleep 2
6520         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6521         error "the read rpcs have not completed in 2s"
6522         rm -f $DIR/$tfile
6523         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6524 }
6525 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6526
6527 test_120a() {
6528         mkdir -p $DIR/$tdir
6529         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6530                skip "no early lock cancel on server" && return 0
6531         lru_resize_disable mdc
6532         lru_resize_disable osc
6533         cancel_lru_locks mdc
6534         stat $DIR/$tdir > /dev/null
6535         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6536         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6537         mkdir $DIR/$tdir/d1
6538         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6539         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6540         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6541         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6542         lru_resize_enable mdc
6543         lru_resize_enable osc
6544 }
6545 run_test 120a "Early Lock Cancel: mkdir test"
6546
6547 test_120b() {
6548         mkdir -p $DIR/$tdir
6549         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6550                skip "no early lock cancel on server" && return 0
6551         lru_resize_disable mdc
6552         lru_resize_disable osc
6553         cancel_lru_locks mdc
6554         stat $DIR/$tdir > /dev/null
6555         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6556         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6557         touch $DIR/$tdir/f1
6558         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6559         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6560         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6561         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6562         lru_resize_enable mdc
6563         lru_resize_enable osc
6564 }
6565 run_test 120b "Early Lock Cancel: create test"
6566
6567 test_120c() {
6568         mkdir -p $DIR/$tdir
6569         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6570                skip "no early lock cancel on server" && return 0
6571         lru_resize_disable mdc
6572         lru_resize_disable osc
6573         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6574         touch $DIR/$tdir/d1/f1
6575         cancel_lru_locks mdc
6576         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6577         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6578         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6579         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6580         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6581         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6582         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6583         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6584         lru_resize_enable mdc
6585         lru_resize_enable osc
6586 }
6587 run_test 120c "Early Lock Cancel: link test"
6588
6589 test_120d() {
6590         mkdir -p $DIR/$tdir
6591         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6592                skip "no early lock cancel on server" && return 0
6593         lru_resize_disable mdc
6594         lru_resize_disable osc
6595         touch $DIR/$tdir
6596         cancel_lru_locks mdc
6597         stat $DIR/$tdir > /dev/null
6598         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6599         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6600         chmod a+x $DIR/$tdir
6601         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6602         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6603         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6604         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6605         lru_resize_enable mdc
6606         lru_resize_enable osc
6607 }
6608 run_test 120d "Early Lock Cancel: setattr test"
6609
6610 test_120e() {
6611         mkdir -p $DIR/$tdir
6612         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6613                skip "no early lock cancel on server" && return 0
6614         lru_resize_disable mdc
6615         lru_resize_disable osc
6616         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6617         cancel_lru_locks mdc
6618         cancel_lru_locks osc
6619         dd if=$DIR/$tdir/f1 of=/dev/null
6620         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6621         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6622               awk '/ldlm_cancel/ {print $2}'`
6623         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6624               awk '/ldlm_bl_callback/ {print $2}'`
6625         unlink $DIR/$tdir/f1
6626         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6627               awk '/ldlm_cancel/ {print $2}'`
6628         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6629               awk '/ldlm_bl_callback/ {print $2}'`
6630         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6631         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6632         lru_resize_enable mdc
6633         lru_resize_enable osc
6634 }
6635 run_test 120e "Early Lock Cancel: unlink test"
6636
6637 test_120f() {
6638         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6639                skip "no early lock cancel on server" && return 0
6640         mkdir -p $DIR/$tdir
6641         lru_resize_disable mdc
6642         lru_resize_disable osc
6643         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6644         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6645         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6646         cancel_lru_locks mdc
6647         cancel_lru_locks osc
6648         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6649         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6650         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6651         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6652               awk '/ldlm_cancel/ {print $2}'`
6653         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6654               awk '/ldlm_bl_callback/ {print $2}'`
6655         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6656         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6657               awk '/ldlm_cancel/ {print $2}'`
6658         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6659               awk '/ldlm_bl_callback/ {print $2}'`
6660         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6661         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6662         lru_resize_enable mdc
6663         lru_resize_enable osc
6664 }
6665 run_test 120f "Early Lock Cancel: rename test"
6666
6667 test_120g() {
6668         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6669                skip "no early lock cancel on server" && return 0
6670         lru_resize_disable mdc
6671         lru_resize_disable osc
6672         count=10000
6673         echo create $count files
6674         mkdir -p $DIR/$tdir
6675         cancel_lru_locks mdc
6676         cancel_lru_locks osc
6677         t0=`date +%s`
6678
6679         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6680               awk '/ldlm_cancel/ {print $2}'`
6681         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6682               awk '/ldlm_bl_callback/ {print $2}'`
6683         createmany -o $DIR/$tdir/f $count
6684         sync
6685         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6686               awk '/ldlm_cancel/ {print $2}'`
6687         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6688               awk '/ldlm_bl_callback/ {print $2}'`
6689         t1=`date +%s`
6690         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6691         echo rm $count files
6692         rm -r $DIR/$tdir
6693         sync
6694         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6695               awk '/ldlm_cancel/ {print $2}'`
6696         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6697               awk '/ldlm_bl_callback/ {print $2}'`
6698         t2=`date +%s`
6699         echo total: $count removes in $((t2-t1))
6700         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6701         sleep 2
6702         # wait for commitment of removal
6703         lru_resize_enable mdc
6704         lru_resize_enable osc
6705 }
6706 run_test 120g "Early Lock Cancel: performance test"
6707
6708 test_121() { #bug #10589
6709         rm -rf $DIR/$tfile
6710         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6711 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6712         lctl set_param fail_loc=0x310
6713         cancel_lru_locks osc > /dev/null
6714         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6715         lctl set_param fail_loc=0
6716         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6717 }
6718 run_test 121 "read cancel race ========="
6719
6720 test_123a() { # was test 123, statahead(bug 11401)
6721         SLOWOK=0
6722         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6723             log "testing on UP system. Performance may be not as good as expected."
6724                         SLOWOK=1
6725         fi
6726
6727         rm -rf $DIR/$tdir
6728         mkdir -p $DIR/$tdir
6729         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6730         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6731         MULT=10
6732         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6733                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6734
6735                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6736                 lctl set_param -n llite.*.statahead_max 0
6737                 lctl get_param llite.*.statahead_max
6738                 cancel_lru_locks mdc
6739                 cancel_lru_locks osc
6740                 stime=`date +%s`
6741                 time ls -l $DIR/$tdir | wc -l
6742                 etime=`date +%s`
6743                 delta=$((etime - stime))
6744                 log "ls $i files without statahead: $delta sec"
6745                 lctl set_param llite.*.statahead_max=$max
6746
6747                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6748                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6749                 cancel_lru_locks mdc
6750                 cancel_lru_locks osc
6751                 stime=`date +%s`
6752                 time ls -l $DIR/$tdir | wc -l
6753                 etime=`date +%s`
6754                 delta_sa=$((etime - stime))
6755                 log "ls $i files with statahead: $delta_sa sec"
6756                 lctl get_param -n llite.*.statahead_stats
6757                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6758
6759                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6760                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6761
6762                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6763                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6764                     lctl set_param -n llite.*.statahead_max 0
6765                     lctl get_param llite.*.statahead_max
6766                     cancel_lru_locks mdc
6767                     cancel_lru_locks osc
6768                     stime=`date +%s`
6769                     time ls -l $DIR/$tdir | wc -l
6770                     etime=`date +%s`
6771                     delta=$((etime - stime))
6772                     log "ls $i files again without statahead: $delta sec"
6773                     lctl set_param llite.*.statahead_max=$max
6774                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6775                         if [  $SLOWOK -eq 0 ]; then
6776                                 error "ls $i files is slower with statahead!"
6777                         else
6778                                 log "ls $i files is slower with statahead!"
6779                         fi
6780                         break
6781                     fi
6782                 fi
6783
6784                 [ $delta -gt 20 ] && break
6785                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6786                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6787         done
6788         log "ls done"
6789
6790         stime=`date +%s`
6791         rm -r $DIR/$tdir
6792         sync
6793         etime=`date +%s`
6794         delta=$((etime - stime))
6795         log "rm -r $DIR/$tdir/: $delta seconds"
6796         log "rm done"
6797         lctl get_param -n llite.*.statahead_stats
6798 }
6799 run_test 123a "verify statahead work"
6800
6801 test_123b () { # statahead(bug 15027)
6802         mkdir -p $DIR/$tdir
6803         createmany -o $DIR/$tdir/$tfile-%d 1000
6804
6805         cancel_lru_locks mdc
6806         cancel_lru_locks osc
6807
6808 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6809         lctl set_param fail_loc=0x80000803
6810         ls -lR $DIR/$tdir > /dev/null
6811         log "ls done"
6812         lctl set_param fail_loc=0x0
6813         lctl get_param -n llite.*.statahead_stats
6814         rm -r $DIR/$tdir
6815         sync
6816
6817 }
6818 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6819
6820 test_124a() {
6821         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6822                skip "no lru resize on server" && return 0
6823         local NR=2000
6824         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6825
6826         log "create $NR files at $DIR/$tdir"
6827         createmany -o $DIR/$tdir/f $NR ||
6828                 error "failed to create $NR files in $DIR/$tdir"
6829
6830         cancel_lru_locks mdc
6831         ls -l $DIR/$tdir > /dev/null
6832
6833         local NSDIR=""
6834         local LRU_SIZE=0
6835         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6836                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6837                 LRU_SIZE=$(lctl get_param -n $PARAM)
6838                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6839                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6840                                                 log "NSDIR=$NSDIR"
6841                         log "NS=$(basename $NSDIR)"
6842                         break
6843                 fi
6844         done
6845
6846         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
6847                 skip "Not enough cached locks created!"
6848                 return 0
6849         fi
6850         log "LRU=$LRU_SIZE"
6851
6852         local SLEEP=30
6853
6854         # We know that lru resize allows one client to hold $LIMIT locks
6855         # for 10h. After that locks begin to be killed by client.
6856         local MAX_HRS=10
6857         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
6858                 log "LIMIT=$LIMIT"
6859
6860         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
6861         # killing locks. Some time was spent for creating locks. This means
6862         # that up to the moment of sleep finish we must have killed some of
6863         # them (10-100 locks). This depends on how fast ther were created.
6864         # Many of them were touched in almost the same moment and thus will
6865         # be killed in groups.
6866         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
6867
6868         # Use $LRU_SIZE_B here to take into account real number of locks
6869         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
6870         local LRU_SIZE_B=$LRU_SIZE
6871         log "LVF=$LVF"
6872         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
6873                 log "OLD_LVF=$OLD_LVF"
6874         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
6875
6876         # Let's make sure that we really have some margin. Client checks
6877         # cached locks every 10 sec.
6878         SLEEP=$((SLEEP+20))
6879         log "Sleep ${SLEEP} sec"
6880         local SEC=0
6881         while ((SEC<$SLEEP)); do
6882                 echo -n "..."
6883                 sleep 5
6884                 SEC=$((SEC+5))
6885                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
6886                 echo -n "$LRU_SIZE"
6887         done
6888         echo ""
6889         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
6890         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
6891
6892         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
6893                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
6894                 unlinkmany $DIR/$tdir/f $NR
6895                 return
6896         }
6897
6898         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
6899         log "unlink $NR files at $DIR/$tdir"
6900         unlinkmany $DIR/$tdir/f $NR
6901 }
6902 run_test 124a "lru resize ======================================="
6903
6904 get_max_pool_limit()
6905 {
6906         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
6907         local max=0
6908         for l in $limit; do
6909                 if test $l -gt $max; then
6910                         max=$l
6911                 fi
6912         done
6913         echo $max
6914 }
6915
6916 test_124b() {
6917         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6918                skip "no lru resize on server" && return 0
6919
6920         LIMIT=`get_max_pool_limit`
6921
6922         NR=$(($(default_lru_size)*20))
6923         if [ $NR -gt $LIMIT ]; then
6924                 log "Limit lock number by $LIMIT locks"
6925                 NR=$LIMIT
6926         fi
6927         lru_resize_disable mdc
6928         mkdir -p $DIR/$tdir/disable_lru_resize ||
6929                 error "failed to create $DIR/$tdir/disable_lru_resize"
6930
6931         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
6932         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
6933         cancel_lru_locks mdc
6934         stime=`date +%s`
6935         PID=""
6936         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6937         PID="$PID $!"
6938         sleep 2
6939         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6940         PID="$PID $!"
6941         sleep 2
6942         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6943         PID="$PID $!"
6944         wait $PID
6945         etime=`date +%s`
6946         nolruresize_delta=$((etime-stime))
6947         log "ls -la time: $nolruresize_delta seconds"
6948         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6949         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
6950
6951         lru_resize_enable mdc
6952         mkdir -p $DIR/$tdir/enable_lru_resize ||
6953                 error "failed to create $DIR/$tdir/enable_lru_resize"
6954
6955         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
6956         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
6957         cancel_lru_locks mdc
6958         stime=`date +%s`
6959         PID=""
6960         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6961         PID="$PID $!"
6962         sleep 2
6963         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6964         PID="$PID $!"
6965         sleep 2
6966         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6967         PID="$PID $!"
6968         wait $PID
6969         etime=`date +%s`
6970         lruresize_delta=$((etime-stime))
6971         log "ls -la time: $lruresize_delta seconds"
6972         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6973
6974         if [ $lruresize_delta -gt $nolruresize_delta ]; then
6975                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
6976         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
6977                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
6978         else
6979                 log "lru resize performs the same with no lru resize"
6980         fi
6981         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
6982 }
6983 run_test 124b "lru resize (performance test) ======================="
6984
6985 test_125() { # 13358
6986         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6987         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6988         mkdir -p $DIR/d125 || error "mkdir failed"
6989         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
6990         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
6991         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
6992 }
6993 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
6994
6995 test_126() { # bug 12829/13455
6996         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6997         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
6998         $GSS && skip "must run as gss disabled" && return
6999
7000         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7001         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7002         rm -f $DIR/$tfile
7003         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7004 }
7005 run_test 126 "check that the fsgid provided by the client is taken into account"
7006
7007 test_127a() { # bug 15521
7008         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7009         $LCTL set_param osc.*.stats=0
7010         FSIZE=$((2048 * 1024))
7011         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7012         cancel_lru_locks osc
7013         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7014
7015         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7016         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7017                 echo "got $COUNT $NAME"
7018                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7019                 eval $NAME=$COUNT || error "Wrong proc format"
7020
7021                 case $NAME in
7022                         read_bytes|write_bytes)
7023                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7024                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7025                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7026                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7027                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7028                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7029                                 error "sumsquare is too small: $SUMSQ"
7030                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7031                                 error "sumsquare is too big: $SUMSQ"
7032                         ;;
7033                         *) ;;
7034                 esac
7035         done < $DIR/${tfile}.tmp
7036
7037         #check that we actually got some stats
7038         [ "$read_bytes" ] || error "Missing read_bytes stats"
7039         [ "$write_bytes" ] || error "Missing write_bytes stats"
7040         [ "$read_bytes" != 0 ] || error "no read done"
7041         [ "$write_bytes" != 0 ] || error "no write done"
7042 }
7043 run_test 127a "verify the client stats are sane"
7044
7045 test_127b() { # bug LU-333
7046         $LCTL set_param llite.*.stats=0
7047         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7048         # perform 2 reads and writes so MAX is different from SUM.
7049         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7050         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7051         cancel_lru_locks osc
7052         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7053         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7054
7055         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7056         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7057                 echo "got $COUNT $NAME"
7058                 eval $NAME=$COUNT || error "Wrong proc format"
7059
7060         case $NAME in
7061                 read_bytes)
7062                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7063                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7064                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7065                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7066                         ;;
7067                 write_bytes)
7068                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7069                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7070                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7071                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7072                         ;;
7073                         *) ;;
7074                 esac
7075         done < $TMP/${tfile}.tmp
7076
7077         #check that we actually got some stats
7078         [ "$read_bytes" ] || error "Missing read_bytes stats"
7079         [ "$write_bytes" ] || error "Missing write_bytes stats"
7080         [ "$read_bytes" != 0 ] || error "no read done"
7081         [ "$write_bytes" != 0 ] || error "no write done"
7082 }
7083 run_test 127b "verify the llite client stats are sane"
7084
7085 test_128() { # bug 15212
7086         touch $DIR/$tfile
7087         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7088                 find $DIR/$tfile
7089                 find $DIR/$tfile
7090         EOF
7091
7092         result=$(grep error $TMP/$tfile.log)
7093         rm -f $DIR/$tfile
7094         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7095 }
7096 run_test 128 "interactive lfs for 2 consecutive find's"
7097
7098 set_dir_limits () {
7099         local mntdev
7100         local canondev
7101         local node
7102
7103         local LDPROC=/proc/fs/ldiskfs
7104
7105         for facet in $(get_facets MDS); do
7106                 canondev=$(ldiskfs_canon *.$(convert_facet2label $facet).mntdev $facet)
7107                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" || LDPROC=/sys/fs/ldiskfs
7108                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7109         done
7110 }
7111 test_129() {
7112         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7113                 skip "Only applicable to ldiskfs-based MDTs"
7114                 return
7115         fi
7116         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7117
7118         EFBIG=27
7119         MAX=16384
7120
7121         set_dir_limits $MAX
7122
7123         mkdir -p $DIR/$tdir
7124
7125         I=0
7126         J=0
7127         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7128                 $MULTIOP $DIR/$tdir/$J Oc
7129                 rc=$?
7130                 if [ $rc -eq $EFBIG ]; then
7131                         set_dir_limits 0
7132                         echo "return code $rc received as expected"
7133                         return 0
7134                 elif [ $rc -ne 0 ]; then
7135                         set_dir_limits 0
7136                         error_exit "return code $rc received instead of expected $EFBIG"
7137                 fi
7138                 J=$((J+1))
7139                 I=$(stat -c%s "$DIR/$tdir")
7140         done
7141
7142         set_dir_limits 0
7143         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7144 }
7145 run_test 129 "test directory size limit ========================"
7146
7147 OLDIFS="$IFS"
7148 cleanup_130() {
7149         trap 0
7150         IFS="$OLDIFS"
7151 }
7152
7153 test_130a() {
7154         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7155         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7156                 return
7157
7158         trap cleanup_130 EXIT RETURN
7159
7160         local fm_file=$DIR/$tfile
7161         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7162         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7163                 error "dd failed for $fm_file"
7164
7165         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7166         filefrag -ves $fm_file
7167         RC=$?
7168         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7169                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7170         [ $RC != 0 ] && error "filefrag $fm_file failed"
7171
7172         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7173                       grep -v "ext:" | grep -v "found")
7174         lun=$($GETSTRIPE -i $fm_file)
7175
7176         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7177         IFS=$'\n'
7178         tot_len=0
7179         for line in $filefrag_op
7180         do
7181                 frag_lun=`echo $line | cut -d: -f5`
7182                 ext_len=`echo $line | cut -d: -f4`
7183                 if (( $frag_lun != $lun )); then
7184                         cleanup_130
7185                         error "FIEMAP on 1-stripe file($fm_file) failed"
7186                         return
7187                 fi
7188                 (( tot_len += ext_len ))
7189         done
7190
7191         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7192                 cleanup_130
7193                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7194                 return
7195         fi
7196
7197         cleanup_130
7198
7199         echo "FIEMAP on single striped file succeeded"
7200 }
7201 run_test 130a "FIEMAP (1-stripe file)"
7202
7203 test_130b() {
7204         [ "$OSTCOUNT" -lt "2" ] &&
7205                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7206
7207         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7208         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7209                 return
7210
7211         trap cleanup_130 EXIT RETURN
7212
7213         local fm_file=$DIR/$tfile
7214         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7215         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7216                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7217
7218         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7219                 error "dd failed on $fm_file"
7220
7221         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7222         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7223                       grep -v "ext:" | grep -v "found")
7224
7225         last_lun=$(echo $filefrag_op | cut -d: -f5)
7226
7227         IFS=$'\n'
7228         tot_len=0
7229         num_luns=1
7230         for line in $filefrag_op
7231         do
7232                 frag_lun=`echo $line | cut -d: -f5`
7233                 ext_len=`echo $line | cut -d: -f4`
7234                 if (( $frag_lun != $last_lun )); then
7235                         if (( tot_len != 1024 )); then
7236                                 cleanup_130
7237                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7238                                 return
7239                         else
7240                                 (( num_luns += 1 ))
7241                                 tot_len=0
7242                         fi
7243                 fi
7244                 (( tot_len += ext_len ))
7245                 last_lun=$frag_lun
7246         done
7247         if (( num_luns != 2 || tot_len != 1024 )); then
7248                 cleanup_130
7249                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7250                 return
7251         fi
7252
7253         cleanup_130
7254
7255         echo "FIEMAP on 2-stripe file succeeded"
7256 }
7257 run_test 130b "FIEMAP (2-stripe file)"
7258
7259 test_130c() {
7260         [ "$OSTCOUNT" -lt "2" ] &&
7261                 skip_env "skipping FIEMAP on 2-stripe file" && return
7262
7263         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7264         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7265                 return
7266
7267         trap cleanup_130 EXIT RETURN
7268
7269         local fm_file=$DIR/$tfile
7270         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7271         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7272                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7273
7274         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7275
7276         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7277         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7278
7279         last_lun=`echo $filefrag_op | cut -d: -f5`
7280
7281         IFS=$'\n'
7282         tot_len=0
7283         num_luns=1
7284         for line in $filefrag_op
7285         do
7286                 frag_lun=`echo $line | cut -d: -f5`
7287                 ext_len=`echo $line | cut -d: -f4`
7288                 if (( $frag_lun != $last_lun )); then
7289                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7290                         if (( logical != 512 )); then
7291                                 cleanup_130
7292                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7293                                 return
7294                         fi
7295                         if (( tot_len != 512 )); then
7296                                 cleanup_130
7297                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7298                                 return
7299                         else
7300                                 (( num_luns += 1 ))
7301                                 tot_len=0
7302                         fi
7303                 fi
7304                 (( tot_len += ext_len ))
7305                 last_lun=$frag_lun
7306         done
7307         if (( num_luns != 2 || tot_len != 512 )); then
7308                 cleanup_130
7309                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7310                 return
7311         fi
7312
7313         cleanup_130
7314
7315         echo "FIEMAP on 2-stripe file with hole succeeded"
7316 }
7317 run_test 130c "FIEMAP (2-stripe file with hole)"
7318
7319 test_130d() {
7320         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7321
7322         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7323         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7324
7325         trap cleanup_130 EXIT RETURN
7326
7327         local fm_file=$DIR/$tfile
7328         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7329         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7330                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7331         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7332
7333         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7334         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7335
7336         last_lun=`echo $filefrag_op | cut -d: -f5`
7337
7338         IFS=$'\n'
7339         tot_len=0
7340         num_luns=1
7341         for line in $filefrag_op
7342         do
7343                 frag_lun=`echo $line | cut -d: -f5`
7344                 ext_len=`echo $line | cut -d: -f4`
7345                 if (( $frag_lun != $last_lun )); then
7346                         if (( tot_len != 1024 )); then
7347                                 cleanup_130
7348                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7349                                 return
7350                         else
7351                                 (( num_luns += 1 ))
7352                                 tot_len=0
7353                         fi
7354                 fi
7355                 (( tot_len += ext_len ))
7356                 last_lun=$frag_lun
7357         done
7358         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7359                 cleanup_130
7360                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7361                 return
7362         fi
7363
7364         cleanup_130
7365
7366         echo "FIEMAP on N-stripe file succeeded"
7367 }
7368 run_test 130d "FIEMAP (N-stripe file)"
7369
7370 test_130e() {
7371         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7372
7373         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7374         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7375
7376         trap cleanup_130 EXIT RETURN
7377
7378         local fm_file=$DIR/$tfile
7379         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7380         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7381                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7382
7383         NUM_BLKS=512
7384         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7385         for ((i = 0; i < $NUM_BLKS; i++))
7386         do
7387                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7388         done
7389
7390         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7391         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7392
7393         last_lun=`echo $filefrag_op | cut -d: -f5`
7394
7395         IFS=$'\n'
7396         tot_len=0
7397         num_luns=1
7398         for line in $filefrag_op
7399         do
7400                 frag_lun=`echo $line | cut -d: -f5`
7401                 ext_len=`echo $line | cut -d: -f4`
7402                 if (( $frag_lun != $last_lun )); then
7403                         if (( tot_len != $EXPECTED_LEN )); then
7404                                 cleanup_130
7405                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7406                                 return
7407                         else
7408                                 (( num_luns += 1 ))
7409                                 tot_len=0
7410                         fi
7411                 fi
7412                 (( tot_len += ext_len ))
7413                 last_lun=$frag_lun
7414         done
7415         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7416                 cleanup_130
7417                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7418                 return
7419         fi
7420
7421         cleanup_130
7422
7423         echo "FIEMAP with continuation calls succeeded"
7424 }
7425 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7426
7427 # Test for writev/readv
7428 test_131a() {
7429         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7430         error "writev test failed"
7431         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7432         error "readv failed"
7433         rm -f $DIR/$tfile
7434 }
7435 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7436
7437 test_131b() {
7438         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7439         error "append writev test failed"
7440         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7441         error "append writev test failed"
7442         rm -f $DIR/$tfile
7443 }
7444 run_test 131b "test append writev"
7445
7446 test_131c() {
7447         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7448         error "NOT PASS"
7449 }
7450 run_test 131c "test read/write on file w/o objects"
7451
7452 test_131d() {
7453         rwv -f $DIR/$tfile -w -n 1 1572864
7454         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7455         if [ "$NOB" != 1572864 ]; then
7456                 error "Short read filed: read $NOB bytes instead of 1572864"
7457         fi
7458         rm -f $DIR/$tfile
7459 }
7460 run_test 131d "test short read"
7461
7462 test_131e() {
7463         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7464         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7465         error "read hitting hole failed"
7466         rm -f $DIR/$tfile
7467 }
7468 run_test 131e "test read hitting hole"
7469
7470 get_ost_param() {
7471         local token=$1
7472         local gl_sum=0
7473         for node in $(osts_nodes); do
7474                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7475                 [ x$gl = x"" ] && gl=0
7476                 gl_sum=$((gl_sum + gl))
7477         done
7478         echo $gl
7479 }
7480
7481 som_mode_switch() {
7482         local som=$1
7483         local gl1=$2
7484         local gl2=$3
7485
7486         if [ x$som = x"enabled" ]; then
7487                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7488                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7489                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7490         else
7491                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7492                 MOUNTOPT="$MOUNTOPT,som_preview"
7493                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7494         fi
7495
7496         # do remount to make new mount-conf parameters actual
7497         echo remounting...
7498         sync
7499         stopall
7500         setupall
7501 }
7502
7503 test_132() { #1028, SOM
7504         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7505         local num=$(get_mds_dir $DIR)
7506         local mymds=mds${num}
7507         local MOUNTOPT_SAVE=$MOUNTOPT
7508
7509         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7510         cancel_lru_locks osc
7511
7512         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7513
7514         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7515         stat $DIR/$tfile >/dev/null
7516         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7517         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7518         rm $DIR/$tfile
7519         som_mode_switch $som1 $gl1 $gl2
7520
7521         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7522         cancel_lru_locks osc
7523
7524         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7525         if [ $som1 == $som2 ]; then
7526             error "som is still "$som2
7527             if [ x$som2 = x"enabled" ]; then
7528                 som2="disabled"
7529             else
7530                 som2="enabled"
7531             fi
7532         fi
7533
7534         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7535         stat $DIR/$tfile >/dev/null
7536         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7537         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7538         som_mode_switch $som2 $gl1 $gl2
7539         MOUNTOPT=$MOUNTOPT_SAVE
7540 }
7541 run_test 132 "som avoids glimpse rpc"
7542
7543 check_stats() {
7544         local res
7545         local count
7546         case $1 in
7547         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7548                  ;;
7549         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7550                  ;;
7551         *) error "Wrong argument $1" ;;
7552         esac
7553         echo $res
7554         count=`echo $res | awk '{print $2}'`
7555         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7556         # if the argument $3 is zero, it means any stat increment is ok.
7557         if [ $3 -gt 0 ] ; then
7558                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7559         fi
7560 }
7561
7562 test_133a() {
7563         remote_ost_nodsh && skip "remote OST with nodsh" && return
7564         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7565
7566         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7567                 { skip "MDS doesn't support rename stats"; return; }
7568         local testdir=$DIR/${tdir}/stats_testdir
7569         mkdir -p $DIR/${tdir}
7570
7571         # clear stats.
7572         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7573         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7574
7575         # verify mdt stats first.
7576         mkdir ${testdir} || error "mkdir failed"
7577         check_stats $SINGLEMDS "mkdir" 1
7578         touch ${testdir}/${tfile} || "touch failed"
7579         check_stats $SINGLEMDS "open" 1
7580         check_stats $SINGLEMDS "close" 1
7581         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7582         check_stats $SINGLEMDS "mknod" 1
7583         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7584         check_stats $SINGLEMDS "unlink" 1
7585         rm -f ${testdir}/${tfile} || error "file remove failed"
7586         check_stats $SINGLEMDS "unlink" 2
7587
7588         # remove working dir and check mdt stats again.
7589         rmdir ${testdir} || error "rmdir failed"
7590         check_stats $SINGLEMDS "rmdir" 1
7591
7592         local testdir1=$DIR/${tdir}/stats_testdir1
7593         mkdir -p ${testdir}
7594         mkdir -p ${testdir1}
7595         touch ${testdir1}/test1
7596         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7597         check_stats $SINGLEMDS "crossdir_rename" 1
7598
7599         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7600         check_stats $SINGLEMDS "samedir_rename" 1
7601
7602         rm -rf $DIR/${tdir}
7603 }
7604 run_test 133a "Verifying MDT stats ========================================"
7605
7606 test_133b() {
7607         remote_ost_nodsh && skip "remote OST with nodsh" && return
7608         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7609         local testdir=$DIR/${tdir}/stats_testdir
7610         mkdir -p ${testdir} || error "mkdir failed"
7611         touch ${testdir}/${tfile} || "touch failed"
7612         cancel_lru_locks mdc
7613
7614         # clear stats.
7615         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7616         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7617
7618         # extra mdt stats verification.
7619         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7620         check_stats $SINGLEMDS "setattr" 1
7621         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7622         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
7623         then            # LU-1740
7624                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7625                 check_stats $SINGLEMDS "getattr" 1
7626         fi
7627         $LFS df || error "lfs failed"
7628         check_stats $SINGLEMDS "statfs" 1
7629
7630         rm -rf $DIR/${tdir}
7631 }
7632 run_test 133b "Verifying extra MDT stats =================================="
7633
7634 test_133c() {
7635         remote_ost_nodsh && skip "remote OST with nodsh" && return
7636         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7637         local testdir=$DIR/${tdir}/stats_testdir
7638         mkdir -p ${testdir} || error "mkdir failed"
7639
7640         # verify obdfilter stats.
7641         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7642         sync
7643         cancel_lru_locks osc
7644         wait_delete_completed
7645
7646         # clear stats.
7647         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7648         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7649
7650         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7651         sync
7652         cancel_lru_locks osc
7653         check_stats ost "write" 1
7654
7655         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7656         check_stats ost "read" 1
7657
7658         > ${testdir}/${tfile} || error "truncate failed"
7659         check_stats ost "punch" 1
7660
7661         rm -f ${testdir}/${tfile} || error "file remove failed"
7662         wait_delete_completed
7663         check_stats ost "destroy" 1
7664
7665         rm -rf $DIR/${tdir}
7666 }
7667 run_test 133c "Verifying OST stats ========================================"
7668
7669 order_2() {
7670     local value=$1
7671     local orig=$value
7672     local order=1
7673
7674     while [ $value -ge 2 ]; do
7675         order=$((order*2))
7676         value=$((value/2))
7677     done
7678
7679     if [ $orig -gt $order ]; then
7680         order=$((order*2))
7681     fi
7682     echo $order
7683 }
7684
7685 size_in_KMGT() {
7686     local value=$1
7687     local size=('K' 'M' 'G' 'T');
7688     local i=0
7689     local size_string=$value
7690
7691     while [ $value -ge 1024 ]; do
7692         if [ $i -gt 3 ]; then
7693             #T is the biggest unit we get here, if that is bigger,
7694             #just return XXXT
7695             size_string=${value}T
7696             break
7697         fi
7698         value=$((value >> 10))
7699         if [ $value -lt 1024 ]; then
7700             size_string=${value}${size[$i]}
7701             break
7702         fi
7703         i=$((i + 1))
7704     done
7705
7706     echo $size_string
7707 }
7708
7709 get_rename_size() {
7710     local size=$1
7711     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
7712                    awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7713     echo $sample
7714 }
7715
7716 test_133d() {
7717     remote_ost_nodsh && skip "remote OST with nodsh" && return
7718     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7719     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7720         { skip "MDS doesn't support rename stats"; return; }
7721
7722     local testdir1=$DIR/${tdir}/stats_testdir1
7723     local testdir2=$DIR/${tdir}/stats_testdir2
7724
7725     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7726
7727     mkdir -p ${testdir1} || error "mkdir failed"
7728     mkdir -p ${testdir2} || error "mkdir failed"
7729
7730     createmany -o $testdir1/test 512 || error "createmany failed"
7731
7732         # check samedir rename size
7733         mv ${testdir1}/test0 ${testdir1}/test_0
7734
7735         local testdir1_size=$(ls -l $DIR/${tdir} |
7736                 awk '/stats_testdir1/ {print $5}')
7737         local testdir2_size=$(ls -l $DIR/${tdir} |
7738                 awk '/stats_testdir2/ {print $5}')
7739
7740         testdir1_size=$(order_2 $testdir1_size)
7741         testdir2_size=$(order_2 $testdir2_size)
7742
7743         testdir1_size=$(size_in_KMGT $testdir1_size)
7744         testdir2_size=$(size_in_KMGT $testdir2_size)
7745
7746         echo "source rename dir size: ${testdir1_size}"
7747         echo "target rename dir size: ${testdir2_size}"
7748
7749     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
7750     eval $cmd || error "$cmd failed"
7751     local samedir=$($cmd | grep 'same_dir')
7752     local same_sample=$(get_rename_size $testdir1_size)
7753     [ -z "$samedir" ] && error "samedir_rename_size count error"
7754     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
7755     echo "Check same dir rename stats success"
7756
7757     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7758
7759     # check crossdir rename size
7760     mv ${testdir1}/test_0 ${testdir2}/test_0
7761
7762         testdir1_size=$(ls -l $DIR/${tdir} |
7763                 awk '/stats_testdir1/ {print $5}')
7764         testdir2_size=$(ls -l $DIR/${tdir} |
7765                 awk '/stats_testdir2/ {print $5}')
7766
7767         testdir1_size=$(order_2 $testdir1_size)
7768         testdir2_size=$(order_2 $testdir2_size)
7769
7770         testdir1_size=$(size_in_KMGT $testdir1_size)
7771         testdir2_size=$(size_in_KMGT $testdir2_size)
7772
7773         echo "source rename dir size: ${testdir1_size}"
7774         echo "target rename dir size: ${testdir2_size}"
7775
7776     eval $cmd || error "$cmd failed"
7777     local crossdir=$($cmd | grep 'crossdir')
7778     local src_sample=$(get_rename_size $testdir1_size)
7779     local tgt_sample=$(get_rename_size $testdir2_size)
7780     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
7781     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
7782     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
7783     echo "Check cross dir rename stats success"
7784     rm -rf $DIR/${tdir}
7785 }
7786 run_test 133d "Verifying rename_stats ========================================"
7787
7788 test_140() { #bug-17379
7789         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7790         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7791         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7792
7793         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7794         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
7795         local i=0
7796         while i=`expr $i + 1`; do
7797                 mkdir -p $i || error "Creating dir $i"
7798                 cd $i || error "Changing to $i"
7799                 ln -s ../stat stat || error "Creating stat symlink"
7800                 # Read the symlink until ELOOP present,
7801                 # not LBUGing the system is considered success,
7802                 # we didn't overrun the stack.
7803                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7804                 [ $ret -ne 0 ] && {
7805                         if [ $ret -eq 40 ]; then
7806                                 break  # -ELOOP
7807                         else
7808                                 error "Open stat symlink"
7809                                 return
7810                         fi
7811                 }
7812         done
7813         i=`expr $i - 1`
7814         echo "The symlink depth = $i"
7815         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
7816                                         error "Invalid symlink depth"
7817
7818         # Test recursive symlink
7819         ln -s symlink_self symlink_self
7820         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
7821         echo "open symlink_self returns $ret"
7822         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
7823 }
7824 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7825
7826 test_150() {
7827         local TF="$TMP/$tfile"
7828
7829         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7830         cp $TF $DIR/$tfile
7831         cancel_lru_locks osc
7832         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7833         remount_client $MOUNT
7834         df -P $MOUNT
7835         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7836
7837         $TRUNCATE $TF 6000
7838         $TRUNCATE $DIR/$tfile 6000
7839         cancel_lru_locks osc
7840         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7841
7842         echo "12345" >>$TF
7843         echo "12345" >>$DIR/$tfile
7844         cancel_lru_locks osc
7845         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
7846
7847         echo "12345" >>$TF
7848         echo "12345" >>$DIR/$tfile
7849         cancel_lru_locks osc
7850         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
7851
7852         rm -f $TF
7853         true
7854 }
7855 run_test 150 "truncate/append tests"
7856
7857 function roc_hit() {
7858         local list=$(comma_list $(osts_nodes))
7859
7860         echo $(get_obdfilter_param $list '' stats |
7861                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
7862 }
7863
7864 function set_cache() {
7865         local on=1
7866
7867         if [ "$2" == "off" ]; then
7868                 on=0;
7869         fi
7870         local list=$(comma_list $(osts_nodes))
7871         set_obdfilter_param $list '' $1_cache_enable $on
7872
7873         cancel_lru_locks osc
7874 }
7875
7876 test_151() {
7877         remote_ost_nodsh && skip "remote OST with nodsh" && return
7878
7879         local CPAGES=3
7880         local list=$(comma_list $(osts_nodes))
7881
7882         # check whether obdfilter is cache capable at all
7883         if ! get_obdfilter_param $list '' read_cache_enable >/dev/null; then
7884                 echo "not cache-capable obdfilter"
7885                 return 0
7886         fi
7887
7888         # check cache is enabled on all obdfilters
7889         if get_obdfilter_param $list '' read_cache_enable | grep 0; then
7890                 echo "oss cache is disabled"
7891                 return 0
7892         fi
7893
7894         set_obdfilter_param $list '' writethrough_cache_enable 1
7895
7896         # pages should be in the case right after write
7897         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
7898         local BEFORE=`roc_hit`
7899         cancel_lru_locks osc
7900         cat $DIR/$tfile >/dev/null
7901         local AFTER=`roc_hit`
7902         if ! let "AFTER - BEFORE == CPAGES"; then
7903                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7904         fi
7905
7906         # the following read invalidates the cache
7907         cancel_lru_locks osc
7908         set_obdfilter_param $list '' read_cache_enable 0
7909         cat $DIR/$tfile >/dev/null
7910
7911         # now data shouldn't be found in the cache
7912         BEFORE=`roc_hit`
7913         cancel_lru_locks osc
7914         cat $DIR/$tfile >/dev/null
7915         AFTER=`roc_hit`
7916         if let "AFTER - BEFORE != 0"; then
7917                 error "IN CACHE: before: $BEFORE, after: $AFTER"
7918         fi
7919
7920         set_obdfilter_param $list '' read_cache_enable 1
7921         rm -f $DIR/$tfile
7922 }
7923 run_test 151 "test cache on oss and controls ==============================="
7924
7925 test_152() {
7926         local TF="$TMP/$tfile"
7927
7928         # simulate ENOMEM during write
7929 #define OBD_FAIL_OST_NOMEM      0x226
7930         lctl set_param fail_loc=0x80000226
7931         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7932         cp $TF $DIR/$tfile
7933         sync || error "sync failed"
7934         lctl set_param fail_loc=0
7935
7936         # discard client's cache
7937         cancel_lru_locks osc
7938
7939         # simulate ENOMEM during read
7940         lctl set_param fail_loc=0x80000226
7941         cmp $TF $DIR/$tfile || error "cmp failed"
7942         lctl set_param fail_loc=0
7943
7944         rm -f $TF
7945 }
7946 run_test 152 "test read/write with enomem ============================"
7947
7948 test_153() {
7949         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
7950 }
7951 run_test 153 "test if fdatasync does not crash ======================="
7952
7953 test_154() {
7954         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
7955                 { skip "Need MDS version at least 2.2.51"; return 0; }
7956
7957         cp /etc/hosts $DIR/$tfile
7958
7959         fid=$($LFS path2fid $DIR/$tfile)
7960         rc=$?
7961         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7962
7963         ffid=$DIR/.lustre/fid/$fid
7964
7965         echo "stat fid $fid"
7966         stat $ffid > /dev/null || error "stat $ffid failed."
7967         echo "touch fid $fid"
7968         touch $ffid || error "touch $ffid failed."
7969         echo "write to fid $fid"
7970         cat /etc/hosts > $ffid || error "write $ffid failed."
7971         echo "read fid $fid"
7972         diff /etc/hosts $ffid || error "read $ffid failed."
7973         echo "append write to fid $fid"
7974         cat /etc/hosts >> $ffid || error "append write $ffid failed."
7975         echo "rename fid $fid"
7976         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
7977         touch $DIR/$tfile.1
7978         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
7979         rm -f $DIR/$tfile.1
7980         echo "truncate fid $fid"
7981         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
7982         echo "link fid $fid"
7983         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
7984         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
7985                 echo "setfacl fid $fid"
7986                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
7987                 echo "getfacl fid $fid"
7988                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
7989         fi
7990         echo "unlink fid $fid"
7991         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
7992         echo "mknod fid $fid"
7993         mknod $ffid c 1 3 && error "mknod $ffid should fail."
7994
7995         fid=[0xf00000400:0x1:0x0]
7996         ffid=$DIR/.lustre/fid/$fid
7997
7998         echo "stat non-exist fid $fid"
7999         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8000         echo "write to non-exist fid $fid"
8001         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8002         echo "link new fid $fid"
8003         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8004
8005         mkdir -p $DIR/$tdir
8006         touch $DIR/$tdir/$tfile
8007         fid=$($LFS path2fid $DIR/$tdir)
8008         rc=$?
8009         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8010
8011         ffid=$DIR/.lustre/fid/$fid
8012
8013         echo "ls $fid"
8014         ls $ffid > /dev/null || error "ls $ffid failed."
8015         echo "touch $fid/$tfile.1"
8016         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8017
8018         echo "touch $DIR/.lustre/fid/$tfile"
8019         touch $DIR/.lustre/fid/$tfile && \
8020                 error "touch $DIR/.lustre/fid/$tfile should fail."
8021
8022         echo "setxattr to $DIR/.lustre/fid"
8023         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8024                 error "setxattr should fail."
8025
8026         echo "listxattr for $DIR/.lustre/fid"
8027         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8028                 error "listxattr should fail."
8029
8030         echo "delxattr from $DIR/.lustre/fid"
8031         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8032                 error "delxattr should fail."
8033
8034         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8035         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8036                 error "touch invalid fid should fail."
8037
8038         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8039         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8040                 error "touch non-normal fid should fail."
8041
8042         echo "rename $tdir to $DIR/.lustre/fid"
8043         mrename $DIR/$tdir $DIR/.lustre/fid &&
8044                 error "rename to $DIR/.lustre/fid should fail."
8045
8046         echo "rename .lustre to itself"
8047         fid=$($LFS path2fid $DIR)
8048         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8049                 error "rename .lustre to itself should fail."
8050
8051         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8052         fid=$($LFS path2fid $DIR/$tfile-2)
8053         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8054         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8055                 error "create lov data thru .lustre should fail."
8056         echo "cp /etc/passwd $DIR/$tfile-2"
8057         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8058         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8059         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8060                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8061
8062         echo "Open-by-FID succeeded"
8063 }
8064 run_test 154 "Open-by-FID"
8065
8066 test_155_small_load() {
8067     local temp=$TMP/$tfile
8068     local file=$DIR/$tfile
8069
8070     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8071         error "dd of=$temp bs=6096 count=1 failed"
8072     cp $temp $file
8073     cancel_lru_locks osc
8074     cmp $temp $file || error "$temp $file differ"
8075
8076     $TRUNCATE $temp 6000
8077     $TRUNCATE $file 6000
8078     cmp $temp $file || error "$temp $file differ (truncate1)"
8079
8080     echo "12345" >>$temp
8081     echo "12345" >>$file
8082     cmp $temp $file || error "$temp $file differ (append1)"
8083
8084     echo "12345" >>$temp
8085     echo "12345" >>$file
8086     cmp $temp $file || error "$temp $file differ (append2)"
8087
8088     rm -f $temp $file
8089     true
8090 }
8091
8092 test_155_big_load() {
8093     remote_ost_nodsh && skip "remote OST with nodsh" && return
8094     local temp=$TMP/$tfile
8095     local file=$DIR/$tfile
8096
8097     free_min_max
8098     local cache_size=$(do_facet ost$((MAXI+1)) \
8099         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8100     local large_file_size=$((cache_size * 2))
8101
8102     echo "OSS cache size: $cache_size KB"
8103     echo "Large file size: $large_file_size KB"
8104
8105     [ $MAXV -le $large_file_size ] && \
8106         skip_env "max available OST size needs > $large_file_size KB" && \
8107         return 0
8108
8109     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8110
8111     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8112         error "dd of=$temp bs=$large_file_size count=1k failed"
8113     cp $temp $file
8114     ls -lh $temp $file
8115     cancel_lru_locks osc
8116     cmp $temp $file || error "$temp $file differ"
8117
8118     rm -f $temp $file
8119     true
8120 }
8121
8122 test_155a() {
8123     set_cache read on
8124     set_cache writethrough on
8125     test_155_small_load
8126 }
8127 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8128
8129 test_155b() {
8130     set_cache read on
8131     set_cache writethrough off
8132     test_155_small_load
8133 }
8134 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8135
8136 test_155c() {
8137     set_cache read off
8138     set_cache writethrough on
8139     test_155_small_load
8140 }
8141 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8142
8143 test_155d() {
8144     set_cache read off
8145     set_cache writethrough off
8146     test_155_small_load
8147 }
8148 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8149
8150 test_155e() {
8151     set_cache read on
8152     set_cache writethrough on
8153     test_155_big_load
8154 }
8155 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8156
8157 test_155f() {
8158     set_cache read on
8159     set_cache writethrough off
8160     test_155_big_load
8161 }
8162 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8163
8164 test_155g() {
8165     set_cache read off
8166     set_cache writethrough on
8167     test_155_big_load
8168 }
8169 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8170
8171 test_155h() {
8172     set_cache read off
8173     set_cache writethrough off
8174     test_155_big_load
8175 }
8176 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8177
8178 test_156() {
8179     local CPAGES=3
8180     local BEFORE
8181     local AFTER
8182     local file="$DIR/$tfile"
8183
8184     log "Turn on read and write cache"
8185     set_cache read on
8186     set_cache writethrough on
8187
8188     log "Write data and read it back."
8189     log "Read should be satisfied from the cache."
8190     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8191     BEFORE=`roc_hit`
8192     cancel_lru_locks osc
8193     cat $file >/dev/null
8194     AFTER=`roc_hit`
8195     if ! let "AFTER - BEFORE == CPAGES"; then
8196         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8197     else
8198         log "cache hits:: before: $BEFORE, after: $AFTER"
8199     fi
8200
8201     log "Read again; it should be satisfied from the cache."
8202     BEFORE=$AFTER
8203     cancel_lru_locks osc
8204     cat $file >/dev/null
8205     AFTER=`roc_hit`
8206     if ! let "AFTER - BEFORE == CPAGES"; then
8207         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8208     else
8209         log "cache hits:: before: $BEFORE, after: $AFTER"
8210     fi
8211
8212
8213     log "Turn off the read cache and turn on the write cache"
8214     set_cache read off
8215     set_cache writethrough on
8216
8217     log "Read again; it should be satisfied from the cache."
8218     BEFORE=`roc_hit`
8219     cancel_lru_locks osc
8220     cat $file >/dev/null
8221     AFTER=`roc_hit`
8222     if ! let "AFTER - BEFORE == CPAGES"; then
8223         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8224     else
8225         log "cache hits:: before: $BEFORE, after: $AFTER"
8226     fi
8227
8228     log "Read again; it should not be satisfied from the cache."
8229     BEFORE=$AFTER
8230     cancel_lru_locks osc
8231     cat $file >/dev/null
8232     AFTER=`roc_hit`
8233     if ! let "AFTER - BEFORE == 0"; then
8234         error "IN CACHE: before: $BEFORE, after: $AFTER"
8235     else
8236         log "cache hits:: before: $BEFORE, after: $AFTER"
8237     fi
8238
8239     log "Write data and read it back."
8240     log "Read should be satisfied from the cache."
8241     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8242     BEFORE=`roc_hit`
8243     cancel_lru_locks osc
8244     cat $file >/dev/null
8245     AFTER=`roc_hit`
8246     if ! let "AFTER - BEFORE == CPAGES"; then
8247         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8248     else
8249         log "cache hits:: before: $BEFORE, after: $AFTER"
8250     fi
8251
8252     log "Read again; it should not be satisfied from the cache."
8253     BEFORE=$AFTER
8254     cancel_lru_locks osc
8255     cat $file >/dev/null
8256     AFTER=`roc_hit`
8257     if ! let "AFTER - BEFORE == 0"; then
8258         error "IN CACHE: before: $BEFORE, after: $AFTER"
8259     else
8260         log "cache hits:: before: $BEFORE, after: $AFTER"
8261     fi
8262
8263
8264     log "Turn off read and write cache"
8265     set_cache read off
8266     set_cache writethrough off
8267
8268     log "Write data and read it back"
8269     log "It should not be satisfied from the cache."
8270     rm -f $file
8271     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8272     cancel_lru_locks osc
8273     BEFORE=`roc_hit`
8274     cat $file >/dev/null
8275     AFTER=`roc_hit`
8276     if ! let "AFTER - BEFORE == 0"; then
8277         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8278     else
8279         log "cache hits:: before: $BEFORE, after: $AFTER"
8280     fi
8281
8282
8283     log "Turn on the read cache and turn off the write cache"
8284     set_cache read on
8285     set_cache writethrough off
8286
8287     log "Write data and read it back"
8288     log "It should not be satisfied from the cache."
8289     rm -f $file
8290     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8291     BEFORE=`roc_hit`
8292     cancel_lru_locks osc
8293     cat $file >/dev/null
8294     AFTER=`roc_hit`
8295     if ! let "AFTER - BEFORE == 0"; then
8296         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8297     else
8298         log "cache hits:: before: $BEFORE, after: $AFTER"
8299     fi
8300
8301     log "Read again; it should be satisfied from the cache."
8302     BEFORE=`roc_hit`
8303     cancel_lru_locks osc
8304     cat $file >/dev/null
8305     AFTER=`roc_hit`
8306     if ! let "AFTER - BEFORE == CPAGES"; then
8307         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8308     else
8309         log "cache hits:: before: $BEFORE, after: $AFTER"
8310     fi
8311
8312     rm -f $file
8313 }
8314 run_test 156 "Verification of tunables ============================"
8315
8316 #Changelogs
8317 err17935 () {
8318     if [ $MDSCOUNT -gt 1 ]; then
8319         error_ignore 17935 $*
8320     else
8321         error $*
8322     fi
8323 }
8324
8325 changelog_chmask()
8326 {
8327     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8328            grep -c $1)
8329
8330     if [ $MASK -eq 1 ]; then
8331         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8332     else
8333         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8334     fi
8335 }
8336
8337 test_160() {
8338     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8339     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8340         { skip "Need MDS version at least 2.2.0"; return; }
8341     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8342     echo "Registered as changelog user $USER"
8343     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8344         grep -q $USER || error "User $USER not found in changelog_users"
8345
8346     # change something
8347     mkdir -p $DIR/$tdir/pics/2008/zachy
8348     touch $DIR/$tdir/pics/2008/zachy/timestamp
8349     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8350     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8351     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8352     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8353     rm $DIR/$tdir/pics/desktop.jpg
8354
8355     $LFS changelog $MDT0 | tail -5
8356
8357     echo "verifying changelog mask"
8358     changelog_chmask "MKDIR"
8359     changelog_chmask "CLOSE"
8360
8361     mkdir -p $DIR/$tdir/pics/zach/sofia
8362     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8363
8364     changelog_chmask "MKDIR"
8365     changelog_chmask "CLOSE"
8366
8367     mkdir -p $DIR/$tdir/pics/2008/sofia
8368     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8369
8370     $LFS changelog $MDT0
8371     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8372     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8373     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8374     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8375
8376     # verify contents
8377     echo "verifying target fid"
8378     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8379         tail -1 | awk '{print $6}')
8380     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8381     [ "$fidc" == "t=$fidf" ] || \
8382         err17935 "fid in changelog $fidc != file fid $fidf"
8383     echo "verifying parent fid"
8384     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8385         tail -1 | awk '{print $7}')
8386     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8387     [ "$fidc" == "p=$fidf" ] || \
8388         err17935 "pfid in changelog $fidc != dir fid $fidf"
8389
8390     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8391         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8392     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8393     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8394         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8395     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8396     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8397         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8398
8399     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8400         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8401     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8402     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8403     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8404         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8405
8406     echo "verifying user deregister"
8407     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8408     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8409         grep -q $USER && error "User $USER still found in changelog_users"
8410
8411     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8412         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8413     if [ $USERS -eq 0 ]; then
8414         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8415             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8416         touch $DIR/$tdir/chloe
8417         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8418             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8419         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8420         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8421     else
8422         echo "$USERS other changelog users; can't verify off"
8423     fi
8424 }
8425 run_test 160 "changelog sanity"
8426
8427 test_161() {
8428     mkdir -p $DIR/$tdir
8429     cp /etc/hosts $DIR/$tdir/$tfile
8430     mkdir $DIR/$tdir/foo1
8431     mkdir $DIR/$tdir/foo2
8432     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8433     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8434     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8435     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8436     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8437     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8438         $LFS fid2path $DIR $FID
8439         err17935 "bad link ea"
8440     fi
8441     # middle
8442     rm $DIR/$tdir/foo2/zachary
8443     # last
8444     rm $DIR/$tdir/foo2/thor
8445     # first
8446     rm $DIR/$tdir/$tfile
8447     # rename
8448     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8449     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8450         then
8451         $LFS fid2path $DIR $FID
8452         err17935 "bad link rename"
8453     fi
8454     rm $DIR/$tdir/foo2/maggie
8455
8456     # overflow the EA
8457     local longname=filename_avg_len_is_thirty_two_
8458     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8459         error "failed to hardlink many files"
8460     links=$($LFS fid2path $DIR $FID | wc -l)
8461     echo -n "${links}/1000 links in link EA"
8462     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8463     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8464         error "failed to unlink many hardlinks"
8465 }
8466 run_test 161 "link ea sanity"
8467
8468 check_path() {
8469     local expected=$1
8470     shift
8471     local fid=$2
8472
8473     local path=$(${LFS} fid2path $*)
8474     RC=$?
8475
8476     if [ $RC -ne 0 ]; then
8477         err17935 "path looked up of $expected failed. Error $RC"
8478         return $RC
8479     elif [ "${path}" != "${expected}" ]; then
8480         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8481         return 2
8482     fi
8483     echo "fid $fid resolves to path $path (expected $expected)"
8484 }
8485
8486 test_162() {
8487         # Make changes to filesystem
8488         mkdir -p $DIR/$tdir/d2
8489         touch $DIR/$tdir/d2/$tfile
8490         touch $DIR/$tdir/d2/x1
8491         touch $DIR/$tdir/d2/x2
8492         mkdir -p $DIR/$tdir/d2/a/b/c
8493         mkdir -p $DIR/$tdir/d2/p/q/r
8494         # regular file
8495         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8496         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8497
8498         # softlink
8499         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8500         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8501         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8502
8503         # softlink to wrong file
8504         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8505         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8506         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8507
8508         # hardlink
8509         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8510         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8511         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8512         # fid2path dir/fsname should both work
8513         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8514         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8515
8516         # hardlink count: check that there are 2 links
8517         # Doesnt work with CMD yet: 17935
8518         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8519                 err17935 "expected 2 links"
8520
8521         # hardlink indexing: remove the first link
8522         rm $DIR/$tdir/d2/p/q/r/hlink
8523         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8524
8525         return 0
8526 }
8527 run_test 162 "path lookup sanity"
8528
8529 test_163() {
8530         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8531         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8532         copytool $FSNAME &
8533         sleep 1
8534         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8535         # this proc file is temporary and linux-only
8536         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8537          error "kernel->userspace send failed"
8538         kill -INT $!
8539 }
8540 run_test 163 "kernel <-> userspace comms"
8541
8542 test_169() {
8543         # do directio so as not to populate the page cache
8544         log "creating a 10 Mb file"
8545         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8546         log "starting reads"
8547         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8548         log "truncating the file"
8549         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8550         log "killing dd"
8551         kill %+ || true # reads might have finished
8552         echo "wait until dd is finished"
8553         wait
8554         log "removing the temporary file"
8555         rm -rf $DIR/$tfile || error "tmp file removal failed"
8556 }
8557 run_test 169 "parallel read and truncate should not deadlock"
8558
8559 test_170() {
8560         $LCTL clear     # bug 18514
8561         $LCTL debug_daemon start $TMP/${tfile}_log_good
8562         touch $DIR/$tfile
8563         $LCTL debug_daemon stop
8564         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8565                error "sed failed to read log_good"
8566
8567         $LCTL debug_daemon start $TMP/${tfile}_log_good
8568         rm -rf $DIR/$tfile
8569         $LCTL debug_daemon stop
8570
8571         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8572                error "lctl df log_bad failed"
8573
8574         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8575         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8576
8577         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8578         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8579
8580         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8581                 error "bad_line good_line1 good_line2 are empty"
8582
8583         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8584         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8585         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8586
8587         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8588         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8589         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8590
8591         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8592                 error "bad_line_new good_line_new are empty"
8593
8594         local expected_good=$((good_line1 + good_line2*2))
8595
8596         rm -f $TMP/${tfile}*
8597         # LU-231, short malformed line may not be counted into bad lines
8598         if [ $bad_line -ne $bad_line_new ] &&
8599                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8600                 error "expected $bad_line bad lines, but got $bad_line_new"
8601                 return 1
8602         fi
8603
8604         if [ $expected_good -ne $good_line_new ]; then
8605                 error "expected $expected_good good lines, but got $good_line_new"
8606                 return 2
8607         fi
8608         true
8609 }
8610 run_test 170 "test lctl df to handle corrupted log ====================="
8611
8612 test_171() { # bug20592
8613 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8614         $LCTL set_param fail_loc=0x50e
8615         $LCTL set_param fail_val=3000
8616         multiop_bg_pause $DIR/$tfile O_s || true
8617         local MULTIPID=$!
8618         kill -USR1 $MULTIPID
8619         # cause log dump
8620         sleep 3
8621         wait $MULTIPID
8622         if dmesg | grep "recursive fault"; then
8623                 error "caught a recursive fault"
8624         fi
8625         $LCTL set_param fail_loc=0
8626         true
8627 }
8628 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8629
8630 # it would be good to share it with obdfilter-survey/libecho code
8631 setup_obdecho_osc () {
8632         local rc=0
8633         local ost_nid=$1
8634         local obdfilter_name=$2
8635         echo "Creating new osc for $obdfilter_name on $ost_nid"
8636         # make sure we can find loopback nid
8637         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8638
8639         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8640                            ${obdfilter_name}_osc_UUID || rc=2; }
8641         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8642                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8643         return $rc
8644 }
8645
8646 cleanup_obdecho_osc () {
8647         local obdfilter_name=$1
8648         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8649         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8650         return 0
8651 }
8652
8653 obdecho_create_test() {
8654         local OBD=$1
8655         local node=$2
8656         local rc=0
8657         local id
8658         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8659         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8660                            rc=2; }
8661         if [ $rc -eq 0 ]; then
8662             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8663             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8664         fi
8665         echo "New object id is $id"
8666         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8667                            rc=4; }
8668         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8669                                         "cleanup" || rc=5; }
8670         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8671                                         "detach" || rc=6; }
8672         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8673         return $rc
8674 }
8675
8676 test_180a() {
8677         remote_ost_nodsh && skip "remote OST with nodsh" && return
8678         local rc=0
8679         local rmmod_local=0
8680
8681         if ! module_loaded obdecho; then
8682             load_module obdecho/obdecho
8683             rmmod_local=1
8684         fi
8685
8686         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8687         local host=$(lctl get_param -n osc.$osc.import |
8688                              awk '/current_connection:/ {print $2}' )
8689         local target=$(lctl get_param -n osc.$osc.import |
8690                              awk '/target:/ {print $2}' )
8691         target=${target%_UUID}
8692
8693         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8694         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8695         [[ -n $target ]] && cleanup_obdecho_osc $target
8696         [ $rmmod_local -eq 1 ] && rmmod obdecho
8697         return $rc
8698 }
8699 run_test 180a "test obdecho on osc"
8700
8701 test_180b() {
8702         remote_ost_nodsh && skip "remote OST with nodsh" && return
8703         local rc=0
8704         local rmmod_remote=0
8705
8706         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8707                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8708                       "modprobe obdecho; }" && rmmod_remote=1
8709         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8710         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8711         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8712         return $rc
8713 }
8714 run_test 180b "test obdecho directly on obdfilter"
8715
8716 test_181() { # bug 22177
8717         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8718         # create enough files to index the directory
8719         createmany -o $DIR/$tdir/foobar 4000
8720         # print attributes for debug purpose
8721         lsattr -d .
8722         # open dir
8723         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8724         MULTIPID=$!
8725         # remove the files & current working dir
8726         unlinkmany $DIR/$tdir/foobar 4000
8727         rmdir $DIR/$tdir
8728         kill -USR1 $MULTIPID
8729         wait $MULTIPID
8730         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
8731         return 0
8732 }
8733 run_test 181 "Test open-unlinked dir ========================"
8734
8735 test_182() {
8736         # disable MDC RPC lock wouldn't crash client
8737         local fcount=1000
8738         local tcount=4
8739
8740         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8741 #define OBD_FAIL_MDC_RPCS_SEM           0x804
8742         $LCTL set_param fail_loc=0x804
8743
8744         for (( i=0; i < $tcount; i++ )) ; do
8745                 mkdir $DIR/$tdir/$i
8746                 createmany -o $DIR/$tdir/$i/f- $fcount &
8747         done
8748         wait
8749
8750         for (( i=0; i < $tcount; i++ )) ; do
8751                 unlinkmany $DIR/$tdir/$i/f- $fcount &
8752         done
8753         wait
8754
8755         rm -rf $DIR/$tdir
8756
8757         $LCTL set_param fail_loc=0
8758 }
8759 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
8760
8761 # OST pools tests
8762 check_file_in_pool()
8763 {
8764         local file=$1
8765         local pool=$2
8766         local tlist="$3"
8767         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
8768         for i in $res
8769         do
8770                 for t in $tlist ; do
8771                         [ "$i" -eq "$t" ] && continue 2
8772                 done
8773
8774                 echo "pool list: $tlist"
8775                 echo "striping: $res"
8776                 error_noexit "$file not allocated in $pool"
8777                 return 1
8778         done
8779         return 0
8780 }
8781
8782 pool_add() {
8783         echo "Creating new pool"
8784         local pool=$1
8785
8786         create_pool $FSNAME.$pool ||
8787                 { error_noexit "No pool created, result code $?"; return 1; }
8788         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
8789                 { error_noexit "$pool not in lfs pool_list"; return 2; }
8790 }
8791
8792 pool_add_targets() {
8793         echo "Adding targets to pool"
8794         local pool=$1
8795         local first=$2
8796         local last=$3
8797         local step=${4:-1}
8798
8799         local list=$(seq $first $step $last)
8800
8801         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
8802         do_facet mgs $LCTL pool_add \
8803                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
8804         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
8805                         | sort -u | tr '\n' ' ' " "$t" || { 
8806                 error_noexit "Add to pool failed"
8807                 return 1
8808         }
8809         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
8810         local addcount=$(((last - first) / step + 1))
8811         [ $lfscount -eq $addcount ] || {
8812                 error_noexit "lfs pool_list bad ost count" \
8813                                                 "$lfscount != $addcount"
8814                 return 2
8815         }
8816 }
8817
8818 pool_set_dir() {
8819         local pool=$1
8820         local tdir=$2
8821         echo "Setting pool on directory $tdir"
8822
8823         $SETSTRIPE -c 2 -p $pool $tdir && return 0
8824
8825         error_noexit "Cannot set pool $pool to $tdir"
8826         return 1
8827 }
8828
8829 pool_check_dir() {
8830         local pool=$1
8831         local tdir=$2
8832         echo "Checking pool on directory $tdir"
8833
8834         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
8835         [ "$res" = "$pool" ] && return 0
8836
8837         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
8838         return 1
8839 }
8840
8841 pool_dir_rel_path() {
8842         echo "Testing relative path works well"
8843         local pool=$1
8844         local tdir=$2
8845         local root=$3
8846
8847         mkdir -p $root/$tdir/$tdir
8848         cd $root/$tdir
8849         pool_set_dir $pool $tdir          || return 1
8850         pool_set_dir $pool ./$tdir        || return 2
8851         pool_set_dir $pool ../$tdir       || return 3
8852         pool_set_dir $pool ../$tdir/$tdir || return 4
8853         rm -rf $tdir; cd - > /dev/null
8854 }
8855
8856 pool_alloc_files() {
8857         echo "Checking files allocation from directory pool"
8858         local pool=$1
8859         local tdir=$2
8860         local count=$3
8861         local tlist="$4"
8862
8863         local failed=0
8864         for i in $(seq -w 1 $count)
8865         do
8866                 local file=$tdir/file-$i
8867                 touch $file
8868                 check_file_in_pool $file $pool "$tlist" || \
8869                         failed=$((failed + 1))
8870         done
8871         [ "$failed" = 0 ] && return 0
8872
8873         error_noexit "$failed files not allocated in $pool"
8874         return 1
8875 }
8876
8877 pool_create_files() {
8878         echo "Creating files in pool"
8879         local pool=$1
8880         local tdir=$2
8881         local count=$3
8882         local tlist="$4"
8883
8884         mkdir -p $tdir
8885         local failed=0
8886         for i in $(seq -w 1 $count)
8887         do
8888                 local file=$tdir/spoo-$i
8889                 $SETSTRIPE -p $pool $file
8890                 check_file_in_pool $file $pool "$tlist" || \
8891                         failed=$((failed + 1))
8892         done
8893         [ "$failed" = 0 ] && return 0
8894
8895         error_noexit "$failed files not allocated in $pool"
8896         return 1
8897 }
8898
8899 pool_lfs_df() {
8900         echo "Checking 'lfs df' output"
8901         local pool=$1
8902
8903         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
8904                         tr '\n' ' ')
8905         local res=$($LFS df --pool $FSNAME.$pool |
8906                         awk '{print $1}' |
8907                         grep "$FSNAME-OST" |
8908                         tr '\n' ' ')
8909         [ "$res" = "$t" ] && return 0
8910
8911         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
8912         return 1
8913 }
8914
8915 pool_file_rel_path() {
8916         echo "Creating files in a pool with relative pathname"
8917         local pool=$1
8918         local tdir=$2
8919
8920         mkdir -p $tdir ||
8921                 { error_noexit "unable to create $tdir"; return 1 ; }
8922         local file="/..$tdir/$tfile-1"
8923         $SETSTRIPE -p $pool $file ||
8924                 { error_noexit "unable to create $file" ; return 2 ; }
8925
8926         cd $tdir
8927         $SETSTRIPE -p $pool $tfile-2 || {
8928                 error_noexit "unable to create $tfile-2 in $tdir"
8929                 return 3
8930         }
8931 }
8932
8933 pool_remove_first_target() {
8934         echo "Removing first target from a pool"
8935         local pool=$1
8936
8937         local pname="lov.$FSNAME-*.pools.$pool"
8938         local t=$($LCTL get_param -n $pname | head -1)
8939         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8940         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
8941                 error_noexit "$t not removed from $FSNAME.$pool"
8942                 return 1
8943         }
8944 }
8945
8946 pool_remove_all_targets() {
8947         echo "Removing all targets from pool"
8948         local pool=$1
8949         local file=$2
8950         local pname="lov.$FSNAME-*.pools.$pool"
8951         for t in $($LCTL get_param -n $pname | sort -u)
8952         do
8953                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8954         done
8955         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
8956                 error_noexit "Pool $FSNAME.$pool cannot be drained"
8957                 return 1
8958         }
8959         # striping on an empty/nonexistant pool should fall back 
8960         # to "pool of everything"
8961         touch $file || {
8962                 error_noexit "failed to use fallback striping for empty pool"
8963                 return 2
8964         }
8965         # setstripe on an empty pool should fail
8966         $SETSTRIPE -p $pool $file 2>/dev/null && {
8967                 error_noexit "expected failure when creating file" \
8968                                                         "with empty pool"
8969                 return 3
8970         }
8971         return 0
8972 }
8973
8974 pool_remove() {
8975         echo "Destroying pool"
8976         local pool=$1
8977         local file=$2
8978
8979         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
8980
8981         sleep 2
8982         # striping on an empty/nonexistant pool should fall back 
8983         # to "pool of everything"
8984         touch $file || {
8985                 error_noexit "failed to use fallback striping for missing pool"
8986                 return 1
8987         }
8988         # setstripe on an empty pool should fail
8989         $SETSTRIPE -p $pool $file 2>/dev/null && {
8990                 error_noexit "expected failure when creating file" \
8991                                                         "with missing pool"
8992                 return 2
8993         }
8994
8995         # get param should return err once pool is gone
8996         if wait_update $HOSTNAME "lctl get_param -n \
8997                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
8998         then
8999                 remove_pool_from_list $FSNAME.$pool
9000                 return 0
9001         fi
9002         error_noexit "Pool $FSNAME.$pool is not destroyed"
9003         return 3
9004 }
9005
9006 test_200() {
9007         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9008
9009         local POOL=${POOL:-cea1}
9010         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9011         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9012         # Pool OST targets
9013         local first_ost=0
9014         local last_ost=$(($OSTCOUNT - 1))
9015         local ost_step=2
9016         local ost_list=$(seq $first_ost $ost_step $last_ost)
9017         local ost_range="$first_ost $last_ost $ost_step"
9018         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9019         local file_dir=$POOL_ROOT/file_tst
9020
9021         local rc=0
9022         while : ; do
9023                 # former test_200a test_200b
9024                 pool_add $POOL                          || { rc=$? ; break; }
9025                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9026                 # former test_200c test_200d
9027                 mkdir -p $test_path
9028                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9029                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9030                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9031                                                         || { rc=$? ; break; }
9032                 # former test_200e test_200f
9033                 local files=$((OSTCOUNT*3))
9034                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9035                                                         || { rc=$? ; break; }
9036                 pool_create_files $POOL $file_dir $files "$ost_list" \
9037                                                         || { rc=$? ; break; }
9038                 # former test_200g test_200h
9039                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9040                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9041
9042                 # former test_201a test_201b test_201c
9043                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9044
9045                 local f=$test_path/$tfile
9046                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9047                 pool_remove $POOL $f                    || { rc=$? ; break; }
9048                 break
9049         done
9050
9051         cleanup_pools
9052         return $rc
9053 }
9054 run_test 200 "OST pools"
9055
9056 # usage: default_attr <count | size | offset>
9057 default_attr() {
9058         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9059 }
9060
9061 # usage: check_default_stripe_attr
9062 check_default_stripe_attr() {
9063         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9064         case $1 in
9065         --stripe-count|--count)
9066                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9067         --stripe-size|--size)
9068                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9069         --stripe-index|--index)
9070                 EXPECTED=-1;;
9071         *)
9072                 error "unknown getstripe attr '$1'"
9073         esac
9074
9075         [ $ACTUAL != $EXPECTED ] &&
9076                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9077 }
9078
9079 test_204a() {
9080         mkdir -p $DIR/$tdir
9081         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9082
9083         check_default_stripe_attr --stripe-count
9084         check_default_stripe_attr --stripe-size
9085         check_default_stripe_attr --stripe-index
9086
9087         return 0
9088 }
9089 run_test 204a "Print default stripe attributes ================="
9090
9091 test_204b() {
9092         mkdir -p $DIR/$tdir
9093         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9094
9095         check_default_stripe_attr --stripe-size
9096         check_default_stripe_attr --stripe-index
9097
9098         return 0
9099 }
9100 run_test 204b "Print default stripe size and offset  ==========="
9101
9102 test_204c() {
9103         mkdir -p $DIR/$tdir
9104         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9105
9106         check_default_stripe_attr --stripe-count
9107         check_default_stripe_attr --stripe-index
9108
9109         return 0
9110 }
9111 run_test 204c "Print default stripe count and offset ==========="
9112
9113 test_204d() {
9114         mkdir -p $DIR/$tdir
9115         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9116
9117         check_default_stripe_attr --stripe-count
9118         check_default_stripe_attr --stripe-size
9119
9120         return 0
9121 }
9122 run_test 204d "Print default stripe count and size ============="
9123
9124 test_204e() {
9125         mkdir -p $DIR/$tdir
9126         $SETSTRIPE -d $DIR/$tdir
9127
9128         check_default_stripe_attr --stripe-count --raw
9129         check_default_stripe_attr --stripe-size --raw
9130         check_default_stripe_attr --stripe-index --raw
9131
9132         return 0
9133 }
9134 run_test 204e "Print raw stripe attributes ================="
9135
9136 test_204f() {
9137         mkdir -p $DIR/$tdir
9138         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9139
9140         check_default_stripe_attr --stripe-size --raw
9141         check_default_stripe_attr --stripe-index --raw
9142
9143         return 0
9144 }
9145 run_test 204f "Print raw stripe size and offset  ==========="
9146
9147 test_204g() {
9148         mkdir -p $DIR/$tdir
9149         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9150
9151         check_default_stripe_attr --stripe-count --raw
9152         check_default_stripe_attr --stripe-index --raw
9153
9154         return 0
9155 }
9156 run_test 204g "Print raw stripe count and offset ==========="
9157
9158 test_204h() {
9159         mkdir -p $DIR/$tdir
9160         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9161
9162         check_default_stripe_attr --stripe-count --raw
9163         check_default_stripe_attr --stripe-size --raw
9164
9165         return 0
9166 }
9167 run_test 204h "Print raw stripe count and size ============="
9168
9169 # Figure out which job scheduler is being used, if any,
9170 # or use a fake one
9171 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9172         JOBENV=SLURM_JOB_ID
9173 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9174         JOBENV=LSB_JOBID
9175 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9176         JOBENV=PBS_JOBID
9177 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9178         JOBENV=LOADL_STEP_ID
9179 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9180         JOBENV=JOB_ID
9181 else
9182         JOBENV=FAKE_JOBID
9183 fi
9184
9185 verify_jobstats() {
9186         local cmd=$1
9187         local target=$2
9188
9189         # clear old jobstats
9190         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9191         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9192
9193         # use a new JobID for this test, or we might see an old one
9194         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9195
9196         JOBVAL=${!JOBENV}
9197         log "Test: $cmd"
9198         log "Using JobID environment variable $JOBENV=$JOBVAL"
9199
9200         if [ $JOBENV = "FAKE_JOBID" ]; then
9201                 FAKE_JOBID=$JOBVAL $cmd
9202         else
9203                 $cmd
9204         fi
9205
9206         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9207                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9208                 do_facet $FACET lctl get_param mdt.*.job_stats |
9209                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9210         fi
9211         if [ "$target" = "ost" -o "$target" = "both" ]; then
9212                 FACET=ost1
9213                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9214                         grep $JOBVAL || error "No job stats found on OST $FACET"
9215         fi
9216 }
9217
9218 test_205() { # Job stats
9219         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9220                 skip "Server doesn't support jobstats" && return 0
9221
9222         local cmd
9223         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9224         if [ $OLD_JOBENV != $JOBENV ]; then
9225                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9226                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9227                         $JOBENV || return 1
9228         fi
9229
9230         # mkdir
9231         cmd="mkdir $DIR/$tfile"
9232         verify_jobstats "$cmd" "mdt"
9233         # rmdir
9234         cmd="rm -fr $DIR/$tfile"
9235         verify_jobstats "$cmd" "mdt"
9236         # mknod
9237         cmd="mknod $DIR/$tfile c 1 3"
9238         verify_jobstats "$cmd" "mdt"
9239         # unlink
9240         cmd="rm -f $DIR/$tfile"
9241         verify_jobstats "$cmd" "mdt"
9242         # open & close
9243         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9244         verify_jobstats "$cmd" "mdt"
9245         # setattr
9246         cmd="touch $DIR/$tfile"
9247         verify_jobstats "$cmd" "both"
9248         # write
9249         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9250         verify_jobstats "$cmd" "ost"
9251         # read
9252         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9253         verify_jobstats "$cmd" "ost"
9254         # truncate
9255         cmd="$TRUNCATE $DIR/$tfile 0"
9256         verify_jobstats "$cmd" "both"
9257         # rename
9258         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9259         verify_jobstats "$cmd" "mdt"
9260
9261         # cleanup
9262         rm -f $DIR/jobstats_test_rename
9263
9264         if [ $OLD_JOBENV != $JOBENV ]; then
9265                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9266                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9267                         $OLD_JOBENV || return 1
9268         fi
9269 }
9270 run_test 205 "Verify job stats"
9271
9272 # LU-1480, LU-1773 and LU-1657
9273 test_206() {
9274         mkdir -p $DIR/$tdir
9275         lfs setstripe -c -1 $DIR/$tdir
9276 #define OBD_FAIL_LOV_INIT 0x1403
9277         $LCTL set_param fail_loc=0xa0001403
9278         $LCTL set_param fail_val=1
9279         touch $DIR/$tdir/$tfile || true
9280 }
9281 run_test 206 "fail lov_init_raid0() doesn't lbug"
9282
9283 test_212() {
9284         size=`date +%s`
9285         size=$((size % 8192 + 1))
9286         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9287         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9288         rm -f $DIR/f212 $DIR/f212.xyz
9289 }
9290 run_test 212 "Sendfile test ============================================"
9291
9292 test_213() {
9293         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9294         cancel_lru_locks osc
9295         lctl set_param fail_loc=0x8000040f
9296         # generate a read lock
9297         cat $DIR/$tfile > /dev/null
9298         # write to the file, it will try to cancel the above read lock.
9299         cat /etc/hosts >> $DIR/$tfile
9300 }
9301 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9302
9303 test_214() { # for bug 20133
9304         mkdir -p $DIR/d214p/d214c
9305         for (( i=0; i < 340; i++ )) ; do
9306                 touch $DIR/d214p/d214c/a$i
9307         done
9308
9309         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9310         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9311         ls $DIR/d214c || error "ls $DIR/d214c failed"
9312         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9313 }
9314 run_test 214 "hash-indexed directory test - bug 20133"
9315
9316 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9317 create_lnet_proc_files() {
9318         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9319         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9320
9321         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9322         rm -f "$TMP/lnet_$1.sys_tmp"
9323 }
9324
9325 # counterpart of create_lnet_proc_files
9326 remove_lnet_proc_files() {
9327         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9328 }
9329
9330 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9331 # 3rd arg as regexp for body
9332 check_lnet_proc_stats() {
9333         local l=$(cat "$TMP/lnet_$1" |wc -l)
9334         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9335
9336         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9337 }
9338
9339 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9340 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9341 # optional and can be regexp for 2nd line (lnet.routes case)
9342 check_lnet_proc_entry() {
9343         local blp=2            # blp stands for 'position of 1st line of body'
9344         [ "$5" = "" ] || blp=3 # lnet.routes case
9345
9346         local l=$(cat "$TMP/lnet_$1" |wc -l)
9347         # subtracting one from $blp because the body can be empty
9348         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9349
9350         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9351                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9352
9353         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9354                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9355
9356         # bail out if any unexpected line happened
9357         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9358         [ "$?" != 0 ] || error "$2 misformatted"
9359 }
9360
9361 test_215() { # for bugs 18102, 21079, 21517
9362         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9363         local P='[1-9][0-9]*'           # positive numeric
9364         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9365         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9366         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9367         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9368
9369         local L1 # regexp for 1st line
9370         local L2 # regexp for 2nd line (optional)
9371         local BR # regexp for the rest (body)
9372
9373         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9374         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9375         create_lnet_proc_files "stats"
9376         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9377         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9378         remove_lnet_proc_files "stats"
9379
9380         # /proc/sys/lnet/routes should look like this:
9381         # Routing disabled/enabled
9382         # net hops state router
9383         # where net is a string like tcp0, hops >= 0, state is up/down,
9384         # router is a string like 192.168.1.1@tcp2
9385         L1="^Routing (disabled|enabled)$"
9386         L2="^net +hops +state +router$"
9387         BR="^$NET +$N +(up|down) +$NID$"
9388         create_lnet_proc_files "routes"
9389         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9390         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9391         remove_lnet_proc_files "routes"
9392
9393         # /proc/sys/lnet/routers should look like this:
9394         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9395         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9396         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9397         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9398         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9399         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9400         create_lnet_proc_files "routers"
9401         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9402         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9403         remove_lnet_proc_files "routers"
9404
9405         # /proc/sys/lnet/peers should look like this:
9406         # nid refs state last max rtr min tx min queue
9407         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9408         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9409         # numeric (0 or >0 or <0), queue >= 0.
9410         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9411         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9412         create_lnet_proc_files "peers"
9413         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9414         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9415         remove_lnet_proc_files "peers"
9416
9417         # /proc/sys/lnet/buffers  should look like this:
9418         # pages count credits min
9419         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9420         L1="^pages +count +credits +min$"
9421         BR="^ +$N +$N +$I +$I$"
9422         create_lnet_proc_files "buffers"
9423         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9424         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9425         remove_lnet_proc_files "buffers"
9426
9427         # /proc/sys/lnet/nis should look like this:
9428         # nid status alive refs peer rtr max tx min
9429         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9430         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9431         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9432         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9433         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9434         create_lnet_proc_files "nis"
9435         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9436         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9437         remove_lnet_proc_files "nis"
9438
9439         # can we successfully write to /proc/sys/lnet/stats?
9440         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9441         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9442 }
9443 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9444
9445 test_216() { # bug 20317
9446         remote_ost_nodsh && skip "remote OST with nodsh" && return
9447         local node
9448         local p="$TMP/sanityN-$TESTNAME.parameters"
9449         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9450         for node in $(osts_nodes); do
9451                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9452                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9453                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9454         done
9455         clear_osc_stats
9456
9457         # agressive lockless i/o settings
9458         for node in $(osts_nodes); do
9459                 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'
9460         done
9461         lctl set_param -n osc.*.contention_seconds 60
9462
9463         $DIRECTIO write $DIR/$tfile 0 10 4096
9464         $CHECKSTAT -s 40960 $DIR/$tfile
9465
9466         # disable lockless i/o
9467         for node in $(osts_nodes); do
9468                 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'
9469         done
9470         lctl set_param -n osc.*.contention_seconds 0
9471         clear_osc_stats
9472
9473         dd if=/dev/zero of=$DIR/$tfile count=0
9474         $CHECKSTAT -s 0 $DIR/$tfile
9475
9476         restore_lustre_params <$p
9477         rm -f $p
9478         rm $DIR/$tfile
9479 }
9480 run_test 216 "check lockless direct write works and updates file size and kms correctly"
9481
9482 test_217() { # bug 22430
9483         local node
9484         local nid
9485
9486         for node in $(nodes_list); do
9487                 nid=$(host_nids_address $node $NETTYPE)
9488                 if [[ $nid = *-* ]] ; then
9489                         echo "lctl ping $nid@$NETTYPE"
9490                         lctl ping $nid@$NETTYPE
9491                 else
9492                         echo "skipping $node (no hyphen detected)"
9493                 fi
9494         done
9495 }
9496 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
9497
9498 test_218() {
9499        # do directio so as not to populate the page cache
9500        log "creating a 10 Mb file"
9501        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9502        log "starting reads"
9503        dd if=$DIR/$tfile of=/dev/null bs=4096 &
9504        log "truncating the file"
9505        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9506        log "killing dd"
9507        kill %+ || true # reads might have finished
9508        echo "wait until dd is finished"
9509        wait
9510        log "removing the temporary file"
9511        rm -rf $DIR/$tfile || error "tmp file removal failed"
9512 }
9513 run_test 218 "parallel read and truncate should not deadlock ======================="
9514
9515 test_219() {
9516         # write one partial page
9517         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
9518         # set no grant so vvp_io_commit_write will do sync write
9519         $LCTL set_param fail_loc=0x411
9520         # write a full page at the end of file
9521         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
9522
9523         $LCTL set_param fail_loc=0
9524         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
9525         $LCTL set_param fail_loc=0x411
9526         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
9527 }
9528 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
9529
9530 test_220() { #LU-325
9531         remote_ost_nodsh && skip "remote OST with nodsh" && return
9532         local OSTIDX=0
9533
9534         mkdir -p $DIR/$tdir
9535         local OST=$(lfs osts | grep ${OSTIDX}": " | \
9536                 awk '{print $2}' | sed -e 's/_UUID$//')
9537
9538         # on the mdt's osc
9539         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
9540         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
9541                         osc.$mdtosc_proc1.prealloc_last_id)
9542         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
9543                         osc.$mdtosc_proc1.prealloc_next_id)
9544
9545         $LFS df -i
9546
9547         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
9548         #define OBD_FAIL_OST_ENOINO              0x229
9549         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
9550         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
9551         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
9552
9553         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
9554
9555         MDSOBJS=$((last_id - next_id))
9556         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
9557
9558         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
9559         echo "OST still has $count kbytes free"
9560
9561         echo "create $MDSOBJS files @next_id..."
9562         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
9563
9564         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9565                         osc.$mdtosc_proc1.prealloc_last_id)
9566         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9567                         osc.$mdtosc_proc1.prealloc_next_id)
9568
9569         echo "after creation, last_id=$last_id2, next_id=$next_id2"
9570         $LFS df -i
9571
9572         echo "cleanup..."
9573
9574         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
9575         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
9576
9577         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
9578         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
9579         echo "unlink $MDSOBJS files @$next_id..."
9580         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
9581 }
9582 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
9583
9584 test_221() {
9585         dd if=`which date` of=$MOUNT/date oflag=sync
9586         chmod +x $MOUNT/date
9587
9588         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
9589         $LCTL set_param fail_loc=0x80001401
9590
9591         $MOUNT/date > /dev/null
9592         rm -f $MOUNT/date
9593 }
9594 run_test 221 "make sure fault and truncate race to not cause OOM"
9595
9596 test_222a () {
9597        rm -rf $DIR/$tdir
9598        mkdir -p $DIR/$tdir
9599        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9600        createmany -o $DIR/$tdir/$tfile 10
9601        cancel_lru_locks mdc
9602        cancel_lru_locks osc
9603        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9604        $LCTL set_param fail_loc=0x31a
9605        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
9606        $LCTL set_param fail_loc=0
9607        rm -r $DIR/$tdir
9608 }
9609 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
9610
9611 test_222b () {
9612        rm -rf $DIR/$tdir
9613        mkdir -p $DIR/$tdir
9614        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9615        createmany -o $DIR/$tdir/$tfile 10
9616        cancel_lru_locks mdc
9617        cancel_lru_locks osc
9618        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9619        $LCTL set_param fail_loc=0x31a
9620        rm -r $DIR/$tdir || "AGL for rmdir failed"
9621        $LCTL set_param fail_loc=0
9622 }
9623 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
9624
9625 test_223 () {
9626        rm -rf $DIR/$tdir
9627        mkdir -p $DIR/$tdir
9628        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9629        createmany -o $DIR/$tdir/$tfile 10
9630        cancel_lru_locks mdc
9631        cancel_lru_locks osc
9632        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
9633        $LCTL set_param fail_loc=0x31b
9634        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
9635        $LCTL set_param fail_loc=0
9636        rm -r $DIR/$tdir
9637 }
9638 run_test 223 "osc reenqueue if without AGL lock granted ======================="
9639
9640 test_224a() { # LU-1039, MRP-303
9641         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
9642         $LCTL set_param fail_loc=0x508
9643         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
9644         $LCTL set_param fail_loc=0
9645         df $DIR
9646 }
9647 run_test 224a "Don't panic on bulk IO failure"
9648
9649 test_224b() { # LU-1039, MRP-303
9650         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
9651         cancel_lru_locks osc
9652         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
9653         $LCTL set_param fail_loc=0x515
9654         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
9655         $LCTL set_param fail_loc=0
9656         df $DIR
9657 }
9658 run_test 224b "Don't panic on bulk IO failure"
9659
9660 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
9661 test_225a () {
9662        if [ -z ${MDSSURVEY} ]; then
9663               skip_env "mds-survey not found" && return
9664        fi
9665        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9666             { skip "Need MDS version at least 2.2.51"; return; }
9667
9668        local mds=$(facet_host $SINGLEMDS)
9669        local target=$(do_nodes $mds 'lctl dl' | \
9670                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9671
9672        local cmd1="file_count=1000 thrhi=4"
9673        local cmd2="dir_count=2 layer=mdd stripe_count=0"
9674        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9675        local cmd="$cmd1 $cmd2 $cmd3"
9676
9677        rm -f ${TMP}/mds_survey*
9678        echo + $cmd
9679        eval $cmd || error "mds-survey with zero-stripe failed"
9680        cat ${TMP}/mds_survey*
9681        rm -f ${TMP}/mds_survey*
9682 }
9683 run_test 225a "Metadata survey sanity with zero-stripe"
9684
9685 test_225b () {
9686        if [ -z ${MDSSURVEY} ]; then
9687               skip_env "mds-survey not found" && return
9688        fi
9689        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9690             { skip "Need MDS version at least 2.2.51"; return; }
9691
9692        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
9693               skip_env "Need to mount OST to test" && return
9694        fi
9695
9696        local mds=$(facet_host $SINGLEMDS)
9697        local target=$(do_nodes $mds 'lctl dl' | \
9698                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9699
9700        local cmd1="file_count=1000 thrhi=4"
9701        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9702        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9703        local cmd="$cmd1 $cmd2 $cmd3"
9704
9705        rm -f ${TMP}/mds_survey*
9706        echo + $cmd
9707        eval $cmd || error "mds-survey with stripe_count failed"
9708        cat ${TMP}/mds_survey*
9709        rm -f ${TMP}/mds_survey*
9710 }
9711 run_test 225b "Metadata survey sanity with stripe_count = 1"
9712
9713 mcreate_path2fid () {
9714         local mode=$1
9715         local major=$2
9716         local minor=$3
9717         local name=$4
9718         local desc=$5
9719         local path=$DIR/$tdir/$name
9720         local fid
9721         local rc
9722         local fid_path
9723
9724         $MCREATE --mode=$1 --major=$2 --minor=$3 $path || \
9725                 error "error: cannot create $desc"
9726
9727         fid=$($LFS path2fid $path)
9728         rc=$?
9729         [ $rc -ne 0 ] && error "error: cannot get fid of a $desc"
9730
9731         fid_path=$($LFS fid2path $DIR $fid)
9732         rc=$?
9733         [ $rc -ne 0 ] && error "error: cannot get path of a $desc by fid"
9734
9735         [ "$path" == "$fid_path" ] || \
9736                 error "error: fid2path returned \`$fid_path', expected \`$path'"
9737 }
9738
9739 test_226 () {
9740         rm -rf $DIR/$tdir
9741         mkdir -p $DIR/$tdir
9742
9743         mcreate_path2fid 0010666 0 0 fifo "FIFO"
9744         mcreate_path2fid 0020666 1 3 null "character special file (null)"
9745         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
9746         mcreate_path2fid 0040666 0 0 dir "directory"
9747         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
9748         mcreate_path2fid 0100666 0 0 file "regular file"
9749         mcreate_path2fid 0120666 0 0 link "symbolic link"
9750         mcreate_path2fid 0140666 0 0 sock "socket"
9751 }
9752 run_test 226 "call path2fid and fid2path on files of all type"
9753
9754 # LU-1299 Executing or running ldd on a truncated executable does not
9755 # cause an out-of-memory condition.
9756 test_227() {
9757         dd if=`which date` of=$MOUNT/date bs=1k count=1
9758         chmod +x $MOUNT/date
9759
9760         $MOUNT/date > /dev/null
9761         ldd $MOUNT/date > /dev/null
9762         rm -f $MOUNT/date
9763 }
9764 run_test 227 "running truncated executable does not cause OOM"
9765
9766 # LU-1512 try to reuse idle OI blocks
9767 test_228a() {
9768         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9769
9770         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9771         local myDIR=$DIR/$tdir
9772
9773         mkdir -p $myDIR
9774         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9775         $LCTL set_param fail_loc=0x80001002
9776         createmany -o $myDIR/t- 10000
9777         $LCTL set_param fail_loc=0
9778         # The guard is current the largest FID holder
9779         touch $myDIR/guard
9780         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9781                     tr -d '[')
9782         local IDX=$(($SEQ % 64))
9783
9784         do_facet $SINGLEMDS sync
9785         # Make sure journal flushed.
9786         sleep 6
9787         local blk1=$(do_facet $SINGLEMDS \
9788                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9789                      grep Blockcount | awk '{print $4}')
9790
9791         # Remove old files, some OI blocks will become idle.
9792         unlinkmany $myDIR/t- 10000
9793         # Create new files, idle OI blocks should be reused.
9794         createmany -o $myDIR/t- 2000
9795         do_facet $SINGLEMDS sync
9796         # Make sure journal flushed.
9797         sleep 6
9798         local blk2=$(do_facet $SINGLEMDS \
9799                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9800                      grep Blockcount | awk '{print $4}')
9801
9802         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9803 }
9804 run_test 228a "try to reuse idle OI blocks"
9805
9806 test_228b() {
9807         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9808
9809         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9810         local myDIR=$DIR/$tdir
9811
9812         mkdir -p $myDIR
9813         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9814         $LCTL set_param fail_loc=0x80001002
9815         createmany -o $myDIR/t- 10000
9816         $LCTL set_param fail_loc=0
9817         # The guard is current the largest FID holder
9818         touch $myDIR/guard
9819         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9820                     tr -d '[')
9821         local IDX=$(($SEQ % 64))
9822
9823         do_facet $SINGLEMDS sync
9824         # Make sure journal flushed.
9825         sleep 6
9826         local blk1=$(do_facet $SINGLEMDS \
9827                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9828                      grep Blockcount | awk '{print $4}')
9829
9830         # Remove old files, some OI blocks will become idle.
9831         unlinkmany $myDIR/t- 10000
9832
9833         # stop the MDT
9834         stop $SINGLEMDS || error "Fail to stop MDT."
9835         # remount the MDT
9836         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9837
9838         df $MOUNT || error "Fail to df."
9839         # Create new files, idle OI blocks should be reused.
9840         createmany -o $myDIR/t- 2000
9841         do_facet $SINGLEMDS sync
9842         # Make sure journal flushed.
9843         sleep 6
9844         local blk2=$(do_facet $SINGLEMDS \
9845                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9846                      grep Blockcount | awk '{print $4}')
9847
9848         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9849 }
9850 run_test 228b "idle OI blocks can be reused after MDT restart"
9851
9852 #LU-1881
9853 test_228c() {
9854         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9855
9856         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9857         local myDIR=$DIR/$tdir
9858
9859         mkdir -p $myDIR
9860         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9861         $LCTL set_param fail_loc=0x80001002
9862         # 20000 files can guarantee there are index nodes in the OI file
9863         createmany -o $myDIR/t- 20000
9864         $LCTL set_param fail_loc=0
9865         # The guard is current the largest FID holder
9866         touch $myDIR/guard
9867         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9868                     tr -d '[')
9869         local IDX=$(($SEQ % 64))
9870
9871         do_facet $SINGLEMDS sync
9872         # Make sure journal flushed.
9873         sleep 6
9874         local blk1=$(do_facet $SINGLEMDS \
9875                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9876                      grep Blockcount | awk '{print $4}')
9877
9878         # Remove old files, some OI blocks will become idle.
9879         unlinkmany $myDIR/t- 20000
9880         rm -f $myDIR/guard
9881         # The OI file should become empty now
9882
9883         # Create new files, idle OI blocks should be reused.
9884         createmany -o $myDIR/t- 2000
9885         do_facet $SINGLEMDS sync
9886         # Make sure journal flushed.
9887         sleep 6
9888         local blk2=$(do_facet $SINGLEMDS \
9889                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9890                      grep Blockcount | awk '{print $4}')
9891
9892         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9893 }
9894 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
9895
9896 #
9897 # tests that do cleanup/setup should be run at the end
9898 #
9899
9900 test_900() {
9901         local ls
9902         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
9903         $LCTL set_param fail_loc=0x903
9904         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
9905         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
9906                 echo "clear" > $ls
9907         done
9908         FAIL_ON_ERROR=true cleanup
9909         FAIL_ON_ERROR=true setup
9910 }
9911 run_test 900 "umount should not race with any mgc requeue thread"
9912
9913 complete $SECONDS
9914 check_and_cleanup_lustre
9915 if [ "$I_MOUNTED" != "yes" ]; then
9916         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
9917 fi
9918 exit_status