Whamcloud - gitweb
LU-2405 tests: account for inter-op testing sanity/17g
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 4; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19
20 SRCDIR=$(cd $(dirname $0); echo $PWD)
21 export PATH=$PATH:/sbin
22
23 TMP=${TMP:-/tmp}
24
25 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
26 CREATETEST=${CREATETEST:-createtest}
27 LFS=${LFS:-lfs}
28 LFIND=${LFIND:-"$LFS find"}
29 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
30 LCTL=${LCTL:-lctl}
31 MCREATE=${MCREATE:-mcreate}
32 OPENFILE=${OPENFILE:-openfile}
33 OPENUNLINK=${OPENUNLINK:-openunlink}
34 export MULTIOP=${MULTIOP:-multiop}
35 READS=${READS:-"reads"}
36 MUNLINK=${MUNLINK:-munlink}
37 SOCKETSERVER=${SOCKETSERVER:-socketserver}
38 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
39 MEMHOG=${MEMHOG:-memhog}
40 DIRECTIO=${DIRECTIO:-directio}
41 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
42 UMOUNT=${UMOUNT:-"umount -d"}
43 STRIPES_PER_OBJ=-1
44 CHECK_GRANT=${CHECK_GRANT:-"yes"}
45 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
46 export PARALLEL=${PARALLEL:-"no"}
47
48 export NAME=${NAME:-local}
49
50 SAVE_PWD=$PWD
51
52 CLEANUP=${CLEANUP:-:}
53 SETUP=${SETUP:-:}
54 TRACE=${TRACE:-""}
55 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
56 . $LUSTRE/tests/test-framework.sh
57 init_test_env $@
58 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
59 init_logging
60
61 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b"
62
63 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
64 # bug number for skipped test:        LU-2834 LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 18      34h     40      48a     180     184c"
66
67 FAIL_ON_ERROR=false
68
69 cleanup() {
70         echo -n "cln.."
71         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
72         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
73 }
74 setup() {
75         echo -n "mnt.."
76         load_modules
77         setupall || exit 10
78         echo "done"
79 }
80
81 check_kernel_version() {
82         WANT_VER=$1
83         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
84         case $GOT_VER in
85         patchless|patchless_client) return 0;;
86         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
87         esac
88         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
89         return 1
90 }
91
92 if [ "$ONLY" == "cleanup" ]; then
93        sh llmountcleanup.sh
94        exit 0
95 fi
96
97 check_and_setup_lustre
98
99 DIR=${DIR:-$MOUNT}
100 assert_DIR
101
102 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
103     awk '{gsub(/_UUID/,""); print $1}' | head -1)
104 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
105 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
106 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
107 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
108 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
109 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
110
111 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
112 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
113 rm -rf $DIR/[Rdfs][0-9]*
114
115 # $RUNAS_ID may get set incorrectly somewhere else
116 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
117
118 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
119
120 build_test_filter
121
122 if [ "${ONLY}" = "MOUNT" ] ; then
123         echo "Lustre is up, please go on"
124         exit
125 fi
126
127 echo "preparing for tests involving mounts"
128 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
129 touch $EXT2_DEV
130 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
131 echo # add a newline after mke2fs.
132
133 umask 077
134
135 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
136 lctl set_param debug=-1 2> /dev/null || true
137 test_0() {
138         touch $DIR/$tfile
139         $CHECKSTAT -t file $DIR/$tfile || error
140         rm $DIR/$tfile
141         $CHECKSTAT -a $DIR/$tfile || error
142 }
143 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
144
145 test_0b() {
146         chmod 0755 $DIR || error
147         $CHECKSTAT -p 0755 $DIR || error
148 }
149 run_test 0b "chmod 0755 $DIR ============================="
150
151 test_0c() {
152     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
153     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
154 }
155 run_test 0c "check import proc ============================="
156
157 test_1a() {
158         test_mkdir -p $DIR/$tdir
159         test_mkdir -p $DIR/$tdir/d2
160         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
161         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
162 }
163 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
164
165 test_1b() {
166         rmdir $DIR/$tdir/d2
167         rmdir $DIR/$tdir
168         $CHECKSTAT -a $DIR/$tdir || error
169 }
170 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
171
172 test_2a() {
173         test_mkdir $DIR/$tdir
174         touch $DIR/$tdir/$tfile
175         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
176 }
177 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
178
179 test_2b() {
180         rm -r $DIR/$tdir
181         $CHECKSTAT -a $DIR/$tdir || error
182 }
183 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
184
185 test_3a() {
186         test_mkdir -p $DIR/$tdir
187         $CHECKSTAT -t dir $DIR/$tdir || error
188 }
189 run_test 3a "mkdir .../d3 ======================================"
190
191 test_3b() {
192         if [ ! -d $DIR/$tdir ]; then
193                 mkdir $DIR/$tdir
194         fi
195         touch $DIR/$tdir/$tfile
196         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
197 }
198 run_test 3b "touch .../d3/f ===================================="
199
200 test_3c() {
201         rm -r $DIR/$tdir
202         $CHECKSTAT -a $DIR/$tdir || error
203 }
204 run_test 3c "rm -r .../d3 ======================================"
205
206 test_4a() {
207         test_mkdir -p $DIR/$tdir
208         $CHECKSTAT -t dir $DIR/$tdir || error
209 }
210 run_test 4a "mkdir .../d4 ======================================"
211
212 test_4b() {
213         if [ ! -d $DIR/$tdir ]; then
214                 test_mkdir $DIR/$tdir
215         fi
216         test_mkdir $DIR/$tdir/d2
217         mkdir $DIR/$tdir/d2
218         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
219 }
220 run_test 4b "mkdir .../d4/d2 ==================================="
221
222 test_5() {
223         test_mkdir $DIR/$tdir
224         test_mkdir $DIR/$tdir/d2
225         chmod 0707 $DIR/$tdir/d2
226         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error
227 }
228 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
229
230 test_6a() {
231         touch $DIR/$tfile
232         chmod 0666 $DIR/$tfile || error
233         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
234 }
235 run_test 6a "touch .../f6a; chmod .../f6a ======================"
236
237 test_6b() {
238         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
239         if [ ! -f $DIR/$tfile ]; then
240                 touch $DIR/$tfile
241                 chmod 0666 $DIR/$tfile
242         fi
243         $RUNAS chmod 0444 $DIR/$tfile && error
244         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
245 }
246 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
247
248 test_6c() {
249         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
250         touch $DIR/$tfile
251         chown $RUNAS_ID $DIR/$tfile || error
252         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
253 }
254 run_test 6c "touch .../f6c; chown .../f6c ======================"
255
256 test_6d() {
257         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
258         if [ ! -f $DIR/$tfile ]; then
259                 touch $DIR/$tfile
260                 chown $RUNAS_ID $DIR/$tfile
261         fi
262         $RUNAS chown $UID $DIR/$tfile && error
263         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
264 }
265 run_test 6d "$RUNAS chown .../f6c (should return error) =="
266
267 test_6e() {
268         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
269         touch $DIR/$tfile
270         chgrp $RUNAS_ID $DIR/$tfile || error
271         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
272 }
273 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
274
275 test_6f() {
276         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
277         if [ ! -f $DIR/$tfile ]; then
278                 touch $DIR/$tfile
279                 chgrp $RUNAS_ID $DIR/$tfile
280         fi
281         $RUNAS chgrp $UID $DIR/$tfile && error
282         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
283 }
284 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
285
286 test_6g() {
287         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
288         test_mkdir $DIR/$tdir || error
289         chmod 777 $DIR/$tdir || error
290         $RUNAS mkdir $DIR/$tdir/d || error
291         chmod g+s $DIR/$tdir/d || error
292         test_mkdir $DIR/$tdir/d/subdir
293         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir || error
294 }
295 run_test 6g "Is new dir in sgid dir inheriting group?"
296
297 test_6h() { # bug 7331
298         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
299         touch $DIR/$tfile || error "touch failed"
300         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
301         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
302                 error "chown worked"
303         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile || error
304 }
305 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
306
307 test_7a() {
308         test_mkdir $DIR/$tdir
309         $MCREATE $DIR/$tdir/$tfile
310         chmod 0666 $DIR/$tdir/$tfile
311         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
312 }
313 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
314
315 test_7b() {
316         if [ ! -d $DIR/$tdir ]; then
317                 mkdir $DIR/$tdir
318         fi
319         $MCREATE $DIR/$tdir/$tfile
320         echo -n foo > $DIR/$tdir/$tfile
321         [ "`cat $DIR/$tdir/$tfile`" = "foo" ] || error
322         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error
323 }
324 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
325
326 test_8() {
327         test_mkdir $DIR/$tdir
328         touch $DIR/$tdir/$tfile
329         chmod 0666 $DIR/$tdir/$tfile
330         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
331 }
332 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
333
334 test_9() {
335         test_mkdir $DIR/$tdir
336         test_mkdir $DIR/$tdir/d2
337         test_mkdir $DIR/$tdir/d2/d3
338         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error
339 }
340 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
341
342 test_10() {
343         test_mkdir $DIR/$tdir
344         test_mkdir $DIR/$tdir/d2
345         touch $DIR/$tdir/d2/$tfile
346         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile || error
347 }
348 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
349
350 test_11() {
351         test_mkdir $DIR/$tdir
352         test_mkdir $DIR/$tdir/d2
353         chmod 0666 $DIR/$tdir/d2
354         chmod 0705 $DIR/$tdir/d2
355         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 || error
356 }
357 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
358
359 test_12() {
360         test_mkdir $DIR/$tdir
361         touch $DIR/$tdir/$tfile
362         chmod 0666 $DIR/$tdir/$tfile
363         chmod 0654 $DIR/$tdir/$tfile
364         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile || error
365 }
366 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
367
368 test_13() {
369         test_mkdir $DIR/$tdir
370         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
371         >  $DIR/$tdir/$tfile
372         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile || error
373 }
374 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
375
376 test_14() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error
381 }
382 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
383
384 test_15() {
385         test_mkdir $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
388         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 || error
389 }
390 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
391
392 test_16() {
393         test_mkdir $DIR/$tdir
394         touch $DIR/$tdir/$tfile
395         rm -rf $DIR/$tdir/$tfile
396         $CHECKSTAT -a $DIR/$tdir/$tfile || error
397 }
398 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
399
400 test_17a() {
401         test_mkdir -p $DIR/$tdir
402         touch $DIR/$tdir/$tfile
403         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
404         ls -l $DIR/$tdir
405         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist || error
406         $CHECKSTAT -f -t f $DIR/$tdir/l-exist || error
407         rm -f $DIR/$tdir/l-exist
408         $CHECKSTAT -a $DIR/$tdir/l-exist || error
409 }
410 run_test 17a "symlinks: create, remove (real) =================="
411
412 test_17b() {
413         test_mkdir -p $DIR/$tdir
414         ln -s no-such-file $DIR/$tdir/l-dangle
415         ls -l $DIR/$tdir
416         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle || error
417         $CHECKSTAT -fa $DIR/$tdir/l-dangle || error
418         rm -f $DIR/$tdir/l-dangle
419         $CHECKSTAT -a $DIR/$tdir/l-dangle || error
420 }
421 run_test 17b "symlinks: create, remove (dangling) =============="
422
423 test_17c() { # bug 3440 - don't save failed open RPC for replay
424         test_mkdir -p $DIR/$tdir
425         ln -s foo $DIR/$tdir/$tfile
426         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
427 }
428 run_test 17c "symlinks: open dangling (should return error) ===="
429
430 test_17d() {
431         test_mkdir -p $DIR/$tdir
432         ln -s foo $DIR/$tdir/$tfile
433         touch $DIR/$tdir/$tfile || error "creating to new symlink"
434 }
435 run_test 17d "symlinks: create dangling ========================"
436
437 test_17e() {
438         test_mkdir -p $DIR/$tdir
439         local foo=$DIR/$tdir/$tfile
440         ln -s $foo $foo || error "create symlink failed"
441         ls -l $foo || error "ls -l failed"
442         ls $foo && error "ls not failed" || true
443 }
444 run_test 17e "symlinks: create recursive symlink (should return error) ===="
445
446 test_17f() {
447         test_mkdir -p $DIR/d17f
448         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
453         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
454         ls -l  $DIR/d17f
455 }
456 run_test 17f "symlinks: long and very long symlink name ========================"
457
458 # str_repeat(S, N) generate a string that is string S repeated N times
459 str_repeat() {
460         local s=$1
461         local n=$2
462         local ret=''
463         while [ $((n -= 1)) -ge 0 ]; do
464                 ret=$ret$s
465         done
466         echo $ret
467 }
468
469 # Long symlinks and LU-2241
470 test_17g() {
471         test_mkdir -p $DIR/$tdir
472         local TESTS="59 60 61 4094 4095"
473
474         # Fix for inode size boundary in 2.1.4
475         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
476                 TESTS="4094 4095"
477
478         # Patch not applied to 2.2 or 2.3 branches
479         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
480         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
481                 TESTS="4094 4095"
482
483         for i in $TESTS; do
484                 local SYMNAME=$(str_repeat 'x' $i)
485                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
486                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
487         done
488 }
489 run_test 17g "symlinks: really long symlink name and inode boundaries"
490
491 test_17h() { #bug 17378
492         remote_mds_nodsh && skip "remote MDS with nodsh" && return
493         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
494         local mdt_idx
495         test_mkdir -p $DIR/$tdir
496         if [ $MDSCOUNT -gt 1 ]; then
497                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
498         else
499                 mdt_idx=0
500         fi
501         $SETSTRIPE -c -1 $DIR/$tdir
502 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
503         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
504         touch $DIR/$tdir/$tfile || true
505 }
506 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
507
508 test_17i() { #bug 20018
509         remote_mds_nodsh && skip "remote MDS with nodsh" && return
510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
511         test_mkdir -p $DIR/$tdir
512         local foo=$DIR/$tdir/$tfile
513         local mdt_idx
514         if [ $MDSCOUNT -gt 1 ]; then
515                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
516         else
517                 mdt_idx=0
518         fi
519         ln -s $foo $foo || error "create symlink failed"
520 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
521         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
522         ls -l $foo && error "error not detected"
523         return 0
524 }
525 run_test 17i "don't panic on short symlink"
526
527 test_17k() { #bug 22301
528         rsync --help | grep -q xattr ||
529                 skip_env "$(rsync --version| head -1) does not support xattrs"
530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
531         test_mkdir -p $DIR/$tdir
532         test_mkdir -p $DIR/$tdir.new
533         touch $DIR/$tdir/$tfile
534         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
535         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
536                 error "rsync failed with xattrs enabled"
537 }
538 run_test 17k "symlinks: rsync with xattrs enabled ========================="
539
540 test_17l() { # LU-279
541         mkdir -p $DIR/$tdir
542         touch $DIR/$tdir/$tfile
543         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
544         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
545                 # -h to not follow symlinks. -m '' to list all the xattrs.
546                 # grep to remove first line: '# file: $path'.
547                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
548                 do
549                         lgetxattr_size_check $path $xattr ||
550                                 error "lgetxattr_size_check $path $xattr failed"
551                 done
552         done
553 }
554 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
555
556 # LU-1540
557 test_17m() {
558         local short_sym="0123456789"
559         local WDIR=$DIR/${tdir}m
560         local mds_index
561         local devname
562         local cmd
563         local i
564         local rc=0
565
566         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
567         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
568                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
569
570         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
571                 skip "only for ldiskfs MDT" && return 0
572
573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
574
575         mkdir -p $WDIR
576         long_sym=$short_sym
577         # create a long symlink file
578         for ((i = 0; i < 4; ++i)); do
579                 long_sym=${long_sym}${long_sym}
580         done
581
582         echo "create 512 short and long symlink files under $WDIR"
583         for ((i = 0; i < 256; ++i)); do
584                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
585                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
586         done
587
588         echo "erase them"
589         rm -f $WDIR/*
590         sync
591         wait_delete_completed
592
593         echo "recreate the 512 symlink files with a shorter string"
594         for ((i = 0; i < 512; ++i)); do
595                 # rewrite the symlink file with a shorter string
596                 ln -sf ${long_sym} $WDIR/long-$i
597                 ln -sf ${short_sym} $WDIR/short-$i
598         done
599
600         mds_index=$($LFS getstripe -M $WDIR)
601         mds_index=$((mds_index+1))
602         devname=$(mdsdevname $mds_index)
603         cmd="$E2FSCK -fnvd $devname"
604
605         echo "stop and checking mds${mds_index}: $cmd"
606         # e2fsck should not return error
607         stop mds${mds_index} -f
608         do_facet mds${mds_index} $cmd || rc=$?
609
610         start mds${mds_index} $devname $MDS_MOUNT_OPTS
611         df $MOUNT > /dev/null 2>&1
612         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
613                 "short/long symlink MDT: rc=$rc"
614         return $rc
615 }
616 run_test 17m "run e2fsck against MDT which contains short/long symlink"
617
618 check_fs_consistency_17n() {
619         local mdt_index
620         local devname
621         local cmd
622         local rc=0
623
624         for mdt_index in $(seq 1 $MDSCOUNT); do
625                 devname=$(mdsdevname $mdt_index)
626                 cmd="$E2FSCK -fnvd $devname"
627
628                 echo "stop and checking mds${mdt_index}: $cmd"
629                 # e2fsck should not return error
630                 stop mds${mdt_index}
631                 do_facet mds${mdt_index} $cmd || rc=$?
632
633                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
634                 df $MOUNT > /dev/null 2>&1
635                 [ $rc -ne 0 ] && break
636         done
637         return $rc
638 }
639
640 test_17n() {
641         local i
642
643         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
644         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
645                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
646
647         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
648                 skip "only for ldiskfs MDT" && return 0
649
650         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
651
652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
653
654         mkdir -p $DIR/$tdir
655         for ((i=0; i<10; i++)); do
656                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
657                         error "create remote dir error $i"
658                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
659                         error "create files under remote dir failed $i"
660         done
661
662         check_fs_consistency_17n || error "e2fsck report error"
663
664         for ((i=0;i<10;i++)); do
665                 rm -rf $DIR/$tdir/remote_dir_${i} ||
666                         error "destroy remote dir error $i"
667         done
668
669         check_fs_consistency_17n || error "e2fsck report error"
670 }
671 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
672
673 test_18() {
674         touch $DIR/f
675         ls $DIR || error
676 }
677 run_test 18 "touch .../f ; ls ... =============================="
678
679 test_19a() {
680         touch $DIR/f19
681         ls -l $DIR
682         rm $DIR/f19
683         $CHECKSTAT -a $DIR/f19 || error
684 }
685 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
686
687 test_19b() {
688         ls -l $DIR/f19 && error || true
689 }
690 run_test 19b "ls -l .../f19 (should return error) =============="
691
692 test_19c() {
693         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
694         $RUNAS touch $DIR/f19 && error || true
695 }
696 run_test 19c "$RUNAS touch .../f19 (should return error) =="
697
698 test_19d() {
699         cat $DIR/f19 && error || true
700 }
701 run_test 19d "cat .../f19 (should return error) =============="
702
703 test_20() {
704         touch $DIR/f
705         rm $DIR/f
706         log "1 done"
707         touch $DIR/f
708         rm $DIR/f
709         log "2 done"
710         touch $DIR/f
711         rm $DIR/f
712         log "3 done"
713         $CHECKSTAT -a $DIR/f || error
714 }
715 run_test 20 "touch .../f ; ls -l ... ==========================="
716
717 test_21() {
718         test_mkdir $DIR/d21
719         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
720         ln -s dangle $DIR/d21/link
721         echo foo >> $DIR/d21/link
722         cat $DIR/d21/dangle
723         $CHECKSTAT -t link $DIR/d21/link || error
724         $CHECKSTAT -f -t file $DIR/d21/link || error
725 }
726 run_test 21 "write to dangling link ============================"
727
728 test_22() {
729         WDIR=$DIR/$tdir
730         test_mkdir -p $DIR/$tdir
731         chown $RUNAS_ID:$RUNAS_GID $WDIR
732         (cd $WDIR || error "cd $WDIR failed";
733         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
734         $RUNAS tar xf -)
735         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
736         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
737         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
738 }
739 run_test 22 "unpack tar archive as non-root user ==============="
740
741 # was test_23
742 test_23a() {
743         test_mkdir -p $DIR/$tdir
744         local file=$DIR/$tdir/$tfile
745
746         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
747         openfile -f O_CREAT:O_EXCL $file &&
748                 error "$file recreate succeeded" || true
749 }
750 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
751
752 test_23b() { # bug 18988
753         test_mkdir -p $DIR/$tdir
754         local file=$DIR/$tdir/$tfile
755
756         rm -f $file
757         echo foo > $file || error "write filed"
758         echo bar >> $file || error "append filed"
759         $CHECKSTAT -s 8 $file || error "wrong size"
760         rm $file
761 }
762 run_test 23b "O_APPEND check =========================="
763
764 test_24a() {
765         echo '== rename sanity =============================================='
766         echo '-- same directory rename'
767         test_mkdir $DIR/R1
768         touch $DIR/R1/f
769         mv $DIR/R1/f $DIR/R1/g
770         $CHECKSTAT -t file $DIR/R1/g || error
771 }
772 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
773
774 test_24b() {
775         test_mkdir $DIR/R2
776         touch $DIR/R2/{f,g}
777         mv $DIR/R2/f $DIR/R2/g
778         $CHECKSTAT -a $DIR/R2/f || error
779         $CHECKSTAT -t file $DIR/R2/g || error
780 }
781 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
782
783 test_24c() {
784         test_mkdir $DIR/R3
785         test_mkdir $DIR/R3/f
786         mv $DIR/R3/f $DIR/R3/g
787         $CHECKSTAT -a $DIR/R3/f || error
788         $CHECKSTAT -t dir $DIR/R3/g || error
789 }
790 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
791
792 test_24d() {
793         test_mkdir $DIR/R4
794         test_mkdir $DIR/R4/f
795         test_mkdir $DIR/R4/g
796         mrename $DIR/R4/f $DIR/R4/g
797         $CHECKSTAT -a $DIR/R4/f || error
798         $CHECKSTAT -t dir $DIR/R4/g || error
799 }
800 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
801
802 test_24e() {
803         echo '-- cross directory renames --'
804         test_mkdir $DIR/R5a
805         test_mkdir $DIR/R5b
806         touch $DIR/R5a/f
807         mv $DIR/R5a/f $DIR/R5b/g
808         $CHECKSTAT -a $DIR/R5a/f || error
809         $CHECKSTAT -t file $DIR/R5b/g || error
810 }
811 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
812
813 test_24f() {
814         test_mkdir $DIR/R6a
815         test_mkdir $DIR/R6b
816         touch $DIR/R6a/f $DIR/R6b/g
817         mv $DIR/R6a/f $DIR/R6b/g
818         $CHECKSTAT -a $DIR/R6a/f || error
819         $CHECKSTAT -t file $DIR/R6b/g || error
820 }
821 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
822
823 test_24g() {
824         test_mkdir $DIR/R7a
825         test_mkdir $DIR/R7b
826         test_mkdir $DIR/R7a/d
827         mv $DIR/R7a/d $DIR/R7b/e
828         $CHECKSTAT -a $DIR/R7a/d || error
829         $CHECKSTAT -t dir $DIR/R7b/e || error
830 }
831 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
832
833 test_24h() {
834         test_mkdir $DIR/R8a
835         test_mkdir $DIR/R8b
836         test_mkdir $DIR/R8a/d
837         test_mkdir $DIR/R8b/e
838         mrename $DIR/R8a/d $DIR/R8b/e
839         $CHECKSTAT -a $DIR/R8a/d || error
840         $CHECKSTAT -t dir $DIR/R8b/e || error
841 }
842 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
843
844 test_24i() {
845         echo "-- rename error cases"
846         test_mkdir $DIR/R9
847         test_mkdir $DIR/R9/a
848         touch $DIR/R9/f
849         mrename $DIR/R9/f $DIR/R9/a
850         $CHECKSTAT -t file $DIR/R9/f || error
851         $CHECKSTAT -t dir  $DIR/R9/a || error
852         $CHECKSTAT -a $DIR/R9/a/f || error
853 }
854 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
855
856 test_24j() {
857         test_mkdir $DIR/R10
858         mrename $DIR/R10/f $DIR/R10/g
859         $CHECKSTAT -t dir $DIR/R10 || error
860         $CHECKSTAT -a $DIR/R10/f || error
861         $CHECKSTAT -a $DIR/R10/g || error
862 }
863 run_test 24j "source does not exist ============================"
864
865 test_24k() {
866         test_mkdir $DIR/R11a
867         test_mkdir $DIR/R11a/d
868         touch $DIR/R11a/f
869         mv $DIR/R11a/f $DIR/R11a/d
870         $CHECKSTAT -a $DIR/R11a/f || error
871         $CHECKSTAT -t file $DIR/R11a/d/f || error
872 }
873 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
874
875 # bug 2429 - rename foo foo foo creates invalid file
876 test_24l() {
877         f="$DIR/f24l"
878         $MULTIOP $f OcNs || error
879 }
880 run_test 24l "Renaming a file to itself ========================"
881
882 test_24m() {
883         f="$DIR/f24m"
884         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
885         # on ext3 this does not remove either the source or target files
886         # though the "expected" operation would be to remove the source
887         $CHECKSTAT -t file ${f} || error "${f} missing"
888         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
889 }
890 run_test 24m "Renaming a file to a hard link to itself ========="
891
892 test_24n() {
893     f="$DIR/f24n"
894     # this stats the old file after it was renamed, so it should fail
895     touch ${f}
896     $CHECKSTAT ${f}
897     mv ${f} ${f}.rename
898     $CHECKSTAT ${f}.rename
899     $CHECKSTAT -a ${f}
900 }
901 run_test 24n "Statting the old file after renaming (Posix rename 2)"
902
903 test_24o() {
904         check_kernel_version 37 || return 0
905         test_mkdir -p $DIR/d24o
906         rename_many -s random -v -n 10 $DIR/d24o
907 }
908 run_test 24o "rename of files during htree split ==============="
909
910 test_24p() {
911         test_mkdir $DIR/R12a
912         test_mkdir $DIR/R12b
913         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
914         mrename $DIR/R12a $DIR/R12b
915         $CHECKSTAT -a $DIR/R12a || error
916         $CHECKSTAT -t dir $DIR/R12b || error
917         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
918         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
919 }
920 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
921
922 cleanup_multiop_pause() {
923         trap 0
924         kill -USR1 $MULTIPID
925 }
926
927 test_24q() {
928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
929         test_mkdir $DIR/R13a
930         test_mkdir $DIR/R13b
931         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
932         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
933         MULTIPID=$!
934
935         trap cleanup_multiop_pause EXIT
936         mrename $DIR/R13a $DIR/R13b
937         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
938         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
939         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
940         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
941         cleanup_multiop_pause
942         wait $MULTIPID || error "multiop close failed"
943 }
944 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
945
946 test_24r() { #bug 3789
947         test_mkdir $DIR/R14a
948         test_mkdir $DIR/R14a/b
949         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
950         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
951         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
952 }
953 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
954
955 test_24s() {
956         test_mkdir $DIR/R15a
957         test_mkdir $DIR/R15a/b
958         test_mkdir $DIR/R15a/b/c
959         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
960         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
961         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
962 }
963 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
964 test_24t() {
965         test_mkdir $DIR/R16a
966         test_mkdir $DIR/R16a/b
967         test_mkdir $DIR/R16a/b/c
968         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
969         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
970         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
971 }
972 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
973
974 test_24u() { # bug12192
975         rm -rf $DIR/$tfile
976         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
977         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
978 }
979 run_test 24u "create stripe file"
980
981 page_size() {
982         getconf PAGE_SIZE
983 }
984
985 simple_cleanup_common() {
986         trap 0
987         rm -rf $DIR/$tdir
988         wait_delete_completed
989 }
990
991 max_pages_per_rpc() {
992         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
993 }
994
995 test_24v() {
996         local NRFILES=100000
997         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
998         [ $FREE_INODES -lt $NRFILES ] && \
999                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1000                 return
1001
1002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1003         trap simple_cleanup_common EXIT
1004
1005         mkdir -p $DIR/$tdir
1006         createmany -m $DIR/$tdir/$tfile $NRFILES
1007
1008         cancel_lru_locks mdc
1009         lctl set_param mdc.*.stats clear
1010
1011         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1012
1013         # LU-5 large readdir
1014         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1015         #               8 bytes for name(filename is mostly 5 in this test) +
1016         #               8 bytes for luda_type
1017         # take into account of overhead in lu_dirpage header and end mark in
1018         # each page, plus one in RPC_NUM calculation.
1019         DIRENT_SIZE=48
1020         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1021         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1022         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1023                                 awk '/^mds_readpage/ {print $2}')
1024         [ $mds_readpage -gt $RPC_NUM ] && \
1025                 error "large readdir doesn't take effect"
1026
1027         simple_cleanup_common
1028 }
1029 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1030
1031 test_24w() { # bug21506
1032         SZ1=234852
1033         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1034         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1035         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1036         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1037         [ "$SZ1" = "$SZ2" ] || \
1038                 error "Error reading at the end of the file $tfile"
1039 }
1040 run_test 24w "Reading a file larger than 4Gb"
1041
1042 test_24x() {
1043         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1045         local MDTIDX=1
1046         local remote_dir=$DIR/$tdir/remote_dir
1047
1048         mkdir -p $DIR/$tdir
1049         $LFS mkdir -i $MDTIDX $remote_dir ||
1050                 error "create remote directory failed"
1051
1052         mkdir -p $DIR/$tdir/src_dir
1053         touch $DIR/$tdir/src_file
1054         mkdir -p $remote_dir/tgt_dir
1055         touch $remote_dir/tgt_file
1056
1057         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1058                 error "rename dir cross MDT works!"
1059
1060         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1061                 error "rename file cross MDT works!"
1062
1063         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1064                 error "ln file cross MDT should not work!"
1065
1066         rm -rf $DIR/$tdir || error "Can not delete directories"
1067 }
1068 run_test 24x "cross rename/link should be failed"
1069
1070 test_24y() {
1071         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1072         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1073         local MDTIDX=1
1074         local remote_dir=$DIR/$tdir/remote_dir
1075
1076         mkdir -p $DIR/$tdir
1077         $LFS mkdir -i $MDTIDX $remote_dir ||
1078                    error "create remote directory failed"
1079
1080         mkdir -p $remote_dir/src_dir
1081         touch $remote_dir/src_file
1082         mkdir -p $remote_dir/tgt_dir
1083         touch $remote_dir/tgt_file
1084
1085         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1086                 error "rename subdir in the same remote dir failed!"
1087
1088         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1089                 error "rename files in the same remote dir failed!"
1090
1091         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1092                 error "link files in the same remote dir failed!"
1093
1094         rm -rf $DIR/$tdir || error "Can not delete directories"
1095 }
1096 run_test 24y "rename/link on the same dir should succeed"
1097
1098 test_24z() {
1099         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1101         local MDTIDX=1
1102         local remote_src=$DIR/$tdir/remote_dir
1103         local remote_tgt=$DIR/$tdir/remote_tgt
1104
1105         mkdir -p $DIR/$tdir
1106         $LFS mkdir -i $MDTIDX $remote_src ||
1107                    error "create remote directory failed"
1108
1109         $LFS mkdir -i $MDTIDX $remote_tgt ||
1110                    error "create remote directory failed"
1111
1112         mrename $remote_src $remote_tgt &&
1113                 error "rename remote dirs should not work!"
1114
1115         rm -rf $DIR/$tdir || error "Can not delete directories"
1116 }
1117 run_test 24z "rename one remote dir to another remote dir should fail"
1118
1119 test_25a() {
1120         echo '== symlink sanity ============================================='
1121
1122         test_mkdir $DIR/d25
1123         ln -s d25 $DIR/s25
1124         touch $DIR/s25/foo || error
1125 }
1126 run_test 25a "create file in symlinked directory ==============="
1127
1128 test_25b() {
1129         [ ! -d $DIR/d25 ] && test_25a
1130         $CHECKSTAT -t file $DIR/s25/foo || error
1131 }
1132 run_test 25b "lookup file in symlinked directory ==============="
1133
1134 test_26a() {
1135         test_mkdir $DIR/d26
1136         test_mkdir $DIR/d26/d26-2
1137         ln -s d26/d26-2 $DIR/s26
1138         touch $DIR/s26/foo || error
1139 }
1140 run_test 26a "multiple component symlink ======================="
1141
1142 test_26b() {
1143         test_mkdir -p $DIR/d26b/d26-2
1144         ln -s d26b/d26-2/foo $DIR/s26-2
1145         touch $DIR/s26-2 || error
1146 }
1147 run_test 26b "multiple component symlink at end of lookup ======"
1148
1149 test_26c() {
1150         test_mkdir $DIR/d26.2
1151         touch $DIR/d26.2/foo
1152         ln -s d26.2 $DIR/s26.2-1
1153         ln -s s26.2-1 $DIR/s26.2-2
1154         ln -s s26.2-2 $DIR/s26.2-3
1155         chmod 0666 $DIR/s26.2-3/foo
1156 }
1157 run_test 26c "chain of symlinks ================================"
1158
1159 # recursive symlinks (bug 439)
1160 test_26d() {
1161         ln -s d26-3/foo $DIR/d26-3
1162 }
1163 run_test 26d "create multiple component recursive symlink ======"
1164
1165 test_26e() {
1166         [ ! -h $DIR/d26-3 ] && test_26d
1167         rm $DIR/d26-3
1168 }
1169 run_test 26e "unlink multiple component recursive symlink ======"
1170
1171 # recursive symlinks (bug 7022)
1172 test_26f() {
1173         test_mkdir -p $DIR/$tdir
1174         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1175         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1176         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1177         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1178         cd $tfile                || error "cd $tfile failed"
1179         ln -s .. dotdot          || error "ln dotdot failed"
1180         ln -s dotdot/lndir lndir || error "ln lndir failed"
1181         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1182         output=`ls $tfile/$tfile/lndir/bar1`
1183         [ "$output" = bar1 ] && error "unexpected output"
1184         rm -r $tfile             || error "rm $tfile failed"
1185         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1186 }
1187 run_test 26f "rm -r of a directory which has recursive symlink ="
1188
1189 test_27a() {
1190         echo '== stripe sanity =============================================='
1191         test_mkdir -p $DIR/d27 || error "mkdir failed"
1192         $GETSTRIPE $DIR/d27
1193         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1194         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1195         pass
1196         log "== test_27a: write to one stripe file ========================="
1197         cp /etc/hosts $DIR/d27/f0 || error
1198 }
1199 run_test 27a "one stripe file =================================="
1200
1201 test_27b() {
1202         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1203         test_mkdir -p $DIR/d27
1204         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1205         $GETSTRIPE -c $DIR/d27/f01
1206         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1207                 error "two-stripe file doesn't have two stripes"
1208 }
1209 run_test 27b "create two stripe file"
1210
1211 test_27c() {
1212         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1213
1214         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1215 }
1216 run_test 27c "write to two stripe file"
1217
1218 test_27d() {
1219         test_mkdir -p $DIR/d27
1220         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1221         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1222         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1223 }
1224 run_test 27d "create file with default settings ================"
1225
1226 test_27e() {
1227         test_mkdir -p $DIR/d27
1228         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1229         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1230         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1231 }
1232 run_test 27e "setstripe existing file (should return error) ======"
1233
1234 test_27f() {
1235         test_mkdir -p $DIR/d27
1236         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1237         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1238         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1239 }
1240 run_test 27f "setstripe with bad stripe size (should return error)"
1241
1242 test_27g() {
1243         test_mkdir -p $DIR/d27
1244         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1245         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1246                 error "$DIR/d27/fnone has object"
1247 }
1248 run_test 27g "$GETSTRIPE with no objects"
1249
1250 test_27i() {
1251         touch $DIR/d27/fsome || error "touch failed"
1252         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1253 }
1254 run_test 27i "$GETSTRIPE with some objects"
1255
1256 test_27j() {
1257         test_mkdir -p $DIR/d27
1258         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1259 }
1260 run_test 27j "setstripe with bad stripe offset (should return error)"
1261
1262 test_27k() { # bug 2844
1263         test_mkdir -p $DIR/d27
1264         FILE=$DIR/d27/f27k
1265         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1266         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1267         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1268         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1269         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1270         dd if=/dev/zero of=$FILE bs=4k count=1
1271         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1272         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1273 }
1274 run_test 27k "limit i_blksize for broken user apps ============="
1275
1276 test_27l() {
1277         test_mkdir -p $DIR/d27
1278         mcreate $DIR/f27l || error "creating file"
1279         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1280                 error "setstripe should have failed" || true
1281 }
1282 run_test 27l "check setstripe permissions (should return error)"
1283
1284 test_27m() {
1285         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1286                 return
1287         if [ $ORIGFREE -gt $MAXFREE ]; then
1288                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1289                 return
1290         fi
1291         trap simple_cleanup_common EXIT
1292         test_mkdir -p $DIR/$tdir
1293         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1294         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1295                 error "dd should fill OST0"
1296         i=2
1297         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1298                 i=`expr $i + 1`
1299                 [ $i -gt 256 ] && break
1300         done
1301         i=`expr $i + 1`
1302         touch $DIR/$tdir/f27m_$i
1303         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1304                 error "OST0 was full but new created file still use it"
1305         i=`expr $i + 1`
1306         touch $DIR/$tdir/f27m_$i
1307         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1308                 error "OST0 was full but new created file still use it"
1309         simple_cleanup_common
1310 }
1311 run_test 27m "create file while OST0 was full =================="
1312
1313 sleep_maxage() {
1314         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1315         sleep $DELAY
1316 }
1317
1318 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1319 # if the OST isn't full anymore.
1320 reset_enospc() {
1321         local OSTIDX=${1:-""}
1322
1323         local list=$(comma_list $(osts_nodes))
1324         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1325
1326         do_nodes $list lctl set_param fail_loc=0
1327         sync    # initiate all OST_DESTROYs from MDS to OST
1328         sleep_maxage
1329 }
1330
1331 exhaust_precreations() {
1332         local OSTIDX=$1
1333         local FAILLOC=$2
1334         local FAILIDX=${3:-$OSTIDX}
1335
1336         test_mkdir -p $DIR/$tdir
1337         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1338         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1339
1340         local OST=$(ostname_from_index $OSTIDX)
1341         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1342                           sed -e 's/_UUID$//;s/^.*-//')
1343
1344         # on the mdt's osc
1345         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1346         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1347         osc.$mdtosc_proc1.prealloc_last_id)
1348         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1349         osc.$mdtosc_proc1.prealloc_next_id)
1350
1351         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1352         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1353
1354         test_mkdir -p $DIR/$tdir/${OST}
1355         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1356 #define OBD_FAIL_OST_ENOSPC              0x215
1357         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1358         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1359         echo "Creating to objid $last_id on ost $OST..."
1360         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1361         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1362         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1363         sleep_maxage
1364 }
1365
1366 exhaust_all_precreations() {
1367         local i
1368         for (( i=0; i < OSTCOUNT; i++ )) ; do
1369                 exhaust_precreations $i $1 -1
1370         done
1371 }
1372
1373 test_27n() {
1374         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1375         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1376         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1377         remote_ost_nodsh && skip "remote OST with nodsh" && return
1378
1379         reset_enospc
1380         rm -f $DIR/$tdir/$tfile
1381         exhaust_precreations 0 0x80000215
1382         $SETSTRIPE -c -1 $DIR/$tdir
1383         touch $DIR/$tdir/$tfile || error
1384         $GETSTRIPE $DIR/$tdir/$tfile
1385         reset_enospc
1386 }
1387 run_test 27n "create file with some full OSTs =================="
1388
1389 test_27o() {
1390         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1392         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1393         remote_ost_nodsh && skip "remote OST with nodsh" && return
1394
1395         reset_enospc
1396         rm -f $DIR/$tdir/$tfile
1397         exhaust_all_precreations 0x215
1398
1399         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1400
1401         reset_enospc
1402         rm -rf $DIR/$tdir/*
1403 }
1404 run_test 27o "create file with all full OSTs (should error) ===="
1405
1406 test_27p() {
1407         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1409         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1410         remote_ost_nodsh && skip "remote OST with nodsh" && return
1411
1412         reset_enospc
1413         rm -f $DIR/$tdir/$tfile
1414         test_mkdir -p $DIR/$tdir
1415
1416         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1417         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1418         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1419
1420         exhaust_precreations 0 0x80000215
1421         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1422         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1423         $GETSTRIPE $DIR/$tdir/$tfile
1424
1425         reset_enospc
1426 }
1427 run_test 27p "append to a truncated file with some full OSTs ==="
1428
1429 test_27q() {
1430         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1431         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1432         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1433         remote_ost_nodsh && skip "remote OST with nodsh" && return
1434
1435         reset_enospc
1436         rm -f $DIR/$tdir/$tfile
1437
1438         test_mkdir -p $DIR/$tdir
1439         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1440         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1441         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1442
1443         exhaust_all_precreations 0x215
1444
1445         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1446         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1447
1448         reset_enospc
1449 }
1450 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1451
1452 test_27r() {
1453         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1455         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1456         remote_ost_nodsh && skip "remote OST with nodsh" && return
1457
1458         reset_enospc
1459         rm -f $DIR/$tdir/$tfile
1460         exhaust_precreations 0 0x80000215
1461
1462         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1463
1464         reset_enospc
1465 }
1466 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1467
1468 test_27s() { # bug 10725
1469         test_mkdir -p $DIR/$tdir
1470         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1471         local stripe_count=0
1472         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1473         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1474                 error "stripe width >= 2^32 succeeded" || true
1475
1476 }
1477 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1478
1479 test_27t() { # bug 10864
1480         WDIR=`pwd`
1481         WLFS=`which lfs`
1482         cd $DIR
1483         touch $tfile
1484         $WLFS getstripe $tfile
1485         cd $WDIR
1486 }
1487 run_test 27t "check that utils parse path correctly"
1488
1489 test_27u() { # bug 4900
1490         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1491         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1492
1493 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1494         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1495         test_mkdir -p $DIR/$tdir
1496         rm -rf $DIR/$tdir/*
1497         createmany -o $DIR/$tdir/t- 1000
1498         do_facet $SINGLEMDS lctl set_param fail_loc=0
1499
1500         TLOG=$DIR/$tfile.getstripe
1501         $GETSTRIPE $DIR/$tdir > $TLOG
1502         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1503         unlinkmany $DIR/$tdir/t- 1000
1504         [ $OBJS -gt 0 ] && \
1505                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1506 }
1507 run_test 27u "skip object creation on OSC w/o objects =========="
1508
1509 test_27v() { # bug 4900
1510         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1511         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1512         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1513         remote_ost_nodsh && skip "remote OST with nodsh" && return
1514
1515         exhaust_all_precreations 0x215
1516         reset_enospc
1517
1518         test_mkdir -p $DIR/$tdir
1519         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1520
1521         touch $DIR/$tdir/$tfile
1522         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1523         # all except ost1
1524         for (( i=1; i < OSTCOUNT; i++ )); do
1525                 do_facet ost$i lctl set_param fail_loc=0x705
1526         done
1527         local START=`date +%s`
1528         createmany -o $DIR/$tdir/$tfile 32
1529
1530         local FINISH=`date +%s`
1531         local TIMEOUT=`lctl get_param -n timeout`
1532         local PROCESS=$((FINISH - START))
1533         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1534                error "$FINISH - $START >= $TIMEOUT / 2"
1535         sleep $((TIMEOUT / 2 - PROCESS))
1536         reset_enospc
1537 }
1538 run_test 27v "skip object creation on slow OST ================="
1539
1540 test_27w() { # bug 10997
1541         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1542         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1543         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1544                 error "stripe size $size != 65536" || true
1545         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1546                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1547 }
1548 run_test 27w "check $SETSTRIPE -S option"
1549
1550 test_27wa() {
1551         [ "$OSTCOUNT" -lt "2" ] &&
1552                 skip_env "skipping multiple stripe count/offset test" && return
1553
1554         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1555         for i in $(seq 1 $OSTCOUNT); do
1556                 offset=$((i - 1))
1557                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1558                         error "setstripe -c $i -i $offset failed"
1559                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1560                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1561                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1562                 [ $index -ne $offset ] &&
1563                         error "stripe offset $index != $offset" || true
1564         done
1565 }
1566 run_test 27wa "check $SETSTRIPE -c -i options"
1567
1568 test_27x() {
1569         remote_ost_nodsh && skip "remote OST with nodsh" && return
1570         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1572         OFFSET=$(($OSTCOUNT - 1))
1573         OSTIDX=0
1574         local OST=$(ostname_from_index $OSTIDX)
1575
1576         test_mkdir -p $DIR/$tdir
1577         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1578         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1579         sleep_maxage
1580         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1581         for i in `seq 0 $OFFSET`; do
1582                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1583                 error "OST0 was degraded but new created file still use it"
1584         done
1585         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1586 }
1587 run_test 27x "create files while OST0 is degraded"
1588
1589 test_27y() {
1590         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1591         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1592         remote_ost_nodsh && skip "remote OST with nodsh" && return
1593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1594
1595         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1596         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1597             osc.$mdtosc.prealloc_last_id)
1598         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1599             osc.$mdtosc.prealloc_next_id)
1600         local fcount=$((last_id - next_id))
1601         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1602         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1603
1604         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1605                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1606         local OST_DEACTIVE_IDX=-1
1607         local OSC
1608         local OSTIDX
1609         local OST
1610
1611         for OSC in $MDS_OSCS; do
1612                 OST=$(osc_to_ost $OSC)
1613                 OSTIDX=$(index_from_ostuuid $OST)
1614                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1615                         OST_DEACTIVE_IDX=$OSTIDX
1616                 fi
1617                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1618                         echo $OSC "is Deactivated:"
1619                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1620                 fi
1621         done
1622
1623         OSTIDX=$(index_from_ostuuid $OST)
1624         mkdir -p $DIR/$tdir
1625         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1626
1627         for OSC in $MDS_OSCS; do
1628                 OST=$(osc_to_ost $OSC)
1629                 OSTIDX=$(index_from_ostuuid $OST)
1630                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1631                         echo $OST "is degraded:"
1632                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1633                                                 obdfilter.$OST.degraded=1
1634                 fi
1635         done
1636
1637         sleep_maxage
1638         createmany -o $DIR/$tdir/$tfile $fcount
1639
1640         for OSC in $MDS_OSCS; do
1641                 OST=$(osc_to_ost $OSC)
1642                 OSTIDX=$(index_from_ostuuid $OST)
1643                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1644                         echo $OST "is recovered from degraded:"
1645                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1646                                                 obdfilter.$OST.degraded=0
1647                 else
1648                         do_facet $SINGLEMDS lctl --device %$OSC activate
1649                 fi
1650         done
1651
1652         # all osp devices get activated, hence -1 stripe count restored
1653         local stripecnt=0
1654
1655         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1656         # devices get activated.
1657         sleep_maxage
1658         $SETSTRIPE -c -1 $DIR/$tfile
1659         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1660         rm -f $DIR/$tfile
1661         [ $stripecnt -ne $OSTCOUNT ] &&
1662                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1663         return 0
1664 }
1665 run_test 27y "create files while OST0 is degraded and the rest inactive"
1666
1667 check_seq_oid()
1668 {
1669         log "check file $1"
1670
1671         lmm_count=$($GETSTRIPE -c $1)
1672         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1673         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1674
1675         local old_ifs="$IFS"
1676         IFS=$'[:]'
1677         fid=($($LFS path2fid $1))
1678         IFS="$old_ifs"
1679
1680         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1681         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1682
1683         # compare lmm_seq and lu_fid->f_seq
1684         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1685         # compare lmm_object_id and lu_fid->oid
1686         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1687
1688         # check the trusted.fid attribute of the OST objects of the file
1689         local have_obdidx=false
1690         local stripe_nr=0
1691         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1692                 # skip lines up to and including "obdidx"
1693                 [ -z "$obdidx" ] && break
1694                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1695                 $have_obdidx || continue
1696
1697                 local ost=$((obdidx + 1))
1698                 local dev=$(ostdevname $ost)
1699
1700                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1701                         echo "Currently only works with ldiskfs-based OSTs"
1702                         continue
1703                 fi
1704
1705                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1706
1707                 #don't unmount/remount the OSTs if we don't need to do that
1708                 #local dir=$(facet_mntpt ost$ost)
1709                 #stop ost$dev
1710                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1711                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1712                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1713                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1714                 seq=$(echo $seq | sed -e "s/^0x//g")
1715                 if [ $seq == 0 ]; then
1716                         oid_hex=$(echo $oid)
1717                 else
1718                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1719                 fi
1720                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1721                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1722                            $dev 2>/dev/null" | grep "parent=")
1723
1724                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1725
1726                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1727
1728                 #do_facet ost$ost umount -d $dir
1729                 #start ost$ost $dev $OST_MOUNT_OPTS
1730
1731                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1732                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1733                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1734                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1735                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1736                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1737
1738                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1739                 [ $ff_pseq = $lmm_seq ] ||
1740                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1741                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1742                 [ $ff_poid = $lmm_oid ] ||
1743                         error "FF parent OID $ff_poid != $lmm_oid"
1744                 [ $ff_pstripe = $stripe_nr ] ||
1745                         error "FF stripe $ff_pstripe != $stripe_nr"
1746
1747                 stripe_nr=$((stripe_nr + 1))
1748         done
1749 }
1750
1751 test_27z() {
1752         remote_ost_nodsh && skip "remote OST with nodsh" && return
1753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1754         test_mkdir -p $DIR/$tdir
1755
1756         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1757                 { error "setstripe -c -1 failed"; return 1; }
1758         # We need to send a write to every object to get parent FID info set.
1759         # This _should_ also work for setattr, but does not currently.
1760         # touch $DIR/$tdir/$tfile-1 ||
1761         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1762                 { error "dd $tfile-1 failed"; return 2; }
1763         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1764                 { error "setstripe -c -1 failed"; return 3; }
1765         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1766                 { error "dd $tfile-2 failed"; return 4; }
1767
1768         # make sure write RPCs have been sent to OSTs
1769         sync; sleep 5; sync
1770
1771         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1772         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1773 }
1774 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1775
1776 test_27A() { # b=19102
1777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1778         local restore_size=$($GETSTRIPE -S $MOUNT)
1779         local restore_count=$($GETSTRIPE -c $MOUNT)
1780         local restore_offset=$($GETSTRIPE -i $MOUNT)
1781         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1782         local default_size=$($GETSTRIPE -S $MOUNT)
1783         local default_count=$($GETSTRIPE -c $MOUNT)
1784         local default_offset=$($GETSTRIPE -i $MOUNT)
1785         local dsize=$((1024 * 1024))
1786         [ $default_size -eq $dsize ] ||
1787                 error "stripe size $default_size != $dsize"
1788         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1789         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1790         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1791 }
1792 run_test 27A "check filesystem-wide default LOV EA values"
1793
1794 test_27B() { # LU-2523
1795         test_mkdir -p $DIR/$tdir
1796         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1797         touch $DIR/$tdir/f0
1798         # open f1 with O_LOV_DELAY_CREATE
1799         # rename f0 onto f1
1800         # call setstripe ioctl on open file descriptor for f1
1801         # close
1802         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1803                 $DIR/$tdir/f0
1804
1805         rm -f $DIR/$tdir/f1
1806         # open f1 with O_LOV_DELAY_CREATE
1807         # unlink f1
1808         # call setstripe ioctl on open file descriptor for f1
1809         # close
1810         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1811
1812         # Allow multiop to fail in imitation of NFS's busted semantics.
1813         true
1814 }
1815 run_test 27B "call setstripe on open unlinked file/rename victim"
1816
1817 test_27C() { #LU-2871
1818         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1819
1820         declare -a ost_idx
1821         local index
1822         local i
1823         local j
1824
1825         test_mkdir -p $DIR/$tdir
1826         cd $DIR/$tdir
1827         for i in $(seq 0 $((OSTCOUNT - 1))); do
1828                 # set stripe across all OSTs starting from OST$i
1829                 $SETSTRIPE -i $i -c -1 $tfile$i
1830                 # get striping information
1831                 ost_idx=($($GETSTRIPE $tfile$i |
1832                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1833                 echo ${ost_idx[@]}
1834                 # check the layout
1835                 for j in $(seq 0 $((OSTCOUNT - 1))); do
1836                         index=$(((i + j) % OSTCOUNT))
1837                         [ ${ost_idx[$j]} -eq $index ] || error
1838                 done
1839         done
1840 }
1841 run_test 27C "check full striping across all OSTs"
1842
1843 # createtest also checks that device nodes are created and
1844 # then visible correctly (#2091)
1845 test_28() { # bug 2091
1846         test_mkdir $DIR/d28
1847         $CREATETEST $DIR/d28/ct || error
1848 }
1849 run_test 28 "create/mknod/mkdir with bad file types ============"
1850
1851 test_29() {
1852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1853         cancel_lru_locks mdc
1854         test_mkdir $DIR/d29
1855         touch $DIR/d29/foo
1856         log 'first d29'
1857         ls -l $DIR/d29
1858
1859         declare -i LOCKCOUNTORIG=0
1860         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1861                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1862         done
1863         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1864
1865         declare -i LOCKUNUSEDCOUNTORIG=0
1866         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1867                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1868         done
1869
1870         log 'second d29'
1871         ls -l $DIR/d29
1872         log 'done'
1873
1874         declare -i LOCKCOUNTCURRENT=0
1875         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1876                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1877         done
1878
1879         declare -i LOCKUNUSEDCOUNTCURRENT=0
1880         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1881                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1882         done
1883
1884         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1885                 lctl set_param -n ldlm.dump_namespaces ""
1886                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1887                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1888                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1889                 return 2
1890         fi
1891         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1892                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1893                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1894                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1895                 return 3
1896         fi
1897 }
1898 run_test 29 "IT_GETATTR regression  ============================"
1899
1900 test_30a() { # was test_30
1901         cp `which ls` $DIR || cp /bin/ls $DIR
1902         $DIR/ls / || error
1903         rm $DIR/ls
1904 }
1905 run_test 30a "execute binary from Lustre (execve) =============="
1906
1907 test_30b() {
1908         cp `which ls` $DIR || cp /bin/ls $DIR
1909         chmod go+rx $DIR/ls
1910         $RUNAS $DIR/ls / || error
1911         rm $DIR/ls
1912 }
1913 run_test 30b "execute binary from Lustre as non-root ==========="
1914
1915 test_30c() { # b=22376
1916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1917         cp `which ls` $DIR || cp /bin/ls $DIR
1918         chmod a-rw $DIR/ls
1919         cancel_lru_locks mdc
1920         cancel_lru_locks osc
1921         $RUNAS $DIR/ls / || error
1922         rm -f $DIR/ls
1923 }
1924 run_test 30c "execute binary from Lustre without read perms ===="
1925
1926 test_31a() {
1927         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1928         $CHECKSTAT -a $DIR/f31 || error
1929 }
1930 run_test 31a "open-unlink file =================================="
1931
1932 test_31b() {
1933         touch $DIR/f31 || error
1934         ln $DIR/f31 $DIR/f31b || error
1935         $MULTIOP $DIR/f31b Ouc || error
1936         $CHECKSTAT -t file $DIR/f31 || error
1937 }
1938 run_test 31b "unlink file with multiple links while open ======="
1939
1940 test_31c() {
1941         touch $DIR/f31 || error
1942         ln $DIR/f31 $DIR/f31c || error
1943         multiop_bg_pause $DIR/f31 O_uc || return 1
1944         MULTIPID=$!
1945         $MULTIOP $DIR/f31c Ouc
1946         kill -USR1 $MULTIPID
1947         wait $MULTIPID
1948 }
1949 run_test 31c "open-unlink file with multiple links ============="
1950
1951 test_31d() {
1952         opendirunlink $DIR/d31d $DIR/d31d || error
1953         $CHECKSTAT -a $DIR/d31d || error
1954 }
1955 run_test 31d "remove of open directory ========================="
1956
1957 test_31e() { # bug 2904
1958         check_kernel_version 34 || return 0
1959         openfilleddirunlink $DIR/d31e || error
1960 }
1961 run_test 31e "remove of open non-empty directory ==============="
1962
1963 test_31f() { # bug 4554
1964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1965         set -vx
1966         test_mkdir $DIR/d31f
1967         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1968         cp /etc/hosts $DIR/d31f
1969         ls -l $DIR/d31f
1970         $GETSTRIPE $DIR/d31f/hosts
1971         multiop_bg_pause $DIR/d31f D_c || return 1
1972         MULTIPID=$!
1973
1974         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1975         test_mkdir $DIR/d31f
1976         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1977         cp /etc/hosts $DIR/d31f
1978         ls -l $DIR/d31f
1979         $GETSTRIPE $DIR/d31f/hosts
1980         multiop_bg_pause $DIR/d31f D_c || return 1
1981         MULTIPID2=$!
1982
1983         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1984         wait $MULTIPID || error "first opendir $MULTIPID failed"
1985
1986         sleep 6
1987
1988         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1989         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1990         set +vx
1991 }
1992 run_test 31f "remove of open directory with open-unlink file ==="
1993
1994 test_31g() {
1995         echo "-- cross directory link --"
1996         test_mkdir $DIR/d31ga
1997         test_mkdir $DIR/d31gb
1998         touch $DIR/d31ga/f
1999         ln $DIR/d31ga/f $DIR/d31gb/g
2000         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
2001         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
2002         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
2003         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
2004 }
2005 run_test 31g "cross directory link==============="
2006
2007 test_31h() {
2008         echo "-- cross directory link --"
2009         test_mkdir $DIR/d31h
2010         test_mkdir $DIR/d31h/dir
2011         touch $DIR/d31h/f
2012         ln $DIR/d31h/f $DIR/d31h/dir/g
2013         $CHECKSTAT -t file $DIR/d31h/f || error "source"
2014         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
2015         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
2016         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
2017 }
2018 run_test 31h "cross directory link under child==============="
2019
2020 test_31i() {
2021         echo "-- cross directory link --"
2022         test_mkdir $DIR/d31i
2023         test_mkdir $DIR/d31i/dir
2024         touch $DIR/d31i/dir/f
2025         ln $DIR/d31i/dir/f $DIR/d31i/g
2026         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
2027         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
2028         $CHECKSTAT -t file $DIR/d31i/g || error "target"
2029         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
2030 }
2031 run_test 31i "cross directory link under parent==============="
2032
2033
2034 test_31j() {
2035         test_mkdir $DIR/d31j
2036         test_mkdir $DIR/d31j/dir1
2037         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
2038         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
2039         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
2040         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
2041         return 0
2042 }
2043 run_test 31j "link for directory==============="
2044
2045
2046 test_31k() {
2047         test_mkdir $DIR/d31k
2048         touch $DIR/d31k/s
2049         touch $DIR/d31k/exist
2050         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
2051         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
2052         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
2053         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
2054         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
2055         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
2056         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
2057         return 0
2058 }
2059 run_test 31k "link to file: the same, non-existing, dir==============="
2060
2061 test_31m() {
2062         test_mkdir $DIR/d31m
2063         touch $DIR/d31m/s
2064         test_mkdir $DIR/d31m2
2065         touch $DIR/d31m2/exist
2066         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2067         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2068         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2069         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2070         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2071         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2072         return 0
2073 }
2074 run_test 31m "link to file: the same, non-existing, dir==============="
2075
2076 test_31n() {
2077         [ -e /proc/self/fd/173 ] && echo "skipping, fd 173 is in use" && return
2078         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2079         nlink=$(stat --format=%h $DIR/$tfile)
2080         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2081         exec 173<$DIR/$tfile
2082         trap "exec 173<&-" EXIT
2083         nlink=$(stat --dereference --format=%h /proc/self/fd/173)
2084         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2085         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2086         nlink=$(stat --dereference --format=%h /proc/self/fd/173)
2087         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2088         exec 173<&-
2089 }
2090 run_test 31n "check link count of unlinked file"
2091
2092 cleanup_test32_mount() {
2093         trap 0
2094         $UMOUNT $DIR/$tdir/ext2-mountpoint
2095 }
2096
2097 test_32a() {
2098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2099         echo "== more mountpoints and symlinks ================="
2100         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2101         trap cleanup_test32_mount EXIT
2102         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2103         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2104         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2105         cleanup_test32_mount
2106 }
2107 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2108
2109 test_32b() {
2110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2111         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2112         trap cleanup_test32_mount EXIT
2113         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2114         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2115         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2116         cleanup_test32_mount
2117 }
2118 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2119
2120 test_32c() {
2121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2122         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2123         trap cleanup_test32_mount EXIT
2124         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2125         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2126         test_mkdir -p $DIR/$tdir/d2/test_dir
2127         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2128         cleanup_test32_mount
2129 }
2130 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2131
2132 test_32d() {
2133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2134         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2135         trap cleanup_test32_mount EXIT
2136         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2137         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2138         test_mkdir -p $DIR/$tdir/d2/test_dir
2139         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2140         cleanup_test32_mount
2141 }
2142 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2143
2144 test_32e() {
2145         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2146         test_mkdir -p $DIR/d32e/tmp
2147         TMP_DIR=$DIR/d32e/tmp
2148         ln -s $DIR/d32e $TMP_DIR/symlink11
2149         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2150         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2151         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2152 }
2153 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2154
2155 test_32f() {
2156         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2157         test_mkdir -p $DIR/d32f/tmp
2158         TMP_DIR=$DIR/d32f/tmp
2159         ln -s $DIR/d32f $TMP_DIR/symlink11
2160         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2161         ls $DIR/d32f/tmp/symlink11  || error
2162         ls $DIR/d32f/symlink01 || error
2163 }
2164 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2165
2166 test_32g() {
2167         TMP_DIR=$DIR/$tdir/tmp
2168         test_mkdir -p $DIR/$tdir/tmp
2169         test_mkdir $DIR/${tdir}2
2170         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2171         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2172         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2173         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2174         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2175         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2176 }
2177 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2178
2179 test_32h() {
2180         rm -fr $DIR/$tdir $DIR/${tdir}2
2181         TMP_DIR=$DIR/$tdir/tmp
2182         test_mkdir -p $DIR/$tdir/tmp
2183         test_mkdir $DIR/${tdir}2
2184         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2185         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2186         ls $TMP_DIR/symlink12 || error
2187         ls $DIR/$tdir/symlink02  || error
2188 }
2189 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2190
2191 test_32i() {
2192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2193         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2194         trap cleanup_test32_mount EXIT
2195         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2196         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2197         touch $DIR/$tdir/test_file
2198         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2199         cleanup_test32_mount
2200 }
2201 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2202
2203 test_32j() {
2204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2205         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2206         trap cleanup_test32_mount EXIT
2207         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2208         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2209         touch $DIR/$tdir/test_file
2210         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2211         cleanup_test32_mount
2212 }
2213 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2214
2215 test_32k() {
2216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2217         rm -fr $DIR/$tdir
2218         trap cleanup_test32_mount EXIT
2219         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2220         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2221         test_mkdir -p $DIR/$tdir/d2
2222         touch $DIR/$tdir/d2/test_file || error
2223         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2224         cleanup_test32_mount
2225 }
2226 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2227
2228 test_32l() {
2229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2230         rm -fr $DIR/$tdir
2231         trap cleanup_test32_mount EXIT
2232         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2233         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2234         test_mkdir -p $DIR/$tdir/d2
2235         touch $DIR/$tdir/d2/test_file
2236         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2237         cleanup_test32_mount
2238 }
2239 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2240
2241 test_32m() {
2242         rm -fr $DIR/d32m
2243         test_mkdir -p $DIR/d32m/tmp
2244         TMP_DIR=$DIR/d32m/tmp
2245         ln -s $DIR $TMP_DIR/symlink11
2246         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2247         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2248         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2249 }
2250 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2251
2252 test_32n() {
2253         rm -fr $DIR/d32n
2254         test_mkdir -p $DIR/d32n/tmp
2255         TMP_DIR=$DIR/d32n/tmp
2256         ln -s $DIR $TMP_DIR/symlink11
2257         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2258         ls -l $DIR/d32n/tmp/symlink11  || error
2259         ls -l $DIR/d32n/symlink01 || error
2260 }
2261 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2262
2263 test_32o() {
2264         rm -fr $DIR/d32o $DIR/$tfile
2265         touch $DIR/$tfile
2266         test_mkdir -p $DIR/d32o/tmp
2267         TMP_DIR=$DIR/d32o/tmp
2268         ln -s $DIR/$tfile $TMP_DIR/symlink12
2269         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2270         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2271         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2272         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2273         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2274 }
2275 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2276
2277 test_32p() {
2278     log 32p_1
2279         rm -fr $DIR/d32p
2280     log 32p_2
2281         rm -f $DIR/$tfile
2282     log 32p_3
2283         touch $DIR/$tfile
2284     log 32p_4
2285         test_mkdir -p $DIR/d32p/tmp
2286     log 32p_5
2287         TMP_DIR=$DIR/d32p/tmp
2288     log 32p_6
2289         ln -s $DIR/$tfile $TMP_DIR/symlink12
2290     log 32p_7
2291         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2292     log 32p_8
2293         cat $DIR/d32p/tmp/symlink12 || error
2294     log 32p_9
2295         cat $DIR/d32p/symlink02 || error
2296     log 32p_10
2297 }
2298 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2299
2300 cleanup_testdir_mount() {
2301         trap 0
2302         $UMOUNT $DIR/$tdir
2303 }
2304
2305 test_32q() {
2306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2307         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2308         trap cleanup_testdir_mount EXIT
2309         test_mkdir -p $DIR/$tdir
2310         touch $DIR/$tdir/under_the_mount
2311         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2312         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2313         cleanup_testdir_mount
2314 }
2315 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2316
2317 test_32r() {
2318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2319         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2320         trap cleanup_testdir_mount EXIT
2321         test_mkdir -p $DIR/$tdir
2322         touch $DIR/$tdir/under_the_mount
2323         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2324         ls $DIR/$tdir | grep -q under_the_mount && error || true
2325         cleanup_testdir_mount
2326 }
2327 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2328
2329 test_33aa() {
2330         rm -f $DIR/$tfile
2331         touch $DIR/$tfile
2332         chmod 444 $DIR/$tfile
2333         chown $RUNAS_ID $DIR/$tfile
2334         log 33_1
2335         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2336         log 33_2
2337 }
2338 run_test 33aa "write file with mode 444 (should return error) ===="
2339
2340 test_33a() {
2341         rm -fr $DIR/d33
2342         test_mkdir -p $DIR/d33
2343         chown $RUNAS_ID $DIR/d33
2344         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2345         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2346                 error "open RDWR" || true
2347 }
2348 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2349
2350 test_33b() {
2351         rm -fr $DIR/d33
2352         test_mkdir -p $DIR/d33
2353         chown $RUNAS_ID $DIR/d33
2354         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2355 }
2356 run_test 33b "test open file with malformed flags (No panic and return error)"
2357
2358 test_33c() {
2359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2360         local ostnum
2361         local ostname
2362         local write_bytes
2363         local all_zeros
2364
2365         remote_ost_nodsh && skip "remote OST with nodsh" && return
2366         all_zeros=:
2367         rm -fr $DIR/d33
2368         test_mkdir -p $DIR/d33
2369         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2370
2371         sync
2372         for ostnum in $(seq $OSTCOUNT); do
2373                 # test-framework's OST numbering is one-based, while Lustre's
2374                 # is zero-based
2375                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2376                 # Parsing llobdstat's output sucks; we could grep the /proc
2377                 # path, but that's likely to not be as portable as using the
2378                 # llobdstat utility.  So we parse lctl output instead.
2379                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2380                         obdfilter/$ostname/stats |
2381                         awk '/^write_bytes/ {print $7}' )
2382                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2383                 if (( ${write_bytes:-0} > 0 ))
2384                 then
2385                         all_zeros=false
2386                         break;
2387                 fi
2388         done
2389
2390         $all_zeros || return 0
2391
2392         # Write four bytes
2393         echo foo > $DIR/d33/bar
2394         # Really write them
2395         sync
2396
2397         # Total up write_bytes after writing.  We'd better find non-zeros.
2398         for ostnum in $(seq $OSTCOUNT); do
2399                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2400                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2401                         obdfilter/$ostname/stats |
2402                         awk '/^write_bytes/ {print $7}' )
2403                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2404                 if (( ${write_bytes:-0} > 0 ))
2405                 then
2406                         all_zeros=false
2407                         break;
2408                 fi
2409         done
2410
2411         if $all_zeros
2412         then
2413                 for ostnum in $(seq $OSTCOUNT); do
2414                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2415                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2416                         do_facet ost$ostnum lctl get_param -n \
2417                                 obdfilter/$ostname/stats
2418                 done
2419                 error "OST not keeping write_bytes stats (b22312)"
2420         fi
2421 }
2422 run_test 33c "test llobdstat and write_bytes"
2423
2424 test_33d() {
2425         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2427         local MDTIDX=1
2428         local remote_dir=$DIR/$tdir/remote_dir
2429
2430         mkdir -p $DIR/$tdir
2431         $LFS mkdir -i $MDTIDX $remote_dir ||
2432                 error "create remote directory failed"
2433
2434         touch $remote_dir/$tfile
2435         chmod 444 $remote_dir/$tfile
2436         chown $RUNAS_ID $remote_dir/$tfile
2437
2438         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2439
2440         chown $RUNAS_ID $remote_dir
2441         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2442                                         error "create" || true
2443         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2444                                     error "open RDWR" || true
2445         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2446                                     error "create" || true
2447 }
2448 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2449
2450 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2451 test_34a() {
2452         rm -f $DIR/f34
2453         $MCREATE $DIR/f34 || error
2454         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2455         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2456         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2457         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2458 }
2459 run_test 34a "truncate file that has not been opened ==========="
2460
2461 test_34b() {
2462         [ ! -f $DIR/f34 ] && test_34a
2463         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2464         $OPENFILE -f O_RDONLY $DIR/f34
2465         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2466         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2467 }
2468 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2469
2470 test_34c() {
2471         [ ! -f $DIR/f34 ] && test_34a
2472         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2473         $OPENFILE -f O_RDWR $DIR/f34
2474         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2475         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2476 }
2477 run_test 34c "O_RDWR opening file-with-size works =============="
2478
2479 test_34d() {
2480         [ ! -f $DIR/f34 ] && test_34a
2481         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2482         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2483         rm $DIR/f34
2484 }
2485 run_test 34d "write to sparse file ============================="
2486
2487 test_34e() {
2488         rm -f $DIR/f34e
2489         $MCREATE $DIR/f34e || error
2490         $TRUNCATE $DIR/f34e 1000 || error
2491         $CHECKSTAT -s 1000 $DIR/f34e || error
2492         $OPENFILE -f O_RDWR $DIR/f34e
2493         $CHECKSTAT -s 1000 $DIR/f34e || error
2494 }
2495 run_test 34e "create objects, some with size and some without =="
2496
2497 test_34f() { # bug 6242, 6243
2498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2499         SIZE34F=48000
2500         rm -f $DIR/f34f
2501         $MCREATE $DIR/f34f || error
2502         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2503         dd if=$DIR/f34f of=$TMP/f34f
2504         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2505         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2506         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2507         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2508         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2509 }
2510 run_test 34f "read from a file with no objects until EOF ======="
2511
2512 test_34g() {
2513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2514         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2515         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2516         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2517         cancel_lru_locks osc
2518         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2519                 error "wrong size after lock cancel"
2520
2521         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2522         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2523                 error "expanding truncate failed"
2524         cancel_lru_locks osc
2525         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2526                 error "wrong expanded size after lock cancel"
2527 }
2528 run_test 34g "truncate long file ==============================="
2529
2530 test_34h() {
2531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2532         local gid=10
2533         local sz=1000
2534
2535         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2536         sync # Flush the cache so that multiop below does not block on cache
2537              # flush when getting the group lock
2538         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2539         MULTIPID=$!
2540         sleep 2
2541
2542         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2543                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2544                 kill -9 $MULTIPID
2545         fi
2546         wait $MULTIPID
2547         local nsz=`stat -c %s $DIR/$tfile`
2548         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2549 }
2550 run_test 34h "ftruncate file under grouplock should not block"
2551
2552 test_35a() {
2553         cp /bin/sh $DIR/f35a
2554         chmod 444 $DIR/f35a
2555         chown $RUNAS_ID $DIR/f35a
2556         $RUNAS $DIR/f35a && error || true
2557         rm $DIR/f35a
2558 }
2559 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2560
2561 test_36a() {
2562         rm -f $DIR/f36
2563         utime $DIR/f36 || error
2564 }
2565 run_test 36a "MDS utime check (mknod, utime) ==================="
2566
2567 test_36b() {
2568         echo "" > $DIR/f36
2569         utime $DIR/f36 || error
2570 }
2571 run_test 36b "OST utime check (open, utime) ===================="
2572
2573 test_36c() {
2574         rm -f $DIR/d36/f36
2575         test_mkdir $DIR/d36
2576         chown $RUNAS_ID $DIR/d36
2577         $RUNAS utime $DIR/d36/f36 || error
2578 }
2579 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2580
2581 test_36d() {
2582         [ ! -d $DIR/d36 ] && test_36c
2583         echo "" > $DIR/d36/f36
2584         $RUNAS utime $DIR/d36/f36 || error
2585 }
2586 run_test 36d "non-root OST utime check (open, utime) ==========="
2587
2588 test_36e() {
2589         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2590         test_mkdir -p $DIR/$tdir
2591         touch $DIR/$tdir/$tfile
2592         $RUNAS utime $DIR/$tdir/$tfile && \
2593                 error "utime worked, expected failure" || true
2594 }
2595 run_test 36e "utime on non-owned file (should return error) ===="
2596
2597 subr_36fh() {
2598         local fl="$1"
2599         local LANG_SAVE=$LANG
2600         local LC_LANG_SAVE=$LC_LANG
2601         export LANG=C LC_LANG=C # for date language
2602
2603         DATESTR="Dec 20  2000"
2604         test_mkdir -p $DIR/$tdir
2605         lctl set_param fail_loc=$fl
2606         date; date +%s
2607         cp /etc/hosts $DIR/$tdir/$tfile
2608         sync & # write RPC generated with "current" inode timestamp, but delayed
2609         sleep 1
2610         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2611         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2612         cancel_lru_locks osc
2613         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2614         date; date +%s
2615         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2616                 echo "BEFORE: $LS_BEFORE" && \
2617                 echo "AFTER : $LS_AFTER" && \
2618                 echo "WANT  : $DATESTR" && \
2619                 error "$DIR/$tdir/$tfile timestamps changed" || true
2620
2621         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2622 }
2623
2624 test_36f() {
2625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2626         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2627         subr_36fh "0x80000214"
2628 }
2629 run_test 36f "utime on file racing with OST BRW write =========="
2630
2631 test_36g() {
2632         remote_ost_nodsh && skip "remote OST with nodsh" && return
2633         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2634         local fmd_max_age
2635         local fmd_before
2636         local fmd_after
2637
2638         test_mkdir -p $DIR/$tdir
2639         fmd_max_age=$(do_facet ost1 \
2640                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2641                 head -n 1")
2642
2643         fmd_before=$(do_facet ost1 \
2644                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2645         touch $DIR/$tdir/$tfile
2646         sleep $((fmd_max_age + 12))
2647         fmd_after=$(do_facet ost1 \
2648                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2649
2650         echo "fmd_before: $fmd_before"
2651         echo "fmd_after: $fmd_after"
2652         [ "$fmd_after" -gt "$fmd_before" ] && \
2653                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2654                 error "fmd didn't expire after ping" || true
2655 }
2656 run_test 36g "filter mod data cache expiry ====================="
2657
2658 test_36h() {
2659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2660         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2661         subr_36fh "0x80000227"
2662 }
2663 run_test 36h "utime on file racing with OST BRW write =========="
2664
2665 # test_37 - duplicate with tests 32q 32r
2666
2667 test_38() {
2668         local file=$DIR/$tfile
2669         touch $file
2670         openfile -f O_DIRECTORY $file
2671         local RC=$?
2672         local ENOTDIR=20
2673         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2674         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2675 }
2676 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2677
2678 test_39() {
2679         touch $DIR/$tfile
2680         touch $DIR/${tfile}2
2681 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2682 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2683 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2684         sleep 2
2685         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2686         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2687                 echo "mtime"
2688                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2689                 echo "atime"
2690                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2691                 echo "ctime"
2692                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2693                 error "O_TRUNC didn't change timestamps"
2694         fi
2695 }
2696 run_test 39 "mtime changed on create ==========================="
2697
2698 test_39b() {
2699         test_mkdir -p $DIR/$tdir
2700         cp -p /etc/passwd $DIR/$tdir/fopen
2701         cp -p /etc/passwd $DIR/$tdir/flink
2702         cp -p /etc/passwd $DIR/$tdir/funlink
2703         cp -p /etc/passwd $DIR/$tdir/frename
2704         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2705
2706         sleep 1
2707         echo "aaaaaa" >> $DIR/$tdir/fopen
2708         echo "aaaaaa" >> $DIR/$tdir/flink
2709         echo "aaaaaa" >> $DIR/$tdir/funlink
2710         echo "aaaaaa" >> $DIR/$tdir/frename
2711
2712         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2713         local link_new=`stat -c %Y $DIR/$tdir/flink`
2714         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2715         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2716
2717         cat $DIR/$tdir/fopen > /dev/null
2718         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2719         rm -f $DIR/$tdir/funlink2
2720         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2721
2722         for (( i=0; i < 2; i++ )) ; do
2723                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2724                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2725                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2726                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2727
2728                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2729                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2730                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2731                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2732
2733                 cancel_lru_locks osc
2734                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2735         done
2736 }
2737 run_test 39b "mtime change on open, link, unlink, rename  ======"
2738
2739 # this should be set to past
2740 TEST_39_MTIME=`date -d "1 year ago" +%s`
2741
2742 # bug 11063
2743 test_39c() {
2744         touch $DIR1/$tfile
2745         sleep 2
2746         local mtime0=`stat -c %Y $DIR1/$tfile`
2747
2748         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2749         local mtime1=`stat -c %Y $DIR1/$tfile`
2750         [ "$mtime1" = $TEST_39_MTIME ] || \
2751                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2752
2753         local d1=`date +%s`
2754         echo hello >> $DIR1/$tfile
2755         local d2=`date +%s`
2756         local mtime2=`stat -c %Y $DIR1/$tfile`
2757         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2758                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2759
2760         mv $DIR1/$tfile $DIR1/$tfile-1
2761
2762         for (( i=0; i < 2; i++ )) ; do
2763                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2764                 [ "$mtime2" = "$mtime3" ] || \
2765                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2766
2767                 cancel_lru_locks osc
2768                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2769         done
2770 }
2771 run_test 39c "mtime change on rename ==========================="
2772
2773 # bug 21114
2774 test_39d() {
2775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2776         touch $DIR1/$tfile
2777
2778         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2779
2780         for (( i=0; i < 2; i++ )) ; do
2781                 local mtime=`stat -c %Y $DIR1/$tfile`
2782                 [ $mtime = $TEST_39_MTIME ] || \
2783                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2784
2785                 cancel_lru_locks osc
2786                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2787         done
2788 }
2789 run_test 39d "create, utime, stat =============================="
2790
2791 # bug 21114
2792 test_39e() {
2793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2794         touch $DIR1/$tfile
2795         local mtime1=`stat -c %Y $DIR1/$tfile`
2796
2797         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2798
2799         for (( i=0; i < 2; i++ )) ; do
2800                 local mtime2=`stat -c %Y $DIR1/$tfile`
2801                 [ $mtime2 = $TEST_39_MTIME ] || \
2802                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2803
2804                 cancel_lru_locks osc
2805                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2806         done
2807 }
2808 run_test 39e "create, stat, utime, stat ========================"
2809
2810 # bug 21114
2811 test_39f() {
2812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2813         touch $DIR1/$tfile
2814         mtime1=`stat -c %Y $DIR1/$tfile`
2815
2816         sleep 2
2817         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2818
2819         for (( i=0; i < 2; i++ )) ; do
2820                 local mtime2=`stat -c %Y $DIR1/$tfile`
2821                 [ $mtime2 = $TEST_39_MTIME ] || \
2822                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2823
2824                 cancel_lru_locks osc
2825                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2826         done
2827 }
2828 run_test 39f "create, stat, sleep, utime, stat ================="
2829
2830 # bug 11063
2831 test_39g() {
2832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2833         echo hello >> $DIR1/$tfile
2834         local mtime1=`stat -c %Y $DIR1/$tfile`
2835
2836         sleep 2
2837         chmod o+r $DIR1/$tfile
2838
2839         for (( i=0; i < 2; i++ )) ; do
2840                 local mtime2=`stat -c %Y $DIR1/$tfile`
2841                 [ "$mtime1" = "$mtime2" ] || \
2842                         error "lost mtime: $mtime2, should be $mtime1"
2843
2844                 cancel_lru_locks osc
2845                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2846         done
2847 }
2848 run_test 39g "write, chmod, stat ==============================="
2849
2850 # bug 11063
2851 test_39h() {
2852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2853         touch $DIR1/$tfile
2854         sleep 1
2855
2856         local d1=`date`
2857         echo hello >> $DIR1/$tfile
2858         local mtime1=`stat -c %Y $DIR1/$tfile`
2859
2860         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2861         local d2=`date`
2862         if [ "$d1" != "$d2" ]; then
2863                 echo "write and touch not within one second"
2864         else
2865                 for (( i=0; i < 2; i++ )) ; do
2866                         local mtime2=`stat -c %Y $DIR1/$tfile`
2867                         [ "$mtime2" = $TEST_39_MTIME ] || \
2868                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2869
2870                         cancel_lru_locks osc
2871                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2872                 done
2873         fi
2874 }
2875 run_test 39h "write, utime within one second, stat ============="
2876
2877 test_39i() {
2878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2879         touch $DIR1/$tfile
2880         sleep 1
2881
2882         echo hello >> $DIR1/$tfile
2883         local mtime1=`stat -c %Y $DIR1/$tfile`
2884
2885         mv $DIR1/$tfile $DIR1/$tfile-1
2886
2887         for (( i=0; i < 2; i++ )) ; do
2888                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2889
2890                 [ "$mtime1" = "$mtime2" ] || \
2891                         error "lost mtime: $mtime2, should be $mtime1"
2892
2893                 cancel_lru_locks osc
2894                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2895         done
2896 }
2897 run_test 39i "write, rename, stat =============================="
2898
2899 test_39j() {
2900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2901         start_full_debug_logging
2902         touch $DIR1/$tfile
2903         sleep 1
2904
2905         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2906         lctl set_param fail_loc=0x80000412
2907         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2908                 error "multiop failed"
2909         local multipid=$!
2910         local mtime1=`stat -c %Y $DIR1/$tfile`
2911
2912         mv $DIR1/$tfile $DIR1/$tfile-1
2913
2914         kill -USR1 $multipid
2915         wait $multipid || error "multiop close failed"
2916
2917         for (( i=0; i < 2; i++ )) ; do
2918                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2919                 [ "$mtime1" = "$mtime2" ] ||
2920                         error "mtime is lost on close: $mtime2, " \
2921                               "should be $mtime1"
2922
2923                 cancel_lru_locks osc
2924                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2925         done
2926         lctl set_param fail_loc=0
2927         stop_full_debug_logging
2928 }
2929 run_test 39j "write, rename, close, stat ======================="
2930
2931 test_39k() {
2932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2933         touch $DIR1/$tfile
2934         sleep 1
2935
2936         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2937         local multipid=$!
2938         local mtime1=`stat -c %Y $DIR1/$tfile`
2939
2940         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2941
2942         kill -USR1 $multipid
2943         wait $multipid || error "multiop close failed"
2944
2945         for (( i=0; i < 2; i++ )) ; do
2946                 local mtime2=`stat -c %Y $DIR1/$tfile`
2947
2948                 [ "$mtime2" = $TEST_39_MTIME ] || \
2949                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2950
2951                 cancel_lru_locks osc
2952                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2953         done
2954 }
2955 run_test 39k "write, utime, close, stat ========================"
2956
2957 # this should be set to future
2958 TEST_39_ATIME=`date -d "1 year" +%s`
2959
2960 test_39l() {
2961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2962         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2963         local atime_diff=$(do_facet $SINGLEMDS \
2964                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2965         rm -rf $DIR/$tdir
2966         mkdir -p $DIR/$tdir
2967
2968         # test setting directory atime to future
2969         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2970         local atime=$(stat -c %X $DIR/$tdir)
2971         [ "$atime" = $TEST_39_ATIME ] || \
2972                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2973
2974         # test setting directory atime from future to now
2975         local d1=$(date +%s)
2976         ls $DIR/$tdir
2977         local d2=$(date +%s)
2978
2979         cancel_lru_locks mdc
2980         atime=$(stat -c %X $DIR/$tdir)
2981         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2982                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2983
2984         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2985         sleep 3
2986
2987         # test setting directory atime when now > dir atime + atime_diff
2988         d1=$(date +%s)
2989         ls $DIR/$tdir
2990         d2=$(date +%s)
2991         cancel_lru_locks mdc
2992         atime=$(stat -c %X $DIR/$tdir)
2993         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2994                 error "atime is not updated  : $atime, should be $d2"
2995
2996         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2997         sleep 3
2998
2999         # test not setting directory atime when now < dir atime + atime_diff
3000         ls $DIR/$tdir
3001         cancel_lru_locks mdc
3002         atime=$(stat -c %X $DIR/$tdir)
3003         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3004                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3005
3006         do_facet $SINGLEMDS \
3007                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3008 }
3009 run_test 39l "directory atime update ==========================="
3010
3011 test_39m() {
3012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3013         touch $DIR1/$tfile
3014         sleep 2
3015         local far_past_mtime=$(date -d "May 29 1953" +%s)
3016         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3017
3018         touch -m -d @$far_past_mtime $DIR1/$tfile
3019         touch -a -d @$far_past_atime $DIR1/$tfile
3020
3021         for (( i=0; i < 2; i++ )) ; do
3022                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3023                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3024                         error "atime or mtime set incorrectly"
3025
3026                 cancel_lru_locks osc
3027                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3028         done
3029 }
3030 run_test 39m "test atime and mtime before 1970"
3031
3032 test_40() {
3033         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
3034         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
3035         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
3036 }
3037 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3038
3039 test_41() {
3040         # bug 1553
3041         small_write $DIR/f41 18
3042 }
3043 run_test 41 "test small file write + fstat ====================="
3044
3045 count_ost_writes() {
3046         lctl get_param -n osc.*.stats |
3047             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
3048 }
3049
3050 # decent default
3051 WRITEBACK_SAVE=500
3052 DIRTY_RATIO_SAVE=40
3053 MAX_DIRTY_RATIO=50
3054 BG_DIRTY_RATIO_SAVE=10
3055 MAX_BG_DIRTY_RATIO=25
3056
3057 start_writeback() {
3058         trap 0
3059         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3060         # dirty_ratio, dirty_background_ratio
3061         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3062                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3063                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3064                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3065         else
3066                 # if file not here, we are a 2.4 kernel
3067                 kill -CONT `pidof kupdated`
3068         fi
3069 }
3070
3071 stop_writeback() {
3072         # setup the trap first, so someone cannot exit the test at the
3073         # exact wrong time and mess up a machine
3074         trap start_writeback EXIT
3075         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3076         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3077                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3078                 sysctl -w vm.dirty_writeback_centisecs=0
3079                 sysctl -w vm.dirty_writeback_centisecs=0
3080                 # save and increase /proc/sys/vm/dirty_ratio
3081                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3082                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3083                 # save and increase /proc/sys/vm/dirty_background_ratio
3084                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3085                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3086         else
3087                 # if file not here, we are a 2.4 kernel
3088                 kill -STOP `pidof kupdated`
3089         fi
3090 }
3091
3092 # ensure that all stripes have some grant before we test client-side cache
3093 setup_test42() {
3094         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3095                 dd if=/dev/zero of=$i bs=4k count=1
3096                 rm $i
3097         done
3098 }
3099
3100 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3101 # file truncation, and file removal.
3102 test_42a() {
3103         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3104         setup_test42
3105         cancel_lru_locks osc
3106         stop_writeback
3107         sync; sleep 1; sync # just to be safe
3108         BEFOREWRITES=`count_ost_writes`
3109         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3110         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3111         AFTERWRITES=`count_ost_writes`
3112         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3113                 error "$BEFOREWRITES < $AFTERWRITES"
3114         start_writeback
3115 }
3116 run_test 42a "ensure that we don't flush on close =============="
3117
3118 test_42b() {
3119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3120         setup_test42
3121         cancel_lru_locks osc
3122         stop_writeback
3123         sync
3124         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3125         BEFOREWRITES=`count_ost_writes`
3126         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3127         AFTERWRITES=`count_ost_writes`
3128         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3129                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3130         fi
3131         BEFOREWRITES=`count_ost_writes`
3132         sync || error "sync: $?"
3133         AFTERWRITES=`count_ost_writes`
3134         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3135                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3136         fi
3137         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3138         start_writeback
3139         return 0
3140 }
3141 run_test 42b "test destroy of file with cached dirty data ======"
3142
3143 # if these tests just want to test the effect of truncation,
3144 # they have to be very careful.  consider:
3145 # - the first open gets a {0,EOF}PR lock
3146 # - the first write conflicts and gets a {0, count-1}PW
3147 # - the rest of the writes are under {count,EOF}PW
3148 # - the open for truncate tries to match a {0,EOF}PR
3149 #   for the filesize and cancels the PWs.
3150 # any number of fixes (don't get {0,EOF} on open, match
3151 # composite locks, do smarter file size management) fix
3152 # this, but for now we want these tests to verify that
3153 # the cancellation with truncate intent works, so we
3154 # start the file with a full-file pw lock to match against
3155 # until the truncate.
3156 trunc_test() {
3157         test=$1
3158         file=$DIR/$test
3159         offset=$2
3160         cancel_lru_locks osc
3161         stop_writeback
3162         # prime the file with 0,EOF PW to match
3163         touch $file
3164         $TRUNCATE $file 0
3165         sync; sync
3166         # now the real test..
3167         dd if=/dev/zero of=$file bs=1024 count=100
3168         BEFOREWRITES=`count_ost_writes`
3169         $TRUNCATE $file $offset
3170         cancel_lru_locks osc
3171         AFTERWRITES=`count_ost_writes`
3172         start_writeback
3173 }
3174
3175 test_42c() {
3176         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3177         trunc_test 42c 1024
3178         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3179             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3180         rm $file
3181 }
3182 run_test 42c "test partial truncate of file with cached dirty data"
3183
3184 test_42d() {
3185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3186         trunc_test 42d 0
3187         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3188             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3189         rm $file
3190 }
3191 run_test 42d "test complete truncate of file with cached dirty data"
3192
3193 test_42e() { # bug22074
3194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3195         local TDIR=$DIR/${tdir}e
3196         local pagesz=$(page_size)
3197         local pages=16 # hardcoded 16 pages, don't change it.
3198         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3199         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3200         local max_dirty_mb
3201         local warmup_files
3202
3203         test_mkdir -p $DIR/${tdir}e
3204         $SETSTRIPE -c 1 $TDIR
3205         createmany -o $TDIR/f $files
3206
3207         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3208
3209         # we assume that with $OSTCOUNT files, at least one of them will
3210         # be allocated on OST0.
3211         warmup_files=$((OSTCOUNT * max_dirty_mb))
3212         createmany -o $TDIR/w $warmup_files
3213
3214         # write a large amount of data into one file and sync, to get good
3215         # avail_grant number from OST.
3216         for ((i=0; i<$warmup_files; i++)); do
3217                 idx=$($GETSTRIPE -i $TDIR/w$i)
3218                 [ $idx -ne 0 ] && continue
3219                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3220                 break
3221         done
3222         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3223         sync
3224         $LCTL get_param $proc_osc0/cur_dirty_bytes
3225         $LCTL get_param $proc_osc0/cur_grant_bytes
3226
3227         # create as much dirty pages as we can while not to trigger the actual
3228         # RPCs directly. but depends on the env, VFS may trigger flush during this
3229         # period, hopefully we are good.
3230         for ((i=0; i<$warmup_files; i++)); do
3231                 idx=$($GETSTRIPE -i $TDIR/w$i)
3232                 [ $idx -ne 0 ] && continue
3233                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3234         done
3235         $LCTL get_param $proc_osc0/cur_dirty_bytes
3236         $LCTL get_param $proc_osc0/cur_grant_bytes
3237
3238         # perform the real test
3239         $LCTL set_param $proc_osc0/rpc_stats 0
3240         for ((;i<$files; i++)); do
3241                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3242                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3243         done
3244         sync
3245         $LCTL get_param $proc_osc0/rpc_stats
3246
3247         local percent=0
3248         local have_ppr=false
3249         $LCTL get_param $proc_osc0/rpc_stats |
3250                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3251                         # skip lines until we are at the RPC histogram data
3252                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3253                         $have_ppr || continue
3254
3255                         # we only want the percent stat for < 16 pages
3256                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3257
3258                         percent=$((percent + WPCT))
3259                         if [ $percent -gt 15 ]; then
3260                                 error "less than 16-pages write RPCs" \
3261                                       "$percent% > 15%"
3262                                 break
3263                         fi
3264                 done
3265         rm -rf $TDIR
3266 }
3267 run_test 42e "verify sub-RPC writes are not done synchronously"
3268
3269 test_43() {
3270         test_mkdir -p $DIR/$tdir
3271         cp -p /bin/ls $DIR/$tdir/$tfile
3272         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3273         pid=$!
3274         # give multiop a chance to open
3275         sleep 1
3276
3277         $DIR/$tdir/$tfile && error || true
3278         kill -USR1 $pid
3279 }
3280 run_test 43 "execution of file opened for write should return -ETXTBSY"
3281
3282 test_43a() {
3283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3284         test_mkdir -p $DIR/$tdir
3285         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3286                         cp -p multiop $DIR/$tdir/multiop
3287         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3288                         return 1
3289         MULTIOP_PID=$!
3290         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3291         kill -USR1 $MULTIOP_PID || return 2
3292         wait $MULTIOP_PID || return 3
3293         rm $TMP/test43.junk
3294 }
3295 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3296
3297 test_43b() {
3298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3299         test_mkdir -p $DIR/$tdir
3300         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3301                         cp -p multiop $DIR/$tdir/multiop
3302         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3303                         return 1
3304         MULTIOP_PID=$!
3305         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3306         kill -USR1 $MULTIOP_PID || return 2
3307         wait $MULTIOP_PID || return 3
3308         rm $TMP/test43.junk
3309 }
3310 run_test 43b "truncate of file being executed should return -ETXTBSY"
3311
3312 test_43c() {
3313         local testdir="$DIR/$tdir"
3314         test_mkdir -p $DIR/$tdir
3315         cp $SHELL $testdir/
3316         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3317                 ( cd $testdir && md5sum -c)
3318 }
3319 run_test 43c "md5sum of copy into lustre========================"
3320
3321 test_44() {
3322         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3323         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3324         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3325 }
3326 run_test 44 "zero length read from a sparse stripe ============="
3327
3328 test_44a() {
3329     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3330                          awk '{print $2}'`
3331     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3332     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3333     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3334                       awk '{print $2}'`
3335     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3336         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3337     fi
3338
3339     OFFSETS="0 $((stride/2)) $((stride-1))"
3340     for offset in $OFFSETS ; do
3341       for i in `seq 0 $((nstripe-1))`; do
3342         local GLOBALOFFSETS=""
3343         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3344         local myfn=$DIR/d44a-$size
3345         echo "--------writing $myfn at $size"
3346         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3347         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3348         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3349                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3350
3351         for j in `seq 0 $((nstripe-1))`; do
3352             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3353             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3354             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3355         done
3356         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3357                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3358         rm -f $myfn
3359       done
3360     done
3361 }
3362 run_test 44a "test sparse pwrite ==============================="
3363
3364 dirty_osc_total() {
3365         tot=0
3366         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3367                 tot=$(($tot + $d))
3368         done
3369         echo $tot
3370 }
3371 do_dirty_record() {
3372         before=`dirty_osc_total`
3373         echo executing "\"$*\""
3374         eval $*
3375         after=`dirty_osc_total`
3376         echo before $before, after $after
3377 }
3378 test_45() {
3379         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3380         f="$DIR/f45"
3381         # Obtain grants from OST if it supports it
3382         echo blah > ${f}_grant
3383         stop_writeback
3384         sync
3385         do_dirty_record "echo blah > $f"
3386         [ $before -eq $after ] && error "write wasn't cached"
3387         do_dirty_record "> $f"
3388         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3389         do_dirty_record "echo blah > $f"
3390         [ $before -eq $after ] && error "write wasn't cached"
3391         do_dirty_record "sync"
3392         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3393         do_dirty_record "echo blah > $f"
3394         [ $before -eq $after ] && error "write wasn't cached"
3395         do_dirty_record "cancel_lru_locks osc"
3396         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3397         start_writeback
3398 }
3399 run_test 45 "osc io page accounting ============================"
3400
3401 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3402 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3403 # objects offset and an assert hit when an rpc was built with 1023's mapped
3404 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3405 test_46() {
3406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3407         f="$DIR/f46"
3408         stop_writeback
3409         sync
3410         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3411         sync
3412         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3413         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3414         sync
3415         start_writeback
3416 }
3417 run_test 46 "dirtying a previously written page ================"
3418
3419 # test_47 is removed "Device nodes check" is moved to test_28
3420
3421 test_48a() { # bug 2399
3422         check_kernel_version 34 || return 0
3423         test_mkdir -p $DIR/$tdir
3424         cd $DIR/$tdir
3425         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3426         test_mkdir $DIR/$tdir || error "recreate directory failed"
3427         touch foo || error "'touch foo' failed after recreating cwd"
3428         test_mkdir $DIR/$tdir/bar ||
3429                      error "'mkdir foo' failed after recreating cwd"
3430         if check_kernel_version 44; then
3431                 touch .foo || error "'touch .foo' failed after recreating cwd"
3432                 test_mkdir $DIR/$tdir/.bar ||
3433                               error "'mkdir .foo' failed after recreating cwd"
3434         fi
3435         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3436         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3437         cd . || error "'cd .' failed after recreating cwd"
3438         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3439         rmdir . && error "'rmdir .' worked after recreating cwd"
3440         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3441         cd .. || error "'cd ..' failed after recreating cwd"
3442 }
3443 run_test 48a "Access renamed working dir (should return errors)="
3444
3445 test_48b() { # bug 2399
3446         check_kernel_version 34 || return 0
3447         rm -rf $DIR/$tdir
3448         test_mkdir -p $DIR/$tdir
3449         cd $DIR/$tdir
3450         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3451         touch foo && error "'touch foo' worked after removing cwd"
3452         test_mkdir $DIR/$tdir/foo &&
3453                      error "'mkdir foo' worked after removing cwd"
3454         if check_kernel_version 44; then
3455                 touch .foo && error "'touch .foo' worked after removing cwd"
3456                 test_mkdir $DIR/$tdir/.foo &&
3457                               error "'mkdir .foo' worked after removing cwd"
3458         fi
3459         ls . > /dev/null && error "'ls .' worked after removing cwd"
3460         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3461         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3462         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3463         rmdir . && error "'rmdir .' worked after removing cwd"
3464         ln -s . foo && error "'ln -s .' worked after removing cwd"
3465         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3466 }
3467 run_test 48b "Access removed working dir (should return errors)="
3468
3469 test_48c() { # bug 2350
3470         check_kernel_version 36 || return 0
3471         #lctl set_param debug=-1
3472         #set -vx
3473         rm -rf $DIR/$tdir
3474         test_mkdir -p $DIR/$tdir/dir
3475         cd $DIR/$tdir/dir
3476         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3477         $TRACE touch foo && error "touch foo worked after removing cwd"
3478         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3479         if check_kernel_version 44; then
3480                 touch .foo && error "touch .foo worked after removing cwd"
3481                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3482         fi
3483         $TRACE ls . && error "'ls .' worked after removing cwd"
3484         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3485         is_patchless || ( $TRACE cd . &&
3486                         error "'cd .' worked after removing cwd" )
3487         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3488         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3489         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3490         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3491 }
3492 run_test 48c "Access removed working subdir (should return errors)"
3493
3494 test_48d() { # bug 2350
3495         check_kernel_version 36 || return 0
3496         #lctl set_param debug=-1
3497         #set -vx
3498         rm -rf $DIR/$tdir
3499         test_mkdir -p $DIR/$tdir/dir
3500         cd $DIR/$tdir/dir
3501         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3502         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3503         $TRACE touch foo && error "'touch foo' worked after removing parent"
3504         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3505         if check_kernel_version 44; then
3506                 touch .foo && error "'touch .foo' worked after removing parent"
3507                 test_mkdir .foo &&
3508                               error "mkdir .foo worked after removing parent"
3509         fi
3510         $TRACE ls . && error "'ls .' worked after removing parent"
3511         $TRACE ls .. && error "'ls ..' worked after removing parent"
3512         is_patchless || ( $TRACE cd . &&
3513                         error "'cd .' worked after recreate parent" )
3514         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3515         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3516         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3517         is_patchless || ( $TRACE cd .. &&
3518                         error "'cd ..' worked after removing parent" || true )
3519 }
3520 run_test 48d "Access removed parent subdir (should return errors)"
3521
3522 test_48e() { # bug 4134
3523         check_kernel_version 41 || return 0
3524         #lctl set_param debug=-1
3525         #set -vx
3526         rm -rf $DIR/$tdir
3527         test_mkdir -p $DIR/$tdir/dir
3528         cd $DIR/$tdir/dir
3529         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3530         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3531         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3532         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3533         # On a buggy kernel addition of "touch foo" after cd .. will
3534         # produce kernel oops in lookup_hash_it
3535         touch ../foo && error "'cd ..' worked after recreate parent"
3536         cd $DIR
3537         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3538 }
3539 run_test 48e "Access to recreated parent subdir (should return errors)"
3540
3541 test_49() { # LU-1030
3542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3543         # get ost1 size - lustre-OST0000
3544         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3545         # write 800M at maximum
3546         [ $ost1_size -gt 819200 ] && ost1_size=819200
3547
3548         lfs setstripe -c 1 -i 0 $DIR/$tfile
3549         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3550         local dd_pid=$!
3551
3552         # change max_pages_per_rpc while writing the file
3553         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3554         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3555         # loop until dd process exits
3556         while ps ax -opid | grep -wq $dd_pid; do
3557                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3558                 sleep $((RANDOM % 5 + 1))
3559         done
3560         # restore original max_pages_per_rpc
3561         $LCTL set_param $osc1_mppc=$orig_mppc
3562         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3563 }
3564 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3565
3566 test_50() {
3567         # bug 1485
3568         test_mkdir $DIR/$tdir
3569         cd $DIR/$tdir
3570         ls /proc/$$/cwd || error
3571 }
3572 run_test 50 "special situations: /proc symlinks  ==============="
3573
3574 test_51a() {    # was test_51
3575         # bug 1516 - create an empty entry right after ".." then split dir
3576         test_mkdir -p $DIR/$tdir
3577         touch $DIR/$tdir/foo
3578         $MCREATE $DIR/$tdir/bar
3579         rm $DIR/$tdir/foo
3580         createmany -m $DIR/$tdir/longfile 201
3581         FNUM=202
3582         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3583                 $MCREATE $DIR/$tdir/longfile$FNUM
3584                 FNUM=$(($FNUM + 1))
3585                 echo -n "+"
3586         done
3587         echo
3588         ls -l $DIR/$tdir > /dev/null || error
3589 }
3590 run_test 51a "special situations: split htree with empty entry =="
3591
3592 export NUMTEST=70000
3593 test_51b() {
3594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3595         local BASE=$DIR/$tdir
3596
3597         # cleanup the directory
3598         rm -fr $BASE
3599
3600         test_mkdir -p $BASE
3601
3602         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3603         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3604         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3605                 return
3606
3607         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3608                 echo "reduced count to $NUMTEST due to inodes"
3609
3610         # need to check free space for the directories as well
3611         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3612         numfree=$((blkfree / 4))
3613         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3614                 echo "reduced count to $NUMTEST due to blocks"
3615
3616         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3617                 echo "failed" > $BASE/fnum
3618 }
3619 run_test 51b "exceed 64k subdirectory nlink limit"
3620
3621 test_51ba() { # LU-993
3622         local BASE=$DIR/$tdir
3623         # unlink all but 100 subdirectories, then check it still works
3624         local LEFT=100
3625         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3626
3627         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3628         local DELETE=$((NUMTEST - LEFT))
3629
3630         # continue on to run this test even if 51b didn't finish,
3631         # just to delete the many subdirectories created.
3632         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3633
3634         # for ldiskfs the nlink count should be 1, but this is OSD specific
3635         # and so this is listed for informational purposes only
3636         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3637         unlinkmany -d $BASE/d $DELETE
3638         RC=$?
3639
3640         if [ $RC -ne 0 ]; then
3641                 if [ "$NUMPREV" == "failed" ]; then
3642                         skip "previous setup failed"
3643                         return 0
3644                 else
3645                         error "unlink of first $DELETE subdirs failed"
3646                         return $RC
3647                 fi
3648         fi
3649
3650         echo "nlink between: $(stat -c %h $BASE)"
3651         # trim the first line of ls output
3652         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3653         [ $FOUND -ne $LEFT ] &&
3654                 error "can't find subdirs: found only $FOUND/$LEFT"
3655
3656         unlinkmany -d $BASE/d $DELETE $LEFT ||
3657                 error "unlink of second $LEFT subdirs failed"
3658         # regardless of whether the backing filesystem tracks nlink accurately
3659         # or not, the nlink count shouldn't be more than "." and ".." here
3660         local AFTER=$(stat -c %h $BASE)
3661         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3662                 echo "nlink after: $AFTER"
3663 }
3664 run_test 51ba "verify nlink for many subdirectory cleanup"
3665
3666 test_51d() {
3667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3668         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3669         test_mkdir -p $DIR/$tdir
3670         createmany -o $DIR/$tdir/t- 1000
3671         $GETSTRIPE $DIR/$tdir > $TMP/files
3672         for N in `seq 0 $((OSTCOUNT - 1))`; do
3673             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3674             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3675             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3676         done
3677         unlinkmany $DIR/$tdir/t- 1000
3678
3679         NLAST=0
3680         for N in `seq 1 $((OSTCOUNT - 1))`; do
3681             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3682                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3683             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3684                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3685
3686             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3687                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3688             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3689                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3690             NLAST=$N
3691         done
3692 }
3693 run_test 51d "check object distribution ===================="
3694
3695 test_52a() {
3696         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3697         test_mkdir -p $DIR/$tdir
3698         touch $DIR/$tdir/foo
3699         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3700         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3701         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3702         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3703         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3704                                         error "link worked"
3705         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3706         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3707         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3708                                                      error "lsattr"
3709         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3710         cp -r $DIR/$tdir /tmp/
3711         rm -fr $DIR/$tdir || error "cleanup rm failed"
3712 }
3713 run_test 52a "append-only flag test (should return errors) ====="
3714
3715 test_52b() {
3716         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3717         test_mkdir -p $DIR/$tdir
3718         touch $DIR/$tdir/foo
3719         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3720         cat test > $DIR/$tdir/foo && error "cat test worked"
3721         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3722         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3723         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3724                                         error "link worked"
3725         echo foo >> $DIR/$tdir/foo && error "echo worked"
3726         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3727         [ -f $DIR/$tdir/foo ] || error
3728         [ -f $DIR/$tdir/foo_ren ] && error
3729         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3730                                                         error "lsattr"
3731         chattr -i $DIR/$tdir/foo || error "chattr failed"
3732
3733         rm -fr $DIR/$tdir || error
3734 }
3735 run_test 52b "immutable flag test (should return errors) ======="
3736
3737 test_53() {
3738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3739         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3740         remote_ost_nodsh && skip "remote OST with nodsh" && return
3741
3742         local param
3743         local ostname
3744         local mds_last
3745         local ost_last
3746         local ostnum
3747         local node
3748
3749         # only test MDT0000
3750         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3751         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3752                 param=`echo ${value[0]} | cut -d "=" -f1`
3753                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3754                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3755                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3756                 node=$(facet_active_host ost$((ostnum+1)))
3757                 param="obdfilter.$ostname.last_id"
3758                 ost_last=$(do_node $node lctl get_param -n $param | head -n 1)
3759                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3760                 if [ $ost_last != $mds_last ]; then
3761                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3762                 fi
3763         done
3764 }
3765 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3766
3767 test_54a() {
3768         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3769         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3770         $SOCKETSERVER $DIR/socket
3771         $SOCKETCLIENT $DIR/socket || error
3772         $MUNLINK $DIR/socket
3773 }
3774 run_test 54a "unix domain socket test =========================="
3775
3776 test_54b() {
3777         f="$DIR/f54b"
3778         mknod $f c 1 3
3779         chmod 0666 $f
3780         dd if=/dev/zero of=$f bs=`page_size` count=1
3781 }
3782 run_test 54b "char device works in lustre ======================"
3783
3784 find_loop_dev() {
3785         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3786         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3787         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3788
3789         for i in `seq 3 7`; do
3790                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3791                 LOOPDEV=$LOOPBASE$i
3792                 LOOPNUM=$i
3793                 break
3794         done
3795 }
3796
3797 test_54c() {
3798         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3799         tfile="$DIR/f54c"
3800         tdir="$DIR/d54c"
3801         loopdev="$DIR/loop54c"
3802
3803         find_loop_dev
3804         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3805         mknod $loopdev b 7 $LOOPNUM
3806         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3807         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3808         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3809         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3810         test_mkdir -p $tdir
3811         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3812         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3813         df $tdir
3814         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3815         $UMOUNT $tdir
3816         losetup -d $loopdev
3817         rm $loopdev
3818 }
3819 run_test 54c "block device works in lustre ====================="
3820
3821 test_54d() {
3822         f="$DIR/f54d"
3823         string="aaaaaa"
3824         mknod $f p
3825         [ "$string" = `echo $string > $f | cat $f` ] || error
3826 }
3827 run_test 54d "fifo device works in lustre ======================"
3828
3829 test_54e() {
3830         check_kernel_version 46 || return 0
3831         f="$DIR/f54e"
3832         string="aaaaaa"
3833         cp -aL /dev/console $f
3834         echo $string > $f || error
3835 }
3836 run_test 54e "console/tty device works in lustre ======================"
3837
3838 #The test_55 used to be iopen test and it was removed by bz#24037.
3839 #run_test 55 "check iopen_connect_dentry() ======================"
3840
3841 test_56a() {    # was test_56
3842         rm -rf $DIR/$tdir
3843         $SETSTRIPE -d $DIR
3844         test_mkdir $DIR/$tdir
3845         test_mkdir $DIR/$tdir/dir
3846         NUMFILES=3
3847         NUMFILESx2=$(($NUMFILES * 2))
3848         for i in `seq 1 $NUMFILES` ; do
3849                 touch $DIR/$tdir/file$i
3850                 touch $DIR/$tdir/dir/file$i
3851         done
3852
3853         # test lfs getstripe with --recursive
3854         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3855         [ $FILENUM -eq $NUMFILESx2 ] ||
3856                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3857         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3858         [ $FILENUM -eq $NUMFILES ] ||
3859                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3860         echo "$GETSTRIPE --recursive passed."
3861
3862         # test lfs getstripe with file instead of dir
3863         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3864         [ $FILENUM  -eq 1 ] || error \
3865                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3866         echo "$GETSTRIPE file1 passed."
3867
3868         #test lfs getstripe with --verbose
3869         [ `$GETSTRIPE --verbose $DIR/$tdir |
3870                         grep -c lmm_magic` -eq $NUMFILES ] ||
3871                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3872         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3873             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3874         echo "$GETSTRIPE --verbose passed."
3875
3876         #test lfs getstripe with --obd
3877         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3878                                         grep -q "unknown obduuid" ||
3879                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3880
3881         [  "$OSTCOUNT" -lt 2 ] &&
3882                 skip_env "skipping other $GETSTRIPE --obd test" && return
3883
3884         OSTIDX=1
3885         OBDUUID=$(ostuuid_from_index $OSTIDX)
3886         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3887         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3888         [ $FOUND -eq $FILENUM ] ||
3889                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3890         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3891                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3892                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3893                 error "$GETSTRIPE --obd: should not show file on other obd"
3894         echo "$GETSTRIPE --obd passed"
3895 }
3896 run_test 56a "check $GETSTRIPE"
3897
3898 NUMFILES=3
3899 NUMDIRS=3
3900 setup_56() {
3901         local LOCAL_NUMFILES="$1"
3902         local LOCAL_NUMDIRS="$2"
3903         local MKDIR_PARAMS="$3"
3904
3905         if [ ! -d "$TDIR" ] ; then
3906                 test_mkdir -p $TDIR
3907                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3908                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3909                         touch $TDIR/file$i
3910                 done
3911                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3912                         test_mkdir $TDIR/dir$i
3913                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3914                                 touch $TDIR/dir$i/file$j
3915                         done
3916                 done
3917         fi
3918 }
3919
3920 setup_56_special() {
3921         LOCAL_NUMFILES=$1
3922         LOCAL_NUMDIRS=$2
3923         setup_56 $1 $2
3924         if [ ! -e "$TDIR/loop1b" ] ; then
3925                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3926                         mknod $TDIR/loop${i}b b 7 $i
3927                         mknod $TDIR/null${i}c c 1 3
3928                         ln -s $TDIR/file1 $TDIR/link${i}l
3929                 done
3930                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3931                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3932                         mknod $TDIR/dir$i/null${i}c c 1 3
3933                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3934                 done
3935         fi
3936 }
3937
3938 test_56g() {
3939         $SETSTRIPE -d $DIR
3940
3941         TDIR=$DIR/${tdir}g
3942         setup_56 $NUMFILES $NUMDIRS
3943
3944         EXPECTED=$(($NUMDIRS + 2))
3945         # test lfs find with -name
3946         for i in $(seq 1 $NUMFILES) ; do
3947                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3948                 [ $NUMS -eq $EXPECTED ] ||
3949                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3950                               "found $NUMS, expected $EXPECTED"
3951         done
3952 }
3953 run_test 56g "check lfs find -name ============================="
3954
3955 test_56h() {
3956         $SETSTRIPE -d $DIR
3957
3958         TDIR=$DIR/${tdir}g
3959         setup_56 $NUMFILES $NUMDIRS
3960
3961         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3962         # test lfs find with ! -name
3963         for i in $(seq 1 $NUMFILES) ; do
3964                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3965                 [ $NUMS -eq $EXPECTED ] ||
3966                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3967                               "found $NUMS, expected $EXPECTED"
3968         done
3969 }
3970 run_test 56h "check lfs find ! -name ============================="
3971
3972 test_56i() {
3973        tdir=${tdir}i
3974        test_mkdir -p $DIR/$tdir
3975        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3976        CMD="$LFIND -ost $UUID $DIR/$tdir"
3977        OUT=$($CMD)
3978        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3979 }
3980 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3981
3982 test_56j() {
3983         TDIR=$DIR/${tdir}g
3984         setup_56_special $NUMFILES $NUMDIRS
3985
3986         EXPECTED=$((NUMDIRS + 1))
3987         CMD="$LFIND -type d $TDIR"
3988         NUMS=$($CMD | wc -l)
3989         [ $NUMS -eq $EXPECTED ] ||
3990                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3991 }
3992 run_test 56j "check lfs find -type d ============================="
3993
3994 test_56k() {
3995         TDIR=$DIR/${tdir}g
3996         setup_56_special $NUMFILES $NUMDIRS
3997
3998         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3999         CMD="$LFIND -type f $TDIR"
4000         NUMS=$($CMD | wc -l)
4001         [ $NUMS -eq $EXPECTED ] ||
4002                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4003 }
4004 run_test 56k "check lfs find -type f ============================="
4005
4006 test_56l() {
4007         TDIR=$DIR/${tdir}g
4008         setup_56_special $NUMFILES $NUMDIRS
4009
4010         EXPECTED=$((NUMDIRS + NUMFILES))
4011         CMD="$LFIND -type b $TDIR"
4012         NUMS=$($CMD | wc -l)
4013         [ $NUMS -eq $EXPECTED ] ||
4014                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4015 }
4016 run_test 56l "check lfs find -type b ============================="
4017
4018 test_56m() {
4019         TDIR=$DIR/${tdir}g
4020         setup_56_special $NUMFILES $NUMDIRS
4021
4022         EXPECTED=$((NUMDIRS + NUMFILES))
4023         CMD="$LFIND -type c $TDIR"
4024         NUMS=$($CMD | wc -l)
4025         [ $NUMS -eq $EXPECTED ] ||
4026                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4027 }
4028 run_test 56m "check lfs find -type c ============================="
4029
4030 test_56n() {
4031         TDIR=$DIR/${tdir}g
4032         setup_56_special $NUMFILES $NUMDIRS
4033
4034         EXPECTED=$((NUMDIRS + NUMFILES))
4035         CMD="$LFIND -type l $TDIR"
4036         NUMS=$($CMD | wc -l)
4037         [ $NUMS -eq $EXPECTED ] ||
4038                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4039 }
4040 run_test 56n "check lfs find -type l ============================="
4041
4042 test_56o() {
4043         TDIR=$DIR/${tdir}o
4044         setup_56 $NUMFILES $NUMDIRS
4045
4046         utime $TDIR/file1 > /dev/null || error "utime (1)"
4047         utime $TDIR/file2 > /dev/null || error "utime (2)"
4048         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4049         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4050         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4051         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4052
4053         EXPECTED=4
4054         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4055         [ $NUMS -eq $EXPECTED ] || \
4056                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4057
4058         EXPECTED=12
4059         CMD="$LFIND -mtime 0 $TDIR"
4060         NUMS=$($CMD | wc -l)
4061         [ $NUMS -eq $EXPECTED ] ||
4062                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4063 }
4064 run_test 56o "check lfs find -mtime for old files =========================="
4065
4066 test_56p() {
4067         [ $RUNAS_ID -eq $UID ] &&
4068                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4069
4070         TDIR=$DIR/${tdir}p
4071         setup_56 $NUMFILES $NUMDIRS
4072
4073         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4074         EXPECTED=$NUMFILES
4075         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4076         NUMS=$($CMD | wc -l)
4077         [ $NUMS -eq $EXPECTED ] || \
4078                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4079
4080         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4081         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4082         NUMS=$($CMD | wc -l)
4083         [ $NUMS -eq $EXPECTED ] || \
4084                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4085 }
4086 run_test 56p "check lfs find -uid and ! -uid ==============================="
4087
4088 test_56q() {
4089         [ $RUNAS_ID -eq $UID ] &&
4090                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4091
4092         TDIR=$DIR/${tdir}q
4093         setup_56 $NUMFILES $NUMDIRS
4094
4095         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4096
4097         EXPECTED=$NUMFILES
4098         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4099         NUMS=$($CMD | wc -l)
4100         [ $NUMS -eq $EXPECTED ] ||
4101                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4102
4103         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4104         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4105         NUMS=$($CMD | wc -l)
4106         [ $NUMS -eq $EXPECTED ] ||
4107                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4108 }
4109 run_test 56q "check lfs find -gid and ! -gid ==============================="
4110
4111 test_56r() {
4112         TDIR=$DIR/${tdir}r
4113         setup_56 $NUMFILES $NUMDIRS
4114
4115         EXPECTED=12
4116         CMD="$LFIND -size 0 -type f $TDIR"
4117         NUMS=$($CMD | wc -l)
4118         [ $NUMS -eq $EXPECTED ] ||
4119                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4120         EXPECTED=0
4121         CMD="$LFIND ! -size 0 -type f $TDIR"
4122         NUMS=$($CMD | wc -l)
4123         [ $NUMS -eq $EXPECTED ] ||
4124                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4125         echo "test" > $TDIR/$tfile
4126         echo "test2" > $TDIR/$tfile.2 && sync
4127         EXPECTED=1
4128         CMD="$LFIND -size 5 -type f $TDIR"
4129         NUMS=$($CMD | wc -l)
4130         [ $NUMS -eq $EXPECTED ] ||
4131                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4132         EXPECTED=1
4133         CMD="$LFIND -size +5 -type f $TDIR"
4134         NUMS=$($CMD | wc -l)
4135         [ $NUMS -eq $EXPECTED ] ||
4136                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4137         EXPECTED=2
4138         CMD="$LFIND -size +0 -type f $TDIR"
4139         NUMS=$($CMD | wc -l)
4140         [ $NUMS -eq $EXPECTED ] ||
4141                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4142         EXPECTED=2
4143         CMD="$LFIND ! -size -5 -type f $TDIR"
4144         NUMS=$($CMD | wc -l)
4145         [ $NUMS -eq $EXPECTED ] ||
4146                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4147         EXPECTED=12
4148         CMD="$LFIND -size -5 -type f $TDIR"
4149         NUMS=$($CMD | wc -l)
4150         [ $NUMS -eq $EXPECTED ] ||
4151                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4152 }
4153 run_test 56r "check lfs find -size works =========================="
4154
4155 test_56s() { # LU-611
4156         TDIR=$DIR/${tdir}s
4157         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4158
4159         if [ $OSTCOUNT -gt 1 ]; then
4160                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4161                 ONESTRIPE=4
4162                 EXTRA=4
4163         else
4164                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4165                 EXTRA=0
4166         fi
4167
4168         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4169         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4170         NUMS=$($CMD | wc -l)
4171         [ $NUMS -eq $EXPECTED ] ||
4172                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4173
4174         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4175         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4176         NUMS=$($CMD | wc -l)
4177         [ $NUMS -eq $EXPECTED ] ||
4178                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4179
4180         EXPECTED=$ONESTRIPE
4181         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4182         NUMS=$($CMD | wc -l)
4183         [ $NUMS -eq $EXPECTED ] ||
4184                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4185
4186         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4187         NUMS=$($CMD | wc -l)
4188         [ $NUMS -eq $EXPECTED ] ||
4189                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4190
4191         EXPECTED=0
4192         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4193         NUMS=$($CMD | wc -l)
4194         [ $NUMS -eq $EXPECTED ] ||
4195                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4196 }
4197 run_test 56s "check lfs find -stripe-count works"
4198
4199 test_56t() { # LU-611
4200         TDIR=$DIR/${tdir}t
4201         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4202
4203         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4204
4205         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4206         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4207         NUMS=$($CMD | wc -l)
4208         [ $NUMS -eq $EXPECTED ] ||
4209                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4210
4211         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4212         NUMS=$($CMD | wc -l)
4213         [ $NUMS -eq $EXPECTED ] ||
4214                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4215
4216         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4217         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4218         NUMS=$($CMD | wc -l)
4219         [ $NUMS -eq $EXPECTED ] ||
4220                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4221
4222         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4223         NUMS=$($CMD | wc -l)
4224         [ $NUMS -eq $EXPECTED ] ||
4225                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4226
4227         EXPECTED=4
4228         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4229         NUMS=$($CMD | wc -l)
4230         [ $NUMS -eq $EXPECTED ] ||
4231                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4232
4233         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4234         NUMS=$($CMD | wc -l)
4235         [ $NUMS -eq $EXPECTED ] ||
4236                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4237
4238         EXPECTED=0
4239         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4240         NUMS=$($CMD | wc -l)
4241         [ $NUMS -eq $EXPECTED ] ||
4242                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4243 }
4244 run_test 56t "check lfs find -stripe-size works"
4245
4246 test_56u() { # LU-611
4247         TDIR=$DIR/${tdir}u
4248         setup_56 $NUMFILES $NUMDIRS "-i 0"
4249
4250         if [ $OSTCOUNT -gt 1 ]; then
4251                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4252                 ONESTRIPE=4
4253         else
4254                 ONESTRIPE=0
4255         fi
4256
4257         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4258         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4259         NUMS=$($CMD | wc -l)
4260         [ $NUMS -eq $EXPECTED ] ||
4261                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4262
4263         EXPECTED=$ONESTRIPE
4264         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4265         NUMS=$($CMD | wc -l)
4266         [ $NUMS -eq $EXPECTED ] ||
4267                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4268
4269         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4270         NUMS=$($CMD | wc -l)
4271         [ $NUMS -eq $EXPECTED ] ||
4272                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4273
4274         EXPECTED=0
4275         # This should produce an error and not return any files
4276         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4277         NUMS=$($CMD 2>/dev/null | wc -l)
4278         [ $NUMS -eq $EXPECTED ] ||
4279                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4280
4281         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4282         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4283         NUMS=$($CMD | wc -l)
4284         [ $NUMS -eq $EXPECTED ] ||
4285                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4286 }
4287 run_test 56u "check lfs find -stripe-index works"
4288
4289 test_56v() {
4290     local MDT_IDX=0
4291
4292     TDIR=$DIR/${tdir}v
4293     rm -rf $TDIR
4294     setup_56 $NUMFILES $NUMDIRS
4295
4296     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4297     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4298
4299     for file in $($LFIND -mdt $UUID $TDIR); do
4300         file_mdt_idx=$($GETSTRIPE -M $file)
4301         [ $file_mdt_idx -eq $MDT_IDX ] ||
4302             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4303     done
4304 }
4305 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4306
4307 # Get and check the actual stripe count of one file.
4308 # Usage: check_stripe_count <file> <expected_stripe_count>
4309 check_stripe_count() {
4310     local file=$1
4311     local expected=$2
4312     local actual
4313
4314     [[ -z "$file" || -z "$expected" ]] &&
4315         error "check_stripe_count: invalid argument!"
4316
4317     local cmd="$GETSTRIPE -c $file"
4318     actual=$($cmd) || error "$cmd failed"
4319     actual=${actual%% *}
4320
4321     if [[ $actual -ne $expected ]]; then
4322         [[ $expected -eq -1 ]] ||
4323             error "$cmd wrong: found $actual, expected $expected"
4324         [[ $actual -eq $OSTCOUNT ]] ||
4325             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4326     fi
4327 }
4328
4329 test_56w() {
4330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4331         TDIR=$DIR/${tdir}w
4332
4333     rm -rf $TDIR || error "remove $TDIR failed"
4334     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4335
4336     local stripe_size
4337     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4338         error "$GETSTRIPE -S -d $TDIR failed"
4339     stripe_size=${stripe_size%% *}
4340
4341     local file_size=$((stripe_size * OSTCOUNT))
4342     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4343     local required_space=$((file_num * file_size))
4344     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4345     [[ $free_space -le $((required_space / 1024)) ]] &&
4346         skip_env "need at least $required_space bytes free space," \
4347                  "have $free_space kbytes" && return
4348
4349     local dd_bs=65536
4350     local dd_count=$((file_size / dd_bs))
4351
4352     # write data into the files
4353     local i
4354     local j
4355     local file
4356     for i in $(seq 1 $NUMFILES); do
4357         file=$TDIR/file$i
4358         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4359             error "write data into $file failed"
4360     done
4361     for i in $(seq 1 $NUMDIRS); do
4362         for j in $(seq 1 $NUMFILES); do
4363             file=$TDIR/dir$i/file$j
4364             yes | dd bs=$dd_bs count=$dd_count of=$file \
4365                 >/dev/null 2>&1 ||
4366                 error "write data into $file failed"
4367         done
4368     done
4369
4370     local expected=-1
4371     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4372
4373     # lfs_migrate file
4374     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4375     echo "$cmd"
4376     eval $cmd || error "$cmd failed"
4377
4378     check_stripe_count $TDIR/file1 $expected
4379
4380     # lfs_migrate dir
4381     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4382     echo "$cmd"
4383     eval $cmd || error "$cmd failed"
4384
4385     for j in $(seq 1 $NUMFILES); do
4386         check_stripe_count $TDIR/dir1/file$j $expected
4387     done
4388
4389     # lfs_migrate works with lfs find
4390     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4391          $LFS_MIGRATE -y -c $expected"
4392     echo "$cmd"
4393     eval $cmd || error "$cmd failed"
4394
4395     for i in $(seq 2 $NUMFILES); do
4396         check_stripe_count $TDIR/file$i $expected
4397     done
4398     for i in $(seq 2 $NUMDIRS); do
4399         for j in $(seq 1 $NUMFILES); do
4400             check_stripe_count $TDIR/dir$i/file$j $expected
4401         done
4402     done
4403 }
4404 run_test 56w "check lfs_migrate -c stripe_count works"
4405
4406 test_57a() {
4407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4408         # note test will not do anything if MDS is not local
4409         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4410                 skip "Only applicable to ldiskfs-based MDTs"
4411                 return
4412         fi
4413
4414         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4415         local MNTDEV="osd*.*MDT*.mntdev"
4416         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4417         [ -z "$DEV" ] && error "can't access $MNTDEV"
4418         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4419                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4420                         error "can't access $DEV"
4421                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4422                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4423                 rm $TMP/t57a.dump
4424         done
4425 }
4426 run_test 57a "verify MDS filesystem created with large inodes =="
4427
4428 test_57b() {
4429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4430         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4431                 skip "Only applicable to ldiskfs-based MDTs"
4432                 return
4433         fi
4434
4435         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4436         local dir=$DIR/d57b
4437
4438         local FILECOUNT=100
4439         local FILE1=$dir/f1
4440         local FILEN=$dir/f$FILECOUNT
4441
4442         rm -rf $dir || error "removing $dir"
4443         test_mkdir -p $dir || error "creating $dir"
4444         local num=$(get_mds_dir $dir)
4445         local mymds=mds$num
4446
4447         echo "mcreating $FILECOUNT files"
4448         createmany -m $dir/f 1 $FILECOUNT || \
4449                 error "creating files in $dir"
4450
4451         # verify that files do not have EAs yet
4452         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4453         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4454
4455         sync
4456         sleep 1
4457         df $dir  #make sure we get new statfs data
4458         local MDSFREE=$(do_facet $mymds \
4459                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4460         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4461         echo "opening files to create objects/EAs"
4462         local FILE
4463         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4464                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4465         done
4466
4467         # verify that files have EAs now
4468         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4469         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4470
4471         sleep 1  #make sure we get new statfs data
4472         df $dir
4473         local MDSFREE2=$(do_facet $mymds \
4474                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4475         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4476         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4477                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4478                         error "MDC before $MDCFREE != after $MDCFREE2"
4479                 else
4480                         echo "MDC before $MDCFREE != after $MDCFREE2"
4481                         echo "unable to confirm if MDS has large inodes"
4482                 fi
4483         fi
4484         rm -rf $dir
4485 }
4486 run_test 57b "default LOV EAs are stored inside large inodes ==="
4487
4488 test_58() {
4489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4490         [ -z "$(which wiretest 2>/dev/null)" ] &&
4491                         skip_env "could not find wiretest" && return
4492         wiretest
4493 }
4494 run_test 58 "verify cross-platform wire constants =============="
4495
4496 test_59() {
4497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4498         echo "touch 130 files"
4499         createmany -o $DIR/f59- 130
4500         echo "rm 130 files"
4501         unlinkmany $DIR/f59- 130
4502         sync
4503         # wait for commitment of removal
4504         wait_delete_completed
4505 }
4506 run_test 59 "verify cancellation of llog records async ========="
4507
4508 TEST60_HEAD="test_60 run $RANDOM"
4509 test_60a() {
4510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4511         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4512         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4513         log "$TEST60_HEAD - from kernel mode"
4514         do_facet mgs sh run-llog.sh
4515 }
4516 run_test 60a "llog sanity tests run from kernel module =========="
4517
4518 test_60b() { # bug 6411
4519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4520         dmesg > $DIR/$tfile
4521         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4522                                  /llog.test/ {
4523                                          if (marker)
4524                                                  from_marker++
4525                                          from_begin++
4526                                  }
4527                                  END {
4528                                          if (marker)
4529                                                  print from_marker
4530                                          else
4531                                                  print from_begin
4532                                  }"`
4533         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4534 }
4535 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4536
4537 test_60c() {
4538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4539         echo "create 5000 files"
4540         createmany -o $DIR/f60c- 5000
4541 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4542         lctl set_param fail_loc=0x80000137
4543         unlinkmany $DIR/f60c- 5000
4544         lctl set_param fail_loc=0
4545 }
4546 run_test 60c "unlink file when mds full"
4547
4548 test_60d() {
4549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4550         SAVEPRINTK=$(lctl get_param -n printk)
4551
4552         # verify "lctl mark" is even working"
4553         MESSAGE="test message ID $RANDOM $$"
4554         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4555         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4556
4557         lctl set_param printk=0 || error "set lnet.printk failed"
4558         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4559         MESSAGE="new test message ID $RANDOM $$"
4560         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4561         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4562         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4563
4564         lctl set_param -n printk="$SAVEPRINTK"
4565 }
4566 run_test 60d "test printk console message masking"
4567
4568 test_61() {
4569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4570         f="$DIR/f61"
4571         dd if=/dev/zero of=$f bs=`page_size` count=1
4572         cancel_lru_locks osc
4573         $MULTIOP $f OSMWUc || error
4574         sync
4575 }
4576 run_test 61 "mmap() writes don't make sync hang ================"
4577
4578 # bug 2330 - insufficient obd_match error checking causes LBUG
4579 test_62() {
4580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4581         f="$DIR/f62"
4582         echo foo > $f
4583         cancel_lru_locks osc
4584         lctl set_param fail_loc=0x405
4585         cat $f && error "cat succeeded, expect -EIO"
4586         lctl set_param fail_loc=0
4587 }
4588 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4589 # match every page all of the time.
4590 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4591
4592 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4593 test_63a() {    # was test_63
4594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4595         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4596         lctl set_param -n osc.*.max_dirty_mb 0
4597         for i in `seq 10` ; do
4598                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4599                 sleep 5
4600                 kill $!
4601                 sleep 1
4602         done
4603
4604         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4605         rm -f $DIR/f63 || true
4606 }
4607 run_test 63a "Verify oig_wait interruption does not crash ======="
4608
4609 # bug 2248 - async write errors didn't return to application on sync
4610 # bug 3677 - async write errors left page locked
4611 test_63b() {
4612         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4613         debugsave
4614         lctl set_param debug=-1
4615
4616         # ensure we have a grant to do async writes
4617         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4618         rm $DIR/$tfile
4619
4620         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4621         lctl set_param fail_loc=0x80000406
4622         $MULTIOP $DIR/$tfile Owy && \
4623                 error "sync didn't return ENOMEM"
4624         sync; sleep 2; sync     # do a real sync this time to flush page
4625         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4626                 error "locked page left in cache after async error" || true
4627         debugrestore
4628 }
4629 run_test 63b "async write errors should be returned to fsync ==="
4630
4631 test_64a () {
4632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4633         df $DIR
4634         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4635 }
4636 run_test 64a "verify filter grant calculations (in kernel) ====="
4637
4638 test_64b () {
4639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4640         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4641         sh oos.sh $MOUNT
4642 }
4643 run_test 64b "check out-of-space detection on client ==========="
4644
4645 test_64c() {
4646         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4647 }
4648 run_test 64c "verify grant shrink ========================------"
4649
4650 # bug 1414 - set/get directories' stripe info
4651 test_65a() {
4652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4653         test_mkdir -p $DIR/$tdir
4654         touch $DIR/$tdir/f1
4655         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4656 }
4657 run_test 65a "directory with no stripe info ===================="
4658
4659 test_65b() {
4660         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4661         test_mkdir -p $DIR/$tdir
4662         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4663                                                 error "setstripe"
4664         touch $DIR/$tdir/f2
4665         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4666 }
4667 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4668
4669 test_65c() {
4670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4671         if [ $OSTCOUNT -gt 1 ]; then
4672                 test_mkdir -p $DIR/$tdir
4673                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4674                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4675                 touch $DIR/$tdir/f3
4676                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4677         fi
4678 }
4679 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4680
4681 test_65d() {
4682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4683         test_mkdir -p $DIR/$tdir
4684         if [ $STRIPECOUNT -le 0 ]; then
4685                 sc=1
4686         elif [ $STRIPECOUNT -gt 2000 ]; then
4687 #LOV_MAX_STRIPE_COUNT is 2000
4688                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4689         else
4690                 sc=$(($STRIPECOUNT - 1))
4691         fi
4692         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4693         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4694         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4695                                                 error "lverify failed"
4696 }
4697 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4698
4699 test_65e() {
4700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4701         test_mkdir -p $DIR/$tdir
4702
4703         $SETSTRIPE $DIR/$tdir || error "setstripe"
4704         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4705                                         error "no stripe info failed"
4706         touch $DIR/$tdir/f6
4707         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4708 }
4709 run_test 65e "directory setstripe defaults ======================="
4710
4711 test_65f() {
4712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4713         test_mkdir -p $DIR/${tdir}f
4714         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4715 }
4716 run_test 65f "dir setstripe permission (should return error) ==="
4717
4718 test_65g() {
4719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4720         test_mkdir -p $DIR/$tdir
4721         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4722                                                         error "setstripe"
4723         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4724         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4725                 error "delete default stripe failed"
4726 }
4727 run_test 65g "directory setstripe -d ==========================="
4728
4729 test_65h() {
4730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4731         test_mkdir -p $DIR/$tdir
4732         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4733                                                         error "setstripe"
4734         test_mkdir -p $DIR/$tdir/dd1
4735         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4736                 error "stripe info inherit failed"
4737 }
4738 run_test 65h "directory stripe info inherit ===================="
4739
4740 test_65i() { # bug6367
4741         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4742         $SETSTRIPE -S 65536 -c -1 $MOUNT
4743 }
4744 run_test 65i "set non-default striping on root directory (bug 6367)="
4745
4746 test_65ia() { # bug12836
4747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4748         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4749 }
4750 run_test 65ia "getstripe on -1 default directory striping"
4751
4752 test_65ib() { # bug12836
4753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4754         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4755 }
4756 run_test 65ib "getstripe -v on -1 default directory striping"
4757
4758 test_65ic() { # bug12836
4759         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4760         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4761 }
4762 run_test 65ic "new find on -1 default directory striping"
4763
4764 test_65j() { # bug6367
4765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4766         sync; sleep 1
4767         # if we aren't already remounting for each test, do so for this test
4768         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4769                 cleanup || error "failed to unmount"
4770                 setup
4771         fi
4772         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4773 }
4774 run_test 65j "set default striping on root directory (bug 6367)="
4775
4776 test_65k() { # bug11679
4777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4778         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4779         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4780
4781     echo "Check OST status: "
4782     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4783               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4784
4785     for OSC in $MDS_OSCS; do
4786         echo $OSC "is activate"
4787         do_facet $SINGLEMDS lctl --device %$OSC activate
4788     done
4789
4790     mkdir -p $DIR/$tdir
4791     for INACTIVE_OSC in $MDS_OSCS; do
4792         echo "Deactivate: " $INACTIVE_OSC
4793         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4794         for STRIPE_OSC in $MDS_OSCS; do
4795             OST=`osc_to_ost $STRIPE_OSC`
4796             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4797                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4798
4799             [ -f $DIR/$tdir/$IDX ] && continue
4800             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4801             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4802             RC=$?
4803             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4804         done
4805         rm -f $DIR/$tdir/*
4806         echo $INACTIVE_OSC "is Activate."
4807         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4808     done
4809 }
4810 run_test 65k "validate manual striping works properly with deactivated OSCs"
4811
4812 test_65l() { # bug 12836
4813         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4814         test_mkdir -p $DIR/$tdir/test_dir
4815         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4816         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4817 }
4818 run_test 65l "lfs find on -1 stripe dir ========================"
4819
4820 # bug 2543 - update blocks count on client
4821 test_66() {
4822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4823         COUNT=${COUNT:-8}
4824         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4825         sync; sync_all_data; sync; sync_all_data
4826         cancel_lru_locks osc
4827         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4828         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4829 }
4830 run_test 66 "update inode blocks count on client ==============="
4831
4832 LLOOP=
4833 LLITELOOPLOAD=
4834 cleanup_68() {
4835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4836         trap 0
4837         if [ ! -z "$LLOOP" ]; then
4838                 if swapon -s | grep -q $LLOOP; then
4839                         swapoff $LLOOP || error "swapoff failed"
4840                 fi
4841
4842                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4843                 rm -f $LLOOP
4844                 unset LLOOP
4845         fi
4846         if [ ! -z "$LLITELOOPLOAD" ]; then
4847                 rmmod llite_lloop
4848                 unset LLITELOOPLOAD
4849         fi
4850         rm -f $DIR/f68*
4851 }
4852
4853 meminfo() {
4854         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4855 }
4856
4857 swap_used() {
4858         swapon -s | awk '($1 == "'$1'") { print $4 }'
4859 }
4860
4861 # test case for lloop driver, basic function
4862 test_68a() {
4863         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4864         [ "$UID" != 0 ] && skip_env "must run as root" && return
4865         llite_lloop_enabled || \
4866                 { skip_env "llite_lloop module disabled" && return; }
4867
4868         trap cleanup_68 EXIT
4869
4870         if ! module_loaded llite_lloop; then
4871                 if load_module llite/llite_lloop; then
4872                         LLITELOOPLOAD=yes
4873                 else
4874                         skip_env "can't find module llite_lloop"
4875                         return
4876                 fi
4877         fi
4878
4879         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4880         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4881         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4882
4883         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4884         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4885
4886         cleanup_68
4887 }
4888 run_test 68a "lloop driver - basic test ========================"
4889
4890 # excercise swapping to lustre by adding a high priority swapfile entry
4891 # and then consuming memory until it is used.
4892 test_68b() {  # was test_68
4893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4894         [ "$UID" != 0 ] && skip_env "must run as root" && return
4895         lctl get_param -n devices | grep -q obdfilter && \
4896                 skip "local OST" && return
4897
4898         grep -q llite_lloop /proc/modules
4899         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4900
4901         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4902                 skip "can't reliably test swap with TCP" && return
4903
4904         MEMTOTAL=`meminfo MemTotal`
4905         NR_BLOCKS=$((MEMTOTAL>>8))
4906         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4907
4908         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4909         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4910         mkswap $DIR/f68b
4911
4912         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4913
4914         trap cleanup_68 EXIT
4915
4916         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4917
4918         echo "before: `swapon -s | grep $LLOOP`"
4919         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4920         echo "after: `swapon -s | grep $LLOOP`"
4921         SWAPUSED=`swap_used $LLOOP`
4922
4923         cleanup_68
4924
4925         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4926 }
4927 run_test 68b "support swapping to Lustre ========================"
4928
4929 # bug5265, obdfilter oa2dentry return -ENOENT
4930 # #define OBD_FAIL_OST_ENOENT 0x217
4931 test_69() {
4932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4933         remote_ost_nodsh && skip "remote OST with nodsh" && return
4934
4935         f="$DIR/$tfile"
4936         $SETSTRIPE -c 1 -i 0 $f
4937
4938         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4939
4940         do_facet ost1 lctl set_param fail_loc=0x217
4941         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4942         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4943
4944         do_facet ost1 lctl set_param fail_loc=0
4945         $DIRECTIO write $f 0 2 || error "write error"
4946
4947         cancel_lru_locks osc
4948         $DIRECTIO read $f 0 1 || error "read error"
4949
4950         do_facet ost1 lctl set_param fail_loc=0x217
4951         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4952
4953         do_facet ost1 lctl set_param fail_loc=0
4954         rm -f $f
4955 }
4956 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4957
4958 test_71() {
4959     test_mkdir -p $DIR/$tdir
4960     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4961 }
4962 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4963
4964 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4966         check_kernel_version 43 || return 0
4967         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4968
4969         # Check that testing environment is properly set up. Skip if not
4970         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4971                 skip_env "User $RUNAS_ID does not exist - skipping"
4972                 return 0
4973         }
4974         # We had better clear the $DIR to get enough space for dd
4975         rm -rf $DIR/*
4976         touch $DIR/f72
4977         chmod 777 $DIR/f72
4978         chmod ug+s $DIR/f72
4979         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4980         # See if we are still setuid/sgid
4981         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4982         # Now test that MDS is updated too
4983         cancel_lru_locks mdc
4984         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4985         rm -f $DIR/f72
4986 }
4987 run_test 72a "Test that remove suid works properly (bug5695) ===="
4988
4989 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4990         local perm
4991
4992         [ "$RUNAS_ID" = "$UID" ] && \
4993                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4994         [ "$RUNAS_ID" -eq 0 ] && \
4995                 skip_env "RUNAS_ID = 0 -- skipping" && return
4996
4997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4998         # Check that testing environment is properly set up. Skip if not
4999         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5000                 skip_env "User $RUNAS_ID does not exist - skipping"
5001                 return 0
5002         }
5003         touch $DIR/${tfile}-f{g,u}
5004         test_mkdir $DIR/${tfile}-dg
5005         test_mkdir $DIR/${tfile}-du
5006         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5007         chmod g+s $DIR/${tfile}-{f,d}g
5008         chmod u+s $DIR/${tfile}-{f,d}u
5009         for perm in 777 2777 4777; do
5010                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5011                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5012                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5013                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5014         done
5015         true
5016 }
5017 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5018
5019 # bug 3462 - multiple simultaneous MDC requests
5020 test_73() {
5021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5022         test_mkdir $DIR/d73-1
5023         test_mkdir $DIR/d73-2
5024         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5025         pid1=$!
5026
5027         lctl set_param fail_loc=0x80000129
5028         $MULTIOP $DIR/d73-1/f73-2 Oc &
5029         sleep 1
5030         lctl set_param fail_loc=0
5031
5032         $MULTIOP $DIR/d73-2/f73-3 Oc &
5033         pid3=$!
5034
5035         kill -USR1 $pid1
5036         wait $pid1 || return 1
5037
5038         sleep 25
5039
5040         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5041         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5042         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5043
5044         rm -rf $DIR/d73-*
5045 }
5046 run_test 73 "multiple MDC requests (should not deadlock)"
5047
5048 test_74a() { # bug 6149, 6184
5049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5050         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5051         #
5052         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5053         # will spin in a tight reconnection loop
5054         touch $DIR/f74a
5055         lctl set_param fail_loc=0x8000030e
5056         # get any lock that won't be difficult - lookup works.
5057         ls $DIR/f74a
5058         lctl set_param fail_loc=0
5059         true
5060         rm -f $DIR/f74a
5061 }
5062 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5063
5064 test_74b() { # bug 13310
5065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5066         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5067         #
5068         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5069         # will spin in a tight reconnection loop
5070         lctl set_param fail_loc=0x8000030e
5071         # get a "difficult" lock
5072         touch $DIR/f74b
5073         lctl set_param fail_loc=0
5074         true
5075         rm -f $DIR/f74b
5076 }
5077 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5078
5079 test_74c() {
5080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5081 #define OBD_FAIL_LDLM_NEW_LOCK
5082         lctl set_param fail_loc=0x80000319
5083         touch $DIR/$tfile && error "Touch successful"
5084         true
5085 }
5086 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5087
5088 num_inodes() {
5089         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5090 }
5091
5092 get_inode_slab_tunables() {
5093         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5094 }
5095
5096 set_inode_slab_tunables() {
5097         echo "lustre_inode_cache $1" > /proc/slabinfo
5098 }
5099
5100 test_76() { # Now for bug 20433, added originally in bug 1443
5101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5102         local SLAB_SETTINGS=`get_inode_slab_tunables`
5103         local CPUS=`getconf _NPROCESSORS_ONLN`
5104         # we cannot set limit below 1 which means 1 inode in each
5105         # per-cpu cache is still allowed
5106         set_inode_slab_tunables "1 1 0"
5107         cancel_lru_locks osc
5108         BEFORE_INODES=`num_inodes`
5109         echo "before inodes: $BEFORE_INODES"
5110         local COUNT=1000
5111         [ "$SLOW" = "no" ] && COUNT=100
5112         for i in `seq $COUNT`; do
5113                 touch $DIR/$tfile
5114                 rm -f $DIR/$tfile
5115         done
5116         cancel_lru_locks osc
5117         AFTER_INODES=`num_inodes`
5118         echo "after inodes: $AFTER_INODES"
5119         local wait=0
5120         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5121                 sleep 2
5122                 AFTER_INODES=`num_inodes`
5123                 wait=$((wait+2))
5124                 echo "wait $wait seconds inodes: $AFTER_INODES"
5125                 if [ $wait -gt 30 ]; then
5126                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5127                 fi
5128         done
5129         set_inode_slab_tunables "$SLAB_SETTINGS"
5130 }
5131 run_test 76 "confirm clients recycle inodes properly ===="
5132
5133
5134 export ORIG_CSUM=""
5135 set_checksums()
5136 {
5137         # Note: in sptlrpc modes which enable its own bulk checksum, the
5138         # original crc32_le bulk checksum will be automatically disabled,
5139         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5140         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5141         # In this case set_checksums() will not be no-op, because sptlrpc
5142         # bulk checksum will be enabled all through the test.
5143
5144         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5145         lctl set_param -n osc.*.checksums $1
5146         return 0
5147 }
5148
5149 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5150                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5151 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5152 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5153 set_checksum_type()
5154 {
5155         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5156         log "set checksum type to $1"
5157         return 0
5158 }
5159 F77_TMP=$TMP/f77-temp
5160 F77SZ=8
5161 setup_f77() {
5162         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5163                 error "error writing to $F77_TMP"
5164 }
5165
5166 test_77a() { # bug 10889
5167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5168         $GSS && skip "could not run with gss" && return
5169         [ ! -f $F77_TMP ] && setup_f77
5170         set_checksums 1
5171         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5172         set_checksums 0
5173         rm -f $DIR/$tfile
5174 }
5175 run_test 77a "normal checksum read/write operation ============="
5176
5177 test_77b() { # bug 10889
5178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5179         $GSS && skip "could not run with gss" && return
5180         [ ! -f $F77_TMP ] && setup_f77
5181         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5182         lctl set_param fail_loc=0x80000409
5183         set_checksums 1
5184         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
5185                 error "dd error: $?"
5186         lctl set_param fail_loc=0
5187         set_checksums 0
5188 }
5189 run_test 77b "checksum error on client write ===================="
5190
5191 test_77c() { # bug 10889
5192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5193         $GSS && skip "could not run with gss" && return
5194         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
5195         set_checksums 1
5196         for algo in $CKSUM_TYPES; do
5197                 cancel_lru_locks osc
5198                 set_checksum_type $algo
5199                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5200                 lctl set_param fail_loc=0x80000408
5201                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
5202                 lctl set_param fail_loc=0
5203         done
5204         set_checksums 0
5205         set_checksum_type $ORIG_CSUM_TYPE
5206         rm -f $DIR/f77b
5207 }
5208 run_test 77c "checksum error on client read ==================="
5209
5210 test_77d() { # bug 10889
5211         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5212         $GSS && skip "could not run with gss" && return
5213         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5214         lctl set_param fail_loc=0x80000409
5215         set_checksums 1
5216         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5217                 error "direct write: rc=$?"
5218         lctl set_param fail_loc=0
5219         set_checksums 0
5220 }
5221 run_test 77d "checksum error on OST direct write ==============="
5222
5223 test_77e() { # bug 10889
5224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5225         $GSS && skip "could not run with gss" && return
5226         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5227         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5228         lctl set_param fail_loc=0x80000408
5229         set_checksums 1
5230         cancel_lru_locks osc
5231         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5232                 error "direct read: rc=$?"
5233         lctl set_param fail_loc=0
5234         set_checksums 0
5235 }
5236 run_test 77e "checksum error on OST direct read ================"
5237
5238 test_77f() { # bug 10889
5239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5240         $GSS && skip "could not run with gss" && return
5241         set_checksums 1
5242         for algo in $CKSUM_TYPES; do
5243                 cancel_lru_locks osc
5244                 set_checksum_type $algo
5245                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5246                 lctl set_param fail_loc=0x409
5247                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5248                         error "direct write succeeded"
5249                 lctl set_param fail_loc=0
5250         done
5251         set_checksum_type $ORIG_CSUM_TYPE
5252         set_checksums 0
5253 }
5254 run_test 77f "repeat checksum error on write (expect error) ===="
5255
5256 test_77g() { # bug 10889
5257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5258         $GSS && skip "could not run with gss" && return
5259         remote_ost_nodsh && skip "remote OST with nodsh" && return
5260
5261         [ ! -f $F77_TMP ] && setup_f77
5262
5263         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5264         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5265         do_facet ost1 lctl set_param fail_loc=0x8000021a
5266         set_checksums 1
5267         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5268                 error "write error: rc=$?"
5269         do_facet ost1 lctl set_param fail_loc=0
5270         set_checksums 0
5271 }
5272 run_test 77g "checksum error on OST write ======================"
5273
5274 test_77h() { # bug 10889
5275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5276         $GSS && skip "could not run with gss" && return
5277         remote_ost_nodsh && skip "remote OST with nodsh" && return
5278
5279         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5280         cancel_lru_locks osc
5281         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5282         do_facet ost1 lctl set_param fail_loc=0x8000021b
5283         set_checksums 1
5284         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5285         do_facet ost1 lctl set_param fail_loc=0
5286         set_checksums 0
5287 }
5288 run_test 77h "checksum error on OST read ======================="
5289
5290 test_77i() { # bug 13805
5291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5292         $GSS && skip "could not run with gss" && return
5293         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5294         lctl set_param fail_loc=0x40b
5295         remount_client $MOUNT
5296         lctl set_param fail_loc=0
5297         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5298                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5299                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5300                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5301         done
5302         remount_client $MOUNT
5303 }
5304 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5305
5306 test_77j() { # bug 13805
5307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5308         $GSS && skip "could not run with gss" && return
5309         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5310         lctl set_param fail_loc=0x40c
5311         remount_client $MOUNT
5312         lctl set_param fail_loc=0
5313         sleep 2 # wait async osc connect to finish
5314         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5315                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5316                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5317                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5318         done
5319         remount_client $MOUNT
5320 }
5321 run_test 77j "client only supporting ADLER32 ===================="
5322
5323 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5324 rm -f $F77_TMP
5325 unset F77_TMP
5326
5327 test_78() { # bug 10901
5328         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5329         remote_ost || { skip_env "local OST" && return; }
5330
5331         NSEQ=5
5332         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5333         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5334         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5335         echo "MemTotal: $MEMTOTAL"
5336 # reserve 256MB of memory for the kernel and other running processes,
5337 # and then take 1/2 of the remaining memory for the read/write buffers.
5338     if [ $MEMTOTAL -gt 512 ] ;then
5339         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5340     else
5341         # for those poor memory-starved high-end clusters...
5342         MEMTOTAL=$((MEMTOTAL / 2))
5343     fi
5344         echo "Mem to use for directio: $MEMTOTAL"
5345         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5346         [ $F78SIZE -gt 512 ] && F78SIZE=512
5347         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5348         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5349         echo "Smallest OST: $SMALLESTOST"
5350         [ $SMALLESTOST -lt 10240 ] && \
5351                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5352
5353         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5354                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5355
5356         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5357         echo "File size: $F78SIZE"
5358         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5359         for i in `seq 1 $NSEQ`
5360         do
5361                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5362                 echo directIO rdwr round $i of $NSEQ
5363                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5364         done
5365
5366         rm -f $DIR/$tfile
5367 }
5368 run_test 78 "handle large O_DIRECT writes correctly ============"
5369
5370 test_79() { # bug 12743
5371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5372         wait_delete_completed
5373
5374         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5375         BKFREE=$(calc_osc_kbytes kbytesfree)
5376         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5377
5378         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5379         DFTOTAL=`echo $STRING | cut -d, -f1`
5380         DFUSED=`echo $STRING  | cut -d, -f2`
5381         DFAVAIL=`echo $STRING | cut -d, -f3`
5382         DFFREE=$(($DFTOTAL - $DFUSED))
5383
5384         ALLOWANCE=$((64 * $OSTCOUNT))
5385
5386         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5387            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5388                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5389         fi
5390         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5391            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5392                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5393         fi
5394         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5395            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5396                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5397         fi
5398 }
5399 run_test 79 "df report consistency check ======================="
5400
5401 test_80() { # bug 10718
5402         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5403         # relax strong synchronous semantics for slow backends like ZFS
5404         local soc="obdfilter.*.sync_on_lock_cancel"
5405         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5406         local hosts=
5407         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5408                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5409                           facet_active_host $host; done | sort -u)
5410                 do_nodes $hosts lctl set_param $soc=never
5411         fi
5412
5413         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5414         sync; sleep 1; sync
5415         local BEFORE=`date +%s`
5416         cancel_lru_locks osc
5417         local AFTER=`date +%s`
5418         local DIFF=$((AFTER-BEFORE))
5419         if [ $DIFF -gt 1 ] ; then
5420                 error "elapsed for 1M@1T = $DIFF"
5421         fi
5422
5423         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5424
5425         rm -f $DIR/$tfile
5426 }
5427 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5428
5429 test_81a() { # LU-456
5430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5431         remote_ost_nodsh && skip "remote OST with nodsh" && return
5432         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5433         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5434         do_facet ost1 lctl set_param fail_loc=0x80000228
5435
5436         # write should trigger a retry and success
5437         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5438         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5439         RC=$?
5440         if [ $RC -ne 0 ] ; then
5441                 error "write should success, but failed for $RC"
5442         fi
5443 }
5444 run_test 81a "OST should retry write when get -ENOSPC ==============="
5445
5446 test_81b() { # LU-456
5447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5448         remote_ost_nodsh && skip "remote OST with nodsh" && return
5449         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5450         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5451         do_facet ost1 lctl set_param fail_loc=0x228
5452
5453         # write should retry several times and return -ENOSPC finally
5454         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5455         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5456         RC=$?
5457         ENOSPC=28
5458         if [ $RC -ne $ENOSPC ] ; then
5459                 error "dd should fail for -ENOSPC, but succeed."
5460         fi
5461 }
5462 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5463
5464 test_82() { # LU-1031
5465         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5466         local gid1=14091995
5467         local gid2=16022000
5468
5469         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5470         local MULTIPID1=$!
5471         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5472         local MULTIPID2=$!
5473         kill -USR1 $MULTIPID2
5474         sleep 2
5475         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5476                 error "First grouplock does not block second one"
5477         else
5478                 echo "Second grouplock blocks first one"
5479         fi
5480         kill -USR1 $MULTIPID1
5481         wait $MULTIPID1
5482         wait $MULTIPID2
5483 }
5484 run_test 82 "Basic grouplock test ==============================="
5485
5486 test_99a() {
5487         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5488             return
5489         test_mkdir -p $DIR/d99cvsroot
5490         chown $RUNAS_ID $DIR/d99cvsroot
5491         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5492         cd $TMP
5493
5494         $RUNAS cvs -d $DIR/d99cvsroot init || error
5495         cd $oldPWD
5496 }
5497 run_test 99a "cvs init ========================================="
5498
5499 test_99b() {
5500         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5501         [ ! -d $DIR/d99cvsroot ] && test_99a
5502         cd /etc/init.d
5503         # some versions of cvs import exit(1) when asked to import links or
5504         # files they can't read.  ignore those files.
5505         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5506                         ! -perm +4 -printf '-I %f\n')
5507         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5508                 d99reposname vtag rtag
5509 }
5510 run_test 99b "cvs import ======================================="
5511
5512 test_99c() {
5513         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5514         [ ! -d $DIR/d99cvsroot ] && test_99b
5515         cd $DIR
5516         test_mkdir -p $DIR/d99reposname
5517         chown $RUNAS_ID $DIR/d99reposname
5518         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5519 }
5520 run_test 99c "cvs checkout ====================================="
5521
5522 test_99d() {
5523         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5524         [ ! -d $DIR/d99cvsroot ] && test_99c
5525         cd $DIR/d99reposname
5526         $RUNAS touch foo99
5527         $RUNAS cvs add -m 'addmsg' foo99
5528 }
5529 run_test 99d "cvs add =========================================="
5530
5531 test_99e() {
5532         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5533         [ ! -d $DIR/d99cvsroot ] && test_99c
5534         cd $DIR/d99reposname
5535         $RUNAS cvs update
5536 }
5537 run_test 99e "cvs update ======================================="
5538
5539 test_99f() {
5540         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5541         [ ! -d $DIR/d99cvsroot ] && test_99d
5542         cd $DIR/d99reposname
5543         $RUNAS cvs commit -m 'nomsg' foo99
5544     rm -fr $DIR/d99cvsroot
5545 }
5546 run_test 99f "cvs commit ======================================="
5547
5548 test_100() {
5549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5550         [ "$NETTYPE" = tcp ] || \
5551                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5552                         return ; }
5553
5554         remote_ost_nodsh && skip "remote OST with nodsh" && return
5555         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5556         remote_servers || \
5557                 { skip "useless for local single node setup" && return; }
5558
5559         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5560                 [ "$PROT" != "tcp" ] && continue
5561                 RPORT=$(echo $REMOTE | cut -d: -f2)
5562                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5563
5564                 rc=0
5565                 LPORT=`echo $LOCAL | cut -d: -f2`
5566                 if [ $LPORT -ge 1024 ]; then
5567                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5568                         netstat -tna
5569                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5570                 fi
5571         done
5572         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5573 }
5574 run_test 100 "check local port using privileged port ==========="
5575
5576 function get_named_value()
5577 {
5578     local tag
5579
5580     tag=$1
5581     while read ;do
5582         line=$REPLY
5583         case $line in
5584         $tag*)
5585             echo $line | sed "s/^$tag[ ]*//"
5586             break
5587             ;;
5588         esac
5589     done
5590 }
5591
5592 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5593                    awk '/^max_cached_mb/ { print $2 }')
5594
5595 cleanup_101a() {
5596         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5597         trap 0
5598 }
5599
5600 test_101a() {
5601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5602         local s
5603         local discard
5604         local nreads=10000
5605         local cache_limit=32
5606
5607         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5608         trap cleanup_101a EXIT
5609         $LCTL set_param -n llite.*.read_ahead_stats 0
5610         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5611
5612         #
5613         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5614         #
5615         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5616         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5617
5618         discard=0
5619         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5620                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5621                         discard=$(($discard + $s))
5622         done
5623         cleanup_101a
5624
5625         if [ $(($discard * 10)) -gt $nreads ] ;then
5626                 $LCTL get_param osc.*-osc*.rpc_stats
5627                 $LCTL get_param llite.*.read_ahead_stats
5628                 error "too many ($discard) discarded pages"
5629         fi
5630         rm -f $DIR/$tfile || true
5631 }
5632 run_test 101a "check read-ahead for random reads ================"
5633
5634 setup_test101bc() {
5635         test_mkdir -p $DIR/$tdir
5636         STRIPE_SIZE=1048576
5637         STRIPE_COUNT=$OSTCOUNT
5638         STRIPE_OFFSET=0
5639
5640         local list=$(comma_list $(osts_nodes))
5641         set_osd_param $list '' read_cache_enable 0
5642         set_osd_param $list '' writethrough_cache_enable 0
5643
5644         trap cleanup_test101bc EXIT
5645         # prepare the read-ahead file
5646         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5647
5648         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5649 }
5650
5651 cleanup_test101bc() {
5652         trap 0
5653         rm -rf $DIR/$tdir
5654         rm -f $DIR/$tfile
5655
5656         local list=$(comma_list $(osts_nodes))
5657         set_osd_param $list '' read_cache_enable 1
5658         set_osd_param $list '' writethrough_cache_enable 1
5659 }
5660
5661 calc_total() {
5662         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5663 }
5664
5665 ra_check_101() {
5666         local READ_SIZE=$1
5667         local STRIPE_SIZE=1048576
5668         local RA_INC=1048576
5669         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5670         local FILE_LENGTH=$((64*100))
5671         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5672                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5673         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5674                         get_named_value 'read but discarded' | \
5675                         cut -d" " -f1 | calc_total`
5676         if [ $DISCARD -gt $discard_limit ]; then
5677                 $LCTL get_param llite.*.read_ahead_stats
5678                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5679         else
5680                 echo "Read-ahead success for size ${READ_SIZE}"
5681         fi
5682 }
5683
5684 test_101b() {
5685         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5686         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5687         local STRIPE_SIZE=1048576
5688         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5689         local FILE_LENGTH=$((STRIPE_SIZE*100))
5690         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5691         # prepare the read-ahead file
5692         setup_test101bc
5693         cancel_lru_locks osc
5694         for BIDX in 2 4 8 16 32 64 128 256
5695         do
5696                 local BSIZE=$((BIDX*4096))
5697                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5698                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5699                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5700                 $LCTL set_param -n llite.*.read_ahead_stats 0
5701                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5702                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5703                 cancel_lru_locks osc
5704                 ra_check_101 $BSIZE
5705         done
5706         cleanup_test101bc
5707         true
5708 }
5709 run_test 101b "check stride-io mode read-ahead ================="
5710
5711 test_101c() {
5712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5713         local STRIPE_SIZE=1048576
5714         local FILE_LENGTH=$((STRIPE_SIZE*100))
5715         local nreads=10000
5716         local osc
5717
5718     setup_test101bc
5719
5720     cancel_lru_locks osc
5721     $LCTL set_param osc.*.rpc_stats 0
5722     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5723     for osc in $($LCTL get_param -N osc.*); do
5724         if [ "$osc" == "osc.num_refs" ]; then
5725             continue
5726         fi
5727
5728         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5729         if [ $lines -le 20 ]; then
5730             continue
5731         fi
5732
5733         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5734                                      awk '$1 == "1:" { print $2; exit; }')
5735         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5736                                      awk '$1 == "2:" { print $2; exit; }')
5737         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5738                                      awk '$1 == "4:" { print $2; exit; }')
5739         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5740                                      awk '$1 == "8:" { print $2; exit; }')
5741
5742         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5743         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5744         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5745         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5746         echo "${osc} rpc check passed!"
5747     done
5748     cleanup_test101bc
5749     true
5750 }
5751 run_test 101c "check stripe_size aligned read-ahead ================="
5752
5753 set_read_ahead() {
5754    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5755    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5756 }
5757
5758 test_101d() {
5759         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5760         local file=$DIR/$tfile
5761         local size=${FILESIZE_101c:-500}
5762         local ra_MB=${READAHEAD_MB:-40}
5763
5764         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5765         [ $space -gt $((size * 1024)) ] ||
5766                 { skip "Need free space ${size}M, have $space" && return; }
5767
5768     echo Creating ${size}M test file $file
5769     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5770     echo Cancel LRU locks on lustre client to flush the client cache
5771     cancel_lru_locks osc
5772
5773     echo Disable read-ahead
5774     local old_READAHEAD=$(set_read_ahead 0)
5775
5776     echo Reading the test file $file with read-ahead disabled
5777     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5778
5779     echo Cancel LRU locks on lustre client to flush the client cache
5780     cancel_lru_locks osc
5781     echo Enable read-ahead with ${ra_MB}MB
5782     set_read_ahead $ra_MB
5783
5784     echo Reading the test file $file with read-ahead enabled
5785     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5786
5787     echo read-ahead disabled time read $time_ra_OFF
5788     echo read-ahead enabled  time read $time_ra_ON
5789
5790         set_read_ahead $old_READAHEAD
5791         rm -f $file
5792         wait_delete_completed
5793
5794     [ $time_ra_ON -lt $time_ra_OFF ] ||
5795         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5796                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5797 }
5798 run_test 101d "file read with and without read-ahead enabled  ================="
5799
5800 test_101e() {
5801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5802     local file=$DIR/$tfile
5803     local size=500  #KB
5804     local count=100
5805     local blksize=1024
5806
5807     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5808     local need_space=$((count * size))
5809     [ $space -gt $need_space ] ||
5810         { skip_env "Need free space $need_space, have $space" && return; }
5811
5812     echo Creating $count ${size}K test files
5813     for ((i = 0; i < $count; i++)); do
5814         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5815     done
5816
5817     echo Cancel LRU locks on lustre client to flush the client cache
5818     cancel_lru_locks osc
5819
5820     echo Reset readahead stats
5821     $LCTL set_param -n llite.*.read_ahead_stats 0
5822
5823     for ((i = 0; i < $count; i++)); do
5824         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5825     done
5826
5827     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5828           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5829
5830     for ((i = 0; i < $count; i++)); do
5831         rm -rf ${file}_${i} 2>/dev/null
5832     done
5833
5834     #10000 means 20% reads are missing in readahead
5835     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5836 }
5837 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5838
5839 cleanup_test101f() {
5840     trap 0
5841     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5842     rm -rf $DIR/$tfile 2>/dev/null
5843 }
5844
5845 test_101f() {
5846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5847     local file=$DIR/$tfile
5848     local nreads=1000
5849
5850     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5851     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5852     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5853     trap cleanup_test101f EXIT
5854
5855     echo Cancel LRU locks on lustre client to flush the client cache
5856     cancel_lru_locks osc
5857
5858     echo Reset readahead stats
5859     $LCTL set_param -n llite.*.read_ahead_stats 0
5860     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5861     # readahead should read in 2M file on second read, so only miss
5862     # 2 pages.
5863     echo Random 4K reads on 2M file for 1000 times
5864     $READS -f $file -s 2097152 -b 4096 -n $nreads
5865
5866     echo checking missing pages
5867     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5868           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5869
5870     [ $miss -lt 3 ] || error "misses too much pages!"
5871     cleanup_test101f
5872 }
5873 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5874
5875 setup_test102() {
5876         test_mkdir -p $DIR/$tdir
5877         chown $RUNAS_ID $DIR/$tdir
5878         STRIPE_SIZE=65536
5879         STRIPE_OFFSET=1
5880         STRIPE_COUNT=$OSTCOUNT
5881         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5882
5883         trap cleanup_test102 EXIT
5884         cd $DIR
5885         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5886         cd $DIR/$tdir
5887         for num in 1 2 3 4; do
5888                 for count in $(seq 1 $STRIPE_COUNT); do
5889                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5890                                 local size=`expr $STRIPE_SIZE \* $num`
5891                                 local file=file"$num-$idx-$count"
5892                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5893                         done
5894                 done
5895         done
5896
5897         cd $DIR
5898         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5899 }
5900
5901 cleanup_test102() {
5902         trap 0
5903         rm -f $TMP/f102.tar
5904         rm -rf $DIR/d0.sanity/d102
5905 }
5906
5907 test_102a() {
5908         local testfile=$DIR/xattr_testfile
5909
5910         touch $testfile
5911
5912         [ "$UID" != 0 ] && skip_env "must run as root" && return
5913         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5914                 skip_env "must have user_xattr" && return
5915
5916         [ -z "$(which setfattr 2>/dev/null)" ] &&
5917                 skip_env "could not find setfattr" && return
5918
5919         echo "set/get xattr..."
5920         setfattr -n trusted.name1 -v value1 $testfile || error
5921         getfattr -n trusted.name1 $testfile 2> /dev/null |
5922           grep "trusted.name1=.value1" ||
5923                 error "$testfile missing trusted.name1=value1"
5924
5925         setfattr -n user.author1 -v author1 $testfile || error
5926         getfattr -n user.author1 $testfile 2> /dev/null |
5927           grep "user.author1=.author1" ||
5928                 error "$testfile missing trusted.author1=author1"
5929
5930         echo "listxattr..."
5931         setfattr -n trusted.name2 -v value2 $testfile ||
5932                 error "$testfile unable to set trusted.name2"
5933         setfattr -n trusted.name3 -v value3 $testfile ||
5934                 error "$testfile unable to set trusted.name3"
5935         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5936             grep "trusted.name" | wc -l) -eq 3 ] ||
5937                 error "$testfile missing 3 trusted.name xattrs"
5938
5939         setfattr -n user.author2 -v author2 $testfile ||
5940                 error "$testfile unable to set user.author2"
5941         setfattr -n user.author3 -v author3 $testfile ||
5942                 error "$testfile unable to set user.author3"
5943         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5944             grep "user.author" | wc -l) -eq 3 ] ||
5945                 error "$testfile missing 3 user.author xattrs"
5946
5947         echo "remove xattr..."
5948         setfattr -x trusted.name1 $testfile ||
5949                 error "$testfile error deleting trusted.name1"
5950         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5951                 error "$testfile did not delete trusted.name1 xattr"
5952
5953         setfattr -x user.author1 $testfile ||
5954                 error "$testfile error deleting user.author1"
5955         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5956                 error "$testfile did not delete trusted.name1 xattr"
5957
5958         # b10667: setting lustre special xattr be silently discarded
5959         echo "set lustre special xattr ..."
5960         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5961                 error "$testfile allowed setting trusted.lov"
5962 }
5963 run_test 102a "user xattr test =================================="
5964
5965 test_102b() {
5966         # b10930: get/set/list trusted.lov xattr
5967         echo "get/set/list trusted.lov xattr ..."
5968         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5969         local testfile=$DIR/$tfile
5970         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5971                 error "setstripe failed"
5972         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5973                 error "getstripe failed"
5974         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5975         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5976
5977         local testfile2=${testfile}2
5978         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5979                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5980
5981         $MCREATE $testfile2
5982         setfattr -n trusted.lov -v $value $testfile2
5983         local stripe_size=$($GETSTRIPE -S $testfile2)
5984         local stripe_count=$($GETSTRIPE -c $testfile2)
5985         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5986         [ $stripe_count -eq $STRIPECOUNT ] ||
5987                 error "stripe count $stripe_count != $STRIPECOUNT"
5988         rm -f $DIR/$tfile
5989 }
5990 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5991
5992 test_102c() {
5993         # b10930: get/set/list lustre.lov xattr
5994         echo "get/set/list lustre.lov xattr ..."
5995         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5996         test_mkdir -p $DIR/$tdir
5997         chown $RUNAS_ID $DIR/$tdir
5998         local testfile=$DIR/$tdir/$tfile
5999         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6000                 error "setstripe failed"
6001         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6002                 error "getstripe failed"
6003         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6004         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6005
6006         local testfile2=${testfile}2
6007         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6008                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6009
6010         $RUNAS $MCREATE $testfile2
6011         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6012         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6013         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6014         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6015         [ $stripe_count -eq $STRIPECOUNT ] ||
6016                 error "stripe count $stripe_count != $STRIPECOUNT"
6017 }
6018 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6019
6020 compare_stripe_info1() {
6021         local stripe_index_all_zero=true
6022
6023         for num in 1 2 3 4; do
6024                 for count in $(seq 1 $STRIPE_COUNT); do
6025                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6026                                 local size=$((STRIPE_SIZE * num))
6027                                 local file=file"$num-$offset-$count"
6028                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6029                                 [ $stripe_size -ne $size ] &&
6030                                     error "$file: size $stripe_size != $size"
6031                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6032                                 # allow fewer stripes to be created, ORI-601
6033                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
6034                                     error "$file: count $stripe_count != $count"
6035                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6036                                 [ $stripe_index -ne 0 ] &&
6037                                         stripe_index_all_zero=false
6038                         done
6039                 done
6040         done
6041         $stripe_index_all_zero &&
6042                 error "all files are being extracted starting from OST index 0"
6043         return 0
6044 }
6045
6046 find_lustre_tar() {
6047         [ -n "$(which tar 2>/dev/null)" ] &&
6048                 strings $(which tar) | grep -q "lustre" && echo tar
6049 }
6050
6051 test_102d() {
6052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6053         # b10930: tar test for trusted.lov xattr
6054         TAR=$(find_lustre_tar)
6055         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6056         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6057         setup_test102
6058         test_mkdir -p $DIR/d102d
6059         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6060         cd $DIR/d102d/$tdir
6061         compare_stripe_info1
6062 }
6063 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6064
6065 test_102f() {
6066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6067         # b10930: tar test for trusted.lov xattr
6068         TAR=$(find_lustre_tar)
6069         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6070         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6071         setup_test102
6072         test_mkdir -p $DIR/d102f
6073         cd $DIR
6074         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6075         cd $DIR/d102f/$tdir
6076         compare_stripe_info1
6077 }
6078 run_test 102f "tar copy files, not keep osts ==========="
6079
6080 grow_xattr() {
6081         local xsize=${1:-1024}  # in bytes
6082         local file=$DIR/$tfile
6083
6084         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6085                 skip "must have user_xattr" && return 0
6086         [ -z "$(which setfattr 2>/dev/null)" ] &&
6087                 skip_env "could not find setfattr" && return 0
6088         [ -z "$(which getfattr 2>/dev/null)" ] &&
6089                 skip_env "could not find getfattr" && return 0
6090
6091         touch $file
6092
6093         local value="$(generate_string $xsize)"
6094
6095         local xbig=trusted.big
6096         log "save $xbig on $file"
6097         setfattr -n $xbig -v $value $file ||
6098                 error "saving $xbig on $file failed"
6099
6100         local orig=$(get_xattr_value $xbig $file)
6101         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6102
6103         local xsml=trusted.sml
6104         log "save $xsml on $file"
6105         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6106
6107         local new=$(get_xattr_value $xbig $file)
6108         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6109
6110         log "grow $xsml on $file"
6111         setfattr -n $xsml -v "$value" $file ||
6112                 error "growing $xsml on $file failed"
6113
6114         new=$(get_xattr_value $xbig $file)
6115         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6116         log "$xbig still valid after growing $xsml"
6117
6118         rm -f $file
6119 }
6120
6121 test_102h() { # bug 15777
6122         grow_xattr 1024
6123 }
6124 run_test 102h "grow xattr from inside inode to external block"
6125
6126 test_102ha() {
6127         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6128         grow_xattr $(max_xattr_size)
6129 }
6130 run_test 102ha "grow xattr from inside inode to external inode"
6131
6132 test_102i() { # bug 17038
6133         touch $DIR/$tfile
6134         ln -s $DIR/$tfile $DIR/${tfile}link
6135         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6136         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"
6137         rm -f $DIR/$tfile $DIR/${tfile}link
6138 }
6139 run_test 102i "lgetxattr test on symbolic link ============"
6140
6141 test_102j() {
6142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6143         TAR=$(find_lustre_tar)
6144         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6145         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6146         setup_test102 "$RUNAS"
6147         test_mkdir -p $DIR/d102j
6148         chown $RUNAS_ID $DIR/d102j
6149         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6150         cd $DIR/d102j/$tdir
6151         compare_stripe_info1 "$RUNAS"
6152 }
6153 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6154
6155 test_102k() {
6156         touch $DIR/$tfile
6157         # b22187 just check that does not crash for regular file.
6158         setfattr -n trusted.lov $DIR/$tfile
6159         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6160         local test_kdir=$DIR/d102k
6161         test_mkdir $test_kdir
6162         local default_size=`$GETSTRIPE -S $test_kdir`
6163         local default_count=`$GETSTRIPE -c $test_kdir`
6164         local default_offset=`$GETSTRIPE -i $test_kdir`
6165         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
6166                 error 'dir setstripe failed'
6167         setfattr -n trusted.lov $test_kdir
6168         local stripe_size=`$GETSTRIPE -S $test_kdir`
6169         local stripe_count=`$GETSTRIPE -c $test_kdir`
6170         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6171         [ $stripe_size -eq $default_size ] ||
6172                 error "stripe size $stripe_size != $default_size"
6173         [ $stripe_count -eq $default_count ] ||
6174                 error "stripe count $stripe_count != $default_count"
6175         [ $stripe_offset -eq $default_offset ] ||
6176                 error "stripe offset $stripe_offset != $default_offset"
6177         rm -rf $DIR/$tfile $test_kdir
6178 }
6179 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6180
6181 test_102l() {
6182         # LU-532 trusted. xattr is invisible to non-root
6183         local testfile=$DIR/$tfile
6184
6185         touch $testfile
6186
6187         echo "listxattr as user..."
6188         chown $RUNAS_ID $testfile
6189         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6190             grep -q "trusted" &&
6191                 error "$testfile trusted xattrs are user visible"
6192
6193         return 0;
6194 }
6195 run_test 102l "listxattr filter test =================================="
6196
6197 cleanup_test102
6198
6199 run_acl_subtest()
6200 {
6201     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6202     return $?
6203 }
6204
6205 test_103 () {
6206     [ "$UID" != 0 ] && skip_env "must run as root" && return
6207     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6208     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
6209     $GSS && skip "could not run under gss" && return
6210
6211     declare -a identity_old
6212
6213         for num in $(seq $MDSCOUNT); do
6214                 switch_identity $num true || identity_old[$num]=$?
6215         done
6216
6217     SAVE_UMASK=`umask`
6218     umask 0022
6219     cd $DIR
6220
6221     echo "performing cp ..."
6222     run_acl_subtest cp || error
6223     echo "performing getfacl-noacl..."
6224     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6225     echo "performing misc..."
6226     run_acl_subtest misc || error  "misc test failed"
6227     echo "performing permissions..."
6228     run_acl_subtest permissions || error "permissions failed"
6229     echo "performing setfacl..."
6230     run_acl_subtest setfacl || error  "setfacl test failed"
6231
6232     # inheritance test got from HP
6233     echo "performing inheritance..."
6234     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6235     chmod +x make-tree || error "chmod +x failed"
6236     run_acl_subtest inheritance || error "inheritance test failed"
6237     rm -f make-tree
6238
6239         echo "LU-974 ignore umask when acl is enabled..."
6240         run_acl_subtest 974 || error "LU-974 test failed"
6241         if [ $MDSCOUNT -ge 2 ]; then
6242                 run_acl_subtest 974_remote ||
6243                         error "LU-974 test failed under remote dir"
6244         fi
6245
6246     echo "LU-2561 newly created file is same size as directory..."
6247     run_acl_subtest 2561 || error "LU-2561 test failed"
6248
6249     cd $SAVE_PWD
6250     umask $SAVE_UMASK
6251
6252         for num in $(seq $MDSCOUNT); do
6253                 if [ "${identity_old[$num]}" = 1 ]; then
6254                         switch_identity $num false || identity_old[$num]=$?
6255                 fi
6256         done
6257 }
6258 run_test 103 "acl test ========================================="
6259
6260 test_104a() {
6261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6262         touch $DIR/$tfile
6263         lfs df || error "lfs df failed"
6264         lfs df -ih || error "lfs df -ih failed"
6265         lfs df -h $DIR || error "lfs df -h $DIR failed"
6266         lfs df -i $DIR || error "lfs df -i $DIR failed"
6267         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6268         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6269
6270         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6271         lctl --device %$OSC deactivate
6272         lfs df || error "lfs df with deactivated OSC failed"
6273         lctl --device %$OSC activate
6274         # wait the osc back to normal
6275         wait_osc_import_state client ost FULL
6276
6277         lfs df || error "lfs df with reactivated OSC failed"
6278         rm -f $DIR/$tfile
6279 }
6280 run_test 104a "lfs df [-ih] [path] test ========================="
6281
6282 test_104b() {
6283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6284         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6285         chmod 666 /dev/obd
6286         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6287         if [ $denied_cnt -ne 0 ];
6288         then
6289                     error "lfs check servers test failed"
6290         fi
6291 }
6292 run_test 104b "$RUNAS lfs check servers test ===================="
6293
6294 test_105a() {
6295         # doesn't work on 2.4 kernels
6296         touch $DIR/$tfile
6297         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6298                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6299         else
6300                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6301         fi
6302         rm -f $DIR/$tfile
6303 }
6304 run_test 105a "flock when mounted without -o flock test ========"
6305
6306 test_105b() {
6307         touch $DIR/$tfile
6308         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6309                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6310         else
6311                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6312         fi
6313         rm -f $DIR/$tfile
6314 }
6315 run_test 105b "fcntl when mounted without -o flock test ========"
6316
6317 test_105c() {
6318         touch $DIR/$tfile
6319         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6320                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6321         else
6322                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6323         fi
6324         rm -f $DIR/$tfile
6325 }
6326 run_test 105c "lockf when mounted without -o flock test ========"
6327
6328 test_105d() { # bug 15924
6329         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6330         test_mkdir -p $DIR/$tdir
6331         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6332                 skip "mount w/o flock enabled" && return
6333         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6334         $LCTL set_param fail_loc=0x80000315
6335         flocks_test 2 $DIR/$tdir
6336 }
6337 run_test 105d "flock race (should not freeze) ========"
6338
6339 test_105e() { # bug 22660 && 22040
6340         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6341                 skip "mount w/o flock enabled" && return
6342         touch $DIR/$tfile
6343         flocks_test 3 $DIR/$tfile
6344 }
6345 run_test 105e "Two conflicting flocks from same process ======="
6346
6347 test_106() { #bug 10921
6348         test_mkdir -p $DIR/$tdir
6349         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6350         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6351 }
6352 run_test 106 "attempt exec of dir followed by chown of that dir"
6353
6354 test_107() {
6355         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6356         CDIR=`pwd`
6357         cd $DIR
6358
6359         local file=core
6360         rm -f $file
6361
6362         local save_pattern=$(sysctl -n kernel.core_pattern)
6363         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6364         sysctl -w kernel.core_pattern=$file
6365         sysctl -w kernel.core_uses_pid=0
6366
6367         ulimit -c unlimited
6368         sleep 60 &
6369         SLEEPPID=$!
6370
6371         sleep 1
6372
6373         kill -s 11 $SLEEPPID
6374         wait $SLEEPPID
6375         if [ -e $file ]; then
6376                 size=`stat -c%s $file`
6377                 [ $size -eq 0 ] && error "Fail to create core file $file"
6378         else
6379                 error "Fail to create core file $file"
6380         fi
6381         rm -f $file
6382         sysctl -w kernel.core_pattern=$save_pattern
6383         sysctl -w kernel.core_uses_pid=$save_uses_pid
6384         cd $CDIR
6385 }
6386 run_test 107 "Coredump on SIG"
6387
6388 test_110() {
6389         test_mkdir -p $DIR/$tdir
6390         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6391                 error "mkdir with 255 char failed"
6392         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6393                 error "mkdir with 256 char should fail, but did not"
6394         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6395                 error "create with 255 char failed"
6396         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6397                 error "create with 256 char should fail, but did not"
6398
6399         ls -l $DIR/$tdir
6400         rm -rf $DIR/$tdir
6401 }
6402 run_test 110 "filename length checking"
6403
6404 test_115() {
6405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6406         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6407             cut -c11-20)
6408         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6409             return
6410         echo "Starting with $OSTIO_pre threads"
6411
6412         NUMTEST=20000
6413         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6414         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6415         echo "$NUMTEST creates/unlinks"
6416         test_mkdir -p $DIR/$tdir
6417         createmany -o $DIR/$tdir/$tfile $NUMTEST
6418         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6419
6420         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6421             cut -c11-20)
6422
6423         # don't return an error
6424         [ $OSTIO_post == $OSTIO_pre ] && echo \
6425             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6426             echo "This may be fine, depending on what ran before this test" &&
6427             echo "and how fast this system is." && return
6428
6429         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6430 }
6431 run_test 115 "verify dynamic thread creation===================="
6432
6433 free_min_max () {
6434         wait_delete_completed
6435         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6436         echo OST kbytes available: ${AVAIL[@]}
6437         MAXI=0; MAXV=${AVAIL[0]}
6438         MINI=0; MINV=${AVAIL[0]}
6439         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6440             #echo OST $i: ${AVAIL[i]}kb
6441             if [ ${AVAIL[i]} -gt $MAXV ]; then
6442                 MAXV=${AVAIL[i]}; MAXI=$i
6443             fi
6444             if [ ${AVAIL[i]} -lt $MINV ]; then
6445                 MINV=${AVAIL[i]}; MINI=$i
6446             fi
6447         done
6448         echo Min free space: OST $MINI: $MINV
6449         echo Max free space: OST $MAXI: $MAXV
6450 }
6451
6452 test_116a() { # was previously test_116()
6453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6454         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6455
6456         echo -n "Free space priority "
6457         lctl get_param -n lov.*-clilov-*.qos_prio_free
6458         declare -a AVAIL
6459         free_min_max
6460         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6461                 return
6462
6463         # generate uneven OSTs
6464         test_mkdir -p $DIR/$tdir/OST${MINI}
6465         declare -i FILL
6466         FILL=$(($MINV / 4))
6467         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6468         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6469         i=0
6470         while [ $FILL -gt 0 ]; do
6471             i=$(($i + 1))
6472             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6473             FILL=$(($FILL - 2048))
6474             echo -n .
6475         done
6476         FILL=$(($MINV / 4))
6477         sync
6478         sleep_maxage
6479
6480         free_min_max
6481         DIFF=$(($MAXV - $MINV))
6482         DIFF2=$(($DIFF * 100 / $MINV))
6483         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6484         if [ $DIFF2 -gt 20 ]; then
6485             echo "ok"
6486         else
6487             echo "failed - QOS mode won't be used"
6488             error_ignore "QOS imbalance criteria not met"
6489             return
6490         fi
6491
6492         MINI1=$MINI; MINV1=$MINV
6493         MAXI1=$MAXI; MAXV1=$MAXV
6494
6495         # now fill using QOS
6496         echo writing a bunch of files to QOS-assigned OSTs
6497         $SETSTRIPE -c 1 $DIR/$tdir
6498         i=0
6499         while [ $FILL -gt 0 ]; do
6500             i=$(($i + 1))
6501             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6502             FILL=$(($FILL - 200))
6503             echo -n .
6504         done
6505         echo "wrote $i 200k files"
6506         sync
6507         sleep_maxage
6508
6509         echo "Note: free space may not be updated, so measurements might be off"
6510         free_min_max
6511         DIFF2=$(($MAXV - $MINV))
6512         echo "free space delta: orig $DIFF final $DIFF2"
6513         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6514         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6515         echo "Wrote $DIFF to smaller OST $MINI1"
6516         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6517         echo "Wrote $DIFF2 to larger OST $MAXI1"
6518         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6519
6520         # Figure out which files were written where
6521         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6522                awk '/'$MINI1': / {print $2; exit}')
6523         echo $UUID
6524         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6525         echo "$MINC files created on smaller OST $MINI1"
6526         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6527                awk '/'$MAXI1': / {print $2; exit}')
6528         echo $UUID
6529         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6530         echo "$MAXC files created on larger OST $MAXI1"
6531         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6532         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6533
6534         rm -rf $DIR/$tdir
6535 }
6536 run_test 116a "stripe QOS: free space balance ==================="
6537
6538 test_116b() { # LU-2093
6539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6540 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6541         local old_rr
6542         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6543         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6544         mkdir -p $DIR/$tdir
6545         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6546         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6547         do_facet $SINGLEMDS lctl set_param fail_loc=0
6548         rm -rf $DIR/$tdir
6549         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6550 }
6551 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6552
6553 test_117() # bug 10891
6554 {
6555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6556         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6557         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6558         lctl set_param fail_loc=0x21e
6559         > $DIR/$tfile || error "truncate failed"
6560         lctl set_param fail_loc=0
6561         echo "Truncate succeeded."
6562         rm -f $DIR/$tfile
6563 }
6564 run_test 117 "verify fsfilt_extend =========="
6565
6566 NO_SLOW_RESENDCOUNT=4
6567 export OLD_RESENDCOUNT=""
6568 set_resend_count () {
6569         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6570         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6571         lctl set_param -n $PROC_RESENDCOUNT $1
6572         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6573 }
6574
6575 # for reduce test_118* time (b=14842)
6576 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6577
6578 # Reset async IO behavior after error case
6579 reset_async() {
6580         FILE=$DIR/reset_async
6581
6582         # Ensure all OSCs are cleared
6583         $SETSTRIPE -c -1 $FILE
6584         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6585         sync
6586         rm $FILE
6587 }
6588
6589 test_118a() #bug 11710
6590 {
6591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6592         reset_async
6593
6594         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6595         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6596         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6597
6598         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6599                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6600                 return 1;
6601         fi
6602         rm -f $DIR/$tfile
6603 }
6604 run_test 118a "verify O_SYNC works =========="
6605
6606 test_118b()
6607 {
6608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6609         remote_ost_nodsh && skip "remote OST with nodsh" && return
6610
6611         reset_async
6612
6613         #define OBD_FAIL_OST_ENOENT 0x217
6614         set_nodes_failloc "$(osts_nodes)" 0x217
6615         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6616         RC=$?
6617         set_nodes_failloc "$(osts_nodes)" 0
6618         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6619         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6620                     grep -c writeback)
6621
6622         if [[ $RC -eq 0 ]]; then
6623                 error "Must return error due to dropped pages, rc=$RC"
6624                 return 1;
6625         fi
6626
6627         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6628                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6629                 return 1;
6630         fi
6631
6632         echo "Dirty pages not leaked on ENOENT"
6633
6634         # Due to the above error the OSC will issue all RPCs syncronously
6635         # until a subsequent RPC completes successfully without error.
6636         $MULTIOP $DIR/$tfile Ow4096yc
6637         rm -f $DIR/$tfile
6638
6639         return 0
6640 }
6641 run_test 118b "Reclaim dirty pages on fatal error =========="
6642
6643 test_118c()
6644 {
6645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6646
6647         # for 118c, restore the original resend count, LU-1940
6648         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
6649                                 set_resend_count $OLD_RESENDCOUNT
6650         remote_ost_nodsh && skip "remote OST with nodsh" && return
6651
6652         reset_async
6653
6654         #define OBD_FAIL_OST_EROFS               0x216
6655         set_nodes_failloc "$(osts_nodes)" 0x216
6656
6657         # multiop should block due to fsync until pages are written
6658         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6659         MULTIPID=$!
6660         sleep 1
6661
6662         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6663                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6664         fi
6665
6666         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6667                     grep -c writeback)
6668         if [[ $WRITEBACK -eq 0 ]]; then
6669                 error "No page in writeback, writeback=$WRITEBACK"
6670         fi
6671
6672         set_nodes_failloc "$(osts_nodes)" 0
6673         wait $MULTIPID
6674         RC=$?
6675         if [[ $RC -ne 0 ]]; then
6676                 error "Multiop fsync failed, rc=$RC"
6677         fi
6678
6679         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6680         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6681                     grep -c writeback)
6682         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6683                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6684         fi
6685
6686         rm -f $DIR/$tfile
6687         echo "Dirty pages flushed via fsync on EROFS"
6688         return 0
6689 }
6690 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6691
6692 # continue to use small resend count to reduce test_118* time (b=14842)
6693 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6694
6695 test_118d()
6696 {
6697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6698         remote_ost_nodsh && skip "remote OST with nodsh" && return
6699
6700         reset_async
6701
6702         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6703         set_nodes_failloc "$(osts_nodes)" 0x214
6704         # multiop should block due to fsync until pages are written
6705         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6706         MULTIPID=$!
6707         sleep 1
6708
6709         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6710                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6711         fi
6712
6713         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6714                     grep -c writeback)
6715         if [[ $WRITEBACK -eq 0 ]]; then
6716                 error "No page in writeback, writeback=$WRITEBACK"
6717         fi
6718
6719         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6720         set_nodes_failloc "$(osts_nodes)" 0
6721
6722         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6723         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6724                     grep -c writeback)
6725         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6726                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6727         fi
6728
6729         rm -f $DIR/$tfile
6730         echo "Dirty pages gaurenteed flushed via fsync"
6731         return 0
6732 }
6733 run_test 118d "Fsync validation inject a delay of the bulk =========="
6734
6735 test_118f() {
6736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6737         reset_async
6738
6739         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6740         lctl set_param fail_loc=0x8000040a
6741
6742         # Should simulate EINVAL error which is fatal
6743         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6744         RC=$?
6745         if [[ $RC -eq 0 ]]; then
6746                 error "Must return error due to dropped pages, rc=$RC"
6747         fi
6748
6749         lctl set_param fail_loc=0x0
6750
6751         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6752         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6753         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6754                     grep -c writeback)
6755         if [[ $LOCKED -ne 0 ]]; then
6756                 error "Locked pages remain in cache, locked=$LOCKED"
6757         fi
6758
6759         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6760                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6761         fi
6762
6763         rm -f $DIR/$tfile
6764         echo "No pages locked after fsync"
6765
6766         reset_async
6767         return 0
6768 }
6769 run_test 118f "Simulate unrecoverable OSC side error =========="
6770
6771 test_118g() {
6772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6773         reset_async
6774
6775         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6776         lctl set_param fail_loc=0x406
6777
6778         # simulate local -ENOMEM
6779         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6780         RC=$?
6781
6782         lctl set_param fail_loc=0
6783         if [[ $RC -eq 0 ]]; then
6784                 error "Must return error due to dropped pages, rc=$RC"
6785         fi
6786
6787         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6788         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6789         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6790                         grep -c writeback)
6791         if [[ $LOCKED -ne 0 ]]; then
6792                 error "Locked pages remain in cache, locked=$LOCKED"
6793         fi
6794
6795         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6796                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6797         fi
6798
6799         rm -f $DIR/$tfile
6800         echo "No pages locked after fsync"
6801
6802         reset_async
6803         return 0
6804 }
6805 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6806
6807 test_118h() {
6808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6809         remote_ost_nodsh && skip "remote OST with nodsh" && return
6810
6811         reset_async
6812
6813         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6814         set_nodes_failloc "$(osts_nodes)" 0x20e
6815         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6816         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6817         RC=$?
6818
6819         set_nodes_failloc "$(osts_nodes)" 0
6820         if [[ $RC -eq 0 ]]; then
6821                 error "Must return error due to dropped pages, rc=$RC"
6822         fi
6823
6824         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6825         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6826         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6827                     grep -c writeback)
6828         if [[ $LOCKED -ne 0 ]]; then
6829                 error "Locked pages remain in cache, locked=$LOCKED"
6830         fi
6831
6832         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6833                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6834         fi
6835
6836         rm -f $DIR/$tfile
6837         echo "No pages locked after fsync"
6838
6839         return 0
6840 }
6841 run_test 118h "Verify timeout in handling recoverables errors  =========="
6842
6843 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6844
6845 test_118i() {
6846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6847         remote_ost_nodsh && skip "remote OST with nodsh" && return
6848
6849         reset_async
6850
6851         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6852         set_nodes_failloc "$(osts_nodes)" 0x20e
6853
6854         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6855         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6856         PID=$!
6857         sleep 5
6858         set_nodes_failloc "$(osts_nodes)" 0
6859
6860         wait $PID
6861         RC=$?
6862         if [[ $RC -ne 0 ]]; then
6863                 error "got error, but should be not, rc=$RC"
6864         fi
6865
6866         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6867         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6868         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6869         if [[ $LOCKED -ne 0 ]]; then
6870                 error "Locked pages remain in cache, locked=$LOCKED"
6871         fi
6872
6873         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6874                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6875         fi
6876
6877         rm -f $DIR/$tfile
6878         echo "No pages locked after fsync"
6879
6880         return 0
6881 }
6882 run_test 118i "Fix error before timeout in recoverable error  =========="
6883
6884 [ "$SLOW" = "no" ] && set_resend_count 4
6885
6886 test_118j() {
6887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6888         remote_ost_nodsh && skip "remote OST with nodsh" && return
6889
6890         reset_async
6891
6892         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6893         set_nodes_failloc "$(osts_nodes)" 0x220
6894
6895         # return -EIO from OST
6896         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6897         RC=$?
6898         set_nodes_failloc "$(osts_nodes)" 0x0
6899         if [[ $RC -eq 0 ]]; then
6900                 error "Must return error due to dropped pages, rc=$RC"
6901         fi
6902
6903         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6904         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6905         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6906         if [[ $LOCKED -ne 0 ]]; then
6907                 error "Locked pages remain in cache, locked=$LOCKED"
6908         fi
6909
6910         # in recoverable error on OST we want resend and stay until it finished
6911         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6912                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6913         fi
6914
6915         rm -f $DIR/$tfile
6916         echo "No pages locked after fsync"
6917
6918         return 0
6919 }
6920 run_test 118j "Simulate unrecoverable OST side error =========="
6921
6922 test_118k()
6923 {
6924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6925         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6926
6927         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6928         set_nodes_failloc "$(osts_nodes)" 0x20e
6929         test_mkdir -p $DIR/$tdir
6930
6931         for ((i=0;i<10;i++)); do
6932                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6933                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6934                 SLEEPPID=$!
6935                 sleep 0.500s
6936                 kill $SLEEPPID
6937                 wait $SLEEPPID
6938         done
6939
6940         set_nodes_failloc "$(osts_nodes)" 0
6941         rm -rf $DIR/$tdir
6942 }
6943 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6944
6945 test_118l()
6946 {
6947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6948         # LU-646
6949         test_mkdir -p $DIR/$tdir
6950         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6951         rm -rf $DIR/$tdir
6952 }
6953 run_test 118l "fsync dir ========="
6954
6955 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6956
6957 test_119a() # bug 11737
6958 {
6959         BSIZE=$((512 * 1024))
6960         directio write $DIR/$tfile 0 1 $BSIZE
6961         # We ask to read two blocks, which is more than a file size.
6962         # directio will indicate an error when requested and actual
6963         # sizes aren't equeal (a normal situation in this case) and
6964         # print actual read amount.
6965         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6966         if [ "$NOB" != "$BSIZE" ]; then
6967                 error "read $NOB bytes instead of $BSIZE"
6968         fi
6969         rm -f $DIR/$tfile
6970 }
6971 run_test 119a "Short directIO read must return actual read amount"
6972
6973 test_119b() # bug 11737
6974 {
6975         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6976
6977         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6978         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6979         sync
6980         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6981                 error "direct read failed"
6982         rm -f $DIR/$tfile
6983 }
6984 run_test 119b "Sparse directIO read must return actual read amount"
6985
6986 test_119c() # bug 13099
6987 {
6988         BSIZE=1048576
6989         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6990         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6991         rm -f $DIR/$tfile
6992 }
6993 run_test 119c "Testing for direct read hitting hole"
6994
6995 test_119d() # bug 15950
6996 {
6997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6998         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6999         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7000         BSIZE=1048576
7001         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7002         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7003         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7004         lctl set_param fail_loc=0x40d
7005         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7006         pid_dio=$!
7007         sleep 1
7008         cat $DIR/$tfile > /dev/null &
7009         lctl set_param fail_loc=0
7010         pid_reads=$!
7011         wait $pid_dio
7012         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7013         sleep 2
7014         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7015         error "the read rpcs have not completed in 2s"
7016         rm -f $DIR/$tfile
7017         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7018 }
7019 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7020
7021 test_120a() {
7022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7023         test_mkdir -p $DIR/$tdir
7024         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7025                skip "no early lock cancel on server" && return 0
7026         lru_resize_disable mdc
7027         lru_resize_disable osc
7028         cancel_lru_locks mdc
7029         stat $DIR/$tdir > /dev/null
7030         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7031         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7032         test_mkdir $DIR/$tdir/d1
7033         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7034         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7035         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7036         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7037         lru_resize_enable mdc
7038         lru_resize_enable osc
7039 }
7040 run_test 120a "Early Lock Cancel: mkdir test"
7041
7042 test_120b() {
7043         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7044         test_mkdir -p $DIR/$tdir
7045         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7046                skip "no early lock cancel on server" && return 0
7047         lru_resize_disable mdc
7048         lru_resize_disable osc
7049         cancel_lru_locks mdc
7050         stat $DIR/$tdir > /dev/null
7051         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7052         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7053         touch $DIR/$tdir/f1
7054         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7055         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7056         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7057         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7058         lru_resize_enable mdc
7059         lru_resize_enable osc
7060 }
7061 run_test 120b "Early Lock Cancel: create test"
7062
7063 test_120c() {
7064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7065         test_mkdir -p $DIR/$tdir
7066         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7067                skip "no early lock cancel on server" && return 0
7068         lru_resize_disable mdc
7069         lru_resize_disable osc
7070         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7071         touch $DIR/$tdir/d1/f1
7072         cancel_lru_locks mdc
7073         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7074         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7075         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7076         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7077         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7078         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7079         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7080         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7081         lru_resize_enable mdc
7082         lru_resize_enable osc
7083 }
7084 run_test 120c "Early Lock Cancel: link test"
7085
7086 test_120d() {
7087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7088         test_mkdir -p $DIR/$tdir
7089         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7090                skip "no early lock cancel on server" && return 0
7091         lru_resize_disable mdc
7092         lru_resize_disable osc
7093         touch $DIR/$tdir
7094         cancel_lru_locks mdc
7095         stat $DIR/$tdir > /dev/null
7096         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7097         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7098         chmod a+x $DIR/$tdir
7099         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7100         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7101         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7102         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7103         lru_resize_enable mdc
7104         lru_resize_enable osc
7105 }
7106 run_test 120d "Early Lock Cancel: setattr test"
7107
7108 test_120e() {
7109         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7110         test_mkdir -p $DIR/$tdir
7111         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7112                skip "no early lock cancel on server" && return 0
7113         lru_resize_disable mdc
7114         lru_resize_disable osc
7115         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7116         cancel_lru_locks mdc
7117         cancel_lru_locks osc
7118         dd if=$DIR/$tdir/f1 of=/dev/null
7119         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7120         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7121               awk '/ldlm_cancel/ {print $2}'`
7122         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7123               awk '/ldlm_bl_callback/ {print $2}'`
7124         unlink $DIR/$tdir/f1
7125         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7126               awk '/ldlm_cancel/ {print $2}'`
7127         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7128               awk '/ldlm_bl_callback/ {print $2}'`
7129         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7130         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7131         lru_resize_enable mdc
7132         lru_resize_enable osc
7133 }
7134 run_test 120e "Early Lock Cancel: unlink test"
7135
7136 test_120f() {
7137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7138         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7139                skip "no early lock cancel on server" && return 0
7140         test_mkdir -p $DIR/$tdir
7141         lru_resize_disable mdc
7142         lru_resize_disable osc
7143         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7144         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7145         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7146         cancel_lru_locks mdc
7147         cancel_lru_locks osc
7148         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7149         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7150         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7151         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7152               awk '/ldlm_cancel/ {print $2}'`
7153         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7154               awk '/ldlm_bl_callback/ {print $2}'`
7155         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7156         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7157               awk '/ldlm_cancel/ {print $2}'`
7158         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7159               awk '/ldlm_bl_callback/ {print $2}'`
7160         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7161         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7162         lru_resize_enable mdc
7163         lru_resize_enable osc
7164 }
7165 run_test 120f "Early Lock Cancel: rename test"
7166
7167 test_120g() {
7168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7169         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7170                skip "no early lock cancel on server" && return 0
7171         lru_resize_disable mdc
7172         lru_resize_disable osc
7173         count=10000
7174         echo create $count files
7175         test_mkdir -p $DIR/$tdir
7176         cancel_lru_locks mdc
7177         cancel_lru_locks osc
7178         t0=`date +%s`
7179
7180         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7181               awk '/ldlm_cancel/ {print $2}'`
7182         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7183               awk '/ldlm_bl_callback/ {print $2}'`
7184         createmany -o $DIR/$tdir/f $count
7185         sync
7186         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7187               awk '/ldlm_cancel/ {print $2}'`
7188         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7189               awk '/ldlm_bl_callback/ {print $2}'`
7190         t1=`date +%s`
7191         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7192         echo rm $count files
7193         rm -r $DIR/$tdir
7194         sync
7195         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7196               awk '/ldlm_cancel/ {print $2}'`
7197         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7198               awk '/ldlm_bl_callback/ {print $2}'`
7199         t2=`date +%s`
7200         echo total: $count removes in $((t2-t1))
7201         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7202         sleep 2
7203         # wait for commitment of removal
7204         lru_resize_enable mdc
7205         lru_resize_enable osc
7206 }
7207 run_test 120g "Early Lock Cancel: performance test"
7208
7209 test_121() { #bug #10589
7210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7211         rm -rf $DIR/$tfile
7212         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7213 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7214         lctl set_param fail_loc=0x310
7215         cancel_lru_locks osc > /dev/null
7216         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7217         lctl set_param fail_loc=0
7218         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7219 }
7220 run_test 121 "read cancel race ========="
7221
7222 test_123a() { # was test 123, statahead(bug 11401)
7223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7224         SLOWOK=0
7225         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7226             log "testing on UP system. Performance may be not as good as expected."
7227                         SLOWOK=1
7228         fi
7229
7230         rm -rf $DIR/$tdir
7231         test_mkdir -p $DIR/$tdir
7232         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7233         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7234         MULT=10
7235         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7236                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7237
7238                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7239                 lctl set_param -n llite.*.statahead_max 0
7240                 lctl get_param llite.*.statahead_max
7241                 cancel_lru_locks mdc
7242                 cancel_lru_locks osc
7243                 stime=`date +%s`
7244                 time ls -l $DIR/$tdir | wc -l
7245                 etime=`date +%s`
7246                 delta=$((etime - stime))
7247                 log "ls $i files without statahead: $delta sec"
7248                 lctl set_param llite.*.statahead_max=$max
7249
7250                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7251                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7252                 cancel_lru_locks mdc
7253                 cancel_lru_locks osc
7254                 stime=`date +%s`
7255                 time ls -l $DIR/$tdir | wc -l
7256                 etime=`date +%s`
7257                 delta_sa=$((etime - stime))
7258                 log "ls $i files with statahead: $delta_sa sec"
7259                 lctl get_param -n llite.*.statahead_stats
7260                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7261
7262                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7263                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7264
7265                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7266                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7267                     lctl set_param -n llite.*.statahead_max 0
7268                     lctl get_param llite.*.statahead_max
7269                     cancel_lru_locks mdc
7270                     cancel_lru_locks osc
7271                     stime=`date +%s`
7272                     time ls -l $DIR/$tdir | wc -l
7273                     etime=`date +%s`
7274                     delta=$((etime - stime))
7275                     log "ls $i files again without statahead: $delta sec"
7276                     lctl set_param llite.*.statahead_max=$max
7277                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7278                         if [  $SLOWOK -eq 0 ]; then
7279                                 error "ls $i files is slower with statahead!"
7280                         else
7281                                 log "ls $i files is slower with statahead!"
7282                         fi
7283                         break
7284                     fi
7285                 fi
7286
7287                 [ $delta -gt 20 ] && break
7288                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7289                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7290         done
7291         log "ls done"
7292
7293         stime=`date +%s`
7294         rm -r $DIR/$tdir
7295         sync
7296         etime=`date +%s`
7297         delta=$((etime - stime))
7298         log "rm -r $DIR/$tdir/: $delta seconds"
7299         log "rm done"
7300         lctl get_param -n llite.*.statahead_stats
7301 }
7302 run_test 123a "verify statahead work"
7303
7304 test_123b () { # statahead(bug 15027)
7305         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7306         test_mkdir -p $DIR/$tdir
7307         createmany -o $DIR/$tdir/$tfile-%d 1000
7308
7309         cancel_lru_locks mdc
7310         cancel_lru_locks osc
7311
7312 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7313         lctl set_param fail_loc=0x80000803
7314         ls -lR $DIR/$tdir > /dev/null
7315         log "ls done"
7316         lctl set_param fail_loc=0x0
7317         lctl get_param -n llite.*.statahead_stats
7318         rm -r $DIR/$tdir
7319         sync
7320
7321 }
7322 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7323
7324 test_124a() {
7325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7326         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7327                skip "no lru resize on server" && return 0
7328         local NR=2000
7329         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7330
7331         log "create $NR files at $DIR/$tdir"
7332         createmany -o $DIR/$tdir/f $NR ||
7333                 error "failed to create $NR files in $DIR/$tdir"
7334
7335         cancel_lru_locks mdc
7336         ls -l $DIR/$tdir > /dev/null
7337
7338         local NSDIR=""
7339         local LRU_SIZE=0
7340         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7341                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7342                 LRU_SIZE=$(lctl get_param -n $PARAM)
7343                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7344                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7345                                                 log "NSDIR=$NSDIR"
7346                         log "NS=$(basename $NSDIR)"
7347                         break
7348                 fi
7349         done
7350
7351         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7352                 skip "Not enough cached locks created!"
7353                 return 0
7354         fi
7355         log "LRU=$LRU_SIZE"
7356
7357         local SLEEP=30
7358
7359         # We know that lru resize allows one client to hold $LIMIT locks
7360         # for 10h. After that locks begin to be killed by client.
7361         local MAX_HRS=10
7362         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7363                 log "LIMIT=$LIMIT"
7364
7365         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7366         # killing locks. Some time was spent for creating locks. This means
7367         # that up to the moment of sleep finish we must have killed some of
7368         # them (10-100 locks). This depends on how fast ther were created.
7369         # Many of them were touched in almost the same moment and thus will
7370         # be killed in groups.
7371         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7372
7373         # Use $LRU_SIZE_B here to take into account real number of locks
7374         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7375         local LRU_SIZE_B=$LRU_SIZE
7376         log "LVF=$LVF"
7377         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7378                 log "OLD_LVF=$OLD_LVF"
7379         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7380
7381         # Let's make sure that we really have some margin. Client checks
7382         # cached locks every 10 sec.
7383         SLEEP=$((SLEEP+20))
7384         log "Sleep ${SLEEP} sec"
7385         local SEC=0
7386         while ((SEC<$SLEEP)); do
7387                 echo -n "..."
7388                 sleep 5
7389                 SEC=$((SEC+5))
7390                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7391                 echo -n "$LRU_SIZE"
7392         done
7393         echo ""
7394         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7395         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7396
7397         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7398                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7399                 unlinkmany $DIR/$tdir/f $NR
7400                 return
7401         }
7402
7403         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7404         log "unlink $NR files at $DIR/$tdir"
7405         unlinkmany $DIR/$tdir/f $NR
7406 }
7407 run_test 124a "lru resize ======================================="
7408
7409 get_max_pool_limit()
7410 {
7411         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7412         local max=0
7413         for l in $limit; do
7414                 if test $l -gt $max; then
7415                         max=$l
7416                 fi
7417         done
7418         echo $max
7419 }
7420
7421 test_124b() {
7422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7423         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7424                skip "no lru resize on server" && return 0
7425
7426         LIMIT=`get_max_pool_limit`
7427
7428         NR=$(($(default_lru_size)*20))
7429         if [ $NR -gt $LIMIT ]; then
7430                 log "Limit lock number by $LIMIT locks"
7431                 NR=$LIMIT
7432         fi
7433         lru_resize_disable mdc
7434         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7435                 error "failed to create $DIR/$tdir/disable_lru_resize"
7436
7437         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7438         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7439         cancel_lru_locks mdc
7440         stime=`date +%s`
7441         PID=""
7442         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7443         PID="$PID $!"
7444         sleep 2
7445         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7446         PID="$PID $!"
7447         sleep 2
7448         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7449         PID="$PID $!"
7450         wait $PID
7451         etime=`date +%s`
7452         nolruresize_delta=$((etime-stime))
7453         log "ls -la time: $nolruresize_delta seconds"
7454         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7455         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7456
7457         lru_resize_enable mdc
7458         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7459                 error "failed to create $DIR/$tdir/enable_lru_resize"
7460
7461         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7462         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7463         cancel_lru_locks mdc
7464         stime=`date +%s`
7465         PID=""
7466         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7467         PID="$PID $!"
7468         sleep 2
7469         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7470         PID="$PID $!"
7471         sleep 2
7472         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7473         PID="$PID $!"
7474         wait $PID
7475         etime=`date +%s`
7476         lruresize_delta=$((etime-stime))
7477         log "ls -la time: $lruresize_delta seconds"
7478         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7479
7480         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7481                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7482         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7483                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7484         else
7485                 log "lru resize performs the same with no lru resize"
7486         fi
7487         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7488 }
7489 run_test 124b "lru resize (performance test) ======================="
7490
7491 test_125() { # 13358
7492         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7493         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7494         test_mkdir -p $DIR/d125 || error "mkdir failed"
7495         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7496         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7497         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7498 }
7499 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7500
7501 test_126() { # bug 12829/13455
7502         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7503         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7504         $GSS && skip "must run as gss disabled" && return
7505
7506         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7507         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7508         rm -f $DIR/$tfile
7509         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7510 }
7511 run_test 126 "check that the fsgid provided by the client is taken into account"
7512
7513 test_127a() { # bug 15521
7514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7515         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7516         $LCTL set_param osc.*.stats=0
7517         FSIZE=$((2048 * 1024))
7518         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7519         cancel_lru_locks osc
7520         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7521
7522         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7523         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7524                 echo "got $COUNT $NAME"
7525                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7526                 eval $NAME=$COUNT || error "Wrong proc format"
7527
7528                 case $NAME in
7529                         read_bytes|write_bytes)
7530                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7531                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7532                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7533                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7534                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7535                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7536                                 error "sumsquare is too small: $SUMSQ"
7537                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7538                                 error "sumsquare is too big: $SUMSQ"
7539                         ;;
7540                         *) ;;
7541                 esac
7542         done < $DIR/${tfile}.tmp
7543
7544         #check that we actually got some stats
7545         [ "$read_bytes" ] || error "Missing read_bytes stats"
7546         [ "$write_bytes" ] || error "Missing write_bytes stats"
7547         [ "$read_bytes" != 0 ] || error "no read done"
7548         [ "$write_bytes" != 0 ] || error "no write done"
7549 }
7550 run_test 127a "verify the client stats are sane"
7551
7552 test_127b() { # bug LU-333
7553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7554         $LCTL set_param llite.*.stats=0
7555         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7556         # perform 2 reads and writes so MAX is different from SUM.
7557         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7558         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7559         cancel_lru_locks osc
7560         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7561         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7562
7563         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7564         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7565                 echo "got $COUNT $NAME"
7566                 eval $NAME=$COUNT || error "Wrong proc format"
7567
7568         case $NAME in
7569                 read_bytes)
7570                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7571                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7572                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7573                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7574                         ;;
7575                 write_bytes)
7576                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7577                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7578                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7579                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7580                         ;;
7581                         *) ;;
7582                 esac
7583         done < $TMP/${tfile}.tmp
7584
7585         #check that we actually got some stats
7586         [ "$read_bytes" ] || error "Missing read_bytes stats"
7587         [ "$write_bytes" ] || error "Missing write_bytes stats"
7588         [ "$read_bytes" != 0 ] || error "no read done"
7589         [ "$write_bytes" != 0 ] || error "no write done"
7590 }
7591 run_test 127b "verify the llite client stats are sane"
7592
7593 test_128() { # bug 15212
7594         touch $DIR/$tfile
7595         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7596                 find $DIR/$tfile
7597                 find $DIR/$tfile
7598         EOF
7599
7600         result=$(grep error $TMP/$tfile.log)
7601         rm -f $DIR/$tfile
7602         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7603 }
7604 run_test 128 "interactive lfs for 2 consecutive find's"
7605
7606 set_dir_limits () {
7607         local mntdev
7608         local canondev
7609         local node
7610
7611         local LDPROC=/proc/fs/ldiskfs
7612         local facets=$(get_facets MDS)
7613
7614         for facet in ${facets//,/ }; do
7615                 canondev=$(ldiskfs_canon \
7616                            *.$(convert_facet2label $facet).mntdev $facet)
7617                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7618                                                 LDPROC=/sys/fs/ldiskfs
7619                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7620         done
7621 }
7622
7623 test_129() {
7624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7625         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7626                 skip "Only applicable to ldiskfs-based MDTs"
7627                 return
7628         fi
7629         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7630
7631         EFBIG=27
7632         MAX=16384
7633
7634         set_dir_limits $MAX
7635         test_mkdir -p $DIR/$tdir
7636
7637         local I=0
7638         local J=0
7639         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7640                 $MULTIOP $DIR/$tdir/$J Oc
7641                 rc=$?
7642                 if [ $rc -eq $EFBIG ]; then
7643                         set_dir_limits 0
7644                         echo "return code $rc received as expected"
7645                         return 0
7646                 elif [ $rc -ne 0 ]; then
7647                         set_dir_limits 0
7648                         error_exit "return code $rc received instead of expected $EFBIG"
7649                 fi
7650                 J=$((J+1))
7651                 I=$(stat -c%s "$DIR/$tdir")
7652         done
7653
7654         set_dir_limits 0
7655         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7656 }
7657 run_test 129 "test directory size limit ========================"
7658
7659 OLDIFS="$IFS"
7660 cleanup_130() {
7661         trap 0
7662         IFS="$OLDIFS"
7663 }
7664
7665 test_130a() {
7666         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7667         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7668                 return
7669
7670         trap cleanup_130 EXIT RETURN
7671
7672         local fm_file=$DIR/$tfile
7673         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7674         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7675                 error "dd failed for $fm_file"
7676
7677         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7678         filefrag -ves $fm_file
7679         RC=$?
7680         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7681                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7682         [ $RC != 0 ] && error "filefrag $fm_file failed"
7683
7684         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7685                       grep -v "ext:" | grep -v "found")
7686         lun=$($GETSTRIPE -i $fm_file)
7687
7688         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7689         IFS=$'\n'
7690         tot_len=0
7691         for line in $filefrag_op
7692         do
7693                 frag_lun=`echo $line | cut -d: -f5`
7694                 ext_len=`echo $line | cut -d: -f4`
7695                 if (( $frag_lun != $lun )); then
7696                         cleanup_130
7697                         error "FIEMAP on 1-stripe file($fm_file) failed"
7698                         return
7699                 fi
7700                 (( tot_len += ext_len ))
7701         done
7702
7703         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7704                 cleanup_130
7705                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7706                 return
7707         fi
7708
7709         cleanup_130
7710
7711         echo "FIEMAP on single striped file succeeded"
7712 }
7713 run_test 130a "FIEMAP (1-stripe file)"
7714
7715 test_130b() {
7716         [ "$OSTCOUNT" -lt "2" ] &&
7717                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7718
7719         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7720         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7721                 return
7722
7723         trap cleanup_130 EXIT RETURN
7724
7725         local fm_file=$DIR/$tfile
7726         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7727         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7728                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7729
7730         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7731                 error "dd failed on $fm_file"
7732
7733         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7734         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7735                       grep -v "ext:" | grep -v "found")
7736
7737         last_lun=$(echo $filefrag_op | cut -d: -f5)
7738
7739         IFS=$'\n'
7740         tot_len=0
7741         num_luns=1
7742         for line in $filefrag_op
7743         do
7744                 frag_lun=`echo $line | cut -d: -f5`
7745                 ext_len=`echo $line | cut -d: -f4`
7746                 if (( $frag_lun != $last_lun )); then
7747                         if (( tot_len != 1024 )); then
7748                                 cleanup_130
7749                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7750                                 return
7751                         else
7752                                 (( num_luns += 1 ))
7753                                 tot_len=0
7754                         fi
7755                 fi
7756                 (( tot_len += ext_len ))
7757                 last_lun=$frag_lun
7758         done
7759         if (( num_luns != 2 || tot_len != 1024 )); then
7760                 cleanup_130
7761                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7762                 return
7763         fi
7764
7765         cleanup_130
7766
7767         echo "FIEMAP on 2-stripe file succeeded"
7768 }
7769 run_test 130b "FIEMAP (2-stripe file)"
7770
7771 test_130c() {
7772         [ "$OSTCOUNT" -lt "2" ] &&
7773                 skip_env "skipping FIEMAP on 2-stripe file" && return
7774
7775         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7776         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7777                 return
7778
7779         trap cleanup_130 EXIT RETURN
7780
7781         local fm_file=$DIR/$tfile
7782         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7783         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7784                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7785
7786         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7787
7788         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7789         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7790
7791         last_lun=`echo $filefrag_op | cut -d: -f5`
7792
7793         IFS=$'\n'
7794         tot_len=0
7795         num_luns=1
7796         for line in $filefrag_op
7797         do
7798                 frag_lun=`echo $line | cut -d: -f5`
7799                 ext_len=`echo $line | cut -d: -f4`
7800                 if (( $frag_lun != $last_lun )); then
7801                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7802                         if (( logical != 512 )); then
7803                                 cleanup_130
7804                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7805                                 return
7806                         fi
7807                         if (( tot_len != 512 )); then
7808                                 cleanup_130
7809                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7810                                 return
7811                         else
7812                                 (( num_luns += 1 ))
7813                                 tot_len=0
7814                         fi
7815                 fi
7816                 (( tot_len += ext_len ))
7817                 last_lun=$frag_lun
7818         done
7819         if (( num_luns != 2 || tot_len != 512 )); then
7820                 cleanup_130
7821                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7822                 return
7823         fi
7824
7825         cleanup_130
7826
7827         echo "FIEMAP on 2-stripe file with hole succeeded"
7828 }
7829 run_test 130c "FIEMAP (2-stripe file with hole)"
7830
7831 test_130d() {
7832         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7833
7834         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7835         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7836
7837         trap cleanup_130 EXIT RETURN
7838
7839         local fm_file=$DIR/$tfile
7840         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7841         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7842                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7843         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7844
7845         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7846         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7847
7848         last_lun=`echo $filefrag_op | cut -d: -f5`
7849
7850         IFS=$'\n'
7851         tot_len=0
7852         num_luns=1
7853         for line in $filefrag_op
7854         do
7855                 frag_lun=`echo $line | cut -d: -f5`
7856                 ext_len=`echo $line | cut -d: -f4`
7857                 if (( $frag_lun != $last_lun )); then
7858                         if (( tot_len != 1024 )); then
7859                                 cleanup_130
7860                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7861                                 return
7862                         else
7863                                 (( num_luns += 1 ))
7864                                 tot_len=0
7865                         fi
7866                 fi
7867                 (( tot_len += ext_len ))
7868                 last_lun=$frag_lun
7869         done
7870         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7871                 cleanup_130
7872                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7873                 return
7874         fi
7875
7876         cleanup_130
7877
7878         echo "FIEMAP on N-stripe file succeeded"
7879 }
7880 run_test 130d "FIEMAP (N-stripe file)"
7881
7882 test_130e() {
7883         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7884
7885         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7886         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7887
7888         trap cleanup_130 EXIT RETURN
7889
7890         local fm_file=$DIR/$tfile
7891         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7892         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7893                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7894
7895         NUM_BLKS=512
7896         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7897         for ((i = 0; i < $NUM_BLKS; i++))
7898         do
7899                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7900         done
7901
7902         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7903         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7904
7905         last_lun=`echo $filefrag_op | cut -d: -f5`
7906
7907         IFS=$'\n'
7908         tot_len=0
7909         num_luns=1
7910         for line in $filefrag_op
7911         do
7912                 frag_lun=`echo $line | cut -d: -f5`
7913                 ext_len=`echo $line | cut -d: -f4`
7914                 if (( $frag_lun != $last_lun )); then
7915                         if (( tot_len != $EXPECTED_LEN )); then
7916                                 cleanup_130
7917                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7918                                 return
7919                         else
7920                                 (( num_luns += 1 ))
7921                                 tot_len=0
7922                         fi
7923                 fi
7924                 (( tot_len += ext_len ))
7925                 last_lun=$frag_lun
7926         done
7927         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7928                 cleanup_130
7929                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7930                 return
7931         fi
7932
7933         cleanup_130
7934
7935         echo "FIEMAP with continuation calls succeeded"
7936 }
7937 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7938
7939 # Test for writev/readv
7940 test_131a() {
7941         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7942         error "writev test failed"
7943         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7944         error "readv failed"
7945         rm -f $DIR/$tfile
7946 }
7947 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7948
7949 test_131b() {
7950         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7951         error "append writev test failed"
7952         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7953         error "append writev test failed"
7954         rm -f $DIR/$tfile
7955 }
7956 run_test 131b "test append writev"
7957
7958 test_131c() {
7959         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7960         error "NOT PASS"
7961 }
7962 run_test 131c "test read/write on file w/o objects"
7963
7964 test_131d() {
7965         rwv -f $DIR/$tfile -w -n 1 1572864
7966         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7967         if [ "$NOB" != 1572864 ]; then
7968                 error "Short read filed: read $NOB bytes instead of 1572864"
7969         fi
7970         rm -f $DIR/$tfile
7971 }
7972 run_test 131d "test short read"
7973
7974 test_131e() {
7975         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7976         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7977         error "read hitting hole failed"
7978         rm -f $DIR/$tfile
7979 }
7980 run_test 131e "test read hitting hole"
7981
7982 get_ost_param() {
7983         local token=$1
7984         local gl_sum=0
7985         for node in $(osts_nodes); do
7986                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7987                 [ x$gl = x"" ] && gl=0
7988                 gl_sum=$((gl_sum + gl))
7989         done
7990         echo $gl_sum
7991 }
7992
7993 som_mode_switch() {
7994         local som=$1
7995         local gl1=$2
7996         local gl2=$3
7997
7998         if [ x$som = x"enabled" ]; then
7999                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8000                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8001                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8002         else
8003                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8004                 MOUNTOPT="$MOUNTOPT,som_preview"
8005                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8006         fi
8007
8008         # do remount to make new mount-conf parameters actual
8009         echo remounting...
8010         sync
8011         stopall
8012         setupall
8013 }
8014
8015 test_132() { #1028, SOM
8016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8017         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8018         local num=$(get_mds_dir $DIR)
8019         local mymds=mds${num}
8020         local MOUNTOPT_SAVE=$MOUNTOPT
8021
8022         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8023         cancel_lru_locks osc
8024
8025         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8026
8027         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8028         stat $DIR/$tfile >/dev/null
8029         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8030         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8031         rm $DIR/$tfile
8032         som_mode_switch $som1 $gl1 $gl2
8033
8034         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8035         cancel_lru_locks osc
8036
8037         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8038         if [ $som1 == $som2 ]; then
8039             error "som is still "$som2
8040             if [ x$som2 = x"enabled" ]; then
8041                 som2="disabled"
8042             else
8043                 som2="enabled"
8044             fi
8045         fi
8046
8047         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8048         stat $DIR/$tfile >/dev/null
8049         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8050         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8051         som_mode_switch $som2 $gl1 $gl2
8052         MOUNTOPT=$MOUNTOPT_SAVE
8053 }
8054 run_test 132 "som avoids glimpse rpc"
8055
8056 check_stats() {
8057         local res
8058         local count
8059         case $1 in
8060         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8061                  ;;
8062         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8063                  ;;
8064         *) error "Wrong argument $1" ;;
8065         esac
8066         echo $res
8067         count=`echo $res | awk '{print $2}'`
8068         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8069         # if the argument $3 is zero, it means any stat increment is ok.
8070         if [ $3 -gt 0 ] ; then
8071                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
8072         fi
8073 }
8074
8075 test_133a() {
8076         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8077         remote_ost_nodsh && skip "remote OST with nodsh" && return
8078         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8079
8080         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8081                 { skip "MDS doesn't support rename stats"; return; }
8082         local testdir=$DIR/${tdir}/stats_testdir
8083         mkdir -p $DIR/${tdir}
8084
8085         # clear stats.
8086         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8087         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8088
8089         # verify mdt stats first.
8090         mkdir ${testdir} || error "mkdir failed"
8091         check_stats $SINGLEMDS "mkdir" 1
8092         touch ${testdir}/${tfile} || "touch failed"
8093         check_stats $SINGLEMDS "open" 1
8094         check_stats $SINGLEMDS "close" 1
8095         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8096         check_stats $SINGLEMDS "mknod" 1
8097         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8098         check_stats $SINGLEMDS "unlink" 1
8099         rm -f ${testdir}/${tfile} || error "file remove failed"
8100         check_stats $SINGLEMDS "unlink" 2
8101
8102         # remove working dir and check mdt stats again.
8103         rmdir ${testdir} || error "rmdir failed"
8104         check_stats $SINGLEMDS "rmdir" 1
8105
8106         local testdir1=$DIR/${tdir}/stats_testdir1
8107         mkdir -p ${testdir}
8108         mkdir -p ${testdir1}
8109         touch ${testdir1}/test1
8110         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8111         check_stats $SINGLEMDS "crossdir_rename" 1
8112
8113         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8114         check_stats $SINGLEMDS "samedir_rename" 1
8115
8116         rm -rf $DIR/${tdir}
8117 }
8118 run_test 133a "Verifying MDT stats ========================================"
8119
8120 test_133b() {
8121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8122         remote_ost_nodsh && skip "remote OST with nodsh" && return
8123         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8124         local testdir=$DIR/${tdir}/stats_testdir
8125         mkdir -p ${testdir} || error "mkdir failed"
8126         touch ${testdir}/${tfile} || "touch failed"
8127         cancel_lru_locks mdc
8128
8129         # clear stats.
8130         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8131         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8132
8133         # extra mdt stats verification.
8134         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8135         check_stats $SINGLEMDS "setattr" 1
8136         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8137         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8138         then            # LU-1740
8139                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8140                 check_stats $SINGLEMDS "getattr" 1
8141         fi
8142         $LFS df || error "lfs failed"
8143         check_stats $SINGLEMDS "statfs" 1
8144
8145         rm -rf $DIR/${tdir}
8146 }
8147 run_test 133b "Verifying extra MDT stats =================================="
8148
8149 test_133c() {
8150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8151         remote_ost_nodsh && skip "remote OST with nodsh" && return
8152         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8153         local testdir=$DIR/${tdir}/stats_testdir
8154         test_mkdir -p ${testdir} || error "mkdir failed"
8155
8156         # verify obdfilter stats.
8157         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8158         sync
8159         cancel_lru_locks osc
8160         wait_delete_completed
8161
8162         # clear stats.
8163         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8164         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8165
8166         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8167         sync
8168         cancel_lru_locks osc
8169         check_stats ost "write" 1
8170
8171         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8172         check_stats ost "read" 1
8173
8174         > ${testdir}/${tfile} || error "truncate failed"
8175         check_stats ost "punch" 1
8176
8177         rm -f ${testdir}/${tfile} || error "file remove failed"
8178         wait_delete_completed
8179         check_stats ost "destroy" 1
8180
8181         rm -rf $DIR/${tdir}
8182 }
8183 run_test 133c "Verifying OST stats ========================================"
8184
8185 order_2() {
8186     local value=$1
8187     local orig=$value
8188     local order=1
8189
8190     while [ $value -ge 2 ]; do
8191         order=$((order*2))
8192         value=$((value/2))
8193     done
8194
8195     if [ $orig -gt $order ]; then
8196         order=$((order*2))
8197     fi
8198     echo $order
8199 }
8200
8201 size_in_KMGT() {
8202     local value=$1
8203     local size=('K' 'M' 'G' 'T');
8204     local i=0
8205     local size_string=$value
8206
8207     while [ $value -ge 1024 ]; do
8208         if [ $i -gt 3 ]; then
8209             #T is the biggest unit we get here, if that is bigger,
8210             #just return XXXT
8211             size_string=${value}T
8212             break
8213         fi
8214         value=$((value >> 10))
8215         if [ $value -lt 1024 ]; then
8216             size_string=${value}${size[$i]}
8217             break
8218         fi
8219         i=$((i + 1))
8220     done
8221
8222     echo $size_string
8223 }
8224
8225 get_rename_size() {
8226     local size=$1
8227     local context=${2:-.}
8228     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8229                 grep -A1 $context |
8230                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8231     echo $sample
8232 }
8233
8234 test_133d() {
8235         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8236     remote_ost_nodsh && skip "remote OST with nodsh" && return
8237     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8238     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8239         { skip "MDS doesn't support rename stats"; return; }
8240
8241     local testdir1=$DIR/${tdir}/stats_testdir1
8242     local testdir2=$DIR/${tdir}/stats_testdir2
8243
8244     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8245
8246     test_mkdir -p ${testdir1} || error "mkdir failed"
8247     test_mkdir -p ${testdir2} || error "mkdir failed"
8248
8249     createmany -o $testdir1/test 512 || error "createmany failed"
8250
8251         # check samedir rename size
8252         mv ${testdir1}/test0 ${testdir1}/test_0
8253
8254         local testdir1_size=$(ls -l $DIR/${tdir} |
8255                 awk '/stats_testdir1/ {print $5}')
8256         local testdir2_size=$(ls -l $DIR/${tdir} |
8257                 awk '/stats_testdir2/ {print $5}')
8258
8259         testdir1_size=$(order_2 $testdir1_size)
8260         testdir2_size=$(order_2 $testdir2_size)
8261
8262         testdir1_size=$(size_in_KMGT $testdir1_size)
8263         testdir2_size=$(size_in_KMGT $testdir2_size)
8264
8265         echo "source rename dir size: ${testdir1_size}"
8266         echo "target rename dir size: ${testdir2_size}"
8267
8268     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8269     eval $cmd || error "$cmd failed"
8270     local samedir=$($cmd | grep 'same_dir')
8271     local same_sample=$(get_rename_size $testdir1_size)
8272     [ -z "$samedir" ] && error "samedir_rename_size count error"
8273     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8274     echo "Check same dir rename stats success"
8275
8276     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8277
8278     # check crossdir rename size
8279     mv ${testdir1}/test_0 ${testdir2}/test_0
8280
8281         testdir1_size=$(ls -l $DIR/${tdir} |
8282                 awk '/stats_testdir1/ {print $5}')
8283         testdir2_size=$(ls -l $DIR/${tdir} |
8284                 awk '/stats_testdir2/ {print $5}')
8285
8286         testdir1_size=$(order_2 $testdir1_size)
8287         testdir2_size=$(order_2 $testdir2_size)
8288
8289         testdir1_size=$(size_in_KMGT $testdir1_size)
8290         testdir2_size=$(size_in_KMGT $testdir2_size)
8291
8292         echo "source rename dir size: ${testdir1_size}"
8293         echo "target rename dir size: ${testdir2_size}"
8294
8295     eval $cmd || error "$cmd failed"
8296     local crossdir=$($cmd | grep 'crossdir')
8297     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8298     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8299     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8300     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8301     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8302     echo "Check cross dir rename stats success"
8303     rm -rf $DIR/${tdir}
8304 }
8305 run_test 133d "Verifying rename_stats ========================================"
8306
8307 test_133e() {
8308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8309         local testdir=$DIR/${tdir}/stats_testdir
8310         local ctr f0 f1 bs=32768 count=42 sum
8311
8312         remote_ost_nodsh && skip "remote OST with nodsh" && return
8313         mkdir -p ${testdir} || error "mkdir failed"
8314
8315         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8316
8317         for ctr in {write,read}_bytes; do
8318                 sync
8319                 cancel_lru_locks osc
8320
8321                 do_facet ost1 $LCTL set_param -n \
8322                         "obdfilter.*.exports.clear=clear"
8323
8324                 if [ $ctr = write_bytes ]; then
8325                         f0=/dev/zero
8326                         f1=${testdir}/${tfile}
8327                 else
8328                         f0=${testdir}/${tfile}
8329                         f1=/dev/null
8330                 fi
8331
8332                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8333                         error "dd failed"
8334                 sync
8335                 cancel_lru_locks osc
8336
8337                 sum=$(do_facet ost1 $LCTL get_param \
8338                                 "obdfilter.*.exports.*.stats" | \
8339                           awk -v ctr=$ctr '\
8340                                 BEGIN { sum = 0 }
8341                                 $1 == ctr { sum += $7 }
8342                                 END { print sum }')
8343
8344                 if ((sum != bs * count)); then
8345                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8346                 fi
8347         done
8348
8349         rm -rf $DIR/${tdir}
8350 }
8351 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8352
8353 test_133f() {
8354         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8355         local facet
8356
8357         # First without trusting modes.
8358         find $proc_dirs \
8359                 -exec cat '{}' \; &> /dev/null
8360
8361         # Second verifying readability.
8362         find $proc_dirs \
8363                 -type f \
8364                 -readable \
8365                 -exec cat '{}' \; > /dev/null ||
8366                         error "proc file read failed"
8367
8368         for facet in $SINGLEMDS ost1; do
8369                 do_facet $facet find $proc_dirs \
8370                         -not -name req_history \
8371                         -exec cat '{}' \\\; &> /dev/null
8372
8373             do_facet $facet     find $proc_dirs \
8374                         -not -name req_history \
8375                         -type f \
8376                         -readable \
8377                         -exec cat '{}' \\\; > /dev/null ||
8378                                 error "proc file read failed"
8379         done
8380 }
8381 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8382
8383 test_140() { #bug-17379
8384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8385         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8386         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8387         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8388
8389         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8390         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8391         local i=0
8392         while i=`expr $i + 1`; do
8393                 test_mkdir -p $i || error "Creating dir $i"
8394                 cd $i || error "Changing to $i"
8395                 ln -s ../stat stat || error "Creating stat symlink"
8396                 # Read the symlink until ELOOP present,
8397                 # not LBUGing the system is considered success,
8398                 # we didn't overrun the stack.
8399                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8400                 [ $ret -ne 0 ] && {
8401                         if [ $ret -eq 40 ]; then
8402                                 break  # -ELOOP
8403                         else
8404                                 error "Open stat symlink"
8405                                 return
8406                         fi
8407                 }
8408         done
8409         i=`expr $i - 1`
8410         echo "The symlink depth = $i"
8411         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8412                                         error "Invalid symlink depth"
8413
8414         # Test recursive symlink
8415         ln -s symlink_self symlink_self
8416         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8417         echo "open symlink_self returns $ret"
8418         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8419 }
8420 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8421
8422 test_150() {
8423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8424         local TF="$TMP/$tfile"
8425
8426         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8427         cp $TF $DIR/$tfile
8428         cancel_lru_locks osc
8429         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8430         remount_client $MOUNT
8431         df -P $MOUNT
8432         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8433
8434         $TRUNCATE $TF 6000
8435         $TRUNCATE $DIR/$tfile 6000
8436         cancel_lru_locks osc
8437         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8438
8439         echo "12345" >>$TF
8440         echo "12345" >>$DIR/$tfile
8441         cancel_lru_locks osc
8442         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8443
8444         echo "12345" >>$TF
8445         echo "12345" >>$DIR/$tfile
8446         cancel_lru_locks osc
8447         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8448
8449         rm -f $TF
8450         true
8451 }
8452 run_test 150 "truncate/append tests"
8453
8454 function roc_hit() {
8455         local list=$(comma_list $(osts_nodes))
8456         #debug temp debug for LU-2902: lets see what values we get back
8457         echo $(get_osd_param $list '' stats) 1>&2
8458         echo $(get_osd_param $list '' stats |
8459                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8460 }
8461
8462 function set_cache() {
8463         local on=1
8464
8465         if [ "$2" == "off" ]; then
8466                 on=0;
8467         fi
8468         local list=$(comma_list $(osts_nodes))
8469         set_osd_param $list '' $1_cache_enable $on
8470
8471         cancel_lru_locks osc
8472 }
8473
8474 test_151() {
8475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8476         remote_ost_nodsh && skip "remote OST with nodsh" && return
8477
8478         local CPAGES=3
8479         local list=$(comma_list $(osts_nodes))
8480
8481         # check whether obdfilter is cache capable at all
8482         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8483                 echo "not cache-capable obdfilter"
8484                 return 0
8485         fi
8486
8487         # check cache is enabled on all obdfilters
8488         if get_osd_param $list '' read_cache_enable | grep 0; then
8489                 echo "oss cache is disabled"
8490                 return 0
8491         fi
8492
8493         set_osd_param $list '' writethrough_cache_enable 1
8494
8495         # check write cache is enabled on all obdfilters
8496         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
8497                 echo "oss write cache is NOT enabled"
8498                 return 0
8499         fi
8500
8501 #define OBD_FAIL_OBD_NO_LRU  0x609
8502         do_nodes $list $LCTL set_param fail_loc=0x609
8503
8504         # pages should be in the case right after write
8505         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
8506                 error "dd failed"
8507
8508         local BEFORE=`roc_hit`
8509         cancel_lru_locks osc
8510         cat $DIR/$tfile >/dev/null
8511         local AFTER=`roc_hit`
8512
8513         do_nodes $list $LCTL set_param fail_loc=0
8514
8515         if ! let "AFTER - BEFORE == CPAGES"; then
8516                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8517         fi
8518
8519         # the following read invalidates the cache
8520         cancel_lru_locks osc
8521         set_osd_param $list '' read_cache_enable 0
8522         cat $DIR/$tfile >/dev/null
8523
8524         # now data shouldn't be found in the cache
8525         BEFORE=`roc_hit`
8526         cancel_lru_locks osc
8527         cat $DIR/$tfile >/dev/null
8528         AFTER=`roc_hit`
8529         if let "AFTER - BEFORE != 0"; then
8530                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8531         fi
8532
8533         set_osd_param $list '' read_cache_enable 1
8534         rm -f $DIR/$tfile
8535 }
8536 run_test 151 "test cache on oss and controls ==============================="
8537
8538 test_152() {
8539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8540         local TF="$TMP/$tfile"
8541
8542         # simulate ENOMEM during write
8543 #define OBD_FAIL_OST_NOMEM      0x226
8544         lctl set_param fail_loc=0x80000226
8545         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8546         cp $TF $DIR/$tfile
8547         sync || error "sync failed"
8548         lctl set_param fail_loc=0
8549
8550         # discard client's cache
8551         cancel_lru_locks osc
8552
8553         # simulate ENOMEM during read
8554         lctl set_param fail_loc=0x80000226
8555         cmp $TF $DIR/$tfile || error "cmp failed"
8556         lctl set_param fail_loc=0
8557
8558         rm -f $TF
8559 }
8560 run_test 152 "test read/write with enomem ============================"
8561
8562 test_153() {
8563         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8564 }
8565 run_test 153 "test if fdatasync does not crash ======================="
8566
8567 dot_lustre_fid_permission_check() {
8568         local fid=$1
8569         local ffid=$MOUNT/.lustre/fid/$fid
8570         local test_dir=$2
8571
8572         echo "stat fid $fid"
8573         stat $ffid > /dev/null || error "stat $ffid failed."
8574         echo "touch fid $fid"
8575         touch $ffid || error "touch $ffid failed."
8576         echo "write to fid $fid"
8577         cat /etc/hosts > $ffid || error "write $ffid failed."
8578         echo "read fid $fid"
8579         diff /etc/hosts $ffid || error "read $ffid failed."
8580         echo "append write to fid $fid"
8581         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8582         echo "rename fid $fid"
8583         mv $ffid $test_dir/$tfile.1 &&
8584                 error "rename $ffid to $tfile.1 should fail."
8585         touch $test_dir/$tfile.1
8586         mv $test_dir/$tfile.1 $ffid &&
8587                 error "rename $tfile.1 to $ffid should fail."
8588         rm -f $test_dir/$tfile.1
8589         echo "truncate fid $fid"
8590         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8591         echo "link fid $fid"
8592         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
8593         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8594                 echo "setfacl fid $fid"
8595                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8596                 echo "getfacl fid $fid"
8597                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8598         fi
8599         echo "unlink fid $fid"
8600         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
8601         echo "mknod fid $fid"
8602         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8603
8604         fid=[0xf00000400:0x1:0x0]
8605         ffid=$MOUNT/.lustre/fid/$fid
8606
8607         echo "stat non-exist fid $fid"
8608         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8609         echo "write to non-exist fid $fid"
8610         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8611         echo "link new fid $fid"
8612         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
8613
8614         mkdir -p $test_dir/$tdir
8615         touch $test_dir/$tdir/$tfile
8616         fid=$($LFS path2fid $test_dir/$tdir)
8617         rc=$?
8618         [ $rc -ne 0 ] &&
8619                 error "error: could not get fid for $test_dir/$dir/$tfile."
8620
8621         ffid=$MOUNT/.lustre/fid/$fid
8622
8623         echo "ls $fid"
8624         ls $ffid > /dev/null || error "ls $ffid failed."
8625         echo "touch $fid/$tfile.1"
8626         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8627
8628         echo "touch $MOUNT/.lustre/fid/$tfile"
8629         touch $MOUNT/.lustre/fid/$tfile && \
8630                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
8631
8632         echo "setxattr to $MOUNT/.lustre/fid"
8633         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid &&
8634                 error "setxattr should fail."
8635
8636         echo "listxattr for $MOUNT/.lustre/fid"
8637         getfattr -d -m "^trusted" $MOUNT/.lustre/fid &&
8638                 error "listxattr should fail."
8639
8640         echo "delxattr from $MOUNT/.lustre/fid"
8641         setfattr -x trusted.name1 $MOUNT/.lustre/fid &&
8642                 error "delxattr should fail."
8643
8644         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
8645         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
8646                 error "touch invalid fid should fail."
8647
8648         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
8649         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
8650                 error "touch non-normal fid should fail."
8651
8652         echo "rename $tdir to $MOUNT/.lustre/fid"
8653         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
8654                 error "rename to $MOUNT/.lustre/fid should fail."
8655
8656         echo "rename .lustre to itself"
8657         fid=$($LFS path2fid $MOUNT)
8658         mrename $MOUNT/.lustre $MOUNT/.lustre/fid/$fid/.lustre &&
8659                 error "rename .lustre to itself should fail."
8660
8661         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
8662         fid=$($LFS path2fid $test_dir/$tfile-2)
8663         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
8664         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
8665                 error "create lov data thru .lustre should fail."
8666         echo "cp /etc/passwd $test_dir/$tfile-2"
8667         cp /etc/passwd $test_dir/$tfile-2 ||
8668                 error "copy to $test_dir/$tfile-2 failed."
8669         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
8670         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
8671                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
8672
8673         rm -rf $test_dir/tfile.lnk
8674         rm -rf $test_dir/$tfile-2
8675 }
8676
8677 test_154a() {
8678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8679         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8680                 { skip "Need MDS version at least 2.2.51"; return 0; }
8681
8682         cp /etc/hosts $DIR/$tfile
8683
8684         fid=$($LFS path2fid $DIR/$tfile)
8685         rc=$?
8686         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8687
8688         dot_lustre_fid_permission_check "$fid" $DIR ||
8689                 error "dot lustre permission check $fid failed"
8690
8691         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
8692         chmod 777 $MOUNT/.lustre && error ".lustre is not allowed to be chmod"
8693
8694         touch $MOUNT/.lustre/file &&
8695                 error "creation is not allowed under .lustre"
8696
8697         mkdir $MOUNT/.lustre/dir &&
8698                 error "mkdir is not allowed under .lustre"
8699
8700         rm -rf $DIR/$tfile
8701 }
8702 run_test 154a "Open-by-FID"
8703
8704 test_154b() {
8705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8706         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8707                 { skip "Need MDS version at least 2.2.51"; return 0; }
8708
8709         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
8710
8711         local remote_dir=$DIR/$tdir/remote_dir
8712         local MDTIDX=1
8713         local rc=0
8714
8715         mkdir -p $DIR/$tdir
8716         $LFS mkdir -i $MDTIDX $remote_dir ||
8717                 error "create remote directory failed"
8718
8719         cp /etc/hosts $remote_dir/$tfile
8720
8721         fid=$($LFS path2fid $remote_dir/$tfile)
8722         rc=$?
8723         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
8724
8725         dot_lustre_fid_permission_check "$fid" $remote_dir ||
8726                 error "dot lustre permission check $fid failed"
8727         rm -rf $DIR/$tdir
8728 }
8729 run_test 154b "Open-by-FID for remote directory"
8730
8731 test_155_small_load() {
8732     local temp=$TMP/$tfile
8733     local file=$DIR/$tfile
8734
8735     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8736         error "dd of=$temp bs=6096 count=1 failed"
8737     cp $temp $file
8738     cancel_lru_locks osc
8739     cmp $temp $file || error "$temp $file differ"
8740
8741     $TRUNCATE $temp 6000
8742     $TRUNCATE $file 6000
8743     cmp $temp $file || error "$temp $file differ (truncate1)"
8744
8745     echo "12345" >>$temp
8746     echo "12345" >>$file
8747     cmp $temp $file || error "$temp $file differ (append1)"
8748
8749     echo "12345" >>$temp
8750     echo "12345" >>$file
8751     cmp $temp $file || error "$temp $file differ (append2)"
8752
8753     rm -f $temp $file
8754     true
8755 }
8756
8757 test_155_big_load() {
8758     remote_ost_nodsh && skip "remote OST with nodsh" && return
8759     local temp=$TMP/$tfile
8760     local file=$DIR/$tfile
8761
8762     free_min_max
8763     local cache_size=$(do_facet ost$((MAXI+1)) \
8764         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8765     local large_file_size=$((cache_size * 2))
8766
8767     echo "OSS cache size: $cache_size KB"
8768     echo "Large file size: $large_file_size KB"
8769
8770     [ $MAXV -le $large_file_size ] && \
8771         skip_env "max available OST size needs > $large_file_size KB" && \
8772         return 0
8773
8774     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8775
8776     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8777         error "dd of=$temp bs=$large_file_size count=1k failed"
8778     cp $temp $file
8779     ls -lh $temp $file
8780     cancel_lru_locks osc
8781     cmp $temp $file || error "$temp $file differ"
8782
8783     rm -f $temp $file
8784     true
8785 }
8786
8787 test_155a() {
8788         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8789         set_cache read on
8790         set_cache writethrough on
8791         test_155_small_load
8792 }
8793 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8794
8795 test_155b() {
8796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8797         set_cache read on
8798         set_cache writethrough off
8799         test_155_small_load
8800 }
8801 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8802
8803 test_155c() {
8804         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8805         set_cache read off
8806         set_cache writethrough on
8807         test_155_small_load
8808 }
8809 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8810
8811 test_155d() {
8812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8813         set_cache read off
8814         set_cache writethrough off
8815         test_155_small_load
8816 }
8817 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8818
8819 test_155e() {
8820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8821         set_cache read on
8822         set_cache writethrough on
8823         test_155_big_load
8824 }
8825 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8826
8827 test_155f() {
8828         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8829         set_cache read on
8830         set_cache writethrough off
8831         test_155_big_load
8832 }
8833 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8834
8835 test_155g() {
8836         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8837         set_cache read off
8838         set_cache writethrough on
8839         test_155_big_load
8840 }
8841 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8842
8843 test_155h() {
8844         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8845         set_cache read off
8846         set_cache writethrough off
8847         test_155_big_load
8848 }
8849 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8850
8851 test_156() {
8852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8853         local CPAGES=3
8854         local BEFORE
8855         local AFTER
8856         local file="$DIR/$tfile"
8857
8858         [ "$(facet_fstype ost1)" = "zfs" ] &&
8859                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8860                 return
8861
8862     log "Turn on read and write cache"
8863     set_cache read on
8864     set_cache writethrough on
8865
8866     log "Write data and read it back."
8867     log "Read should be satisfied from the cache."
8868     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8869     BEFORE=`roc_hit`
8870     cancel_lru_locks osc
8871     cat $file >/dev/null
8872     AFTER=`roc_hit`
8873     if ! let "AFTER - BEFORE == CPAGES"; then
8874         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8875     else
8876         log "cache hits:: before: $BEFORE, after: $AFTER"
8877     fi
8878
8879     log "Read again; it should be satisfied from the cache."
8880     BEFORE=$AFTER
8881     cancel_lru_locks osc
8882     cat $file >/dev/null
8883     AFTER=`roc_hit`
8884     if ! let "AFTER - BEFORE == CPAGES"; then
8885         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8886     else
8887         log "cache hits:: before: $BEFORE, after: $AFTER"
8888     fi
8889
8890
8891     log "Turn off the read cache and turn on the write cache"
8892     set_cache read off
8893     set_cache writethrough on
8894
8895     log "Read again; it should be satisfied from the cache."
8896     BEFORE=`roc_hit`
8897     cancel_lru_locks osc
8898     cat $file >/dev/null
8899     AFTER=`roc_hit`
8900     if ! let "AFTER - BEFORE == CPAGES"; then
8901         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8902     else
8903         log "cache hits:: before: $BEFORE, after: $AFTER"
8904     fi
8905
8906     log "Read again; it should not be satisfied from the cache."
8907     BEFORE=$AFTER
8908     cancel_lru_locks osc
8909     cat $file >/dev/null
8910     AFTER=`roc_hit`
8911     if ! let "AFTER - BEFORE == 0"; then
8912         error "IN CACHE: before: $BEFORE, after: $AFTER"
8913     else
8914         log "cache hits:: before: $BEFORE, after: $AFTER"
8915     fi
8916
8917     log "Write data and read it back."
8918     log "Read should be satisfied from the cache."
8919     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8920     BEFORE=`roc_hit`
8921     cancel_lru_locks osc
8922     cat $file >/dev/null
8923     AFTER=`roc_hit`
8924     if ! let "AFTER - BEFORE == CPAGES"; then
8925         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8926     else
8927         log "cache hits:: before: $BEFORE, after: $AFTER"
8928     fi
8929
8930     log "Read again; it should not be satisfied from the cache."
8931     BEFORE=$AFTER
8932     cancel_lru_locks osc
8933     cat $file >/dev/null
8934     AFTER=`roc_hit`
8935     if ! let "AFTER - BEFORE == 0"; then
8936         error "IN CACHE: before: $BEFORE, after: $AFTER"
8937     else
8938         log "cache hits:: before: $BEFORE, after: $AFTER"
8939     fi
8940
8941
8942     log "Turn off read and write cache"
8943     set_cache read off
8944     set_cache writethrough off
8945
8946     log "Write data and read it back"
8947     log "It should not be satisfied from the cache."
8948     rm -f $file
8949     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8950     cancel_lru_locks osc
8951     BEFORE=`roc_hit`
8952     cat $file >/dev/null
8953     AFTER=`roc_hit`
8954     if ! let "AFTER - BEFORE == 0"; then
8955         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8956     else
8957         log "cache hits:: before: $BEFORE, after: $AFTER"
8958     fi
8959
8960
8961     log "Turn on the read cache and turn off the write cache"
8962     set_cache read on
8963     set_cache writethrough off
8964
8965     log "Write data and read it back"
8966     log "It should not be satisfied from the cache."
8967     rm -f $file
8968     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8969     BEFORE=`roc_hit`
8970     cancel_lru_locks osc
8971     cat $file >/dev/null
8972     AFTER=`roc_hit`
8973     if ! let "AFTER - BEFORE == 0"; then
8974         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8975     else
8976         log "cache hits:: before: $BEFORE, after: $AFTER"
8977     fi
8978
8979     log "Read again; it should be satisfied from the cache."
8980     BEFORE=`roc_hit`
8981     cancel_lru_locks osc
8982     cat $file >/dev/null
8983     AFTER=`roc_hit`
8984     if ! let "AFTER - BEFORE == CPAGES"; then
8985         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8986     else
8987         log "cache hits:: before: $BEFORE, after: $AFTER"
8988     fi
8989
8990     rm -f $file
8991 }
8992 run_test 156 "Verification of tunables ============================"
8993
8994 #Changelogs
8995 err17935 () {
8996     if [ $MDSCOUNT -gt 1 ]; then
8997         error_ignore 17935 $*
8998     else
8999         error $*
9000     fi
9001 }
9002
9003 changelog_chmask()
9004 {
9005     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
9006            grep -c $1)
9007
9008     if [ $MASK -eq 1 ]; then
9009         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
9010     else
9011         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
9012     fi
9013 }
9014
9015 test_160() {
9016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9017     remote_mds_nodsh && skip "remote MDS with nodsh" && return
9018     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9019         { skip "Need MDS version at least 2.2.0"; return; }
9020     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9021     echo "Registered as changelog user $USER"
9022     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
9023         grep -q $USER || error "User $USER not found in changelog_users"
9024
9025     # change something
9026     test_mkdir -p $DIR/$tdir/pics/2008/zachy
9027     touch $DIR/$tdir/pics/2008/zachy/timestamp
9028     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9029     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9030     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9031     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9032     rm $DIR/$tdir/pics/desktop.jpg
9033
9034     $LFS changelog $MDT0 | tail -5
9035
9036     echo "verifying changelog mask"
9037     changelog_chmask "MKDIR"
9038     changelog_chmask "CLOSE"
9039
9040     test_mkdir -p $DIR/$tdir/pics/zach/sofia
9041     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9042
9043     changelog_chmask "MKDIR"
9044     changelog_chmask "CLOSE"
9045
9046     test_mkdir -p $DIR/$tdir/pics/2008/sofia
9047     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9048
9049     $LFS changelog $MDT0
9050     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9051     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9052     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9053     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9054
9055     # verify contents
9056     echo "verifying target fid"
9057     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
9058         tail -1 | awk '{print $6}')
9059     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9060     [ "$fidc" == "t=$fidf" ] || \
9061         err17935 "fid in changelog $fidc != file fid $fidf"
9062     echo "verifying parent fid"
9063     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
9064         tail -1 | awk '{print $7}')
9065     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9066     [ "$fidc" == "p=$fidf" ] || \
9067         err17935 "pfid in changelog $fidc != dir fid $fidf"
9068
9069     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
9070         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
9071     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9072     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
9073         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
9074     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9075     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
9076         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
9077
9078     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
9079         awk 'min == "" || $2 < min {min = $2}; END {print min}')
9080     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
9081     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9082     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
9083         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
9084
9085     echo "verifying user deregister"
9086     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9087     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
9088         grep -q $USER && error "User $USER still found in changelog_users"
9089
9090     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
9091         mdd.$MDT0.changelog_users | wc -l) - 2 ))
9092     if [ $USERS -eq 0 ]; then
9093         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
9094             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
9095         touch $DIR/$tdir/chloe
9096         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
9097             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
9098         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
9099         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9100     else
9101         echo "$USERS other changelog users; can't verify off"
9102     fi
9103 }
9104 run_test 160 "changelog sanity"
9105
9106 test_161a() {
9107         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9108     test_mkdir -p $DIR/$tdir
9109     cp /etc/hosts $DIR/$tdir/$tfile
9110     test_mkdir $DIR/$tdir/foo1
9111     test_mkdir $DIR/$tdir/foo2
9112     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9113     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9114     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9115     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9116         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9117         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9118                 $LFS fid2path $DIR $FID
9119                 err17935 "bad link ea"
9120         fi
9121     # middle
9122     rm $DIR/$tdir/foo2/zachary
9123     # last
9124     rm $DIR/$tdir/foo2/thor
9125     # first
9126     rm $DIR/$tdir/$tfile
9127     # rename
9128     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9129     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9130         then
9131         $LFS fid2path $DIR $FID
9132         err17935 "bad link rename"
9133     fi
9134     rm $DIR/$tdir/foo2/maggie
9135
9136     # overflow the EA
9137     local longname=filename_avg_len_is_thirty_two_
9138     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
9139         error "failed to hardlink many files"
9140     links=$($LFS fid2path $DIR $FID | wc -l)
9141     echo -n "${links}/1000 links in link EA"
9142     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9143     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
9144         error "failed to unlink many hardlinks"
9145 }
9146 run_test 161a "link ea sanity"
9147
9148 test_161b() {
9149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9150         [ $MDSCOUNT -lt 2 ] &&
9151                 skip "skipping remote directory test" && return
9152         local MDTIDX=1
9153         local remote_dir=$DIR/$tdir/remote_dir
9154
9155         mkdir -p $DIR/$tdir
9156         $LFS mkdir -i $MDTIDX $remote_dir ||
9157                 error "create remote directory failed"
9158
9159         cp /etc/hosts $remote_dir/$tfile
9160         mkdir -p $remote_dir/foo1
9161         mkdir -p $remote_dir/foo2
9162         ln $remote_dir/$tfile $remote_dir/foo1/sofia
9163         ln $remote_dir/$tfile $remote_dir/foo2/zachary
9164         ln $remote_dir/$tfile $remote_dir/foo1/luna
9165         ln $remote_dir/$tfile $remote_dir/foo2/thor
9166
9167         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
9168                      tr -d ']')
9169         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9170                 $LFS fid2path $DIR $FID
9171                 err17935 "bad link ea"
9172         fi
9173         # middle
9174         rm $remote_dir/foo2/zachary
9175         # last
9176         rm $remote_dir/foo2/thor
9177         # first
9178         rm $remote_dir/$tfile
9179         # rename
9180         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
9181         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
9182         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
9183                 $LFS fid2path $DIR $FID
9184                 err17935 "bad link rename"
9185         fi
9186         rm $remote_dir/foo2/maggie
9187
9188         # overflow the EA
9189         local longname=filename_avg_len_is_thirty_two_
9190         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
9191                 error "failed to hardlink many files"
9192         links=$($LFS fid2path $DIR $FID | wc -l)
9193         echo -n "${links}/1000 links in link EA"
9194         [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9195         unlinkmany $remote_dir/foo2/$longname 1000 ||
9196         error "failed to unlink many hardlinks"
9197 }
9198 run_test 161b "link ea sanity under remote directory"
9199
9200 check_path() {
9201     local expected=$1
9202     shift
9203     local fid=$2
9204
9205     local path=$(${LFS} fid2path $*)
9206     RC=$?
9207
9208     if [ $RC -ne 0 ]; then
9209         err17935 "path looked up of $expected failed. Error $RC"
9210         return $RC
9211     elif [ "${path}" != "${expected}" ]; then
9212         err17935 "path looked up \"${path}\" instead of \"${expected}\""
9213         return 2
9214     fi
9215     echo "fid $fid resolves to path $path (expected $expected)"
9216 }
9217
9218 test_162() {
9219         # Make changes to filesystem
9220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9221         test_mkdir -p $DIR/$tdir/d2
9222         touch $DIR/$tdir/d2/$tfile
9223         touch $DIR/$tdir/d2/x1
9224         touch $DIR/$tdir/d2/x2
9225         test_mkdir -p $DIR/$tdir/d2/a/b/c
9226         test_mkdir -p $DIR/$tdir/d2/p/q/r
9227         # regular file
9228         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
9229         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
9230
9231         # softlink
9232         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
9233         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
9234         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
9235
9236         # softlink to wrong file
9237         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
9238         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
9239         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
9240
9241         # hardlink
9242         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
9243         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
9244         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
9245         # fid2path dir/fsname should both work
9246         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
9247         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9248
9249         # hardlink count: check that there are 2 links
9250         # Doesnt work with CMD yet: 17935
9251         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9252                 err17935 "expected 2 links"
9253
9254         # hardlink indexing: remove the first link
9255         rm $DIR/$tdir/d2/p/q/r/hlink
9256         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9257
9258         return 0
9259 }
9260 run_test 162 "path lookup sanity"
9261
9262 test_163() {
9263         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9264         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9265         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
9266         copytool $FSNAME &
9267         sleep 1
9268         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
9269         # this proc file is temporary and linux-only
9270         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
9271          error "kernel->userspace send failed"
9272         kill -INT $!
9273 }
9274 run_test 163 "kernel <-> userspace comms"
9275
9276 test_169() {
9277         # do directio so as not to populate the page cache
9278         log "creating a 10 Mb file"
9279         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9280         log "starting reads"
9281         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9282         log "truncating the file"
9283         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9284         log "killing dd"
9285         kill %+ || true # reads might have finished
9286         echo "wait until dd is finished"
9287         wait
9288         log "removing the temporary file"
9289         rm -rf $DIR/$tfile || error "tmp file removal failed"
9290 }
9291 run_test 169 "parallel read and truncate should not deadlock"
9292
9293 test_170() {
9294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9295         $LCTL clear     # bug 18514
9296         $LCTL debug_daemon start $TMP/${tfile}_log_good
9297         touch $DIR/$tfile
9298         $LCTL debug_daemon stop
9299         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9300                error "sed failed to read log_good"
9301
9302         $LCTL debug_daemon start $TMP/${tfile}_log_good
9303         rm -rf $DIR/$tfile
9304         $LCTL debug_daemon stop
9305
9306         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9307                error "lctl df log_bad failed"
9308
9309         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9310         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9311
9312         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9313         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9314
9315         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9316                 error "bad_line good_line1 good_line2 are empty"
9317
9318         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9319         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9320         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9321
9322         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9323         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9324         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9325
9326         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9327                 error "bad_line_new good_line_new are empty"
9328
9329         local expected_good=$((good_line1 + good_line2*2))
9330
9331         rm -f $TMP/${tfile}*
9332         # LU-231, short malformed line may not be counted into bad lines
9333         if [ $bad_line -ne $bad_line_new ] &&
9334                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9335                 error "expected $bad_line bad lines, but got $bad_line_new"
9336                 return 1
9337         fi
9338
9339         if [ $expected_good -ne $good_line_new ]; then
9340                 error "expected $expected_good good lines, but got $good_line_new"
9341                 return 2
9342         fi
9343         true
9344 }
9345 run_test 170 "test lctl df to handle corrupted log ====================="
9346
9347 test_171() { # bug20592
9348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9349 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9350         $LCTL set_param fail_loc=0x50e
9351         $LCTL set_param fail_val=3000
9352         multiop_bg_pause $DIR/$tfile O_s || true
9353         local MULTIPID=$!
9354         kill -USR1 $MULTIPID
9355         # cause log dump
9356         sleep 3
9357         wait $MULTIPID
9358         if dmesg | grep "recursive fault"; then
9359                 error "caught a recursive fault"
9360         fi
9361         $LCTL set_param fail_loc=0
9362         true
9363 }
9364 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
9365
9366 # it would be good to share it with obdfilter-survey/libecho code
9367 setup_obdecho_osc () {
9368         local rc=0
9369         local ost_nid=$1
9370         local obdfilter_name=$2
9371         echo "Creating new osc for $obdfilter_name on $ost_nid"
9372         # make sure we can find loopback nid
9373         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
9374
9375         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
9376                            ${obdfilter_name}_osc_UUID || rc=2; }
9377         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
9378                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
9379         return $rc
9380 }
9381
9382 cleanup_obdecho_osc () {
9383         local obdfilter_name=$1
9384         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
9385         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
9386         return 0
9387 }
9388
9389 obdecho_create_test() {
9390         local OBD=$1
9391         local node=$2
9392         local rc=0
9393         local id
9394         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
9395         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
9396                            rc=2; }
9397         if [ $rc -eq 0 ]; then
9398             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
9399             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
9400         fi
9401         echo "New object id is $id"
9402         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
9403                            rc=4; }
9404         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
9405                                         "cleanup" || rc=5; }
9406         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
9407                                         "detach" || rc=6; }
9408         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
9409         return $rc
9410 }
9411
9412 test_180a() {
9413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9414         remote_ost_nodsh && skip "remote OST with nodsh" && return
9415         local rc=0
9416         local rmmod_local=0
9417
9418         if ! module_loaded obdecho; then
9419             load_module obdecho/obdecho
9420             rmmod_local=1
9421         fi
9422
9423         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
9424         local host=$(lctl get_param -n osc.$osc.import |
9425                              awk '/current_connection:/ {print $2}' )
9426         local target=$(lctl get_param -n osc.$osc.import |
9427                              awk '/target:/ {print $2}' )
9428         target=${target%_UUID}
9429
9430         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
9431         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
9432         [[ -n $target ]] && cleanup_obdecho_osc $target
9433         [ $rmmod_local -eq 1 ] && rmmod obdecho
9434         return $rc
9435 }
9436 run_test 180a "test obdecho on osc"
9437
9438 test_180b() {
9439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9440         remote_ost_nodsh && skip "remote OST with nodsh" && return
9441         local rc=0
9442         local rmmod_remote=0
9443
9444         do_facet ost1 "lsmod | grep -q obdecho || "                      \
9445                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
9446                       "modprobe obdecho; }" && rmmod_remote=1
9447         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
9448         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
9449         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
9450         return $rc
9451 }
9452 run_test 180b "test obdecho directly on obdfilter"
9453
9454 test_181() { # bug 22177
9455         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9456         # create enough files to index the directory
9457         createmany -o $DIR/$tdir/foobar 4000
9458         # print attributes for debug purpose
9459         lsattr -d .
9460         # open dir
9461         multiop_bg_pause $DIR/$tdir D_Sc || return 1
9462         MULTIPID=$!
9463         # remove the files & current working dir
9464         unlinkmany $DIR/$tdir/foobar 4000
9465         rmdir $DIR/$tdir
9466         kill -USR1 $MULTIPID
9467         wait $MULTIPID
9468         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9469         return 0
9470 }
9471 run_test 181 "Test open-unlinked dir ========================"
9472
9473 test_182() {
9474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9475         # disable MDC RPC lock wouldn't crash client
9476         local fcount=1000
9477         local tcount=4
9478
9479         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9480 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9481         $LCTL set_param fail_loc=0x804
9482
9483         for (( i=0; i < $tcount; i++ )) ; do
9484                 mkdir $DIR/$tdir/$i
9485                 createmany -o $DIR/$tdir/$i/f- $fcount &
9486         done
9487         wait
9488
9489         for (( i=0; i < $tcount; i++ )) ; do
9490                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9491         done
9492         wait
9493
9494         rm -rf $DIR/$tdir
9495
9496         $LCTL set_param fail_loc=0
9497 }
9498 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9499
9500 test_183() { # LU-2275
9501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9502         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9503         echo aaa > $DIR/$tdir/$tfile
9504
9505 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9506         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9507
9508         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9509         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9510
9511         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9512
9513         # Flush negative dentry cache
9514         touch $DIR/$tdir/$tfile
9515
9516         # We are not checking for any leaked references here, they'll
9517         # become evident next time we do cleanup with module unload.
9518         rm -rf $DIR/$tdir
9519 }
9520 run_test 183 "No crash or request leak in case of strange dispositions ========"
9521
9522 test_185() { # LU-2441
9523         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9524         touch $DIR/$tdir/spoo
9525         local mtime1=$(stat -c "%Y" $DIR/$tdir)
9526         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
9527                 error "cannot create/write a volatile file"
9528         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
9529                 error "FID is still valid after close"
9530
9531         multiop_bg_pause $DIR/$tdir vVw4096_c
9532         local multi_pid=$!
9533
9534         local OLD_IFS=$IFS
9535         IFS=":"
9536         local fidv=($fid)
9537         IFS=$OLD_IFS
9538         # assume that the next FID for this client is sequential, since stdout
9539         # is unfortunately eaten by multiop_bg_pause
9540         local n=$((${fidv[1]} + 1))
9541         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
9542         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
9543                 error "FID is missing before close"
9544         kill -USR1 $multi_pid
9545         # 1 second delay, so if mtime change we will see it
9546         sleep 1
9547         local mtime2=$(stat -c "%Y" $DIR/$tdir)
9548         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
9549 }
9550 run_test 185 "Volatile file support"
9551
9552 check_swap_layouts_support()
9553 {
9554         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
9555                 { skip "Does not support layout lock."; return 0; }
9556         return 1
9557 }
9558
9559 # test suite 184 is for LU-2016, LU-2017
9560 test_184a() {
9561         check_swap_layouts_support && return 0
9562
9563         dir0=$DIR/$tdir/$testnum
9564         test_mkdir -p $dir0 || error "creating dir $dir0"
9565         ref1=/etc/passwd
9566         ref2=/etc/group
9567         file1=$dir0/f1
9568         file2=$dir0/f2
9569         $SETSTRIPE -c1 $file1
9570         cp $ref1 $file1
9571         $SETSTRIPE -c2 $file2
9572         cp $ref2 $file2
9573         gen1=$($GETSTRIPE -g $file1)
9574         gen2=$($GETSTRIPE -g $file2)
9575
9576         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
9577         gen=$($GETSTRIPE -g $file1)
9578         [[ $gen1 != $gen ]] ||
9579                 "Layout generation on $file1 does not change"
9580         gen=$($GETSTRIPE -g $file2)
9581         [[ $gen2 != $gen ]] ||
9582                 "Layout generation on $file2 does not change"
9583
9584         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
9585         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
9586 }
9587 run_test 184a "Basic layout swap"
9588
9589 test_184b() {
9590         check_swap_layouts_support && return 0
9591
9592         dir0=$DIR/$tdir/$testnum
9593         mkdir -p $dir0 || error "creating dir $dir0"
9594         file1=$dir0/f1
9595         file2=$dir0/f2
9596         file3=$dir0/f3
9597         dir1=$dir0/d1
9598         dir2=$dir0/d2
9599         mkdir $dir1 $dir2
9600         $SETSTRIPE -c1 $file1
9601         $SETSTRIPE -c2 $file2
9602         $SETSTRIPE -c1 $file3
9603         chown $RUNAS_ID $file3
9604         gen1=$($GETSTRIPE -g $file1)
9605         gen2=$($GETSTRIPE -g $file2)
9606
9607         $LFS swap_layouts $dir1 $dir2 &&
9608                 error "swap of directories layouts should fail"
9609         $LFS swap_layouts $dir1 $file1 &&
9610                 error "swap of directory and file layouts should fail"
9611         $RUNAS $LFS swap_layouts $file1 $file2 &&
9612                 error "swap of file we cannot write should fail"
9613         $LFS swap_layouts $file1 $file3 &&
9614                 error "swap of file with different owner should fail"
9615         /bin/true # to clear error code
9616 }
9617 run_test 184b "Forbidden layout swap (will generate errors)"
9618
9619 test_184c() {
9620         check_swap_layouts_support && return 0
9621
9622         local dir0=$DIR/$tdir/$testnum
9623         mkdir -p $dir0 || error "creating dir $dir0"
9624
9625         local ref1=$dir0/ref1
9626         local ref2=$dir0/ref2
9627         local file1=$dir0/file1
9628         local file2=$dir0/file2
9629         # create a file large enough for the concurent test
9630         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
9631         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
9632         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
9633
9634         cp $ref2 $file2
9635         dd if=$ref1 of=$file1 bs=16k &
9636         local DD_PID=$!
9637
9638         # Make sure dd starts to copy file
9639         while [ ! -f $file1 ]; do sleep 0.1; done
9640
9641         $LFS swap_layouts $file1 $file2
9642         local rc=$?
9643         wait $DD_PID
9644         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
9645         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
9646
9647         # how many bytes copied before swapping layout
9648         local copied=`stat -c %s $file2`
9649         local remaining=`stat -c %s $ref1`
9650         remaining=$((remaining - copied))
9651         echo "Copied $copied bytes before swapping layout..."
9652
9653         cmp -n $copied $file1 $ref2 | grep differ &&
9654                 error "Content mismatch [0, $copied) of ref2 and file1"
9655         cmp -n $copied $file2 $ref1 ||
9656                 error "Content mismatch [0, $copied) of ref1 and file2"
9657         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
9658                 error "Content mismatch [$copied, EOF) of ref1 and file1"
9659
9660         # clean up
9661         rm -f $ref1 $ref2 $file1 $file2
9662 }
9663 run_test 184c "Concurrent write and layout swap"
9664
9665 # OST pools tests
9666 check_file_in_pool()
9667 {
9668         local file=$1
9669         local pool=$2
9670         local tlist="$3"
9671         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9672         for i in $res
9673         do
9674                 for t in $tlist ; do
9675                         [ "$i" -eq "$t" ] && continue 2
9676                 done
9677
9678                 echo "pool list: $tlist"
9679                 echo "striping: $res"
9680                 error_noexit "$file not allocated in $pool"
9681                 return 1
9682         done
9683         return 0
9684 }
9685
9686 pool_add() {
9687         echo "Creating new pool"
9688         local pool=$1
9689
9690         create_pool $FSNAME.$pool ||
9691                 { error_noexit "No pool created, result code $?"; return 1; }
9692         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9693                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9694 }
9695
9696 pool_add_targets() {
9697         echo "Adding targets to pool"
9698         local pool=$1
9699         local first=$2
9700         local last=$3
9701         local step=${4:-1}
9702
9703         local list=$(seq $first $step $last)
9704
9705         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9706         do_facet mgs $LCTL pool_add \
9707                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9708         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9709                         | sort -u | tr '\n' ' ' " "$t" || { 
9710                 error_noexit "Add to pool failed"
9711                 return 1
9712         }
9713         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9714         local addcount=$(((last - first) / step + 1))
9715         [ $lfscount -eq $addcount ] || {
9716                 error_noexit "lfs pool_list bad ost count" \
9717                                                 "$lfscount != $addcount"
9718                 return 2
9719         }
9720 }
9721
9722 pool_set_dir() {
9723         local pool=$1
9724         local tdir=$2
9725         echo "Setting pool on directory $tdir"
9726
9727         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9728
9729         error_noexit "Cannot set pool $pool to $tdir"
9730         return 1
9731 }
9732
9733 pool_check_dir() {
9734         local pool=$1
9735         local tdir=$2
9736         echo "Checking pool on directory $tdir"
9737
9738         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9739         [ "$res" = "$pool" ] && return 0
9740
9741         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9742         return 1
9743 }
9744
9745 pool_dir_rel_path() {
9746         echo "Testing relative path works well"
9747         local pool=$1
9748         local tdir=$2
9749         local root=$3
9750
9751         mkdir -p $root/$tdir/$tdir
9752         cd $root/$tdir
9753         pool_set_dir $pool $tdir          || return 1
9754         pool_set_dir $pool ./$tdir        || return 2
9755         pool_set_dir $pool ../$tdir       || return 3
9756         pool_set_dir $pool ../$tdir/$tdir || return 4
9757         rm -rf $tdir; cd - > /dev/null
9758 }
9759
9760 pool_alloc_files() {
9761         echo "Checking files allocation from directory pool"
9762         local pool=$1
9763         local tdir=$2
9764         local count=$3
9765         local tlist="$4"
9766
9767         local failed=0
9768         for i in $(seq -w 1 $count)
9769         do
9770                 local file=$tdir/file-$i
9771                 touch $file
9772                 check_file_in_pool $file $pool "$tlist" || \
9773                         failed=$((failed + 1))
9774         done
9775         [ "$failed" = 0 ] && return 0
9776
9777         error_noexit "$failed files not allocated in $pool"
9778         return 1
9779 }
9780
9781 pool_create_files() {
9782         echo "Creating files in pool"
9783         local pool=$1
9784         local tdir=$2
9785         local count=$3
9786         local tlist="$4"
9787
9788         mkdir -p $tdir
9789         local failed=0
9790         for i in $(seq -w 1 $count)
9791         do
9792                 local file=$tdir/spoo-$i
9793                 $SETSTRIPE -p $pool $file
9794                 check_file_in_pool $file $pool "$tlist" || \
9795                         failed=$((failed + 1))
9796         done
9797         [ "$failed" = 0 ] && return 0
9798
9799         error_noexit "$failed files not allocated in $pool"
9800         return 1
9801 }
9802
9803 pool_lfs_df() {
9804         echo "Checking 'lfs df' output"
9805         local pool=$1
9806
9807         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9808                         tr '\n' ' ')
9809         local res=$($LFS df --pool $FSNAME.$pool |
9810                         awk '{print $1}' |
9811                         grep "$FSNAME-OST" |
9812                         tr '\n' ' ')
9813         [ "$res" = "$t" ] && return 0
9814
9815         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9816         return 1
9817 }
9818
9819 pool_file_rel_path() {
9820         echo "Creating files in a pool with relative pathname"
9821         local pool=$1
9822         local tdir=$2
9823
9824         mkdir -p $tdir ||
9825                 { error_noexit "unable to create $tdir"; return 1 ; }
9826         local file="/..$tdir/$tfile-1"
9827         $SETSTRIPE -p $pool $file ||
9828                 { error_noexit "unable to create $file" ; return 2 ; }
9829
9830         cd $tdir
9831         $SETSTRIPE -p $pool $tfile-2 || {
9832                 error_noexit "unable to create $tfile-2 in $tdir"
9833                 return 3
9834         }
9835 }
9836
9837 pool_remove_first_target() {
9838         echo "Removing first target from a pool"
9839         local pool=$1
9840
9841         local pname="lov.$FSNAME-*.pools.$pool"
9842         local t=$($LCTL get_param -n $pname | head -1)
9843         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9844         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9845                 error_noexit "$t not removed from $FSNAME.$pool"
9846                 return 1
9847         }
9848 }
9849
9850 pool_remove_all_targets() {
9851         echo "Removing all targets from pool"
9852         local pool=$1
9853         local file=$2
9854         local pname="lov.$FSNAME-*.pools.$pool"
9855         for t in $($LCTL get_param -n $pname | sort -u)
9856         do
9857                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9858         done
9859         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9860                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9861                 return 1
9862         }
9863         # striping on an empty/nonexistant pool should fall back 
9864         # to "pool of everything"
9865         touch $file || {
9866                 error_noexit "failed to use fallback striping for empty pool"
9867                 return 2
9868         }
9869         # setstripe on an empty pool should fail
9870         $SETSTRIPE -p $pool $file 2>/dev/null && {
9871                 error_noexit "expected failure when creating file" \
9872                                                         "with empty pool"
9873                 return 3
9874         }
9875         return 0
9876 }
9877
9878 pool_remove() {
9879         echo "Destroying pool"
9880         local pool=$1
9881         local file=$2
9882
9883         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9884
9885         sleep 2
9886         # striping on an empty/nonexistant pool should fall back 
9887         # to "pool of everything"
9888         touch $file || {
9889                 error_noexit "failed to use fallback striping for missing pool"
9890                 return 1
9891         }
9892         # setstripe on an empty pool should fail
9893         $SETSTRIPE -p $pool $file 2>/dev/null && {
9894                 error_noexit "expected failure when creating file" \
9895                                                         "with missing pool"
9896                 return 2
9897         }
9898
9899         # get param should return err once pool is gone
9900         if wait_update $HOSTNAME "lctl get_param -n \
9901                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9902         then
9903                 remove_pool_from_list $FSNAME.$pool
9904                 return 0
9905         fi
9906         error_noexit "Pool $FSNAME.$pool is not destroyed"
9907         return 3
9908 }
9909
9910 test_200() {
9911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9912         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9913
9914         local POOL=${POOL:-cea1}
9915         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9916         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9917         # Pool OST targets
9918         local first_ost=0
9919         local last_ost=$(($OSTCOUNT - 1))
9920         local ost_step=2
9921         local ost_list=$(seq $first_ost $ost_step $last_ost)
9922         local ost_range="$first_ost $last_ost $ost_step"
9923         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9924         local file_dir=$POOL_ROOT/file_tst
9925
9926         local rc=0
9927         while : ; do
9928                 # former test_200a test_200b
9929                 pool_add $POOL                          || { rc=$? ; break; }
9930                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9931                 # former test_200c test_200d
9932                 mkdir -p $test_path
9933                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9934                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9935                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9936                                                         || { rc=$? ; break; }
9937                 # former test_200e test_200f
9938                 local files=$((OSTCOUNT*3))
9939                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9940                                                         || { rc=$? ; break; }
9941                 pool_create_files $POOL $file_dir $files "$ost_list" \
9942                                                         || { rc=$? ; break; }
9943                 # former test_200g test_200h
9944                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9945                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9946
9947                 # former test_201a test_201b test_201c
9948                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9949
9950                 local f=$test_path/$tfile
9951                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9952                 pool_remove $POOL $f                    || { rc=$? ; break; }
9953                 break
9954         done
9955
9956         cleanup_pools
9957         return $rc
9958 }
9959 run_test 200 "OST pools"
9960
9961 # usage: default_attr <count | size | offset>
9962 default_attr() {
9963         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9964 }
9965
9966 # usage: check_default_stripe_attr
9967 check_default_stripe_attr() {
9968         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9969         case $1 in
9970         --stripe-count|--count)
9971                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9972         --stripe-size|--size)
9973                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9974         --stripe-index|--index)
9975                 EXPECTED=-1;;
9976         *)
9977                 error "unknown getstripe attr '$1'"
9978         esac
9979
9980         [ $ACTUAL != $EXPECTED ] &&
9981                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9982 }
9983
9984 test_204a() {
9985         test_mkdir -p $DIR/$tdir
9986         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9987
9988         check_default_stripe_attr --stripe-count
9989         check_default_stripe_attr --stripe-size
9990         check_default_stripe_attr --stripe-index
9991
9992         return 0
9993 }
9994 run_test 204a "Print default stripe attributes ================="
9995
9996 test_204b() {
9997         test_mkdir -p $DIR/$tdir
9998         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9999
10000         check_default_stripe_attr --stripe-size
10001         check_default_stripe_attr --stripe-index
10002
10003         return 0
10004 }
10005 run_test 204b "Print default stripe size and offset  ==========="
10006
10007 test_204c() {
10008         test_mkdir -p $DIR/$tdir
10009         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10010
10011         check_default_stripe_attr --stripe-count
10012         check_default_stripe_attr --stripe-index
10013
10014         return 0
10015 }
10016 run_test 204c "Print default stripe count and offset ==========="
10017
10018 test_204d() {
10019         test_mkdir -p $DIR/$tdir
10020         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10021
10022         check_default_stripe_attr --stripe-count
10023         check_default_stripe_attr --stripe-size
10024
10025         return 0
10026 }
10027 run_test 204d "Print default stripe count and size ============="
10028
10029 test_204e() {
10030         test_mkdir -p $DIR/$tdir
10031         $SETSTRIPE -d $DIR/$tdir
10032
10033         check_default_stripe_attr --stripe-count --raw
10034         check_default_stripe_attr --stripe-size --raw
10035         check_default_stripe_attr --stripe-index --raw
10036
10037         return 0
10038 }
10039 run_test 204e "Print raw stripe attributes ================="
10040
10041 test_204f() {
10042         test_mkdir -p $DIR/$tdir
10043         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10044
10045         check_default_stripe_attr --stripe-size --raw
10046         check_default_stripe_attr --stripe-index --raw
10047
10048         return 0
10049 }
10050 run_test 204f "Print raw stripe size and offset  ==========="
10051
10052 test_204g() {
10053         test_mkdir -p $DIR/$tdir
10054         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10055
10056         check_default_stripe_attr --stripe-count --raw
10057         check_default_stripe_attr --stripe-index --raw
10058
10059         return 0
10060 }
10061 run_test 204g "Print raw stripe count and offset ==========="
10062
10063 test_204h() {
10064         test_mkdir -p $DIR/$tdir
10065         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10066
10067         check_default_stripe_attr --stripe-count --raw
10068         check_default_stripe_attr --stripe-size --raw
10069
10070         return 0
10071 }
10072 run_test 204h "Print raw stripe count and size ============="
10073
10074 # Figure out which job scheduler is being used, if any,
10075 # or use a fake one
10076 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
10077         JOBENV=SLURM_JOB_ID
10078 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
10079         JOBENV=LSB_JOBID
10080 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
10081         JOBENV=PBS_JOBID
10082 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
10083         JOBENV=LOADL_STEP_ID
10084 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
10085         JOBENV=JOB_ID
10086 else
10087         JOBENV=FAKE_JOBID
10088 fi
10089
10090 verify_jobstats() {
10091         local cmd=$1
10092         local target=$2
10093
10094         # clear old jobstats
10095         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
10096         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
10097
10098         # use a new JobID for this test, or we might see an old one
10099         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
10100
10101         JOBVAL=${!JOBENV}
10102         log "Test: $cmd"
10103         log "Using JobID environment variable $JOBENV=$JOBVAL"
10104
10105         if [ $JOBENV = "FAKE_JOBID" ]; then
10106                 FAKE_JOBID=$JOBVAL $cmd
10107         else
10108                 $cmd
10109         fi
10110
10111         if [ "$target" = "mdt" -o "$target" = "both" ]; then
10112                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
10113                 do_facet $FACET lctl get_param mdt.*.job_stats |
10114                         grep $JOBVAL || error "No job stats found on MDT $FACET"
10115         fi
10116         if [ "$target" = "ost" -o "$target" = "both" ]; then
10117                 FACET=ost1
10118                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
10119                         grep $JOBVAL || error "No job stats found on OST $FACET"
10120         fi
10121 }
10122
10123 jobstats_set() {
10124         trap 0
10125         NEW_JOBENV=${1:-$OLD_JOBENV}
10126         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
10127         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
10128 }
10129
10130 test_205() { # Job stats
10131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10132         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
10133                 skip "Server doesn't support jobstats" && return 0
10134
10135         local cmd
10136         OLD_JOBENV=`$LCTL get_param -n jobid_var`
10137         if [ $OLD_JOBENV != $JOBENV ]; then
10138                 jobstats_set $JOBENV
10139                 trap jobstats_set EXIT
10140         fi
10141
10142         # mkdir
10143         cmd="mkdir $DIR/$tfile"
10144         verify_jobstats "$cmd" "mdt"
10145         # rmdir
10146         cmd="rm -fr $DIR/$tfile"
10147         verify_jobstats "$cmd" "mdt"
10148         # mknod
10149         cmd="mknod $DIR/$tfile c 1 3"
10150         verify_jobstats "$cmd" "mdt"
10151         # unlink
10152         cmd="rm -f $DIR/$tfile"
10153         verify_jobstats "$cmd" "mdt"
10154         # open & close
10155         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
10156         verify_jobstats "$cmd" "mdt"
10157         # setattr
10158         cmd="touch $DIR/$tfile"
10159         verify_jobstats "$cmd" "both"
10160         # write
10161         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
10162         verify_jobstats "$cmd" "ost"
10163         # read
10164         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
10165         verify_jobstats "$cmd" "ost"
10166         # truncate
10167         cmd="$TRUNCATE $DIR/$tfile 0"
10168         verify_jobstats "$cmd" "both"
10169         # rename
10170         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
10171         verify_jobstats "$cmd" "mdt"
10172
10173         # cleanup
10174         rm -f $DIR/jobstats_test_rename
10175
10176         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
10177 }
10178 run_test 205 "Verify job stats"
10179
10180 # LU-1480, LU-1773 and LU-1657
10181 test_206() {
10182         mkdir -p $DIR/$tdir
10183         lfs setstripe -c -1 $DIR/$tdir
10184 #define OBD_FAIL_LOV_INIT 0x1403
10185         $LCTL set_param fail_loc=0xa0001403
10186         $LCTL set_param fail_val=1
10187         touch $DIR/$tdir/$tfile || true
10188 }
10189 run_test 206 "fail lov_init_raid0() doesn't lbug"
10190
10191 test_207a() {
10192         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10193         local fsz=`stat -c %s $DIR/$tfile`
10194         cancel_lru_locks mdc
10195
10196         # do not return layout in getattr intent
10197 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
10198         $LCTL set_param fail_loc=0x170
10199         local sz=`stat -c %s $DIR/$tfile`
10200
10201         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
10202
10203         rm -rf $DIR/$tfile
10204 }
10205 run_test 207a "can refresh layout at glimpse"
10206
10207 test_207b() {
10208         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10209         local cksum=`md5sum $DIR/$tfile`
10210         local fsz=`stat -c %s $DIR/$tfile`
10211         cancel_lru_locks mdc
10212         cancel_lru_locks osc
10213
10214         # do not return layout in getattr intent
10215 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
10216         $LCTL set_param fail_loc=0x171
10217
10218         # it will refresh layout after the file is opened but before read issues
10219         echo checksum is "$cksum"
10220         echo "$cksum" |md5sum -c --quiet || error "file differs"
10221
10222         rm -rf $DIR/$tfile
10223 }
10224 run_test 207b "can refresh layout at open"
10225
10226 test_212() {
10227         size=`date +%s`
10228         size=$((size % 8192 + 1))
10229         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
10230         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
10231         rm -f $DIR/f212 $DIR/f212.xyz
10232 }
10233 run_test 212 "Sendfile test ============================================"
10234
10235 test_213() {
10236         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
10237         cancel_lru_locks osc
10238         lctl set_param fail_loc=0x8000040f
10239         # generate a read lock
10240         cat $DIR/$tfile > /dev/null
10241         # write to the file, it will try to cancel the above read lock.
10242         cat /etc/hosts >> $DIR/$tfile
10243 }
10244 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
10245
10246 test_214() { # for bug 20133
10247         test_mkdir -p $DIR/d214p/d214c
10248         for (( i=0; i < 340; i++ )) ; do
10249                 touch $DIR/d214p/d214c/a$i
10250         done
10251
10252         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
10253         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
10254         ls $DIR/d214c || error "ls $DIR/d214c failed"
10255         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
10256 }
10257 run_test 214 "hash-indexed directory test - bug 20133"
10258
10259 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
10260 create_lnet_proc_files() {
10261         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
10262         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
10263
10264         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
10265         rm -f "$TMP/lnet_$1.sys_tmp"
10266 }
10267
10268 # counterpart of create_lnet_proc_files
10269 remove_lnet_proc_files() {
10270         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
10271 }
10272
10273 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10274 # 3rd arg as regexp for body
10275 check_lnet_proc_stats() {
10276         local l=$(cat "$TMP/lnet_$1" |wc -l)
10277         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
10278
10279         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
10280 }
10281
10282 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10283 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
10284 # optional and can be regexp for 2nd line (lnet.routes case)
10285 check_lnet_proc_entry() {
10286         local blp=2            # blp stands for 'position of 1st line of body'
10287         [ "$5" = "" ] || blp=3 # lnet.routes case
10288
10289         local l=$(cat "$TMP/lnet_$1" |wc -l)
10290         # subtracting one from $blp because the body can be empty
10291         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
10292
10293         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
10294                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
10295
10296         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
10297                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
10298
10299         # bail out if any unexpected line happened
10300         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
10301         [ "$?" != 0 ] || error "$2 misformatted"
10302 }
10303
10304 test_215() { # for bugs 18102, 21079, 21517
10305         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10306         local N='(0|[1-9][0-9]*)'       # non-negative numeric
10307         local P='[1-9][0-9]*'           # positive numeric
10308         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
10309         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
10310         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
10311         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
10312
10313         local L1 # regexp for 1st line
10314         local L2 # regexp for 2nd line (optional)
10315         local BR # regexp for the rest (body)
10316
10317         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
10318         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
10319         create_lnet_proc_files "stats"
10320         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
10321         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
10322         remove_lnet_proc_files "stats"
10323
10324         # /proc/sys/lnet/routes should look like this:
10325         # Routing disabled/enabled
10326         # net hops state router
10327         # where net is a string like tcp0, hops >= 0, state is up/down,
10328         # router is a string like 192.168.1.1@tcp2
10329         L1="^Routing (disabled|enabled)$"
10330         L2="^net +hops +state +router$"
10331         BR="^$NET +$N +(up|down) +$NID$"
10332         create_lnet_proc_files "routes"
10333         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
10334         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
10335         remove_lnet_proc_files "routes"
10336
10337         # /proc/sys/lnet/routers should look like this:
10338         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
10339         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
10340         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
10341         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
10342         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
10343         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
10344         create_lnet_proc_files "routers"
10345         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
10346         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
10347         remove_lnet_proc_files "routers"
10348
10349         # /proc/sys/lnet/peers should look like this:
10350         # nid refs state last max rtr min tx min queue
10351         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
10352         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
10353         # numeric (0 or >0 or <0), queue >= 0.
10354         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
10355         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
10356         create_lnet_proc_files "peers"
10357         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
10358         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
10359         remove_lnet_proc_files "peers"
10360
10361         # /proc/sys/lnet/buffers  should look like this:
10362         # pages count credits min
10363         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
10364         L1="^pages +count +credits +min$"
10365         BR="^ +$N +$N +$I +$I$"
10366         create_lnet_proc_files "buffers"
10367         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
10368         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
10369         remove_lnet_proc_files "buffers"
10370
10371         # /proc/sys/lnet/nis should look like this:
10372         # nid status alive refs peer rtr max tx min
10373         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
10374         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
10375         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
10376         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
10377         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
10378         create_lnet_proc_files "nis"
10379         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
10380         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
10381         remove_lnet_proc_files "nis"
10382
10383         # can we successfully write to /proc/sys/lnet/stats?
10384         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
10385         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
10386 }
10387 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
10388
10389 test_216() { # bug 20317
10390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10391         remote_ost_nodsh && skip "remote OST with nodsh" && return
10392         local node
10393         local p="$TMP/sanityN-$TESTNAME.parameters"
10394         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
10395         for node in $(osts_nodes); do
10396                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
10397                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
10398                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
10399         done
10400         clear_osc_stats
10401
10402         # agressive lockless i/o settings
10403         for node in $(osts_nodes); do
10404                 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'
10405         done
10406         lctl set_param -n osc.*.contention_seconds 60
10407
10408         $DIRECTIO write $DIR/$tfile 0 10 4096
10409         $CHECKSTAT -s 40960 $DIR/$tfile
10410
10411         # disable lockless i/o
10412         for node in $(osts_nodes); do
10413                 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'
10414         done
10415         lctl set_param -n osc.*.contention_seconds 0
10416         clear_osc_stats
10417
10418         dd if=/dev/zero of=$DIR/$tfile count=0
10419         $CHECKSTAT -s 0 $DIR/$tfile
10420
10421         restore_lustre_params <$p
10422         rm -f $p
10423         rm $DIR/$tfile
10424 }
10425 run_test 216 "check lockless direct write works and updates file size and kms correctly"
10426
10427 test_217() { # bug 22430
10428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10429         local node
10430         local nid
10431
10432         for node in $(nodes_list); do
10433                 nid=$(host_nids_address $node $NETTYPE)
10434                 if [[ $nid = *-* ]] ; then
10435                         echo "lctl ping $nid@$NETTYPE"
10436                         lctl ping $nid@$NETTYPE
10437                 else
10438                         echo "skipping $node (no hyphen detected)"
10439                 fi
10440         done
10441 }
10442 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
10443
10444 test_218() {
10445        # do directio so as not to populate the page cache
10446        log "creating a 10 Mb file"
10447        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10448        log "starting reads"
10449        dd if=$DIR/$tfile of=/dev/null bs=4096 &
10450        log "truncating the file"
10451        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10452        log "killing dd"
10453        kill %+ || true # reads might have finished
10454        echo "wait until dd is finished"
10455        wait
10456        log "removing the temporary file"
10457        rm -rf $DIR/$tfile || error "tmp file removal failed"
10458 }
10459 run_test 218 "parallel read and truncate should not deadlock ======================="
10460
10461 test_219() {
10462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10463         # write one partial page
10464         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
10465         # set no grant so vvp_io_commit_write will do sync write
10466         $LCTL set_param fail_loc=0x411
10467         # write a full page at the end of file
10468         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
10469
10470         $LCTL set_param fail_loc=0
10471         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
10472         $LCTL set_param fail_loc=0x411
10473         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
10474 }
10475 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
10476
10477 test_220() { #LU-325
10478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10479         remote_ost_nodsh && skip "remote OST with nodsh" && return
10480         local OSTIDX=0
10481
10482         test_mkdir -p $DIR/$tdir
10483         local OST=$(lfs osts | grep ${OSTIDX}": " | \
10484                 awk '{print $2}' | sed -e 's/_UUID$//')
10485
10486         # on the mdt's osc
10487         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
10488         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
10489                         osc.$mdtosc_proc1.prealloc_last_id)
10490         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
10491                         osc.$mdtosc_proc1.prealloc_next_id)
10492
10493         $LFS df -i
10494
10495         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
10496         #define OBD_FAIL_OST_ENOINO              0x229
10497         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
10498         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
10499         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
10500
10501         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
10502
10503         MDSOBJS=$((last_id - next_id))
10504         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
10505
10506         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
10507         echo "OST still has $count kbytes free"
10508
10509         echo "create $MDSOBJS files @next_id..."
10510         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
10511
10512         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10513                         osc.$mdtosc_proc1.prealloc_last_id)
10514         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10515                         osc.$mdtosc_proc1.prealloc_next_id)
10516
10517         echo "after creation, last_id=$last_id2, next_id=$next_id2"
10518         $LFS df -i
10519
10520         echo "cleanup..."
10521
10522         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
10523         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
10524
10525         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
10526         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
10527         echo "unlink $MDSOBJS files @$next_id..."
10528         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
10529 }
10530 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
10531
10532 test_221() {
10533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10534         dd if=`which date` of=$MOUNT/date oflag=sync
10535         chmod +x $MOUNT/date
10536
10537         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
10538         $LCTL set_param fail_loc=0x80001401
10539
10540         $MOUNT/date > /dev/null
10541         rm -f $MOUNT/date
10542 }
10543 run_test 221 "make sure fault and truncate race to not cause OOM"
10544
10545 test_222a () {
10546         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10547        rm -rf $DIR/$tdir
10548        test_mkdir -p $DIR/$tdir
10549        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10550        createmany -o $DIR/$tdir/$tfile 10
10551        cancel_lru_locks mdc
10552        cancel_lru_locks osc
10553        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10554        $LCTL set_param fail_loc=0x31a
10555        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
10556        $LCTL set_param fail_loc=0
10557        rm -r $DIR/$tdir
10558 }
10559 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
10560
10561 test_222b () {
10562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10563        rm -rf $DIR/$tdir
10564        test_mkdir -p $DIR/$tdir
10565        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10566        createmany -o $DIR/$tdir/$tfile 10
10567        cancel_lru_locks mdc
10568        cancel_lru_locks osc
10569        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10570        $LCTL set_param fail_loc=0x31a
10571        rm -r $DIR/$tdir || "AGL for rmdir failed"
10572        $LCTL set_param fail_loc=0
10573 }
10574 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
10575
10576 test_223 () {
10577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10578        rm -rf $DIR/$tdir
10579        test_mkdir -p $DIR/$tdir
10580        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10581        createmany -o $DIR/$tdir/$tfile 10
10582        cancel_lru_locks mdc
10583        cancel_lru_locks osc
10584        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
10585        $LCTL set_param fail_loc=0x31b
10586        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
10587        $LCTL set_param fail_loc=0
10588        rm -r $DIR/$tdir
10589 }
10590 run_test 223 "osc reenqueue if without AGL lock granted ======================="
10591
10592 test_224a() { # LU-1039, MRP-303
10593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10594         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
10595         $LCTL set_param fail_loc=0x508
10596         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
10597         $LCTL set_param fail_loc=0
10598         df $DIR
10599 }
10600 run_test 224a "Don't panic on bulk IO failure"
10601
10602 test_224b() { # LU-1039, MRP-303
10603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10604         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
10605         cancel_lru_locks osc
10606         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
10607         $LCTL set_param fail_loc=0x515
10608         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
10609         $LCTL set_param fail_loc=0
10610         df $DIR
10611 }
10612 run_test 224b "Don't panic on bulk IO failure"
10613
10614 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
10615 test_225a () {
10616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10617        if [ -z ${MDSSURVEY} ]; then
10618               skip_env "mds-survey not found" && return
10619        fi
10620        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10621             { skip "Need MDS version at least 2.2.51"; return; }
10622
10623        local mds=$(facet_host $SINGLEMDS)
10624        local target=$(do_nodes $mds 'lctl dl' | \
10625                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10626
10627        local cmd1="file_count=1000 thrhi=4"
10628        local cmd2="dir_count=2 layer=mdd stripe_count=0"
10629        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10630        local cmd="$cmd1 $cmd2 $cmd3"
10631
10632        rm -f ${TMP}/mds_survey*
10633        echo + $cmd
10634        eval $cmd || error "mds-survey with zero-stripe failed"
10635        cat ${TMP}/mds_survey*
10636        rm -f ${TMP}/mds_survey*
10637 }
10638 run_test 225a "Metadata survey sanity with zero-stripe"
10639
10640 test_225b () {
10641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10642        if [ -z ${MDSSURVEY} ]; then
10643               skip_env "mds-survey not found" && return
10644        fi
10645        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10646             { skip "Need MDS version at least 2.2.51"; return; }
10647
10648        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
10649               skip_env "Need to mount OST to test" && return
10650        fi
10651
10652        local mds=$(facet_host $SINGLEMDS)
10653        local target=$(do_nodes $mds 'lctl dl' | \
10654                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10655
10656        local cmd1="file_count=1000 thrhi=4"
10657        local cmd2="dir_count=2 layer=mdd stripe_count=1"
10658        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10659        local cmd="$cmd1 $cmd2 $cmd3"
10660
10661        rm -f ${TMP}/mds_survey*
10662        echo + $cmd
10663        eval $cmd || error "mds-survey with stripe_count failed"
10664        cat ${TMP}/mds_survey*
10665        rm -f ${TMP}/mds_survey*
10666 }
10667 run_test 225b "Metadata survey sanity with stripe_count = 1"
10668
10669 mcreate_path2fid () {
10670         local mode=$1
10671         local major=$2
10672         local minor=$3
10673         local name=$4
10674         local desc=$5
10675         local path=$DIR/$tdir/$name
10676         local fid
10677         local rc
10678         local fid_path
10679
10680         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10681                 error "cannot create $desc"
10682
10683         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
10684         rc=$?
10685         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10686
10687         fid_path=$($LFS fid2path $MOUNT $fid)
10688         rc=$?
10689         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10690
10691         [ "$path" == "$fid_path" ] ||
10692                 error "fid2path returned $fid_path, expected $path"
10693
10694         echo "pass with $path and $fid"
10695 }
10696
10697 test_226a () {
10698         rm -rf $DIR/$tdir
10699         mkdir -p $DIR/$tdir
10700
10701         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10702         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10703         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10704         mcreate_path2fid 0040666 0 0 dir "directory"
10705         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10706         mcreate_path2fid 0100666 0 0 file "regular file"
10707         mcreate_path2fid 0120666 0 0 link "symbolic link"
10708         mcreate_path2fid 0140666 0 0 sock "socket"
10709 }
10710 run_test 226a "call path2fid and fid2path on files of all type"
10711
10712 test_226b () {
10713         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10714         rm -rf $DIR/$tdir
10715         local MDTIDX=1
10716
10717         mkdir -p $DIR/$tdir
10718         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
10719                 error "create remote directory failed"
10720         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
10721         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
10722                                 "character special file (null)"
10723         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
10724                                 "character special file (no device)"
10725         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
10726         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
10727                                 "block special file (loop)"
10728         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
10729         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
10730         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
10731 }
10732 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
10733
10734 # LU-1299 Executing or running ldd on a truncated executable does not
10735 # cause an out-of-memory condition.
10736 test_227() {
10737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10738         dd if=`which date` of=$MOUNT/date bs=1k count=1
10739         chmod +x $MOUNT/date
10740
10741         $MOUNT/date > /dev/null
10742         ldd $MOUNT/date > /dev/null
10743         rm -f $MOUNT/date
10744 }
10745 run_test 227 "running truncated executable does not cause OOM"
10746
10747 # LU-1512 try to reuse idle OI blocks
10748 test_228a() {
10749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10750         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10751                 skip "non-ldiskfs backend" && return
10752
10753         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10754         local myDIR=$DIR/$tdir
10755
10756         mkdir -p $myDIR
10757         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10758         $LCTL set_param fail_loc=0x80001002
10759         createmany -o $myDIR/t- 10000
10760         $LCTL set_param fail_loc=0
10761         # The guard is current the largest FID holder
10762         touch $myDIR/guard
10763         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10764                     tr -d '[')
10765         local IDX=$(($SEQ % 64))
10766
10767         do_facet $SINGLEMDS sync
10768         # Make sure journal flushed.
10769         sleep 6
10770         local blk1=$(do_facet $SINGLEMDS \
10771                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10772                      grep Blockcount | awk '{print $4}')
10773
10774         # Remove old files, some OI blocks will become idle.
10775         unlinkmany $myDIR/t- 10000
10776         # Create new files, idle OI blocks should be reused.
10777         createmany -o $myDIR/t- 2000
10778         do_facet $SINGLEMDS sync
10779         # Make sure journal flushed.
10780         sleep 6
10781         local blk2=$(do_facet $SINGLEMDS \
10782                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10783                      grep Blockcount | awk '{print $4}')
10784
10785         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10786 }
10787 run_test 228a "try to reuse idle OI blocks"
10788
10789 test_228b() {
10790         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10791         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10792                 skip "non-ldiskfs backend" && return
10793
10794         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10795         local myDIR=$DIR/$tdir
10796
10797         mkdir -p $myDIR
10798         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10799         $LCTL set_param fail_loc=0x80001002
10800         createmany -o $myDIR/t- 10000
10801         $LCTL set_param fail_loc=0
10802         # The guard is current the largest FID holder
10803         touch $myDIR/guard
10804         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10805                     tr -d '[')
10806         local IDX=$(($SEQ % 64))
10807
10808         do_facet $SINGLEMDS sync
10809         # Make sure journal flushed.
10810         sleep 6
10811         local blk1=$(do_facet $SINGLEMDS \
10812                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10813                      grep Blockcount | awk '{print $4}')
10814
10815         # Remove old files, some OI blocks will become idle.
10816         unlinkmany $myDIR/t- 10000
10817
10818         # stop the MDT
10819         stop $SINGLEMDS || error "Fail to stop MDT."
10820         # remount the MDT
10821         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10822
10823         df $MOUNT || error "Fail to df."
10824         # Create new files, idle OI blocks should be reused.
10825         createmany -o $myDIR/t- 2000
10826         do_facet $SINGLEMDS sync
10827         # Make sure journal flushed.
10828         sleep 6
10829         local blk2=$(do_facet $SINGLEMDS \
10830                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10831                      grep Blockcount | awk '{print $4}')
10832
10833         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10834 }
10835 run_test 228b "idle OI blocks can be reused after MDT restart"
10836
10837 #LU-1881
10838 test_228c() {
10839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10840         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10841                 skip "non-ldiskfs backend" && return
10842
10843         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10844         local myDIR=$DIR/$tdir
10845
10846         mkdir -p $myDIR
10847         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10848         $LCTL set_param fail_loc=0x80001002
10849         # 20000 files can guarantee there are index nodes in the OI file
10850         createmany -o $myDIR/t- 20000
10851         $LCTL set_param fail_loc=0
10852         # The guard is current the largest FID holder
10853         touch $myDIR/guard
10854         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10855                     tr -d '[')
10856         local IDX=$(($SEQ % 64))
10857
10858         do_facet $SINGLEMDS sync
10859         # Make sure journal flushed.
10860         sleep 6
10861         local blk1=$(do_facet $SINGLEMDS \
10862                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10863                      grep Blockcount | awk '{print $4}')
10864
10865         # Remove old files, some OI blocks will become idle.
10866         unlinkmany $myDIR/t- 20000
10867         rm -f $myDIR/guard
10868         # The OI file should become empty now
10869
10870         # Create new files, idle OI blocks should be reused.
10871         createmany -o $myDIR/t- 2000
10872         do_facet $SINGLEMDS sync
10873         # Make sure journal flushed.
10874         sleep 6
10875         local blk2=$(do_facet $SINGLEMDS \
10876                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10877                      grep Blockcount | awk '{print $4}')
10878
10879         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10880 }
10881 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10882
10883 test_230a() {
10884         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10885         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10886         local MDTIDX=1
10887
10888         mkdir -p $DIR/$tdir/test_230_local
10889         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10890         [ $mdt_idx -ne 0 ] &&
10891                 error "create local directory on wrong MDT $mdt_idx"
10892
10893         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10894                         error "create remote directory failed"
10895         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10896         [ $mdt_idx -ne $MDTIDX ] &&
10897                 error "create remote directory on wrong MDT $mdt_idx"
10898
10899         createmany -o $DIR/$tdir/test_230/t- 10 ||
10900                 error "create files on remote directory failed"
10901         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10902         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10903         rm -r $DIR/$tdir || error "unlink remote directory failed"
10904 }
10905 run_test 230a "Create remote directory and files under the remote directory"
10906
10907 test_230b() {
10908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10909         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10910         local MDTIDX=1
10911         local remote_dir=$DIR/$tdir/remote_dir
10912         local rc=0
10913
10914         mkdir -p $DIR/$tdir
10915         $LFS mkdir -i $MDTIDX $remote_dir ||
10916                 error "create remote directory failed"
10917
10918         $LFS mkdir -i 0 $remote_dir/new_dir &&
10919                 error "nested remote directory create succeed!"
10920
10921         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=1
10922         $LFS mkdir -i 0 $remote_dir/new_dir || rc=$?
10923         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=0
10924
10925         [ $rc -ne 0 ] &&
10926            error "create remote directory failed after set enable_remote_dir"
10927
10928         rm -rf $remote_dir || error "first unlink remote directory failed"
10929
10930         $RUNAS -G$RUNAS_GID $LFS mkdir -i $MDTIDX $DIR/$tfile &&
10931                                                         error "chown worked"
10932
10933         do_facet mds$MDTIDX lctl set_param \
10934                                 mdt.*.enable_remote_dir_gid=$RUNAS_GID
10935         $LFS mkdir -i $MDTIDX $remote_dir || rc=$?
10936         do_facet mds$MDTIDX lctl set_param mdt.*.enable_remote_dir_gid=0
10937
10938         [ $rc -ne 0 ] &&
10939            error "create remote dir failed after set enable_remote_dir_gid"
10940
10941         rm -r $DIR/$tdir || error "second unlink remote directory failed"
10942 }
10943 run_test 230b "nested remote directory should be failed"
10944
10945 test_231a()
10946 {
10947         # For simplicity this test assumes that max_pages_per_rpc
10948         # is the same across all OSCs
10949         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
10950         local bulk_size=$((max_pages * 4096))
10951
10952         mkdir -p $DIR/$tdir
10953
10954         # clear the OSC stats
10955         $LCTL set_param osc.*.stats=0 &>/dev/null
10956
10957         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
10958         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
10959                 oflag=direct &>/dev/null || error "dd failed"
10960
10961         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
10962         if [ x$nrpcs != "x1" ]; then
10963                 error "found $nrpc ost_write RPCs, not 1 as expected"
10964         fi
10965
10966         # Drop the OSC cache, otherwise we will read from it
10967         cancel_lru_locks osc
10968
10969         # clear the OSC stats
10970         $LCTL set_param osc.*.stats=0 &>/dev/null
10971
10972         # Client reads $bulk_size.
10973         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
10974                 iflag=direct &>/dev/null || error "dd failed"
10975
10976         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
10977         if [ x$nrpcs != "x1" ]; then
10978                 error "found $nrpc ost_read RPCs, not 1 as expected"
10979         fi
10980 }
10981 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
10982
10983 test_231b() {
10984         mkdir -p $DIR/$tdir
10985         local i
10986         for i in {0..1023}; do
10987                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
10988                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
10989                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
10990         done
10991         sync
10992 }
10993 run_test 231b "must not assert on fully utilized OST request buffer"
10994
10995 test_232() {
10996         mkdir -p $DIR/$tdir
10997         #define OBD_FAIL_LDLM_OST_LVB            0x31c
10998         $LCTL set_param fail_loc=0x31c
10999
11000         # ignore dd failure
11001         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
11002
11003         $LCTL set_param fail_loc=0
11004         umount_client $MOUNT || error "umount failed"
11005         mount_client $MOUNT || error "mount failed"
11006 }
11007 run_test 232 "failed lock should not block umount"
11008
11009 #
11010 # tests that do cleanup/setup should be run at the end
11011 #
11012
11013 test_900() {
11014         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11015         local ls
11016         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
11017         $LCTL set_param fail_loc=0x903
11018         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
11019         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
11020                 echo "clear" > $ls
11021         done
11022         FAIL_ON_ERROR=true cleanup
11023         FAIL_ON_ERROR=true setup
11024 }
11025 run_test 900 "umount should not race with any mgc requeue thread"
11026
11027 complete $SECONDS
11028 check_and_cleanup_lustre
11029 if [ "$I_MOUNTED" != "yes" ]; then
11030         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
11031 fi
11032 exit_status