Whamcloud - gitweb
LU-4471 mdd: mdd_unlink: do trans_start after sanity check
[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 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
197 test_4() {
198         local MDTIDX=1
199         local remote_dir=remote_dir
200
201         test_mkdir $DIR/$remote_dir ||
202                 error "Create remote directory failed"
203
204         touch $DIR/$remote_dir/$tfile ||
205                 error "Create file under remote directory failed"
206
207         rmdir $DIR/$remote_dir &&
208                 error "Expect error removing in-use dir $DIR/$remote_dir"
209
210         test -d $DIR/$remote_dir || error "Remote directory disappeared"
211 }
212 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
213
214 test_5() {
215         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
216         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
217         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
218         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
220 }
221 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
222
223 test_6a() {
224         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
225         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
226         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
227                 error "$tfile does not have perm 0666 or UID $UID"
228         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile should be 0666 and owned by UID $UID"
231 }
232 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
233
234 test_6c() {
235         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
236         touch $DIR/$tfile
237         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
238         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
239                 error "$tfile should be owned by UID $RUNAS_ID"
240         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243 }
244 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
245
246 test_6e() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
248         touch $DIR/$tfile
249         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
250         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
251                 error "$tfile should be owned by GID $UID"
252         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
255 }
256 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
257
258 test_6g() {
259         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
260         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
261         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
262         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
263         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
264         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
265         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
266                 error "$tdir/d/subdir should be GID $RUNAS_GID"
267 }
268 run_test 6g "Is new dir in sgid dir inheriting group?"
269
270 test_6h() { # bug 7331
271         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
272         touch $DIR/$tfile || error "touch failed"
273         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
274         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
275                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
276         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
277                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
278 }
279 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
280
281 test_7a() {
282         test_mkdir $DIR/$tdir
283         $MCREATE $DIR/$tdir/$tfile
284         chmod 0666 $DIR/$tdir/$tfile
285         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
286                 error "$tdir/$tfile should be mode 0666"
287 }
288 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
289
290 test_7b() {
291         if [ ! -d $DIR/$tdir ]; then
292                 mkdir $DIR/$tdir
293         fi
294         $MCREATE $DIR/$tdir/$tfile
295         echo -n foo > $DIR/$tdir/$tfile
296         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
297         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
298 }
299 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
300
301 test_8() {
302         test_mkdir $DIR/$tdir
303         touch $DIR/$tdir/$tfile
304         chmod 0666 $DIR/$tdir/$tfile
305         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
306                 error "$tfile mode not 0666"
307 }
308 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
309
310 test_9() {
311         test_mkdir $DIR/$tdir
312         test_mkdir $DIR/$tdir/d2
313         test_mkdir $DIR/$tdir/d2/d3
314         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
315 }
316 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
317
318 test_10() {
319         test_mkdir $DIR/$tdir
320         test_mkdir $DIR/$tdir/d2
321         touch $DIR/$tdir/d2/$tfile
322         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
323                 error "$tdir/d2/$tfile not a file"
324 }
325 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
326
327 test_11() {
328         test_mkdir $DIR/$tdir
329         test_mkdir $DIR/$tdir/d2
330         chmod 0666 $DIR/$tdir/d2
331         chmod 0705 $DIR/$tdir/d2
332         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
333                 error "$tdir/d2 mode not 0705"
334 }
335 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
336
337 test_12() {
338         test_mkdir $DIR/$tdir
339         touch $DIR/$tdir/$tfile
340         chmod 0666 $DIR/$tdir/$tfile
341         chmod 0654 $DIR/$tdir/$tfile
342         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
343                 error "$tdir/d2 mode not 0654"
344 }
345 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
346
347 test_13() {
348         test_mkdir $DIR/$tdir
349         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
350         >  $DIR/$tdir/$tfile
351         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
352                 error "$tdir/$tfile size not 0 after truncate"
353 }
354 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
355
356 test_14() {
357         test_mkdir $DIR/$tdir
358         touch $DIR/$tdir/$tfile
359         rm $DIR/$tdir/$tfile
360         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
361 }
362 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
363
364 test_15() {
365         test_mkdir $DIR/$tdir
366         touch $DIR/$tdir/$tfile
367         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
368         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
369                 error "$tdir/${tfile_2} not a file after rename"
370 }
371 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
372
373 test_16() {
374         test_mkdir $DIR/$tdir
375         touch $DIR/$tdir/$tfile
376         rm -rf $DIR/$tdir/$tfile
377         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
378 }
379 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
380
381 test_17a() {
382         test_mkdir -p $DIR/$tdir
383         touch $DIR/$tdir/$tfile
384         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
385         ls -l $DIR/$tdir
386         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
387                 error "$tdir/l-exist not a symlink"
388         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
389                 error "$tdir/l-exist not referencing a file"
390         rm -f $DIR/$tdir/l-exist
391         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
392 }
393 run_test 17a "symlinks: create, remove (real) =================="
394
395 test_17b() {
396         test_mkdir -p $DIR/$tdir
397         ln -s no-such-file $DIR/$tdir/l-dangle
398         ls -l $DIR/$tdir
399         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
400                 error "$tdir/l-dangle not referencing no-such-file"
401         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
402                 error "$tdir/l-dangle not referencing non-existent file"
403         rm -f $DIR/$tdir/l-dangle
404         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
405 }
406 run_test 17b "symlinks: create, remove (dangling) =============="
407
408 test_17c() { # bug 3440 - don't save failed open RPC for replay
409         test_mkdir -p $DIR/$tdir
410         ln -s foo $DIR/$tdir/$tfile
411         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
412 }
413 run_test 17c "symlinks: open dangling (should return error) ===="
414
415 test_17d() {
416         test_mkdir -p $DIR/$tdir
417         ln -s foo $DIR/$tdir/$tfile
418         touch $DIR/$tdir/$tfile || error "creating to new symlink"
419 }
420 run_test 17d "symlinks: create dangling ========================"
421
422 test_17e() {
423         test_mkdir -p $DIR/$tdir
424         local foo=$DIR/$tdir/$tfile
425         ln -s $foo $foo || error "create symlink failed"
426         ls -l $foo || error "ls -l failed"
427         ls $foo && error "ls not failed" || true
428 }
429 run_test 17e "symlinks: create recursive symlink (should return error) ===="
430
431 test_17f() {
432         test_mkdir -p $DIR/d17f
433         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
434         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
435         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
436         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
437         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
438         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
439         ls -l  $DIR/d17f
440 }
441 run_test 17f "symlinks: long and very long symlink name ========================"
442
443 # str_repeat(S, N) generate a string that is string S repeated N times
444 str_repeat() {
445         local s=$1
446         local n=$2
447         local ret=''
448         while [ $((n -= 1)) -ge 0 ]; do
449                 ret=$ret$s
450         done
451         echo $ret
452 }
453
454 # Long symlinks and LU-2241
455 test_17g() {
456         test_mkdir -p $DIR/$tdir
457         local TESTS="59 60 61 4094 4095"
458
459         # Fix for inode size boundary in 2.1.4
460         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
461                 TESTS="4094 4095"
462
463         # Patch not applied to 2.2 or 2.3 branches
464         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
465         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
466                 TESTS="4094 4095"
467
468         # skip long symlink name for rhel6.5.
469         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
470         grep -q '6.5' /etc/redhat-release && TESTS="59 60 61 4062 4063"
471
472         for i in $TESTS; do
473                 local SYMNAME=$(str_repeat 'x' $i)
474                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
475                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
476         done
477 }
478 run_test 17g "symlinks: really long symlink name and inode boundaries"
479
480 test_17h() { #bug 17378
481         remote_mds_nodsh && skip "remote MDS with nodsh" && return
482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
483         local mdt_idx
484         test_mkdir -p $DIR/$tdir
485         if [ $MDSCOUNT -gt 1 ]; then
486                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
487         else
488                 mdt_idx=0
489         fi
490         $SETSTRIPE -c -1 $DIR/$tdir
491 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
492         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
493         touch $DIR/$tdir/$tfile || true
494 }
495 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
496
497 test_17i() { #bug 20018
498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
500         test_mkdir -p $DIR/$tdir
501         local foo=$DIR/$tdir/$tfile
502         local mdt_idx
503         if [ $MDSCOUNT -gt 1 ]; then
504                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
505         else
506                 mdt_idx=0
507         fi
508         ln -s $foo $foo || error "create symlink failed"
509 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
510         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
511         ls -l $foo && error "error not detected"
512         return 0
513 }
514 run_test 17i "don't panic on short symlink"
515
516 test_17k() { #bug 22301
517         rsync --help | grep -q xattr ||
518                 skip_env "$(rsync --version| head -1) does not support xattrs"
519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
520         test_mkdir -p $DIR/$tdir
521         test_mkdir -p $DIR/$tdir.new
522         touch $DIR/$tdir/$tfile
523         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
524         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
525                 error "rsync failed with xattrs enabled"
526 }
527 run_test 17k "symlinks: rsync with xattrs enabled ========================="
528
529 test_17l() { # LU-279
530         mkdir -p $DIR/$tdir
531         touch $DIR/$tdir/$tfile
532         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
533         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
534                 # -h to not follow symlinks. -m '' to list all the xattrs.
535                 # grep to remove first line: '# file: $path'.
536                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
537                 do
538                         lgetxattr_size_check $path $xattr ||
539                                 error "lgetxattr_size_check $path $xattr failed"
540                 done
541         done
542 }
543 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
544
545 # LU-1540
546 test_17m() {
547         local short_sym="0123456789"
548         local WDIR=$DIR/${tdir}m
549         local mds_index
550         local devname
551         local cmd
552         local i
553         local rc=0
554
555         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
556         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
557                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
558
559         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
560                 skip "only for ldiskfs MDT" && return 0
561
562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
563
564         mkdir -p $WDIR
565         long_sym=$short_sym
566         # create a long symlink file
567         for ((i = 0; i < 4; ++i)); do
568                 long_sym=${long_sym}${long_sym}
569         done
570
571         echo "create 512 short and long symlink files under $WDIR"
572         for ((i = 0; i < 256; ++i)); do
573                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
574                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
575         done
576
577         echo "erase them"
578         rm -f $WDIR/*
579         sync
580         wait_delete_completed
581
582         echo "recreate the 512 symlink files with a shorter string"
583         for ((i = 0; i < 512; ++i)); do
584                 # rewrite the symlink file with a shorter string
585                 ln -sf ${long_sym} $WDIR/long-$i
586                 ln -sf ${short_sym} $WDIR/short-$i
587         done
588
589         mds_index=$($LFS getstripe -M $WDIR)
590         mds_index=$((mds_index+1))
591         devname=$(mdsdevname $mds_index)
592         cmd="$E2FSCK -fnvd $devname"
593
594         echo "stop and checking mds${mds_index}: $cmd"
595         # e2fsck should not return error
596         stop mds${mds_index}
597         do_facet mds${mds_index} $cmd || rc=$?
598
599         start mds${mds_index} $devname $MDS_MOUNT_OPTS
600         df $MOUNT > /dev/null 2>&1
601         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
602                 "short/long symlink MDT: rc=$rc"
603         return $rc
604 }
605 run_test 17m "run e2fsck against MDT which contains short/long symlink"
606
607 check_fs_consistency_17n() {
608         local mdt_index
609         local devname
610         local cmd
611         local rc=0
612
613         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
614         # so it only check MDT1/MDT2 instead of all of MDTs.
615         for mdt_index in $(seq 1 2); do
616                 devname=$(mdsdevname $mdt_index)
617                 cmd="$E2FSCK -fnvd $devname"
618
619                 echo "stop and checking mds${mdt_index}: $cmd"
620                 # e2fsck should not return error
621                 stop mds${mdt_index}
622                 do_facet mds${mdt_index} $cmd || rc=$?
623
624                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
625                 df $MOUNT > /dev/null 2>&1
626                 [ $rc -ne 0 ] && break
627         done
628         return $rc
629 }
630
631 test_17n() {
632         local i
633
634         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
635         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
636                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
637
638         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
639                 skip "only for ldiskfs MDT" && return 0
640
641         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
642
643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
644
645         mkdir -p $DIR/$tdir
646         for ((i=0; i<10; i++)); do
647                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
648                         error "create remote dir error $i"
649                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
650                         error "create files under remote dir failed $i"
651         done
652
653         check_fs_consistency_17n || error "e2fsck report error"
654
655         for ((i=0;i<10;i++)); do
656                 rm -rf $DIR/$tdir/remote_dir_${i} ||
657                         error "destroy remote dir error $i"
658         done
659
660         check_fs_consistency_17n || error "e2fsck report error"
661 }
662 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
663
664 test_17o() {
665         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ]] &&
666                 skip "Need MDS version at least 2.3.64" && return
667
668         local WDIR=$DIR/${tdir}o
669         local mdt_index
670         local mdtdevname
671         local rc=0
672
673         mkdir -p $WDIR
674         mdt_index=$($LFS getstripe -M $WDIR)
675         mdt_index=$((mdt_index+1))
676         mdtdevname=$(mdsdevname $mdt_index)
677
678         touch $WDIR/$tfile
679         stop mds${mdt_index}
680         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
681
682         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
683         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
684         ls -l $WDIR/$tfile && rc=1
685         do_facet mds${mdt_index} lctl set_param fail_loc=0
686         [[ $rc -ne 0 ]] && error "stat file should fail"
687         true
688 }
689 run_test 17o "stat file with incompat LMA feature"
690
691 test_18() {
692         touch $DIR/f || error "Failed to touch $DIR/f: $?"
693         ls $DIR || error "Failed to ls $DIR: $?"
694 }
695 run_test 18 "touch .../f ; ls ... =============================="
696
697 test_19a() {
698         touch $DIR/$tfile
699         ls -l $DIR
700         rm $DIR/$tfile
701         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
702 }
703 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
704
705 test_19b() {
706         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
707 }
708 run_test 19b "ls -l .../f19 (should return error) =============="
709
710 test_19c() {
711         [ $RUNAS_ID -eq $UID ] &&
712                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
713         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
714 }
715 run_test 19c "$RUNAS touch .../f19 (should return error) =="
716
717 test_19d() {
718         cat $DIR/f19 && error || true
719 }
720 run_test 19d "cat .../f19 (should return error) =============="
721
722 test_20() {
723         touch $DIR/$tfile
724         rm $DIR/$tfile
725         log "1 done"
726         touch $DIR/$tfile
727         rm $DIR/$tfile
728         log "2 done"
729         touch $DIR/$tfile
730         rm $DIR/$tfile
731         log "3 done"
732         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
733 }
734 run_test 20 "touch .../f ; ls -l ... ==========================="
735
736 test_21() {
737         test_mkdir -p $DIR/$tdir
738         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
739         ln -s dangle $DIR/$tdir/link
740         echo foo >> $DIR/$tdir/link
741         cat $DIR/$tdir/dangle
742         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
743         $CHECKSTAT -f -t file $DIR/$tdir/link ||
744                 error "$tdir/link not linked to a file"
745 }
746 run_test 21 "write to dangling link ============================"
747
748 test_22() {
749         WDIR=$DIR/$tdir
750         test_mkdir -p $DIR/$tdir
751         chown $RUNAS_ID:$RUNAS_GID $WDIR
752         (cd $WDIR || error "cd $WDIR failed";
753         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
754         $RUNAS tar xf -)
755         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
756         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
757         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
758 }
759 run_test 22 "unpack tar archive as non-root user ==============="
760
761 # was test_23
762 test_23a() {
763         test_mkdir -p $DIR/$tdir
764         local file=$DIR/$tdir/$tfile
765
766         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
767         openfile -f O_CREAT:O_EXCL $file &&
768                 error "$file recreate succeeded" || true
769 }
770 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
771
772 test_23b() { # bug 18988
773         test_mkdir -p $DIR/$tdir
774         local file=$DIR/$tdir/$tfile
775
776         rm -f $file
777         echo foo > $file || error "write filed"
778         echo bar >> $file || error "append filed"
779         $CHECKSTAT -s 8 $file || error "wrong size"
780         rm $file
781 }
782 run_test 23b "O_APPEND check =========================="
783
784 # rename sanity
785 test_24a() {
786         echo '-- same directory rename'
787         test_mkdir $DIR/$tdir
788         touch $DIR/$tdir/$tfile.1
789         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
790         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
791 }
792 run_test 24a "rename file to non-existent target"
793
794 test_24b() {
795         test_mkdir $DIR/$tdir
796         touch $DIR/$tdir/$tfile.{1,2}
797         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
798         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
799         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
800 }
801 run_test 24b "rename file to existing target"
802
803 test_24c() {
804         test_mkdir $DIR/$tdir
805         test_mkdir $DIR/$tdir/d$testnum.1
806         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
807         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
808         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
809 }
810 run_test 24c "rename directory to non-existent target"
811
812 test_24d() {
813         test_mkdir -c1 $DIR/$tdir
814         test_mkdir -c1 $DIR/$tdir/d$testnum.1
815         test_mkdir -c1 $DIR/$tdir/d$testnum.2
816         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
817         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
818         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
819 }
820 run_test 24d "rename directory to existing target"
821
822 test_24e() {
823         echo '-- cross directory renames --'
824         test_mkdir $DIR/R5a
825         test_mkdir $DIR/R5b
826         touch $DIR/R5a/f
827         mv $DIR/R5a/f $DIR/R5b/g
828         $CHECKSTAT -a $DIR/R5a/f || error
829         $CHECKSTAT -t file $DIR/R5b/g || error
830 }
831 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
832
833 test_24f() {
834         test_mkdir $DIR/R6a
835         test_mkdir $DIR/R6b
836         touch $DIR/R6a/f $DIR/R6b/g
837         mv $DIR/R6a/f $DIR/R6b/g
838         $CHECKSTAT -a $DIR/R6a/f || error
839         $CHECKSTAT -t file $DIR/R6b/g || error
840 }
841 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
842
843 test_24g() {
844         test_mkdir $DIR/R7a
845         test_mkdir $DIR/R7b
846         test_mkdir $DIR/R7a/d
847         mv $DIR/R7a/d $DIR/R7b/e
848         $CHECKSTAT -a $DIR/R7a/d || error
849         $CHECKSTAT -t dir $DIR/R7b/e || error
850 }
851 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
852
853 test_24h() {
854         test_mkdir -c1 $DIR/R8a
855         test_mkdir -c1 $DIR/R8b
856         test_mkdir -c1 $DIR/R8a/d
857         test_mkdir -c1 $DIR/R8b/e
858         mrename $DIR/R8a/d $DIR/R8b/e
859         $CHECKSTAT -a $DIR/R8a/d || error
860         $CHECKSTAT -t dir $DIR/R8b/e || error
861 }
862 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
863
864 test_24i() {
865         echo "-- rename error cases"
866         test_mkdir $DIR/R9
867         test_mkdir $DIR/R9/a
868         touch $DIR/R9/f
869         mrename $DIR/R9/f $DIR/R9/a
870         $CHECKSTAT -t file $DIR/R9/f || error
871         $CHECKSTAT -t dir  $DIR/R9/a || error
872         $CHECKSTAT -a $DIR/R9/a/f || error
873 }
874 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
875
876 test_24j() {
877         test_mkdir $DIR/R10
878         mrename $DIR/R10/f $DIR/R10/g
879         $CHECKSTAT -t dir $DIR/R10 || error
880         $CHECKSTAT -a $DIR/R10/f || error
881         $CHECKSTAT -a $DIR/R10/g || error
882 }
883 run_test 24j "source does not exist ============================"
884
885 test_24k() {
886         test_mkdir $DIR/R11a
887         test_mkdir $DIR/R11a/d
888         touch $DIR/R11a/f
889         mv $DIR/R11a/f $DIR/R11a/d
890         $CHECKSTAT -a $DIR/R11a/f || error
891         $CHECKSTAT -t file $DIR/R11a/d/f || error
892 }
893 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
894
895 # bug 2429 - rename foo foo foo creates invalid file
896 test_24l() {
897         f="$DIR/f24l"
898         $MULTIOP $f OcNs || error
899 }
900 run_test 24l "Renaming a file to itself ========================"
901
902 test_24m() {
903         f="$DIR/f24m"
904         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
905         # on ext3 this does not remove either the source or target files
906         # though the "expected" operation would be to remove the source
907         $CHECKSTAT -t file ${f} || error "${f} missing"
908         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
909 }
910 run_test 24m "Renaming a file to a hard link to itself ========="
911
912 test_24n() {
913     f="$DIR/f24n"
914     # this stats the old file after it was renamed, so it should fail
915     touch ${f}
916     $CHECKSTAT ${f}
917     mv ${f} ${f}.rename
918     $CHECKSTAT ${f}.rename
919     $CHECKSTAT -a ${f}
920 }
921 run_test 24n "Statting the old file after renaming (Posix rename 2)"
922
923 test_24o() {
924         check_kernel_version 37 || return 0
925         test_mkdir -p $DIR/d24o
926         rename_many -s random -v -n 10 $DIR/d24o
927 }
928 run_test 24o "rename of files during htree split ==============="
929
930 test_24p() {
931         test_mkdir $DIR/R12a
932         test_mkdir $DIR/R12b
933         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
934         mrename $DIR/R12a $DIR/R12b
935         $CHECKSTAT -a $DIR/R12a || error
936         $CHECKSTAT -t dir $DIR/R12b || error
937         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
938         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
939 }
940 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
941
942 cleanup_multiop_pause() {
943         trap 0
944         kill -USR1 $MULTIPID
945 }
946
947 test_24q() {
948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
949         test_mkdir $DIR/R13a
950         test_mkdir $DIR/R13b
951         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
952         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
953         MULTIPID=$!
954
955         trap cleanup_multiop_pause EXIT
956         mrename $DIR/R13a $DIR/R13b
957         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
958         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
959         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
960         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
961         cleanup_multiop_pause
962         wait $MULTIPID || error "multiop close failed"
963 }
964 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
965
966 test_24r() { #bug 3789
967         test_mkdir $DIR/R14a
968         test_mkdir $DIR/R14a/b
969         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
970         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
971         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
972 }
973 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
974
975 test_24s() {
976         test_mkdir $DIR/R15a
977         test_mkdir $DIR/R15a/b
978         test_mkdir $DIR/R15a/b/c
979         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
980         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
981         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
982 }
983 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
984 test_24t() {
985         test_mkdir $DIR/R16a
986         test_mkdir $DIR/R16a/b
987         test_mkdir $DIR/R16a/b/c
988         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
989         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
990         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
991 }
992 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
993
994 test_24u() { # bug12192
995         rm -rf $DIR/$tfile
996         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
997         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
998 }
999 run_test 24u "create stripe file"
1000
1001 page_size() {
1002         getconf PAGE_SIZE
1003 }
1004
1005 simple_cleanup_common() {
1006         trap 0
1007         rm -rf $DIR/$tdir
1008         wait_delete_completed
1009 }
1010
1011 max_pages_per_rpc() {
1012         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
1013 }
1014
1015 test_24v() {
1016         local NRFILES=100000
1017         local FREE_INODES=$(mdt_free_inodes 0)
1018         [ $FREE_INODES -lt $NRFILES ] && \
1019                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1020                 return
1021
1022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1023         trap simple_cleanup_common EXIT
1024
1025         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1026         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1027
1028         mkdir -p $DIR/$tdir
1029         createmany -m $DIR/$tdir/$tfile $NRFILES
1030
1031         cancel_lru_locks mdc
1032         lctl set_param mdc.*.stats clear
1033
1034         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1035
1036         # LU-5 large readdir
1037         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1038         #               8 bytes for name(filename is mostly 5 in this test) +
1039         #               8 bytes for luda_type
1040         # take into account of overhead in lu_dirpage header and end mark in
1041         # each page, plus one in RPC_NUM calculation.
1042         DIRENT_SIZE=48
1043         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1044         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1045         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1046                                 awk '/^mds_readpage/ {print $2}')
1047         [ $mds_readpage -gt $RPC_NUM ] && \
1048                 error "large readdir doesn't take effect"
1049
1050         simple_cleanup_common
1051 }
1052 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1053
1054 test_24w() { # bug21506
1055         SZ1=234852
1056         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1057         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1058         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1059         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1060         [ "$SZ1" = "$SZ2" ] || \
1061                 error "Error reading at the end of the file $tfile"
1062 }
1063 run_test 24w "Reading a file larger than 4Gb"
1064
1065 test_24x() {
1066         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1068         local MDTIDX=1
1069         local remote_dir=$DIR/$tdir/remote_dir
1070
1071         mkdir -p $DIR/$tdir
1072         $LFS mkdir -i $MDTIDX $remote_dir ||
1073                 error "create remote directory failed"
1074
1075         mkdir -p $DIR/$tdir/src_dir
1076         touch $DIR/$tdir/src_file
1077         mkdir -p $remote_dir/tgt_dir
1078         touch $remote_dir/tgt_file
1079
1080         mrename $remote_dir $DIR/ &&
1081                 error "rename dir cross MDT works!"
1082
1083         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1084                 error "rename dir cross MDT works!"
1085
1086         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1087                 error "rename file cross MDT works!"
1088
1089         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1090                 error "ln file cross MDT should not work!"
1091
1092         rm -rf $DIR/$tdir || error "Can not delete directories"
1093 }
1094 run_test 24x "cross rename/link should be failed"
1095
1096 test_24y() {
1097         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1099         local MDTIDX=1
1100         local remote_dir=$DIR/$tdir/remote_dir
1101
1102         mkdir -p $DIR/$tdir
1103         $LFS mkdir -i $MDTIDX $remote_dir ||
1104                    error "create remote directory failed"
1105
1106         mkdir -p $remote_dir/src_dir
1107         touch $remote_dir/src_file
1108         mkdir -p $remote_dir/tgt_dir
1109         touch $remote_dir/tgt_file
1110
1111         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1112                 error "rename subdir in the same remote dir failed!"
1113
1114         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1115                 error "rename files in the same remote dir failed!"
1116
1117         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1118                 error "link files in the same remote dir failed!"
1119
1120         rm -rf $DIR/$tdir || error "Can not delete directories"
1121 }
1122 run_test 24y "rename/link on the same dir should succeed"
1123
1124 test_24z() {
1125         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1127         local MDTIDX=1
1128         local remote_src=$DIR/$tdir/remote_dir
1129         local remote_tgt=$DIR/$tdir/remote_tgt
1130
1131         mkdir -p $DIR/$tdir
1132         $LFS mkdir -i $MDTIDX $remote_src ||
1133                    error "create remote directory failed"
1134
1135         $LFS mkdir -i $MDTIDX $remote_tgt ||
1136                    error "create remote directory failed"
1137
1138         mrename $remote_src $remote_tgt &&
1139                 error "rename remote dirs should not work!"
1140
1141         # If target dir does not exists, it should succeed
1142         rm -rf $remote_tgt
1143         mrename $remote_src $remote_tgt ||
1144                 error "rename remote dirs(tgt dir does not exists) failed!"
1145
1146         rm -rf $DIR/$tdir || error "Can not delete directories"
1147 }
1148 run_test 24z "rename one remote dir to another remote dir should fail"
1149
1150 test_24A() { # LU-3182
1151         local NFILES=5000
1152
1153         rm -rf $DIR/$tdir
1154         mkdir -p $DIR/$tdir
1155         createmany -m $DIR/$tdir/$tfile $NFILES
1156         local t=`ls $DIR/$tdir | wc -l`
1157         local u=`ls $DIR/$tdir | sort -u | wc -l`
1158         if [ $t -ne $NFILES -o $u -ne $NFILES ] ; then
1159                 error "Expected $NFILES files, got $t ($u unique)"
1160         fi
1161
1162         rm -rf $DIR/$tdir || error "Can not delete directories"
1163 }
1164 run_test 24A "readdir() returns correct number of entries."
1165
1166 test_25a() {
1167         echo '== symlink sanity ============================================='
1168
1169         test_mkdir $DIR/d25
1170         ln -s d25 $DIR/s25
1171         touch $DIR/s25/foo || error
1172 }
1173 run_test 25a "create file in symlinked directory ==============="
1174
1175 test_25b() {
1176         [ ! -d $DIR/d25 ] && test_25a
1177         $CHECKSTAT -t file $DIR/s25/foo || error
1178 }
1179 run_test 25b "lookup file in symlinked directory ==============="
1180
1181 test_26a() {
1182         test_mkdir $DIR/d26
1183         test_mkdir $DIR/d26/d26-2
1184         ln -s d26/d26-2 $DIR/s26
1185         touch $DIR/s26/foo || error
1186 }
1187 run_test 26a "multiple component symlink ======================="
1188
1189 test_26b() {
1190         test_mkdir -p $DIR/d26b/d26-2
1191         ln -s d26b/d26-2/foo $DIR/s26-2
1192         touch $DIR/s26-2 || error
1193 }
1194 run_test 26b "multiple component symlink at end of lookup ======"
1195
1196 test_26c() {
1197         test_mkdir $DIR/d26.2
1198         touch $DIR/d26.2/foo
1199         ln -s d26.2 $DIR/s26.2-1
1200         ln -s s26.2-1 $DIR/s26.2-2
1201         ln -s s26.2-2 $DIR/s26.2-3
1202         chmod 0666 $DIR/s26.2-3/foo
1203 }
1204 run_test 26c "chain of symlinks ================================"
1205
1206 # recursive symlinks (bug 439)
1207 test_26d() {
1208         ln -s d26-3/foo $DIR/d26-3
1209 }
1210 run_test 26d "create multiple component recursive symlink ======"
1211
1212 test_26e() {
1213         [ ! -h $DIR/d26-3 ] && test_26d
1214         rm $DIR/d26-3
1215 }
1216 run_test 26e "unlink multiple component recursive symlink ======"
1217
1218 # recursive symlinks (bug 7022)
1219 test_26f() {
1220         test_mkdir -p $DIR/$tdir
1221         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1222         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1223         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1224         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1225         cd $tfile                || error "cd $tfile failed"
1226         ln -s .. dotdot          || error "ln dotdot failed"
1227         ln -s dotdot/lndir lndir || error "ln lndir failed"
1228         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1229         output=`ls $tfile/$tfile/lndir/bar1`
1230         [ "$output" = bar1 ] && error "unexpected output"
1231         rm -r $tfile             || error "rm $tfile failed"
1232         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1233 }
1234 run_test 26f "rm -r of a directory which has recursive symlink ="
1235
1236 test_27a() {
1237         echo '== stripe sanity =============================================='
1238         test_mkdir -p $DIR/d27 || error "mkdir failed"
1239         $GETSTRIPE $DIR/d27
1240         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1241         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1242         pass
1243         log "== test_27a: write to one stripe file ========================="
1244         cp /etc/hosts $DIR/d27/f0 || error
1245 }
1246 run_test 27a "one stripe file =================================="
1247
1248 test_27b() {
1249         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1250         test_mkdir -p $DIR/d27
1251         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1252         $GETSTRIPE -c $DIR/d27/f01
1253         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1254                 error "two-stripe file doesn't have two stripes"
1255
1256         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1257 }
1258 run_test 27b "create and write to two stripe file"
1259
1260 test_27d() {
1261         test_mkdir -p $DIR/d27
1262         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1263         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1264         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1265 }
1266 run_test 27d "create file with default settings ================"
1267
1268 test_27e() {
1269         test_mkdir -p $DIR/d27
1270         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1271         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1272         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1273 }
1274 run_test 27e "setstripe existing file (should return error) ======"
1275
1276 test_27f() {
1277         test_mkdir -p $DIR/d27
1278         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1279         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1280         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1281 }
1282 run_test 27f "setstripe with bad stripe size (should return error)"
1283
1284 test_27g() {
1285         test_mkdir -p $DIR/d27
1286         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1287         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1288                 error "$DIR/d27/fnone has object"
1289 }
1290 run_test 27g "$GETSTRIPE with no objects"
1291
1292 test_27i() {
1293         touch $DIR/d27/fsome || error "touch failed"
1294         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1295 }
1296 run_test 27i "$GETSTRIPE with some objects"
1297
1298 test_27j() {
1299         test_mkdir -p $DIR/d27
1300         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1301 }
1302 run_test 27j "setstripe with bad stripe offset (should return error)"
1303
1304 test_27k() { # bug 2844
1305         test_mkdir -p $DIR/d27
1306         FILE=$DIR/d27/f27k
1307         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1308         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1309         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1310         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1311         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1312         dd if=/dev/zero of=$FILE bs=4k count=1
1313         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1314         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1315 }
1316 run_test 27k "limit i_blksize for broken user apps ============="
1317
1318 test_27l() {
1319         test_mkdir -p $DIR/d27
1320         mcreate $DIR/f27l || error "creating file"
1321         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1322                 error "setstripe should have failed" || true
1323 }
1324 run_test 27l "check setstripe permissions (should return error)"
1325
1326 test_27m() {
1327         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1328                 return
1329         if [ $ORIGFREE -gt $MAXFREE ]; then
1330                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1331                 return
1332         fi
1333         trap simple_cleanup_common EXIT
1334         test_mkdir -p $DIR/$tdir
1335         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1336         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1337                 error "dd should fill OST0"
1338         i=2
1339         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1340                 i=`expr $i + 1`
1341                 [ $i -gt 256 ] && break
1342         done
1343         i=`expr $i + 1`
1344         touch $DIR/$tdir/f27m_$i
1345         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1346                 error "OST0 was full but new created file still use it"
1347         i=`expr $i + 1`
1348         touch $DIR/$tdir/f27m_$i
1349         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1350                 error "OST0 was full but new created file still use it"
1351         simple_cleanup_common
1352 }
1353 run_test 27m "create file while OST0 was full =================="
1354
1355 sleep_maxage() {
1356         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1357         sleep $DELAY
1358 }
1359
1360 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1361 # if the OST isn't full anymore.
1362 reset_enospc() {
1363         local OSTIDX=${1:-""}
1364
1365         local list=$(comma_list $(osts_nodes))
1366         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1367
1368         do_nodes $list lctl set_param fail_loc=0
1369         sync    # initiate all OST_DESTROYs from MDS to OST
1370         sleep_maxage
1371 }
1372
1373 exhaust_precreations() {
1374         local OSTIDX=$1
1375         local FAILLOC=$2
1376         local FAILIDX=${3:-$OSTIDX}
1377
1378         test_mkdir -p $DIR/$tdir
1379         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1380         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1381
1382         local OST=$(ostname_from_index $OSTIDX)
1383         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1384                           sed -e 's/_UUID$//;s/^.*-//')
1385
1386         # on the mdt's osc
1387         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1388         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1389                         osc.$mdtosc_proc1.prealloc_last_id)
1390         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1391                         osc.$mdtosc_proc1.prealloc_next_id)
1392
1393         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1394         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1395
1396         test_mkdir -p $DIR/$tdir/${OST}
1397         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1398 #define OBD_FAIL_OST_ENOSPC              0x215
1399         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1400         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1401         echo "Creating to objid $last_id on ost $OST..."
1402         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1403         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1404         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1405         sleep_maxage
1406 }
1407
1408 exhaust_all_precreations() {
1409         local i
1410         for (( i=0; i < OSTCOUNT; i++ )) ; do
1411                 exhaust_precreations $i $1 -1
1412         done
1413 }
1414
1415 test_27n() {
1416         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1418         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1419         remote_ost_nodsh && skip "remote OST with nodsh" && return
1420
1421         reset_enospc
1422         rm -f $DIR/$tdir/$tfile
1423         exhaust_precreations 0 0x80000215
1424         $SETSTRIPE -c -1 $DIR/$tdir
1425         touch $DIR/$tdir/$tfile || error
1426         $GETSTRIPE $DIR/$tdir/$tfile
1427         reset_enospc
1428 }
1429 run_test 27n "create file with some full OSTs =================="
1430
1431 test_27o() {
1432         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1433         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1434         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1435         remote_ost_nodsh && skip "remote OST with nodsh" && return
1436
1437         reset_enospc
1438         rm -f $DIR/$tdir/$tfile
1439         exhaust_all_precreations 0x215
1440
1441         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1442
1443         reset_enospc
1444         rm -rf $DIR/$tdir/*
1445 }
1446 run_test 27o "create file with all full OSTs (should error) ===="
1447
1448 test_27p() {
1449         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1450         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1451         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1452         remote_ost_nodsh && skip "remote OST with nodsh" && return
1453
1454         reset_enospc
1455         rm -f $DIR/$tdir/$tfile
1456         test_mkdir -p $DIR/$tdir
1457
1458         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1459         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1460         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1461
1462         exhaust_precreations 0 0x80000215
1463         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1464         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1465         $GETSTRIPE $DIR/$tdir/$tfile
1466
1467         reset_enospc
1468 }
1469 run_test 27p "append to a truncated file with some full OSTs ==="
1470
1471 test_27q() {
1472         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1474         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1475         remote_ost_nodsh && skip "remote OST with nodsh" && return
1476
1477         reset_enospc
1478         rm -f $DIR/$tdir/$tfile
1479
1480         test_mkdir -p $DIR/$tdir
1481         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1482         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1483         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1484
1485         exhaust_all_precreations 0x215
1486
1487         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1488         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1489
1490         reset_enospc
1491 }
1492 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1493
1494 test_27r() {
1495         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1497         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1498         remote_ost_nodsh && skip "remote OST with nodsh" && return
1499
1500         reset_enospc
1501         rm -f $DIR/$tdir/$tfile
1502         exhaust_precreations 0 0x80000215
1503
1504         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1505
1506         reset_enospc
1507 }
1508 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1509
1510 test_27s() { # bug 10725
1511         test_mkdir -p $DIR/$tdir
1512         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1513         local stripe_count=0
1514         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1515         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1516                 error "stripe width >= 2^32 succeeded" || true
1517
1518 }
1519 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1520
1521 test_27t() { # bug 10864
1522         WDIR=`pwd`
1523         WLFS=`which lfs`
1524         cd $DIR
1525         touch $tfile
1526         $WLFS getstripe $tfile
1527         cd $WDIR
1528 }
1529 run_test 27t "check that utils parse path correctly"
1530
1531 test_27u() { # bug 4900
1532         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1533         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1534         local index
1535         local list=$(comma_list $(mdts_nodes))
1536
1537 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1538         do_nodes $list $LCTL set_param fail_loc=0x139
1539         test_mkdir -p $DIR/$tdir
1540         rm -rf $DIR/$tdir/*
1541         createmany -o $DIR/$tdir/t- 1000
1542         do_nodes $list $LCTL set_param fail_loc=0
1543
1544         TLOG=$DIR/$tfile.getstripe
1545         $GETSTRIPE $DIR/$tdir > $TLOG
1546         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1547         unlinkmany $DIR/$tdir/t- 1000
1548         [ $OBJS -gt 0 ] && \
1549                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1550 }
1551 run_test 27u "skip object creation on OSC w/o objects =========="
1552
1553 test_27v() { # bug 4900
1554         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1556         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1557         remote_ost_nodsh && skip "remote OST with nodsh" && return
1558
1559         exhaust_all_precreations 0x215
1560         reset_enospc
1561
1562         test_mkdir -p $DIR/$tdir
1563         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1564
1565         touch $DIR/$tdir/$tfile
1566         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1567         # all except ost1
1568         for (( i=1; i < OSTCOUNT; i++ )); do
1569                 do_facet ost$i lctl set_param fail_loc=0x705
1570         done
1571         local START=`date +%s`
1572         createmany -o $DIR/$tdir/$tfile 32
1573
1574         local FINISH=`date +%s`
1575         local TIMEOUT=`lctl get_param -n timeout`
1576         local PROCESS=$((FINISH - START))
1577         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1578                error "$FINISH - $START >= $TIMEOUT / 2"
1579         sleep $((TIMEOUT / 2 - PROCESS))
1580         reset_enospc
1581 }
1582 run_test 27v "skip object creation on slow OST ================="
1583
1584 test_27w() { # bug 10997
1585         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1586         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1587         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1588                 error "stripe size $size != 65536" || true
1589         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1590                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1591 }
1592 run_test 27w "check $SETSTRIPE -S option"
1593
1594 test_27wa() {
1595         [ "$OSTCOUNT" -lt "2" ] &&
1596                 skip_env "skipping multiple stripe count/offset test" && return
1597
1598         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1599         for i in $(seq 1 $OSTCOUNT); do
1600                 offset=$((i - 1))
1601                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1602                         error "setstripe -c $i -i $offset failed"
1603                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1604                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1605                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1606                 [ $index -ne $offset ] &&
1607                         error "stripe offset $index != $offset" || true
1608         done
1609 }
1610 run_test 27wa "check $SETSTRIPE -c -i options"
1611
1612 test_27x() {
1613         remote_ost_nodsh && skip "remote OST with nodsh" && return
1614         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1616         OFFSET=$(($OSTCOUNT - 1))
1617         OSTIDX=0
1618         local OST=$(ostname_from_index $OSTIDX)
1619
1620         test_mkdir -p $DIR/$tdir
1621         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1622         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1623         sleep_maxage
1624         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1625         for i in `seq 0 $OFFSET`; do
1626                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1627                 error "OST0 was degraded but new created file still use it"
1628         done
1629         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1630 }
1631 run_test 27x "create files while OST0 is degraded"
1632
1633 test_27y() {
1634         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1635         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1636         remote_ost_nodsh && skip "remote OST with nodsh" && return
1637         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1638
1639         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1640         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1641             osc.$mdtosc.prealloc_last_id)
1642         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1643             osc.$mdtosc.prealloc_next_id)
1644         local fcount=$((last_id - next_id))
1645         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1646         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1647
1648         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1649                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1650         local OST_DEACTIVE_IDX=-1
1651         local OSC
1652         local OSTIDX
1653         local OST
1654
1655         for OSC in $MDS_OSCS; do
1656                 OST=$(osc_to_ost $OSC)
1657                 OSTIDX=$(index_from_ostuuid $OST)
1658                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1659                         OST_DEACTIVE_IDX=$OSTIDX
1660                 fi
1661                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1662                         echo $OSC "is Deactivated:"
1663                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1664                 fi
1665         done
1666
1667         OSTIDX=$(index_from_ostuuid $OST)
1668         mkdir -p $DIR/$tdir
1669         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1670
1671         for OSC in $MDS_OSCS; do
1672                 OST=$(osc_to_ost $OSC)
1673                 OSTIDX=$(index_from_ostuuid $OST)
1674                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1675                         echo $OST "is degraded:"
1676                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1677                                                 obdfilter.$OST.degraded=1
1678                 fi
1679         done
1680
1681         sleep_maxage
1682         createmany -o $DIR/$tdir/$tfile $fcount
1683
1684         for OSC in $MDS_OSCS; do
1685                 OST=$(osc_to_ost $OSC)
1686                 OSTIDX=$(index_from_ostuuid $OST)
1687                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1688                         echo $OST "is recovered from degraded:"
1689                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1690                                                 obdfilter.$OST.degraded=0
1691                 else
1692                         do_facet $SINGLEMDS lctl --device %$OSC activate
1693                 fi
1694         done
1695
1696         # all osp devices get activated, hence -1 stripe count restored
1697         local stripecnt=0
1698
1699         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1700         # devices get activated.
1701         sleep_maxage
1702         $SETSTRIPE -c -1 $DIR/$tfile
1703         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1704         rm -f $DIR/$tfile
1705         [ $stripecnt -ne $OSTCOUNT ] &&
1706                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1707         return 0
1708 }
1709 run_test 27y "create files while OST0 is degraded and the rest inactive"
1710
1711 check_seq_oid()
1712 {
1713         log "check file $1"
1714
1715         lmm_count=$($GETSTRIPE -c $1)
1716         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1717         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1718
1719         local old_ifs="$IFS"
1720         IFS=$'[:]'
1721         fid=($($LFS path2fid $1))
1722         IFS="$old_ifs"
1723
1724         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1725         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1726
1727         # compare lmm_seq and lu_fid->f_seq
1728         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1729         # compare lmm_object_id and lu_fid->oid
1730         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1731
1732         # check the trusted.fid attribute of the OST objects of the file
1733         local have_obdidx=false
1734         local stripe_nr=0
1735         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1736                 # skip lines up to and including "obdidx"
1737                 [ -z "$obdidx" ] && break
1738                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1739                 $have_obdidx || continue
1740
1741                 local ost=$((obdidx + 1))
1742                 local dev=$(ostdevname $ost)
1743                 local oid_hex
1744
1745                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1746
1747                 seq=$(echo $seq | sed -e "s/^0x//g")
1748                 if [ $seq == 0 ]; then
1749                         oid_hex=$(echo $oid)
1750                 else
1751                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1752                 fi
1753                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1754
1755                 local ff
1756                 #
1757                 # Don't unmount/remount the OSTs if we don't need to do that.
1758                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1759                 # update too, until that use mount/ll_decode_filter_fid/mount.
1760                 # Re-enable when debugfs will understand new filter_fid.
1761                 #
1762                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1763                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1764                                 $dev 2>/dev/null" | grep "parent=")
1765                 else
1766                         stop ost$ost
1767                         mount_fstype ost$ost
1768                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1769                                 $(facet_mntpt ost$ost)/$obj_file)
1770                         unmount_fstype ost$ost
1771                         start ost$ost $dev $OST_MOUNT_OPTS
1772                 fi
1773
1774                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1775
1776                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1777
1778                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1779                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1780                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1781                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1782                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1783                 local ff_pstripe
1784                 if echo $ff_parent | grep -q 'stripe='; then
1785                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1786                 else
1787                         #
1788                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1789                         # into f_ver in this case.  See the comment on
1790                         # ff_parent.
1791                         #
1792                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1793                                 sed -e 's/\]//')
1794                 fi
1795
1796                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1797                 [ $ff_pseq = $lmm_seq ] ||
1798                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1799                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1800                 [ $ff_poid = $lmm_oid ] ||
1801                         error "FF parent OID $ff_poid != $lmm_oid"
1802                 (($ff_pstripe == $stripe_nr)) ||
1803                         error "FF stripe $ff_pstripe != $stripe_nr"
1804
1805                 stripe_nr=$((stripe_nr + 1))
1806         done
1807 }
1808
1809 test_27z() {
1810         remote_ost_nodsh && skip "remote OST with nodsh" && return
1811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1812         test_mkdir -p $DIR/$tdir
1813
1814         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1815                 { error "setstripe -c -1 failed"; return 1; }
1816         # We need to send a write to every object to get parent FID info set.
1817         # This _should_ also work for setattr, but does not currently.
1818         # touch $DIR/$tdir/$tfile-1 ||
1819         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1820                 { error "dd $tfile-1 failed"; return 2; }
1821         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1822                 { error "setstripe -c -1 failed"; return 3; }
1823         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1824                 { error "dd $tfile-2 failed"; return 4; }
1825
1826         # make sure write RPCs have been sent to OSTs
1827         sync; sleep 5; sync
1828
1829         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1830         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1831 }
1832 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1833
1834 test_27A() { # b=19102
1835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1836         local restore_size=$($GETSTRIPE -S $MOUNT)
1837         local restore_count=$($GETSTRIPE -c $MOUNT)
1838         local restore_offset=$($GETSTRIPE -i $MOUNT)
1839         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1840         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1841                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1842         local default_size=$($GETSTRIPE -S $MOUNT)
1843         local default_offset=$($GETSTRIPE -i $MOUNT)
1844         local dsize=$((1024 * 1024))
1845         [ $default_size -eq $dsize ] ||
1846                 error "stripe size $default_size != $dsize"
1847         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1848         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1849 }
1850 run_test 27A "check filesystem-wide default LOV EA values"
1851
1852 test_27B() { # LU-2523
1853         test_mkdir -p $DIR/$tdir
1854         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1855         touch $DIR/$tdir/f0
1856         # open f1 with O_LOV_DELAY_CREATE
1857         # rename f0 onto f1
1858         # call setstripe ioctl on open file descriptor for f1
1859         # close
1860         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1861                 $DIR/$tdir/f0
1862
1863         rm -f $DIR/$tdir/f1
1864         # open f1 with O_LOV_DELAY_CREATE
1865         # unlink f1
1866         # call setstripe ioctl on open file descriptor for f1
1867         # close
1868         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1869
1870         # Allow multiop to fail in imitation of NFS's busted semantics.
1871         true
1872 }
1873 run_test 27B "call setstripe on open unlinked file/rename victim"
1874
1875 test_27C() { #LU-2871
1876         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
1877
1878         declare -a ost_idx
1879         local index
1880         local found
1881         local i
1882         local j
1883
1884         test_mkdir -p $DIR/$tdir
1885         cd $DIR/$tdir
1886         for i in $(seq 0 $((OSTCOUNT - 1))); do
1887                 # set stripe across all OSTs starting from OST$i
1888                 $SETSTRIPE -i $i -c -1 $tfile$i
1889                 # get striping information
1890                 ost_idx=($($GETSTRIPE $tfile$i |
1891                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1892                 echo ${ost_idx[@]}
1893
1894                 # check the layout
1895                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1896                         error "${#ost_idx[@]} != $OSTCOUNT"
1897
1898                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1899                         found=0
1900                         for j in $(echo ${ost_idx[@]}); do
1901                                 if [ $index -eq $j ]; then
1902                                         found=1
1903                                         break
1904                                 fi
1905                         done
1906                         [ $found = 1 ] ||
1907                                 error "Can not find $index in ${ost_idx[@]}"
1908                 done
1909         done
1910 }
1911 run_test 27C "check full striping across all OSTs"
1912
1913 # createtest also checks that device nodes are created and
1914 # then visible correctly (#2091)
1915 test_28() { # bug 2091
1916         test_mkdir $DIR/d28
1917         $CREATETEST $DIR/d28/ct || error
1918 }
1919 run_test 28 "create/mknod/mkdir with bad file types ============"
1920
1921 test_29() {
1922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1923         cancel_lru_locks mdc
1924         test_mkdir $DIR/d29
1925         touch $DIR/d29/foo
1926         log 'first d29'
1927         ls -l $DIR/d29
1928
1929         declare -i LOCKCOUNTORIG=0
1930         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1931                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1932         done
1933         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1934
1935         declare -i LOCKUNUSEDCOUNTORIG=0
1936         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1937                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1938         done
1939
1940         log 'second d29'
1941         ls -l $DIR/d29
1942         log 'done'
1943
1944         declare -i LOCKCOUNTCURRENT=0
1945         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1946                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1947         done
1948
1949         declare -i LOCKUNUSEDCOUNTCURRENT=0
1950         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1951                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1952         done
1953
1954         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1955                 lctl set_param -n ldlm.dump_namespaces ""
1956                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1957                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1958                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1959                 return 2
1960         fi
1961         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1962                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1963                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1964                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1965                 return 3
1966         fi
1967 }
1968 run_test 29 "IT_GETATTR regression  ============================"
1969
1970 test_30a() { # was test_30
1971         cp `which ls` $DIR || cp /bin/ls $DIR
1972         $DIR/ls / || error
1973         rm $DIR/ls
1974 }
1975 run_test 30a "execute binary from Lustre (execve) =============="
1976
1977 test_30b() {
1978         cp `which ls` $DIR || cp /bin/ls $DIR
1979         chmod go+rx $DIR/ls
1980         $RUNAS $DIR/ls / || error
1981         rm $DIR/ls
1982 }
1983 run_test 30b "execute binary from Lustre as non-root ==========="
1984
1985 test_30c() { # b=22376
1986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1987         cp `which ls` $DIR || cp /bin/ls $DIR
1988         chmod a-rw $DIR/ls
1989         cancel_lru_locks mdc
1990         cancel_lru_locks osc
1991         $RUNAS $DIR/ls / || error
1992         rm -f $DIR/ls
1993 }
1994 run_test 30c "execute binary from Lustre without read perms ===="
1995
1996 test_31a() {
1997         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1998         $CHECKSTAT -a $DIR/f31 || error
1999 }
2000 run_test 31a "open-unlink file =================================="
2001
2002 test_31b() {
2003         touch $DIR/f31 || error
2004         ln $DIR/f31 $DIR/f31b || error
2005         $MULTIOP $DIR/f31b Ouc || error
2006         $CHECKSTAT -t file $DIR/f31 || error
2007 }
2008 run_test 31b "unlink file with multiple links while open ======="
2009
2010 test_31c() {
2011         touch $DIR/f31 || error
2012         ln $DIR/f31 $DIR/f31c || error
2013         multiop_bg_pause $DIR/f31 O_uc || return 1
2014         MULTIPID=$!
2015         $MULTIOP $DIR/f31c Ouc
2016         kill -USR1 $MULTIPID
2017         wait $MULTIPID
2018 }
2019 run_test 31c "open-unlink file with multiple links ============="
2020
2021 test_31d() {
2022         opendirunlink $DIR/d31d $DIR/d31d || error
2023         $CHECKSTAT -a $DIR/d31d || error
2024 }
2025 run_test 31d "remove of open directory ========================="
2026
2027 test_31e() { # bug 2904
2028         check_kernel_version 34 || return 0
2029         openfilleddirunlink $DIR/d31e || error
2030 }
2031 run_test 31e "remove of open non-empty directory ==============="
2032
2033 test_31f() { # bug 4554
2034         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2035         set -vx
2036         test_mkdir $DIR/d31f
2037         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2038         cp /etc/hosts $DIR/d31f
2039         ls -l $DIR/d31f
2040         $GETSTRIPE $DIR/d31f/hosts
2041         multiop_bg_pause $DIR/d31f D_c || return 1
2042         MULTIPID=$!
2043
2044         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2045         test_mkdir $DIR/d31f
2046         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2047         cp /etc/hosts $DIR/d31f
2048         ls -l $DIR/d31f
2049         $GETSTRIPE $DIR/d31f/hosts
2050         multiop_bg_pause $DIR/d31f D_c || return 1
2051         MULTIPID2=$!
2052
2053         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2054         wait $MULTIPID || error "first opendir $MULTIPID failed"
2055
2056         sleep 6
2057
2058         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2059         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2060         set +vx
2061 }
2062 run_test 31f "remove of open directory with open-unlink file ==="
2063
2064 test_31g() {
2065         echo "-- cross directory link --"
2066         test_mkdir -c1 $DIR/${tdir}ga
2067         test_mkdir -c1 $DIR/${tdir}gb
2068         touch $DIR/${tdir}ga/f
2069         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2070         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2071         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2072         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2073         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2074 }
2075 run_test 31g "cross directory link==============="
2076
2077 test_31h() {
2078         echo "-- cross directory link --"
2079         test_mkdir -c1 $DIR/${tdir}
2080         test_mkdir -c1 $DIR/${tdir}/dir
2081         touch $DIR/${tdir}/f
2082         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2083         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2084         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2085         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2086         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2087 }
2088 run_test 31h "cross directory link under child==============="
2089
2090 test_31i() {
2091         echo "-- cross directory link --"
2092         test_mkdir -c1 $DIR/$tdir
2093         test_mkdir -c1 $DIR/$tdir/dir
2094         touch $DIR/$tdir/dir/f
2095         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2096         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2097         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2098         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2099         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2100 }
2101 run_test 31i "cross directory link under parent==============="
2102
2103 test_31j() {
2104         test_mkdir -c1 -p $DIR/$tdir
2105         test_mkdir -c1 -p $DIR/$tdir/dir1
2106         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2107         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2108         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2109         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2110         return 0
2111 }
2112 run_test 31j "link for directory==============="
2113
2114 test_31k() {
2115         test_mkdir -c1 -p $DIR/$tdir
2116         touch $DIR/$tdir/s
2117         touch $DIR/$tdir/exist
2118         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2119         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2120         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2121         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2122         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2123         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2124         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2125         return 0
2126 }
2127 run_test 31k "link to file: the same, non-existing, dir==============="
2128
2129 test_31m() {
2130         mkdir $DIR/d31m
2131         touch $DIR/d31m/s
2132         mkdir $DIR/d31m2
2133         touch $DIR/d31m2/exist
2134         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2135         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2136         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2137         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2138         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2139         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2140         return 0
2141 }
2142 run_test 31m "link to file: the same, non-existing, dir==============="
2143
2144 test_31n() {
2145         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2146         nlink=$(stat --format=%h $DIR/$tfile)
2147         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2148         local fd=$(free_fd)
2149         local cmd="exec $fd<$DIR/$tfile"
2150         eval $cmd
2151         cmd="exec $fd<&-"
2152         trap "eval $cmd" EXIT
2153         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2154         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2155         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2156         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2157         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2158         eval $cmd
2159 }
2160 run_test 31n "check link count of unlinked file"
2161
2162 link_one() {
2163         local TEMPNAME=$(mktemp $1_XXXXXX)
2164         mlink $TEMPNAME $1 2> /dev/null &&
2165                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2166         munlink $TEMPNAME
2167 }
2168
2169 test_31o() { # LU-2901
2170         mkdir -p $DIR/$tdir
2171         for LOOP in $(seq 100); do
2172                 rm -f $DIR/$tdir/$tfile*
2173                 for THREAD in $(seq 8); do
2174                         link_one $DIR/$tdir/$tfile.$LOOP &
2175                 done
2176                 wait
2177                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2178                 [ $LINKS -gt 1 ] && ls $DIR/$tdir &&
2179                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2180                         break || true
2181         done
2182 }
2183 run_test 31o "duplicate hard links with same filename"
2184
2185 cleanup_test32_mount() {
2186         trap 0
2187         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2188 }
2189
2190 test_32a() {
2191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2192         echo "== more mountpoints and symlinks ================="
2193         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2194         trap cleanup_test32_mount EXIT
2195         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2196         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2197         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2198         cleanup_test32_mount
2199 }
2200 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2201
2202 test_32b() {
2203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2204         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2205         trap cleanup_test32_mount EXIT
2206         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2207         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2208         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2209         cleanup_test32_mount
2210 }
2211 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2212
2213 test_32c() {
2214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2215         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2216         trap cleanup_test32_mount EXIT
2217         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2218         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2219         test_mkdir -p $DIR/$tdir/d2/test_dir
2220         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2221         cleanup_test32_mount
2222 }
2223 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2224
2225 test_32d() {
2226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2227         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2228         trap cleanup_test32_mount EXIT
2229         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2230         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2231         test_mkdir -p $DIR/$tdir/d2/test_dir
2232         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2233         cleanup_test32_mount
2234 }
2235 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2236
2237 test_32e() {
2238         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2239         test_mkdir -p $DIR/d32e/tmp
2240         TMP_DIR=$DIR/d32e/tmp
2241         ln -s $DIR/d32e $TMP_DIR/symlink11
2242         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2243         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2244         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2245 }
2246 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2247
2248 test_32f() {
2249         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2250         test_mkdir -p $DIR/d32f/tmp
2251         TMP_DIR=$DIR/d32f/tmp
2252         ln -s $DIR/d32f $TMP_DIR/symlink11
2253         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2254         ls $DIR/d32f/tmp/symlink11  || error
2255         ls $DIR/d32f/symlink01 || error
2256 }
2257 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2258
2259 test_32g() {
2260         TMP_DIR=$DIR/$tdir/tmp
2261         test_mkdir -p $DIR/$tdir/tmp
2262         test_mkdir $DIR/${tdir}2
2263         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2264         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2265         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2266         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2267         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2268         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2269 }
2270 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2271
2272 test_32h() {
2273         rm -fr $DIR/$tdir $DIR/${tdir}2
2274         TMP_DIR=$DIR/$tdir/tmp
2275         test_mkdir -p $DIR/$tdir/tmp
2276         test_mkdir $DIR/${tdir}2
2277         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2278         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2279         ls $TMP_DIR/symlink12 || error
2280         ls $DIR/$tdir/symlink02  || error
2281 }
2282 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2283
2284 test_32i() {
2285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2286         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2287         trap cleanup_test32_mount EXIT
2288         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2289         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2290         touch $DIR/$tdir/test_file
2291         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2292         cleanup_test32_mount
2293 }
2294 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2295
2296 test_32j() {
2297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2298         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2299         trap cleanup_test32_mount EXIT
2300         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2301         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2302         touch $DIR/$tdir/test_file
2303         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2304         cleanup_test32_mount
2305 }
2306 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2307
2308 test_32k() {
2309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2310         rm -fr $DIR/$tdir
2311         trap cleanup_test32_mount EXIT
2312         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2313         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2314         test_mkdir -p $DIR/$tdir/d2
2315         touch $DIR/$tdir/d2/test_file || error
2316         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2317         cleanup_test32_mount
2318 }
2319 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2320
2321 test_32l() {
2322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2323         rm -fr $DIR/$tdir
2324         trap cleanup_test32_mount EXIT
2325         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2326         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2327         test_mkdir -p $DIR/$tdir/d2
2328         touch $DIR/$tdir/d2/test_file
2329         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2330         cleanup_test32_mount
2331 }
2332 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2333
2334 test_32m() {
2335         rm -fr $DIR/d32m
2336         test_mkdir -p $DIR/d32m/tmp
2337         TMP_DIR=$DIR/d32m/tmp
2338         ln -s $DIR $TMP_DIR/symlink11
2339         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2340         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2341         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2342 }
2343 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2344
2345 test_32n() {
2346         rm -fr $DIR/d32n
2347         test_mkdir -p $DIR/d32n/tmp
2348         TMP_DIR=$DIR/d32n/tmp
2349         ln -s $DIR $TMP_DIR/symlink11
2350         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2351         ls -l $DIR/d32n/tmp/symlink11  || error
2352         ls -l $DIR/d32n/symlink01 || error
2353 }
2354 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2355
2356 test_32o() {
2357         rm -fr $DIR/d32o $DIR/$tfile
2358         touch $DIR/$tfile
2359         test_mkdir -p $DIR/d32o/tmp
2360         TMP_DIR=$DIR/d32o/tmp
2361         ln -s $DIR/$tfile $TMP_DIR/symlink12
2362         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2363         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2364         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2365         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2366         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2367 }
2368 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2369
2370 test_32p() {
2371     log 32p_1
2372         rm -fr $DIR/d32p
2373     log 32p_2
2374         rm -f $DIR/$tfile
2375     log 32p_3
2376         touch $DIR/$tfile
2377     log 32p_4
2378         test_mkdir -p $DIR/d32p/tmp
2379     log 32p_5
2380         TMP_DIR=$DIR/d32p/tmp
2381     log 32p_6
2382         ln -s $DIR/$tfile $TMP_DIR/symlink12
2383     log 32p_7
2384         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2385     log 32p_8
2386         cat $DIR/d32p/tmp/symlink12 || error
2387     log 32p_9
2388         cat $DIR/d32p/symlink02 || error
2389     log 32p_10
2390 }
2391 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2392
2393 cleanup_testdir_mount() {
2394         trap 0
2395         $UMOUNT -d $DIR/$tdir
2396 }
2397
2398 test_32q() {
2399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2400         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2401         trap cleanup_testdir_mount EXIT
2402         test_mkdir -p $DIR/$tdir
2403         touch $DIR/$tdir/under_the_mount
2404         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2405         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2406         cleanup_testdir_mount
2407 }
2408 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2409
2410 test_32r() {
2411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2412         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2413         trap cleanup_testdir_mount EXIT
2414         test_mkdir -p $DIR/$tdir
2415         touch $DIR/$tdir/under_the_mount
2416         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2417         ls $DIR/$tdir | grep -q under_the_mount && error || true
2418         cleanup_testdir_mount
2419 }
2420 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2421
2422 test_33aa() {
2423         rm -f $DIR/$tfile
2424         touch $DIR/$tfile
2425         chmod 444 $DIR/$tfile
2426         chown $RUNAS_ID $DIR/$tfile
2427         log 33_1
2428         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2429         log 33_2
2430 }
2431 run_test 33aa "write file with mode 444 (should return error) ===="
2432
2433 test_33a() {
2434         rm -fr $DIR/d33
2435         test_mkdir -p $DIR/d33
2436         chown $RUNAS_ID $DIR/d33
2437         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2438         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2439                 error "open RDWR" || true
2440 }
2441 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2442
2443 test_33b() {
2444         rm -fr $DIR/d33
2445         test_mkdir -p $DIR/d33
2446         chown $RUNAS_ID $DIR/d33
2447         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2448 }
2449 run_test 33b "test open file with malformed flags (No panic and return error)"
2450
2451 test_33c() {
2452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2453         local ostnum
2454         local ostname
2455         local write_bytes
2456         local all_zeros
2457
2458         remote_ost_nodsh && skip "remote OST with nodsh" && return
2459         all_zeros=:
2460         rm -fr $DIR/d33
2461         test_mkdir -p $DIR/d33
2462         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2463
2464         sync
2465         for ostnum in $(seq $OSTCOUNT); do
2466                 # test-framework's OST numbering is one-based, while Lustre's
2467                 # is zero-based
2468                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2469                 # Parsing llobdstat's output sucks; we could grep the /proc
2470                 # path, but that's likely to not be as portable as using the
2471                 # llobdstat utility.  So we parse lctl output instead.
2472                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2473                         obdfilter/$ostname/stats |
2474                         awk '/^write_bytes/ {print $7}' )
2475                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2476                 if (( ${write_bytes:-0} > 0 ))
2477                 then
2478                         all_zeros=false
2479                         break;
2480                 fi
2481         done
2482
2483         $all_zeros || return 0
2484
2485         # Write four bytes
2486         echo foo > $DIR/d33/bar
2487         # Really write them
2488         sync
2489
2490         # Total up write_bytes after writing.  We'd better find non-zeros.
2491         for ostnum in $(seq $OSTCOUNT); do
2492                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2493                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2494                         obdfilter/$ostname/stats |
2495                         awk '/^write_bytes/ {print $7}' )
2496                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2497                 if (( ${write_bytes:-0} > 0 ))
2498                 then
2499                         all_zeros=false
2500                         break;
2501                 fi
2502         done
2503
2504         if $all_zeros
2505         then
2506                 for ostnum in $(seq $OSTCOUNT); do
2507                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2508                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2509                         do_facet ost$ostnum lctl get_param -n \
2510                                 obdfilter/$ostname/stats
2511                 done
2512                 error "OST not keeping write_bytes stats (b22312)"
2513         fi
2514 }
2515 run_test 33c "test llobdstat and write_bytes"
2516
2517 test_33d() {
2518         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2520         local MDTIDX=1
2521         local remote_dir=$DIR/$tdir/remote_dir
2522
2523         mkdir -p $DIR/$tdir
2524         $LFS mkdir -i $MDTIDX $remote_dir ||
2525                 error "create remote directory failed"
2526
2527         touch $remote_dir/$tfile
2528         chmod 444 $remote_dir/$tfile
2529         chown $RUNAS_ID $remote_dir/$tfile
2530
2531         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2532
2533         chown $RUNAS_ID $remote_dir
2534         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2535                                         error "create" || true
2536         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2537                                     error "open RDWR" || true
2538         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2539                                     error "create" || true
2540 }
2541 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2542
2543 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2544 test_34a() {
2545         rm -f $DIR/f34
2546         $MCREATE $DIR/f34 || error
2547         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2548         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
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 34a "truncate file that has not been opened ==========="
2553
2554 test_34b() {
2555         [ ! -f $DIR/f34 ] && test_34a
2556         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2557         $OPENFILE -f O_RDONLY $DIR/f34
2558         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2559         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2560 }
2561 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2562
2563 test_34c() {
2564         [ ! -f $DIR/f34 ] && test_34a
2565         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2566         $OPENFILE -f O_RDWR $DIR/f34
2567         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2568         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2569 }
2570 run_test 34c "O_RDWR opening file-with-size works =============="
2571
2572 test_34d() {
2573         [ ! -f $DIR/f34 ] && test_34a
2574         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2575         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2576         rm $DIR/f34
2577 }
2578 run_test 34d "write to sparse file ============================="
2579
2580 test_34e() {
2581         rm -f $DIR/f34e
2582         $MCREATE $DIR/f34e || error
2583         $TRUNCATE $DIR/f34e 1000 || error
2584         $CHECKSTAT -s 1000 $DIR/f34e || error
2585         $OPENFILE -f O_RDWR $DIR/f34e
2586         $CHECKSTAT -s 1000 $DIR/f34e || error
2587 }
2588 run_test 34e "create objects, some with size and some without =="
2589
2590 test_34f() { # bug 6242, 6243
2591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2592         SIZE34F=48000
2593         rm -f $DIR/f34f
2594         $MCREATE $DIR/f34f || error
2595         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2596         dd if=$DIR/f34f of=$TMP/f34f
2597         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2598         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2599         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2600         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2601         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2602 }
2603 run_test 34f "read from a file with no objects until EOF ======="
2604
2605 test_34g() {
2606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2607         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2608         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2609         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2610         cancel_lru_locks osc
2611         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2612                 error "wrong size after lock cancel"
2613
2614         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2615         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2616                 error "expanding truncate failed"
2617         cancel_lru_locks osc
2618         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2619                 error "wrong expanded size after lock cancel"
2620 }
2621 run_test 34g "truncate long file ==============================="
2622
2623 test_34h() {
2624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2625         local gid=10
2626         local sz=1000
2627
2628         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2629         sync # Flush the cache so that multiop below does not block on cache
2630              # flush when getting the group lock
2631         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2632         MULTIPID=$!
2633
2634         # Since just timed wait is not good enough, let's do a sync write
2635         # that way we are sure enough time for a roundtrip + processing
2636         # passed + 2 seconds of extra margin.
2637         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2638         rm $DIR/${tfile}-1
2639         sleep 2
2640
2641         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2642                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2643                 kill -9 $MULTIPID
2644         fi
2645         wait $MULTIPID
2646         local nsz=`stat -c %s $DIR/$tfile`
2647         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2648 }
2649 run_test 34h "ftruncate file under grouplock should not block"
2650
2651 test_35a() {
2652         cp /bin/sh $DIR/f35a
2653         chmod 444 $DIR/f35a
2654         chown $RUNAS_ID $DIR/f35a
2655         $RUNAS $DIR/f35a && error || true
2656         rm $DIR/f35a
2657 }
2658 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2659
2660 test_36a() {
2661         rm -f $DIR/f36
2662         utime $DIR/f36 || error
2663 }
2664 run_test 36a "MDS utime check (mknod, utime) ==================="
2665
2666 test_36b() {
2667         echo "" > $DIR/f36
2668         utime $DIR/f36 || error
2669 }
2670 run_test 36b "OST utime check (open, utime) ===================="
2671
2672 test_36c() {
2673         rm -f $DIR/d36/f36
2674         test_mkdir $DIR/d36
2675         chown $RUNAS_ID $DIR/d36
2676         $RUNAS utime $DIR/d36/f36 || error
2677 }
2678 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2679
2680 test_36d() {
2681         [ ! -d $DIR/d36 ] && test_36c
2682         echo "" > $DIR/d36/f36
2683         $RUNAS utime $DIR/d36/f36 || error
2684 }
2685 run_test 36d "non-root OST utime check (open, utime) ==========="
2686
2687 test_36e() {
2688         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2689         test_mkdir -p $DIR/$tdir
2690         touch $DIR/$tdir/$tfile
2691         $RUNAS utime $DIR/$tdir/$tfile && \
2692                 error "utime worked, expected failure" || true
2693 }
2694 run_test 36e "utime on non-owned file (should return error) ===="
2695
2696 subr_36fh() {
2697         local fl="$1"
2698         local LANG_SAVE=$LANG
2699         local LC_LANG_SAVE=$LC_LANG
2700         export LANG=C LC_LANG=C # for date language
2701
2702         DATESTR="Dec 20  2000"
2703         test_mkdir -p $DIR/$tdir
2704         lctl set_param fail_loc=$fl
2705         date; date +%s
2706         cp /etc/hosts $DIR/$tdir/$tfile
2707         sync & # write RPC generated with "current" inode timestamp, but delayed
2708         sleep 1
2709         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2710         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2711         cancel_lru_locks osc
2712         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2713         date; date +%s
2714         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2715                 echo "BEFORE: $LS_BEFORE" && \
2716                 echo "AFTER : $LS_AFTER" && \
2717                 echo "WANT  : $DATESTR" && \
2718                 error "$DIR/$tdir/$tfile timestamps changed" || true
2719
2720         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2721 }
2722
2723 test_36f() {
2724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2725         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2726         subr_36fh "0x80000214"
2727 }
2728 run_test 36f "utime on file racing with OST BRW write =========="
2729
2730 test_36g() {
2731         remote_ost_nodsh && skip "remote OST with nodsh" && return
2732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2733         local fmd_max_age
2734         local fmd_before
2735         local fmd_after
2736
2737         test_mkdir -p $DIR/$tdir
2738         fmd_max_age=$(do_facet ost1 \
2739                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2740                 head -n 1")
2741
2742         fmd_before=$(do_facet ost1 \
2743                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2744         touch $DIR/$tdir/$tfile
2745         sleep $((fmd_max_age + 12))
2746         fmd_after=$(do_facet ost1 \
2747                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2748
2749         echo "fmd_before: $fmd_before"
2750         echo "fmd_after: $fmd_after"
2751         [ "$fmd_after" -gt "$fmd_before" ] && \
2752                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2753                 error "fmd didn't expire after ping" || true
2754 }
2755 run_test 36g "filter mod data cache expiry ====================="
2756
2757 test_36h() {
2758         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2759         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2760         subr_36fh "0x80000227"
2761 }
2762 run_test 36h "utime on file racing with OST BRW write =========="
2763
2764 # test_37 - duplicate with tests 32q 32r
2765
2766 test_38() {
2767         local file=$DIR/$tfile
2768         touch $file
2769         openfile -f O_DIRECTORY $file
2770         local RC=$?
2771         local ENOTDIR=20
2772         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2773         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2774 }
2775 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2776
2777 test_39() {
2778         touch $DIR/$tfile
2779         touch $DIR/${tfile}2
2780 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2781 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2782 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2783         sleep 2
2784         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2785         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2786                 echo "mtime"
2787                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2788                 echo "atime"
2789                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2790                 echo "ctime"
2791                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2792                 error "O_TRUNC didn't change timestamps"
2793         fi
2794 }
2795 run_test 39 "mtime changed on create ==========================="
2796
2797 test_39b() {
2798         test_mkdir -p $DIR/$tdir
2799         cp -p /etc/passwd $DIR/$tdir/fopen
2800         cp -p /etc/passwd $DIR/$tdir/flink
2801         cp -p /etc/passwd $DIR/$tdir/funlink
2802         cp -p /etc/passwd $DIR/$tdir/frename
2803         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2804
2805         sleep 1
2806         echo "aaaaaa" >> $DIR/$tdir/fopen
2807         echo "aaaaaa" >> $DIR/$tdir/flink
2808         echo "aaaaaa" >> $DIR/$tdir/funlink
2809         echo "aaaaaa" >> $DIR/$tdir/frename
2810
2811         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2812         local link_new=`stat -c %Y $DIR/$tdir/flink`
2813         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2814         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2815
2816         cat $DIR/$tdir/fopen > /dev/null
2817         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2818         rm -f $DIR/$tdir/funlink2
2819         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2820
2821         for (( i=0; i < 2; i++ )) ; do
2822                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2823                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2824                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2825                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2826
2827                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2828                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2829                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2830                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2831
2832                 cancel_lru_locks osc
2833                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2834         done
2835 }
2836 run_test 39b "mtime change on open, link, unlink, rename  ======"
2837
2838 # this should be set to past
2839 TEST_39_MTIME=`date -d "1 year ago" +%s`
2840
2841 # bug 11063
2842 test_39c() {
2843         touch $DIR1/$tfile
2844         sleep 2
2845         local mtime0=`stat -c %Y $DIR1/$tfile`
2846
2847         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2848         local mtime1=`stat -c %Y $DIR1/$tfile`
2849         [ "$mtime1" = $TEST_39_MTIME ] || \
2850                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2851
2852         local d1=`date +%s`
2853         echo hello >> $DIR1/$tfile
2854         local d2=`date +%s`
2855         local mtime2=`stat -c %Y $DIR1/$tfile`
2856         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2857                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2858
2859         mv $DIR1/$tfile $DIR1/$tfile-1
2860
2861         for (( i=0; i < 2; i++ )) ; do
2862                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2863                 [ "$mtime2" = "$mtime3" ] || \
2864                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2865
2866                 cancel_lru_locks osc
2867                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2868         done
2869 }
2870 run_test 39c "mtime change on rename ==========================="
2871
2872 # bug 21114
2873 test_39d() {
2874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2875         touch $DIR1/$tfile
2876
2877         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2878
2879         for (( i=0; i < 2; i++ )) ; do
2880                 local mtime=`stat -c %Y $DIR1/$tfile`
2881                 [ $mtime = $TEST_39_MTIME ] || \
2882                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2883
2884                 cancel_lru_locks osc
2885                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2886         done
2887 }
2888 run_test 39d "create, utime, stat =============================="
2889
2890 # bug 21114
2891 test_39e() {
2892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2893         touch $DIR1/$tfile
2894         local mtime1=`stat -c %Y $DIR1/$tfile`
2895
2896         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2897
2898         for (( i=0; i < 2; i++ )) ; do
2899                 local mtime2=`stat -c %Y $DIR1/$tfile`
2900                 [ $mtime2 = $TEST_39_MTIME ] || \
2901                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2902
2903                 cancel_lru_locks osc
2904                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2905         done
2906 }
2907 run_test 39e "create, stat, utime, stat ========================"
2908
2909 # bug 21114
2910 test_39f() {
2911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2912         touch $DIR1/$tfile
2913         mtime1=`stat -c %Y $DIR1/$tfile`
2914
2915         sleep 2
2916         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2917
2918         for (( i=0; i < 2; i++ )) ; do
2919                 local mtime2=`stat -c %Y $DIR1/$tfile`
2920                 [ $mtime2 = $TEST_39_MTIME ] || \
2921                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2922
2923                 cancel_lru_locks osc
2924                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2925         done
2926 }
2927 run_test 39f "create, stat, sleep, utime, stat ================="
2928
2929 # bug 11063
2930 test_39g() {
2931         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2932         echo hello >> $DIR1/$tfile
2933         local mtime1=`stat -c %Y $DIR1/$tfile`
2934
2935         sleep 2
2936         chmod o+r $DIR1/$tfile
2937
2938         for (( i=0; i < 2; i++ )) ; do
2939                 local mtime2=`stat -c %Y $DIR1/$tfile`
2940                 [ "$mtime1" = "$mtime2" ] || \
2941                         error "lost mtime: $mtime2, should be $mtime1"
2942
2943                 cancel_lru_locks osc
2944                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2945         done
2946 }
2947 run_test 39g "write, chmod, stat ==============================="
2948
2949 # bug 11063
2950 test_39h() {
2951         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2952         touch $DIR1/$tfile
2953         sleep 1
2954
2955         local d1=`date`
2956         echo hello >> $DIR1/$tfile
2957         local mtime1=`stat -c %Y $DIR1/$tfile`
2958
2959         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2960         local d2=`date`
2961         if [ "$d1" != "$d2" ]; then
2962                 echo "write and touch not within one second"
2963         else
2964                 for (( i=0; i < 2; i++ )) ; do
2965                         local mtime2=`stat -c %Y $DIR1/$tfile`
2966                         [ "$mtime2" = $TEST_39_MTIME ] || \
2967                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2968
2969                         cancel_lru_locks osc
2970                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2971                 done
2972         fi
2973 }
2974 run_test 39h "write, utime within one second, stat ============="
2975
2976 test_39i() {
2977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2978         touch $DIR1/$tfile
2979         sleep 1
2980
2981         echo hello >> $DIR1/$tfile
2982         local mtime1=`stat -c %Y $DIR1/$tfile`
2983
2984         mv $DIR1/$tfile $DIR1/$tfile-1
2985
2986         for (( i=0; i < 2; i++ )) ; do
2987                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2988
2989                 [ "$mtime1" = "$mtime2" ] || \
2990                         error "lost mtime: $mtime2, should be $mtime1"
2991
2992                 cancel_lru_locks osc
2993                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2994         done
2995 }
2996 run_test 39i "write, rename, stat =============================="
2997
2998 test_39j() {
2999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3000         start_full_debug_logging
3001         touch $DIR1/$tfile
3002         sleep 1
3003
3004         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3005         lctl set_param fail_loc=0x80000412
3006         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3007                 error "multiop failed"
3008         local multipid=$!
3009         local mtime1=`stat -c %Y $DIR1/$tfile`
3010
3011         mv $DIR1/$tfile $DIR1/$tfile-1
3012
3013         kill -USR1 $multipid
3014         wait $multipid || error "multiop close failed"
3015
3016         for (( i=0; i < 2; i++ )) ; do
3017                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3018                 [ "$mtime1" = "$mtime2" ] ||
3019                         error "mtime is lost on close: $mtime2, " \
3020                               "should be $mtime1"
3021
3022                 cancel_lru_locks osc
3023                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3024         done
3025         lctl set_param fail_loc=0
3026         stop_full_debug_logging
3027 }
3028 run_test 39j "write, rename, close, stat ======================="
3029
3030 test_39k() {
3031         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3032         touch $DIR1/$tfile
3033         sleep 1
3034
3035         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3036         local multipid=$!
3037         local mtime1=`stat -c %Y $DIR1/$tfile`
3038
3039         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3040
3041         kill -USR1 $multipid
3042         wait $multipid || error "multiop close failed"
3043
3044         for (( i=0; i < 2; i++ )) ; do
3045                 local mtime2=`stat -c %Y $DIR1/$tfile`
3046
3047                 [ "$mtime2" = $TEST_39_MTIME ] || \
3048                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3049
3050                 cancel_lru_locks osc
3051                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3052         done
3053 }
3054 run_test 39k "write, utime, close, stat ========================"
3055
3056 # this should be set to future
3057 TEST_39_ATIME=`date -d "1 year" +%s`
3058
3059 test_39l() {
3060         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3061         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3062         local atime_diff=$(do_facet $SINGLEMDS \
3063                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3064         rm -rf $DIR/$tdir
3065         mkdir -p $DIR/$tdir
3066
3067         # test setting directory atime to future
3068         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3069         local atime=$(stat -c %X $DIR/$tdir)
3070         [ "$atime" = $TEST_39_ATIME ] || \
3071                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3072
3073         # test setting directory atime from future to now
3074         local d1=$(date +%s)
3075         ls $DIR/$tdir
3076         local d2=$(date +%s)
3077
3078         cancel_lru_locks mdc
3079         atime=$(stat -c %X $DIR/$tdir)
3080         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3081                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3082
3083         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3084         sleep 3
3085
3086         # test setting directory atime when now > dir atime + atime_diff
3087         d1=$(date +%s)
3088         ls $DIR/$tdir
3089         d2=$(date +%s)
3090         cancel_lru_locks mdc
3091         atime=$(stat -c %X $DIR/$tdir)
3092         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3093                 error "atime is not updated  : $atime, should be $d2"
3094
3095         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3096         sleep 3
3097
3098         # test not setting directory atime when now < dir atime + atime_diff
3099         ls $DIR/$tdir
3100         cancel_lru_locks mdc
3101         atime=$(stat -c %X $DIR/$tdir)
3102         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3103                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3104
3105         do_facet $SINGLEMDS \
3106                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3107 }
3108 run_test 39l "directory atime update ==========================="
3109
3110 test_39m() {
3111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3112         touch $DIR1/$tfile
3113         sleep 2
3114         local far_past_mtime=$(date -d "May 29 1953" +%s)
3115         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3116
3117         touch -m -d @$far_past_mtime $DIR1/$tfile
3118         touch -a -d @$far_past_atime $DIR1/$tfile
3119
3120         for (( i=0; i < 2; i++ )) ; do
3121                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3122                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3123                         error "atime or mtime set incorrectly"
3124
3125                 cancel_lru_locks osc
3126                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3127         done
3128 }
3129 run_test 39m "test atime and mtime before 1970"
3130
3131 test_39n() { # LU-3832
3132         local atime_diff=$(do_facet $SINGLEMDS \
3133                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3134         local atime0
3135         local atime1
3136         local atime2
3137
3138         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3139
3140         rm -rf $DIR/$tfile
3141         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3142         atime0=$(stat -c %X $DIR/$tfile)
3143
3144         sleep 5
3145         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3146         atime1=$(stat -c %X $DIR/$tfile)
3147
3148         sleep 5
3149         cancel_lru_locks mdc
3150         cancel_lru_locks osc
3151         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3152         atime2=$(stat -c %X $DIR/$tfile)
3153
3154         do_facet $SINGLEMDS \
3155                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3156
3157         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3158         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3159 }
3160 run_test 39n "check that O_NOATIME is honored"
3161
3162 test_39o() {
3163         TESTDIR=$DIR/$tdir/$tfile
3164         [ -e $TESTDIR ] && rm -rf $TESTDIR
3165         test_mkdir -p $TESTDIR
3166         cd $TESTDIR
3167         links1=2
3168         ls
3169         mkdir a b
3170         ls
3171         links2=$(stat -c %h .)
3172         [ $(($links1 + 2)) != $links2 ] &&
3173                 error "wrong links count $(($links1 + 2)) != $links2"
3174         rmdir b
3175         links3=$(stat -c %h .)
3176         [ $(($links1 + 1)) != $links3 ] &&
3177                 error "wrong links count $links1 != $links3"
3178         return 0
3179 }
3180 run_test 39o "directory cached attributes updated after create ========"
3181
3182 test_39p() {
3183         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3184         local MDTIDX=1
3185         TESTDIR=$DIR/$tdir/$tfile
3186         [ -e $TESTDIR ] && rm -rf $TESTDIR
3187         mkdir -p $TESTDIR
3188         cd $TESTDIR
3189         links1=2
3190         ls
3191         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3192         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3193         ls
3194         links2=$(stat -c %h .)
3195         [ $(($links1 + 2)) != $links2 ] &&
3196                 error "wrong links count $(($links1 + 2)) != $links2"
3197         rmdir remote_dir2
3198         links3=$(stat -c %h .)
3199         [ $(($links1 + 1)) != $links3 ] &&
3200                 error "wrong links count $links1 != $links3"
3201         return 0
3202 }
3203 run_test 39p "remote directory cached attributes updated after create ========"
3204
3205
3206 test_40() {
3207         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3208         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3209                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3210         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3211                 error "$tfile is not 4096 bytes in size"
3212 }
3213 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3214
3215 test_41() {
3216         # bug 1553
3217         small_write $DIR/f41 18
3218 }
3219 run_test 41 "test small file write + fstat ====================="
3220
3221 count_ost_writes() {
3222         lctl get_param -n osc.*.stats |
3223             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
3224 }
3225
3226 # decent default
3227 WRITEBACK_SAVE=500
3228 DIRTY_RATIO_SAVE=40
3229 MAX_DIRTY_RATIO=50
3230 BG_DIRTY_RATIO_SAVE=10
3231 MAX_BG_DIRTY_RATIO=25
3232
3233 start_writeback() {
3234         trap 0
3235         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3236         # dirty_ratio, dirty_background_ratio
3237         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3238                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3239                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3240                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3241         else
3242                 # if file not here, we are a 2.4 kernel
3243                 kill -CONT `pidof kupdated`
3244         fi
3245 }
3246
3247 stop_writeback() {
3248         # setup the trap first, so someone cannot exit the test at the
3249         # exact wrong time and mess up a machine
3250         trap start_writeback EXIT
3251         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3252         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3253                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3254                 sysctl -w vm.dirty_writeback_centisecs=0
3255                 sysctl -w vm.dirty_writeback_centisecs=0
3256                 # save and increase /proc/sys/vm/dirty_ratio
3257                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3258                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3259                 # save and increase /proc/sys/vm/dirty_background_ratio
3260                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3261                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3262         else
3263                 # if file not here, we are a 2.4 kernel
3264                 kill -STOP `pidof kupdated`
3265         fi
3266 }
3267
3268 # ensure that all stripes have some grant before we test client-side cache
3269 setup_test42() {
3270         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3271                 dd if=/dev/zero of=$i bs=4k count=1
3272                 rm $i
3273         done
3274 }
3275
3276 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3277 # file truncation, and file removal.
3278 test_42a() {
3279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3280         setup_test42
3281         cancel_lru_locks osc
3282         stop_writeback
3283         sync; sleep 1; sync # just to be safe
3284         BEFOREWRITES=`count_ost_writes`
3285         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3286         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3287         AFTERWRITES=`count_ost_writes`
3288         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3289                 error "$BEFOREWRITES < $AFTERWRITES"
3290         start_writeback
3291 }
3292 run_test 42a "ensure that we don't flush on close =============="
3293
3294 test_42b() {
3295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3296         setup_test42
3297         cancel_lru_locks osc
3298         stop_writeback
3299         sync
3300         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3301         BEFOREWRITES=`count_ost_writes`
3302         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3303         AFTERWRITES=`count_ost_writes`
3304         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3305                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3306         fi
3307         BEFOREWRITES=`count_ost_writes`
3308         sync || error "sync: $?"
3309         AFTERWRITES=`count_ost_writes`
3310         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3311                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3312         fi
3313         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3314         start_writeback
3315         return 0
3316 }
3317 run_test 42b "test destroy of file with cached dirty data ======"
3318
3319 # if these tests just want to test the effect of truncation,
3320 # they have to be very careful.  consider:
3321 # - the first open gets a {0,EOF}PR lock
3322 # - the first write conflicts and gets a {0, count-1}PW
3323 # - the rest of the writes are under {count,EOF}PW
3324 # - the open for truncate tries to match a {0,EOF}PR
3325 #   for the filesize and cancels the PWs.
3326 # any number of fixes (don't get {0,EOF} on open, match
3327 # composite locks, do smarter file size management) fix
3328 # this, but for now we want these tests to verify that
3329 # the cancellation with truncate intent works, so we
3330 # start the file with a full-file pw lock to match against
3331 # until the truncate.
3332 trunc_test() {
3333         test=$1
3334         file=$DIR/$test
3335         offset=$2
3336         cancel_lru_locks osc
3337         stop_writeback
3338         # prime the file with 0,EOF PW to match
3339         touch $file
3340         $TRUNCATE $file 0
3341         sync; sync
3342         # now the real test..
3343         dd if=/dev/zero of=$file bs=1024 count=100
3344         BEFOREWRITES=`count_ost_writes`
3345         $TRUNCATE $file $offset
3346         cancel_lru_locks osc
3347         AFTERWRITES=`count_ost_writes`
3348         start_writeback
3349 }
3350
3351 test_42c() {
3352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3353         trunc_test 42c 1024
3354         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3355             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3356         rm $file
3357 }
3358 run_test 42c "test partial truncate of file with cached dirty data"
3359
3360 test_42d() {
3361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3362         trunc_test 42d 0
3363         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3364             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3365         rm $file
3366 }
3367 run_test 42d "test complete truncate of file with cached dirty data"
3368
3369 test_42e() { # bug22074
3370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3371         local TDIR=$DIR/${tdir}e
3372         local pagesz=$(page_size)
3373         local pages=16 # hardcoded 16 pages, don't change it.
3374         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3375         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3376         local max_dirty_mb
3377         local warmup_files
3378
3379         test_mkdir -p $DIR/${tdir}e
3380         $SETSTRIPE -c 1 $TDIR
3381         createmany -o $TDIR/f $files
3382
3383         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3384
3385         # we assume that with $OSTCOUNT files, at least one of them will
3386         # be allocated on OST0.
3387         warmup_files=$((OSTCOUNT * max_dirty_mb))
3388         createmany -o $TDIR/w $warmup_files
3389
3390         # write a large amount of data into one file and sync, to get good
3391         # avail_grant number from OST.
3392         for ((i=0; i<$warmup_files; i++)); do
3393                 idx=$($GETSTRIPE -i $TDIR/w$i)
3394                 [ $idx -ne 0 ] && continue
3395                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3396                 break
3397         done
3398         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3399         sync
3400         $LCTL get_param $proc_osc0/cur_dirty_bytes
3401         $LCTL get_param $proc_osc0/cur_grant_bytes
3402
3403         # create as much dirty pages as we can while not to trigger the actual
3404         # RPCs directly. but depends on the env, VFS may trigger flush during this
3405         # period, hopefully we are good.
3406         for ((i=0; i<$warmup_files; i++)); do
3407                 idx=$($GETSTRIPE -i $TDIR/w$i)
3408                 [ $idx -ne 0 ] && continue
3409                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3410         done
3411         $LCTL get_param $proc_osc0/cur_dirty_bytes
3412         $LCTL get_param $proc_osc0/cur_grant_bytes
3413
3414         # perform the real test
3415         $LCTL set_param $proc_osc0/rpc_stats 0
3416         for ((;i<$files; i++)); do
3417                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3418                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3419         done
3420         sync
3421         $LCTL get_param $proc_osc0/rpc_stats
3422
3423         local percent=0
3424         local have_ppr=false
3425         $LCTL get_param $proc_osc0/rpc_stats |
3426                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3427                         # skip lines until we are at the RPC histogram data
3428                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3429                         $have_ppr || continue
3430
3431                         # we only want the percent stat for < 16 pages
3432                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3433
3434                         percent=$((percent + WPCT))
3435                         if [ $percent -gt 15 ]; then
3436                                 error "less than 16-pages write RPCs" \
3437                                       "$percent% > 15%"
3438                                 break
3439                         fi
3440                 done
3441         rm -rf $TDIR
3442 }
3443 run_test 42e "verify sub-RPC writes are not done synchronously"
3444
3445 test_43() {
3446         test_mkdir -p $DIR/$tdir
3447         cp -p /bin/ls $DIR/$tdir/$tfile
3448         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3449         pid=$!
3450         # give multiop a chance to open
3451         sleep 1
3452
3453         $DIR/$tdir/$tfile && error || true
3454         kill -USR1 $pid
3455 }
3456 run_test 43 "execution of file opened for write should return -ETXTBSY"
3457
3458 test_43a() {
3459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3460         test_mkdir -p $DIR/$tdir
3461         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3462                         cp -p multiop $DIR/$tdir/multiop
3463         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3464                         return 1
3465         MULTIOP_PID=$!
3466         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3467         kill -USR1 $MULTIOP_PID || return 2
3468         wait $MULTIOP_PID || return 3
3469         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3470 }
3471 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3472
3473 test_43b() {
3474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3475         test_mkdir -p $DIR/$tdir
3476         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3477                         cp -p multiop $DIR/$tdir/multiop
3478         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3479                         return 1
3480         MULTIOP_PID=$!
3481         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3482         kill -USR1 $MULTIOP_PID || return 2
3483         wait $MULTIOP_PID || return 3
3484         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3485 }
3486 run_test 43b "truncate of file being executed should return -ETXTBSY"
3487
3488 test_43c() {
3489         local testdir="$DIR/$tdir"
3490         test_mkdir -p $DIR/$tdir
3491         cp $SHELL $testdir/
3492         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3493                 ( cd $testdir && md5sum -c)
3494 }
3495 run_test 43c "md5sum of copy into lustre========================"
3496
3497 test_44() {
3498         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3499         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3500         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3501 }
3502 run_test 44 "zero length read from a sparse stripe ============="
3503
3504 test_44a() {
3505     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3506                          awk '{print $2}'`
3507     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3508     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3509     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3510                       awk '{print $2}'`
3511     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3512         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3513     fi
3514
3515     OFFSETS="0 $((stride/2)) $((stride-1))"
3516     for offset in $OFFSETS ; do
3517       for i in `seq 0 $((nstripe-1))`; do
3518         local GLOBALOFFSETS=""
3519         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3520         local myfn=$DIR/d44a-$size
3521         echo "--------writing $myfn at $size"
3522         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3523         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3524         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3525                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3526
3527         for j in `seq 0 $((nstripe-1))`; do
3528             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3529             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3530             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3531         done
3532         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3533                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3534         rm -f $myfn
3535       done
3536     done
3537 }
3538 run_test 44a "test sparse pwrite ==============================="
3539
3540 dirty_osc_total() {
3541         tot=0
3542         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3543                 tot=$(($tot + $d))
3544         done
3545         echo $tot
3546 }
3547 do_dirty_record() {
3548         before=`dirty_osc_total`
3549         echo executing "\"$*\""
3550         eval $*
3551         after=`dirty_osc_total`
3552         echo before $before, after $after
3553 }
3554 test_45() {
3555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3556         f="$DIR/f45"
3557         # Obtain grants from OST if it supports it
3558         echo blah > ${f}_grant
3559         stop_writeback
3560         sync
3561         do_dirty_record "echo blah > $f"
3562         [ $before -eq $after ] && error "write wasn't cached"
3563         do_dirty_record "> $f"
3564         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3565         do_dirty_record "echo blah > $f"
3566         [ $before -eq $after ] && error "write wasn't cached"
3567         do_dirty_record "sync"
3568         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3569         do_dirty_record "echo blah > $f"
3570         [ $before -eq $after ] && error "write wasn't cached"
3571         do_dirty_record "cancel_lru_locks osc"
3572         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3573         start_writeback
3574 }
3575 run_test 45 "osc io page accounting ============================"
3576
3577 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3578 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3579 # objects offset and an assert hit when an rpc was built with 1023's mapped
3580 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3581 test_46() {
3582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3583         f="$DIR/f46"
3584         stop_writeback
3585         sync
3586         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3587         sync
3588         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3589         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3590         sync
3591         start_writeback
3592 }
3593 run_test 46 "dirtying a previously written page ================"
3594
3595 # test_47 is removed "Device nodes check" is moved to test_28
3596
3597 test_48a() { # bug 2399
3598         check_kernel_version 34 || return 0
3599         test_mkdir -p $DIR/$tdir
3600         cd $DIR/$tdir
3601         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3602         test_mkdir $DIR/$tdir || error "recreate directory failed"
3603         touch foo || error "'touch foo' failed after recreating cwd"
3604         test_mkdir $DIR/$tdir/bar ||
3605                      error "'mkdir foo' failed after recreating cwd"
3606         if check_kernel_version 44; then
3607                 touch .foo || error "'touch .foo' failed after recreating cwd"
3608                 test_mkdir $DIR/$tdir/.bar ||
3609                               error "'mkdir .foo' failed after recreating cwd"
3610         fi
3611         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3612         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3613         cd . || error "'cd .' failed after recreating cwd"
3614         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3615         rmdir . && error "'rmdir .' worked after recreating cwd"
3616         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3617         cd .. || error "'cd ..' failed after recreating cwd"
3618 }
3619 run_test 48a "Access renamed working dir (should return errors)="
3620
3621 test_48b() { # bug 2399
3622         check_kernel_version 34 || return 0
3623         rm -rf $DIR/$tdir
3624         test_mkdir -p $DIR/$tdir
3625         cd $DIR/$tdir
3626         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3627         touch foo && error "'touch foo' worked after removing cwd"
3628         test_mkdir $DIR/$tdir/foo &&
3629                      error "'mkdir foo' worked after removing cwd"
3630         if check_kernel_version 44; then
3631                 touch .foo && error "'touch .foo' worked after removing cwd"
3632                 test_mkdir $DIR/$tdir/.foo &&
3633                               error "'mkdir .foo' worked after removing cwd"
3634         fi
3635         ls . > /dev/null && error "'ls .' worked after removing cwd"
3636         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3637         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3638         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3639         rmdir . && error "'rmdir .' worked after removing cwd"
3640         ln -s . foo && error "'ln -s .' worked after removing cwd"
3641         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3642 }
3643 run_test 48b "Access removed working dir (should return errors)="
3644
3645 test_48c() { # bug 2350
3646         check_kernel_version 36 || return 0
3647         #lctl set_param debug=-1
3648         #set -vx
3649         rm -rf $DIR/$tdir
3650         test_mkdir -p $DIR/$tdir/dir
3651         cd $DIR/$tdir/dir
3652         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3653         $TRACE touch foo && error "touch foo worked after removing cwd"
3654         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3655         if check_kernel_version 44; then
3656                 touch .foo && error "touch .foo worked after removing cwd"
3657                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3658         fi
3659         $TRACE ls . && error "'ls .' worked after removing cwd"
3660         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3661         is_patchless || ( $TRACE cd . &&
3662                         error "'cd .' worked after removing cwd" )
3663         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3664         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3665         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3666         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3667 }
3668 run_test 48c "Access removed working subdir (should return errors)"
3669
3670 test_48d() { # bug 2350
3671         check_kernel_version 36 || return 0
3672         #lctl set_param debug=-1
3673         #set -vx
3674         rm -rf $DIR/$tdir
3675         test_mkdir -p $DIR/$tdir/dir
3676         cd $DIR/$tdir/dir
3677         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3678         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3679         $TRACE touch foo && error "'touch foo' worked after removing parent"
3680         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3681         if check_kernel_version 44; then
3682                 touch .foo && error "'touch .foo' worked after removing parent"
3683                 test_mkdir .foo &&
3684                               error "mkdir .foo worked after removing parent"
3685         fi
3686         $TRACE ls . && error "'ls .' worked after removing parent"
3687         $TRACE ls .. && error "'ls ..' worked after removing parent"
3688         is_patchless || ( $TRACE cd . &&
3689                         error "'cd .' worked after recreate parent" )
3690         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3691         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3692         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3693         is_patchless || ( $TRACE cd .. &&
3694                         error "'cd ..' worked after removing parent" || true )
3695 }
3696 run_test 48d "Access removed parent subdir (should return errors)"
3697
3698 test_48e() { # bug 4134
3699         check_kernel_version 41 || return 0
3700         #lctl set_param debug=-1
3701         #set -vx
3702         rm -rf $DIR/$tdir
3703         test_mkdir -p $DIR/$tdir/dir
3704         cd $DIR/$tdir/dir
3705         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3706         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3707         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3708         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3709         # On a buggy kernel addition of "touch foo" after cd .. will
3710         # produce kernel oops in lookup_hash_it
3711         touch ../foo && error "'cd ..' worked after recreate parent"
3712         cd $DIR
3713         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3714 }
3715 run_test 48e "Access to recreated parent subdir (should return errors)"
3716
3717 test_49() { # LU-1030
3718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3719         # get ost1 size - lustre-OST0000
3720         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3721         # write 800M at maximum
3722         [ $ost1_size -gt 819200 ] && ost1_size=819200
3723
3724         lfs setstripe -c 1 -i 0 $DIR/$tfile
3725         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3726         local dd_pid=$!
3727
3728         # change max_pages_per_rpc while writing the file
3729         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3730         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3731         # loop until dd process exits
3732         while ps ax -opid | grep -wq $dd_pid; do
3733                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3734                 sleep $((RANDOM % 5 + 1))
3735         done
3736         # restore original max_pages_per_rpc
3737         $LCTL set_param $osc1_mppc=$orig_mppc
3738         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3739 }
3740 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3741
3742 test_50() {
3743         # bug 1485
3744         test_mkdir $DIR/$tdir
3745         cd $DIR/$tdir
3746         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3747 }
3748 run_test 50 "special situations: /proc symlinks  ==============="
3749
3750 test_51a() {    # was test_51
3751         # bug 1516 - create an empty entry right after ".." then split dir
3752         test_mkdir -p $DIR/$tdir
3753         touch $DIR/$tdir/foo
3754         $MCREATE $DIR/$tdir/bar
3755         rm $DIR/$tdir/foo
3756         createmany -m $DIR/$tdir/longfile 201
3757         FNUM=202
3758         while [ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]; do
3759                 $MCREATE $DIR/$tdir/longfile$FNUM
3760                 FNUM=$(($FNUM + 1))
3761                 echo -n "+"
3762         done
3763         echo
3764         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3765 }
3766 run_test 51a "special situations: split htree with empty entry =="
3767
3768 export NUMTEST=70000
3769 test_51b() {
3770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3771         local BASE=$DIR/d${base}.${TESTSUITE}
3772
3773         # cleanup the directory
3774         rm -fr $BASE
3775
3776         test_mkdir -p $BASE
3777
3778         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3779         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3780         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3781                 return
3782
3783         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3784                 echo "reduced count to $NUMTEST due to inodes"
3785
3786         # need to check free space for the directories as well
3787         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3788         numfree=$((blkfree / 4))
3789         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3790                 echo "reduced count to $NUMTEST due to blocks"
3791
3792         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3793                 echo "failed" > $BASE/fnum
3794 }
3795 run_test 51b "exceed 64k subdirectory nlink limit"
3796
3797 test_51ba() { # LU-993
3798         local BASE=$DIR/d${base}.${TESTSUITE}
3799         # unlink all but 100 subdirectories, then check it still works
3800         local LEFT=100
3801         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3802
3803         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3804         local DELETE=$((NUMTEST - LEFT))
3805
3806         # continue on to run this test even if 51b didn't finish,
3807         # just to delete the many subdirectories created.
3808         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3809
3810         # for ldiskfs the nlink count should be 1, but this is OSD specific
3811         # and so this is listed for informational purposes only
3812         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3813         unlinkmany -d $BASE/d $DELETE
3814         RC=$?
3815
3816         if [ $RC -ne 0 ]; then
3817                 if [ "$NUMPREV" == "failed" ]; then
3818                         skip "previous setup failed"
3819                         return 0
3820                 else
3821                         error "unlink of first $DELETE subdirs failed"
3822                         return $RC
3823                 fi
3824         fi
3825
3826         echo "nlink between: $(stat -c %h $BASE)"
3827         # trim the first line of ls output
3828         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3829         [ $FOUND -ne $LEFT ] &&
3830                 error "can't find subdirs: found only $FOUND/$LEFT"
3831
3832         unlinkmany -d $BASE/d $DELETE $LEFT ||
3833                 error "unlink of second $LEFT subdirs failed"
3834         # regardless of whether the backing filesystem tracks nlink accurately
3835         # or not, the nlink count shouldn't be more than "." and ".." here
3836         local AFTER=$(stat -c %h $BASE)
3837         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3838                 echo "nlink after: $AFTER"
3839 }
3840 run_test 51ba "verify nlink for many subdirectory cleanup"
3841
3842 test_51d() {
3843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3844         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3845         test_mkdir -p $DIR/$tdir
3846         createmany -o $DIR/$tdir/t- 1000
3847         $GETSTRIPE $DIR/$tdir > $TMP/files
3848         for N in `seq 0 $((OSTCOUNT - 1))`; do
3849             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3850             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3851             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3852         done
3853         unlinkmany $DIR/$tdir/t- 1000
3854
3855         NLAST=0
3856         for N in `seq 1 $((OSTCOUNT - 1))`; do
3857             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3858                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3859             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3860                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3861
3862             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3863                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3864             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3865                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3866             NLAST=$N
3867         done
3868 }
3869 run_test 51d "check object distribution ===================="
3870
3871 test_52a() {
3872         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3873         test_mkdir -p $DIR/$tdir
3874         touch $DIR/$tdir/foo
3875         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3876         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3877         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3878         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3879         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3880                                         error "link worked"
3881         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3882         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3883         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3884                                                      error "lsattr"
3885         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3886         cp -r $DIR/$tdir /tmp/
3887         rm -fr $DIR/$tdir || error "cleanup rm failed"
3888 }
3889 run_test 52a "append-only flag test (should return errors) ====="
3890
3891 test_52b() {
3892         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3893         test_mkdir -p $DIR/$tdir
3894         touch $DIR/$tdir/foo
3895         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3896         cat test > $DIR/$tdir/foo && error "cat test worked"
3897         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3898         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3899         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3900                                         error "link worked"
3901         echo foo >> $DIR/$tdir/foo && error "echo worked"
3902         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3903         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
3904         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
3905         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3906                                                         error "lsattr"
3907         chattr -i $DIR/$tdir/foo || error "chattr failed"
3908
3909         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
3910 }
3911 run_test 52b "immutable flag test (should return errors) ======="
3912
3913 test_53() {
3914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3915         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3916         remote_ost_nodsh && skip "remote OST with nodsh" && return
3917
3918         local param
3919         local param_seq
3920         local ostname
3921         local mds_last
3922         local mds_last_seq
3923         local ost_last
3924         local ost_last_seq
3925         local ost_last_id
3926         local ostnum
3927         local node
3928         local found=false
3929         local support_last_seq=true
3930
3931         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
3932                 support_last_seq=false
3933
3934         # only test MDT0000
3935         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3936         local value
3937         for value in $(do_facet $SINGLEMDS \
3938                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
3939                 param=$(echo ${value[0]} | cut -d "=" -f1)
3940                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
3941
3942                 if $support_last_seq; then
3943                         param_seq=$(echo $param |
3944                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
3945                         mds_last_seq=$(do_facet $SINGLEMDS \
3946                                        $LCTL get_param -n $param_seq)
3947                 fi
3948                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
3949
3950                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3951                 node=$(facet_active_host ost$((ostnum+1)))
3952                 param="obdfilter.$ostname.last_id"
3953                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
3954                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
3955                         ost_last_id=$ost_last
3956
3957                         if $support_last_seq; then
3958                                 ost_last_id=$(echo $ost_last |
3959                                               awk -F':' '{print $2}' |
3960                                               sed -e "s/^0x//g")
3961                                 ost_last_seq=$(echo $ost_last |
3962                                                awk -F':' '{print $1}')
3963                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
3964                         fi
3965
3966                         if [[ $ost_last_id != $mds_last ]]; then
3967                                 error "$ost_last_id != $mds_last"
3968                         else
3969                                 found=true
3970                                 break
3971                         fi
3972                 done
3973         done
3974         $found || error "can not match last_seq/last_id for $mdtosc"
3975         return 0
3976 }
3977 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3978
3979 test_54a() {
3980         $SOCKETSERVER $DIR/socket ||
3981                 error "$SOCKETSERVER $DIR/socket failed: $?"
3982         $SOCKETCLIENT $DIR/socket ||
3983                 error "$SOCKETCLIENT $DIR/socket failed: $?"
3984         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
3985 }
3986 run_test 54a "unix domain socket test =========================="
3987
3988 test_54b() {
3989         f="$DIR/f54b"
3990         mknod $f c 1 3
3991         chmod 0666 $f
3992         dd if=/dev/zero of=$f bs=$(page_size) count=1
3993 }
3994 run_test 54b "char device works in lustre ======================"
3995
3996 find_loop_dev() {
3997         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3998         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3999         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4000
4001         for i in $(seq 3 7); do
4002                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4003                 LOOPDEV=$LOOPBASE$i
4004                 LOOPNUM=$i
4005                 break
4006         done
4007 }
4008
4009 cleanup_54c() {
4010         loopdev="$DIR/loop54c"
4011
4012         trap 0
4013         $UMOUNT -d $tdir || rc=$?
4014         losetup -d $loopdev || true
4015         rm $loopdev
4016         return $rc
4017 }
4018
4019 test_54c() {
4020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4021         tfile="$DIR/f54c"
4022         tdir="$DIR/d54c"
4023         loopdev="$DIR/loop54c"
4024
4025         find_loop_dev
4026         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4027         mknod $loopdev b 7 $LOOPNUM
4028         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
4029         dd if=/dev/zero of=$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4030         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
4031         trap cleanup_54c EXIT
4032         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4033         test_mkdir -p $tdir
4034         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
4035         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
4036         df $tdir
4037         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
4038         cleanup_54c
4039 }
4040 run_test 54c "block device works in lustre ====================="
4041
4042 test_54d() {
4043         f="$DIR/f54d"
4044         string="aaaaaa"
4045         mknod $f p
4046         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4047 }
4048 run_test 54d "fifo device works in lustre ======================"
4049
4050 test_54e() {
4051         check_kernel_version 46 || return 0
4052         f="$DIR/f54e"
4053         string="aaaaaa"
4054         cp -aL /dev/console $f
4055         echo $string > $f || error "echo $string to $f failed"
4056 }
4057 run_test 54e "console/tty device works in lustre ======================"
4058
4059 #The test_55 used to be iopen test and it was removed by bz#24037.
4060 #run_test 55 "check iopen_connect_dentry() ======================"
4061
4062 test_56a() {    # was test_56
4063         rm -rf $DIR/$tdir
4064         $SETSTRIPE -d $DIR
4065         test_mkdir -p $DIR/$tdir/dir
4066         NUMFILES=3
4067         NUMFILESx2=$(($NUMFILES * 2))
4068         for i in `seq 1 $NUMFILES` ; do
4069                 touch $DIR/$tdir/file$i
4070                 touch $DIR/$tdir/dir/file$i
4071         done
4072
4073         # test lfs getstripe with --recursive
4074         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
4075         [ $FILENUM -eq $NUMFILESx2 ] ||
4076                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4077         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
4078         [ $FILENUM -eq $NUMFILES ] ||
4079                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4080         echo "$GETSTRIPE --recursive passed."
4081
4082         # test lfs getstripe with file instead of dir
4083         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
4084         [ $FILENUM  -eq 1 ] || error \
4085                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4086         echo "$GETSTRIPE file1 passed."
4087
4088         #test lfs getstripe with --verbose
4089         [ `$GETSTRIPE --verbose $DIR/$tdir |
4090                         grep -c lmm_magic` -eq $NUMFILES ] ||
4091                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4092         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
4093             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4094         echo "$GETSTRIPE --verbose passed."
4095
4096         #test lfs getstripe with --obd
4097         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4098                                         grep -q "unknown obduuid" ||
4099                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4100
4101         [  "$OSTCOUNT" -lt 2 ] &&
4102                 skip_env "skipping other $GETSTRIPE --obd test" && return
4103
4104         OSTIDX=1
4105         OBDUUID=$(ostuuid_from_index $OSTIDX)
4106         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
4107         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
4108         [ $FOUND -eq $FILENUM ] ||
4109                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4110         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4111                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4112                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
4113                 error "$GETSTRIPE --obd: should not show file on other obd"
4114         echo "$GETSTRIPE --obd passed"
4115 }
4116 run_test 56a "check $GETSTRIPE"
4117
4118 NUMFILES=3
4119 NUMDIRS=3
4120 setup_56() {
4121         local LOCAL_NUMFILES="$1"
4122         local LOCAL_NUMDIRS="$2"
4123         local MKDIR_PARAMS="$3"
4124         local DIR_STRIPE_PARAMS="$4"
4125
4126         if [ ! -d "$TDIR" ] ; then
4127                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4128                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4129                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4130                         touch $TDIR/file$i
4131                 done
4132                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4133                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4134                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4135                                 touch $TDIR/dir$i/file$j
4136                         done
4137                 done
4138         fi
4139 }
4140
4141 setup_56_special() {
4142         LOCAL_NUMFILES=$1
4143         LOCAL_NUMDIRS=$2
4144         setup_56 $1 $2
4145         if [ ! -e "$TDIR/loop1b" ] ; then
4146                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4147                         mknod $TDIR/loop${i}b b 7 $i
4148                         mknod $TDIR/null${i}c c 1 3
4149                         ln -s $TDIR/file1 $TDIR/link${i}l
4150                 done
4151                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4152                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4153                         mknod $TDIR/dir$i/null${i}c c 1 3
4154                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4155                 done
4156         fi
4157 }
4158
4159 test_56g() {
4160         $SETSTRIPE -d $DIR
4161
4162         TDIR=$DIR/${tdir}g
4163         setup_56 $NUMFILES $NUMDIRS
4164
4165         EXPECTED=$(($NUMDIRS + 2))
4166         # test lfs find with -name
4167         for i in $(seq 1 $NUMFILES) ; do
4168                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4169                 [ $NUMS -eq $EXPECTED ] ||
4170                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4171                               "found $NUMS, expected $EXPECTED"
4172         done
4173 }
4174 run_test 56g "check lfs find -name ============================="
4175
4176 test_56h() {
4177         $SETSTRIPE -d $DIR
4178
4179         TDIR=$DIR/${tdir}g
4180         setup_56 $NUMFILES $NUMDIRS
4181
4182         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4183         # test lfs find with ! -name
4184         for i in $(seq 1 $NUMFILES) ; do
4185                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4186                 [ $NUMS -eq $EXPECTED ] ||
4187                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4188                               "found $NUMS, expected $EXPECTED"
4189         done
4190 }
4191 run_test 56h "check lfs find ! -name ============================="
4192
4193 test_56i() {
4194        tdir=${tdir}i
4195        test_mkdir -p $DIR/$tdir
4196        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4197        CMD="$LFIND -ost $UUID $DIR/$tdir"
4198        OUT=$($CMD)
4199        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4200 }
4201 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4202
4203 test_56j() {
4204         TDIR=$DIR/${tdir}g
4205         setup_56_special $NUMFILES $NUMDIRS
4206
4207         EXPECTED=$((NUMDIRS + 1))
4208         CMD="$LFIND -type d $TDIR"
4209         NUMS=$($CMD | wc -l)
4210         [ $NUMS -eq $EXPECTED ] ||
4211                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4212 }
4213 run_test 56j "check lfs find -type d ============================="
4214
4215 test_56k() {
4216         TDIR=$DIR/${tdir}g
4217         setup_56_special $NUMFILES $NUMDIRS
4218
4219         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4220         CMD="$LFIND -type f $TDIR"
4221         NUMS=$($CMD | wc -l)
4222         [ $NUMS -eq $EXPECTED ] ||
4223                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4224 }
4225 run_test 56k "check lfs find -type f ============================="
4226
4227 test_56l() {
4228         TDIR=$DIR/${tdir}g
4229         setup_56_special $NUMFILES $NUMDIRS
4230
4231         EXPECTED=$((NUMDIRS + NUMFILES))
4232         CMD="$LFIND -type b $TDIR"
4233         NUMS=$($CMD | wc -l)
4234         [ $NUMS -eq $EXPECTED ] ||
4235                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4236 }
4237 run_test 56l "check lfs find -type b ============================="
4238
4239 test_56m() {
4240         TDIR=$DIR/${tdir}g
4241         setup_56_special $NUMFILES $NUMDIRS
4242
4243         EXPECTED=$((NUMDIRS + NUMFILES))
4244         CMD="$LFIND -type c $TDIR"
4245         NUMS=$($CMD | wc -l)
4246         [ $NUMS -eq $EXPECTED ] ||
4247                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4248 }
4249 run_test 56m "check lfs find -type c ============================="
4250
4251 test_56n() {
4252         TDIR=$DIR/${tdir}g
4253         setup_56_special $NUMFILES $NUMDIRS
4254
4255         EXPECTED=$((NUMDIRS + NUMFILES))
4256         CMD="$LFIND -type l $TDIR"
4257         NUMS=$($CMD | wc -l)
4258         [ $NUMS -eq $EXPECTED ] ||
4259                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4260 }
4261 run_test 56n "check lfs find -type l ============================="
4262
4263 test_56o() {
4264         TDIR=$DIR/${tdir}o
4265         setup_56 $NUMFILES $NUMDIRS
4266         utime $TDIR/file1 > /dev/null || error "utime (1)"
4267         utime $TDIR/file2 > /dev/null || error "utime (2)"
4268         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4269         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4270         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4271         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4272
4273         EXPECTED=4
4274         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4275         [ $NUMS -eq $EXPECTED ] || \
4276                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4277
4278         EXPECTED=12
4279         CMD="$LFIND -mtime 0 $TDIR"
4280         NUMS=$($CMD | wc -l)
4281         [ $NUMS -eq $EXPECTED ] ||
4282                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4283 }
4284 run_test 56o "check lfs find -mtime for old files =========================="
4285
4286 test_56p() {
4287         [ $RUNAS_ID -eq $UID ] &&
4288                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4289
4290         TDIR=$DIR/${tdir}p
4291         setup_56 $NUMFILES $NUMDIRS
4292
4293         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4294         EXPECTED=$NUMFILES
4295         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4296         NUMS=$($CMD | wc -l)
4297         [ $NUMS -eq $EXPECTED ] || \
4298                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4299
4300         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4301         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4302         NUMS=$($CMD | wc -l)
4303         [ $NUMS -eq $EXPECTED ] || \
4304                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4305 }
4306 run_test 56p "check lfs find -uid and ! -uid ==============================="
4307
4308 test_56q() {
4309         [ $RUNAS_ID -eq $UID ] &&
4310                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4311
4312         TDIR=$DIR/${tdir}q
4313         setup_56 $NUMFILES $NUMDIRS
4314
4315         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4316
4317         EXPECTED=$NUMFILES
4318         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4319         NUMS=$($CMD | wc -l)
4320         [ $NUMS -eq $EXPECTED ] ||
4321                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4322
4323         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4324         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4325         NUMS=$($CMD | wc -l)
4326         [ $NUMS -eq $EXPECTED ] ||
4327                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4328 }
4329 run_test 56q "check lfs find -gid and ! -gid ==============================="
4330
4331 test_56r() {
4332         TDIR=$DIR/${tdir}r
4333         setup_56 $NUMFILES $NUMDIRS
4334
4335         EXPECTED=12
4336         CMD="$LFIND -size 0 -type f $TDIR"
4337         NUMS=$($CMD | wc -l)
4338         [ $NUMS -eq $EXPECTED ] ||
4339                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4340         EXPECTED=0
4341         CMD="$LFIND ! -size 0 -type f $TDIR"
4342         NUMS=$($CMD | wc -l)
4343         [ $NUMS -eq $EXPECTED ] ||
4344                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4345         echo "test" > $TDIR/$tfile
4346         echo "test2" > $TDIR/$tfile.2 && sync
4347         EXPECTED=1
4348         CMD="$LFIND -size 5 -type f $TDIR"
4349         NUMS=$($CMD | wc -l)
4350         [ $NUMS -eq $EXPECTED ] ||
4351                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4352         EXPECTED=1
4353         CMD="$LFIND -size +5 -type f $TDIR"
4354         NUMS=$($CMD | wc -l)
4355         [ $NUMS -eq $EXPECTED ] ||
4356                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4357         EXPECTED=2
4358         CMD="$LFIND -size +0 -type f $TDIR"
4359         NUMS=$($CMD | wc -l)
4360         [ $NUMS -eq $EXPECTED ] ||
4361                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4362         EXPECTED=2
4363         CMD="$LFIND ! -size -5 -type f $TDIR"
4364         NUMS=$($CMD | wc -l)
4365         [ $NUMS -eq $EXPECTED ] ||
4366                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4367         EXPECTED=12
4368         CMD="$LFIND -size -5 -type f $TDIR"
4369         NUMS=$($CMD | wc -l)
4370         [ $NUMS -eq $EXPECTED ] ||
4371                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4372 }
4373 run_test 56r "check lfs find -size works =========================="
4374
4375 test_56s() { # LU-611
4376         TDIR=$DIR/${tdir}s
4377         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4378
4379         if [ $OSTCOUNT -gt 1 ]; then
4380                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4381                 ONESTRIPE=4
4382                 EXTRA=4
4383         else
4384                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4385                 EXTRA=0
4386         fi
4387
4388         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4389         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4390         NUMS=$($CMD | wc -l)
4391         [ $NUMS -eq $EXPECTED ] ||
4392                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4393
4394         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4395         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4396         NUMS=$($CMD | wc -l)
4397         [ $NUMS -eq $EXPECTED ] ||
4398                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4399
4400         EXPECTED=$ONESTRIPE
4401         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4402         NUMS=$($CMD | wc -l)
4403         [ $NUMS -eq $EXPECTED ] ||
4404                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4405
4406         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4407         NUMS=$($CMD | wc -l)
4408         [ $NUMS -eq $EXPECTED ] ||
4409                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4410
4411         EXPECTED=0
4412         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4413         NUMS=$($CMD | wc -l)
4414         [ $NUMS -eq $EXPECTED ] ||
4415                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4416 }
4417 run_test 56s "check lfs find -stripe-count works"
4418
4419 test_56t() { # LU-611
4420         TDIR=$DIR/${tdir}t
4421         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4422
4423         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4424
4425         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4426         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4427         NUMS=$($CMD | wc -l)
4428         [ $NUMS -eq $EXPECTED ] ||
4429                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4430
4431         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4432         NUMS=$($CMD | wc -l)
4433         [ $NUMS -eq $EXPECTED ] ||
4434                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4435
4436         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4437         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4438         NUMS=$($CMD | wc -l)
4439         [ $NUMS -eq $EXPECTED ] ||
4440                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4441
4442         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4443         NUMS=$($CMD | wc -l)
4444         [ $NUMS -eq $EXPECTED ] ||
4445                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4446
4447         EXPECTED=4
4448         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4449         NUMS=$($CMD | wc -l)
4450         [ $NUMS -eq $EXPECTED ] ||
4451                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4452
4453         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4454         NUMS=$($CMD | wc -l)
4455         [ $NUMS -eq $EXPECTED ] ||
4456                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4457
4458         EXPECTED=0
4459         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4460         NUMS=$($CMD | wc -l)
4461         [ $NUMS -eq $EXPECTED ] ||
4462                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4463 }
4464 run_test 56t "check lfs find -stripe-size works"
4465
4466 test_56u() { # LU-611
4467         TDIR=$DIR/${tdir}u
4468         setup_56 $NUMFILES $NUMDIRS "-i 0"
4469
4470         if [ $OSTCOUNT -gt 1 ]; then
4471                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4472                 ONESTRIPE=4
4473         else
4474                 ONESTRIPE=0
4475         fi
4476
4477         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4478         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4479         NUMS=$($CMD | wc -l)
4480         [ $NUMS -eq $EXPECTED ] ||
4481                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4482
4483         EXPECTED=$ONESTRIPE
4484         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4485         NUMS=$($CMD | wc -l)
4486         [ $NUMS -eq $EXPECTED ] ||
4487                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4488
4489         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4490         NUMS=$($CMD | wc -l)
4491         [ $NUMS -eq $EXPECTED ] ||
4492                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4493
4494         EXPECTED=0
4495         # This should produce an error and not return any files
4496         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4497         NUMS=$($CMD 2>/dev/null | wc -l)
4498         [ $NUMS -eq $EXPECTED ] ||
4499                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4500
4501         if [ $OSTCOUNT -gt 1 ]; then
4502                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4503                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4504                 NUMS=$($CMD | wc -l)
4505                 [ $NUMS -eq $EXPECTED ] ||
4506                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4507         fi
4508 }
4509 run_test 56u "check lfs find -stripe-index works"
4510
4511 test_56v() {
4512     local MDT_IDX=0
4513
4514     TDIR=$DIR/${tdir}v
4515     rm -rf $TDIR
4516     setup_56 $NUMFILES $NUMDIRS
4517
4518     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4519     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4520
4521     for file in $($LFIND -mdt $UUID $TDIR); do
4522         file_mdt_idx=$($GETSTRIPE -M $file)
4523         [ $file_mdt_idx -eq $MDT_IDX ] ||
4524             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4525     done
4526 }
4527 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4528
4529 # Get and check the actual stripe count of one file.
4530 # Usage: check_stripe_count <file> <expected_stripe_count>
4531 check_stripe_count() {
4532     local file=$1
4533     local expected=$2
4534     local actual
4535
4536     [[ -z "$file" || -z "$expected" ]] &&
4537         error "check_stripe_count: invalid argument!"
4538
4539     local cmd="$GETSTRIPE -c $file"
4540     actual=$($cmd) || error "$cmd failed"
4541     actual=${actual%% *}
4542
4543     if [[ $actual -ne $expected ]]; then
4544         [[ $expected -eq -1 ]] ||
4545             error "$cmd wrong: found $actual, expected $expected"
4546         [[ $actual -eq $OSTCOUNT ]] ||
4547             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4548     fi
4549 }
4550
4551 test_56w() {
4552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4553         TDIR=$DIR/${tdir}w
4554
4555     rm -rf $TDIR || error "remove $TDIR failed"
4556     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4557
4558     local stripe_size
4559     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4560         error "$GETSTRIPE -S -d $TDIR failed"
4561     stripe_size=${stripe_size%% *}
4562
4563     local file_size=$((stripe_size * OSTCOUNT))
4564     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4565     local required_space=$((file_num * file_size))
4566     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4567     [[ $free_space -le $((required_space / 1024)) ]] &&
4568         skip_env "need at least $required_space bytes free space," \
4569                  "have $free_space kbytes" && return
4570
4571     local dd_bs=65536
4572     local dd_count=$((file_size / dd_bs))
4573
4574     # write data into the files
4575     local i
4576     local j
4577     local file
4578     for i in $(seq 1 $NUMFILES); do
4579         file=$TDIR/file$i
4580         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4581             error "write data into $file failed"
4582     done
4583     for i in $(seq 1 $NUMDIRS); do
4584         for j in $(seq 1 $NUMFILES); do
4585             file=$TDIR/dir$i/file$j
4586             yes | dd bs=$dd_bs count=$dd_count of=$file \
4587                 >/dev/null 2>&1 ||
4588                 error "write data into $file failed"
4589         done
4590     done
4591
4592     local expected=-1
4593     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4594
4595     # lfs_migrate file
4596     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4597     echo "$cmd"
4598     eval $cmd || error "$cmd failed"
4599
4600     check_stripe_count $TDIR/file1 $expected
4601
4602     # lfs_migrate dir
4603     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4604     echo "$cmd"
4605     eval $cmd || error "$cmd failed"
4606
4607     for j in $(seq 1 $NUMFILES); do
4608         check_stripe_count $TDIR/dir1/file$j $expected
4609     done
4610
4611     # lfs_migrate works with lfs find
4612     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4613          $LFS_MIGRATE -y -c $expected"
4614     echo "$cmd"
4615     eval $cmd || error "$cmd failed"
4616
4617     for i in $(seq 2 $NUMFILES); do
4618         check_stripe_count $TDIR/file$i $expected
4619     done
4620     for i in $(seq 2 $NUMDIRS); do
4621         for j in $(seq 1 $NUMFILES); do
4622             check_stripe_count $TDIR/dir$i/file$j $expected
4623         done
4624     done
4625 }
4626 run_test 56w "check lfs_migrate -c stripe_count works"
4627
4628 test_56x() {
4629         check_swap_layouts_support && return 0
4630         [ "$OSTCOUNT" -lt "2" ] &&
4631                 skip_env "need 2 OST, skipping test" && return
4632
4633         local dir0=$DIR/$tdir/$testnum
4634         mkdir -p $dir0 || error "creating dir $dir0"
4635
4636         local ref1=/etc/passwd
4637         local file1=$dir0/file1
4638
4639         $SETSTRIPE -c 2 $file1
4640         cp $ref1 $file1
4641         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4642         stripe=$($GETSTRIPE -c $file1)
4643         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4644         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4645
4646         # clean up
4647         rm -f $file1
4648 }
4649 run_test 56x "lfs migration support"
4650
4651 test_56y() {
4652         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4653                 skip "No HSM support on MDS of $(get_lustre_version)," \
4654                          "need 2.4.53 at least" && return
4655         local res=""
4656
4657         local dir0=$DIR/$tdir/$testnum
4658         mkdir -p $dir0 || error "creating dir $dir0"
4659         local f1=$dir0/file1
4660         local f2=$dir0/file2
4661
4662         touch $f1 || error "creating std file $f1"
4663         $MULTIOP $f2 H2c || error "creating released file $f2"
4664
4665         # a directory can be raid0, so ask only for files
4666         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4667         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4668
4669         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4670         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4671
4672         # only files can be released, so no need to force file search
4673         res=$($LFIND $dir0 -L released)
4674         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4675
4676         res=$($LFIND $dir0 \! -L released)
4677         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4678
4679 }
4680 run_test 56y "lfs find -L raid0|released"
4681
4682 test_57a() {
4683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4684         # note test will not do anything if MDS is not local
4685         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4686                 skip "Only applicable to ldiskfs-based MDTs"
4687                 return
4688         fi
4689
4690         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4691         local MNTDEV="osd*.*MDT*.mntdev"
4692         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4693         [ -z "$DEV" ] && error "can't access $MNTDEV"
4694         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4695                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4696                         error "can't access $DEV"
4697                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4698                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4699                 rm $TMP/t57a.dump
4700         done
4701 }
4702 run_test 57a "verify MDS filesystem created with large inodes =="
4703
4704 test_57b() {
4705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4706         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4707                 skip "Only applicable to ldiskfs-based MDTs"
4708                 return
4709         fi
4710
4711         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4712         local dir=$DIR/d57b
4713
4714         local FILECOUNT=100
4715         local FILE1=$dir/f1
4716         local FILEN=$dir/f$FILECOUNT
4717
4718         rm -rf $dir || error "removing $dir"
4719         test_mkdir -p $dir || error "creating $dir"
4720         local num=$(get_mds_dir $dir)
4721         local mymds=mds$num
4722
4723         echo "mcreating $FILECOUNT files"
4724         createmany -m $dir/f 1 $FILECOUNT || \
4725                 error "creating files in $dir"
4726
4727         # verify that files do not have EAs yet
4728         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4729         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4730
4731         sync
4732         sleep 1
4733         df $dir  #make sure we get new statfs data
4734         local MDSFREE=$(do_facet $mymds \
4735                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4736         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4737         echo "opening files to create objects/EAs"
4738         local FILE
4739         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4740                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4741         done
4742
4743         # verify that files have EAs now
4744         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4745         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4746
4747         sleep 1  #make sure we get new statfs data
4748         df $dir
4749         local MDSFREE2=$(do_facet $mymds \
4750                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4751         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4752         if [ "$MDCFREE2" -lt "$((MDCFREE - 16))" ]; then
4753                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4754                         error "MDC before $MDCFREE != after $MDCFREE2"
4755                 else
4756                         echo "MDC before $MDCFREE != after $MDCFREE2"
4757                         echo "unable to confirm if MDS has large inodes"
4758                 fi
4759         fi
4760         rm -rf $dir
4761 }
4762 run_test 57b "default LOV EAs are stored inside large inodes ==="
4763
4764 test_58() {
4765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4766         [ -z "$(which wiretest 2>/dev/null)" ] &&
4767                         skip_env "could not find wiretest" && return
4768         wiretest
4769 }
4770 run_test 58 "verify cross-platform wire constants =============="
4771
4772 test_59() {
4773         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4774         echo "touch 130 files"
4775         createmany -o $DIR/f59- 130
4776         echo "rm 130 files"
4777         unlinkmany $DIR/f59- 130
4778         sync
4779         # wait for commitment of removal
4780         wait_delete_completed
4781 }
4782 run_test 59 "verify cancellation of llog records async ========="
4783
4784 TEST60_HEAD="test_60 run $RANDOM"
4785 test_60a() {
4786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4787         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4788         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4789                 skip_env "missing subtest run-llog.sh" && return
4790         log "$TEST60_HEAD - from kernel mode"
4791         do_facet mgs sh run-llog.sh
4792 }
4793 run_test 60a "llog sanity tests run from kernel module =========="
4794
4795 test_60b() { # bug 6411
4796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4797         dmesg > $DIR/$tfile
4798         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4799                                  /llog.test/ {
4800                                          if (marker)
4801                                                  from_marker++
4802                                          from_begin++
4803                                  }
4804                                  END {
4805                                          if (marker)
4806                                                  print from_marker
4807                                          else
4808                                                  print from_begin
4809                                  }"`
4810         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4811 }
4812 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4813
4814 test_60c() {
4815         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4816         echo "create 5000 files"
4817         createmany -o $DIR/f60c- 5000
4818 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4819         lctl set_param fail_loc=0x80000137
4820         unlinkmany $DIR/f60c- 5000
4821         lctl set_param fail_loc=0
4822 }
4823 run_test 60c "unlink file when mds full"
4824
4825 test_60d() {
4826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4827         SAVEPRINTK=$(lctl get_param -n printk)
4828
4829         # verify "lctl mark" is even working"
4830         MESSAGE="test message ID $RANDOM $$"
4831         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4832         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4833
4834         lctl set_param printk=0 || error "set lnet.printk failed"
4835         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4836         MESSAGE="new test message ID $RANDOM $$"
4837         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4838         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4839         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4840
4841         lctl set_param -n printk="$SAVEPRINTK"
4842 }
4843 run_test 60d "test printk console message masking"
4844
4845 test_61() {
4846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4847         f="$DIR/f61"
4848         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
4849         cancel_lru_locks osc
4850         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
4851         sync
4852 }
4853 run_test 61 "mmap() writes don't make sync hang ================"
4854
4855 # bug 2330 - insufficient obd_match error checking causes LBUG
4856 test_62() {
4857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4858         f="$DIR/f62"
4859         echo foo > $f
4860         cancel_lru_locks osc
4861         lctl set_param fail_loc=0x405
4862         cat $f && error "cat succeeded, expect -EIO"
4863         lctl set_param fail_loc=0
4864 }
4865 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4866 # match every page all of the time.
4867 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4868
4869 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4870 # Though this test is irrelevant anymore, it helped to reveal some
4871 # other grant bugs (LU-4482), let's keep it.
4872 test_63a() {   # was test_63
4873         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4874         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4875         for i in `seq 10` ; do
4876                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4877                 sleep 5
4878                 kill $!
4879                 sleep 1
4880         done
4881
4882         rm -f $DIR/f63 || true
4883 }
4884 run_test 63a "Verify oig_wait interruption does not crash ======="
4885
4886 # bug 2248 - async write errors didn't return to application on sync
4887 # bug 3677 - async write errors left page locked
4888 test_63b() {
4889         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4890         debugsave
4891         lctl set_param debug=-1
4892
4893         # ensure we have a grant to do async writes
4894         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4895         rm $DIR/$tfile
4896
4897         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4898         lctl set_param fail_loc=0x80000406
4899         $MULTIOP $DIR/$tfile Owy && \
4900                 error "sync didn't return ENOMEM"
4901         sync; sleep 2; sync     # do a real sync this time to flush page
4902         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4903                 error "locked page left in cache after async error" || true
4904         debugrestore
4905 }
4906 run_test 63b "async write errors should be returned to fsync ==="
4907
4908 test_64a () {
4909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4910         df $DIR
4911         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4912 }
4913 run_test 64a "verify filter grant calculations (in kernel) ====="
4914
4915 test_64b () {
4916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4917         sh oos.sh $MOUNT || error "oos.sh failed: $?"
4918 }
4919 run_test 64b "check out-of-space detection on client ==========="
4920
4921 test_64c() {
4922         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4923 }
4924 run_test 64c "verify grant shrink ========================------"
4925
4926 # bug 1414 - set/get directories' stripe info
4927 test_65a() {
4928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4929         test_mkdir -p $DIR/$tdir
4930         touch $DIR/$tdir/f1
4931         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4932 }
4933 run_test 65a "directory with no stripe info ===================="
4934
4935 test_65b() {
4936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4937         test_mkdir -p $DIR/$tdir
4938         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4939                                                 error "setstripe"
4940         touch $DIR/$tdir/f2
4941         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4942 }
4943 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4944
4945 test_65c() {
4946         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4947         if [ $OSTCOUNT -gt 1 ]; then
4948                 test_mkdir -p $DIR/$tdir
4949                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4950                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4951                 touch $DIR/$tdir/f3
4952                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4953         fi
4954 }
4955 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4956
4957 test_65d() {
4958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4959         test_mkdir -p $DIR/$tdir
4960         if [ $STRIPECOUNT -le 0 ]; then
4961                 sc=1
4962         elif [ $STRIPECOUNT -gt 2000 ]; then
4963 #LOV_MAX_STRIPE_COUNT is 2000
4964                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4965         else
4966                 sc=$(($STRIPECOUNT - 1))
4967         fi
4968         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4969         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4970         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4971                                                 error "lverify failed"
4972 }
4973 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4974
4975 test_65e() {
4976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4977         test_mkdir -p $DIR/$tdir
4978
4979         $SETSTRIPE $DIR/$tdir || error "setstripe"
4980         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4981                                         error "no stripe info failed"
4982         touch $DIR/$tdir/f6
4983         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4984 }
4985 run_test 65e "directory setstripe defaults ======================="
4986
4987 test_65f() {
4988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4989         test_mkdir -p $DIR/${tdir}f
4990         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4991 }
4992 run_test 65f "dir setstripe permission (should return error) ==="
4993
4994 test_65g() {
4995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4996         test_mkdir -p $DIR/$tdir
4997         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4998                                                         error "setstripe"
4999         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5000         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5001                 error "delete default stripe failed"
5002 }
5003 run_test 65g "directory setstripe -d ==========================="
5004
5005 test_65h() {
5006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5007         test_mkdir -p $DIR/$tdir
5008         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5009                                                         error "setstripe"
5010         test_mkdir -p $DIR/$tdir/dd1
5011         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5012                 error "stripe info inherit failed"
5013 }
5014 run_test 65h "directory stripe info inherit ===================="
5015
5016 test_65i() { # bug6367
5017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5018         $SETSTRIPE -S 65536 -c -1 $MOUNT
5019 }
5020 run_test 65i "set non-default striping on root directory (bug 6367)="
5021
5022 test_65ia() { # bug12836
5023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5024         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5025 }
5026 run_test 65ia "getstripe on -1 default directory striping"
5027
5028 test_65ib() { # bug12836
5029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5030         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5031 }
5032 run_test 65ib "getstripe -v on -1 default directory striping"
5033
5034 test_65ic() { # bug12836
5035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5036         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5037 }
5038 run_test 65ic "new find on -1 default directory striping"
5039
5040 test_65j() { # bug6367
5041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5042         sync; sleep 1
5043         # if we aren't already remounting for each test, do so for this test
5044         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5045                 cleanup || error "failed to unmount"
5046                 setup
5047         fi
5048         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5049 }
5050 run_test 65j "set default striping on root directory (bug 6367)="
5051
5052 test_65k() { # bug11679
5053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5054         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
5055         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5056
5057     echo "Check OST status: "
5058     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5059               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5060
5061     for OSC in $MDS_OSCS; do
5062         echo $OSC "is activate"
5063         do_facet $SINGLEMDS lctl --device %$OSC activate
5064     done
5065
5066     mkdir -p $DIR/$tdir
5067     for INACTIVE_OSC in $MDS_OSCS; do
5068         echo "Deactivate: " $INACTIVE_OSC
5069         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5070         for STRIPE_OSC in $MDS_OSCS; do
5071             OST=`osc_to_ost $STRIPE_OSC`
5072             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5073                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5074
5075             [ -f $DIR/$tdir/$IDX ] && continue
5076             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5077             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5078             RC=$?
5079             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5080         done
5081         rm -f $DIR/$tdir/*
5082         echo $INACTIVE_OSC "is Activate."
5083         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5084     done
5085 }
5086 run_test 65k "validate manual striping works properly with deactivated OSCs"
5087
5088 test_65l() { # bug 12836
5089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5090         test_mkdir -p $DIR/$tdir/test_dir
5091         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5092         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5093 }
5094 run_test 65l "lfs find on -1 stripe dir ========================"
5095
5096 # bug 2543 - update blocks count on client
5097 test_66() {
5098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5099         COUNT=${COUNT:-8}
5100         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5101         sync; sync_all_data; sync; sync_all_data
5102         cancel_lru_locks osc
5103         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5104         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5105 }
5106 run_test 66 "update inode blocks count on client ==============="
5107
5108 LLOOP=
5109 LLITELOOPLOAD=
5110 cleanup_68() {
5111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5112         trap 0
5113         if [ ! -z "$LLOOP" ]; then
5114                 if swapon -s | grep -q $LLOOP; then
5115                         swapoff $LLOOP || error "swapoff failed"
5116                 fi
5117
5118                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5119                 rm -f $LLOOP
5120                 unset LLOOP
5121         fi
5122         if [ ! -z "$LLITELOOPLOAD" ]; then
5123                 rmmod llite_lloop
5124                 unset LLITELOOPLOAD
5125         fi
5126         rm -f $DIR/f68*
5127 }
5128
5129 meminfo() {
5130         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5131 }
5132
5133 swap_used() {
5134         swapon -s | awk '($1 == "'$1'") { print $4 }'
5135 }
5136
5137 # test case for lloop driver, basic function
5138 test_68a() {
5139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5140         [ "$UID" != 0 ] && skip_env "must run as root" && return
5141         llite_lloop_enabled || \
5142                 { skip_env "llite_lloop module disabled" && return; }
5143
5144         trap cleanup_68 EXIT
5145
5146         if ! module_loaded llite_lloop; then
5147                 if load_module llite/llite_lloop; then
5148                         LLITELOOPLOAD=yes
5149                 else
5150                         skip_env "can't find module llite_lloop"
5151                         return
5152                 fi
5153         fi
5154
5155         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5156         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5157         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5158
5159         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5160         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5161
5162         cleanup_68
5163 }
5164 run_test 68a "lloop driver - basic test ========================"
5165
5166 # excercise swapping to lustre by adding a high priority swapfile entry
5167 # and then consuming memory until it is used.
5168 test_68b() {  # was test_68
5169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5170         [ "$UID" != 0 ] && skip_env "must run as root" && return
5171         lctl get_param -n devices | grep -q obdfilter && \
5172                 skip "local OST" && return
5173
5174         grep -q llite_lloop /proc/modules
5175         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5176
5177         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5178                 skip "can't reliably test swap with TCP" && return
5179
5180         MEMTOTAL=`meminfo MemTotal`
5181         NR_BLOCKS=$((MEMTOTAL>>8))
5182         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5183
5184         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5185         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5186         mkswap $DIR/f68b
5187
5188         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5189
5190         trap cleanup_68 EXIT
5191
5192         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5193
5194         echo "before: `swapon -s | grep $LLOOP`"
5195         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5196         echo "after: `swapon -s | grep $LLOOP`"
5197         SWAPUSED=`swap_used $LLOOP`
5198
5199         cleanup_68
5200
5201         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5202 }
5203 run_test 68b "support swapping to Lustre ========================"
5204
5205 # bug5265, obdfilter oa2dentry return -ENOENT
5206 # #define OBD_FAIL_OST_ENOENT 0x217
5207 test_69() {
5208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5209         remote_ost_nodsh && skip "remote OST with nodsh" && return
5210
5211         f="$DIR/$tfile"
5212         $SETSTRIPE -c 1 -i 0 $f
5213
5214         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5215
5216         do_facet ost1 lctl set_param fail_loc=0x217
5217         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5218         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5219
5220         do_facet ost1 lctl set_param fail_loc=0
5221         $DIRECTIO write $f 0 2 || error "write error"
5222
5223         cancel_lru_locks osc
5224         $DIRECTIO read $f 0 1 || error "read error"
5225
5226         do_facet ost1 lctl set_param fail_loc=0x217
5227         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5228
5229         do_facet ost1 lctl set_param fail_loc=0
5230         rm -f $f
5231 }
5232 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5233
5234 test_71() {
5235     test_mkdir -p $DIR/$tdir
5236     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5237 }
5238 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5239
5240 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5242         [ "$RUNAS_ID" = "$UID" ] &&
5243                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5244
5245         # Check that testing environment is properly set up. Skip if not
5246         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5247                 skip_env "User $RUNAS_ID does not exist - skipping"
5248                 return 0
5249         }
5250         # We had better clear the $DIR to get enough space for dd
5251         rm -rf $DIR/*
5252         touch $DIR/$tfile
5253         chmod 777 $DIR/$tfile
5254         chmod ug+s $DIR/$tfile
5255         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5256                 error "$RUNAS dd $DIR/$tfile failed"
5257         # See if we are still setuid/sgid
5258         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5259                 error "S/gid is not dropped on write"
5260         # Now test that MDS is updated too
5261         cancel_lru_locks mdc
5262         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5263                 error "S/gid is not dropped on MDS"
5264         rm -f $DIR/$tfile
5265 }
5266 run_test 72a "Test that remove suid works properly (bug5695) ===="
5267
5268 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5269         local perm
5270
5271         [ "$RUNAS_ID" = "$UID" ] && \
5272                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5273         [ "$RUNAS_ID" -eq 0 ] && \
5274                 skip_env "RUNAS_ID = 0 -- skipping" && return
5275
5276         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5277         # Check that testing environment is properly set up. Skip if not
5278         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5279                 skip_env "User $RUNAS_ID does not exist - skipping"
5280                 return 0
5281         }
5282         touch $DIR/${tfile}-f{g,u}
5283         test_mkdir $DIR/${tfile}-dg
5284         test_mkdir $DIR/${tfile}-du
5285         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5286         chmod g+s $DIR/${tfile}-{f,d}g
5287         chmod u+s $DIR/${tfile}-{f,d}u
5288         for perm in 777 2777 4777; do
5289                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5290                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5291                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5292                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5293         done
5294         true
5295 }
5296 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5297
5298 # bug 3462 - multiple simultaneous MDC requests
5299 test_73() {
5300         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5301         test_mkdir $DIR/d73-1
5302         test_mkdir $DIR/d73-2
5303         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5304         pid1=$!
5305
5306         lctl set_param fail_loc=0x80000129
5307         $MULTIOP $DIR/d73-1/f73-2 Oc &
5308         sleep 1
5309         lctl set_param fail_loc=0
5310
5311         $MULTIOP $DIR/d73-2/f73-3 Oc &
5312         pid3=$!
5313
5314         kill -USR1 $pid1
5315         wait $pid1 || return 1
5316
5317         sleep 25
5318
5319         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5320         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5321         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5322
5323         rm -rf $DIR/d73-*
5324 }
5325 run_test 73 "multiple MDC requests (should not deadlock)"
5326
5327 test_74a() { # bug 6149, 6184
5328         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5329         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5330         #
5331         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5332         # will spin in a tight reconnection loop
5333         touch $DIR/f74a
5334         lctl set_param fail_loc=0x8000030e
5335         # get any lock that won't be difficult - lookup works.
5336         ls $DIR/f74a
5337         lctl set_param fail_loc=0
5338         true
5339         rm -f $DIR/f74a
5340 }
5341 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5342
5343 test_74b() { # bug 13310
5344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5345         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5346         #
5347         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5348         # will spin in a tight reconnection loop
5349         lctl set_param fail_loc=0x8000030e
5350         # get a "difficult" lock
5351         touch $DIR/f74b
5352         lctl set_param fail_loc=0
5353         true
5354         rm -f $DIR/f74b
5355 }
5356 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5357
5358 test_74c() {
5359         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5360 #define OBD_FAIL_LDLM_NEW_LOCK
5361         lctl set_param fail_loc=0x80000319
5362         touch $DIR/$tfile && error "Touch successful"
5363         true
5364 }
5365 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5366
5367 num_inodes() {
5368         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5369 }
5370
5371 get_inode_slab_tunables() {
5372         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5373 }
5374
5375 set_inode_slab_tunables() {
5376         echo "lustre_inode_cache $1" > /proc/slabinfo
5377 }
5378
5379 test_76() { # Now for bug 20433, added originally in bug 1443
5380         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5381         local SLAB_SETTINGS=`get_inode_slab_tunables`
5382         local CPUS=`getconf _NPROCESSORS_ONLN`
5383         # we cannot set limit below 1 which means 1 inode in each
5384         # per-cpu cache is still allowed
5385         set_inode_slab_tunables "1 1 0"
5386         cancel_lru_locks osc
5387         BEFORE_INODES=`num_inodes`
5388         echo "before inodes: $BEFORE_INODES"
5389         local COUNT=1000
5390         [ "$SLOW" = "no" ] && COUNT=100
5391         for i in `seq $COUNT`; do
5392                 touch $DIR/$tfile
5393                 rm -f $DIR/$tfile
5394         done
5395         cancel_lru_locks osc
5396         AFTER_INODES=`num_inodes`
5397         echo "after inodes: $AFTER_INODES"
5398         local wait=0
5399         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5400                 sleep 2
5401                 AFTER_INODES=`num_inodes`
5402                 wait=$((wait+2))
5403                 echo "wait $wait seconds inodes: $AFTER_INODES"
5404                 if [ $wait -gt 30 ]; then
5405                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5406                 fi
5407         done
5408         set_inode_slab_tunables "$SLAB_SETTINGS"
5409 }
5410 run_test 76 "confirm clients recycle inodes properly ===="
5411
5412
5413 export ORIG_CSUM=""
5414 set_checksums()
5415 {
5416         # Note: in sptlrpc modes which enable its own bulk checksum, the
5417         # original crc32_le bulk checksum will be automatically disabled,
5418         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5419         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5420         # In this case set_checksums() will not be no-op, because sptlrpc
5421         # bulk checksum will be enabled all through the test.
5422
5423         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5424         lctl set_param -n osc.*.checksums $1
5425         return 0
5426 }
5427
5428 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5429                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5430 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5431 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5432 set_checksum_type()
5433 {
5434         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5435         log "set checksum type to $1"
5436         return 0
5437 }
5438 F77_TMP=$TMP/f77-temp
5439 F77SZ=8
5440 setup_f77() {
5441         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5442                 error "error writing to $F77_TMP"
5443 }
5444
5445 test_77a() { # bug 10889
5446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5447         $GSS && skip "could not run with gss" && return
5448         [ ! -f $F77_TMP ] && setup_f77
5449         set_checksums 1
5450         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5451         set_checksums 0
5452         rm -f $DIR/$tfile
5453 }
5454 run_test 77a "normal checksum read/write operation"
5455
5456 test_77b() { # bug 10889
5457         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5458         $GSS && skip "could not run with gss" && return
5459         [ ! -f $F77_TMP ] && setup_f77
5460         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5461         $LCTL set_param fail_loc=0x80000409
5462         set_checksums 1
5463
5464         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5465                 error "dd error: $?"
5466         $LCTL set_param fail_loc=0
5467
5468         for algo in $CKSUM_TYPES; do
5469                 cancel_lru_locks osc
5470                 set_checksum_type $algo
5471                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5472                 $LCTL set_param fail_loc=0x80000408
5473                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5474                 $LCTL set_param fail_loc=0
5475         done
5476         set_checksums 0
5477         set_checksum_type $ORIG_CSUM_TYPE
5478         rm -f $DIR/$tfile
5479 }
5480 run_test 77b "checksum error on client write, read"
5481
5482 test_77d() { # bug 10889
5483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5484         $GSS && skip "could not run with gss" && return
5485         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5486         $LCTL set_param fail_loc=0x80000409
5487         set_checksums 1
5488         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5489                 error "direct write: rc=$?"
5490         $LCTL set_param fail_loc=0
5491         set_checksums 0
5492
5493         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5494         $LCTL set_param fail_loc=0x80000408
5495         set_checksums 1
5496         cancel_lru_locks osc
5497         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5498                 error "direct read: rc=$?"
5499         $LCTL set_param fail_loc=0
5500         set_checksums 0
5501 }
5502 run_test 77d "checksum error on OST direct write, read"
5503
5504 test_77f() { # bug 10889
5505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5506         $GSS && skip "could not run with gss" && return
5507         set_checksums 1
5508         for algo in $CKSUM_TYPES; do
5509                 cancel_lru_locks osc
5510                 set_checksum_type $algo
5511                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5512                 $LCTL set_param fail_loc=0x409
5513                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5514                         error "direct write succeeded"
5515                 $LCTL set_param fail_loc=0
5516         done
5517         set_checksum_type $ORIG_CSUM_TYPE
5518         set_checksums 0
5519 }
5520 run_test 77f "repeat checksum error on write (expect error)"
5521
5522 test_77g() { # bug 10889
5523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5524         $GSS && skip "could not run with gss" && return
5525         remote_ost_nodsh && skip "remote OST with nodsh" && return
5526
5527         [ ! -f $F77_TMP ] && setup_f77
5528
5529         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5530         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5531         do_facet ost1 lctl set_param fail_loc=0x8000021a
5532         set_checksums 1
5533         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5534                 error "write error: rc=$?"
5535         do_facet ost1 lctl set_param fail_loc=0
5536         set_checksums 0
5537
5538         cancel_lru_locks osc
5539         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5540         do_facet ost1 lctl set_param fail_loc=0x8000021b
5541         set_checksums 1
5542         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5543         do_facet ost1 lctl set_param fail_loc=0
5544         set_checksums 0
5545 }
5546 run_test 77g "checksum error on OST write, read"
5547
5548 test_77i() { # bug 13805
5549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5550         $GSS && skip "could not run with gss" && return
5551         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5552         lctl set_param fail_loc=0x40b
5553         remount_client $MOUNT
5554         lctl set_param fail_loc=0
5555         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5556                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5557                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5558                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5559         done
5560         remount_client $MOUNT
5561 }
5562 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5563
5564 test_77j() { # bug 13805
5565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5566         $GSS && skip "could not run with gss" && return
5567         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5568         lctl set_param fail_loc=0x40c
5569         remount_client $MOUNT
5570         lctl set_param fail_loc=0
5571         sleep 2 # wait async osc connect to finish
5572         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5573                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5574                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5575                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5576         done
5577         remount_client $MOUNT
5578 }
5579 run_test 77j "client only supporting ADLER32"
5580
5581 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5582 rm -f $F77_TMP
5583 unset F77_TMP
5584
5585 test_78() { # bug 10901
5586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5587         remote_ost || { skip_env "local OST" && return; }
5588
5589         NSEQ=5
5590         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5591         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5592         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5593         echo "MemTotal: $MEMTOTAL"
5594 # reserve 256MB of memory for the kernel and other running processes,
5595 # and then take 1/2 of the remaining memory for the read/write buffers.
5596     if [ $MEMTOTAL -gt 512 ] ;then
5597         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5598     else
5599         # for those poor memory-starved high-end clusters...
5600         MEMTOTAL=$((MEMTOTAL / 2))
5601     fi
5602         echo "Mem to use for directio: $MEMTOTAL"
5603         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5604         [ $F78SIZE -gt 512 ] && F78SIZE=512
5605         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5606         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5607         echo "Smallest OST: $SMALLESTOST"
5608         [ $SMALLESTOST -lt 10240 ] && \
5609                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5610
5611         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5612                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5613
5614         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5615         echo "File size: $F78SIZE"
5616         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5617         for i in `seq 1 $NSEQ`
5618         do
5619                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5620                 echo directIO rdwr round $i of $NSEQ
5621                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5622         done
5623
5624         rm -f $DIR/$tfile
5625 }
5626 run_test 78 "handle large O_DIRECT writes correctly ============"
5627
5628 test_79() { # bug 12743
5629         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5630         wait_delete_completed
5631
5632         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5633         BKFREE=$(calc_osc_kbytes kbytesfree)
5634         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5635
5636         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5637         DFTOTAL=`echo $STRING | cut -d, -f1`
5638         DFUSED=`echo $STRING  | cut -d, -f2`
5639         DFAVAIL=`echo $STRING | cut -d, -f3`
5640         DFFREE=$(($DFTOTAL - $DFUSED))
5641
5642         ALLOWANCE=$((64 * $OSTCOUNT))
5643
5644         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5645            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5646                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5647         fi
5648         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5649            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5650                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5651         fi
5652         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5653            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5654                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5655         fi
5656 }
5657 run_test 79 "df report consistency check ======================="
5658
5659 test_80() { # bug 10718
5660         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5661         # relax strong synchronous semantics for slow backends like ZFS
5662         local soc="obdfilter.*.sync_on_lock_cancel"
5663         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5664         local hosts=
5665         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5666                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5667                           facet_active_host $host; done | sort -u)
5668                 do_nodes $hosts lctl set_param $soc=never
5669         fi
5670
5671         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5672         sync; sleep 1; sync
5673         local BEFORE=`date +%s`
5674         cancel_lru_locks osc
5675         local AFTER=`date +%s`
5676         local DIFF=$((AFTER-BEFORE))
5677         if [ $DIFF -gt 1 ] ; then
5678                 error "elapsed for 1M@1T = $DIFF"
5679         fi
5680
5681         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5682
5683         rm -f $DIR/$tfile
5684 }
5685 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5686
5687 test_81a() { # LU-456
5688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5689         remote_ost_nodsh && skip "remote OST with nodsh" && return
5690         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5691         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5692         do_facet ost1 lctl set_param fail_loc=0x80000228
5693
5694         # write should trigger a retry and success
5695         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5696         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5697         RC=$?
5698         if [ $RC -ne 0 ] ; then
5699                 error "write should success, but failed for $RC"
5700         fi
5701 }
5702 run_test 81a "OST should retry write when get -ENOSPC ==============="
5703
5704 test_81b() { # LU-456
5705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5706         remote_ost_nodsh && skip "remote OST with nodsh" && return
5707         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5708         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5709         do_facet ost1 lctl set_param fail_loc=0x228
5710
5711         # write should retry several times and return -ENOSPC finally
5712         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5713         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5714         RC=$?
5715         ENOSPC=28
5716         if [ $RC -ne $ENOSPC ] ; then
5717                 error "dd should fail for -ENOSPC, but succeed."
5718         fi
5719 }
5720 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5721
5722 test_82() { # LU-1031
5723         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5724         local gid1=14091995
5725         local gid2=16022000
5726
5727         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5728         local MULTIPID1=$!
5729         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5730         local MULTIPID2=$!
5731         kill -USR1 $MULTIPID2
5732         sleep 2
5733         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5734                 error "First grouplock does not block second one"
5735         else
5736                 echo "Second grouplock blocks first one"
5737         fi
5738         kill -USR1 $MULTIPID1
5739         wait $MULTIPID1
5740         wait $MULTIPID2
5741 }
5742 run_test 82 "Basic grouplock test ==============================="
5743
5744 test_99a() {
5745         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5746                 return
5747         test_mkdir -p $DIR/d99cvsroot
5748         chown $RUNAS_ID $DIR/d99cvsroot
5749         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5750         cd $TMP
5751
5752         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5753         cd $oldPWD
5754 }
5755 run_test 99a "cvs init ========================================="
5756
5757 test_99b() {
5758         [ -z "$(which cvs 2>/dev/null)" ] &&
5759                 skip_env "could not find cvs" && return
5760         [ ! -d $DIR/d99cvsroot ] && test_99a
5761         cd /etc/init.d
5762         # some versions of cvs import exit(1) when asked to import links or
5763         # files they can't read.  ignore those files.
5764         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5765                         ! -perm +4 -printf '-I %f\n')
5766         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5767                 d99reposname vtag rtag
5768 }
5769 run_test 99b "cvs import ======================================="
5770
5771 test_99c() {
5772         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5773         [ ! -d $DIR/d99cvsroot ] && test_99b
5774         cd $DIR
5775         test_mkdir -p $DIR/d99reposname
5776         chown $RUNAS_ID $DIR/d99reposname
5777         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5778 }
5779 run_test 99c "cvs checkout ====================================="
5780
5781 test_99d() {
5782         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5783         [ ! -d $DIR/d99cvsroot ] && test_99c
5784         cd $DIR/d99reposname
5785         $RUNAS touch foo99
5786         $RUNAS cvs add -m 'addmsg' foo99
5787 }
5788 run_test 99d "cvs add =========================================="
5789
5790 test_99e() {
5791         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5792         [ ! -d $DIR/d99cvsroot ] && test_99c
5793         cd $DIR/d99reposname
5794         $RUNAS cvs update
5795 }
5796 run_test 99e "cvs update ======================================="
5797
5798 test_99f() {
5799         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5800         [ ! -d $DIR/d99cvsroot ] && test_99d
5801         cd $DIR/d99reposname
5802         $RUNAS cvs commit -m 'nomsg' foo99
5803     rm -fr $DIR/d99cvsroot
5804 }
5805 run_test 99f "cvs commit ======================================="
5806
5807 test_100() {
5808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5809         [ "$NETTYPE" = tcp ] || \
5810                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5811                         return ; }
5812
5813         remote_ost_nodsh && skip "remote OST with nodsh" && return
5814         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5815         remote_servers || \
5816                 { skip "useless for local single node setup" && return; }
5817
5818         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5819                 [ "$PROT" != "tcp" ] && continue
5820                 RPORT=$(echo $REMOTE | cut -d: -f2)
5821                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5822
5823                 rc=0
5824                 LPORT=`echo $LOCAL | cut -d: -f2`
5825                 if [ $LPORT -ge 1024 ]; then
5826                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5827                         netstat -tna
5828                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5829                 fi
5830         done
5831         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5832 }
5833 run_test 100 "check local port using privileged port ==========="
5834
5835 function get_named_value()
5836 {
5837     local tag
5838
5839     tag=$1
5840     while read ;do
5841         line=$REPLY
5842         case $line in
5843         $tag*)
5844             echo $line | sed "s/^$tag[ ]*//"
5845             break
5846             ;;
5847         esac
5848     done
5849 }
5850
5851 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5852                    awk '/^max_cached_mb/ { print $2 }')
5853
5854 cleanup_101a() {
5855         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5856         trap 0
5857 }
5858
5859 test_101a() {
5860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5861         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
5862         local s
5863         local discard
5864         local nreads=10000
5865         local cache_limit=32
5866
5867         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5868         trap cleanup_101a EXIT
5869         $LCTL set_param -n llite.*.read_ahead_stats 0
5870         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5871
5872         #
5873         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5874         #
5875         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5876         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5877
5878         discard=0
5879         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5880                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5881                         discard=$(($discard + $s))
5882         done
5883         cleanup_101a
5884
5885         if [ $(($discard * 10)) -gt $nreads ] ;then
5886                 $LCTL get_param osc.*-osc*.rpc_stats
5887                 $LCTL get_param llite.*.read_ahead_stats
5888                 error "too many ($discard) discarded pages"
5889         fi
5890         rm -f $DIR/$tfile || true
5891 }
5892 run_test 101a "check read-ahead for random reads ================"
5893
5894 setup_test101bc() {
5895         test_mkdir -p $DIR/$tdir
5896         STRIPE_SIZE=1048576
5897         STRIPE_COUNT=$OSTCOUNT
5898         STRIPE_OFFSET=0
5899
5900         local list=$(comma_list $(osts_nodes))
5901         set_osd_param $list '' read_cache_enable 0
5902         set_osd_param $list '' writethrough_cache_enable 0
5903
5904         trap cleanup_test101bc EXIT
5905         # prepare the read-ahead file
5906         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5907
5908         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5909 }
5910
5911 cleanup_test101bc() {
5912         trap 0
5913         rm -rf $DIR/$tdir
5914         rm -f $DIR/$tfile
5915
5916         local list=$(comma_list $(osts_nodes))
5917         set_osd_param $list '' read_cache_enable 1
5918         set_osd_param $list '' writethrough_cache_enable 1
5919 }
5920
5921 calc_total() {
5922         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5923 }
5924
5925 ra_check_101() {
5926         local READ_SIZE=$1
5927         local STRIPE_SIZE=1048576
5928         local RA_INC=1048576
5929         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5930         local FILE_LENGTH=$((64*100))
5931         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5932                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5933         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5934                         get_named_value 'read but discarded' | \
5935                         cut -d" " -f1 | calc_total`
5936         if [ $DISCARD -gt $discard_limit ]; then
5937                 $LCTL get_param llite.*.read_ahead_stats
5938                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5939         else
5940                 echo "Read-ahead success for size ${READ_SIZE}"
5941         fi
5942 }
5943
5944 test_101b() {
5945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5946         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5947         local STRIPE_SIZE=1048576
5948         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5949         local FILE_LENGTH=$((STRIPE_SIZE*100))
5950         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5951         # prepare the read-ahead file
5952         setup_test101bc
5953         cancel_lru_locks osc
5954         for BIDX in 2 4 8 16 32 64 128 256
5955         do
5956                 local BSIZE=$((BIDX*4096))
5957                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5958                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5959                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5960                 $LCTL set_param -n llite.*.read_ahead_stats 0
5961                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5962                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5963                 cancel_lru_locks osc
5964                 ra_check_101 $BSIZE
5965         done
5966         cleanup_test101bc
5967         true
5968 }
5969 run_test 101b "check stride-io mode read-ahead ================="
5970
5971 test_101c() {
5972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5973         local STRIPE_SIZE=1048576
5974         local FILE_LENGTH=$((STRIPE_SIZE*100))
5975         local nreads=10000
5976         local osc_rpc_stats
5977
5978         setup_test101bc
5979
5980         cancel_lru_locks osc
5981         $LCTL set_param osc.*.rpc_stats 0
5982         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5983         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
5984                 local stats=$($LCTL get_param -n $osc_rpc_stats)
5985                 local lines=$(echo "$stats" | awk 'END {print NR;}')
5986                 local size
5987
5988                 if [ $lines -le 20 ]; then
5989                         continue
5990                 fi
5991                 for size in 1 2 4 8; do
5992                         local rpc=$(echo "$stats" |
5993                                     awk '($1 == "'$size':") {print $2; exit; }')
5994                         [ $rpc != 0 ] &&
5995                                 error "Small $((size*4))k read IO $rpc !"
5996                 done
5997                 echo "$osc_rpc_stats check passed!"
5998         done
5999         cleanup_test101bc
6000         true
6001 }
6002 run_test 101c "check stripe_size aligned read-ahead ================="
6003
6004 set_read_ahead() {
6005         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6006         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6007 }
6008
6009 test_101d() {
6010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6011         local file=$DIR/$tfile
6012         local sz_MB=${FILESIZE_101d:-500}
6013         local ra_MB=${READAHEAD_MB:-40}
6014
6015         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6016         [ $free_MB -lt $sz_MB ] &&
6017                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6018
6019         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6020         $SETSTRIPE -c -1 $file || error "setstripe failed"
6021
6022         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6023         echo Cancel LRU locks on lustre client to flush the client cache
6024         cancel_lru_locks osc
6025
6026         echo Disable read-ahead
6027         local old_READAHEAD=$(set_read_ahead 0)
6028
6029         echo Reading the test file $file with read-ahead disabled
6030         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6031
6032         echo Cancel LRU locks on lustre client to flush the client cache
6033         cancel_lru_locks osc
6034         echo Enable read-ahead with ${ra_MB}MB
6035         set_read_ahead $ra_MB
6036
6037         echo Reading the test file $file with read-ahead enabled
6038         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6039
6040         echo "read-ahead disabled time read $raOFF"
6041         echo "read-ahead enabled  time read $raON"
6042
6043         set_read_ahead $old_READAHEAD
6044         rm -f $file
6045         wait_delete_completed
6046
6047         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6048                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6049 }
6050 run_test 101d "file read with and without read-ahead enabled"
6051
6052 test_101e() {
6053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6054         local file=$DIR/$tfile
6055         local size_KB=500  #KB
6056         local count=100
6057         local bsize=1024
6058
6059         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6060         local need_KB=$((count * size_KB))
6061         [ $free_KB -le $need_KB ] &&
6062                 skip_env "Need free space $need_KB, have $free_KB" && return
6063
6064         echo "Creating $count ${size_KB}K test files"
6065         for ((i = 0; i < $count; i++)); do
6066                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6067         done
6068
6069         echo "Cancel LRU locks on lustre client to flush the client cache"
6070         cancel_lru_locks osc
6071
6072         echo "Reset readahead stats"
6073         $LCTL set_param -n llite.*.read_ahead_stats 0
6074
6075         for ((i = 0; i < $count; i++)); do
6076                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6077         done
6078
6079         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6080                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6081
6082         for ((i = 0; i < $count; i++)); do
6083                 rm -rf $file.$i 2>/dev/null
6084         done
6085
6086         #10000 means 20% reads are missing in readahead
6087         [ $miss -lt 10000 ] ||  error "misses too much for small reads"
6088 }
6089 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6090
6091 cleanup_test101f() {
6092     trap 0
6093     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6094     rm -rf $DIR/$tfile 2>/dev/null
6095 }
6096
6097 test_101f() {
6098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6099     local file=$DIR/$tfile
6100     local nreads=1000
6101
6102     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6103     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6104     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6105     trap cleanup_test101f EXIT
6106
6107     echo Cancel LRU locks on lustre client to flush the client cache
6108     cancel_lru_locks osc
6109
6110     echo Reset readahead stats
6111     $LCTL set_param -n llite.*.read_ahead_stats 0
6112     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6113     # readahead should read in 2M file on second read, so only miss
6114     # 2 pages.
6115     echo Random 4K reads on 2M file for 1000 times
6116     $READS -f $file -s 2097152 -b 4096 -n $nreads
6117
6118     echo checking missing pages
6119     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6120           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6121
6122     [ $miss -lt 3 ] || error "misses too much pages!"
6123     cleanup_test101f
6124 }
6125 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6126
6127 setup_test102() {
6128         test_mkdir -p $DIR/$tdir
6129         chown $RUNAS_ID $DIR/$tdir
6130         STRIPE_SIZE=65536
6131         STRIPE_OFFSET=1
6132         STRIPE_COUNT=$OSTCOUNT
6133         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
6134
6135         trap cleanup_test102 EXIT
6136         cd $DIR
6137         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6138         cd $DIR/$tdir
6139         for num in 1 2 3 4; do
6140                 for count in $(seq 1 $STRIPE_COUNT); do
6141                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6142                                 local size=`expr $STRIPE_SIZE \* $num`
6143                                 local file=file"$num-$idx-$count"
6144                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6145                         done
6146                 done
6147         done
6148
6149         cd $DIR
6150         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6151 }
6152
6153 cleanup_test102() {
6154         trap 0
6155         rm -f $TMP/f102.tar
6156         rm -rf $DIR/d0.sanity/d102
6157 }
6158
6159 test_102a() {
6160         local testfile=$DIR/xattr_testfile
6161
6162         touch $testfile
6163
6164         [ "$UID" != 0 ] && skip_env "must run as root" && return
6165         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6166                 skip_env "must have user_xattr" && return
6167
6168         [ -z "$(which setfattr 2>/dev/null)" ] &&
6169                 skip_env "could not find setfattr" && return
6170
6171         echo "set/get xattr..."
6172         setfattr -n trusted.name1 -v value1 $testfile ||
6173                 error "setfattr -n trusted.name1=value1 $testfile failed"
6174         getfattr -n trusted.name1 $testfile 2> /dev/null |
6175           grep "trusted.name1=.value1" ||
6176                 error "$testfile missing trusted.name1=value1"
6177
6178         setfattr -n user.author1 -v author1 $testfile ||
6179                 error "setfattr -n user.author1=author1 $testfile failed"
6180         getfattr -n user.author1 $testfile 2> /dev/null |
6181           grep "user.author1=.author1" ||
6182                 error "$testfile missing trusted.author1=author1"
6183
6184         echo "listxattr..."
6185         setfattr -n trusted.name2 -v value2 $testfile ||
6186                 error "$testfile unable to set trusted.name2"
6187         setfattr -n trusted.name3 -v value3 $testfile ||
6188                 error "$testfile unable to set trusted.name3"
6189         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6190             grep "trusted.name" | wc -l) -eq 3 ] ||
6191                 error "$testfile missing 3 trusted.name xattrs"
6192
6193         setfattr -n user.author2 -v author2 $testfile ||
6194                 error "$testfile unable to set user.author2"
6195         setfattr -n user.author3 -v author3 $testfile ||
6196                 error "$testfile unable to set user.author3"
6197         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6198             grep "user.author" | wc -l) -eq 3 ] ||
6199                 error "$testfile missing 3 user.author xattrs"
6200
6201         echo "remove xattr..."
6202         setfattr -x trusted.name1 $testfile ||
6203                 error "$testfile error deleting trusted.name1"
6204         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6205                 error "$testfile did not delete trusted.name1 xattr"
6206
6207         setfattr -x user.author1 $testfile ||
6208                 error "$testfile error deleting user.author1"
6209         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6210                 error "$testfile did not delete trusted.name1 xattr"
6211
6212         # b10667: setting lustre special xattr be silently discarded
6213         echo "set lustre special xattr ..."
6214         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6215                 error "$testfile allowed setting trusted.lov"
6216 }
6217 run_test 102a "user xattr test =================================="
6218
6219 test_102b() {
6220         # b10930: get/set/list trusted.lov xattr
6221         echo "get/set/list trusted.lov xattr ..."
6222         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6223         local testfile=$DIR/$tfile
6224         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6225                 error "setstripe failed"
6226         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6227                 error "getstripe failed"
6228         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
6229         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
6230
6231         local testfile2=${testfile}2
6232         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
6233                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
6234
6235         $MCREATE $testfile2
6236         setfattr -n trusted.lov -v $value $testfile2
6237         local stripe_size=$($GETSTRIPE -S $testfile2)
6238         local stripe_count=$($GETSTRIPE -c $testfile2)
6239         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6240         [ $stripe_count -eq $STRIPECOUNT ] ||
6241                 error "stripe count $stripe_count != $STRIPECOUNT"
6242         rm -f $DIR/$tfile
6243 }
6244 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6245
6246 test_102c() {
6247         # b10930: get/set/list lustre.lov xattr
6248         echo "get/set/list lustre.lov xattr ..."
6249         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6250         test_mkdir -p $DIR/$tdir
6251         chown $RUNAS_ID $DIR/$tdir
6252         local testfile=$DIR/$tdir/$tfile
6253         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6254                 error "setstripe failed"
6255         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6256                 error "getstripe failed"
6257         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6258         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6259
6260         local testfile2=${testfile}2
6261         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6262                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6263
6264         $RUNAS $MCREATE $testfile2
6265         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6266         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6267         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6268         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6269         [ $stripe_count -eq $STRIPECOUNT ] ||
6270                 error "stripe count $stripe_count != $STRIPECOUNT"
6271 }
6272 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6273
6274 compare_stripe_info1() {
6275         local stripe_index_all_zero=true
6276
6277         for num in 1 2 3 4; do
6278                 for count in $(seq 1 $STRIPE_COUNT); do
6279                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6280                                 local size=$((STRIPE_SIZE * num))
6281                                 local file=file"$num-$offset-$count"
6282                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6283                                 [ $stripe_size -ne $size ] &&
6284                                     error "$file: size $stripe_size != $size"
6285                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6286                                 # allow fewer stripes to be created, ORI-601
6287                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
6288                                     error "$file: count $stripe_count != $count"
6289                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6290                                 [ $stripe_index -ne 0 ] &&
6291                                         stripe_index_all_zero=false
6292                         done
6293                 done
6294         done
6295         $stripe_index_all_zero &&
6296                 error "all files are being extracted starting from OST index 0"
6297         return 0
6298 }
6299
6300 find_lustre_tar() {
6301         [ -n "$(which tar 2>/dev/null)" ] &&
6302                 strings $(which tar) | grep -q "lustre" && echo tar
6303 }
6304
6305 test_102d() {
6306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6307         # b10930: tar test for trusted.lov xattr
6308         TAR=$(find_lustre_tar)
6309         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6310         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6311         setup_test102
6312         test_mkdir -p $DIR/d102d
6313         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6314         cd $DIR/d102d/$tdir
6315         compare_stripe_info1
6316 }
6317 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6318
6319 test_102f() {
6320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6321         # b10930: tar test for trusted.lov xattr
6322         TAR=$(find_lustre_tar)
6323         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6324         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6325         setup_test102
6326         test_mkdir -p $DIR/d102f
6327         cd $DIR
6328         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6329         cd $DIR/d102f/$tdir
6330         compare_stripe_info1
6331 }
6332 run_test 102f "tar copy files, not keep osts ==========="
6333
6334 grow_xattr() {
6335         local xsize=${1:-1024}  # in bytes
6336         local file=$DIR/$tfile
6337
6338         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6339                 skip "must have user_xattr" && return 0
6340         [ -z "$(which setfattr 2>/dev/null)" ] &&
6341                 skip_env "could not find setfattr" && return 0
6342         [ -z "$(which getfattr 2>/dev/null)" ] &&
6343                 skip_env "could not find getfattr" && return 0
6344
6345         touch $file
6346
6347         local value="$(generate_string $xsize)"
6348
6349         local xbig=trusted.big
6350         log "save $xbig on $file"
6351         setfattr -n $xbig -v $value $file ||
6352                 error "saving $xbig on $file failed"
6353
6354         local orig=$(get_xattr_value $xbig $file)
6355         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6356
6357         local xsml=trusted.sml
6358         log "save $xsml on $file"
6359         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6360
6361         local new=$(get_xattr_value $xbig $file)
6362         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6363
6364         log "grow $xsml on $file"
6365         setfattr -n $xsml -v "$value" $file ||
6366                 error "growing $xsml on $file failed"
6367
6368         new=$(get_xattr_value $xbig $file)
6369         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6370         log "$xbig still valid after growing $xsml"
6371
6372         rm -f $file
6373 }
6374
6375 test_102h() { # bug 15777
6376         grow_xattr 1024
6377 }
6378 run_test 102h "grow xattr from inside inode to external block"
6379
6380 test_102ha() {
6381         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6382         grow_xattr $(max_xattr_size)
6383 }
6384 run_test 102ha "grow xattr from inside inode to external inode"
6385
6386 test_102i() { # bug 17038
6387         touch $DIR/$tfile
6388         ln -s $DIR/$tfile $DIR/${tfile}link
6389         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6390         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"
6391         rm -f $DIR/$tfile $DIR/${tfile}link
6392 }
6393 run_test 102i "lgetxattr test on symbolic link ============"
6394
6395 test_102j() {
6396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6397         TAR=$(find_lustre_tar)
6398         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6399         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6400         setup_test102 "$RUNAS"
6401         test_mkdir -p $DIR/d102j
6402         chown $RUNAS_ID $DIR/d102j
6403         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6404         cd $DIR/d102j/$tdir
6405         compare_stripe_info1 "$RUNAS"
6406 }
6407 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6408
6409 test_102k() {
6410         touch $DIR/$tfile
6411         # b22187 just check that does not crash for regular file.
6412         setfattr -n trusted.lov $DIR/$tfile
6413         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6414         local test_kdir=$DIR/d102k
6415         test_mkdir $test_kdir
6416         local default_size=`$GETSTRIPE -S $test_kdir`
6417         local default_count=`$GETSTRIPE -c $test_kdir`
6418         local default_offset=`$GETSTRIPE -i $test_kdir`
6419         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6420                 error 'dir setstripe failed'
6421         setfattr -n trusted.lov $test_kdir
6422         local stripe_size=`$GETSTRIPE -S $test_kdir`
6423         local stripe_count=`$GETSTRIPE -c $test_kdir`
6424         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6425         [ $stripe_size -eq $default_size ] ||
6426                 error "stripe size $stripe_size != $default_size"
6427         [ $stripe_count -eq $default_count ] ||
6428                 error "stripe count $stripe_count != $default_count"
6429         [ $stripe_offset -eq $default_offset ] ||
6430                 error "stripe offset $stripe_offset != $default_offset"
6431         rm -rf $DIR/$tfile $test_kdir
6432 }
6433 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6434
6435 test_102l() {
6436         # LU-532 trusted. xattr is invisible to non-root
6437         local testfile=$DIR/$tfile
6438
6439         touch $testfile
6440
6441         echo "listxattr as user..."
6442         chown $RUNAS_ID $testfile
6443         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6444             grep -q "trusted" &&
6445                 error "$testfile trusted xattrs are user visible"
6446
6447         return 0;
6448 }
6449 run_test 102l "listxattr size test =================================="
6450
6451 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6452         local path=$DIR/$tfile
6453         touch $path
6454
6455         listxattr_size_check $path || error "listattr_size_check $path failed"
6456 }
6457 run_test 102m "Ensure listxattr fails on small bufffer ========"
6458
6459 cleanup_test102
6460
6461 getxattr() { # getxattr path name
6462         # Return the base64 encoding of the value of xattr name on path.
6463         local path=$1
6464         local name=$2
6465
6466         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6467         # file: $path
6468         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6469         #
6470         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6471
6472         getfattr --absolute-names --encoding=base64 --name=$name $path |
6473                 awk -F= -v name=$name '$1 == name {
6474                         print substr($0, index($0, "=") + 1);
6475         }'
6476 }
6477
6478 test_102n() { # LU-4101 mdt: protect internal xattrs
6479         local file0=$DIR/$tfile.0
6480         local file1=$DIR/$tfile.1
6481         local xattr0=$TMP/$tfile.0
6482         local xattr1=$TMP/$tfile.1
6483         local name
6484         local value
6485
6486         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6487         then
6488                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6489                 return
6490         fi
6491
6492         rm -rf $file0 $file1 $xattr0 $xattr1
6493         touch $file0 $file1
6494
6495         # Get 'before' xattrs of $file1.
6496         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6497
6498         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6499                 # Try to copy xattr from $file0 to $file1.
6500                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6501
6502                 setfattr --name=trusted.$name --value="$value" $file1 ||
6503                         error "setxattr 'trusted.$name' failed"
6504
6505                 # Try to set a garbage xattr.
6506                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6507
6508                 setfattr --name=trusted.$name --value="$value" $file1 ||
6509                         error "setxattr 'trusted.$name' failed"
6510
6511                 # Try to remove the xattr from $file1. We don't care if this
6512                 # appears to succeed or fail, we just don't want there to be
6513                 # any changes or crashes.
6514                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6515         done
6516
6517         # Get 'after' xattrs of file1.
6518         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6519
6520         if ! diff $xattr0 $xattr1; then
6521                 error "before and after xattrs of '$file1' differ"
6522         fi
6523
6524         rm -rf $file0 $file1 $xattr0 $xattr1
6525
6526         return 0
6527 }
6528 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6529
6530 run_acl_subtest()
6531 {
6532     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6533     return $?
6534 }
6535
6536 test_103 () {
6537         [ "$UID" != 0 ] && skip_env "must run as root" && return
6538         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6539                 skip "must have acl enabled" && return
6540         [ -z "$(which setfacl 2>/dev/null)" ] &&
6541                 skip_env "could not find setfacl" && return
6542         $GSS && skip "could not run under gss" && return
6543
6544         declare -a identity_old
6545
6546         for num in $(seq $MDSCOUNT); do
6547                 switch_identity $num true || identity_old[$num]=$?
6548         done
6549
6550         SAVE_UMASK=$(umask)
6551         umask 0022
6552         cd $DIR
6553
6554         echo "performing cp ..."
6555         run_acl_subtest cp || error "run_acl_subtest cp failed"
6556         echo "performing getfacl-noacl..."
6557         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6558         echo "performing misc..."
6559         run_acl_subtest misc || error  "misc test failed"
6560         echo "performing permissions..."
6561         run_acl_subtest permissions || error "permissions failed"
6562         echo "performing setfacl..."
6563         run_acl_subtest setfacl || error  "setfacl test failed"
6564
6565         # inheritance test got from HP
6566         echo "performing inheritance..."
6567         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6568         chmod +x make-tree || error "chmod +x failed"
6569         run_acl_subtest inheritance || error "inheritance test failed"
6570         rm -f make-tree
6571
6572         echo "LU-974 ignore umask when acl is enabled..."
6573         run_acl_subtest 974 || error "LU-974 umask test failed"
6574         if [ $MDSCOUNT -ge 2 ]; then
6575                 run_acl_subtest 974_remote ||
6576                         error "LU-974 umask test failed under remote dir"
6577         fi
6578
6579         echo "LU-2561 newly created file is same size as directory..."
6580         run_acl_subtest 2561 || error "LU-2561 test failed"
6581
6582         cd $SAVE_PWD
6583         umask $SAVE_UMASK
6584
6585         for num in $(seq $MDSCOUNT); do
6586                 if [ "${identity_old[$num]}" = 1 ]; then
6587                         switch_identity $num false || identity_old[$num]=$?
6588                 fi
6589         done
6590 }
6591 run_test 103 "acl test ========================================="
6592
6593 test_104a() {
6594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6595         touch $DIR/$tfile
6596         lfs df || error "lfs df failed"
6597         lfs df -ih || error "lfs df -ih failed"
6598         lfs df -h $DIR || error "lfs df -h $DIR failed"
6599         lfs df -i $DIR || error "lfs df -i $DIR failed"
6600         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6601         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6602
6603         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6604         lctl --device %$OSC deactivate
6605         lfs df || error "lfs df with deactivated OSC failed"
6606         lctl --device %$OSC activate
6607         # wait the osc back to normal
6608         wait_osc_import_state client ost FULL
6609
6610         lfs df || error "lfs df with reactivated OSC failed"
6611         rm -f $DIR/$tfile
6612 }
6613 run_test 104a "lfs df [-ih] [path] test ========================="
6614
6615 test_104b() {
6616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6617         [ $RUNAS_ID -eq $UID ] &&
6618                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6619         chmod 666 /dev/obd
6620         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6621                         grep "Permission denied" | wc -l)))
6622         if [ $denied_cnt -ne 0 ]; then
6623                 error "lfs check servers test failed"
6624         fi
6625 }
6626 run_test 104b "$RUNAS lfs check servers test ===================="
6627
6628 test_105a() {
6629         # doesn't work on 2.4 kernels
6630         touch $DIR/$tfile
6631         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6632                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6633         else
6634                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6635         fi
6636         rm -f $DIR/$tfile
6637 }
6638 run_test 105a "flock when mounted without -o flock test ========"
6639
6640 test_105b() {
6641         touch $DIR/$tfile
6642         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6643                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6644         else
6645                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6646         fi
6647         rm -f $DIR/$tfile
6648 }
6649 run_test 105b "fcntl when mounted without -o flock test ========"
6650
6651 test_105c() {
6652         touch $DIR/$tfile
6653         if [ -n "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ]; then
6654                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6655         else
6656                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6657         fi
6658         rm -f $DIR/$tfile
6659 }
6660 run_test 105c "lockf when mounted without -o flock test ========"
6661
6662 test_105d() { # bug 15924
6663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6664         test_mkdir -p $DIR/$tdir
6665         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6666                 skip "mount w/o flock enabled" && return
6667         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6668         $LCTL set_param fail_loc=0x80000315
6669         flocks_test 2 $DIR/$tdir
6670 }
6671 run_test 105d "flock race (should not freeze) ========"
6672
6673 test_105e() { # bug 22660 && 22040
6674         [ -z "$(mount | grep "$MOUNT.*flock" | grep -v noflock)" ] &&
6675                 skip "mount w/o flock enabled" && return
6676         touch $DIR/$tfile
6677         flocks_test 3 $DIR/$tfile
6678 }
6679 run_test 105e "Two conflicting flocks from same process ======="
6680
6681 test_106() { #bug 10921
6682         test_mkdir -p $DIR/$tdir
6683         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6684         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6685 }
6686 run_test 106 "attempt exec of dir followed by chown of that dir"
6687
6688 test_107() {
6689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6690         CDIR=`pwd`
6691         cd $DIR
6692
6693         local file=core
6694         rm -f $file
6695
6696         local save_pattern=$(sysctl -n kernel.core_pattern)
6697         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6698         sysctl -w kernel.core_pattern=$file
6699         sysctl -w kernel.core_uses_pid=0
6700
6701         ulimit -c unlimited
6702         sleep 60 &
6703         SLEEPPID=$!
6704
6705         sleep 1
6706
6707         kill -s 11 $SLEEPPID
6708         wait $SLEEPPID
6709         if [ -e $file ]; then
6710                 size=`stat -c%s $file`
6711                 [ $size -eq 0 ] && error "Fail to create core file $file"
6712         else
6713                 error "Fail to create core file $file"
6714         fi
6715         rm -f $file
6716         sysctl -w kernel.core_pattern=$save_pattern
6717         sysctl -w kernel.core_uses_pid=$save_uses_pid
6718         cd $CDIR
6719 }
6720 run_test 107 "Coredump on SIG"
6721
6722 test_110() {
6723         test_mkdir -p $DIR/$tdir
6724         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6725                 error "mkdir with 255 char failed"
6726         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6727                 error "mkdir with 256 char should fail, but did not"
6728         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6729                 error "create with 255 char failed"
6730         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6731                 error "create with 256 char should fail, but did not"
6732
6733         ls -l $DIR/$tdir
6734         rm -rf $DIR/$tdir
6735 }
6736 run_test 110 "filename length checking"
6737
6738 test_115() {
6739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6740         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6741             cut -c11-20)
6742         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6743             return
6744         echo "Starting with $OSTIO_pre threads"
6745
6746         NUMTEST=20000
6747         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6748         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6749         echo "$NUMTEST creates/unlinks"
6750         test_mkdir -p $DIR/$tdir
6751         createmany -o $DIR/$tdir/$tfile $NUMTEST
6752         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6753
6754         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6755             cut -c11-20)
6756
6757         # don't return an error
6758         [ $OSTIO_post == $OSTIO_pre ] && echo \
6759             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6760             echo "This may be fine, depending on what ran before this test" &&
6761             echo "and how fast this system is." && return
6762
6763         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6764 }
6765 run_test 115 "verify dynamic thread creation===================="
6766
6767 free_min_max () {
6768         wait_delete_completed
6769         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6770         echo OST kbytes available: ${AVAIL[@]}
6771         MAXI=0; MAXV=${AVAIL[0]}
6772         MINI=0; MINV=${AVAIL[0]}
6773         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6774             #echo OST $i: ${AVAIL[i]}kb
6775             if [ ${AVAIL[i]} -gt $MAXV ]; then
6776                 MAXV=${AVAIL[i]}; MAXI=$i
6777             fi
6778             if [ ${AVAIL[i]} -lt $MINV ]; then
6779                 MINV=${AVAIL[i]}; MINI=$i
6780             fi
6781         done
6782         echo Min free space: OST $MINI: $MINV
6783         echo Max free space: OST $MAXI: $MAXV
6784 }
6785
6786 test_116a() { # was previously test_116()
6787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6788         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6789
6790         echo -n "Free space priority "
6791         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
6792                 head -1
6793         declare -a AVAIL
6794         free_min_max
6795
6796         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
6797         trap simple_cleanup_common EXIT
6798
6799         # Check if we need to generate uneven OSTs
6800         test_mkdir -p $DIR/$tdir/OST${MINI}
6801         local FILL=$(($MINV / 4))
6802         local DIFF=$(($MAXV - $MINV))
6803         local DIFF2=$(($DIFF * 100 / $MINV))
6804
6805         local threshold=$(do_facet $SINGLEMDS \
6806                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -1)
6807         threshold=${threshold%%%}
6808         echo -n "Check for uneven OSTs: "
6809         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
6810
6811         if [ $DIFF2 -gt $threshold ]; then
6812                 echo "ok"
6813                 echo "Don't need to fill OST$MINI"
6814         else
6815                 # generate uneven OSTs. Write 2% over the QOS threshold value
6816                 echo "no"
6817                 DIFF=$(($threshold - $DIFF2 + 2))
6818                 DIFF2=$(( ($MINV * $DIFF)/100 ))
6819                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
6820                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
6821                         error "setstripe failed"
6822                 DIFF=$(($DIFF2 / 2048))
6823                 i=0
6824                 while [ $i -lt $DIFF ]; do
6825                         i=$(($i + 1))
6826                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
6827                                 bs=2M count=1 2>/dev/null
6828                         echo -n .
6829                 done
6830                 echo .
6831                 sync
6832                 sleep_maxage
6833                 free_min_max
6834         fi
6835
6836         DIFF=$(($MAXV - $MINV))
6837         DIFF2=$(($DIFF * 100 / $MINV))
6838         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
6839         if [ $DIFF2 -gt $threshold ]; then
6840                 echo "ok"
6841         else
6842                 echo "failed - QOS mode won't be used"
6843                 skip "QOS imbalance criteria not met"
6844                 simple_cleanup_common
6845                 return
6846         fi
6847
6848         MINI1=$MINI; MINV1=$MINV
6849         MAXI1=$MAXI; MAXV1=$MAXV
6850
6851         # now fill using QOS
6852         $SETSTRIPE -c 1 $DIR/$tdir
6853         FILL=$(($FILL / 200))
6854         if [ $FILL -gt 600 ]; then
6855                 FILL=600
6856         fi
6857         echo "writing $FILL files to QOS-assigned OSTs"
6858         i=0
6859         while [ $i -lt $FILL ]; do
6860                 i=$(($i + 1))
6861                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
6862                         count=1 2>/dev/null
6863                 echo -n .
6864         done
6865         echo "wrote $i 200k files"
6866         sync
6867         sleep_maxage
6868
6869         echo "Note: free space may not be updated, so measurements might be off"
6870         free_min_max
6871         DIFF2=$(($MAXV - $MINV))
6872         echo "free space delta: orig $DIFF final $DIFF2"
6873         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6874         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6875         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
6876         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6877         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
6878         FILL=$(($DIFF2 * 100 / $DIFF - 100))
6879         [ $DIFF -gt 0 ] &&
6880                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
6881
6882         # Figure out which files were written where
6883         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6884                   awk '/'$MINI1': / {print $2; exit}')
6885         echo $UUID
6886         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6887         echo "$MINC files created on smaller OST $MINI1"
6888         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6889                   awk '/'$MAXI1': / {print $2; exit}')
6890         echo $UUID
6891         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
6892         echo "$MAXC files created on larger OST $MAXI1"
6893         FILL=$(($MAXC * 100 / $MINC - 100))
6894         [ $MINC -gt 0 ] &&
6895                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
6896         [ $MAXC -gt $MINC ] ||
6897                 error_ignore LU-9 "stripe QOS didn't balance free space"
6898         simple_cleanup_common
6899 }
6900 run_test 116a "stripe QOS: free space balance ==================="
6901
6902 test_116b() { # LU-2093
6903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6904 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6905         local old_rr
6906         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6907         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6908         mkdir -p $DIR/$tdir
6909         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6910         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6911         do_facet $SINGLEMDS lctl set_param fail_loc=0
6912         rm -rf $DIR/$tdir
6913         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6914 }
6915 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6916
6917 test_117() # bug 10891
6918 {
6919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6920         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6921         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6922         lctl set_param fail_loc=0x21e
6923         > $DIR/$tfile || error "truncate failed"
6924         lctl set_param fail_loc=0
6925         echo "Truncate succeeded."
6926         rm -f $DIR/$tfile
6927 }
6928 run_test 117 "verify osd extend =========="
6929
6930 NO_SLOW_RESENDCOUNT=4
6931 export OLD_RESENDCOUNT=""
6932 set_resend_count () {
6933         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6934         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6935         lctl set_param -n $PROC_RESENDCOUNT $1
6936         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6937 }
6938
6939 # for reduce test_118* time (b=14842)
6940 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6941
6942 # Reset async IO behavior after error case
6943 reset_async() {
6944         FILE=$DIR/reset_async
6945
6946         # Ensure all OSCs are cleared
6947         $SETSTRIPE -c -1 $FILE
6948         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6949         sync
6950         rm $FILE
6951 }
6952
6953 test_118a() #bug 11710
6954 {
6955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6956         reset_async
6957
6958         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6959         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6960         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6961
6962         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6963                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6964                 return 1;
6965         fi
6966         rm -f $DIR/$tfile
6967 }
6968 run_test 118a "verify O_SYNC works =========="
6969
6970 test_118b()
6971 {
6972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6973         remote_ost_nodsh && skip "remote OST with nodsh" && return
6974
6975         reset_async
6976
6977         #define OBD_FAIL_OST_ENOENT 0x217
6978         set_nodes_failloc "$(osts_nodes)" 0x217
6979         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6980         RC=$?
6981         set_nodes_failloc "$(osts_nodes)" 0
6982         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6983         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6984                     grep -c writeback)
6985
6986         if [[ $RC -eq 0 ]]; then
6987                 error "Must return error due to dropped pages, rc=$RC"
6988                 return 1;
6989         fi
6990
6991         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6992                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6993                 return 1;
6994         fi
6995
6996         echo "Dirty pages not leaked on ENOENT"
6997
6998         # Due to the above error the OSC will issue all RPCs syncronously
6999         # until a subsequent RPC completes successfully without error.
7000         $MULTIOP $DIR/$tfile Ow4096yc
7001         rm -f $DIR/$tfile
7002
7003         return 0
7004 }
7005 run_test 118b "Reclaim dirty pages on fatal error =========="
7006
7007 test_118c()
7008 {
7009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7010
7011         # for 118c, restore the original resend count, LU-1940
7012         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7013                                 set_resend_count $OLD_RESENDCOUNT
7014         remote_ost_nodsh && skip "remote OST with nodsh" && return
7015
7016         reset_async
7017
7018         #define OBD_FAIL_OST_EROFS               0x216
7019         set_nodes_failloc "$(osts_nodes)" 0x216
7020
7021         # multiop should block due to fsync until pages are written
7022         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7023         MULTIPID=$!
7024         sleep 1
7025
7026         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7027                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7028         fi
7029
7030         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7031                     grep -c writeback)
7032         if [[ $WRITEBACK -eq 0 ]]; then
7033                 error "No page in writeback, writeback=$WRITEBACK"
7034         fi
7035
7036         set_nodes_failloc "$(osts_nodes)" 0
7037         wait $MULTIPID
7038         RC=$?
7039         if [[ $RC -ne 0 ]]; then
7040                 error "Multiop fsync failed, rc=$RC"
7041         fi
7042
7043         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7044         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7045                     grep -c writeback)
7046         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7047                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7048         fi
7049
7050         rm -f $DIR/$tfile
7051         echo "Dirty pages flushed via fsync on EROFS"
7052         return 0
7053 }
7054 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7055
7056 # continue to use small resend count to reduce test_118* time (b=14842)
7057 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7058
7059 test_118d()
7060 {
7061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7062         remote_ost_nodsh && skip "remote OST with nodsh" && return
7063
7064         reset_async
7065
7066         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7067         set_nodes_failloc "$(osts_nodes)" 0x214
7068         # multiop should block due to fsync until pages are written
7069         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7070         MULTIPID=$!
7071         sleep 1
7072
7073         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7074                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7075         fi
7076
7077         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7078                     grep -c writeback)
7079         if [[ $WRITEBACK -eq 0 ]]; then
7080                 error "No page in writeback, writeback=$WRITEBACK"
7081         fi
7082
7083         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7084         set_nodes_failloc "$(osts_nodes)" 0
7085
7086         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7087         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7088                     grep -c writeback)
7089         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7090                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7091         fi
7092
7093         rm -f $DIR/$tfile
7094         echo "Dirty pages gaurenteed flushed via fsync"
7095         return 0
7096 }
7097 run_test 118d "Fsync validation inject a delay of the bulk =========="
7098
7099 test_118f() {
7100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7101         reset_async
7102
7103         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7104         lctl set_param fail_loc=0x8000040a
7105
7106         # Should simulate EINVAL error which is fatal
7107         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7108         RC=$?
7109         if [[ $RC -eq 0 ]]; then
7110                 error "Must return error due to dropped pages, rc=$RC"
7111         fi
7112
7113         lctl set_param fail_loc=0x0
7114
7115         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7116         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7117         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7118                     grep -c writeback)
7119         if [[ $LOCKED -ne 0 ]]; then
7120                 error "Locked pages remain in cache, locked=$LOCKED"
7121         fi
7122
7123         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7124                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7125         fi
7126
7127         rm -f $DIR/$tfile
7128         echo "No pages locked after fsync"
7129
7130         reset_async
7131         return 0
7132 }
7133 run_test 118f "Simulate unrecoverable OSC side error =========="
7134
7135 test_118g() {
7136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7137         reset_async
7138
7139         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7140         lctl set_param fail_loc=0x406
7141
7142         # simulate local -ENOMEM
7143         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7144         RC=$?
7145
7146         lctl set_param fail_loc=0
7147         if [[ $RC -eq 0 ]]; then
7148                 error "Must return error due to dropped pages, rc=$RC"
7149         fi
7150
7151         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7152         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7153         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7154                         grep -c writeback)
7155         if [[ $LOCKED -ne 0 ]]; then
7156                 error "Locked pages remain in cache, locked=$LOCKED"
7157         fi
7158
7159         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7160                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7161         fi
7162
7163         rm -f $DIR/$tfile
7164         echo "No pages locked after fsync"
7165
7166         reset_async
7167         return 0
7168 }
7169 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7170
7171 test_118h() {
7172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7173         remote_ost_nodsh && skip "remote OST with nodsh" && return
7174
7175         reset_async
7176
7177         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7178         set_nodes_failloc "$(osts_nodes)" 0x20e
7179         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7180         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7181         RC=$?
7182
7183         set_nodes_failloc "$(osts_nodes)" 0
7184         if [[ $RC -eq 0 ]]; then
7185                 error "Must return error due to dropped pages, rc=$RC"
7186         fi
7187
7188         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7189         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7190         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7191                     grep -c writeback)
7192         if [[ $LOCKED -ne 0 ]]; then
7193                 error "Locked pages remain in cache, locked=$LOCKED"
7194         fi
7195
7196         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7197                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7198         fi
7199
7200         rm -f $DIR/$tfile
7201         echo "No pages locked after fsync"
7202
7203         return 0
7204 }
7205 run_test 118h "Verify timeout in handling recoverables errors  =========="
7206
7207 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7208
7209 test_118i() {
7210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7211         remote_ost_nodsh && skip "remote OST with nodsh" && return
7212
7213         reset_async
7214
7215         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7216         set_nodes_failloc "$(osts_nodes)" 0x20e
7217
7218         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7219         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7220         PID=$!
7221         sleep 5
7222         set_nodes_failloc "$(osts_nodes)" 0
7223
7224         wait $PID
7225         RC=$?
7226         if [[ $RC -ne 0 ]]; then
7227                 error "got error, but should be not, rc=$RC"
7228         fi
7229
7230         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7231         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7232         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7233         if [[ $LOCKED -ne 0 ]]; then
7234                 error "Locked pages remain in cache, locked=$LOCKED"
7235         fi
7236
7237         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7238                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7239         fi
7240
7241         rm -f $DIR/$tfile
7242         echo "No pages locked after fsync"
7243
7244         return 0
7245 }
7246 run_test 118i "Fix error before timeout in recoverable error  =========="
7247
7248 [ "$SLOW" = "no" ] && set_resend_count 4
7249
7250 test_118j() {
7251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7252         remote_ost_nodsh && skip "remote OST with nodsh" && return
7253
7254         reset_async
7255
7256         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7257         set_nodes_failloc "$(osts_nodes)" 0x220
7258
7259         # return -EIO from OST
7260         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7261         RC=$?
7262         set_nodes_failloc "$(osts_nodes)" 0x0
7263         if [[ $RC -eq 0 ]]; then
7264                 error "Must return error due to dropped pages, rc=$RC"
7265         fi
7266
7267         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7268         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7269         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7270         if [[ $LOCKED -ne 0 ]]; then
7271                 error "Locked pages remain in cache, locked=$LOCKED"
7272         fi
7273
7274         # in recoverable error on OST we want resend and stay until it finished
7275         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7276                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7277         fi
7278
7279         rm -f $DIR/$tfile
7280         echo "No pages locked after fsync"
7281
7282         return 0
7283 }
7284 run_test 118j "Simulate unrecoverable OST side error =========="
7285
7286 test_118k()
7287 {
7288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7289         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7290
7291         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7292         set_nodes_failloc "$(osts_nodes)" 0x20e
7293         test_mkdir -p $DIR/$tdir
7294
7295         for ((i=0;i<10;i++)); do
7296                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7297                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7298                 SLEEPPID=$!
7299                 sleep 0.500s
7300                 kill $SLEEPPID
7301                 wait $SLEEPPID
7302         done
7303
7304         set_nodes_failloc "$(osts_nodes)" 0
7305         rm -rf $DIR/$tdir
7306 }
7307 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7308
7309 test_118l()
7310 {
7311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7312         # LU-646
7313         test_mkdir -p $DIR/$tdir
7314         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7315         rm -rf $DIR/$tdir
7316 }
7317 run_test 118l "fsync dir ========="
7318
7319 test_118m() # LU-3066
7320 {
7321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7322         test_mkdir -p $DIR/$tdir
7323         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7324         rm -rf $DIR/$tdir
7325 }
7326 run_test 118m "fdatasync dir ========="
7327
7328 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7329
7330 test_119a() # bug 11737
7331 {
7332         BSIZE=$((512 * 1024))
7333         directio write $DIR/$tfile 0 1 $BSIZE
7334         # We ask to read two blocks, which is more than a file size.
7335         # directio will indicate an error when requested and actual
7336         # sizes aren't equeal (a normal situation in this case) and
7337         # print actual read amount.
7338         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7339         if [ "$NOB" != "$BSIZE" ]; then
7340                 error "read $NOB bytes instead of $BSIZE"
7341         fi
7342         rm -f $DIR/$tfile
7343 }
7344 run_test 119a "Short directIO read must return actual read amount"
7345
7346 test_119b() # bug 11737
7347 {
7348         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7349
7350         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7351         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7352         sync
7353         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7354                 error "direct read failed"
7355         rm -f $DIR/$tfile
7356 }
7357 run_test 119b "Sparse directIO read must return actual read amount"
7358
7359 test_119c() # bug 13099
7360 {
7361         BSIZE=1048576
7362         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7363         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7364         rm -f $DIR/$tfile
7365 }
7366 run_test 119c "Testing for direct read hitting hole"
7367
7368 test_119d() # bug 15950
7369 {
7370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7371         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7372         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7373         BSIZE=1048576
7374         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7375         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7376         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7377         lctl set_param fail_loc=0x40d
7378         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7379         pid_dio=$!
7380         sleep 1
7381         cat $DIR/$tfile > /dev/null &
7382         lctl set_param fail_loc=0
7383         pid_reads=$!
7384         wait $pid_dio
7385         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7386         sleep 2
7387         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7388         error "the read rpcs have not completed in 2s"
7389         rm -f $DIR/$tfile
7390         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7391 }
7392 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7393
7394 test_120a() {
7395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7396         test_mkdir -p $DIR/$tdir
7397         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7398                skip "no early lock cancel on server" && return 0
7399
7400         lru_resize_disable mdc
7401         lru_resize_disable osc
7402         cancel_lru_locks mdc
7403         # asynchronous object destroy at MDT could cause bl ast to client
7404         cancel_lru_locks osc
7405
7406         stat $DIR/$tdir > /dev/null
7407         can1=$(lctl get_param -n ldlm.services.ldlm_canceld.stats |
7408                                         awk '/ldlm_cancel/ {print $2}')
7409         blk1=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
7410                                         awk '/ldlm_bl_callback/ {print $2}')
7411         test_mkdir -c1 $DIR/$tdir/d1
7412         can2=$(lctl get_param -n ldlm.services.ldlm_canceld.stats |
7413                                         awk '/ldlm_cancel/ {print $2}')
7414         blk2=$(lctl get_param -n ldlm.services.ldlm_cbd.stats |
7415                                         awk '/ldlm_bl_callback/ {print $2}')
7416         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7417         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7418         lru_resize_enable mdc
7419         lru_resize_enable osc
7420 }
7421 run_test 120a "Early Lock Cancel: mkdir test"
7422
7423 test_120b() {
7424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7425         test_mkdir $DIR/$tdir
7426         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7427                skip "no early lock cancel on server" && return 0
7428         lru_resize_disable mdc
7429         lru_resize_disable osc
7430         cancel_lru_locks mdc
7431         stat $DIR/$tdir > /dev/null
7432         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7433         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7434         touch $DIR/$tdir/f1
7435         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7436         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7437         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7438         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7439         lru_resize_enable mdc
7440         lru_resize_enable osc
7441 }
7442 run_test 120b "Early Lock Cancel: create test"
7443
7444 test_120c() {
7445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7446         test_mkdir -c1 $DIR/$tdir
7447         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7448                skip "no early lock cancel on server" && return 0
7449         lru_resize_disable mdc
7450         lru_resize_disable osc
7451         test_mkdir -p -c1 $DIR/$tdir/d1
7452         test_mkdir -p -c1 $DIR/$tdir/d2
7453         touch $DIR/$tdir/d1/f1
7454         cancel_lru_locks mdc
7455         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7456         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7457         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7458         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7459         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7460         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7461         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7462         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7463         lru_resize_enable mdc
7464         lru_resize_enable osc
7465 }
7466 run_test 120c "Early Lock Cancel: link test"
7467
7468 test_120d() {
7469         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7470         test_mkdir -p -c1 $DIR/$tdir
7471         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7472                skip "no early lock cancel on server" && return 0
7473         lru_resize_disable mdc
7474         lru_resize_disable osc
7475         touch $DIR/$tdir
7476         cancel_lru_locks mdc
7477         stat $DIR/$tdir > /dev/null
7478         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7479         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7480         chmod a+x $DIR/$tdir
7481         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7482         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7483         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7484         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7485         lru_resize_enable mdc
7486         lru_resize_enable osc
7487 }
7488 run_test 120d "Early Lock Cancel: setattr test"
7489
7490 test_120e() {
7491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7492         test_mkdir -p -c1 $DIR/$tdir
7493         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7494                skip "no early lock cancel on server" && return 0
7495         lru_resize_disable mdc
7496         lru_resize_disable osc
7497         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7498         cancel_lru_locks mdc
7499         cancel_lru_locks osc
7500         dd if=$DIR/$tdir/f1 of=/dev/null
7501         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7502         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7503               awk '/ldlm_cancel/ {print $2}'`
7504         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7505               awk '/ldlm_bl_callback/ {print $2}'`
7506         unlink $DIR/$tdir/f1
7507         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7508               awk '/ldlm_cancel/ {print $2}'`
7509         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7510               awk '/ldlm_bl_callback/ {print $2}'`
7511         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7512         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7513         lru_resize_enable mdc
7514         lru_resize_enable osc
7515 }
7516 run_test 120e "Early Lock Cancel: unlink test"
7517
7518 test_120f() {
7519         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7520         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7521                skip "no early lock cancel on server" && return 0
7522         test_mkdir -p -c1 $DIR/$tdir
7523         lru_resize_disable mdc
7524         lru_resize_disable osc
7525         test_mkdir -p -c1 $DIR/$tdir/d1
7526         test_mkdir -p -c1 $DIR/$tdir/d2
7527         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7528         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7529         cancel_lru_locks mdc
7530         cancel_lru_locks osc
7531         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7532         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7533         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7534         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7535               awk '/ldlm_cancel/ {print $2}'`
7536         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7537               awk '/ldlm_bl_callback/ {print $2}'`
7538         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7539         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7540               awk '/ldlm_cancel/ {print $2}'`
7541         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7542               awk '/ldlm_bl_callback/ {print $2}'`
7543         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7544         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7545         lru_resize_enable mdc
7546         lru_resize_enable osc
7547 }
7548 run_test 120f "Early Lock Cancel: rename test"
7549
7550 test_120g() {
7551         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7552         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7553                skip "no early lock cancel on server" && return 0
7554         lru_resize_disable mdc
7555         lru_resize_disable osc
7556         count=10000
7557         echo create $count files
7558         test_mkdir -p $DIR/$tdir
7559         cancel_lru_locks mdc
7560         cancel_lru_locks osc
7561         t0=`date +%s`
7562
7563         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7564               awk '/ldlm_cancel/ {print $2}'`
7565         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7566               awk '/ldlm_bl_callback/ {print $2}'`
7567         createmany -o $DIR/$tdir/f $count
7568         sync
7569         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7570               awk '/ldlm_cancel/ {print $2}'`
7571         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7572               awk '/ldlm_bl_callback/ {print $2}'`
7573         t1=`date +%s`
7574         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7575         echo rm $count files
7576         rm -r $DIR/$tdir
7577         sync
7578         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7579               awk '/ldlm_cancel/ {print $2}'`
7580         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7581               awk '/ldlm_bl_callback/ {print $2}'`
7582         t2=`date +%s`
7583         echo total: $count removes in $((t2-t1))
7584         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7585         sleep 2
7586         # wait for commitment of removal
7587         lru_resize_enable mdc
7588         lru_resize_enable osc
7589 }
7590 run_test 120g "Early Lock Cancel: performance test"
7591
7592 test_121() { #bug #10589
7593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7594         rm -rf $DIR/$tfile
7595         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7596 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7597         lctl set_param fail_loc=0x310
7598         cancel_lru_locks osc > /dev/null
7599         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7600         lctl set_param fail_loc=0
7601         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7602 }
7603 run_test 121 "read cancel race ========="
7604
7605 test_123a() { # was test 123, statahead(bug 11401)
7606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7607         SLOWOK=0
7608         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7609             log "testing on UP system. Performance may be not as good as expected."
7610                         SLOWOK=1
7611         fi
7612
7613         rm -rf $DIR/$tdir
7614         test_mkdir -p $DIR/$tdir
7615         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7616         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7617         MULT=10
7618         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7619                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7620
7621                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7622                 lctl set_param -n llite.*.statahead_max 0
7623                 lctl get_param llite.*.statahead_max
7624                 cancel_lru_locks mdc
7625                 cancel_lru_locks osc
7626                 stime=`date +%s`
7627                 time ls -l $DIR/$tdir | wc -l
7628                 etime=`date +%s`
7629                 delta=$((etime - stime))
7630                 log "ls $i files without statahead: $delta sec"
7631                 lctl set_param llite.*.statahead_max=$max
7632
7633                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7634                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7635                 cancel_lru_locks mdc
7636                 cancel_lru_locks osc
7637                 stime=`date +%s`
7638                 time ls -l $DIR/$tdir | wc -l
7639                 etime=`date +%s`
7640                 delta_sa=$((etime - stime))
7641                 log "ls $i files with statahead: $delta_sa sec"
7642                 lctl get_param -n llite.*.statahead_stats
7643                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7644
7645                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7646                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7647
7648                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7649                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7650                     lctl set_param -n llite.*.statahead_max 0
7651                     lctl get_param llite.*.statahead_max
7652                     cancel_lru_locks mdc
7653                     cancel_lru_locks osc
7654                     stime=`date +%s`
7655                     time ls -l $DIR/$tdir | wc -l
7656                     etime=`date +%s`
7657                     delta=$((etime - stime))
7658                     log "ls $i files again without statahead: $delta sec"
7659                     lctl set_param llite.*.statahead_max=$max
7660                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7661                         if [  $SLOWOK -eq 0 ]; then
7662                                 error "ls $i files is slower with statahead!"
7663                         else
7664                                 log "ls $i files is slower with statahead!"
7665                         fi
7666                         break
7667                     fi
7668                 fi
7669
7670                 [ $delta -gt 20 ] && break
7671                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7672                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7673         done
7674         log "ls done"
7675
7676         stime=`date +%s`
7677         rm -r $DIR/$tdir
7678         sync
7679         etime=`date +%s`
7680         delta=$((etime - stime))
7681         log "rm -r $DIR/$tdir/: $delta seconds"
7682         log "rm done"
7683         lctl get_param -n llite.*.statahead_stats
7684 }
7685 run_test 123a "verify statahead work"
7686
7687 test_123b () { # statahead(bug 15027)
7688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7689         test_mkdir -p $DIR/$tdir
7690         createmany -o $DIR/$tdir/$tfile-%d 1000
7691
7692         cancel_lru_locks mdc
7693         cancel_lru_locks osc
7694
7695 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7696         lctl set_param fail_loc=0x80000803
7697         ls -lR $DIR/$tdir > /dev/null
7698         log "ls done"
7699         lctl set_param fail_loc=0x0
7700         lctl get_param -n llite.*.statahead_stats
7701         rm -r $DIR/$tdir
7702         sync
7703
7704 }
7705 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7706
7707 test_124a() {
7708         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7709         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7710                skip "no lru resize on server" && return 0
7711         local NR=2000
7712         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7713
7714         log "create $NR files at $DIR/$tdir"
7715         createmany -o $DIR/$tdir/f $NR ||
7716                 error "failed to create $NR files in $DIR/$tdir"
7717
7718         cancel_lru_locks mdc
7719         ls -l $DIR/$tdir > /dev/null
7720
7721         local NSDIR=""
7722         local LRU_SIZE=0
7723         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7724                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7725                 LRU_SIZE=$(lctl get_param -n $PARAM)
7726                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7727                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7728                                                 log "NSDIR=$NSDIR"
7729                         log "NS=$(basename $NSDIR)"
7730                         break
7731                 fi
7732         done
7733
7734         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7735                 skip "Not enough cached locks created!"
7736                 return 0
7737         fi
7738         log "LRU=$LRU_SIZE"
7739
7740         local SLEEP=30
7741
7742         # We know that lru resize allows one client to hold $LIMIT locks
7743         # for 10h. After that locks begin to be killed by client.
7744         local MAX_HRS=10
7745         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7746                 log "LIMIT=$LIMIT"
7747
7748         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7749         # killing locks. Some time was spent for creating locks. This means
7750         # that up to the moment of sleep finish we must have killed some of
7751         # them (10-100 locks). This depends on how fast ther were created.
7752         # Many of them were touched in almost the same moment and thus will
7753         # be killed in groups.
7754         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7755
7756         # Use $LRU_SIZE_B here to take into account real number of locks
7757         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7758         local LRU_SIZE_B=$LRU_SIZE
7759         log "LVF=$LVF"
7760         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7761                 log "OLD_LVF=$OLD_LVF"
7762         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7763
7764         # Let's make sure that we really have some margin. Client checks
7765         # cached locks every 10 sec.
7766         SLEEP=$((SLEEP+20))
7767         log "Sleep ${SLEEP} sec"
7768         local SEC=0
7769         while ((SEC<$SLEEP)); do
7770                 echo -n "..."
7771                 sleep 5
7772                 SEC=$((SEC+5))
7773                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7774                 echo -n "$LRU_SIZE"
7775         done
7776         echo ""
7777         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7778         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7779
7780         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7781                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7782                 unlinkmany $DIR/$tdir/f $NR
7783                 return
7784         }
7785
7786         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7787         log "unlink $NR files at $DIR/$tdir"
7788         unlinkmany $DIR/$tdir/f $NR
7789 }
7790 run_test 124a "lru resize ======================================="
7791
7792 get_max_pool_limit()
7793 {
7794         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7795         local max=0
7796         for l in $limit; do
7797                 if test $l -gt $max; then
7798                         max=$l
7799                 fi
7800         done
7801         echo $max
7802 }
7803
7804 test_124b() {
7805         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7806         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7807                skip "no lru resize on server" && return 0
7808
7809         LIMIT=`get_max_pool_limit`
7810
7811         NR=$(($(default_lru_size)*20))
7812         if [ $NR -gt $LIMIT ]; then
7813                 log "Limit lock number by $LIMIT locks"
7814                 NR=$LIMIT
7815         fi
7816         lru_resize_disable mdc
7817         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7818                 error "failed to create $DIR/$tdir/disable_lru_resize"
7819
7820         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7821         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7822         cancel_lru_locks mdc
7823         stime=`date +%s`
7824         PID=""
7825         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7826         PID="$PID $!"
7827         sleep 2
7828         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7829         PID="$PID $!"
7830         sleep 2
7831         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7832         PID="$PID $!"
7833         wait $PID
7834         etime=`date +%s`
7835         nolruresize_delta=$((etime-stime))
7836         log "ls -la time: $nolruresize_delta seconds"
7837         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7838         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7839
7840         lru_resize_enable mdc
7841         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7842                 error "failed to create $DIR/$tdir/enable_lru_resize"
7843
7844         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7845         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7846         cancel_lru_locks mdc
7847         stime=`date +%s`
7848         PID=""
7849         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7850         PID="$PID $!"
7851         sleep 2
7852         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7853         PID="$PID $!"
7854         sleep 2
7855         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7856         PID="$PID $!"
7857         wait $PID
7858         etime=`date +%s`
7859         lruresize_delta=$((etime-stime))
7860         log "ls -la time: $lruresize_delta seconds"
7861         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7862
7863         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7864                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7865         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7866                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7867         else
7868                 log "lru resize performs the same with no lru resize"
7869         fi
7870         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7871 }
7872 run_test 124b "lru resize (performance test) ======================="
7873
7874 test_125() { # 13358
7875         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7876         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7877         test_mkdir -p $DIR/d125 || error "mkdir failed"
7878         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7879         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7880         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7881 }
7882 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7883
7884 test_126() { # bug 12829/13455
7885         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7886         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7887         $GSS && skip "must run as gss disabled" && return
7888
7889         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7890         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7891         rm -f $DIR/$tfile
7892         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7893 }
7894 run_test 126 "check that the fsgid provided by the client is taken into account"
7895
7896 test_127a() { # bug 15521
7897         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7898         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7899         $LCTL set_param osc.*.stats=0
7900         FSIZE=$((2048 * 1024))
7901         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7902         cancel_lru_locks osc
7903         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7904
7905         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7906         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7907                 echo "got $COUNT $NAME"
7908                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7909                 eval $NAME=$COUNT || error "Wrong proc format"
7910
7911                 case $NAME in
7912                         read_bytes|write_bytes)
7913                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7914                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7915                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7916                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7917                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7918                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7919                                 error "sumsquare is too small: $SUMSQ"
7920                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7921                                 error "sumsquare is too big: $SUMSQ"
7922                         ;;
7923                         *) ;;
7924                 esac
7925         done < $DIR/${tfile}.tmp
7926
7927         #check that we actually got some stats
7928         [ "$read_bytes" ] || error "Missing read_bytes stats"
7929         [ "$write_bytes" ] || error "Missing write_bytes stats"
7930         [ "$read_bytes" != 0 ] || error "no read done"
7931         [ "$write_bytes" != 0 ] || error "no write done"
7932 }
7933 run_test 127a "verify the client stats are sane"
7934
7935 test_127b() { # bug LU-333
7936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7937         $LCTL set_param llite.*.stats=0
7938         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7939         # perform 2 reads and writes so MAX is different from SUM.
7940         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7941         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7942         cancel_lru_locks osc
7943         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7944         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7945
7946         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7947         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7948                 echo "got $COUNT $NAME"
7949                 eval $NAME=$COUNT || error "Wrong proc format"
7950
7951         case $NAME in
7952                 read_bytes)
7953                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7954                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7955                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7956                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7957                         ;;
7958                 write_bytes)
7959                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7960                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7961                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7962                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7963                         ;;
7964                         *) ;;
7965                 esac
7966         done < $TMP/${tfile}.tmp
7967
7968         #check that we actually got some stats
7969         [ "$read_bytes" ] || error "Missing read_bytes stats"
7970         [ "$write_bytes" ] || error "Missing write_bytes stats"
7971         [ "$read_bytes" != 0 ] || error "no read done"
7972         [ "$write_bytes" != 0 ] || error "no write done"
7973 }
7974 run_test 127b "verify the llite client stats are sane"
7975
7976 test_128() { # bug 15212
7977         touch $DIR/$tfile
7978         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7979                 find $DIR/$tfile
7980                 find $DIR/$tfile
7981         EOF
7982
7983         result=$(grep error $TMP/$tfile.log)
7984         rm -f $DIR/$tfile
7985         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7986 }
7987 run_test 128 "interactive lfs for 2 consecutive find's"
7988
7989 set_dir_limits () {
7990         local mntdev
7991         local canondev
7992         local node
7993
7994         local LDPROC=/proc/fs/ldiskfs
7995         local facets=$(get_facets MDS)
7996
7997         for facet in ${facets//,/ }; do
7998                 canondev=$(ldiskfs_canon \
7999                            *.$(convert_facet2label $facet).mntdev $facet)
8000                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8001                                                 LDPROC=/sys/fs/ldiskfs
8002                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8003         done
8004 }
8005
8006 test_129() {
8007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8008         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8009                 skip "Only applicable to ldiskfs-based MDTs"
8010                 return
8011         fi
8012         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8013         ENOSPC=28
8014         EFBIG=27
8015
8016         rm -rf $DIR/$tdir
8017         test_mkdir -p $DIR/$tdir
8018
8019         # block size of mds1
8020         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8021         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8022         local MAX=$((MDSBLOCKSIZE * 3))
8023         set_dir_limits $MAX
8024         local I=$(stat -c%s "$DIR/$tdir")
8025         local J=0
8026         local STRIPE_COUNT=1
8027         [ $MDSCOUNT -ge 2 ] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8028         MAX=$((MAX*STRIPE_COUNT))
8029         while [ ! $I -gt $MAX ]; do
8030                 $MULTIOP $DIR/$tdir/$J Oc
8031                 rc=$?
8032                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8033                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8034                 #and EFBIG for previous versions
8035                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8036                         set_dir_limits 0
8037                         echo "return code $rc received as expected"
8038                         multiop $DIR/$tdir/$J Oc ||
8039                                 error_exit "multiop failed w/o dir size limit"
8040
8041                         I=$(stat -c%s "$DIR/$tdir")
8042
8043                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8044                                         $(version_code 2.4.51) ]
8045                         then
8046                                 [ $I -eq $MAX ] && return 0
8047                         else
8048                                 [ $I -gt $MAX ] && return 0
8049                         fi
8050                         error_exit "current dir size $I, previous limit $MAX"
8051                 elif [ $rc -ne 0 ]; then
8052                         set_dir_limits 0
8053                         error_exit "return code $rc received instead of expected " \
8054                                    "$EFBIG or $ENOSPC, files in dir $I"
8055                 fi
8056                 J=$((J+1))
8057                 I=$(stat -c%s "$DIR/$tdir")
8058         done
8059
8060         set_dir_limits 0
8061         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8062 }
8063 run_test 129 "test directory size limit ========================"
8064
8065 OLDIFS="$IFS"
8066 cleanup_130() {
8067         trap 0
8068         IFS="$OLDIFS"
8069 }
8070
8071 test_130a() {
8072         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8073         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8074                 return
8075
8076         trap cleanup_130 EXIT RETURN
8077
8078         local fm_file=$DIR/$tfile
8079         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8080         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8081                 error "dd failed for $fm_file"
8082
8083         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8084         filefrag -ves $fm_file
8085         RC=$?
8086         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8087                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8088         [ $RC != 0 ] && error "filefrag $fm_file failed"
8089
8090         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8091                       grep -v "ext:" | grep -v "found")
8092         lun=$($GETSTRIPE -i $fm_file)
8093
8094         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8095         IFS=$'\n'
8096         tot_len=0
8097         for line in $filefrag_op
8098         do
8099                 frag_lun=`echo $line | cut -d: -f5`
8100                 ext_len=`echo $line | cut -d: -f4`
8101                 if (( $frag_lun != $lun )); then
8102                         cleanup_130
8103                         error "FIEMAP on 1-stripe file($fm_file) failed"
8104                         return
8105                 fi
8106                 (( tot_len += ext_len ))
8107         done
8108
8109         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8110                 cleanup_130
8111                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8112                 return
8113         fi
8114
8115         cleanup_130
8116
8117         echo "FIEMAP on single striped file succeeded"
8118 }
8119 run_test 130a "FIEMAP (1-stripe file)"
8120
8121 test_130b() {
8122         [ "$OSTCOUNT" -lt "2" ] &&
8123                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8124
8125         [ "$OSTCOUNT" -ge "10" ] &&
8126                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8127
8128         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8129         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8130                 return
8131
8132         trap cleanup_130 EXIT RETURN
8133
8134         local fm_file=$DIR/$tfile
8135         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8136         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8137                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8138
8139         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8140                 error "dd failed on $fm_file"
8141
8142         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8143         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8144                       grep -v "ext:" | grep -v "found")
8145
8146         last_lun=$(echo $filefrag_op | cut -d: -f5)
8147
8148         IFS=$'\n'
8149         tot_len=0
8150         num_luns=1
8151         for line in $filefrag_op
8152         do
8153                 frag_lun=`echo $line | cut -d: -f5`
8154                 ext_len=`echo $line | cut -d: -f4`
8155                 if (( $frag_lun != $last_lun )); then
8156                         if (( tot_len != 1024 )); then
8157                                 cleanup_130
8158                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8159                                 return
8160                         else
8161                                 (( num_luns += 1 ))
8162                                 tot_len=0
8163                         fi
8164                 fi
8165                 (( tot_len += ext_len ))
8166                 last_lun=$frag_lun
8167         done
8168         if (( num_luns != 2 || tot_len != 1024 )); then
8169                 cleanup_130
8170                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8171                 return
8172         fi
8173
8174         cleanup_130
8175
8176         echo "FIEMAP on 2-stripe file succeeded"
8177 }
8178 run_test 130b "FIEMAP (2-stripe file)"
8179
8180 test_130c() {
8181         [ "$OSTCOUNT" -lt "2" ] &&
8182                 skip_env "skipping FIEMAP on 2-stripe file" && return
8183
8184         [ "$OSTCOUNT" -ge "10" ] &&
8185                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8186
8187         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8188         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8189                 return
8190
8191         trap cleanup_130 EXIT RETURN
8192
8193         local fm_file=$DIR/$tfile
8194         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8195         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8196                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8197
8198         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8199
8200         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8201         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8202
8203         last_lun=`echo $filefrag_op | cut -d: -f5`
8204
8205         IFS=$'\n'
8206         tot_len=0
8207         num_luns=1
8208         for line in $filefrag_op
8209         do
8210                 frag_lun=`echo $line | cut -d: -f5`
8211                 ext_len=`echo $line | cut -d: -f4`
8212                 if (( $frag_lun != $last_lun )); then
8213                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8214                         if (( logical != 512 )); then
8215                                 cleanup_130
8216                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8217                                 return
8218                         fi
8219                         if (( tot_len != 512 )); then
8220                                 cleanup_130
8221                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8222                                 return
8223                         else
8224                                 (( num_luns += 1 ))
8225                                 tot_len=0
8226                         fi
8227                 fi
8228                 (( tot_len += ext_len ))
8229                 last_lun=$frag_lun
8230         done
8231         if (( num_luns != 2 || tot_len != 512 )); then
8232                 cleanup_130
8233                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8234                 return
8235         fi
8236
8237         cleanup_130
8238
8239         echo "FIEMAP on 2-stripe file with hole succeeded"
8240 }
8241 run_test 130c "FIEMAP (2-stripe file with hole)"
8242
8243 test_130d() {
8244         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8245
8246         [ "$OSTCOUNT" -ge "10" ] &&
8247                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8248
8249         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8250         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8251
8252         trap cleanup_130 EXIT RETURN
8253
8254         local fm_file=$DIR/$tfile
8255         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8256         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8257                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8258
8259         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8260         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8261                 error "dd failed on $fm_file"
8262
8263         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8264         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8265                 grep -v "ext:" | grep -v "found"`
8266
8267         last_lun=`echo $filefrag_op | cut -d: -f5`
8268
8269         IFS=$'\n'
8270         tot_len=0
8271         num_luns=1
8272         for line in $filefrag_op
8273         do
8274                 frag_lun=`echo $line | cut -d: -f5`
8275                 ext_len=`echo $line | cut -d: -f4`
8276                 if (( $frag_lun != $last_lun )); then
8277                         if (( tot_len != 1024 )); then
8278                                 cleanup_130
8279                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8280                                 return
8281                         else
8282                                 (( num_luns += 1 ))
8283                                 tot_len=0
8284                         fi
8285                 fi
8286                 (( tot_len += ext_len ))
8287                 last_lun=$frag_lun
8288         done
8289         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8290                 cleanup_130
8291                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8292                 return
8293         fi
8294
8295         cleanup_130
8296
8297         echo "FIEMAP on N-stripe file succeeded"
8298 }
8299 run_test 130d "FIEMAP (N-stripe file)"
8300
8301 test_130e() {
8302         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8303
8304         [ "$OSTCOUNT" -ge "10" ] &&
8305                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8306
8307         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8308         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8309
8310         trap cleanup_130 EXIT RETURN
8311
8312         local fm_file=$DIR/$tfile
8313         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8314         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8315                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8316
8317         NUM_BLKS=512
8318         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8319         for ((i = 0; i < $NUM_BLKS; i++))
8320         do
8321                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8322         done
8323
8324         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8325         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8326
8327         last_lun=`echo $filefrag_op | cut -d: -f5`
8328
8329         IFS=$'\n'
8330         tot_len=0
8331         num_luns=1
8332         for line in $filefrag_op
8333         do
8334                 frag_lun=`echo $line | cut -d: -f5`
8335                 ext_len=`echo $line | cut -d: -f4`
8336                 if (( $frag_lun != $last_lun )); then
8337                         if (( tot_len != $EXPECTED_LEN )); then
8338                                 cleanup_130
8339                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8340                                 return
8341                         else
8342                                 (( num_luns += 1 ))
8343                                 tot_len=0
8344                         fi
8345                 fi
8346                 (( tot_len += ext_len ))
8347                 last_lun=$frag_lun
8348         done
8349         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8350                 cleanup_130
8351                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8352                 return
8353         fi
8354
8355         cleanup_130
8356
8357         echo "FIEMAP with continuation calls succeeded"
8358 }
8359 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8360
8361 # Test for writev/readv
8362 test_131a() {
8363         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8364         error "writev test failed"
8365         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8366         error "readv failed"
8367         rm -f $DIR/$tfile
8368 }
8369 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8370
8371 test_131b() {
8372         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8373         error "append writev test failed"
8374         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8375         error "append writev test failed"
8376         rm -f $DIR/$tfile
8377 }
8378 run_test 131b "test append writev"
8379
8380 test_131c() {
8381         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8382         error "NOT PASS"
8383 }
8384 run_test 131c "test read/write on file w/o objects"
8385
8386 test_131d() {
8387         rwv -f $DIR/$tfile -w -n 1 1572864
8388         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8389         if [ "$NOB" != 1572864 ]; then
8390                 error "Short read filed: read $NOB bytes instead of 1572864"
8391         fi
8392         rm -f $DIR/$tfile
8393 }
8394 run_test 131d "test short read"
8395
8396 test_131e() {
8397         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8398         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8399         error "read hitting hole failed"
8400         rm -f $DIR/$tfile
8401 }
8402 run_test 131e "test read hitting hole"
8403
8404 get_ost_param() {
8405         local token=$1
8406         local gl_sum=0
8407         for node in $(osts_nodes); do
8408                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8409                 [ x$gl = x"" ] && gl=0
8410                 gl_sum=$((gl_sum + gl))
8411         done
8412         echo $gl_sum
8413 }
8414
8415 som_mode_switch() {
8416         local som=$1
8417         local gl1=$2
8418         local gl2=$3
8419
8420         if [ x$som = x"enabled" ]; then
8421                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8422                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8423                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8424         else
8425                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8426                 MOUNTOPT="$MOUNTOPT,som_preview"
8427                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8428         fi
8429
8430         # do remount to make new mount-conf parameters actual
8431         echo remounting...
8432         sync
8433         stopall
8434         setupall
8435 }
8436
8437 test_132() { #1028, SOM
8438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8439         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8440         local num=$(get_mds_dir $DIR)
8441         local mymds=mds${num}
8442         local MOUNTOPT_SAVE=$MOUNTOPT
8443
8444         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8445         cancel_lru_locks osc
8446
8447         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8448
8449         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8450         stat $DIR/$tfile >/dev/null
8451         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8452         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8453         rm $DIR/$tfile
8454         som_mode_switch $som1 $gl1 $gl2
8455
8456         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8457         cancel_lru_locks osc
8458
8459         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8460         if [ $som1 == $som2 ]; then
8461             error "som is still "$som2
8462             if [ x$som2 = x"enabled" ]; then
8463                 som2="disabled"
8464             else
8465                 som2="enabled"
8466             fi
8467         fi
8468
8469         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8470         stat $DIR/$tfile >/dev/null
8471         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8472         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8473         som_mode_switch $som2 $gl1 $gl2
8474         MOUNTOPT=$MOUNTOPT_SAVE
8475 }
8476 run_test 132 "som avoids glimpse rpc"
8477
8478 check_stats() {
8479         local res
8480         local count
8481         case $1 in
8482         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8483                  ;;
8484         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8485                  ;;
8486         *) error "Wrong argument $1" ;;
8487         esac
8488         echo $res
8489         count=`echo $res | awk '{print $2}'`
8490         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8491         # if the argument $3 is zero, it means any stat increment is ok.
8492         if [ $3 -gt 0 ] ; then
8493                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
8494         fi
8495 }
8496
8497 test_133a() {
8498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8499         remote_ost_nodsh && skip "remote OST with nodsh" && return
8500         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8501
8502         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8503                 { skip "MDS doesn't support rename stats"; return; }
8504         local testdir=$DIR/${tdir}/stats_testdir
8505         mkdir -p $DIR/${tdir}
8506
8507         # clear stats.
8508         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8509         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8510
8511         # verify mdt stats first.
8512         mkdir ${testdir} || error "mkdir failed"
8513         check_stats $SINGLEMDS "mkdir" 1
8514         touch ${testdir}/${tfile} || "touch failed"
8515         check_stats $SINGLEMDS "open" 1
8516         check_stats $SINGLEMDS "close" 1
8517         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8518         check_stats $SINGLEMDS "mknod" 1
8519         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8520         check_stats $SINGLEMDS "unlink" 1
8521         rm -f ${testdir}/${tfile} || error "file remove failed"
8522         check_stats $SINGLEMDS "unlink" 2
8523
8524         # remove working dir and check mdt stats again.
8525         rmdir ${testdir} || error "rmdir failed"
8526         check_stats $SINGLEMDS "rmdir" 1
8527
8528         local testdir1=$DIR/${tdir}/stats_testdir1
8529         mkdir -p ${testdir}
8530         mkdir -p ${testdir1}
8531         touch ${testdir1}/test1
8532         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8533         check_stats $SINGLEMDS "crossdir_rename" 1
8534
8535         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8536         check_stats $SINGLEMDS "samedir_rename" 1
8537
8538         rm -rf $DIR/${tdir}
8539 }
8540 run_test 133a "Verifying MDT stats ========================================"
8541
8542 test_133b() {
8543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8544         remote_ost_nodsh && skip "remote OST with nodsh" && return
8545         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8546         local testdir=$DIR/${tdir}/stats_testdir
8547         mkdir -p ${testdir} || error "mkdir failed"
8548         touch ${testdir}/${tfile} || "touch failed"
8549         cancel_lru_locks mdc
8550
8551         # clear stats.
8552         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8553         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8554
8555         # extra mdt stats verification.
8556         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8557         check_stats $SINGLEMDS "setattr" 1
8558         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8559         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8560         then            # LU-1740
8561                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8562                 check_stats $SINGLEMDS "getattr" 1
8563         fi
8564         $LFS df || error "lfs failed"
8565         check_stats $SINGLEMDS "statfs" 1
8566
8567         rm -rf $DIR/${tdir}
8568 }
8569 run_test 133b "Verifying extra MDT stats =================================="
8570
8571 test_133c() {
8572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8573         remote_ost_nodsh && skip "remote OST with nodsh" && return
8574         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8575         local testdir=$DIR/${tdir}/stats_testdir
8576         test_mkdir -p ${testdir} || error "mkdir failed"
8577
8578         # verify obdfilter stats.
8579         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8580         sync
8581         cancel_lru_locks osc
8582         wait_delete_completed
8583
8584         # clear stats.
8585         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8586         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8587
8588         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8589         sync
8590         cancel_lru_locks osc
8591         check_stats ost "write" 1
8592
8593         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8594         check_stats ost "read" 1
8595
8596         > ${testdir}/${tfile} || error "truncate failed"
8597         check_stats ost "punch" 1
8598
8599         rm -f ${testdir}/${tfile} || error "file remove failed"
8600         wait_delete_completed
8601         check_stats ost "destroy" 1
8602
8603         rm -rf $DIR/${tdir}
8604 }
8605 run_test 133c "Verifying OST stats ========================================"
8606
8607 order_2() {
8608     local value=$1
8609     local orig=$value
8610     local order=1
8611
8612     while [ $value -ge 2 ]; do
8613         order=$((order*2))
8614         value=$((value/2))
8615     done
8616
8617     if [ $orig -gt $order ]; then
8618         order=$((order*2))
8619     fi
8620     echo $order
8621 }
8622
8623 size_in_KMGT() {
8624     local value=$1
8625     local size=('K' 'M' 'G' 'T');
8626     local i=0
8627     local size_string=$value
8628
8629     while [ $value -ge 1024 ]; do
8630         if [ $i -gt 3 ]; then
8631             #T is the biggest unit we get here, if that is bigger,
8632             #just return XXXT
8633             size_string=${value}T
8634             break
8635         fi
8636         value=$((value >> 10))
8637         if [ $value -lt 1024 ]; then
8638             size_string=${value}${size[$i]}
8639             break
8640         fi
8641         i=$((i + 1))
8642     done
8643
8644     echo $size_string
8645 }
8646
8647 get_rename_size() {
8648     local size=$1
8649     local context=${2:-.}
8650     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8651                 grep -A1 $context |
8652                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8653     echo $sample
8654 }
8655
8656 test_133d() {
8657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8658         remote_ost_nodsh && skip "remote OST with nodsh" && return
8659         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8660         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8661         { skip "MDS doesn't support rename stats"; return; }
8662
8663         local testdir1=$DIR/${tdir}/stats_testdir1
8664         local testdir2=$DIR/${tdir}/stats_testdir2
8665
8666         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8667
8668         mkdir -p ${testdir1} || error "mkdir failed"
8669         mkdir -p ${testdir2} || error "mkdir failed"
8670
8671         createmany -o $testdir1/test 512 || error "createmany failed"
8672
8673         # check samedir rename size
8674         mv ${testdir1}/test0 ${testdir1}/test_0
8675
8676         local testdir1_size=$(ls -l $DIR/${tdir} |
8677                 awk '/stats_testdir1/ {print $5}')
8678         local testdir2_size=$(ls -l $DIR/${tdir} |
8679                 awk '/stats_testdir2/ {print $5}')
8680
8681         testdir1_size=$(order_2 $testdir1_size)
8682         testdir2_size=$(order_2 $testdir2_size)
8683
8684         testdir1_size=$(size_in_KMGT $testdir1_size)
8685         testdir2_size=$(size_in_KMGT $testdir2_size)
8686
8687         echo "source rename dir size: ${testdir1_size}"
8688         echo "target rename dir size: ${testdir2_size}"
8689
8690         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8691         eval $cmd || error "$cmd failed"
8692         local samedir=$($cmd | grep 'same_dir')
8693         local same_sample=$(get_rename_size $testdir1_size)
8694         [ -z "$samedir" ] && error "samedir_rename_size count error"
8695         [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8696         echo "Check same dir rename stats success"
8697
8698         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8699
8700         # check crossdir rename size
8701         mv ${testdir1}/test_0 ${testdir2}/test_0
8702
8703         testdir1_size=$(ls -l $DIR/${tdir} |
8704                 awk '/stats_testdir1/ {print $5}')
8705         testdir2_size=$(ls -l $DIR/${tdir} |
8706                 awk '/stats_testdir2/ {print $5}')
8707
8708         testdir1_size=$(order_2 $testdir1_size)
8709         testdir2_size=$(order_2 $testdir2_size)
8710
8711         testdir1_size=$(size_in_KMGT $testdir1_size)
8712         testdir2_size=$(size_in_KMGT $testdir2_size)
8713
8714         echo "source rename dir size: ${testdir1_size}"
8715         echo "target rename dir size: ${testdir2_size}"
8716
8717         eval $cmd || error "$cmd failed"
8718         local crossdir=$($cmd | grep 'crossdir')
8719         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8720         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8721         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8722         [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8723         [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8724         echo "Check cross dir rename stats success"
8725         rm -rf $DIR/${tdir}
8726 }
8727 run_test 133d "Verifying rename_stats ========================================"
8728
8729 test_133e() {
8730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8731         local testdir=$DIR/${tdir}/stats_testdir
8732         local ctr f0 f1 bs=32768 count=42 sum
8733
8734         remote_ost_nodsh && skip "remote OST with nodsh" && return
8735         mkdir -p ${testdir} || error "mkdir failed"
8736
8737         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8738
8739         for ctr in {write,read}_bytes; do
8740                 sync
8741                 cancel_lru_locks osc
8742
8743                 do_facet ost1 $LCTL set_param -n \
8744                         "obdfilter.*.exports.clear=clear"
8745
8746                 if [ $ctr = write_bytes ]; then
8747                         f0=/dev/zero
8748                         f1=${testdir}/${tfile}
8749                 else
8750                         f0=${testdir}/${tfile}
8751                         f1=/dev/null
8752                 fi
8753
8754                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8755                         error "dd failed"
8756                 sync
8757                 cancel_lru_locks osc
8758
8759                 sum=$(do_facet ost1 $LCTL get_param \
8760                                 "obdfilter.*.exports.*.stats" | \
8761                           awk -v ctr=$ctr '\
8762                                 BEGIN { sum = 0 }
8763                                 $1 == ctr { sum += $7 }
8764                                 END { print sum }')
8765
8766                 if ((sum != bs * count)); then
8767                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8768                 fi
8769         done
8770
8771         rm -rf $DIR/${tdir}
8772 }
8773 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8774
8775 test_133f() {
8776         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8777         local facet
8778
8779         # First without trusting modes.
8780         find $proc_dirs \
8781                 -exec cat '{}' \; &> /dev/null
8782
8783         # Second verifying readability.
8784         find $proc_dirs \
8785                 -type f \
8786                 -readable \
8787                 -exec cat '{}' \; > /dev/null ||
8788                         error "proc file read failed"
8789
8790         for facet in $SINGLEMDS ost1; do
8791                 do_facet $facet find $proc_dirs \
8792                         -not -name req_history \
8793                         -exec cat '{}' \\\; &> /dev/null
8794
8795             do_facet $facet     find $proc_dirs \
8796                         -not -name req_history \
8797                         -type f \
8798                         -readable \
8799                         -exec cat '{}' \\\; > /dev/null ||
8800                                 error "proc file read failed"
8801         done
8802 }
8803 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
8804
8805 test_140() { #bug-17379
8806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8807         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8808         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8809         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8810
8811         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8812         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8813         local i=0
8814         while i=`expr $i + 1`; do
8815                 test_mkdir -p $i || error "Creating dir $i"
8816                 cd $i || error "Changing to $i"
8817                 ln -s ../stat stat || error "Creating stat symlink"
8818                 # Read the symlink until ELOOP present,
8819                 # not LBUGing the system is considered success,
8820                 # we didn't overrun the stack.
8821                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8822                 [ $ret -ne 0 ] && {
8823                         if [ $ret -eq 40 ]; then
8824                                 break  # -ELOOP
8825                         else
8826                                 error "Open stat symlink"
8827                                 return
8828                         fi
8829                 }
8830         done
8831         i=`expr $i - 1`
8832         echo "The symlink depth = $i"
8833         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8834                                         error "Invalid symlink depth"
8835
8836         # Test recursive symlink
8837         ln -s symlink_self symlink_self
8838         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8839         echo "open symlink_self returns $ret"
8840         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8841 }
8842 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8843
8844 test_150() {
8845         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8846         local TF="$TMP/$tfile"
8847
8848         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8849         cp $TF $DIR/$tfile
8850         cancel_lru_locks osc
8851         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8852         remount_client $MOUNT
8853         df -P $MOUNT
8854         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8855
8856         $TRUNCATE $TF 6000
8857         $TRUNCATE $DIR/$tfile 6000
8858         cancel_lru_locks osc
8859         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8860
8861         echo "12345" >>$TF
8862         echo "12345" >>$DIR/$tfile
8863         cancel_lru_locks osc
8864         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8865
8866         echo "12345" >>$TF
8867         echo "12345" >>$DIR/$tfile
8868         cancel_lru_locks osc
8869         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8870
8871         rm -f $TF
8872         true
8873 }
8874 run_test 150 "truncate/append tests"
8875
8876 #LU-2902 roc_hit was not able to read all values from lproc
8877 function roc_hit_init() {
8878         local list=$(comma_list $(osts_nodes))
8879         local dir=$DIR/$tdir-check
8880         local file=$dir/file
8881         local BEFORE
8882         local AFTER
8883         local idx
8884
8885         test_mkdir -p $dir
8886         #use setstripe to do a write to every ost
8887         for i in $(seq 0 $((OSTCOUNT-1))); do
8888                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
8889                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
8890                 idx=$(printf %04x $i)
8891                 BEFORE=$(get_osd_param $list *OST*$idx stats |
8892                     awk '$1 == "cache_access" {sum += $2} END { print sum }')
8893                 if [ -z "$BEFORE" ]; then
8894                         BEFORE=0
8895                 fi
8896
8897                 cancel_lru_locks osc
8898                 cat $file >/dev/null
8899
8900                 AFTER=$(get_osd_param $list *OST*$idx stats |
8901                     awk '$1 == "cache_access" {sum += $2} END { print sum }')
8902
8903                 echo BEFORE:$BEFORE AFTER:$AFTER
8904                 if ! let "AFTER - BEFORE == 4"; then
8905                         rm -rf $dir
8906                         error "roc_hit is not safe to use"
8907                 fi
8908                 rm $file
8909         done
8910
8911         rm -rf $dir
8912 }
8913
8914 function roc_hit() {
8915         local list=$(comma_list $(osts_nodes))
8916         echo $(get_osd_param $list '' stats |
8917                 awk '$1 == "cache_hit" {sum += $2} END { print sum }')
8918 }
8919
8920 function set_cache() {
8921         local on=1
8922
8923         if [ "$2" == "off" ]; then
8924                 on=0;
8925         fi
8926         local list=$(comma_list $(osts_nodes))
8927         set_osd_param $list '' $1_cache_enable $on
8928
8929         cancel_lru_locks osc
8930 }
8931
8932 test_151() {
8933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8934         remote_ost_nodsh && skip "remote OST with nodsh" && return
8935
8936         local CPAGES=3
8937         local list=$(comma_list $(osts_nodes))
8938
8939         # check whether obdfilter is cache capable at all
8940         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8941                 echo "not cache-capable obdfilter"
8942                 return 0
8943         fi
8944
8945         # check cache is enabled on all obdfilters
8946         if get_osd_param $list '' read_cache_enable | grep 0; then
8947                 echo "oss cache is disabled"
8948                 return 0
8949         fi
8950
8951         set_osd_param $list '' writethrough_cache_enable 1
8952
8953         # check write cache is enabled on all obdfilters
8954         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
8955                 echo "oss write cache is NOT enabled"
8956                 return 0
8957         fi
8958
8959         roc_hit_init
8960
8961         #define OBD_FAIL_OBD_NO_LRU  0x609
8962         do_nodes $list $LCTL set_param fail_loc=0x609
8963
8964         # pages should be in the case right after write
8965         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
8966                 error "dd failed"
8967
8968         local BEFORE=$(roc_hit)
8969         cancel_lru_locks osc
8970         cat $DIR/$tfile >/dev/null
8971         local AFTER=$(roc_hit)
8972
8973         do_nodes $list $LCTL set_param fail_loc=0
8974
8975         if ! let "AFTER - BEFORE == CPAGES"; then
8976                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8977         fi
8978
8979         # the following read invalidates the cache
8980         cancel_lru_locks osc
8981         set_osd_param $list '' read_cache_enable 0
8982         cat $DIR/$tfile >/dev/null
8983
8984         # now data shouldn't be found in the cache
8985         BEFORE=$(roc_hit)
8986         cancel_lru_locks osc
8987         cat $DIR/$tfile >/dev/null
8988         AFTER=$(roc_hit)
8989         if let "AFTER - BEFORE != 0"; then
8990                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8991         fi
8992
8993         set_osd_param $list '' read_cache_enable 1
8994         rm -f $DIR/$tfile
8995 }
8996 run_test 151 "test cache on oss and controls ==============================="
8997
8998 test_152() {
8999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9000         local TF="$TMP/$tfile"
9001
9002         # simulate ENOMEM during write
9003 #define OBD_FAIL_OST_NOMEM      0x226
9004         lctl set_param fail_loc=0x80000226
9005         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9006         cp $TF $DIR/$tfile
9007         sync || error "sync failed"
9008         lctl set_param fail_loc=0
9009
9010         # discard client's cache
9011         cancel_lru_locks osc
9012
9013         # simulate ENOMEM during read
9014         lctl set_param fail_loc=0x80000226
9015         cmp $TF $DIR/$tfile || error "cmp failed"
9016         lctl set_param fail_loc=0
9017
9018         rm -f $TF
9019 }
9020 run_test 152 "test read/write with enomem ============================"
9021
9022 test_153() {
9023         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9024 }
9025 run_test 153 "test if fdatasync does not crash ======================="
9026
9027 dot_lustre_fid_permission_check() {
9028         local fid=$1
9029         local ffid=$MOUNT/.lustre/fid/$fid
9030         local test_dir=$2
9031
9032         echo "stat fid $fid"
9033         stat $ffid > /dev/null || error "stat $ffid failed."
9034         echo "touch fid $fid"
9035         touch $ffid || error "touch $ffid failed."
9036         echo "write to fid $fid"
9037         cat /etc/hosts > $ffid || error "write $ffid failed."
9038         echo "read fid $fid"
9039         diff /etc/hosts $ffid || error "read $ffid failed."
9040         echo "append write to fid $fid"
9041         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9042         echo "rename fid $fid"
9043         mv $ffid $test_dir/$tfile.1 &&
9044                 error "rename $ffid to $tfile.1 should fail."
9045         touch $test_dir/$tfile.1
9046         mv $test_dir/$tfile.1 $ffid &&
9047                 error "rename $tfile.1 to $ffid should fail."
9048         rm -f $test_dir/$tfile.1
9049         echo "truncate fid $fid"
9050         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9051         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9052                 echo "link fid $fid"
9053                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9054         fi
9055         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9056                 echo "setfacl fid $fid"
9057                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9058                 echo "getfacl fid $fid"
9059                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9060         fi
9061         echo "unlink fid $fid"
9062         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9063         echo "mknod fid $fid"
9064         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9065
9066         fid=[0xf00000400:0x1:0x0]
9067         ffid=$MOUNT/.lustre/fid/$fid
9068
9069         echo "stat non-exist fid $fid"
9070         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9071         echo "write to non-exist fid $fid"
9072         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9073         echo "link new fid $fid"
9074         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9075
9076         mkdir -p $test_dir/$tdir
9077         touch $test_dir/$tdir/$tfile
9078         fid=$($LFS path2fid $test_dir/$tdir)
9079         rc=$?
9080         [ $rc -ne 0 ] &&
9081                 error "error: could not get fid for $test_dir/$dir/$tfile."
9082
9083         ffid=$MOUNT/.lustre/fid/$fid
9084
9085         echo "ls $fid"
9086         ls $ffid > /dev/null || error "ls $ffid failed."
9087         echo "touch $fid/$tfile.1"
9088         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9089
9090         echo "touch $MOUNT/.lustre/fid/$tfile"
9091         touch $MOUNT/.lustre/fid/$tfile && \
9092                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9093
9094         echo "setxattr to $MOUNT/.lustre/fid"
9095         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9096
9097         echo "listxattr for $MOUNT/.lustre/fid"
9098         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9099
9100         echo "delxattr from $MOUNT/.lustre/fid"
9101         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9102
9103         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9104         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9105                 error "touch invalid fid should fail."
9106
9107         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9108         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9109                 error "touch non-normal fid should fail."
9110
9111         echo "rename $tdir to $MOUNT/.lustre/fid"
9112         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9113                 error "rename to $MOUNT/.lustre/fid should fail."
9114
9115         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9116         then            # LU-3547
9117                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9118                 local new_obf_mode=777
9119
9120                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9121                 chmod $new_obf_mode $DIR/.lustre/fid ||
9122                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9123
9124                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9125                 [ $obf_mode -eq $new_obf_mode ] ||
9126                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9127
9128                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9129                 chmod $old_obf_mode $DIR/.lustre/fid ||
9130                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9131         fi
9132
9133         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9134         fid=$($LFS path2fid $test_dir/$tfile-2)
9135         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9136         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9137                 error "create lov data thru .lustre should fail."
9138         echo "cp /etc/passwd $test_dir/$tfile-2"
9139         cp /etc/passwd $test_dir/$tfile-2 ||
9140                 error "copy to $test_dir/$tfile-2 failed."
9141         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9142         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9143                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9144
9145         rm -rf $test_dir/tfile.lnk
9146         rm -rf $test_dir/$tfile-2
9147 }
9148
9149 test_154A() {
9150         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9151                 skip "Need MDS version at least 2.4.1" && return
9152
9153         touch $DIR/$tfile
9154         local FID=$($LFS path2fid $DIR/$tfile)
9155         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9156
9157         # check that we get the same pathname back
9158         local FOUND=$($LFS fid2path $MOUNT $FID)
9159         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9160         [ "$FOUND" != "$DIR/$tfile" ] &&
9161                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9162
9163         rm -rf $DIR/$tfile
9164 }
9165 run_test 154A "lfs path2fid and fid2path basic checks"
9166
9167 test_154a() {
9168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9169         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9170                 { skip "Need MDS version at least 2.2.51"; return 0; }
9171
9172         cp /etc/hosts $DIR/$tfile
9173
9174         fid=$($LFS path2fid $DIR/$tfile)
9175         rc=$?
9176         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9177
9178         dot_lustre_fid_permission_check "$fid" $DIR ||
9179                 error "dot lustre permission check $fid failed"
9180
9181         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9182
9183         touch $MOUNT/.lustre/file &&
9184                 error "creation is not allowed under .lustre"
9185
9186         mkdir $MOUNT/.lustre/dir &&
9187                 error "mkdir is not allowed under .lustre"
9188
9189         rm -rf $DIR/$tfile
9190 }
9191 run_test 154a "Open-by-FID"
9192
9193 test_154b() {
9194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9195         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9196                 { skip "Need MDS version at least 2.2.51"; return 0; }
9197
9198         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9199
9200         local remote_dir=$DIR/$tdir/remote_dir
9201         local MDTIDX=1
9202         local rc=0
9203
9204         mkdir -p $DIR/$tdir
9205         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9206                 error "create remote directory failed"
9207
9208         cp /etc/hosts $remote_dir/$tfile
9209
9210         fid=$($LFS path2fid $remote_dir/$tfile)
9211         rc=$?
9212         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9213
9214         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9215                 error "dot lustre permission check $fid failed"
9216         rm -rf $DIR/$tdir
9217 }
9218 run_test 154b "Open-by-FID for remote directory"
9219
9220 test_154c() {
9221         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9222                 skip "Need MDS version at least 2.4.1" && return
9223
9224         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9225         local FID1=$($LFS path2fid $DIR/$tfile.1)
9226         local FID2=$($LFS path2fid $DIR/$tfile.2)
9227         local FID3=$($LFS path2fid $DIR/$tfile.3)
9228
9229         local N=1
9230         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9231                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9232                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9233                 local want=FID$N
9234                 [ "$FID" = "${!want}" ] ||
9235                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9236                 N=$((N + 1))
9237         done
9238
9239         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9240                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9241                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9242                 N=$((N + 1))
9243         done
9244 }
9245 run_test 154c "lfs path2fid and fid2path multiple arguments"
9246
9247 test_154d() {
9248         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9249                 skip "Need MDS version at least 2.5.53" && return
9250
9251         if remote_mds; then
9252                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9253         else
9254                 nid="0@lo"
9255         fi
9256         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9257         local fd
9258         local cmd
9259
9260         rm -f $DIR/$tfile
9261         touch $DIR/$tfile
9262
9263         fid=$($LFS path2fid $DIR/$tfile)
9264         # Open the file
9265         fd=$(free_fd)
9266         cmd="exec $fd<$DIR/$tfile"
9267         eval $cmd
9268         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9269         echo $fid_list | grep $fid
9270         rc=$?
9271
9272         cmd="exec $fd>/dev/null"
9273         eval $cmd
9274         if [ $rc -ne 0 ]; then
9275                 error "FID $fid not found in open files list $fid_list"
9276         fi
9277 }
9278 run_test 154d "Verify open file fid"
9279
9280 test_155_small_load() {
9281     local temp=$TMP/$tfile
9282     local file=$DIR/$tfile
9283
9284     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9285         error "dd of=$temp bs=6096 count=1 failed"
9286     cp $temp $file
9287     cancel_lru_locks osc
9288     cmp $temp $file || error "$temp $file differ"
9289
9290     $TRUNCATE $temp 6000
9291     $TRUNCATE $file 6000
9292     cmp $temp $file || error "$temp $file differ (truncate1)"
9293
9294     echo "12345" >>$temp
9295     echo "12345" >>$file
9296     cmp $temp $file || error "$temp $file differ (append1)"
9297
9298     echo "12345" >>$temp
9299     echo "12345" >>$file
9300     cmp $temp $file || error "$temp $file differ (append2)"
9301
9302     rm -f $temp $file
9303     true
9304 }
9305
9306 test_155_big_load() {
9307     remote_ost_nodsh && skip "remote OST with nodsh" && return
9308     local temp=$TMP/$tfile
9309     local file=$DIR/$tfile
9310
9311     free_min_max
9312     local cache_size=$(do_facet ost$((MAXI+1)) \
9313         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9314     local large_file_size=$((cache_size * 2))
9315
9316     echo "OSS cache size: $cache_size KB"
9317     echo "Large file size: $large_file_size KB"
9318
9319     [ $MAXV -le $large_file_size ] && \
9320         skip_env "max available OST size needs > $large_file_size KB" && \
9321         return 0
9322
9323     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9324
9325     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9326         error "dd of=$temp bs=$large_file_size count=1k failed"
9327     cp $temp $file
9328     ls -lh $temp $file
9329     cancel_lru_locks osc
9330     cmp $temp $file || error "$temp $file differ"
9331
9332     rm -f $temp $file
9333     true
9334 }
9335
9336 test_155a() {
9337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9338         set_cache read on
9339         set_cache writethrough on
9340         test_155_small_load
9341 }
9342 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9343
9344 test_155b() {
9345         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9346         set_cache read on
9347         set_cache writethrough off
9348         test_155_small_load
9349 }
9350 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9351
9352 test_155c() {
9353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9354         set_cache read off
9355         set_cache writethrough on
9356         test_155_small_load
9357 }
9358 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9359
9360 test_155d() {
9361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9362         set_cache read off
9363         set_cache writethrough off
9364         test_155_small_load
9365 }
9366 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9367
9368 test_155e() {
9369         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9370         set_cache read on
9371         set_cache writethrough on
9372         test_155_big_load
9373 }
9374 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9375
9376 test_155f() {
9377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9378         set_cache read on
9379         set_cache writethrough off
9380         test_155_big_load
9381 }
9382 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9383
9384 test_155g() {
9385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9386         set_cache read off
9387         set_cache writethrough on
9388         test_155_big_load
9389 }
9390 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9391
9392 test_155h() {
9393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9394         set_cache read off
9395         set_cache writethrough off
9396         test_155_big_load
9397 }
9398 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9399
9400 test_156() {
9401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9402         local CPAGES=3
9403         local BEFORE
9404         local AFTER
9405         local file="$DIR/$tfile"
9406
9407         [ "$(facet_fstype ost1)" = "zfs" ] &&
9408                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9409                 return
9410
9411         roc_hit_init
9412
9413     log "Turn on read and write cache"
9414     set_cache read on
9415     set_cache writethrough on
9416
9417     log "Write data and read it back."
9418     log "Read should be satisfied from the cache."
9419     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9420     BEFORE=`roc_hit`
9421     cancel_lru_locks osc
9422     cat $file >/dev/null
9423     AFTER=`roc_hit`
9424     if ! let "AFTER - BEFORE == CPAGES"; then
9425         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9426     else
9427         log "cache hits:: before: $BEFORE, after: $AFTER"
9428     fi
9429
9430     log "Read again; it should be satisfied from the cache."
9431     BEFORE=$AFTER
9432     cancel_lru_locks osc
9433     cat $file >/dev/null
9434     AFTER=`roc_hit`
9435     if ! let "AFTER - BEFORE == CPAGES"; then
9436         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9437     else
9438         log "cache hits:: before: $BEFORE, after: $AFTER"
9439     fi
9440
9441
9442     log "Turn off the read cache and turn on the write cache"
9443     set_cache read off
9444     set_cache writethrough on
9445
9446     log "Read again; it should be satisfied from the cache."
9447     BEFORE=`roc_hit`
9448     cancel_lru_locks osc
9449     cat $file >/dev/null
9450     AFTER=`roc_hit`
9451     if ! let "AFTER - BEFORE == CPAGES"; then
9452         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9453     else
9454         log "cache hits:: before: $BEFORE, after: $AFTER"
9455     fi
9456
9457     log "Read again; it should not be satisfied from the cache."
9458     BEFORE=$AFTER
9459     cancel_lru_locks osc
9460     cat $file >/dev/null
9461     AFTER=`roc_hit`
9462     if ! let "AFTER - BEFORE == 0"; then
9463         error "IN CACHE: before: $BEFORE, after: $AFTER"
9464     else
9465         log "cache hits:: before: $BEFORE, after: $AFTER"
9466     fi
9467
9468     log "Write data and read it back."
9469     log "Read should be satisfied from the cache."
9470     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9471     BEFORE=`roc_hit`
9472     cancel_lru_locks osc
9473     cat $file >/dev/null
9474     AFTER=`roc_hit`
9475     if ! let "AFTER - BEFORE == CPAGES"; then
9476         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9477     else
9478         log "cache hits:: before: $BEFORE, after: $AFTER"
9479     fi
9480
9481     log "Read again; it should not be satisfied from the cache."
9482     BEFORE=$AFTER
9483     cancel_lru_locks osc
9484     cat $file >/dev/null
9485     AFTER=`roc_hit`
9486     if ! let "AFTER - BEFORE == 0"; then
9487         error "IN CACHE: before: $BEFORE, after: $AFTER"
9488     else
9489         log "cache hits:: before: $BEFORE, after: $AFTER"
9490     fi
9491
9492
9493     log "Turn off read and write cache"
9494     set_cache read off
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     cancel_lru_locks osc
9502     BEFORE=`roc_hit`
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 "Turn on the read cache and turn off the write cache"
9512     set_cache read on
9513     set_cache writethrough off
9514
9515     log "Write data and read it back"
9516     log "It should not be satisfied from the cache."
9517     rm -f $file
9518     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9519     BEFORE=`roc_hit`
9520     cancel_lru_locks osc
9521     cat $file >/dev/null
9522     AFTER=`roc_hit`
9523         if ! let "AFTER - BEFORE == 0"; then
9524                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9525         else
9526                 log "cache hits:: before: $BEFORE, after: $AFTER"
9527         fi
9528
9529     log "Read again; it should be satisfied from the cache."
9530     BEFORE=`roc_hit`
9531     cancel_lru_locks osc
9532     cat $file >/dev/null
9533     AFTER=`roc_hit`
9534     if ! let "AFTER - BEFORE == CPAGES"; then
9535         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9536     else
9537         log "cache hits:: before: $BEFORE, after: $AFTER"
9538     fi
9539
9540     rm -f $file
9541 }
9542 run_test 156 "Verification of tunables ============================"
9543
9544 #Changelogs
9545 err17935 () {
9546         if [ $MDSCOUNT -gt 1 ]; then
9547                 error_ignore bz17935 $*
9548         else
9549                 error $*
9550         fi
9551 }
9552
9553 changelog_chmask()
9554 {
9555         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9556
9557         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9558
9559         if [ $MASK -eq 1 ]; then
9560                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9561         else
9562                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9563         fi
9564 }
9565
9566 changelog_extract_field() {
9567         local mdt=$1
9568         local cltype=$2
9569         local file=$3
9570         local identifier=$4
9571
9572         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9573                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9574                 tail -1
9575 }
9576
9577 test_160a() {
9578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9579         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9580         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9581                 { skip "Need MDS version at least 2.2.0"; return; }
9582
9583         local CL_USERS="mdd.$MDT0.changelog_users"
9584         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9585         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9586         echo "Registered as changelog user $USER"
9587         $GET_CL_USERS | grep -q $USER ||
9588                 error "User $USER not found in changelog_users"
9589
9590         # change something
9591         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9592         touch $DIR/$tdir/pics/2008/zachy/timestamp
9593         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9594         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9595         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9596         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9597         rm $DIR/$tdir/pics/desktop.jpg
9598
9599         $LFS changelog $MDT0 | tail -5
9600
9601         echo "verifying changelog mask"
9602         changelog_chmask "MKDIR"
9603         changelog_chmask "CLOSE"
9604
9605         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9606         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9607
9608         changelog_chmask "MKDIR"
9609         changelog_chmask "CLOSE"
9610
9611         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9612         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9613
9614         $LFS changelog $MDT0
9615         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9616         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9617         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9618         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9619
9620         # verify contents
9621         echo "verifying target fid"
9622         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9623         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9624         [ "$fidc" == "$fidf" ] ||
9625                 err17935 "fid in changelog $fidc != file fid $fidf"
9626         echo "verifying parent fid"
9627         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9628         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9629         [ "$fidc" == "$fidf" ] ||
9630                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9631
9632         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9633         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9634         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9635         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9636         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9637                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9638
9639         MIN_REC=$($GET_CL_USERS |
9640                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9641         FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
9642         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9643         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9644                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9645
9646         # LU-3446 changelog index reset on MDT restart
9647         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9648         CUR_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9649         $LFS changelog_clear $MDT0 $USER 0
9650         stop $SINGLEMDS || error "Fail to stop MDT."
9651         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9652         CUR_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9653         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9654         [ $CUR_REC1 == $CUR_REC2 ] ||
9655                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9656
9657         echo "verifying user deregister"
9658         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9659         $GET_CL_USERS | grep -q $USER &&
9660                 error "User $USER still in changelog_users"
9661
9662         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9663         if [ $USERS -eq 0 ]; then
9664                 LAST_REC1=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9665                 touch $DIR/$tdir/chloe
9666                 LAST_REC2=$($GET_CL_USERS | head -1 | cut -f3 -d' ')
9667                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9668                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9669         else
9670                 echo "$USERS other changelog users; can't verify off"
9671         fi
9672 }
9673 run_test 160a "changelog sanity"
9674
9675 test_160b() { # LU-3587
9676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9677         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9678         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9679                 { skip "Need MDS version at least 2.2.0"; return; }
9680
9681         local CL_USERS="mdd.$MDT0.changelog_users"
9682         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9683         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9684         echo "Registered as changelog user $USER"
9685         $GET_CL_USERS | grep -q $USER ||
9686                 error "User $USER not found in changelog_users"
9687
9688         local LONGNAME1=$(str_repeat a 255)
9689         local LONGNAME2=$(str_repeat b 255)
9690
9691         cd $DIR
9692         echo "creating very long named file"
9693         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
9694         echo "moving very long named file"
9695         mv $LONGNAME1 $LONGNAME2
9696
9697         $LFS changelog $MDT0 | grep RENME
9698
9699         echo "deregistering $USER"
9700         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9701
9702         rm -f $LONGNAME2
9703 }
9704 run_test 160b "Verify that very long rename doesn't crash in changelog"
9705
9706 test_161a() {
9707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9708         test_mkdir -p -c1 $DIR/$tdir
9709         cp /etc/hosts $DIR/$tdir/$tfile
9710         test_mkdir -c1 $DIR/$tdir/foo1
9711         test_mkdir -c1 $DIR/$tdir/foo2
9712         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9713         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9714         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9715         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9716         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9717         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9718                 $LFS fid2path $DIR $FID
9719                 err17935 "bad link ea"
9720         fi
9721     # middle
9722     rm $DIR/$tdir/foo2/zachary
9723     # last
9724     rm $DIR/$tdir/foo2/thor
9725     # first
9726     rm $DIR/$tdir/$tfile
9727     # rename
9728     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9729     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9730         then
9731         $LFS fid2path $DIR $FID
9732         err17935 "bad link rename"
9733     fi
9734     rm $DIR/$tdir/foo2/maggie
9735
9736     # overflow the EA
9737     local longname=filename_avg_len_is_thirty_two_
9738     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
9739         error "failed to hardlink many files"
9740     links=$($LFS fid2path $DIR $FID | wc -l)
9741     echo -n "${links}/1000 links in link EA"
9742     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9743     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
9744         error "failed to unlink many hardlinks"
9745 }
9746 run_test 161a "link ea sanity"
9747
9748 test_161b() {
9749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9750         [ $MDSCOUNT -lt 2 ] &&
9751                 skip "skipping remote directory test" && return
9752         local MDTIDX=1
9753         local remote_dir=$DIR/$tdir/remote_dir
9754
9755         mkdir -p $DIR/$tdir
9756         $LFS mkdir -i $MDTIDX $remote_dir ||
9757                 error "create remote directory failed"
9758
9759         cp /etc/hosts $remote_dir/$tfile
9760         mkdir -p $remote_dir/foo1
9761         mkdir -p $remote_dir/foo2
9762         ln $remote_dir/$tfile $remote_dir/foo1/sofia
9763         ln $remote_dir/$tfile $remote_dir/foo2/zachary
9764         ln $remote_dir/$tfile $remote_dir/foo1/luna
9765         ln $remote_dir/$tfile $remote_dir/foo2/thor
9766
9767         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
9768                      tr -d ']')
9769         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9770                 $LFS fid2path $DIR $FID
9771                 err17935 "bad link ea"
9772         fi
9773         # middle
9774         rm $remote_dir/foo2/zachary
9775         # last
9776         rm $remote_dir/foo2/thor
9777         # first
9778         rm $remote_dir/$tfile
9779         # rename
9780         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
9781         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
9782         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
9783                 $LFS fid2path $DIR $FID
9784                 err17935 "bad link rename"
9785         fi
9786         rm $remote_dir/foo2/maggie
9787
9788         # overflow the EA
9789         local longname=filename_avg_len_is_thirty_two_
9790         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
9791                 error "failed to hardlink many files"
9792         links=$($LFS fid2path $DIR $FID | wc -l)
9793         echo -n "${links}/1000 links in link EA"
9794         [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
9795         unlinkmany $remote_dir/foo2/$longname 1000 ||
9796         error "failed to unlink many hardlinks"
9797 }
9798 run_test 161b "link ea sanity under remote directory"
9799
9800 test_161c() {
9801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9802         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
9803                 skip "Need MDS version at least 2.1.5" && return
9804
9805         # define CLF_RENAME_LAST 0x0001
9806         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
9807         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9808                 changelog_register -n)
9809         rm -rf $DIR/$tdir
9810         mkdir -p $DIR/$tdir
9811         touch $DIR/$tdir/foo_161c
9812         touch $DIR/$tdir/bar_161c
9813         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9814         $LFS changelog $MDT0 | grep RENME
9815         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
9816                 cut -f5 -d' ')
9817         $LFS changelog_clear $MDT0 $USER 0
9818         if [ x$flags != "x0x1" ]; then
9819                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9820                         $USER
9821                 error "flag $flags is not 0x1"
9822         fi
9823         echo "rename overwrite a target having nlink = 1," \
9824                 "changelog record has flags of $flags"
9825
9826         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
9827         touch $DIR/$tdir/foo_161c
9828         touch $DIR/$tdir/bar_161c
9829         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9830         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
9831         $LFS changelog $MDT0 | grep RENME
9832         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9833         $LFS changelog_clear $MDT0 $USER 0
9834         if [ x$flags != "x0x0" ]; then
9835                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9836                         $USER
9837                 error "flag $flags is not 0x0"
9838         fi
9839         echo "rename overwrite a target having nlink > 1," \
9840                 "changelog record has flags of $flags"
9841
9842         # rename doesn't overwrite a target (changelog flag 0x0)
9843         touch $DIR/$tdir/foo_161c
9844         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
9845         $LFS changelog $MDT0 | grep RENME
9846         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
9847         $LFS changelog_clear $MDT0 $USER 0
9848         if [ x$flags != "x0x0" ]; then
9849                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9850                         $USER
9851                 error "flag $flags is not 0x0"
9852         fi
9853         echo "rename doesn't overwrite a target," \
9854                 "changelog record has flags of $flags"
9855
9856         # define CLF_UNLINK_LAST 0x0001
9857         # unlink a file having nlink = 1 (changelog flag 0x1)
9858         rm -f $DIR/$tdir/foo2_161c
9859         $LFS changelog $MDT0 | grep UNLNK
9860         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9861         $LFS changelog_clear $MDT0 $USER 0
9862         if [ x$flags != "x0x1" ]; then
9863                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9864                         $USER
9865                 error "flag $flags is not 0x1"
9866         fi
9867         echo "unlink a file having nlink = 1," \
9868                 "changelog record has flags of $flags"
9869
9870         # unlink a file having nlink > 1 (changelog flag 0x0)
9871         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
9872         rm -f $DIR/$tdir/foobar_161c
9873         $LFS changelog $MDT0 | grep UNLNK
9874         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
9875         $LFS changelog_clear $MDT0 $USER 0
9876         if [ x$flags != "x0x0" ]; then
9877                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
9878                         $USER
9879                 error "flag $flags is not 0x0"
9880         fi
9881         echo "unlink a file having nlink > 1," \
9882                 "changelog record has flags of $flags"
9883         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9884 }
9885 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
9886
9887 check_path() {
9888     local expected=$1
9889     shift
9890     local fid=$2
9891
9892     local path=$(${LFS} fid2path $*)
9893     RC=$?
9894
9895     if [ $RC -ne 0 ]; then
9896         err17935 "path looked up of $expected failed. Error $RC"
9897         return $RC
9898     elif [ "${path}" != "${expected}" ]; then
9899         err17935 "path looked up \"${path}\" instead of \"${expected}\""
9900         return 2
9901     fi
9902     echo "fid $fid resolves to path $path (expected $expected)"
9903 }
9904
9905 test_162() {
9906         # Make changes to filesystem
9907         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9908         test_mkdir -p $DIR/$tdir/d2
9909         touch $DIR/$tdir/d2/$tfile
9910         touch $DIR/$tdir/d2/x1
9911         touch $DIR/$tdir/d2/x2
9912         test_mkdir -p $DIR/$tdir/d2/a/b/c
9913         test_mkdir -p $DIR/$tdir/d2/p/q/r
9914         # regular file
9915         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
9916         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
9917
9918         # softlink
9919         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
9920         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
9921         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
9922
9923         # softlink to wrong file
9924         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
9925         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
9926         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
9927
9928         # hardlink
9929         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
9930         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
9931         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
9932         # fid2path dir/fsname should both work
9933         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
9934         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9935
9936         # hardlink count: check that there are 2 links
9937         # Doesnt work with CMD yet: 17935
9938         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9939                 err17935 "expected 2 links"
9940
9941         # hardlink indexing: remove the first link
9942         rm $DIR/$tdir/d2/p/q/r/hlink
9943         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9944
9945         return 0
9946 }
9947 run_test 162 "path lookup sanity"
9948
9949 test_169() {
9950         # do directio so as not to populate the page cache
9951         log "creating a 10 Mb file"
9952         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9953         log "starting reads"
9954         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9955         log "truncating the file"
9956         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9957         log "killing dd"
9958         kill %+ || true # reads might have finished
9959         echo "wait until dd is finished"
9960         wait
9961         log "removing the temporary file"
9962         rm -rf $DIR/$tfile || error "tmp file removal failed"
9963 }
9964 run_test 169 "parallel read and truncate should not deadlock"
9965
9966 test_170() {
9967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9968         $LCTL clear     # bug 18514
9969         $LCTL debug_daemon start $TMP/${tfile}_log_good
9970         touch $DIR/$tfile
9971         $LCTL debug_daemon stop
9972         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9973                error "sed failed to read log_good"
9974
9975         $LCTL debug_daemon start $TMP/${tfile}_log_good
9976         rm -rf $DIR/$tfile
9977         $LCTL debug_daemon stop
9978
9979         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9980                error "lctl df log_bad failed"
9981
9982         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9983         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9984
9985         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9986         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9987
9988         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9989                 error "bad_line good_line1 good_line2 are empty"
9990
9991         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9992         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9993         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9994
9995         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9996         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9997         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9998
9999         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10000                 error "bad_line_new good_line_new are empty"
10001
10002         local expected_good=$((good_line1 + good_line2*2))
10003
10004         rm -f $TMP/${tfile}*
10005         # LU-231, short malformed line may not be counted into bad lines
10006         if [ $bad_line -ne $bad_line_new ] &&
10007                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10008                 error "expected $bad_line bad lines, but got $bad_line_new"
10009                 return 1
10010         fi
10011
10012         if [ $expected_good -ne $good_line_new ]; then
10013                 error "expected $expected_good good lines, but got $good_line_new"
10014                 return 2
10015         fi
10016         true
10017 }
10018 run_test 170 "test lctl df to handle corrupted log ====================="
10019
10020 test_171() { # bug20592
10021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10022 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10023         $LCTL set_param fail_loc=0x50e
10024         $LCTL set_param fail_val=3000
10025         multiop_bg_pause $DIR/$tfile O_s || true
10026         local MULTIPID=$!
10027         kill -USR1 $MULTIPID
10028         # cause log dump
10029         sleep 3
10030         wait $MULTIPID
10031         if dmesg | grep "recursive fault"; then
10032                 error "caught a recursive fault"
10033         fi
10034         $LCTL set_param fail_loc=0
10035         true
10036 }
10037 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10038
10039 # it would be good to share it with obdfilter-survey/libecho code
10040 setup_obdecho_osc () {
10041         local rc=0
10042         local ost_nid=$1
10043         local obdfilter_name=$2
10044         echo "Creating new osc for $obdfilter_name on $ost_nid"
10045         # make sure we can find loopback nid
10046         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10047
10048         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10049                            ${obdfilter_name}_osc_UUID || rc=2; }
10050         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10051                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10052         return $rc
10053 }
10054
10055 cleanup_obdecho_osc () {
10056         local obdfilter_name=$1
10057         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10058         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10059         return 0
10060 }
10061
10062 obdecho_test() {
10063         local OBD=$1
10064         local node=$2
10065         local pages=${3:-64}
10066         local rc=0
10067         local id
10068
10069         local count=10
10070         local obd_size=$(get_obd_size $node $OBD)
10071         local page_size=$(get_page_size $node)
10072         if [[ -n "$obd_size" ]]; then
10073                 local new_count=$((obd_size / (pages * page_size / 1024)))
10074                 [[ $new_count -ge $count ]] || count=$new_count
10075         fi
10076
10077         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10078         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10079                            rc=2; }
10080         if [ $rc -eq 0 ]; then
10081             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10082             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10083         fi
10084         echo "New object id is $id"
10085         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10086                            rc=4; }
10087         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10088                            "test_brw $count w v $pages $id" || rc=4; }
10089         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10090                            rc=4; }
10091         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10092                                         "cleanup" || rc=5; }
10093         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10094                                         "detach" || rc=6; }
10095         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10096         return $rc
10097 }
10098
10099 test_180a() {
10100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10101         remote_ost_nodsh && skip "remote OST with nodsh" && return
10102         local rc=0
10103         local rmmod_local=0
10104
10105         if ! module_loaded obdecho; then
10106             load_module obdecho/obdecho
10107             rmmod_local=1
10108         fi
10109
10110         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10111         local host=$(lctl get_param -n osc.$osc.import |
10112                              awk '/current_connection:/ {print $2}' )
10113         local target=$(lctl get_param -n osc.$osc.import |
10114                              awk '/target:/ {print $2}' )
10115         target=${target%_UUID}
10116
10117         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10118         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10119         [[ -n $target ]] && cleanup_obdecho_osc $target
10120         [ $rmmod_local -eq 1 ] && rmmod obdecho
10121         return $rc
10122 }
10123 run_test 180a "test obdecho on osc"
10124
10125 test_180b() {
10126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10127         remote_ost_nodsh && skip "remote OST with nodsh" && return
10128         local rc=0
10129         local rmmod_remote=0
10130
10131         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10132                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10133                       "modprobe obdecho; }" && rmmod_remote=1
10134         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10135         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10136         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10137         return $rc
10138 }
10139 run_test 180b "test obdecho directly on obdfilter"
10140
10141 test_180c() { # LU-2598
10142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10143         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10144                 skip "Need MDS version at least 2.4.0" && return
10145
10146         local rc=0
10147         local rmmod_remote=false
10148         local pages=16384 # 64MB bulk I/O RPC size
10149         local target
10150
10151         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10152                 rmmod_remote=true || error "failed to load module obdecho"
10153
10154         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4}'|head -1)
10155         if [[ -n $target ]]; then
10156                 obdecho_test "$target" ost1 "$pages" ||
10157                         rc=${PIPESTATUS[0]}
10158         else
10159                 echo "there is no obdfilter target on ost1"
10160                 rc=2
10161         fi
10162         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10163         return $rc
10164 }
10165 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10166
10167 test_181() { # bug 22177
10168         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10169         # create enough files to index the directory
10170         createmany -o $DIR/$tdir/foobar 4000
10171         # print attributes for debug purpose
10172         lsattr -d .
10173         # open dir
10174         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10175         MULTIPID=$!
10176         # remove the files & current working dir
10177         unlinkmany $DIR/$tdir/foobar 4000
10178         rmdir $DIR/$tdir
10179         kill -USR1 $MULTIPID
10180         wait $MULTIPID
10181         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10182         return 0
10183 }
10184 run_test 181 "Test open-unlinked dir ========================"
10185
10186 test_182() {
10187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10188         # disable MDC RPC lock wouldn't crash client
10189         local fcount=1000
10190         local tcount=4
10191
10192         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10193 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10194         $LCTL set_param fail_loc=0x804
10195
10196         for (( i=0; i < $tcount; i++ )) ; do
10197                 mkdir $DIR/$tdir/$i
10198                 createmany -o $DIR/$tdir/$i/f- $fcount &
10199         done
10200         wait
10201
10202         for (( i=0; i < $tcount; i++ )) ; do
10203                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10204         done
10205         wait
10206
10207         rm -rf $DIR/$tdir
10208
10209         $LCTL set_param fail_loc=0
10210 }
10211 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10212
10213 test_183() { # LU-2275
10214         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10215                 skip "Need MDS version at least 2.3.56" && return
10216
10217         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10218         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10219         echo aaa > $DIR/$tdir/$tfile
10220
10221 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10222         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10223
10224         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10225         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10226
10227         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10228
10229         # Flush negative dentry cache
10230         touch $DIR/$tdir/$tfile
10231
10232         # We are not checking for any leaked references here, they'll
10233         # become evident next time we do cleanup with module unload.
10234         rm -rf $DIR/$tdir
10235 }
10236 run_test 183 "No crash or request leak in case of strange dispositions ========"
10237
10238 # test suite 184 is for LU-2016, LU-2017
10239 test_184a() {
10240         check_swap_layouts_support && return 0
10241
10242         dir0=$DIR/$tdir/$testnum
10243         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10244         ref1=/etc/passwd
10245         ref2=/etc/group
10246         file1=$dir0/f1
10247         file2=$dir0/f2
10248         $SETSTRIPE -c1 $file1
10249         cp $ref1 $file1
10250         $SETSTRIPE -c2 $file2
10251         cp $ref2 $file2
10252         gen1=$($GETSTRIPE -g $file1)
10253         gen2=$($GETSTRIPE -g $file2)
10254
10255         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10256         gen=$($GETSTRIPE -g $file1)
10257         [[ $gen1 != $gen ]] ||
10258                 "Layout generation on $file1 does not change"
10259         gen=$($GETSTRIPE -g $file2)
10260         [[ $gen2 != $gen ]] ||
10261                 "Layout generation on $file2 does not change"
10262
10263         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10264         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10265 }
10266 run_test 184a "Basic layout swap"
10267
10268 test_184b() {
10269         check_swap_layouts_support && return 0
10270
10271         dir0=$DIR/$tdir/$testnum
10272         mkdir -p $dir0 || error "creating dir $dir0"
10273         file1=$dir0/f1
10274         file2=$dir0/f2
10275         file3=$dir0/f3
10276         dir1=$dir0/d1
10277         dir2=$dir0/d2
10278         mkdir $dir1 $dir2
10279         $SETSTRIPE -c1 $file1
10280         $SETSTRIPE -c2 $file2
10281         $SETSTRIPE -c1 $file3
10282         chown $RUNAS_ID $file3
10283         gen1=$($GETSTRIPE -g $file1)
10284         gen2=$($GETSTRIPE -g $file2)
10285
10286         $LFS swap_layouts $dir1 $dir2 &&
10287                 error "swap of directories layouts should fail"
10288         $LFS swap_layouts $dir1 $file1 &&
10289                 error "swap of directory and file layouts should fail"
10290         $RUNAS $LFS swap_layouts $file1 $file2 &&
10291                 error "swap of file we cannot write should fail"
10292         $LFS swap_layouts $file1 $file3 &&
10293                 error "swap of file with different owner should fail"
10294         /bin/true # to clear error code
10295 }
10296 run_test 184b "Forbidden layout swap (will generate errors)"
10297
10298 test_184c() {
10299         check_swap_layouts_support && return 0
10300
10301         local dir0=$DIR/$tdir/$testnum
10302         mkdir -p $dir0 || error "creating dir $dir0"
10303
10304         local ref1=$dir0/ref1
10305         local ref2=$dir0/ref2
10306         local file1=$dir0/file1
10307         local file2=$dir0/file2
10308         # create a file large enough for the concurent test
10309         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10310         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10311         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
10312
10313         cp $ref2 $file2
10314         dd if=$ref1 of=$file1 bs=16k &
10315         local DD_PID=$!
10316
10317         # Make sure dd starts to copy file
10318         while [ ! -f $file1 ]; do sleep 0.1; done
10319
10320         $LFS swap_layouts $file1 $file2
10321         local rc=$?
10322         wait $DD_PID
10323         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10324         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10325
10326         # how many bytes copied before swapping layout
10327         local copied=`stat -c %s $file2`
10328         local remaining=`stat -c %s $ref1`
10329         remaining=$((remaining - copied))
10330         echo "Copied $copied bytes before swapping layout..."
10331
10332         cmp -n $copied $file1 $ref2 | grep differ &&
10333                 error "Content mismatch [0, $copied) of ref2 and file1"
10334         cmp -n $copied $file2 $ref1 ||
10335                 error "Content mismatch [0, $copied) of ref1 and file2"
10336         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10337                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10338
10339         # clean up
10340         rm -f $ref1 $ref2 $file1 $file2
10341 }
10342 run_test 184c "Concurrent write and layout swap"
10343
10344 test_184d() {
10345         check_swap_layouts_support && return 0
10346
10347         local file1=$DIR/$tdir/$tfile-1
10348         local file2=$DIR/$tdir/$tfile-2
10349         local file3=$DIR/$tdir/$tfile-3
10350         local lovea1
10351         local lovea2
10352
10353         mkdir -p $DIR/$tdir
10354         touch $file1 || error "create $file1 failed"
10355         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10356                 error "create $file2 failed"
10357         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10358                 error "create $file3 failed"
10359         lovea1=$($LFS getstripe $file1 | sed 1d)
10360
10361         $LFS swap_layouts $file2 $file3 ||
10362                 error "swap $file2 $file3 layouts failed"
10363         $LFS swap_layouts $file1 $file2 ||
10364                 error "swap $file1 $file2 layouts failed"
10365
10366         lovea2=$($LFS getstripe $file2 | sed 1d)
10367         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10368
10369         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10370         [ -z $lovea1 ] || error "$file1 shouldn't have lovea"
10371 }
10372 run_test 184d "allow stripeless layouts swap"
10373
10374
10375 test_185() { # LU-2441
10376         # LU-3553 - no volatile file support in old servers
10377         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10378                 { skip "Need MDS version at least 2.3.60"; return 0; }
10379
10380         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10381         touch $DIR/$tdir/spoo
10382         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10383         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10384                 error "cannot create/write a volatile file"
10385         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10386                 error "FID is still valid after close"
10387
10388         multiop_bg_pause $DIR/$tdir vVw4096_c
10389         local multi_pid=$!
10390
10391         local OLD_IFS=$IFS
10392         IFS=":"
10393         local fidv=($fid)
10394         IFS=$OLD_IFS
10395         # assume that the next FID for this client is sequential, since stdout
10396         # is unfortunately eaten by multiop_bg_pause
10397         local n=$((${fidv[1]} + 1))
10398         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10399         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10400                 error "FID is missing before close"
10401         kill -USR1 $multi_pid
10402         # 1 second delay, so if mtime change we will see it
10403         sleep 1
10404         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10405         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10406 }
10407 run_test 185 "Volatile file support"
10408
10409 test_187a() {
10410         local dir0=$DIR/$tdir/$testnum
10411         mkdir -p $dir0 || error "creating dir $dir0"
10412
10413         local file=$dir0/file1
10414         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10415         local dv1=$($LFS data_version $file)
10416         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10417         local dv2=$($LFS data_version $file)
10418         [[ $dv1 != $dv2 ]] ||
10419                 error "data version did not change on write $dv1 == $dv2"
10420
10421         # clean up
10422         rm -f $file1
10423 }
10424 run_test 187a "Test data version change"
10425
10426 test_187b() {
10427         local dir0=$DIR/$tdir/$testnum
10428         mkdir -p $dir0 || error "creating dir $dir0"
10429
10430         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10431         [[ ${DV[0]} != ${DV[1]} ]] ||
10432                 error "data version did not change on write"\
10433                       " ${DV[0]} == ${DV[1]}"
10434
10435         # clean up
10436         rm -f $file1
10437 }
10438 run_test 187b "Test data version change on volatile file"
10439
10440 # OST pools tests
10441 check_file_in_pool()
10442 {
10443         local file=$1
10444         local pool=$2
10445         local tlist="$3"
10446         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10447         for i in $res
10448         do
10449                 for t in $tlist ; do
10450                         [ "$i" -eq "$t" ] && continue 2
10451                 done
10452
10453                 echo "pool list: $tlist"
10454                 echo "striping: $res"
10455                 error_noexit "$file not allocated in $pool"
10456                 return 1
10457         done
10458         return 0
10459 }
10460
10461 pool_add() {
10462         echo "Creating new pool"
10463         local pool=$1
10464
10465         create_pool $FSNAME.$pool ||
10466                 { error_noexit "No pool created, result code $?"; return 1; }
10467         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10468                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10469 }
10470
10471 pool_add_targets() {
10472         echo "Adding targets to pool"
10473         local pool=$1
10474         local first=$2
10475         local last=$3
10476         local step=${4:-1}
10477
10478         local list=$(seq $first $step $last)
10479
10480         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10481         do_facet mgs $LCTL pool_add \
10482                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10483         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10484                         | sort -u | tr '\n' ' ' " "$t" || { 
10485                 error_noexit "Add to pool failed"
10486                 return 1
10487         }
10488         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10489         local addcount=$(((last - first) / step + 1))
10490         [ $lfscount -eq $addcount ] || {
10491                 error_noexit "lfs pool_list bad ost count" \
10492                                                 "$lfscount != $addcount"
10493                 return 2
10494         }
10495 }
10496
10497 pool_set_dir() {
10498         local pool=$1
10499         local tdir=$2
10500         echo "Setting pool on directory $tdir"
10501
10502         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10503
10504         error_noexit "Cannot set pool $pool to $tdir"
10505         return 1
10506 }
10507
10508 pool_check_dir() {
10509         local pool=$1
10510         local tdir=$2
10511         echo "Checking pool on directory $tdir"
10512
10513         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10514         [ "$res" = "$pool" ] && return 0
10515
10516         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10517         return 1
10518 }
10519
10520 pool_dir_rel_path() {
10521         echo "Testing relative path works well"
10522         local pool=$1
10523         local tdir=$2
10524         local root=$3
10525
10526         mkdir -p $root/$tdir/$tdir
10527         cd $root/$tdir
10528         pool_set_dir $pool $tdir          || return 1
10529         pool_set_dir $pool ./$tdir        || return 2
10530         pool_set_dir $pool ../$tdir       || return 3
10531         pool_set_dir $pool ../$tdir/$tdir || return 4
10532         rm -rf $tdir; cd - > /dev/null
10533 }
10534
10535 pool_alloc_files() {
10536         echo "Checking files allocation from directory pool"
10537         local pool=$1
10538         local tdir=$2
10539         local count=$3
10540         local tlist="$4"
10541
10542         local failed=0
10543         for i in $(seq -w 1 $count)
10544         do
10545                 local file=$tdir/file-$i
10546                 touch $file
10547                 check_file_in_pool $file $pool "$tlist" || \
10548                         failed=$((failed + 1))
10549         done
10550         [ "$failed" = 0 ] && return 0
10551
10552         error_noexit "$failed files not allocated in $pool"
10553         return 1
10554 }
10555
10556 pool_create_files() {
10557         echo "Creating files in pool"
10558         local pool=$1
10559         local tdir=$2
10560         local count=$3
10561         local tlist="$4"
10562
10563         mkdir -p $tdir
10564         local failed=0
10565         for i in $(seq -w 1 $count)
10566         do
10567                 local file=$tdir/spoo-$i
10568                 $SETSTRIPE -p $pool $file
10569                 check_file_in_pool $file $pool "$tlist" || \
10570                         failed=$((failed + 1))
10571         done
10572         [ "$failed" = 0 ] && return 0
10573
10574         error_noexit "$failed files not allocated in $pool"
10575         return 1
10576 }
10577
10578 pool_lfs_df() {
10579         echo "Checking 'lfs df' output"
10580         local pool=$1
10581
10582         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10583                         tr '\n' ' ')
10584         local res=$($LFS df --pool $FSNAME.$pool |
10585                         awk '{print $1}' |
10586                         grep "$FSNAME-OST" |
10587                         tr '\n' ' ')
10588         [ "$res" = "$t" ] && return 0
10589
10590         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10591         return 1
10592 }
10593
10594 pool_file_rel_path() {
10595         echo "Creating files in a pool with relative pathname"
10596         local pool=$1
10597         local tdir=$2
10598
10599         mkdir -p $tdir ||
10600                 { error_noexit "unable to create $tdir"; return 1 ; }
10601         local file="/..$tdir/$tfile-1"
10602         $SETSTRIPE -p $pool $file ||
10603                 { error_noexit "unable to create $file" ; return 2 ; }
10604
10605         cd $tdir
10606         $SETSTRIPE -p $pool $tfile-2 || {
10607                 error_noexit "unable to create $tfile-2 in $tdir"
10608                 return 3
10609         }
10610 }
10611
10612 pool_remove_first_target() {
10613         echo "Removing first target from a pool"
10614         local pool=$1
10615
10616         local pname="lov.$FSNAME-*.pools.$pool"
10617         local t=$($LCTL get_param -n $pname | head -1)
10618         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10619         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10620                 error_noexit "$t not removed from $FSNAME.$pool"
10621                 return 1
10622         }
10623 }
10624
10625 pool_remove_all_targets() {
10626         echo "Removing all targets from pool"
10627         local pool=$1
10628         local file=$2
10629         local pname="lov.$FSNAME-*.pools.$pool"
10630         for t in $($LCTL get_param -n $pname | sort -u)
10631         do
10632                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10633         done
10634         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
10635                 error_noexit "Pool $FSNAME.$pool cannot be drained"
10636                 return 1
10637         }
10638         # striping on an empty/nonexistant pool should fall back 
10639         # to "pool of everything"
10640         touch $file || {
10641                 error_noexit "failed to use fallback striping for empty pool"
10642                 return 2
10643         }
10644         # setstripe on an empty pool should fail
10645         $SETSTRIPE -p $pool $file 2>/dev/null && {
10646                 error_noexit "expected failure when creating file" \
10647                                                         "with empty pool"
10648                 return 3
10649         }
10650         return 0
10651 }
10652
10653 pool_remove() {
10654         echo "Destroying pool"
10655         local pool=$1
10656         local file=$2
10657
10658         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
10659
10660         sleep 2
10661         # striping on an empty/nonexistant pool should fall back 
10662         # to "pool of everything"
10663         touch $file || {
10664                 error_noexit "failed to use fallback striping for missing pool"
10665                 return 1
10666         }
10667         # setstripe on an empty pool should fail
10668         $SETSTRIPE -p $pool $file 2>/dev/null && {
10669                 error_noexit "expected failure when creating file" \
10670                                                         "with missing pool"
10671                 return 2
10672         }
10673
10674         # get param should return err once pool is gone
10675         if wait_update $HOSTNAME "lctl get_param -n \
10676                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
10677         then
10678                 remove_pool_from_list $FSNAME.$pool
10679                 return 0
10680         fi
10681         error_noexit "Pool $FSNAME.$pool is not destroyed"
10682         return 3
10683 }
10684
10685 test_200() {
10686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10687         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
10688
10689         local POOL=${POOL:-cea1}
10690         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
10691         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
10692         # Pool OST targets
10693         local first_ost=0
10694         local last_ost=$(($OSTCOUNT - 1))
10695         local ost_step=2
10696         local ost_list=$(seq $first_ost $ost_step $last_ost)
10697         local ost_range="$first_ost $last_ost $ost_step"
10698         local test_path=$POOL_ROOT/$POOL_DIR_NAME
10699         local file_dir=$POOL_ROOT/file_tst
10700
10701         local rc=0
10702         while : ; do
10703                 # former test_200a test_200b
10704                 pool_add $POOL                          || { rc=$? ; break; }
10705                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
10706                 # former test_200c test_200d
10707                 mkdir -p $test_path
10708                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
10709                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
10710                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
10711                                                         || { rc=$? ; break; }
10712                 # former test_200e test_200f
10713                 local files=$((OSTCOUNT*3))
10714                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
10715                                                         || { rc=$? ; break; }
10716                 pool_create_files $POOL $file_dir $files "$ost_list" \
10717                                                         || { rc=$? ; break; }
10718                 # former test_200g test_200h
10719                 pool_lfs_df $POOL                       || { rc=$? ; break; }
10720                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
10721
10722                 # former test_201a test_201b test_201c
10723                 pool_remove_first_target $POOL          || { rc=$? ; break; }
10724
10725                 local f=$test_path/$tfile
10726                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
10727                 pool_remove $POOL $f                    || { rc=$? ; break; }
10728                 break
10729         done
10730
10731         cleanup_pools
10732         return $rc
10733 }
10734 run_test 200 "OST pools"
10735
10736 # usage: default_attr <count | size | offset>
10737 default_attr() {
10738         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
10739 }
10740
10741 # usage: check_default_stripe_attr
10742 check_default_stripe_attr() {
10743         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
10744         case $1 in
10745         --stripe-count|--count)
10746                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
10747         --stripe-size|--size)
10748                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
10749         --stripe-index|--index)
10750                 EXPECTED=-1;;
10751         *)
10752                 error "unknown getstripe attr '$1'"
10753         esac
10754
10755         [ $ACTUAL != $EXPECTED ] &&
10756                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
10757 }
10758
10759 test_204a() {
10760         test_mkdir -p $DIR/$tdir
10761         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
10762
10763         check_default_stripe_attr --stripe-count
10764         check_default_stripe_attr --stripe-size
10765         check_default_stripe_attr --stripe-index
10766
10767         return 0
10768 }
10769 run_test 204a "Print default stripe attributes ================="
10770
10771 test_204b() {
10772         test_mkdir -p $DIR/$tdir
10773         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10774
10775         check_default_stripe_attr --stripe-size
10776         check_default_stripe_attr --stripe-index
10777
10778         return 0
10779 }
10780 run_test 204b "Print default stripe size and offset  ==========="
10781
10782 test_204c() {
10783         test_mkdir -p $DIR/$tdir
10784         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10785
10786         check_default_stripe_attr --stripe-count
10787         check_default_stripe_attr --stripe-index
10788
10789         return 0
10790 }
10791 run_test 204c "Print default stripe count and offset ==========="
10792
10793 test_204d() {
10794         test_mkdir -p $DIR/$tdir
10795         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10796
10797         check_default_stripe_attr --stripe-count
10798         check_default_stripe_attr --stripe-size
10799
10800         return 0
10801 }
10802 run_test 204d "Print default stripe count and size ============="
10803
10804 test_204e() {
10805         test_mkdir -p $DIR/$tdir
10806         $SETSTRIPE -d $DIR/$tdir
10807
10808         check_default_stripe_attr --stripe-count --raw
10809         check_default_stripe_attr --stripe-size --raw
10810         check_default_stripe_attr --stripe-index --raw
10811
10812         return 0
10813 }
10814 run_test 204e "Print raw stripe attributes ================="
10815
10816 test_204f() {
10817         test_mkdir -p $DIR/$tdir
10818         $SETSTRIPE --stripe-count 1 $DIR/$tdir
10819
10820         check_default_stripe_attr --stripe-size --raw
10821         check_default_stripe_attr --stripe-index --raw
10822
10823         return 0
10824 }
10825 run_test 204f "Print raw stripe size and offset  ==========="
10826
10827 test_204g() {
10828         test_mkdir -p $DIR/$tdir
10829         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
10830
10831         check_default_stripe_attr --stripe-count --raw
10832         check_default_stripe_attr --stripe-index --raw
10833
10834         return 0
10835 }
10836 run_test 204g "Print raw stripe count and offset ==========="
10837
10838 test_204h() {
10839         test_mkdir -p $DIR/$tdir
10840         $SETSTRIPE --stripe-index 0 $DIR/$tdir
10841
10842         check_default_stripe_attr --stripe-count --raw
10843         check_default_stripe_attr --stripe-size --raw
10844
10845         return 0
10846 }
10847 run_test 204h "Print raw stripe count and size ============="
10848
10849 # Figure out which job scheduler is being used, if any,
10850 # or use a fake one
10851 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
10852         JOBENV=SLURM_JOB_ID
10853 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
10854         JOBENV=LSB_JOBID
10855 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
10856         JOBENV=PBS_JOBID
10857 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
10858         JOBENV=LOADL_STEP_ID
10859 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
10860         JOBENV=JOB_ID
10861 else
10862         JOBENV=FAKE_JOBID
10863 fi
10864
10865 verify_jobstats() {
10866         local cmd=$1
10867         local target=$2
10868
10869         # clear old jobstats
10870         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
10871         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
10872
10873         # use a new JobID for this test, or we might see an old one
10874         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
10875
10876         JOBVAL=${!JOBENV}
10877         log "Test: $cmd"
10878         log "Using JobID environment variable $JOBENV=$JOBVAL"
10879
10880         if [ $JOBENV = "FAKE_JOBID" ]; then
10881                 FAKE_JOBID=$JOBVAL $cmd
10882         else
10883                 $cmd
10884         fi
10885
10886         if [ "$target" = "mdt" -o "$target" = "both" ]; then
10887                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
10888                 do_facet $FACET lctl get_param mdt.*.job_stats |
10889                         grep $JOBVAL || error "No job stats found on MDT $FACET"
10890         fi
10891         if [ "$target" = "ost" -o "$target" = "both" ]; then
10892                 FACET=ost1
10893                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
10894                         grep $JOBVAL || error "No job stats found on OST $FACET"
10895         fi
10896 }
10897
10898 jobstats_set() {
10899         trap 0
10900         NEW_JOBENV=${1:-$OLD_JOBENV}
10901         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
10902         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
10903 }
10904
10905 test_205() { # Job stats
10906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10907         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
10908                 skip "Server doesn't support jobstats" && return 0
10909
10910         local cmd
10911         OLD_JOBENV=`$LCTL get_param -n jobid_var`
10912         if [ $OLD_JOBENV != $JOBENV ]; then
10913                 jobstats_set $JOBENV
10914                 trap jobstats_set EXIT
10915         fi
10916
10917         # mkdir
10918         cmd="mkdir $DIR/$tfile"
10919         verify_jobstats "$cmd" "mdt"
10920         # rmdir
10921         cmd="rm -fr $DIR/$tfile"
10922         verify_jobstats "$cmd" "mdt"
10923         # mknod
10924         cmd="mknod $DIR/$tfile c 1 3"
10925         verify_jobstats "$cmd" "mdt"
10926         # unlink
10927         cmd="rm -f $DIR/$tfile"
10928         verify_jobstats "$cmd" "mdt"
10929         # open & close
10930         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
10931         verify_jobstats "$cmd" "mdt"
10932         # setattr
10933         cmd="touch $DIR/$tfile"
10934         verify_jobstats "$cmd" "both"
10935         # write
10936         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
10937         verify_jobstats "$cmd" "ost"
10938         # read
10939         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
10940         verify_jobstats "$cmd" "ost"
10941         # truncate
10942         cmd="$TRUNCATE $DIR/$tfile 0"
10943         verify_jobstats "$cmd" "both"
10944         # rename
10945         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
10946         verify_jobstats "$cmd" "mdt"
10947
10948         # cleanup
10949         rm -f $DIR/jobstats_test_rename
10950
10951         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
10952 }
10953 run_test 205 "Verify job stats"
10954
10955 # LU-1480, LU-1773 and LU-1657
10956 test_206() {
10957         mkdir -p $DIR/$tdir
10958         lfs setstripe -c -1 $DIR/$tdir
10959 #define OBD_FAIL_LOV_INIT 0x1403
10960         $LCTL set_param fail_loc=0xa0001403
10961         $LCTL set_param fail_val=1
10962         touch $DIR/$tdir/$tfile || true
10963 }
10964 run_test 206 "fail lov_init_raid0() doesn't lbug"
10965
10966 test_207a() {
10967         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10968         local fsz=`stat -c %s $DIR/$tfile`
10969         cancel_lru_locks mdc
10970
10971         # do not return layout in getattr intent
10972 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
10973         $LCTL set_param fail_loc=0x170
10974         local sz=`stat -c %s $DIR/$tfile`
10975
10976         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
10977
10978         rm -rf $DIR/$tfile
10979 }
10980 run_test 207a "can refresh layout at glimpse"
10981
10982 test_207b() {
10983         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
10984         local cksum=`md5sum $DIR/$tfile`
10985         local fsz=`stat -c %s $DIR/$tfile`
10986         cancel_lru_locks mdc
10987         cancel_lru_locks osc
10988
10989         # do not return layout in getattr intent
10990 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
10991         $LCTL set_param fail_loc=0x171
10992
10993         # it will refresh layout after the file is opened but before read issues
10994         echo checksum is "$cksum"
10995         echo "$cksum" |md5sum -c --quiet || error "file differs"
10996
10997         rm -rf $DIR/$tfile
10998 }
10999 run_test 207b "can refresh layout at open"
11000
11001 test_208() {
11002         # FIXME: in this test suite, only RD lease is used. This is okay
11003         # for now as only exclusive open is supported. After generic lease
11004         # is done, this test suite should be revised. - Jinshan
11005
11006         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11007                 { skip "Need MDS version at least 2.4.52"; return 0; }
11008
11009         echo "==== test 1: verify get lease work"
11010         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11011
11012         echo "==== test 2: verify lease can be broken by upcoming open"
11013         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11014         local PID=$!
11015         sleep 1
11016
11017         $MULTIOP $DIR/$tfile oO_RDONLY:c
11018         kill -USR1 $PID && wait $PID || error "break lease error"
11019
11020         echo "==== test 3: verify lease can't be granted if an open already exists"
11021         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11022         local PID=$!
11023         sleep 1
11024
11025         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11026         kill -USR1 $PID && wait $PID || error "open file error"
11027
11028         echo "==== test 4: lease can sustain over recovery"
11029         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11030         PID=$!
11031         sleep 1
11032
11033         fail mds1
11034
11035         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11036
11037         echo "==== test 5: lease broken can't be regained by replay"
11038         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11039         PID=$!
11040         sleep 1
11041
11042         # open file to break lease and then recovery
11043         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11044         fail mds1
11045
11046         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11047
11048         rm -f $DIR/$tfile
11049 }
11050 run_test 208 "Exclusive open"
11051
11052 test_209() {
11053         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11054                 skip_env "must have disp_stripe" && return
11055
11056         touch $DIR/$tfile
11057         sync; sleep 5; sync;
11058
11059         echo 3 > /proc/sys/vm/drop_caches
11060         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11061
11062         # open/close 500 times
11063         for i in $(seq 500); do
11064                 cat $DIR/$tfile
11065         done
11066
11067         echo 3 > /proc/sys/vm/drop_caches
11068         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11069
11070         echo "before: $req_before, after: $req_after"
11071         [ $((req_after - req_before)) -ge 300 ] &&
11072                 error "open/close requests are not freed"
11073         return 0
11074 }
11075 run_test 209 "read-only open/close requests should be freed promptly"
11076
11077 test_212() {
11078         size=`date +%s`
11079         size=$((size % 8192 + 1))
11080         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11081         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11082         rm -f $DIR/f212 $DIR/f212.xyz
11083 }
11084 run_test 212 "Sendfile test ============================================"
11085
11086 test_213() {
11087         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11088         cancel_lru_locks osc
11089         lctl set_param fail_loc=0x8000040f
11090         # generate a read lock
11091         cat $DIR/$tfile > /dev/null
11092         # write to the file, it will try to cancel the above read lock.
11093         cat /etc/hosts >> $DIR/$tfile
11094 }
11095 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11096
11097 test_214() { # for bug 20133
11098         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11099         for (( i=0; i < 340; i++ )) ; do
11100                 touch $DIR/$tdir/d214c/a$i
11101         done
11102
11103         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11104         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11105         ls $DIR/d214c || error "ls $DIR/d214c failed"
11106         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11107         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11108 }
11109 run_test 214 "hash-indexed directory test - bug 20133"
11110
11111 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11112 create_lnet_proc_files() {
11113         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11114         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11115
11116         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11117         rm -f "$TMP/lnet_$1.sys_tmp"
11118 }
11119
11120 # counterpart of create_lnet_proc_files
11121 remove_lnet_proc_files() {
11122         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11123 }
11124
11125 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11126 # 3rd arg as regexp for body
11127 check_lnet_proc_stats() {
11128         local l=$(cat "$TMP/lnet_$1" |wc -l)
11129         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11130
11131         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11132 }
11133
11134 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11135 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11136 # optional and can be regexp for 2nd line (lnet.routes case)
11137 check_lnet_proc_entry() {
11138         local blp=2            # blp stands for 'position of 1st line of body'
11139         [ "$5" = "" ] || blp=3 # lnet.routes case
11140
11141         local l=$(cat "$TMP/lnet_$1" |wc -l)
11142         # subtracting one from $blp because the body can be empty
11143         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11144
11145         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11146                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11147
11148         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11149                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11150
11151         # bail out if any unexpected line happened
11152         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
11153         [ "$?" != 0 ] || error "$2 misformatted"
11154 }
11155
11156 test_215() { # for bugs 18102, 21079, 21517
11157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11158         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11159         local P='[1-9][0-9]*'           # positive numeric
11160         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11161         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11162         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11163         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11164
11165         local L1 # regexp for 1st line
11166         local L2 # regexp for 2nd line (optional)
11167         local BR # regexp for the rest (body)
11168
11169         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11170         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11171         create_lnet_proc_files "stats"
11172         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11173         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11174         remove_lnet_proc_files "stats"
11175
11176         # /proc/sys/lnet/routes should look like this:
11177         # Routing disabled/enabled
11178         # net hops priority state router
11179         # where net is a string like tcp0, hops > 0, priority >= 0,
11180         # state is up/down,
11181         # router is a string like 192.168.1.1@tcp2
11182         L1="^Routing (disabled|enabled)$"
11183         L2="^net +hops +priority +state +router$"
11184         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11185         create_lnet_proc_files "routes"
11186         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11187         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11188         remove_lnet_proc_files "routes"
11189
11190         # /proc/sys/lnet/routers should look like this:
11191         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11192         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11193         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11194         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11195         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11196         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11197         create_lnet_proc_files "routers"
11198         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11199         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11200         remove_lnet_proc_files "routers"
11201
11202         # /proc/sys/lnet/peers should look like this:
11203         # nid refs state last max rtr min tx min queue
11204         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11205         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11206         # numeric (0 or >0 or <0), queue >= 0.
11207         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11208         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11209         create_lnet_proc_files "peers"
11210         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11211         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11212         remove_lnet_proc_files "peers"
11213
11214         # /proc/sys/lnet/buffers  should look like this:
11215         # pages count credits min
11216         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11217         L1="^pages +count +credits +min$"
11218         BR="^ +$N +$N +$I +$I$"
11219         create_lnet_proc_files "buffers"
11220         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11221         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11222         remove_lnet_proc_files "buffers"
11223
11224         # /proc/sys/lnet/nis should look like this:
11225         # nid status alive refs peer rtr max tx min
11226         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11227         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11228         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11229         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11230         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11231         create_lnet_proc_files "nis"
11232         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11233         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11234         remove_lnet_proc_files "nis"
11235
11236         # can we successfully write to /proc/sys/lnet/stats?
11237         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11238         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11239 }
11240 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11241
11242 test_216() { # bug 20317
11243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11244         remote_ost_nodsh && skip "remote OST with nodsh" && return
11245
11246         local node
11247         local facets=$(get_facets OST)
11248         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11249
11250         save_lustre_params client "osc.*.contention_seconds" > $p
11251         save_lustre_params $facets \
11252                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11253         save_lustre_params $facets \
11254                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11255         save_lustre_params $facets \
11256                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11257         clear_osc_stats
11258
11259         # agressive lockless i/o settings
11260         for node in $(osts_nodes); do
11261                 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'
11262         done
11263         lctl set_param -n osc.*.contention_seconds 60
11264
11265         $DIRECTIO write $DIR/$tfile 0 10 4096
11266         $CHECKSTAT -s 40960 $DIR/$tfile
11267
11268         # disable lockless i/o
11269         for node in $(osts_nodes); do
11270                 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'
11271         done
11272         lctl set_param -n osc.*.contention_seconds 0
11273         clear_osc_stats
11274
11275         dd if=/dev/zero of=$DIR/$tfile count=0
11276         $CHECKSTAT -s 0 $DIR/$tfile
11277
11278         restore_lustre_params <$p
11279         rm -f $p
11280         rm $DIR/$tfile
11281 }
11282 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11283
11284 test_217() { # bug 22430
11285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11286         local node
11287         local nid
11288
11289         for node in $(nodes_list); do
11290                 nid=$(host_nids_address $node $NETTYPE)
11291                 if [[ $nid = *-* ]] ; then
11292                         echo "lctl ping $nid@$NETTYPE"
11293                         lctl ping $nid@$NETTYPE
11294                 else
11295                         echo "skipping $node (no hyphen detected)"
11296                 fi
11297         done
11298 }
11299 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11300
11301 test_218() {
11302        # do directio so as not to populate the page cache
11303        log "creating a 10 Mb file"
11304        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11305        log "starting reads"
11306        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11307        log "truncating the file"
11308        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11309        log "killing dd"
11310        kill %+ || true # reads might have finished
11311        echo "wait until dd is finished"
11312        wait
11313        log "removing the temporary file"
11314        rm -rf $DIR/$tfile || error "tmp file removal failed"
11315 }
11316 run_test 218 "parallel read and truncate should not deadlock ======================="
11317
11318 test_219() {
11319         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11320         # write one partial page
11321         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11322         # set no grant so vvp_io_commit_write will do sync write
11323         $LCTL set_param fail_loc=0x411
11324         # write a full page at the end of file
11325         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11326
11327         $LCTL set_param fail_loc=0
11328         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11329         $LCTL set_param fail_loc=0x411
11330         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11331
11332         # LU-4201
11333         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11334         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11335 }
11336 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11337
11338 test_220() { #LU-325
11339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11340         remote_ost_nodsh && skip "remote OST with nodsh" && return
11341         local OSTIDX=0
11342
11343         test_mkdir -p $DIR/$tdir
11344         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11345                 awk '{print $2}' | sed -e 's/_UUID$//')
11346
11347         # on the mdt's osc
11348         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11349         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11350                         osc.$mdtosc_proc1.prealloc_last_id)
11351         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11352                         osc.$mdtosc_proc1.prealloc_next_id)
11353
11354         $LFS df -i
11355
11356         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11357         #define OBD_FAIL_OST_ENOINO              0x229
11358         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11359         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11360         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11361
11362         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11363
11364         MDSOBJS=$((last_id - next_id))
11365         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11366
11367         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11368         echo "OST still has $count kbytes free"
11369
11370         echo "create $MDSOBJS files @next_id..."
11371         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11372
11373         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11374                         osc.$mdtosc_proc1.prealloc_last_id)
11375         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11376                         osc.$mdtosc_proc1.prealloc_next_id)
11377
11378         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11379         $LFS df -i
11380
11381         echo "cleanup..."
11382
11383         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11384         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11385
11386         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11387         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11388         echo "unlink $MDSOBJS files @$next_id..."
11389         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11390 }
11391 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11392
11393 test_221() {
11394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11395         dd if=`which date` of=$MOUNT/date oflag=sync
11396         chmod +x $MOUNT/date
11397
11398         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11399         $LCTL set_param fail_loc=0x80001401
11400
11401         $MOUNT/date > /dev/null
11402         rm -f $MOUNT/date
11403 }
11404 run_test 221 "make sure fault and truncate race to not cause OOM"
11405
11406 test_222a () {
11407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11408        rm -rf $DIR/$tdir
11409        test_mkdir -p $DIR/$tdir
11410        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11411        createmany -o $DIR/$tdir/$tfile 10
11412        cancel_lru_locks mdc
11413        cancel_lru_locks osc
11414        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11415        $LCTL set_param fail_loc=0x31a
11416        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11417        $LCTL set_param fail_loc=0
11418        rm -r $DIR/$tdir
11419 }
11420 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11421
11422 test_222b () {
11423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11424        rm -rf $DIR/$tdir
11425        test_mkdir -p $DIR/$tdir
11426        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11427        createmany -o $DIR/$tdir/$tfile 10
11428        cancel_lru_locks mdc
11429        cancel_lru_locks osc
11430        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11431        $LCTL set_param fail_loc=0x31a
11432        rm -r $DIR/$tdir || "AGL for rmdir failed"
11433        $LCTL set_param fail_loc=0
11434 }
11435 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11436
11437 test_223 () {
11438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11439        rm -rf $DIR/$tdir
11440        test_mkdir -p $DIR/$tdir
11441        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11442        createmany -o $DIR/$tdir/$tfile 10
11443        cancel_lru_locks mdc
11444        cancel_lru_locks osc
11445        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11446        $LCTL set_param fail_loc=0x31b
11447        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11448        $LCTL set_param fail_loc=0
11449        rm -r $DIR/$tdir
11450 }
11451 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11452
11453 test_224a() { # LU-1039, MRP-303
11454         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11455         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11456         $LCTL set_param fail_loc=0x508
11457         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11458         $LCTL set_param fail_loc=0
11459         df $DIR
11460 }
11461 run_test 224a "Don't panic on bulk IO failure"
11462
11463 test_224b() { # LU-1039, MRP-303
11464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11465         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11466         cancel_lru_locks osc
11467         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11468         $LCTL set_param fail_loc=0x515
11469         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11470         $LCTL set_param fail_loc=0
11471         df $DIR
11472 }
11473 run_test 224b "Don't panic on bulk IO failure"
11474
11475 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11476 test_225a () {
11477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11478         if [ -z ${MDSSURVEY} ]; then
11479               skip_env "mds-survey not found" && return
11480         fi
11481
11482         [ $MDSCOUNT -ge 2 ] &&
11483                 skip "skipping now for more than one MDT" && return
11484
11485        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11486             { skip "Need MDS version at least 2.2.51"; return; }
11487
11488        local mds=$(facet_host $SINGLEMDS)
11489        local target=$(do_nodes $mds 'lctl dl' | \
11490                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11491
11492        local cmd1="file_count=1000 thrhi=4"
11493        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11494        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11495        local cmd="$cmd1 $cmd2 $cmd3"
11496
11497        rm -f ${TMP}/mds_survey*
11498        echo + $cmd
11499        eval $cmd || error "mds-survey with zero-stripe failed"
11500        cat ${TMP}/mds_survey*
11501        rm -f ${TMP}/mds_survey*
11502 }
11503 run_test 225a "Metadata survey sanity with zero-stripe"
11504
11505 test_225b () {
11506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11507
11508         if [ -z ${MDSSURVEY} ]; then
11509               skip_env "mds-survey not found" && return
11510         fi
11511         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11512             { skip "Need MDS version at least 2.2.51"; return; }
11513
11514         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11515               skip_env "Need to mount OST to test" && return
11516         fi
11517
11518         [ $MDSCOUNT -ge 2 ] &&
11519                 skip "skipping now for more than one MDT" && return
11520        local mds=$(facet_host $SINGLEMDS)
11521        local target=$(do_nodes $mds 'lctl dl' | \
11522                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11523
11524        local cmd1="file_count=1000 thrhi=4"
11525        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11526        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11527        local cmd="$cmd1 $cmd2 $cmd3"
11528
11529        rm -f ${TMP}/mds_survey*
11530        echo + $cmd
11531        eval $cmd || error "mds-survey with stripe_count failed"
11532        cat ${TMP}/mds_survey*
11533        rm -f ${TMP}/mds_survey*
11534 }
11535 run_test 225b "Metadata survey sanity with stripe_count = 1"
11536
11537 mcreate_path2fid () {
11538         local mode=$1
11539         local major=$2
11540         local minor=$3
11541         local name=$4
11542         local desc=$5
11543         local path=$DIR/$tdir/$name
11544         local fid
11545         local rc
11546         local fid_path
11547
11548         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11549                 error "cannot create $desc"
11550
11551         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11552         rc=$?
11553         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11554
11555         fid_path=$($LFS fid2path $MOUNT $fid)
11556         rc=$?
11557         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11558
11559         [ "$path" == "$fid_path" ] ||
11560                 error "fid2path returned $fid_path, expected $path"
11561
11562         echo "pass with $path and $fid"
11563 }
11564
11565 test_226a () {
11566         rm -rf $DIR/$tdir
11567         mkdir -p $DIR/$tdir
11568
11569         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11570         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11571         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11572         mcreate_path2fid 0040666 0 0 dir "directory"
11573         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11574         mcreate_path2fid 0100666 0 0 file "regular file"
11575         mcreate_path2fid 0120666 0 0 link "symbolic link"
11576         mcreate_path2fid 0140666 0 0 sock "socket"
11577 }
11578 run_test 226a "call path2fid and fid2path on files of all type"
11579
11580 test_226b () {
11581         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11582         rm -rf $DIR/$tdir
11583         local MDTIDX=1
11584
11585         mkdir -p $DIR/$tdir
11586         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11587                 error "create remote directory failed"
11588         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11589         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11590                                 "character special file (null)"
11591         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11592                                 "character special file (no device)"
11593         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11594         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11595                                 "block special file (loop)"
11596         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11597         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11598         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11599 }
11600 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11601
11602 # LU-1299 Executing or running ldd on a truncated executable does not
11603 # cause an out-of-memory condition.
11604 test_227() {
11605         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11606         dd if=`which date` of=$MOUNT/date bs=1k count=1
11607         chmod +x $MOUNT/date
11608
11609         $MOUNT/date > /dev/null
11610         ldd $MOUNT/date > /dev/null
11611         rm -f $MOUNT/date
11612 }
11613 run_test 227 "running truncated executable does not cause OOM"
11614
11615 # LU-1512 try to reuse idle OI blocks
11616 test_228a() {
11617         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11618         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11619                 skip "non-ldiskfs backend" && return
11620
11621         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11622         local myDIR=$DIR/$tdir
11623
11624         mkdir -p $myDIR
11625         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11626         $LCTL set_param fail_loc=0x80001002
11627         createmany -o $myDIR/t- 10000
11628         $LCTL set_param fail_loc=0
11629         # The guard is current the largest FID holder
11630         touch $myDIR/guard
11631         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11632                     tr -d '[')
11633         local IDX=$(($SEQ % 64))
11634
11635         do_facet $SINGLEMDS sync
11636         # Make sure journal flushed.
11637         sleep 6
11638         local blk1=$(do_facet $SINGLEMDS \
11639                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11640                      grep Blockcount | awk '{print $4}')
11641
11642         # Remove old files, some OI blocks will become idle.
11643         unlinkmany $myDIR/t- 10000
11644         # Create new files, idle OI blocks should be reused.
11645         createmany -o $myDIR/t- 2000
11646         do_facet $SINGLEMDS sync
11647         # Make sure journal flushed.
11648         sleep 6
11649         local blk2=$(do_facet $SINGLEMDS \
11650                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11651                      grep Blockcount | awk '{print $4}')
11652
11653         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11654 }
11655 run_test 228a "try to reuse idle OI blocks"
11656
11657 test_228b() {
11658         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11659         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11660                 skip "non-ldiskfs backend" && return
11661
11662         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11663         local myDIR=$DIR/$tdir
11664
11665         mkdir -p $myDIR
11666         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11667         $LCTL set_param fail_loc=0x80001002
11668         createmany -o $myDIR/t- 10000
11669         $LCTL set_param fail_loc=0
11670         # The guard is current the largest FID holder
11671         touch $myDIR/guard
11672         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11673                     tr -d '[')
11674         local IDX=$(($SEQ % 64))
11675
11676         do_facet $SINGLEMDS sync
11677         # Make sure journal flushed.
11678         sleep 6
11679         local blk1=$(do_facet $SINGLEMDS \
11680                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11681                      grep Blockcount | awk '{print $4}')
11682
11683         # Remove old files, some OI blocks will become idle.
11684         unlinkmany $myDIR/t- 10000
11685
11686         # stop the MDT
11687         stop $SINGLEMDS || error "Fail to stop MDT."
11688         # remount the MDT
11689         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11690
11691         df $MOUNT || error "Fail to df."
11692         # Create new files, idle OI blocks should be reused.
11693         createmany -o $myDIR/t- 2000
11694         do_facet $SINGLEMDS sync
11695         # Make sure journal flushed.
11696         sleep 6
11697         local blk2=$(do_facet $SINGLEMDS \
11698                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11699                      grep Blockcount | awk '{print $4}')
11700
11701         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11702 }
11703 run_test 228b "idle OI blocks can be reused after MDT restart"
11704
11705 #LU-1881
11706 test_228c() {
11707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11708         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11709                 skip "non-ldiskfs backend" && return
11710
11711         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11712         local myDIR=$DIR/$tdir
11713
11714         mkdir -p $myDIR
11715         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11716         $LCTL set_param fail_loc=0x80001002
11717         # 20000 files can guarantee there are index nodes in the OI file
11718         createmany -o $myDIR/t- 20000
11719         $LCTL set_param fail_loc=0
11720         # The guard is current the largest FID holder
11721         touch $myDIR/guard
11722         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11723                     tr -d '[')
11724         local IDX=$(($SEQ % 64))
11725
11726         do_facet $SINGLEMDS sync
11727         # Make sure journal flushed.
11728         sleep 6
11729         local blk1=$(do_facet $SINGLEMDS \
11730                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11731                      grep Blockcount | awk '{print $4}')
11732
11733         # Remove old files, some OI blocks will become idle.
11734         unlinkmany $myDIR/t- 20000
11735         rm -f $myDIR/guard
11736         # The OI file should become empty now
11737
11738         # Create new files, idle OI blocks should be reused.
11739         createmany -o $myDIR/t- 2000
11740         do_facet $SINGLEMDS sync
11741         # Make sure journal flushed.
11742         sleep 6
11743         local blk2=$(do_facet $SINGLEMDS \
11744                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11745                      grep Blockcount | awk '{print $4}')
11746
11747         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11748 }
11749 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
11750
11751 test_229() { # LU-2482, LU-3448
11752         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
11753                 skip "No HSM support on MDS of $(get_lustre_version)," \
11754                          "need 2.4.53 at least" && return
11755         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11756         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
11757
11758         rm -f $DIR/$tfile
11759
11760         # Create a file with a released layout and stripe count 2.
11761         $MULTIOP $DIR/$tfile H2c ||
11762                 error "failed to create file with released layout"
11763
11764         $GETSTRIPE -v $DIR/$tfile
11765
11766         local pattern=$($GETSTRIPE -L $DIR/$tfile)
11767         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
11768
11769         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
11770         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
11771         stat $DIR/$tfile || error "failed to stat released file"
11772
11773         chown $RUNAS_ID $DIR/$tfile ||
11774                 error "chown $RUNAS_ID $DIR/$tfile failed"
11775
11776         chgrp $RUNAS_ID $DIR/$tfile ||
11777                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
11778
11779         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
11780         rm $DIR/$tfile || error "failed to remove released file"
11781 }
11782 run_test 229 "getstripe/stat/rm/attr changes work on released files"
11783
11784 test_230a() {
11785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11786         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11787         local MDTIDX=1
11788
11789         mkdir -p $DIR/$tdir/test_230_local
11790         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
11791         [ $mdt_idx -ne 0 ] &&
11792                 error "create local directory on wrong MDT $mdt_idx"
11793
11794         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
11795                         error "create remote directory failed"
11796         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
11797         [ $mdt_idx -ne $MDTIDX ] &&
11798                 error "create remote directory on wrong MDT $mdt_idx"
11799
11800         createmany -o $DIR/$tdir/test_230/t- 10 ||
11801                 error "create files on remote directory failed"
11802         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
11803         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
11804         rm -r $DIR/$tdir || error "unlink remote directory failed"
11805 }
11806 run_test 230a "Create remote directory and files under the remote directory"
11807
11808 test_231a()
11809 {
11810         # For simplicity this test assumes that max_pages_per_rpc
11811         # is the same across all OSCs
11812         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
11813         local bulk_size=$((max_pages * 4096))
11814
11815         mkdir -p $DIR/$tdir
11816
11817         # clear the OSC stats
11818         $LCTL set_param osc.*.stats=0 &>/dev/null
11819
11820         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
11821         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
11822                 oflag=direct &>/dev/null || error "dd failed"
11823
11824         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
11825         if [ x$nrpcs != "x1" ]; then
11826                 error "found $nrpc ost_write RPCs, not 1 as expected"
11827         fi
11828
11829         # Drop the OSC cache, otherwise we will read from it
11830         cancel_lru_locks osc
11831
11832         # clear the OSC stats
11833         $LCTL set_param osc.*.stats=0 &>/dev/null
11834
11835         # Client reads $bulk_size.
11836         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
11837                 iflag=direct &>/dev/null || error "dd failed"
11838
11839         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
11840         if [ x$nrpcs != "x1" ]; then
11841                 error "found $nrpc ost_read RPCs, not 1 as expected"
11842         fi
11843 }
11844 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
11845
11846 test_231b() {
11847         mkdir -p $DIR/$tdir
11848         local i
11849         for i in {0..1023}; do
11850                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
11851                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
11852                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
11853         done
11854         sync
11855 }
11856 run_test 231b "must not assert on fully utilized OST request buffer"
11857
11858 test_232() {
11859         mkdir -p $DIR/$tdir
11860         #define OBD_FAIL_LDLM_OST_LVB            0x31c
11861         $LCTL set_param fail_loc=0x31c
11862
11863         # ignore dd failure
11864         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
11865
11866         $LCTL set_param fail_loc=0
11867         umount_client $MOUNT || error "umount failed"
11868         mount_client $MOUNT || error "mount failed"
11869 }
11870 run_test 232 "failed lock should not block umount"
11871
11872 test_233a() {
11873         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
11874         { skip "Need MDS version at least 2.3.64"; return; }
11875
11876         local fid=$($LFS path2fid $MOUNT)
11877         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11878                 error "cannot access $MOUNT using its FID '$fid'"
11879 }
11880 run_test 233a "checking that OBF of the FS root succeeds"
11881
11882 test_233b() {
11883         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
11884         { skip "Need MDS version at least 2.5.90"; return; }
11885
11886         local fid=$($LFS path2fid $MOUNT/.lustre)
11887         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11888                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
11889
11890         fid=$($LFS path2fid $MOUNT/.lustre/fid)
11891         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
11892                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
11893 }
11894 run_test 233b "checking that OBF of the FS .lustre succeeds"
11895
11896 test_234() {
11897         local p="$TMP/sanityN-$TESTNAME.parameters"
11898         save_lustre_params client "llite.*.xattr_cache" > $p
11899         lctl set_param llite.*.xattr_cache 1 ||
11900                 { skip "xattr cache is not supported"; return 0; }
11901
11902         mkdir -p $DIR/$tdir || error "mkdir failed"
11903         touch $DIR/$tdir/$tfile || error "touch failed"
11904         # OBD_FAIL_LLITE_XATTR_ENOMEM
11905         $LCTL set_param fail_loc=0x1405
11906         if [ ! -f /etc/SuSE-release ]; then
11907                 # attr pre-2.4.44-7 had a bug with rc
11908                 # LU-3703 - SLES clients have older attr
11909                 getfattr -n user.attr $DIR/$tdir/$tfile &&
11910                         error "getfattr should have failed with ENOMEM"
11911         fi
11912         $LCTL set_param fail_loc=0x0
11913         rm -rf $DIR/$tdir
11914
11915         restore_lustre_params < $p
11916         rm -f $p
11917 }
11918 run_test 234 "xattr cache should not crash on ENOMEM"
11919
11920 test_235() {
11921          [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
11922                 skip "Need MDS version at least 2.4.52" && return
11923         flock_deadlock $DIR/$tfile
11924         local RC=$?
11925         case $RC in
11926                 0)
11927                 ;;
11928                 124) error "process hangs on a deadlock"
11929                 ;;
11930                 *) error "error executing flock_deadlock $DIR/$tfile"
11931                 ;;
11932         esac
11933 }
11934 run_test 235 "LU-1715: flock deadlock detection does not work properly"
11935
11936 #LU-2935
11937 test_236() {
11938         check_swap_layouts_support && return 0
11939         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
11940
11941         local ref1=/etc/passwd
11942         local ref2=/etc/group
11943         local file1=$DIR/$tdir/f1
11944         local file2=$DIR/$tdir/f2
11945
11946         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
11947         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
11948         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
11949         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
11950         local fd=$(free_fd)
11951         local cmd="exec $fd<>$file2"
11952         eval $cmd
11953         rm $file2
11954         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
11955                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
11956         cmd="exec $fd>&-"
11957         eval $cmd
11958         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11959
11960         #cleanup
11961         rm -rf $DIR/$tdir
11962 }
11963 run_test 236 "Layout swap on open unlinked file"
11964
11965 # test to verify file handle related system calls
11966 # (name_to_handle_at/open_by_handle_at)
11967 # The new system calls are supported in glibc >= 2.14.
11968
11969 test_237() {
11970         echo "Test file_handle syscalls" > $DIR/$tfile
11971         check_fhandle_syscalls $DIR/$tfile ||
11972                 error "check_fhandle_syscalls failed"
11973 }
11974 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
11975
11976 test_striped_dir() {
11977         local mdt_index=$1
11978         local stripe_count
11979         local stripe_index
11980
11981         mkdir -p $DIR/$tdir
11982         $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
11983                 error "set striped dir error"
11984
11985         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
11986         if [ "$stripe_count" != "2" ]; then
11987                 error "stripe_count is $stripe_count, expect 2"
11988         fi
11989
11990         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
11991         if [ "$stripe_index" != "$mdt_index" ]; then
11992                 error "stripe_index is $stripe_index, expect $mdt_index"
11993         fi
11994
11995         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
11996                 error "nlink error after create striped dir"
11997
11998         mkdir $DIR/$tdir/striped_dir/a
11999         mkdir $DIR/$tdir/striped_dir/b
12000
12001         stat $DIR/$tdir/striped_dir/a ||
12002                 error "create dir under striped dir failed"
12003         stat $DIR/$tdir/striped_dir/b ||
12004                 error "create dir under striped dir failed"
12005
12006         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12007                 error "nlink error after mkdir"
12008
12009         rmdir $DIR/$tdir/striped_dir/a
12010         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12011                 error "nlink error after rmdir"
12012
12013         rmdir $DIR/$tdir/striped_dir/b
12014         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12015                 error "nlink error after rmdir"
12016
12017         rmdir $DIR/$tdir/striped_dir ||
12018                 error "rmdir striped dir error"
12019         true
12020 }
12021
12022 test_300a() {
12023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12024         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12025
12026         test_striped_dir 0 || error "failed on striped dir on MDT0"
12027         test_striped_dir 1 || error "failed on striped dir on MDT0"
12028 }
12029 run_test 300a "basic striped dir sanity test"
12030
12031 test_300b() {
12032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12033         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12034         local i
12035         local mtime1
12036         local mtime2
12037         local mtime3
12038
12039         test_mkdir $DIR/$tdir || error "mkdir fail"
12040         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12041                 error "set striped dir error"
12042         for ((i=0; i<10; i++)); do
12043                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12044                 sleep 1
12045                 touch $DIR/$tdir/striped_dir/file_$i ||
12046                                         error "touch error $i"
12047                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12048                 [ $mtime1 -eq $mtime2 ] &&
12049                         error "mtime not change after create"
12050                 sleep 1
12051                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12052                                         error "unlink error $i"
12053                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12054                 [ $mtime2 -eq $mtime3 ] &&
12055                         error "mtime did not change after unlink"
12056         done
12057         true
12058 }
12059 run_test 300b "check ctime/mtime for striped dir"
12060
12061 test_300c() {
12062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12063         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12064         local file_count
12065
12066         mkdir -p $DIR/$tdir
12067         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12068                 error "set striped dir error"
12069
12070         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12071                 error "chown striped dir failed"
12072
12073         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12074                 error "create 5k files failed"
12075
12076         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12077
12078         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12079
12080         rm -rf $DIR/$tdir
12081 }
12082 run_test 300c "chown && check ls under striped directory"
12083
12084 test_300d() {
12085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12086         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12087         local stripe_count
12088         local file
12089
12090         mkdir -p $DIR/$tdir
12091         $SETSTRIPE -c 2 $DIR/$tdir
12092
12093         #local striped directory
12094         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12095                 error "set striped dir error"
12096         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12097                 error "create 10 files failed"
12098
12099         #remote striped directory
12100         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12101                 error "set striped dir error"
12102         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12103                 error "create 10 files failed"
12104
12105         for file in $(find $DIR/$tdir); do
12106                 stripe_count=$($GETSTRIPE -c $file)
12107                 [ $stripe_count -eq 2 ] ||
12108                         error "wrong stripe $stripe_count for $file"
12109         done
12110
12111         rm -rf $DIR/$tdir
12112 }
12113 run_test 300d "check default stripe under striped directory"
12114
12115 test_300e() {
12116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12117         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12118         local stripe_count
12119         local file
12120
12121         mkdir -p $DIR/$tdir
12122
12123         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12124                 error "set striped dir error"
12125
12126         touch $DIR/$tdir/striped_dir/a
12127         touch $DIR/$tdir/striped_dir/b
12128         touch $DIR/$tdir/striped_dir/c
12129
12130         mkdir $DIR/$tdir/striped_dir/dir_a
12131         mkdir $DIR/$tdir/striped_dir/dir_b
12132         mkdir $DIR/$tdir/striped_dir/dir_c
12133
12134         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12135                 error "set striped dir under striped dir error"
12136
12137         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12138                 error "set striped dir under striped dir error"
12139
12140         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12141                 error "set striped dir under striped dir error"
12142
12143         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12144                 error "rename file under striped dir should fail"
12145
12146         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12147                 error "rename dir under striped dir should fail"
12148
12149         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12150                 error "rename dir under different stripes should fail"
12151
12152         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12153                 error "rename file under striped dir should succeed"
12154
12155         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12156                 error "rename dir under striped dir should succeed"
12157
12158         rm -rf $DIR/$tdir
12159 }
12160 run_test 300e "check rename under striped directory"
12161
12162 test_300f() {
12163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12164         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12165         local stripe_count
12166         local file
12167
12168         rm -rf $DIR/$tdir
12169         mkdir -p $DIR/$tdir
12170
12171         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12172                 error "set striped dir error"
12173
12174         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12175                 error "set striped dir error"
12176
12177         touch $DIR/$tdir/striped_dir/a
12178         mkdir $DIR/$tdir/striped_dir/dir_a
12179         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12180                 error "create striped dir under striped dir fails"
12181
12182         touch $DIR/$tdir/striped_dir1/b
12183         mkdir $DIR/$tdir/striped_dir1/dir_b
12184         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12185                 error "create striped dir under striped dir fails"
12186
12187         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12188                 error "rename file under different striped dir should fail"
12189
12190         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12191                 error "rename dir under different striped dir should fail"
12192
12193         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12194                 error "rename striped dir under diff striped dir should fail"
12195
12196         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12197                 error "rename file under diff striped dirs fails"
12198
12199         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12200                 error "rename dir under diff striped dirs fails"
12201
12202         rm -rf $DIR/$tdir
12203 }
12204 run_test 300f "check rename cross striped directory"
12205
12206 #
12207 # tests that do cleanup/setup should be run at the end
12208 #
12209
12210 test_900() {
12211         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12212         local ls
12213         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12214         $LCTL set_param fail_loc=0x903
12215         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12216         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12217                 echo "clear" > $ls
12218         done
12219         FAIL_ON_ERROR=true cleanup
12220         FAIL_ON_ERROR=true setup
12221 }
12222 run_test 900 "umount should not race with any mgc requeue thread"
12223
12224 complete $SECONDS
12225 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
12226 check_and_cleanup_lustre
12227 if [ "$I_MOUNTED" != "yes" ]; then
12228         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12229 fi
12230 exit_status