Whamcloud - gitweb
4a0ebee3326b452102ecd534c105566ad3b97f87
[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_0a() {
145         touch $DIR/$tfile
146         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
147         rm $DIR/$tfile
148         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
149 }
150 run_test 0a "touch; rm ====================="
151
152 test_0b() {
153         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
154         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
155 }
156 run_test 0b "chmod 0755 $DIR ============================="
157
158 test_0c() {
159         $LCTL get_param mdc.*.import | grep "state: FULL" ||
160                 error "import not FULL"
161         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
162                 error "bad target"
163 }
164 run_test 0c "check import proc ============================="
165
166 test_1() {
167         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
168         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
169         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
170         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
171         rmdir $DIR/$tdir/d2
172         rmdir $DIR/$tdir
173         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
174 }
175 run_test 1 "mkdir; remkdir; rmdir =============================="
176
177 test_2() {
178         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
179         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
180         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
181         rm -r $DIR/$tdir
182         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
183 }
184 run_test 2 "mkdir; touch; rmdir; check file ===================="
185
186 test_3() {
187         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
188         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
189         touch $DIR/$tdir/$tfile
190         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
191         rm -r $DIR/$tdir
192         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
193 }
194 run_test 3 "mkdir; touch; rmdir; check dir ====================="
195
196 test_5() {
197         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
198         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
199         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
200         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
201         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
202 }
203 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
204
205 test_6a() {
206         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
207         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
208         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
209                 error "$tfile does not have perm 0666 or UID $UID"
210         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
211         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
212                 error "$tfile should be 0666 and owned by UID $UID"
213 }
214 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
215
216 test_6c() {
217         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
218         touch $DIR/$tfile
219         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
220         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
221                 error "$tfile should be owned by UID $RUNAS_ID"
222         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
223         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
224                 error "$tfile should be owned by UID $RUNAS_ID"
225 }
226 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
227
228 test_6e() {
229         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
230         touch $DIR/$tfile
231         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
232         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
233                 error "$tfile should be owned by GID $UID"
234         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
235         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
236                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
237 }
238 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
239
240 test_6g() {
241         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
242         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
243         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
244         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
245         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
246         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
247         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
248                 error "$tdir/d/subdir should be GID $RUNAS_GID"
249 }
250 run_test 6g "Is new dir in sgid dir inheriting group?"
251
252 test_6h() { # bug 7331
253         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
254         touch $DIR/$tfile || error "touch failed"
255         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
256         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
257                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
258         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
259                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
260 }
261 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
262
263 test_7a() {
264         test_mkdir $DIR/$tdir
265         $MCREATE $DIR/$tdir/$tfile
266         chmod 0666 $DIR/$tdir/$tfile
267         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
268                 error "$tdir/$tfile should be mode 0666"
269 }
270 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
271
272 test_7b() {
273         if [ ! -d $DIR/$tdir ]; then
274                 mkdir $DIR/$tdir
275         fi
276         $MCREATE $DIR/$tdir/$tfile
277         echo -n foo > $DIR/$tdir/$tfile
278         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
279         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
280 }
281 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
282
283 test_8() {
284         test_mkdir $DIR/$tdir
285         touch $DIR/$tdir/$tfile
286         chmod 0666 $DIR/$tdir/$tfile
287         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
288                 error "$tfile mode not 0666"
289 }
290 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
291
292 test_9() {
293         test_mkdir $DIR/$tdir
294         test_mkdir $DIR/$tdir/d2
295         test_mkdir $DIR/$tdir/d2/d3
296         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
297 }
298 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
299
300 test_10() {
301         test_mkdir $DIR/$tdir
302         test_mkdir $DIR/$tdir/d2
303         touch $DIR/$tdir/d2/$tfile
304         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
305                 error "$tdir/d2/$tfile not a file"
306 }
307 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
308
309 test_11() {
310         test_mkdir $DIR/$tdir
311         test_mkdir $DIR/$tdir/d2
312         chmod 0666 $DIR/$tdir/d2
313         chmod 0705 $DIR/$tdir/d2
314         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
315                 error "$tdir/d2 mode not 0705"
316 }
317 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
318
319 test_12() {
320         test_mkdir $DIR/$tdir
321         touch $DIR/$tdir/$tfile
322         chmod 0666 $DIR/$tdir/$tfile
323         chmod 0654 $DIR/$tdir/$tfile
324         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
325                 error "$tdir/d2 mode not 0654"
326 }
327 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
328
329 test_13() {
330         test_mkdir $DIR/$tdir
331         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
332         >  $DIR/$tdir/$tfile
333         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
334                 error "$tdir/$tfile size not 0 after truncate"
335 }
336 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
337
338 test_14() {
339         test_mkdir $DIR/$tdir
340         touch $DIR/$tdir/$tfile
341         rm $DIR/$tdir/$tfile
342         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
343 }
344 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
345
346 test_15() {
347         test_mkdir $DIR/$tdir
348         touch $DIR/$tdir/$tfile
349         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
350         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
351                 error "$tdir/${tfile_2} not a file after rename"
352 }
353 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
354
355 test_16() {
356         test_mkdir $DIR/$tdir
357         touch $DIR/$tdir/$tfile
358         rm -rf $DIR/$tdir/$tfile
359         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
360 }
361 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
362
363 test_17a() {
364         test_mkdir -p $DIR/$tdir
365         touch $DIR/$tdir/$tfile
366         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
367         ls -l $DIR/$tdir
368         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
369                 error "$tdir/l-exist not a symlink"
370         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
371                 error "$tdir/l-exist not referencing a file"
372         rm -f $DIR/$tdir/l-exist
373         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
374 }
375 run_test 17a "symlinks: create, remove (real) =================="
376
377 test_17b() {
378         test_mkdir -p $DIR/$tdir
379         ln -s no-such-file $DIR/$tdir/l-dangle
380         ls -l $DIR/$tdir
381         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
382                 error "$tdir/l-dangle not referencing no-such-file"
383         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
384                 error "$tdir/l-dangle not referencing non-existent file"
385         rm -f $DIR/$tdir/l-dangle
386         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
387 }
388 run_test 17b "symlinks: create, remove (dangling) =============="
389
390 test_17c() { # bug 3440 - don't save failed open RPC for replay
391         test_mkdir -p $DIR/$tdir
392         ln -s foo $DIR/$tdir/$tfile
393         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
394 }
395 run_test 17c "symlinks: open dangling (should return error) ===="
396
397 test_17d() {
398         test_mkdir -p $DIR/$tdir
399         ln -s foo $DIR/$tdir/$tfile
400         touch $DIR/$tdir/$tfile || error "creating to new symlink"
401 }
402 run_test 17d "symlinks: create dangling ========================"
403
404 test_17e() {
405         test_mkdir -p $DIR/$tdir
406         local foo=$DIR/$tdir/$tfile
407         ln -s $foo $foo || error "create symlink failed"
408         ls -l $foo || error "ls -l failed"
409         ls $foo && error "ls not failed" || true
410 }
411 run_test 17e "symlinks: create recursive symlink (should return error) ===="
412
413 test_17f() {
414         test_mkdir -p $DIR/d17f
415         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
416         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
417         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
418         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
419         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
420         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
421         ls -l  $DIR/d17f
422 }
423 run_test 17f "symlinks: long and very long symlink name ========================"
424
425 # str_repeat(S, N) generate a string that is string S repeated N times
426 str_repeat() {
427         local s=$1
428         local n=$2
429         local ret=''
430         while [ $((n -= 1)) -ge 0 ]; do
431                 ret=$ret$s
432         done
433         echo $ret
434 }
435
436 # Long symlinks and LU-2241
437 test_17g() {
438         test_mkdir -p $DIR/$tdir
439         local TESTS="59 60 61 4094 4095"
440
441         # Fix for inode size boundary in 2.1.4
442         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
443                 TESTS="4094 4095"
444
445         # Patch not applied to 2.2 or 2.3 branches
446         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
447         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
448                 TESTS="4094 4095"
449
450         # skip long symlink name for rhel6.5.
451         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
452         grep -q '6.5' /etc/redhat-release && TESTS="59 60 61 4062 4063"
453
454         for i in $TESTS; do
455                 local SYMNAME=$(str_repeat 'x' $i)
456                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
457                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
458         done
459 }
460 run_test 17g "symlinks: really long symlink name and inode boundaries"
461
462 test_17h() { #bug 17378
463         remote_mds_nodsh && skip "remote MDS with nodsh" && return
464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
465         local mdt_idx
466         test_mkdir -p $DIR/$tdir
467         if [ $MDSCOUNT -gt 1 ]; then
468                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
469         else
470                 mdt_idx=0
471         fi
472         $SETSTRIPE -c -1 $DIR/$tdir
473 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
474         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
475         touch $DIR/$tdir/$tfile || true
476 }
477 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
478
479 test_17i() { #bug 20018
480         remote_mds_nodsh && skip "remote MDS with nodsh" && return
481         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
482         test_mkdir -p $DIR/$tdir
483         local foo=$DIR/$tdir/$tfile
484         local mdt_idx
485         if [ $MDSCOUNT -gt 1 ]; then
486                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
487         else
488                 mdt_idx=0
489         fi
490         ln -s $foo $foo || error "create symlink failed"
491 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
492         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
493         ls -l $foo && error "error not detected"
494         return 0
495 }
496 run_test 17i "don't panic on short symlink"
497
498 test_17k() { #bug 22301
499         rsync --help | grep -q xattr ||
500                 skip_env "$(rsync --version| head -1) does not support xattrs"
501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
502         test_mkdir -p $DIR/$tdir
503         test_mkdir -p $DIR/$tdir.new
504         touch $DIR/$tdir/$tfile
505         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
506         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
507                 error "rsync failed with xattrs enabled"
508 }
509 run_test 17k "symlinks: rsync with xattrs enabled ========================="
510
511 test_17l() { # LU-279
512         mkdir -p $DIR/$tdir
513         touch $DIR/$tdir/$tfile
514         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
515         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
516                 # -h to not follow symlinks. -m '' to list all the xattrs.
517                 # grep to remove first line: '# file: $path'.
518                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
519                 do
520                         lgetxattr_size_check $path $xattr ||
521                                 error "lgetxattr_size_check $path $xattr failed"
522                 done
523         done
524 }
525 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
526
527 # LU-1540
528 test_17m() {
529         local short_sym="0123456789"
530         local WDIR=$DIR/${tdir}m
531         local mds_index
532         local devname
533         local cmd
534         local i
535         local rc=0
536
537         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
538         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
539                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
540
541         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
542                 skip "only for ldiskfs MDT" && return 0
543
544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
545
546         mkdir -p $WDIR
547         long_sym=$short_sym
548         # create a long symlink file
549         for ((i = 0; i < 4; ++i)); do
550                 long_sym=${long_sym}${long_sym}
551         done
552
553         echo "create 512 short and long symlink files under $WDIR"
554         for ((i = 0; i < 256; ++i)); do
555                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
556                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
557         done
558
559         echo "erase them"
560         rm -f $WDIR/*
561         sync
562         wait_delete_completed
563
564         echo "recreate the 512 symlink files with a shorter string"
565         for ((i = 0; i < 512; ++i)); do
566                 # rewrite the symlink file with a shorter string
567                 ln -sf ${long_sym} $WDIR/long-$i
568                 ln -sf ${short_sym} $WDIR/short-$i
569         done
570
571         mds_index=$($LFS getstripe -M $WDIR)
572         mds_index=$((mds_index+1))
573         devname=$(mdsdevname $mds_index)
574         cmd="$E2FSCK -fnvd $devname"
575
576         echo "stop and checking mds${mds_index}: $cmd"
577         # e2fsck should not return error
578         stop mds${mds_index}
579         do_facet mds${mds_index} $cmd || rc=$?
580
581         start mds${mds_index} $devname $MDS_MOUNT_OPTS
582         df $MOUNT > /dev/null 2>&1
583         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
584                 "short/long symlink MDT: rc=$rc"
585         return $rc
586 }
587 run_test 17m "run e2fsck against MDT which contains short/long symlink"
588
589 check_fs_consistency_17n() {
590         local mdt_index
591         local devname
592         local cmd
593         local rc=0
594
595         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
596         # so it only check MDT1/MDT2 instead of all of MDTs.
597         for mdt_index in $(seq 1 2); do
598                 devname=$(mdsdevname $mdt_index)
599                 cmd="$E2FSCK -fnvd $devname"
600
601                 echo "stop and checking mds${mdt_index}: $cmd"
602                 # e2fsck should not return error
603                 stop mds${mdt_index}
604                 do_facet mds${mdt_index} $cmd || rc=$?
605
606                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
607                 df $MOUNT > /dev/null 2>&1
608                 [ $rc -ne 0 ] && break
609         done
610         return $rc
611 }
612
613 test_17n() {
614         local i
615
616         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
617         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
618                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
619
620         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
621                 skip "only for ldiskfs MDT" && return 0
622
623         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
624
625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
626
627         mkdir -p $DIR/$tdir
628         for ((i=0; i<10; i++)); do
629                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
630                         error "create remote dir error $i"
631                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
632                         error "create files under remote dir failed $i"
633         done
634
635         check_fs_consistency_17n || error "e2fsck report error"
636
637         for ((i=0;i<10;i++)); do
638                 rm -rf $DIR/$tdir/remote_dir_${i} ||
639                         error "destroy remote dir error $i"
640         done
641
642         check_fs_consistency_17n || error "e2fsck report error"
643 }
644 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
645
646 test_17o() {
647         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ]] &&
648                 skip "Need MDS version at least 2.3.64" && return
649
650         local WDIR=$DIR/${tdir}o
651         local mdt_index
652         local mdtdevname
653         local rc=0
654
655         mkdir -p $WDIR
656         mdt_index=$($LFS getstripe -M $WDIR)
657         mdt_index=$((mdt_index+1))
658         mdtdevname=$(mdsdevname $mdt_index)
659
660         touch $WDIR/$tfile
661         stop mds${mdt_index}
662         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
663
664         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
665         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
666         ls -l $WDIR/$tfile && rc=1
667         do_facet mds${mdt_index} lctl set_param fail_loc=0
668         [[ $rc -ne 0 ]] && error "stat file should fail"
669         true
670 }
671 run_test 17o "stat file with incompat LMA feature"
672
673 test_18() {
674         touch $DIR/f || error "Failed to touch $DIR/f: $?"
675         ls $DIR || error "Failed to ls $DIR: $?"
676 }
677 run_test 18 "touch .../f ; ls ... =============================="
678
679 test_19a() {
680         touch $DIR/$tfile
681         ls -l $DIR
682         rm $DIR/$tfile
683         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
684 }
685 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
686
687 test_19b() {
688         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
689 }
690 run_test 19b "ls -l .../f19 (should return error) =============="
691
692 test_19c() {
693         [ $RUNAS_ID -eq $UID ] &&
694                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
695         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
696 }
697 run_test 19c "$RUNAS touch .../f19 (should return error) =="
698
699 test_19d() {
700         cat $DIR/f19 && error || true
701 }
702 run_test 19d "cat .../f19 (should return error) =============="
703
704 test_20() {
705         touch $DIR/$tfile
706         rm $DIR/$tfile
707         log "1 done"
708         touch $DIR/$tfile
709         rm $DIR/$tfile
710         log "2 done"
711         touch $DIR/$tfile
712         rm $DIR/$tfile
713         log "3 done"
714         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
715 }
716 run_test 20 "touch .../f ; ls -l ... ==========================="
717
718 test_21() {
719         test_mkdir -p $DIR/$tdir
720         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
721         ln -s dangle $DIR/$tdir/link
722         echo foo >> $DIR/$tdir/link
723         cat $DIR/$tdir/dangle
724         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
725         $CHECKSTAT -f -t file $DIR/$tdir/link ||
726                 error "$tdir/link not linked to a file"
727 }
728 run_test 21 "write to dangling link ============================"
729
730 test_22() {
731         WDIR=$DIR/$tdir
732         test_mkdir -p $DIR/$tdir
733         chown $RUNAS_ID:$RUNAS_GID $WDIR
734         (cd $WDIR || error "cd $WDIR failed";
735         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
736         $RUNAS tar xf -)
737         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
738         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
739         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
740 }
741 run_test 22 "unpack tar archive as non-root user ==============="
742
743 # was test_23
744 test_23a() {
745         test_mkdir -p $DIR/$tdir
746         local file=$DIR/$tdir/$tfile
747
748         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
749         openfile -f O_CREAT:O_EXCL $file &&
750                 error "$file recreate succeeded" || true
751 }
752 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
753
754 test_23b() { # bug 18988
755         test_mkdir -p $DIR/$tdir
756         local file=$DIR/$tdir/$tfile
757
758         rm -f $file
759         echo foo > $file || error "write filed"
760         echo bar >> $file || error "append filed"
761         $CHECKSTAT -s 8 $file || error "wrong size"
762         rm $file
763 }
764 run_test 23b "O_APPEND check =========================="
765
766 # rename sanity
767 test_24a() {
768         echo '-- same directory rename'
769         test_mkdir $DIR/$tdir
770         touch $DIR/$tdir/$tfile.1
771         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
772         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
773 }
774 run_test 24a "rename file to non-existent target"
775
776 test_24b() {
777         test_mkdir $DIR/$tdir
778         touch $DIR/$tdir/$tfile.{1,2}
779         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
780         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
781         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
782 }
783 run_test 24b "rename file to existing target"
784
785 test_24c() {
786         test_mkdir $DIR/$tdir
787         test_mkdir $DIR/$tdir/d$testnum.1
788         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
789         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
790         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
791 }
792 run_test 24c "rename directory to non-existent target"
793
794 test_24d() {
795         test_mkdir -c1 $DIR/$tdir
796         test_mkdir -c1 $DIR/$tdir/d$testnum.1
797         test_mkdir -c1 $DIR/$tdir/d$testnum.2
798         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
799         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
800         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
801 }
802 run_test 24d "rename directory to existing target"
803
804 test_24e() {
805         echo '-- cross directory renames --'
806         test_mkdir $DIR/R5a
807         test_mkdir $DIR/R5b
808         touch $DIR/R5a/f
809         mv $DIR/R5a/f $DIR/R5b/g
810         $CHECKSTAT -a $DIR/R5a/f || error
811         $CHECKSTAT -t file $DIR/R5b/g || error
812 }
813 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
814
815 test_24f() {
816         test_mkdir $DIR/R6a
817         test_mkdir $DIR/R6b
818         touch $DIR/R6a/f $DIR/R6b/g
819         mv $DIR/R6a/f $DIR/R6b/g
820         $CHECKSTAT -a $DIR/R6a/f || error
821         $CHECKSTAT -t file $DIR/R6b/g || error
822 }
823 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
824
825 test_24g() {
826         test_mkdir $DIR/R7a
827         test_mkdir $DIR/R7b
828         test_mkdir $DIR/R7a/d
829         mv $DIR/R7a/d $DIR/R7b/e
830         $CHECKSTAT -a $DIR/R7a/d || error
831         $CHECKSTAT -t dir $DIR/R7b/e || error
832 }
833 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
834
835 test_24h() {
836         test_mkdir -c1 $DIR/R8a
837         test_mkdir -c1 $DIR/R8b
838         test_mkdir -c1 $DIR/R8a/d
839         test_mkdir -c1 $DIR/R8b/e
840         mrename $DIR/R8a/d $DIR/R8b/e
841         $CHECKSTAT -a $DIR/R8a/d || error
842         $CHECKSTAT -t dir $DIR/R8b/e || error
843 }
844 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
845
846 test_24i() {
847         echo "-- rename error cases"
848         test_mkdir $DIR/R9
849         test_mkdir $DIR/R9/a
850         touch $DIR/R9/f
851         mrename $DIR/R9/f $DIR/R9/a
852         $CHECKSTAT -t file $DIR/R9/f || error
853         $CHECKSTAT -t dir  $DIR/R9/a || error
854         $CHECKSTAT -a $DIR/R9/a/f || error
855 }
856 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
857
858 test_24j() {
859         test_mkdir $DIR/R10
860         mrename $DIR/R10/f $DIR/R10/g
861         $CHECKSTAT -t dir $DIR/R10 || error
862         $CHECKSTAT -a $DIR/R10/f || error
863         $CHECKSTAT -a $DIR/R10/g || error
864 }
865 run_test 24j "source does not exist ============================"
866
867 test_24k() {
868         test_mkdir $DIR/R11a
869         test_mkdir $DIR/R11a/d
870         touch $DIR/R11a/f
871         mv $DIR/R11a/f $DIR/R11a/d
872         $CHECKSTAT -a $DIR/R11a/f || error
873         $CHECKSTAT -t file $DIR/R11a/d/f || error
874 }
875 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
876
877 # bug 2429 - rename foo foo foo creates invalid file
878 test_24l() {
879         f="$DIR/f24l"
880         $MULTIOP $f OcNs || error
881 }
882 run_test 24l "Renaming a file to itself ========================"
883
884 test_24m() {
885         f="$DIR/f24m"
886         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
887         # on ext3 this does not remove either the source or target files
888         # though the "expected" operation would be to remove the source
889         $CHECKSTAT -t file ${f} || error "${f} missing"
890         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
891 }
892 run_test 24m "Renaming a file to a hard link to itself ========="
893
894 test_24n() {
895     f="$DIR/f24n"
896     # this stats the old file after it was renamed, so it should fail
897     touch ${f}
898     $CHECKSTAT ${f}
899     mv ${f} ${f}.rename
900     $CHECKSTAT ${f}.rename
901     $CHECKSTAT -a ${f}
902 }
903 run_test 24n "Statting the old file after renaming (Posix rename 2)"
904
905 test_24o() {
906         check_kernel_version 37 || return 0
907         test_mkdir -p $DIR/d24o
908         rename_many -s random -v -n 10 $DIR/d24o
909 }
910 run_test 24o "rename of files during htree split ==============="
911
912 test_24p() {
913         test_mkdir $DIR/R12a
914         test_mkdir $DIR/R12b
915         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
916         mrename $DIR/R12a $DIR/R12b
917         $CHECKSTAT -a $DIR/R12a || error
918         $CHECKSTAT -t dir $DIR/R12b || error
919         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
920         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
921 }
922 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
923
924 cleanup_multiop_pause() {
925         trap 0
926         kill -USR1 $MULTIPID
927 }
928
929 test_24q() {
930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
931         test_mkdir $DIR/R13a
932         test_mkdir $DIR/R13b
933         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
934         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
935         MULTIPID=$!
936
937         trap cleanup_multiop_pause EXIT
938         mrename $DIR/R13a $DIR/R13b
939         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
940         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
941         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
942         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
943         cleanup_multiop_pause
944         wait $MULTIPID || error "multiop close failed"
945 }
946 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
947
948 test_24r() { #bug 3789
949         test_mkdir $DIR/R14a
950         test_mkdir $DIR/R14a/b
951         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
952         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
953         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
954 }
955 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
956
957 test_24s() {
958         test_mkdir $DIR/R15a
959         test_mkdir $DIR/R15a/b
960         test_mkdir $DIR/R15a/b/c
961         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
962         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
963         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
964 }
965 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
966 test_24t() {
967         test_mkdir $DIR/R16a
968         test_mkdir $DIR/R16a/b
969         test_mkdir $DIR/R16a/b/c
970         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
971         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
972         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
973 }
974 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
975
976 test_24u() { # bug12192
977         rm -rf $DIR/$tfile
978         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
979         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
980 }
981 run_test 24u "create stripe file"
982
983 page_size() {
984         getconf PAGE_SIZE
985 }
986
987 simple_cleanup_common() {
988         trap 0
989         rm -rf $DIR/$tdir
990         wait_delete_completed
991 }
992
993 max_pages_per_rpc() {
994         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
995 }
996
997 test_24v() {
998         local NRFILES=100000
999         local FREE_INODES=$(mdt_free_inodes 0)
1000         [ $FREE_INODES -lt $NRFILES ] && \
1001                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1002                 return
1003
1004         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1005         trap simple_cleanup_common EXIT
1006
1007         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1008         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1009
1010         mkdir -p $DIR/$tdir
1011         createmany -m $DIR/$tdir/$tfile $NRFILES
1012
1013         cancel_lru_locks mdc
1014         lctl set_param mdc.*.stats clear
1015
1016         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1017
1018         # LU-5 large readdir
1019         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1020         #               8 bytes for name(filename is mostly 5 in this test) +
1021         #               8 bytes for luda_type
1022         # take into account of overhead in lu_dirpage header and end mark in
1023         # each page, plus one in RPC_NUM calculation.
1024         DIRENT_SIZE=48
1025         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1026         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1027         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1028                                 awk '/^mds_readpage/ {print $2}')
1029         [ $mds_readpage -gt $RPC_NUM ] && \
1030                 error "large readdir doesn't take effect"
1031
1032         simple_cleanup_common
1033 }
1034 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1035
1036 test_24w() { # bug21506
1037         SZ1=234852
1038         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1039         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1040         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1041         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1042         [ "$SZ1" = "$SZ2" ] || \
1043                 error "Error reading at the end of the file $tfile"
1044 }
1045 run_test 24w "Reading a file larger than 4Gb"
1046
1047 test_24x() {
1048         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1050         local MDTIDX=1
1051         local remote_dir=$DIR/$tdir/remote_dir
1052
1053         mkdir -p $DIR/$tdir
1054         $LFS mkdir -i $MDTIDX $remote_dir ||
1055                 error "create remote directory failed"
1056
1057         mkdir -p $DIR/$tdir/src_dir
1058         touch $DIR/$tdir/src_file
1059         mkdir -p $remote_dir/tgt_dir
1060         touch $remote_dir/tgt_file
1061
1062         mrename $remote_dir $DIR/ &&
1063                 error "rename dir cross MDT works!"
1064
1065         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1066                 error "rename dir cross MDT works!"
1067
1068         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1069                 error "rename file cross MDT works!"
1070
1071         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1072                 error "ln file cross MDT should not work!"
1073
1074         rm -rf $DIR/$tdir || error "Can not delete directories"
1075 }
1076 run_test 24x "cross rename/link should be failed"
1077
1078 test_24y() {
1079         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1081         local MDTIDX=1
1082         local remote_dir=$DIR/$tdir/remote_dir
1083
1084         mkdir -p $DIR/$tdir
1085         $LFS mkdir -i $MDTIDX $remote_dir ||
1086                    error "create remote directory failed"
1087
1088         mkdir -p $remote_dir/src_dir
1089         touch $remote_dir/src_file
1090         mkdir -p $remote_dir/tgt_dir
1091         touch $remote_dir/tgt_file
1092
1093         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1094                 error "rename subdir in the same remote dir failed!"
1095
1096         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1097                 error "rename files in the same remote dir failed!"
1098
1099         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1100                 error "link files in the same remote dir failed!"
1101
1102         rm -rf $DIR/$tdir || error "Can not delete directories"
1103 }
1104 run_test 24y "rename/link on the same dir should succeed"
1105
1106 test_24z() {
1107         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1109         local MDTIDX=1
1110         local remote_src=$DIR/$tdir/remote_dir
1111         local remote_tgt=$DIR/$tdir/remote_tgt
1112
1113         mkdir -p $DIR/$tdir
1114         $LFS mkdir -i $MDTIDX $remote_src ||
1115                    error "create remote directory failed"
1116
1117         $LFS mkdir -i $MDTIDX $remote_tgt ||
1118                    error "create remote directory failed"
1119
1120         mrename $remote_src $remote_tgt &&
1121                 error "rename remote dirs should not work!"
1122
1123         # If target dir does not exists, it should succeed
1124         rm -rf $remote_tgt
1125         mrename $remote_src $remote_tgt ||
1126                 error "rename remote dirs(tgt dir does not exists) failed!"
1127
1128         rm -rf $DIR/$tdir || error "Can not delete directories"
1129 }
1130 run_test 24z "rename one remote dir to another remote dir should fail"
1131
1132 test_24A() { # LU-3182
1133         local NFILES=5000
1134
1135         rm -rf $DIR/$tdir
1136         mkdir -p $DIR/$tdir
1137         createmany -m $DIR/$tdir/$tfile $NFILES
1138         local t=`ls $DIR/$tdir | wc -l`
1139         local u=`ls $DIR/$tdir | sort -u | wc -l`
1140         if [ $t -ne $NFILES -o $u -ne $NFILES ] ; then
1141                 error "Expected $NFILES files, got $t ($u unique)"
1142         fi
1143
1144         rm -rf $DIR/$tdir || error "Can not delete directories"
1145 }
1146 run_test 24A "readdir() returns correct number of entries."
1147
1148 test_25a() {
1149         echo '== symlink sanity ============================================='
1150
1151         test_mkdir $DIR/d25
1152         ln -s d25 $DIR/s25
1153         touch $DIR/s25/foo || error
1154 }
1155 run_test 25a "create file in symlinked directory ==============="
1156
1157 test_25b() {
1158         [ ! -d $DIR/d25 ] && test_25a
1159         $CHECKSTAT -t file $DIR/s25/foo || error
1160 }
1161 run_test 25b "lookup file in symlinked directory ==============="
1162
1163 test_26a() {
1164         test_mkdir $DIR/d26
1165         test_mkdir $DIR/d26/d26-2
1166         ln -s d26/d26-2 $DIR/s26
1167         touch $DIR/s26/foo || error
1168 }
1169 run_test 26a "multiple component symlink ======================="
1170
1171 test_26b() {
1172         test_mkdir -p $DIR/d26b/d26-2
1173         ln -s d26b/d26-2/foo $DIR/s26-2
1174         touch $DIR/s26-2 || error
1175 }
1176 run_test 26b "multiple component symlink at end of lookup ======"
1177
1178 test_26c() {
1179         test_mkdir $DIR/d26.2
1180         touch $DIR/d26.2/foo
1181         ln -s d26.2 $DIR/s26.2-1
1182         ln -s s26.2-1 $DIR/s26.2-2
1183         ln -s s26.2-2 $DIR/s26.2-3
1184         chmod 0666 $DIR/s26.2-3/foo
1185 }
1186 run_test 26c "chain of symlinks ================================"
1187
1188 # recursive symlinks (bug 439)
1189 test_26d() {
1190         ln -s d26-3/foo $DIR/d26-3
1191 }
1192 run_test 26d "create multiple component recursive symlink ======"
1193
1194 test_26e() {
1195         [ ! -h $DIR/d26-3 ] && test_26d
1196         rm $DIR/d26-3
1197 }
1198 run_test 26e "unlink multiple component recursive symlink ======"
1199
1200 # recursive symlinks (bug 7022)
1201 test_26f() {
1202         test_mkdir -p $DIR/$tdir
1203         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1204         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1205         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1206         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1207         cd $tfile                || error "cd $tfile failed"
1208         ln -s .. dotdot          || error "ln dotdot failed"
1209         ln -s dotdot/lndir lndir || error "ln lndir failed"
1210         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1211         output=`ls $tfile/$tfile/lndir/bar1`
1212         [ "$output" = bar1 ] && error "unexpected output"
1213         rm -r $tfile             || error "rm $tfile failed"
1214         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1215 }
1216 run_test 26f "rm -r of a directory which has recursive symlink ="
1217
1218 test_27a() {
1219         echo '== stripe sanity =============================================='
1220         test_mkdir -p $DIR/d27 || error "mkdir failed"
1221         $GETSTRIPE $DIR/d27
1222         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1223         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1224         pass
1225         log "== test_27a: write to one stripe file ========================="
1226         cp /etc/hosts $DIR/d27/f0 || error
1227 }
1228 run_test 27a "one stripe file =================================="
1229
1230 test_27b() {
1231         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1232         test_mkdir -p $DIR/d27
1233         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1234         $GETSTRIPE -c $DIR/d27/f01
1235         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1236                 error "two-stripe file doesn't have two stripes"
1237
1238         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1239 }
1240 run_test 27b "create and write to two stripe file"
1241
1242 test_27d() {
1243         test_mkdir -p $DIR/d27
1244         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1245         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1246         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1247 }
1248 run_test 27d "create file with default settings ================"
1249
1250 test_27e() {
1251         test_mkdir -p $DIR/d27
1252         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1253         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1254         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1255 }
1256 run_test 27e "setstripe existing file (should return error) ======"
1257
1258 test_27f() {
1259         test_mkdir -p $DIR/d27
1260         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1261         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1262         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1263 }
1264 run_test 27f "setstripe with bad stripe size (should return error)"
1265
1266 test_27g() {
1267         test_mkdir -p $DIR/d27
1268         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1269         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1270                 error "$DIR/d27/fnone has object"
1271 }
1272 run_test 27g "$GETSTRIPE with no objects"
1273
1274 test_27i() {
1275         touch $DIR/d27/fsome || error "touch failed"
1276         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1277 }
1278 run_test 27i "$GETSTRIPE with some objects"
1279
1280 test_27j() {
1281         test_mkdir -p $DIR/d27
1282         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1283 }
1284 run_test 27j "setstripe with bad stripe offset (should return error)"
1285
1286 test_27k() { # bug 2844
1287         test_mkdir -p $DIR/d27
1288         FILE=$DIR/d27/f27k
1289         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1290         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1291         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1292         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1293         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1294         dd if=/dev/zero of=$FILE bs=4k count=1
1295         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1296         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1297 }
1298 run_test 27k "limit i_blksize for broken user apps ============="
1299
1300 test_27l() {
1301         test_mkdir -p $DIR/d27
1302         mcreate $DIR/f27l || error "creating file"
1303         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1304                 error "setstripe should have failed" || true
1305 }
1306 run_test 27l "check setstripe permissions (should return error)"
1307
1308 test_27m() {
1309         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1310                 return
1311         if [ $ORIGFREE -gt $MAXFREE ]; then
1312                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1313                 return
1314         fi
1315         trap simple_cleanup_common EXIT
1316         test_mkdir -p $DIR/$tdir
1317         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1318         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1319                 error "dd should fill OST0"
1320         i=2
1321         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1322                 i=`expr $i + 1`
1323                 [ $i -gt 256 ] && break
1324         done
1325         i=`expr $i + 1`
1326         touch $DIR/$tdir/f27m_$i
1327         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1328                 error "OST0 was full but new created file still use it"
1329         i=`expr $i + 1`
1330         touch $DIR/$tdir/f27m_$i
1331         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1332                 error "OST0 was full but new created file still use it"
1333         simple_cleanup_common
1334 }
1335 run_test 27m "create file while OST0 was full =================="
1336
1337 sleep_maxage() {
1338         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1339         sleep $DELAY
1340 }
1341
1342 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1343 # if the OST isn't full anymore.
1344 reset_enospc() {
1345         local OSTIDX=${1:-""}
1346
1347         local list=$(comma_list $(osts_nodes))
1348         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1349
1350         do_nodes $list lctl set_param fail_loc=0
1351         sync    # initiate all OST_DESTROYs from MDS to OST
1352         sleep_maxage
1353 }
1354
1355 exhaust_precreations() {
1356         local OSTIDX=$1
1357         local FAILLOC=$2
1358         local FAILIDX=${3:-$OSTIDX}
1359
1360         test_mkdir -p $DIR/$tdir
1361         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1362         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1363
1364         local OST=$(ostname_from_index $OSTIDX)
1365         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1366                           sed -e 's/_UUID$//;s/^.*-//')
1367
1368         # on the mdt's osc
1369         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1370         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1371                         osc.$mdtosc_proc1.prealloc_last_id)
1372         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1373                         osc.$mdtosc_proc1.prealloc_next_id)
1374
1375         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1376         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1377
1378         test_mkdir -p $DIR/$tdir/${OST}
1379         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1380 #define OBD_FAIL_OST_ENOSPC              0x215
1381         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1382         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1383         echo "Creating to objid $last_id on ost $OST..."
1384         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1385         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1386         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1387         sleep_maxage
1388 }
1389
1390 exhaust_all_precreations() {
1391         local i
1392         for (( i=0; i < OSTCOUNT; i++ )) ; do
1393                 exhaust_precreations $i $1 -1
1394         done
1395 }
1396
1397 test_27n() {
1398         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1400         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1401         remote_ost_nodsh && skip "remote OST with nodsh" && return
1402
1403         reset_enospc
1404         rm -f $DIR/$tdir/$tfile
1405         exhaust_precreations 0 0x80000215
1406         $SETSTRIPE -c -1 $DIR/$tdir
1407         touch $DIR/$tdir/$tfile || error
1408         $GETSTRIPE $DIR/$tdir/$tfile
1409         reset_enospc
1410 }
1411 run_test 27n "create file with some full OSTs =================="
1412
1413 test_27o() {
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         exhaust_all_precreations 0x215
1422
1423         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1424
1425         reset_enospc
1426         rm -rf $DIR/$tdir/*
1427 }
1428 run_test 27o "create file with all full OSTs (should error) ===="
1429
1430 test_27p() {
1431         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1433         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1434         remote_ost_nodsh && skip "remote OST with nodsh" && return
1435
1436         reset_enospc
1437         rm -f $DIR/$tdir/$tfile
1438         test_mkdir -p $DIR/$tdir
1439
1440         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1441         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1442         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1443
1444         exhaust_precreations 0 0x80000215
1445         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1446         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1447         $GETSTRIPE $DIR/$tdir/$tfile
1448
1449         reset_enospc
1450 }
1451 run_test 27p "append to a truncated file with some full OSTs ==="
1452
1453 test_27q() {
1454         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1456         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1457         remote_ost_nodsh && skip "remote OST with nodsh" && return
1458
1459         reset_enospc
1460         rm -f $DIR/$tdir/$tfile
1461
1462         test_mkdir -p $DIR/$tdir
1463         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1464         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1465         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1466
1467         exhaust_all_precreations 0x215
1468
1469         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1470         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1471
1472         reset_enospc
1473 }
1474 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1475
1476 test_27r() {
1477         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1479         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1480         remote_ost_nodsh && skip "remote OST with nodsh" && return
1481
1482         reset_enospc
1483         rm -f $DIR/$tdir/$tfile
1484         exhaust_precreations 0 0x80000215
1485
1486         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1487
1488         reset_enospc
1489 }
1490 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1491
1492 test_27s() { # bug 10725
1493         test_mkdir -p $DIR/$tdir
1494         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1495         local stripe_count=0
1496         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1497         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1498                 error "stripe width >= 2^32 succeeded" || true
1499
1500 }
1501 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1502
1503 test_27t() { # bug 10864
1504         WDIR=`pwd`
1505         WLFS=`which lfs`
1506         cd $DIR
1507         touch $tfile
1508         $WLFS getstripe $tfile
1509         cd $WDIR
1510 }
1511 run_test 27t "check that utils parse path correctly"
1512
1513 test_27u() { # bug 4900
1514         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1516         local index
1517         local list=$(comma_list $(mdts_nodes))
1518
1519 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1520         do_nodes $list $LCTL set_param fail_loc=0x139
1521         test_mkdir -p $DIR/$tdir
1522         rm -rf $DIR/$tdir/*
1523         createmany -o $DIR/$tdir/t- 1000
1524         do_nodes $list $LCTL set_param fail_loc=0
1525
1526         TLOG=$DIR/$tfile.getstripe
1527         $GETSTRIPE $DIR/$tdir > $TLOG
1528         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1529         unlinkmany $DIR/$tdir/t- 1000
1530         [ $OBJS -gt 0 ] && \
1531                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1532 }
1533 run_test 27u "skip object creation on OSC w/o objects =========="
1534
1535 test_27v() { # bug 4900
1536         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1538         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1539         remote_ost_nodsh && skip "remote OST with nodsh" && return
1540
1541         exhaust_all_precreations 0x215
1542         reset_enospc
1543
1544         test_mkdir -p $DIR/$tdir
1545         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1546
1547         touch $DIR/$tdir/$tfile
1548         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1549         # all except ost1
1550         for (( i=1; i < OSTCOUNT; i++ )); do
1551                 do_facet ost$i lctl set_param fail_loc=0x705
1552         done
1553         local START=`date +%s`
1554         createmany -o $DIR/$tdir/$tfile 32
1555
1556         local FINISH=`date +%s`
1557         local TIMEOUT=`lctl get_param -n timeout`
1558         local PROCESS=$((FINISH - START))
1559         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1560                error "$FINISH - $START >= $TIMEOUT / 2"
1561         sleep $((TIMEOUT / 2 - PROCESS))
1562         reset_enospc
1563 }
1564 run_test 27v "skip object creation on slow OST ================="
1565
1566 test_27w() { # bug 10997
1567         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1568         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1569         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1570                 error "stripe size $size != 65536" || true
1571         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1572                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1573 }
1574 run_test 27w "check $SETSTRIPE -S option"
1575
1576 test_27wa() {
1577         [ "$OSTCOUNT" -lt "2" ] &&
1578                 skip_env "skipping multiple stripe count/offset test" && return
1579
1580         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1581         for i in $(seq 1 $OSTCOUNT); do
1582                 offset=$((i - 1))
1583                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1584                         error "setstripe -c $i -i $offset failed"
1585                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1586                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1587                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1588                 [ $index -ne $offset ] &&
1589                         error "stripe offset $index != $offset" || true
1590         done
1591 }
1592 run_test 27wa "check $SETSTRIPE -c -i options"
1593
1594 test_27x() {
1595         remote_ost_nodsh && skip "remote OST with nodsh" && return
1596         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1598         OFFSET=$(($OSTCOUNT - 1))
1599         OSTIDX=0
1600         local OST=$(ostname_from_index $OSTIDX)
1601
1602         test_mkdir -p $DIR/$tdir
1603         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1604         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1605         sleep_maxage
1606         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1607         for i in `seq 0 $OFFSET`; do
1608                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1609                 error "OST0 was degraded but new created file still use it"
1610         done
1611         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1612 }
1613 run_test 27x "create files while OST0 is degraded"
1614
1615 test_27y() {
1616         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1617         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1618         remote_ost_nodsh && skip "remote OST with nodsh" && return
1619         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1620
1621         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1622         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1623             osc.$mdtosc.prealloc_last_id)
1624         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1625             osc.$mdtosc.prealloc_next_id)
1626         local fcount=$((last_id - next_id))
1627         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1628         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1629
1630         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1631                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1632         local OST_DEACTIVE_IDX=-1
1633         local OSC
1634         local OSTIDX
1635         local OST
1636
1637         for OSC in $MDS_OSCS; do
1638                 OST=$(osc_to_ost $OSC)
1639                 OSTIDX=$(index_from_ostuuid $OST)
1640                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1641                         OST_DEACTIVE_IDX=$OSTIDX
1642                 fi
1643                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1644                         echo $OSC "is Deactivated:"
1645                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1646                 fi
1647         done
1648
1649         OSTIDX=$(index_from_ostuuid $OST)
1650         mkdir -p $DIR/$tdir
1651         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1652
1653         for OSC in $MDS_OSCS; do
1654                 OST=$(osc_to_ost $OSC)
1655                 OSTIDX=$(index_from_ostuuid $OST)
1656                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1657                         echo $OST "is degraded:"
1658                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1659                                                 obdfilter.$OST.degraded=1
1660                 fi
1661         done
1662
1663         sleep_maxage
1664         createmany -o $DIR/$tdir/$tfile $fcount
1665
1666         for OSC in $MDS_OSCS; do
1667                 OST=$(osc_to_ost $OSC)
1668                 OSTIDX=$(index_from_ostuuid $OST)
1669                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1670                         echo $OST "is recovered from degraded:"
1671                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1672                                                 obdfilter.$OST.degraded=0
1673                 else
1674                         do_facet $SINGLEMDS lctl --device %$OSC activate
1675                 fi
1676         done
1677
1678         # all osp devices get activated, hence -1 stripe count restored
1679         local stripecnt=0
1680
1681         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1682         # devices get activated.
1683         sleep_maxage
1684         $SETSTRIPE -c -1 $DIR/$tfile
1685         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1686         rm -f $DIR/$tfile
1687         [ $stripecnt -ne $OSTCOUNT ] &&
1688                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1689         return 0
1690 }
1691 run_test 27y "create files while OST0 is degraded and the rest inactive"
1692
1693 check_seq_oid()
1694 {
1695         log "check file $1"
1696
1697         lmm_count=$($GETSTRIPE -c $1)
1698         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1699         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1700
1701         local old_ifs="$IFS"
1702         IFS=$'[:]'
1703         fid=($($LFS path2fid $1))
1704         IFS="$old_ifs"
1705
1706         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1707         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1708
1709         # compare lmm_seq and lu_fid->f_seq
1710         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1711         # compare lmm_object_id and lu_fid->oid
1712         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1713
1714         # check the trusted.fid attribute of the OST objects of the file
1715         local have_obdidx=false
1716         local stripe_nr=0
1717         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1718                 # skip lines up to and including "obdidx"
1719                 [ -z "$obdidx" ] && break
1720                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1721                 $have_obdidx || continue
1722
1723                 local ost=$((obdidx + 1))
1724                 local dev=$(ostdevname $ost)
1725                 local oid_hex
1726
1727                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1728
1729                 seq=$(echo $seq | sed -e "s/^0x//g")
1730                 if [ $seq == 0 ]; then
1731                         oid_hex=$(echo $oid)
1732                 else
1733                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1734                 fi
1735                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1736
1737                 local ff
1738                 #
1739                 # Don't unmount/remount the OSTs if we don't need to do that.
1740                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1741                 # update too, until that use mount/ll_decode_filter_fid/mount.
1742                 # Re-enable when debugfs will understand new filter_fid.
1743                 #
1744                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1745                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1746                                 $dev 2>/dev/null" | grep "parent=")
1747                 else
1748                         stop ost$ost
1749                         mount_fstype ost$ost
1750                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1751                                 $(facet_mntpt ost$ost)/$obj_file)
1752                         unmount_fstype ost$ost
1753                         start ost$ost $dev $OST_MOUNT_OPTS
1754                 fi
1755
1756                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1757
1758                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1759
1760                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1761                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1762                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1763                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1764                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1765                 local ff_pstripe
1766                 if echo $ff_parent | grep -q 'stripe='; then
1767                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1768                 else
1769                         #
1770                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1771                         # into f_ver in this case.  See the comment on
1772                         # ff_parent.
1773                         #
1774                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1775                                 sed -e 's/\]//')
1776                 fi
1777
1778                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1779                 [ $ff_pseq = $lmm_seq ] ||
1780                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1781                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1782                 [ $ff_poid = $lmm_oid ] ||
1783                         error "FF parent OID $ff_poid != $lmm_oid"
1784                 (($ff_pstripe == $stripe_nr)) ||
1785                         error "FF stripe $ff_pstripe != $stripe_nr"
1786
1787                 stripe_nr=$((stripe_nr + 1))
1788         done
1789 }
1790
1791 test_27z() {
1792         remote_ost_nodsh && skip "remote OST with nodsh" && return
1793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1794         test_mkdir -p $DIR/$tdir
1795
1796         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1797                 { error "setstripe -c -1 failed"; return 1; }
1798         # We need to send a write to every object to get parent FID info set.
1799         # This _should_ also work for setattr, but does not currently.
1800         # touch $DIR/$tdir/$tfile-1 ||
1801         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1802                 { error "dd $tfile-1 failed"; return 2; }
1803         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1804                 { error "setstripe -c -1 failed"; return 3; }
1805         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1806                 { error "dd $tfile-2 failed"; return 4; }
1807
1808         # make sure write RPCs have been sent to OSTs
1809         sync; sleep 5; sync
1810
1811         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1812         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1813 }
1814 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1815
1816 test_27A() { # b=19102
1817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1818         local restore_size=$($GETSTRIPE -S $MOUNT)
1819         local restore_count=$($GETSTRIPE -c $MOUNT)
1820         local restore_offset=$($GETSTRIPE -i $MOUNT)
1821         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1822         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1823                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1824         local default_size=$($GETSTRIPE -S $MOUNT)
1825         local default_offset=$($GETSTRIPE -i $MOUNT)
1826         local dsize=$((1024 * 1024))
1827         [ $default_size -eq $dsize ] ||
1828                 error "stripe size $default_size != $dsize"
1829         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1830         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1831 }
1832 run_test 27A "check filesystem-wide default LOV EA values"
1833
1834 test_27B() { # LU-2523
1835         test_mkdir -p $DIR/$tdir
1836         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1837         touch $DIR/$tdir/f0
1838         # open f1 with O_LOV_DELAY_CREATE
1839         # rename f0 onto f1
1840         # call setstripe ioctl on open file descriptor for f1
1841         # close
1842         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1843                 $DIR/$tdir/f0
1844
1845         rm -f $DIR/$tdir/f1
1846         # open f1 with O_LOV_DELAY_CREATE
1847         # unlink f1
1848         # call setstripe ioctl on open file descriptor for f1
1849         # close
1850         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1851
1852         # Allow multiop to fail in imitation of NFS's busted semantics.
1853         true
1854 }
1855 run_test 27B "call setstripe on open unlinked file/rename victim"
1856
1857 test_27C() { #LU-2871
1858         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1859
1860         declare -a ost_idx
1861         local index
1862         local found
1863         local i
1864         local j
1865
1866         test_mkdir -p $DIR/$tdir
1867         cd $DIR/$tdir
1868         for i in $(seq 0 $((OSTCOUNT - 1))); do
1869                 # set stripe across all OSTs starting from OST$i
1870                 $SETSTRIPE -i $i -c -1 $tfile$i
1871                 # get striping information
1872                 ost_idx=($($GETSTRIPE $tfile$i |
1873                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1874                 echo ${ost_idx[@]}
1875
1876                 # check the layout
1877                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1878                         error "${#ost_idx[@]} != $OSTCOUNT"
1879
1880                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1881                         found=0
1882                         for j in $(echo ${ost_idx[@]}); do
1883                                 if [ $index -eq $j ]; then
1884                                         found=1
1885                                         break
1886                                 fi
1887                         done
1888                         [ $found = 1 ] ||
1889                                 error "Can not find $index in ${ost_idx[@]}"
1890                 done
1891         done
1892 }
1893 run_test 27C "check full striping across all OSTs"
1894
1895 # createtest also checks that device nodes are created and
1896 # then visible correctly (#2091)
1897 test_28() { # bug 2091
1898         test_mkdir $DIR/d28
1899         $CREATETEST $DIR/d28/ct || error
1900 }
1901 run_test 28 "create/mknod/mkdir with bad file types ============"
1902
1903 test_29() {
1904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1905         cancel_lru_locks mdc
1906         test_mkdir $DIR/d29
1907         touch $DIR/d29/foo
1908         log 'first d29'
1909         ls -l $DIR/d29
1910
1911         declare -i LOCKCOUNTORIG=0
1912         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1913                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1914         done
1915         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1916
1917         declare -i LOCKUNUSEDCOUNTORIG=0
1918         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1919                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1920         done
1921
1922         log 'second d29'
1923         ls -l $DIR/d29
1924         log 'done'
1925
1926         declare -i LOCKCOUNTCURRENT=0
1927         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1928                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1929         done
1930
1931         declare -i LOCKUNUSEDCOUNTCURRENT=0
1932         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1933                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1934         done
1935
1936         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1937                 lctl set_param -n ldlm.dump_namespaces ""
1938                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1939                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1940                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1941                 return 2
1942         fi
1943         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1944                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1945                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1946                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1947                 return 3
1948         fi
1949 }
1950 run_test 29 "IT_GETATTR regression  ============================"
1951
1952 test_30a() { # was test_30
1953         cp `which ls` $DIR || cp /bin/ls $DIR
1954         $DIR/ls / || error
1955         rm $DIR/ls
1956 }
1957 run_test 30a "execute binary from Lustre (execve) =============="
1958
1959 test_30b() {
1960         cp `which ls` $DIR || cp /bin/ls $DIR
1961         chmod go+rx $DIR/ls
1962         $RUNAS $DIR/ls / || error
1963         rm $DIR/ls
1964 }
1965 run_test 30b "execute binary from Lustre as non-root ==========="
1966
1967 test_30c() { # b=22376
1968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1969         cp `which ls` $DIR || cp /bin/ls $DIR
1970         chmod a-rw $DIR/ls
1971         cancel_lru_locks mdc
1972         cancel_lru_locks osc
1973         $RUNAS $DIR/ls / || error
1974         rm -f $DIR/ls
1975 }
1976 run_test 30c "execute binary from Lustre without read perms ===="
1977
1978 test_31a() {
1979         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1980         $CHECKSTAT -a $DIR/f31 || error
1981 }
1982 run_test 31a "open-unlink file =================================="
1983
1984 test_31b() {
1985         touch $DIR/f31 || error
1986         ln $DIR/f31 $DIR/f31b || error
1987         $MULTIOP $DIR/f31b Ouc || error
1988         $CHECKSTAT -t file $DIR/f31 || error
1989 }
1990 run_test 31b "unlink file with multiple links while open ======="
1991
1992 test_31c() {
1993         touch $DIR/f31 || error
1994         ln $DIR/f31 $DIR/f31c || error
1995         multiop_bg_pause $DIR/f31 O_uc || return 1
1996         MULTIPID=$!
1997         $MULTIOP $DIR/f31c Ouc
1998         kill -USR1 $MULTIPID
1999         wait $MULTIPID
2000 }
2001 run_test 31c "open-unlink file with multiple links ============="
2002
2003 test_31d() {
2004         opendirunlink $DIR/d31d $DIR/d31d || error
2005         $CHECKSTAT -a $DIR/d31d || error
2006 }
2007 run_test 31d "remove of open directory ========================="
2008
2009 test_31e() { # bug 2904
2010         check_kernel_version 34 || return 0
2011         openfilleddirunlink $DIR/d31e || error
2012 }
2013 run_test 31e "remove of open non-empty directory ==============="
2014
2015 test_31f() { # bug 4554
2016         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2017         set -vx
2018         test_mkdir $DIR/d31f
2019         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2020         cp /etc/hosts $DIR/d31f
2021         ls -l $DIR/d31f
2022         $GETSTRIPE $DIR/d31f/hosts
2023         multiop_bg_pause $DIR/d31f D_c || return 1
2024         MULTIPID=$!
2025
2026         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2027         test_mkdir $DIR/d31f
2028         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2029         cp /etc/hosts $DIR/d31f
2030         ls -l $DIR/d31f
2031         $GETSTRIPE $DIR/d31f/hosts
2032         multiop_bg_pause $DIR/d31f D_c || return 1
2033         MULTIPID2=$!
2034
2035         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2036         wait $MULTIPID || error "first opendir $MULTIPID failed"
2037
2038         sleep 6
2039
2040         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2041         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2042         set +vx
2043 }
2044 run_test 31f "remove of open directory with open-unlink file ==="
2045
2046 test_31g() {
2047         echo "-- cross directory link --"
2048         test_mkdir -c1 $DIR/${tdir}ga
2049         test_mkdir -c1 $DIR/${tdir}gb
2050         touch $DIR/${tdir}ga/f
2051         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2052         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2053         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2054         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2055         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2056 }
2057 run_test 31g "cross directory link==============="
2058
2059 test_31h() {
2060         echo "-- cross directory link --"
2061         test_mkdir -c1 $DIR/${tdir}
2062         test_mkdir -c1 $DIR/${tdir}/dir
2063         touch $DIR/${tdir}/f
2064         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2065         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2066         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2067         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2068         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2069 }
2070 run_test 31h "cross directory link under child==============="
2071
2072 test_31i() {
2073         echo "-- cross directory link --"
2074         test_mkdir -c1 $DIR/$tdir
2075         test_mkdir -c1 $DIR/$tdir/dir
2076         touch $DIR/$tdir/dir/f
2077         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2078         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2079         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2080         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2081         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2082 }
2083 run_test 31i "cross directory link under parent==============="
2084
2085 test_31j() {
2086         test_mkdir -c1 -p $DIR/$tdir
2087         test_mkdir -c1 -p $DIR/$tdir/dir1
2088         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2089         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2090         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2091         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2092         return 0
2093 }
2094 run_test 31j "link for directory==============="
2095
2096 test_31k() {
2097         test_mkdir -c1 -p $DIR/$tdir
2098         touch $DIR/$tdir/s
2099         touch $DIR/$tdir/exist
2100         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2101         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2102         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2103         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2104         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2105         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2106         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2107         return 0
2108 }
2109 run_test 31k "link to file: the same, non-existing, dir==============="
2110
2111 test_31m() {
2112         mkdir $DIR/d31m
2113         touch $DIR/d31m/s
2114         mkdir $DIR/d31m2
2115         touch $DIR/d31m2/exist
2116         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2117         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2118         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2119         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2120         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2121         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2122         return 0
2123 }
2124 run_test 31m "link to file: the same, non-existing, dir==============="
2125
2126 test_31n() {
2127         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2128         nlink=$(stat --format=%h $DIR/$tfile)
2129         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2130         local fd=$(free_fd)
2131         local cmd="exec $fd<$DIR/$tfile"
2132         eval $cmd
2133         cmd="exec $fd<&-"
2134         trap "eval $cmd" EXIT
2135         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2136         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2137         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2138         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2139         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2140         eval $cmd
2141 }
2142 run_test 31n "check link count of unlinked file"
2143
2144 link_one() {
2145         local TEMPNAME=$(mktemp $1_XXXXXX)
2146         mlink $TEMPNAME $1 2> /dev/null &&
2147                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2148         munlink $TEMPNAME
2149 }
2150
2151 test_31o() { # LU-2901
2152         mkdir -p $DIR/$tdir
2153         for LOOP in $(seq 100); do
2154                 rm -f $DIR/$tdir/$tfile*
2155                 for THREAD in $(seq 8); do
2156                         link_one $DIR/$tdir/$tfile.$LOOP &
2157                 done
2158                 wait
2159                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2160                 [ $LINKS -gt 1 ] && ls $DIR/$tdir &&
2161                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2162                         break || true
2163         done
2164 }
2165 run_test 31o "duplicate hard links with same filename"
2166
2167 cleanup_test32_mount() {
2168         trap 0
2169         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2170 }
2171
2172 test_32a() {
2173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2174         echo "== more mountpoints and symlinks ================="
2175         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2176         trap cleanup_test32_mount EXIT
2177         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2178         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2179         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2180         cleanup_test32_mount
2181 }
2182 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2183
2184 test_32b() {
2185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2186         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2187         trap cleanup_test32_mount EXIT
2188         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2189         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2190         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2191         cleanup_test32_mount
2192 }
2193 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2194
2195 test_32c() {
2196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2197         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2198         trap cleanup_test32_mount EXIT
2199         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2200         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2201         test_mkdir -p $DIR/$tdir/d2/test_dir
2202         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2203         cleanup_test32_mount
2204 }
2205 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2206
2207 test_32d() {
2208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2209         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2210         trap cleanup_test32_mount EXIT
2211         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2212         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2213         test_mkdir -p $DIR/$tdir/d2/test_dir
2214         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2215         cleanup_test32_mount
2216 }
2217 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2218
2219 test_32e() {
2220         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2221         test_mkdir -p $DIR/d32e/tmp
2222         TMP_DIR=$DIR/d32e/tmp
2223         ln -s $DIR/d32e $TMP_DIR/symlink11
2224         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2225         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2226         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2227 }
2228 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2229
2230 test_32f() {
2231         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2232         test_mkdir -p $DIR/d32f/tmp
2233         TMP_DIR=$DIR/d32f/tmp
2234         ln -s $DIR/d32f $TMP_DIR/symlink11
2235         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2236         ls $DIR/d32f/tmp/symlink11  || error
2237         ls $DIR/d32f/symlink01 || error
2238 }
2239 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2240
2241 test_32g() {
2242         TMP_DIR=$DIR/$tdir/tmp
2243         test_mkdir -p $DIR/$tdir/tmp
2244         test_mkdir $DIR/${tdir}2
2245         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2246         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2247         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2248         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2249         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2250         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2251 }
2252 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2253
2254 test_32h() {
2255         rm -fr $DIR/$tdir $DIR/${tdir}2
2256         TMP_DIR=$DIR/$tdir/tmp
2257         test_mkdir -p $DIR/$tdir/tmp
2258         test_mkdir $DIR/${tdir}2
2259         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2260         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2261         ls $TMP_DIR/symlink12 || error
2262         ls $DIR/$tdir/symlink02  || error
2263 }
2264 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2265
2266 test_32i() {
2267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2268         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2269         trap cleanup_test32_mount EXIT
2270         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2271         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2272         touch $DIR/$tdir/test_file
2273         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2274         cleanup_test32_mount
2275 }
2276 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2277
2278 test_32j() {
2279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2280         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2281         trap cleanup_test32_mount EXIT
2282         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2283         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2284         touch $DIR/$tdir/test_file
2285         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2286         cleanup_test32_mount
2287 }
2288 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2289
2290 test_32k() {
2291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2292         rm -fr $DIR/$tdir
2293         trap cleanup_test32_mount EXIT
2294         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2295         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2296         test_mkdir -p $DIR/$tdir/d2
2297         touch $DIR/$tdir/d2/test_file || error
2298         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2299         cleanup_test32_mount
2300 }
2301 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2302
2303 test_32l() {
2304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2305         rm -fr $DIR/$tdir
2306         trap cleanup_test32_mount EXIT
2307         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2308         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2309         test_mkdir -p $DIR/$tdir/d2
2310         touch $DIR/$tdir/d2/test_file
2311         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2312         cleanup_test32_mount
2313 }
2314 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2315
2316 test_32m() {
2317         rm -fr $DIR/d32m
2318         test_mkdir -p $DIR/d32m/tmp
2319         TMP_DIR=$DIR/d32m/tmp
2320         ln -s $DIR $TMP_DIR/symlink11
2321         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2322         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2323         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2324 }
2325 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2326
2327 test_32n() {
2328         rm -fr $DIR/d32n
2329         test_mkdir -p $DIR/d32n/tmp
2330         TMP_DIR=$DIR/d32n/tmp
2331         ln -s $DIR $TMP_DIR/symlink11
2332         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2333         ls -l $DIR/d32n/tmp/symlink11  || error
2334         ls -l $DIR/d32n/symlink01 || error
2335 }
2336 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2337
2338 test_32o() {
2339         rm -fr $DIR/d32o $DIR/$tfile
2340         touch $DIR/$tfile
2341         test_mkdir -p $DIR/d32o/tmp
2342         TMP_DIR=$DIR/d32o/tmp
2343         ln -s $DIR/$tfile $TMP_DIR/symlink12
2344         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2345         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2346         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2347         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2348         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2349 }
2350 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2351
2352 test_32p() {
2353     log 32p_1
2354         rm -fr $DIR/d32p
2355     log 32p_2
2356         rm -f $DIR/$tfile
2357     log 32p_3
2358         touch $DIR/$tfile
2359     log 32p_4
2360         test_mkdir -p $DIR/d32p/tmp
2361     log 32p_5
2362         TMP_DIR=$DIR/d32p/tmp
2363     log 32p_6
2364         ln -s $DIR/$tfile $TMP_DIR/symlink12
2365     log 32p_7
2366         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2367     log 32p_8
2368         cat $DIR/d32p/tmp/symlink12 || error
2369     log 32p_9
2370         cat $DIR/d32p/symlink02 || error
2371     log 32p_10
2372 }
2373 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2374
2375 cleanup_testdir_mount() {
2376         trap 0
2377         $UMOUNT -d $DIR/$tdir
2378 }
2379
2380 test_32q() {
2381         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2382         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2383         trap cleanup_testdir_mount EXIT
2384         test_mkdir -p $DIR/$tdir
2385         touch $DIR/$tdir/under_the_mount
2386         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2387         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2388         cleanup_testdir_mount
2389 }
2390 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2391
2392 test_32r() {
2393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2394         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2395         trap cleanup_testdir_mount EXIT
2396         test_mkdir -p $DIR/$tdir
2397         touch $DIR/$tdir/under_the_mount
2398         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2399         ls $DIR/$tdir | grep -q under_the_mount && error || true
2400         cleanup_testdir_mount
2401 }
2402 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2403
2404 test_33aa() {
2405         rm -f $DIR/$tfile
2406         touch $DIR/$tfile
2407         chmod 444 $DIR/$tfile
2408         chown $RUNAS_ID $DIR/$tfile
2409         log 33_1
2410         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2411         log 33_2
2412 }
2413 run_test 33aa "write file with mode 444 (should return error) ===="
2414
2415 test_33a() {
2416         rm -fr $DIR/d33
2417         test_mkdir -p $DIR/d33
2418         chown $RUNAS_ID $DIR/d33
2419         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2420         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2421                 error "open RDWR" || true
2422 }
2423 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2424
2425 test_33b() {
2426         rm -fr $DIR/d33
2427         test_mkdir -p $DIR/d33
2428         chown $RUNAS_ID $DIR/d33
2429         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2430 }
2431 run_test 33b "test open file with malformed flags (No panic and return error)"
2432
2433 test_33c() {
2434         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2435         local ostnum
2436         local ostname
2437         local write_bytes
2438         local all_zeros
2439
2440         remote_ost_nodsh && skip "remote OST with nodsh" && return
2441         all_zeros=:
2442         rm -fr $DIR/d33
2443         test_mkdir -p $DIR/d33
2444         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2445
2446         sync
2447         for ostnum in $(seq $OSTCOUNT); do
2448                 # test-framework's OST numbering is one-based, while Lustre's
2449                 # is zero-based
2450                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2451                 # Parsing llobdstat's output sucks; we could grep the /proc
2452                 # path, but that's likely to not be as portable as using the
2453                 # llobdstat utility.  So we parse lctl output instead.
2454                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2455                         obdfilter/$ostname/stats |
2456                         awk '/^write_bytes/ {print $7}' )
2457                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2458                 if (( ${write_bytes:-0} > 0 ))
2459                 then
2460                         all_zeros=false
2461                         break;
2462                 fi
2463         done
2464
2465         $all_zeros || return 0
2466
2467         # Write four bytes
2468         echo foo > $DIR/d33/bar
2469         # Really write them
2470         sync
2471
2472         # Total up write_bytes after writing.  We'd better find non-zeros.
2473         for ostnum in $(seq $OSTCOUNT); do
2474                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2475                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2476                         obdfilter/$ostname/stats |
2477                         awk '/^write_bytes/ {print $7}' )
2478                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2479                 if (( ${write_bytes:-0} > 0 ))
2480                 then
2481                         all_zeros=false
2482                         break;
2483                 fi
2484         done
2485
2486         if $all_zeros
2487         then
2488                 for ostnum in $(seq $OSTCOUNT); do
2489                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2490                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2491                         do_facet ost$ostnum lctl get_param -n \
2492                                 obdfilter/$ostname/stats
2493                 done
2494                 error "OST not keeping write_bytes stats (b22312)"
2495         fi
2496 }
2497 run_test 33c "test llobdstat and write_bytes"
2498
2499 test_33d() {
2500         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2502         local MDTIDX=1
2503         local remote_dir=$DIR/$tdir/remote_dir
2504
2505         mkdir -p $DIR/$tdir
2506         $LFS mkdir -i $MDTIDX $remote_dir ||
2507                 error "create remote directory failed"
2508
2509         touch $remote_dir/$tfile
2510         chmod 444 $remote_dir/$tfile
2511         chown $RUNAS_ID $remote_dir/$tfile
2512
2513         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2514
2515         chown $RUNAS_ID $remote_dir
2516         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2517                                         error "create" || true
2518         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2519                                     error "open RDWR" || true
2520         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2521                                     error "create" || true
2522 }
2523 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2524
2525 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2526 test_34a() {
2527         rm -f $DIR/f34
2528         $MCREATE $DIR/f34 || error
2529         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2530         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2531         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2532         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2533 }
2534 run_test 34a "truncate file that has not been opened ==========="
2535
2536 test_34b() {
2537         [ ! -f $DIR/f34 ] && test_34a
2538         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2539         $OPENFILE -f O_RDONLY $DIR/f34
2540         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2541         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2542 }
2543 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2544
2545 test_34c() {
2546         [ ! -f $DIR/f34 ] && test_34a
2547         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2548         $OPENFILE -f O_RDWR $DIR/f34
2549         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2550         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2551 }
2552 run_test 34c "O_RDWR opening file-with-size works =============="
2553
2554 test_34d() {
2555         [ ! -f $DIR/f34 ] && test_34a
2556         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2557         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2558         rm $DIR/f34
2559 }
2560 run_test 34d "write to sparse file ============================="
2561
2562 test_34e() {
2563         rm -f $DIR/f34e
2564         $MCREATE $DIR/f34e || error
2565         $TRUNCATE $DIR/f34e 1000 || error
2566         $CHECKSTAT -s 1000 $DIR/f34e || error
2567         $OPENFILE -f O_RDWR $DIR/f34e
2568         $CHECKSTAT -s 1000 $DIR/f34e || error
2569 }
2570 run_test 34e "create objects, some with size and some without =="
2571
2572 test_34f() { # bug 6242, 6243
2573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2574         SIZE34F=48000
2575         rm -f $DIR/f34f
2576         $MCREATE $DIR/f34f || error
2577         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2578         dd if=$DIR/f34f of=$TMP/f34f
2579         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2580         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2581         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2582         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2583         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2584 }
2585 run_test 34f "read from a file with no objects until EOF ======="
2586
2587 test_34g() {
2588         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2589         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2590         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2591         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2592         cancel_lru_locks osc
2593         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2594                 error "wrong size after lock cancel"
2595
2596         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2597         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2598                 error "expanding truncate failed"
2599         cancel_lru_locks osc
2600         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2601                 error "wrong expanded size after lock cancel"
2602 }
2603 run_test 34g "truncate long file ==============================="
2604
2605 test_34h() {
2606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2607         local gid=10
2608         local sz=1000
2609
2610         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2611         sync # Flush the cache so that multiop below does not block on cache
2612              # flush when getting the group lock
2613         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2614         MULTIPID=$!
2615
2616         # Since just timed wait is not good enough, let's do a sync write
2617         # that way we are sure enough time for a roundtrip + processing
2618         # passed + 2 seconds of extra margin.
2619         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2620         rm $DIR/${tfile}-1
2621         sleep 2
2622
2623         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2624                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2625                 kill -9 $MULTIPID
2626         fi
2627         wait $MULTIPID
2628         local nsz=`stat -c %s $DIR/$tfile`
2629         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2630 }
2631 run_test 34h "ftruncate file under grouplock should not block"
2632
2633 test_35a() {
2634         cp /bin/sh $DIR/f35a
2635         chmod 444 $DIR/f35a
2636         chown $RUNAS_ID $DIR/f35a
2637         $RUNAS $DIR/f35a && error || true
2638         rm $DIR/f35a
2639 }
2640 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2641
2642 test_36a() {
2643         rm -f $DIR/f36
2644         utime $DIR/f36 || error
2645 }
2646 run_test 36a "MDS utime check (mknod, utime) ==================="
2647
2648 test_36b() {
2649         echo "" > $DIR/f36
2650         utime $DIR/f36 || error
2651 }
2652 run_test 36b "OST utime check (open, utime) ===================="
2653
2654 test_36c() {
2655         rm -f $DIR/d36/f36
2656         test_mkdir $DIR/d36
2657         chown $RUNAS_ID $DIR/d36
2658         $RUNAS utime $DIR/d36/f36 || error
2659 }
2660 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2661
2662 test_36d() {
2663         [ ! -d $DIR/d36 ] && test_36c
2664         echo "" > $DIR/d36/f36
2665         $RUNAS utime $DIR/d36/f36 || error
2666 }
2667 run_test 36d "non-root OST utime check (open, utime) ==========="
2668
2669 test_36e() {
2670         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2671         test_mkdir -p $DIR/$tdir
2672         touch $DIR/$tdir/$tfile
2673         $RUNAS utime $DIR/$tdir/$tfile && \
2674                 error "utime worked, expected failure" || true
2675 }
2676 run_test 36e "utime on non-owned file (should return error) ===="
2677
2678 subr_36fh() {
2679         local fl="$1"
2680         local LANG_SAVE=$LANG
2681         local LC_LANG_SAVE=$LC_LANG
2682         export LANG=C LC_LANG=C # for date language
2683
2684         DATESTR="Dec 20  2000"
2685         test_mkdir -p $DIR/$tdir
2686         lctl set_param fail_loc=$fl
2687         date; date +%s
2688         cp /etc/hosts $DIR/$tdir/$tfile
2689         sync & # write RPC generated with "current" inode timestamp, but delayed
2690         sleep 1
2691         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2692         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2693         cancel_lru_locks osc
2694         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2695         date; date +%s
2696         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2697                 echo "BEFORE: $LS_BEFORE" && \
2698                 echo "AFTER : $LS_AFTER" && \
2699                 echo "WANT  : $DATESTR" && \
2700                 error "$DIR/$tdir/$tfile timestamps changed" || true
2701
2702         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2703 }
2704
2705 test_36f() {
2706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2707         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2708         subr_36fh "0x80000214"
2709 }
2710 run_test 36f "utime on file racing with OST BRW write =========="
2711
2712 test_36g() {
2713         remote_ost_nodsh && skip "remote OST with nodsh" && return
2714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2715         local fmd_max_age
2716         local fmd_before
2717         local fmd_after
2718
2719         test_mkdir -p $DIR/$tdir
2720         fmd_max_age=$(do_facet ost1 \
2721                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2722                 head -n 1")
2723
2724         fmd_before=$(do_facet ost1 \
2725                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2726         touch $DIR/$tdir/$tfile
2727         sleep $((fmd_max_age + 12))
2728         fmd_after=$(do_facet ost1 \
2729                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2730
2731         echo "fmd_before: $fmd_before"
2732         echo "fmd_after: $fmd_after"
2733         [ "$fmd_after" -gt "$fmd_before" ] && \
2734                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2735                 error "fmd didn't expire after ping" || true
2736 }
2737 run_test 36g "filter mod data cache expiry ====================="
2738
2739 test_36h() {
2740         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2741         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2742         subr_36fh "0x80000227"
2743 }
2744 run_test 36h "utime on file racing with OST BRW write =========="
2745
2746 # test_37 - duplicate with tests 32q 32r
2747
2748 test_38() {
2749         local file=$DIR/$tfile
2750         touch $file
2751         openfile -f O_DIRECTORY $file
2752         local RC=$?
2753         local ENOTDIR=20
2754         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2755         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2756 }
2757 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2758
2759 test_39() {
2760         touch $DIR/$tfile
2761         touch $DIR/${tfile}2
2762 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2763 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2764 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2765         sleep 2
2766         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2767         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2768                 echo "mtime"
2769                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2770                 echo "atime"
2771                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2772                 echo "ctime"
2773                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2774                 error "O_TRUNC didn't change timestamps"
2775         fi
2776 }
2777 run_test 39 "mtime changed on create ==========================="
2778
2779 test_39b() {
2780         test_mkdir -p $DIR/$tdir
2781         cp -p /etc/passwd $DIR/$tdir/fopen
2782         cp -p /etc/passwd $DIR/$tdir/flink
2783         cp -p /etc/passwd $DIR/$tdir/funlink
2784         cp -p /etc/passwd $DIR/$tdir/frename
2785         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2786
2787         sleep 1
2788         echo "aaaaaa" >> $DIR/$tdir/fopen
2789         echo "aaaaaa" >> $DIR/$tdir/flink
2790         echo "aaaaaa" >> $DIR/$tdir/funlink
2791         echo "aaaaaa" >> $DIR/$tdir/frename
2792
2793         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2794         local link_new=`stat -c %Y $DIR/$tdir/flink`
2795         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2796         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2797
2798         cat $DIR/$tdir/fopen > /dev/null
2799         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2800         rm -f $DIR/$tdir/funlink2
2801         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2802
2803         for (( i=0; i < 2; i++ )) ; do
2804                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2805                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2806                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2807                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2808
2809                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2810                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2811                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2812                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2813
2814                 cancel_lru_locks osc
2815                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2816         done
2817 }
2818 run_test 39b "mtime change on open, link, unlink, rename  ======"
2819
2820 # this should be set to past
2821 TEST_39_MTIME=`date -d "1 year ago" +%s`
2822
2823 # bug 11063
2824 test_39c() {
2825         touch $DIR1/$tfile
2826         sleep 2
2827         local mtime0=`stat -c %Y $DIR1/$tfile`
2828
2829         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2830         local mtime1=`stat -c %Y $DIR1/$tfile`
2831         [ "$mtime1" = $TEST_39_MTIME ] || \
2832                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2833
2834         local d1=`date +%s`
2835         echo hello >> $DIR1/$tfile
2836         local d2=`date +%s`
2837         local mtime2=`stat -c %Y $DIR1/$tfile`
2838         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2839                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2840
2841         mv $DIR1/$tfile $DIR1/$tfile-1
2842
2843         for (( i=0; i < 2; i++ )) ; do
2844                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2845                 [ "$mtime2" = "$mtime3" ] || \
2846                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2847
2848                 cancel_lru_locks osc
2849                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2850         done
2851 }
2852 run_test 39c "mtime change on rename ==========================="
2853
2854 # bug 21114
2855 test_39d() {
2856         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2857         touch $DIR1/$tfile
2858
2859         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2860
2861         for (( i=0; i < 2; i++ )) ; do
2862                 local mtime=`stat -c %Y $DIR1/$tfile`
2863                 [ $mtime = $TEST_39_MTIME ] || \
2864                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2865
2866                 cancel_lru_locks osc
2867                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2868         done
2869 }
2870 run_test 39d "create, utime, stat =============================="
2871
2872 # bug 21114
2873 test_39e() {
2874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2875         touch $DIR1/$tfile
2876         local mtime1=`stat -c %Y $DIR1/$tfile`
2877
2878         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2879
2880         for (( i=0; i < 2; i++ )) ; do
2881                 local mtime2=`stat -c %Y $DIR1/$tfile`
2882                 [ $mtime2 = $TEST_39_MTIME ] || \
2883                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2884
2885                 cancel_lru_locks osc
2886                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2887         done
2888 }
2889 run_test 39e "create, stat, utime, stat ========================"
2890
2891 # bug 21114
2892 test_39f() {
2893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2894         touch $DIR1/$tfile
2895         mtime1=`stat -c %Y $DIR1/$tfile`
2896
2897         sleep 2
2898         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2899
2900         for (( i=0; i < 2; i++ )) ; do
2901                 local mtime2=`stat -c %Y $DIR1/$tfile`
2902                 [ $mtime2 = $TEST_39_MTIME ] || \
2903                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2904
2905                 cancel_lru_locks osc
2906                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2907         done
2908 }
2909 run_test 39f "create, stat, sleep, utime, stat ================="
2910
2911 # bug 11063
2912 test_39g() {
2913         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2914         echo hello >> $DIR1/$tfile
2915         local mtime1=`stat -c %Y $DIR1/$tfile`
2916
2917         sleep 2
2918         chmod o+r $DIR1/$tfile
2919
2920         for (( i=0; i < 2; i++ )) ; do
2921                 local mtime2=`stat -c %Y $DIR1/$tfile`
2922                 [ "$mtime1" = "$mtime2" ] || \
2923                         error "lost mtime: $mtime2, should be $mtime1"
2924
2925                 cancel_lru_locks osc
2926                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2927         done
2928 }
2929 run_test 39g "write, chmod, stat ==============================="
2930
2931 # bug 11063
2932 test_39h() {
2933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2934         touch $DIR1/$tfile
2935         sleep 1
2936
2937         local d1=`date`
2938         echo hello >> $DIR1/$tfile
2939         local mtime1=`stat -c %Y $DIR1/$tfile`
2940
2941         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2942         local d2=`date`
2943         if [ "$d1" != "$d2" ]; then
2944                 echo "write and touch not within one second"
2945         else
2946                 for (( i=0; i < 2; i++ )) ; do
2947                         local mtime2=`stat -c %Y $DIR1/$tfile`
2948                         [ "$mtime2" = $TEST_39_MTIME ] || \
2949                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2950
2951                         cancel_lru_locks osc
2952                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2953                 done
2954         fi
2955 }
2956 run_test 39h "write, utime within one second, stat ============="
2957
2958 test_39i() {
2959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2960         touch $DIR1/$tfile
2961         sleep 1
2962
2963         echo hello >> $DIR1/$tfile
2964         local mtime1=`stat -c %Y $DIR1/$tfile`
2965
2966         mv $DIR1/$tfile $DIR1/$tfile-1
2967
2968         for (( i=0; i < 2; i++ )) ; do
2969                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2970
2971                 [ "$mtime1" = "$mtime2" ] || \
2972                         error "lost mtime: $mtime2, should be $mtime1"
2973
2974                 cancel_lru_locks osc
2975                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2976         done
2977 }
2978 run_test 39i "write, rename, stat =============================="
2979
2980 test_39j() {
2981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2982         start_full_debug_logging
2983         touch $DIR1/$tfile
2984         sleep 1
2985
2986         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2987         lctl set_param fail_loc=0x80000412
2988         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2989                 error "multiop failed"
2990         local multipid=$!
2991         local mtime1=`stat -c %Y $DIR1/$tfile`
2992
2993         mv $DIR1/$tfile $DIR1/$tfile-1
2994
2995         kill -USR1 $multipid
2996         wait $multipid || error "multiop close failed"
2997
2998         for (( i=0; i < 2; i++ )) ; do
2999                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3000                 [ "$mtime1" = "$mtime2" ] ||
3001                         error "mtime is lost on close: $mtime2, " \
3002                               "should be $mtime1"
3003
3004                 cancel_lru_locks osc
3005                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3006         done
3007         lctl set_param fail_loc=0
3008         stop_full_debug_logging
3009 }
3010 run_test 39j "write, rename, close, stat ======================="
3011
3012 test_39k() {
3013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3014         touch $DIR1/$tfile
3015         sleep 1
3016
3017         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3018         local multipid=$!
3019         local mtime1=`stat -c %Y $DIR1/$tfile`
3020
3021         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3022
3023         kill -USR1 $multipid
3024         wait $multipid || error "multiop close failed"
3025
3026         for (( i=0; i < 2; i++ )) ; do
3027                 local mtime2=`stat -c %Y $DIR1/$tfile`
3028
3029                 [ "$mtime2" = $TEST_39_MTIME ] || \
3030                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3031
3032                 cancel_lru_locks osc
3033                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3034         done
3035 }
3036 run_test 39k "write, utime, close, stat ========================"
3037
3038 # this should be set to future
3039 TEST_39_ATIME=`date -d "1 year" +%s`
3040
3041 test_39l() {
3042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3043         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3044         local atime_diff=$(do_facet $SINGLEMDS \
3045                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3046         rm -rf $DIR/$tdir
3047         mkdir -p $DIR/$tdir
3048
3049         # test setting directory atime to future
3050         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3051         local atime=$(stat -c %X $DIR/$tdir)
3052         [ "$atime" = $TEST_39_ATIME ] || \
3053                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3054
3055         # test setting directory atime from future to now
3056         local d1=$(date +%s)
3057         ls $DIR/$tdir
3058         local d2=$(date +%s)
3059
3060         cancel_lru_locks mdc
3061         atime=$(stat -c %X $DIR/$tdir)
3062         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3063                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3064
3065         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3066         sleep 3
3067
3068         # test setting directory atime when now > dir atime + atime_diff
3069         d1=$(date +%s)
3070         ls $DIR/$tdir
3071         d2=$(date +%s)
3072         cancel_lru_locks mdc
3073         atime=$(stat -c %X $DIR/$tdir)
3074         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3075                 error "atime is not updated  : $atime, should be $d2"
3076
3077         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3078         sleep 3
3079
3080         # test not setting directory atime when now < dir atime + atime_diff
3081         ls $DIR/$tdir
3082         cancel_lru_locks mdc
3083         atime=$(stat -c %X $DIR/$tdir)
3084         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3085                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3086
3087         do_facet $SINGLEMDS \
3088                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3089 }
3090 run_test 39l "directory atime update ==========================="
3091
3092 test_39m() {
3093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3094         touch $DIR1/$tfile
3095         sleep 2
3096         local far_past_mtime=$(date -d "May 29 1953" +%s)
3097         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3098
3099         touch -m -d @$far_past_mtime $DIR1/$tfile
3100         touch -a -d @$far_past_atime $DIR1/$tfile
3101
3102         for (( i=0; i < 2; i++ )) ; do
3103                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3104                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3105                         error "atime or mtime set incorrectly"
3106
3107                 cancel_lru_locks osc
3108                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3109         done
3110 }
3111 run_test 39m "test atime and mtime before 1970"
3112
3113 test_39n() { # LU-3832
3114         local atime_diff=$(do_facet $SINGLEMDS \
3115                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3116         local atime0
3117         local atime1
3118         local atime2
3119
3120         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3121
3122         rm -rf $DIR/$tfile
3123         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3124         atime0=$(stat -c %X $DIR/$tfile)
3125
3126         sleep 5
3127         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3128         atime1=$(stat -c %X $DIR/$tfile)
3129
3130         sleep 5
3131         cancel_lru_locks mdc
3132         cancel_lru_locks osc
3133         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3134         atime2=$(stat -c %X $DIR/$tfile)
3135
3136         do_facet $SINGLEMDS \
3137                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3138
3139         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3140         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3141 }
3142 run_test 39n "check that O_NOATIME is honored"
3143
3144 test_39o() {
3145         TESTDIR=$DIR/$tdir/$tfile
3146         [ -e $TESTDIR ] && rm -rf $TESTDIR
3147         test_mkdir -p $TESTDIR
3148         cd $TESTDIR
3149         links1=2
3150         ls
3151         mkdir a b
3152         ls
3153         links2=$(stat -c %h .)
3154         [ $(($links1 + 2)) != $links2 ] &&
3155                 error "wrong links count $(($links1 + 2)) != $links2"
3156         rmdir b
3157         links3=$(stat -c %h .)
3158         [ $(($links1 + 1)) != $links3 ] &&
3159                 error "wrong links count $links1 != $links3"
3160         return 0
3161 }
3162 run_test 39o "directory cached attributes updated after create ========"
3163
3164 test_39p() {
3165         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3166         local MDTIDX=1
3167         TESTDIR=$DIR/$tdir/$tfile
3168         [ -e $TESTDIR ] && rm -rf $TESTDIR
3169         mkdir -p $TESTDIR
3170         cd $TESTDIR
3171         links1=2
3172         ls
3173         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3174         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3175         ls
3176         links2=$(stat -c %h .)
3177         [ $(($links1 + 2)) != $links2 ] &&
3178                 error "wrong links count $(($links1 + 2)) != $links2"
3179         rmdir remote_dir2
3180         links3=$(stat -c %h .)
3181         [ $(($links1 + 1)) != $links3 ] &&
3182                 error "wrong links count $links1 != $links3"
3183         return 0
3184 }
3185 run_test 39p "remote directory cached attributes updated after create ========"
3186
3187
3188 test_40() {
3189         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3190         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3191                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3192         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3193                 error "$tfile is not 4096 bytes in size"
3194 }
3195 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3196
3197 test_41() {
3198         # bug 1553
3199         small_write $DIR/f41 18
3200 }
3201 run_test 41 "test small file write + fstat ====================="
3202
3203 count_ost_writes() {
3204         lctl get_param -n osc.*.stats |
3205             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
3206 }
3207
3208 # decent default
3209 WRITEBACK_SAVE=500
3210 DIRTY_RATIO_SAVE=40
3211 MAX_DIRTY_RATIO=50
3212 BG_DIRTY_RATIO_SAVE=10
3213 MAX_BG_DIRTY_RATIO=25
3214
3215 start_writeback() {
3216         trap 0
3217         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3218         # dirty_ratio, dirty_background_ratio
3219         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3220                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3221                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3222                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3223         else
3224                 # if file not here, we are a 2.4 kernel
3225                 kill -CONT `pidof kupdated`
3226         fi
3227 }
3228
3229 stop_writeback() {
3230         # setup the trap first, so someone cannot exit the test at the
3231         # exact wrong time and mess up a machine
3232         trap start_writeback EXIT
3233         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3234         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3235                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3236                 sysctl -w vm.dirty_writeback_centisecs=0
3237                 sysctl -w vm.dirty_writeback_centisecs=0
3238                 # save and increase /proc/sys/vm/dirty_ratio
3239                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3240                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3241                 # save and increase /proc/sys/vm/dirty_background_ratio
3242                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3243                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3244         else
3245                 # if file not here, we are a 2.4 kernel
3246                 kill -STOP `pidof kupdated`
3247         fi
3248 }
3249
3250 # ensure that all stripes have some grant before we test client-side cache
3251 setup_test42() {
3252         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3253                 dd if=/dev/zero of=$i bs=4k count=1
3254                 rm $i
3255         done
3256 }
3257
3258 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3259 # file truncation, and file removal.
3260 test_42a() {
3261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3262         setup_test42
3263         cancel_lru_locks osc
3264         stop_writeback
3265         sync; sleep 1; sync # just to be safe
3266         BEFOREWRITES=`count_ost_writes`
3267         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3268         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3269         AFTERWRITES=`count_ost_writes`
3270         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3271                 error "$BEFOREWRITES < $AFTERWRITES"
3272         start_writeback
3273 }
3274 run_test 42a "ensure that we don't flush on close =============="
3275
3276 test_42b() {
3277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3278         setup_test42
3279         cancel_lru_locks osc
3280         stop_writeback
3281         sync
3282         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3283         BEFOREWRITES=`count_ost_writes`
3284         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3285         AFTERWRITES=`count_ost_writes`
3286         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3287                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3288         fi
3289         BEFOREWRITES=`count_ost_writes`
3290         sync || error "sync: $?"
3291         AFTERWRITES=`count_ost_writes`
3292         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3293                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3294         fi
3295         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3296         start_writeback
3297         return 0
3298 }
3299 run_test 42b "test destroy of file with cached dirty data ======"
3300
3301 # if these tests just want to test the effect of truncation,
3302 # they have to be very careful.  consider:
3303 # - the first open gets a {0,EOF}PR lock
3304 # - the first write conflicts and gets a {0, count-1}PW
3305 # - the rest of the writes are under {count,EOF}PW
3306 # - the open for truncate tries to match a {0,EOF}PR
3307 #   for the filesize and cancels the PWs.
3308 # any number of fixes (don't get {0,EOF} on open, match
3309 # composite locks, do smarter file size management) fix
3310 # this, but for now we want these tests to verify that
3311 # the cancellation with truncate intent works, so we
3312 # start the file with a full-file pw lock to match against
3313 # until the truncate.
3314 trunc_test() {
3315         test=$1
3316         file=$DIR/$test
3317         offset=$2
3318         cancel_lru_locks osc
3319         stop_writeback
3320         # prime the file with 0,EOF PW to match
3321         touch $file
3322         $TRUNCATE $file 0
3323         sync; sync
3324         # now the real test..
3325         dd if=/dev/zero of=$file bs=1024 count=100
3326         BEFOREWRITES=`count_ost_writes`
3327         $TRUNCATE $file $offset
3328         cancel_lru_locks osc
3329         AFTERWRITES=`count_ost_writes`
3330         start_writeback
3331 }
3332
3333 test_42c() {
3334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3335         trunc_test 42c 1024
3336         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3337             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3338         rm $file
3339 }
3340 run_test 42c "test partial truncate of file with cached dirty data"
3341
3342 test_42d() {
3343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3344         trunc_test 42d 0
3345         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3346             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3347         rm $file
3348 }
3349 run_test 42d "test complete truncate of file with cached dirty data"
3350
3351 test_42e() { # bug22074
3352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3353         local TDIR=$DIR/${tdir}e
3354         local pagesz=$(page_size)
3355         local pages=16 # hardcoded 16 pages, don't change it.
3356         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3357         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3358         local max_dirty_mb
3359         local warmup_files
3360
3361         test_mkdir -p $DIR/${tdir}e
3362         $SETSTRIPE -c 1 $TDIR
3363         createmany -o $TDIR/f $files
3364
3365         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3366
3367         # we assume that with $OSTCOUNT files, at least one of them will
3368         # be allocated on OST0.
3369         warmup_files=$((OSTCOUNT * max_dirty_mb))
3370         createmany -o $TDIR/w $warmup_files
3371
3372         # write a large amount of data into one file and sync, to get good
3373         # avail_grant number from OST.
3374         for ((i=0; i<$warmup_files; i++)); do
3375                 idx=$($GETSTRIPE -i $TDIR/w$i)
3376                 [ $idx -ne 0 ] && continue
3377                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3378                 break
3379         done
3380         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3381         sync
3382         $LCTL get_param $proc_osc0/cur_dirty_bytes
3383         $LCTL get_param $proc_osc0/cur_grant_bytes
3384
3385         # create as much dirty pages as we can while not to trigger the actual
3386         # RPCs directly. but depends on the env, VFS may trigger flush during this
3387         # period, hopefully we are good.
3388         for ((i=0; i<$warmup_files; i++)); do
3389                 idx=$($GETSTRIPE -i $TDIR/w$i)
3390                 [ $idx -ne 0 ] && continue
3391                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3392         done
3393         $LCTL get_param $proc_osc0/cur_dirty_bytes
3394         $LCTL get_param $proc_osc0/cur_grant_bytes
3395
3396         # perform the real test
3397         $LCTL set_param $proc_osc0/rpc_stats 0
3398         for ((;i<$files; i++)); do
3399                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3400                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3401         done
3402         sync
3403         $LCTL get_param $proc_osc0/rpc_stats
3404
3405         local percent=0
3406         local have_ppr=false
3407         $LCTL get_param $proc_osc0/rpc_stats |
3408                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3409                         # skip lines until we are at the RPC histogram data
3410                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3411                         $have_ppr || continue
3412
3413                         # we only want the percent stat for < 16 pages
3414                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3415
3416                         percent=$((percent + WPCT))
3417                         if [ $percent -gt 15 ]; then
3418                                 error "less than 16-pages write RPCs" \
3419                                       "$percent% > 15%"
3420                                 break
3421                         fi
3422                 done
3423         rm -rf $TDIR
3424 }
3425 run_test 42e "verify sub-RPC writes are not done synchronously"
3426
3427 test_43() {
3428         test_mkdir -p $DIR/$tdir
3429         cp -p /bin/ls $DIR/$tdir/$tfile
3430         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3431         pid=$!
3432         # give multiop a chance to open
3433         sleep 1
3434
3435         $DIR/$tdir/$tfile && error || true
3436         kill -USR1 $pid
3437 }
3438 run_test 43 "execution of file opened for write should return -ETXTBSY"
3439
3440 test_43a() {
3441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3442         test_mkdir -p $DIR/$tdir
3443         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3444                         cp -p multiop $DIR/$tdir/multiop
3445         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3446                         return 1
3447         MULTIOP_PID=$!
3448         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3449         kill -USR1 $MULTIOP_PID || return 2
3450         wait $MULTIOP_PID || return 3
3451         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3452 }
3453 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3454
3455 test_43b() {
3456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3457         test_mkdir -p $DIR/$tdir
3458         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3459                         cp -p multiop $DIR/$tdir/multiop
3460         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3461                         return 1
3462         MULTIOP_PID=$!
3463         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3464         kill -USR1 $MULTIOP_PID || return 2
3465         wait $MULTIOP_PID || return 3
3466         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3467 }
3468 run_test 43b "truncate of file being executed should return -ETXTBSY"
3469
3470 test_43c() {
3471         local testdir="$DIR/$tdir"
3472         test_mkdir -p $DIR/$tdir
3473         cp $SHELL $testdir/
3474         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3475                 ( cd $testdir && md5sum -c)
3476 }
3477 run_test 43c "md5sum of copy into lustre========================"
3478
3479 test_44() {
3480         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3481         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3482         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3483 }
3484 run_test 44 "zero length read from a sparse stripe ============="
3485
3486 test_44a() {
3487     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3488                          awk '{print $2}'`
3489     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3490     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3491     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3492                       awk '{print $2}'`
3493     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3494         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3495     fi
3496
3497     OFFSETS="0 $((stride/2)) $((stride-1))"
3498     for offset in $OFFSETS ; do
3499       for i in `seq 0 $((nstripe-1))`; do
3500         local GLOBALOFFSETS=""
3501         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3502         local myfn=$DIR/d44a-$size
3503         echo "--------writing $myfn at $size"
3504         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3505         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3506         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3507                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3508
3509         for j in `seq 0 $((nstripe-1))`; do
3510             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3511             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3512             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3513         done
3514         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3515                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3516         rm -f $myfn
3517       done
3518     done
3519 }
3520 run_test 44a "test sparse pwrite ==============================="
3521
3522 dirty_osc_total() {
3523         tot=0
3524         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3525                 tot=$(($tot + $d))
3526         done
3527         echo $tot
3528 }
3529 do_dirty_record() {
3530         before=`dirty_osc_total`
3531         echo executing "\"$*\""
3532         eval $*
3533         after=`dirty_osc_total`
3534         echo before $before, after $after
3535 }
3536 test_45() {
3537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3538         f="$DIR/f45"
3539         # Obtain grants from OST if it supports it
3540         echo blah > ${f}_grant
3541         stop_writeback
3542         sync
3543         do_dirty_record "echo blah > $f"
3544         [ $before -eq $after ] && error "write wasn't cached"
3545         do_dirty_record "> $f"
3546         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3547         do_dirty_record "echo blah > $f"
3548         [ $before -eq $after ] && error "write wasn't cached"
3549         do_dirty_record "sync"
3550         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3551         do_dirty_record "echo blah > $f"
3552         [ $before -eq $after ] && error "write wasn't cached"
3553         do_dirty_record "cancel_lru_locks osc"
3554         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3555         start_writeback
3556 }
3557 run_test 45 "osc io page accounting ============================"
3558
3559 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3560 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3561 # objects offset and an assert hit when an rpc was built with 1023's mapped
3562 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3563 test_46() {
3564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3565         f="$DIR/f46"
3566         stop_writeback
3567         sync
3568         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3569         sync
3570         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3571         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3572         sync
3573         start_writeback
3574 }
3575 run_test 46 "dirtying a previously written page ================"
3576
3577 # test_47 is removed "Device nodes check" is moved to test_28
3578
3579 test_48a() { # bug 2399
3580         check_kernel_version 34 || return 0
3581         test_mkdir -p $DIR/$tdir
3582         cd $DIR/$tdir
3583         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3584         test_mkdir $DIR/$tdir || error "recreate directory failed"
3585         touch foo || error "'touch foo' failed after recreating cwd"
3586         test_mkdir $DIR/$tdir/bar ||
3587                      error "'mkdir foo' failed after recreating cwd"
3588         if check_kernel_version 44; then
3589                 touch .foo || error "'touch .foo' failed after recreating cwd"
3590                 test_mkdir $DIR/$tdir/.bar ||
3591                               error "'mkdir .foo' failed after recreating cwd"
3592         fi
3593         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3594         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3595         cd . || error "'cd .' failed after recreating cwd"
3596         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3597         rmdir . && error "'rmdir .' worked after recreating cwd"
3598         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3599         cd .. || error "'cd ..' failed after recreating cwd"
3600 }
3601 run_test 48a "Access renamed working dir (should return errors)="
3602
3603 test_48b() { # bug 2399
3604         check_kernel_version 34 || return 0
3605         rm -rf $DIR/$tdir
3606         test_mkdir -p $DIR/$tdir
3607         cd $DIR/$tdir
3608         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3609         touch foo && error "'touch foo' worked after removing cwd"
3610         test_mkdir $DIR/$tdir/foo &&
3611                      error "'mkdir foo' worked after removing cwd"
3612         if check_kernel_version 44; then
3613                 touch .foo && error "'touch .foo' worked after removing cwd"
3614                 test_mkdir $DIR/$tdir/.foo &&
3615                               error "'mkdir .foo' worked after removing cwd"
3616         fi
3617         ls . > /dev/null && error "'ls .' worked after removing cwd"
3618         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3619         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3620         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3621         rmdir . && error "'rmdir .' worked after removing cwd"
3622         ln -s . foo && error "'ln -s .' worked after removing cwd"
3623         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3624 }
3625 run_test 48b "Access removed working dir (should return errors)="
3626
3627 test_48c() { # bug 2350
3628         check_kernel_version 36 || return 0
3629         #lctl set_param debug=-1
3630         #set -vx
3631         rm -rf $DIR/$tdir
3632         test_mkdir -p $DIR/$tdir/dir
3633         cd $DIR/$tdir/dir
3634         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3635         $TRACE touch foo && error "touch foo worked after removing cwd"
3636         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3637         if check_kernel_version 44; then
3638                 touch .foo && error "touch .foo worked after removing cwd"
3639                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3640         fi
3641         $TRACE ls . && error "'ls .' worked after removing cwd"
3642         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3643         is_patchless || ( $TRACE cd . &&
3644                         error "'cd .' worked after removing cwd" )
3645         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3646         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3647         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3648         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3649 }
3650 run_test 48c "Access removed working subdir (should return errors)"
3651
3652 test_48d() { # bug 2350
3653         check_kernel_version 36 || return 0
3654         #lctl set_param debug=-1
3655         #set -vx
3656         rm -rf $DIR/$tdir
3657         test_mkdir -p $DIR/$tdir/dir
3658         cd $DIR/$tdir/dir
3659         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3660         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3661         $TRACE touch foo && error "'touch foo' worked after removing parent"
3662         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3663         if check_kernel_version 44; then
3664                 touch .foo && error "'touch .foo' worked after removing parent"
3665                 test_mkdir .foo &&
3666                               error "mkdir .foo worked after removing parent"
3667         fi
3668         $TRACE ls . && error "'ls .' worked after removing parent"
3669         $TRACE ls .. && error "'ls ..' worked after removing parent"
3670         is_patchless || ( $TRACE cd . &&
3671                         error "'cd .' worked after recreate parent" )
3672         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3673         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3674         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3675         is_patchless || ( $TRACE cd .. &&
3676                         error "'cd ..' worked after removing parent" || true )
3677 }
3678 run_test 48d "Access removed parent subdir (should return errors)"
3679
3680 test_48e() { # bug 4134
3681         check_kernel_version 41 || return 0
3682         #lctl set_param debug=-1
3683         #set -vx
3684         rm -rf $DIR/$tdir
3685         test_mkdir -p $DIR/$tdir/dir
3686         cd $DIR/$tdir/dir
3687         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3688         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3689         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3690         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3691         # On a buggy kernel addition of "touch foo" after cd .. will
3692         # produce kernel oops in lookup_hash_it
3693         touch ../foo && error "'cd ..' worked after recreate parent"
3694         cd $DIR
3695         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3696 }
3697 run_test 48e "Access to recreated parent subdir (should return errors)"
3698
3699 test_49() { # LU-1030
3700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3701         # get ost1 size - lustre-OST0000
3702         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3703         # write 800M at maximum
3704         [ $ost1_size -gt 819200 ] && ost1_size=819200
3705
3706         lfs setstripe -c 1 -i 0 $DIR/$tfile
3707         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3708         local dd_pid=$!
3709
3710         # change max_pages_per_rpc while writing the file
3711         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3712         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3713         # loop until dd process exits
3714         while ps ax -opid | grep -wq $dd_pid; do
3715                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3716                 sleep $((RANDOM % 5 + 1))
3717         done
3718         # restore original max_pages_per_rpc
3719         $LCTL set_param $osc1_mppc=$orig_mppc
3720         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3721 }
3722 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3723
3724 test_50() {
3725         # bug 1485
3726         test_mkdir $DIR/$tdir
3727         cd $DIR/$tdir
3728         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3729 }
3730 run_test 50 "special situations: /proc symlinks  ==============="
3731
3732 test_51a() {    # was test_51
3733         # bug 1516 - create an empty entry right after ".." then split dir
3734         test_mkdir -p $DIR/$tdir
3735         touch $DIR/$tdir/foo
3736         $MCREATE $DIR/$tdir/bar
3737         rm $DIR/$tdir/foo
3738         createmany -m $DIR/$tdir/longfile 201
3739         FNUM=202
3740         while [ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]; do
3741                 $MCREATE $DIR/$tdir/longfile$FNUM
3742                 FNUM=$(($FNUM + 1))
3743                 echo -n "+"
3744         done
3745         echo
3746         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3747 }
3748 run_test 51a "special situations: split htree with empty entry =="
3749
3750 export NUMTEST=70000
3751 test_51b() {
3752         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3753         local BASE=$DIR/d${base}.${TESTSUITE}
3754
3755         # cleanup the directory
3756         rm -fr $BASE
3757
3758         test_mkdir -p $BASE
3759
3760         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3761         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3762         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3763                 return
3764
3765         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3766                 echo "reduced count to $NUMTEST due to inodes"
3767
3768         # need to check free space for the directories as well
3769         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3770         numfree=$((blkfree / 4))
3771         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3772                 echo "reduced count to $NUMTEST due to blocks"
3773
3774         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3775                 echo "failed" > $BASE/fnum
3776 }
3777 run_test 51b "exceed 64k subdirectory nlink limit"
3778
3779 test_51ba() { # LU-993
3780         local BASE=$DIR/d${base}.${TESTSUITE}
3781         # unlink all but 100 subdirectories, then check it still works
3782         local LEFT=100
3783         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3784
3785         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3786         local DELETE=$((NUMTEST - LEFT))
3787
3788         # continue on to run this test even if 51b didn't finish,
3789         # just to delete the many subdirectories created.
3790         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3791
3792         # for ldiskfs the nlink count should be 1, but this is OSD specific
3793         # and so this is listed for informational purposes only
3794         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3795         unlinkmany -d $BASE/d $DELETE
3796         RC=$?
3797
3798         if [ $RC -ne 0 ]; then
3799                 if [ "$NUMPREV" == "failed" ]; then
3800                         skip "previous setup failed"
3801                         return 0
3802                 else
3803                         error "unlink of first $DELETE subdirs failed"
3804                         return $RC
3805                 fi
3806         fi
3807
3808         echo "nlink between: $(stat -c %h $BASE)"
3809         # trim the first line of ls output
3810         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3811         [ $FOUND -ne $LEFT ] &&
3812                 error "can't find subdirs: found only $FOUND/$LEFT"
3813
3814         unlinkmany -d $BASE/d $DELETE $LEFT ||
3815                 error "unlink of second $LEFT subdirs failed"
3816         # regardless of whether the backing filesystem tracks nlink accurately
3817         # or not, the nlink count shouldn't be more than "." and ".." here
3818         local AFTER=$(stat -c %h $BASE)
3819         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3820                 echo "nlink after: $AFTER"
3821 }
3822 run_test 51ba "verify nlink for many subdirectory cleanup"
3823
3824 test_51d() {
3825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3826         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3827         test_mkdir -p $DIR/$tdir
3828         createmany -o $DIR/$tdir/t- 1000
3829         $GETSTRIPE $DIR/$tdir > $TMP/files
3830         for N in `seq 0 $((OSTCOUNT - 1))`; do
3831             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3832             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3833             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3834         done
3835         unlinkmany $DIR/$tdir/t- 1000
3836
3837         NLAST=0
3838         for N in `seq 1 $((OSTCOUNT - 1))`; do
3839             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3840                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3841             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3842                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3843
3844             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3845                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3846             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3847                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3848             NLAST=$N
3849         done
3850 }
3851 run_test 51d "check object distribution ===================="
3852
3853 test_52a() {
3854         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3855         test_mkdir -p $DIR/$tdir
3856         touch $DIR/$tdir/foo
3857         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3858         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3859         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3860         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3861         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3862                                         error "link worked"
3863         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3864         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3865         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3866                                                      error "lsattr"
3867         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3868         cp -r $DIR/$tdir /tmp/
3869         rm -fr $DIR/$tdir || error "cleanup rm failed"
3870 }
3871 run_test 52a "append-only flag test (should return errors) ====="
3872
3873 test_52b() {
3874         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3875         test_mkdir -p $DIR/$tdir
3876         touch $DIR/$tdir/foo
3877         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3878         cat test > $DIR/$tdir/foo && error "cat test worked"
3879         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3880         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3881         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3882                                         error "link worked"
3883         echo foo >> $DIR/$tdir/foo && error "echo worked"
3884         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3885         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
3886         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
3887         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3888                                                         error "lsattr"
3889         chattr -i $DIR/$tdir/foo || error "chattr failed"
3890
3891         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
3892 }
3893 run_test 52b "immutable flag test (should return errors) ======="
3894
3895 test_53() {
3896         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3897         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3898         remote_ost_nodsh && skip "remote OST with nodsh" && return
3899
3900         local param
3901         local param_seq
3902         local ostname
3903         local mds_last
3904         local mds_last_seq
3905         local ost_last
3906         local ost_last_seq
3907         local ost_last_id
3908         local ostnum
3909         local node
3910         local found=false
3911         local support_last_seq=true
3912
3913         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
3914                 support_last_seq=false
3915
3916         # only test MDT0000
3917         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3918         local value
3919         for value in $(do_facet $SINGLEMDS \
3920                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
3921                 param=$(echo ${value[0]} | cut -d "=" -f1)
3922                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
3923
3924                 if $support_last_seq; then
3925                         param_seq=$(echo $param |
3926                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
3927                         mds_last_seq=$(do_facet $SINGLEMDS \
3928                                        $LCTL get_param -n $param_seq)
3929                 fi
3930                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
3931
3932                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3933                 node=$(facet_active_host ost$((ostnum+1)))
3934                 param="obdfilter.$ostname.last_id"
3935                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
3936                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
3937                         ost_last_id=$ost_last
3938
3939                         if $support_last_seq; then
3940                                 ost_last_id=$(echo $ost_last |
3941                                               awk -F':' '{print $2}' |
3942                                               sed -e "s/^0x//g")
3943                                 ost_last_seq=$(echo $ost_last |
3944                                                awk -F':' '{print $1}')
3945                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
3946                         fi
3947
3948                         if [[ $ost_last_id != $mds_last ]]; then
3949                                 error "$ost_last_id != $mds_last"
3950                         else
3951                                 found=true
3952                                 break
3953                         fi
3954                 done
3955         done
3956         $found || error "can not match last_seq/last_id for $mdtosc"
3957         return 0
3958 }
3959 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3960
3961 test_54a() {
3962         $SOCKETSERVER $DIR/socket ||
3963                 error "$SOCKETSERVER $DIR/socket failed: $?"
3964         $SOCKETCLIENT $DIR/socket ||
3965                 error "$SOCKETCLIENT $DIR/socket failed: $?"
3966         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
3967 }
3968 run_test 54a "unix domain socket test =========================="
3969
3970 test_54b() {
3971         f="$DIR/f54b"
3972         mknod $f c 1 3
3973         chmod 0666 $f
3974         dd if=/dev/zero of=$f bs=$(page_size) count=1
3975 }
3976 run_test 54b "char device works in lustre ======================"
3977
3978 find_loop_dev() {
3979         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3980         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3981         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3982
3983         for i in $(seq 3 7); do
3984                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3985                 LOOPDEV=$LOOPBASE$i
3986                 LOOPNUM=$i
3987                 break
3988         done
3989 }
3990
3991 cleanup_54c() {
3992         loopdev="$DIR/loop54c"
3993
3994         trap 0
3995         $UMOUNT -d $tdir || rc=$?
3996         losetup -d $loopdev || true
3997         rm $loopdev
3998         return $rc
3999 }
4000
4001 test_54c() {
4002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4003         tfile="$DIR/f54c"
4004         tdir="$DIR/d54c"
4005         loopdev="$DIR/loop54c"
4006
4007         find_loop_dev
4008         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4009         mknod $loopdev b 7 $LOOPNUM
4010         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
4011         dd if=/dev/zero of=$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4012         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
4013         trap cleanup_54c EXIT
4014         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4015         test_mkdir -p $tdir
4016         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
4017         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
4018         df $tdir
4019         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
4020         cleanup_54c
4021 }
4022 run_test 54c "block device works in lustre ====================="
4023
4024 test_54d() {
4025         f="$DIR/f54d"
4026         string="aaaaaa"
4027         mknod $f p
4028         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4029 }
4030 run_test 54d "fifo device works in lustre ======================"
4031
4032 test_54e() {
4033         check_kernel_version 46 || return 0
4034         f="$DIR/f54e"
4035         string="aaaaaa"
4036         cp -aL /dev/console $f
4037         echo $string > $f || error "echo $string to $f failed"
4038 }
4039 run_test 54e "console/tty device works in lustre ======================"
4040
4041 #The test_55 used to be iopen test and it was removed by bz#24037.
4042 #run_test 55 "check iopen_connect_dentry() ======================"
4043
4044 test_56a() {    # was test_56
4045         rm -rf $DIR/$tdir
4046         $SETSTRIPE -d $DIR
4047         test_mkdir -p $DIR/$tdir/dir
4048         NUMFILES=3
4049         NUMFILESx2=$(($NUMFILES * 2))
4050         for i in `seq 1 $NUMFILES` ; do
4051                 touch $DIR/$tdir/file$i
4052                 touch $DIR/$tdir/dir/file$i
4053         done
4054
4055         # test lfs getstripe with --recursive
4056         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
4057         [ $FILENUM -eq $NUMFILESx2 ] ||
4058                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4059         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
4060         [ $FILENUM -eq $NUMFILES ] ||
4061                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4062         echo "$GETSTRIPE --recursive passed."
4063
4064         # test lfs getstripe with file instead of dir
4065         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
4066         [ $FILENUM  -eq 1 ] || error \
4067                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4068         echo "$GETSTRIPE file1 passed."
4069
4070         #test lfs getstripe with --verbose
4071         [ `$GETSTRIPE --verbose $DIR/$tdir |
4072                         grep -c lmm_magic` -eq $NUMFILES ] ||
4073                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4074         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
4075             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4076         echo "$GETSTRIPE --verbose passed."
4077
4078         #test lfs getstripe with --obd
4079         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4080                                         grep -q "unknown obduuid" ||
4081                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4082
4083         [  "$OSTCOUNT" -lt 2 ] &&
4084                 skip_env "skipping other $GETSTRIPE --obd test" && return
4085
4086         OSTIDX=1
4087         OBDUUID=$(ostuuid_from_index $OSTIDX)
4088         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
4089         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
4090         [ $FOUND -eq $FILENUM ] ||
4091                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4092         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4093                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4094                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
4095                 error "$GETSTRIPE --obd: should not show file on other obd"
4096         echo "$GETSTRIPE --obd passed"
4097 }
4098 run_test 56a "check $GETSTRIPE"
4099
4100 NUMFILES=3
4101 NUMDIRS=3
4102 setup_56() {
4103         local LOCAL_NUMFILES="$1"
4104         local LOCAL_NUMDIRS="$2"
4105         local MKDIR_PARAMS="$3"
4106         local DIR_STRIPE_PARAMS="$4"
4107
4108         if [ ! -d "$TDIR" ] ; then
4109                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4110                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4111                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4112                         touch $TDIR/file$i
4113                 done
4114                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4115                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4116                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4117                                 touch $TDIR/dir$i/file$j
4118                         done
4119                 done
4120         fi
4121 }
4122
4123 setup_56_special() {
4124         LOCAL_NUMFILES=$1
4125         LOCAL_NUMDIRS=$2
4126         setup_56 $1 $2
4127         if [ ! -e "$TDIR/loop1b" ] ; then
4128                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4129                         mknod $TDIR/loop${i}b b 7 $i
4130                         mknod $TDIR/null${i}c c 1 3
4131                         ln -s $TDIR/file1 $TDIR/link${i}l
4132                 done
4133                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4134                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4135                         mknod $TDIR/dir$i/null${i}c c 1 3
4136                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4137                 done
4138         fi
4139 }
4140
4141 test_56g() {
4142         $SETSTRIPE -d $DIR
4143
4144         TDIR=$DIR/${tdir}g
4145         setup_56 $NUMFILES $NUMDIRS
4146
4147         EXPECTED=$(($NUMDIRS + 2))
4148         # test lfs find with -name
4149         for i in $(seq 1 $NUMFILES) ; do
4150                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4151                 [ $NUMS -eq $EXPECTED ] ||
4152                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4153                               "found $NUMS, expected $EXPECTED"
4154         done
4155 }
4156 run_test 56g "check lfs find -name ============================="
4157
4158 test_56h() {
4159         $SETSTRIPE -d $DIR
4160
4161         TDIR=$DIR/${tdir}g
4162         setup_56 $NUMFILES $NUMDIRS
4163
4164         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4165         # test lfs find with ! -name
4166         for i in $(seq 1 $NUMFILES) ; do
4167                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4168                 [ $NUMS -eq $EXPECTED ] ||
4169                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4170                               "found $NUMS, expected $EXPECTED"
4171         done
4172 }
4173 run_test 56h "check lfs find ! -name ============================="
4174
4175 test_56i() {
4176        tdir=${tdir}i
4177        test_mkdir -p $DIR/$tdir
4178        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4179        CMD="$LFIND -ost $UUID $DIR/$tdir"
4180        OUT=$($CMD)
4181        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4182 }
4183 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4184
4185 test_56j() {
4186         TDIR=$DIR/${tdir}g
4187         setup_56_special $NUMFILES $NUMDIRS
4188
4189         EXPECTED=$((NUMDIRS + 1))
4190         CMD="$LFIND -type d $TDIR"
4191         NUMS=$($CMD | wc -l)
4192         [ $NUMS -eq $EXPECTED ] ||
4193                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4194 }
4195 run_test 56j "check lfs find -type d ============================="
4196
4197 test_56k() {
4198         TDIR=$DIR/${tdir}g
4199         setup_56_special $NUMFILES $NUMDIRS
4200
4201         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4202         CMD="$LFIND -type f $TDIR"
4203         NUMS=$($CMD | wc -l)
4204         [ $NUMS -eq $EXPECTED ] ||
4205                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4206 }
4207 run_test 56k "check lfs find -type f ============================="
4208
4209 test_56l() {
4210         TDIR=$DIR/${tdir}g
4211         setup_56_special $NUMFILES $NUMDIRS
4212
4213         EXPECTED=$((NUMDIRS + NUMFILES))
4214         CMD="$LFIND -type b $TDIR"
4215         NUMS=$($CMD | wc -l)
4216         [ $NUMS -eq $EXPECTED ] ||
4217                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4218 }
4219 run_test 56l "check lfs find -type b ============================="
4220
4221 test_56m() {
4222         TDIR=$DIR/${tdir}g
4223         setup_56_special $NUMFILES $NUMDIRS
4224
4225         EXPECTED=$((NUMDIRS + NUMFILES))
4226         CMD="$LFIND -type c $TDIR"
4227         NUMS=$($CMD | wc -l)
4228         [ $NUMS -eq $EXPECTED ] ||
4229                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4230 }
4231 run_test 56m "check lfs find -type c ============================="
4232
4233 test_56n() {
4234         TDIR=$DIR/${tdir}g
4235         setup_56_special $NUMFILES $NUMDIRS
4236
4237         EXPECTED=$((NUMDIRS + NUMFILES))
4238         CMD="$LFIND -type l $TDIR"
4239         NUMS=$($CMD | wc -l)
4240         [ $NUMS -eq $EXPECTED ] ||
4241                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4242 }
4243 run_test 56n "check lfs find -type l ============================="
4244
4245 test_56o() {
4246         TDIR=$DIR/${tdir}o
4247         setup_56 $NUMFILES $NUMDIRS
4248         utime $TDIR/file1 > /dev/null || error "utime (1)"
4249         utime $TDIR/file2 > /dev/null || error "utime (2)"
4250         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4251         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4252         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4253         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4254
4255         EXPECTED=4
4256         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4257         [ $NUMS -eq $EXPECTED ] || \
4258                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4259
4260         EXPECTED=12
4261         CMD="$LFIND -mtime 0 $TDIR"
4262         NUMS=$($CMD | wc -l)
4263         [ $NUMS -eq $EXPECTED ] ||
4264                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4265 }
4266 run_test 56o "check lfs find -mtime for old files =========================="
4267
4268 test_56p() {
4269         [ $RUNAS_ID -eq $UID ] &&
4270                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4271
4272         TDIR=$DIR/${tdir}p
4273         setup_56 $NUMFILES $NUMDIRS
4274
4275         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4276         EXPECTED=$NUMFILES
4277         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4278         NUMS=$($CMD | wc -l)
4279         [ $NUMS -eq $EXPECTED ] || \
4280                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4281
4282         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4283         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4284         NUMS=$($CMD | wc -l)
4285         [ $NUMS -eq $EXPECTED ] || \
4286                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4287 }
4288 run_test 56p "check lfs find -uid and ! -uid ==============================="
4289
4290 test_56q() {
4291         [ $RUNAS_ID -eq $UID ] &&
4292                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4293
4294         TDIR=$DIR/${tdir}q
4295         setup_56 $NUMFILES $NUMDIRS
4296
4297         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4298
4299         EXPECTED=$NUMFILES
4300         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4301         NUMS=$($CMD | wc -l)
4302         [ $NUMS -eq $EXPECTED ] ||
4303                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4304
4305         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4306         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4307         NUMS=$($CMD | wc -l)
4308         [ $NUMS -eq $EXPECTED ] ||
4309                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4310 }
4311 run_test 56q "check lfs find -gid and ! -gid ==============================="
4312
4313 test_56r() {
4314         TDIR=$DIR/${tdir}r
4315         setup_56 $NUMFILES $NUMDIRS
4316
4317         EXPECTED=12
4318         CMD="$LFIND -size 0 -type f $TDIR"
4319         NUMS=$($CMD | wc -l)
4320         [ $NUMS -eq $EXPECTED ] ||
4321                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4322         EXPECTED=0
4323         CMD="$LFIND ! -size 0 -type f $TDIR"
4324         NUMS=$($CMD | wc -l)
4325         [ $NUMS -eq $EXPECTED ] ||
4326                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4327         echo "test" > $TDIR/$tfile
4328         echo "test2" > $TDIR/$tfile.2 && sync
4329         EXPECTED=1
4330         CMD="$LFIND -size 5 -type f $TDIR"
4331         NUMS=$($CMD | wc -l)
4332         [ $NUMS -eq $EXPECTED ] ||
4333                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4334         EXPECTED=1
4335         CMD="$LFIND -size +5 -type f $TDIR"
4336         NUMS=$($CMD | wc -l)
4337         [ $NUMS -eq $EXPECTED ] ||
4338                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4339         EXPECTED=2
4340         CMD="$LFIND -size +0 -type f $TDIR"
4341         NUMS=$($CMD | wc -l)
4342         [ $NUMS -eq $EXPECTED ] ||
4343                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4344         EXPECTED=2
4345         CMD="$LFIND ! -size -5 -type f $TDIR"
4346         NUMS=$($CMD | wc -l)
4347         [ $NUMS -eq $EXPECTED ] ||
4348                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4349         EXPECTED=12
4350         CMD="$LFIND -size -5 -type f $TDIR"
4351         NUMS=$($CMD | wc -l)
4352         [ $NUMS -eq $EXPECTED ] ||
4353                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4354 }
4355 run_test 56r "check lfs find -size works =========================="
4356
4357 test_56s() { # LU-611
4358         TDIR=$DIR/${tdir}s
4359         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4360
4361         if [ $OSTCOUNT -gt 1 ]; then
4362                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4363                 ONESTRIPE=4
4364                 EXTRA=4
4365         else
4366                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4367                 EXTRA=0
4368         fi
4369
4370         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4371         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4372         NUMS=$($CMD | wc -l)
4373         [ $NUMS -eq $EXPECTED ] ||
4374                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4375
4376         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4377         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4378         NUMS=$($CMD | wc -l)
4379         [ $NUMS -eq $EXPECTED ] ||
4380                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4381
4382         EXPECTED=$ONESTRIPE
4383         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4384         NUMS=$($CMD | wc -l)
4385         [ $NUMS -eq $EXPECTED ] ||
4386                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4387
4388         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4389         NUMS=$($CMD | wc -l)
4390         [ $NUMS -eq $EXPECTED ] ||
4391                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4392
4393         EXPECTED=0
4394         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4395         NUMS=$($CMD | wc -l)
4396         [ $NUMS -eq $EXPECTED ] ||
4397                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4398 }
4399 run_test 56s "check lfs find -stripe-count works"
4400
4401 test_56t() { # LU-611
4402         TDIR=$DIR/${tdir}t
4403         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4404
4405         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4406
4407         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4408         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4409         NUMS=$($CMD | wc -l)
4410         [ $NUMS -eq $EXPECTED ] ||
4411                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4412
4413         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4414         NUMS=$($CMD | wc -l)
4415         [ $NUMS -eq $EXPECTED ] ||
4416                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4417
4418         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4419         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4420         NUMS=$($CMD | wc -l)
4421         [ $NUMS -eq $EXPECTED ] ||
4422                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4423
4424         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4425         NUMS=$($CMD | wc -l)
4426         [ $NUMS -eq $EXPECTED ] ||
4427                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4428
4429         EXPECTED=4
4430         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4431         NUMS=$($CMD | wc -l)
4432         [ $NUMS -eq $EXPECTED ] ||
4433                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4434
4435         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4436         NUMS=$($CMD | wc -l)
4437         [ $NUMS -eq $EXPECTED ] ||
4438                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4439
4440         EXPECTED=0
4441         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4442         NUMS=$($CMD | wc -l)
4443         [ $NUMS -eq $EXPECTED ] ||
4444                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4445 }
4446 run_test 56t "check lfs find -stripe-size works"
4447
4448 test_56u() { # LU-611
4449         TDIR=$DIR/${tdir}u
4450         setup_56 $NUMFILES $NUMDIRS "-i 0"
4451
4452         if [ $OSTCOUNT -gt 1 ]; then
4453                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4454                 ONESTRIPE=4
4455         else
4456                 ONESTRIPE=0
4457         fi
4458
4459         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4460         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4461         NUMS=$($CMD | wc -l)
4462         [ $NUMS -eq $EXPECTED ] ||
4463                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4464
4465         EXPECTED=$ONESTRIPE
4466         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4467         NUMS=$($CMD | wc -l)
4468         [ $NUMS -eq $EXPECTED ] ||
4469                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4470
4471         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4472         NUMS=$($CMD | wc -l)
4473         [ $NUMS -eq $EXPECTED ] ||
4474                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4475
4476         EXPECTED=0
4477         # This should produce an error and not return any files
4478         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4479         NUMS=$($CMD 2>/dev/null | wc -l)
4480         [ $NUMS -eq $EXPECTED ] ||
4481                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4482
4483         if [ $OSTCOUNT -gt 1 ]; then
4484                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4485                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4486                 NUMS=$($CMD | wc -l)
4487                 [ $NUMS -eq $EXPECTED ] ||
4488                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4489         fi
4490 }
4491 run_test 56u "check lfs find -stripe-index works"
4492
4493 test_56v() {
4494     local MDT_IDX=0
4495
4496     TDIR=$DIR/${tdir}v
4497     rm -rf $TDIR
4498     setup_56 $NUMFILES $NUMDIRS
4499
4500     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4501     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4502
4503     for file in $($LFIND -mdt $UUID $TDIR); do
4504         file_mdt_idx=$($GETSTRIPE -M $file)
4505         [ $file_mdt_idx -eq $MDT_IDX ] ||
4506             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4507     done
4508 }
4509 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4510
4511 # Get and check the actual stripe count of one file.
4512 # Usage: check_stripe_count <file> <expected_stripe_count>
4513 check_stripe_count() {
4514     local file=$1
4515     local expected=$2
4516     local actual
4517
4518     [[ -z "$file" || -z "$expected" ]] &&
4519         error "check_stripe_count: invalid argument!"
4520
4521     local cmd="$GETSTRIPE -c $file"
4522     actual=$($cmd) || error "$cmd failed"
4523     actual=${actual%% *}
4524
4525     if [[ $actual -ne $expected ]]; then
4526         [[ $expected -eq -1 ]] ||
4527             error "$cmd wrong: found $actual, expected $expected"
4528         [[ $actual -eq $OSTCOUNT ]] ||
4529             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4530     fi
4531 }
4532
4533 test_56w() {
4534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4535         TDIR=$DIR/${tdir}w
4536
4537     rm -rf $TDIR || error "remove $TDIR failed"
4538     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4539
4540     local stripe_size
4541     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4542         error "$GETSTRIPE -S -d $TDIR failed"
4543     stripe_size=${stripe_size%% *}
4544
4545     local file_size=$((stripe_size * OSTCOUNT))
4546     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4547     local required_space=$((file_num * file_size))
4548     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4549     [[ $free_space -le $((required_space / 1024)) ]] &&
4550         skip_env "need at least $required_space bytes free space," \
4551                  "have $free_space kbytes" && return
4552
4553     local dd_bs=65536
4554     local dd_count=$((file_size / dd_bs))
4555
4556     # write data into the files
4557     local i
4558     local j
4559     local file
4560     for i in $(seq 1 $NUMFILES); do
4561         file=$TDIR/file$i
4562         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4563             error "write data into $file failed"
4564     done
4565     for i in $(seq 1 $NUMDIRS); do
4566         for j in $(seq 1 $NUMFILES); do
4567             file=$TDIR/dir$i/file$j
4568             yes | dd bs=$dd_bs count=$dd_count of=$file \
4569                 >/dev/null 2>&1 ||
4570                 error "write data into $file failed"
4571         done
4572     done
4573
4574     local expected=-1
4575     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4576
4577     # lfs_migrate file
4578     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4579     echo "$cmd"
4580     eval $cmd || error "$cmd failed"
4581
4582     check_stripe_count $TDIR/file1 $expected
4583
4584     # lfs_migrate dir
4585     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4586     echo "$cmd"
4587     eval $cmd || error "$cmd failed"
4588
4589     for j in $(seq 1 $NUMFILES); do
4590         check_stripe_count $TDIR/dir1/file$j $expected
4591     done
4592
4593     # lfs_migrate works with lfs find
4594     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4595          $LFS_MIGRATE -y -c $expected"
4596     echo "$cmd"
4597     eval $cmd || error "$cmd failed"
4598
4599     for i in $(seq 2 $NUMFILES); do
4600         check_stripe_count $TDIR/file$i $expected
4601     done
4602     for i in $(seq 2 $NUMDIRS); do
4603         for j in $(seq 1 $NUMFILES); do
4604             check_stripe_count $TDIR/dir$i/file$j $expected
4605         done
4606     done
4607 }
4608 run_test 56w "check lfs_migrate -c stripe_count works"
4609
4610 test_56x() {
4611         check_swap_layouts_support && return 0
4612         [ "$OSTCOUNT" -lt "2" ] &&
4613                 skip_env "need 2 OST, skipping test" && return
4614
4615         local dir0=$DIR/$tdir/$testnum
4616         mkdir -p $dir0 || error "creating dir $dir0"
4617
4618         local ref1=/etc/passwd
4619         local file1=$dir0/file1
4620
4621         $SETSTRIPE -c 2 $file1
4622         cp $ref1 $file1
4623         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4624         stripe=$($GETSTRIPE -c $file1)
4625         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4626         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4627
4628         # clean up
4629         rm -f $file1
4630 }
4631 run_test 56x "lfs migration support"
4632
4633 test_56y() {
4634         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4635                 skip "No HSM support on MDS of $(get_lustre_version)," \
4636                          "need 2.4.53 at least" && return
4637         local res=""
4638
4639         local dir0=$DIR/$tdir/$testnum
4640         mkdir -p $dir0 || error "creating dir $dir0"
4641         local f1=$dir0/file1
4642         local f2=$dir0/file2
4643
4644         touch $f1 || error "creating std file $f1"
4645         $MULTIOP $f2 H2c || error "creating released file $f2"
4646
4647         # a directory can be raid0, so ask only for files
4648         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4649         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4650
4651         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4652         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4653
4654         # only files can be released, so no need to force file search
4655         res=$($LFIND $dir0 -L released)
4656         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4657
4658         res=$($LFIND $dir0 \! -L released)
4659         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4660
4661 }
4662 run_test 56y "lfs find -L raid0|released"
4663
4664 test_57a() {
4665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4666         # note test will not do anything if MDS is not local
4667         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4668                 skip "Only applicable to ldiskfs-based MDTs"
4669                 return
4670         fi
4671
4672         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4673         local MNTDEV="osd*.*MDT*.mntdev"
4674         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4675         [ -z "$DEV" ] && error "can't access $MNTDEV"
4676         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4677                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4678                         error "can't access $DEV"
4679                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4680                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4681                 rm $TMP/t57a.dump
4682         done
4683 }
4684 run_test 57a "verify MDS filesystem created with large inodes =="
4685
4686 test_57b() {
4687         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4688         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4689                 skip "Only applicable to ldiskfs-based MDTs"
4690                 return
4691         fi
4692
4693         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4694         local dir=$DIR/d57b
4695
4696         local FILECOUNT=100
4697         local FILE1=$dir/f1
4698         local FILEN=$dir/f$FILECOUNT
4699
4700         rm -rf $dir || error "removing $dir"
4701         test_mkdir -p $dir || error "creating $dir"
4702         local num=$(get_mds_dir $dir)
4703         local mymds=mds$num
4704
4705         echo "mcreating $FILECOUNT files"
4706         createmany -m $dir/f 1 $FILECOUNT || \
4707                 error "creating files in $dir"
4708
4709         # verify that files do not have EAs yet
4710         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4711         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4712
4713         sync
4714         sleep 1
4715         df $dir  #make sure we get new statfs data
4716         local MDSFREE=$(do_facet $mymds \
4717                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4718         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4719         echo "opening files to create objects/EAs"
4720         local FILE
4721         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4722                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4723         done
4724
4725         # verify that files have EAs now
4726         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4727         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4728
4729         sleep 1  #make sure we get new statfs data
4730         df $dir
4731         local MDSFREE2=$(do_facet $mymds \
4732                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4733         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4734         if [ "$MDCFREE2" -lt "$((MDCFREE - 16))" ]; then
4735                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4736                         error "MDC before $MDCFREE != after $MDCFREE2"
4737                 else
4738                         echo "MDC before $MDCFREE != after $MDCFREE2"
4739                         echo "unable to confirm if MDS has large inodes"
4740                 fi
4741         fi
4742         rm -rf $dir
4743 }
4744 run_test 57b "default LOV EAs are stored inside large inodes ==="
4745
4746 test_58() {
4747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4748         [ -z "$(which wiretest 2>/dev/null)" ] &&
4749                         skip_env "could not find wiretest" && return
4750         wiretest
4751 }
4752 run_test 58 "verify cross-platform wire constants =============="
4753
4754 test_59() {
4755         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4756         echo "touch 130 files"
4757         createmany -o $DIR/f59- 130
4758         echo "rm 130 files"
4759         unlinkmany $DIR/f59- 130
4760         sync
4761         # wait for commitment of removal
4762         wait_delete_completed
4763 }
4764 run_test 59 "verify cancellation of llog records async ========="
4765
4766 TEST60_HEAD="test_60 run $RANDOM"
4767 test_60a() {
4768         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4769         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4770         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4771                 skip_env "missing subtest run-llog.sh" && return
4772         log "$TEST60_HEAD - from kernel mode"
4773         do_facet mgs sh run-llog.sh
4774 }
4775 run_test 60a "llog sanity tests run from kernel module =========="
4776
4777 test_60b() { # bug 6411
4778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4779         dmesg > $DIR/$tfile
4780         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4781                                  /llog.test/ {
4782                                          if (marker)
4783                                                  from_marker++
4784                                          from_begin++
4785                                  }
4786                                  END {
4787                                          if (marker)
4788                                                  print from_marker
4789                                          else
4790                                                  print from_begin
4791                                  }"`
4792         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4793 }
4794 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4795
4796 test_60c() {
4797         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4798         echo "create 5000 files"
4799         createmany -o $DIR/f60c- 5000
4800 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4801         lctl set_param fail_loc=0x80000137
4802         unlinkmany $DIR/f60c- 5000
4803         lctl set_param fail_loc=0
4804 }
4805 run_test 60c "unlink file when mds full"
4806
4807 test_60d() {
4808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4809         SAVEPRINTK=$(lctl get_param -n printk)
4810
4811         # verify "lctl mark" is even working"
4812         MESSAGE="test message ID $RANDOM $$"
4813         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4814         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4815
4816         lctl set_param printk=0 || error "set lnet.printk failed"
4817         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4818         MESSAGE="new test message ID $RANDOM $$"
4819         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4820         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4821         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4822
4823         lctl set_param -n printk="$SAVEPRINTK"
4824 }
4825 run_test 60d "test printk console message masking"
4826
4827 test_61() {
4828         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4829         f="$DIR/f61"
4830         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
4831         cancel_lru_locks osc
4832         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
4833         sync
4834 }
4835 run_test 61 "mmap() writes don't make sync hang ================"
4836
4837 # bug 2330 - insufficient obd_match error checking causes LBUG
4838 test_62() {
4839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4840         f="$DIR/f62"
4841         echo foo > $f
4842         cancel_lru_locks osc
4843         lctl set_param fail_loc=0x405
4844         cat $f && error "cat succeeded, expect -EIO"
4845         lctl set_param fail_loc=0
4846 }
4847 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4848 # match every page all of the time.
4849 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4850
4851 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4852 # Though this test is irrelevant anymore, it helped to reveal some
4853 # other grant bugs (LU-4482), let's keep it.
4854 test_63a() {   # was test_63
4855         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4856         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4857         for i in `seq 10` ; do
4858                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4859                 sleep 5
4860                 kill $!
4861                 sleep 1
4862         done
4863
4864         rm -f $DIR/f63 || true
4865 }
4866 run_test 63a "Verify oig_wait interruption does not crash ======="
4867
4868 # bug 2248 - async write errors didn't return to application on sync
4869 # bug 3677 - async write errors left page locked
4870 test_63b() {
4871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4872         debugsave
4873         lctl set_param debug=-1
4874
4875         # ensure we have a grant to do async writes
4876         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4877         rm $DIR/$tfile
4878
4879         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4880         lctl set_param fail_loc=0x80000406
4881         $MULTIOP $DIR/$tfile Owy && \
4882                 error "sync didn't return ENOMEM"
4883         sync; sleep 2; sync     # do a real sync this time to flush page
4884         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4885                 error "locked page left in cache after async error" || true
4886         debugrestore
4887 }
4888 run_test 63b "async write errors should be returned to fsync ==="
4889
4890 test_64a () {
4891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4892         df $DIR
4893         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4894 }
4895 run_test 64a "verify filter grant calculations (in kernel) ====="
4896
4897 test_64b () {
4898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4899         sh oos.sh $MOUNT || error "oos.sh failed: $?"
4900 }
4901 run_test 64b "check out-of-space detection on client ==========="
4902
4903 test_64c() {
4904         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4905 }
4906 run_test 64c "verify grant shrink ========================------"
4907
4908 # bug 1414 - set/get directories' stripe info
4909 test_65a() {
4910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4911         test_mkdir -p $DIR/$tdir
4912         touch $DIR/$tdir/f1
4913         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4914 }
4915 run_test 65a "directory with no stripe info ===================="
4916
4917 test_65b() {
4918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4919         test_mkdir -p $DIR/$tdir
4920         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4921                                                 error "setstripe"
4922         touch $DIR/$tdir/f2
4923         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4924 }
4925 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4926
4927 test_65c() {
4928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4929         if [ $OSTCOUNT -gt 1 ]; then
4930                 test_mkdir -p $DIR/$tdir
4931                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4932                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4933                 touch $DIR/$tdir/f3
4934                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4935         fi
4936 }
4937 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4938
4939 test_65d() {
4940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4941         test_mkdir -p $DIR/$tdir
4942         if [ $STRIPECOUNT -le 0 ]; then
4943                 sc=1
4944         elif [ $STRIPECOUNT -gt 2000 ]; then
4945 #LOV_MAX_STRIPE_COUNT is 2000
4946                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4947         else
4948                 sc=$(($STRIPECOUNT - 1))
4949         fi
4950         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4951         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4952         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4953                                                 error "lverify failed"
4954 }
4955 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4956
4957 test_65e() {
4958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4959         test_mkdir -p $DIR/$tdir
4960
4961         $SETSTRIPE $DIR/$tdir || error "setstripe"
4962         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4963                                         error "no stripe info failed"
4964         touch $DIR/$tdir/f6
4965         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4966 }
4967 run_test 65e "directory setstripe defaults ======================="
4968
4969 test_65f() {
4970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4971         test_mkdir -p $DIR/${tdir}f
4972         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4973 }
4974 run_test 65f "dir setstripe permission (should return error) ==="
4975
4976 test_65g() {
4977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4978         test_mkdir -p $DIR/$tdir
4979         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4980                                                         error "setstripe"
4981         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4982         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4983                 error "delete default stripe failed"
4984 }
4985 run_test 65g "directory setstripe -d ==========================="
4986
4987 test_65h() {
4988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4989         test_mkdir -p $DIR/$tdir
4990         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4991                                                         error "setstripe"
4992         test_mkdir -p $DIR/$tdir/dd1
4993         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4994                 error "stripe info inherit failed"
4995 }
4996 run_test 65h "directory stripe info inherit ===================="
4997
4998 test_65i() { # bug6367
4999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5000         $SETSTRIPE -S 65536 -c -1 $MOUNT
5001 }
5002 run_test 65i "set non-default striping on root directory (bug 6367)="
5003
5004 test_65ia() { # bug12836
5005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5006         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5007 }
5008 run_test 65ia "getstripe on -1 default directory striping"
5009
5010 test_65ib() { # bug12836
5011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5012         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5013 }
5014 run_test 65ib "getstripe -v on -1 default directory striping"
5015
5016 test_65ic() { # bug12836
5017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5018         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5019 }
5020 run_test 65ic "new find on -1 default directory striping"
5021
5022 test_65j() { # bug6367
5023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5024         sync; sleep 1
5025         # if we aren't already remounting for each test, do so for this test
5026         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5027                 cleanup || error "failed to unmount"
5028                 setup
5029         fi
5030         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5031 }
5032 run_test 65j "set default striping on root directory (bug 6367)="
5033
5034 test_65k() { # bug11679
5035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5036         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
5037         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5038
5039     echo "Check OST status: "
5040     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5041               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5042
5043     for OSC in $MDS_OSCS; do
5044         echo $OSC "is activate"
5045         do_facet $SINGLEMDS lctl --device %$OSC activate
5046     done
5047
5048     mkdir -p $DIR/$tdir
5049     for INACTIVE_OSC in $MDS_OSCS; do
5050         echo "Deactivate: " $INACTIVE_OSC
5051         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5052         for STRIPE_OSC in $MDS_OSCS; do
5053             OST=`osc_to_ost $STRIPE_OSC`
5054             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5055                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5056
5057             [ -f $DIR/$tdir/$IDX ] && continue
5058             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5059             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5060             RC=$?
5061             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5062         done
5063         rm -f $DIR/$tdir/*
5064         echo $INACTIVE_OSC "is Activate."
5065         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5066     done
5067 }
5068 run_test 65k "validate manual striping works properly with deactivated OSCs"
5069
5070 test_65l() { # bug 12836
5071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5072         test_mkdir -p $DIR/$tdir/test_dir
5073         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5074         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5075 }
5076 run_test 65l "lfs find on -1 stripe dir ========================"
5077
5078 # bug 2543 - update blocks count on client
5079 test_66() {
5080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5081         COUNT=${COUNT:-8}
5082         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5083         sync; sync_all_data; sync; sync_all_data
5084         cancel_lru_locks osc
5085         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5086         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5087 }
5088 run_test 66 "update inode blocks count on client ==============="
5089
5090 LLOOP=
5091 LLITELOOPLOAD=
5092 cleanup_68() {
5093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5094         trap 0
5095         if [ ! -z "$LLOOP" ]; then
5096                 if swapon -s | grep -q $LLOOP; then
5097                         swapoff $LLOOP || error "swapoff failed"
5098                 fi
5099
5100                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5101                 rm -f $LLOOP
5102                 unset LLOOP
5103         fi
5104         if [ ! -z "$LLITELOOPLOAD" ]; then
5105                 rmmod llite_lloop
5106                 unset LLITELOOPLOAD
5107         fi
5108         rm -f $DIR/f68*
5109 }
5110
5111 meminfo() {
5112         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5113 }
5114
5115 swap_used() {
5116         swapon -s | awk '($1 == "'$1'") { print $4 }'
5117 }
5118
5119 # test case for lloop driver, basic function
5120 test_68a() {
5121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5122         [ "$UID" != 0 ] && skip_env "must run as root" && return
5123         llite_lloop_enabled || \
5124                 { skip_env "llite_lloop module disabled" && return; }
5125
5126         trap cleanup_68 EXIT
5127
5128         if ! module_loaded llite_lloop; then
5129                 if load_module llite/llite_lloop; then
5130                         LLITELOOPLOAD=yes
5131                 else
5132                         skip_env "can't find module llite_lloop"
5133                         return
5134                 fi
5135         fi
5136
5137         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5138         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5139         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5140
5141         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5142         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5143
5144         cleanup_68
5145 }
5146 run_test 68a "lloop driver - basic test ========================"
5147
5148 # excercise swapping to lustre by adding a high priority swapfile entry
5149 # and then consuming memory until it is used.
5150 test_68b() {  # was test_68
5151         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5152         [ "$UID" != 0 ] && skip_env "must run as root" && return
5153         lctl get_param -n devices | grep -q obdfilter && \
5154                 skip "local OST" && return
5155
5156         grep -q llite_lloop /proc/modules
5157         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5158
5159         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5160                 skip "can't reliably test swap with TCP" && return
5161
5162         MEMTOTAL=`meminfo MemTotal`
5163         NR_BLOCKS=$((MEMTOTAL>>8))
5164         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5165
5166         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5167         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5168         mkswap $DIR/f68b
5169
5170         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5171
5172         trap cleanup_68 EXIT
5173
5174         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5175
5176         echo "before: `swapon -s | grep $LLOOP`"
5177         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5178         echo "after: `swapon -s | grep $LLOOP`"
5179         SWAPUSED=`swap_used $LLOOP`
5180
5181         cleanup_68
5182
5183         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5184 }
5185 run_test 68b "support swapping to Lustre ========================"
5186
5187 # bug5265, obdfilter oa2dentry return -ENOENT
5188 # #define OBD_FAIL_OST_ENOENT 0x217
5189 test_69() {
5190         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5191         remote_ost_nodsh && skip "remote OST with nodsh" && return
5192
5193         f="$DIR/$tfile"
5194         $SETSTRIPE -c 1 -i 0 $f
5195
5196         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5197
5198         do_facet ost1 lctl set_param fail_loc=0x217
5199         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5200         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5201
5202         do_facet ost1 lctl set_param fail_loc=0
5203         $DIRECTIO write $f 0 2 || error "write error"
5204
5205         cancel_lru_locks osc
5206         $DIRECTIO read $f 0 1 || error "read error"
5207
5208         do_facet ost1 lctl set_param fail_loc=0x217
5209         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5210
5211         do_facet ost1 lctl set_param fail_loc=0
5212         rm -f $f
5213 }
5214 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5215
5216 test_71() {
5217     test_mkdir -p $DIR/$tdir
5218     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5219 }
5220 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5221
5222 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5224         [ "$RUNAS_ID" = "$UID" ] &&
5225                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5226
5227         # Check that testing environment is properly set up. Skip if not
5228         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5229                 skip_env "User $RUNAS_ID does not exist - skipping"
5230                 return 0
5231         }
5232         # We had better clear the $DIR to get enough space for dd
5233         rm -rf $DIR/*
5234         touch $DIR/$tfile
5235         chmod 777 $DIR/$tfile
5236         chmod ug+s $DIR/$tfile
5237         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5238                 error "$RUNAS dd $DIR/$tfile failed"
5239         # See if we are still setuid/sgid
5240         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5241                 error "S/gid is not dropped on write"
5242         # Now test that MDS is updated too
5243         cancel_lru_locks mdc
5244         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5245                 error "S/gid is not dropped on MDS"
5246         rm -f $DIR/$tfile
5247 }
5248 run_test 72a "Test that remove suid works properly (bug5695) ===="
5249
5250 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5251         local perm
5252
5253         [ "$RUNAS_ID" = "$UID" ] && \
5254                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5255         [ "$RUNAS_ID" -eq 0 ] && \
5256                 skip_env "RUNAS_ID = 0 -- skipping" && return
5257
5258         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5259         # Check that testing environment is properly set up. Skip if not
5260         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5261                 skip_env "User $RUNAS_ID does not exist - skipping"
5262                 return 0
5263         }
5264         touch $DIR/${tfile}-f{g,u}
5265         test_mkdir $DIR/${tfile}-dg
5266         test_mkdir $DIR/${tfile}-du
5267         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5268         chmod g+s $DIR/${tfile}-{f,d}g
5269         chmod u+s $DIR/${tfile}-{f,d}u
5270         for perm in 777 2777 4777; do
5271                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5272                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5273                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5274                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5275         done
5276         true
5277 }
5278 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5279
5280 # bug 3462 - multiple simultaneous MDC requests
5281 test_73() {
5282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5283         test_mkdir $DIR/d73-1
5284         test_mkdir $DIR/d73-2
5285         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5286         pid1=$!
5287
5288         lctl set_param fail_loc=0x80000129
5289         $MULTIOP $DIR/d73-1/f73-2 Oc &
5290         sleep 1
5291         lctl set_param fail_loc=0
5292
5293         $MULTIOP $DIR/d73-2/f73-3 Oc &
5294         pid3=$!
5295
5296         kill -USR1 $pid1
5297         wait $pid1 || return 1
5298
5299         sleep 25
5300
5301         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5302         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5303         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5304
5305         rm -rf $DIR/d73-*
5306 }
5307 run_test 73 "multiple MDC requests (should not deadlock)"
5308
5309 test_74a() { # bug 6149, 6184
5310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5311         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5312         #
5313         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5314         # will spin in a tight reconnection loop
5315         touch $DIR/f74a
5316         lctl set_param fail_loc=0x8000030e
5317         # get any lock that won't be difficult - lookup works.
5318         ls $DIR/f74a
5319         lctl set_param fail_loc=0
5320         true
5321         rm -f $DIR/f74a
5322 }
5323 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5324
5325 test_74b() { # bug 13310
5326         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5327         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5328         #
5329         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5330         # will spin in a tight reconnection loop
5331         lctl set_param fail_loc=0x8000030e
5332         # get a "difficult" lock
5333         touch $DIR/f74b
5334         lctl set_param fail_loc=0
5335         true
5336         rm -f $DIR/f74b
5337 }
5338 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5339
5340 test_74c() {
5341         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5342 #define OBD_FAIL_LDLM_NEW_LOCK
5343         lctl set_param fail_loc=0x80000319
5344         touch $DIR/$tfile && error "Touch successful"
5345         true
5346 }
5347 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5348
5349 num_inodes() {
5350         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5351 }
5352
5353 get_inode_slab_tunables() {
5354         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5355 }
5356
5357 set_inode_slab_tunables() {
5358         echo "lustre_inode_cache $1" > /proc/slabinfo
5359 }
5360
5361 test_76() { # Now for bug 20433, added originally in bug 1443
5362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5363         local SLAB_SETTINGS=`get_inode_slab_tunables`
5364         local CPUS=`getconf _NPROCESSORS_ONLN`
5365         # we cannot set limit below 1 which means 1 inode in each
5366         # per-cpu cache is still allowed
5367         set_inode_slab_tunables "1 1 0"
5368         cancel_lru_locks osc
5369         BEFORE_INODES=`num_inodes`
5370         echo "before inodes: $BEFORE_INODES"
5371         local COUNT=1000
5372         [ "$SLOW" = "no" ] && COUNT=100
5373         for i in `seq $COUNT`; do
5374                 touch $DIR/$tfile
5375                 rm -f $DIR/$tfile
5376         done
5377         cancel_lru_locks osc
5378         AFTER_INODES=`num_inodes`
5379         echo "after inodes: $AFTER_INODES"
5380         local wait=0
5381         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5382                 sleep 2
5383                 AFTER_INODES=`num_inodes`
5384                 wait=$((wait+2))
5385                 echo "wait $wait seconds inodes: $AFTER_INODES"
5386                 if [ $wait -gt 30 ]; then
5387                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5388                 fi
5389         done
5390         set_inode_slab_tunables "$SLAB_SETTINGS"
5391 }
5392 run_test 76 "confirm clients recycle inodes properly ===="
5393
5394
5395 export ORIG_CSUM=""
5396 set_checksums()
5397 {
5398         # Note: in sptlrpc modes which enable its own bulk checksum, the
5399         # original crc32_le bulk checksum will be automatically disabled,
5400         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5401         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5402         # In this case set_checksums() will not be no-op, because sptlrpc
5403         # bulk checksum will be enabled all through the test.
5404
5405         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5406         lctl set_param -n osc.*.checksums $1
5407         return 0
5408 }
5409
5410 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5411                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5412 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5413 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5414 set_checksum_type()
5415 {
5416         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5417         log "set checksum type to $1"
5418         return 0
5419 }
5420 F77_TMP=$TMP/f77-temp
5421 F77SZ=8
5422 setup_f77() {
5423         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5424                 error "error writing to $F77_TMP"
5425 }
5426
5427 test_77a() { # bug 10889
5428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5429         $GSS && skip "could not run with gss" && return
5430         [ ! -f $F77_TMP ] && setup_f77
5431         set_checksums 1
5432         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5433         set_checksums 0
5434         rm -f $DIR/$tfile
5435 }
5436 run_test 77a "normal checksum read/write operation"
5437
5438 test_77b() { # bug 10889
5439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5440         $GSS && skip "could not run with gss" && return
5441         [ ! -f $F77_TMP ] && setup_f77
5442         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5443         $LCTL set_param fail_loc=0x80000409
5444         set_checksums 1
5445
5446         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5447                 error "dd error: $?"
5448         $LCTL set_param fail_loc=0
5449
5450         for algo in $CKSUM_TYPES; do
5451                 cancel_lru_locks osc
5452                 set_checksum_type $algo
5453                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5454                 $LCTL set_param fail_loc=0x80000408
5455                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5456                 $LCTL set_param fail_loc=0
5457         done
5458         set_checksums 0
5459         set_checksum_type $ORIG_CSUM_TYPE
5460         rm -f $DIR/$tfile
5461 }
5462 run_test 77b "checksum error on client write, read"
5463
5464 test_77d() { # bug 10889
5465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5466         $GSS && skip "could not run with gss" && return
5467         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5468         $LCTL set_param fail_loc=0x80000409
5469         set_checksums 1
5470         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5471                 error "direct write: rc=$?"
5472         $LCTL set_param fail_loc=0
5473         set_checksums 0
5474
5475         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5476         $LCTL set_param fail_loc=0x80000408
5477         set_checksums 1
5478         cancel_lru_locks osc
5479         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5480                 error "direct read: rc=$?"
5481         $LCTL set_param fail_loc=0
5482         set_checksums 0
5483 }
5484 run_test 77d "checksum error on OST direct write, read"
5485
5486 test_77f() { # bug 10889
5487         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5488         $GSS && skip "could not run with gss" && return
5489         set_checksums 1
5490         for algo in $CKSUM_TYPES; do
5491                 cancel_lru_locks osc
5492                 set_checksum_type $algo
5493                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5494                 $LCTL set_param fail_loc=0x409
5495                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5496                         error "direct write succeeded"
5497                 $LCTL set_param fail_loc=0
5498         done
5499         set_checksum_type $ORIG_CSUM_TYPE
5500         set_checksums 0
5501 }
5502 run_test 77f "repeat checksum error on write (expect error)"
5503
5504 test_77g() { # bug 10889
5505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5506         $GSS && skip "could not run with gss" && return
5507         remote_ost_nodsh && skip "remote OST with nodsh" && return
5508
5509         [ ! -f $F77_TMP ] && setup_f77
5510
5511         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5512         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5513         do_facet ost1 lctl set_param fail_loc=0x8000021a
5514         set_checksums 1
5515         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5516                 error "write error: rc=$?"
5517         do_facet ost1 lctl set_param fail_loc=0
5518         set_checksums 0
5519
5520         cancel_lru_locks osc
5521         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5522         do_facet ost1 lctl set_param fail_loc=0x8000021b
5523         set_checksums 1
5524         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5525         do_facet ost1 lctl set_param fail_loc=0
5526         set_checksums 0
5527 }
5528 run_test 77g "checksum error on OST write, read"
5529
5530 test_77i() { # bug 13805
5531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5532         $GSS && skip "could not run with gss" && return
5533         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5534         lctl set_param fail_loc=0x40b
5535         remount_client $MOUNT
5536         lctl set_param fail_loc=0
5537         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5538                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5539                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5540                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5541         done
5542         remount_client $MOUNT
5543 }
5544 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5545
5546 test_77j() { # bug 13805
5547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5548         $GSS && skip "could not run with gss" && return
5549         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5550         lctl set_param fail_loc=0x40c
5551         remount_client $MOUNT
5552         lctl set_param fail_loc=0
5553         sleep 2 # wait async osc connect to finish
5554         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5555                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5556                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5557                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5558         done
5559         remount_client $MOUNT
5560 }
5561 run_test 77j "client only supporting ADLER32"
5562
5563 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5564 rm -f $F77_TMP
5565 unset F77_TMP
5566
5567 test_78() { # bug 10901
5568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5569         remote_ost || { skip_env "local OST" && return; }
5570
5571         NSEQ=5
5572         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5573         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5574         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5575         echo "MemTotal: $MEMTOTAL"
5576 # reserve 256MB of memory for the kernel and other running processes,
5577 # and then take 1/2 of the remaining memory for the read/write buffers.
5578     if [ $MEMTOTAL -gt 512 ] ;then
5579         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5580     else
5581         # for those poor memory-starved high-end clusters...
5582         MEMTOTAL=$((MEMTOTAL / 2))
5583     fi
5584         echo "Mem to use for directio: $MEMTOTAL"
5585         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5586         [ $F78SIZE -gt 512 ] && F78SIZE=512
5587         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5588         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5589         echo "Smallest OST: $SMALLESTOST"
5590         [ $SMALLESTOST -lt 10240 ] && \
5591                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5592
5593         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5594                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5595
5596         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5597         echo "File size: $F78SIZE"
5598         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5599         for i in `seq 1 $NSEQ`
5600         do
5601                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5602                 echo directIO rdwr round $i of $NSEQ
5603                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5604         done
5605
5606         rm -f $DIR/$tfile
5607 }
5608 run_test 78 "handle large O_DIRECT writes correctly ============"
5609
5610 test_79() { # bug 12743
5611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5612         wait_delete_completed
5613
5614         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5615         BKFREE=$(calc_osc_kbytes kbytesfree)
5616         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5617
5618         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5619         DFTOTAL=`echo $STRING | cut -d, -f1`
5620         DFUSED=`echo $STRING  | cut -d, -f2`
5621         DFAVAIL=`echo $STRING | cut -d, -f3`
5622         DFFREE=$(($DFTOTAL - $DFUSED))
5623
5624         ALLOWANCE=$((64 * $OSTCOUNT))
5625
5626         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5627            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5628                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5629         fi
5630         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5631            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5632                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5633         fi
5634         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5635            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5636                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5637         fi
5638 }
5639 run_test 79 "df report consistency check ======================="
5640
5641 test_80() { # bug 10718
5642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5643         # relax strong synchronous semantics for slow backends like ZFS
5644         local soc="obdfilter.*.sync_on_lock_cancel"
5645         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5646         local hosts=
5647         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5648                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5649                           facet_active_host $host; done | sort -u)
5650                 do_nodes $hosts lctl set_param $soc=never
5651         fi
5652
5653         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5654         sync; sleep 1; sync
5655         local BEFORE=`date +%s`
5656         cancel_lru_locks osc
5657         local AFTER=`date +%s`
5658         local DIFF=$((AFTER-BEFORE))
5659         if [ $DIFF -gt 1 ] ; then
5660                 error "elapsed for 1M@1T = $DIFF"
5661         fi
5662
5663         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5664
5665         rm -f $DIR/$tfile
5666 }
5667 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5668
5669 test_81a() { # LU-456
5670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5671         remote_ost_nodsh && skip "remote OST with nodsh" && return
5672         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5673         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5674         do_facet ost1 lctl set_param fail_loc=0x80000228
5675
5676         # write should trigger a retry and success
5677         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5678         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5679         RC=$?
5680         if [ $RC -ne 0 ] ; then
5681                 error "write should success, but failed for $RC"
5682         fi
5683 }
5684 run_test 81a "OST should retry write when get -ENOSPC ==============="
5685
5686 test_81b() { # LU-456
5687         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5688         remote_ost_nodsh && skip "remote OST with nodsh" && return
5689         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5690         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5691         do_facet ost1 lctl set_param fail_loc=0x228
5692
5693         # write should retry several times and return -ENOSPC finally
5694         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5695         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5696         RC=$?
5697         ENOSPC=28
5698         if [ $RC -ne $ENOSPC ] ; then
5699                 error "dd should fail for -ENOSPC, but succeed."
5700         fi
5701 }
5702 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5703
5704 test_82() { # LU-1031
5705         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5706         local gid1=14091995
5707         local gid2=16022000
5708
5709         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5710         local MULTIPID1=$!
5711         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5712         local MULTIPID2=$!
5713         kill -USR1 $MULTIPID2
5714         sleep 2
5715         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5716                 error "First grouplock does not block second one"
5717         else
5718                 echo "Second grouplock blocks first one"
5719         fi
5720         kill -USR1 $MULTIPID1
5721         wait $MULTIPID1
5722         wait $MULTIPID2
5723 }
5724 run_test 82 "Basic grouplock test ==============================="
5725
5726 test_99a() {
5727         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5728                 return
5729         test_mkdir -p $DIR/d99cvsroot
5730         chown $RUNAS_ID $DIR/d99cvsroot
5731         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5732         cd $TMP
5733
5734         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5735         cd $oldPWD
5736 }
5737 run_test 99a "cvs init ========================================="
5738
5739 test_99b() {
5740         [ -z "$(which cvs 2>/dev/null)" ] &&
5741                 skip_env "could not find cvs" && return
5742         [ ! -d $DIR/d99cvsroot ] && test_99a
5743         cd /etc/init.d
5744         # some versions of cvs import exit(1) when asked to import links or
5745         # files they can't read.  ignore those files.
5746         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5747                         ! -perm +4 -printf '-I %f\n')
5748         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5749                 d99reposname vtag rtag
5750 }
5751 run_test 99b "cvs import ======================================="
5752
5753 test_99c() {
5754         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5755         [ ! -d $DIR/d99cvsroot ] && test_99b
5756         cd $DIR
5757         test_mkdir -p $DIR/d99reposname
5758         chown $RUNAS_ID $DIR/d99reposname
5759         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5760 }
5761 run_test 99c "cvs checkout ====================================="
5762
5763 test_99d() {
5764         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5765         [ ! -d $DIR/d99cvsroot ] && test_99c
5766         cd $DIR/d99reposname
5767         $RUNAS touch foo99
5768         $RUNAS cvs add -m 'addmsg' foo99
5769 }
5770 run_test 99d "cvs add =========================================="
5771
5772 test_99e() {
5773         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5774         [ ! -d $DIR/d99cvsroot ] && test_99c
5775         cd $DIR/d99reposname
5776         $RUNAS cvs update
5777 }
5778 run_test 99e "cvs update ======================================="
5779
5780 test_99f() {
5781         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5782         [ ! -d $DIR/d99cvsroot ] && test_99d
5783         cd $DIR/d99reposname
5784         $RUNAS cvs commit -m 'nomsg' foo99
5785     rm -fr $DIR/d99cvsroot
5786 }
5787 run_test 99f "cvs commit ======================================="
5788
5789 test_100() {
5790         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5791         [ "$NETTYPE" = tcp ] || \
5792                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5793                         return ; }
5794
5795         remote_ost_nodsh && skip "remote OST with nodsh" && return
5796         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5797         remote_servers || \
5798                 { skip "useless for local single node setup" && return; }
5799
5800         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5801                 [ "$PROT" != "tcp" ] && continue
5802                 RPORT=$(echo $REMOTE | cut -d: -f2)
5803                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5804
5805                 rc=0
5806                 LPORT=`echo $LOCAL | cut -d: -f2`
5807                 if [ $LPORT -ge 1024 ]; then
5808                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5809                         netstat -tna
5810                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5811                 fi
5812         done
5813         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5814 }
5815 run_test 100 "check local port using privileged port ==========="
5816
5817 function get_named_value()
5818 {
5819     local tag
5820
5821     tag=$1
5822     while read ;do
5823         line=$REPLY
5824         case $line in
5825         $tag*)
5826             echo $line | sed "s/^$tag[ ]*//"
5827             break
5828             ;;
5829         esac
5830     done
5831 }
5832
5833 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5834                    awk '/^max_cached_mb/ { print $2 }')
5835
5836 cleanup_101a() {
5837         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5838         trap 0
5839 }
5840
5841 test_101a() {
5842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5843         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
5844         local s
5845         local discard
5846         local nreads=10000
5847         local cache_limit=32
5848
5849         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5850         trap cleanup_101a EXIT
5851         $LCTL set_param -n llite.*.read_ahead_stats 0
5852         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5853
5854         #
5855         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5856         #
5857         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5858         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5859
5860         discard=0
5861         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5862                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5863                         discard=$(($discard + $s))
5864         done
5865         cleanup_101a
5866
5867         if [ $(($discard * 10)) -gt $nreads ] ;then
5868                 $LCTL get_param osc.*-osc*.rpc_stats
5869                 $LCTL get_param llite.*.read_ahead_stats
5870                 error "too many ($discard) discarded pages"
5871         fi
5872         rm -f $DIR/$tfile || true
5873 }
5874 run_test 101a "check read-ahead for random reads ================"
5875
5876 setup_test101bc() {
5877         test_mkdir -p $DIR/$tdir
5878         STRIPE_SIZE=1048576
5879         STRIPE_COUNT=$OSTCOUNT
5880         STRIPE_OFFSET=0
5881
5882         local list=$(comma_list $(osts_nodes))
5883         set_osd_param $list '' read_cache_enable 0
5884         set_osd_param $list '' writethrough_cache_enable 0
5885
5886         trap cleanup_test101bc EXIT
5887         # prepare the read-ahead file
5888         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5889
5890         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5891 }
5892
5893 cleanup_test101bc() {
5894         trap 0
5895         rm -rf $DIR/$tdir
5896         rm -f $DIR/$tfile
5897
5898         local list=$(comma_list $(osts_nodes))
5899         set_osd_param $list '' read_cache_enable 1
5900         set_osd_param $list '' writethrough_cache_enable 1
5901 }
5902
5903 calc_total() {
5904         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5905 }
5906
5907 ra_check_101() {
5908         local READ_SIZE=$1
5909         local STRIPE_SIZE=1048576
5910         local RA_INC=1048576
5911         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5912         local FILE_LENGTH=$((64*100))
5913         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5914                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5915         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5916                         get_named_value 'read but discarded' | \
5917                         cut -d" " -f1 | calc_total`
5918         if [ $DISCARD -gt $discard_limit ]; then
5919                 $LCTL get_param llite.*.read_ahead_stats
5920                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5921         else
5922                 echo "Read-ahead success for size ${READ_SIZE}"
5923         fi
5924 }
5925
5926 test_101b() {
5927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5928         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5929         local STRIPE_SIZE=1048576
5930         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5931         local FILE_LENGTH=$((STRIPE_SIZE*100))
5932         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5933         # prepare the read-ahead file
5934         setup_test101bc
5935         cancel_lru_locks osc
5936         for BIDX in 2 4 8 16 32 64 128 256
5937         do
5938                 local BSIZE=$((BIDX*4096))
5939                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5940                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5941                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5942                 $LCTL set_param -n llite.*.read_ahead_stats 0
5943                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5944                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5945                 cancel_lru_locks osc
5946                 ra_check_101 $BSIZE
5947         done
5948         cleanup_test101bc
5949         true
5950 }
5951 run_test 101b "check stride-io mode read-ahead ================="
5952
5953 test_101c() {
5954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5955         local STRIPE_SIZE=1048576
5956         local FILE_LENGTH=$((STRIPE_SIZE*100))
5957         local nreads=10000
5958         local osc_rpc_stats
5959
5960         setup_test101bc
5961
5962         cancel_lru_locks osc
5963         $LCTL set_param osc.*.rpc_stats 0
5964         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5965         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
5966                 local stats=$($LCTL get_param -n $osc_rpc_stats)
5967                 local lines=$(echo "$stats" | awk 'END {print NR;}')
5968                 local size
5969
5970                 if [ $lines -le 20 ]; then
5971                         continue
5972                 fi
5973                 for size in 1 2 4 8; do
5974                         local rpc=$(echo "$stats" |
5975                                     awk '($1 == "'$size':") {print $2; exit; }')
5976                         [ $rpc != 0 ] &&
5977                                 error "Small $((size*4))k read IO $rpc !"
5978                 done
5979                 echo "$osc_rpc_stats check passed!"
5980         done
5981         cleanup_test101bc
5982         true
5983 }
5984 run_test 101c "check stripe_size aligned read-ahead ================="
5985
5986 set_read_ahead() {
5987         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5988         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5989 }
5990
5991 test_101d() {
5992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5993         local file=$DIR/$tfile
5994         local sz_MB=${FILESIZE_101d:-500}
5995         local ra_MB=${READAHEAD_MB:-40}
5996
5997         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
5998         [ $free_MB -lt $sz_MB ] &&
5999                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6000
6001         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6002         $SETSTRIPE -c -1 $file || error "setstripe failed"
6003
6004         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6005         echo Cancel LRU locks on lustre client to flush the client cache
6006         cancel_lru_locks osc
6007
6008         echo Disable read-ahead
6009         local old_READAHEAD=$(set_read_ahead 0)
6010
6011         echo Reading the test file $file with read-ahead disabled
6012         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6013
6014         echo Cancel LRU locks on lustre client to flush the client cache
6015         cancel_lru_locks osc
6016         echo Enable read-ahead with ${ra_MB}MB
6017         set_read_ahead $ra_MB
6018
6019         echo Reading the test file $file with read-ahead enabled
6020         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6021
6022         echo "read-ahead disabled time read $raOFF"
6023         echo "read-ahead enabled  time read $raON"
6024
6025         set_read_ahead $old_READAHEAD
6026         rm -f $file
6027         wait_delete_completed
6028
6029         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6030                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6031 }
6032 run_test 101d "file read with and without read-ahead enabled"
6033
6034 test_101e() {
6035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6036         local file=$DIR/$tfile
6037         local size_KB=500  #KB
6038         local count=100
6039         local bsize=1024
6040
6041         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6042         local need_KB=$((count * size_KB))
6043         [ $free_KB -le $need_KB ] &&
6044                 skip_env "Need free space $need_KB, have $free_KB" && return
6045
6046         echo "Creating $count ${size_KB}K test files"
6047         for ((i = 0; i < $count; i++)); do
6048                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6049         done
6050
6051         echo "Cancel LRU locks on lustre client to flush the client cache"
6052         cancel_lru_locks osc
6053
6054         echo "Reset readahead stats"
6055         $LCTL set_param -n llite.*.read_ahead_stats 0
6056
6057         for ((i = 0; i < $count; i++)); do
6058                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6059         done
6060
6061         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6062                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6063
6064         for ((i = 0; i < $count; i++)); do
6065                 rm -rf $file.$i 2>/dev/null
6066         done
6067
6068         #10000 means 20% reads are missing in readahead
6069         [ $miss -lt 10000 ] ||  error "misses too much for small reads"
6070 }
6071 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6072
6073 cleanup_test101f() {
6074     trap 0
6075     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6076     rm -rf $DIR/$tfile 2>/dev/null
6077 }
6078
6079 test_101f() {
6080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6081     local file=$DIR/$tfile
6082     local nreads=1000
6083
6084     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6085     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6086     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6087     trap cleanup_test101f EXIT
6088
6089     echo Cancel LRU locks on lustre client to flush the client cache
6090     cancel_lru_locks osc
6091
6092     echo Reset readahead stats
6093     $LCTL set_param -n llite.*.read_ahead_stats 0
6094     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6095     # readahead should read in 2M file on second read, so only miss
6096     # 2 pages.
6097     echo Random 4K reads on 2M file for 1000 times
6098     $READS -f $file -s 2097152 -b 4096 -n $nreads
6099
6100     echo checking missing pages
6101     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6102           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6103
6104     [ $miss -lt 3 ] || error "misses too much pages!"
6105     cleanup_test101f
6106 }
6107 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6108
6109 setup_test102() {
6110         test_mkdir -p $DIR/$tdir
6111         chown $RUNAS_ID $DIR/$tdir
6112         STRIPE_SIZE=65536
6113         STRIPE_OFFSET=1
6114         STRIPE_COUNT=$OSTCOUNT
6115         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
6116
6117         trap cleanup_test102 EXIT
6118         cd $DIR
6119         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6120         cd $DIR/$tdir
6121         for num in 1 2 3 4; do
6122                 for count in $(seq 1 $STRIPE_COUNT); do
6123                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6124                                 local size=`expr $STRIPE_SIZE \* $num`
6125                                 local file=file"$num-$idx-$count"
6126                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6127                         done
6128                 done
6129         done
6130
6131         cd $DIR
6132         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6133 }
6134
6135 cleanup_test102() {
6136         trap 0
6137         rm -f $TMP/f102.tar
6138         rm -rf $DIR/d0.sanity/d102
6139 }
6140
6141 test_102a() {
6142         local testfile=$DIR/xattr_testfile
6143
6144         touch $testfile
6145
6146         [ "$UID" != 0 ] && skip_env "must run as root" && return
6147         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6148                 skip_env "must have user_xattr" && return
6149
6150         [ -z "$(which setfattr 2>/dev/null)" ] &&
6151                 skip_env "could not find setfattr" && return
6152
6153         echo "set/get xattr..."
6154         setfattr -n trusted.name1 -v value1 $testfile ||
6155                 error "setfattr -n trusted.name1=value1 $testfile failed"
6156         getfattr -n trusted.name1 $testfile 2> /dev/null |
6157           grep "trusted.name1=.value1" ||
6158                 error "$testfile missing trusted.name1=value1"
6159
6160         setfattr -n user.author1 -v author1 $testfile ||
6161                 error "setfattr -n user.author1=author1 $testfile failed"
6162         getfattr -n user.author1 $testfile 2> /dev/null |
6163           grep "user.author1=.author1" ||
6164                 error "$testfile missing trusted.author1=author1"
6165
6166         echo "listxattr..."
6167         setfattr -n trusted.name2 -v value2 $testfile ||
6168                 error "$testfile unable to set trusted.name2"
6169         setfattr -n trusted.name3 -v value3 $testfile ||
6170                 error "$testfile unable to set trusted.name3"
6171         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6172             grep "trusted.name" | wc -l) -eq 3 ] ||
6173                 error "$testfile missing 3 trusted.name xattrs"
6174
6175         setfattr -n user.author2 -v author2 $testfile ||
6176                 error "$testfile unable to set user.author2"
6177         setfattr -n user.author3 -v author3 $testfile ||
6178                 error "$testfile unable to set user.author3"
6179         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6180             grep "user.author" | wc -l) -eq 3 ] ||
6181                 error "$testfile missing 3 user.author xattrs"
6182
6183         echo "remove xattr..."
6184         setfattr -x trusted.name1 $testfile ||
6185                 error "$testfile error deleting trusted.name1"
6186         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6187                 error "$testfile did not delete trusted.name1 xattr"
6188
6189         setfattr -x user.author1 $testfile ||
6190                 error "$testfile error deleting user.author1"
6191         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6192                 error "$testfile did not delete trusted.name1 xattr"
6193
6194         # b10667: setting lustre special xattr be silently discarded
6195         echo "set lustre special xattr ..."
6196         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6197                 error "$testfile allowed setting trusted.lov"
6198 }
6199 run_test 102a "user xattr test =================================="
6200
6201 test_102b() {
6202         # b10930: get/set/list trusted.lov xattr
6203         echo "get/set/list trusted.lov xattr ..."
6204         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6205         local testfile=$DIR/$tfile
6206         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6207                 error "setstripe failed"
6208         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6209                 error "getstripe failed"
6210         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
6211         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
6212
6213         local testfile2=${testfile}2
6214         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
6215                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
6216
6217         $MCREATE $testfile2
6218         setfattr -n trusted.lov -v $value $testfile2
6219         local stripe_size=$($GETSTRIPE -S $testfile2)
6220         local stripe_count=$($GETSTRIPE -c $testfile2)
6221         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6222         [ $stripe_count -eq $STRIPECOUNT ] ||
6223                 error "stripe count $stripe_count != $STRIPECOUNT"
6224         rm -f $DIR/$tfile
6225 }
6226 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6227
6228 test_102c() {
6229         # b10930: get/set/list lustre.lov xattr
6230         echo "get/set/list lustre.lov xattr ..."
6231         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6232         test_mkdir -p $DIR/$tdir
6233         chown $RUNAS_ID $DIR/$tdir
6234         local testfile=$DIR/$tdir/$tfile
6235         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6236                 error "setstripe failed"
6237         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6238                 error "getstripe failed"
6239         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6240         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6241
6242         local testfile2=${testfile}2
6243         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6244                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6245
6246         $RUNAS $MCREATE $testfile2
6247         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6248         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6249         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6250         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6251         [ $stripe_count -eq $STRIPECOUNT ] ||
6252                 error "stripe count $stripe_count != $STRIPECOUNT"
6253 }
6254 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6255
6256 compare_stripe_info1() {
6257         local stripe_index_all_zero=true
6258
6259         for num in 1 2 3 4; do
6260                 for count in $(seq 1 $STRIPE_COUNT); do
6261                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6262                                 local size=$((STRIPE_SIZE * num))
6263                                 local file=file"$num-$offset-$count"
6264                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6265                                 [ $stripe_size -ne $size ] &&
6266                                     error "$file: size $stripe_size != $size"
6267                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6268                                 # allow fewer stripes to be created, ORI-601
6269                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
6270                                     error "$file: count $stripe_count != $count"
6271                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6272                                 [ $stripe_index -ne 0 ] &&
6273                                         stripe_index_all_zero=false
6274                         done
6275                 done
6276         done
6277         $stripe_index_all_zero &&
6278                 error "all files are being extracted starting from OST index 0"
6279         return 0
6280 }
6281
6282 find_lustre_tar() {
6283         [ -n "$(which tar 2>/dev/null)" ] &&
6284                 strings $(which tar) | grep -q "lustre" && echo tar
6285 }
6286
6287 test_102d() {
6288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6289         # b10930: tar test for trusted.lov xattr
6290         TAR=$(find_lustre_tar)
6291         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6292         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6293         setup_test102
6294         test_mkdir -p $DIR/d102d
6295         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6296         cd $DIR/d102d/$tdir
6297         compare_stripe_info1
6298 }
6299 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6300
6301 test_102f() {
6302         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6303         # b10930: tar test for trusted.lov xattr
6304         TAR=$(find_lustre_tar)
6305         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6306         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6307         setup_test102
6308         test_mkdir -p $DIR/d102f
6309         cd $DIR
6310         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6311         cd $DIR/d102f/$tdir
6312         compare_stripe_info1
6313 }
6314 run_test 102f "tar copy files, not keep osts ==========="
6315
6316 grow_xattr() {
6317         local xsize=${1:-1024}  # in bytes
6318         local file=$DIR/$tfile
6319
6320         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6321                 skip "must have user_xattr" && return 0
6322         [ -z "$(which setfattr 2>/dev/null)" ] &&
6323                 skip_env "could not find setfattr" && return 0
6324         [ -z "$(which getfattr 2>/dev/null)" ] &&
6325                 skip_env "could not find getfattr" && return 0
6326
6327         touch $file
6328
6329         local value="$(generate_string $xsize)"
6330
6331         local xbig=trusted.big
6332         log "save $xbig on $file"
6333         setfattr -n $xbig -v $value $file ||
6334                 error "saving $xbig on $file failed"
6335
6336         local orig=$(get_xattr_value $xbig $file)
6337         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6338
6339         local xsml=trusted.sml
6340         log "save $xsml on $file"
6341         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6342
6343         local new=$(get_xattr_value $xbig $file)
6344         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6345
6346         log "grow $xsml on $file"
6347         setfattr -n $xsml -v "$value" $file ||
6348                 error "growing $xsml on $file failed"
6349
6350         new=$(get_xattr_value $xbig $file)
6351         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6352         log "$xbig still valid after growing $xsml"
6353
6354         rm -f $file
6355 }
6356
6357 test_102h() { # bug 15777
6358         grow_xattr 1024
6359 }
6360 run_test 102h "grow xattr from inside inode to external block"
6361
6362 test_102ha() {
6363         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6364         grow_xattr $(max_xattr_size)
6365 }
6366 run_test 102ha "grow xattr from inside inode to external inode"
6367
6368 test_102i() { # bug 17038
6369         touch $DIR/$tfile
6370         ln -s $DIR/$tfile $DIR/${tfile}link
6371         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6372         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"
6373         rm -f $DIR/$tfile $DIR/${tfile}link
6374 }
6375 run_test 102i "lgetxattr test on symbolic link ============"
6376
6377 test_102j() {
6378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6379         TAR=$(find_lustre_tar)
6380         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6381         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6382         setup_test102 "$RUNAS"
6383         test_mkdir -p $DIR/d102j
6384         chown $RUNAS_ID $DIR/d102j
6385         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6386         cd $DIR/d102j/$tdir
6387         compare_stripe_info1 "$RUNAS"
6388 }
6389 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6390
6391 test_102k() {
6392         touch $DIR/$tfile
6393         # b22187 just check that does not crash for regular file.
6394         setfattr -n trusted.lov $DIR/$tfile
6395         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6396         local test_kdir=$DIR/d102k
6397         test_mkdir $test_kdir
6398         local default_size=`$GETSTRIPE -S $test_kdir`
6399         local default_count=`$GETSTRIPE -c $test_kdir`
6400         local default_offset=`$GETSTRIPE -i $test_kdir`
6401         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6402                 error 'dir setstripe failed'
6403         setfattr -n trusted.lov $test_kdir
6404         local stripe_size=`$GETSTRIPE -S $test_kdir`
6405         local stripe_count=`$GETSTRIPE -c $test_kdir`
6406         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6407         [ $stripe_size -eq $default_size ] ||
6408                 error "stripe size $stripe_size != $default_size"
6409         [ $stripe_count -eq $default_count ] ||
6410                 error "stripe count $stripe_count != $default_count"
6411         [ $stripe_offset -eq $default_offset ] ||
6412                 error "stripe offset $stripe_offset != $default_offset"
6413         rm -rf $DIR/$tfile $test_kdir
6414 }
6415 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6416
6417 test_102l() {
6418         # LU-532 trusted. xattr is invisible to non-root
6419         local testfile=$DIR/$tfile
6420
6421         touch $testfile
6422
6423         echo "listxattr as user..."
6424         chown $RUNAS_ID $testfile
6425         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6426             grep -q "trusted" &&
6427                 error "$testfile trusted xattrs are user visible"
6428
6429         return 0;
6430 }
6431 run_test 102l "listxattr size test =================================="
6432
6433 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6434         local path=$DIR/$tfile
6435         touch $path
6436
6437         listxattr_size_check $path || error "listattr_size_check $path failed"
6438 }
6439 run_test 102m "Ensure listxattr fails on small bufffer ========"
6440
6441 cleanup_test102
6442
6443 getxattr() { # getxattr path name
6444         # Return the base64 encoding of the value of xattr name on path.
6445         local path=$1
6446         local name=$2
6447
6448         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6449         # file: $path
6450         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6451         #
6452         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6453
6454         getfattr --absolute-names --encoding=base64 --name=$name $path |
6455                 awk -F= -v name=$name '$1 == name {
6456                         print substr($0, index($0, "=") + 1);
6457         }'
6458 }
6459
6460 test_102n() { # LU-4101 mdt: protect internal xattrs
6461         local file0=$DIR/$tfile.0
6462         local file1=$DIR/$tfile.1
6463         local xattr0=$TMP/$tfile.0
6464         local xattr1=$TMP/$tfile.1
6465         local name
6466         local value
6467
6468         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6469         then
6470                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6471                 return
6472         fi
6473
6474         rm -rf $file0 $file1 $xattr0 $xattr1
6475         touch $file0 $file1
6476
6477         # Get 'before' xattrs of $file1.
6478         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6479
6480         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6481                 # Try to copy xattr from $file0 to $file1.
6482                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6483
6484                 setfattr --name=trusted.$name --value="$value" $file1 ||
6485                         error "setxattr 'trusted.$name' failed"
6486
6487                 # Try to set a garbage xattr.
6488                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6489
6490                 setfattr --name=trusted.$name --value="$value" $file1 ||
6491                         error "setxattr 'trusted.$name' failed"
6492
6493                 # Try to remove the xattr from $file1. We don't care if this
6494                 # appears to succeed or fail, we just don't want there to be
6495                 # any changes or crashes.
6496                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6497         done
6498
6499         # Get 'after' xattrs of file1.
6500         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6501
6502         if ! diff $xattr0 $xattr1; then
6503                 error "before and after xattrs of '$file1' differ"
6504         fi
6505
6506         rm -rf $file0 $file1 $xattr0 $xattr1
6507
6508         return 0
6509 }
6510 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6511
6512 run_acl_subtest()
6513 {
6514     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6515     return $?
6516 }
6517
6518 test_103 () {
6519         [ "$UID" != 0 ] && skip_env "must run as root" && return
6520         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6521                 skip "must have acl enabled" && return
6522         [ -z "$(which setfacl 2>/dev/null)" ] &&
6523                 skip_env "could not find setfacl" && return
6524         $GSS && skip "could not run under gss" && return
6525
6526         declare -a identity_old
6527
6528         for num in $(seq $MDSCOUNT); do
6529                 switch_identity $num true || identity_old[$num]=$?
6530         done
6531
6532         SAVE_UMASK=$(umask)
6533         umask 0022
6534         cd $DIR
6535
6536         echo "performing cp ..."
6537         run_acl_subtest cp || error "run_acl_subtest cp failed"
6538         echo "performing getfacl-noacl..."
6539         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6540         echo "performing misc..."
6541         run_acl_subtest misc || error  "misc test failed"
6542         echo "performing permissions..."
6543         run_acl_subtest permissions || error "permissions failed"
6544         echo "performing setfacl..."
6545         run_acl_subtest setfacl || error  "setfacl test failed"
6546
6547         # inheritance test got from HP
6548         echo "performing inheritance..."
6549         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6550         chmod +x make-tree || error "chmod +x failed"
6551         run_acl_subtest inheritance || error "inheritance test failed"
6552         rm -f make-tree
6553
6554         echo "LU-974 ignore umask when acl is enabled..."
6555         run_acl_subtest 974 || error "LU-974 umask test failed"
6556         if [ $MDSCOUNT -ge 2 ]; then
6557                 run_acl_subtest 974_remote ||
6558                         error "LU-974 umask test failed under remote dir"
6559         fi
6560
6561         echo "LU-2561 newly created file is same size as directory..."
6562         run_acl_subtest 2561 || error "LU-2561 test failed"
6563
6564         cd $SAVE_PWD
6565         umask $SAVE_UMASK
6566
6567         for num in $(seq $MDSCOUNT); do
6568                 if [ "${identity_old[$num]}" = 1 ]; then
6569                         switch_identity $num false || identity_old[$num]=$?
6570                 fi
6571         done
6572 }
6573 run_test 103 "acl test ========================================="
6574
6575 test_104a() {
6576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6577         touch $DIR/$tfile
6578         lfs df || error "lfs df failed"
6579         lfs df -ih || error "lfs df -ih failed"
6580         lfs df -h $DIR || error "lfs df -h $DIR failed"
6581         lfs df -i $DIR || error "lfs df -i $DIR failed"
6582         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6583         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6584
6585         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6586         lctl --device %$OSC deactivate
6587         lfs df || error "lfs df with deactivated OSC failed"
6588         lctl --device %$OSC activate
6589         # wait the osc back to normal
6590         wait_osc_import_state client ost FULL
6591
6592         lfs df || error "lfs df with reactivated OSC failed"
6593         rm -f $DIR/$tfile
6594 }
6595 run_test 104a "lfs df [-ih] [path] test ========================="
6596
6597 test_104b() {
6598         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6599         [ $RUNAS_ID -eq $UID ] &&
6600                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6601         chmod 666 /dev/obd
6602         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6603                         grep "Permission denied" | wc -l)))
6604         if [ $denied_cnt -ne 0 ]; then
6605                 error "lfs check servers test failed"
6606         fi
6607 }
6608 run_test 104b "$RUNAS lfs check servers test ===================="
6609
6610 test_105a() {
6611         # doesn't work on 2.4 kernels
6612         touch $DIR/$tfile
6613         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6614                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6615         else
6616                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6617         fi
6618         rm -f $DIR/$tfile
6619 }
6620 run_test 105a "flock when mounted without -o flock test ========"
6621
6622 test_105b() {
6623         touch $DIR/$tfile
6624         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6625                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6626         else
6627                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6628         fi
6629         rm -f $DIR/$tfile
6630 }
6631 run_test 105b "fcntl when mounted without -o flock test ========"
6632
6633 test_105c() {
6634         touch $DIR/$tfile
6635         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6636                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6637         else
6638                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6639         fi
6640         rm -f $DIR/$tfile
6641 }
6642 run_test 105c "lockf when mounted without -o flock test ========"
6643
6644 test_105d() { # bug 15924
6645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6646         test_mkdir -p $DIR/$tdir
6647         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6648                 skip "mount w/o flock enabled" && return
6649         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6650         $LCTL set_param fail_loc=0x80000315
6651         flocks_test 2 $DIR/$tdir
6652 }
6653 run_test 105d "flock race (should not freeze) ========"
6654
6655 test_105e() { # bug 22660 && 22040
6656         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6657                 skip "mount w/o flock enabled" && return
6658         touch $DIR/$tfile
6659         flocks_test 3 $DIR/$tfile
6660 }
6661 run_test 105e "Two conflicting flocks from same process ======="
6662
6663 test_106() { #bug 10921
6664         test_mkdir -p $DIR/$tdir
6665         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6666         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6667 }
6668 run_test 106 "attempt exec of dir followed by chown of that dir"
6669
6670 test_107() {
6671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6672         CDIR=`pwd`
6673         cd $DIR
6674
6675         local file=core
6676         rm -f $file
6677
6678         local save_pattern=$(sysctl -n kernel.core_pattern)
6679         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6680         sysctl -w kernel.core_pattern=$file
6681         sysctl -w kernel.core_uses_pid=0
6682
6683         ulimit -c unlimited
6684         sleep 60 &
6685         SLEEPPID=$!
6686
6687         sleep 1
6688
6689         kill -s 11 $SLEEPPID
6690         wait $SLEEPPID
6691         if [ -e $file ]; then
6692                 size=`stat -c%s $file`
6693                 [ $size -eq 0 ] && error "Fail to create core file $file"
6694         else
6695                 error "Fail to create core file $file"
6696         fi
6697         rm -f $file
6698         sysctl -w kernel.core_pattern=$save_pattern
6699         sysctl -w kernel.core_uses_pid=$save_uses_pid
6700         cd $CDIR
6701 }
6702 run_test 107 "Coredump on SIG"
6703
6704 test_110() {
6705         test_mkdir -p $DIR/$tdir
6706         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6707                 error "mkdir with 255 char failed"
6708         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6709                 error "mkdir with 256 char should fail, but did not"
6710         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6711                 error "create with 255 char failed"
6712         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6713                 error "create with 256 char should fail, but did not"
6714
6715         ls -l $DIR/$tdir
6716         rm -rf $DIR/$tdir
6717 }
6718 run_test 110 "filename length checking"
6719
6720 test_115() {
6721         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6722         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6723             cut -c11-20)
6724         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6725             return
6726         echo "Starting with $OSTIO_pre threads"
6727
6728         NUMTEST=20000
6729         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6730         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6731         echo "$NUMTEST creates/unlinks"
6732         test_mkdir -p $DIR/$tdir
6733         createmany -o $DIR/$tdir/$tfile $NUMTEST
6734         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6735
6736         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6737             cut -c11-20)
6738
6739         # don't return an error
6740         [ $OSTIO_post == $OSTIO_pre ] && echo \
6741             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6742             echo "This may be fine, depending on what ran before this test" &&
6743             echo "and how fast this system is." && return
6744
6745         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6746 }
6747 run_test 115 "verify dynamic thread creation===================="
6748
6749 free_min_max () {
6750         wait_delete_completed
6751         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6752         echo OST kbytes available: ${AVAIL[@]}
6753         MAXI=0; MAXV=${AVAIL[0]}
6754         MINI=0; MINV=${AVAIL[0]}
6755         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6756             #echo OST $i: ${AVAIL[i]}kb
6757             if [ ${AVAIL[i]} -gt $MAXV ]; then
6758                 MAXV=${AVAIL[i]}; MAXI=$i
6759             fi
6760             if [ ${AVAIL[i]} -lt $MINV ]; then
6761                 MINV=${AVAIL[i]}; MINI=$i
6762             fi
6763         done
6764         echo Min free space: OST $MINI: $MINV
6765         echo Max free space: OST $MAXI: $MAXV
6766 }
6767
6768 test_116a() { # was previously test_116()
6769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6770         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6771
6772         echo -n "Free space priority "
6773         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
6774                 head -1
6775         declare -a AVAIL
6776         free_min_max
6777
6778         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
6779         trap simple_cleanup_common EXIT
6780
6781         # Check if we need to generate uneven OSTs
6782         test_mkdir -p $DIR/$tdir/OST${MINI}
6783         local FILL=$(($MINV / 4))
6784         local DIFF=$(($MAXV - $MINV))
6785         local DIFF2=$(($DIFF * 100 / $MINV))
6786
6787         local threshold=$(do_facet $SINGLEMDS \
6788                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -1)
6789         threshold=${threshold%%%}
6790         echo -n "Check for uneven OSTs: "
6791         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
6792
6793         if [ $DIFF2 -gt $threshold ]; then
6794                 echo "ok"
6795                 echo "Don't need to fill OST$MINI"
6796         else
6797                 # generate uneven OSTs. Write 2% over the QOS threshold value
6798                 echo "no"
6799                 DIFF=$(($threshold - $DIFF2 + 2))
6800                 DIFF2=$(( ($MINV * $DIFF)/100 ))
6801                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
6802                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
6803                         error "setstripe failed"
6804                 DIFF=$(($DIFF2 / 2048))
6805                 i=0
6806                 while [ $i -lt $DIFF ]; do
6807                         i=$(($i + 1))
6808                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
6809                                 bs=2M count=1 2>/dev/null
6810                         echo -n .
6811                 done
6812                 echo .
6813                 sync
6814                 sleep_maxage
6815                 free_min_max
6816         fi
6817
6818         DIFF=$(($MAXV - $MINV))
6819         DIFF2=$(($DIFF * 100 / $MINV))
6820         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
6821         if [ $DIFF2 -gt $threshold ]; then
6822                 echo "ok"
6823         else
6824                 echo "failed - QOS mode won't be used"
6825                 skip "QOS imbalance criteria not met"
6826                 simple_cleanup_common
6827                 return
6828         fi
6829
6830         MINI1=$MINI; MINV1=$MINV
6831         MAXI1=$MAXI; MAXV1=$MAXV
6832
6833         # now fill using QOS
6834         $SETSTRIPE -c 1 $DIR/$tdir
6835         FILL=$(($FILL / 200))
6836         if [ $FILL -gt 600 ]; then
6837                 FILL=600
6838         fi
6839         echo "writing $FILL files to QOS-assigned OSTs"
6840         i=0
6841         while [ $i -lt $FILL ]; do
6842                 i=$(($i + 1))
6843                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
6844                         count=1 2>/dev/null
6845                 echo -n .
6846         done
6847         echo "wrote $i 200k files"
6848         sync
6849         sleep_maxage
6850
6851         echo "Note: free space may not be updated, so measurements might be off"
6852         free_min_max
6853         DIFF2=$(($MAXV - $MINV))
6854         echo "free space delta: orig $DIFF final $DIFF2"
6855         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6856         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6857         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
6858         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6859         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
6860         FILL=$(($DIFF2 * 100 / $DIFF - 100))
6861         [ $DIFF -gt 0 ] &&
6862                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
6863
6864         # Figure out which files were written where
6865         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6866                   awk '/'$MINI1': / {print $2; exit}')
6867         echo $UUID
6868         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6869         echo "$MINC files created on smaller OST $MINI1"
6870         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6871                   awk '/'$MAXI1': / {print $2; exit}')
6872         echo $UUID
6873         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6874         echo "$MAXC files created on larger OST $MAXI1"
6875         FILL=$(($MAXC * 100 / $MINC - 100))
6876         [ $MINC -gt 0 ] &&
6877                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
6878         [ $MAXC -gt $MINC ] ||
6879                 error_ignore LU-9 "stripe QOS didn't balance free space"
6880         simple_cleanup_common
6881 }
6882 run_test 116a "stripe QOS: free space balance ==================="
6883
6884 test_116b() { # LU-2093
6885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6886 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6887         local old_rr
6888         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6889         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6890         mkdir -p $DIR/$tdir
6891         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6892         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6893         do_facet $SINGLEMDS lctl set_param fail_loc=0
6894         rm -rf $DIR/$tdir
6895         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6896 }
6897 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6898
6899 test_117() # bug 10891
6900 {
6901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6902         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6903         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6904         lctl set_param fail_loc=0x21e
6905         > $DIR/$tfile || error "truncate failed"
6906         lctl set_param fail_loc=0
6907         echo "Truncate succeeded."
6908         rm -f $DIR/$tfile
6909 }
6910 run_test 117 "verify osd extend =========="
6911
6912 NO_SLOW_RESENDCOUNT=4
6913 export OLD_RESENDCOUNT=""
6914 set_resend_count () {
6915         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6916         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6917         lctl set_param -n $PROC_RESENDCOUNT $1
6918         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6919 }
6920
6921 # for reduce test_118* time (b=14842)
6922 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6923
6924 # Reset async IO behavior after error case
6925 reset_async() {
6926         FILE=$DIR/reset_async
6927
6928         # Ensure all OSCs are cleared
6929         $SETSTRIPE -c -1 $FILE
6930         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6931         sync
6932         rm $FILE
6933 }
6934
6935 test_118a() #bug 11710
6936 {
6937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6938         reset_async
6939
6940         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6941         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6942         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6943
6944         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6945                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6946                 return 1;
6947         fi
6948         rm -f $DIR/$tfile
6949 }
6950 run_test 118a "verify O_SYNC works =========="
6951
6952 test_118b()
6953 {
6954         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6955         remote_ost_nodsh && skip "remote OST with nodsh" && return
6956
6957         reset_async
6958
6959         #define OBD_FAIL_OST_ENOENT 0x217
6960         set_nodes_failloc "$(osts_nodes)" 0x217
6961         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6962         RC=$?
6963         set_nodes_failloc "$(osts_nodes)" 0
6964         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6965         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6966                     grep -c writeback)
6967
6968         if [[ $RC -eq 0 ]]; then
6969                 error "Must return error due to dropped pages, rc=$RC"
6970                 return 1;
6971         fi
6972
6973         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6974                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6975                 return 1;
6976         fi
6977
6978         echo "Dirty pages not leaked on ENOENT"
6979
6980         # Due to the above error the OSC will issue all RPCs syncronously
6981         # until a subsequent RPC completes successfully without error.
6982         $MULTIOP $DIR/$tfile Ow4096yc
6983         rm -f $DIR/$tfile
6984
6985         return 0
6986 }
6987 run_test 118b "Reclaim dirty pages on fatal error =========="
6988
6989 test_118c()
6990 {
6991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6992
6993         # for 118c, restore the original resend count, LU-1940
6994         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
6995                                 set_resend_count $OLD_RESENDCOUNT
6996         remote_ost_nodsh && skip "remote OST with nodsh" && return
6997
6998         reset_async
6999
7000         #define OBD_FAIL_OST_EROFS               0x216
7001         set_nodes_failloc "$(osts_nodes)" 0x216
7002
7003         # multiop should block due to fsync until pages are written
7004         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7005         MULTIPID=$!
7006         sleep 1
7007
7008         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7009                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7010         fi
7011
7012         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7013                     grep -c writeback)
7014         if [[ $WRITEBACK -eq 0 ]]; then
7015                 error "No page in writeback, writeback=$WRITEBACK"
7016         fi
7017
7018         set_nodes_failloc "$(osts_nodes)" 0
7019         wait $MULTIPID
7020         RC=$?
7021         if [[ $RC -ne 0 ]]; then
7022                 error "Multiop fsync failed, rc=$RC"
7023         fi
7024
7025         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7026         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7027                     grep -c writeback)
7028         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7029                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7030         fi
7031
7032         rm -f $DIR/$tfile
7033         echo "Dirty pages flushed via fsync on EROFS"
7034         return 0
7035 }
7036 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7037
7038 # continue to use small resend count to reduce test_118* time (b=14842)
7039 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7040
7041 test_118d()
7042 {
7043         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7044         remote_ost_nodsh && skip "remote OST with nodsh" && return
7045
7046         reset_async
7047
7048         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7049         set_nodes_failloc "$(osts_nodes)" 0x214
7050         # multiop should block due to fsync until pages are written
7051         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7052         MULTIPID=$!
7053         sleep 1
7054
7055         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7056                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7057         fi
7058
7059         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7060                     grep -c writeback)
7061         if [[ $WRITEBACK -eq 0 ]]; then
7062                 error "No page in writeback, writeback=$WRITEBACK"
7063         fi
7064
7065         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7066         set_nodes_failloc "$(osts_nodes)" 0
7067
7068         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7069         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7070                     grep -c writeback)
7071         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7072                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7073         fi
7074
7075         rm -f $DIR/$tfile
7076         echo "Dirty pages gaurenteed flushed via fsync"
7077         return 0
7078 }
7079 run_test 118d "Fsync validation inject a delay of the bulk =========="
7080
7081 test_118f() {
7082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7083         reset_async
7084
7085         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7086         lctl set_param fail_loc=0x8000040a
7087
7088         # Should simulate EINVAL error which is fatal
7089         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7090         RC=$?
7091         if [[ $RC -eq 0 ]]; then
7092                 error "Must return error due to dropped pages, rc=$RC"
7093         fi
7094
7095         lctl set_param fail_loc=0x0
7096
7097         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7098         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7099         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7100                     grep -c writeback)
7101         if [[ $LOCKED -ne 0 ]]; then
7102                 error "Locked pages remain in cache, locked=$LOCKED"
7103         fi
7104
7105         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7106                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7107         fi
7108
7109         rm -f $DIR/$tfile
7110         echo "No pages locked after fsync"
7111
7112         reset_async
7113         return 0
7114 }
7115 run_test 118f "Simulate unrecoverable OSC side error =========="
7116
7117 test_118g() {
7118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7119         reset_async
7120
7121         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7122         lctl set_param fail_loc=0x406
7123
7124         # simulate local -ENOMEM
7125         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7126         RC=$?
7127
7128         lctl set_param fail_loc=0
7129         if [[ $RC -eq 0 ]]; then
7130                 error "Must return error due to dropped pages, rc=$RC"
7131         fi
7132
7133         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7134         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7135         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7136                         grep -c writeback)
7137         if [[ $LOCKED -ne 0 ]]; then
7138                 error "Locked pages remain in cache, locked=$LOCKED"
7139         fi
7140
7141         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7142                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7143         fi
7144
7145         rm -f $DIR/$tfile
7146         echo "No pages locked after fsync"
7147
7148         reset_async
7149         return 0
7150 }
7151 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7152
7153 test_118h() {
7154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7155         remote_ost_nodsh && skip "remote OST with nodsh" && return
7156
7157         reset_async
7158
7159         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7160         set_nodes_failloc "$(osts_nodes)" 0x20e
7161         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7162         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7163         RC=$?
7164
7165         set_nodes_failloc "$(osts_nodes)" 0
7166         if [[ $RC -eq 0 ]]; then
7167                 error "Must return error due to dropped pages, rc=$RC"
7168         fi
7169
7170         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7171         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7172         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7173                     grep -c writeback)
7174         if [[ $LOCKED -ne 0 ]]; then
7175                 error "Locked pages remain in cache, locked=$LOCKED"
7176         fi
7177
7178         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7179                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7180         fi
7181
7182         rm -f $DIR/$tfile
7183         echo "No pages locked after fsync"
7184
7185         return 0
7186 }
7187 run_test 118h "Verify timeout in handling recoverables errors  =========="
7188
7189 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7190
7191 test_118i() {
7192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7193         remote_ost_nodsh && skip "remote OST with nodsh" && return
7194
7195         reset_async
7196
7197         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7198         set_nodes_failloc "$(osts_nodes)" 0x20e
7199
7200         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7201         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7202         PID=$!
7203         sleep 5
7204         set_nodes_failloc "$(osts_nodes)" 0
7205
7206         wait $PID
7207         RC=$?
7208         if [[ $RC -ne 0 ]]; then
7209                 error "got error, but should be not, rc=$RC"
7210         fi
7211
7212         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7213         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7214         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7215         if [[ $LOCKED -ne 0 ]]; then
7216                 error "Locked pages remain in cache, locked=$LOCKED"
7217         fi
7218
7219         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7220                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7221         fi
7222
7223         rm -f $DIR/$tfile
7224         echo "No pages locked after fsync"
7225
7226         return 0
7227 }
7228 run_test 118i "Fix error before timeout in recoverable error  =========="
7229
7230 [ "$SLOW" = "no" ] && set_resend_count 4
7231
7232 test_118j() {
7233         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7234         remote_ost_nodsh && skip "remote OST with nodsh" && return
7235
7236         reset_async
7237
7238         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7239         set_nodes_failloc "$(osts_nodes)" 0x220
7240
7241         # return -EIO from OST
7242         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7243         RC=$?
7244         set_nodes_failloc "$(osts_nodes)" 0x0
7245         if [[ $RC -eq 0 ]]; then
7246                 error "Must return error due to dropped pages, rc=$RC"
7247         fi
7248
7249         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7250         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7251         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7252         if [[ $LOCKED -ne 0 ]]; then
7253                 error "Locked pages remain in cache, locked=$LOCKED"
7254         fi
7255
7256         # in recoverable error on OST we want resend and stay until it finished
7257         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7258                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7259         fi
7260
7261         rm -f $DIR/$tfile
7262         echo "No pages locked after fsync"
7263
7264         return 0
7265 }
7266 run_test 118j "Simulate unrecoverable OST side error =========="
7267
7268 test_118k()
7269 {
7270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7271         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7272
7273         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7274         set_nodes_failloc "$(osts_nodes)" 0x20e
7275         test_mkdir -p $DIR/$tdir
7276
7277         for ((i=0;i<10;i++)); do
7278                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7279                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7280                 SLEEPPID=$!
7281                 sleep 0.500s
7282                 kill $SLEEPPID
7283                 wait $SLEEPPID
7284         done
7285
7286         set_nodes_failloc "$(osts_nodes)" 0
7287         rm -rf $DIR/$tdir
7288 }
7289 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7290
7291 test_118l()
7292 {
7293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7294         # LU-646
7295         test_mkdir -p $DIR/$tdir
7296         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7297         rm -rf $DIR/$tdir
7298 }
7299 run_test 118l "fsync dir ========="
7300
7301 test_118m() # LU-3066
7302 {
7303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7304         test_mkdir -p $DIR/$tdir
7305         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7306         rm -rf $DIR/$tdir
7307 }
7308 run_test 118m "fdatasync dir ========="
7309
7310 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7311
7312 test_119a() # bug 11737
7313 {
7314         BSIZE=$((512 * 1024))
7315         directio write $DIR/$tfile 0 1 $BSIZE
7316         # We ask to read two blocks, which is more than a file size.
7317         # directio will indicate an error when requested and actual
7318         # sizes aren't equeal (a normal situation in this case) and
7319         # print actual read amount.
7320         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7321         if [ "$NOB" != "$BSIZE" ]; then
7322                 error "read $NOB bytes instead of $BSIZE"
7323         fi
7324         rm -f $DIR/$tfile
7325 }
7326 run_test 119a "Short directIO read must return actual read amount"
7327
7328 test_119b() # bug 11737
7329 {
7330         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7331
7332         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7333         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7334         sync
7335         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7336                 error "direct read failed"
7337         rm -f $DIR/$tfile
7338 }
7339 run_test 119b "Sparse directIO read must return actual read amount"
7340
7341 test_119c() # bug 13099
7342 {
7343         BSIZE=1048576
7344         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7345         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7346         rm -f $DIR/$tfile
7347 }
7348 run_test 119c "Testing for direct read hitting hole"
7349
7350 test_119d() # bug 15950
7351 {
7352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7353         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7354         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7355         BSIZE=1048576
7356         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7357         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7358         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7359         lctl set_param fail_loc=0x40d
7360         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7361         pid_dio=$!
7362         sleep 1
7363         cat $DIR/$tfile > /dev/null &
7364         lctl set_param fail_loc=0
7365         pid_reads=$!
7366         wait $pid_dio
7367         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7368         sleep 2
7369         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7370         error "the read rpcs have not completed in 2s"
7371         rm -f $DIR/$tfile
7372         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7373 }
7374 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7375
7376 test_120a() {
7377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7378         test_mkdir -p $DIR/$tdir
7379         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7380                skip "no early lock cancel on server" && return 0
7381
7382         lru_resize_disable mdc
7383         lru_resize_disable osc
7384         cancel_lru_locks mdc
7385         # asynchronous object destroy at MDT could cause bl ast to client
7386         cancel_lru_locks osc
7387
7388         stat $DIR/$tdir > /dev/null
7389         can1=$(lctl get_param -n ldlm.services.ldlm_canceld.stats |
7390                                         awk '/ldlm_cancel/ {print $2}')
7391         blk1=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
7392                                         awk '/ldlm_bl_callback/ {print $2}')
7393         test_mkdir -c1 $DIR/$tdir/d1
7394         can2=$(lctl get_param -n ldlm.services.ldlm_canceld.stats |
7395                                         awk '/ldlm_cancel/ {print $2}')
7396         blk2=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
7397                                         awk '/ldlm_bl_callback/ {print $2}')
7398         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7399         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7400         lru_resize_enable mdc
7401         lru_resize_enable osc
7402 }
7403 run_test 120a "Early Lock Cancel: mkdir test"
7404
7405 test_120b() {
7406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7407         test_mkdir $DIR/$tdir
7408         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7409                skip "no early lock cancel on server" && return 0
7410         lru_resize_disable mdc
7411         lru_resize_disable osc
7412         cancel_lru_locks mdc
7413         stat $DIR/$tdir > /dev/null
7414         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7415         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7416         touch $DIR/$tdir/f1
7417         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7418         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7419         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7420         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7421         lru_resize_enable mdc
7422         lru_resize_enable osc
7423 }
7424 run_test 120b "Early Lock Cancel: create test"
7425
7426 test_120c() {
7427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7428         test_mkdir -c1 $DIR/$tdir
7429         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7430                skip "no early lock cancel on server" && return 0
7431         lru_resize_disable mdc
7432         lru_resize_disable osc
7433         test_mkdir -p -c1 $DIR/$tdir/d1
7434         test_mkdir -p -c1 $DIR/$tdir/d2
7435         touch $DIR/$tdir/d1/f1
7436         cancel_lru_locks mdc
7437         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7438         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7439         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7440         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7441         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7442         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7443         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7444         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7445         lru_resize_enable mdc
7446         lru_resize_enable osc
7447 }
7448 run_test 120c "Early Lock Cancel: link test"
7449
7450 test_120d() {
7451         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7452         test_mkdir -p -c1 $DIR/$tdir
7453         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7454                skip "no early lock cancel on server" && return 0
7455         lru_resize_disable mdc
7456         lru_resize_disable osc
7457         touch $DIR/$tdir
7458         cancel_lru_locks mdc
7459         stat $DIR/$tdir > /dev/null
7460         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7461         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7462         chmod a+x $DIR/$tdir
7463         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7464         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7465         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7466         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7467         lru_resize_enable mdc
7468         lru_resize_enable osc
7469 }
7470 run_test 120d "Early Lock Cancel: setattr test"
7471
7472 test_120e() {
7473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7474         test_mkdir -p -c1 $DIR/$tdir
7475         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7476                skip "no early lock cancel on server" && return 0
7477         lru_resize_disable mdc
7478         lru_resize_disable osc
7479         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7480         cancel_lru_locks mdc
7481         cancel_lru_locks osc
7482         dd if=$DIR/$tdir/f1 of=/dev/null
7483         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7484         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7485               awk '/ldlm_cancel/ {print $2}'`
7486         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7487               awk '/ldlm_bl_callback/ {print $2}'`
7488         unlink $DIR/$tdir/f1
7489         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7490               awk '/ldlm_cancel/ {print $2}'`
7491         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7492               awk '/ldlm_bl_callback/ {print $2}'`
7493         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7494         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7495         lru_resize_enable mdc
7496         lru_resize_enable osc
7497 }
7498 run_test 120e "Early Lock Cancel: unlink test"
7499
7500 test_120f() {
7501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7502         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7503                skip "no early lock cancel on server" && return 0
7504         test_mkdir -p -c1 $DIR/$tdir
7505         lru_resize_disable mdc
7506         lru_resize_disable osc
7507         test_mkdir -p -c1 $DIR/$tdir/d1
7508         test_mkdir -p -c1 $DIR/$tdir/d2
7509         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7510         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7511         cancel_lru_locks mdc
7512         cancel_lru_locks osc
7513         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7514         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7515         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7516         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7517               awk '/ldlm_cancel/ {print $2}'`
7518         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7519               awk '/ldlm_bl_callback/ {print $2}'`
7520         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7521         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7522               awk '/ldlm_cancel/ {print $2}'`
7523         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7524               awk '/ldlm_bl_callback/ {print $2}'`
7525         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7526         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7527         lru_resize_enable mdc
7528         lru_resize_enable osc
7529 }
7530 run_test 120f "Early Lock Cancel: rename test"
7531
7532 test_120g() {
7533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7534         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7535                skip "no early lock cancel on server" && return 0
7536         lru_resize_disable mdc
7537         lru_resize_disable osc
7538         count=10000
7539         echo create $count files
7540         test_mkdir -p $DIR/$tdir
7541         cancel_lru_locks mdc
7542         cancel_lru_locks osc
7543         t0=`date +%s`
7544
7545         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7546               awk '/ldlm_cancel/ {print $2}'`
7547         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7548               awk '/ldlm_bl_callback/ {print $2}'`
7549         createmany -o $DIR/$tdir/f $count
7550         sync
7551         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7552               awk '/ldlm_cancel/ {print $2}'`
7553         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7554               awk '/ldlm_bl_callback/ {print $2}'`
7555         t1=`date +%s`
7556         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7557         echo rm $count files
7558         rm -r $DIR/$tdir
7559         sync
7560         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7561               awk '/ldlm_cancel/ {print $2}'`
7562         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7563               awk '/ldlm_bl_callback/ {print $2}'`
7564         t2=`date +%s`
7565         echo total: $count removes in $((t2-t1))
7566         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7567         sleep 2
7568         # wait for commitment of removal
7569         lru_resize_enable mdc
7570         lru_resize_enable osc
7571 }
7572 run_test 120g "Early Lock Cancel: performance test"
7573
7574 test_121() { #bug #10589
7575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7576         rm -rf $DIR/$tfile
7577         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7578 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7579         lctl set_param fail_loc=0x310
7580         cancel_lru_locks osc > /dev/null
7581         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7582         lctl set_param fail_loc=0
7583         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7584 }
7585 run_test 121 "read cancel race ========="
7586
7587 test_123a() { # was test 123, statahead(bug 11401)
7588         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7589         SLOWOK=0
7590         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7591             log "testing on UP system. Performance may be not as good as expected."
7592                         SLOWOK=1
7593         fi
7594
7595         rm -rf $DIR/$tdir
7596         test_mkdir -p $DIR/$tdir
7597         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7598         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7599         MULT=10
7600         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7601                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7602
7603                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7604                 lctl set_param -n llite.*.statahead_max 0
7605                 lctl get_param llite.*.statahead_max
7606                 cancel_lru_locks mdc
7607                 cancel_lru_locks osc
7608                 stime=`date +%s`
7609                 time ls -l $DIR/$tdir | wc -l
7610                 etime=`date +%s`
7611                 delta=$((etime - stime))
7612                 log "ls $i files without statahead: $delta sec"
7613                 lctl set_param llite.*.statahead_max=$max
7614
7615                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7616                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7617                 cancel_lru_locks mdc
7618                 cancel_lru_locks osc
7619                 stime=`date +%s`
7620                 time ls -l $DIR/$tdir | wc -l
7621                 etime=`date +%s`
7622                 delta_sa=$((etime - stime))
7623                 log "ls $i files with statahead: $delta_sa sec"
7624                 lctl get_param -n llite.*.statahead_stats
7625                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7626
7627                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7628                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7629
7630                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7631                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7632                     lctl set_param -n llite.*.statahead_max 0
7633                     lctl get_param llite.*.statahead_max
7634                     cancel_lru_locks mdc
7635                     cancel_lru_locks osc
7636                     stime=`date +%s`
7637                     time ls -l $DIR/$tdir | wc -l
7638                     etime=`date +%s`
7639                     delta=$((etime - stime))
7640                     log "ls $i files again without statahead: $delta sec"
7641                     lctl set_param llite.*.statahead_max=$max
7642                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7643                         if [  $SLOWOK -eq 0 ]; then
7644                                 error "ls $i files is slower with statahead!"
7645                         else
7646                                 log "ls $i files is slower with statahead!"
7647                         fi
7648                         break
7649                     fi
7650                 fi
7651
7652                 [ $delta -gt 20 ] && break
7653                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7654                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7655         done
7656         log "ls done"
7657
7658         stime=`date +%s`
7659         rm -r $DIR/$tdir
7660         sync
7661         etime=`date +%s`
7662         delta=$((etime - stime))
7663         log "rm -r $DIR/$tdir/: $delta seconds"
7664         log "rm done"
7665         lctl get_param -n llite.*.statahead_stats
7666 }
7667 run_test 123a "verify statahead work"
7668
7669 test_123b () { # statahead(bug 15027)
7670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7671         test_mkdir -p $DIR/$tdir
7672         createmany -o $DIR/$tdir/$tfile-%d 1000
7673
7674         cancel_lru_locks mdc
7675         cancel_lru_locks osc
7676
7677 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7678         lctl set_param fail_loc=0x80000803
7679         ls -lR $DIR/$tdir > /dev/null
7680         log "ls done"
7681         lctl set_param fail_loc=0x0
7682         lctl get_param -n llite.*.statahead_stats
7683         rm -r $DIR/$tdir
7684         sync
7685
7686 }
7687 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7688
7689 test_124a() {
7690         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7691         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7692                skip "no lru resize on server" && return 0
7693         local NR=2000
7694         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7695
7696         log "create $NR files at $DIR/$tdir"
7697         createmany -o $DIR/$tdir/f $NR ||
7698                 error "failed to create $NR files in $DIR/$tdir"
7699
7700         cancel_lru_locks mdc
7701         ls -l $DIR/$tdir > /dev/null
7702
7703         local NSDIR=""
7704         local LRU_SIZE=0
7705         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7706                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7707                 LRU_SIZE=$(lctl get_param -n $PARAM)
7708                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7709                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7710                                                 log "NSDIR=$NSDIR"
7711                         log "NS=$(basename $NSDIR)"
7712                         break
7713                 fi
7714         done
7715
7716         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7717                 skip "Not enough cached locks created!"
7718                 return 0
7719         fi
7720         log "LRU=$LRU_SIZE"
7721
7722         local SLEEP=30
7723
7724         # We know that lru resize allows one client to hold $LIMIT locks
7725         # for 10h. After that locks begin to be killed by client.
7726         local MAX_HRS=10
7727         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7728                 log "LIMIT=$LIMIT"
7729
7730         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7731         # killing locks. Some time was spent for creating locks. This means
7732         # that up to the moment of sleep finish we must have killed some of
7733         # them (10-100 locks). This depends on how fast ther were created.
7734         # Many of them were touched in almost the same moment and thus will
7735         # be killed in groups.
7736         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7737
7738         # Use $LRU_SIZE_B here to take into account real number of locks
7739         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7740         local LRU_SIZE_B=$LRU_SIZE
7741         log "LVF=$LVF"
7742         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7743                 log "OLD_LVF=$OLD_LVF"
7744         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7745
7746         # Let's make sure that we really have some margin. Client checks
7747         # cached locks every 10 sec.
7748         SLEEP=$((SLEEP+20))
7749         log "Sleep ${SLEEP} sec"
7750         local SEC=0
7751         while ((SEC<$SLEEP)); do
7752                 echo -n "..."
7753                 sleep 5
7754                 SEC=$((SEC+5))
7755                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7756                 echo -n "$LRU_SIZE"
7757         done
7758         echo ""
7759         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7760         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7761
7762         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7763                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7764                 unlinkmany $DIR/$tdir/f $NR
7765                 return
7766         }
7767
7768         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7769         log "unlink $NR files at $DIR/$tdir"
7770         unlinkmany $DIR/$tdir/f $NR
7771 }
7772 run_test 124a "lru resize ======================================="
7773
7774 get_max_pool_limit()
7775 {
7776         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7777         local max=0
7778         for l in $limit; do
7779                 if test $l -gt $max; then
7780                         max=$l
7781                 fi
7782         done
7783         echo $max
7784 }
7785
7786 test_124b() {
7787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7788         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7789                skip "no lru resize on server" && return 0
7790
7791         LIMIT=`get_max_pool_limit`
7792
7793         NR=$(($(default_lru_size)*20))
7794         if [ $NR -gt $LIMIT ]; then
7795                 log "Limit lock number by $LIMIT locks"
7796                 NR=$LIMIT
7797         fi
7798         lru_resize_disable mdc
7799         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7800                 error "failed to create $DIR/$tdir/disable_lru_resize"
7801
7802         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7803         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7804         cancel_lru_locks mdc
7805         stime=`date +%s`
7806         PID=""
7807         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7808         PID="$PID $!"
7809         sleep 2
7810         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7811         PID="$PID $!"
7812         sleep 2
7813         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7814         PID="$PID $!"
7815         wait $PID
7816         etime=`date +%s`
7817         nolruresize_delta=$((etime-stime))
7818         log "ls -la time: $nolruresize_delta seconds"
7819         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7820         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7821
7822         lru_resize_enable mdc
7823         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7824                 error "failed to create $DIR/$tdir/enable_lru_resize"
7825
7826         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7827         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7828         cancel_lru_locks mdc
7829         stime=`date +%s`
7830         PID=""
7831         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7832         PID="$PID $!"
7833         sleep 2
7834         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7835         PID="$PID $!"
7836         sleep 2
7837         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7838         PID="$PID $!"
7839         wait $PID
7840         etime=`date +%s`
7841         lruresize_delta=$((etime-stime))
7842         log "ls -la time: $lruresize_delta seconds"
7843         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7844
7845         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7846                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7847         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7848                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7849         else
7850                 log "lru resize performs the same with no lru resize"
7851         fi
7852         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7853 }
7854 run_test 124b "lru resize (performance test) ======================="
7855
7856 test_125() { # 13358
7857         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7858         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7859         test_mkdir -p $DIR/d125 || error "mkdir failed"
7860         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7861         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7862         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7863 }
7864 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7865
7866 test_126() { # bug 12829/13455
7867         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7868         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7869         $GSS && skip "must run as gss disabled" && return
7870
7871         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7872         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7873         rm -f $DIR/$tfile
7874         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7875 }
7876 run_test 126 "check that the fsgid provided by the client is taken into account"
7877
7878 test_127a() { # bug 15521
7879         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7880         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7881         $LCTL set_param osc.*.stats=0
7882         FSIZE=$((2048 * 1024))
7883         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7884         cancel_lru_locks osc
7885         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7886
7887         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7888         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7889                 echo "got $COUNT $NAME"
7890                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7891                 eval $NAME=$COUNT || error "Wrong proc format"
7892
7893                 case $NAME in
7894                         read_bytes|write_bytes)
7895                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7896                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7897                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7898                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7899                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7900                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7901                                 error "sumsquare is too small: $SUMSQ"
7902                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7903                                 error "sumsquare is too big: $SUMSQ"
7904                         ;;
7905                         *) ;;
7906                 esac
7907         done < $DIR/${tfile}.tmp
7908
7909         #check that we actually got some stats
7910         [ "$read_bytes" ] || error "Missing read_bytes stats"
7911         [ "$write_bytes" ] || error "Missing write_bytes stats"
7912         [ "$read_bytes" != 0 ] || error "no read done"
7913         [ "$write_bytes" != 0 ] || error "no write done"
7914 }
7915 run_test 127a "verify the client stats are sane"
7916
7917 test_127b() { # bug LU-333
7918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7919         $LCTL set_param llite.*.stats=0
7920         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7921         # perform 2 reads and writes so MAX is different from SUM.
7922         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7923         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7924         cancel_lru_locks osc
7925         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7926         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7927
7928         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7929         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7930                 echo "got $COUNT $NAME"
7931                 eval $NAME=$COUNT || error "Wrong proc format"
7932
7933         case $NAME in
7934                 read_bytes)
7935                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7936                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7937                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7938                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7939                         ;;
7940                 write_bytes)
7941                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7942                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7943                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7944                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7945                         ;;
7946                         *) ;;
7947                 esac
7948         done < $TMP/${tfile}.tmp
7949
7950         #check that we actually got some stats
7951         [ "$read_bytes" ] || error "Missing read_bytes stats"
7952         [ "$write_bytes" ] || error "Missing write_bytes stats"
7953         [ "$read_bytes" != 0 ] || error "no read done"
7954         [ "$write_bytes" != 0 ] || error "no write done"
7955 }
7956 run_test 127b "verify the llite client stats are sane"
7957
7958 test_128() { # bug 15212
7959         touch $DIR/$tfile
7960         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7961                 find $DIR/$tfile
7962                 find $DIR/$tfile
7963         EOF
7964
7965         result=$(grep error $TMP/$tfile.log)
7966         rm -f $DIR/$tfile
7967         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7968 }
7969 run_test 128 "interactive lfs for 2 consecutive find's"
7970
7971 set_dir_limits () {
7972         local mntdev
7973         local canondev
7974         local node
7975
7976         local LDPROC=/proc/fs/ldiskfs
7977         local facets=$(get_facets MDS)
7978
7979         for facet in ${facets//,/ }; do
7980                 canondev=$(ldiskfs_canon \
7981                            *.$(convert_facet2label $facet).mntdev $facet)
7982                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7983                                                 LDPROC=/sys/fs/ldiskfs
7984                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7985         done
7986 }
7987
7988 test_129() {
7989         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7990         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7991                 skip "Only applicable to ldiskfs-based MDTs"
7992                 return
7993         fi
7994         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7995         ENOSPC=28
7996         EFBIG=27
7997
7998         rm -rf $DIR/$tdir
7999         test_mkdir -p $DIR/$tdir
8000
8001         # block size of mds1
8002         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8003         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8004         local MAX=$((MDSBLOCKSIZE * 3))
8005         set_dir_limits $MAX
8006         local I=$(stat -c%s "$DIR/$tdir")
8007         local J=0
8008         local STRIPE_COUNT=1
8009         [ $MDSCOUNT -ge 2 ] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8010         MAX=$((MAX*STRIPE_COUNT))
8011         while [ ! $I -gt $MAX ]; do
8012                 $MULTIOP $DIR/$tdir/$J Oc
8013                 rc=$?
8014                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8015                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8016                 #and EFBIG for previous versions
8017                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8018                         set_dir_limits 0
8019                         echo "return code $rc received as expected"
8020                         multiop $DIR/$tdir/$J Oc ||
8021                                 error_exit "multiop failed w/o dir size limit"
8022
8023                         I=$(stat -c%s "$DIR/$tdir")
8024
8025                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8026                                         $(version_code 2.4.51) ]
8027                         then
8028                                 [ $I -eq $MAX ] && return 0
8029                         else
8030                                 [ $I -gt $MAX ] && return 0
8031                         fi
8032                         error_exit "current dir size $I, previous limit $MAX"
8033                 elif [ $rc -ne 0 ]; then
8034                         set_dir_limits 0
8035                         error_exit "return code $rc received instead of expected " \
8036                                    "$EFBIG or $ENOSPC, files in dir $I"
8037                 fi
8038                 J=$((J+1))
8039                 I=$(stat -c%s "$DIR/$tdir")
8040         done
8041
8042         set_dir_limits 0
8043         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8044 }
8045 run_test 129 "test directory size limit ========================"
8046
8047 OLDIFS="$IFS"
8048 cleanup_130() {
8049         trap 0
8050         IFS="$OLDIFS"
8051 }
8052
8053 test_130a() {
8054         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8055         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8056                 return
8057
8058         trap cleanup_130 EXIT RETURN
8059
8060         local fm_file=$DIR/$tfile
8061         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8062         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8063                 error "dd failed for $fm_file"
8064
8065         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8066         filefrag -ves $fm_file
8067         RC=$?
8068         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8069                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8070         [ $RC != 0 ] && error "filefrag $fm_file failed"
8071
8072         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8073                       grep -v "ext:" | grep -v "found")
8074         lun=$($GETSTRIPE -i $fm_file)
8075
8076         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8077         IFS=$'\n'
8078         tot_len=0
8079         for line in $filefrag_op
8080         do
8081                 frag_lun=`echo $line | cut -d: -f5`
8082                 ext_len=`echo $line | cut -d: -f4`
8083                 if (( $frag_lun != $lun )); then
8084                         cleanup_130
8085                         error "FIEMAP on 1-stripe file($fm_file) failed"
8086                         return
8087                 fi
8088                 (( tot_len += ext_len ))
8089         done
8090
8091         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8092                 cleanup_130
8093                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8094                 return
8095         fi
8096
8097         cleanup_130
8098
8099         echo "FIEMAP on single striped file succeeded"
8100 }
8101 run_test 130a "FIEMAP (1-stripe file)"
8102
8103 test_130b() {
8104         [ "$OSTCOUNT" -lt "2" ] &&
8105                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8106
8107         [ "$OSTCOUNT" -ge "10" ] &&
8108                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8109
8110         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8111         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8112                 return
8113
8114         trap cleanup_130 EXIT RETURN
8115
8116         local fm_file=$DIR/$tfile
8117         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8118         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8119                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8120
8121         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8122                 error "dd failed on $fm_file"
8123
8124         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8125         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8126                       grep -v "ext:" | grep -v "found")
8127
8128         last_lun=$(echo $filefrag_op | cut -d: -f5)
8129
8130         IFS=$'\n'
8131         tot_len=0
8132         num_luns=1
8133         for line in $filefrag_op
8134         do
8135                 frag_lun=`echo $line | cut -d: -f5`
8136                 ext_len=`echo $line | cut -d: -f4`
8137                 if (( $frag_lun != $last_lun )); then
8138                         if (( tot_len != 1024 )); then
8139                                 cleanup_130
8140                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8141                                 return
8142                         else
8143                                 (( num_luns += 1 ))
8144                                 tot_len=0
8145                         fi
8146                 fi
8147                 (( tot_len += ext_len ))
8148                 last_lun=$frag_lun
8149         done
8150         if (( num_luns != 2 || tot_len != 1024 )); then
8151                 cleanup_130
8152                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8153                 return
8154         fi
8155
8156         cleanup_130
8157
8158         echo "FIEMAP on 2-stripe file succeeded"
8159 }
8160 run_test 130b "FIEMAP (2-stripe file)"
8161
8162 test_130c() {
8163         [ "$OSTCOUNT" -lt "2" ] &&
8164                 skip_env "skipping FIEMAP on 2-stripe file" && return
8165
8166         [ "$OSTCOUNT" -ge "10" ] &&
8167                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8168
8169         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8170         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8171                 return
8172
8173         trap cleanup_130 EXIT RETURN
8174
8175         local fm_file=$DIR/$tfile
8176         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8177         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8178                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8179
8180         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8181
8182         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8183         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8184
8185         last_lun=`echo $filefrag_op | cut -d: -f5`
8186
8187         IFS=$'\n'
8188         tot_len=0
8189         num_luns=1
8190         for line in $filefrag_op
8191         do
8192                 frag_lun=`echo $line | cut -d: -f5`
8193                 ext_len=`echo $line | cut -d: -f4`
8194                 if (( $frag_lun != $last_lun )); then
8195                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8196                         if (( logical != 512 )); then
8197                                 cleanup_130
8198                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8199                                 return
8200                         fi
8201                         if (( tot_len != 512 )); then
8202                                 cleanup_130
8203                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8204                                 return
8205                         else
8206                                 (( num_luns += 1 ))
8207                                 tot_len=0
8208                         fi
8209                 fi
8210                 (( tot_len += ext_len ))
8211                 last_lun=$frag_lun
8212         done
8213         if (( num_luns != 2 || tot_len != 512 )); then
8214                 cleanup_130
8215                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8216                 return
8217         fi
8218
8219         cleanup_130
8220
8221         echo "FIEMAP on 2-stripe file with hole succeeded"
8222 }
8223 run_test 130c "FIEMAP (2-stripe file with hole)"
8224
8225 test_130d() {
8226         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8227
8228         [ "$OSTCOUNT" -ge "10" ] &&
8229                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8230
8231         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8232         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8233
8234         trap cleanup_130 EXIT RETURN
8235
8236         local fm_file=$DIR/$tfile
8237         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8238         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8239                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8240
8241         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8242         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8243                 error "dd failed on $fm_file"
8244
8245         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8246         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8247                 grep -v "ext:" | grep -v "found"`
8248
8249         last_lun=`echo $filefrag_op | cut -d: -f5`
8250
8251         IFS=$'\n'
8252         tot_len=0
8253         num_luns=1
8254         for line in $filefrag_op
8255         do
8256                 frag_lun=`echo $line | cut -d: -f5`
8257                 ext_len=`echo $line | cut -d: -f4`
8258                 if (( $frag_lun != $last_lun )); then
8259                         if (( tot_len != 1024 )); then
8260                                 cleanup_130
8261                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8262                                 return
8263                         else
8264                                 (( num_luns += 1 ))
8265                                 tot_len=0
8266                         fi
8267                 fi
8268                 (( tot_len += ext_len ))
8269                 last_lun=$frag_lun
8270         done
8271         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8272                 cleanup_130
8273                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8274                 return
8275         fi
8276
8277         cleanup_130
8278
8279         echo "FIEMAP on N-stripe file succeeded"
8280 }
8281 run_test 130d "FIEMAP (N-stripe file)"
8282
8283 test_130e() {
8284         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8285
8286         [ "$OSTCOUNT" -ge "10" ] &&
8287                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8288
8289         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8290         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8291
8292         trap cleanup_130 EXIT RETURN
8293
8294         local fm_file=$DIR/$tfile
8295         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8296         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8297                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8298
8299         NUM_BLKS=512
8300         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8301         for ((i = 0; i < $NUM_BLKS; i++))
8302         do
8303                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8304         done
8305
8306         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8307         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8308
8309         last_lun=`echo $filefrag_op | cut -d: -f5`
8310
8311         IFS=$'\n'
8312         tot_len=0
8313         num_luns=1
8314         for line in $filefrag_op
8315         do
8316                 frag_lun=`echo $line | cut -d: -f5`
8317                 ext_len=`echo $line | cut -d: -f4`
8318                 if (( $frag_lun != $last_lun )); then
8319                         if (( tot_len != $EXPECTED_LEN )); then
8320                                 cleanup_130
8321                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8322                                 return
8323                         else
8324                                 (( num_luns += 1 ))
8325                                 tot_len=0
8326                         fi
8327                 fi
8328                 (( tot_len += ext_len ))
8329                 last_lun=$frag_lun
8330         done
8331         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8332                 cleanup_130
8333                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8334                 return
8335         fi
8336
8337         cleanup_130
8338
8339         echo "FIEMAP with continuation calls succeeded"
8340 }
8341 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8342
8343 # Test for writev/readv
8344 test_131a() {
8345         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8346         error "writev test failed"
8347         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8348         error "readv failed"
8349         rm -f $DIR/$tfile
8350 }
8351 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8352
8353 test_131b() {
8354         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8355         error "append writev test failed"
8356         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8357         error "append writev test failed"
8358         rm -f $DIR/$tfile
8359 }
8360 run_test 131b "test append writev"
8361
8362 test_131c() {
8363         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8364         error "NOT PASS"
8365 }
8366 run_test 131c "test read/write on file w/o objects"
8367
8368 test_131d() {
8369         rwv -f $DIR/$tfile -w -n 1 1572864
8370         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8371         if [ "$NOB" != 1572864 ]; then
8372                 error "Short read filed: read $NOB bytes instead of 1572864"
8373         fi
8374         rm -f $DIR/$tfile
8375 }
8376 run_test 131d "test short read"
8377
8378 test_131e() {
8379         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8380         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8381         error "read hitting hole failed"
8382         rm -f $DIR/$tfile
8383 }
8384 run_test 131e "test read hitting hole"
8385
8386 get_ost_param() {
8387         local token=$1
8388         local gl_sum=0
8389         for node in $(osts_nodes); do
8390                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8391                 [ x$gl = x"" ] && gl=0
8392                 gl_sum=$((gl_sum + gl))
8393         done
8394         echo $gl_sum
8395 }
8396
8397 som_mode_switch() {
8398         local som=$1
8399         local gl1=$2
8400         local gl2=$3
8401
8402         if [ x$som = x"enabled" ]; then
8403                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8404                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8405                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8406         else
8407                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8408                 MOUNTOPT="$MOUNTOPT,som_preview"
8409                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8410         fi
8411
8412         # do remount to make new mount-conf parameters actual
8413         echo remounting...
8414         sync
8415         stopall
8416         setupall
8417 }
8418
8419 test_132() { #1028, SOM
8420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8421         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8422         local num=$(get_mds_dir $DIR)
8423         local mymds=mds${num}
8424         local MOUNTOPT_SAVE=$MOUNTOPT
8425
8426         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8427         cancel_lru_locks osc
8428
8429         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8430
8431         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8432         stat $DIR/$tfile >/dev/null
8433         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8434         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8435         rm $DIR/$tfile
8436         som_mode_switch $som1 $gl1 $gl2
8437
8438         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8439         cancel_lru_locks osc
8440
8441         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8442         if [ $som1 == $som2 ]; then
8443             error "som is still "$som2
8444             if [ x$som2 = x"enabled" ]; then
8445                 som2="disabled"
8446             else
8447                 som2="enabled"
8448             fi
8449         fi
8450
8451         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8452         stat $DIR/$tfile >/dev/null
8453         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8454         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8455         som_mode_switch $som2 $gl1 $gl2
8456         MOUNTOPT=$MOUNTOPT_SAVE
8457 }
8458 run_test 132 "som avoids glimpse rpc"
8459
8460 check_stats() {
8461         local res
8462         local count
8463         case $1 in
8464         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8465                  ;;
8466         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8467                  ;;
8468         *) error "Wrong argument $1" ;;
8469         esac
8470         echo $res
8471         count=`echo $res | awk '{print $2}'`
8472         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8473         # if the argument $3 is zero, it means any stat increment is ok.
8474         if [ $3 -gt 0 ] ; then
8475                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
8476         fi
8477 }
8478
8479 test_133a() {
8480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8481         remote_ost_nodsh && skip "remote OST with nodsh" && return
8482         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8483
8484         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8485                 { skip "MDS doesn't support rename stats"; return; }
8486         local testdir=$DIR/${tdir}/stats_testdir
8487         mkdir -p $DIR/${tdir}
8488
8489         # clear stats.
8490         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8491         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8492
8493         # verify mdt stats first.
8494         mkdir ${testdir} || error "mkdir failed"
8495         check_stats $SINGLEMDS "mkdir" 1
8496         touch ${testdir}/${tfile} || "touch failed"
8497         check_stats $SINGLEMDS "open" 1
8498         check_stats $SINGLEMDS "close" 1
8499         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8500         check_stats $SINGLEMDS "mknod" 1
8501         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8502         check_stats $SINGLEMDS "unlink" 1
8503         rm -f ${testdir}/${tfile} || error "file remove failed"
8504         check_stats $SINGLEMDS "unlink" 2
8505
8506         # remove working dir and check mdt stats again.
8507         rmdir ${testdir} || error "rmdir failed"
8508         check_stats $SINGLEMDS "rmdir" 1
8509
8510         local testdir1=$DIR/${tdir}/stats_testdir1
8511         mkdir -p ${testdir}
8512         mkdir -p ${testdir1}
8513         touch ${testdir1}/test1
8514         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8515         check_stats $SINGLEMDS "crossdir_rename" 1
8516
8517         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8518         check_stats $SINGLEMDS "samedir_rename" 1
8519
8520         rm -rf $DIR/${tdir}
8521 }
8522 run_test 133a "Verifying MDT stats ========================================"
8523
8524 test_133b() {
8525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8526         remote_ost_nodsh && skip "remote OST with nodsh" && return
8527         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8528         local testdir=$DIR/${tdir}/stats_testdir
8529         mkdir -p ${testdir} || error "mkdir failed"
8530         touch ${testdir}/${tfile} || "touch failed"
8531         cancel_lru_locks mdc
8532
8533         # clear stats.
8534         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8535         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8536
8537         # extra mdt stats verification.
8538         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8539         check_stats $SINGLEMDS "setattr" 1
8540         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8541         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8542         then            # LU-1740
8543                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8544                 check_stats $SINGLEMDS "getattr" 1
8545         fi
8546         $LFS df || error "lfs failed"
8547         check_stats $SINGLEMDS "statfs" 1
8548
8549         rm -rf $DIR/${tdir}
8550 }
8551 run_test 133b "Verifying extra MDT stats =================================="
8552
8553 test_133c() {
8554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8555         remote_ost_nodsh && skip "remote OST with nodsh" && return
8556         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8557         local testdir=$DIR/${tdir}/stats_testdir
8558         test_mkdir -p ${testdir} || error "mkdir failed"
8559
8560         # verify obdfilter stats.
8561         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8562         sync
8563         cancel_lru_locks osc
8564         wait_delete_completed
8565
8566         # clear stats.
8567         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8568         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8569
8570         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8571         sync
8572         cancel_lru_locks osc
8573         check_stats ost "write" 1
8574
8575         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8576         check_stats ost "read" 1
8577
8578         > ${testdir}/${tfile} || error "truncate failed"
8579         check_stats ost "punch" 1
8580
8581         rm -f ${testdir}/${tfile} || error "file remove failed"
8582         wait_delete_completed
8583         check_stats ost "destroy" 1
8584
8585         rm -rf $DIR/${tdir}
8586 }
8587 run_test 133c "Verifying OST stats ========================================"
8588
8589 order_2() {
8590     local value=$1
8591     local orig=$value
8592     local order=1
8593
8594     while [ $value -ge 2 ]; do
8595         order=$((order*2))
8596         value=$((value/2))
8597     done
8598
8599     if [ $orig -gt $order ]; then
8600         order=$((order*2))
8601     fi
8602     echo $order
8603 }
8604
8605 size_in_KMGT() {
8606     local value=$1
8607     local size=('K' 'M' 'G' 'T');
8608     local i=0
8609     local size_string=$value
8610
8611     while [ $value -ge 1024 ]; do
8612         if [ $i -gt 3 ]; then
8613             #T is the biggest unit we get here, if that is bigger,
8614             #just return XXXT
8615             size_string=${value}T
8616             break
8617         fi
8618         value=$((value >> 10))
8619         if [ $value -lt 1024 ]; then
8620             size_string=${value}${size[$i]}
8621             break
8622         fi
8623         i=$((i + 1))
8624     done
8625
8626     echo $size_string
8627 }
8628
8629 get_rename_size() {
8630     local size=$1
8631     local context=${2:-.}
8632     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8633                 grep -A1 $context |
8634                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8635     echo $sample
8636 }
8637
8638 test_133d() {
8639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8640         remote_ost_nodsh && skip "remote OST with nodsh" && return
8641         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8642         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8643         { skip "MDS doesn't support rename stats"; return; }
8644
8645         local testdir1=$DIR/${tdir}/stats_testdir1
8646         local testdir2=$DIR/${tdir}/stats_testdir2
8647
8648         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8649
8650         mkdir -p ${testdir1} || error "mkdir failed"
8651         mkdir -p ${testdir2} || error "mkdir failed"
8652
8653         createmany -o $testdir1/test 512 || error "createmany failed"
8654
8655         # check samedir rename size
8656         mv ${testdir1}/test0 ${testdir1}/test_0
8657
8658         local testdir1_size=$(ls -l $DIR/${tdir} |
8659                 awk '/stats_testdir1/ {print $5}')
8660         local testdir2_size=$(ls -l $DIR/${tdir} |
8661                 awk '/stats_testdir2/ {print $5}')
8662
8663         testdir1_size=$(order_2 $testdir1_size)
8664         testdir2_size=$(order_2 $testdir2_size)
8665
8666         testdir1_size=$(size_in_KMGT $testdir1_size)
8667         testdir2_size=$(size_in_KMGT $testdir2_size)
8668
8669         echo "source rename dir size: ${testdir1_size}"
8670         echo "target rename dir size: ${testdir2_size}"
8671
8672         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8673         eval $cmd || error "$cmd failed"
8674         local samedir=$($cmd | grep 'same_dir')
8675         local same_sample=$(get_rename_size $testdir1_size)
8676         [ -z "$samedir" ] && error "samedir_rename_size count error"
8677         [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8678         echo "Check same dir rename stats success"
8679
8680         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8681
8682         # check crossdir rename size
8683         mv ${testdir1}/test_0 ${testdir2}/test_0
8684
8685         testdir1_size=$(ls -l $DIR/${tdir} |
8686                 awk '/stats_testdir1/ {print $5}')
8687         testdir2_size=$(ls -l $DIR/${tdir} |
8688                 awk '/stats_testdir2/ {print $5}')
8689
8690         testdir1_size=$(order_2 $testdir1_size)
8691         testdir2_size=$(order_2 $testdir2_size)
8692
8693         testdir1_size=$(size_in_KMGT $testdir1_size)
8694         testdir2_size=$(size_in_KMGT $testdir2_size)
8695
8696         echo "source rename dir size: ${testdir1_size}"
8697         echo "target rename dir size: ${testdir2_size}"
8698
8699         eval $cmd || error "$cmd failed"
8700         local crossdir=$($cmd | grep 'crossdir')
8701         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8702         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8703         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8704         [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8705         [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8706         echo "Check cross dir rename stats success"
8707         rm -rf $DIR/${tdir}
8708 }
8709 run_test 133d "Verifying rename_stats ========================================"
8710
8711 test_133e() {
8712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8713         local testdir=$DIR/${tdir}/stats_testdir
8714         local ctr f0 f1 bs=32768 count=42 sum
8715
8716         remote_ost_nodsh && skip "remote OST with nodsh" && return
8717         mkdir -p ${testdir} || error "mkdir failed"
8718
8719         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8720
8721         for ctr in {write,read}_bytes; do
8722                 sync
8723                 cancel_lru_locks osc
8724
8725                 do_facet ost1 $LCTL set_param -n \
8726                         "obdfilter.*.exports.clear=clear"
8727
8728                 if [ $ctr = write_bytes ]; then
8729                         f0=/dev/zero
8730                         f1=${testdir}/${tfile}
8731                 else
8732                         f0=${testdir}/${tfile}
8733                         f1=/dev/null
8734                 fi
8735
8736                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8737                         error "dd failed"
8738                 sync
8739                 cancel_lru_locks osc
8740
8741                 sum=$(do_facet ost1 $LCTL get_param \
8742                                 "obdfilter.*.exports.*.stats" | \
8743                           awk -v ctr=$ctr '\
8744                                 BEGIN { sum = 0 }
8745                                 $1 == ctr { sum += $7 }
8746                                 END { print sum }')
8747
8748                 if ((sum != bs * count)); then
8749                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8750                 fi
8751         done
8752
8753         rm -rf $DIR/${tdir}
8754 }
8755 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8756
8757 test_133f() {
8758         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8759         local facet
8760
8761         # First without trusting modes.
8762         find $proc_dirs \
8763                 -exec cat '{}' \; &> /dev/null
8764
8765         # Second verifying readability.
8766         find $proc_dirs \
8767                 -type f \
8768                 -readable \
8769                 -exec cat '{}' \; > /dev/null ||
8770                         error "proc file read failed"
8771
8772         for facet in $SINGLEMDS ost1; do
8773                 do_facet $facet find $proc_dirs \
8774                         -not -name req_history \
8775                         -exec cat '{}' \\\; &> /dev/null
8776
8777             do_facet $facet     find $proc_dirs \
8778                         -not -name req_history \
8779                         -type f \
8780                         -readable \
8781                         -exec cat '{}' \\\; > /dev/null ||
8782                                 error "proc file read failed"
8783         done
8784 }
8785 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8786
8787 test_140() { #bug-17379
8788         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8789         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8790         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8791         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8792
8793         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8794         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8795         local i=0
8796         while i=`expr $i + 1`; do
8797                 test_mkdir -p $i || error "Creating dir $i"
8798                 cd $i || error "Changing to $i"
8799                 ln -s ../stat stat || error "Creating stat symlink"
8800                 # Read the symlink until ELOOP present,
8801                 # not LBUGing the system is considered success,
8802                 # we didn't overrun the stack.
8803                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8804                 [ $ret -ne 0 ] && {
8805                         if [ $ret -eq 40 ]; then
8806                                 break  # -ELOOP
8807                         else
8808                                 error "Open stat symlink"
8809                                 return
8810                         fi
8811                 }
8812         done
8813         i=`expr $i - 1`
8814         echo "The symlink depth = $i"
8815         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8816                                         error "Invalid symlink depth"
8817
8818         # Test recursive symlink
8819         ln -s symlink_self symlink_self
8820         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8821         echo "open symlink_self returns $ret"
8822         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8823 }
8824 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8825
8826 test_150() {
8827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8828         local TF="$TMP/$tfile"
8829
8830         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8831         cp $TF $DIR/$tfile
8832         cancel_lru_locks osc
8833         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8834         remount_client $MOUNT
8835         df -P $MOUNT
8836         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8837
8838         $TRUNCATE $TF 6000
8839         $TRUNCATE $DIR/$tfile 6000
8840         cancel_lru_locks osc
8841         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8842
8843         echo "12345" >>$TF
8844         echo "12345" >>$DIR/$tfile
8845         cancel_lru_locks osc
8846         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8847
8848         echo "12345" >>$TF
8849         echo "12345" >>$DIR/$tfile
8850         cancel_lru_locks osc
8851         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8852
8853         rm -f $TF
8854         true
8855 }
8856 run_test 150 "truncate/append tests"
8857
8858 #LU-2902 roc_hit was not able to read all values from lproc
8859 function roc_hit_init() {
8860         local list=$(comma_list $(osts_nodes))
8861         local dir=$DIR/$tdir-check
8862         local file=$dir/file
8863         local BEFORE
8864         local AFTER
8865         local idx
8866
8867         test_mkdir -p $dir
8868         #use setstripe to do a write to every ost
8869         for i in $(seq 0 $((OSTCOUNT-1))); do
8870                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
8871                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
8872                 idx=$(printf %04x $i)
8873                 BEFORE=$(get_osd_param $list *OST*$idx stats |
8874                     awk '$1 == "cache_access" {sum += $2} END { print sum }')
8875                 if [ -z "$BEFORE" ]; then
8876                         BEFORE=0
8877                 fi
8878
8879                 cancel_lru_locks osc
8880                 cat $file >/dev/null
8881
8882                 AFTER=$(get_osd_param $list *OST*$idx stats |
8883                     awk '$1 == "cache_access" {sum += $2} END { print sum }')
8884
8885                 echo BEFORE:$BEFORE AFTER:$AFTER
8886                 if ! let "AFTER - BEFORE == 4"; then
8887                         rm -rf $dir
8888                         error "roc_hit is not safe to use"
8889                 fi
8890                 rm $file
8891         done
8892
8893         rm -rf $dir
8894 }
8895
8896 function roc_hit() {
8897         local list=$(comma_list $(osts_nodes))
8898         echo $(get_osd_param $list '' stats |
8899                 awk '$1 == "cache_hit" {sum += $2} END { print sum }')
8900 }
8901
8902 function set_cache() {
8903         local on=1
8904
8905         if [ "$2" == "off" ]; then
8906                 on=0;
8907         fi
8908         local list=$(comma_list $(osts_nodes))
8909         set_osd_param $list '' $1_cache_enable $on
8910
8911         cancel_lru_locks osc
8912 }
8913
8914 test_151() {
8915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8916         remote_ost_nodsh && skip "remote OST with nodsh" && return
8917
8918         local CPAGES=3
8919         local list=$(comma_list $(osts_nodes))
8920
8921         # check whether obdfilter is cache capable at all
8922         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8923                 echo "not cache-capable obdfilter"
8924                 return 0
8925         fi
8926
8927         # check cache is enabled on all obdfilters
8928         if get_osd_param $list '' read_cache_enable | grep 0; then
8929                 echo "oss cache is disabled"
8930                 return 0
8931         fi
8932
8933         set_osd_param $list '' writethrough_cache_enable 1
8934
8935         # check write cache is enabled on all obdfilters
8936         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
8937                 echo "oss write cache is NOT enabled"
8938                 return 0
8939         fi
8940
8941         roc_hit_init
8942
8943         #define OBD_FAIL_OBD_NO_LRU  0x609
8944         do_nodes $list $LCTL set_param fail_loc=0x609
8945
8946         # pages should be in the case right after write
8947         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
8948                 error "dd failed"
8949
8950         local BEFORE=$(roc_hit)
8951         cancel_lru_locks osc
8952         cat $DIR/$tfile >/dev/null
8953         local AFTER=$(roc_hit)
8954
8955         do_nodes $list $LCTL set_param fail_loc=0
8956
8957         if ! let "AFTER - BEFORE == CPAGES"; then
8958                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8959         fi
8960
8961         # the following read invalidates the cache
8962         cancel_lru_locks osc
8963         set_osd_param $list '' read_cache_enable 0
8964         cat $DIR/$tfile >/dev/null
8965
8966         # now data shouldn't be found in the cache
8967         BEFORE=$(roc_hit)
8968         cancel_lru_locks osc
8969         cat $DIR/$tfile >/dev/null
8970         AFTER=$(roc_hit)
8971         if let "AFTER - BEFORE != 0"; then
8972                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8973         fi
8974
8975         set_osd_param $list '' read_cache_enable 1
8976         rm -f $DIR/$tfile
8977 }
8978 run_test 151 "test cache on oss and controls ==============================="
8979
8980 test_152() {
8981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8982         local TF="$TMP/$tfile"
8983
8984         # simulate ENOMEM during write
8985 #define OBD_FAIL_OST_NOMEM      0x226
8986         lctl set_param fail_loc=0x80000226
8987         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8988         cp $TF $DIR/$tfile
8989         sync || error "sync failed"
8990         lctl set_param fail_loc=0
8991
8992         # discard client's cache
8993         cancel_lru_locks osc
8994
8995         # simulate ENOMEM during read
8996         lctl set_param fail_loc=0x80000226
8997         cmp $TF $DIR/$tfile || error "cmp failed"
8998         lctl set_param fail_loc=0
8999
9000         rm -f $TF
9001 }
9002 run_test 152 "test read/write with enomem ============================"
9003
9004 test_153() {
9005         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9006 }
9007 run_test 153 "test if fdatasync does not crash ======================="
9008
9009 dot_lustre_fid_permission_check() {
9010         local fid=$1
9011         local ffid=$MOUNT/.lustre/fid/$fid
9012         local test_dir=$2
9013
9014         echo "stat fid $fid"
9015         stat $ffid > /dev/null || error "stat $ffid failed."
9016         echo "touch fid $fid"
9017         touch $ffid || error "touch $ffid failed."
9018         echo "write to fid $fid"
9019         cat /etc/hosts > $ffid || error "write $ffid failed."
9020         echo "read fid $fid"
9021         diff /etc/hosts $ffid || error "read $ffid failed."
9022         echo "append write to fid $fid"
9023         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9024         echo "rename fid $fid"
9025         mv $ffid $test_dir/$tfile.1 &&
9026                 error "rename $ffid to $tfile.1 should fail."
9027         touch $test_dir/$tfile.1
9028         mv $test_dir/$tfile.1 $ffid &&
9029                 error "rename $tfile.1 to $ffid should fail."
9030         rm -f $test_dir/$tfile.1
9031         echo "truncate fid $fid"
9032         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9033         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9034                 echo "link fid $fid"
9035                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9036         fi
9037         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9038                 echo "setfacl fid $fid"
9039                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9040                 echo "getfacl fid $fid"
9041                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9042         fi
9043         echo "unlink fid $fid"
9044         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9045         echo "mknod fid $fid"
9046         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9047
9048         fid=[0xf00000400:0x1:0x0]
9049         ffid=$MOUNT/.lustre/fid/$fid
9050
9051         echo "stat non-exist fid $fid"
9052         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9053         echo "write to non-exist fid $fid"
9054         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9055         echo "link new fid $fid"
9056         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9057
9058         mkdir -p $test_dir/$tdir
9059         touch $test_dir/$tdir/$tfile
9060         fid=$($LFS path2fid $test_dir/$tdir)
9061         rc=$?
9062         [ $rc -ne 0 ] &&
9063                 error "error: could not get fid for $test_dir/$dir/$tfile."
9064
9065         ffid=$MOUNT/.lustre/fid/$fid
9066
9067         echo "ls $fid"
9068         ls $ffid > /dev/null || error "ls $ffid failed."
9069         echo "touch $fid/$tfile.1"
9070         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9071
9072         echo "touch $MOUNT/.lustre/fid/$tfile"
9073         touch $MOUNT/.lustre/fid/$tfile && \
9074                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9075
9076         echo "setxattr to $MOUNT/.lustre/fid"
9077         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9078
9079         echo "listxattr for $MOUNT/.lustre/fid"
9080         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9081
9082         echo "delxattr from $MOUNT/.lustre/fid"
9083         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9084
9085         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9086         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9087                 error "touch invalid fid should fail."
9088
9089         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9090         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9091                 error "touch non-normal fid should fail."
9092
9093         echo "rename $tdir to $MOUNT/.lustre/fid"
9094         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9095                 error "rename to $MOUNT/.lustre/fid should fail."
9096
9097         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9098         then            # LU-3547
9099                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9100                 local new_obf_mode=777
9101
9102                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9103                 chmod $new_obf_mode $DIR/.lustre/fid ||
9104                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9105
9106                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9107                 [ $obf_mode -eq $new_obf_mode ] ||
9108                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9109
9110                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9111                 chmod $old_obf_mode $DIR/.lustre/fid ||
9112                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9113         fi
9114
9115         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9116         fid=$($LFS path2fid $test_dir/$tfile-2)
9117         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9118         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9119                 error "create lov data thru .lustre should fail."
9120         echo "cp /etc/passwd $test_dir/$tfile-2"
9121         cp /etc/passwd $test_dir/$tfile-2 ||
9122                 error "copy to $test_dir/$tfile-2 failed."
9123         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9124         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9125                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9126
9127         rm -rf $test_dir/tfile.lnk
9128         rm -rf $test_dir/$tfile-2
9129 }
9130
9131 test_154A() {
9132         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9133                 skip "Need MDS version at least 2.4.1" && return
9134
9135         touch $DIR/$tfile
9136         local FID=$($LFS path2fid $DIR/$tfile)
9137         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9138
9139         # check that we get the same pathname back
9140         local FOUND=$($LFS fid2path $MOUNT $FID)
9141         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9142         [ "$FOUND" != "$DIR/$tfile" ] &&
9143                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9144
9145         rm -rf $DIR/$tfile
9146 }
9147 run_test 154A "lfs path2fid and fid2path basic checks"
9148
9149 test_154a() {
9150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9151         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9152                 { skip "Need MDS version at least 2.2.51"; return 0; }
9153
9154         cp /etc/hosts $DIR/$tfile
9155
9156         fid=$($LFS path2fid $DIR/$tfile)
9157         rc=$?
9158         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9159
9160         dot_lustre_fid_permission_check "$fid" $DIR ||
9161                 error "dot lustre permission check $fid failed"
9162
9163         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9164
9165         touch $MOUNT/.lustre/file &&
9166                 error "creation is not allowed under .lustre"
9167
9168         mkdir $MOUNT/.lustre/dir &&
9169                 error "mkdir is not allowed under .lustre"
9170
9171         rm -rf $DIR/$tfile
9172 }
9173 run_test 154a "Open-by-FID"
9174
9175 test_154b() {
9176         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9177         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9178                 { skip "Need MDS version at least 2.2.51"; return 0; }
9179
9180         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9181
9182         local remote_dir=$DIR/$tdir/remote_dir
9183         local MDTIDX=1
9184         local rc=0
9185
9186         mkdir -p $DIR/$tdir
9187         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9188                 error "create remote directory failed"
9189
9190         cp /etc/hosts $remote_dir/$tfile
9191
9192         fid=$($LFS path2fid $remote_dir/$tfile)
9193         rc=$?
9194         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9195
9196         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9197                 error "dot lustre permission check $fid failed"
9198         rm -rf $DIR/$tdir
9199 }
9200 run_test 154b "Open-by-FID for remote directory"
9201
9202 test_154c() {
9203         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9204                 skip "Need MDS version at least 2.4.1" && return
9205
9206         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9207         local FID1=$($LFS path2fid $DIR/$tfile.1)
9208         local FID2=$($LFS path2fid $DIR/$tfile.2)
9209         local FID3=$($LFS path2fid $DIR/$tfile.3)
9210
9211         local N=1
9212         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9213                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9214                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9215                 local want=FID$N
9216                 [ "$FID" = "${!want}" ] ||
9217                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9218                 N=$((N + 1))
9219         done
9220
9221         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9222                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9223                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9224                 N=$((N + 1))
9225         done
9226 }
9227 run_test 154c "lfs path2fid and fid2path multiple arguments"
9228
9229 test_154d() {
9230         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9231                 skip "Need MDS version at least 2.5.53" && return
9232
9233         if remote_mds; then
9234                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9235         else
9236                 nid="0@lo"
9237         fi
9238         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9239         local fd
9240         local cmd
9241
9242         rm -f $DIR/$tfile
9243         touch $DIR/$tfile
9244
9245         fid=$($LFS path2fid $DIR/$tfile)
9246         # Open the file
9247         fd=$(free_fd)
9248         cmd="exec $fd<$DIR/$tfile"
9249         eval $cmd
9250         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9251         echo $fid_list | grep $fid
9252         rc=$?
9253
9254         cmd="exec $fd>/dev/null"
9255         eval $cmd
9256         if [ $rc -ne 0 ]; then
9257                 error "FID $fid not found in open files list $fid_list"
9258         fi
9259 }
9260 run_test 154d "Verify open file fid"
9261
9262 test_155_small_load() {
9263     local temp=$TMP/$tfile
9264     local file=$DIR/$tfile
9265
9266     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9267         error "dd of=$temp bs=6096 count=1 failed"
9268     cp $temp $file
9269     cancel_lru_locks osc
9270     cmp $temp $file || error "$temp $file differ"
9271
9272     $TRUNCATE $temp 6000
9273     $TRUNCATE $file 6000
9274     cmp $temp $file || error "$temp $file differ (truncate1)"
9275
9276     echo "12345" >>$temp
9277     echo "12345" >>$file
9278     cmp $temp $file || error "$temp $file differ (append1)"
9279
9280     echo "12345" >>$temp
9281     echo "12345" >>$file
9282     cmp $temp $file || error "$temp $file differ (append2)"
9283
9284     rm -f $temp $file
9285     true
9286 }
9287
9288 test_155_big_load() {
9289     remote_ost_nodsh && skip "remote OST with nodsh" && return
9290     local temp=$TMP/$tfile
9291     local file=$DIR/$tfile
9292
9293     free_min_max
9294     local cache_size=$(do_facet ost$((MAXI+1)) \
9295         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9296     local large_file_size=$((cache_size * 2))
9297
9298     echo "OSS cache size: $cache_size KB"
9299     echo "Large file size: $large_file_size KB"
9300
9301     [ $MAXV -le $large_file_size ] && \
9302         skip_env "max available OST size needs > $large_file_size KB" && \
9303         return 0
9304
9305     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9306
9307     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9308         error "dd of=$temp bs=$large_file_size count=1k failed"
9309     cp $temp $file
9310     ls -lh $temp $file
9311     cancel_lru_locks osc
9312     cmp $temp $file || error "$temp $file differ"
9313
9314     rm -f $temp $file
9315     true
9316 }
9317
9318 test_155a() {
9319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9320         set_cache read on
9321         set_cache writethrough on
9322         test_155_small_load
9323 }
9324 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9325
9326 test_155b() {
9327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9328         set_cache read on
9329         set_cache writethrough off
9330         test_155_small_load
9331 }
9332 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9333
9334 test_155c() {
9335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9336         set_cache read off
9337         set_cache writethrough on
9338         test_155_small_load
9339 }
9340 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9341
9342 test_155d() {
9343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9344         set_cache read off
9345         set_cache writethrough off
9346         test_155_small_load
9347 }
9348 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9349
9350 test_155e() {
9351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9352         set_cache read on
9353         set_cache writethrough on
9354         test_155_big_load
9355 }
9356 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9357
9358 test_155f() {
9359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9360         set_cache read on
9361         set_cache writethrough off
9362         test_155_big_load
9363 }
9364 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9365
9366 test_155g() {
9367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9368         set_cache read off
9369         set_cache writethrough on
9370         test_155_big_load
9371 }
9372 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9373
9374 test_155h() {
9375         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9376         set_cache read off
9377         set_cache writethrough off
9378         test_155_big_load
9379 }
9380 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9381
9382 test_156() {
9383         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9384         local CPAGES=3
9385         local BEFORE
9386         local AFTER
9387         local file="$DIR/$tfile"
9388
9389         [ "$(facet_fstype ost1)" = "zfs" ] &&
9390                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9391                 return
9392
9393         roc_hit_init
9394
9395     log "Turn on read and write cache"
9396     set_cache read on
9397     set_cache writethrough on
9398
9399     log "Write data and read it back."
9400     log "Read should be satisfied from the cache."
9401     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9402     BEFORE=`roc_hit`
9403     cancel_lru_locks osc
9404     cat $file >/dev/null
9405     AFTER=`roc_hit`
9406     if ! let "AFTER - BEFORE == CPAGES"; then
9407         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9408     else
9409         log "cache hits:: before: $BEFORE, after: $AFTER"
9410     fi
9411
9412     log "Read again; it should be satisfied from the cache."
9413     BEFORE=$AFTER
9414     cancel_lru_locks osc
9415     cat $file >/dev/null
9416     AFTER=`roc_hit`
9417     if ! let "AFTER - BEFORE == CPAGES"; then
9418         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9419     else
9420         log "cache hits:: before: $BEFORE, after: $AFTER"
9421     fi
9422
9423
9424     log "Turn off the read cache and turn on the write cache"
9425     set_cache read off
9426     set_cache writethrough on
9427
9428     log "Read again; it should be satisfied from the cache."
9429     BEFORE=`roc_hit`
9430     cancel_lru_locks osc
9431     cat $file >/dev/null
9432     AFTER=`roc_hit`
9433     if ! let "AFTER - BEFORE == CPAGES"; then
9434         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9435     else
9436         log "cache hits:: before: $BEFORE, after: $AFTER"
9437     fi
9438
9439     log "Read again; it should not be satisfied from the cache."
9440     BEFORE=$AFTER
9441     cancel_lru_locks osc
9442     cat $file >/dev/null
9443     AFTER=`roc_hit`
9444     if ! let "AFTER - BEFORE == 0"; then
9445         error "IN CACHE: before: $BEFORE, after: $AFTER"
9446     else
9447         log "cache hits:: before: $BEFORE, after: $AFTER"
9448     fi
9449
9450     log "Write data and read it back."
9451     log "Read should be satisfied from the cache."
9452     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9453     BEFORE=`roc_hit`
9454     cancel_lru_locks osc
9455     cat $file >/dev/null
9456     AFTER=`roc_hit`
9457     if ! let "AFTER - BEFORE == CPAGES"; then
9458         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9459     else
9460         log "cache hits:: before: $BEFORE, after: $AFTER"
9461     fi
9462
9463     log "Read again; it should not be satisfied from the cache."
9464     BEFORE=$AFTER
9465     cancel_lru_locks osc
9466     cat $file >/dev/null
9467     AFTER=`roc_hit`
9468     if ! let "AFTER - BEFORE == 0"; then
9469         error "IN CACHE: before: $BEFORE, after: $AFTER"
9470     else
9471         log "cache hits:: before: $BEFORE, after: $AFTER"
9472     fi
9473
9474
9475     log "Turn off read and write cache"
9476     set_cache read off
9477     set_cache writethrough off
9478
9479     log "Write data and read it back"
9480     log "It should not be satisfied from the cache."
9481     rm -f $file
9482     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9483     cancel_lru_locks osc
9484     BEFORE=`roc_hit`
9485     cat $file >/dev/null
9486     AFTER=`roc_hit`
9487         if ! let "AFTER - BEFORE == 0"; then
9488                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9489         else
9490                 log "cache hits:: before: $BEFORE, after: $AFTER"
9491         fi
9492
9493     log "Turn on the read cache and turn off the write cache"
9494     set_cache read on
9495     set_cache writethrough off
9496
9497     log "Write data and read it back"
9498     log "It should not be satisfied from the cache."
9499     rm -f $file
9500     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9501     BEFORE=`roc_hit`
9502     cancel_lru_locks osc
9503     cat $file >/dev/null
9504     AFTER=`roc_hit`
9505         if ! let "AFTER - BEFORE == 0"; then
9506                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9507         else
9508                 log "cache hits:: before: $BEFORE, after: $AFTER"
9509         fi
9510
9511     log "Read again; it should be satisfied from the cache."
9512     BEFORE=`roc_hit`
9513     cancel_lru_locks osc
9514     cat $file >/dev/null
9515     AFTER=`roc_hit`
9516     if ! let "AFTER - BEFORE == CPAGES"; then
9517         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9518     else
9519         log "cache hits:: before: $BEFORE, after: $AFTER"
9520     fi
9521
9522     rm -f $file
9523 }
9524 run_test 156 "Verification of tunables ============================"
9525
9526 #Changelogs
9527 err17935 () {
9528         if [ $MDSCOUNT -gt 1 ]; then
9529                 error_ignore bz17935 $*
9530         else
9531                 error $*
9532         fi
9533 }
9534
9535 changelog_chmask()
9536 {
9537         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9538
9539         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9540
9541         if [ $MASK -eq 1 ]; then
9542                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9543         else
9544                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9545         fi
9546 }
9547
9548 changelog_extract_field() {
9549         local mdt=$1
9550         local cltype=$2
9551         local file=$3
9552         local identifier=$4
9553
9554         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9555                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9556                 tail -1
9557 }
9558
9559 test_160a() {
9560         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9561         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9562         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9563                 { skip "Need MDS version at least 2.2.0"; return; }
9564
9565         local CL_USERS="mdd.$MDT0.changelog_users"
9566         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9567         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9568         echo "Registered as changelog user $USER"
9569         $GET_CL_USERS | grep -q $USER ||
9570                 error "User $USER not found in changelog_users"
9571
9572         # change something
9573         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9574         touch $DIR/$tdir/pics/2008/zachy/timestamp
9575         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9576         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9577         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9578         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9579         rm $DIR/$tdir/pics/desktop.jpg
9580
9581         $LFS changelog $MDT0 | tail -5
9582
9583         echo "verifying changelog mask"
9584         changelog_chmask "MKDIR"
9585         changelog_chmask "CLOSE"
9586
9587         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9588         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9589
9590         changelog_chmask "MKDIR"
9591         changelog_chmask "CLOSE"
9592
9593         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9594         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9595
9596         $LFS changelog $MDT0
9597         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9598         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9599         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9600         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9601
9602         # verify contents
9603         echo "verifying target fid"
9604         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9605         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9606         [ "$fidc" == "$fidf" ] ||
9607                 err17935 "fid in changelog $fidc != file fid $fidf"
9608         echo "verifying parent fid"
9609         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9610         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9611         [ "$fidc" == "$fidf" ] ||
9612                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9613
9614         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9615         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9616         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9617         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9618         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9619                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9620
9621         MIN_REC=$($GET_CL_USERS |
9622                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9623         FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
9624         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9625         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9626                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9627
9628         # LU-3446 changelog index reset on MDT restart
9629         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9630         CUR_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9631         $LFS changelog_clear $MDT0 $USER 0
9632         stop $SINGLEMDS || error "Fail to stop MDT."
9633         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9634         CUR_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9635         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9636         [ $CUR_REC1 == $CUR_REC2 ] ||
9637                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9638
9639         echo "verifying user deregister"
9640         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9641         $GET_CL_USERS | grep -q $USER &&
9642                 error "User $USER still in changelog_users"
9643
9644         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9645         if [ $USERS -eq 0 ]; then
9646                 LAST_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9647                 touch $DIR/$tdir/chloe
9648                 LAST_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9649                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9650                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9651         else
9652                 echo "$USERS other changelog users; can't verify off"
9653         fi
9654 }
9655 run_test 160a "changelog sanity"
9656
9657 test_160b() { # LU-3587
9658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9659         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9660         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9661                 { skip "Need MDS version at least 2.2.0"; return; }
9662
9663         local CL_USERS="mdd.$MDT0.changelog_users"
9664         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9665         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9666         echo "Registered as changelog user $USER"
9667         $GET_CL_USERS | grep -q $USER ||
9668                 error "User $USER not found in changelog_users"
9669
9670         local LONGNAME1=$(str_repeat a 255)
9671         local LONGNAME2=$(str_repeat b 255)
9672
9673         cd $DIR
9674         echo "creating very long named file"
9675         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
9676         echo "moving very long named file"
9677         mv $LONGNAME1 $LONGNAME2
9678
9679         $LFS changelog $MDT0 | grep RENME
9680
9681         echo "deregistering $USER"
9682         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9683
9684         rm -f $LONGNAME2
9685 }
9686 run_test 160b "Verify that very long rename doesn't crash in changelog"
9687
9688 test_161a() {
9689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9690         test_mkdir -p -c1 $DIR/$tdir
9691         cp /etc/hosts $DIR/$tdir/$tfile
9692         test_mkdir -c1 $DIR/$tdir/foo1
9693         test_mkdir -c1 $DIR/$tdir/foo2
9694         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9695         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9696         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9697         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9698         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9699         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9700                 $LFS fid2path $DIR $FID
9701                 err17935 "bad link ea"
9702         fi
9703     # middle
9704     rm $DIR/$tdir/foo2/zachary
9705     # last
9706     rm $DIR/$tdir/foo2/thor
9707     # first
9708     rm $DIR/$tdir/$tfile
9709     # rename
9710     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9711     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9712         then
9713         $LFS fid2path $DIR $FID
9714         err17935 "bad link rename"
9715     fi
9716     rm $DIR/$tdir/foo2/maggie
9717
9718     # overflow the EA
9719     local longname=filename_avg_len_is_thirty_two_
9720     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
9721         error "failed to hardlink many files"
9722     links=$($LFS fid2path $DIR $FID | wc -l)
9723     echo -n "${links}/1000 links in link EA"
9724     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9725     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
9726         error "failed to unlink many hardlinks"
9727 }
9728 run_test 161a "link ea sanity"
9729
9730 test_161b() {
9731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9732         [ $MDSCOUNT -lt 2 ] &&
9733                 skip "skipping remote directory test" && return
9734         local MDTIDX=1
9735         local remote_dir=$DIR/$tdir/remote_dir
9736
9737         mkdir -p $DIR/$tdir
9738         $LFS mkdir -i $MDTIDX $remote_dir ||
9739                 error "create remote directory failed"
9740
9741         cp /etc/hosts $remote_dir/$tfile
9742         mkdir -p $remote_dir/foo1
9743         mkdir -p $remote_dir/foo2
9744         ln $remote_dir/$tfile $remote_dir/foo1/sofia
9745         ln $remote_dir/$tfile $remote_dir/foo2/zachary
9746         ln $remote_dir/$tfile $remote_dir/foo1/luna
9747         ln $remote_dir/$tfile $remote_dir/foo2/thor
9748
9749         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
9750                      tr -d ']')
9751         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9752                 $LFS fid2path $DIR $FID
9753                 err17935 "bad link ea"
9754         fi
9755         # middle
9756         rm $remote_dir/foo2/zachary
9757         # last
9758         rm $remote_dir/foo2/thor
9759         # first
9760         rm $remote_dir/$tfile
9761         # rename
9762         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
9763         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
9764         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
9765                 $LFS fid2path $DIR $FID
9766                 err17935 "bad link rename"
9767         fi
9768         rm $remote_dir/foo2/maggie
9769
9770         # overflow the EA
9771         local longname=filename_avg_len_is_thirty_two_
9772         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
9773                 error "failed to hardlink many files"
9774         links=$($LFS fid2path $DIR $FID | wc -l)
9775         echo -n "${links}/1000 links in link EA"
9776         [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9777         unlinkmany $remote_dir/foo2/$longname 1000 ||
9778         error "failed to unlink many hardlinks"
9779 }
9780 run_test 161b "link ea sanity under remote directory"
9781
9782 test_161c() {
9783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9784         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
9785                 skip "Need MDS version at least 2.1.5" && return
9786
9787         # define CLF_RENAME_LAST 0x0001
9788         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
9789         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9790                 changelog_register -n)
9791         rm -rf $DIR/$tdir
9792         mkdir -p $DIR/$tdir
9793         touch $DIR/$tdir/foo_161c
9794         touch $DIR/$tdir/bar_161c
9795         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9796         $LFS changelog $MDT0 | grep RENME
9797         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
9798                 cut -f5 -d' ')
9799         $LFS changelog_clear $MDT0 $USER 0
9800         if [ x$flags != "x0x1" ]; then
9801                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9802                         $USER
9803                 error "flag $flags is not 0x1"
9804         fi
9805         echo "rename overwrite a target having nlink = 1," \
9806                 "changelog record has flags of $flags"
9807
9808         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
9809         touch $DIR/$tdir/foo_161c
9810         touch $DIR/$tdir/bar_161c
9811         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9812         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9813         $LFS changelog $MDT0 | grep RENME
9814         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9815         $LFS changelog_clear $MDT0 $USER 0
9816         if [ x$flags != "x0x0" ]; then
9817                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9818                         $USER
9819                 error "flag $flags is not 0x0"
9820         fi
9821         echo "rename overwrite a target having nlink > 1," \
9822                 "changelog record has flags of $flags"
9823
9824         # rename doesn't overwrite a target (changelog flag 0x0)
9825         touch $DIR/$tdir/foo_161c
9826         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
9827         $LFS changelog $MDT0 | grep RENME
9828         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9829         $LFS changelog_clear $MDT0 $USER 0
9830         if [ x$flags != "x0x0" ]; then
9831                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9832                         $USER
9833                 error "flag $flags is not 0x0"
9834         fi
9835         echo "rename doesn't overwrite a target," \
9836                 "changelog record has flags of $flags"
9837
9838         # define CLF_UNLINK_LAST 0x0001
9839         # unlink a file having nlink = 1 (changelog flag 0x1)
9840         rm -f $DIR/$tdir/foo2_161c
9841         $LFS changelog $MDT0 | grep UNLNK
9842         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9843         $LFS changelog_clear $MDT0 $USER 0
9844         if [ x$flags != "x0x1" ]; then
9845                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9846                         $USER
9847                 error "flag $flags is not 0x1"
9848         fi
9849         echo "unlink a file having nlink = 1," \
9850                 "changelog record has flags of $flags"
9851
9852         # unlink a file having nlink > 1 (changelog flag 0x0)
9853         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9854         rm -f $DIR/$tdir/foobar_161c
9855         $LFS changelog $MDT0 | grep UNLNK
9856         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9857         $LFS changelog_clear $MDT0 $USER 0
9858         if [ x$flags != "x0x0" ]; then
9859                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9860                         $USER
9861                 error "flag $flags is not 0x0"
9862         fi
9863         echo "unlink a file having nlink > 1," \
9864                 "changelog record has flags of $flags"
9865         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9866 }
9867 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
9868
9869 check_path() {
9870     local expected=$1
9871     shift
9872     local fid=$2
9873
9874     local path=$(${LFS} fid2path $*)
9875     RC=$?
9876
9877     if [ $RC -ne 0 ]; then
9878         err17935 "path looked up of $expected failed. Error $RC"
9879         return $RC
9880     elif [ "${path}" != "${expected}" ]; then
9881         err17935 "path looked up \"${path}\" instead of \"${expected}\""
9882         return 2
9883     fi
9884     echo "fid $fid resolves to path $path (expected $expected)"
9885 }
9886
9887 test_162() {
9888         # Make changes to filesystem
9889         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9890         test_mkdir -p $DIR/$tdir/d2
9891         touch $DIR/$tdir/d2/$tfile
9892         touch $DIR/$tdir/d2/x1
9893         touch $DIR/$tdir/d2/x2
9894         test_mkdir -p $DIR/$tdir/d2/a/b/c
9895         test_mkdir -p $DIR/$tdir/d2/p/q/r
9896         # regular file
9897         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
9898         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
9899
9900         # softlink
9901         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
9902         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
9903         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
9904
9905         # softlink to wrong file
9906         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
9907         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
9908         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
9909
9910         # hardlink
9911         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
9912         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
9913         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
9914         # fid2path dir/fsname should both work
9915         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
9916         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9917
9918         # hardlink count: check that there are 2 links
9919         # Doesnt work with CMD yet: 17935
9920         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9921                 err17935 "expected 2 links"
9922
9923         # hardlink indexing: remove the first link
9924         rm $DIR/$tdir/d2/p/q/r/hlink
9925         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9926
9927         return 0
9928 }
9929 run_test 162 "path lookup sanity"
9930
9931 test_169() {
9932         # do directio so as not to populate the page cache
9933         log "creating a 10 Mb file"
9934         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9935         log "starting reads"
9936         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9937         log "truncating the file"
9938         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9939         log "killing dd"
9940         kill %+ || true # reads might have finished
9941         echo "wait until dd is finished"
9942         wait
9943         log "removing the temporary file"
9944         rm -rf $DIR/$tfile || error "tmp file removal failed"
9945 }
9946 run_test 169 "parallel read and truncate should not deadlock"
9947
9948 test_170() {
9949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9950         $LCTL clear     # bug 18514
9951         $LCTL debug_daemon start $TMP/${tfile}_log_good
9952         touch $DIR/$tfile
9953         $LCTL debug_daemon stop
9954         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9955                error "sed failed to read log_good"
9956
9957         $LCTL debug_daemon start $TMP/${tfile}_log_good
9958         rm -rf $DIR/$tfile
9959         $LCTL debug_daemon stop
9960
9961         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9962                error "lctl df log_bad failed"
9963
9964         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9965         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9966
9967         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9968         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9969
9970         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9971                 error "bad_line good_line1 good_line2 are empty"
9972
9973         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9974         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9975         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9976
9977         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9978         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9979         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9980
9981         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9982                 error "bad_line_new good_line_new are empty"
9983
9984         local expected_good=$((good_line1 + good_line2*2))
9985
9986         rm -f $TMP/${tfile}*
9987         # LU-231, short malformed line may not be counted into bad lines
9988         if [ $bad_line -ne $bad_line_new ] &&
9989                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9990                 error "expected $bad_line bad lines, but got $bad_line_new"
9991                 return 1
9992         fi
9993
9994         if [ $expected_good -ne $good_line_new ]; then
9995                 error "expected $expected_good good lines, but got $good_line_new"
9996                 return 2
9997         fi
9998         true
9999 }
10000 run_test 170 "test lctl df to handle corrupted log ====================="
10001
10002 test_171() { # bug20592
10003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10004 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10005         $LCTL set_param fail_loc=0x50e
10006         $LCTL set_param fail_val=3000
10007         multiop_bg_pause $DIR/$tfile O_s || true
10008         local MULTIPID=$!
10009         kill -USR1 $MULTIPID
10010         # cause log dump
10011         sleep 3
10012         wait $MULTIPID
10013         if dmesg | grep "recursive fault"; then
10014                 error "caught a recursive fault"
10015         fi
10016         $LCTL set_param fail_loc=0
10017         true
10018 }
10019 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10020
10021 # it would be good to share it with obdfilter-survey/libecho code
10022 setup_obdecho_osc () {
10023         local rc=0
10024         local ost_nid=$1
10025         local obdfilter_name=$2
10026         echo "Creating new osc for $obdfilter_name on $ost_nid"
10027         # make sure we can find loopback nid
10028         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10029
10030         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10031                            ${obdfilter_name}_osc_UUID || rc=2; }
10032         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10033                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10034         return $rc
10035 }
10036
10037 cleanup_obdecho_osc () {
10038         local obdfilter_name=$1
10039         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10040         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10041         return 0
10042 }
10043
10044 obdecho_test() {
10045         local OBD=$1
10046         local node=$2
10047         local pages=${3:-64}
10048         local rc=0
10049         local id
10050
10051         local count=10
10052         local obd_size=$(get_obd_size $node $OBD)
10053         local page_size=$(get_page_size $node)
10054         if [[ -n "$obd_size" ]]; then
10055                 local new_count=$((obd_size / (pages * page_size / 1024)))
10056                 [[ $new_count -ge $count ]] || count=$new_count
10057         fi
10058
10059         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10060         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10061                            rc=2; }
10062         if [ $rc -eq 0 ]; then
10063             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10064             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10065         fi
10066         echo "New object id is $id"
10067         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10068                            rc=4; }
10069         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10070                            "test_brw $count w v $pages $id" || rc=4; }
10071         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10072                            rc=4; }
10073         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10074                                         "cleanup" || rc=5; }
10075         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10076                                         "detach" || rc=6; }
10077         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10078         return $rc
10079 }
10080
10081 test_180a() {
10082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10083         remote_ost_nodsh && skip "remote OST with nodsh" && return
10084         local rc=0
10085         local rmmod_local=0
10086
10087         if ! module_loaded obdecho; then
10088             load_module obdecho/obdecho
10089             rmmod_local=1
10090         fi
10091
10092         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10093         local host=$(lctl get_param -n osc.$osc.import |
10094                              awk '/current_connection:/ {print $2}' )
10095         local target=$(lctl get_param -n osc.$osc.import |
10096                              awk '/target:/ {print $2}' )
10097         target=${target%_UUID}
10098
10099         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10100         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10101         [[ -n $target ]] && cleanup_obdecho_osc $target
10102         [ $rmmod_local -eq 1 ] && rmmod obdecho
10103         return $rc
10104 }
10105 run_test 180a "test obdecho on osc"
10106
10107 test_180b() {
10108         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10109         remote_ost_nodsh && skip "remote OST with nodsh" && return
10110         local rc=0
10111         local rmmod_remote=0
10112
10113         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10114                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10115                       "modprobe obdecho; }" && rmmod_remote=1
10116         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10117         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10118         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10119         return $rc
10120 }
10121 run_test 180b "test obdecho directly on obdfilter"
10122
10123 test_180c() { # LU-2598
10124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10125         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10126                 skip "Need MDS version at least 2.4.0" && return
10127
10128         local rc=0
10129         local rmmod_remote=false
10130         local pages=16384 # 64MB bulk I/O RPC size
10131         local target
10132
10133         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10134                 rmmod_remote=true || error "failed to load module obdecho"
10135
10136         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4}'|head -1)
10137         if [[ -n $target ]]; then
10138                 obdecho_test "$target" ost1 "$pages" ||
10139                         rc=${PIPESTATUS[0]}
10140         else
10141                 echo "there is no obdfilter target on ost1"
10142                 rc=2
10143         fi
10144         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10145         return $rc
10146 }
10147 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10148
10149 test_181() { # bug 22177
10150         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10151         # create enough files to index the directory
10152         createmany -o $DIR/$tdir/foobar 4000
10153         # print attributes for debug purpose
10154         lsattr -d .
10155         # open dir
10156         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10157         MULTIPID=$!
10158         # remove the files & current working dir
10159         unlinkmany $DIR/$tdir/foobar 4000
10160         rmdir $DIR/$tdir
10161         kill -USR1 $MULTIPID
10162         wait $MULTIPID
10163         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10164         return 0
10165 }
10166 run_test 181 "Test open-unlinked dir ========================"
10167
10168 test_182() {
10169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10170         # disable MDC RPC lock wouldn't crash client
10171         local fcount=1000
10172         local tcount=4
10173
10174         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10175 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10176         $LCTL set_param fail_loc=0x804
10177
10178         for (( i=0; i < $tcount; i++ )) ; do
10179                 mkdir $DIR/$tdir/$i
10180                 createmany -o $DIR/$tdir/$i/f- $fcount &
10181         done
10182         wait
10183
10184         for (( i=0; i < $tcount; i++ )) ; do
10185                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10186         done
10187         wait
10188
10189         rm -rf $DIR/$tdir
10190
10191         $LCTL set_param fail_loc=0
10192 }
10193 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10194
10195 test_183() { # LU-2275
10196         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10197                 skip "Need MDS version at least 2.3.56" && return
10198
10199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10200         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10201         echo aaa > $DIR/$tdir/$tfile
10202
10203 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10204         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10205
10206         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10207         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10208
10209         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10210
10211         # Flush negative dentry cache
10212         touch $DIR/$tdir/$tfile
10213
10214         # We are not checking for any leaked references here, they'll
10215         # become evident next time we do cleanup with module unload.
10216         rm -rf $DIR/$tdir
10217 }
10218 run_test 183 "No crash or request leak in case of strange dispositions ========"
10219
10220 # test suite 184 is for LU-2016, LU-2017
10221 test_184a() {
10222         check_swap_layouts_support && return 0
10223
10224         dir0=$DIR/$tdir/$testnum
10225         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10226         ref1=/etc/passwd
10227         ref2=/etc/group
10228         file1=$dir0/f1
10229         file2=$dir0/f2
10230         $SETSTRIPE -c1 $file1
10231         cp $ref1 $file1
10232         $SETSTRIPE -c2 $file2
10233         cp $ref2 $file2
10234         gen1=$($GETSTRIPE -g $file1)
10235         gen2=$($GETSTRIPE -g $file2)
10236
10237         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10238         gen=$($GETSTRIPE -g $file1)
10239         [[ $gen1 != $gen ]] ||
10240                 "Layout generation on $file1 does not change"
10241         gen=$($GETSTRIPE -g $file2)
10242         [[ $gen2 != $gen ]] ||
10243                 "Layout generation on $file2 does not change"
10244
10245         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10246         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10247 }
10248 run_test 184a "Basic layout swap"
10249
10250 test_184b() {
10251         check_swap_layouts_support && return 0
10252
10253         dir0=$DIR/$tdir/$testnum
10254         mkdir -p $dir0 || error "creating dir $dir0"
10255         file1=$dir0/f1
10256         file2=$dir0/f2
10257         file3=$dir0/f3
10258         dir1=$dir0/d1
10259         dir2=$dir0/d2
10260         mkdir $dir1 $dir2
10261         $SETSTRIPE -c1 $file1
10262         $SETSTRIPE -c2 $file2
10263         $SETSTRIPE -c1 $file3
10264         chown $RUNAS_ID $file3
10265         gen1=$($GETSTRIPE -g $file1)
10266         gen2=$($GETSTRIPE -g $file2)
10267
10268         $LFS swap_layouts $dir1 $dir2 &&
10269                 error "swap of directories layouts should fail"
10270         $LFS swap_layouts $dir1 $file1 &&
10271                 error "swap of directory and file layouts should fail"
10272         $RUNAS $LFS swap_layouts $file1 $file2 &&
10273                 error "swap of file we cannot write should fail"
10274         $LFS swap_layouts $file1 $file3 &&
10275                 error "swap of file with different owner should fail"
10276         /bin/true # to clear error code
10277 }
10278 run_test 184b "Forbidden layout swap (will generate errors)"
10279
10280 test_184c() {
10281         check_swap_layouts_support && return 0
10282
10283         local dir0=$DIR/$tdir/$testnum
10284         mkdir -p $dir0 || error "creating dir $dir0"
10285
10286         local ref1=$dir0/ref1
10287         local ref2=$dir0/ref2
10288         local file1=$dir0/file1
10289         local file2=$dir0/file2
10290         # create a file large enough for the concurent test
10291         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10292         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10293         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
10294
10295         cp $ref2 $file2
10296         dd if=$ref1 of=$file1 bs=16k &
10297         local DD_PID=$!
10298
10299         # Make sure dd starts to copy file
10300         while [ ! -f $file1 ]; do sleep 0.1; done
10301
10302         $LFS swap_layouts $file1 $file2
10303         local rc=$?
10304         wait $DD_PID
10305         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10306         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10307
10308         # how many bytes copied before swapping layout
10309         local copied=`stat -c %s $file2`
10310         local remaining=`stat -c %s $ref1`
10311         remaining=$((remaining - copied))
10312         echo "Copied $copied bytes before swapping layout..."
10313
10314         cmp -n $copied $file1 $ref2 | grep differ &&
10315                 error "Content mismatch [0, $copied) of ref2 and file1"
10316         cmp -n $copied $file2 $ref1 ||
10317                 error "Content mismatch [0, $copied) of ref1 and file2"
10318         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10319                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10320
10321         # clean up
10322         rm -f $ref1 $ref2 $file1 $file2
10323 }
10324 run_test 184c "Concurrent write and layout swap"
10325
10326 test_184d() {
10327         check_swap_layouts_support && return 0
10328
10329         local file1=$DIR/$tdir/$tfile-1
10330         local file2=$DIR/$tdir/$tfile-2
10331         local file3=$DIR/$tdir/$tfile-3
10332         local lovea1
10333         local lovea2
10334
10335         mkdir -p $DIR/$tdir
10336         touch $file1 || error "create $file1 failed"
10337         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10338                 error "create $file2 failed"
10339         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10340                 error "create $file3 failed"
10341         lovea1=$($LFS getstripe $file1 | sed 1d)
10342
10343         $LFS swap_layouts $file2 $file3 ||
10344                 error "swap $file2 $file3 layouts failed"
10345         $LFS swap_layouts $file1 $file2 ||
10346                 error "swap $file1 $file2 layouts failed"
10347
10348         lovea2=$($LFS getstripe $file2 | sed 1d)
10349         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10350
10351         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10352         [ -z $lovea1 ] || error "$file1 shouldn't have lovea"
10353 }
10354 run_test 184d "allow stripeless layouts swap"
10355
10356
10357 test_185() { # LU-2441
10358         # LU-3553 - no volatile file support in old servers
10359         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10360                 { skip "Need MDS version at least 2.3.60"; return 0; }
10361
10362         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10363         touch $DIR/$tdir/spoo
10364         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10365         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10366                 error "cannot create/write a volatile file"
10367         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10368                 error "FID is still valid after close"
10369
10370         multiop_bg_pause $DIR/$tdir vVw4096_c
10371         local multi_pid=$!
10372
10373         local OLD_IFS=$IFS
10374         IFS=":"
10375         local fidv=($fid)
10376         IFS=$OLD_IFS
10377         # assume that the next FID for this client is sequential, since stdout
10378         # is unfortunately eaten by multiop_bg_pause
10379         local n=$((${fidv[1]} + 1))
10380         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10381         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10382                 error "FID is missing before close"
10383         kill -USR1 $multi_pid
10384         # 1 second delay, so if mtime change we will see it
10385         sleep 1
10386         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10387         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10388 }
10389 run_test 185 "Volatile file support"
10390
10391 test_187a() {
10392         local dir0=$DIR/$tdir/$testnum
10393         mkdir -p $dir0 || error "creating dir $dir0"
10394
10395         local file=$dir0/file1
10396         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10397         local dv1=$($LFS data_version $file)
10398         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10399         local dv2=$($LFS data_version $file)
10400         [[ $dv1 != $dv2 ]] ||
10401                 error "data version did not change on write $dv1 == $dv2"
10402
10403         # clean up
10404         rm -f $file1
10405 }
10406 run_test 187a "Test data version change"
10407
10408 test_187b() {
10409         local dir0=$DIR/$tdir/$testnum
10410         mkdir -p $dir0 || error "creating dir $dir0"
10411
10412         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10413         [[ ${DV[0]} != ${DV[1]} ]] ||
10414                 error "data version did not change on write"\
10415                       " ${DV[0]} == ${DV[1]}"
10416
10417         # clean up
10418         rm -f $file1
10419 }
10420 run_test 187b "Test data version change on volatile file"
10421
10422 # OST pools tests
10423 check_file_in_pool()
10424 {
10425         local file=$1
10426         local pool=$2
10427         local tlist="$3"
10428         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10429         for i in $res
10430         do
10431                 for t in $tlist ; do
10432                         [ "$i" -eq "$t" ] && continue 2
10433                 done
10434
10435                 echo "pool list: $tlist"
10436                 echo "striping: $res"
10437                 error_noexit "$file not allocated in $pool"
10438                 return 1
10439         done
10440         return 0
10441 }
10442
10443 pool_add() {
10444         echo "Creating new pool"
10445         local pool=$1
10446
10447         create_pool $FSNAME.$pool ||
10448                 { error_noexit "No pool created, result code $?"; return 1; }
10449         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10450                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10451 }
10452
10453 pool_add_targets() {
10454         echo "Adding targets to pool"
10455         local pool=$1
10456         local first=$2
10457         local last=$3
10458         local step=${4:-1}
10459
10460         local list=$(seq $first $step $last)
10461
10462         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10463         do_facet mgs $LCTL pool_add \
10464                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10465         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10466                         | sort -u | tr '\n' ' ' " "$t" || { 
10467                 error_noexit "Add to pool failed"
10468                 return 1
10469         }
10470         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10471         local addcount=$(((last - first) / step + 1))
10472         [ $lfscount -eq $addcount ] || {
10473                 error_noexit "lfs pool_list bad ost count" \
10474                                                 "$lfscount != $addcount"
10475                 return 2
10476         }
10477 }
10478
10479 pool_set_dir() {
10480         local pool=$1
10481         local tdir=$2
10482         echo "Setting pool on directory $tdir"
10483
10484         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10485
10486         error_noexit "Cannot set pool $pool to $tdir"
10487         return 1
10488 }
10489
10490 pool_check_dir() {
10491         local pool=$1
10492         local tdir=$2
10493         echo "Checking pool on directory $tdir"
10494
10495         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10496         [ "$res" = "$pool" ] && return 0
10497
10498         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10499         return 1
10500 }
10501
10502 pool_dir_rel_path() {
10503         echo "Testing relative path works well"
10504         local pool=$1
10505         local tdir=$2
10506         local root=$3
10507
10508         mkdir -p $root/$tdir/$tdir
10509         cd $root/$tdir
10510         pool_set_dir $pool $tdir          || return 1
10511         pool_set_dir $pool ./$tdir        || return 2
10512         pool_set_dir $pool ../$tdir       || return 3
10513         pool_set_dir $pool ../$tdir/$tdir || return 4
10514         rm -rf $tdir; cd - > /dev/null
10515 }
10516
10517 pool_alloc_files() {
10518         echo "Checking files allocation from directory pool"
10519         local pool=$1
10520         local tdir=$2
10521         local count=$3
10522         local tlist="$4"
10523
10524         local failed=0
10525         for i in $(seq -w 1 $count)
10526         do
10527                 local file=$tdir/file-$i
10528                 touch $file
10529                 check_file_in_pool $file $pool "$tlist" || \
10530                         failed=$((failed + 1))
10531         done
10532         [ "$failed" = 0 ] && return 0
10533
10534         error_noexit "$failed files not allocated in $pool"
10535         return 1
10536 }
10537
10538 pool_create_files() {
10539         echo "Creating files in pool"
10540         local pool=$1
10541         local tdir=$2
10542         local count=$3
10543         local tlist="$4"
10544
10545         mkdir -p $tdir
10546         local failed=0
10547         for i in $(seq -w 1 $count)
10548         do
10549                 local file=$tdir/spoo-$i
10550                 $SETSTRIPE -p $pool $file
10551                 check_file_in_pool $file $pool "$tlist" || \
10552                         failed=$((failed + 1))
10553         done
10554         [ "$failed" = 0 ] && return 0
10555
10556         error_noexit "$failed files not allocated in $pool"
10557         return 1
10558 }
10559
10560 pool_lfs_df() {
10561         echo "Checking 'lfs df' output"
10562         local pool=$1
10563
10564         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10565                         tr '\n' ' ')
10566         local res=$($LFS df --pool $FSNAME.$pool |
10567                         awk '{print $1}' |
10568                         grep "$FSNAME-OST" |
10569                         tr '\n' ' ')
10570         [ "$res" = "$t" ] && return 0
10571
10572         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10573         return 1
10574 }
10575
10576 pool_file_rel_path() {
10577         echo "Creating files in a pool with relative pathname"
10578         local pool=$1
10579         local tdir=$2
10580
10581         mkdir -p $tdir ||
10582                 { error_noexit "unable to create $tdir"; return 1 ; }
10583         local file="/..$tdir/$tfile-1"
10584         $SETSTRIPE -p $pool $file ||
10585                 { error_noexit "unable to create $file" ; return 2 ; }
10586
10587         cd $tdir
10588         $SETSTRIPE -p $pool $tfile-2 || {
10589                 error_noexit "unable to create $tfile-2 in $tdir"
10590                 return 3
10591         }
10592 }
10593
10594 pool_remove_first_target() {
10595         echo "Removing first target from a pool"
10596         local pool=$1
10597
10598         local pname="lov.$FSNAME-*.pools.$pool"
10599         local t=$($LCTL get_param -n $pname | head -1)
10600         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10601         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10602                 error_noexit "$t not removed from $FSNAME.$pool"
10603                 return 1
10604         }
10605 }
10606
10607 pool_remove_all_targets() {
10608         echo "Removing all targets from pool"
10609         local pool=$1
10610         local file=$2
10611         local pname="lov.$FSNAME-*.pools.$pool"
10612         for t in $($LCTL get_param -n $pname | sort -u)
10613         do
10614                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10615         done
10616         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
10617                 error_noexit "Pool $FSNAME.$pool cannot be drained"
10618                 return 1
10619         }
10620         # striping on an empty/nonexistant pool should fall back 
10621         # to "pool of everything"
10622         touch $file || {
10623                 error_noexit "failed to use fallback striping for empty pool"
10624                 return 2
10625         }
10626         # setstripe on an empty pool should fail
10627         $SETSTRIPE -p $pool $file 2>/dev/null && {
10628                 error_noexit "expected failure when creating file" \
10629                                                         "with empty pool"
10630                 return 3
10631         }
10632         return 0
10633 }
10634
10635 pool_remove() {
10636         echo "Destroying pool"
10637         local pool=$1
10638         local file=$2
10639
10640         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
10641
10642         sleep 2
10643         # striping on an empty/nonexistant pool should fall back 
10644         # to "pool of everything"
10645         touch $file || {
10646                 error_noexit "failed to use fallback striping for missing pool"
10647                 return 1
10648         }
10649         # setstripe on an empty pool should fail
10650         $SETSTRIPE -p $pool $file 2>/dev/null && {
10651                 error_noexit "expected failure when creating file" \
10652                                                         "with missing pool"
10653                 return 2
10654         }
10655
10656         # get param should return err once pool is gone
10657         if wait_update $HOSTNAME "lctl get_param -n \
10658                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
10659         then
10660                 remove_pool_from_list $FSNAME.$pool
10661                 return 0
10662         fi
10663         error_noexit "Pool $FSNAME.$pool is not destroyed"
10664         return 3
10665 }
10666
10667 test_200() {
10668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10669         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
10670
10671         local POOL=${POOL:-cea1}
10672         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
10673         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
10674         # Pool OST targets
10675         local first_ost=0
10676         local last_ost=$(($OSTCOUNT - 1))
10677         local ost_step=2
10678         local ost_list=$(seq $first_ost $ost_step $last_ost)
10679         local ost_range="$first_ost $last_ost $ost_step"
10680         local test_path=$POOL_ROOT/$POOL_DIR_NAME
10681         local file_dir=$POOL_ROOT/file_tst
10682
10683         local rc=0
10684         while : ; do
10685                 # former test_200a test_200b
10686                 pool_add $POOL                          || { rc=$? ; break; }
10687                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
10688                 # former test_200c test_200d
10689                 mkdir -p $test_path
10690                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
10691                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
10692                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
10693                                                         || { rc=$? ; break; }
10694                 # former test_200e test_200f
10695                 local files=$((OSTCOUNT*3))
10696                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
10697                                                         || { rc=$? ; break; }
10698                 pool_create_files $POOL $file_dir $files "$ost_list" \
10699                                                         || { rc=$? ; break; }
10700                 # former test_200g test_200h
10701                 pool_lfs_df $POOL                       || { rc=$? ; break; }
10702                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
10703
10704                 # former test_201a test_201b test_201c
10705                 pool_remove_first_target $POOL          || { rc=$? ; break; }
10706
10707                 local f=$test_path/$tfile
10708                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
10709                 pool_remove $POOL $f                    || { rc=$? ; break; }
10710                 break
10711         done
10712
10713         cleanup_pools
10714         return $rc
10715 }
10716 run_test 200 "OST pools"
10717
10718 # usage: default_attr <count | size | offset>
10719 default_attr() {
10720         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
10721 }
10722
10723 # usage: check_default_stripe_attr
10724 check_default_stripe_attr() {
10725         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
10726         case $1 in
10727         --stripe-count|--count)
10728                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
10729         --stripe-size|--size)
10730                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
10731         --stripe-index|--index)
10732                 EXPECTED=-1;;
10733         *)
10734                 error "unknown getstripe attr '$1'"
10735         esac
10736
10737         [ $ACTUAL != $EXPECTED ] &&
10738                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
10739 }
10740
10741 test_204a() {
10742         test_mkdir -p $DIR/$tdir
10743         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
10744
10745         check_default_stripe_attr --stripe-count
10746         check_default_stripe_attr --stripe-size
10747         check_default_stripe_attr --stripe-index
10748
10749         return 0
10750 }
10751 run_test 204a "Print default stripe attributes ================="
10752
10753 test_204b() {
10754         test_mkdir -p $DIR/$tdir
10755         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10756
10757         check_default_stripe_attr --stripe-size
10758         check_default_stripe_attr --stripe-index
10759
10760         return 0
10761 }
10762 run_test 204b "Print default stripe size and offset  ==========="
10763
10764 test_204c() {
10765         test_mkdir -p $DIR/$tdir
10766         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10767
10768         check_default_stripe_attr --stripe-count
10769         check_default_stripe_attr --stripe-index
10770
10771         return 0
10772 }
10773 run_test 204c "Print default stripe count and offset ==========="
10774
10775 test_204d() {
10776         test_mkdir -p $DIR/$tdir
10777         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10778
10779         check_default_stripe_attr --stripe-count
10780         check_default_stripe_attr --stripe-size
10781
10782         return 0
10783 }
10784 run_test 204d "Print default stripe count and size ============="
10785
10786 test_204e() {
10787         test_mkdir -p $DIR/$tdir
10788         $SETSTRIPE -d $DIR/$tdir
10789
10790         check_default_stripe_attr --stripe-count --raw
10791         check_default_stripe_attr --stripe-size --raw
10792         check_default_stripe_attr --stripe-index --raw
10793
10794         return 0
10795 }
10796 run_test 204e "Print raw stripe attributes ================="
10797
10798 test_204f() {
10799         test_mkdir -p $DIR/$tdir
10800         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10801
10802         check_default_stripe_attr --stripe-size --raw
10803         check_default_stripe_attr --stripe-index --raw
10804
10805         return 0
10806 }
10807 run_test 204f "Print raw stripe size and offset  ==========="
10808
10809 test_204g() {
10810         test_mkdir -p $DIR/$tdir
10811         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10812
10813         check_default_stripe_attr --stripe-count --raw
10814         check_default_stripe_attr --stripe-index --raw
10815
10816         return 0
10817 }
10818 run_test 204g "Print raw stripe count and offset ==========="
10819
10820 test_204h() {
10821         test_mkdir -p $DIR/$tdir
10822         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10823
10824         check_default_stripe_attr --stripe-count --raw
10825         check_default_stripe_attr --stripe-size --raw
10826
10827         return 0
10828 }
10829 run_test 204h "Print raw stripe count and size ============="
10830
10831 # Figure out which job scheduler is being used, if any,
10832 # or use a fake one
10833 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
10834         JOBENV=SLURM_JOB_ID
10835 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
10836         JOBENV=LSB_JOBID
10837 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
10838         JOBENV=PBS_JOBID
10839 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
10840         JOBENV=LOADL_STEP_ID
10841 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
10842         JOBENV=JOB_ID
10843 else
10844         JOBENV=FAKE_JOBID
10845 fi
10846
10847 verify_jobstats() {
10848         local cmd=$1
10849         local target=$2
10850
10851         # clear old jobstats
10852         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
10853         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
10854
10855         # use a new JobID for this test, or we might see an old one
10856         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
10857
10858         JOBVAL=${!JOBENV}
10859         log "Test: $cmd"
10860         log "Using JobID environment variable $JOBENV=$JOBVAL"
10861
10862         if [ $JOBENV = "FAKE_JOBID" ]; then
10863                 FAKE_JOBID=$JOBVAL $cmd
10864         else
10865                 $cmd
10866         fi
10867
10868         if [ "$target" = "mdt" -o "$target" = "both" ]; then
10869                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
10870                 do_facet $FACET lctl get_param mdt.*.job_stats |
10871                         grep $JOBVAL || error "No job stats found on MDT $FACET"
10872         fi
10873         if [ "$target" = "ost" -o "$target" = "both" ]; then
10874                 FACET=ost1
10875                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
10876                         grep $JOBVAL || error "No job stats found on OST $FACET"
10877         fi
10878 }
10879
10880 jobstats_set() {
10881         trap 0
10882         NEW_JOBENV=${1:-$OLD_JOBENV}
10883         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
10884         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
10885 }
10886
10887 test_205() { # Job stats
10888         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10889         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
10890                 skip "Server doesn't support jobstats" && return 0
10891
10892         local cmd
10893         OLD_JOBENV=`$LCTL get_param -n jobid_var`
10894         if [ $OLD_JOBENV != $JOBENV ]; then
10895                 jobstats_set $JOBENV
10896                 trap jobstats_set EXIT
10897         fi
10898
10899         # mkdir
10900         cmd="mkdir $DIR/$tfile"
10901         verify_jobstats "$cmd" "mdt"
10902         # rmdir
10903         cmd="rm -fr $DIR/$tfile"
10904         verify_jobstats "$cmd" "mdt"
10905         # mknod
10906         cmd="mknod $DIR/$tfile c 1 3"
10907         verify_jobstats "$cmd" "mdt"
10908         # unlink
10909         cmd="rm -f $DIR/$tfile"
10910         verify_jobstats "$cmd" "mdt"
10911         # open & close
10912         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
10913         verify_jobstats "$cmd" "mdt"
10914         # setattr
10915         cmd="touch $DIR/$tfile"
10916         verify_jobstats "$cmd" "both"
10917         # write
10918         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
10919         verify_jobstats "$cmd" "ost"
10920         # read
10921         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
10922         verify_jobstats "$cmd" "ost"
10923         # truncate
10924         cmd="$TRUNCATE $DIR/$tfile 0"
10925         verify_jobstats "$cmd" "both"
10926         # rename
10927         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
10928         verify_jobstats "$cmd" "mdt"
10929
10930         # cleanup
10931         rm -f $DIR/jobstats_test_rename
10932
10933         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
10934 }
10935 run_test 205 "Verify job stats"
10936
10937 # LU-1480, LU-1773 and LU-1657
10938 test_206() {
10939         mkdir -p $DIR/$tdir
10940         lfs setstripe -c -1 $DIR/$tdir
10941 #define OBD_FAIL_LOV_INIT 0x1403
10942         $LCTL set_param fail_loc=0xa0001403
10943         $LCTL set_param fail_val=1
10944         touch $DIR/$tdir/$tfile || true
10945 }
10946 run_test 206 "fail lov_init_raid0() doesn't lbug"
10947
10948 test_207a() {
10949         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10950         local fsz=`stat -c %s $DIR/$tfile`
10951         cancel_lru_locks mdc
10952
10953         # do not return layout in getattr intent
10954 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
10955         $LCTL set_param fail_loc=0x170
10956         local sz=`stat -c %s $DIR/$tfile`
10957
10958         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
10959
10960         rm -rf $DIR/$tfile
10961 }
10962 run_test 207a "can refresh layout at glimpse"
10963
10964 test_207b() {
10965         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10966         local cksum=`md5sum $DIR/$tfile`
10967         local fsz=`stat -c %s $DIR/$tfile`
10968         cancel_lru_locks mdc
10969         cancel_lru_locks osc
10970
10971         # do not return layout in getattr intent
10972 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
10973         $LCTL set_param fail_loc=0x171
10974
10975         # it will refresh layout after the file is opened but before read issues
10976         echo checksum is "$cksum"
10977         echo "$cksum" |md5sum -c --quiet || error "file differs"
10978
10979         rm -rf $DIR/$tfile
10980 }
10981 run_test 207b "can refresh layout at open"
10982
10983 test_208() {
10984         # FIXME: in this test suite, only RD lease is used. This is okay
10985         # for now as only exclusive open is supported. After generic lease
10986         # is done, this test suite should be revised. - Jinshan
10987
10988         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
10989                 { skip "Need MDS version at least 2.4.52"; return 0; }
10990
10991         echo "==== test 1: verify get lease work"
10992         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
10993
10994         echo "==== test 2: verify lease can be broken by upcoming open"
10995         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
10996         local PID=$!
10997         sleep 1
10998
10999         $MULTIOP $DIR/$tfile oO_RDONLY:c
11000         kill -USR1 $PID && wait $PID || error "break lease error"
11001
11002         echo "==== test 3: verify lease can't be granted if an open already exists"
11003         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11004         local PID=$!
11005         sleep 1
11006
11007         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11008         kill -USR1 $PID && wait $PID || error "open file error"
11009
11010         echo "==== test 4: lease can sustain over recovery"
11011         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11012         PID=$!
11013         sleep 1
11014
11015         fail mds1
11016
11017         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11018
11019         echo "==== test 5: lease broken can't be regained by replay"
11020         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11021         PID=$!
11022         sleep 1
11023
11024         # open file to break lease and then recovery
11025         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11026         fail mds1
11027
11028         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11029
11030         rm -f $DIR/$tfile
11031 }
11032 run_test 208 "Exclusive open"
11033
11034 test_209() {
11035         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11036                 skip_env "must have disp_stripe" && return
11037
11038         touch $DIR/$tfile
11039         sync; sleep 5; sync;
11040
11041         echo 3 > /proc/sys/vm/drop_caches
11042         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11043
11044         # open/close 500 times
11045         for i in $(seq 500); do
11046                 cat $DIR/$tfile
11047         done
11048
11049         echo 3 > /proc/sys/vm/drop_caches
11050         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11051
11052         echo "before: $req_before, after: $req_after"
11053         [ $((req_after - req_before)) -ge 300 ] &&
11054                 error "open/close requests are not freed"
11055         return 0
11056 }
11057 run_test 209 "read-only open/close requests should be freed promptly"
11058
11059 test_212() {
11060         size=`date +%s`
11061         size=$((size % 8192 + 1))
11062         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11063         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11064         rm -f $DIR/f212 $DIR/f212.xyz
11065 }
11066 run_test 212 "Sendfile test ============================================"
11067
11068 test_213() {
11069         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11070         cancel_lru_locks osc
11071         lctl set_param fail_loc=0x8000040f
11072         # generate a read lock
11073         cat $DIR/$tfile > /dev/null
11074         # write to the file, it will try to cancel the above read lock.
11075         cat /etc/hosts >> $DIR/$tfile
11076 }
11077 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11078
11079 test_214() { # for bug 20133
11080         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11081         for (( i=0; i < 340; i++ )) ; do
11082                 touch $DIR/$tdir/d214c/a$i
11083         done
11084
11085         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11086         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11087         ls $DIR/d214c || error "ls $DIR/d214c failed"
11088         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11089         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11090 }
11091 run_test 214 "hash-indexed directory test - bug 20133"
11092
11093 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11094 create_lnet_proc_files() {
11095         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11096         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11097
11098         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11099         rm -f "$TMP/lnet_$1.sys_tmp"
11100 }
11101
11102 # counterpart of create_lnet_proc_files
11103 remove_lnet_proc_files() {
11104         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11105 }
11106
11107 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11108 # 3rd arg as regexp for body
11109 check_lnet_proc_stats() {
11110         local l=$(cat "$TMP/lnet_$1" |wc -l)
11111         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11112
11113         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11114 }
11115
11116 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11117 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11118 # optional and can be regexp for 2nd line (lnet.routes case)
11119 check_lnet_proc_entry() {
11120         local blp=2            # blp stands for 'position of 1st line of body'
11121         [ "$5" = "" ] || blp=3 # lnet.routes case
11122
11123         local l=$(cat "$TMP/lnet_$1" |wc -l)
11124         # subtracting one from $blp because the body can be empty
11125         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11126
11127         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11128                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11129
11130         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11131                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11132
11133         # bail out if any unexpected line happened
11134         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
11135         [ "$?" != 0 ] || error "$2 misformatted"
11136 }
11137
11138 test_215() { # for bugs 18102, 21079, 21517
11139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11140         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11141         local P='[1-9][0-9]*'           # positive numeric
11142         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11143         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11144         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11145         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11146
11147         local L1 # regexp for 1st line
11148         local L2 # regexp for 2nd line (optional)
11149         local BR # regexp for the rest (body)
11150
11151         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11152         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11153         create_lnet_proc_files "stats"
11154         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11155         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11156         remove_lnet_proc_files "stats"
11157
11158         # /proc/sys/lnet/routes should look like this:
11159         # Routing disabled/enabled
11160         # net hops priority state router
11161         # where net is a string like tcp0, hops > 0, priority >= 0,
11162         # state is up/down,
11163         # router is a string like 192.168.1.1@tcp2
11164         L1="^Routing (disabled|enabled)$"
11165         L2="^net +hops +priority +state +router$"
11166         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11167         create_lnet_proc_files "routes"
11168         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11169         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11170         remove_lnet_proc_files "routes"
11171
11172         # /proc/sys/lnet/routers should look like this:
11173         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11174         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11175         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11176         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11177         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11178         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11179         create_lnet_proc_files "routers"
11180         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11181         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11182         remove_lnet_proc_files "routers"
11183
11184         # /proc/sys/lnet/peers should look like this:
11185         # nid refs state last max rtr min tx min queue
11186         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11187         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11188         # numeric (0 or >0 or <0), queue >= 0.
11189         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11190         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11191         create_lnet_proc_files "peers"
11192         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11193         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11194         remove_lnet_proc_files "peers"
11195
11196         # /proc/sys/lnet/buffers  should look like this:
11197         # pages count credits min
11198         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11199         L1="^pages +count +credits +min$"
11200         BR="^ +$N +$N +$I +$I$"
11201         create_lnet_proc_files "buffers"
11202         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11203         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11204         remove_lnet_proc_files "buffers"
11205
11206         # /proc/sys/lnet/nis should look like this:
11207         # nid status alive refs peer rtr max tx min
11208         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11209         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11210         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11211         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11212         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11213         create_lnet_proc_files "nis"
11214         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11215         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11216         remove_lnet_proc_files "nis"
11217
11218         # can we successfully write to /proc/sys/lnet/stats?
11219         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11220         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11221 }
11222 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11223
11224 test_216() { # bug 20317
11225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11226         remote_ost_nodsh && skip "remote OST with nodsh" && return
11227
11228         local node
11229         local facets=$(get_facets OST)
11230         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11231
11232         save_lustre_params client "osc.*.contention_seconds" > $p
11233         save_lustre_params $facets \
11234                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11235         save_lustre_params $facets \
11236                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11237         save_lustre_params $facets \
11238                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11239         clear_osc_stats
11240
11241         # agressive lockless i/o settings
11242         for node in $(osts_nodes); do
11243                 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'
11244         done
11245         lctl set_param -n osc.*.contention_seconds 60
11246
11247         $DIRECTIO write $DIR/$tfile 0 10 4096
11248         $CHECKSTAT -s 40960 $DIR/$tfile
11249
11250         # disable lockless i/o
11251         for node in $(osts_nodes); do
11252                 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'
11253         done
11254         lctl set_param -n osc.*.contention_seconds 0
11255         clear_osc_stats
11256
11257         dd if=/dev/zero of=$DIR/$tfile count=0
11258         $CHECKSTAT -s 0 $DIR/$tfile
11259
11260         restore_lustre_params <$p
11261         rm -f $p
11262         rm $DIR/$tfile
11263 }
11264 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11265
11266 test_217() { # bug 22430
11267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11268         local node
11269         local nid
11270
11271         for node in $(nodes_list); do
11272                 nid=$(host_nids_address $node $NETTYPE)
11273                 if [[ $nid = *-* ]] ; then
11274                         echo "lctl ping $nid@$NETTYPE"
11275                         lctl ping $nid@$NETTYPE
11276                 else
11277                         echo "skipping $node (no hyphen detected)"
11278                 fi
11279         done
11280 }
11281 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11282
11283 test_218() {
11284        # do directio so as not to populate the page cache
11285        log "creating a 10 Mb file"
11286        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11287        log "starting reads"
11288        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11289        log "truncating the file"
11290        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11291        log "killing dd"
11292        kill %+ || true # reads might have finished
11293        echo "wait until dd is finished"
11294        wait
11295        log "removing the temporary file"
11296        rm -rf $DIR/$tfile || error "tmp file removal failed"
11297 }
11298 run_test 218 "parallel read and truncate should not deadlock ======================="
11299
11300 test_219() {
11301         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11302         # write one partial page
11303         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11304         # set no grant so vvp_io_commit_write will do sync write
11305         $LCTL set_param fail_loc=0x411
11306         # write a full page at the end of file
11307         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11308
11309         $LCTL set_param fail_loc=0
11310         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11311         $LCTL set_param fail_loc=0x411
11312         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11313
11314         # LU-4201
11315         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11316         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11317 }
11318 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11319
11320 test_220() { #LU-325
11321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11322         remote_ost_nodsh && skip "remote OST with nodsh" && return
11323         local OSTIDX=0
11324
11325         test_mkdir -p $DIR/$tdir
11326         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11327                 awk '{print $2}' | sed -e 's/_UUID$//')
11328
11329         # on the mdt's osc
11330         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11331         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11332                         osc.$mdtosc_proc1.prealloc_last_id)
11333         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11334                         osc.$mdtosc_proc1.prealloc_next_id)
11335
11336         $LFS df -i
11337
11338         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11339         #define OBD_FAIL_OST_ENOINO              0x229
11340         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11341         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11342         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11343
11344         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11345
11346         MDSOBJS=$((last_id - next_id))
11347         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11348
11349         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11350         echo "OST still has $count kbytes free"
11351
11352         echo "create $MDSOBJS files @next_id..."
11353         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11354
11355         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11356                         osc.$mdtosc_proc1.prealloc_last_id)
11357         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11358                         osc.$mdtosc_proc1.prealloc_next_id)
11359
11360         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11361         $LFS df -i
11362
11363         echo "cleanup..."
11364
11365         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11366         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11367
11368         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11369         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11370         echo "unlink $MDSOBJS files @$next_id..."
11371         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11372 }
11373 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11374
11375 test_221() {
11376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11377         dd if=`which date` of=$MOUNT/date oflag=sync
11378         chmod +x $MOUNT/date
11379
11380         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11381         $LCTL set_param fail_loc=0x80001401
11382
11383         $MOUNT/date > /dev/null
11384         rm -f $MOUNT/date
11385 }
11386 run_test 221 "make sure fault and truncate race to not cause OOM"
11387
11388 test_222a () {
11389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11390        rm -rf $DIR/$tdir
11391        test_mkdir -p $DIR/$tdir
11392        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11393        createmany -o $DIR/$tdir/$tfile 10
11394        cancel_lru_locks mdc
11395        cancel_lru_locks osc
11396        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11397        $LCTL set_param fail_loc=0x31a
11398        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11399        $LCTL set_param fail_loc=0
11400        rm -r $DIR/$tdir
11401 }
11402 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11403
11404 test_222b () {
11405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11406        rm -rf $DIR/$tdir
11407        test_mkdir -p $DIR/$tdir
11408        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11409        createmany -o $DIR/$tdir/$tfile 10
11410        cancel_lru_locks mdc
11411        cancel_lru_locks osc
11412        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11413        $LCTL set_param fail_loc=0x31a
11414        rm -r $DIR/$tdir || "AGL for rmdir failed"
11415        $LCTL set_param fail_loc=0
11416 }
11417 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11418
11419 test_223 () {
11420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11421        rm -rf $DIR/$tdir
11422        test_mkdir -p $DIR/$tdir
11423        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11424        createmany -o $DIR/$tdir/$tfile 10
11425        cancel_lru_locks mdc
11426        cancel_lru_locks osc
11427        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11428        $LCTL set_param fail_loc=0x31b
11429        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11430        $LCTL set_param fail_loc=0
11431        rm -r $DIR/$tdir
11432 }
11433 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11434
11435 test_224a() { # LU-1039, MRP-303
11436         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11437         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11438         $LCTL set_param fail_loc=0x508
11439         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11440         $LCTL set_param fail_loc=0
11441         df $DIR
11442 }
11443 run_test 224a "Don't panic on bulk IO failure"
11444
11445 test_224b() { # LU-1039, MRP-303
11446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11447         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11448         cancel_lru_locks osc
11449         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11450         $LCTL set_param fail_loc=0x515
11451         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11452         $LCTL set_param fail_loc=0
11453         df $DIR
11454 }
11455 run_test 224b "Don't panic on bulk IO failure"
11456
11457 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11458 test_225a () {
11459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11460         if [ -z ${MDSSURVEY} ]; then
11461               skip_env "mds-survey not found" && return
11462         fi
11463
11464         [ $MDSCOUNT -ge 2 ] &&
11465                 skip "skipping now for more than one MDT" && return
11466
11467        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11468             { skip "Need MDS version at least 2.2.51"; return; }
11469
11470        local mds=$(facet_host $SINGLEMDS)
11471        local target=$(do_nodes $mds 'lctl dl' | \
11472                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11473
11474        local cmd1="file_count=1000 thrhi=4"
11475        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11476        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11477        local cmd="$cmd1 $cmd2 $cmd3"
11478
11479        rm -f ${TMP}/mds_survey*
11480        echo + $cmd
11481        eval $cmd || error "mds-survey with zero-stripe failed"
11482        cat ${TMP}/mds_survey*
11483        rm -f ${TMP}/mds_survey*
11484 }
11485 run_test 225a "Metadata survey sanity with zero-stripe"
11486
11487 test_225b () {
11488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11489
11490         if [ -z ${MDSSURVEY} ]; then
11491               skip_env "mds-survey not found" && return
11492         fi
11493         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11494             { skip "Need MDS version at least 2.2.51"; return; }
11495
11496         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11497               skip_env "Need to mount OST to test" && return
11498         fi
11499
11500         [ $MDSCOUNT -ge 2 ] &&
11501                 skip "skipping now for more than one MDT" && return
11502        local mds=$(facet_host $SINGLEMDS)
11503        local target=$(do_nodes $mds 'lctl dl' | \
11504                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11505
11506        local cmd1="file_count=1000 thrhi=4"
11507        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11508        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11509        local cmd="$cmd1 $cmd2 $cmd3"
11510
11511        rm -f ${TMP}/mds_survey*
11512        echo + $cmd
11513        eval $cmd || error "mds-survey with stripe_count failed"
11514        cat ${TMP}/mds_survey*
11515        rm -f ${TMP}/mds_survey*
11516 }
11517 run_test 225b "Metadata survey sanity with stripe_count = 1"
11518
11519 mcreate_path2fid () {
11520         local mode=$1
11521         local major=$2
11522         local minor=$3
11523         local name=$4
11524         local desc=$5
11525         local path=$DIR/$tdir/$name
11526         local fid
11527         local rc
11528         local fid_path
11529
11530         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11531                 error "cannot create $desc"
11532
11533         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11534         rc=$?
11535         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11536
11537         fid_path=$($LFS fid2path $MOUNT $fid)
11538         rc=$?
11539         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11540
11541         [ "$path" == "$fid_path" ] ||
11542                 error "fid2path returned $fid_path, expected $path"
11543
11544         echo "pass with $path and $fid"
11545 }
11546
11547 test_226a () {
11548         rm -rf $DIR/$tdir
11549         mkdir -p $DIR/$tdir
11550
11551         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11552         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11553         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11554         mcreate_path2fid 0040666 0 0 dir "directory"
11555         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11556         mcreate_path2fid 0100666 0 0 file "regular file"
11557         mcreate_path2fid 0120666 0 0 link "symbolic link"
11558         mcreate_path2fid 0140666 0 0 sock "socket"
11559 }
11560 run_test 226a "call path2fid and fid2path on files of all type"
11561
11562 test_226b () {
11563         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11564         rm -rf $DIR/$tdir
11565         local MDTIDX=1
11566
11567         mkdir -p $DIR/$tdir
11568         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11569                 error "create remote directory failed"
11570         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11571         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11572                                 "character special file (null)"
11573         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11574                                 "character special file (no device)"
11575         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11576         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11577                                 "block special file (loop)"
11578         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11579         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11580         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11581 }
11582 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11583
11584 # LU-1299 Executing or running ldd on a truncated executable does not
11585 # cause an out-of-memory condition.
11586 test_227() {
11587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11588         dd if=`which date` of=$MOUNT/date bs=1k count=1
11589         chmod +x $MOUNT/date
11590
11591         $MOUNT/date > /dev/null
11592         ldd $MOUNT/date > /dev/null
11593         rm -f $MOUNT/date
11594 }
11595 run_test 227 "running truncated executable does not cause OOM"
11596
11597 # LU-1512 try to reuse idle OI blocks
11598 test_228a() {
11599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11600         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11601                 skip "non-ldiskfs backend" && return
11602
11603         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11604         local myDIR=$DIR/$tdir
11605
11606         mkdir -p $myDIR
11607         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11608         $LCTL set_param fail_loc=0x80001002
11609         createmany -o $myDIR/t- 10000
11610         $LCTL set_param fail_loc=0
11611         # The guard is current the largest FID holder
11612         touch $myDIR/guard
11613         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11614                     tr -d '[')
11615         local IDX=$(($SEQ % 64))
11616
11617         do_facet $SINGLEMDS sync
11618         # Make sure journal flushed.
11619         sleep 6
11620         local blk1=$(do_facet $SINGLEMDS \
11621                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11622                      grep Blockcount | awk '{print $4}')
11623
11624         # Remove old files, some OI blocks will become idle.
11625         unlinkmany $myDIR/t- 10000
11626         # Create new files, idle OI blocks should be reused.
11627         createmany -o $myDIR/t- 2000
11628         do_facet $SINGLEMDS sync
11629         # Make sure journal flushed.
11630         sleep 6
11631         local blk2=$(do_facet $SINGLEMDS \
11632                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11633                      grep Blockcount | awk '{print $4}')
11634
11635         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11636 }
11637 run_test 228a "try to reuse idle OI blocks"
11638
11639 test_228b() {
11640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11641         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11642                 skip "non-ldiskfs backend" && return
11643
11644         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11645         local myDIR=$DIR/$tdir
11646
11647         mkdir -p $myDIR
11648         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11649         $LCTL set_param fail_loc=0x80001002
11650         createmany -o $myDIR/t- 10000
11651         $LCTL set_param fail_loc=0
11652         # The guard is current the largest FID holder
11653         touch $myDIR/guard
11654         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11655                     tr -d '[')
11656         local IDX=$(($SEQ % 64))
11657
11658         do_facet $SINGLEMDS sync
11659         # Make sure journal flushed.
11660         sleep 6
11661         local blk1=$(do_facet $SINGLEMDS \
11662                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11663                      grep Blockcount | awk '{print $4}')
11664
11665         # Remove old files, some OI blocks will become idle.
11666         unlinkmany $myDIR/t- 10000
11667
11668         # stop the MDT
11669         stop $SINGLEMDS || error "Fail to stop MDT."
11670         # remount the MDT
11671         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11672
11673         df $MOUNT || error "Fail to df."
11674         # Create new files, idle OI blocks should be reused.
11675         createmany -o $myDIR/t- 2000
11676         do_facet $SINGLEMDS sync
11677         # Make sure journal flushed.
11678         sleep 6
11679         local blk2=$(do_facet $SINGLEMDS \
11680                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11681                      grep Blockcount | awk '{print $4}')
11682
11683         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11684 }
11685 run_test 228b "idle OI blocks can be reused after MDT restart"
11686
11687 #LU-1881
11688 test_228c() {
11689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11690         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11691                 skip "non-ldiskfs backend" && return
11692
11693         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11694         local myDIR=$DIR/$tdir
11695
11696         mkdir -p $myDIR
11697         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11698         $LCTL set_param fail_loc=0x80001002
11699         # 20000 files can guarantee there are index nodes in the OI file
11700         createmany -o $myDIR/t- 20000
11701         $LCTL set_param fail_loc=0
11702         # The guard is current the largest FID holder
11703         touch $myDIR/guard
11704         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11705                     tr -d '[')
11706         local IDX=$(($SEQ % 64))
11707
11708         do_facet $SINGLEMDS sync
11709         # Make sure journal flushed.
11710         sleep 6
11711         local blk1=$(do_facet $SINGLEMDS \
11712                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11713                      grep Blockcount | awk '{print $4}')
11714
11715         # Remove old files, some OI blocks will become idle.
11716         unlinkmany $myDIR/t- 20000
11717         rm -f $myDIR/guard
11718         # The OI file should become empty now
11719
11720         # Create new files, idle OI blocks should be reused.
11721         createmany -o $myDIR/t- 2000
11722         do_facet $SINGLEMDS sync
11723         # Make sure journal flushed.
11724         sleep 6
11725         local blk2=$(do_facet $SINGLEMDS \
11726                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11727                      grep Blockcount | awk '{print $4}')
11728
11729         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11730 }
11731 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
11732
11733 test_229() { # LU-2482, LU-3448
11734         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
11735                 skip "No HSM support on MDS of $(get_lustre_version)," \
11736                          "need 2.4.53 at least" && return
11737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11738         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
11739
11740         rm -f $DIR/$tfile
11741
11742         # Create a file with a released layout and stripe count 2.
11743         $MULTIOP $DIR/$tfile H2c ||
11744                 error "failed to create file with released layout"
11745
11746         $GETSTRIPE -v $DIR/$tfile
11747
11748         local pattern=$($GETSTRIPE -L $DIR/$tfile)
11749         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
11750
11751         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
11752         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
11753         stat $DIR/$tfile || error "failed to stat released file"
11754
11755         chown $RUNAS_ID $DIR/$tfile ||
11756                 error "chown $RUNAS_ID $DIR/$tfile failed"
11757
11758         chgrp $RUNAS_ID $DIR/$tfile ||
11759                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
11760
11761         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
11762         rm $DIR/$tfile || error "failed to remove released file"
11763 }
11764 run_test 229 "getstripe/stat/rm/attr changes work on released files"
11765
11766 test_230a() {
11767         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11768         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11769         local MDTIDX=1
11770
11771         mkdir -p $DIR/$tdir/test_230_local
11772         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
11773         [ $mdt_idx -ne 0 ] &&
11774                 error "create local directory on wrong MDT $mdt_idx"
11775
11776         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
11777                         error "create remote directory failed"
11778         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
11779         [ $mdt_idx -ne $MDTIDX ] &&
11780                 error "create remote directory on wrong MDT $mdt_idx"
11781
11782         createmany -o $DIR/$tdir/test_230/t- 10 ||
11783                 error "create files on remote directory failed"
11784         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
11785         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
11786         rm -r $DIR/$tdir || error "unlink remote directory failed"
11787 }
11788 run_test 230a "Create remote directory and files under the remote directory"
11789
11790 test_231a()
11791 {
11792         # For simplicity this test assumes that max_pages_per_rpc
11793         # is the same across all OSCs
11794         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
11795         local bulk_size=$((max_pages * 4096))
11796
11797         mkdir -p $DIR/$tdir
11798
11799         # clear the OSC stats
11800         $LCTL set_param osc.*.stats=0 &>/dev/null
11801
11802         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
11803         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
11804                 oflag=direct &>/dev/null || error "dd failed"
11805
11806         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
11807         if [ x$nrpcs != "x1" ]; then
11808                 error "found $nrpc ost_write RPCs, not 1 as expected"
11809         fi
11810
11811         # Drop the OSC cache, otherwise we will read from it
11812         cancel_lru_locks osc
11813
11814         # clear the OSC stats
11815         $LCTL set_param osc.*.stats=0 &>/dev/null
11816
11817         # Client reads $bulk_size.
11818         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
11819                 iflag=direct &>/dev/null || error "dd failed"
11820
11821         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
11822         if [ x$nrpcs != "x1" ]; then
11823                 error "found $nrpc ost_read RPCs, not 1 as expected"
11824         fi
11825 }
11826 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
11827
11828 test_231b() {
11829         mkdir -p $DIR/$tdir
11830         local i
11831         for i in {0..1023}; do
11832                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
11833                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
11834                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
11835         done
11836         sync
11837 }
11838 run_test 231b "must not assert on fully utilized OST request buffer"
11839
11840 test_232() {
11841         mkdir -p $DIR/$tdir
11842         #define OBD_FAIL_LDLM_OST_LVB            0x31c
11843         $LCTL set_param fail_loc=0x31c
11844
11845         # ignore dd failure
11846         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
11847
11848         $LCTL set_param fail_loc=0
11849         umount_client $MOUNT || error "umount failed"
11850         mount_client $MOUNT || error "mount failed"
11851 }
11852 run_test 232 "failed lock should not block umount"
11853
11854 test_233a() {
11855         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
11856         { skip "Need MDS version at least 2.3.64"; return; }
11857
11858         local fid=$($LFS path2fid $MOUNT)
11859         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11860                 error "cannot access $MOUNT using its FID '$fid'"
11861 }
11862 run_test 233a "checking that OBF of the FS root succeeds"
11863
11864 test_233b() {
11865         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
11866         { skip "Need MDS version at least 2.5.90"; return; }
11867
11868         local fid=$($LFS path2fid $MOUNT/.lustre)
11869         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11870                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
11871
11872         fid=$($LFS path2fid $MOUNT/.lustre/fid)
11873         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11874                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
11875 }
11876 run_test 233b "checking that OBF of the FS .lustre succeeds"
11877
11878 test_234() {
11879         local p="$TMP/sanityN-$TESTNAME.parameters"
11880         save_lustre_params client "llite.*.xattr_cache" > $p
11881         lctl set_param llite.*.xattr_cache 1 ||
11882                 { skip "xattr cache is not supported"; return 0; }
11883
11884         mkdir -p $DIR/$tdir || error "mkdir failed"
11885         touch $DIR/$tdir/$tfile || error "touch failed"
11886         # OBD_FAIL_LLITE_XATTR_ENOMEM
11887         $LCTL set_param fail_loc=0x1405
11888         if [ ! -f /etc/SuSE-release ]; then
11889                 # attr pre-2.4.44-7 had a bug with rc
11890                 # LU-3703 - SLES clients have older attr
11891                 getfattr -n user.attr $DIR/$tdir/$tfile &&
11892                         error "getfattr should have failed with ENOMEM"
11893         fi
11894         $LCTL set_param fail_loc=0x0
11895         rm -rf $DIR/$tdir
11896
11897         restore_lustre_params < $p
11898         rm -f $p
11899 }
11900 run_test 234 "xattr cache should not crash on ENOMEM"
11901
11902 test_235() {
11903          [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
11904                 skip "Need MDS version at least 2.4.52" && return
11905         flock_deadlock $DIR/$tfile
11906         local RC=$?
11907         case $RC in
11908                 0)
11909                 ;;
11910                 124) error "process hangs on a deadlock"
11911                 ;;
11912                 *) error "error executing flock_deadlock $DIR/$tfile"
11913                 ;;
11914         esac
11915 }
11916 run_test 235 "LU-1715: flock deadlock detection does not work properly"
11917
11918 #LU-2935
11919 test_236() {
11920         check_swap_layouts_support && return 0
11921         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
11922
11923         local ref1=/etc/passwd
11924         local ref2=/etc/group
11925         local file1=$DIR/$tdir/f1
11926         local file2=$DIR/$tdir/f2
11927
11928         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
11929         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
11930         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
11931         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
11932         local fd=$(free_fd)
11933         local cmd="exec $fd<>$file2"
11934         eval $cmd
11935         rm $file2
11936         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
11937                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
11938         cmd="exec $fd>&-"
11939         eval $cmd
11940         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11941
11942         #cleanup
11943         rm -rf $DIR/$tdir
11944 }
11945 run_test 236 "Layout swap on open unlinked file"
11946
11947 # test to verify file handle related system calls
11948 # (name_to_handle_at/open_by_handle_at)
11949 # The new system calls are supported in glibc >= 2.14.
11950
11951 test_237() {
11952         echo "Test file_handle syscalls" > $DIR/$tfile
11953         check_fhandle_syscalls $DIR/$tfile ||
11954                 error "check_fhandle_syscalls failed"
11955 }
11956 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
11957
11958 test_striped_dir() {
11959         local mdt_index=$1
11960         local stripe_count
11961         local stripe_index
11962
11963         mkdir -p $DIR/$tdir
11964         $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
11965                 error "set striped dir error"
11966
11967         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
11968         if [ "$stripe_count" != "2" ]; then
11969                 error "stripe_count is $stripe_count, expect 2"
11970         fi
11971
11972         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
11973         if [ "$stripe_index" != "$mdt_index" ]; then
11974                 error "stripe_index is $stripe_index, expect $mdt_index"
11975         fi
11976
11977         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
11978                 error "nlink error after create striped dir"
11979
11980         mkdir $DIR/$tdir/striped_dir/a
11981         mkdir $DIR/$tdir/striped_dir/b
11982
11983         stat $DIR/$tdir/striped_dir/a ||
11984                 error "create dir under striped dir failed"
11985         stat $DIR/$tdir/striped_dir/b ||
11986                 error "create dir under striped dir failed"
11987
11988         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
11989                 error "nlink error after mkdir"
11990
11991         rmdir $DIR/$tdir/striped_dir/a
11992         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
11993                 error "nlink error after rmdir"
11994
11995         rmdir $DIR/$tdir/striped_dir/b
11996         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
11997                 error "nlink error after rmdir"
11998
11999         rmdir $DIR/$tdir/striped_dir ||
12000                 error "rmdir striped dir error"
12001         true
12002 }
12003
12004 test_300a() {
12005         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12006         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12007
12008         test_striped_dir 0 || error "failed on striped dir on MDT0"
12009         test_striped_dir 1 || error "failed on striped dir on MDT0"
12010 }
12011 run_test 300a "basic striped dir sanity test"
12012
12013 test_300b() {
12014         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12015         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12016         local i
12017         local mtime1
12018         local mtime2
12019         local mtime3
12020
12021         test_mkdir $DIR/$tdir || error "mkdir fail"
12022         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12023                 error "set striped dir error"
12024         for ((i=0; i<10; i++)); do
12025                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12026                 sleep 1
12027                 touch $DIR/$tdir/striped_dir/file_$i ||
12028                                         error "touch error $i"
12029                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12030                 [ $mtime1 -eq $mtime2 ] &&
12031                         error "mtime not change after create"
12032                 sleep 1
12033                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12034                                         error "unlink error $i"
12035                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12036                 [ $mtime2 -eq $mtime3 ] &&
12037                         error "mtime did not change after unlink"
12038         done
12039         true
12040 }
12041 run_test 300b "check ctime/mtime for striped dir"
12042
12043 test_300c() {
12044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12045         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12046         local file_count
12047
12048         mkdir -p $DIR/$tdir
12049         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12050                 error "set striped dir error"
12051
12052         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12053                 error "chown striped dir failed"
12054
12055         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12056                 error "create 5k files failed"
12057
12058         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12059
12060         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12061
12062         rm -rf $DIR/$tdir
12063 }
12064 run_test 300c "chown && check ls under striped directory"
12065
12066 test_300d() {
12067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12068         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12069         local stripe_count
12070         local file
12071
12072         mkdir -p $DIR/$tdir
12073         $SETSTRIPE -c 2 $DIR/$tdir
12074
12075         #local striped directory
12076         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12077                 error "set striped dir error"
12078         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12079                 error "create 10 files failed"
12080
12081         #remote striped directory
12082         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12083                 error "set striped dir error"
12084         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12085                 error "create 10 files failed"
12086
12087         for file in $(find $DIR/$tdir); do
12088                 stripe_count=$($GETSTRIPE -c $file)
12089                 [ $stripe_count -eq 2 ] ||
12090                         error "wrong stripe $stripe_count for $file"
12091         done
12092
12093         rm -rf $DIR/$tdir
12094 }
12095 run_test 300d "check default stripe under striped directory"
12096
12097 test_300e() {
12098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12099         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12100         local stripe_count
12101         local file
12102
12103         mkdir -p $DIR/$tdir
12104
12105         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12106                 error "set striped dir error"
12107
12108         touch $DIR/$tdir/striped_dir/a
12109         touch $DIR/$tdir/striped_dir/b
12110         touch $DIR/$tdir/striped_dir/c
12111
12112         mkdir $DIR/$tdir/striped_dir/dir_a
12113         mkdir $DIR/$tdir/striped_dir/dir_b
12114         mkdir $DIR/$tdir/striped_dir/dir_c
12115
12116         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12117                 error "set striped dir under striped dir error"
12118
12119         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12120                 error "set striped dir under striped dir error"
12121
12122         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12123                 error "set striped dir under striped dir error"
12124
12125         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12126                 error "rename file under striped dir should fail"
12127
12128         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12129                 error "rename dir under striped dir should fail"
12130
12131         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12132                 error "rename dir under different stripes should fail"
12133
12134         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12135                 error "rename file under striped dir should succeed"
12136
12137         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12138                 error "rename dir under striped dir should succeed"
12139
12140         rm -rf $DIR/$tdir
12141 }
12142 run_test 300e "check rename under striped directory"
12143
12144 test_300f() {
12145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12146         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12147         local stripe_count
12148         local file
12149
12150         rm -rf $DIR/$tdir
12151         mkdir -p $DIR/$tdir
12152
12153         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12154                 error "set striped dir error"
12155
12156         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12157                 error "set striped dir error"
12158
12159         touch $DIR/$tdir/striped_dir/a
12160         mkdir $DIR/$tdir/striped_dir/dir_a
12161         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12162                 error "create striped dir under striped dir fails"
12163
12164         touch $DIR/$tdir/striped_dir1/b
12165         mkdir $DIR/$tdir/striped_dir1/dir_b
12166         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12167                 error "create striped dir under striped dir fails"
12168
12169         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12170                 error "rename file under different striped dir should fail"
12171
12172         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12173                 error "rename dir under different striped dir should fail"
12174
12175         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12176                 error "rename striped dir under diff striped dir should fail"
12177
12178         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12179                 error "rename file under diff striped dirs fails"
12180
12181         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12182                 error "rename dir under diff striped dirs fails"
12183
12184         rm -rf $DIR/$tdir
12185 }
12186 run_test 300f "check rename cross striped directory"
12187
12188 #
12189 # tests that do cleanup/setup should be run at the end
12190 #
12191
12192 test_900() {
12193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12194         local ls
12195         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12196         $LCTL set_param fail_loc=0x903
12197         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12198         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12199                 echo "clear" > $ls
12200         done
12201         FAIL_ON_ERROR=true cleanup
12202         FAIL_ON_ERROR=true setup
12203 }
12204 run_test 900 "umount should not race with any mgc requeue thread"
12205
12206 complete $SECONDS
12207 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
12208 check_and_cleanup_lustre
12209 if [ "$I_MOUNTED" != "yes" ]; then
12210         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12211 fi
12212 exit_status