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