Whamcloud - gitweb
LU-3066 llite: fix crash in fdatasync(directory)
[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-1593 LU-2610 LU-2833 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 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 check_swap_layouts_support()
93 {
94         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
95                 { skip "Does not support layout lock."; return 0; }
96         return 1
97 }
98
99 if [ "$ONLY" == "cleanup" ]; then
100        sh llmountcleanup.sh
101        exit 0
102 fi
103
104 check_and_setup_lustre
105
106 DIR=${DIR:-$MOUNT}
107 assert_DIR
108
109 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
110     awk '{gsub(/_UUID/,""); print $1}' | head -1)
111 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
112 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
113 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
114 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
115 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
116 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
117
118 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
119 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
120 rm -rf $DIR/[Rdfs][0-9]*
121
122 # $RUNAS_ID may get set incorrectly somewhere else
123 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
124
125 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
126
127 build_test_filter
128
129 if [ "${ONLY}" = "MOUNT" ] ; then
130         echo "Lustre is up, please go on"
131         exit
132 fi
133
134 echo "preparing for tests involving mounts"
135 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
136 touch $EXT2_DEV
137 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
138 echo # add a newline after mke2fs.
139
140 umask 077
141
142 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
143 lctl set_param debug=-1 2> /dev/null || true
144 test_0() {
145         touch $DIR/$tfile
146         $CHECKSTAT -t file $DIR/$tfile || error
147         rm $DIR/$tfile
148         $CHECKSTAT -a $DIR/$tfile || error
149 }
150 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
151
152 test_0b() {
153         chmod 0755 $DIR || error
154         $CHECKSTAT -p 0755 $DIR || error
155 }
156 run_test 0b "chmod 0755 $DIR ============================="
157
158 test_0c() {
159     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
160     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
161 }
162 run_test 0c "check import proc ============================="
163
164 test_1a() {
165         test_mkdir -p $DIR/$tdir
166         test_mkdir -p $DIR/$tdir/d2
167         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
168         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
169 }
170 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
171
172 test_1b() {
173         rmdir $DIR/$tdir/d2
174         rmdir $DIR/$tdir
175         $CHECKSTAT -a $DIR/$tdir || error
176 }
177 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
178
179 test_2a() {
180         test_mkdir $DIR/$tdir
181         touch $DIR/$tdir/$tfile
182         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
183 }
184 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
185
186 test_2b() {
187         rm -r $DIR/$tdir
188         $CHECKSTAT -a $DIR/$tdir || error
189 }
190 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
191
192 test_3a() {
193         test_mkdir -p $DIR/$tdir
194         $CHECKSTAT -t dir $DIR/$tdir || error
195 }
196 run_test 3a "mkdir .../d3 ======================================"
197
198 test_3b() {
199         if [ ! -d $DIR/$tdir ]; then
200                 mkdir $DIR/$tdir
201         fi
202         touch $DIR/$tdir/$tfile
203         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
204 }
205 run_test 3b "touch .../d3/f ===================================="
206
207 test_3c() {
208         rm -r $DIR/$tdir
209         $CHECKSTAT -a $DIR/$tdir || error
210 }
211 run_test 3c "rm -r .../d3 ======================================"
212
213 test_4a() {
214         test_mkdir -p $DIR/$tdir
215         $CHECKSTAT -t dir $DIR/$tdir || error
216 }
217 run_test 4a "mkdir .../d4 ======================================"
218
219 test_4b() {
220         if [ ! -d $DIR/$tdir ]; then
221                 test_mkdir $DIR/$tdir
222         fi
223         test_mkdir $DIR/$tdir/d2
224         mkdir $DIR/$tdir/d2
225         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
226 }
227 run_test 4b "mkdir .../d4/d2 ==================================="
228
229 test_5() {
230         test_mkdir $DIR/$tdir
231         test_mkdir $DIR/$tdir/d2
232         chmod 0707 $DIR/$tdir/d2
233         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error
234 }
235 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
236
237 test_6a() {
238         touch $DIR/$tfile
239         chmod 0666 $DIR/$tfile || error
240         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
241 }
242 run_test 6a "touch .../f6a; chmod .../f6a ======================"
243
244 test_6b() {
245         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
246         if [ ! -f $DIR/$tfile ]; then
247                 touch $DIR/$tfile
248                 chmod 0666 $DIR/$tfile
249         fi
250         $RUNAS chmod 0444 $DIR/$tfile && error
251         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
252 }
253 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
254
255 test_6c() {
256         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
257         touch $DIR/$tfile
258         chown $RUNAS_ID $DIR/$tfile || error
259         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
260 }
261 run_test 6c "touch .../f6c; chown .../f6c ======================"
262
263 test_6d() {
264         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
265         if [ ! -f $DIR/$tfile ]; then
266                 touch $DIR/$tfile
267                 chown $RUNAS_ID $DIR/$tfile
268         fi
269         $RUNAS chown $UID $DIR/$tfile && error
270         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
271 }
272 run_test 6d "$RUNAS chown .../f6c (should return error) =="
273
274 test_6e() {
275         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
276         touch $DIR/$tfile
277         chgrp $RUNAS_ID $DIR/$tfile || error
278         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
279 }
280 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
281
282 test_6f() {
283         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
284         if [ ! -f $DIR/$tfile ]; then
285                 touch $DIR/$tfile
286                 chgrp $RUNAS_ID $DIR/$tfile
287         fi
288         $RUNAS chgrp $UID $DIR/$tfile && error
289         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
290 }
291 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
292
293 test_6g() {
294         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
295         test_mkdir $DIR/$tdir || error
296         chmod 777 $DIR/$tdir || error
297         $RUNAS mkdir $DIR/$tdir/d || error
298         chmod g+s $DIR/$tdir/d || error
299         test_mkdir $DIR/$tdir/d/subdir
300         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir || error
301 }
302 run_test 6g "Is new dir in sgid dir inheriting group?"
303
304 test_6h() { # bug 7331
305         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
306         touch $DIR/$tfile || error "touch failed"
307         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
308         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
309                 error "chown worked"
310         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile || error
311 }
312 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
313
314 test_7a() {
315         test_mkdir $DIR/$tdir
316         $MCREATE $DIR/$tdir/$tfile
317         chmod 0666 $DIR/$tdir/$tfile
318         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
319 }
320 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
321
322 test_7b() {
323         if [ ! -d $DIR/$tdir ]; then
324                 mkdir $DIR/$tdir
325         fi
326         $MCREATE $DIR/$tdir/$tfile
327         echo -n foo > $DIR/$tdir/$tfile
328         [ "`cat $DIR/$tdir/$tfile`" = "foo" ] || error
329         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error
330 }
331 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
332
333 test_8() {
334         test_mkdir $DIR/$tdir
335         touch $DIR/$tdir/$tfile
336         chmod 0666 $DIR/$tdir/$tfile
337         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
338 }
339 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
340
341 test_9() {
342         test_mkdir $DIR/$tdir
343         test_mkdir $DIR/$tdir/d2
344         test_mkdir $DIR/$tdir/d2/d3
345         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error
346 }
347 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
348
349 test_10() {
350         test_mkdir $DIR/$tdir
351         test_mkdir $DIR/$tdir/d2
352         touch $DIR/$tdir/d2/$tfile
353         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile || error
354 }
355 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
356
357 test_11() {
358         test_mkdir $DIR/$tdir
359         test_mkdir $DIR/$tdir/d2
360         chmod 0666 $DIR/$tdir/d2
361         chmod 0705 $DIR/$tdir/d2
362         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 || error
363 }
364 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
365
366 test_12() {
367         test_mkdir $DIR/$tdir
368         touch $DIR/$tdir/$tfile
369         chmod 0666 $DIR/$tdir/$tfile
370         chmod 0654 $DIR/$tdir/$tfile
371         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile || error
372 }
373 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
374
375 test_13() {
376         test_mkdir $DIR/$tdir
377         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
378         >  $DIR/$tdir/$tfile
379         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile || error
380 }
381 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
382
383 test_14() {
384         test_mkdir $DIR/$tdir
385         touch $DIR/$tdir/$tfile
386         rm $DIR/$tdir/$tfile
387         $CHECKSTAT -a $DIR/$tdir/$tfile || error
388 }
389 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
390
391 test_15() {
392         test_mkdir $DIR/$tdir
393         touch $DIR/$tdir/$tfile
394         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
395         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 || error
396 }
397 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
398
399 test_16() {
400         test_mkdir $DIR/$tdir
401         touch $DIR/$tdir/$tfile
402         rm -rf $DIR/$tdir/$tfile
403         $CHECKSTAT -a $DIR/$tdir/$tfile || error
404 }
405 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
406
407 test_17a() {
408         test_mkdir -p $DIR/$tdir
409         touch $DIR/$tdir/$tfile
410         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
411         ls -l $DIR/$tdir
412         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist || error
413         $CHECKSTAT -f -t f $DIR/$tdir/l-exist || error
414         rm -f $DIR/$tdir/l-exist
415         $CHECKSTAT -a $DIR/$tdir/l-exist || error
416 }
417 run_test 17a "symlinks: create, remove (real) =================="
418
419 test_17b() {
420         test_mkdir -p $DIR/$tdir
421         ln -s no-such-file $DIR/$tdir/l-dangle
422         ls -l $DIR/$tdir
423         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle || error
424         $CHECKSTAT -fa $DIR/$tdir/l-dangle || error
425         rm -f $DIR/$tdir/l-dangle
426         $CHECKSTAT -a $DIR/$tdir/l-dangle || error
427 }
428 run_test 17b "symlinks: create, remove (dangling) =============="
429
430 test_17c() { # bug 3440 - don't save failed open RPC for replay
431         test_mkdir -p $DIR/$tdir
432         ln -s foo $DIR/$tdir/$tfile
433         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
434 }
435 run_test 17c "symlinks: open dangling (should return error) ===="
436
437 test_17d() {
438         test_mkdir -p $DIR/$tdir
439         ln -s foo $DIR/$tdir/$tfile
440         touch $DIR/$tdir/$tfile || error "creating to new symlink"
441 }
442 run_test 17d "symlinks: create dangling ========================"
443
444 test_17e() {
445         test_mkdir -p $DIR/$tdir
446         local foo=$DIR/$tdir/$tfile
447         ln -s $foo $foo || error "create symlink failed"
448         ls -l $foo || error "ls -l failed"
449         ls $foo && error "ls not failed" || true
450 }
451 run_test 17e "symlinks: create recursive symlink (should return error) ===="
452
453 test_17f() {
454         test_mkdir -p $DIR/d17f
455         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
456         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
457         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
458         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
459         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
460         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
461         ls -l  $DIR/d17f
462 }
463 run_test 17f "symlinks: long and very long symlink name ========================"
464
465 # str_repeat(S, N) generate a string that is string S repeated N times
466 str_repeat() {
467         local s=$1
468         local n=$2
469         local ret=''
470         while [ $((n -= 1)) -ge 0 ]; do
471                 ret=$ret$s
472         done
473         echo $ret
474 }
475
476 # Long symlinks and LU-2241
477 test_17g() {
478         test_mkdir -p $DIR/$tdir
479         local TESTS="59 60 61 4094 4095"
480
481         # Fix for inode size boundary in 2.1.4
482         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
483                 TESTS="4094 4095"
484
485         # Patch not applied to 2.2 or 2.3 branches
486         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
487         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
488                 TESTS="4094 4095"
489
490         for i in $TESTS; do
491                 local SYMNAME=$(str_repeat 'x' $i)
492                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
493                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
494         done
495 }
496 run_test 17g "symlinks: really long symlink name and inode boundaries"
497
498 test_17h() { #bug 17378
499         remote_mds_nodsh && skip "remote MDS with nodsh" && return
500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
501         local mdt_idx
502         test_mkdir -p $DIR/$tdir
503         if [ $MDSCOUNT -gt 1 ]; then
504                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
505         else
506                 mdt_idx=0
507         fi
508         $SETSTRIPE -c -1 $DIR/$tdir
509 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
510         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
511         touch $DIR/$tdir/$tfile || true
512 }
513 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
514
515 test_17i() { #bug 20018
516         remote_mds_nodsh && skip "remote MDS with nodsh" && return
517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
518         test_mkdir -p $DIR/$tdir
519         local foo=$DIR/$tdir/$tfile
520         local mdt_idx
521         if [ $MDSCOUNT -gt 1 ]; then
522                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
523         else
524                 mdt_idx=0
525         fi
526         ln -s $foo $foo || error "create symlink failed"
527 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
528         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
529         ls -l $foo && error "error not detected"
530         return 0
531 }
532 run_test 17i "don't panic on short symlink"
533
534 test_17k() { #bug 22301
535         rsync --help | grep -q xattr ||
536                 skip_env "$(rsync --version| head -1) does not support xattrs"
537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
538         test_mkdir -p $DIR/$tdir
539         test_mkdir -p $DIR/$tdir.new
540         touch $DIR/$tdir/$tfile
541         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
542         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
543                 error "rsync failed with xattrs enabled"
544 }
545 run_test 17k "symlinks: rsync with xattrs enabled ========================="
546
547 test_17l() { # LU-279
548         mkdir -p $DIR/$tdir
549         touch $DIR/$tdir/$tfile
550         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
551         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
552                 # -h to not follow symlinks. -m '' to list all the xattrs.
553                 # grep to remove first line: '# file: $path'.
554                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
555                 do
556                         lgetxattr_size_check $path $xattr ||
557                                 error "lgetxattr_size_check $path $xattr failed"
558                 done
559         done
560 }
561 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
562
563 # LU-1540
564 test_17m() {
565         local short_sym="0123456789"
566         local WDIR=$DIR/${tdir}m
567         local mds_index
568         local devname
569         local cmd
570         local i
571         local rc=0
572
573         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
574         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
575                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
576
577         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
578                 skip "only for ldiskfs MDT" && return 0
579
580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
581
582         mkdir -p $WDIR
583         long_sym=$short_sym
584         # create a long symlink file
585         for ((i = 0; i < 4; ++i)); do
586                 long_sym=${long_sym}${long_sym}
587         done
588
589         echo "create 512 short and long symlink files under $WDIR"
590         for ((i = 0; i < 256; ++i)); do
591                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
592                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
593         done
594
595         echo "erase them"
596         rm -f $WDIR/*
597         sync
598         wait_delete_completed
599
600         echo "recreate the 512 symlink files with a shorter string"
601         for ((i = 0; i < 512; ++i)); do
602                 # rewrite the symlink file with a shorter string
603                 ln -sf ${long_sym} $WDIR/long-$i
604                 ln -sf ${short_sym} $WDIR/short-$i
605         done
606
607         mds_index=$($LFS getstripe -M $WDIR)
608         mds_index=$((mds_index+1))
609         devname=$(mdsdevname $mds_index)
610         cmd="$E2FSCK -fnvd $devname"
611
612         echo "stop and checking mds${mds_index}: $cmd"
613         # e2fsck should not return error
614         stop mds${mds_index} -f
615         do_facet mds${mds_index} $cmd || rc=$?
616
617         start mds${mds_index} $devname $MDS_MOUNT_OPTS
618         df $MOUNT > /dev/null 2>&1
619         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
620                 "short/long symlink MDT: rc=$rc"
621         return $rc
622 }
623 run_test 17m "run e2fsck against MDT which contains short/long symlink"
624
625 check_fs_consistency_17n() {
626         local mdt_index
627         local devname
628         local cmd
629         local rc=0
630
631         for mdt_index in $(seq 1 $MDSCOUNT); do
632                 devname=$(mdsdevname $mdt_index)
633                 cmd="$E2FSCK -fnvd $devname"
634
635                 echo "stop and checking mds${mdt_index}: $cmd"
636                 # e2fsck should not return error
637                 stop mds${mdt_index}
638                 do_facet mds${mdt_index} $cmd || rc=$?
639
640                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
641                 df $MOUNT > /dev/null 2>&1
642                 [ $rc -ne 0 ] && break
643         done
644         return $rc
645 }
646
647 test_17n() {
648         local i
649
650         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
651         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
652                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
653
654         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
655                 skip "only for ldiskfs MDT" && return 0
656
657         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
658
659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
660
661         mkdir -p $DIR/$tdir
662         for ((i=0; i<10; i++)); do
663                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
664                         error "create remote dir error $i"
665                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
666                         error "create files under remote dir failed $i"
667         done
668
669         check_fs_consistency_17n || error "e2fsck report error"
670
671         for ((i=0;i<10;i++)); do
672                 rm -rf $DIR/$tdir/remote_dir_${i} ||
673                         error "destroy remote dir error $i"
674         done
675
676         check_fs_consistency_17n || error "e2fsck report error"
677 }
678 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
679
680 test_18() {
681         touch $DIR/f || error "Failed to touch $DIR/f: $?"
682         ls $DIR || error "Failed to ls $DIR: $?"
683 }
684 run_test 18 "touch .../f ; ls ... =============================="
685
686 test_19a() {
687         touch $DIR/f19
688         ls -l $DIR
689         rm $DIR/f19
690         $CHECKSTAT -a $DIR/f19 || error
691 }
692 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
693
694 test_19b() {
695         ls -l $DIR/f19 && error || true
696 }
697 run_test 19b "ls -l .../f19 (should return error) =============="
698
699 test_19c() {
700         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
701         $RUNAS touch $DIR/f19 && error || true
702 }
703 run_test 19c "$RUNAS touch .../f19 (should return error) =="
704
705 test_19d() {
706         cat $DIR/f19 && error || true
707 }
708 run_test 19d "cat .../f19 (should return error) =============="
709
710 test_20() {
711         touch $DIR/f
712         rm $DIR/f
713         log "1 done"
714         touch $DIR/f
715         rm $DIR/f
716         log "2 done"
717         touch $DIR/f
718         rm $DIR/f
719         log "3 done"
720         $CHECKSTAT -a $DIR/f || error
721 }
722 run_test 20 "touch .../f ; ls -l ... ==========================="
723
724 test_21() {
725         test_mkdir $DIR/d21
726         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
727         ln -s dangle $DIR/d21/link
728         echo foo >> $DIR/d21/link
729         cat $DIR/d21/dangle
730         $CHECKSTAT -t link $DIR/d21/link || error
731         $CHECKSTAT -f -t file $DIR/d21/link || error
732 }
733 run_test 21 "write to dangling link ============================"
734
735 test_22() {
736         WDIR=$DIR/$tdir
737         test_mkdir -p $DIR/$tdir
738         chown $RUNAS_ID:$RUNAS_GID $WDIR
739         (cd $WDIR || error "cd $WDIR failed";
740         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
741         $RUNAS tar xf -)
742         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
743         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
744         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
745 }
746 run_test 22 "unpack tar archive as non-root user ==============="
747
748 # was test_23
749 test_23a() {
750         test_mkdir -p $DIR/$tdir
751         local file=$DIR/$tdir/$tfile
752
753         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
754         openfile -f O_CREAT:O_EXCL $file &&
755                 error "$file recreate succeeded" || true
756 }
757 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
758
759 test_23b() { # bug 18988
760         test_mkdir -p $DIR/$tdir
761         local file=$DIR/$tdir/$tfile
762
763         rm -f $file
764         echo foo > $file || error "write filed"
765         echo bar >> $file || error "append filed"
766         $CHECKSTAT -s 8 $file || error "wrong size"
767         rm $file
768 }
769 run_test 23b "O_APPEND check =========================="
770
771 test_24a() {
772         echo '== rename sanity =============================================='
773         echo '-- same directory rename'
774         test_mkdir $DIR/R1
775         touch $DIR/R1/f
776         mv $DIR/R1/f $DIR/R1/g
777         $CHECKSTAT -t file $DIR/R1/g || error
778 }
779 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
780
781 test_24b() {
782         test_mkdir $DIR/R2
783         touch $DIR/R2/{f,g}
784         mv $DIR/R2/f $DIR/R2/g
785         $CHECKSTAT -a $DIR/R2/f || error
786         $CHECKSTAT -t file $DIR/R2/g || error
787 }
788 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
789
790 test_24c() {
791         test_mkdir $DIR/R3
792         test_mkdir $DIR/R3/f
793         mv $DIR/R3/f $DIR/R3/g
794         $CHECKSTAT -a $DIR/R3/f || error
795         $CHECKSTAT -t dir $DIR/R3/g || error
796 }
797 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
798
799 test_24d() {
800         test_mkdir $DIR/R4
801         test_mkdir $DIR/R4/f
802         test_mkdir $DIR/R4/g
803         mrename $DIR/R4/f $DIR/R4/g
804         $CHECKSTAT -a $DIR/R4/f || error
805         $CHECKSTAT -t dir $DIR/R4/g || error
806 }
807 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
808
809 test_24e() {
810         echo '-- cross directory renames --'
811         test_mkdir $DIR/R5a
812         test_mkdir $DIR/R5b
813         touch $DIR/R5a/f
814         mv $DIR/R5a/f $DIR/R5b/g
815         $CHECKSTAT -a $DIR/R5a/f || error
816         $CHECKSTAT -t file $DIR/R5b/g || error
817 }
818 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
819
820 test_24f() {
821         test_mkdir $DIR/R6a
822         test_mkdir $DIR/R6b
823         touch $DIR/R6a/f $DIR/R6b/g
824         mv $DIR/R6a/f $DIR/R6b/g
825         $CHECKSTAT -a $DIR/R6a/f || error
826         $CHECKSTAT -t file $DIR/R6b/g || error
827 }
828 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
829
830 test_24g() {
831         test_mkdir $DIR/R7a
832         test_mkdir $DIR/R7b
833         test_mkdir $DIR/R7a/d
834         mv $DIR/R7a/d $DIR/R7b/e
835         $CHECKSTAT -a $DIR/R7a/d || error
836         $CHECKSTAT -t dir $DIR/R7b/e || error
837 }
838 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
839
840 test_24h() {
841         test_mkdir $DIR/R8a
842         test_mkdir $DIR/R8b
843         test_mkdir $DIR/R8a/d
844         test_mkdir $DIR/R8b/e
845         mrename $DIR/R8a/d $DIR/R8b/e
846         $CHECKSTAT -a $DIR/R8a/d || error
847         $CHECKSTAT -t dir $DIR/R8b/e || error
848 }
849 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
850
851 test_24i() {
852         echo "-- rename error cases"
853         test_mkdir $DIR/R9
854         test_mkdir $DIR/R9/a
855         touch $DIR/R9/f
856         mrename $DIR/R9/f $DIR/R9/a
857         $CHECKSTAT -t file $DIR/R9/f || error
858         $CHECKSTAT -t dir  $DIR/R9/a || error
859         $CHECKSTAT -a $DIR/R9/a/f || error
860 }
861 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
862
863 test_24j() {
864         test_mkdir $DIR/R10
865         mrename $DIR/R10/f $DIR/R10/g
866         $CHECKSTAT -t dir $DIR/R10 || error
867         $CHECKSTAT -a $DIR/R10/f || error
868         $CHECKSTAT -a $DIR/R10/g || error
869 }
870 run_test 24j "source does not exist ============================"
871
872 test_24k() {
873         test_mkdir $DIR/R11a
874         test_mkdir $DIR/R11a/d
875         touch $DIR/R11a/f
876         mv $DIR/R11a/f $DIR/R11a/d
877         $CHECKSTAT -a $DIR/R11a/f || error
878         $CHECKSTAT -t file $DIR/R11a/d/f || error
879 }
880 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
881
882 # bug 2429 - rename foo foo foo creates invalid file
883 test_24l() {
884         f="$DIR/f24l"
885         $MULTIOP $f OcNs || error
886 }
887 run_test 24l "Renaming a file to itself ========================"
888
889 test_24m() {
890         f="$DIR/f24m"
891         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
892         # on ext3 this does not remove either the source or target files
893         # though the "expected" operation would be to remove the source
894         $CHECKSTAT -t file ${f} || error "${f} missing"
895         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
896 }
897 run_test 24m "Renaming a file to a hard link to itself ========="
898
899 test_24n() {
900     f="$DIR/f24n"
901     # this stats the old file after it was renamed, so it should fail
902     touch ${f}
903     $CHECKSTAT ${f}
904     mv ${f} ${f}.rename
905     $CHECKSTAT ${f}.rename
906     $CHECKSTAT -a ${f}
907 }
908 run_test 24n "Statting the old file after renaming (Posix rename 2)"
909
910 test_24o() {
911         check_kernel_version 37 || return 0
912         test_mkdir -p $DIR/d24o
913         rename_many -s random -v -n 10 $DIR/d24o
914 }
915 run_test 24o "rename of files during htree split ==============="
916
917 test_24p() {
918         test_mkdir $DIR/R12a
919         test_mkdir $DIR/R12b
920         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
921         mrename $DIR/R12a $DIR/R12b
922         $CHECKSTAT -a $DIR/R12a || error
923         $CHECKSTAT -t dir $DIR/R12b || error
924         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
925         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
926 }
927 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
928
929 cleanup_multiop_pause() {
930         trap 0
931         kill -USR1 $MULTIPID
932 }
933
934 test_24q() {
935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
936         test_mkdir $DIR/R13a
937         test_mkdir $DIR/R13b
938         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
939         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
940         MULTIPID=$!
941
942         trap cleanup_multiop_pause EXIT
943         mrename $DIR/R13a $DIR/R13b
944         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
945         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
946         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
947         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
948         cleanup_multiop_pause
949         wait $MULTIPID || error "multiop close failed"
950 }
951 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
952
953 test_24r() { #bug 3789
954         test_mkdir $DIR/R14a
955         test_mkdir $DIR/R14a/b
956         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
957         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
958         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
959 }
960 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
961
962 test_24s() {
963         test_mkdir $DIR/R15a
964         test_mkdir $DIR/R15a/b
965         test_mkdir $DIR/R15a/b/c
966         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
967         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
968         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
969 }
970 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
971 test_24t() {
972         test_mkdir $DIR/R16a
973         test_mkdir $DIR/R16a/b
974         test_mkdir $DIR/R16a/b/c
975         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
976         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
977         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
978 }
979 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
980
981 test_24u() { # bug12192
982         rm -rf $DIR/$tfile
983         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
984         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
985 }
986 run_test 24u "create stripe file"
987
988 page_size() {
989         getconf PAGE_SIZE
990 }
991
992 simple_cleanup_common() {
993         trap 0
994         rm -rf $DIR/$tdir
995         wait_delete_completed
996 }
997
998 max_pages_per_rpc() {
999         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
1000 }
1001
1002 test_24v() {
1003         local NRFILES=100000
1004         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
1005         [ $FREE_INODES -lt $NRFILES ] && \
1006                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1007                 return
1008
1009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1010         trap simple_cleanup_common EXIT
1011
1012         mkdir -p $DIR/$tdir
1013         createmany -m $DIR/$tdir/$tfile $NRFILES
1014
1015         cancel_lru_locks mdc
1016         lctl set_param mdc.*.stats clear
1017
1018         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1019
1020         # LU-5 large readdir
1021         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1022         #               8 bytes for name(filename is mostly 5 in this test) +
1023         #               8 bytes for luda_type
1024         # take into account of overhead in lu_dirpage header and end mark in
1025         # each page, plus one in RPC_NUM calculation.
1026         DIRENT_SIZE=48
1027         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1028         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1029         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1030                                 awk '/^mds_readpage/ {print $2}')
1031         [ $mds_readpage -gt $RPC_NUM ] && \
1032                 error "large readdir doesn't take effect"
1033
1034         simple_cleanup_common
1035 }
1036 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1037
1038 test_24w() { # bug21506
1039         SZ1=234852
1040         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1041         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1042         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1043         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1044         [ "$SZ1" = "$SZ2" ] || \
1045                 error "Error reading at the end of the file $tfile"
1046 }
1047 run_test 24w "Reading a file larger than 4Gb"
1048
1049 test_24x() {
1050         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1052         local MDTIDX=1
1053         local remote_dir=$DIR/$tdir/remote_dir
1054
1055         mkdir -p $DIR/$tdir
1056         $LFS mkdir -i $MDTIDX $remote_dir ||
1057                 error "create remote directory failed"
1058
1059         mkdir -p $DIR/$tdir/src_dir
1060         touch $DIR/$tdir/src_file
1061         mkdir -p $remote_dir/tgt_dir
1062         touch $remote_dir/tgt_file
1063
1064         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1065                 error "rename dir cross MDT works!"
1066
1067         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1068                 error "rename file cross MDT works!"
1069
1070         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1071                 error "ln file cross MDT should not work!"
1072
1073         rm -rf $DIR/$tdir || error "Can not delete directories"
1074 }
1075 run_test 24x "cross rename/link should be failed"
1076
1077 test_24y() {
1078         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1079         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1080         local MDTIDX=1
1081         local remote_dir=$DIR/$tdir/remote_dir
1082
1083         mkdir -p $DIR/$tdir
1084         $LFS mkdir -i $MDTIDX $remote_dir ||
1085                    error "create remote directory failed"
1086
1087         mkdir -p $remote_dir/src_dir
1088         touch $remote_dir/src_file
1089         mkdir -p $remote_dir/tgt_dir
1090         touch $remote_dir/tgt_file
1091
1092         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1093                 error "rename subdir in the same remote dir failed!"
1094
1095         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1096                 error "rename files in the same remote dir failed!"
1097
1098         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1099                 error "link files in the same remote dir failed!"
1100
1101         rm -rf $DIR/$tdir || error "Can not delete directories"
1102 }
1103 run_test 24y "rename/link on the same dir should succeed"
1104
1105 test_24z() {
1106         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1107         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1108         local MDTIDX=1
1109         local remote_src=$DIR/$tdir/remote_dir
1110         local remote_tgt=$DIR/$tdir/remote_tgt
1111
1112         mkdir -p $DIR/$tdir
1113         $LFS mkdir -i $MDTIDX $remote_src ||
1114                    error "create remote directory failed"
1115
1116         $LFS mkdir -i $MDTIDX $remote_tgt ||
1117                    error "create remote directory failed"
1118
1119         mrename $remote_src $remote_tgt &&
1120                 error "rename remote dirs should not work!"
1121
1122         rm -rf $DIR/$tdir || error "Can not delete directories"
1123 }
1124 run_test 24z "rename one remote dir to another remote dir should fail"
1125
1126 test_25a() {
1127         echo '== symlink sanity ============================================='
1128
1129         test_mkdir $DIR/d25
1130         ln -s d25 $DIR/s25
1131         touch $DIR/s25/foo || error
1132 }
1133 run_test 25a "create file in symlinked directory ==============="
1134
1135 test_25b() {
1136         [ ! -d $DIR/d25 ] && test_25a
1137         $CHECKSTAT -t file $DIR/s25/foo || error
1138 }
1139 run_test 25b "lookup file in symlinked directory ==============="
1140
1141 test_26a() {
1142         test_mkdir $DIR/d26
1143         test_mkdir $DIR/d26/d26-2
1144         ln -s d26/d26-2 $DIR/s26
1145         touch $DIR/s26/foo || error
1146 }
1147 run_test 26a "multiple component symlink ======================="
1148
1149 test_26b() {
1150         test_mkdir -p $DIR/d26b/d26-2
1151         ln -s d26b/d26-2/foo $DIR/s26-2
1152         touch $DIR/s26-2 || error
1153 }
1154 run_test 26b "multiple component symlink at end of lookup ======"
1155
1156 test_26c() {
1157         test_mkdir $DIR/d26.2
1158         touch $DIR/d26.2/foo
1159         ln -s d26.2 $DIR/s26.2-1
1160         ln -s s26.2-1 $DIR/s26.2-2
1161         ln -s s26.2-2 $DIR/s26.2-3
1162         chmod 0666 $DIR/s26.2-3/foo
1163 }
1164 run_test 26c "chain of symlinks ================================"
1165
1166 # recursive symlinks (bug 439)
1167 test_26d() {
1168         ln -s d26-3/foo $DIR/d26-3
1169 }
1170 run_test 26d "create multiple component recursive symlink ======"
1171
1172 test_26e() {
1173         [ ! -h $DIR/d26-3 ] && test_26d
1174         rm $DIR/d26-3
1175 }
1176 run_test 26e "unlink multiple component recursive symlink ======"
1177
1178 # recursive symlinks (bug 7022)
1179 test_26f() {
1180         test_mkdir -p $DIR/$tdir
1181         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1182         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1183         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1184         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1185         cd $tfile                || error "cd $tfile failed"
1186         ln -s .. dotdot          || error "ln dotdot failed"
1187         ln -s dotdot/lndir lndir || error "ln lndir failed"
1188         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1189         output=`ls $tfile/$tfile/lndir/bar1`
1190         [ "$output" = bar1 ] && error "unexpected output"
1191         rm -r $tfile             || error "rm $tfile failed"
1192         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1193 }
1194 run_test 26f "rm -r of a directory which has recursive symlink ="
1195
1196 test_27a() {
1197         echo '== stripe sanity =============================================='
1198         test_mkdir -p $DIR/d27 || error "mkdir failed"
1199         $GETSTRIPE $DIR/d27
1200         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1201         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1202         pass
1203         log "== test_27a: write to one stripe file ========================="
1204         cp /etc/hosts $DIR/d27/f0 || error
1205 }
1206 run_test 27a "one stripe file =================================="
1207
1208 test_27b() {
1209         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1210         test_mkdir -p $DIR/d27
1211         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1212         $GETSTRIPE -c $DIR/d27/f01
1213         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1214                 error "two-stripe file doesn't have two stripes"
1215 }
1216 run_test 27b "create two stripe file"
1217
1218 test_27c() {
1219         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1220
1221         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1222 }
1223 run_test 27c "write to two stripe file"
1224
1225 test_27d() {
1226         test_mkdir -p $DIR/d27
1227         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1228         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1229         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1230 }
1231 run_test 27d "create file with default settings ================"
1232
1233 test_27e() {
1234         test_mkdir -p $DIR/d27
1235         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1236         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1237         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1238 }
1239 run_test 27e "setstripe existing file (should return error) ======"
1240
1241 test_27f() {
1242         test_mkdir -p $DIR/d27
1243         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1244         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1245         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1246 }
1247 run_test 27f "setstripe with bad stripe size (should return error)"
1248
1249 test_27g() {
1250         test_mkdir -p $DIR/d27
1251         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1252         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1253                 error "$DIR/d27/fnone has object"
1254 }
1255 run_test 27g "$GETSTRIPE with no objects"
1256
1257 test_27i() {
1258         touch $DIR/d27/fsome || error "touch failed"
1259         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1260 }
1261 run_test 27i "$GETSTRIPE with some objects"
1262
1263 test_27j() {
1264         test_mkdir -p $DIR/d27
1265         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1266 }
1267 run_test 27j "setstripe with bad stripe offset (should return error)"
1268
1269 test_27k() { # bug 2844
1270         test_mkdir -p $DIR/d27
1271         FILE=$DIR/d27/f27k
1272         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1273         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1274         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1275         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1276         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1277         dd if=/dev/zero of=$FILE bs=4k count=1
1278         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1279         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1280 }
1281 run_test 27k "limit i_blksize for broken user apps ============="
1282
1283 test_27l() {
1284         test_mkdir -p $DIR/d27
1285         mcreate $DIR/f27l || error "creating file"
1286         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1287                 error "setstripe should have failed" || true
1288 }
1289 run_test 27l "check setstripe permissions (should return error)"
1290
1291 test_27m() {
1292         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1293                 return
1294         if [ $ORIGFREE -gt $MAXFREE ]; then
1295                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1296                 return
1297         fi
1298         trap simple_cleanup_common EXIT
1299         test_mkdir -p $DIR/$tdir
1300         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1301         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1302                 error "dd should fill OST0"
1303         i=2
1304         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1305                 i=`expr $i + 1`
1306                 [ $i -gt 256 ] && break
1307         done
1308         i=`expr $i + 1`
1309         touch $DIR/$tdir/f27m_$i
1310         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1311                 error "OST0 was full but new created file still use it"
1312         i=`expr $i + 1`
1313         touch $DIR/$tdir/f27m_$i
1314         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1315                 error "OST0 was full but new created file still use it"
1316         simple_cleanup_common
1317 }
1318 run_test 27m "create file while OST0 was full =================="
1319
1320 sleep_maxage() {
1321         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1322         sleep $DELAY
1323 }
1324
1325 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1326 # if the OST isn't full anymore.
1327 reset_enospc() {
1328         local OSTIDX=${1:-""}
1329
1330         local list=$(comma_list $(osts_nodes))
1331         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1332
1333         do_nodes $list lctl set_param fail_loc=0
1334         sync    # initiate all OST_DESTROYs from MDS to OST
1335         sleep_maxage
1336 }
1337
1338 exhaust_precreations() {
1339         local OSTIDX=$1
1340         local FAILLOC=$2
1341         local FAILIDX=${3:-$OSTIDX}
1342
1343         test_mkdir -p $DIR/$tdir
1344         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1345         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1346
1347         local OST=$(ostname_from_index $OSTIDX)
1348         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1349                           sed -e 's/_UUID$//;s/^.*-//')
1350
1351         # on the mdt's osc
1352         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1353         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1354         osc.$mdtosc_proc1.prealloc_last_id)
1355         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1356         osc.$mdtosc_proc1.prealloc_next_id)
1357
1358         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1359         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1360
1361         test_mkdir -p $DIR/$tdir/${OST}
1362         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1363 #define OBD_FAIL_OST_ENOSPC              0x215
1364         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1365         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1366         echo "Creating to objid $last_id on ost $OST..."
1367         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1368         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1369         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1370         sleep_maxage
1371 }
1372
1373 exhaust_all_precreations() {
1374         local i
1375         for (( i=0; i < OSTCOUNT; i++ )) ; do
1376                 exhaust_precreations $i $1 -1
1377         done
1378 }
1379
1380 test_27n() {
1381         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1383         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1384         remote_ost_nodsh && skip "remote OST with nodsh" && return
1385
1386         reset_enospc
1387         rm -f $DIR/$tdir/$tfile
1388         exhaust_precreations 0 0x80000215
1389         $SETSTRIPE -c -1 $DIR/$tdir
1390         touch $DIR/$tdir/$tfile || error
1391         $GETSTRIPE $DIR/$tdir/$tfile
1392         reset_enospc
1393 }
1394 run_test 27n "create file with some full OSTs =================="
1395
1396 test_27o() {
1397         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1398         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1399         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1400         remote_ost_nodsh && skip "remote OST with nodsh" && return
1401
1402         reset_enospc
1403         rm -f $DIR/$tdir/$tfile
1404         exhaust_all_precreations 0x215
1405
1406         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1407
1408         reset_enospc
1409         rm -rf $DIR/$tdir/*
1410 }
1411 run_test 27o "create file with all full OSTs (should error) ===="
1412
1413 test_27p() {
1414         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1415         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1416         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1417         remote_ost_nodsh && skip "remote OST with nodsh" && return
1418
1419         reset_enospc
1420         rm -f $DIR/$tdir/$tfile
1421         test_mkdir -p $DIR/$tdir
1422
1423         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1424         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1425         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1426
1427         exhaust_precreations 0 0x80000215
1428         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1429         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1430         $GETSTRIPE $DIR/$tdir/$tfile
1431
1432         reset_enospc
1433 }
1434 run_test 27p "append to a truncated file with some full OSTs ==="
1435
1436 test_27q() {
1437         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1439         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1440         remote_ost_nodsh && skip "remote OST with nodsh" && return
1441
1442         reset_enospc
1443         rm -f $DIR/$tdir/$tfile
1444
1445         test_mkdir -p $DIR/$tdir
1446         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1447         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1448         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1449
1450         exhaust_all_precreations 0x215
1451
1452         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1453         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1454
1455         reset_enospc
1456 }
1457 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1458
1459 test_27r() {
1460         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1462         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1463         remote_ost_nodsh && skip "remote OST with nodsh" && return
1464
1465         reset_enospc
1466         rm -f $DIR/$tdir/$tfile
1467         exhaust_precreations 0 0x80000215
1468
1469         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1470
1471         reset_enospc
1472 }
1473 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1474
1475 test_27s() { # bug 10725
1476         test_mkdir -p $DIR/$tdir
1477         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1478         local stripe_count=0
1479         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1480         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1481                 error "stripe width >= 2^32 succeeded" || true
1482
1483 }
1484 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1485
1486 test_27t() { # bug 10864
1487         WDIR=`pwd`
1488         WLFS=`which lfs`
1489         cd $DIR
1490         touch $tfile
1491         $WLFS getstripe $tfile
1492         cd $WDIR
1493 }
1494 run_test 27t "check that utils parse path correctly"
1495
1496 test_27u() { # bug 4900
1497         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1499
1500 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1501         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1502         test_mkdir -p $DIR/$tdir
1503         rm -rf $DIR/$tdir/*
1504         createmany -o $DIR/$tdir/t- 1000
1505         do_facet $SINGLEMDS lctl set_param fail_loc=0
1506
1507         TLOG=$DIR/$tfile.getstripe
1508         $GETSTRIPE $DIR/$tdir > $TLOG
1509         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1510         unlinkmany $DIR/$tdir/t- 1000
1511         [ $OBJS -gt 0 ] && \
1512                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1513 }
1514 run_test 27u "skip object creation on OSC w/o objects =========="
1515
1516 test_27v() { # bug 4900
1517         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1519         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1520         remote_ost_nodsh && skip "remote OST with nodsh" && return
1521
1522         exhaust_all_precreations 0x215
1523         reset_enospc
1524
1525         test_mkdir -p $DIR/$tdir
1526         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1527
1528         touch $DIR/$tdir/$tfile
1529         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1530         # all except ost1
1531         for (( i=1; i < OSTCOUNT; i++ )); do
1532                 do_facet ost$i lctl set_param fail_loc=0x705
1533         done
1534         local START=`date +%s`
1535         createmany -o $DIR/$tdir/$tfile 32
1536
1537         local FINISH=`date +%s`
1538         local TIMEOUT=`lctl get_param -n timeout`
1539         local PROCESS=$((FINISH - START))
1540         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1541                error "$FINISH - $START >= $TIMEOUT / 2"
1542         sleep $((TIMEOUT / 2 - PROCESS))
1543         reset_enospc
1544 }
1545 run_test 27v "skip object creation on slow OST ================="
1546
1547 test_27w() { # bug 10997
1548         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1549         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1550         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1551                 error "stripe size $size != 65536" || true
1552         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1553                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1554 }
1555 run_test 27w "check $SETSTRIPE -S option"
1556
1557 test_27wa() {
1558         [ "$OSTCOUNT" -lt "2" ] &&
1559                 skip_env "skipping multiple stripe count/offset test" && return
1560
1561         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1562         for i in $(seq 1 $OSTCOUNT); do
1563                 offset=$((i - 1))
1564                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1565                         error "setstripe -c $i -i $offset failed"
1566                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1567                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1568                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1569                 [ $index -ne $offset ] &&
1570                         error "stripe offset $index != $offset" || true
1571         done
1572 }
1573 run_test 27wa "check $SETSTRIPE -c -i options"
1574
1575 test_27x() {
1576         remote_ost_nodsh && skip "remote OST with nodsh" && return
1577         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1579         OFFSET=$(($OSTCOUNT - 1))
1580         OSTIDX=0
1581         local OST=$(ostname_from_index $OSTIDX)
1582
1583         test_mkdir -p $DIR/$tdir
1584         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1585         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1586         sleep_maxage
1587         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1588         for i in `seq 0 $OFFSET`; do
1589                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1590                 error "OST0 was degraded but new created file still use it"
1591         done
1592         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1593 }
1594 run_test 27x "create files while OST0 is degraded"
1595
1596 test_27y() {
1597         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1598         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1599         remote_ost_nodsh && skip "remote OST with nodsh" && return
1600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1601
1602         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1603         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1604             osc.$mdtosc.prealloc_last_id)
1605         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1606             osc.$mdtosc.prealloc_next_id)
1607         local fcount=$((last_id - next_id))
1608         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1609         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1610
1611         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1612                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1613         local OST_DEACTIVE_IDX=-1
1614         local OSC
1615         local OSTIDX
1616         local OST
1617
1618         for OSC in $MDS_OSCS; do
1619                 OST=$(osc_to_ost $OSC)
1620                 OSTIDX=$(index_from_ostuuid $OST)
1621                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1622                         OST_DEACTIVE_IDX=$OSTIDX
1623                 fi
1624                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1625                         echo $OSC "is Deactivated:"
1626                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1627                 fi
1628         done
1629
1630         OSTIDX=$(index_from_ostuuid $OST)
1631         mkdir -p $DIR/$tdir
1632         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1633
1634         for OSC in $MDS_OSCS; do
1635                 OST=$(osc_to_ost $OSC)
1636                 OSTIDX=$(index_from_ostuuid $OST)
1637                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1638                         echo $OST "is degraded:"
1639                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1640                                                 obdfilter.$OST.degraded=1
1641                 fi
1642         done
1643
1644         sleep_maxage
1645         createmany -o $DIR/$tdir/$tfile $fcount
1646
1647         for OSC in $MDS_OSCS; do
1648                 OST=$(osc_to_ost $OSC)
1649                 OSTIDX=$(index_from_ostuuid $OST)
1650                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1651                         echo $OST "is recovered from degraded:"
1652                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1653                                                 obdfilter.$OST.degraded=0
1654                 else
1655                         do_facet $SINGLEMDS lctl --device %$OSC activate
1656                 fi
1657         done
1658
1659         # all osp devices get activated, hence -1 stripe count restored
1660         local stripecnt=0
1661
1662         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1663         # devices get activated.
1664         sleep_maxage
1665         $SETSTRIPE -c -1 $DIR/$tfile
1666         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1667         rm -f $DIR/$tfile
1668         [ $stripecnt -ne $OSTCOUNT ] &&
1669                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1670         return 0
1671 }
1672 run_test 27y "create files while OST0 is degraded and the rest inactive"
1673
1674 check_seq_oid()
1675 {
1676         log "check file $1"
1677
1678         lmm_count=$($GETSTRIPE -c $1)
1679         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1680         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1681
1682         local old_ifs="$IFS"
1683         IFS=$'[:]'
1684         fid=($($LFS path2fid $1))
1685         IFS="$old_ifs"
1686
1687         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1688         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1689
1690         # compare lmm_seq and lu_fid->f_seq
1691         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1692         # compare lmm_object_id and lu_fid->oid
1693         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1694
1695         # check the trusted.fid attribute of the OST objects of the file
1696         local have_obdidx=false
1697         local stripe_nr=0
1698         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1699                 # skip lines up to and including "obdidx"
1700                 [ -z "$obdidx" ] && break
1701                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1702                 $have_obdidx || continue
1703
1704                 local ost=$((obdidx + 1))
1705                 local dev=$(ostdevname $ost)
1706
1707                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1708                         echo "Currently only works with ldiskfs-based OSTs"
1709                         continue
1710                 fi
1711
1712                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1713
1714                 #don't unmount/remount the OSTs if we don't need to do that
1715                 #local dir=$(facet_mntpt ost$ost)
1716                 #stop ost$dev
1717                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1718                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1719                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1720                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1721                 seq=$(echo $seq | sed -e "s/^0x//g")
1722                 if [ $seq == 0 ]; then
1723                         oid_hex=$(echo $oid)
1724                 else
1725                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1726                 fi
1727                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1728                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1729                            $dev 2>/dev/null" | grep "parent=")
1730
1731                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1732
1733                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1734
1735                 #do_facet ost$ost umount -d $dir
1736                 #start ost$ost $dev $OST_MOUNT_OPTS
1737
1738                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1739                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1740                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1741                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1742                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1743                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1744
1745                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1746                 [ $ff_pseq = $lmm_seq ] ||
1747                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1748                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1749                 [ $ff_poid = $lmm_oid ] ||
1750                         error "FF parent OID $ff_poid != $lmm_oid"
1751                 [ $ff_pstripe = $stripe_nr ] ||
1752                         error "FF stripe $ff_pstripe != $stripe_nr"
1753
1754                 stripe_nr=$((stripe_nr + 1))
1755         done
1756 }
1757
1758 test_27z() {
1759         remote_ost_nodsh && skip "remote OST with nodsh" && return
1760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1761         test_mkdir -p $DIR/$tdir
1762
1763         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1764                 { error "setstripe -c -1 failed"; return 1; }
1765         # We need to send a write to every object to get parent FID info set.
1766         # This _should_ also work for setattr, but does not currently.
1767         # touch $DIR/$tdir/$tfile-1 ||
1768         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1769                 { error "dd $tfile-1 failed"; return 2; }
1770         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1771                 { error "setstripe -c -1 failed"; return 3; }
1772         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1773                 { error "dd $tfile-2 failed"; return 4; }
1774
1775         # make sure write RPCs have been sent to OSTs
1776         sync; sleep 5; sync
1777
1778         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1779         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1780 }
1781 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1782
1783 test_27A() { # b=19102
1784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1785         local restore_size=$($GETSTRIPE -S $MOUNT)
1786         local restore_count=$($GETSTRIPE -c $MOUNT)
1787         local restore_offset=$($GETSTRIPE -i $MOUNT)
1788         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1789         local default_size=$($GETSTRIPE -S $MOUNT)
1790         local default_count=$($GETSTRIPE -c $MOUNT)
1791         local default_offset=$($GETSTRIPE -i $MOUNT)
1792         local dsize=$((1024 * 1024))
1793         [ $default_size -eq $dsize ] ||
1794                 error "stripe size $default_size != $dsize"
1795         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1796         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1797         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1798 }
1799 run_test 27A "check filesystem-wide default LOV EA values"
1800
1801 test_27B() { # LU-2523
1802         test_mkdir -p $DIR/$tdir
1803         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1804         touch $DIR/$tdir/f0
1805         # open f1 with O_LOV_DELAY_CREATE
1806         # rename f0 onto f1
1807         # call setstripe ioctl on open file descriptor for f1
1808         # close
1809         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1810                 $DIR/$tdir/f0
1811
1812         rm -f $DIR/$tdir/f1
1813         # open f1 with O_LOV_DELAY_CREATE
1814         # unlink f1
1815         # call setstripe ioctl on open file descriptor for f1
1816         # close
1817         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1818
1819         # Allow multiop to fail in imitation of NFS's busted semantics.
1820         true
1821 }
1822 run_test 27B "call setstripe on open unlinked file/rename victim"
1823
1824 test_27C() { #LU-2871
1825         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1826
1827         declare -a ost_idx
1828         local index
1829         local i
1830         local j
1831
1832         test_mkdir -p $DIR/$tdir
1833         cd $DIR/$tdir
1834         for i in $(seq 0 $((OSTCOUNT - 1))); do
1835                 # set stripe across all OSTs starting from OST$i
1836                 $SETSTRIPE -i $i -c -1 $tfile$i
1837                 # get striping information
1838                 ost_idx=($($GETSTRIPE $tfile$i |
1839                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1840                 echo ${ost_idx[@]}
1841                 # check the layout
1842                 for j in $(seq 0 $((OSTCOUNT - 1))); do
1843                         index=$(((i + j) % OSTCOUNT))
1844                         [ ${ost_idx[$j]} -eq $index ] || error
1845                 done
1846         done
1847 }
1848 run_test 27C "check full striping across all OSTs"
1849
1850 # createtest also checks that device nodes are created and
1851 # then visible correctly (#2091)
1852 test_28() { # bug 2091
1853         test_mkdir $DIR/d28
1854         $CREATETEST $DIR/d28/ct || error
1855 }
1856 run_test 28 "create/mknod/mkdir with bad file types ============"
1857
1858 test_29() {
1859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1860         cancel_lru_locks mdc
1861         test_mkdir $DIR/d29
1862         touch $DIR/d29/foo
1863         log 'first d29'
1864         ls -l $DIR/d29
1865
1866         declare -i LOCKCOUNTORIG=0
1867         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1868                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1869         done
1870         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1871
1872         declare -i LOCKUNUSEDCOUNTORIG=0
1873         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1874                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1875         done
1876
1877         log 'second d29'
1878         ls -l $DIR/d29
1879         log 'done'
1880
1881         declare -i LOCKCOUNTCURRENT=0
1882         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1883                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1884         done
1885
1886         declare -i LOCKUNUSEDCOUNTCURRENT=0
1887         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1888                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1889         done
1890
1891         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1892                 lctl set_param -n ldlm.dump_namespaces ""
1893                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1894                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1895                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1896                 return 2
1897         fi
1898         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1899                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1900                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1901                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1902                 return 3
1903         fi
1904 }
1905 run_test 29 "IT_GETATTR regression  ============================"
1906
1907 test_30a() { # was test_30
1908         cp `which ls` $DIR || cp /bin/ls $DIR
1909         $DIR/ls / || error
1910         rm $DIR/ls
1911 }
1912 run_test 30a "execute binary from Lustre (execve) =============="
1913
1914 test_30b() {
1915         cp `which ls` $DIR || cp /bin/ls $DIR
1916         chmod go+rx $DIR/ls
1917         $RUNAS $DIR/ls / || error
1918         rm $DIR/ls
1919 }
1920 run_test 30b "execute binary from Lustre as non-root ==========="
1921
1922 test_30c() { # b=22376
1923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1924         cp `which ls` $DIR || cp /bin/ls $DIR
1925         chmod a-rw $DIR/ls
1926         cancel_lru_locks mdc
1927         cancel_lru_locks osc
1928         $RUNAS $DIR/ls / || error
1929         rm -f $DIR/ls
1930 }
1931 run_test 30c "execute binary from Lustre without read perms ===="
1932
1933 test_31a() {
1934         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1935         $CHECKSTAT -a $DIR/f31 || error
1936 }
1937 run_test 31a "open-unlink file =================================="
1938
1939 test_31b() {
1940         touch $DIR/f31 || error
1941         ln $DIR/f31 $DIR/f31b || error
1942         $MULTIOP $DIR/f31b Ouc || error
1943         $CHECKSTAT -t file $DIR/f31 || error
1944 }
1945 run_test 31b "unlink file with multiple links while open ======="
1946
1947 test_31c() {
1948         touch $DIR/f31 || error
1949         ln $DIR/f31 $DIR/f31c || error
1950         multiop_bg_pause $DIR/f31 O_uc || return 1
1951         MULTIPID=$!
1952         $MULTIOP $DIR/f31c Ouc
1953         kill -USR1 $MULTIPID
1954         wait $MULTIPID
1955 }
1956 run_test 31c "open-unlink file with multiple links ============="
1957
1958 test_31d() {
1959         opendirunlink $DIR/d31d $DIR/d31d || error
1960         $CHECKSTAT -a $DIR/d31d || error
1961 }
1962 run_test 31d "remove of open directory ========================="
1963
1964 test_31e() { # bug 2904
1965         check_kernel_version 34 || return 0
1966         openfilleddirunlink $DIR/d31e || error
1967 }
1968 run_test 31e "remove of open non-empty directory ==============="
1969
1970 test_31f() { # bug 4554
1971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1972         set -vx
1973         test_mkdir $DIR/d31f
1974         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1975         cp /etc/hosts $DIR/d31f
1976         ls -l $DIR/d31f
1977         $GETSTRIPE $DIR/d31f/hosts
1978         multiop_bg_pause $DIR/d31f D_c || return 1
1979         MULTIPID=$!
1980
1981         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1982         test_mkdir $DIR/d31f
1983         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1984         cp /etc/hosts $DIR/d31f
1985         ls -l $DIR/d31f
1986         $GETSTRIPE $DIR/d31f/hosts
1987         multiop_bg_pause $DIR/d31f D_c || return 1
1988         MULTIPID2=$!
1989
1990         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1991         wait $MULTIPID || error "first opendir $MULTIPID failed"
1992
1993         sleep 6
1994
1995         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1996         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1997         set +vx
1998 }
1999 run_test 31f "remove of open directory with open-unlink file ==="
2000
2001 test_31g() {
2002         echo "-- cross directory link --"
2003         test_mkdir $DIR/d31ga
2004         test_mkdir $DIR/d31gb
2005         touch $DIR/d31ga/f
2006         ln $DIR/d31ga/f $DIR/d31gb/g
2007         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
2008         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
2009         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
2010         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
2011 }
2012 run_test 31g "cross directory link==============="
2013
2014 test_31h() {
2015         echo "-- cross directory link --"
2016         test_mkdir $DIR/d31h
2017         test_mkdir $DIR/d31h/dir
2018         touch $DIR/d31h/f
2019         ln $DIR/d31h/f $DIR/d31h/dir/g
2020         $CHECKSTAT -t file $DIR/d31h/f || error "source"
2021         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
2022         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
2023         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
2024 }
2025 run_test 31h "cross directory link under child==============="
2026
2027 test_31i() {
2028         echo "-- cross directory link --"
2029         test_mkdir $DIR/d31i
2030         test_mkdir $DIR/d31i/dir
2031         touch $DIR/d31i/dir/f
2032         ln $DIR/d31i/dir/f $DIR/d31i/g
2033         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
2034         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
2035         $CHECKSTAT -t file $DIR/d31i/g || error "target"
2036         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
2037 }
2038 run_test 31i "cross directory link under parent==============="
2039
2040
2041 test_31j() {
2042         test_mkdir $DIR/d31j
2043         test_mkdir $DIR/d31j/dir1
2044         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
2045         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
2046         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
2047         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
2048         return 0
2049 }
2050 run_test 31j "link for directory==============="
2051
2052
2053 test_31k() {
2054         test_mkdir $DIR/d31k
2055         touch $DIR/d31k/s
2056         touch $DIR/d31k/exist
2057         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
2058         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
2059         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
2060         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
2061         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
2062         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
2063         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
2064         return 0
2065 }
2066 run_test 31k "link to file: the same, non-existing, dir==============="
2067
2068 test_31m() {
2069         test_mkdir $DIR/d31m
2070         touch $DIR/d31m/s
2071         test_mkdir $DIR/d31m2
2072         touch $DIR/d31m2/exist
2073         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2074         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2075         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2076         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2077         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2078         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2079         return 0
2080 }
2081 run_test 31m "link to file: the same, non-existing, dir==============="
2082
2083 test_31n() {
2084         [ -e /proc/self/fd/173 ] && echo "skipping, fd 173 is in use" && return
2085         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2086         nlink=$(stat --format=%h $DIR/$tfile)
2087         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2088         exec 173<$DIR/$tfile
2089         trap "exec 173<&-" EXIT
2090         nlink=$(stat --dereference --format=%h /proc/self/fd/173)
2091         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2092         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2093         nlink=$(stat --dereference --format=%h /proc/self/fd/173)
2094         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2095         exec 173<&-
2096 }
2097 run_test 31n "check link count of unlinked file"
2098
2099 cleanup_test32_mount() {
2100         trap 0
2101         $UMOUNT $DIR/$tdir/ext2-mountpoint
2102 }
2103
2104 test_32a() {
2105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2106         echo "== more mountpoints and symlinks ================="
2107         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2108         trap cleanup_test32_mount EXIT
2109         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2110         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2111         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2112         cleanup_test32_mount
2113 }
2114 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2115
2116 test_32b() {
2117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2118         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2119         trap cleanup_test32_mount EXIT
2120         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2121         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2122         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2123         cleanup_test32_mount
2124 }
2125 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2126
2127 test_32c() {
2128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2129         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2130         trap cleanup_test32_mount EXIT
2131         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2132         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2133         test_mkdir -p $DIR/$tdir/d2/test_dir
2134         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2135         cleanup_test32_mount
2136 }
2137 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2138
2139 test_32d() {
2140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2141         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2142         trap cleanup_test32_mount EXIT
2143         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2144         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2145         test_mkdir -p $DIR/$tdir/d2/test_dir
2146         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2147         cleanup_test32_mount
2148 }
2149 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2150
2151 test_32e() {
2152         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2153         test_mkdir -p $DIR/d32e/tmp
2154         TMP_DIR=$DIR/d32e/tmp
2155         ln -s $DIR/d32e $TMP_DIR/symlink11
2156         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2157         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2158         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2159 }
2160 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2161
2162 test_32f() {
2163         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2164         test_mkdir -p $DIR/d32f/tmp
2165         TMP_DIR=$DIR/d32f/tmp
2166         ln -s $DIR/d32f $TMP_DIR/symlink11
2167         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2168         ls $DIR/d32f/tmp/symlink11  || error
2169         ls $DIR/d32f/symlink01 || error
2170 }
2171 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2172
2173 test_32g() {
2174         TMP_DIR=$DIR/$tdir/tmp
2175         test_mkdir -p $DIR/$tdir/tmp
2176         test_mkdir $DIR/${tdir}2
2177         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2178         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2179         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2180         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2181         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2182         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2183 }
2184 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2185
2186 test_32h() {
2187         rm -fr $DIR/$tdir $DIR/${tdir}2
2188         TMP_DIR=$DIR/$tdir/tmp
2189         test_mkdir -p $DIR/$tdir/tmp
2190         test_mkdir $DIR/${tdir}2
2191         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2192         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2193         ls $TMP_DIR/symlink12 || error
2194         ls $DIR/$tdir/symlink02  || error
2195 }
2196 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2197
2198 test_32i() {
2199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2200         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2201         trap cleanup_test32_mount EXIT
2202         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2203         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2204         touch $DIR/$tdir/test_file
2205         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2206         cleanup_test32_mount
2207 }
2208 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2209
2210 test_32j() {
2211         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2212         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2213         trap cleanup_test32_mount EXIT
2214         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2215         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2216         touch $DIR/$tdir/test_file
2217         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2218         cleanup_test32_mount
2219 }
2220 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2221
2222 test_32k() {
2223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2224         rm -fr $DIR/$tdir
2225         trap cleanup_test32_mount EXIT
2226         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2227         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2228         test_mkdir -p $DIR/$tdir/d2
2229         touch $DIR/$tdir/d2/test_file || error
2230         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2231         cleanup_test32_mount
2232 }
2233 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2234
2235 test_32l() {
2236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2237         rm -fr $DIR/$tdir
2238         trap cleanup_test32_mount EXIT
2239         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2240         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2241         test_mkdir -p $DIR/$tdir/d2
2242         touch $DIR/$tdir/d2/test_file
2243         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2244         cleanup_test32_mount
2245 }
2246 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2247
2248 test_32m() {
2249         rm -fr $DIR/d32m
2250         test_mkdir -p $DIR/d32m/tmp
2251         TMP_DIR=$DIR/d32m/tmp
2252         ln -s $DIR $TMP_DIR/symlink11
2253         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2254         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2255         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2256 }
2257 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2258
2259 test_32n() {
2260         rm -fr $DIR/d32n
2261         test_mkdir -p $DIR/d32n/tmp
2262         TMP_DIR=$DIR/d32n/tmp
2263         ln -s $DIR $TMP_DIR/symlink11
2264         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2265         ls -l $DIR/d32n/tmp/symlink11  || error
2266         ls -l $DIR/d32n/symlink01 || error
2267 }
2268 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2269
2270 test_32o() {
2271         rm -fr $DIR/d32o $DIR/$tfile
2272         touch $DIR/$tfile
2273         test_mkdir -p $DIR/d32o/tmp
2274         TMP_DIR=$DIR/d32o/tmp
2275         ln -s $DIR/$tfile $TMP_DIR/symlink12
2276         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2277         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2278         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2279         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2280         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2281 }
2282 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2283
2284 test_32p() {
2285     log 32p_1
2286         rm -fr $DIR/d32p
2287     log 32p_2
2288         rm -f $DIR/$tfile
2289     log 32p_3
2290         touch $DIR/$tfile
2291     log 32p_4
2292         test_mkdir -p $DIR/d32p/tmp
2293     log 32p_5
2294         TMP_DIR=$DIR/d32p/tmp
2295     log 32p_6
2296         ln -s $DIR/$tfile $TMP_DIR/symlink12
2297     log 32p_7
2298         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2299     log 32p_8
2300         cat $DIR/d32p/tmp/symlink12 || error
2301     log 32p_9
2302         cat $DIR/d32p/symlink02 || error
2303     log 32p_10
2304 }
2305 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2306
2307 cleanup_testdir_mount() {
2308         trap 0
2309         $UMOUNT $DIR/$tdir
2310 }
2311
2312 test_32q() {
2313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2314         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2315         trap cleanup_testdir_mount EXIT
2316         test_mkdir -p $DIR/$tdir
2317         touch $DIR/$tdir/under_the_mount
2318         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2319         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2320         cleanup_testdir_mount
2321 }
2322 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2323
2324 test_32r() {
2325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2326         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2327         trap cleanup_testdir_mount EXIT
2328         test_mkdir -p $DIR/$tdir
2329         touch $DIR/$tdir/under_the_mount
2330         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2331         ls $DIR/$tdir | grep -q under_the_mount && error || true
2332         cleanup_testdir_mount
2333 }
2334 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2335
2336 test_33aa() {
2337         rm -f $DIR/$tfile
2338         touch $DIR/$tfile
2339         chmod 444 $DIR/$tfile
2340         chown $RUNAS_ID $DIR/$tfile
2341         log 33_1
2342         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2343         log 33_2
2344 }
2345 run_test 33aa "write file with mode 444 (should return error) ===="
2346
2347 test_33a() {
2348         rm -fr $DIR/d33
2349         test_mkdir -p $DIR/d33
2350         chown $RUNAS_ID $DIR/d33
2351         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2352         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2353                 error "open RDWR" || true
2354 }
2355 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2356
2357 test_33b() {
2358         rm -fr $DIR/d33
2359         test_mkdir -p $DIR/d33
2360         chown $RUNAS_ID $DIR/d33
2361         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2362 }
2363 run_test 33b "test open file with malformed flags (No panic and return error)"
2364
2365 test_33c() {
2366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2367         local ostnum
2368         local ostname
2369         local write_bytes
2370         local all_zeros
2371
2372         remote_ost_nodsh && skip "remote OST with nodsh" && return
2373         all_zeros=:
2374         rm -fr $DIR/d33
2375         test_mkdir -p $DIR/d33
2376         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2377
2378         sync
2379         for ostnum in $(seq $OSTCOUNT); do
2380                 # test-framework's OST numbering is one-based, while Lustre's
2381                 # is zero-based
2382                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2383                 # Parsing llobdstat's output sucks; we could grep the /proc
2384                 # path, but that's likely to not be as portable as using the
2385                 # llobdstat utility.  So we parse lctl output instead.
2386                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2387                         obdfilter/$ostname/stats |
2388                         awk '/^write_bytes/ {print $7}' )
2389                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2390                 if (( ${write_bytes:-0} > 0 ))
2391                 then
2392                         all_zeros=false
2393                         break;
2394                 fi
2395         done
2396
2397         $all_zeros || return 0
2398
2399         # Write four bytes
2400         echo foo > $DIR/d33/bar
2401         # Really write them
2402         sync
2403
2404         # Total up write_bytes after writing.  We'd better find non-zeros.
2405         for ostnum in $(seq $OSTCOUNT); do
2406                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2407                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2408                         obdfilter/$ostname/stats |
2409                         awk '/^write_bytes/ {print $7}' )
2410                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2411                 if (( ${write_bytes:-0} > 0 ))
2412                 then
2413                         all_zeros=false
2414                         break;
2415                 fi
2416         done
2417
2418         if $all_zeros
2419         then
2420                 for ostnum in $(seq $OSTCOUNT); do
2421                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2422                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2423                         do_facet ost$ostnum lctl get_param -n \
2424                                 obdfilter/$ostname/stats
2425                 done
2426                 error "OST not keeping write_bytes stats (b22312)"
2427         fi
2428 }
2429 run_test 33c "test llobdstat and write_bytes"
2430
2431 test_33d() {
2432         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2433         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2434         local MDTIDX=1
2435         local remote_dir=$DIR/$tdir/remote_dir
2436
2437         mkdir -p $DIR/$tdir
2438         $LFS mkdir -i $MDTIDX $remote_dir ||
2439                 error "create remote directory failed"
2440
2441         touch $remote_dir/$tfile
2442         chmod 444 $remote_dir/$tfile
2443         chown $RUNAS_ID $remote_dir/$tfile
2444
2445         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2446
2447         chown $RUNAS_ID $remote_dir
2448         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2449                                         error "create" || true
2450         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2451                                     error "open RDWR" || true
2452         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2453                                     error "create" || true
2454 }
2455 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2456
2457 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2458 test_34a() {
2459         rm -f $DIR/f34
2460         $MCREATE $DIR/f34 || error
2461         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2462         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2463         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2464         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2465 }
2466 run_test 34a "truncate file that has not been opened ==========="
2467
2468 test_34b() {
2469         [ ! -f $DIR/f34 ] && test_34a
2470         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2471         $OPENFILE -f O_RDONLY $DIR/f34
2472         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2473         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2474 }
2475 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2476
2477 test_34c() {
2478         [ ! -f $DIR/f34 ] && test_34a
2479         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2480         $OPENFILE -f O_RDWR $DIR/f34
2481         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2482         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2483 }
2484 run_test 34c "O_RDWR opening file-with-size works =============="
2485
2486 test_34d() {
2487         [ ! -f $DIR/f34 ] && test_34a
2488         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2489         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2490         rm $DIR/f34
2491 }
2492 run_test 34d "write to sparse file ============================="
2493
2494 test_34e() {
2495         rm -f $DIR/f34e
2496         $MCREATE $DIR/f34e || error
2497         $TRUNCATE $DIR/f34e 1000 || error
2498         $CHECKSTAT -s 1000 $DIR/f34e || error
2499         $OPENFILE -f O_RDWR $DIR/f34e
2500         $CHECKSTAT -s 1000 $DIR/f34e || error
2501 }
2502 run_test 34e "create objects, some with size and some without =="
2503
2504 test_34f() { # bug 6242, 6243
2505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2506         SIZE34F=48000
2507         rm -f $DIR/f34f
2508         $MCREATE $DIR/f34f || error
2509         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2510         dd if=$DIR/f34f of=$TMP/f34f
2511         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2512         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2513         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2514         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2515         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2516 }
2517 run_test 34f "read from a file with no objects until EOF ======="
2518
2519 test_34g() {
2520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2521         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2522         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2523         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2524         cancel_lru_locks osc
2525         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2526                 error "wrong size after lock cancel"
2527
2528         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2529         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2530                 error "expanding truncate failed"
2531         cancel_lru_locks osc
2532         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2533                 error "wrong expanded size after lock cancel"
2534 }
2535 run_test 34g "truncate long file ==============================="
2536
2537 test_34h() {
2538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2539         local gid=10
2540         local sz=1000
2541
2542         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2543         sync # Flush the cache so that multiop below does not block on cache
2544              # flush when getting the group lock
2545         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2546         MULTIPID=$!
2547         sleep 2
2548
2549         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2550                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2551                 kill -9 $MULTIPID
2552         fi
2553         wait $MULTIPID
2554         local nsz=`stat -c %s $DIR/$tfile`
2555         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2556 }
2557 run_test 34h "ftruncate file under grouplock should not block"
2558
2559 test_35a() {
2560         cp /bin/sh $DIR/f35a
2561         chmod 444 $DIR/f35a
2562         chown $RUNAS_ID $DIR/f35a
2563         $RUNAS $DIR/f35a && error || true
2564         rm $DIR/f35a
2565 }
2566 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2567
2568 test_36a() {
2569         rm -f $DIR/f36
2570         utime $DIR/f36 || error
2571 }
2572 run_test 36a "MDS utime check (mknod, utime) ==================="
2573
2574 test_36b() {
2575         echo "" > $DIR/f36
2576         utime $DIR/f36 || error
2577 }
2578 run_test 36b "OST utime check (open, utime) ===================="
2579
2580 test_36c() {
2581         rm -f $DIR/d36/f36
2582         test_mkdir $DIR/d36
2583         chown $RUNAS_ID $DIR/d36
2584         $RUNAS utime $DIR/d36/f36 || error
2585 }
2586 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2587
2588 test_36d() {
2589         [ ! -d $DIR/d36 ] && test_36c
2590         echo "" > $DIR/d36/f36
2591         $RUNAS utime $DIR/d36/f36 || error
2592 }
2593 run_test 36d "non-root OST utime check (open, utime) ==========="
2594
2595 test_36e() {
2596         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2597         test_mkdir -p $DIR/$tdir
2598         touch $DIR/$tdir/$tfile
2599         $RUNAS utime $DIR/$tdir/$tfile && \
2600                 error "utime worked, expected failure" || true
2601 }
2602 run_test 36e "utime on non-owned file (should return error) ===="
2603
2604 subr_36fh() {
2605         local fl="$1"
2606         local LANG_SAVE=$LANG
2607         local LC_LANG_SAVE=$LC_LANG
2608         export LANG=C LC_LANG=C # for date language
2609
2610         DATESTR="Dec 20  2000"
2611         test_mkdir -p $DIR/$tdir
2612         lctl set_param fail_loc=$fl
2613         date; date +%s
2614         cp /etc/hosts $DIR/$tdir/$tfile
2615         sync & # write RPC generated with "current" inode timestamp, but delayed
2616         sleep 1
2617         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2618         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2619         cancel_lru_locks osc
2620         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2621         date; date +%s
2622         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2623                 echo "BEFORE: $LS_BEFORE" && \
2624                 echo "AFTER : $LS_AFTER" && \
2625                 echo "WANT  : $DATESTR" && \
2626                 error "$DIR/$tdir/$tfile timestamps changed" || true
2627
2628         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2629 }
2630
2631 test_36f() {
2632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2633         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2634         subr_36fh "0x80000214"
2635 }
2636 run_test 36f "utime on file racing with OST BRW write =========="
2637
2638 test_36g() {
2639         remote_ost_nodsh && skip "remote OST with nodsh" && return
2640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2641         local fmd_max_age
2642         local fmd_before
2643         local fmd_after
2644
2645         test_mkdir -p $DIR/$tdir
2646         fmd_max_age=$(do_facet ost1 \
2647                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2648                 head -n 1")
2649
2650         fmd_before=$(do_facet ost1 \
2651                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2652         touch $DIR/$tdir/$tfile
2653         sleep $((fmd_max_age + 12))
2654         fmd_after=$(do_facet ost1 \
2655                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2656
2657         echo "fmd_before: $fmd_before"
2658         echo "fmd_after: $fmd_after"
2659         [ "$fmd_after" -gt "$fmd_before" ] && \
2660                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2661                 error "fmd didn't expire after ping" || true
2662 }
2663 run_test 36g "filter mod data cache expiry ====================="
2664
2665 test_36h() {
2666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2667         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2668         subr_36fh "0x80000227"
2669 }
2670 run_test 36h "utime on file racing with OST BRW write =========="
2671
2672 # test_37 - duplicate with tests 32q 32r
2673
2674 test_38() {
2675         local file=$DIR/$tfile
2676         touch $file
2677         openfile -f O_DIRECTORY $file
2678         local RC=$?
2679         local ENOTDIR=20
2680         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2681         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2682 }
2683 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2684
2685 test_39() {
2686         touch $DIR/$tfile
2687         touch $DIR/${tfile}2
2688 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2689 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2690 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2691         sleep 2
2692         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2693         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2694                 echo "mtime"
2695                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2696                 echo "atime"
2697                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2698                 echo "ctime"
2699                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2700                 error "O_TRUNC didn't change timestamps"
2701         fi
2702 }
2703 run_test 39 "mtime changed on create ==========================="
2704
2705 test_39b() {
2706         test_mkdir -p $DIR/$tdir
2707         cp -p /etc/passwd $DIR/$tdir/fopen
2708         cp -p /etc/passwd $DIR/$tdir/flink
2709         cp -p /etc/passwd $DIR/$tdir/funlink
2710         cp -p /etc/passwd $DIR/$tdir/frename
2711         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2712
2713         sleep 1
2714         echo "aaaaaa" >> $DIR/$tdir/fopen
2715         echo "aaaaaa" >> $DIR/$tdir/flink
2716         echo "aaaaaa" >> $DIR/$tdir/funlink
2717         echo "aaaaaa" >> $DIR/$tdir/frename
2718
2719         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2720         local link_new=`stat -c %Y $DIR/$tdir/flink`
2721         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2722         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2723
2724         cat $DIR/$tdir/fopen > /dev/null
2725         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2726         rm -f $DIR/$tdir/funlink2
2727         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2728
2729         for (( i=0; i < 2; i++ )) ; do
2730                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2731                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2732                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2733                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2734
2735                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2736                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2737                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2738                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2739
2740                 cancel_lru_locks osc
2741                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2742         done
2743 }
2744 run_test 39b "mtime change on open, link, unlink, rename  ======"
2745
2746 # this should be set to past
2747 TEST_39_MTIME=`date -d "1 year ago" +%s`
2748
2749 # bug 11063
2750 test_39c() {
2751         touch $DIR1/$tfile
2752         sleep 2
2753         local mtime0=`stat -c %Y $DIR1/$tfile`
2754
2755         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2756         local mtime1=`stat -c %Y $DIR1/$tfile`
2757         [ "$mtime1" = $TEST_39_MTIME ] || \
2758                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2759
2760         local d1=`date +%s`
2761         echo hello >> $DIR1/$tfile
2762         local d2=`date +%s`
2763         local mtime2=`stat -c %Y $DIR1/$tfile`
2764         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2765                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2766
2767         mv $DIR1/$tfile $DIR1/$tfile-1
2768
2769         for (( i=0; i < 2; i++ )) ; do
2770                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2771                 [ "$mtime2" = "$mtime3" ] || \
2772                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2773
2774                 cancel_lru_locks osc
2775                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2776         done
2777 }
2778 run_test 39c "mtime change on rename ==========================="
2779
2780 # bug 21114
2781 test_39d() {
2782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2783         touch $DIR1/$tfile
2784
2785         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2786
2787         for (( i=0; i < 2; i++ )) ; do
2788                 local mtime=`stat -c %Y $DIR1/$tfile`
2789                 [ $mtime = $TEST_39_MTIME ] || \
2790                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2791
2792                 cancel_lru_locks osc
2793                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2794         done
2795 }
2796 run_test 39d "create, utime, stat =============================="
2797
2798 # bug 21114
2799 test_39e() {
2800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2801         touch $DIR1/$tfile
2802         local mtime1=`stat -c %Y $DIR1/$tfile`
2803
2804         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2805
2806         for (( i=0; i < 2; i++ )) ; do
2807                 local mtime2=`stat -c %Y $DIR1/$tfile`
2808                 [ $mtime2 = $TEST_39_MTIME ] || \
2809                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2810
2811                 cancel_lru_locks osc
2812                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2813         done
2814 }
2815 run_test 39e "create, stat, utime, stat ========================"
2816
2817 # bug 21114
2818 test_39f() {
2819         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2820         touch $DIR1/$tfile
2821         mtime1=`stat -c %Y $DIR1/$tfile`
2822
2823         sleep 2
2824         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2825
2826         for (( i=0; i < 2; i++ )) ; do
2827                 local mtime2=`stat -c %Y $DIR1/$tfile`
2828                 [ $mtime2 = $TEST_39_MTIME ] || \
2829                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2830
2831                 cancel_lru_locks osc
2832                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2833         done
2834 }
2835 run_test 39f "create, stat, sleep, utime, stat ================="
2836
2837 # bug 11063
2838 test_39g() {
2839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2840         echo hello >> $DIR1/$tfile
2841         local mtime1=`stat -c %Y $DIR1/$tfile`
2842
2843         sleep 2
2844         chmod o+r $DIR1/$tfile
2845
2846         for (( i=0; i < 2; i++ )) ; do
2847                 local mtime2=`stat -c %Y $DIR1/$tfile`
2848                 [ "$mtime1" = "$mtime2" ] || \
2849                         error "lost mtime: $mtime2, should be $mtime1"
2850
2851                 cancel_lru_locks osc
2852                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2853         done
2854 }
2855 run_test 39g "write, chmod, stat ==============================="
2856
2857 # bug 11063
2858 test_39h() {
2859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2860         touch $DIR1/$tfile
2861         sleep 1
2862
2863         local d1=`date`
2864         echo hello >> $DIR1/$tfile
2865         local mtime1=`stat -c %Y $DIR1/$tfile`
2866
2867         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2868         local d2=`date`
2869         if [ "$d1" != "$d2" ]; then
2870                 echo "write and touch not within one second"
2871         else
2872                 for (( i=0; i < 2; i++ )) ; do
2873                         local mtime2=`stat -c %Y $DIR1/$tfile`
2874                         [ "$mtime2" = $TEST_39_MTIME ] || \
2875                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2876
2877                         cancel_lru_locks osc
2878                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2879                 done
2880         fi
2881 }
2882 run_test 39h "write, utime within one second, stat ============="
2883
2884 test_39i() {
2885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2886         touch $DIR1/$tfile
2887         sleep 1
2888
2889         echo hello >> $DIR1/$tfile
2890         local mtime1=`stat -c %Y $DIR1/$tfile`
2891
2892         mv $DIR1/$tfile $DIR1/$tfile-1
2893
2894         for (( i=0; i < 2; i++ )) ; do
2895                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2896
2897                 [ "$mtime1" = "$mtime2" ] || \
2898                         error "lost mtime: $mtime2, should be $mtime1"
2899
2900                 cancel_lru_locks osc
2901                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2902         done
2903 }
2904 run_test 39i "write, rename, stat =============================="
2905
2906 test_39j() {
2907         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2908         start_full_debug_logging
2909         touch $DIR1/$tfile
2910         sleep 1
2911
2912         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2913         lctl set_param fail_loc=0x80000412
2914         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2915                 error "multiop failed"
2916         local multipid=$!
2917         local mtime1=`stat -c %Y $DIR1/$tfile`
2918
2919         mv $DIR1/$tfile $DIR1/$tfile-1
2920
2921         kill -USR1 $multipid
2922         wait $multipid || error "multiop close failed"
2923
2924         for (( i=0; i < 2; i++ )) ; do
2925                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2926                 [ "$mtime1" = "$mtime2" ] ||
2927                         error "mtime is lost on close: $mtime2, " \
2928                               "should be $mtime1"
2929
2930                 cancel_lru_locks osc
2931                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2932         done
2933         lctl set_param fail_loc=0
2934         stop_full_debug_logging
2935 }
2936 run_test 39j "write, rename, close, stat ======================="
2937
2938 test_39k() {
2939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2940         touch $DIR1/$tfile
2941         sleep 1
2942
2943         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2944         local multipid=$!
2945         local mtime1=`stat -c %Y $DIR1/$tfile`
2946
2947         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2948
2949         kill -USR1 $multipid
2950         wait $multipid || error "multiop close failed"
2951
2952         for (( i=0; i < 2; i++ )) ; do
2953                 local mtime2=`stat -c %Y $DIR1/$tfile`
2954
2955                 [ "$mtime2" = $TEST_39_MTIME ] || \
2956                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2957
2958                 cancel_lru_locks osc
2959                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2960         done
2961 }
2962 run_test 39k "write, utime, close, stat ========================"
2963
2964 # this should be set to future
2965 TEST_39_ATIME=`date -d "1 year" +%s`
2966
2967 test_39l() {
2968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2969         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2970         local atime_diff=$(do_facet $SINGLEMDS \
2971                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2972         rm -rf $DIR/$tdir
2973         mkdir -p $DIR/$tdir
2974
2975         # test setting directory atime to future
2976         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2977         local atime=$(stat -c %X $DIR/$tdir)
2978         [ "$atime" = $TEST_39_ATIME ] || \
2979                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2980
2981         # test setting directory atime from future to now
2982         local d1=$(date +%s)
2983         ls $DIR/$tdir
2984         local d2=$(date +%s)
2985
2986         cancel_lru_locks mdc
2987         atime=$(stat -c %X $DIR/$tdir)
2988         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2989                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2990
2991         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2992         sleep 3
2993
2994         # test setting directory atime when now > dir atime + atime_diff
2995         d1=$(date +%s)
2996         ls $DIR/$tdir
2997         d2=$(date +%s)
2998         cancel_lru_locks mdc
2999         atime=$(stat -c %X $DIR/$tdir)
3000         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3001                 error "atime is not updated  : $atime, should be $d2"
3002
3003         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3004         sleep 3
3005
3006         # test not setting directory atime when now < dir atime + atime_diff
3007         ls $DIR/$tdir
3008         cancel_lru_locks mdc
3009         atime=$(stat -c %X $DIR/$tdir)
3010         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3011                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3012
3013         do_facet $SINGLEMDS \
3014                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3015 }
3016 run_test 39l "directory atime update ==========================="
3017
3018 test_39m() {
3019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3020         touch $DIR1/$tfile
3021         sleep 2
3022         local far_past_mtime=$(date -d "May 29 1953" +%s)
3023         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3024
3025         touch -m -d @$far_past_mtime $DIR1/$tfile
3026         touch -a -d @$far_past_atime $DIR1/$tfile
3027
3028         for (( i=0; i < 2; i++ )) ; do
3029                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3030                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3031                         error "atime or mtime set incorrectly"
3032
3033                 cancel_lru_locks osc
3034                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3035         done
3036 }
3037 run_test 39m "test atime and mtime before 1970"
3038
3039 test_40() {
3040         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
3041         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
3042         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
3043 }
3044 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3045
3046 test_41() {
3047         # bug 1553
3048         small_write $DIR/f41 18
3049 }
3050 run_test 41 "test small file write + fstat ====================="
3051
3052 count_ost_writes() {
3053         lctl get_param -n osc.*.stats |
3054             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
3055 }
3056
3057 # decent default
3058 WRITEBACK_SAVE=500
3059 DIRTY_RATIO_SAVE=40
3060 MAX_DIRTY_RATIO=50
3061 BG_DIRTY_RATIO_SAVE=10
3062 MAX_BG_DIRTY_RATIO=25
3063
3064 start_writeback() {
3065         trap 0
3066         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3067         # dirty_ratio, dirty_background_ratio
3068         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3069                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3070                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3071                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3072         else
3073                 # if file not here, we are a 2.4 kernel
3074                 kill -CONT `pidof kupdated`
3075         fi
3076 }
3077
3078 stop_writeback() {
3079         # setup the trap first, so someone cannot exit the test at the
3080         # exact wrong time and mess up a machine
3081         trap start_writeback EXIT
3082         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3083         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3084                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3085                 sysctl -w vm.dirty_writeback_centisecs=0
3086                 sysctl -w vm.dirty_writeback_centisecs=0
3087                 # save and increase /proc/sys/vm/dirty_ratio
3088                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3089                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3090                 # save and increase /proc/sys/vm/dirty_background_ratio
3091                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3092                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3093         else
3094                 # if file not here, we are a 2.4 kernel
3095                 kill -STOP `pidof kupdated`
3096         fi
3097 }
3098
3099 # ensure that all stripes have some grant before we test client-side cache
3100 setup_test42() {
3101         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3102                 dd if=/dev/zero of=$i bs=4k count=1
3103                 rm $i
3104         done
3105 }
3106
3107 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3108 # file truncation, and file removal.
3109 test_42a() {
3110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3111         setup_test42
3112         cancel_lru_locks osc
3113         stop_writeback
3114         sync; sleep 1; sync # just to be safe
3115         BEFOREWRITES=`count_ost_writes`
3116         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3117         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3118         AFTERWRITES=`count_ost_writes`
3119         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3120                 error "$BEFOREWRITES < $AFTERWRITES"
3121         start_writeback
3122 }
3123 run_test 42a "ensure that we don't flush on close =============="
3124
3125 test_42b() {
3126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3127         setup_test42
3128         cancel_lru_locks osc
3129         stop_writeback
3130         sync
3131         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3132         BEFOREWRITES=`count_ost_writes`
3133         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3134         AFTERWRITES=`count_ost_writes`
3135         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3136                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3137         fi
3138         BEFOREWRITES=`count_ost_writes`
3139         sync || error "sync: $?"
3140         AFTERWRITES=`count_ost_writes`
3141         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3142                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3143         fi
3144         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3145         start_writeback
3146         return 0
3147 }
3148 run_test 42b "test destroy of file with cached dirty data ======"
3149
3150 # if these tests just want to test the effect of truncation,
3151 # they have to be very careful.  consider:
3152 # - the first open gets a {0,EOF}PR lock
3153 # - the first write conflicts and gets a {0, count-1}PW
3154 # - the rest of the writes are under {count,EOF}PW
3155 # - the open for truncate tries to match a {0,EOF}PR
3156 #   for the filesize and cancels the PWs.
3157 # any number of fixes (don't get {0,EOF} on open, match
3158 # composite locks, do smarter file size management) fix
3159 # this, but for now we want these tests to verify that
3160 # the cancellation with truncate intent works, so we
3161 # start the file with a full-file pw lock to match against
3162 # until the truncate.
3163 trunc_test() {
3164         test=$1
3165         file=$DIR/$test
3166         offset=$2
3167         cancel_lru_locks osc
3168         stop_writeback
3169         # prime the file with 0,EOF PW to match
3170         touch $file
3171         $TRUNCATE $file 0
3172         sync; sync
3173         # now the real test..
3174         dd if=/dev/zero of=$file bs=1024 count=100
3175         BEFOREWRITES=`count_ost_writes`
3176         $TRUNCATE $file $offset
3177         cancel_lru_locks osc
3178         AFTERWRITES=`count_ost_writes`
3179         start_writeback
3180 }
3181
3182 test_42c() {
3183         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3184         trunc_test 42c 1024
3185         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3186             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3187         rm $file
3188 }
3189 run_test 42c "test partial truncate of file with cached dirty data"
3190
3191 test_42d() {
3192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3193         trunc_test 42d 0
3194         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3195             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3196         rm $file
3197 }
3198 run_test 42d "test complete truncate of file with cached dirty data"
3199
3200 test_42e() { # bug22074
3201         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3202         local TDIR=$DIR/${tdir}e
3203         local pagesz=$(page_size)
3204         local pages=16 # hardcoded 16 pages, don't change it.
3205         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3206         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3207         local max_dirty_mb
3208         local warmup_files
3209
3210         test_mkdir -p $DIR/${tdir}e
3211         $SETSTRIPE -c 1 $TDIR
3212         createmany -o $TDIR/f $files
3213
3214         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3215
3216         # we assume that with $OSTCOUNT files, at least one of them will
3217         # be allocated on OST0.
3218         warmup_files=$((OSTCOUNT * max_dirty_mb))
3219         createmany -o $TDIR/w $warmup_files
3220
3221         # write a large amount of data into one file and sync, to get good
3222         # avail_grant number from OST.
3223         for ((i=0; i<$warmup_files; i++)); do
3224                 idx=$($GETSTRIPE -i $TDIR/w$i)
3225                 [ $idx -ne 0 ] && continue
3226                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3227                 break
3228         done
3229         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3230         sync
3231         $LCTL get_param $proc_osc0/cur_dirty_bytes
3232         $LCTL get_param $proc_osc0/cur_grant_bytes
3233
3234         # create as much dirty pages as we can while not to trigger the actual
3235         # RPCs directly. but depends on the env, VFS may trigger flush during this
3236         # period, hopefully we are good.
3237         for ((i=0; i<$warmup_files; i++)); do
3238                 idx=$($GETSTRIPE -i $TDIR/w$i)
3239                 [ $idx -ne 0 ] && continue
3240                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3241         done
3242         $LCTL get_param $proc_osc0/cur_dirty_bytes
3243         $LCTL get_param $proc_osc0/cur_grant_bytes
3244
3245         # perform the real test
3246         $LCTL set_param $proc_osc0/rpc_stats 0
3247         for ((;i<$files; i++)); do
3248                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3249                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3250         done
3251         sync
3252         $LCTL get_param $proc_osc0/rpc_stats
3253
3254         local percent=0
3255         local have_ppr=false
3256         $LCTL get_param $proc_osc0/rpc_stats |
3257                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3258                         # skip lines until we are at the RPC histogram data
3259                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3260                         $have_ppr || continue
3261
3262                         # we only want the percent stat for < 16 pages
3263                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3264
3265                         percent=$((percent + WPCT))
3266                         if [ $percent -gt 15 ]; then
3267                                 error "less than 16-pages write RPCs" \
3268                                       "$percent% > 15%"
3269                                 break
3270                         fi
3271                 done
3272         rm -rf $TDIR
3273 }
3274 run_test 42e "verify sub-RPC writes are not done synchronously"
3275
3276 test_43() {
3277         test_mkdir -p $DIR/$tdir
3278         cp -p /bin/ls $DIR/$tdir/$tfile
3279         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3280         pid=$!
3281         # give multiop a chance to open
3282         sleep 1
3283
3284         $DIR/$tdir/$tfile && error || true
3285         kill -USR1 $pid
3286 }
3287 run_test 43 "execution of file opened for write should return -ETXTBSY"
3288
3289 test_43a() {
3290         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3291         test_mkdir -p $DIR/$tdir
3292         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3293                         cp -p multiop $DIR/$tdir/multiop
3294         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3295                         return 1
3296         MULTIOP_PID=$!
3297         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3298         kill -USR1 $MULTIOP_PID || return 2
3299         wait $MULTIOP_PID || return 3
3300         rm $TMP/test43.junk
3301 }
3302 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3303
3304 test_43b() {
3305         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3306         test_mkdir -p $DIR/$tdir
3307         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3308                         cp -p multiop $DIR/$tdir/multiop
3309         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3310                         return 1
3311         MULTIOP_PID=$!
3312         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3313         kill -USR1 $MULTIOP_PID || return 2
3314         wait $MULTIOP_PID || return 3
3315         rm $TMP/test43.junk
3316 }
3317 run_test 43b "truncate of file being executed should return -ETXTBSY"
3318
3319 test_43c() {
3320         local testdir="$DIR/$tdir"
3321         test_mkdir -p $DIR/$tdir
3322         cp $SHELL $testdir/
3323         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3324                 ( cd $testdir && md5sum -c)
3325 }
3326 run_test 43c "md5sum of copy into lustre========================"
3327
3328 test_44() {
3329         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3330         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3331         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3332 }
3333 run_test 44 "zero length read from a sparse stripe ============="
3334
3335 test_44a() {
3336     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3337                          awk '{print $2}'`
3338     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3339     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3340     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3341                       awk '{print $2}'`
3342     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3343         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3344     fi
3345
3346     OFFSETS="0 $((stride/2)) $((stride-1))"
3347     for offset in $OFFSETS ; do
3348       for i in `seq 0 $((nstripe-1))`; do
3349         local GLOBALOFFSETS=""
3350         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3351         local myfn=$DIR/d44a-$size
3352         echo "--------writing $myfn at $size"
3353         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3354         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3355         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3356                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3357
3358         for j in `seq 0 $((nstripe-1))`; do
3359             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3360             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3361             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3362         done
3363         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3364                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3365         rm -f $myfn
3366       done
3367     done
3368 }
3369 run_test 44a "test sparse pwrite ==============================="
3370
3371 dirty_osc_total() {
3372         tot=0
3373         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3374                 tot=$(($tot + $d))
3375         done
3376         echo $tot
3377 }
3378 do_dirty_record() {
3379         before=`dirty_osc_total`
3380         echo executing "\"$*\""
3381         eval $*
3382         after=`dirty_osc_total`
3383         echo before $before, after $after
3384 }
3385 test_45() {
3386         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3387         f="$DIR/f45"
3388         # Obtain grants from OST if it supports it
3389         echo blah > ${f}_grant
3390         stop_writeback
3391         sync
3392         do_dirty_record "echo blah > $f"
3393         [ $before -eq $after ] && error "write wasn't cached"
3394         do_dirty_record "> $f"
3395         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3396         do_dirty_record "echo blah > $f"
3397         [ $before -eq $after ] && error "write wasn't cached"
3398         do_dirty_record "sync"
3399         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3400         do_dirty_record "echo blah > $f"
3401         [ $before -eq $after ] && error "write wasn't cached"
3402         do_dirty_record "cancel_lru_locks osc"
3403         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3404         start_writeback
3405 }
3406 run_test 45 "osc io page accounting ============================"
3407
3408 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3409 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3410 # objects offset and an assert hit when an rpc was built with 1023's mapped
3411 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3412 test_46() {
3413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3414         f="$DIR/f46"
3415         stop_writeback
3416         sync
3417         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3418         sync
3419         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3420         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3421         sync
3422         start_writeback
3423 }
3424 run_test 46 "dirtying a previously written page ================"
3425
3426 # test_47 is removed "Device nodes check" is moved to test_28
3427
3428 test_48a() { # bug 2399
3429         check_kernel_version 34 || return 0
3430         test_mkdir -p $DIR/$tdir
3431         cd $DIR/$tdir
3432         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3433         test_mkdir $DIR/$tdir || error "recreate directory failed"
3434         touch foo || error "'touch foo' failed after recreating cwd"
3435         test_mkdir $DIR/$tdir/bar ||
3436                      error "'mkdir foo' failed after recreating cwd"
3437         if check_kernel_version 44; then
3438                 touch .foo || error "'touch .foo' failed after recreating cwd"
3439                 test_mkdir $DIR/$tdir/.bar ||
3440                               error "'mkdir .foo' failed after recreating cwd"
3441         fi
3442         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3443         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3444         cd . || error "'cd .' failed after recreating cwd"
3445         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3446         rmdir . && error "'rmdir .' worked after recreating cwd"
3447         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3448         cd .. || error "'cd ..' failed after recreating cwd"
3449 }
3450 run_test 48a "Access renamed working dir (should return errors)="
3451
3452 test_48b() { # bug 2399
3453         check_kernel_version 34 || return 0
3454         rm -rf $DIR/$tdir
3455         test_mkdir -p $DIR/$tdir
3456         cd $DIR/$tdir
3457         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3458         touch foo && error "'touch foo' worked after removing cwd"
3459         test_mkdir $DIR/$tdir/foo &&
3460                      error "'mkdir foo' worked after removing cwd"
3461         if check_kernel_version 44; then
3462                 touch .foo && error "'touch .foo' worked after removing cwd"
3463                 test_mkdir $DIR/$tdir/.foo &&
3464                               error "'mkdir .foo' worked after removing cwd"
3465         fi
3466         ls . > /dev/null && error "'ls .' worked after removing cwd"
3467         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3468         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3469         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3470         rmdir . && error "'rmdir .' worked after removing cwd"
3471         ln -s . foo && error "'ln -s .' worked after removing cwd"
3472         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3473 }
3474 run_test 48b "Access removed working dir (should return errors)="
3475
3476 test_48c() { # bug 2350
3477         check_kernel_version 36 || return 0
3478         #lctl set_param debug=-1
3479         #set -vx
3480         rm -rf $DIR/$tdir
3481         test_mkdir -p $DIR/$tdir/dir
3482         cd $DIR/$tdir/dir
3483         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3484         $TRACE touch foo && error "touch foo worked after removing cwd"
3485         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3486         if check_kernel_version 44; then
3487                 touch .foo && error "touch .foo worked after removing cwd"
3488                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3489         fi
3490         $TRACE ls . && error "'ls .' worked after removing cwd"
3491         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3492         is_patchless || ( $TRACE cd . &&
3493                         error "'cd .' worked after removing cwd" )
3494         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3495         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3496         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3497         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3498 }
3499 run_test 48c "Access removed working subdir (should return errors)"
3500
3501 test_48d() { # bug 2350
3502         check_kernel_version 36 || return 0
3503         #lctl set_param debug=-1
3504         #set -vx
3505         rm -rf $DIR/$tdir
3506         test_mkdir -p $DIR/$tdir/dir
3507         cd $DIR/$tdir/dir
3508         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3509         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3510         $TRACE touch foo && error "'touch foo' worked after removing parent"
3511         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3512         if check_kernel_version 44; then
3513                 touch .foo && error "'touch .foo' worked after removing parent"
3514                 test_mkdir .foo &&
3515                               error "mkdir .foo worked after removing parent"
3516         fi
3517         $TRACE ls . && error "'ls .' worked after removing parent"
3518         $TRACE ls .. && error "'ls ..' worked after removing parent"
3519         is_patchless || ( $TRACE cd . &&
3520                         error "'cd .' worked after recreate parent" )
3521         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3522         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3523         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3524         is_patchless || ( $TRACE cd .. &&
3525                         error "'cd ..' worked after removing parent" || true )
3526 }
3527 run_test 48d "Access removed parent subdir (should return errors)"
3528
3529 test_48e() { # bug 4134
3530         check_kernel_version 41 || return 0
3531         #lctl set_param debug=-1
3532         #set -vx
3533         rm -rf $DIR/$tdir
3534         test_mkdir -p $DIR/$tdir/dir
3535         cd $DIR/$tdir/dir
3536         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3537         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3538         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3539         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3540         # On a buggy kernel addition of "touch foo" after cd .. will
3541         # produce kernel oops in lookup_hash_it
3542         touch ../foo && error "'cd ..' worked after recreate parent"
3543         cd $DIR
3544         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3545 }
3546 run_test 48e "Access to recreated parent subdir (should return errors)"
3547
3548 test_49() { # LU-1030
3549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3550         # get ost1 size - lustre-OST0000
3551         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3552         # write 800M at maximum
3553         [ $ost1_size -gt 819200 ] && ost1_size=819200
3554
3555         lfs setstripe -c 1 -i 0 $DIR/$tfile
3556         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3557         local dd_pid=$!
3558
3559         # change max_pages_per_rpc while writing the file
3560         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3561         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3562         # loop until dd process exits
3563         while ps ax -opid | grep -wq $dd_pid; do
3564                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3565                 sleep $((RANDOM % 5 + 1))
3566         done
3567         # restore original max_pages_per_rpc
3568         $LCTL set_param $osc1_mppc=$orig_mppc
3569         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3570 }
3571 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3572
3573 test_50() {
3574         # bug 1485
3575         test_mkdir $DIR/$tdir
3576         cd $DIR/$tdir
3577         ls /proc/$$/cwd || error
3578 }
3579 run_test 50 "special situations: /proc symlinks  ==============="
3580
3581 test_51a() {    # was test_51
3582         # bug 1516 - create an empty entry right after ".." then split dir
3583         test_mkdir -p $DIR/$tdir
3584         touch $DIR/$tdir/foo
3585         $MCREATE $DIR/$tdir/bar
3586         rm $DIR/$tdir/foo
3587         createmany -m $DIR/$tdir/longfile 201
3588         FNUM=202
3589         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3590                 $MCREATE $DIR/$tdir/longfile$FNUM
3591                 FNUM=$(($FNUM + 1))
3592                 echo -n "+"
3593         done
3594         echo
3595         ls -l $DIR/$tdir > /dev/null || error
3596 }
3597 run_test 51a "special situations: split htree with empty entry =="
3598
3599 export NUMTEST=70000
3600 test_51b() {
3601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3602         local BASE=$DIR/$tdir
3603
3604         # cleanup the directory
3605         rm -fr $BASE
3606
3607         test_mkdir -p $BASE
3608
3609         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3610         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3611         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3612                 return
3613
3614         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3615                 echo "reduced count to $NUMTEST due to inodes"
3616
3617         # need to check free space for the directories as well
3618         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3619         numfree=$((blkfree / 4))
3620         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3621                 echo "reduced count to $NUMTEST due to blocks"
3622
3623         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3624                 echo "failed" > $BASE/fnum
3625 }
3626 run_test 51b "exceed 64k subdirectory nlink limit"
3627
3628 test_51ba() { # LU-993
3629         local BASE=$DIR/$tdir
3630         # unlink all but 100 subdirectories, then check it still works
3631         local LEFT=100
3632         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3633
3634         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3635         local DELETE=$((NUMTEST - LEFT))
3636
3637         # continue on to run this test even if 51b didn't finish,
3638         # just to delete the many subdirectories created.
3639         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3640
3641         # for ldiskfs the nlink count should be 1, but this is OSD specific
3642         # and so this is listed for informational purposes only
3643         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3644         unlinkmany -d $BASE/d $DELETE
3645         RC=$?
3646
3647         if [ $RC -ne 0 ]; then
3648                 if [ "$NUMPREV" == "failed" ]; then
3649                         skip "previous setup failed"
3650                         return 0
3651                 else
3652                         error "unlink of first $DELETE subdirs failed"
3653                         return $RC
3654                 fi
3655         fi
3656
3657         echo "nlink between: $(stat -c %h $BASE)"
3658         # trim the first line of ls output
3659         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3660         [ $FOUND -ne $LEFT ] &&
3661                 error "can't find subdirs: found only $FOUND/$LEFT"
3662
3663         unlinkmany -d $BASE/d $DELETE $LEFT ||
3664                 error "unlink of second $LEFT subdirs failed"
3665         # regardless of whether the backing filesystem tracks nlink accurately
3666         # or not, the nlink count shouldn't be more than "." and ".." here
3667         local AFTER=$(stat -c %h $BASE)
3668         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3669                 echo "nlink after: $AFTER"
3670 }
3671 run_test 51ba "verify nlink for many subdirectory cleanup"
3672
3673 test_51d() {
3674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3675         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3676         test_mkdir -p $DIR/$tdir
3677         createmany -o $DIR/$tdir/t- 1000
3678         $GETSTRIPE $DIR/$tdir > $TMP/files
3679         for N in `seq 0 $((OSTCOUNT - 1))`; do
3680             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3681             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3682             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3683         done
3684         unlinkmany $DIR/$tdir/t- 1000
3685
3686         NLAST=0
3687         for N in `seq 1 $((OSTCOUNT - 1))`; do
3688             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3689                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3690             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3691                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3692
3693             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3694                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3695             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3696                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3697             NLAST=$N
3698         done
3699 }
3700 run_test 51d "check object distribution ===================="
3701
3702 test_52a() {
3703         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3704         test_mkdir -p $DIR/$tdir
3705         touch $DIR/$tdir/foo
3706         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3707         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3708         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3709         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3710         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3711                                         error "link worked"
3712         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3713         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3714         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3715                                                      error "lsattr"
3716         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3717         cp -r $DIR/$tdir /tmp/
3718         rm -fr $DIR/$tdir || error "cleanup rm failed"
3719 }
3720 run_test 52a "append-only flag test (should return errors) ====="
3721
3722 test_52b() {
3723         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3724         test_mkdir -p $DIR/$tdir
3725         touch $DIR/$tdir/foo
3726         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3727         cat test > $DIR/$tdir/foo && error "cat test worked"
3728         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3729         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3730         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3731                                         error "link worked"
3732         echo foo >> $DIR/$tdir/foo && error "echo worked"
3733         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3734         [ -f $DIR/$tdir/foo ] || error
3735         [ -f $DIR/$tdir/foo_ren ] && error
3736         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3737                                                         error "lsattr"
3738         chattr -i $DIR/$tdir/foo || error "chattr failed"
3739
3740         rm -fr $DIR/$tdir || error
3741 }
3742 run_test 52b "immutable flag test (should return errors) ======="
3743
3744 test_53() {
3745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3746         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3747         remote_ost_nodsh && skip "remote OST with nodsh" && return
3748
3749         local param
3750         local ostname
3751         local mds_last
3752         local ost_last
3753         local ostnum
3754         local node
3755
3756         # only test MDT0000
3757         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3758         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3759                 param=`echo ${value[0]} | cut -d "=" -f1`
3760                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3761                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3762                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3763                 node=$(facet_active_host ost$((ostnum+1)))
3764                 param="obdfilter.$ostname.last_id"
3765                 ost_last=$(do_node $node lctl get_param -n $param | head -n 1)
3766                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3767                 if [ $ost_last != $mds_last ]; then
3768                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3769                 fi
3770         done
3771 }
3772 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3773
3774 test_54a() {
3775         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3776         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3777         $SOCKETSERVER $DIR/socket
3778         $SOCKETCLIENT $DIR/socket || error
3779         $MUNLINK $DIR/socket
3780 }
3781 run_test 54a "unix domain socket test =========================="
3782
3783 test_54b() {
3784         f="$DIR/f54b"
3785         mknod $f c 1 3
3786         chmod 0666 $f
3787         dd if=/dev/zero of=$f bs=`page_size` count=1
3788 }
3789 run_test 54b "char device works in lustre ======================"
3790
3791 find_loop_dev() {
3792         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3793         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3794         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3795
3796         for i in `seq 3 7`; do
3797                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3798                 LOOPDEV=$LOOPBASE$i
3799                 LOOPNUM=$i
3800                 break
3801         done
3802 }
3803
3804 test_54c() {
3805         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3806         tfile="$DIR/f54c"
3807         tdir="$DIR/d54c"
3808         loopdev="$DIR/loop54c"
3809
3810         find_loop_dev
3811         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3812         mknod $loopdev b 7 $LOOPNUM
3813         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3814         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3815         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3816         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3817         test_mkdir -p $tdir
3818         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3819         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3820         df $tdir
3821         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3822         $UMOUNT $tdir
3823         losetup -d $loopdev
3824         rm $loopdev
3825 }
3826 run_test 54c "block device works in lustre ====================="
3827
3828 test_54d() {
3829         f="$DIR/f54d"
3830         string="aaaaaa"
3831         mknod $f p
3832         [ "$string" = `echo $string > $f | cat $f` ] || error
3833 }
3834 run_test 54d "fifo device works in lustre ======================"
3835
3836 test_54e() {
3837         check_kernel_version 46 || return 0
3838         f="$DIR/f54e"
3839         string="aaaaaa"
3840         cp -aL /dev/console $f
3841         echo $string > $f || error
3842 }
3843 run_test 54e "console/tty device works in lustre ======================"
3844
3845 #The test_55 used to be iopen test and it was removed by bz#24037.
3846 #run_test 55 "check iopen_connect_dentry() ======================"
3847
3848 test_56a() {    # was test_56
3849         rm -rf $DIR/$tdir
3850         $SETSTRIPE -d $DIR
3851         test_mkdir $DIR/$tdir
3852         test_mkdir $DIR/$tdir/dir
3853         NUMFILES=3
3854         NUMFILESx2=$(($NUMFILES * 2))
3855         for i in `seq 1 $NUMFILES` ; do
3856                 touch $DIR/$tdir/file$i
3857                 touch $DIR/$tdir/dir/file$i
3858         done
3859
3860         # test lfs getstripe with --recursive
3861         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3862         [ $FILENUM -eq $NUMFILESx2 ] ||
3863                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3864         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3865         [ $FILENUM -eq $NUMFILES ] ||
3866                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3867         echo "$GETSTRIPE --recursive passed."
3868
3869         # test lfs getstripe with file instead of dir
3870         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3871         [ $FILENUM  -eq 1 ] || error \
3872                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3873         echo "$GETSTRIPE file1 passed."
3874
3875         #test lfs getstripe with --verbose
3876         [ `$GETSTRIPE --verbose $DIR/$tdir |
3877                         grep -c lmm_magic` -eq $NUMFILES ] ||
3878                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3879         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3880             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3881         echo "$GETSTRIPE --verbose passed."
3882
3883         #test lfs getstripe with --obd
3884         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3885                                         grep -q "unknown obduuid" ||
3886                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3887
3888         [  "$OSTCOUNT" -lt 2 ] &&
3889                 skip_env "skipping other $GETSTRIPE --obd test" && return
3890
3891         OSTIDX=1
3892         OBDUUID=$(ostuuid_from_index $OSTIDX)
3893         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3894         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3895         [ $FOUND -eq $FILENUM ] ||
3896                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3897         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3898                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3899                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3900                 error "$GETSTRIPE --obd: should not show file on other obd"
3901         echo "$GETSTRIPE --obd passed"
3902 }
3903 run_test 56a "check $GETSTRIPE"
3904
3905 NUMFILES=3
3906 NUMDIRS=3
3907 setup_56() {
3908         local LOCAL_NUMFILES="$1"
3909         local LOCAL_NUMDIRS="$2"
3910         local MKDIR_PARAMS="$3"
3911
3912         if [ ! -d "$TDIR" ] ; then
3913                 test_mkdir -p $TDIR
3914                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3915                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3916                         touch $TDIR/file$i
3917                 done
3918                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3919                         test_mkdir $TDIR/dir$i
3920                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3921                                 touch $TDIR/dir$i/file$j
3922                         done
3923                 done
3924         fi
3925 }
3926
3927 setup_56_special() {
3928         LOCAL_NUMFILES=$1
3929         LOCAL_NUMDIRS=$2
3930         setup_56 $1 $2
3931         if [ ! -e "$TDIR/loop1b" ] ; then
3932                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3933                         mknod $TDIR/loop${i}b b 7 $i
3934                         mknod $TDIR/null${i}c c 1 3
3935                         ln -s $TDIR/file1 $TDIR/link${i}l
3936                 done
3937                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3938                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3939                         mknod $TDIR/dir$i/null${i}c c 1 3
3940                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3941                 done
3942         fi
3943 }
3944
3945 test_56g() {
3946         $SETSTRIPE -d $DIR
3947
3948         TDIR=$DIR/${tdir}g
3949         setup_56 $NUMFILES $NUMDIRS
3950
3951         EXPECTED=$(($NUMDIRS + 2))
3952         # test lfs find with -name
3953         for i in $(seq 1 $NUMFILES) ; do
3954                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3955                 [ $NUMS -eq $EXPECTED ] ||
3956                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3957                               "found $NUMS, expected $EXPECTED"
3958         done
3959 }
3960 run_test 56g "check lfs find -name ============================="
3961
3962 test_56h() {
3963         $SETSTRIPE -d $DIR
3964
3965         TDIR=$DIR/${tdir}g
3966         setup_56 $NUMFILES $NUMDIRS
3967
3968         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3969         # test lfs find with ! -name
3970         for i in $(seq 1 $NUMFILES) ; do
3971                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3972                 [ $NUMS -eq $EXPECTED ] ||
3973                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3974                               "found $NUMS, expected $EXPECTED"
3975         done
3976 }
3977 run_test 56h "check lfs find ! -name ============================="
3978
3979 test_56i() {
3980        tdir=${tdir}i
3981        test_mkdir -p $DIR/$tdir
3982        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3983        CMD="$LFIND -ost $UUID $DIR/$tdir"
3984        OUT=$($CMD)
3985        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3986 }
3987 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3988
3989 test_56j() {
3990         TDIR=$DIR/${tdir}g
3991         setup_56_special $NUMFILES $NUMDIRS
3992
3993         EXPECTED=$((NUMDIRS + 1))
3994         CMD="$LFIND -type d $TDIR"
3995         NUMS=$($CMD | wc -l)
3996         [ $NUMS -eq $EXPECTED ] ||
3997                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3998 }
3999 run_test 56j "check lfs find -type d ============================="
4000
4001 test_56k() {
4002         TDIR=$DIR/${tdir}g
4003         setup_56_special $NUMFILES $NUMDIRS
4004
4005         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4006         CMD="$LFIND -type f $TDIR"
4007         NUMS=$($CMD | wc -l)
4008         [ $NUMS -eq $EXPECTED ] ||
4009                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4010 }
4011 run_test 56k "check lfs find -type f ============================="
4012
4013 test_56l() {
4014         TDIR=$DIR/${tdir}g
4015         setup_56_special $NUMFILES $NUMDIRS
4016
4017         EXPECTED=$((NUMDIRS + NUMFILES))
4018         CMD="$LFIND -type b $TDIR"
4019         NUMS=$($CMD | wc -l)
4020         [ $NUMS -eq $EXPECTED ] ||
4021                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4022 }
4023 run_test 56l "check lfs find -type b ============================="
4024
4025 test_56m() {
4026         TDIR=$DIR/${tdir}g
4027         setup_56_special $NUMFILES $NUMDIRS
4028
4029         EXPECTED=$((NUMDIRS + NUMFILES))
4030         CMD="$LFIND -type c $TDIR"
4031         NUMS=$($CMD | wc -l)
4032         [ $NUMS -eq $EXPECTED ] ||
4033                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4034 }
4035 run_test 56m "check lfs find -type c ============================="
4036
4037 test_56n() {
4038         TDIR=$DIR/${tdir}g
4039         setup_56_special $NUMFILES $NUMDIRS
4040
4041         EXPECTED=$((NUMDIRS + NUMFILES))
4042         CMD="$LFIND -type l $TDIR"
4043         NUMS=$($CMD | wc -l)
4044         [ $NUMS -eq $EXPECTED ] ||
4045                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4046 }
4047 run_test 56n "check lfs find -type l ============================="
4048
4049 test_56o() {
4050         TDIR=$DIR/${tdir}o
4051         setup_56 $NUMFILES $NUMDIRS
4052
4053         utime $TDIR/file1 > /dev/null || error "utime (1)"
4054         utime $TDIR/file2 > /dev/null || error "utime (2)"
4055         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4056         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4057         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4058         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4059
4060         EXPECTED=4
4061         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4062         [ $NUMS -eq $EXPECTED ] || \
4063                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4064
4065         EXPECTED=12
4066         CMD="$LFIND -mtime 0 $TDIR"
4067         NUMS=$($CMD | wc -l)
4068         [ $NUMS -eq $EXPECTED ] ||
4069                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4070 }
4071 run_test 56o "check lfs find -mtime for old files =========================="
4072
4073 test_56p() {
4074         [ $RUNAS_ID -eq $UID ] &&
4075                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4076
4077         TDIR=$DIR/${tdir}p
4078         setup_56 $NUMFILES $NUMDIRS
4079
4080         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4081         EXPECTED=$NUMFILES
4082         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4083         NUMS=$($CMD | wc -l)
4084         [ $NUMS -eq $EXPECTED ] || \
4085                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4086
4087         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4088         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4089         NUMS=$($CMD | wc -l)
4090         [ $NUMS -eq $EXPECTED ] || \
4091                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4092 }
4093 run_test 56p "check lfs find -uid and ! -uid ==============================="
4094
4095 test_56q() {
4096         [ $RUNAS_ID -eq $UID ] &&
4097                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4098
4099         TDIR=$DIR/${tdir}q
4100         setup_56 $NUMFILES $NUMDIRS
4101
4102         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4103
4104         EXPECTED=$NUMFILES
4105         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4106         NUMS=$($CMD | wc -l)
4107         [ $NUMS -eq $EXPECTED ] ||
4108                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4109
4110         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4111         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4112         NUMS=$($CMD | wc -l)
4113         [ $NUMS -eq $EXPECTED ] ||
4114                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4115 }
4116 run_test 56q "check lfs find -gid and ! -gid ==============================="
4117
4118 test_56r() {
4119         TDIR=$DIR/${tdir}r
4120         setup_56 $NUMFILES $NUMDIRS
4121
4122         EXPECTED=12
4123         CMD="$LFIND -size 0 -type f $TDIR"
4124         NUMS=$($CMD | wc -l)
4125         [ $NUMS -eq $EXPECTED ] ||
4126                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4127         EXPECTED=0
4128         CMD="$LFIND ! -size 0 -type f $TDIR"
4129         NUMS=$($CMD | wc -l)
4130         [ $NUMS -eq $EXPECTED ] ||
4131                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4132         echo "test" > $TDIR/$tfile
4133         echo "test2" > $TDIR/$tfile.2 && sync
4134         EXPECTED=1
4135         CMD="$LFIND -size 5 -type f $TDIR"
4136         NUMS=$($CMD | wc -l)
4137         [ $NUMS -eq $EXPECTED ] ||
4138                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4139         EXPECTED=1
4140         CMD="$LFIND -size +5 -type f $TDIR"
4141         NUMS=$($CMD | wc -l)
4142         [ $NUMS -eq $EXPECTED ] ||
4143                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4144         EXPECTED=2
4145         CMD="$LFIND -size +0 -type f $TDIR"
4146         NUMS=$($CMD | wc -l)
4147         [ $NUMS -eq $EXPECTED ] ||
4148                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4149         EXPECTED=2
4150         CMD="$LFIND ! -size -5 -type f $TDIR"
4151         NUMS=$($CMD | wc -l)
4152         [ $NUMS -eq $EXPECTED ] ||
4153                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4154         EXPECTED=12
4155         CMD="$LFIND -size -5 -type f $TDIR"
4156         NUMS=$($CMD | wc -l)
4157         [ $NUMS -eq $EXPECTED ] ||
4158                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4159 }
4160 run_test 56r "check lfs find -size works =========================="
4161
4162 test_56s() { # LU-611
4163         TDIR=$DIR/${tdir}s
4164         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4165
4166         if [ $OSTCOUNT -gt 1 ]; then
4167                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4168                 ONESTRIPE=4
4169                 EXTRA=4
4170         else
4171                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4172                 EXTRA=0
4173         fi
4174
4175         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4176         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4177         NUMS=$($CMD | wc -l)
4178         [ $NUMS -eq $EXPECTED ] ||
4179                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4180
4181         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4182         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4183         NUMS=$($CMD | wc -l)
4184         [ $NUMS -eq $EXPECTED ] ||
4185                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4186
4187         EXPECTED=$ONESTRIPE
4188         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4189         NUMS=$($CMD | wc -l)
4190         [ $NUMS -eq $EXPECTED ] ||
4191                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4192
4193         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4194         NUMS=$($CMD | wc -l)
4195         [ $NUMS -eq $EXPECTED ] ||
4196                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4197
4198         EXPECTED=0
4199         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4200         NUMS=$($CMD | wc -l)
4201         [ $NUMS -eq $EXPECTED ] ||
4202                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4203 }
4204 run_test 56s "check lfs find -stripe-count works"
4205
4206 test_56t() { # LU-611
4207         TDIR=$DIR/${tdir}t
4208         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4209
4210         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4211
4212         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4213         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4214         NUMS=$($CMD | wc -l)
4215         [ $NUMS -eq $EXPECTED ] ||
4216                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4217
4218         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4219         NUMS=$($CMD | wc -l)
4220         [ $NUMS -eq $EXPECTED ] ||
4221                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4222
4223         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4224         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4225         NUMS=$($CMD | wc -l)
4226         [ $NUMS -eq $EXPECTED ] ||
4227                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4228
4229         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4230         NUMS=$($CMD | wc -l)
4231         [ $NUMS -eq $EXPECTED ] ||
4232                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4233
4234         EXPECTED=4
4235         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4236         NUMS=$($CMD | wc -l)
4237         [ $NUMS -eq $EXPECTED ] ||
4238                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4239
4240         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4241         NUMS=$($CMD | wc -l)
4242         [ $NUMS -eq $EXPECTED ] ||
4243                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4244
4245         EXPECTED=0
4246         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4247         NUMS=$($CMD | wc -l)
4248         [ $NUMS -eq $EXPECTED ] ||
4249                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4250 }
4251 run_test 56t "check lfs find -stripe-size works"
4252
4253 test_56u() { # LU-611
4254         TDIR=$DIR/${tdir}u
4255         setup_56 $NUMFILES $NUMDIRS "-i 0"
4256
4257         if [ $OSTCOUNT -gt 1 ]; then
4258                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4259                 ONESTRIPE=4
4260         else
4261                 ONESTRIPE=0
4262         fi
4263
4264         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4265         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4266         NUMS=$($CMD | wc -l)
4267         [ $NUMS -eq $EXPECTED ] ||
4268                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4269
4270         EXPECTED=$ONESTRIPE
4271         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4272         NUMS=$($CMD | wc -l)
4273         [ $NUMS -eq $EXPECTED ] ||
4274                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4275
4276         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4277         NUMS=$($CMD | wc -l)
4278         [ $NUMS -eq $EXPECTED ] ||
4279                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4280
4281         EXPECTED=0
4282         # This should produce an error and not return any files
4283         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4284         NUMS=$($CMD 2>/dev/null | wc -l)
4285         [ $NUMS -eq $EXPECTED ] ||
4286                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4287
4288         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4289         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4290         NUMS=$($CMD | wc -l)
4291         [ $NUMS -eq $EXPECTED ] ||
4292                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4293 }
4294 run_test 56u "check lfs find -stripe-index works"
4295
4296 test_56v() {
4297     local MDT_IDX=0
4298
4299     TDIR=$DIR/${tdir}v
4300     rm -rf $TDIR
4301     setup_56 $NUMFILES $NUMDIRS
4302
4303     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4304     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4305
4306     for file in $($LFIND -mdt $UUID $TDIR); do
4307         file_mdt_idx=$($GETSTRIPE -M $file)
4308         [ $file_mdt_idx -eq $MDT_IDX ] ||
4309             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4310     done
4311 }
4312 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4313
4314 # Get and check the actual stripe count of one file.
4315 # Usage: check_stripe_count <file> <expected_stripe_count>
4316 check_stripe_count() {
4317     local file=$1
4318     local expected=$2
4319     local actual
4320
4321     [[ -z "$file" || -z "$expected" ]] &&
4322         error "check_stripe_count: invalid argument!"
4323
4324     local cmd="$GETSTRIPE -c $file"
4325     actual=$($cmd) || error "$cmd failed"
4326     actual=${actual%% *}
4327
4328     if [[ $actual -ne $expected ]]; then
4329         [[ $expected -eq -1 ]] ||
4330             error "$cmd wrong: found $actual, expected $expected"
4331         [[ $actual -eq $OSTCOUNT ]] ||
4332             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4333     fi
4334 }
4335
4336 test_56w() {
4337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4338         TDIR=$DIR/${tdir}w
4339
4340     rm -rf $TDIR || error "remove $TDIR failed"
4341     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4342
4343     local stripe_size
4344     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4345         error "$GETSTRIPE -S -d $TDIR failed"
4346     stripe_size=${stripe_size%% *}
4347
4348     local file_size=$((stripe_size * OSTCOUNT))
4349     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4350     local required_space=$((file_num * file_size))
4351     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4352     [[ $free_space -le $((required_space / 1024)) ]] &&
4353         skip_env "need at least $required_space bytes free space," \
4354                  "have $free_space kbytes" && return
4355
4356     local dd_bs=65536
4357     local dd_count=$((file_size / dd_bs))
4358
4359     # write data into the files
4360     local i
4361     local j
4362     local file
4363     for i in $(seq 1 $NUMFILES); do
4364         file=$TDIR/file$i
4365         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4366             error "write data into $file failed"
4367     done
4368     for i in $(seq 1 $NUMDIRS); do
4369         for j in $(seq 1 $NUMFILES); do
4370             file=$TDIR/dir$i/file$j
4371             yes | dd bs=$dd_bs count=$dd_count of=$file \
4372                 >/dev/null 2>&1 ||
4373                 error "write data into $file failed"
4374         done
4375     done
4376
4377     local expected=-1
4378     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4379
4380     # lfs_migrate file
4381     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4382     echo "$cmd"
4383     eval $cmd || error "$cmd failed"
4384
4385     check_stripe_count $TDIR/file1 $expected
4386
4387     # lfs_migrate dir
4388     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4389     echo "$cmd"
4390     eval $cmd || error "$cmd failed"
4391
4392     for j in $(seq 1 $NUMFILES); do
4393         check_stripe_count $TDIR/dir1/file$j $expected
4394     done
4395
4396     # lfs_migrate works with lfs find
4397     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4398          $LFS_MIGRATE -y -c $expected"
4399     echo "$cmd"
4400     eval $cmd || error "$cmd failed"
4401
4402     for i in $(seq 2 $NUMFILES); do
4403         check_stripe_count $TDIR/file$i $expected
4404     done
4405     for i in $(seq 2 $NUMDIRS); do
4406         for j in $(seq 1 $NUMFILES); do
4407             check_stripe_count $TDIR/dir$i/file$j $expected
4408         done
4409     done
4410 }
4411 run_test 56w "check lfs_migrate -c stripe_count works"
4412
4413 test_56x() {
4414         check_swap_layouts_support && return 0
4415         [ "$OSTCOUNT" -lt "2" ] &&
4416                 skip_env "need 2 OST, skipping test" && return
4417
4418         local dir0=$DIR/$tdir/$testnum
4419         mkdir -p $dir0 || error "creating dir $dir0"
4420
4421         local ref1=/etc/passwd
4422         local file1=$dir0/file1
4423
4424         $SETSTRIPE -c 2 $file1
4425         cp $ref1 $file1
4426         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4427         stripe=$($GETSTRIPE -c $file1)
4428         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4429         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4430
4431         # clean up
4432         rm -f $file1
4433 }
4434 run_test 56x "lfs migration support"
4435
4436 test_57a() {
4437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4438         # note test will not do anything if MDS is not local
4439         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4440                 skip "Only applicable to ldiskfs-based MDTs"
4441                 return
4442         fi
4443
4444         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4445         local MNTDEV="osd*.*MDT*.mntdev"
4446         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4447         [ -z "$DEV" ] && error "can't access $MNTDEV"
4448         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4449                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4450                         error "can't access $DEV"
4451                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4452                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4453                 rm $TMP/t57a.dump
4454         done
4455 }
4456 run_test 57a "verify MDS filesystem created with large inodes =="
4457
4458 test_57b() {
4459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4460         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4461                 skip "Only applicable to ldiskfs-based MDTs"
4462                 return
4463         fi
4464
4465         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4466         local dir=$DIR/d57b
4467
4468         local FILECOUNT=100
4469         local FILE1=$dir/f1
4470         local FILEN=$dir/f$FILECOUNT
4471
4472         rm -rf $dir || error "removing $dir"
4473         test_mkdir -p $dir || error "creating $dir"
4474         local num=$(get_mds_dir $dir)
4475         local mymds=mds$num
4476
4477         echo "mcreating $FILECOUNT files"
4478         createmany -m $dir/f 1 $FILECOUNT || \
4479                 error "creating files in $dir"
4480
4481         # verify that files do not have EAs yet
4482         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4483         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4484
4485         sync
4486         sleep 1
4487         df $dir  #make sure we get new statfs data
4488         local MDSFREE=$(do_facet $mymds \
4489                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4490         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4491         echo "opening files to create objects/EAs"
4492         local FILE
4493         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4494                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4495         done
4496
4497         # verify that files have EAs now
4498         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4499         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4500
4501         sleep 1  #make sure we get new statfs data
4502         df $dir
4503         local MDSFREE2=$(do_facet $mymds \
4504                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4505         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4506         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4507                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4508                         error "MDC before $MDCFREE != after $MDCFREE2"
4509                 else
4510                         echo "MDC before $MDCFREE != after $MDCFREE2"
4511                         echo "unable to confirm if MDS has large inodes"
4512                 fi
4513         fi
4514         rm -rf $dir
4515 }
4516 run_test 57b "default LOV EAs are stored inside large inodes ==="
4517
4518 test_58() {
4519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4520         [ -z "$(which wiretest 2>/dev/null)" ] &&
4521                         skip_env "could not find wiretest" && return
4522         wiretest
4523 }
4524 run_test 58 "verify cross-platform wire constants =============="
4525
4526 test_59() {
4527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4528         echo "touch 130 files"
4529         createmany -o $DIR/f59- 130
4530         echo "rm 130 files"
4531         unlinkmany $DIR/f59- 130
4532         sync
4533         # wait for commitment of removal
4534         wait_delete_completed
4535 }
4536 run_test 59 "verify cancellation of llog records async ========="
4537
4538 TEST60_HEAD="test_60 run $RANDOM"
4539 test_60a() {
4540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4541         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4542         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4543         log "$TEST60_HEAD - from kernel mode"
4544         do_facet mgs sh run-llog.sh
4545 }
4546 run_test 60a "llog sanity tests run from kernel module =========="
4547
4548 test_60b() { # bug 6411
4549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4550         dmesg > $DIR/$tfile
4551         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4552                                  /llog.test/ {
4553                                          if (marker)
4554                                                  from_marker++
4555                                          from_begin++
4556                                  }
4557                                  END {
4558                                          if (marker)
4559                                                  print from_marker
4560                                          else
4561                                                  print from_begin
4562                                  }"`
4563         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4564 }
4565 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4566
4567 test_60c() {
4568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4569         echo "create 5000 files"
4570         createmany -o $DIR/f60c- 5000
4571 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4572         lctl set_param fail_loc=0x80000137
4573         unlinkmany $DIR/f60c- 5000
4574         lctl set_param fail_loc=0
4575 }
4576 run_test 60c "unlink file when mds full"
4577
4578 test_60d() {
4579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4580         SAVEPRINTK=$(lctl get_param -n printk)
4581
4582         # verify "lctl mark" is even working"
4583         MESSAGE="test message ID $RANDOM $$"
4584         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4585         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4586
4587         lctl set_param printk=0 || error "set lnet.printk failed"
4588         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4589         MESSAGE="new test message ID $RANDOM $$"
4590         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4591         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4592         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4593
4594         lctl set_param -n printk="$SAVEPRINTK"
4595 }
4596 run_test 60d "test printk console message masking"
4597
4598 test_61() {
4599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4600         f="$DIR/f61"
4601         dd if=/dev/zero of=$f bs=`page_size` count=1
4602         cancel_lru_locks osc
4603         $MULTIOP $f OSMWUc || error
4604         sync
4605 }
4606 run_test 61 "mmap() writes don't make sync hang ================"
4607
4608 # bug 2330 - insufficient obd_match error checking causes LBUG
4609 test_62() {
4610         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4611         f="$DIR/f62"
4612         echo foo > $f
4613         cancel_lru_locks osc
4614         lctl set_param fail_loc=0x405
4615         cat $f && error "cat succeeded, expect -EIO"
4616         lctl set_param fail_loc=0
4617 }
4618 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4619 # match every page all of the time.
4620 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4621
4622 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4623 test_63a() {    # was test_63
4624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4625         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4626         lctl set_param -n osc.*.max_dirty_mb 0
4627         for i in `seq 10` ; do
4628                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4629                 sleep 5
4630                 kill $!
4631                 sleep 1
4632         done
4633
4634         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4635         rm -f $DIR/f63 || true
4636 }
4637 run_test 63a "Verify oig_wait interruption does not crash ======="
4638
4639 # bug 2248 - async write errors didn't return to application on sync
4640 # bug 3677 - async write errors left page locked
4641 test_63b() {
4642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4643         debugsave
4644         lctl set_param debug=-1
4645
4646         # ensure we have a grant to do async writes
4647         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4648         rm $DIR/$tfile
4649
4650         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4651         lctl set_param fail_loc=0x80000406
4652         $MULTIOP $DIR/$tfile Owy && \
4653                 error "sync didn't return ENOMEM"
4654         sync; sleep 2; sync     # do a real sync this time to flush page
4655         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4656                 error "locked page left in cache after async error" || true
4657         debugrestore
4658 }
4659 run_test 63b "async write errors should be returned to fsync ==="
4660
4661 test_64a () {
4662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4663         df $DIR
4664         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4665 }
4666 run_test 64a "verify filter grant calculations (in kernel) ====="
4667
4668 test_64b () {
4669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4670         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4671         sh oos.sh $MOUNT
4672 }
4673 run_test 64b "check out-of-space detection on client ==========="
4674
4675 test_64c() {
4676         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4677 }
4678 run_test 64c "verify grant shrink ========================------"
4679
4680 # bug 1414 - set/get directories' stripe info
4681 test_65a() {
4682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4683         test_mkdir -p $DIR/$tdir
4684         touch $DIR/$tdir/f1
4685         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4686 }
4687 run_test 65a "directory with no stripe info ===================="
4688
4689 test_65b() {
4690         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4691         test_mkdir -p $DIR/$tdir
4692         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4693                                                 error "setstripe"
4694         touch $DIR/$tdir/f2
4695         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4696 }
4697 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4698
4699 test_65c() {
4700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4701         if [ $OSTCOUNT -gt 1 ]; then
4702                 test_mkdir -p $DIR/$tdir
4703                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4704                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4705                 touch $DIR/$tdir/f3
4706                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4707         fi
4708 }
4709 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4710
4711 test_65d() {
4712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4713         test_mkdir -p $DIR/$tdir
4714         if [ $STRIPECOUNT -le 0 ]; then
4715                 sc=1
4716         elif [ $STRIPECOUNT -gt 2000 ]; then
4717 #LOV_MAX_STRIPE_COUNT is 2000
4718                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4719         else
4720                 sc=$(($STRIPECOUNT - 1))
4721         fi
4722         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4723         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4724         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4725                                                 error "lverify failed"
4726 }
4727 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4728
4729 test_65e() {
4730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4731         test_mkdir -p $DIR/$tdir
4732
4733         $SETSTRIPE $DIR/$tdir || error "setstripe"
4734         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4735                                         error "no stripe info failed"
4736         touch $DIR/$tdir/f6
4737         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4738 }
4739 run_test 65e "directory setstripe defaults ======================="
4740
4741 test_65f() {
4742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4743         test_mkdir -p $DIR/${tdir}f
4744         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4745 }
4746 run_test 65f "dir setstripe permission (should return error) ==="
4747
4748 test_65g() {
4749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4750         test_mkdir -p $DIR/$tdir
4751         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4752                                                         error "setstripe"
4753         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4754         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4755                 error "delete default stripe failed"
4756 }
4757 run_test 65g "directory setstripe -d ==========================="
4758
4759 test_65h() {
4760         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4761         test_mkdir -p $DIR/$tdir
4762         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4763                                                         error "setstripe"
4764         test_mkdir -p $DIR/$tdir/dd1
4765         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4766                 error "stripe info inherit failed"
4767 }
4768 run_test 65h "directory stripe info inherit ===================="
4769
4770 test_65i() { # bug6367
4771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4772         $SETSTRIPE -S 65536 -c -1 $MOUNT
4773 }
4774 run_test 65i "set non-default striping on root directory (bug 6367)="
4775
4776 test_65ia() { # bug12836
4777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4778         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4779 }
4780 run_test 65ia "getstripe on -1 default directory striping"
4781
4782 test_65ib() { # bug12836
4783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4784         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4785 }
4786 run_test 65ib "getstripe -v on -1 default directory striping"
4787
4788 test_65ic() { # bug12836
4789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4790         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4791 }
4792 run_test 65ic "new find on -1 default directory striping"
4793
4794 test_65j() { # bug6367
4795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4796         sync; sleep 1
4797         # if we aren't already remounting for each test, do so for this test
4798         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4799                 cleanup || error "failed to unmount"
4800                 setup
4801         fi
4802         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4803 }
4804 run_test 65j "set default striping on root directory (bug 6367)="
4805
4806 test_65k() { # bug11679
4807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4808         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4809         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4810
4811     echo "Check OST status: "
4812     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4813               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4814
4815     for OSC in $MDS_OSCS; do
4816         echo $OSC "is activate"
4817         do_facet $SINGLEMDS lctl --device %$OSC activate
4818     done
4819
4820     mkdir -p $DIR/$tdir
4821     for INACTIVE_OSC in $MDS_OSCS; do
4822         echo "Deactivate: " $INACTIVE_OSC
4823         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4824         for STRIPE_OSC in $MDS_OSCS; do
4825             OST=`osc_to_ost $STRIPE_OSC`
4826             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4827                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4828
4829             [ -f $DIR/$tdir/$IDX ] && continue
4830             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4831             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4832             RC=$?
4833             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4834         done
4835         rm -f $DIR/$tdir/*
4836         echo $INACTIVE_OSC "is Activate."
4837         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4838     done
4839 }
4840 run_test 65k "validate manual striping works properly with deactivated OSCs"
4841
4842 test_65l() { # bug 12836
4843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4844         test_mkdir -p $DIR/$tdir/test_dir
4845         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4846         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4847 }
4848 run_test 65l "lfs find on -1 stripe dir ========================"
4849
4850 # bug 2543 - update blocks count on client
4851 test_66() {
4852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4853         COUNT=${COUNT:-8}
4854         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4855         sync; sync_all_data; sync; sync_all_data
4856         cancel_lru_locks osc
4857         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4858         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4859 }
4860 run_test 66 "update inode blocks count on client ==============="
4861
4862 LLOOP=
4863 LLITELOOPLOAD=
4864 cleanup_68() {
4865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4866         trap 0
4867         if [ ! -z "$LLOOP" ]; then
4868                 if swapon -s | grep -q $LLOOP; then
4869                         swapoff $LLOOP || error "swapoff failed"
4870                 fi
4871
4872                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4873                 rm -f $LLOOP
4874                 unset LLOOP
4875         fi
4876         if [ ! -z "$LLITELOOPLOAD" ]; then
4877                 rmmod llite_lloop
4878                 unset LLITELOOPLOAD
4879         fi
4880         rm -f $DIR/f68*
4881 }
4882
4883 meminfo() {
4884         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4885 }
4886
4887 swap_used() {
4888         swapon -s | awk '($1 == "'$1'") { print $4 }'
4889 }
4890
4891 # test case for lloop driver, basic function
4892 test_68a() {
4893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4894         [ "$UID" != 0 ] && skip_env "must run as root" && return
4895         llite_lloop_enabled || \
4896                 { skip_env "llite_lloop module disabled" && return; }
4897
4898         trap cleanup_68 EXIT
4899
4900         if ! module_loaded llite_lloop; then
4901                 if load_module llite/llite_lloop; then
4902                         LLITELOOPLOAD=yes
4903                 else
4904                         skip_env "can't find module llite_lloop"
4905                         return
4906                 fi
4907         fi
4908
4909         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4910         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4911         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4912
4913         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4914         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4915
4916         cleanup_68
4917 }
4918 run_test 68a "lloop driver - basic test ========================"
4919
4920 # excercise swapping to lustre by adding a high priority swapfile entry
4921 # and then consuming memory until it is used.
4922 test_68b() {  # was test_68
4923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4924         [ "$UID" != 0 ] && skip_env "must run as root" && return
4925         lctl get_param -n devices | grep -q obdfilter && \
4926                 skip "local OST" && return
4927
4928         grep -q llite_lloop /proc/modules
4929         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4930
4931         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4932                 skip "can't reliably test swap with TCP" && return
4933
4934         MEMTOTAL=`meminfo MemTotal`
4935         NR_BLOCKS=$((MEMTOTAL>>8))
4936         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4937
4938         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4939         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4940         mkswap $DIR/f68b
4941
4942         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4943
4944         trap cleanup_68 EXIT
4945
4946         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4947
4948         echo "before: `swapon -s | grep $LLOOP`"
4949         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4950         echo "after: `swapon -s | grep $LLOOP`"
4951         SWAPUSED=`swap_used $LLOOP`
4952
4953         cleanup_68
4954
4955         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4956 }
4957 run_test 68b "support swapping to Lustre ========================"
4958
4959 # bug5265, obdfilter oa2dentry return -ENOENT
4960 # #define OBD_FAIL_OST_ENOENT 0x217
4961 test_69() {
4962         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4963         remote_ost_nodsh && skip "remote OST with nodsh" && return
4964
4965         f="$DIR/$tfile"
4966         $SETSTRIPE -c 1 -i 0 $f
4967
4968         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4969
4970         do_facet ost1 lctl set_param fail_loc=0x217
4971         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4972         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4973
4974         do_facet ost1 lctl set_param fail_loc=0
4975         $DIRECTIO write $f 0 2 || error "write error"
4976
4977         cancel_lru_locks osc
4978         $DIRECTIO read $f 0 1 || error "read error"
4979
4980         do_facet ost1 lctl set_param fail_loc=0x217
4981         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4982
4983         do_facet ost1 lctl set_param fail_loc=0
4984         rm -f $f
4985 }
4986 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4987
4988 test_71() {
4989     test_mkdir -p $DIR/$tdir
4990     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4991 }
4992 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4993
4994 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4996         check_kernel_version 43 || return 0
4997         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4998
4999         # Check that testing environment is properly set up. Skip if not
5000         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5001                 skip_env "User $RUNAS_ID does not exist - skipping"
5002                 return 0
5003         }
5004         # We had better clear the $DIR to get enough space for dd
5005         rm -rf $DIR/*
5006         touch $DIR/f72
5007         chmod 777 $DIR/f72
5008         chmod ug+s $DIR/f72
5009         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
5010         # See if we are still setuid/sgid
5011         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
5012         # Now test that MDS is updated too
5013         cancel_lru_locks mdc
5014         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
5015         rm -f $DIR/f72
5016 }
5017 run_test 72a "Test that remove suid works properly (bug5695) ===="
5018
5019 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5020         local perm
5021
5022         [ "$RUNAS_ID" = "$UID" ] && \
5023                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5024         [ "$RUNAS_ID" -eq 0 ] && \
5025                 skip_env "RUNAS_ID = 0 -- skipping" && return
5026
5027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5028         # Check that testing environment is properly set up. Skip if not
5029         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5030                 skip_env "User $RUNAS_ID does not exist - skipping"
5031                 return 0
5032         }
5033         touch $DIR/${tfile}-f{g,u}
5034         test_mkdir $DIR/${tfile}-dg
5035         test_mkdir $DIR/${tfile}-du
5036         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5037         chmod g+s $DIR/${tfile}-{f,d}g
5038         chmod u+s $DIR/${tfile}-{f,d}u
5039         for perm in 777 2777 4777; do
5040                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5041                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5042                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5043                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5044         done
5045         true
5046 }
5047 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5048
5049 # bug 3462 - multiple simultaneous MDC requests
5050 test_73() {
5051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5052         test_mkdir $DIR/d73-1
5053         test_mkdir $DIR/d73-2
5054         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5055         pid1=$!
5056
5057         lctl set_param fail_loc=0x80000129
5058         $MULTIOP $DIR/d73-1/f73-2 Oc &
5059         sleep 1
5060         lctl set_param fail_loc=0
5061
5062         $MULTIOP $DIR/d73-2/f73-3 Oc &
5063         pid3=$!
5064
5065         kill -USR1 $pid1
5066         wait $pid1 || return 1
5067
5068         sleep 25
5069
5070         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5071         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5072         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5073
5074         rm -rf $DIR/d73-*
5075 }
5076 run_test 73 "multiple MDC requests (should not deadlock)"
5077
5078 test_74a() { # bug 6149, 6184
5079         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5080         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5081         #
5082         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5083         # will spin in a tight reconnection loop
5084         touch $DIR/f74a
5085         lctl set_param fail_loc=0x8000030e
5086         # get any lock that won't be difficult - lookup works.
5087         ls $DIR/f74a
5088         lctl set_param fail_loc=0
5089         true
5090         rm -f $DIR/f74a
5091 }
5092 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5093
5094 test_74b() { # bug 13310
5095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5096         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5097         #
5098         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5099         # will spin in a tight reconnection loop
5100         lctl set_param fail_loc=0x8000030e
5101         # get a "difficult" lock
5102         touch $DIR/f74b
5103         lctl set_param fail_loc=0
5104         true
5105         rm -f $DIR/f74b
5106 }
5107 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5108
5109 test_74c() {
5110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5111 #define OBD_FAIL_LDLM_NEW_LOCK
5112         lctl set_param fail_loc=0x80000319
5113         touch $DIR/$tfile && error "Touch successful"
5114         true
5115 }
5116 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5117
5118 num_inodes() {
5119         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5120 }
5121
5122 get_inode_slab_tunables() {
5123         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5124 }
5125
5126 set_inode_slab_tunables() {
5127         echo "lustre_inode_cache $1" > /proc/slabinfo
5128 }
5129
5130 test_76() { # Now for bug 20433, added originally in bug 1443
5131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5132         local SLAB_SETTINGS=`get_inode_slab_tunables`
5133         local CPUS=`getconf _NPROCESSORS_ONLN`
5134         # we cannot set limit below 1 which means 1 inode in each
5135         # per-cpu cache is still allowed
5136         set_inode_slab_tunables "1 1 0"
5137         cancel_lru_locks osc
5138         BEFORE_INODES=`num_inodes`
5139         echo "before inodes: $BEFORE_INODES"
5140         local COUNT=1000
5141         [ "$SLOW" = "no" ] && COUNT=100
5142         for i in `seq $COUNT`; do
5143                 touch $DIR/$tfile
5144                 rm -f $DIR/$tfile
5145         done
5146         cancel_lru_locks osc
5147         AFTER_INODES=`num_inodes`
5148         echo "after inodes: $AFTER_INODES"
5149         local wait=0
5150         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5151                 sleep 2
5152                 AFTER_INODES=`num_inodes`
5153                 wait=$((wait+2))
5154                 echo "wait $wait seconds inodes: $AFTER_INODES"
5155                 if [ $wait -gt 30 ]; then
5156                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5157                 fi
5158         done
5159         set_inode_slab_tunables "$SLAB_SETTINGS"
5160 }
5161 run_test 76 "confirm clients recycle inodes properly ===="
5162
5163
5164 export ORIG_CSUM=""
5165 set_checksums()
5166 {
5167         # Note: in sptlrpc modes which enable its own bulk checksum, the
5168         # original crc32_le bulk checksum will be automatically disabled,
5169         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5170         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5171         # In this case set_checksums() will not be no-op, because sptlrpc
5172         # bulk checksum will be enabled all through the test.
5173
5174         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5175         lctl set_param -n osc.*.checksums $1
5176         return 0
5177 }
5178
5179 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5180                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5181 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5182 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5183 set_checksum_type()
5184 {
5185         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5186         log "set checksum type to $1"
5187         return 0
5188 }
5189 F77_TMP=$TMP/f77-temp
5190 F77SZ=8
5191 setup_f77() {
5192         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5193                 error "error writing to $F77_TMP"
5194 }
5195
5196 test_77a() { # bug 10889
5197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5198         $GSS && skip "could not run with gss" && return
5199         [ ! -f $F77_TMP ] && setup_f77
5200         set_checksums 1
5201         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5202         set_checksums 0
5203         rm -f $DIR/$tfile
5204 }
5205 run_test 77a "normal checksum read/write operation ============="
5206
5207 test_77b() { # bug 10889
5208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5209         $GSS && skip "could not run with gss" && return
5210         [ ! -f $F77_TMP ] && setup_f77
5211         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5212         lctl set_param fail_loc=0x80000409
5213         set_checksums 1
5214         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
5215                 error "dd error: $?"
5216         lctl set_param fail_loc=0
5217         set_checksums 0
5218 }
5219 run_test 77b "checksum error on client write ===================="
5220
5221 test_77c() { # bug 10889
5222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5223         $GSS && skip "could not run with gss" && return
5224         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
5225         set_checksums 1
5226         for algo in $CKSUM_TYPES; do
5227                 cancel_lru_locks osc
5228                 set_checksum_type $algo
5229                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5230                 lctl set_param fail_loc=0x80000408
5231                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
5232                 lctl set_param fail_loc=0
5233         done
5234         set_checksums 0
5235         set_checksum_type $ORIG_CSUM_TYPE
5236         rm -f $DIR/f77b
5237 }
5238 run_test 77c "checksum error on client read ==================="
5239
5240 test_77d() { # bug 10889
5241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5242         $GSS && skip "could not run with gss" && return
5243         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5244         lctl set_param fail_loc=0x80000409
5245         set_checksums 1
5246         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5247                 error "direct write: rc=$?"
5248         lctl set_param fail_loc=0
5249         set_checksums 0
5250 }
5251 run_test 77d "checksum error on OST direct write ==============="
5252
5253 test_77e() { # bug 10889
5254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5255         $GSS && skip "could not run with gss" && return
5256         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5257         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5258         lctl set_param fail_loc=0x80000408
5259         set_checksums 1
5260         cancel_lru_locks osc
5261         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5262                 error "direct read: rc=$?"
5263         lctl set_param fail_loc=0
5264         set_checksums 0
5265 }
5266 run_test 77e "checksum error on OST direct read ================"
5267
5268 test_77f() { # bug 10889
5269         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5270         $GSS && skip "could not run with gss" && return
5271         set_checksums 1
5272         for algo in $CKSUM_TYPES; do
5273                 cancel_lru_locks osc
5274                 set_checksum_type $algo
5275                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5276                 lctl set_param fail_loc=0x409
5277                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5278                         error "direct write succeeded"
5279                 lctl set_param fail_loc=0
5280         done
5281         set_checksum_type $ORIG_CSUM_TYPE
5282         set_checksums 0
5283 }
5284 run_test 77f "repeat checksum error on write (expect error) ===="
5285
5286 test_77g() { # bug 10889
5287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5288         $GSS && skip "could not run with gss" && return
5289         remote_ost_nodsh && skip "remote OST with nodsh" && return
5290
5291         [ ! -f $F77_TMP ] && setup_f77
5292
5293         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5294         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5295         do_facet ost1 lctl set_param fail_loc=0x8000021a
5296         set_checksums 1
5297         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5298                 error "write error: rc=$?"
5299         do_facet ost1 lctl set_param fail_loc=0
5300         set_checksums 0
5301 }
5302 run_test 77g "checksum error on OST write ======================"
5303
5304 test_77h() { # bug 10889
5305         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5306         $GSS && skip "could not run with gss" && return
5307         remote_ost_nodsh && skip "remote OST with nodsh" && return
5308
5309         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5310         cancel_lru_locks osc
5311         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5312         do_facet ost1 lctl set_param fail_loc=0x8000021b
5313         set_checksums 1
5314         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5315         do_facet ost1 lctl set_param fail_loc=0
5316         set_checksums 0
5317 }
5318 run_test 77h "checksum error on OST read ======================="
5319
5320 test_77i() { # bug 13805
5321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5322         $GSS && skip "could not run with gss" && return
5323         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5324         lctl set_param fail_loc=0x40b
5325         remount_client $MOUNT
5326         lctl set_param fail_loc=0
5327         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5328                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5329                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5330                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5331         done
5332         remount_client $MOUNT
5333 }
5334 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5335
5336 test_77j() { # bug 13805
5337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5338         $GSS && skip "could not run with gss" && return
5339         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5340         lctl set_param fail_loc=0x40c
5341         remount_client $MOUNT
5342         lctl set_param fail_loc=0
5343         sleep 2 # wait async osc connect to finish
5344         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5345                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5346                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5347                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5348         done
5349         remount_client $MOUNT
5350 }
5351 run_test 77j "client only supporting ADLER32 ===================="
5352
5353 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5354 rm -f $F77_TMP
5355 unset F77_TMP
5356
5357 test_78() { # bug 10901
5358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5359         remote_ost || { skip_env "local OST" && return; }
5360
5361         NSEQ=5
5362         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5363         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5364         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5365         echo "MemTotal: $MEMTOTAL"
5366 # reserve 256MB of memory for the kernel and other running processes,
5367 # and then take 1/2 of the remaining memory for the read/write buffers.
5368     if [ $MEMTOTAL -gt 512 ] ;then
5369         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5370     else
5371         # for those poor memory-starved high-end clusters...
5372         MEMTOTAL=$((MEMTOTAL / 2))
5373     fi
5374         echo "Mem to use for directio: $MEMTOTAL"
5375         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5376         [ $F78SIZE -gt 512 ] && F78SIZE=512
5377         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5378         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5379         echo "Smallest OST: $SMALLESTOST"
5380         [ $SMALLESTOST -lt 10240 ] && \
5381                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5382
5383         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5384                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5385
5386         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5387         echo "File size: $F78SIZE"
5388         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5389         for i in `seq 1 $NSEQ`
5390         do
5391                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5392                 echo directIO rdwr round $i of $NSEQ
5393                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5394         done
5395
5396         rm -f $DIR/$tfile
5397 }
5398 run_test 78 "handle large O_DIRECT writes correctly ============"
5399
5400 test_79() { # bug 12743
5401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5402         wait_delete_completed
5403
5404         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5405         BKFREE=$(calc_osc_kbytes kbytesfree)
5406         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5407
5408         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5409         DFTOTAL=`echo $STRING | cut -d, -f1`
5410         DFUSED=`echo $STRING  | cut -d, -f2`
5411         DFAVAIL=`echo $STRING | cut -d, -f3`
5412         DFFREE=$(($DFTOTAL - $DFUSED))
5413
5414         ALLOWANCE=$((64 * $OSTCOUNT))
5415
5416         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5417            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5418                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5419         fi
5420         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5421            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5422                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5423         fi
5424         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5425            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5426                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5427         fi
5428 }
5429 run_test 79 "df report consistency check ======================="
5430
5431 test_80() { # bug 10718
5432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5433         # relax strong synchronous semantics for slow backends like ZFS
5434         local soc="obdfilter.*.sync_on_lock_cancel"
5435         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5436         local hosts=
5437         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5438                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5439                           facet_active_host $host; done | sort -u)
5440                 do_nodes $hosts lctl set_param $soc=never
5441         fi
5442
5443         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5444         sync; sleep 1; sync
5445         local BEFORE=`date +%s`
5446         cancel_lru_locks osc
5447         local AFTER=`date +%s`
5448         local DIFF=$((AFTER-BEFORE))
5449         if [ $DIFF -gt 1 ] ; then
5450                 error "elapsed for 1M@1T = $DIFF"
5451         fi
5452
5453         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5454
5455         rm -f $DIR/$tfile
5456 }
5457 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5458
5459 test_81a() { # LU-456
5460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5461         remote_ost_nodsh && skip "remote OST with nodsh" && return
5462         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5463         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5464         do_facet ost1 lctl set_param fail_loc=0x80000228
5465
5466         # write should trigger a retry and success
5467         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5468         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5469         RC=$?
5470         if [ $RC -ne 0 ] ; then
5471                 error "write should success, but failed for $RC"
5472         fi
5473 }
5474 run_test 81a "OST should retry write when get -ENOSPC ==============="
5475
5476 test_81b() { # LU-456
5477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5478         remote_ost_nodsh && skip "remote OST with nodsh" && return
5479         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5480         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5481         do_facet ost1 lctl set_param fail_loc=0x228
5482
5483         # write should retry several times and return -ENOSPC finally
5484         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5485         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5486         RC=$?
5487         ENOSPC=28
5488         if [ $RC -ne $ENOSPC ] ; then
5489                 error "dd should fail for -ENOSPC, but succeed."
5490         fi
5491 }
5492 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5493
5494 test_82() { # LU-1031
5495         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5496         local gid1=14091995
5497         local gid2=16022000
5498
5499         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5500         local MULTIPID1=$!
5501         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5502         local MULTIPID2=$!
5503         kill -USR1 $MULTIPID2
5504         sleep 2
5505         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5506                 error "First grouplock does not block second one"
5507         else
5508                 echo "Second grouplock blocks first one"
5509         fi
5510         kill -USR1 $MULTIPID1
5511         wait $MULTIPID1
5512         wait $MULTIPID2
5513 }
5514 run_test 82 "Basic grouplock test ==============================="
5515
5516 test_99a() {
5517         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5518             return
5519         test_mkdir -p $DIR/d99cvsroot
5520         chown $RUNAS_ID $DIR/d99cvsroot
5521         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5522         cd $TMP
5523
5524         $RUNAS cvs -d $DIR/d99cvsroot init || error
5525         cd $oldPWD
5526 }
5527 run_test 99a "cvs init ========================================="
5528
5529 test_99b() {
5530         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5531         [ ! -d $DIR/d99cvsroot ] && test_99a
5532         cd /etc/init.d
5533         # some versions of cvs import exit(1) when asked to import links or
5534         # files they can't read.  ignore those files.
5535         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5536                         ! -perm +4 -printf '-I %f\n')
5537         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5538                 d99reposname vtag rtag
5539 }
5540 run_test 99b "cvs import ======================================="
5541
5542 test_99c() {
5543         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5544         [ ! -d $DIR/d99cvsroot ] && test_99b
5545         cd $DIR
5546         test_mkdir -p $DIR/d99reposname
5547         chown $RUNAS_ID $DIR/d99reposname
5548         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5549 }
5550 run_test 99c "cvs checkout ====================================="
5551
5552 test_99d() {
5553         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5554         [ ! -d $DIR/d99cvsroot ] && test_99c
5555         cd $DIR/d99reposname
5556         $RUNAS touch foo99
5557         $RUNAS cvs add -m 'addmsg' foo99
5558 }
5559 run_test 99d "cvs add =========================================="
5560
5561 test_99e() {
5562         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5563         [ ! -d $DIR/d99cvsroot ] && test_99c
5564         cd $DIR/d99reposname
5565         $RUNAS cvs update
5566 }
5567 run_test 99e "cvs update ======================================="
5568
5569 test_99f() {
5570         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5571         [ ! -d $DIR/d99cvsroot ] && test_99d
5572         cd $DIR/d99reposname
5573         $RUNAS cvs commit -m 'nomsg' foo99
5574     rm -fr $DIR/d99cvsroot
5575 }
5576 run_test 99f "cvs commit ======================================="
5577
5578 test_100() {
5579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5580         [ "$NETTYPE" = tcp ] || \
5581                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5582                         return ; }
5583
5584         remote_ost_nodsh && skip "remote OST with nodsh" && return
5585         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5586         remote_servers || \
5587                 { skip "useless for local single node setup" && return; }
5588
5589         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5590                 [ "$PROT" != "tcp" ] && continue
5591                 RPORT=$(echo $REMOTE | cut -d: -f2)
5592                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5593
5594                 rc=0
5595                 LPORT=`echo $LOCAL | cut -d: -f2`
5596                 if [ $LPORT -ge 1024 ]; then
5597                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5598                         netstat -tna
5599                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5600                 fi
5601         done
5602         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5603 }
5604 run_test 100 "check local port using privileged port ==========="
5605
5606 function get_named_value()
5607 {
5608     local tag
5609
5610     tag=$1
5611     while read ;do
5612         line=$REPLY
5613         case $line in
5614         $tag*)
5615             echo $line | sed "s/^$tag[ ]*//"
5616             break
5617             ;;
5618         esac
5619     done
5620 }
5621
5622 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5623                    awk '/^max_cached_mb/ { print $2 }')
5624
5625 cleanup_101a() {
5626         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5627         trap 0
5628 }
5629
5630 test_101a() {
5631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5632         local s
5633         local discard
5634         local nreads=10000
5635         local cache_limit=32
5636
5637         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5638         trap cleanup_101a EXIT
5639         $LCTL set_param -n llite.*.read_ahead_stats 0
5640         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5641
5642         #
5643         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5644         #
5645         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5646         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5647
5648         discard=0
5649         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5650                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5651                         discard=$(($discard + $s))
5652         done
5653         cleanup_101a
5654
5655         if [ $(($discard * 10)) -gt $nreads ] ;then
5656                 $LCTL get_param osc.*-osc*.rpc_stats
5657                 $LCTL get_param llite.*.read_ahead_stats
5658                 error "too many ($discard) discarded pages"
5659         fi
5660         rm -f $DIR/$tfile || true
5661 }
5662 run_test 101a "check read-ahead for random reads ================"
5663
5664 setup_test101bc() {
5665         test_mkdir -p $DIR/$tdir
5666         STRIPE_SIZE=1048576
5667         STRIPE_COUNT=$OSTCOUNT
5668         STRIPE_OFFSET=0
5669
5670         local list=$(comma_list $(osts_nodes))
5671         set_osd_param $list '' read_cache_enable 0
5672         set_osd_param $list '' writethrough_cache_enable 0
5673
5674         trap cleanup_test101bc EXIT
5675         # prepare the read-ahead file
5676         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5677
5678         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5679 }
5680
5681 cleanup_test101bc() {
5682         trap 0
5683         rm -rf $DIR/$tdir
5684         rm -f $DIR/$tfile
5685
5686         local list=$(comma_list $(osts_nodes))
5687         set_osd_param $list '' read_cache_enable 1
5688         set_osd_param $list '' writethrough_cache_enable 1
5689 }
5690
5691 calc_total() {
5692         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5693 }
5694
5695 ra_check_101() {
5696         local READ_SIZE=$1
5697         local STRIPE_SIZE=1048576
5698         local RA_INC=1048576
5699         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5700         local FILE_LENGTH=$((64*100))
5701         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5702                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5703         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5704                         get_named_value 'read but discarded' | \
5705                         cut -d" " -f1 | calc_total`
5706         if [ $DISCARD -gt $discard_limit ]; then
5707                 $LCTL get_param llite.*.read_ahead_stats
5708                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5709         else
5710                 echo "Read-ahead success for size ${READ_SIZE}"
5711         fi
5712 }
5713
5714 test_101b() {
5715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5716         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5717         local STRIPE_SIZE=1048576
5718         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5719         local FILE_LENGTH=$((STRIPE_SIZE*100))
5720         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5721         # prepare the read-ahead file
5722         setup_test101bc
5723         cancel_lru_locks osc
5724         for BIDX in 2 4 8 16 32 64 128 256
5725         do
5726                 local BSIZE=$((BIDX*4096))
5727                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5728                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5729                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5730                 $LCTL set_param -n llite.*.read_ahead_stats 0
5731                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5732                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5733                 cancel_lru_locks osc
5734                 ra_check_101 $BSIZE
5735         done
5736         cleanup_test101bc
5737         true
5738 }
5739 run_test 101b "check stride-io mode read-ahead ================="
5740
5741 test_101c() {
5742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5743         local STRIPE_SIZE=1048576
5744         local FILE_LENGTH=$((STRIPE_SIZE*100))
5745         local nreads=10000
5746         local osc
5747
5748     setup_test101bc
5749
5750     cancel_lru_locks osc
5751     $LCTL set_param osc.*.rpc_stats 0
5752     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5753     for osc in $($LCTL get_param -N osc.*); do
5754         if [ "$osc" == "osc.num_refs" ]; then
5755             continue
5756         fi
5757
5758         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5759         if [ $lines -le 20 ]; then
5760             continue
5761         fi
5762
5763         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5764                                      awk '$1 == "1:" { print $2; exit; }')
5765         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5766                                      awk '$1 == "2:" { print $2; exit; }')
5767         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5768                                      awk '$1 == "4:" { print $2; exit; }')
5769         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5770                                      awk '$1 == "8:" { print $2; exit; }')
5771
5772         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5773         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5774         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5775         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5776         echo "${osc} rpc check passed!"
5777     done
5778     cleanup_test101bc
5779     true
5780 }
5781 run_test 101c "check stripe_size aligned read-ahead ================="
5782
5783 set_read_ahead() {
5784    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5785    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5786 }
5787
5788 test_101d() {
5789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5790         local file=$DIR/$tfile
5791         local size=${FILESIZE_101c:-500}
5792         local ra_MB=${READAHEAD_MB:-40}
5793
5794         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5795         [ $space -gt $((size * 1024)) ] ||
5796                 { skip "Need free space ${size}M, have $space" && return; }
5797
5798     echo Creating ${size}M test file $file
5799     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5800     echo Cancel LRU locks on lustre client to flush the client cache
5801     cancel_lru_locks osc
5802
5803     echo Disable read-ahead
5804     local old_READAHEAD=$(set_read_ahead 0)
5805
5806     echo Reading the test file $file with read-ahead disabled
5807     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5808
5809     echo Cancel LRU locks on lustre client to flush the client cache
5810     cancel_lru_locks osc
5811     echo Enable read-ahead with ${ra_MB}MB
5812     set_read_ahead $ra_MB
5813
5814     echo Reading the test file $file with read-ahead enabled
5815     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5816
5817     echo read-ahead disabled time read $time_ra_OFF
5818     echo read-ahead enabled  time read $time_ra_ON
5819
5820         set_read_ahead $old_READAHEAD
5821         rm -f $file
5822         wait_delete_completed
5823
5824     [ $time_ra_ON -lt $time_ra_OFF ] ||
5825         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5826                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5827 }
5828 run_test 101d "file read with and without read-ahead enabled  ================="
5829
5830 test_101e() {
5831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5832     local file=$DIR/$tfile
5833     local size=500  #KB
5834     local count=100
5835     local blksize=1024
5836
5837     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5838     local need_space=$((count * size))
5839     [ $space -gt $need_space ] ||
5840         { skip_env "Need free space $need_space, have $space" && return; }
5841
5842     echo Creating $count ${size}K test files
5843     for ((i = 0; i < $count; i++)); do
5844         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5845     done
5846
5847     echo Cancel LRU locks on lustre client to flush the client cache
5848     cancel_lru_locks osc
5849
5850     echo Reset readahead stats
5851     $LCTL set_param -n llite.*.read_ahead_stats 0
5852
5853     for ((i = 0; i < $count; i++)); do
5854         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5855     done
5856
5857     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5858           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5859
5860     for ((i = 0; i < $count; i++)); do
5861         rm -rf ${file}_${i} 2>/dev/null
5862     done
5863
5864     #10000 means 20% reads are missing in readahead
5865     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5866 }
5867 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5868
5869 cleanup_test101f() {
5870     trap 0
5871     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5872     rm -rf $DIR/$tfile 2>/dev/null
5873 }
5874
5875 test_101f() {
5876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5877     local file=$DIR/$tfile
5878     local nreads=1000
5879
5880     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5881     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5882     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5883     trap cleanup_test101f EXIT
5884
5885     echo Cancel LRU locks on lustre client to flush the client cache
5886     cancel_lru_locks osc
5887
5888     echo Reset readahead stats
5889     $LCTL set_param -n llite.*.read_ahead_stats 0
5890     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5891     # readahead should read in 2M file on second read, so only miss
5892     # 2 pages.
5893     echo Random 4K reads on 2M file for 1000 times
5894     $READS -f $file -s 2097152 -b 4096 -n $nreads
5895
5896     echo checking missing pages
5897     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5898           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5899
5900     [ $miss -lt 3 ] || error "misses too much pages!"
5901     cleanup_test101f
5902 }
5903 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5904
5905 setup_test102() {
5906         test_mkdir -p $DIR/$tdir
5907         chown $RUNAS_ID $DIR/$tdir
5908         STRIPE_SIZE=65536
5909         STRIPE_OFFSET=1
5910         STRIPE_COUNT=$OSTCOUNT
5911         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5912
5913         trap cleanup_test102 EXIT
5914         cd $DIR
5915         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5916         cd $DIR/$tdir
5917         for num in 1 2 3 4; do
5918                 for count in $(seq 1 $STRIPE_COUNT); do
5919                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5920                                 local size=`expr $STRIPE_SIZE \* $num`
5921                                 local file=file"$num-$idx-$count"
5922                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5923                         done
5924                 done
5925         done
5926
5927         cd $DIR
5928         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5929 }
5930
5931 cleanup_test102() {
5932         trap 0
5933         rm -f $TMP/f102.tar
5934         rm -rf $DIR/d0.sanity/d102
5935 }
5936
5937 test_102a() {
5938         local testfile=$DIR/xattr_testfile
5939
5940         touch $testfile
5941
5942         [ "$UID" != 0 ] && skip_env "must run as root" && return
5943         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5944                 skip_env "must have user_xattr" && return
5945
5946         [ -z "$(which setfattr 2>/dev/null)" ] &&
5947                 skip_env "could not find setfattr" && return
5948
5949         echo "set/get xattr..."
5950         setfattr -n trusted.name1 -v value1 $testfile || error
5951         getfattr -n trusted.name1 $testfile 2> /dev/null |
5952           grep "trusted.name1=.value1" ||
5953                 error "$testfile missing trusted.name1=value1"
5954
5955         setfattr -n user.author1 -v author1 $testfile || error
5956         getfattr -n user.author1 $testfile 2> /dev/null |
5957           grep "user.author1=.author1" ||
5958                 error "$testfile missing trusted.author1=author1"
5959
5960         echo "listxattr..."
5961         setfattr -n trusted.name2 -v value2 $testfile ||
5962                 error "$testfile unable to set trusted.name2"
5963         setfattr -n trusted.name3 -v value3 $testfile ||
5964                 error "$testfile unable to set trusted.name3"
5965         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5966             grep "trusted.name" | wc -l) -eq 3 ] ||
5967                 error "$testfile missing 3 trusted.name xattrs"
5968
5969         setfattr -n user.author2 -v author2 $testfile ||
5970                 error "$testfile unable to set user.author2"
5971         setfattr -n user.author3 -v author3 $testfile ||
5972                 error "$testfile unable to set user.author3"
5973         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5974             grep "user.author" | wc -l) -eq 3 ] ||
5975                 error "$testfile missing 3 user.author xattrs"
5976
5977         echo "remove xattr..."
5978         setfattr -x trusted.name1 $testfile ||
5979                 error "$testfile error deleting trusted.name1"
5980         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5981                 error "$testfile did not delete trusted.name1 xattr"
5982
5983         setfattr -x user.author1 $testfile ||
5984                 error "$testfile error deleting user.author1"
5985         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5986                 error "$testfile did not delete trusted.name1 xattr"
5987
5988         # b10667: setting lustre special xattr be silently discarded
5989         echo "set lustre special xattr ..."
5990         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5991                 error "$testfile allowed setting trusted.lov"
5992 }
5993 run_test 102a "user xattr test =================================="
5994
5995 test_102b() {
5996         # b10930: get/set/list trusted.lov xattr
5997         echo "get/set/list trusted.lov xattr ..."
5998         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5999         local testfile=$DIR/$tfile
6000         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6001                 error "setstripe failed"
6002         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6003                 error "getstripe failed"
6004         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
6005         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
6006
6007         local testfile2=${testfile}2
6008         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
6009                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
6010
6011         $MCREATE $testfile2
6012         setfattr -n trusted.lov -v $value $testfile2
6013         local stripe_size=$($GETSTRIPE -S $testfile2)
6014         local stripe_count=$($GETSTRIPE -c $testfile2)
6015         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6016         [ $stripe_count -eq $STRIPECOUNT ] ||
6017                 error "stripe count $stripe_count != $STRIPECOUNT"
6018         rm -f $DIR/$tfile
6019 }
6020 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6021
6022 test_102c() {
6023         # b10930: get/set/list lustre.lov xattr
6024         echo "get/set/list lustre.lov xattr ..."
6025         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6026         test_mkdir -p $DIR/$tdir
6027         chown $RUNAS_ID $DIR/$tdir
6028         local testfile=$DIR/$tdir/$tfile
6029         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6030                 error "setstripe failed"
6031         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6032                 error "getstripe failed"
6033         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6034         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6035
6036         local testfile2=${testfile}2
6037         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6038                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6039
6040         $RUNAS $MCREATE $testfile2
6041         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6042         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6043         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6044         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6045         [ $stripe_count -eq $STRIPECOUNT ] ||
6046                 error "stripe count $stripe_count != $STRIPECOUNT"
6047 }
6048 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6049
6050 compare_stripe_info1() {
6051         local stripe_index_all_zero=true
6052
6053         for num in 1 2 3 4; do
6054                 for count in $(seq 1 $STRIPE_COUNT); do
6055                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6056                                 local size=$((STRIPE_SIZE * num))
6057                                 local file=file"$num-$offset-$count"
6058                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6059                                 [ $stripe_size -ne $size ] &&
6060                                     error "$file: size $stripe_size != $size"
6061                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6062                                 # allow fewer stripes to be created, ORI-601
6063                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
6064                                     error "$file: count $stripe_count != $count"
6065                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6066                                 [ $stripe_index -ne 0 ] &&
6067                                         stripe_index_all_zero=false
6068                         done
6069                 done
6070         done
6071         $stripe_index_all_zero &&
6072                 error "all files are being extracted starting from OST index 0"
6073         return 0
6074 }
6075
6076 find_lustre_tar() {
6077         [ -n "$(which tar 2>/dev/null)" ] &&
6078                 strings $(which tar) | grep -q "lustre" && echo tar
6079 }
6080
6081 test_102d() {
6082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6083         # b10930: tar test for trusted.lov xattr
6084         TAR=$(find_lustre_tar)
6085         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6086         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6087         setup_test102
6088         test_mkdir -p $DIR/d102d
6089         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6090         cd $DIR/d102d/$tdir
6091         compare_stripe_info1
6092 }
6093 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6094
6095 test_102f() {
6096         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6097         # b10930: tar test for trusted.lov xattr
6098         TAR=$(find_lustre_tar)
6099         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6100         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6101         setup_test102
6102         test_mkdir -p $DIR/d102f
6103         cd $DIR
6104         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6105         cd $DIR/d102f/$tdir
6106         compare_stripe_info1
6107 }
6108 run_test 102f "tar copy files, not keep osts ==========="
6109
6110 grow_xattr() {
6111         local xsize=${1:-1024}  # in bytes
6112         local file=$DIR/$tfile
6113
6114         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6115                 skip "must have user_xattr" && return 0
6116         [ -z "$(which setfattr 2>/dev/null)" ] &&
6117                 skip_env "could not find setfattr" && return 0
6118         [ -z "$(which getfattr 2>/dev/null)" ] &&
6119                 skip_env "could not find getfattr" && return 0
6120
6121         touch $file
6122
6123         local value="$(generate_string $xsize)"
6124
6125         local xbig=trusted.big
6126         log "save $xbig on $file"
6127         setfattr -n $xbig -v $value $file ||
6128                 error "saving $xbig on $file failed"
6129
6130         local orig=$(get_xattr_value $xbig $file)
6131         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6132
6133         local xsml=trusted.sml
6134         log "save $xsml on $file"
6135         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6136
6137         local new=$(get_xattr_value $xbig $file)
6138         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6139
6140         log "grow $xsml on $file"
6141         setfattr -n $xsml -v "$value" $file ||
6142                 error "growing $xsml on $file failed"
6143
6144         new=$(get_xattr_value $xbig $file)
6145         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6146         log "$xbig still valid after growing $xsml"
6147
6148         rm -f $file
6149 }
6150
6151 test_102h() { # bug 15777
6152         grow_xattr 1024
6153 }
6154 run_test 102h "grow xattr from inside inode to external block"
6155
6156 test_102ha() {
6157         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6158         grow_xattr $(max_xattr_size)
6159 }
6160 run_test 102ha "grow xattr from inside inode to external inode"
6161
6162 test_102i() { # bug 17038
6163         touch $DIR/$tfile
6164         ln -s $DIR/$tfile $DIR/${tfile}link
6165         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6166         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"
6167         rm -f $DIR/$tfile $DIR/${tfile}link
6168 }
6169 run_test 102i "lgetxattr test on symbolic link ============"
6170
6171 test_102j() {
6172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6173         TAR=$(find_lustre_tar)
6174         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6175         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6176         setup_test102 "$RUNAS"
6177         test_mkdir -p $DIR/d102j
6178         chown $RUNAS_ID $DIR/d102j
6179         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6180         cd $DIR/d102j/$tdir
6181         compare_stripe_info1 "$RUNAS"
6182 }
6183 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6184
6185 test_102k() {
6186         touch $DIR/$tfile
6187         # b22187 just check that does not crash for regular file.
6188         setfattr -n trusted.lov $DIR/$tfile
6189         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6190         local test_kdir=$DIR/d102k
6191         test_mkdir $test_kdir
6192         local default_size=`$GETSTRIPE -S $test_kdir`
6193         local default_count=`$GETSTRIPE -c $test_kdir`
6194         local default_offset=`$GETSTRIPE -i $test_kdir`
6195         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
6196                 error 'dir setstripe failed'
6197         setfattr -n trusted.lov $test_kdir
6198         local stripe_size=`$GETSTRIPE -S $test_kdir`
6199         local stripe_count=`$GETSTRIPE -c $test_kdir`
6200         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6201         [ $stripe_size -eq $default_size ] ||
6202                 error "stripe size $stripe_size != $default_size"
6203         [ $stripe_count -eq $default_count ] ||
6204                 error "stripe count $stripe_count != $default_count"
6205         [ $stripe_offset -eq $default_offset ] ||
6206                 error "stripe offset $stripe_offset != $default_offset"
6207         rm -rf $DIR/$tfile $test_kdir
6208 }
6209 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6210
6211 test_102l() {
6212         # LU-532 trusted. xattr is invisible to non-root
6213         local testfile=$DIR/$tfile
6214
6215         touch $testfile
6216
6217         echo "listxattr as user..."
6218         chown $RUNAS_ID $testfile
6219         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6220             grep -q "trusted" &&
6221                 error "$testfile trusted xattrs are user visible"
6222
6223         return 0;
6224 }
6225 run_test 102l "listxattr filter test =================================="
6226
6227 cleanup_test102
6228
6229 run_acl_subtest()
6230 {
6231     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6232     return $?
6233 }
6234
6235 test_103 () {
6236     [ "$UID" != 0 ] && skip_env "must run as root" && return
6237     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6238     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
6239     $GSS && skip "could not run under gss" && return
6240
6241     declare -a identity_old
6242
6243         for num in $(seq $MDSCOUNT); do
6244                 switch_identity $num true || identity_old[$num]=$?
6245         done
6246
6247     SAVE_UMASK=`umask`
6248     umask 0022
6249     cd $DIR
6250
6251     echo "performing cp ..."
6252     run_acl_subtest cp || error
6253     echo "performing getfacl-noacl..."
6254     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6255     echo "performing misc..."
6256     run_acl_subtest misc || error  "misc test failed"
6257     echo "performing permissions..."
6258     run_acl_subtest permissions || error "permissions failed"
6259     echo "performing setfacl..."
6260     run_acl_subtest setfacl || error  "setfacl test failed"
6261
6262     # inheritance test got from HP
6263     echo "performing inheritance..."
6264     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6265     chmod +x make-tree || error "chmod +x failed"
6266     run_acl_subtest inheritance || error "inheritance test failed"
6267     rm -f make-tree
6268
6269         echo "LU-974 ignore umask when acl is enabled..."
6270         run_acl_subtest 974 || error "LU-974 test failed"
6271         if [ $MDSCOUNT -ge 2 ]; then
6272                 run_acl_subtest 974_remote ||
6273                         error "LU-974 test failed under remote dir"
6274         fi
6275
6276     echo "LU-2561 newly created file is same size as directory..."
6277     run_acl_subtest 2561 || error "LU-2561 test failed"
6278
6279     cd $SAVE_PWD
6280     umask $SAVE_UMASK
6281
6282         for num in $(seq $MDSCOUNT); do
6283                 if [ "${identity_old[$num]}" = 1 ]; then
6284                         switch_identity $num false || identity_old[$num]=$?
6285                 fi
6286         done
6287 }
6288 run_test 103 "acl test ========================================="
6289
6290 test_104a() {
6291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6292         touch $DIR/$tfile
6293         lfs df || error "lfs df failed"
6294         lfs df -ih || error "lfs df -ih failed"
6295         lfs df -h $DIR || error "lfs df -h $DIR failed"
6296         lfs df -i $DIR || error "lfs df -i $DIR failed"
6297         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6298         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6299
6300         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6301         lctl --device %$OSC deactivate
6302         lfs df || error "lfs df with deactivated OSC failed"
6303         lctl --device %$OSC activate
6304         # wait the osc back to normal
6305         wait_osc_import_state client ost FULL
6306
6307         lfs df || error "lfs df with reactivated OSC failed"
6308         rm -f $DIR/$tfile
6309 }
6310 run_test 104a "lfs df [-ih] [path] test ========================="
6311
6312 test_104b() {
6313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6314         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6315         chmod 666 /dev/obd
6316         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6317         if [ $denied_cnt -ne 0 ];
6318         then
6319                     error "lfs check servers test failed"
6320         fi
6321 }
6322 run_test 104b "$RUNAS lfs check servers test ===================="
6323
6324 test_105a() {
6325         # doesn't work on 2.4 kernels
6326         touch $DIR/$tfile
6327         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6328                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6329         else
6330                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6331         fi
6332         rm -f $DIR/$tfile
6333 }
6334 run_test 105a "flock when mounted without -o flock test ========"
6335
6336 test_105b() {
6337         touch $DIR/$tfile
6338         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6339                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6340         else
6341                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6342         fi
6343         rm -f $DIR/$tfile
6344 }
6345 run_test 105b "fcntl when mounted without -o flock test ========"
6346
6347 test_105c() {
6348         touch $DIR/$tfile
6349         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6350                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6351         else
6352                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6353         fi
6354         rm -f $DIR/$tfile
6355 }
6356 run_test 105c "lockf when mounted without -o flock test ========"
6357
6358 test_105d() { # bug 15924
6359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6360         test_mkdir -p $DIR/$tdir
6361         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6362                 skip "mount w/o flock enabled" && return
6363         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6364         $LCTL set_param fail_loc=0x80000315
6365         flocks_test 2 $DIR/$tdir
6366 }
6367 run_test 105d "flock race (should not freeze) ========"
6368
6369 test_105e() { # bug 22660 && 22040
6370         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6371                 skip "mount w/o flock enabled" && return
6372         touch $DIR/$tfile
6373         flocks_test 3 $DIR/$tfile
6374 }
6375 run_test 105e "Two conflicting flocks from same process ======="
6376
6377 test_106() { #bug 10921
6378         test_mkdir -p $DIR/$tdir
6379         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6380         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6381 }
6382 run_test 106 "attempt exec of dir followed by chown of that dir"
6383
6384 test_107() {
6385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6386         CDIR=`pwd`
6387         cd $DIR
6388
6389         local file=core
6390         rm -f $file
6391
6392         local save_pattern=$(sysctl -n kernel.core_pattern)
6393         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6394         sysctl -w kernel.core_pattern=$file
6395         sysctl -w kernel.core_uses_pid=0
6396
6397         ulimit -c unlimited
6398         sleep 60 &
6399         SLEEPPID=$!
6400
6401         sleep 1
6402
6403         kill -s 11 $SLEEPPID
6404         wait $SLEEPPID
6405         if [ -e $file ]; then
6406                 size=`stat -c%s $file`
6407                 [ $size -eq 0 ] && error "Fail to create core file $file"
6408         else
6409                 error "Fail to create core file $file"
6410         fi
6411         rm -f $file
6412         sysctl -w kernel.core_pattern=$save_pattern
6413         sysctl -w kernel.core_uses_pid=$save_uses_pid
6414         cd $CDIR
6415 }
6416 run_test 107 "Coredump on SIG"
6417
6418 test_110() {
6419         test_mkdir -p $DIR/$tdir
6420         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6421                 error "mkdir with 255 char failed"
6422         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6423                 error "mkdir with 256 char should fail, but did not"
6424         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6425                 error "create with 255 char failed"
6426         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6427                 error "create with 256 char should fail, but did not"
6428
6429         ls -l $DIR/$tdir
6430         rm -rf $DIR/$tdir
6431 }
6432 run_test 110 "filename length checking"
6433
6434 test_115() {
6435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6436         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6437             cut -c11-20)
6438         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6439             return
6440         echo "Starting with $OSTIO_pre threads"
6441
6442         NUMTEST=20000
6443         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6444         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6445         echo "$NUMTEST creates/unlinks"
6446         test_mkdir -p $DIR/$tdir
6447         createmany -o $DIR/$tdir/$tfile $NUMTEST
6448         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6449
6450         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6451             cut -c11-20)
6452
6453         # don't return an error
6454         [ $OSTIO_post == $OSTIO_pre ] && echo \
6455             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6456             echo "This may be fine, depending on what ran before this test" &&
6457             echo "and how fast this system is." && return
6458
6459         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6460 }
6461 run_test 115 "verify dynamic thread creation===================="
6462
6463 free_min_max () {
6464         wait_delete_completed
6465         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6466         echo OST kbytes available: ${AVAIL[@]}
6467         MAXI=0; MAXV=${AVAIL[0]}
6468         MINI=0; MINV=${AVAIL[0]}
6469         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6470             #echo OST $i: ${AVAIL[i]}kb
6471             if [ ${AVAIL[i]} -gt $MAXV ]; then
6472                 MAXV=${AVAIL[i]}; MAXI=$i
6473             fi
6474             if [ ${AVAIL[i]} -lt $MINV ]; then
6475                 MINV=${AVAIL[i]}; MINI=$i
6476             fi
6477         done
6478         echo Min free space: OST $MINI: $MINV
6479         echo Max free space: OST $MAXI: $MAXV
6480 }
6481
6482 test_116a() { # was previously test_116()
6483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6484         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6485
6486         echo -n "Free space priority "
6487         lctl get_param -n lov.*-clilov-*.qos_prio_free
6488         declare -a AVAIL
6489         free_min_max
6490         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6491                 return
6492
6493         # generate uneven OSTs
6494         test_mkdir -p $DIR/$tdir/OST${MINI}
6495         declare -i FILL
6496         FILL=$(($MINV / 4))
6497         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6498         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6499         i=0
6500         while [ $FILL -gt 0 ]; do
6501             i=$(($i + 1))
6502             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6503             FILL=$(($FILL - 2048))
6504             echo -n .
6505         done
6506         FILL=$(($MINV / 4))
6507         sync
6508         sleep_maxage
6509
6510         free_min_max
6511         DIFF=$(($MAXV - $MINV))
6512         DIFF2=$(($DIFF * 100 / $MINV))
6513         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6514         if [ $DIFF2 -gt 20 ]; then
6515             echo "ok"
6516         else
6517             echo "failed - QOS mode won't be used"
6518             error_ignore "QOS imbalance criteria not met"
6519             return
6520         fi
6521
6522         MINI1=$MINI; MINV1=$MINV
6523         MAXI1=$MAXI; MAXV1=$MAXV
6524
6525         # now fill using QOS
6526         echo writing a bunch of files to QOS-assigned OSTs
6527         $SETSTRIPE -c 1 $DIR/$tdir
6528         i=0
6529         while [ $FILL -gt 0 ]; do
6530             i=$(($i + 1))
6531             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6532             FILL=$(($FILL - 200))
6533             echo -n .
6534         done
6535         echo "wrote $i 200k files"
6536         sync
6537         sleep_maxage
6538
6539         echo "Note: free space may not be updated, so measurements might be off"
6540         free_min_max
6541         DIFF2=$(($MAXV - $MINV))
6542         echo "free space delta: orig $DIFF final $DIFF2"
6543         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6544         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6545         echo "Wrote $DIFF to smaller OST $MINI1"
6546         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6547         echo "Wrote $DIFF2 to larger OST $MAXI1"
6548         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6549
6550         # Figure out which files were written where
6551         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6552                awk '/'$MINI1': / {print $2; exit}')
6553         echo $UUID
6554         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6555         echo "$MINC files created on smaller OST $MINI1"
6556         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6557                awk '/'$MAXI1': / {print $2; exit}')
6558         echo $UUID
6559         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6560         echo "$MAXC files created on larger OST $MAXI1"
6561         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6562         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6563
6564         rm -rf $DIR/$tdir
6565 }
6566 run_test 116a "stripe QOS: free space balance ==================="
6567
6568 test_116b() { # LU-2093
6569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6570 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6571         local old_rr
6572         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6573         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6574         mkdir -p $DIR/$tdir
6575         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6576         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6577         do_facet $SINGLEMDS lctl set_param fail_loc=0
6578         rm -rf $DIR/$tdir
6579         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6580 }
6581 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6582
6583 test_117() # bug 10891
6584 {
6585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6586         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6587         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6588         lctl set_param fail_loc=0x21e
6589         > $DIR/$tfile || error "truncate failed"
6590         lctl set_param fail_loc=0
6591         echo "Truncate succeeded."
6592         rm -f $DIR/$tfile
6593 }
6594 run_test 117 "verify fsfilt_extend =========="
6595
6596 NO_SLOW_RESENDCOUNT=4
6597 export OLD_RESENDCOUNT=""
6598 set_resend_count () {
6599         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6600         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6601         lctl set_param -n $PROC_RESENDCOUNT $1
6602         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6603 }
6604
6605 # for reduce test_118* time (b=14842)
6606 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6607
6608 # Reset async IO behavior after error case
6609 reset_async() {
6610         FILE=$DIR/reset_async
6611
6612         # Ensure all OSCs are cleared
6613         $SETSTRIPE -c -1 $FILE
6614         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6615         sync
6616         rm $FILE
6617 }
6618
6619 test_118a() #bug 11710
6620 {
6621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6622         reset_async
6623
6624         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6625         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6626         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6627
6628         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6629                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6630                 return 1;
6631         fi
6632         rm -f $DIR/$tfile
6633 }
6634 run_test 118a "verify O_SYNC works =========="
6635
6636 test_118b()
6637 {
6638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6639         remote_ost_nodsh && skip "remote OST with nodsh" && return
6640
6641         reset_async
6642
6643         #define OBD_FAIL_OST_ENOENT 0x217
6644         set_nodes_failloc "$(osts_nodes)" 0x217
6645         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6646         RC=$?
6647         set_nodes_failloc "$(osts_nodes)" 0
6648         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6649         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6650                     grep -c writeback)
6651
6652         if [[ $RC -eq 0 ]]; then
6653                 error "Must return error due to dropped pages, rc=$RC"
6654                 return 1;
6655         fi
6656
6657         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6658                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6659                 return 1;
6660         fi
6661
6662         echo "Dirty pages not leaked on ENOENT"
6663
6664         # Due to the above error the OSC will issue all RPCs syncronously
6665         # until a subsequent RPC completes successfully without error.
6666         $MULTIOP $DIR/$tfile Ow4096yc
6667         rm -f $DIR/$tfile
6668
6669         return 0
6670 }
6671 run_test 118b "Reclaim dirty pages on fatal error =========="
6672
6673 test_118c()
6674 {
6675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6676
6677         # for 118c, restore the original resend count, LU-1940
6678         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
6679                                 set_resend_count $OLD_RESENDCOUNT
6680         remote_ost_nodsh && skip "remote OST with nodsh" && return
6681
6682         reset_async
6683
6684         #define OBD_FAIL_OST_EROFS               0x216
6685         set_nodes_failloc "$(osts_nodes)" 0x216
6686
6687         # multiop should block due to fsync until pages are written
6688         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6689         MULTIPID=$!
6690         sleep 1
6691
6692         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6693                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6694         fi
6695
6696         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6697                     grep -c writeback)
6698         if [[ $WRITEBACK -eq 0 ]]; then
6699                 error "No page in writeback, writeback=$WRITEBACK"
6700         fi
6701
6702         set_nodes_failloc "$(osts_nodes)" 0
6703         wait $MULTIPID
6704         RC=$?
6705         if [[ $RC -ne 0 ]]; then
6706                 error "Multiop fsync failed, rc=$RC"
6707         fi
6708
6709         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6710         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6711                     grep -c writeback)
6712         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6713                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6714         fi
6715
6716         rm -f $DIR/$tfile
6717         echo "Dirty pages flushed via fsync on EROFS"
6718         return 0
6719 }
6720 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6721
6722 # continue to use small resend count to reduce test_118* time (b=14842)
6723 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6724
6725 test_118d()
6726 {
6727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6728         remote_ost_nodsh && skip "remote OST with nodsh" && return
6729
6730         reset_async
6731
6732         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6733         set_nodes_failloc "$(osts_nodes)" 0x214
6734         # multiop should block due to fsync until pages are written
6735         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6736         MULTIPID=$!
6737         sleep 1
6738
6739         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6740                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6741         fi
6742
6743         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6744                     grep -c writeback)
6745         if [[ $WRITEBACK -eq 0 ]]; then
6746                 error "No page in writeback, writeback=$WRITEBACK"
6747         fi
6748
6749         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6750         set_nodes_failloc "$(osts_nodes)" 0
6751
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 [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6756                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6757         fi
6758
6759         rm -f $DIR/$tfile
6760         echo "Dirty pages gaurenteed flushed via fsync"
6761         return 0
6762 }
6763 run_test 118d "Fsync validation inject a delay of the bulk =========="
6764
6765 test_118f() {
6766         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6767         reset_async
6768
6769         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6770         lctl set_param fail_loc=0x8000040a
6771
6772         # Should simulate EINVAL error which is fatal
6773         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6774         RC=$?
6775         if [[ $RC -eq 0 ]]; then
6776                 error "Must return error due to dropped pages, rc=$RC"
6777         fi
6778
6779         lctl set_param fail_loc=0x0
6780
6781         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6782         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6783         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6784                     grep -c writeback)
6785         if [[ $LOCKED -ne 0 ]]; then
6786                 error "Locked pages remain in cache, locked=$LOCKED"
6787         fi
6788
6789         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6790                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6791         fi
6792
6793         rm -f $DIR/$tfile
6794         echo "No pages locked after fsync"
6795
6796         reset_async
6797         return 0
6798 }
6799 run_test 118f "Simulate unrecoverable OSC side error =========="
6800
6801 test_118g() {
6802         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6803         reset_async
6804
6805         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6806         lctl set_param fail_loc=0x406
6807
6808         # simulate local -ENOMEM
6809         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6810         RC=$?
6811
6812         lctl set_param fail_loc=0
6813         if [[ $RC -eq 0 ]]; then
6814                 error "Must return error due to dropped pages, rc=$RC"
6815         fi
6816
6817         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6818         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6819         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6820                         grep -c writeback)
6821         if [[ $LOCKED -ne 0 ]]; then
6822                 error "Locked pages remain in cache, locked=$LOCKED"
6823         fi
6824
6825         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6826                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6827         fi
6828
6829         rm -f $DIR/$tfile
6830         echo "No pages locked after fsync"
6831
6832         reset_async
6833         return 0
6834 }
6835 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6836
6837 test_118h() {
6838         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6839         remote_ost_nodsh && skip "remote OST with nodsh" && return
6840
6841         reset_async
6842
6843         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6844         set_nodes_failloc "$(osts_nodes)" 0x20e
6845         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6846         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6847         RC=$?
6848
6849         set_nodes_failloc "$(osts_nodes)" 0
6850         if [[ $RC -eq 0 ]]; then
6851                 error "Must return error due to dropped pages, rc=$RC"
6852         fi
6853
6854         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6855         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6856         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6857                     grep -c writeback)
6858         if [[ $LOCKED -ne 0 ]]; then
6859                 error "Locked pages remain in cache, locked=$LOCKED"
6860         fi
6861
6862         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6863                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6864         fi
6865
6866         rm -f $DIR/$tfile
6867         echo "No pages locked after fsync"
6868
6869         return 0
6870 }
6871 run_test 118h "Verify timeout in handling recoverables errors  =========="
6872
6873 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6874
6875 test_118i() {
6876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6877         remote_ost_nodsh && skip "remote OST with nodsh" && return
6878
6879         reset_async
6880
6881         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6882         set_nodes_failloc "$(osts_nodes)" 0x20e
6883
6884         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6885         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6886         PID=$!
6887         sleep 5
6888         set_nodes_failloc "$(osts_nodes)" 0
6889
6890         wait $PID
6891         RC=$?
6892         if [[ $RC -ne 0 ]]; then
6893                 error "got error, but should be not, rc=$RC"
6894         fi
6895
6896         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6897         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6898         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6899         if [[ $LOCKED -ne 0 ]]; then
6900                 error "Locked pages remain in cache, locked=$LOCKED"
6901         fi
6902
6903         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6904                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6905         fi
6906
6907         rm -f $DIR/$tfile
6908         echo "No pages locked after fsync"
6909
6910         return 0
6911 }
6912 run_test 118i "Fix error before timeout in recoverable error  =========="
6913
6914 [ "$SLOW" = "no" ] && set_resend_count 4
6915
6916 test_118j() {
6917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6918         remote_ost_nodsh && skip "remote OST with nodsh" && return
6919
6920         reset_async
6921
6922         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6923         set_nodes_failloc "$(osts_nodes)" 0x220
6924
6925         # return -EIO from OST
6926         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6927         RC=$?
6928         set_nodes_failloc "$(osts_nodes)" 0x0
6929         if [[ $RC -eq 0 ]]; then
6930                 error "Must return error due to dropped pages, rc=$RC"
6931         fi
6932
6933         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6934         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6935         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6936         if [[ $LOCKED -ne 0 ]]; then
6937                 error "Locked pages remain in cache, locked=$LOCKED"
6938         fi
6939
6940         # in recoverable error on OST we want resend and stay until it finished
6941         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6942                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6943         fi
6944
6945         rm -f $DIR/$tfile
6946         echo "No pages locked after fsync"
6947
6948         return 0
6949 }
6950 run_test 118j "Simulate unrecoverable OST side error =========="
6951
6952 test_118k()
6953 {
6954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6955         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6956
6957         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6958         set_nodes_failloc "$(osts_nodes)" 0x20e
6959         test_mkdir -p $DIR/$tdir
6960
6961         for ((i=0;i<10;i++)); do
6962                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6963                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6964                 SLEEPPID=$!
6965                 sleep 0.500s
6966                 kill $SLEEPPID
6967                 wait $SLEEPPID
6968         done
6969
6970         set_nodes_failloc "$(osts_nodes)" 0
6971         rm -rf $DIR/$tdir
6972 }
6973 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6974
6975 test_118l()
6976 {
6977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6978         # LU-646
6979         test_mkdir -p $DIR/$tdir
6980         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6981         rm -rf $DIR/$tdir
6982 }
6983 run_test 118l "fsync dir ========="
6984
6985 test_118m() # LU-3066
6986 {
6987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6988         test_mkdir -p $DIR/$tdir
6989         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
6990         rm -rf $DIR/$tdir
6991 }
6992 run_test 118m "fdatasync dir ========="
6993
6994 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6995
6996 test_119a() # bug 11737
6997 {
6998         BSIZE=$((512 * 1024))
6999         directio write $DIR/$tfile 0 1 $BSIZE
7000         # We ask to read two blocks, which is more than a file size.
7001         # directio will indicate an error when requested and actual
7002         # sizes aren't equeal (a normal situation in this case) and
7003         # print actual read amount.
7004         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7005         if [ "$NOB" != "$BSIZE" ]; then
7006                 error "read $NOB bytes instead of $BSIZE"
7007         fi
7008         rm -f $DIR/$tfile
7009 }
7010 run_test 119a "Short directIO read must return actual read amount"
7011
7012 test_119b() # bug 11737
7013 {
7014         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7015
7016         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7017         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7018         sync
7019         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7020                 error "direct read failed"
7021         rm -f $DIR/$tfile
7022 }
7023 run_test 119b "Sparse directIO read must return actual read amount"
7024
7025 test_119c() # bug 13099
7026 {
7027         BSIZE=1048576
7028         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7029         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7030         rm -f $DIR/$tfile
7031 }
7032 run_test 119c "Testing for direct read hitting hole"
7033
7034 test_119d() # bug 15950
7035 {
7036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7037         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7038         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7039         BSIZE=1048576
7040         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7041         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7042         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7043         lctl set_param fail_loc=0x40d
7044         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7045         pid_dio=$!
7046         sleep 1
7047         cat $DIR/$tfile > /dev/null &
7048         lctl set_param fail_loc=0
7049         pid_reads=$!
7050         wait $pid_dio
7051         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7052         sleep 2
7053         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7054         error "the read rpcs have not completed in 2s"
7055         rm -f $DIR/$tfile
7056         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7057 }
7058 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7059
7060 test_120a() {
7061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7062         test_mkdir -p $DIR/$tdir
7063         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7064                skip "no early lock cancel on server" && return 0
7065         lru_resize_disable mdc
7066         lru_resize_disable osc
7067         cancel_lru_locks mdc
7068         stat $DIR/$tdir > /dev/null
7069         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7070         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7071         test_mkdir $DIR/$tdir/d1
7072         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7073         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7074         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7075         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7076         lru_resize_enable mdc
7077         lru_resize_enable osc
7078 }
7079 run_test 120a "Early Lock Cancel: mkdir test"
7080
7081 test_120b() {
7082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7083         test_mkdir -p $DIR/$tdir
7084         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7085                skip "no early lock cancel on server" && return 0
7086         lru_resize_disable mdc
7087         lru_resize_disable osc
7088         cancel_lru_locks mdc
7089         stat $DIR/$tdir > /dev/null
7090         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7091         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7092         touch $DIR/$tdir/f1
7093         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7094         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7095         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7096         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7097         lru_resize_enable mdc
7098         lru_resize_enable osc
7099 }
7100 run_test 120b "Early Lock Cancel: create test"
7101
7102 test_120c() {
7103         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7104         test_mkdir -p $DIR/$tdir
7105         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7106                skip "no early lock cancel on server" && return 0
7107         lru_resize_disable mdc
7108         lru_resize_disable osc
7109         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7110         touch $DIR/$tdir/d1/f1
7111         cancel_lru_locks mdc
7112         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7113         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7114         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7115         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7116         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7117         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7118         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7119         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7120         lru_resize_enable mdc
7121         lru_resize_enable osc
7122 }
7123 run_test 120c "Early Lock Cancel: link test"
7124
7125 test_120d() {
7126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7127         test_mkdir -p $DIR/$tdir
7128         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7129                skip "no early lock cancel on server" && return 0
7130         lru_resize_disable mdc
7131         lru_resize_disable osc
7132         touch $DIR/$tdir
7133         cancel_lru_locks mdc
7134         stat $DIR/$tdir > /dev/null
7135         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7136         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7137         chmod a+x $DIR/$tdir
7138         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7139         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7140         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7141         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7142         lru_resize_enable mdc
7143         lru_resize_enable osc
7144 }
7145 run_test 120d "Early Lock Cancel: setattr test"
7146
7147 test_120e() {
7148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7149         test_mkdir -p $DIR/$tdir
7150         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7151                skip "no early lock cancel on server" && return 0
7152         lru_resize_disable mdc
7153         lru_resize_disable osc
7154         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7155         cancel_lru_locks mdc
7156         cancel_lru_locks osc
7157         dd if=$DIR/$tdir/f1 of=/dev/null
7158         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7159         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7160               awk '/ldlm_cancel/ {print $2}'`
7161         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7162               awk '/ldlm_bl_callback/ {print $2}'`
7163         unlink $DIR/$tdir/f1
7164         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7165               awk '/ldlm_cancel/ {print $2}'`
7166         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7167               awk '/ldlm_bl_callback/ {print $2}'`
7168         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7169         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7170         lru_resize_enable mdc
7171         lru_resize_enable osc
7172 }
7173 run_test 120e "Early Lock Cancel: unlink test"
7174
7175 test_120f() {
7176         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7177         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7178                skip "no early lock cancel on server" && return 0
7179         test_mkdir -p $DIR/$tdir
7180         lru_resize_disable mdc
7181         lru_resize_disable osc
7182         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7183         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7184         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7185         cancel_lru_locks mdc
7186         cancel_lru_locks osc
7187         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7188         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7189         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7190         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7191               awk '/ldlm_cancel/ {print $2}'`
7192         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7193               awk '/ldlm_bl_callback/ {print $2}'`
7194         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
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         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7200         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7201         lru_resize_enable mdc
7202         lru_resize_enable osc
7203 }
7204 run_test 120f "Early Lock Cancel: rename test"
7205
7206 test_120g() {
7207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7208         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7209                skip "no early lock cancel on server" && return 0
7210         lru_resize_disable mdc
7211         lru_resize_disable osc
7212         count=10000
7213         echo create $count files
7214         test_mkdir -p $DIR/$tdir
7215         cancel_lru_locks mdc
7216         cancel_lru_locks osc
7217         t0=`date +%s`
7218
7219         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7220               awk '/ldlm_cancel/ {print $2}'`
7221         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7222               awk '/ldlm_bl_callback/ {print $2}'`
7223         createmany -o $DIR/$tdir/f $count
7224         sync
7225         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7226               awk '/ldlm_cancel/ {print $2}'`
7227         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7228               awk '/ldlm_bl_callback/ {print $2}'`
7229         t1=`date +%s`
7230         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7231         echo rm $count files
7232         rm -r $DIR/$tdir
7233         sync
7234         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7235               awk '/ldlm_cancel/ {print $2}'`
7236         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7237               awk '/ldlm_bl_callback/ {print $2}'`
7238         t2=`date +%s`
7239         echo total: $count removes in $((t2-t1))
7240         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7241         sleep 2
7242         # wait for commitment of removal
7243         lru_resize_enable mdc
7244         lru_resize_enable osc
7245 }
7246 run_test 120g "Early Lock Cancel: performance test"
7247
7248 test_121() { #bug #10589
7249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7250         rm -rf $DIR/$tfile
7251         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7252 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7253         lctl set_param fail_loc=0x310
7254         cancel_lru_locks osc > /dev/null
7255         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7256         lctl set_param fail_loc=0
7257         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7258 }
7259 run_test 121 "read cancel race ========="
7260
7261 test_123a() { # was test 123, statahead(bug 11401)
7262         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7263         SLOWOK=0
7264         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7265             log "testing on UP system. Performance may be not as good as expected."
7266                         SLOWOK=1
7267         fi
7268
7269         rm -rf $DIR/$tdir
7270         test_mkdir -p $DIR/$tdir
7271         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7272         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7273         MULT=10
7274         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7275                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7276
7277                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7278                 lctl set_param -n llite.*.statahead_max 0
7279                 lctl get_param llite.*.statahead_max
7280                 cancel_lru_locks mdc
7281                 cancel_lru_locks osc
7282                 stime=`date +%s`
7283                 time ls -l $DIR/$tdir | wc -l
7284                 etime=`date +%s`
7285                 delta=$((etime - stime))
7286                 log "ls $i files without statahead: $delta sec"
7287                 lctl set_param llite.*.statahead_max=$max
7288
7289                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7290                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7291                 cancel_lru_locks mdc
7292                 cancel_lru_locks osc
7293                 stime=`date +%s`
7294                 time ls -l $DIR/$tdir | wc -l
7295                 etime=`date +%s`
7296                 delta_sa=$((etime - stime))
7297                 log "ls $i files with statahead: $delta_sa sec"
7298                 lctl get_param -n llite.*.statahead_stats
7299                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7300
7301                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7302                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7303
7304                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7305                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7306                     lctl set_param -n llite.*.statahead_max 0
7307                     lctl get_param llite.*.statahead_max
7308                     cancel_lru_locks mdc
7309                     cancel_lru_locks osc
7310                     stime=`date +%s`
7311                     time ls -l $DIR/$tdir | wc -l
7312                     etime=`date +%s`
7313                     delta=$((etime - stime))
7314                     log "ls $i files again without statahead: $delta sec"
7315                     lctl set_param llite.*.statahead_max=$max
7316                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7317                         if [  $SLOWOK -eq 0 ]; then
7318                                 error "ls $i files is slower with statahead!"
7319                         else
7320                                 log "ls $i files is slower with statahead!"
7321                         fi
7322                         break
7323                     fi
7324                 fi
7325
7326                 [ $delta -gt 20 ] && break
7327                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7328                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7329         done
7330         log "ls done"
7331
7332         stime=`date +%s`
7333         rm -r $DIR/$tdir
7334         sync
7335         etime=`date +%s`
7336         delta=$((etime - stime))
7337         log "rm -r $DIR/$tdir/: $delta seconds"
7338         log "rm done"
7339         lctl get_param -n llite.*.statahead_stats
7340 }
7341 run_test 123a "verify statahead work"
7342
7343 test_123b () { # statahead(bug 15027)
7344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7345         test_mkdir -p $DIR/$tdir
7346         createmany -o $DIR/$tdir/$tfile-%d 1000
7347
7348         cancel_lru_locks mdc
7349         cancel_lru_locks osc
7350
7351 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7352         lctl set_param fail_loc=0x80000803
7353         ls -lR $DIR/$tdir > /dev/null
7354         log "ls done"
7355         lctl set_param fail_loc=0x0
7356         lctl get_param -n llite.*.statahead_stats
7357         rm -r $DIR/$tdir
7358         sync
7359
7360 }
7361 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7362
7363 test_124a() {
7364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7365         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7366                skip "no lru resize on server" && return 0
7367         local NR=2000
7368         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7369
7370         log "create $NR files at $DIR/$tdir"
7371         createmany -o $DIR/$tdir/f $NR ||
7372                 error "failed to create $NR files in $DIR/$tdir"
7373
7374         cancel_lru_locks mdc
7375         ls -l $DIR/$tdir > /dev/null
7376
7377         local NSDIR=""
7378         local LRU_SIZE=0
7379         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7380                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7381                 LRU_SIZE=$(lctl get_param -n $PARAM)
7382                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7383                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7384                                                 log "NSDIR=$NSDIR"
7385                         log "NS=$(basename $NSDIR)"
7386                         break
7387                 fi
7388         done
7389
7390         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7391                 skip "Not enough cached locks created!"
7392                 return 0
7393         fi
7394         log "LRU=$LRU_SIZE"
7395
7396         local SLEEP=30
7397
7398         # We know that lru resize allows one client to hold $LIMIT locks
7399         # for 10h. After that locks begin to be killed by client.
7400         local MAX_HRS=10
7401         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7402                 log "LIMIT=$LIMIT"
7403
7404         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7405         # killing locks. Some time was spent for creating locks. This means
7406         # that up to the moment of sleep finish we must have killed some of
7407         # them (10-100 locks). This depends on how fast ther were created.
7408         # Many of them were touched in almost the same moment and thus will
7409         # be killed in groups.
7410         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7411
7412         # Use $LRU_SIZE_B here to take into account real number of locks
7413         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7414         local LRU_SIZE_B=$LRU_SIZE
7415         log "LVF=$LVF"
7416         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7417                 log "OLD_LVF=$OLD_LVF"
7418         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7419
7420         # Let's make sure that we really have some margin. Client checks
7421         # cached locks every 10 sec.
7422         SLEEP=$((SLEEP+20))
7423         log "Sleep ${SLEEP} sec"
7424         local SEC=0
7425         while ((SEC<$SLEEP)); do
7426                 echo -n "..."
7427                 sleep 5
7428                 SEC=$((SEC+5))
7429                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7430                 echo -n "$LRU_SIZE"
7431         done
7432         echo ""
7433         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7434         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7435
7436         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7437                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7438                 unlinkmany $DIR/$tdir/f $NR
7439                 return
7440         }
7441
7442         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7443         log "unlink $NR files at $DIR/$tdir"
7444         unlinkmany $DIR/$tdir/f $NR
7445 }
7446 run_test 124a "lru resize ======================================="
7447
7448 get_max_pool_limit()
7449 {
7450         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7451         local max=0
7452         for l in $limit; do
7453                 if test $l -gt $max; then
7454                         max=$l
7455                 fi
7456         done
7457         echo $max
7458 }
7459
7460 test_124b() {
7461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7462         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7463                skip "no lru resize on server" && return 0
7464
7465         LIMIT=`get_max_pool_limit`
7466
7467         NR=$(($(default_lru_size)*20))
7468         if [ $NR -gt $LIMIT ]; then
7469                 log "Limit lock number by $LIMIT locks"
7470                 NR=$LIMIT
7471         fi
7472         lru_resize_disable mdc
7473         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7474                 error "failed to create $DIR/$tdir/disable_lru_resize"
7475
7476         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7477         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7478         cancel_lru_locks mdc
7479         stime=`date +%s`
7480         PID=""
7481         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7482         PID="$PID $!"
7483         sleep 2
7484         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7485         PID="$PID $!"
7486         sleep 2
7487         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7488         PID="$PID $!"
7489         wait $PID
7490         etime=`date +%s`
7491         nolruresize_delta=$((etime-stime))
7492         log "ls -la time: $nolruresize_delta seconds"
7493         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7494         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7495
7496         lru_resize_enable mdc
7497         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7498                 error "failed to create $DIR/$tdir/enable_lru_resize"
7499
7500         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7501         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7502         cancel_lru_locks mdc
7503         stime=`date +%s`
7504         PID=""
7505         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7506         PID="$PID $!"
7507         sleep 2
7508         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7509         PID="$PID $!"
7510         sleep 2
7511         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7512         PID="$PID $!"
7513         wait $PID
7514         etime=`date +%s`
7515         lruresize_delta=$((etime-stime))
7516         log "ls -la time: $lruresize_delta seconds"
7517         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7518
7519         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7520                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7521         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7522                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7523         else
7524                 log "lru resize performs the same with no lru resize"
7525         fi
7526         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7527 }
7528 run_test 124b "lru resize (performance test) ======================="
7529
7530 test_125() { # 13358
7531         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7532         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7533         test_mkdir -p $DIR/d125 || error "mkdir failed"
7534         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7535         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7536         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7537 }
7538 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7539
7540 test_126() { # bug 12829/13455
7541         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7542         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7543         $GSS && skip "must run as gss disabled" && return
7544
7545         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7546         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7547         rm -f $DIR/$tfile
7548         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7549 }
7550 run_test 126 "check that the fsgid provided by the client is taken into account"
7551
7552 test_127a() { # bug 15521
7553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7554         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7555         $LCTL set_param osc.*.stats=0
7556         FSIZE=$((2048 * 1024))
7557         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7558         cancel_lru_locks osc
7559         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7560
7561         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7562         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7563                 echo "got $COUNT $NAME"
7564                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7565                 eval $NAME=$COUNT || error "Wrong proc format"
7566
7567                 case $NAME in
7568                         read_bytes|write_bytes)
7569                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7570                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7571                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7572                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7573                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7574                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7575                                 error "sumsquare is too small: $SUMSQ"
7576                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7577                                 error "sumsquare is too big: $SUMSQ"
7578                         ;;
7579                         *) ;;
7580                 esac
7581         done < $DIR/${tfile}.tmp
7582
7583         #check that we actually got some stats
7584         [ "$read_bytes" ] || error "Missing read_bytes stats"
7585         [ "$write_bytes" ] || error "Missing write_bytes stats"
7586         [ "$read_bytes" != 0 ] || error "no read done"
7587         [ "$write_bytes" != 0 ] || error "no write done"
7588 }
7589 run_test 127a "verify the client stats are sane"
7590
7591 test_127b() { # bug LU-333
7592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7593         $LCTL set_param llite.*.stats=0
7594         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7595         # perform 2 reads and writes so MAX is different from SUM.
7596         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7597         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7598         cancel_lru_locks osc
7599         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7600         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7601
7602         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7603         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7604                 echo "got $COUNT $NAME"
7605                 eval $NAME=$COUNT || error "Wrong proc format"
7606
7607         case $NAME in
7608                 read_bytes)
7609                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7610                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7611                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7612                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7613                         ;;
7614                 write_bytes)
7615                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7616                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7617                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7618                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7619                         ;;
7620                         *) ;;
7621                 esac
7622         done < $TMP/${tfile}.tmp
7623
7624         #check that we actually got some stats
7625         [ "$read_bytes" ] || error "Missing read_bytes stats"
7626         [ "$write_bytes" ] || error "Missing write_bytes stats"
7627         [ "$read_bytes" != 0 ] || error "no read done"
7628         [ "$write_bytes" != 0 ] || error "no write done"
7629 }
7630 run_test 127b "verify the llite client stats are sane"
7631
7632 test_128() { # bug 15212
7633         touch $DIR/$tfile
7634         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7635                 find $DIR/$tfile
7636                 find $DIR/$tfile
7637         EOF
7638
7639         result=$(grep error $TMP/$tfile.log)
7640         rm -f $DIR/$tfile
7641         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7642 }
7643 run_test 128 "interactive lfs for 2 consecutive find's"
7644
7645 set_dir_limits () {
7646         local mntdev
7647         local canondev
7648         local node
7649
7650         local LDPROC=/proc/fs/ldiskfs
7651         local facets=$(get_facets MDS)
7652
7653         for facet in ${facets//,/ }; do
7654                 canondev=$(ldiskfs_canon \
7655                            *.$(convert_facet2label $facet).mntdev $facet)
7656                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7657                                                 LDPROC=/sys/fs/ldiskfs
7658                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7659         done
7660 }
7661
7662 test_129() {
7663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7664         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7665                 skip "Only applicable to ldiskfs-based MDTs"
7666                 return
7667         fi
7668         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7669
7670         EFBIG=27
7671         MAX=16384
7672
7673         set_dir_limits $MAX
7674         test_mkdir -p $DIR/$tdir
7675
7676         local I=0
7677         local J=0
7678         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7679                 $MULTIOP $DIR/$tdir/$J Oc
7680                 rc=$?
7681                 if [ $rc -eq $EFBIG ]; then
7682                         set_dir_limits 0
7683                         echo "return code $rc received as expected"
7684                         return 0
7685                 elif [ $rc -ne 0 ]; then
7686                         set_dir_limits 0
7687                         error_exit "return code $rc received instead of expected $EFBIG"
7688                 fi
7689                 J=$((J+1))
7690                 I=$(stat -c%s "$DIR/$tdir")
7691         done
7692
7693         set_dir_limits 0
7694         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7695 }
7696 run_test 129 "test directory size limit ========================"
7697
7698 OLDIFS="$IFS"
7699 cleanup_130() {
7700         trap 0
7701         IFS="$OLDIFS"
7702 }
7703
7704 test_130a() {
7705         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7706         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7707                 return
7708
7709         trap cleanup_130 EXIT RETURN
7710
7711         local fm_file=$DIR/$tfile
7712         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7713         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7714                 error "dd failed for $fm_file"
7715
7716         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7717         filefrag -ves $fm_file
7718         RC=$?
7719         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7720                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7721         [ $RC != 0 ] && error "filefrag $fm_file failed"
7722
7723         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7724                       grep -v "ext:" | grep -v "found")
7725         lun=$($GETSTRIPE -i $fm_file)
7726
7727         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7728         IFS=$'\n'
7729         tot_len=0
7730         for line in $filefrag_op
7731         do
7732                 frag_lun=`echo $line | cut -d: -f5`
7733                 ext_len=`echo $line | cut -d: -f4`
7734                 if (( $frag_lun != $lun )); then
7735                         cleanup_130
7736                         error "FIEMAP on 1-stripe file($fm_file) failed"
7737                         return
7738                 fi
7739                 (( tot_len += ext_len ))
7740         done
7741
7742         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7743                 cleanup_130
7744                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7745                 return
7746         fi
7747
7748         cleanup_130
7749
7750         echo "FIEMAP on single striped file succeeded"
7751 }
7752 run_test 130a "FIEMAP (1-stripe file)"
7753
7754 test_130b() {
7755         [ "$OSTCOUNT" -lt "2" ] &&
7756                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7757
7758         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7759         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7760                 return
7761
7762         trap cleanup_130 EXIT RETURN
7763
7764         local fm_file=$DIR/$tfile
7765         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7766         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7767                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7768
7769         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7770                 error "dd failed on $fm_file"
7771
7772         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7773         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7774                       grep -v "ext:" | grep -v "found")
7775
7776         last_lun=$(echo $filefrag_op | cut -d: -f5)
7777
7778         IFS=$'\n'
7779         tot_len=0
7780         num_luns=1
7781         for line in $filefrag_op
7782         do
7783                 frag_lun=`echo $line | cut -d: -f5`
7784                 ext_len=`echo $line | cut -d: -f4`
7785                 if (( $frag_lun != $last_lun )); then
7786                         if (( tot_len != 1024 )); then
7787                                 cleanup_130
7788                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7789                                 return
7790                         else
7791                                 (( num_luns += 1 ))
7792                                 tot_len=0
7793                         fi
7794                 fi
7795                 (( tot_len += ext_len ))
7796                 last_lun=$frag_lun
7797         done
7798         if (( num_luns != 2 || tot_len != 1024 )); then
7799                 cleanup_130
7800                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7801                 return
7802         fi
7803
7804         cleanup_130
7805
7806         echo "FIEMAP on 2-stripe file succeeded"
7807 }
7808 run_test 130b "FIEMAP (2-stripe file)"
7809
7810 test_130c() {
7811         [ "$OSTCOUNT" -lt "2" ] &&
7812                 skip_env "skipping FIEMAP on 2-stripe file" && return
7813
7814         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7815         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7816                 return
7817
7818         trap cleanup_130 EXIT RETURN
7819
7820         local fm_file=$DIR/$tfile
7821         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7822         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7823                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7824
7825         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7826
7827         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7828         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7829
7830         last_lun=`echo $filefrag_op | cut -d: -f5`
7831
7832         IFS=$'\n'
7833         tot_len=0
7834         num_luns=1
7835         for line in $filefrag_op
7836         do
7837                 frag_lun=`echo $line | cut -d: -f5`
7838                 ext_len=`echo $line | cut -d: -f4`
7839                 if (( $frag_lun != $last_lun )); then
7840                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7841                         if (( logical != 512 )); then
7842                                 cleanup_130
7843                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7844                                 return
7845                         fi
7846                         if (( tot_len != 512 )); then
7847                                 cleanup_130
7848                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7849                                 return
7850                         else
7851                                 (( num_luns += 1 ))
7852                                 tot_len=0
7853                         fi
7854                 fi
7855                 (( tot_len += ext_len ))
7856                 last_lun=$frag_lun
7857         done
7858         if (( num_luns != 2 || tot_len != 512 )); then
7859                 cleanup_130
7860                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7861                 return
7862         fi
7863
7864         cleanup_130
7865
7866         echo "FIEMAP on 2-stripe file with hole succeeded"
7867 }
7868 run_test 130c "FIEMAP (2-stripe file with hole)"
7869
7870 test_130d() {
7871         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7872
7873         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7874         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7875
7876         trap cleanup_130 EXIT RETURN
7877
7878         local fm_file=$DIR/$tfile
7879         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7880         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7881                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7882         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7883
7884         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7885         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7886
7887         last_lun=`echo $filefrag_op | cut -d: -f5`
7888
7889         IFS=$'\n'
7890         tot_len=0
7891         num_luns=1
7892         for line in $filefrag_op
7893         do
7894                 frag_lun=`echo $line | cut -d: -f5`
7895                 ext_len=`echo $line | cut -d: -f4`
7896                 if (( $frag_lun != $last_lun )); then
7897                         if (( tot_len != 1024 )); then
7898                                 cleanup_130
7899                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7900                                 return
7901                         else
7902                                 (( num_luns += 1 ))
7903                                 tot_len=0
7904                         fi
7905                 fi
7906                 (( tot_len += ext_len ))
7907                 last_lun=$frag_lun
7908         done
7909         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7910                 cleanup_130
7911                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7912                 return
7913         fi
7914
7915         cleanup_130
7916
7917         echo "FIEMAP on N-stripe file succeeded"
7918 }
7919 run_test 130d "FIEMAP (N-stripe file)"
7920
7921 test_130e() {
7922         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7923
7924         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7925         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7926
7927         trap cleanup_130 EXIT RETURN
7928
7929         local fm_file=$DIR/$tfile
7930         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7931         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7932                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7933
7934         NUM_BLKS=512
7935         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7936         for ((i = 0; i < $NUM_BLKS; i++))
7937         do
7938                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7939         done
7940
7941         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7942         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7943
7944         last_lun=`echo $filefrag_op | cut -d: -f5`
7945
7946         IFS=$'\n'
7947         tot_len=0
7948         num_luns=1
7949         for line in $filefrag_op
7950         do
7951                 frag_lun=`echo $line | cut -d: -f5`
7952                 ext_len=`echo $line | cut -d: -f4`
7953                 if (( $frag_lun != $last_lun )); then
7954                         if (( tot_len != $EXPECTED_LEN )); then
7955                                 cleanup_130
7956                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7957                                 return
7958                         else
7959                                 (( num_luns += 1 ))
7960                                 tot_len=0
7961                         fi
7962                 fi
7963                 (( tot_len += ext_len ))
7964                 last_lun=$frag_lun
7965         done
7966         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7967                 cleanup_130
7968                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7969                 return
7970         fi
7971
7972         cleanup_130
7973
7974         echo "FIEMAP with continuation calls succeeded"
7975 }
7976 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7977
7978 # Test for writev/readv
7979 test_131a() {
7980         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7981         error "writev test failed"
7982         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7983         error "readv failed"
7984         rm -f $DIR/$tfile
7985 }
7986 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7987
7988 test_131b() {
7989         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7990         error "append writev test failed"
7991         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7992         error "append writev test failed"
7993         rm -f $DIR/$tfile
7994 }
7995 run_test 131b "test append writev"
7996
7997 test_131c() {
7998         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7999         error "NOT PASS"
8000 }
8001 run_test 131c "test read/write on file w/o objects"
8002
8003 test_131d() {
8004         rwv -f $DIR/$tfile -w -n 1 1572864
8005         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8006         if [ "$NOB" != 1572864 ]; then
8007                 error "Short read filed: read $NOB bytes instead of 1572864"
8008         fi
8009         rm -f $DIR/$tfile
8010 }
8011 run_test 131d "test short read"
8012
8013 test_131e() {
8014         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8015         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8016         error "read hitting hole failed"
8017         rm -f $DIR/$tfile
8018 }
8019 run_test 131e "test read hitting hole"
8020
8021 get_ost_param() {
8022         local token=$1
8023         local gl_sum=0
8024         for node in $(osts_nodes); do
8025                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8026                 [ x$gl = x"" ] && gl=0
8027                 gl_sum=$((gl_sum + gl))
8028         done
8029         echo $gl_sum
8030 }
8031
8032 som_mode_switch() {
8033         local som=$1
8034         local gl1=$2
8035         local gl2=$3
8036
8037         if [ x$som = x"enabled" ]; then
8038                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8039                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8040                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8041         else
8042                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8043                 MOUNTOPT="$MOUNTOPT,som_preview"
8044                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8045         fi
8046
8047         # do remount to make new mount-conf parameters actual
8048         echo remounting...
8049         sync
8050         stopall
8051         setupall
8052 }
8053
8054 test_132() { #1028, SOM
8055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8056         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8057         local num=$(get_mds_dir $DIR)
8058         local mymds=mds${num}
8059         local MOUNTOPT_SAVE=$MOUNTOPT
8060
8061         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8062         cancel_lru_locks osc
8063
8064         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8065
8066         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8067         stat $DIR/$tfile >/dev/null
8068         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8069         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8070         rm $DIR/$tfile
8071         som_mode_switch $som1 $gl1 $gl2
8072
8073         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8074         cancel_lru_locks osc
8075
8076         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8077         if [ $som1 == $som2 ]; then
8078             error "som is still "$som2
8079             if [ x$som2 = x"enabled" ]; then
8080                 som2="disabled"
8081             else
8082                 som2="enabled"
8083             fi
8084         fi
8085
8086         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8087         stat $DIR/$tfile >/dev/null
8088         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8089         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8090         som_mode_switch $som2 $gl1 $gl2
8091         MOUNTOPT=$MOUNTOPT_SAVE
8092 }
8093 run_test 132 "som avoids glimpse rpc"
8094
8095 check_stats() {
8096         local res
8097         local count
8098         case $1 in
8099         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8100                  ;;
8101         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8102                  ;;
8103         *) error "Wrong argument $1" ;;
8104         esac
8105         echo $res
8106         count=`echo $res | awk '{print $2}'`
8107         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8108         # if the argument $3 is zero, it means any stat increment is ok.
8109         if [ $3 -gt 0 ] ; then
8110                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
8111         fi
8112 }
8113
8114 test_133a() {
8115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8116         remote_ost_nodsh && skip "remote OST with nodsh" && return
8117         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8118
8119         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8120                 { skip "MDS doesn't support rename stats"; return; }
8121         local testdir=$DIR/${tdir}/stats_testdir
8122         mkdir -p $DIR/${tdir}
8123
8124         # clear stats.
8125         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8126         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8127
8128         # verify mdt stats first.
8129         mkdir ${testdir} || error "mkdir failed"
8130         check_stats $SINGLEMDS "mkdir" 1
8131         touch ${testdir}/${tfile} || "touch failed"
8132         check_stats $SINGLEMDS "open" 1
8133         check_stats $SINGLEMDS "close" 1
8134         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8135         check_stats $SINGLEMDS "mknod" 1
8136         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8137         check_stats $SINGLEMDS "unlink" 1
8138         rm -f ${testdir}/${tfile} || error "file remove failed"
8139         check_stats $SINGLEMDS "unlink" 2
8140
8141         # remove working dir and check mdt stats again.
8142         rmdir ${testdir} || error "rmdir failed"
8143         check_stats $SINGLEMDS "rmdir" 1
8144
8145         local testdir1=$DIR/${tdir}/stats_testdir1
8146         mkdir -p ${testdir}
8147         mkdir -p ${testdir1}
8148         touch ${testdir1}/test1
8149         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8150         check_stats $SINGLEMDS "crossdir_rename" 1
8151
8152         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8153         check_stats $SINGLEMDS "samedir_rename" 1
8154
8155         rm -rf $DIR/${tdir}
8156 }
8157 run_test 133a "Verifying MDT stats ========================================"
8158
8159 test_133b() {
8160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8161         remote_ost_nodsh && skip "remote OST with nodsh" && return
8162         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8163         local testdir=$DIR/${tdir}/stats_testdir
8164         mkdir -p ${testdir} || error "mkdir failed"
8165         touch ${testdir}/${tfile} || "touch failed"
8166         cancel_lru_locks mdc
8167
8168         # clear stats.
8169         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8170         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8171
8172         # extra mdt stats verification.
8173         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8174         check_stats $SINGLEMDS "setattr" 1
8175         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8176         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8177         then            # LU-1740
8178                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8179                 check_stats $SINGLEMDS "getattr" 1
8180         fi
8181         $LFS df || error "lfs failed"
8182         check_stats $SINGLEMDS "statfs" 1
8183
8184         rm -rf $DIR/${tdir}
8185 }
8186 run_test 133b "Verifying extra MDT stats =================================="
8187
8188 test_133c() {
8189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8190         remote_ost_nodsh && skip "remote OST with nodsh" && return
8191         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8192         local testdir=$DIR/${tdir}/stats_testdir
8193         test_mkdir -p ${testdir} || error "mkdir failed"
8194
8195         # verify obdfilter stats.
8196         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8197         sync
8198         cancel_lru_locks osc
8199         wait_delete_completed
8200
8201         # clear stats.
8202         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8203         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8204
8205         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8206         sync
8207         cancel_lru_locks osc
8208         check_stats ost "write" 1
8209
8210         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8211         check_stats ost "read" 1
8212
8213         > ${testdir}/${tfile} || error "truncate failed"
8214         check_stats ost "punch" 1
8215
8216         rm -f ${testdir}/${tfile} || error "file remove failed"
8217         wait_delete_completed
8218         check_stats ost "destroy" 1
8219
8220         rm -rf $DIR/${tdir}
8221 }
8222 run_test 133c "Verifying OST stats ========================================"
8223
8224 order_2() {
8225     local value=$1
8226     local orig=$value
8227     local order=1
8228
8229     while [ $value -ge 2 ]; do
8230         order=$((order*2))
8231         value=$((value/2))
8232     done
8233
8234     if [ $orig -gt $order ]; then
8235         order=$((order*2))
8236     fi
8237     echo $order
8238 }
8239
8240 size_in_KMGT() {
8241     local value=$1
8242     local size=('K' 'M' 'G' 'T');
8243     local i=0
8244     local size_string=$value
8245
8246     while [ $value -ge 1024 ]; do
8247         if [ $i -gt 3 ]; then
8248             #T is the biggest unit we get here, if that is bigger,
8249             #just return XXXT
8250             size_string=${value}T
8251             break
8252         fi
8253         value=$((value >> 10))
8254         if [ $value -lt 1024 ]; then
8255             size_string=${value}${size[$i]}
8256             break
8257         fi
8258         i=$((i + 1))
8259     done
8260
8261     echo $size_string
8262 }
8263
8264 get_rename_size() {
8265     local size=$1
8266     local context=${2:-.}
8267     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8268                 grep -A1 $context |
8269                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8270     echo $sample
8271 }
8272
8273 test_133d() {
8274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8275     remote_ost_nodsh && skip "remote OST with nodsh" && return
8276     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8277     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8278         { skip "MDS doesn't support rename stats"; return; }
8279
8280     local testdir1=$DIR/${tdir}/stats_testdir1
8281     local testdir2=$DIR/${tdir}/stats_testdir2
8282
8283     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8284
8285     test_mkdir -p ${testdir1} || error "mkdir failed"
8286     test_mkdir -p ${testdir2} || error "mkdir failed"
8287
8288     createmany -o $testdir1/test 512 || error "createmany failed"
8289
8290         # check samedir rename size
8291         mv ${testdir1}/test0 ${testdir1}/test_0
8292
8293         local testdir1_size=$(ls -l $DIR/${tdir} |
8294                 awk '/stats_testdir1/ {print $5}')
8295         local testdir2_size=$(ls -l $DIR/${tdir} |
8296                 awk '/stats_testdir2/ {print $5}')
8297
8298         testdir1_size=$(order_2 $testdir1_size)
8299         testdir2_size=$(order_2 $testdir2_size)
8300
8301         testdir1_size=$(size_in_KMGT $testdir1_size)
8302         testdir2_size=$(size_in_KMGT $testdir2_size)
8303
8304         echo "source rename dir size: ${testdir1_size}"
8305         echo "target rename dir size: ${testdir2_size}"
8306
8307     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8308     eval $cmd || error "$cmd failed"
8309     local samedir=$($cmd | grep 'same_dir')
8310     local same_sample=$(get_rename_size $testdir1_size)
8311     [ -z "$samedir" ] && error "samedir_rename_size count error"
8312     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8313     echo "Check same dir rename stats success"
8314
8315     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8316
8317     # check crossdir rename size
8318     mv ${testdir1}/test_0 ${testdir2}/test_0
8319
8320         testdir1_size=$(ls -l $DIR/${tdir} |
8321                 awk '/stats_testdir1/ {print $5}')
8322         testdir2_size=$(ls -l $DIR/${tdir} |
8323                 awk '/stats_testdir2/ {print $5}')
8324
8325         testdir1_size=$(order_2 $testdir1_size)
8326         testdir2_size=$(order_2 $testdir2_size)
8327
8328         testdir1_size=$(size_in_KMGT $testdir1_size)
8329         testdir2_size=$(size_in_KMGT $testdir2_size)
8330
8331         echo "source rename dir size: ${testdir1_size}"
8332         echo "target rename dir size: ${testdir2_size}"
8333
8334     eval $cmd || error "$cmd failed"
8335     local crossdir=$($cmd | grep 'crossdir')
8336     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8337     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8338     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8339     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8340     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8341     echo "Check cross dir rename stats success"
8342     rm -rf $DIR/${tdir}
8343 }
8344 run_test 133d "Verifying rename_stats ========================================"
8345
8346 test_133e() {
8347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8348         local testdir=$DIR/${tdir}/stats_testdir
8349         local ctr f0 f1 bs=32768 count=42 sum
8350
8351         remote_ost_nodsh && skip "remote OST with nodsh" && return
8352         mkdir -p ${testdir} || error "mkdir failed"
8353
8354         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8355
8356         for ctr in {write,read}_bytes; do
8357                 sync
8358                 cancel_lru_locks osc
8359
8360                 do_facet ost1 $LCTL set_param -n \
8361                         "obdfilter.*.exports.clear=clear"
8362
8363                 if [ $ctr = write_bytes ]; then
8364                         f0=/dev/zero
8365                         f1=${testdir}/${tfile}
8366                 else
8367                         f0=${testdir}/${tfile}
8368                         f1=/dev/null
8369                 fi
8370
8371                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8372                         error "dd failed"
8373                 sync
8374                 cancel_lru_locks osc
8375
8376                 sum=$(do_facet ost1 $LCTL get_param \
8377                                 "obdfilter.*.exports.*.stats" | \
8378                           awk -v ctr=$ctr '\
8379                                 BEGIN { sum = 0 }
8380                                 $1 == ctr { sum += $7 }
8381                                 END { print sum }')
8382
8383                 if ((sum != bs * count)); then
8384                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8385                 fi
8386         done
8387
8388         rm -rf $DIR/${tdir}
8389 }
8390 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8391
8392 test_133f() {
8393         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8394         local facet
8395
8396         # First without trusting modes.
8397         find $proc_dirs \
8398                 -exec cat '{}' \; &> /dev/null
8399
8400         # Second verifying readability.
8401         find $proc_dirs \
8402                 -type f \
8403                 -readable \
8404                 -exec cat '{}' \; > /dev/null ||
8405                         error "proc file read failed"
8406
8407         for facet in $SINGLEMDS ost1; do
8408                 do_facet $facet find $proc_dirs \
8409                         -not -name req_history \
8410                         -exec cat '{}' \\\; &> /dev/null
8411
8412             do_facet $facet     find $proc_dirs \
8413                         -not -name req_history \
8414                         -type f \
8415                         -readable \
8416                         -exec cat '{}' \\\; > /dev/null ||
8417                                 error "proc file read failed"
8418         done
8419 }
8420 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8421
8422 test_140() { #bug-17379
8423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8424         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8425         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8426         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8427
8428         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8429         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8430         local i=0
8431         while i=`expr $i + 1`; do
8432                 test_mkdir -p $i || error "Creating dir $i"
8433                 cd $i || error "Changing to $i"
8434                 ln -s ../stat stat || error "Creating stat symlink"
8435                 # Read the symlink until ELOOP present,
8436                 # not LBUGing the system is considered success,
8437                 # we didn't overrun the stack.
8438                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8439                 [ $ret -ne 0 ] && {
8440                         if [ $ret -eq 40 ]; then
8441                                 break  # -ELOOP
8442                         else
8443                                 error "Open stat symlink"
8444                                 return
8445                         fi
8446                 }
8447         done
8448         i=`expr $i - 1`
8449         echo "The symlink depth = $i"
8450         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8451                                         error "Invalid symlink depth"
8452
8453         # Test recursive symlink
8454         ln -s symlink_self symlink_self
8455         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8456         echo "open symlink_self returns $ret"
8457         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8458 }
8459 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8460
8461 test_150() {
8462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8463         local TF="$TMP/$tfile"
8464
8465         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8466         cp $TF $DIR/$tfile
8467         cancel_lru_locks osc
8468         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8469         remount_client $MOUNT
8470         df -P $MOUNT
8471         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8472
8473         $TRUNCATE $TF 6000
8474         $TRUNCATE $DIR/$tfile 6000
8475         cancel_lru_locks osc
8476         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8477
8478         echo "12345" >>$TF
8479         echo "12345" >>$DIR/$tfile
8480         cancel_lru_locks osc
8481         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8482
8483         echo "12345" >>$TF
8484         echo "12345" >>$DIR/$tfile
8485         cancel_lru_locks osc
8486         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8487
8488         rm -f $TF
8489         true
8490 }
8491 run_test 150 "truncate/append tests"
8492
8493 function roc_hit() {
8494         local list=$(comma_list $(osts_nodes))
8495         #debug temp debug for LU-2902: lets see what values we get back
8496         echo $(get_osd_param $list '' stats) 1>&2
8497         echo $(get_osd_param $list '' stats |
8498                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8499 }
8500
8501 function set_cache() {
8502         local on=1
8503
8504         if [ "$2" == "off" ]; then
8505                 on=0;
8506         fi
8507         local list=$(comma_list $(osts_nodes))
8508         set_osd_param $list '' $1_cache_enable $on
8509
8510         cancel_lru_locks osc
8511 }
8512
8513 test_151() {
8514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8515         remote_ost_nodsh && skip "remote OST with nodsh" && return
8516
8517         local CPAGES=3
8518         local list=$(comma_list $(osts_nodes))
8519
8520         # check whether obdfilter is cache capable at all
8521         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8522                 echo "not cache-capable obdfilter"
8523                 return 0
8524         fi
8525
8526         # check cache is enabled on all obdfilters
8527         if get_osd_param $list '' read_cache_enable | grep 0; then
8528                 echo "oss cache is disabled"
8529                 return 0
8530         fi
8531
8532         set_osd_param $list '' writethrough_cache_enable 1
8533
8534         # check write cache is enabled on all obdfilters
8535         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
8536                 echo "oss write cache is NOT enabled"
8537                 return 0
8538         fi
8539
8540 #define OBD_FAIL_OBD_NO_LRU  0x609
8541         do_nodes $list $LCTL set_param fail_loc=0x609
8542
8543         # pages should be in the case right after write
8544         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
8545                 error "dd failed"
8546
8547         local BEFORE=`roc_hit`
8548         cancel_lru_locks osc
8549         cat $DIR/$tfile >/dev/null
8550         local AFTER=`roc_hit`
8551
8552         do_nodes $list $LCTL set_param fail_loc=0
8553
8554         if ! let "AFTER - BEFORE == CPAGES"; then
8555                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8556         fi
8557
8558         # the following read invalidates the cache
8559         cancel_lru_locks osc
8560         set_osd_param $list '' read_cache_enable 0
8561         cat $DIR/$tfile >/dev/null
8562
8563         # now data shouldn't be found in the cache
8564         BEFORE=`roc_hit`
8565         cancel_lru_locks osc
8566         cat $DIR/$tfile >/dev/null
8567         AFTER=`roc_hit`
8568         if let "AFTER - BEFORE != 0"; then
8569                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8570         fi
8571
8572         set_osd_param $list '' read_cache_enable 1
8573         rm -f $DIR/$tfile
8574 }
8575 run_test 151 "test cache on oss and controls ==============================="
8576
8577 test_152() {
8578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8579         local TF="$TMP/$tfile"
8580
8581         # simulate ENOMEM during write
8582 #define OBD_FAIL_OST_NOMEM      0x226
8583         lctl set_param fail_loc=0x80000226
8584         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8585         cp $TF $DIR/$tfile
8586         sync || error "sync failed"
8587         lctl set_param fail_loc=0
8588
8589         # discard client's cache
8590         cancel_lru_locks osc
8591
8592         # simulate ENOMEM during read
8593         lctl set_param fail_loc=0x80000226
8594         cmp $TF $DIR/$tfile || error "cmp failed"
8595         lctl set_param fail_loc=0
8596
8597         rm -f $TF
8598 }
8599 run_test 152 "test read/write with enomem ============================"
8600
8601 test_153() {
8602         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8603 }
8604 run_test 153 "test if fdatasync does not crash ======================="
8605
8606 dot_lustre_fid_permission_check() {
8607         local fid=$1
8608         local ffid=$MOUNT/.lustre/fid/$fid
8609         local test_dir=$2
8610
8611         echo "stat fid $fid"
8612         stat $ffid > /dev/null || error "stat $ffid failed."
8613         echo "touch fid $fid"
8614         touch $ffid || error "touch $ffid failed."
8615         echo "write to fid $fid"
8616         cat /etc/hosts > $ffid || error "write $ffid failed."
8617         echo "read fid $fid"
8618         diff /etc/hosts $ffid || error "read $ffid failed."
8619         echo "append write to fid $fid"
8620         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8621         echo "rename fid $fid"
8622         mv $ffid $test_dir/$tfile.1 &&
8623                 error "rename $ffid to $tfile.1 should fail."
8624         touch $test_dir/$tfile.1
8625         mv $test_dir/$tfile.1 $ffid &&
8626                 error "rename $tfile.1 to $ffid should fail."
8627         rm -f $test_dir/$tfile.1
8628         echo "truncate fid $fid"
8629         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8630         echo "link fid $fid"
8631         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
8632         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8633                 echo "setfacl fid $fid"
8634                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8635                 echo "getfacl fid $fid"
8636                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8637         fi
8638         echo "unlink fid $fid"
8639         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
8640         echo "mknod fid $fid"
8641         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8642
8643         fid=[0xf00000400:0x1:0x0]
8644         ffid=$MOUNT/.lustre/fid/$fid
8645
8646         echo "stat non-exist fid $fid"
8647         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8648         echo "write to non-exist fid $fid"
8649         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8650         echo "link new fid $fid"
8651         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
8652
8653         mkdir -p $test_dir/$tdir
8654         touch $test_dir/$tdir/$tfile
8655         fid=$($LFS path2fid $test_dir/$tdir)
8656         rc=$?
8657         [ $rc -ne 0 ] &&
8658                 error "error: could not get fid for $test_dir/$dir/$tfile."
8659
8660         ffid=$MOUNT/.lustre/fid/$fid
8661
8662         echo "ls $fid"
8663         ls $ffid > /dev/null || error "ls $ffid failed."
8664         echo "touch $fid/$tfile.1"
8665         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8666
8667         echo "touch $MOUNT/.lustre/fid/$tfile"
8668         touch $MOUNT/.lustre/fid/$tfile && \
8669                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
8670
8671         echo "setxattr to $MOUNT/.lustre/fid"
8672         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid &&
8673                 error "setxattr should fail."
8674
8675         echo "listxattr for $MOUNT/.lustre/fid"
8676         getfattr -d -m "^trusted" $MOUNT/.lustre/fid &&
8677                 error "listxattr should fail."
8678
8679         echo "delxattr from $MOUNT/.lustre/fid"
8680         setfattr -x trusted.name1 $MOUNT/.lustre/fid &&
8681                 error "delxattr should fail."
8682
8683         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
8684         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
8685                 error "touch invalid fid should fail."
8686
8687         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
8688         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
8689                 error "touch non-normal fid should fail."
8690
8691         echo "rename $tdir to $MOUNT/.lustre/fid"
8692         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
8693                 error "rename to $MOUNT/.lustre/fid should fail."
8694
8695         echo "rename .lustre to itself"
8696         fid=$($LFS path2fid $MOUNT)
8697         mrename $MOUNT/.lustre $MOUNT/.lustre/fid/$fid/.lustre &&
8698                 error "rename .lustre to itself should fail."
8699
8700         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
8701         fid=$($LFS path2fid $test_dir/$tfile-2)
8702         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
8703         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
8704                 error "create lov data thru .lustre should fail."
8705         echo "cp /etc/passwd $test_dir/$tfile-2"
8706         cp /etc/passwd $test_dir/$tfile-2 ||
8707                 error "copy to $test_dir/$tfile-2 failed."
8708         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
8709         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
8710                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
8711
8712         rm -rf $test_dir/tfile.lnk
8713         rm -rf $test_dir/$tfile-2
8714 }
8715
8716 test_154a() {
8717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8718         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8719                 { skip "Need MDS version at least 2.2.51"; return 0; }
8720
8721         cp /etc/hosts $DIR/$tfile
8722
8723         fid=$($LFS path2fid $DIR/$tfile)
8724         rc=$?
8725         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8726
8727         dot_lustre_fid_permission_check "$fid" $DIR ||
8728                 error "dot lustre permission check $fid failed"
8729
8730         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
8731         chmod 777 $MOUNT/.lustre && error ".lustre is not allowed to be chmod"
8732
8733         touch $MOUNT/.lustre/file &&
8734                 error "creation is not allowed under .lustre"
8735
8736         mkdir $MOUNT/.lustre/dir &&
8737                 error "mkdir is not allowed under .lustre"
8738
8739         rm -rf $DIR/$tfile
8740 }
8741 run_test 154a "Open-by-FID"
8742
8743 test_154b() {
8744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8745         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8746                 { skip "Need MDS version at least 2.2.51"; return 0; }
8747
8748         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
8749
8750         local remote_dir=$DIR/$tdir/remote_dir
8751         local MDTIDX=1
8752         local rc=0
8753
8754         mkdir -p $DIR/$tdir
8755         $LFS mkdir -i $MDTIDX $remote_dir ||
8756                 error "create remote directory failed"
8757
8758         cp /etc/hosts $remote_dir/$tfile
8759
8760         fid=$($LFS path2fid $remote_dir/$tfile)
8761         rc=$?
8762         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
8763
8764         dot_lustre_fid_permission_check "$fid" $remote_dir ||
8765                 error "dot lustre permission check $fid failed"
8766         rm -rf $DIR/$tdir
8767 }
8768 run_test 154b "Open-by-FID for remote directory"
8769
8770 test_155_small_load() {
8771     local temp=$TMP/$tfile
8772     local file=$DIR/$tfile
8773
8774     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8775         error "dd of=$temp bs=6096 count=1 failed"
8776     cp $temp $file
8777     cancel_lru_locks osc
8778     cmp $temp $file || error "$temp $file differ"
8779
8780     $TRUNCATE $temp 6000
8781     $TRUNCATE $file 6000
8782     cmp $temp $file || error "$temp $file differ (truncate1)"
8783
8784     echo "12345" >>$temp
8785     echo "12345" >>$file
8786     cmp $temp $file || error "$temp $file differ (append1)"
8787
8788     echo "12345" >>$temp
8789     echo "12345" >>$file
8790     cmp $temp $file || error "$temp $file differ (append2)"
8791
8792     rm -f $temp $file
8793     true
8794 }
8795
8796 test_155_big_load() {
8797     remote_ost_nodsh && skip "remote OST with nodsh" && return
8798     local temp=$TMP/$tfile
8799     local file=$DIR/$tfile
8800
8801     free_min_max
8802     local cache_size=$(do_facet ost$((MAXI+1)) \
8803         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8804     local large_file_size=$((cache_size * 2))
8805
8806     echo "OSS cache size: $cache_size KB"
8807     echo "Large file size: $large_file_size KB"
8808
8809     [ $MAXV -le $large_file_size ] && \
8810         skip_env "max available OST size needs > $large_file_size KB" && \
8811         return 0
8812
8813     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8814
8815     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8816         error "dd of=$temp bs=$large_file_size count=1k failed"
8817     cp $temp $file
8818     ls -lh $temp $file
8819     cancel_lru_locks osc
8820     cmp $temp $file || error "$temp $file differ"
8821
8822     rm -f $temp $file
8823     true
8824 }
8825
8826 test_155a() {
8827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8828         set_cache read on
8829         set_cache writethrough on
8830         test_155_small_load
8831 }
8832 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8833
8834 test_155b() {
8835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8836         set_cache read on
8837         set_cache writethrough off
8838         test_155_small_load
8839 }
8840 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8841
8842 test_155c() {
8843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8844         set_cache read off
8845         set_cache writethrough on
8846         test_155_small_load
8847 }
8848 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8849
8850 test_155d() {
8851         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8852         set_cache read off
8853         set_cache writethrough off
8854         test_155_small_load
8855 }
8856 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8857
8858 test_155e() {
8859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8860         set_cache read on
8861         set_cache writethrough on
8862         test_155_big_load
8863 }
8864 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8865
8866 test_155f() {
8867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8868         set_cache read on
8869         set_cache writethrough off
8870         test_155_big_load
8871 }
8872 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8873
8874 test_155g() {
8875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8876         set_cache read off
8877         set_cache writethrough on
8878         test_155_big_load
8879 }
8880 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8881
8882 test_155h() {
8883         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8884         set_cache read off
8885         set_cache writethrough off
8886         test_155_big_load
8887 }
8888 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8889
8890 test_156() {
8891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8892         local CPAGES=3
8893         local BEFORE
8894         local AFTER
8895         local file="$DIR/$tfile"
8896
8897         [ "$(facet_fstype ost1)" = "zfs" ] &&
8898                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8899                 return
8900
8901     log "Turn on read and write cache"
8902     set_cache read on
8903     set_cache writethrough on
8904
8905     log "Write data and read it back."
8906     log "Read should be satisfied from the cache."
8907     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8908     BEFORE=`roc_hit`
8909     cancel_lru_locks osc
8910     cat $file >/dev/null
8911     AFTER=`roc_hit`
8912     if ! let "AFTER - BEFORE == CPAGES"; then
8913         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8914     else
8915         log "cache hits:: before: $BEFORE, after: $AFTER"
8916     fi
8917
8918     log "Read again; it should be satisfied from the cache."
8919     BEFORE=$AFTER
8920     cancel_lru_locks osc
8921     cat $file >/dev/null
8922     AFTER=`roc_hit`
8923     if ! let "AFTER - BEFORE == CPAGES"; then
8924         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8925     else
8926         log "cache hits:: before: $BEFORE, after: $AFTER"
8927     fi
8928
8929
8930     log "Turn off the read cache and turn on the write cache"
8931     set_cache read off
8932     set_cache writethrough on
8933
8934     log "Read again; it should be satisfied from the cache."
8935     BEFORE=`roc_hit`
8936     cancel_lru_locks osc
8937     cat $file >/dev/null
8938     AFTER=`roc_hit`
8939     if ! let "AFTER - BEFORE == CPAGES"; then
8940         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8941     else
8942         log "cache hits:: before: $BEFORE, after: $AFTER"
8943     fi
8944
8945     log "Read again; it should not be satisfied from the cache."
8946     BEFORE=$AFTER
8947     cancel_lru_locks osc
8948     cat $file >/dev/null
8949     AFTER=`roc_hit`
8950     if ! let "AFTER - BEFORE == 0"; then
8951         error "IN CACHE: before: $BEFORE, after: $AFTER"
8952     else
8953         log "cache hits:: before: $BEFORE, after: $AFTER"
8954     fi
8955
8956     log "Write data and read it back."
8957     log "Read should be satisfied from the cache."
8958     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8959     BEFORE=`roc_hit`
8960     cancel_lru_locks osc
8961     cat $file >/dev/null
8962     AFTER=`roc_hit`
8963     if ! let "AFTER - BEFORE == CPAGES"; then
8964         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8965     else
8966         log "cache hits:: before: $BEFORE, after: $AFTER"
8967     fi
8968
8969     log "Read again; it should not be satisfied from the cache."
8970     BEFORE=$AFTER
8971     cancel_lru_locks osc
8972     cat $file >/dev/null
8973     AFTER=`roc_hit`
8974     if ! let "AFTER - BEFORE == 0"; then
8975         error "IN CACHE: before: $BEFORE, after: $AFTER"
8976     else
8977         log "cache hits:: before: $BEFORE, after: $AFTER"
8978     fi
8979
8980
8981     log "Turn off read and write cache"
8982     set_cache read off
8983     set_cache writethrough off
8984
8985     log "Write data and read it back"
8986     log "It should not be satisfied from the cache."
8987     rm -f $file
8988     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8989     cancel_lru_locks osc
8990     BEFORE=`roc_hit`
8991     cat $file >/dev/null
8992     AFTER=`roc_hit`
8993     if ! let "AFTER - BEFORE == 0"; then
8994         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8995     else
8996         log "cache hits:: before: $BEFORE, after: $AFTER"
8997     fi
8998
8999
9000     log "Turn on the read cache and turn off the write cache"
9001     set_cache read on
9002     set_cache writethrough off
9003
9004     log "Write data and read it back"
9005     log "It should not be satisfied from the cache."
9006     rm -f $file
9007     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9008     BEFORE=`roc_hit`
9009     cancel_lru_locks osc
9010     cat $file >/dev/null
9011     AFTER=`roc_hit`
9012     if ! let "AFTER - BEFORE == 0"; then
9013         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9014     else
9015         log "cache hits:: before: $BEFORE, after: $AFTER"
9016     fi
9017
9018     log "Read again; it should be satisfied from the cache."
9019     BEFORE=`roc_hit`
9020     cancel_lru_locks osc
9021     cat $file >/dev/null
9022     AFTER=`roc_hit`
9023     if ! let "AFTER - BEFORE == CPAGES"; then
9024         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9025     else
9026         log "cache hits:: before: $BEFORE, after: $AFTER"
9027     fi
9028
9029     rm -f $file
9030 }
9031 run_test 156 "Verification of tunables ============================"
9032
9033 #Changelogs
9034 err17935 () {
9035     if [ $MDSCOUNT -gt 1 ]; then
9036         error_ignore 17935 $*
9037     else
9038         error $*
9039     fi
9040 }
9041
9042 changelog_chmask()
9043 {
9044     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
9045            grep -c $1)
9046
9047     if [ $MASK -eq 1 ]; then
9048         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
9049     else
9050         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
9051     fi
9052 }
9053
9054 test_160() {
9055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9056     remote_mds_nodsh && skip "remote MDS with nodsh" && return
9057     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9058         { skip "Need MDS version at least 2.2.0"; return; }
9059     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9060     echo "Registered as changelog user $USER"
9061     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
9062         grep -q $USER || error "User $USER not found in changelog_users"
9063
9064     # change something
9065     test_mkdir -p $DIR/$tdir/pics/2008/zachy
9066     touch $DIR/$tdir/pics/2008/zachy/timestamp
9067     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9068     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9069     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9070     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9071     rm $DIR/$tdir/pics/desktop.jpg
9072
9073     $LFS changelog $MDT0 | tail -5
9074
9075     echo "verifying changelog mask"
9076     changelog_chmask "MKDIR"
9077     changelog_chmask "CLOSE"
9078
9079     test_mkdir -p $DIR/$tdir/pics/zach/sofia
9080     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9081
9082     changelog_chmask "MKDIR"
9083     changelog_chmask "CLOSE"
9084
9085     test_mkdir -p $DIR/$tdir/pics/2008/sofia
9086     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9087
9088     $LFS changelog $MDT0
9089     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9090     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9091     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9092     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9093
9094     # verify contents
9095     echo "verifying target fid"
9096     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
9097         tail -1 | awk '{print $6}')
9098     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9099     [ "$fidc" == "t=$fidf" ] || \
9100         err17935 "fid in changelog $fidc != file fid $fidf"
9101     echo "verifying parent fid"
9102     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
9103         tail -1 | awk '{print $7}')
9104     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9105     [ "$fidc" == "p=$fidf" ] || \
9106         err17935 "pfid in changelog $fidc != dir fid $fidf"
9107
9108     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
9109         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
9110     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9111     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
9112         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
9113     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9114     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
9115         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
9116
9117     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
9118         awk 'min == "" || $2 < min {min = $2}; END {print min}')
9119     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
9120     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9121     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
9122         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
9123
9124     echo "verifying user deregister"
9125     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9126     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
9127         grep -q $USER && error "User $USER still found in changelog_users"
9128
9129     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
9130         mdd.$MDT0.changelog_users | wc -l) - 2 ))
9131     if [ $USERS -eq 0 ]; then
9132         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
9133             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
9134         touch $DIR/$tdir/chloe
9135         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
9136             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
9137         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
9138         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9139     else
9140         echo "$USERS other changelog users; can't verify off"
9141     fi
9142 }
9143 run_test 160 "changelog sanity"
9144
9145 test_161a() {
9146         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9147     test_mkdir -p $DIR/$tdir
9148     cp /etc/hosts $DIR/$tdir/$tfile
9149     test_mkdir $DIR/$tdir/foo1
9150     test_mkdir $DIR/$tdir/foo2
9151     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9152     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9153     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9154     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9155         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9156         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9157                 $LFS fid2path $DIR $FID
9158                 err17935 "bad link ea"
9159         fi
9160     # middle
9161     rm $DIR/$tdir/foo2/zachary
9162     # last
9163     rm $DIR/$tdir/foo2/thor
9164     # first
9165     rm $DIR/$tdir/$tfile
9166     # rename
9167     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9168     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9169         then
9170         $LFS fid2path $DIR $FID
9171         err17935 "bad link rename"
9172     fi
9173     rm $DIR/$tdir/foo2/maggie
9174
9175     # overflow the EA
9176     local longname=filename_avg_len_is_thirty_two_
9177     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
9178         error "failed to hardlink many files"
9179     links=$($LFS fid2path $DIR $FID | wc -l)
9180     echo -n "${links}/1000 links in link EA"
9181     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9182     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
9183         error "failed to unlink many hardlinks"
9184 }
9185 run_test 161a "link ea sanity"
9186
9187 test_161b() {
9188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9189         [ $MDSCOUNT -lt 2 ] &&
9190                 skip "skipping remote directory test" && return
9191         local MDTIDX=1
9192         local remote_dir=$DIR/$tdir/remote_dir
9193
9194         mkdir -p $DIR/$tdir
9195         $LFS mkdir -i $MDTIDX $remote_dir ||
9196                 error "create remote directory failed"
9197
9198         cp /etc/hosts $remote_dir/$tfile
9199         mkdir -p $remote_dir/foo1
9200         mkdir -p $remote_dir/foo2
9201         ln $remote_dir/$tfile $remote_dir/foo1/sofia
9202         ln $remote_dir/$tfile $remote_dir/foo2/zachary
9203         ln $remote_dir/$tfile $remote_dir/foo1/luna
9204         ln $remote_dir/$tfile $remote_dir/foo2/thor
9205
9206         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
9207                      tr -d ']')
9208         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9209                 $LFS fid2path $DIR $FID
9210                 err17935 "bad link ea"
9211         fi
9212         # middle
9213         rm $remote_dir/foo2/zachary
9214         # last
9215         rm $remote_dir/foo2/thor
9216         # first
9217         rm $remote_dir/$tfile
9218         # rename
9219         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
9220         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
9221         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
9222                 $LFS fid2path $DIR $FID
9223                 err17935 "bad link rename"
9224         fi
9225         rm $remote_dir/foo2/maggie
9226
9227         # overflow the EA
9228         local longname=filename_avg_len_is_thirty_two_
9229         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
9230                 error "failed to hardlink many files"
9231         links=$($LFS fid2path $DIR $FID | wc -l)
9232         echo -n "${links}/1000 links in link EA"
9233         [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9234         unlinkmany $remote_dir/foo2/$longname 1000 ||
9235         error "failed to unlink many hardlinks"
9236 }
9237 run_test 161b "link ea sanity under remote directory"
9238
9239 check_path() {
9240     local expected=$1
9241     shift
9242     local fid=$2
9243
9244     local path=$(${LFS} fid2path $*)
9245     RC=$?
9246
9247     if [ $RC -ne 0 ]; then
9248         err17935 "path looked up of $expected failed. Error $RC"
9249         return $RC
9250     elif [ "${path}" != "${expected}" ]; then
9251         err17935 "path looked up \"${path}\" instead of \"${expected}\""
9252         return 2
9253     fi
9254     echo "fid $fid resolves to path $path (expected $expected)"
9255 }
9256
9257 test_162() {
9258         # Make changes to filesystem
9259         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9260         test_mkdir -p $DIR/$tdir/d2
9261         touch $DIR/$tdir/d2/$tfile
9262         touch $DIR/$tdir/d2/x1
9263         touch $DIR/$tdir/d2/x2
9264         test_mkdir -p $DIR/$tdir/d2/a/b/c
9265         test_mkdir -p $DIR/$tdir/d2/p/q/r
9266         # regular file
9267         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
9268         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
9269
9270         # softlink
9271         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
9272         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
9273         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
9274
9275         # softlink to wrong file
9276         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
9277         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
9278         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
9279
9280         # hardlink
9281         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
9282         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
9283         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
9284         # fid2path dir/fsname should both work
9285         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
9286         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9287
9288         # hardlink count: check that there are 2 links
9289         # Doesnt work with CMD yet: 17935
9290         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9291                 err17935 "expected 2 links"
9292
9293         # hardlink indexing: remove the first link
9294         rm $DIR/$tdir/d2/p/q/r/hlink
9295         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9296
9297         return 0
9298 }
9299 run_test 162 "path lookup sanity"
9300
9301 test_163() {
9302         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9303         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9304         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
9305         copytool $FSNAME &
9306         sleep 1
9307         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
9308         # this proc file is temporary and linux-only
9309         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
9310          error "kernel->userspace send failed"
9311         kill -INT $!
9312 }
9313 run_test 163 "kernel <-> userspace comms"
9314
9315 test_169() {
9316         # do directio so as not to populate the page cache
9317         log "creating a 10 Mb file"
9318         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9319         log "starting reads"
9320         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9321         log "truncating the file"
9322         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9323         log "killing dd"
9324         kill %+ || true # reads might have finished
9325         echo "wait until dd is finished"
9326         wait
9327         log "removing the temporary file"
9328         rm -rf $DIR/$tfile || error "tmp file removal failed"
9329 }
9330 run_test 169 "parallel read and truncate should not deadlock"
9331
9332 test_170() {
9333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9334         $LCTL clear     # bug 18514
9335         $LCTL debug_daemon start $TMP/${tfile}_log_good
9336         touch $DIR/$tfile
9337         $LCTL debug_daemon stop
9338         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9339                error "sed failed to read log_good"
9340
9341         $LCTL debug_daemon start $TMP/${tfile}_log_good
9342         rm -rf $DIR/$tfile
9343         $LCTL debug_daemon stop
9344
9345         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9346                error "lctl df log_bad failed"
9347
9348         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9349         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9350
9351         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9352         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9353
9354         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9355                 error "bad_line good_line1 good_line2 are empty"
9356
9357         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9358         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9359         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9360
9361         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9362         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9363         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9364
9365         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9366                 error "bad_line_new good_line_new are empty"
9367
9368         local expected_good=$((good_line1 + good_line2*2))
9369
9370         rm -f $TMP/${tfile}*
9371         # LU-231, short malformed line may not be counted into bad lines
9372         if [ $bad_line -ne $bad_line_new ] &&
9373                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9374                 error "expected $bad_line bad lines, but got $bad_line_new"
9375                 return 1
9376         fi
9377
9378         if [ $expected_good -ne $good_line_new ]; then
9379                 error "expected $expected_good good lines, but got $good_line_new"
9380                 return 2
9381         fi
9382         true
9383 }
9384 run_test 170 "test lctl df to handle corrupted log ====================="
9385
9386 test_171() { # bug20592
9387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9388 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9389         $LCTL set_param fail_loc=0x50e
9390         $LCTL set_param fail_val=3000
9391         multiop_bg_pause $DIR/$tfile O_s || true
9392         local MULTIPID=$!
9393         kill -USR1 $MULTIPID
9394         # cause log dump
9395         sleep 3
9396         wait $MULTIPID
9397         if dmesg | grep "recursive fault"; then
9398                 error "caught a recursive fault"
9399         fi
9400         $LCTL set_param fail_loc=0
9401         true
9402 }
9403 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
9404
9405 # it would be good to share it with obdfilter-survey/libecho code
9406 setup_obdecho_osc () {
9407         local rc=0
9408         local ost_nid=$1
9409         local obdfilter_name=$2
9410         echo "Creating new osc for $obdfilter_name on $ost_nid"
9411         # make sure we can find loopback nid
9412         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
9413
9414         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
9415                            ${obdfilter_name}_osc_UUID || rc=2; }
9416         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
9417                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
9418         return $rc
9419 }
9420
9421 cleanup_obdecho_osc () {
9422         local obdfilter_name=$1
9423         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
9424         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
9425         return 0
9426 }
9427
9428 obdecho_create_test() {
9429         local OBD=$1
9430         local node=$2
9431         local rc=0
9432         local id
9433         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
9434         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
9435                            rc=2; }
9436         if [ $rc -eq 0 ]; then
9437             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
9438             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
9439         fi
9440         echo "New object id is $id"
9441         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
9442                            rc=4; }
9443         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
9444                                         "cleanup" || rc=5; }
9445         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
9446                                         "detach" || rc=6; }
9447         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
9448         return $rc
9449 }
9450
9451 test_180a() {
9452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9453         remote_ost_nodsh && skip "remote OST with nodsh" && return
9454         local rc=0
9455         local rmmod_local=0
9456
9457         if ! module_loaded obdecho; then
9458             load_module obdecho/obdecho
9459             rmmod_local=1
9460         fi
9461
9462         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
9463         local host=$(lctl get_param -n osc.$osc.import |
9464                              awk '/current_connection:/ {print $2}' )
9465         local target=$(lctl get_param -n osc.$osc.import |
9466                              awk '/target:/ {print $2}' )
9467         target=${target%_UUID}
9468
9469         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
9470         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
9471         [[ -n $target ]] && cleanup_obdecho_osc $target
9472         [ $rmmod_local -eq 1 ] && rmmod obdecho
9473         return $rc
9474 }
9475 run_test 180a "test obdecho on osc"
9476
9477 test_180b() {
9478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9479         remote_ost_nodsh && skip "remote OST with nodsh" && return
9480         local rc=0
9481         local rmmod_remote=0
9482
9483         do_facet ost1 "lsmod | grep -q obdecho || "                      \
9484                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
9485                       "modprobe obdecho; }" && rmmod_remote=1
9486         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
9487         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
9488         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
9489         return $rc
9490 }
9491 run_test 180b "test obdecho directly on obdfilter"
9492
9493 test_181() { # bug 22177
9494         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9495         # create enough files to index the directory
9496         createmany -o $DIR/$tdir/foobar 4000
9497         # print attributes for debug purpose
9498         lsattr -d .
9499         # open dir
9500         multiop_bg_pause $DIR/$tdir D_Sc || return 1
9501         MULTIPID=$!
9502         # remove the files & current working dir
9503         unlinkmany $DIR/$tdir/foobar 4000
9504         rmdir $DIR/$tdir
9505         kill -USR1 $MULTIPID
9506         wait $MULTIPID
9507         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9508         return 0
9509 }
9510 run_test 181 "Test open-unlinked dir ========================"
9511
9512 test_182() {
9513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9514         # disable MDC RPC lock wouldn't crash client
9515         local fcount=1000
9516         local tcount=4
9517
9518         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9519 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9520         $LCTL set_param fail_loc=0x804
9521
9522         for (( i=0; i < $tcount; i++ )) ; do
9523                 mkdir $DIR/$tdir/$i
9524                 createmany -o $DIR/$tdir/$i/f- $fcount &
9525         done
9526         wait
9527
9528         for (( i=0; i < $tcount; i++ )) ; do
9529                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9530         done
9531         wait
9532
9533         rm -rf $DIR/$tdir
9534
9535         $LCTL set_param fail_loc=0
9536 }
9537 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9538
9539 test_183() { # LU-2275
9540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9541         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9542         echo aaa > $DIR/$tdir/$tfile
9543
9544 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9545         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9546
9547         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9548         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9549
9550         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9551
9552         # Flush negative dentry cache
9553         touch $DIR/$tdir/$tfile
9554
9555         # We are not checking for any leaked references here, they'll
9556         # become evident next time we do cleanup with module unload.
9557         rm -rf $DIR/$tdir
9558 }
9559 run_test 183 "No crash or request leak in case of strange dispositions ========"
9560
9561 # test suite 184 is for LU-2016, LU-2017
9562 test_184a() {
9563         check_swap_layouts_support && return 0
9564
9565         dir0=$DIR/$tdir/$testnum
9566         test_mkdir -p $dir0 || error "creating dir $dir0"
9567         ref1=/etc/passwd
9568         ref2=/etc/group
9569         file1=$dir0/f1
9570         file2=$dir0/f2
9571         $SETSTRIPE -c1 $file1
9572         cp $ref1 $file1
9573         $SETSTRIPE -c2 $file2
9574         cp $ref2 $file2
9575         gen1=$($GETSTRIPE -g $file1)
9576         gen2=$($GETSTRIPE -g $file2)
9577
9578         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
9579         gen=$($GETSTRIPE -g $file1)
9580         [[ $gen1 != $gen ]] ||
9581                 "Layout generation on $file1 does not change"
9582         gen=$($GETSTRIPE -g $file2)
9583         [[ $gen2 != $gen ]] ||
9584                 "Layout generation on $file2 does not change"
9585
9586         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
9587         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
9588 }
9589 run_test 184a "Basic layout swap"
9590
9591 test_184b() {
9592         check_swap_layouts_support && return 0
9593
9594         dir0=$DIR/$tdir/$testnum
9595         mkdir -p $dir0 || error "creating dir $dir0"
9596         file1=$dir0/f1
9597         file2=$dir0/f2
9598         file3=$dir0/f3
9599         dir1=$dir0/d1
9600         dir2=$dir0/d2
9601         mkdir $dir1 $dir2
9602         $SETSTRIPE -c1 $file1
9603         $SETSTRIPE -c2 $file2
9604         $SETSTRIPE -c1 $file3
9605         chown $RUNAS_ID $file3
9606         gen1=$($GETSTRIPE -g $file1)
9607         gen2=$($GETSTRIPE -g $file2)
9608
9609         $LFS swap_layouts $dir1 $dir2 &&
9610                 error "swap of directories layouts should fail"
9611         $LFS swap_layouts $dir1 $file1 &&
9612                 error "swap of directory and file layouts should fail"
9613         $RUNAS $LFS swap_layouts $file1 $file2 &&
9614                 error "swap of file we cannot write should fail"
9615         $LFS swap_layouts $file1 $file3 &&
9616                 error "swap of file with different owner should fail"
9617         /bin/true # to clear error code
9618 }
9619 run_test 184b "Forbidden layout swap (will generate errors)"
9620
9621 test_184c() {
9622         check_swap_layouts_support && return 0
9623
9624         local dir0=$DIR/$tdir/$testnum
9625         mkdir -p $dir0 || error "creating dir $dir0"
9626
9627         local ref1=$dir0/ref1
9628         local ref2=$dir0/ref2
9629         local file1=$dir0/file1
9630         local file2=$dir0/file2
9631         # create a file large enough for the concurent test
9632         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
9633         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
9634         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
9635
9636         cp $ref2 $file2
9637         dd if=$ref1 of=$file1 bs=16k &
9638         local DD_PID=$!
9639
9640         # Make sure dd starts to copy file
9641         while [ ! -f $file1 ]; do sleep 0.1; done
9642
9643         $LFS swap_layouts $file1 $file2
9644         local rc=$?
9645         wait $DD_PID
9646         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
9647         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
9648
9649         # how many bytes copied before swapping layout
9650         local copied=`stat -c %s $file2`
9651         local remaining=`stat -c %s $ref1`
9652         remaining=$((remaining - copied))
9653         echo "Copied $copied bytes before swapping layout..."
9654
9655         cmp -n $copied $file1 $ref2 | grep differ &&
9656                 error "Content mismatch [0, $copied) of ref2 and file1"
9657         cmp -n $copied $file2 $ref1 ||
9658                 error "Content mismatch [0, $copied) of ref1 and file2"
9659         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
9660                 error "Content mismatch [$copied, EOF) of ref1 and file1"
9661
9662         # clean up
9663         rm -f $ref1 $ref2 $file1 $file2
9664 }
9665 run_test 184c "Concurrent write and layout swap"
9666
9667 test_185() { # LU-2441
9668         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9669         touch $DIR/$tdir/spoo
9670         local mtime1=$(stat -c "%Y" $DIR/$tdir)
9671         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
9672                 error "cannot create/write a volatile file"
9673         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
9674                 error "FID is still valid after close"
9675
9676         multiop_bg_pause $DIR/$tdir vVw4096_c
9677         local multi_pid=$!
9678
9679         local OLD_IFS=$IFS
9680         IFS=":"
9681         local fidv=($fid)
9682         IFS=$OLD_IFS
9683         # assume that the next FID for this client is sequential, since stdout
9684         # is unfortunately eaten by multiop_bg_pause
9685         local n=$((${fidv[1]} + 1))
9686         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
9687         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
9688                 error "FID is missing before close"
9689         kill -USR1 $multi_pid
9690         # 1 second delay, so if mtime change we will see it
9691         sleep 1
9692         local mtime2=$(stat -c "%Y" $DIR/$tdir)
9693         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
9694 }
9695 run_test 185 "Volatile file support"
9696
9697 # OST pools tests
9698 check_file_in_pool()
9699 {
9700         local file=$1
9701         local pool=$2
9702         local tlist="$3"
9703         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9704         for i in $res
9705         do
9706                 for t in $tlist ; do
9707                         [ "$i" -eq "$t" ] && continue 2
9708                 done
9709
9710                 echo "pool list: $tlist"
9711                 echo "striping: $res"
9712                 error_noexit "$file not allocated in $pool"
9713                 return 1
9714         done
9715         return 0
9716 }
9717
9718 pool_add() {
9719         echo "Creating new pool"
9720         local pool=$1
9721
9722         create_pool $FSNAME.$pool ||
9723                 { error_noexit "No pool created, result code $?"; return 1; }
9724         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9725                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9726 }
9727
9728 pool_add_targets() {
9729         echo "Adding targets to pool"
9730         local pool=$1
9731         local first=$2
9732         local last=$3
9733         local step=${4:-1}
9734
9735         local list=$(seq $first $step $last)
9736
9737         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9738         do_facet mgs $LCTL pool_add \
9739                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9740         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9741                         | sort -u | tr '\n' ' ' " "$t" || { 
9742                 error_noexit "Add to pool failed"
9743                 return 1
9744         }
9745         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9746         local addcount=$(((last - first) / step + 1))
9747         [ $lfscount -eq $addcount ] || {
9748                 error_noexit "lfs pool_list bad ost count" \
9749                                                 "$lfscount != $addcount"
9750                 return 2
9751         }
9752 }
9753
9754 pool_set_dir() {
9755         local pool=$1
9756         local tdir=$2
9757         echo "Setting pool on directory $tdir"
9758
9759         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9760
9761         error_noexit "Cannot set pool $pool to $tdir"
9762         return 1
9763 }
9764
9765 pool_check_dir() {
9766         local pool=$1
9767         local tdir=$2
9768         echo "Checking pool on directory $tdir"
9769
9770         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9771         [ "$res" = "$pool" ] && return 0
9772
9773         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9774         return 1
9775 }
9776
9777 pool_dir_rel_path() {
9778         echo "Testing relative path works well"
9779         local pool=$1
9780         local tdir=$2
9781         local root=$3
9782
9783         mkdir -p $root/$tdir/$tdir
9784         cd $root/$tdir
9785         pool_set_dir $pool $tdir          || return 1
9786         pool_set_dir $pool ./$tdir        || return 2
9787         pool_set_dir $pool ../$tdir       || return 3
9788         pool_set_dir $pool ../$tdir/$tdir || return 4
9789         rm -rf $tdir; cd - > /dev/null
9790 }
9791
9792 pool_alloc_files() {
9793         echo "Checking files allocation from directory pool"
9794         local pool=$1
9795         local tdir=$2
9796         local count=$3
9797         local tlist="$4"
9798
9799         local failed=0
9800         for i in $(seq -w 1 $count)
9801         do
9802                 local file=$tdir/file-$i
9803                 touch $file
9804                 check_file_in_pool $file $pool "$tlist" || \
9805                         failed=$((failed + 1))
9806         done
9807         [ "$failed" = 0 ] && return 0
9808
9809         error_noexit "$failed files not allocated in $pool"
9810         return 1
9811 }
9812
9813 pool_create_files() {
9814         echo "Creating files in pool"
9815         local pool=$1
9816         local tdir=$2
9817         local count=$3
9818         local tlist="$4"
9819
9820         mkdir -p $tdir
9821         local failed=0
9822         for i in $(seq -w 1 $count)
9823         do
9824                 local file=$tdir/spoo-$i
9825                 $SETSTRIPE -p $pool $file
9826                 check_file_in_pool $file $pool "$tlist" || \
9827                         failed=$((failed + 1))
9828         done
9829         [ "$failed" = 0 ] && return 0
9830
9831         error_noexit "$failed files not allocated in $pool"
9832         return 1
9833 }
9834
9835 pool_lfs_df() {
9836         echo "Checking 'lfs df' output"
9837         local pool=$1
9838
9839         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9840                         tr '\n' ' ')
9841         local res=$($LFS df --pool $FSNAME.$pool |
9842                         awk '{print $1}' |
9843                         grep "$FSNAME-OST" |
9844                         tr '\n' ' ')
9845         [ "$res" = "$t" ] && return 0
9846
9847         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9848         return 1
9849 }
9850
9851 pool_file_rel_path() {
9852         echo "Creating files in a pool with relative pathname"
9853         local pool=$1
9854         local tdir=$2
9855
9856         mkdir -p $tdir ||
9857                 { error_noexit "unable to create $tdir"; return 1 ; }
9858         local file="/..$tdir/$tfile-1"
9859         $SETSTRIPE -p $pool $file ||
9860                 { error_noexit "unable to create $file" ; return 2 ; }
9861
9862         cd $tdir
9863         $SETSTRIPE -p $pool $tfile-2 || {
9864                 error_noexit "unable to create $tfile-2 in $tdir"
9865                 return 3
9866         }
9867 }
9868
9869 pool_remove_first_target() {
9870         echo "Removing first target from a pool"
9871         local pool=$1
9872
9873         local pname="lov.$FSNAME-*.pools.$pool"
9874         local t=$($LCTL get_param -n $pname | head -1)
9875         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9876         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9877                 error_noexit "$t not removed from $FSNAME.$pool"
9878                 return 1
9879         }
9880 }
9881
9882 pool_remove_all_targets() {
9883         echo "Removing all targets from pool"
9884         local pool=$1
9885         local file=$2
9886         local pname="lov.$FSNAME-*.pools.$pool"
9887         for t in $($LCTL get_param -n $pname | sort -u)
9888         do
9889                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9890         done
9891         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9892                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9893                 return 1
9894         }
9895         # striping on an empty/nonexistant pool should fall back 
9896         # to "pool of everything"
9897         touch $file || {
9898                 error_noexit "failed to use fallback striping for empty pool"
9899                 return 2
9900         }
9901         # setstripe on an empty pool should fail
9902         $SETSTRIPE -p $pool $file 2>/dev/null && {
9903                 error_noexit "expected failure when creating file" \
9904                                                         "with empty pool"
9905                 return 3
9906         }
9907         return 0
9908 }
9909
9910 pool_remove() {
9911         echo "Destroying pool"
9912         local pool=$1
9913         local file=$2
9914
9915         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9916
9917         sleep 2
9918         # striping on an empty/nonexistant pool should fall back 
9919         # to "pool of everything"
9920         touch $file || {
9921                 error_noexit "failed to use fallback striping for missing pool"
9922                 return 1
9923         }
9924         # setstripe on an empty pool should fail
9925         $SETSTRIPE -p $pool $file 2>/dev/null && {
9926                 error_noexit "expected failure when creating file" \
9927                                                         "with missing pool"
9928                 return 2
9929         }
9930
9931         # get param should return err once pool is gone
9932         if wait_update $HOSTNAME "lctl get_param -n \
9933                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9934         then
9935                 remove_pool_from_list $FSNAME.$pool
9936                 return 0
9937         fi
9938         error_noexit "Pool $FSNAME.$pool is not destroyed"
9939         return 3
9940 }
9941
9942 test_200() {
9943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9944         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9945
9946         local POOL=${POOL:-cea1}
9947         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9948         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9949         # Pool OST targets
9950         local first_ost=0
9951         local last_ost=$(($OSTCOUNT - 1))
9952         local ost_step=2
9953         local ost_list=$(seq $first_ost $ost_step $last_ost)
9954         local ost_range="$first_ost $last_ost $ost_step"
9955         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9956         local file_dir=$POOL_ROOT/file_tst
9957
9958         local rc=0
9959         while : ; do
9960                 # former test_200a test_200b
9961                 pool_add $POOL                          || { rc=$? ; break; }
9962                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9963                 # former test_200c test_200d
9964                 mkdir -p $test_path
9965                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9966                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9967                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9968                                                         || { rc=$? ; break; }
9969                 # former test_200e test_200f
9970                 local files=$((OSTCOUNT*3))
9971                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9972                                                         || { rc=$? ; break; }
9973                 pool_create_files $POOL $file_dir $files "$ost_list" \
9974                                                         || { rc=$? ; break; }
9975                 # former test_200g test_200h
9976                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9977                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9978
9979                 # former test_201a test_201b test_201c
9980                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9981
9982                 local f=$test_path/$tfile
9983                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9984                 pool_remove $POOL $f                    || { rc=$? ; break; }
9985                 break
9986         done
9987
9988         cleanup_pools
9989         return $rc
9990 }
9991 run_test 200 "OST pools"
9992
9993 # usage: default_attr <count | size | offset>
9994 default_attr() {
9995         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9996 }
9997
9998 # usage: check_default_stripe_attr
9999 check_default_stripe_attr() {
10000         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
10001         case $1 in
10002         --stripe-count|--count)
10003                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
10004         --stripe-size|--size)
10005                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
10006         --stripe-index|--index)
10007                 EXPECTED=-1;;
10008         *)
10009                 error "unknown getstripe attr '$1'"
10010         esac
10011
10012         [ $ACTUAL != $EXPECTED ] &&
10013                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
10014 }
10015
10016 test_204a() {
10017         test_mkdir -p $DIR/$tdir
10018         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
10019
10020         check_default_stripe_attr --stripe-count
10021         check_default_stripe_attr --stripe-size
10022         check_default_stripe_attr --stripe-index
10023
10024         return 0
10025 }
10026 run_test 204a "Print default stripe attributes ================="
10027
10028 test_204b() {
10029         test_mkdir -p $DIR/$tdir
10030         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10031
10032         check_default_stripe_attr --stripe-size
10033         check_default_stripe_attr --stripe-index
10034
10035         return 0
10036 }
10037 run_test 204b "Print default stripe size and offset  ==========="
10038
10039 test_204c() {
10040         test_mkdir -p $DIR/$tdir
10041         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10042
10043         check_default_stripe_attr --stripe-count
10044         check_default_stripe_attr --stripe-index
10045
10046         return 0
10047 }
10048 run_test 204c "Print default stripe count and offset ==========="
10049
10050 test_204d() {
10051         test_mkdir -p $DIR/$tdir
10052         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10053
10054         check_default_stripe_attr --stripe-count
10055         check_default_stripe_attr --stripe-size
10056
10057         return 0
10058 }
10059 run_test 204d "Print default stripe count and size ============="
10060
10061 test_204e() {
10062         test_mkdir -p $DIR/$tdir
10063         $SETSTRIPE -d $DIR/$tdir
10064
10065         check_default_stripe_attr --stripe-count --raw
10066         check_default_stripe_attr --stripe-size --raw
10067         check_default_stripe_attr --stripe-index --raw
10068
10069         return 0
10070 }
10071 run_test 204e "Print raw stripe attributes ================="
10072
10073 test_204f() {
10074         test_mkdir -p $DIR/$tdir
10075         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10076
10077         check_default_stripe_attr --stripe-size --raw
10078         check_default_stripe_attr --stripe-index --raw
10079
10080         return 0
10081 }
10082 run_test 204f "Print raw stripe size and offset  ==========="
10083
10084 test_204g() {
10085         test_mkdir -p $DIR/$tdir
10086         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10087
10088         check_default_stripe_attr --stripe-count --raw
10089         check_default_stripe_attr --stripe-index --raw
10090
10091         return 0
10092 }
10093 run_test 204g "Print raw stripe count and offset ==========="
10094
10095 test_204h() {
10096         test_mkdir -p $DIR/$tdir
10097         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10098
10099         check_default_stripe_attr --stripe-count --raw
10100         check_default_stripe_attr --stripe-size --raw
10101
10102         return 0
10103 }
10104 run_test 204h "Print raw stripe count and size ============="
10105
10106 # Figure out which job scheduler is being used, if any,
10107 # or use a fake one
10108 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
10109         JOBENV=SLURM_JOB_ID
10110 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
10111         JOBENV=LSB_JOBID
10112 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
10113         JOBENV=PBS_JOBID
10114 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
10115         JOBENV=LOADL_STEP_ID
10116 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
10117         JOBENV=JOB_ID
10118 else
10119         JOBENV=FAKE_JOBID
10120 fi
10121
10122 verify_jobstats() {
10123         local cmd=$1
10124         local target=$2
10125
10126         # clear old jobstats
10127         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
10128         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
10129
10130         # use a new JobID for this test, or we might see an old one
10131         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
10132
10133         JOBVAL=${!JOBENV}
10134         log "Test: $cmd"
10135         log "Using JobID environment variable $JOBENV=$JOBVAL"
10136
10137         if [ $JOBENV = "FAKE_JOBID" ]; then
10138                 FAKE_JOBID=$JOBVAL $cmd
10139         else
10140                 $cmd
10141         fi
10142
10143         if [ "$target" = "mdt" -o "$target" = "both" ]; then
10144                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
10145                 do_facet $FACET lctl get_param mdt.*.job_stats |
10146                         grep $JOBVAL || error "No job stats found on MDT $FACET"
10147         fi
10148         if [ "$target" = "ost" -o "$target" = "both" ]; then
10149                 FACET=ost1
10150                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
10151                         grep $JOBVAL || error "No job stats found on OST $FACET"
10152         fi
10153 }
10154
10155 jobstats_set() {
10156         trap 0
10157         NEW_JOBENV=${1:-$OLD_JOBENV}
10158         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
10159         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
10160 }
10161
10162 test_205() { # Job stats
10163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10164         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
10165                 skip "Server doesn't support jobstats" && return 0
10166
10167         local cmd
10168         OLD_JOBENV=`$LCTL get_param -n jobid_var`
10169         if [ $OLD_JOBENV != $JOBENV ]; then
10170                 jobstats_set $JOBENV
10171                 trap jobstats_set EXIT
10172         fi
10173
10174         # mkdir
10175         cmd="mkdir $DIR/$tfile"
10176         verify_jobstats "$cmd" "mdt"
10177         # rmdir
10178         cmd="rm -fr $DIR/$tfile"
10179         verify_jobstats "$cmd" "mdt"
10180         # mknod
10181         cmd="mknod $DIR/$tfile c 1 3"
10182         verify_jobstats "$cmd" "mdt"
10183         # unlink
10184         cmd="rm -f $DIR/$tfile"
10185         verify_jobstats "$cmd" "mdt"
10186         # open & close
10187         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
10188         verify_jobstats "$cmd" "mdt"
10189         # setattr
10190         cmd="touch $DIR/$tfile"
10191         verify_jobstats "$cmd" "both"
10192         # write
10193         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
10194         verify_jobstats "$cmd" "ost"
10195         # read
10196         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
10197         verify_jobstats "$cmd" "ost"
10198         # truncate
10199         cmd="$TRUNCATE $DIR/$tfile 0"
10200         verify_jobstats "$cmd" "both"
10201         # rename
10202         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
10203         verify_jobstats "$cmd" "mdt"
10204
10205         # cleanup
10206         rm -f $DIR/jobstats_test_rename
10207
10208         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
10209 }
10210 run_test 205 "Verify job stats"
10211
10212 # LU-1480, LU-1773 and LU-1657
10213 test_206() {
10214         mkdir -p $DIR/$tdir
10215         lfs setstripe -c -1 $DIR/$tdir
10216 #define OBD_FAIL_LOV_INIT 0x1403
10217         $LCTL set_param fail_loc=0xa0001403
10218         $LCTL set_param fail_val=1
10219         touch $DIR/$tdir/$tfile || true
10220 }
10221 run_test 206 "fail lov_init_raid0() doesn't lbug"
10222
10223 test_207a() {
10224         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10225         local fsz=`stat -c %s $DIR/$tfile`
10226         cancel_lru_locks mdc
10227
10228         # do not return layout in getattr intent
10229 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
10230         $LCTL set_param fail_loc=0x170
10231         local sz=`stat -c %s $DIR/$tfile`
10232
10233         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
10234
10235         rm -rf $DIR/$tfile
10236 }
10237 run_test 207a "can refresh layout at glimpse"
10238
10239 test_207b() {
10240         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10241         local cksum=`md5sum $DIR/$tfile`
10242         local fsz=`stat -c %s $DIR/$tfile`
10243         cancel_lru_locks mdc
10244         cancel_lru_locks osc
10245
10246         # do not return layout in getattr intent
10247 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
10248         $LCTL set_param fail_loc=0x171
10249
10250         # it will refresh layout after the file is opened but before read issues
10251         echo checksum is "$cksum"
10252         echo "$cksum" |md5sum -c --quiet || error "file differs"
10253
10254         rm -rf $DIR/$tfile
10255 }
10256 run_test 207b "can refresh layout at open"
10257
10258 test_212() {
10259         size=`date +%s`
10260         size=$((size % 8192 + 1))
10261         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
10262         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
10263         rm -f $DIR/f212 $DIR/f212.xyz
10264 }
10265 run_test 212 "Sendfile test ============================================"
10266
10267 test_213() {
10268         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
10269         cancel_lru_locks osc
10270         lctl set_param fail_loc=0x8000040f
10271         # generate a read lock
10272         cat $DIR/$tfile > /dev/null
10273         # write to the file, it will try to cancel the above read lock.
10274         cat /etc/hosts >> $DIR/$tfile
10275 }
10276 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
10277
10278 test_214() { # for bug 20133
10279         test_mkdir -p $DIR/d214p/d214c
10280         for (( i=0; i < 340; i++ )) ; do
10281                 touch $DIR/d214p/d214c/a$i
10282         done
10283
10284         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
10285         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
10286         ls $DIR/d214c || error "ls $DIR/d214c failed"
10287         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
10288 }
10289 run_test 214 "hash-indexed directory test - bug 20133"
10290
10291 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
10292 create_lnet_proc_files() {
10293         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
10294         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
10295
10296         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
10297         rm -f "$TMP/lnet_$1.sys_tmp"
10298 }
10299
10300 # counterpart of create_lnet_proc_files
10301 remove_lnet_proc_files() {
10302         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
10303 }
10304
10305 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10306 # 3rd arg as regexp for body
10307 check_lnet_proc_stats() {
10308         local l=$(cat "$TMP/lnet_$1" |wc -l)
10309         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
10310
10311         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
10312 }
10313
10314 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10315 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
10316 # optional and can be regexp for 2nd line (lnet.routes case)
10317 check_lnet_proc_entry() {
10318         local blp=2            # blp stands for 'position of 1st line of body'
10319         [ "$5" = "" ] || blp=3 # lnet.routes case
10320
10321         local l=$(cat "$TMP/lnet_$1" |wc -l)
10322         # subtracting one from $blp because the body can be empty
10323         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
10324
10325         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
10326                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
10327
10328         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
10329                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
10330
10331         # bail out if any unexpected line happened
10332         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
10333         [ "$?" != 0 ] || error "$2 misformatted"
10334 }
10335
10336 test_215() { # for bugs 18102, 21079, 21517
10337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10338         local N='(0|[1-9][0-9]*)'       # non-negative numeric
10339         local P='[1-9][0-9]*'           # positive numeric
10340         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
10341         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
10342         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
10343         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
10344
10345         local L1 # regexp for 1st line
10346         local L2 # regexp for 2nd line (optional)
10347         local BR # regexp for the rest (body)
10348
10349         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
10350         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
10351         create_lnet_proc_files "stats"
10352         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
10353         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
10354         remove_lnet_proc_files "stats"
10355
10356         # /proc/sys/lnet/routes should look like this:
10357         # Routing disabled/enabled
10358         # net hops state router
10359         # where net is a string like tcp0, hops >= 0, state is up/down,
10360         # router is a string like 192.168.1.1@tcp2
10361         L1="^Routing (disabled|enabled)$"
10362         L2="^net +hops +state +router$"
10363         BR="^$NET +$N +(up|down) +$NID$"
10364         create_lnet_proc_files "routes"
10365         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
10366         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
10367         remove_lnet_proc_files "routes"
10368
10369         # /proc/sys/lnet/routers should look like this:
10370         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
10371         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
10372         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
10373         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
10374         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
10375         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
10376         create_lnet_proc_files "routers"
10377         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
10378         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
10379         remove_lnet_proc_files "routers"
10380
10381         # /proc/sys/lnet/peers should look like this:
10382         # nid refs state last max rtr min tx min queue
10383         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
10384         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
10385         # numeric (0 or >0 or <0), queue >= 0.
10386         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
10387         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
10388         create_lnet_proc_files "peers"
10389         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
10390         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
10391         remove_lnet_proc_files "peers"
10392
10393         # /proc/sys/lnet/buffers  should look like this:
10394         # pages count credits min
10395         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
10396         L1="^pages +count +credits +min$"
10397         BR="^ +$N +$N +$I +$I$"
10398         create_lnet_proc_files "buffers"
10399         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
10400         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
10401         remove_lnet_proc_files "buffers"
10402
10403         # /proc/sys/lnet/nis should look like this:
10404         # nid status alive refs peer rtr max tx min
10405         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
10406         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
10407         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
10408         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
10409         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
10410         create_lnet_proc_files "nis"
10411         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
10412         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
10413         remove_lnet_proc_files "nis"
10414
10415         # can we successfully write to /proc/sys/lnet/stats?
10416         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
10417         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
10418 }
10419 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
10420
10421 test_216() { # bug 20317
10422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10423         remote_ost_nodsh && skip "remote OST with nodsh" && return
10424         local node
10425         local p="$TMP/sanityN-$TESTNAME.parameters"
10426         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
10427         for node in $(osts_nodes); do
10428                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
10429                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
10430                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
10431         done
10432         clear_osc_stats
10433
10434         # agressive lockless i/o settings
10435         for node in $(osts_nodes); do
10436                 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'
10437         done
10438         lctl set_param -n osc.*.contention_seconds 60
10439
10440         $DIRECTIO write $DIR/$tfile 0 10 4096
10441         $CHECKSTAT -s 40960 $DIR/$tfile
10442
10443         # disable lockless i/o
10444         for node in $(osts_nodes); do
10445                 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'
10446         done
10447         lctl set_param -n osc.*.contention_seconds 0
10448         clear_osc_stats
10449
10450         dd if=/dev/zero of=$DIR/$tfile count=0
10451         $CHECKSTAT -s 0 $DIR/$tfile
10452
10453         restore_lustre_params <$p
10454         rm -f $p
10455         rm $DIR/$tfile
10456 }
10457 run_test 216 "check lockless direct write works and updates file size and kms correctly"
10458
10459 test_217() { # bug 22430
10460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10461         local node
10462         local nid
10463
10464         for node in $(nodes_list); do
10465                 nid=$(host_nids_address $node $NETTYPE)
10466                 if [[ $nid = *-* ]] ; then
10467                         echo "lctl ping $nid@$NETTYPE"
10468                         lctl ping $nid@$NETTYPE
10469                 else
10470                         echo "skipping $node (no hyphen detected)"
10471                 fi
10472         done
10473 }
10474 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
10475
10476 test_218() {
10477        # do directio so as not to populate the page cache
10478        log "creating a 10 Mb file"
10479        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10480        log "starting reads"
10481        dd if=$DIR/$tfile of=/dev/null bs=4096 &
10482        log "truncating the file"
10483        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10484        log "killing dd"
10485        kill %+ || true # reads might have finished
10486        echo "wait until dd is finished"
10487        wait
10488        log "removing the temporary file"
10489        rm -rf $DIR/$tfile || error "tmp file removal failed"
10490 }
10491 run_test 218 "parallel read and truncate should not deadlock ======================="
10492
10493 test_219() {
10494         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10495         # write one partial page
10496         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
10497         # set no grant so vvp_io_commit_write will do sync write
10498         $LCTL set_param fail_loc=0x411
10499         # write a full page at the end of file
10500         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
10501
10502         $LCTL set_param fail_loc=0
10503         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
10504         $LCTL set_param fail_loc=0x411
10505         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
10506 }
10507 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
10508
10509 test_220() { #LU-325
10510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10511         remote_ost_nodsh && skip "remote OST with nodsh" && return
10512         local OSTIDX=0
10513
10514         test_mkdir -p $DIR/$tdir
10515         local OST=$(lfs osts | grep ${OSTIDX}": " | \
10516                 awk '{print $2}' | sed -e 's/_UUID$//')
10517
10518         # on the mdt's osc
10519         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
10520         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
10521                         osc.$mdtosc_proc1.prealloc_last_id)
10522         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
10523                         osc.$mdtosc_proc1.prealloc_next_id)
10524
10525         $LFS df -i
10526
10527         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
10528         #define OBD_FAIL_OST_ENOINO              0x229
10529         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
10530         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
10531         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
10532
10533         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
10534
10535         MDSOBJS=$((last_id - next_id))
10536         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
10537
10538         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
10539         echo "OST still has $count kbytes free"
10540
10541         echo "create $MDSOBJS files @next_id..."
10542         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
10543
10544         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10545                         osc.$mdtosc_proc1.prealloc_last_id)
10546         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10547                         osc.$mdtosc_proc1.prealloc_next_id)
10548
10549         echo "after creation, last_id=$last_id2, next_id=$next_id2"
10550         $LFS df -i
10551
10552         echo "cleanup..."
10553
10554         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
10555         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
10556
10557         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
10558         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
10559         echo "unlink $MDSOBJS files @$next_id..."
10560         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
10561 }
10562 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
10563
10564 test_221() {
10565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10566         dd if=`which date` of=$MOUNT/date oflag=sync
10567         chmod +x $MOUNT/date
10568
10569         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
10570         $LCTL set_param fail_loc=0x80001401
10571
10572         $MOUNT/date > /dev/null
10573         rm -f $MOUNT/date
10574 }
10575 run_test 221 "make sure fault and truncate race to not cause OOM"
10576
10577 test_222a () {
10578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10579        rm -rf $DIR/$tdir
10580        test_mkdir -p $DIR/$tdir
10581        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10582        createmany -o $DIR/$tdir/$tfile 10
10583        cancel_lru_locks mdc
10584        cancel_lru_locks osc
10585        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10586        $LCTL set_param fail_loc=0x31a
10587        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
10588        $LCTL set_param fail_loc=0
10589        rm -r $DIR/$tdir
10590 }
10591 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
10592
10593 test_222b () {
10594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10595        rm -rf $DIR/$tdir
10596        test_mkdir -p $DIR/$tdir
10597        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10598        createmany -o $DIR/$tdir/$tfile 10
10599        cancel_lru_locks mdc
10600        cancel_lru_locks osc
10601        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10602        $LCTL set_param fail_loc=0x31a
10603        rm -r $DIR/$tdir || "AGL for rmdir failed"
10604        $LCTL set_param fail_loc=0
10605 }
10606 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
10607
10608 test_223 () {
10609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10610        rm -rf $DIR/$tdir
10611        test_mkdir -p $DIR/$tdir
10612        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10613        createmany -o $DIR/$tdir/$tfile 10
10614        cancel_lru_locks mdc
10615        cancel_lru_locks osc
10616        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
10617        $LCTL set_param fail_loc=0x31b
10618        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
10619        $LCTL set_param fail_loc=0
10620        rm -r $DIR/$tdir
10621 }
10622 run_test 223 "osc reenqueue if without AGL lock granted ======================="
10623
10624 test_224a() { # LU-1039, MRP-303
10625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10626         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
10627         $LCTL set_param fail_loc=0x508
10628         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
10629         $LCTL set_param fail_loc=0
10630         df $DIR
10631 }
10632 run_test 224a "Don't panic on bulk IO failure"
10633
10634 test_224b() { # LU-1039, MRP-303
10635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10636         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
10637         cancel_lru_locks osc
10638         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
10639         $LCTL set_param fail_loc=0x515
10640         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
10641         $LCTL set_param fail_loc=0
10642         df $DIR
10643 }
10644 run_test 224b "Don't panic on bulk IO failure"
10645
10646 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
10647 test_225a () {
10648         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10649        if [ -z ${MDSSURVEY} ]; then
10650               skip_env "mds-survey not found" && return
10651        fi
10652        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10653             { skip "Need MDS version at least 2.2.51"; return; }
10654
10655        local mds=$(facet_host $SINGLEMDS)
10656        local target=$(do_nodes $mds 'lctl dl' | \
10657                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10658
10659        local cmd1="file_count=1000 thrhi=4"
10660        local cmd2="dir_count=2 layer=mdd stripe_count=0"
10661        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10662        local cmd="$cmd1 $cmd2 $cmd3"
10663
10664        rm -f ${TMP}/mds_survey*
10665        echo + $cmd
10666        eval $cmd || error "mds-survey with zero-stripe failed"
10667        cat ${TMP}/mds_survey*
10668        rm -f ${TMP}/mds_survey*
10669 }
10670 run_test 225a "Metadata survey sanity with zero-stripe"
10671
10672 test_225b () {
10673         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10674        if [ -z ${MDSSURVEY} ]; then
10675               skip_env "mds-survey not found" && return
10676        fi
10677        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10678             { skip "Need MDS version at least 2.2.51"; return; }
10679
10680        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
10681               skip_env "Need to mount OST to test" && return
10682        fi
10683
10684        local mds=$(facet_host $SINGLEMDS)
10685        local target=$(do_nodes $mds 'lctl dl' | \
10686                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10687
10688        local cmd1="file_count=1000 thrhi=4"
10689        local cmd2="dir_count=2 layer=mdd stripe_count=1"
10690        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10691        local cmd="$cmd1 $cmd2 $cmd3"
10692
10693        rm -f ${TMP}/mds_survey*
10694        echo + $cmd
10695        eval $cmd || error "mds-survey with stripe_count failed"
10696        cat ${TMP}/mds_survey*
10697        rm -f ${TMP}/mds_survey*
10698 }
10699 run_test 225b "Metadata survey sanity with stripe_count = 1"
10700
10701 mcreate_path2fid () {
10702         local mode=$1
10703         local major=$2
10704         local minor=$3
10705         local name=$4
10706         local desc=$5
10707         local path=$DIR/$tdir/$name
10708         local fid
10709         local rc
10710         local fid_path
10711
10712         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10713                 error "cannot create $desc"
10714
10715         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
10716         rc=$?
10717         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10718
10719         fid_path=$($LFS fid2path $MOUNT $fid)
10720         rc=$?
10721         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10722
10723         [ "$path" == "$fid_path" ] ||
10724                 error "fid2path returned $fid_path, expected $path"
10725
10726         echo "pass with $path and $fid"
10727 }
10728
10729 test_226a () {
10730         rm -rf $DIR/$tdir
10731         mkdir -p $DIR/$tdir
10732
10733         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10734         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10735         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10736         mcreate_path2fid 0040666 0 0 dir "directory"
10737         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10738         mcreate_path2fid 0100666 0 0 file "regular file"
10739         mcreate_path2fid 0120666 0 0 link "symbolic link"
10740         mcreate_path2fid 0140666 0 0 sock "socket"
10741 }
10742 run_test 226a "call path2fid and fid2path on files of all type"
10743
10744 test_226b () {
10745         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10746         rm -rf $DIR/$tdir
10747         local MDTIDX=1
10748
10749         mkdir -p $DIR/$tdir
10750         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
10751                 error "create remote directory failed"
10752         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
10753         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
10754                                 "character special file (null)"
10755         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
10756                                 "character special file (no device)"
10757         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
10758         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
10759                                 "block special file (loop)"
10760         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
10761         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
10762         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
10763 }
10764 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
10765
10766 # LU-1299 Executing or running ldd on a truncated executable does not
10767 # cause an out-of-memory condition.
10768 test_227() {
10769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10770         dd if=`which date` of=$MOUNT/date bs=1k count=1
10771         chmod +x $MOUNT/date
10772
10773         $MOUNT/date > /dev/null
10774         ldd $MOUNT/date > /dev/null
10775         rm -f $MOUNT/date
10776 }
10777 run_test 227 "running truncated executable does not cause OOM"
10778
10779 # LU-1512 try to reuse idle OI blocks
10780 test_228a() {
10781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10782         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10783                 skip "non-ldiskfs backend" && return
10784
10785         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10786         local myDIR=$DIR/$tdir
10787
10788         mkdir -p $myDIR
10789         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10790         $LCTL set_param fail_loc=0x80001002
10791         createmany -o $myDIR/t- 10000
10792         $LCTL set_param fail_loc=0
10793         # The guard is current the largest FID holder
10794         touch $myDIR/guard
10795         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10796                     tr -d '[')
10797         local IDX=$(($SEQ % 64))
10798
10799         do_facet $SINGLEMDS sync
10800         # Make sure journal flushed.
10801         sleep 6
10802         local blk1=$(do_facet $SINGLEMDS \
10803                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10804                      grep Blockcount | awk '{print $4}')
10805
10806         # Remove old files, some OI blocks will become idle.
10807         unlinkmany $myDIR/t- 10000
10808         # Create new files, idle OI blocks should be reused.
10809         createmany -o $myDIR/t- 2000
10810         do_facet $SINGLEMDS sync
10811         # Make sure journal flushed.
10812         sleep 6
10813         local blk2=$(do_facet $SINGLEMDS \
10814                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10815                      grep Blockcount | awk '{print $4}')
10816
10817         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10818 }
10819 run_test 228a "try to reuse idle OI blocks"
10820
10821 test_228b() {
10822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10823         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10824                 skip "non-ldiskfs backend" && return
10825
10826         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10827         local myDIR=$DIR/$tdir
10828
10829         mkdir -p $myDIR
10830         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10831         $LCTL set_param fail_loc=0x80001002
10832         createmany -o $myDIR/t- 10000
10833         $LCTL set_param fail_loc=0
10834         # The guard is current the largest FID holder
10835         touch $myDIR/guard
10836         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10837                     tr -d '[')
10838         local IDX=$(($SEQ % 64))
10839
10840         do_facet $SINGLEMDS sync
10841         # Make sure journal flushed.
10842         sleep 6
10843         local blk1=$(do_facet $SINGLEMDS \
10844                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10845                      grep Blockcount | awk '{print $4}')
10846
10847         # Remove old files, some OI blocks will become idle.
10848         unlinkmany $myDIR/t- 10000
10849
10850         # stop the MDT
10851         stop $SINGLEMDS || error "Fail to stop MDT."
10852         # remount the MDT
10853         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10854
10855         df $MOUNT || error "Fail to df."
10856         # Create new files, idle OI blocks should be reused.
10857         createmany -o $myDIR/t- 2000
10858         do_facet $SINGLEMDS sync
10859         # Make sure journal flushed.
10860         sleep 6
10861         local blk2=$(do_facet $SINGLEMDS \
10862                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10863                      grep Blockcount | awk '{print $4}')
10864
10865         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10866 }
10867 run_test 228b "idle OI blocks can be reused after MDT restart"
10868
10869 #LU-1881
10870 test_228c() {
10871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10872         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10873                 skip "non-ldiskfs backend" && return
10874
10875         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10876         local myDIR=$DIR/$tdir
10877
10878         mkdir -p $myDIR
10879         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10880         $LCTL set_param fail_loc=0x80001002
10881         # 20000 files can guarantee there are index nodes in the OI file
10882         createmany -o $myDIR/t- 20000
10883         $LCTL set_param fail_loc=0
10884         # The guard is current the largest FID holder
10885         touch $myDIR/guard
10886         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10887                     tr -d '[')
10888         local IDX=$(($SEQ % 64))
10889
10890         do_facet $SINGLEMDS sync
10891         # Make sure journal flushed.
10892         sleep 6
10893         local blk1=$(do_facet $SINGLEMDS \
10894                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10895                      grep Blockcount | awk '{print $4}')
10896
10897         # Remove old files, some OI blocks will become idle.
10898         unlinkmany $myDIR/t- 20000
10899         rm -f $myDIR/guard
10900         # The OI file should become empty now
10901
10902         # Create new files, idle OI blocks should be reused.
10903         createmany -o $myDIR/t- 2000
10904         do_facet $SINGLEMDS sync
10905         # Make sure journal flushed.
10906         sleep 6
10907         local blk2=$(do_facet $SINGLEMDS \
10908                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10909                      grep Blockcount | awk '{print $4}')
10910
10911         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10912 }
10913 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10914
10915 test_230a() {
10916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10917         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10918         local MDTIDX=1
10919
10920         mkdir -p $DIR/$tdir/test_230_local
10921         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10922         [ $mdt_idx -ne 0 ] &&
10923                 error "create local directory on wrong MDT $mdt_idx"
10924
10925         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10926                         error "create remote directory failed"
10927         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10928         [ $mdt_idx -ne $MDTIDX ] &&
10929                 error "create remote directory on wrong MDT $mdt_idx"
10930
10931         createmany -o $DIR/$tdir/test_230/t- 10 ||
10932                 error "create files on remote directory failed"
10933         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10934         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10935         rm -r $DIR/$tdir || error "unlink remote directory failed"
10936 }
10937 run_test 230a "Create remote directory and files under the remote directory"
10938
10939 test_230b() {
10940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10941         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10942         local MDTIDX=1
10943         local remote_dir=$DIR/$tdir/remote_dir
10944         local rc=0
10945
10946         mkdir -p $DIR/$tdir
10947         $LFS mkdir -i $MDTIDX $remote_dir ||
10948                 error "create remote directory failed"
10949
10950         $LFS mkdir -i 0 $remote_dir/new_dir &&
10951                 error "nested remote directory create succeed!"
10952
10953         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=1
10954         $LFS mkdir -i 0 $remote_dir/new_dir || rc=$?
10955         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=0
10956
10957         [ $rc -ne 0 ] &&
10958            error "create remote directory failed after set enable_remote_dir"
10959
10960         rm -rf $remote_dir || error "first unlink remote directory failed"
10961
10962         $RUNAS -G$RUNAS_GID $LFS mkdir -i $MDTIDX $DIR/$tfile &&
10963                                                         error "chown worked"
10964
10965         do_facet mds$MDTIDX lctl set_param \
10966                                 mdt.*.enable_remote_dir_gid=$RUNAS_GID
10967         $LFS mkdir -i $MDTIDX $remote_dir || rc=$?
10968         do_facet mds$MDTIDX lctl set_param mdt.*.enable_remote_dir_gid=0
10969
10970         [ $rc -ne 0 ] &&
10971            error "create remote dir failed after set enable_remote_dir_gid"
10972
10973         rm -r $DIR/$tdir || error "second unlink remote directory failed"
10974 }
10975 run_test 230b "nested remote directory should be failed"
10976
10977 test_231a()
10978 {
10979         # For simplicity this test assumes that max_pages_per_rpc
10980         # is the same across all OSCs
10981         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
10982         local bulk_size=$((max_pages * 4096))
10983
10984         mkdir -p $DIR/$tdir
10985
10986         # clear the OSC stats
10987         $LCTL set_param osc.*.stats=0 &>/dev/null
10988
10989         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
10990         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
10991                 oflag=direct &>/dev/null || error "dd failed"
10992
10993         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
10994         if [ x$nrpcs != "x1" ]; then
10995                 error "found $nrpc ost_write RPCs, not 1 as expected"
10996         fi
10997
10998         # Drop the OSC cache, otherwise we will read from it
10999         cancel_lru_locks osc
11000
11001         # clear the OSC stats
11002         $LCTL set_param osc.*.stats=0 &>/dev/null
11003
11004         # Client reads $bulk_size.
11005         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
11006                 iflag=direct &>/dev/null || error "dd failed"
11007
11008         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
11009         if [ x$nrpcs != "x1" ]; then
11010                 error "found $nrpc ost_read RPCs, not 1 as expected"
11011         fi
11012 }
11013 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
11014
11015 test_231b() {
11016         mkdir -p $DIR/$tdir
11017         local i
11018         for i in {0..1023}; do
11019                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
11020                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
11021                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
11022         done
11023         sync
11024 }
11025 run_test 231b "must not assert on fully utilized OST request buffer"
11026
11027 test_232() {
11028         mkdir -p $DIR/$tdir
11029         #define OBD_FAIL_LDLM_OST_LVB            0x31c
11030         $LCTL set_param fail_loc=0x31c
11031
11032         # ignore dd failure
11033         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
11034
11035         $LCTL set_param fail_loc=0
11036         umount_client $MOUNT || error "umount failed"
11037         mount_client $MOUNT || error "mount failed"
11038 }
11039 run_test 232 "failed lock should not block umount"
11040
11041 #
11042 # tests that do cleanup/setup should be run at the end
11043 #
11044
11045 test_900() {
11046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11047         local ls
11048         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
11049         $LCTL set_param fail_loc=0x903
11050         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
11051         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
11052                 echo "clear" > $ls
11053         done
11054         FAIL_ON_ERROR=true cleanup
11055         FAIL_ON_ERROR=true setup
11056 }
11057 run_test 900 "umount should not race with any mgc requeue thread"
11058
11059 complete $SECONDS
11060 check_and_cleanup_lustre
11061 if [ "$I_MOUNTED" != "yes" ]; then
11062         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
11063 fi
11064 exit_status