Whamcloud - gitweb
2920c3d2bdfce339838d9b584728199438c11234
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; 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 SRCDIR=$(cd $(dirname $0); echo $PWD)
20 export PATH=$PATH:/sbin
21
22 TMP=${TMP:-/tmp}
23
24 CC=${CC:-cc}
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 OPENFILE=${OPENFILE:-openfile}
32 OPENUNLINK=${OPENUNLINK:-openunlink}
33 export MULTIOP=${MULTIOP:-multiop}
34 READS=${READS:-"reads"}
35 MUNLINK=${MUNLINK:-munlink}
36 SOCKETSERVER=${SOCKETSERVER:-socketserver}
37 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
38 MEMHOG=${MEMHOG:-memhog}
39 DIRECTIO=${DIRECTIO:-directio}
40 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
41 UMOUNT=${UMOUNT:-"umount -d"}
42 STRIPES_PER_OBJ=-1
43 CHECK_GRANT=${CHECK_GRANT:-"yes"}
44 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
45 export PARALLEL=${PARALLEL:-"no"}
46
47 export NAME=${NAME:-local}
48
49 SAVE_PWD=$PWD
50
51 CLEANUP=${CLEANUP:-:}
52 SETUP=${SETUP:-:}
53 TRACE=${TRACE:-""}
54 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
55 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
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 230d"
62
63 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
64 # bug number for skipped test:        LU-1593 LU-2833 LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     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 -n1)
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
200         test_mkdir $DIR/$tdir ||
201                 error "Create remote directory failed"
202
203         touch $DIR/$tdir/$tfile ||
204                 error "Create file under remote directory failed"
205
206         rmdir $DIR/$tdir &&
207                 error "Expect error removing in-use dir $DIR/$tdir"
208
209         test -d $DIR/$tdir || error "Remote directory disappeared"
210
211         rm -rf $DIR/$tdir || error "remove remote dir error"
212 }
213 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
214
215 test_5() {
216         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
217         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
218         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
219         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
220         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
221 }
222 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
223
224 test_6a() {
225         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
226         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
227         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
228                 error "$tfile does not have perm 0666 or UID $UID"
229         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
230         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
231                 error "$tfile should be 0666 and owned by UID $UID"
232 }
233 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
234
235 test_6c() {
236         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
237         touch $DIR/$tfile
238         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
239         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
240                 error "$tfile should be owned by UID $RUNAS_ID"
241         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
242         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
243                 error "$tfile should be owned by UID $RUNAS_ID"
244 }
245 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
246
247 test_6e() {
248         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
249         touch $DIR/$tfile
250         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
251         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
252                 error "$tfile should be owned by GID $UID"
253         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
254         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
255                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
256 }
257 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
258
259 test_6g() {
260         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
261         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
262         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
263         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
264         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
265         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
266         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
267                 error "$tdir/d/subdir should be GID $RUNAS_GID"
268 }
269 run_test 6g "Is new dir in sgid dir inheriting group?"
270
271 test_6h() { # bug 7331
272         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
273         touch $DIR/$tfile || error "touch failed"
274         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
275         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
276                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
277         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
278                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
279 }
280 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
281
282 test_7a() {
283         test_mkdir $DIR/$tdir
284         $MCREATE $DIR/$tdir/$tfile
285         chmod 0666 $DIR/$tdir/$tfile
286         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
287                 error "$tdir/$tfile should be mode 0666"
288 }
289 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
290
291 test_7b() {
292         if [ ! -d $DIR/$tdir ]; then
293                 mkdir $DIR/$tdir
294         fi
295         $MCREATE $DIR/$tdir/$tfile
296         echo -n foo > $DIR/$tdir/$tfile
297         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
298         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
299 }
300 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
301
302 test_8() {
303         test_mkdir $DIR/$tdir
304         touch $DIR/$tdir/$tfile
305         chmod 0666 $DIR/$tdir/$tfile
306         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
307                 error "$tfile mode not 0666"
308 }
309 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
310
311 test_9() {
312         test_mkdir $DIR/$tdir
313         test_mkdir $DIR/$tdir/d2
314         test_mkdir $DIR/$tdir/d2/d3
315         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
316 }
317 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
318
319 test_10() {
320         test_mkdir $DIR/$tdir
321         test_mkdir $DIR/$tdir/d2
322         touch $DIR/$tdir/d2/$tfile
323         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
324                 error "$tdir/d2/$tfile not a file"
325 }
326 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
327
328 test_11() {
329         test_mkdir $DIR/$tdir
330         test_mkdir $DIR/$tdir/d2
331         chmod 0666 $DIR/$tdir/d2
332         chmod 0705 $DIR/$tdir/d2
333         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
334                 error "$tdir/d2 mode not 0705"
335 }
336 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
337
338 test_12() {
339         test_mkdir $DIR/$tdir
340         touch $DIR/$tdir/$tfile
341         chmod 0666 $DIR/$tdir/$tfile
342         chmod 0654 $DIR/$tdir/$tfile
343         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
344                 error "$tdir/d2 mode not 0654"
345 }
346 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
347
348 test_13() {
349         test_mkdir $DIR/$tdir
350         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
351         >  $DIR/$tdir/$tfile
352         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
353                 error "$tdir/$tfile size not 0 after truncate"
354 }
355 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
356
357 test_14() {
358         test_mkdir $DIR/$tdir
359         touch $DIR/$tdir/$tfile
360         rm $DIR/$tdir/$tfile
361         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
362 }
363 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
364
365 test_15() {
366         test_mkdir $DIR/$tdir
367         touch $DIR/$tdir/$tfile
368         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
369         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
370                 error "$tdir/${tfile_2} not a file after rename"
371 }
372 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
373
374 test_16() {
375         test_mkdir $DIR/$tdir
376         touch $DIR/$tdir/$tfile
377         rm -rf $DIR/$tdir/$tfile
378         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
379 }
380 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
381
382 test_17a() {
383         test_mkdir -p $DIR/$tdir
384         touch $DIR/$tdir/$tfile
385         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
386         ls -l $DIR/$tdir
387         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
388                 error "$tdir/l-exist not a symlink"
389         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
390                 error "$tdir/l-exist not referencing a file"
391         rm -f $DIR/$tdir/l-exist
392         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
393 }
394 run_test 17a "symlinks: create, remove (real) =================="
395
396 test_17b() {
397         test_mkdir -p $DIR/$tdir
398         ln -s no-such-file $DIR/$tdir/l-dangle
399         ls -l $DIR/$tdir
400         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
401                 error "$tdir/l-dangle not referencing no-such-file"
402         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
403                 error "$tdir/l-dangle not referencing non-existent file"
404         rm -f $DIR/$tdir/l-dangle
405         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
406 }
407 run_test 17b "symlinks: create, remove (dangling) =============="
408
409 test_17c() { # bug 3440 - don't save failed open RPC for replay
410         test_mkdir -p $DIR/$tdir
411         ln -s foo $DIR/$tdir/$tfile
412         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
413 }
414 run_test 17c "symlinks: open dangling (should return error) ===="
415
416 test_17d() {
417         test_mkdir -p $DIR/$tdir
418         ln -s foo $DIR/$tdir/$tfile
419         touch $DIR/$tdir/$tfile || error "creating to new symlink"
420 }
421 run_test 17d "symlinks: create dangling ========================"
422
423 test_17e() {
424         test_mkdir -p $DIR/$tdir
425         local foo=$DIR/$tdir/$tfile
426         ln -s $foo $foo || error "create symlink failed"
427         ls -l $foo || error "ls -l failed"
428         ls $foo && error "ls not failed" || true
429 }
430 run_test 17e "symlinks: create recursive symlink (should return error) ===="
431
432 test_17f() {
433         test_mkdir -p $DIR/$tdir
434         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
435         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
436         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
437         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
438         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
439         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
440         ls -l  $DIR/$tdir
441 }
442 run_test 17f "symlinks: long and very long symlink name ========================"
443
444 # str_repeat(S, N) generate a string that is string S repeated N times
445 str_repeat() {
446         local s=$1
447         local n=$2
448         local ret=''
449         while [ $((n -= 1)) -ge 0 ]; do
450                 ret=$ret$s
451         done
452         echo $ret
453 }
454
455 # Long symlinks and LU-2241
456 test_17g() {
457         test_mkdir -p $DIR/$tdir
458         local TESTS="59 60 61 4094 4095"
459
460         # Fix for inode size boundary in 2.1.4
461         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
462                 TESTS="4094 4095"
463
464         # Patch not applied to 2.2 or 2.3 branches
465         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
466         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
467                 TESTS="4094 4095"
468
469         # skip long symlink name for rhel6.5.
470         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
471         grep -q '6.5' /etc/redhat-release &>/dev/null &&
472                 TESTS="59 60 61 4062 4063"
473
474         for i in $TESTS; do
475                 local SYMNAME=$(str_repeat 'x' $i)
476                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
477                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
478         done
479 }
480 run_test 17g "symlinks: really long symlink name and inode boundaries"
481
482 test_17h() { #bug 17378
483         remote_mds_nodsh && skip "remote MDS with nodsh" && return
484         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
485         local mdt_idx
486         test_mkdir -p $DIR/$tdir
487         if [[ $MDSCOUNT -gt 1 ]]; then
488                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
489         else
490                 mdt_idx=0
491         fi
492         $SETSTRIPE -c -1 $DIR/$tdir
493 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
494         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
495         touch $DIR/$tdir/$tfile || true
496 }
497 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
498
499 test_17i() { #bug 20018
500         remote_mds_nodsh && skip "remote MDS with nodsh" && return
501         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
502         test_mkdir -c1 $DIR/$tdir
503         local foo=$DIR/$tdir/$tfile
504         local mdt_idx
505         if [[ $MDSCOUNT -gt 1 ]]; then
506                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
507         else
508                 mdt_idx=0
509         fi
510         ln -s $foo $foo || error "create symlink failed"
511 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
512         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
513         ls -l $foo && error "error not detected"
514         return 0
515 }
516 run_test 17i "don't panic on short symlink"
517
518 test_17k() { #bug 22301
519         [[ -z "$(which rsync 2>/dev/null)" ]] &&
520                 skip "no rsync command" && return 0
521         rsync --help | grep -q xattr ||
522                 skip_env "$(rsync --version | head -n1) does not support xattrs"
523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
524         test_mkdir -p $DIR/$tdir
525         test_mkdir -p $DIR/$tdir.new
526         touch $DIR/$tdir/$tfile
527         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
528         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
529                 error "rsync failed with xattrs enabled"
530 }
531 run_test 17k "symlinks: rsync with xattrs enabled ========================="
532
533 test_17l() { # LU-279
534         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
535                 skip "no getfattr command" && return 0
536         mkdir -p $DIR/$tdir
537         touch $DIR/$tdir/$tfile
538         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
539         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
540                 # -h to not follow symlinks. -m '' to list all the xattrs.
541                 # grep to remove first line: '# file: $path'.
542                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
543                 do
544                         lgetxattr_size_check $path $xattr ||
545                                 error "lgetxattr_size_check $path $xattr failed"
546                 done
547         done
548 }
549 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
550
551 # LU-1540
552 test_17m() {
553         local short_sym="0123456789"
554         local WDIR=$DIR/${tdir}m
555         local mds_index
556         local devname
557         local cmd
558         local i
559         local rc=0
560
561         remote_mds_nodsh && skip "remote MDS with nodsh" && return
562         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
563         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
564                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
565
566         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
567                 skip "only for ldiskfs MDT" && return 0
568
569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
570
571         mkdir -p $WDIR
572         long_sym=$short_sym
573         # create a long symlink file
574         for ((i = 0; i < 4; ++i)); do
575                 long_sym=${long_sym}${long_sym}
576         done
577
578         echo "create 512 short and long symlink files under $WDIR"
579         for ((i = 0; i < 256; ++i)); do
580                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
581                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
582         done
583
584         echo "erase them"
585         rm -f $WDIR/*
586         sync
587         wait_delete_completed
588
589         echo "recreate the 512 symlink files with a shorter string"
590         for ((i = 0; i < 512; ++i)); do
591                 # rewrite the symlink file with a shorter string
592                 ln -sf ${long_sym} $WDIR/long-$i
593                 ln -sf ${short_sym} $WDIR/short-$i
594         done
595
596         mds_index=$($LFS getstripe -M $WDIR)
597         mds_index=$((mds_index+1))
598         devname=$(mdsdevname $mds_index)
599         cmd="$E2FSCK -fnvd $devname"
600
601         echo "stop and checking mds${mds_index}: $cmd"
602         # e2fsck should not return error
603         stop mds${mds_index}
604         do_facet mds${mds_index} $cmd || rc=$?
605
606         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
607         df $MOUNT > /dev/null 2>&1
608         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
609                 "short/long symlink MDT: rc=$rc"
610         return $rc
611 }
612 run_test 17m "run e2fsck against MDT which contains short/long symlink"
613
614 check_fs_consistency_17n() {
615         local mdt_index
616         local devname
617         local cmd
618         local rc=0
619
620         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
621         # so it only check MDT1/MDT2 instead of all of MDTs.
622         for mdt_index in $(seq 1 2); do
623                 devname=$(mdsdevname $mdt_index)
624                 cmd="$E2FSCK -fnvd $devname"
625
626                 echo "stop and checking mds${mdt_index}: $cmd"
627                 # e2fsck should not return error
628                 stop mds${mdt_index}
629                 do_facet mds${mdt_index} $cmd || rc=$?
630
631                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
632                         error "mount mds${mdt_index} failed"
633                 df $MOUNT > /dev/null 2>&1
634                 [ $rc -ne 0 ] && break
635         done
636         return $rc
637 }
638
639 test_17n() {
640         local i
641
642         remote_mds_nodsh && skip "remote MDS with nodsh" && return
643         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
644         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
645                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
646
647         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
648                 skip "only for ldiskfs MDT" && return 0
649
650         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
651
652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
653
654         mkdir $DIR/$tdir
655         for ((i=0; i<10; i++)); do
656                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
657                         error "create remote dir error $i"
658                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
659                         error "create files under remote dir failed $i"
660         done
661
662         check_fs_consistency_17n ||
663                 error "e2fsck report error after create files under remote dir"
664
665         for ((i=0;i<10;i++)); do
666                 rm -rf $DIR/$tdir/remote_dir_${i} ||
667                         error "destroy remote dir error $i"
668         done
669
670         check_fs_consistency_17n ||
671                 error "e2fsck report error after unlink files under remote dir"
672
673         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
674                 skip "lustre < 2.4.50 does not support migrate mv " && return
675
676         for ((i=0; i<10; i++)); do
677                 mkdir -p $DIR/$tdir/remote_dir_${i}
678                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
679                         error "create files under remote dir failed $i"
680                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
681                         error "migrate remote dir error $i"
682         done
683         check_fs_consistency_17n || error "e2fsck report error after migration"
684
685         for ((i=0;i<10;i++)); do
686                 rm -rf $DIR/$tdir/remote_dir_${i} ||
687                         error "destroy remote dir error $i"
688         done
689
690         check_fs_consistency_17n || error "e2fsck report error after unlink"
691 }
692 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
693
694 test_17o() {
695         remote_mds_nodsh && skip "remote MDS with nodsh" && return
696         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
697                 skip "Need MDS version at least 2.3.64" && return
698
699         local WDIR=$DIR/${tdir}o
700         local mdt_index
701         local mdtdevname
702         local rc=0
703
704         mkdir -p $WDIR
705         mdt_index=$($LFS getstripe -M $WDIR)
706         mdt_index=$((mdt_index+1))
707         mdtdevname=$(mdsdevname $mdt_index)
708
709         touch $WDIR/$tfile
710         stop mds${mdt_index}
711         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS ||
712                 error "mount mds${mdt_index} failed"
713
714         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
715         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
716         ls -l $WDIR/$tfile && rc=1
717         do_facet mds${mdt_index} lctl set_param fail_loc=0
718         [[ $rc -ne 0 ]] && error "stat file should fail"
719         true
720 }
721 run_test 17o "stat file with incompat LMA feature"
722
723 test_18() {
724         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
725         ls $DIR || error "Failed to ls $DIR: $?"
726 }
727 run_test 18 "touch .../f ; ls ... =============================="
728
729 test_19a() {
730         touch $DIR/$tfile
731         ls -l $DIR
732         rm $DIR/$tfile
733         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
734 }
735 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
736
737 test_19b() {
738         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
739 }
740 run_test 19b "ls -l .../f19 (should return error) =============="
741
742 test_19c() {
743         [ $RUNAS_ID -eq $UID ] &&
744                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
745         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
746 }
747 run_test 19c "$RUNAS touch .../f19 (should return error) =="
748
749 test_19d() {
750         cat $DIR/f19 && error || true
751 }
752 run_test 19d "cat .../f19 (should return error) =============="
753
754 test_20() {
755         touch $DIR/$tfile
756         rm $DIR/$tfile
757         log "1 done"
758         touch $DIR/$tfile
759         rm $DIR/$tfile
760         log "2 done"
761         touch $DIR/$tfile
762         rm $DIR/$tfile
763         log "3 done"
764         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
765 }
766 run_test 20 "touch .../f ; ls -l ... ==========================="
767
768 test_21() {
769         test_mkdir -p $DIR/$tdir
770         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
771         ln -s dangle $DIR/$tdir/link
772         echo foo >> $DIR/$tdir/link
773         cat $DIR/$tdir/dangle
774         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
775         $CHECKSTAT -f -t file $DIR/$tdir/link ||
776                 error "$tdir/link not linked to a file"
777 }
778 run_test 21 "write to dangling link ============================"
779
780 test_22() {
781         WDIR=$DIR/$tdir
782         test_mkdir -p $DIR/$tdir
783         chown $RUNAS_ID:$RUNAS_GID $WDIR
784         (cd $WDIR || error "cd $WDIR failed";
785         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
786         $RUNAS tar xf -)
787         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
788         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
789         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
790 }
791 run_test 22 "unpack tar archive as non-root user ==============="
792
793 # was test_23
794 test_23a() {
795         test_mkdir -p $DIR/$tdir
796         local file=$DIR/$tdir/$tfile
797
798         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
799         openfile -f O_CREAT:O_EXCL $file &&
800                 error "$file recreate succeeded" || true
801 }
802 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
803
804 test_23b() { # bug 18988
805         test_mkdir -p $DIR/$tdir
806         local file=$DIR/$tdir/$tfile
807
808         rm -f $file
809         echo foo > $file || error "write filed"
810         echo bar >> $file || error "append filed"
811         $CHECKSTAT -s 8 $file || error "wrong size"
812         rm $file
813 }
814 run_test 23b "O_APPEND check =========================="
815
816 # rename sanity
817 test_24a() {
818         echo '-- same directory rename'
819         test_mkdir $DIR/$tdir
820         touch $DIR/$tdir/$tfile.1
821         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
822         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
823 }
824 run_test 24a "rename file to non-existent target"
825
826 test_24b() {
827         test_mkdir $DIR/$tdir
828         touch $DIR/$tdir/$tfile.{1,2}
829         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
830         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
831         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
832 }
833 run_test 24b "rename file to existing target"
834
835 test_24c() {
836         test_mkdir $DIR/$tdir
837         test_mkdir $DIR/$tdir/d$testnum.1
838         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
839         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
840         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
841 }
842 run_test 24c "rename directory to non-existent target"
843
844 test_24d() {
845         test_mkdir -c1 $DIR/$tdir
846         test_mkdir -c1 $DIR/$tdir/d$testnum.1
847         test_mkdir -c1 $DIR/$tdir/d$testnum.2
848         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
849         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
850         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
851 }
852 run_test 24d "rename directory to existing target"
853
854 test_24e() {
855         echo '-- cross directory renames --'
856         test_mkdir $DIR/R5a
857         test_mkdir $DIR/R5b
858         touch $DIR/R5a/f
859         mv $DIR/R5a/f $DIR/R5b/g
860         $CHECKSTAT -a $DIR/R5a/f || error
861         $CHECKSTAT -t file $DIR/R5b/g || error
862 }
863 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
864
865 test_24f() {
866         test_mkdir $DIR/R6a
867         test_mkdir $DIR/R6b
868         touch $DIR/R6a/f $DIR/R6b/g
869         mv $DIR/R6a/f $DIR/R6b/g
870         $CHECKSTAT -a $DIR/R6a/f || error
871         $CHECKSTAT -t file $DIR/R6b/g || error
872 }
873 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
874
875 test_24g() {
876         test_mkdir $DIR/R7a
877         test_mkdir $DIR/R7b
878         test_mkdir $DIR/R7a/d
879         mv $DIR/R7a/d $DIR/R7b/e
880         $CHECKSTAT -a $DIR/R7a/d || error
881         $CHECKSTAT -t dir $DIR/R7b/e || error
882 }
883 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
884
885 test_24h() {
886         test_mkdir -c1 $DIR/R8a
887         test_mkdir -c1 $DIR/R8b
888         test_mkdir -c1 $DIR/R8a/d
889         test_mkdir -c1 $DIR/R8b/e
890         mrename $DIR/R8a/d $DIR/R8b/e
891         $CHECKSTAT -a $DIR/R8a/d || error
892         $CHECKSTAT -t dir $DIR/R8b/e || error
893 }
894 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
895
896 test_24i() {
897         echo "-- rename error cases"
898         test_mkdir $DIR/R9
899         test_mkdir $DIR/R9/a
900         touch $DIR/R9/f
901         mrename $DIR/R9/f $DIR/R9/a
902         $CHECKSTAT -t file $DIR/R9/f || error
903         $CHECKSTAT -t dir  $DIR/R9/a || error
904         $CHECKSTAT -a $DIR/R9/a/f || error
905 }
906 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
907
908 test_24j() {
909         test_mkdir $DIR/R10
910         mrename $DIR/R10/f $DIR/R10/g
911         $CHECKSTAT -t dir $DIR/R10 || error
912         $CHECKSTAT -a $DIR/R10/f || error
913         $CHECKSTAT -a $DIR/R10/g || error
914 }
915 run_test 24j "source does not exist ============================"
916
917 test_24k() {
918         test_mkdir $DIR/R11a
919         test_mkdir $DIR/R11a/d
920         touch $DIR/R11a/f
921         mv $DIR/R11a/f $DIR/R11a/d
922         $CHECKSTAT -a $DIR/R11a/f || error
923         $CHECKSTAT -t file $DIR/R11a/d/f || error
924 }
925 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
926
927 # bug 2429 - rename foo foo foo creates invalid file
928 test_24l() {
929         f="$DIR/f24l"
930         $MULTIOP $f OcNs || error
931 }
932 run_test 24l "Renaming a file to itself ========================"
933
934 test_24m() {
935         f="$DIR/f24m"
936         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
937         # on ext3 this does not remove either the source or target files
938         # though the "expected" operation would be to remove the source
939         $CHECKSTAT -t file ${f} || error "${f} missing"
940         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
941 }
942 run_test 24m "Renaming a file to a hard link to itself ========="
943
944 test_24n() {
945     f="$DIR/f24n"
946     # this stats the old file after it was renamed, so it should fail
947     touch ${f}
948     $CHECKSTAT ${f}
949     mv ${f} ${f}.rename
950     $CHECKSTAT ${f}.rename
951     $CHECKSTAT -a ${f}
952 }
953 run_test 24n "Statting the old file after renaming (Posix rename 2)"
954
955 test_24o() {
956         check_kernel_version 37 || return 0
957         test_mkdir -p $DIR/d24o
958         rename_many -s random -v -n 10 $DIR/d24o
959 }
960 run_test 24o "rename of files during htree split ==============="
961
962 test_24p() {
963         test_mkdir $DIR/R12a
964         test_mkdir $DIR/R12b
965         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
966         mrename $DIR/R12a $DIR/R12b
967         $CHECKSTAT -a $DIR/R12a || error
968         $CHECKSTAT -t dir $DIR/R12b || error
969         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
970         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
971 }
972 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
973
974 cleanup_multiop_pause() {
975         trap 0
976         kill -USR1 $MULTIPID
977 }
978
979 test_24q() {
980         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
981         test_mkdir $DIR/R13a
982         test_mkdir $DIR/R13b
983         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
984         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
985         MULTIPID=$!
986
987         trap cleanup_multiop_pause EXIT
988         mrename $DIR/R13a $DIR/R13b
989         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
990         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
991         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
992         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
993         cleanup_multiop_pause
994         wait $MULTIPID || error "multiop close failed"
995 }
996 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
997
998 test_24r() { #bug 3789
999         test_mkdir $DIR/R14a
1000         test_mkdir $DIR/R14a/b
1001         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1002         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1003         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1004 }
1005 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1006
1007 test_24s() {
1008         test_mkdir $DIR/R15a
1009         test_mkdir $DIR/R15a/b
1010         test_mkdir $DIR/R15a/b/c
1011         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1012         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1013         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1014 }
1015 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1016 test_24t() {
1017         test_mkdir $DIR/R16a
1018         test_mkdir $DIR/R16a/b
1019         test_mkdir $DIR/R16a/b/c
1020         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1021         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1022         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1023 }
1024 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1025
1026 test_24u() { # bug12192
1027         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1028         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1029 }
1030 run_test 24u "create stripe file"
1031
1032 page_size() {
1033         getconf PAGE_SIZE
1034 }
1035
1036 simple_cleanup_common() {
1037         trap 0
1038         rm -rf $DIR/$tdir
1039         wait_delete_completed
1040 }
1041
1042 max_pages_per_rpc() {
1043         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1044 }
1045
1046 test_24v() {
1047         local NRFILES=100000
1048         local FREE_INODES=$(mdt_free_inodes 0)
1049         [[ $FREE_INODES -lt $NRFILES ]] &&
1050                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1051                 return
1052
1053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1054         trap simple_cleanup_common EXIT
1055
1056         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1057         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1058
1059         mkdir -p $DIR/$tdir
1060         createmany -m $DIR/$tdir/$tfile $NRFILES
1061
1062         cancel_lru_locks mdc
1063         lctl set_param mdc.*.stats clear
1064
1065         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1066
1067         # LU-5 large readdir
1068         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1069         #               8 bytes for name(filename is mostly 5 in this test) +
1070         #               8 bytes for luda_type
1071         # take into account of overhead in lu_dirpage header and end mark in
1072         # each page, plus one in RPC_NUM calculation.
1073         DIRENT_SIZE=48
1074         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1075         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1076         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1077                                 awk '/^mds_readpage/ {print $2}')
1078         [[ $mds_readpage -gt $RPC_NUM ]] &&
1079                 error "large readdir doesn't take effect"
1080
1081         simple_cleanup_common
1082 }
1083 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1084
1085 test_24w() { # bug21506
1086         SZ1=234852
1087         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1088         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1089         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1090         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1091         [[ "$SZ1" -eq "$SZ2" ]] ||
1092                 error "Error reading at the end of the file $tfile"
1093 }
1094 run_test 24w "Reading a file larger than 4Gb"
1095
1096 test_24x() {
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 $DIR/$tdir/src_dir
1107         touch $DIR/$tdir/src_file
1108         mkdir -p $remote_dir/tgt_dir
1109         touch $remote_dir/tgt_file
1110
1111         mrename $remote_dir $DIR/ &&
1112                 error "rename dir cross MDT works!"
1113
1114         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1115                 error "rename dir cross MDT works!"
1116
1117         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1118                 error "rename file cross MDT works!"
1119
1120         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1121                 error "ln file cross MDT should not work!"
1122
1123         rm -rf $DIR/$tdir || error "Can not delete directories"
1124 }
1125 run_test 24x "cross rename/link should be failed"
1126
1127 test_24y() {
1128         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1130         local MDTIDX=1
1131         local remote_dir=$DIR/$tdir/remote_dir
1132
1133         mkdir -p $DIR/$tdir
1134         $LFS mkdir -i $MDTIDX $remote_dir ||
1135                    error "create remote directory failed"
1136
1137         mkdir -p $remote_dir/src_dir
1138         touch $remote_dir/src_file
1139         mkdir -p $remote_dir/tgt_dir
1140         touch $remote_dir/tgt_file
1141
1142         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1143                 error "rename subdir in the same remote dir failed!"
1144
1145         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1146                 error "rename files in the same remote dir failed!"
1147
1148         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1149                 error "link files in the same remote dir failed!"
1150
1151         rm -rf $DIR/$tdir || error "Can not delete directories"
1152 }
1153 run_test 24y "rename/link on the same dir should succeed"
1154
1155 test_24z() {
1156         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1158         local MDTIDX=1
1159         local remote_src=$DIR/$tdir/remote_dir
1160         local remote_tgt=$DIR/$tdir/remote_tgt
1161
1162         mkdir -p $DIR/$tdir
1163         $LFS mkdir -i $MDTIDX $remote_src ||
1164                    error "create remote directory failed"
1165
1166         $LFS mkdir -i $MDTIDX $remote_tgt ||
1167                    error "create remote directory failed"
1168
1169         mrename $remote_src $remote_tgt &&
1170                 error "rename remote dirs should not work!"
1171
1172         # If target dir does not exists, it should succeed
1173         rm -rf $remote_tgt
1174         mrename $remote_src $remote_tgt ||
1175                 error "rename remote dirs(tgt dir does not exists) failed!"
1176
1177         rm -rf $DIR/$tdir || error "Can not delete directories"
1178 }
1179 run_test 24z "rename one remote dir to another remote dir should fail"
1180
1181 test_24A() { # LU-3182
1182         local NFILES=5000
1183
1184         rm -rf $DIR/$tdir
1185         mkdir -p $DIR/$tdir
1186         createmany -m $DIR/$tdir/$tfile $NFILES
1187         local t=$(ls $DIR/$tdir | wc -l)
1188         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1189         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1190         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1191                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1192         fi
1193
1194         rm -rf $DIR/$tdir || error "Can not delete directories"
1195 }
1196 run_test 24A "readdir() returns correct number of entries."
1197
1198 test_24B() { # LU-4805
1199         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1200         local count
1201
1202         mkdir $DIR/$tdir
1203         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1204                 error "create striped dir failed"
1205
1206         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1207         [ $count -eq 2 ] || error "Expected 2, got $count"
1208
1209         touch $DIR/$tdir/striped_dir/a
1210
1211         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1212         [ $count -eq 3 ] || error "Expected 3, got $count"
1213
1214         touch $DIR/$tdir/striped_dir/.f
1215
1216         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1217         [ $count -eq 4 ] || error "Expected 4, got $count"
1218
1219         rm -rf $DIR/$tdir || error "Can not delete directories"
1220 }
1221 run_test 24B "readdir for striped dir return correct number of entries"
1222
1223 test_24C() {
1224         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1225
1226         mkdir $DIR/$tdir
1227         mkdir $DIR/$tdir/d0
1228         mkdir $DIR/$tdir/d1
1229
1230         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1231                 error "create striped dir failed"
1232
1233         cd $DIR/$tdir/d0/striped_dir
1234
1235         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1236         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1237         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1238
1239         [ "$d0_ino" = "$parent_ino" ] ||
1240                 error ".. wrong, expect $d0_ino, get $parent_ino"
1241
1242         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1243                 error "mv striped dir failed"
1244
1245         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1246
1247         [ "$d1_ino" = "$parent_ino" ] ||
1248                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1249 }
1250 run_test 24C "check .. in striped dir"
1251
1252 test_25a() {
1253         echo '== symlink sanity ============================================='
1254
1255         test_mkdir $DIR/d25
1256         ln -s d25 $DIR/s25
1257         touch $DIR/s25/foo || error
1258 }
1259 run_test 25a "create file in symlinked directory ==============="
1260
1261 test_25b() {
1262         [ ! -d $DIR/d25 ] && test_25a
1263         $CHECKSTAT -t file $DIR/s25/foo || error
1264 }
1265 run_test 25b "lookup file in symlinked directory ==============="
1266
1267 test_26a() {
1268         test_mkdir $DIR/d26
1269         test_mkdir $DIR/d26/d26-2
1270         ln -s d26/d26-2 $DIR/s26
1271         touch $DIR/s26/foo || error
1272 }
1273 run_test 26a "multiple component symlink ======================="
1274
1275 test_26b() {
1276         test_mkdir -p $DIR/d26b/d26-2
1277         ln -s d26b/d26-2/foo $DIR/s26-2
1278         touch $DIR/s26-2 || error
1279 }
1280 run_test 26b "multiple component symlink at end of lookup ======"
1281
1282 test_26c() {
1283         test_mkdir $DIR/d26.2
1284         touch $DIR/d26.2/foo
1285         ln -s d26.2 $DIR/s26.2-1
1286         ln -s s26.2-1 $DIR/s26.2-2
1287         ln -s s26.2-2 $DIR/s26.2-3
1288         chmod 0666 $DIR/s26.2-3/foo
1289 }
1290 run_test 26c "chain of symlinks ================================"
1291
1292 # recursive symlinks (bug 439)
1293 test_26d() {
1294         ln -s d26-3/foo $DIR/d26-3
1295 }
1296 run_test 26d "create multiple component recursive symlink ======"
1297
1298 test_26e() {
1299         [ ! -h $DIR/d26-3 ] && test_26d
1300         rm $DIR/d26-3
1301 }
1302 run_test 26e "unlink multiple component recursive symlink ======"
1303
1304 # recursive symlinks (bug 7022)
1305 test_26f() {
1306         test_mkdir -p $DIR/$tdir
1307         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1308         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1309         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1310         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1311         cd $tfile                || error "cd $tfile failed"
1312         ln -s .. dotdot          || error "ln dotdot failed"
1313         ln -s dotdot/lndir lndir || error "ln lndir failed"
1314         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1315         output=`ls $tfile/$tfile/lndir/bar1`
1316         [ "$output" = bar1 ] && error "unexpected output"
1317         rm -r $tfile             || error "rm $tfile failed"
1318         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1319 }
1320 run_test 26f "rm -r of a directory which has recursive symlink ="
1321
1322 test_27a() {
1323         echo '== stripe sanity =============================================='
1324         test_mkdir -p $DIR/d27 || error "mkdir failed"
1325         $GETSTRIPE $DIR/d27
1326         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1327         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1328         log "== test_27a: write to one stripe file ========================="
1329         cp /etc/hosts $DIR/d27/f0 || error
1330 }
1331 run_test 27a "one stripe file =================================="
1332
1333 test_27b() {
1334         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1335         test_mkdir -p $DIR/d27
1336         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1337         $GETSTRIPE -c $DIR/d27/f01
1338         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1339                 error "two-stripe file doesn't have two stripes"
1340
1341         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1342 }
1343 run_test 27b "create and write to two stripe file"
1344
1345 test_27d() {
1346         test_mkdir -p $DIR/d27
1347         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1348         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1349         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1350 }
1351 run_test 27d "create file with default settings ================"
1352
1353 test_27e() {
1354         test_mkdir -p $DIR/d27
1355         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1356         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1357         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1358 }
1359 run_test 27e "setstripe existing file (should return error) ======"
1360
1361 test_27f() {
1362         test_mkdir $DIR/$tdir
1363         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1364                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1365         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1366                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1367         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1368         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1369 }
1370 run_test 27f "setstripe with bad stripe size (should return error)"
1371
1372 test_27g() {
1373         test_mkdir -p $DIR/d27
1374         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1375         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1376                 error "$DIR/d27/fnone has object"
1377 }
1378 run_test 27g "$GETSTRIPE with no objects"
1379
1380 test_27i() {
1381         touch $DIR/d27/fsome || error "touch failed"
1382         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1383 }
1384 run_test 27i "$GETSTRIPE with some objects"
1385
1386 test_27j() {
1387         test_mkdir -p $DIR/d27
1388         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1389 }
1390 run_test 27j "setstripe with bad stripe offset (should return error)"
1391
1392 test_27k() { # bug 2844
1393         test_mkdir -p $DIR/d27
1394         FILE=$DIR/d27/f27k
1395         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1396         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1397         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1398         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1399         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1400         dd if=/dev/zero of=$FILE bs=4k count=1
1401         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1402         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1403 }
1404 run_test 27k "limit i_blksize for broken user apps ============="
1405
1406 test_27l() {
1407         test_mkdir -p $DIR/d27
1408         mcreate $DIR/f27l || error "creating file"
1409         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1410                 error "setstripe should have failed" || true
1411 }
1412 run_test 27l "check setstripe permissions (should return error)"
1413
1414 test_27m() {
1415         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1416                 return
1417         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1418                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1419                 return
1420         fi
1421         trap simple_cleanup_common EXIT
1422         test_mkdir -p $DIR/$tdir
1423         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1424         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1425                 error "dd should fill OST0"
1426         i=2
1427         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1428                 i=$((i + 1))
1429                 [ $i -gt 256 ] && break
1430         done
1431         i=$((i + 1))
1432         touch $DIR/$tdir/f27m_$i
1433         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1434                 error "OST0 was full but new created file still use it"
1435         i=$((i + 1))
1436         touch $DIR/$tdir/f27m_$i
1437         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1438                 error "OST0 was full but new created file still use it"
1439         simple_cleanup_common
1440 }
1441 run_test 27m "create file while OST0 was full =================="
1442
1443 sleep_maxage() {
1444         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1445         sleep $DELAY
1446 }
1447
1448 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1449 # if the OST isn't full anymore.
1450 reset_enospc() {
1451         local OSTIDX=${1:-""}
1452
1453         local list=$(comma_list $(osts_nodes))
1454         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1455
1456         do_nodes $list lctl set_param fail_loc=0
1457         sync    # initiate all OST_DESTROYs from MDS to OST
1458         sleep_maxage
1459 }
1460
1461 exhaust_precreations() {
1462         local OSTIDX=$1
1463         local FAILLOC=$2
1464         local FAILIDX=${3:-$OSTIDX}
1465
1466         test_mkdir -p $DIR/$tdir
1467         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1468         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1469
1470         local OST=$(ostname_from_index $OSTIDX)
1471         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1472                           sed -e 's/_UUID$//;s/^.*-//')
1473
1474         # on the mdt's osc
1475         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1476         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1477                         osc.$mdtosc_proc1.prealloc_last_id)
1478         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1479                         osc.$mdtosc_proc1.prealloc_next_id)
1480
1481         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1482         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1483
1484         test_mkdir -p $DIR/$tdir/${OST}
1485         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1486 #define OBD_FAIL_OST_ENOSPC              0x215
1487         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1488         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1489         echo "Creating to objid $last_id on ost $OST..."
1490         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1491         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1492         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1493         sleep_maxage
1494 }
1495
1496 exhaust_all_precreations() {
1497         local i
1498         for (( i=0; i < OSTCOUNT; i++ )) ; do
1499                 exhaust_precreations $i $1 -1
1500         done
1501 }
1502
1503 test_27n() {
1504         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1506         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1507         remote_ost_nodsh && skip "remote OST with nodsh" && return
1508
1509         reset_enospc
1510         rm -f $DIR/$tdir/$tfile
1511         exhaust_precreations 0 0x80000215
1512         $SETSTRIPE -c -1 $DIR/$tdir
1513         touch $DIR/$tdir/$tfile || error
1514         $GETSTRIPE $DIR/$tdir/$tfile
1515         reset_enospc
1516 }
1517 run_test 27n "create file with some full OSTs =================="
1518
1519 test_27o() {
1520         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1522         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1523         remote_ost_nodsh && skip "remote OST with nodsh" && return
1524
1525         reset_enospc
1526         rm -f $DIR/$tdir/$tfile
1527         exhaust_all_precreations 0x215
1528
1529         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1530
1531         reset_enospc
1532         rm -rf $DIR/$tdir/*
1533 }
1534 run_test 27o "create file with all full OSTs (should error) ===="
1535
1536 test_27p() {
1537         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1539         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1540         remote_ost_nodsh && skip "remote OST with nodsh" && return
1541
1542         reset_enospc
1543         rm -f $DIR/$tdir/$tfile
1544         test_mkdir -p $DIR/$tdir
1545
1546         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1547         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1548         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1549
1550         exhaust_precreations 0 0x80000215
1551         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1552         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1553         $GETSTRIPE $DIR/$tdir/$tfile
1554
1555         reset_enospc
1556 }
1557 run_test 27p "append to a truncated file with some full OSTs ==="
1558
1559 test_27q() {
1560         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1561         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1562         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1563         remote_ost_nodsh && skip "remote OST with nodsh" && return
1564
1565         reset_enospc
1566         rm -f $DIR/$tdir/$tfile
1567
1568         test_mkdir -p $DIR/$tdir
1569         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1570         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1571         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1572
1573         exhaust_all_precreations 0x215
1574
1575         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1576         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1577
1578         reset_enospc
1579 }
1580 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1581
1582 test_27r() {
1583         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1585         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1586         remote_ost_nodsh && skip "remote OST with nodsh" && return
1587
1588         reset_enospc
1589         rm -f $DIR/$tdir/$tfile
1590         exhaust_precreations 0 0x80000215
1591
1592         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1593
1594         reset_enospc
1595 }
1596 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1597
1598 test_27s() { # bug 10725
1599         test_mkdir -p $DIR/$tdir
1600         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1601         local stripe_count=0
1602         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1603         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1604                 error "stripe width >= 2^32 succeeded" || true
1605
1606 }
1607 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1608
1609 test_27t() { # bug 10864
1610         WDIR=$(pwd)
1611         WLFS=$(which lfs)
1612         cd $DIR
1613         touch $tfile
1614         $WLFS getstripe $tfile
1615         cd $WDIR
1616 }
1617 run_test 27t "check that utils parse path correctly"
1618
1619 test_27u() { # bug 4900
1620         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1621         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1622         local index
1623         local list=$(comma_list $(mdts_nodes))
1624
1625 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1626         do_nodes $list $LCTL set_param fail_loc=0x139
1627         test_mkdir -p $DIR/$tdir
1628         rm -rf $DIR/$tdir/*
1629         createmany -o $DIR/$tdir/t- 1000
1630         do_nodes $list $LCTL set_param fail_loc=0
1631
1632         TLOG=$DIR/$tfile.getstripe
1633         $GETSTRIPE $DIR/$tdir > $TLOG
1634         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1635         unlinkmany $DIR/$tdir/t- 1000
1636         [[ $OBJS -gt 0 ]] &&
1637                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1638 }
1639 run_test 27u "skip object creation on OSC w/o objects =========="
1640
1641 test_27v() { # bug 4900
1642         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1644         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1645         remote_ost_nodsh && skip "remote OST with nodsh" && return
1646
1647         exhaust_all_precreations 0x215
1648         reset_enospc
1649
1650         test_mkdir -p $DIR/$tdir
1651         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1652
1653         touch $DIR/$tdir/$tfile
1654         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1655         # all except ost1
1656         for (( i=1; i < OSTCOUNT; i++ )); do
1657                 do_facet ost$i lctl set_param fail_loc=0x705
1658         done
1659         local START=`date +%s`
1660         createmany -o $DIR/$tdir/$tfile 32
1661
1662         local FINISH=`date +%s`
1663         local TIMEOUT=`lctl get_param -n timeout`
1664         local PROCESS=$((FINISH - START))
1665         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1666                error "$FINISH - $START >= $TIMEOUT / 2"
1667         sleep $((TIMEOUT / 2 - PROCESS))
1668         reset_enospc
1669 }
1670 run_test 27v "skip object creation on slow OST ================="
1671
1672 test_27w() { # bug 10997
1673         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1674         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1675         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1676                 error "stripe size $size != 65536" || true
1677         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1678                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1679 }
1680 run_test 27w "check $SETSTRIPE -S option"
1681
1682 test_27wa() {
1683         [[ $OSTCOUNT -lt 2 ]] &&
1684                 skip_env "skipping multiple stripe count/offset test" && return
1685
1686         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1687         for i in $(seq 1 $OSTCOUNT); do
1688                 offset=$((i - 1))
1689                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1690                         error "setstripe -c $i -i $offset failed"
1691                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1692                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1693                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1694                 [ $index -ne $offset ] &&
1695                         error "stripe offset $index != $offset" || true
1696         done
1697 }
1698 run_test 27wa "check $SETSTRIPE -c -i options"
1699
1700 test_27x() {
1701         remote_ost_nodsh && skip "remote OST with nodsh" && return
1702         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1703         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1704         OFFSET=$(($OSTCOUNT - 1))
1705         OSTIDX=0
1706         local OST=$(ostname_from_index $OSTIDX)
1707
1708         test_mkdir -p $DIR/$tdir
1709         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1710         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1711         sleep_maxage
1712         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1713         for i in `seq 0 $OFFSET`; do
1714                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1715                 error "OST0 was degraded but new created file still use it"
1716         done
1717         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1718 }
1719 run_test 27x "create files while OST0 is degraded"
1720
1721 test_27y() {
1722         [[ $OSTCOUNT -lt 2 ]] &&
1723                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1724         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1725         remote_ost_nodsh && skip "remote OST with nodsh" && return
1726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1727
1728         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1729         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1730             osc.$mdtosc.prealloc_last_id)
1731         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1732             osc.$mdtosc.prealloc_next_id)
1733         local fcount=$((last_id - next_id))
1734         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1735         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1736
1737         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1738                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1739         local OST_DEACTIVE_IDX=-1
1740         local OSC
1741         local OSTIDX
1742         local OST
1743
1744         for OSC in $MDS_OSCS; do
1745                 OST=$(osc_to_ost $OSC)
1746                 OSTIDX=$(index_from_ostuuid $OST)
1747                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1748                         OST_DEACTIVE_IDX=$OSTIDX
1749                 fi
1750                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1751                         echo $OSC "is Deactivated:"
1752                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1753                 fi
1754         done
1755
1756         OSTIDX=$(index_from_ostuuid $OST)
1757         mkdir -p $DIR/$tdir
1758         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1759
1760         for OSC in $MDS_OSCS; do
1761                 OST=$(osc_to_ost $OSC)
1762                 OSTIDX=$(index_from_ostuuid $OST)
1763                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1764                         echo $OST "is degraded:"
1765                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1766                                                 obdfilter.$OST.degraded=1
1767                 fi
1768         done
1769
1770         sleep_maxage
1771         createmany -o $DIR/$tdir/$tfile $fcount
1772
1773         for OSC in $MDS_OSCS; do
1774                 OST=$(osc_to_ost $OSC)
1775                 OSTIDX=$(index_from_ostuuid $OST)
1776                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1777                         echo $OST "is recovered from degraded:"
1778                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1779                                                 obdfilter.$OST.degraded=0
1780                 else
1781                         do_facet $SINGLEMDS lctl --device %$OSC activate
1782                 fi
1783         done
1784
1785         # all osp devices get activated, hence -1 stripe count restored
1786         local stripecnt=0
1787
1788         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1789         # devices get activated.
1790         sleep_maxage
1791         $SETSTRIPE -c -1 $DIR/$tfile
1792         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1793         rm -f $DIR/$tfile
1794         [ $stripecnt -ne $OSTCOUNT ] &&
1795                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1796         return 0
1797 }
1798 run_test 27y "create files while OST0 is degraded and the rest inactive"
1799
1800 check_seq_oid()
1801 {
1802         log "check file $1"
1803
1804         lmm_count=$($GETSTRIPE -c $1)
1805         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1806         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1807
1808         local old_ifs="$IFS"
1809         IFS=$'[:]'
1810         fid=($($LFS path2fid $1))
1811         IFS="$old_ifs"
1812
1813         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1814         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1815
1816         # compare lmm_seq and lu_fid->f_seq
1817         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1818         # compare lmm_object_id and lu_fid->oid
1819         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1820
1821         # check the trusted.fid attribute of the OST objects of the file
1822         local have_obdidx=false
1823         local stripe_nr=0
1824         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1825                 # skip lines up to and including "obdidx"
1826                 [ -z "$obdidx" ] && break
1827                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1828                 $have_obdidx || continue
1829
1830                 local ost=$((obdidx + 1))
1831                 local dev=$(ostdevname $ost)
1832                 local oid_hex
1833
1834                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1835
1836                 seq=$(echo $seq | sed -e "s/^0x//g")
1837                 if [ $seq == 0 ]; then
1838                         oid_hex=$(echo $oid)
1839                 else
1840                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1841                 fi
1842                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1843
1844                 local ff
1845                 #
1846                 # Don't unmount/remount the OSTs if we don't need to do that.
1847                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1848                 # update too, until that use mount/ll_decode_filter_fid/mount.
1849                 # Re-enable when debugfs will understand new filter_fid.
1850                 #
1851                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1852                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1853                                 $dev 2>/dev/null" | grep "parent=")
1854                 else
1855                         stop ost$ost
1856                         mount_fstype ost$ost
1857                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1858                                 $(facet_mntpt ost$ost)/$obj_file)
1859                         unmount_fstype ost$ost
1860                         start ost$ost $dev $OST_MOUNT_OPTS
1861                 fi
1862
1863                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1864
1865                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1866
1867                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1868                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1869                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1870                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1871                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1872                 local ff_pstripe
1873                 if echo $ff_parent | grep -q 'stripe='; then
1874                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1875                 else
1876                         #
1877                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1878                         # into f_ver in this case.  See the comment on
1879                         # ff_parent.
1880                         #
1881                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1882                                 sed -e 's/\]//')
1883                 fi
1884
1885                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1886                 [ $ff_pseq = $lmm_seq ] ||
1887                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1888                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1889                 [ $ff_poid = $lmm_oid ] ||
1890                         error "FF parent OID $ff_poid != $lmm_oid"
1891                 (($ff_pstripe == $stripe_nr)) ||
1892                         error "FF stripe $ff_pstripe != $stripe_nr"
1893
1894                 stripe_nr=$((stripe_nr + 1))
1895         done
1896 }
1897
1898 test_27z() {
1899         remote_ost_nodsh && skip "remote OST with nodsh" && return
1900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1901         test_mkdir -p $DIR/$tdir
1902
1903         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1904                 { error "setstripe -c -1 failed"; return 1; }
1905         # We need to send a write to every object to get parent FID info set.
1906         # This _should_ also work for setattr, but does not currently.
1907         # touch $DIR/$tdir/$tfile-1 ||
1908         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1909                 { error "dd $tfile-1 failed"; return 2; }
1910         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1911                 { error "setstripe -c -1 failed"; return 3; }
1912         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1913                 { error "dd $tfile-2 failed"; return 4; }
1914
1915         # make sure write RPCs have been sent to OSTs
1916         sync; sleep 5; sync
1917
1918         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1919         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1920 }
1921 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1922
1923 test_27A() { # b=19102
1924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1925         local restore_size=$($GETSTRIPE -S $MOUNT)
1926         local restore_count=$($GETSTRIPE -c $MOUNT)
1927         local restore_offset=$($GETSTRIPE -i $MOUNT)
1928         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1929         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1930                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1931         local default_size=$($GETSTRIPE -S $MOUNT)
1932         local default_offset=$($GETSTRIPE -i $MOUNT)
1933         local dsize=$((1024 * 1024))
1934         [ $default_size -eq $dsize ] ||
1935                 error "stripe size $default_size != $dsize"
1936         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1937         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1938 }
1939 run_test 27A "check filesystem-wide default LOV EA values"
1940
1941 test_27B() { # LU-2523
1942         test_mkdir -p $DIR/$tdir
1943         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1944         touch $DIR/$tdir/f0
1945         # open f1 with O_LOV_DELAY_CREATE
1946         # rename f0 onto f1
1947         # call setstripe ioctl on open file descriptor for f1
1948         # close
1949         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1950                 $DIR/$tdir/f0
1951
1952         rm -f $DIR/$tdir/f1
1953         # open f1 with O_LOV_DELAY_CREATE
1954         # unlink f1
1955         # call setstripe ioctl on open file descriptor for f1
1956         # close
1957         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1958
1959         # Allow multiop to fail in imitation of NFS's busted semantics.
1960         true
1961 }
1962 run_test 27B "call setstripe on open unlinked file/rename victim"
1963
1964 test_27C() { #LU-2871
1965         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1966
1967         declare -a ost_idx
1968         local index
1969         local found
1970         local i
1971         local j
1972
1973         test_mkdir -p $DIR/$tdir
1974         cd $DIR/$tdir
1975         for i in $(seq 0 $((OSTCOUNT - 1))); do
1976                 # set stripe across all OSTs starting from OST$i
1977                 $SETSTRIPE -i $i -c -1 $tfile$i
1978                 # get striping information
1979                 ost_idx=($($GETSTRIPE $tfile$i |
1980                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1981                 echo ${ost_idx[@]}
1982
1983                 # check the layout
1984                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1985                         error "${#ost_idx[@]} != $OSTCOUNT"
1986
1987                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1988                         found=0
1989                         for j in $(echo ${ost_idx[@]}); do
1990                                 if [ $index -eq $j ]; then
1991                                         found=1
1992                                         break
1993                                 fi
1994                         done
1995                         [ $found = 1 ] ||
1996                                 error "Can not find $index in ${ost_idx[@]}"
1997                 done
1998         done
1999 }
2000 run_test 27C "check full striping across all OSTs"
2001
2002 test_27D() {
2003         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2004         local POOL=${POOL:-testpool}
2005         local first_ost=0
2006         local last_ost=$(($OSTCOUNT - 1))
2007         local ost_step=1
2008         local ost_list=$(seq $first_ost $ost_step $last_ost)
2009         local ost_range="$first_ost $last_ost $ost_step"
2010
2011         mkdir -p $DIR/$tdir
2012         pool_add $POOL || error "pool_add failed"
2013         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2014         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2015                 error "llapi_layout_test failed"
2016         cleanup_pools || error "cleanup_pools failed"
2017 }
2018 run_test 27D "validate llapi_layout API"
2019
2020 # createtest also checks that device nodes are created and
2021 # then visible correctly (#2091)
2022 test_28() { # bug 2091
2023         test_mkdir $DIR/d28
2024         $CREATETEST $DIR/d28/ct || error
2025 }
2026 run_test 28 "create/mknod/mkdir with bad file types ============"
2027
2028 test_29() {
2029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2030         cancel_lru_locks mdc
2031         test_mkdir $DIR/d29
2032         touch $DIR/d29/foo
2033         log 'first d29'
2034         ls -l $DIR/d29
2035
2036         declare -i LOCKCOUNTORIG=0
2037         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2038                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2039         done
2040         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2041
2042         declare -i LOCKUNUSEDCOUNTORIG=0
2043         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2044                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2045         done
2046
2047         log 'second d29'
2048         ls -l $DIR/d29
2049         log 'done'
2050
2051         declare -i LOCKCOUNTCURRENT=0
2052         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2053                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2054         done
2055
2056         declare -i LOCKUNUSEDCOUNTCURRENT=0
2057         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2058                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2059         done
2060
2061         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2062                 $LCTL set_param -n ldlm.dump_namespaces ""
2063                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2064                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2065                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2066                 return 2
2067         fi
2068         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2069                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2070                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2071                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2072                 return 3
2073         fi
2074 }
2075 run_test 29 "IT_GETATTR regression  ============================"
2076
2077 test_30a() { # was test_30
2078         cp $(which ls) $DIR || cp /bin/ls $DIR
2079         $DIR/ls / || error
2080         rm $DIR/ls
2081 }
2082 run_test 30a "execute binary from Lustre (execve) =============="
2083
2084 test_30b() {
2085         cp `which ls` $DIR || cp /bin/ls $DIR
2086         chmod go+rx $DIR/ls
2087         $RUNAS $DIR/ls / || error
2088         rm $DIR/ls
2089 }
2090 run_test 30b "execute binary from Lustre as non-root ==========="
2091
2092 test_30c() { # b=22376
2093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2094         cp `which ls` $DIR || cp /bin/ls $DIR
2095         chmod a-rw $DIR/ls
2096         cancel_lru_locks mdc
2097         cancel_lru_locks osc
2098         $RUNAS $DIR/ls / || error
2099         rm -f $DIR/ls
2100 }
2101 run_test 30c "execute binary from Lustre without read perms ===="
2102
2103 test_31a() {
2104         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2105         $CHECKSTAT -a $DIR/f31 || error
2106 }
2107 run_test 31a "open-unlink file =================================="
2108
2109 test_31b() {
2110         touch $DIR/f31 || error
2111         ln $DIR/f31 $DIR/f31b || error
2112         $MULTIOP $DIR/f31b Ouc || error
2113         $CHECKSTAT -t file $DIR/f31 || error
2114 }
2115 run_test 31b "unlink file with multiple links while open ======="
2116
2117 test_31c() {
2118         touch $DIR/f31 || error
2119         ln $DIR/f31 $DIR/f31c || error
2120         multiop_bg_pause $DIR/f31 O_uc || return 1
2121         MULTIPID=$!
2122         $MULTIOP $DIR/f31c Ouc
2123         kill -USR1 $MULTIPID
2124         wait $MULTIPID
2125 }
2126 run_test 31c "open-unlink file with multiple links ============="
2127
2128 test_31d() {
2129         opendirunlink $DIR/d31d $DIR/d31d || error
2130         $CHECKSTAT -a $DIR/d31d || error
2131 }
2132 run_test 31d "remove of open directory ========================="
2133
2134 test_31e() { # bug 2904
2135         check_kernel_version 34 || return 0
2136         openfilleddirunlink $DIR/d31e || error
2137 }
2138 run_test 31e "remove of open non-empty directory ==============="
2139
2140 test_31f() { # bug 4554
2141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2142         set -vx
2143         test_mkdir $DIR/d31f
2144         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2145         cp /etc/hosts $DIR/d31f
2146         ls -l $DIR/d31f
2147         $GETSTRIPE $DIR/d31f/hosts
2148         multiop_bg_pause $DIR/d31f D_c || return 1
2149         MULTIPID=$!
2150
2151         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2152         test_mkdir $DIR/d31f
2153         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2154         cp /etc/hosts $DIR/d31f
2155         ls -l $DIR/d31f
2156         $GETSTRIPE $DIR/d31f/hosts
2157         multiop_bg_pause $DIR/d31f D_c || return 1
2158         MULTIPID2=$!
2159
2160         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2161         wait $MULTIPID || error "first opendir $MULTIPID failed"
2162
2163         sleep 6
2164
2165         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2166         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2167         set +vx
2168 }
2169 run_test 31f "remove of open directory with open-unlink file ==="
2170
2171 test_31g() {
2172         echo "-- cross directory link --"
2173         test_mkdir -c1 $DIR/${tdir}ga
2174         test_mkdir -c1 $DIR/${tdir}gb
2175         touch $DIR/${tdir}ga/f
2176         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2177         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2178         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2179         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2180         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2181 }
2182 run_test 31g "cross directory link==============="
2183
2184 test_31h() {
2185         echo "-- cross directory link --"
2186         test_mkdir -c1 $DIR/${tdir}
2187         test_mkdir -c1 $DIR/${tdir}/dir
2188         touch $DIR/${tdir}/f
2189         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2190         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2191         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2192         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2193         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2194 }
2195 run_test 31h "cross directory link under child==============="
2196
2197 test_31i() {
2198         echo "-- cross directory link --"
2199         test_mkdir -c1 $DIR/$tdir
2200         test_mkdir -c1 $DIR/$tdir/dir
2201         touch $DIR/$tdir/dir/f
2202         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2203         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2204         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2205         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2206         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2207 }
2208 run_test 31i "cross directory link under parent==============="
2209
2210 test_31j() {
2211         test_mkdir -c1 -p $DIR/$tdir
2212         test_mkdir -c1 -p $DIR/$tdir/dir1
2213         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2214         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2215         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2216         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2217         return 0
2218 }
2219 run_test 31j "link for directory==============="
2220
2221 test_31k() {
2222         test_mkdir -c1 -p $DIR/$tdir
2223         touch $DIR/$tdir/s
2224         touch $DIR/$tdir/exist
2225         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2226         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2227         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2228         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2229         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2230         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2231         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2232         return 0
2233 }
2234 run_test 31k "link to file: the same, non-existing, dir==============="
2235
2236 test_31m() {
2237         mkdir $DIR/d31m
2238         touch $DIR/d31m/s
2239         mkdir $DIR/d31m2
2240         touch $DIR/d31m2/exist
2241         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2242         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2243         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2244         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2245         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2246         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2247         return 0
2248 }
2249 run_test 31m "link to file: the same, non-existing, dir==============="
2250
2251 test_31n() {
2252         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2253         nlink=$(stat --format=%h $DIR/$tfile)
2254         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2255         local fd=$(free_fd)
2256         local cmd="exec $fd<$DIR/$tfile"
2257         eval $cmd
2258         cmd="exec $fd<&-"
2259         trap "eval $cmd" EXIT
2260         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2261         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2262         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2263         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2264         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2265         eval $cmd
2266 }
2267 run_test 31n "check link count of unlinked file"
2268
2269 link_one() {
2270         local TEMPNAME=$(mktemp $1_XXXXXX)
2271         mlink $TEMPNAME $1 2> /dev/null &&
2272                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2273         munlink $TEMPNAME
2274 }
2275
2276 test_31o() { # LU-2901
2277         mkdir -p $DIR/$tdir
2278         for LOOP in $(seq 100); do
2279                 rm -f $DIR/$tdir/$tfile*
2280                 for THREAD in $(seq 8); do
2281                         link_one $DIR/$tdir/$tfile.$LOOP &
2282                 done
2283                 wait
2284                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2285                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2286                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2287                         break || true
2288         done
2289 }
2290 run_test 31o "duplicate hard links with same filename"
2291
2292 test_31p() {
2293         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2294
2295         mkdir $DIR/$tdir
2296         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2297         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2298
2299         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2300                 error "open unlink test1 failed"
2301         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2302                 error "open unlink test2 failed"
2303
2304         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2305                 error "test1 still exists"
2306         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2307                 error "test2 still exists"
2308 }
2309 run_test 31p "remove of open striped directory"
2310
2311 cleanup_test32_mount() {
2312         trap 0
2313         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2314 }
2315
2316 test_32a() {
2317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2318         echo "== more mountpoints and symlinks ================="
2319         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2320         trap cleanup_test32_mount EXIT
2321         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2322         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2323         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2324         cleanup_test32_mount
2325 }
2326 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2327
2328 test_32b() {
2329         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2330         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2331         trap cleanup_test32_mount EXIT
2332         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2333         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2334         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2335         cleanup_test32_mount
2336 }
2337 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2338
2339 test_32c() {
2340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2341         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2342         trap cleanup_test32_mount EXIT
2343         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2344         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2345         test_mkdir -p $DIR/$tdir/d2/test_dir
2346         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2347         cleanup_test32_mount
2348 }
2349 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2350
2351 test_32d() {
2352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2353         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2354         trap cleanup_test32_mount EXIT
2355         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2356         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2357         test_mkdir -p $DIR/$tdir/d2/test_dir
2358         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2359         cleanup_test32_mount
2360 }
2361 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2362
2363 test_32e() {
2364         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2365         test_mkdir -p $DIR/d32e/tmp
2366         TMP_DIR=$DIR/d32e/tmp
2367         ln -s $DIR/d32e $TMP_DIR/symlink11
2368         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2369         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2370         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2371 }
2372 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2373
2374 test_32f() {
2375         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2376         test_mkdir -p $DIR/d32f/tmp
2377         TMP_DIR=$DIR/d32f/tmp
2378         ln -s $DIR/d32f $TMP_DIR/symlink11
2379         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2380         ls $DIR/d32f/tmp/symlink11  || error
2381         ls $DIR/d32f/symlink01 || error
2382 }
2383 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2384
2385 test_32g() {
2386         TMP_DIR=$DIR/$tdir/tmp
2387         test_mkdir -p $DIR/$tdir/tmp
2388         test_mkdir $DIR/${tdir}2
2389         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2390         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2391         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2392         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2393         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2394         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2395 }
2396 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2397
2398 test_32h() {
2399         rm -fr $DIR/$tdir $DIR/${tdir}2
2400         TMP_DIR=$DIR/$tdir/tmp
2401         test_mkdir -p $DIR/$tdir/tmp
2402         test_mkdir $DIR/${tdir}2
2403         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2404         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2405         ls $TMP_DIR/symlink12 || error
2406         ls $DIR/$tdir/symlink02  || error
2407 }
2408 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2409
2410 test_32i() {
2411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2412         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2413         trap cleanup_test32_mount EXIT
2414         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2415         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2416         touch $DIR/$tdir/test_file
2417         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2418         cleanup_test32_mount
2419 }
2420 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2421
2422 test_32j() {
2423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2424         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2425         trap cleanup_test32_mount EXIT
2426         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2427         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2428         touch $DIR/$tdir/test_file
2429         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2430         cleanup_test32_mount
2431 }
2432 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2433
2434 test_32k() {
2435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2436         rm -fr $DIR/$tdir
2437         trap cleanup_test32_mount EXIT
2438         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2439         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2440         test_mkdir -p $DIR/$tdir/d2
2441         touch $DIR/$tdir/d2/test_file || error
2442         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2443         cleanup_test32_mount
2444 }
2445 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2446
2447 test_32l() {
2448         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2449         rm -fr $DIR/$tdir
2450         trap cleanup_test32_mount EXIT
2451         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2452         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2453         test_mkdir -p $DIR/$tdir/d2
2454         touch $DIR/$tdir/d2/test_file
2455         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2456         cleanup_test32_mount
2457 }
2458 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2459
2460 test_32m() {
2461         rm -fr $DIR/d32m
2462         test_mkdir -p $DIR/d32m/tmp
2463         TMP_DIR=$DIR/d32m/tmp
2464         ln -s $DIR $TMP_DIR/symlink11
2465         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2466         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2467         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2468 }
2469 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2470
2471 test_32n() {
2472         rm -fr $DIR/d32n
2473         test_mkdir -p $DIR/d32n/tmp
2474         TMP_DIR=$DIR/d32n/tmp
2475         ln -s $DIR $TMP_DIR/symlink11
2476         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2477         ls -l $DIR/d32n/tmp/symlink11  || error
2478         ls -l $DIR/d32n/symlink01 || error
2479 }
2480 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2481
2482 test_32o() {
2483         touch $DIR/$tfile
2484         test_mkdir -p $DIR/d32o/tmp
2485         TMP_DIR=$DIR/d32o/tmp
2486         ln -s $DIR/$tfile $TMP_DIR/symlink12
2487         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2488         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2489         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2490         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2491         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2492 }
2493 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2494
2495 test_32p() {
2496     log 32p_1
2497         rm -fr $DIR/d32p
2498     log 32p_2
2499         rm -f $DIR/$tfile
2500     log 32p_3
2501         touch $DIR/$tfile
2502     log 32p_4
2503         test_mkdir -p $DIR/d32p/tmp
2504     log 32p_5
2505         TMP_DIR=$DIR/d32p/tmp
2506     log 32p_6
2507         ln -s $DIR/$tfile $TMP_DIR/symlink12
2508     log 32p_7
2509         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2510     log 32p_8
2511         cat $DIR/d32p/tmp/symlink12 || error
2512     log 32p_9
2513         cat $DIR/d32p/symlink02 || error
2514     log 32p_10
2515 }
2516 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2517
2518 cleanup_testdir_mount() {
2519         trap 0
2520         $UMOUNT -d $DIR/$tdir
2521 }
2522
2523 test_32q() {
2524         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2525         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2526         trap cleanup_testdir_mount EXIT
2527         test_mkdir -p $DIR/$tdir
2528         touch $DIR/$tdir/under_the_mount
2529         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2530         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2531         cleanup_testdir_mount
2532 }
2533 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2534
2535 test_32r() {
2536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2537         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2538         trap cleanup_testdir_mount EXIT
2539         test_mkdir -p $DIR/$tdir
2540         touch $DIR/$tdir/under_the_mount
2541         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2542         ls $DIR/$tdir | grep -q under_the_mount && error || true
2543         cleanup_testdir_mount
2544 }
2545 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2546
2547 test_33aa() {
2548         rm -f $DIR/$tfile
2549         touch $DIR/$tfile
2550         chmod 444 $DIR/$tfile
2551         chown $RUNAS_ID $DIR/$tfile
2552         log 33_1
2553         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2554         log 33_2
2555 }
2556 run_test 33aa "write file with mode 444 (should return error) ===="
2557
2558 test_33a() {
2559         rm -fr $DIR/d33
2560         test_mkdir -p $DIR/d33
2561         chown $RUNAS_ID $DIR/d33
2562         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2563         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2564                 error "open RDWR" || true
2565 }
2566 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2567
2568 test_33b() {
2569         rm -fr $DIR/d33
2570         test_mkdir -p $DIR/d33
2571         chown $RUNAS_ID $DIR/d33
2572         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33
2573 }
2574 run_test 33b "test open file with malformed flags (No panic)"
2575
2576 test_33c() {
2577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2578         local ostnum
2579         local ostname
2580         local write_bytes
2581         local all_zeros
2582
2583         remote_ost_nodsh && skip "remote OST with nodsh" && return
2584         all_zeros=:
2585         rm -fr $DIR/d33
2586         test_mkdir -p $DIR/d33
2587         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2588
2589         sync
2590         for ostnum in $(seq $OSTCOUNT); do
2591                 # test-framework's OST numbering is one-based, while Lustre's
2592                 # is zero-based
2593                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2594                 # Parsing llobdstat's output sucks; we could grep the /proc
2595                 # path, but that's likely to not be as portable as using the
2596                 # llobdstat utility.  So we parse lctl output instead.
2597                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2598                         obdfilter/$ostname/stats |
2599                         awk '/^write_bytes/ {print $7}' )
2600                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2601                 if (( ${write_bytes:-0} > 0 ))
2602                 then
2603                         all_zeros=false
2604                         break;
2605                 fi
2606         done
2607
2608         $all_zeros || return 0
2609
2610         # Write four bytes
2611         echo foo > $DIR/d33/bar
2612         # Really write them
2613         sync
2614
2615         # Total up write_bytes after writing.  We'd better find non-zeros.
2616         for ostnum in $(seq $OSTCOUNT); do
2617                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2618                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2619                         obdfilter/$ostname/stats |
2620                         awk '/^write_bytes/ {print $7}' )
2621                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2622                 if (( ${write_bytes:-0} > 0 ))
2623                 then
2624                         all_zeros=false
2625                         break;
2626                 fi
2627         done
2628
2629         if $all_zeros
2630         then
2631                 for ostnum in $(seq $OSTCOUNT); do
2632                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2633                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2634                         do_facet ost$ostnum lctl get_param -n \
2635                                 obdfilter/$ostname/stats
2636                 done
2637                 error "OST not keeping write_bytes stats (b22312)"
2638         fi
2639 }
2640 run_test 33c "test llobdstat and write_bytes"
2641
2642 test_33d() {
2643         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2645         local MDTIDX=1
2646         local remote_dir=$DIR/$tdir/remote_dir
2647
2648         mkdir -p $DIR/$tdir
2649         $LFS mkdir -i $MDTIDX $remote_dir ||
2650                 error "create remote directory failed"
2651
2652         touch $remote_dir/$tfile
2653         chmod 444 $remote_dir/$tfile
2654         chown $RUNAS_ID $remote_dir/$tfile
2655
2656         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2657
2658         chown $RUNAS_ID $remote_dir
2659         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2660                                         error "create" || true
2661         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2662                                     error "open RDWR" || true
2663         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2664                                     error "create" || true
2665 }
2666 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2667
2668 test_33e() {
2669         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2670
2671         mkdir $DIR/$tdir
2672
2673         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2674         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2675         mkdir $DIR/$tdir/local_dir
2676
2677         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2678         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2679         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2680
2681         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2682                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2683
2684         rmdir $DIR/$tdir/* || error "rmdir failed"
2685
2686         umask 777
2687         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2688         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2689         mkdir $DIR/$tdir/local_dir
2690
2691         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2692         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2693         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2694
2695         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2696                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2697
2698         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2699
2700         umask 000
2701         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2702         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2703         mkdir $DIR/$tdir/local_dir
2704
2705         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2706         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2707         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2708
2709         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2710                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2711 }
2712 run_test 33e "mkdir and striped directory should have same mode"
2713
2714 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2715 test_34a() {
2716         rm -f $DIR/f34
2717         $MCREATE $DIR/f34 || error
2718         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2719         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2720         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2721         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2722 }
2723 run_test 34a "truncate file that has not been opened ==========="
2724
2725 test_34b() {
2726         [ ! -f $DIR/f34 ] && test_34a
2727         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2728         $OPENFILE -f O_RDONLY $DIR/f34
2729         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2730         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2731 }
2732 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2733
2734 test_34c() {
2735         [ ! -f $DIR/f34 ] && test_34a
2736         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2737         $OPENFILE -f O_RDWR $DIR/f34
2738         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2739         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2740 }
2741 run_test 34c "O_RDWR opening file-with-size works =============="
2742
2743 test_34d() {
2744         [ ! -f $DIR/f34 ] && test_34a
2745         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2746         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2747         rm $DIR/f34
2748 }
2749 run_test 34d "write to sparse file ============================="
2750
2751 test_34e() {
2752         rm -f $DIR/f34e
2753         $MCREATE $DIR/f34e || error
2754         $TRUNCATE $DIR/f34e 1000 || error
2755         $CHECKSTAT -s 1000 $DIR/f34e || error
2756         $OPENFILE -f O_RDWR $DIR/f34e
2757         $CHECKSTAT -s 1000 $DIR/f34e || error
2758 }
2759 run_test 34e "create objects, some with size and some without =="
2760
2761 test_34f() { # bug 6242, 6243
2762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2763         SIZE34F=48000
2764         rm -f $DIR/f34f
2765         $MCREATE $DIR/f34f || error
2766         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2767         dd if=$DIR/f34f of=$TMP/f34f
2768         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2769         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2770         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2771         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2772         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2773 }
2774 run_test 34f "read from a file with no objects until EOF ======="
2775
2776 test_34g() {
2777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2778         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2779         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2780         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2781         cancel_lru_locks osc
2782         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2783                 error "wrong size after lock cancel"
2784
2785         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2786         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2787                 error "expanding truncate failed"
2788         cancel_lru_locks osc
2789         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2790                 error "wrong expanded size after lock cancel"
2791 }
2792 run_test 34g "truncate long file ==============================="
2793
2794 test_34h() {
2795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2796         local gid=10
2797         local sz=1000
2798
2799         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2800         sync # Flush the cache so that multiop below does not block on cache
2801              # flush when getting the group lock
2802         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2803         MULTIPID=$!
2804
2805         # Since just timed wait is not good enough, let's do a sync write
2806         # that way we are sure enough time for a roundtrip + processing
2807         # passed + 2 seconds of extra margin.
2808         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2809         rm $DIR/${tfile}-1
2810         sleep 2
2811
2812         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2813                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2814                 kill -9 $MULTIPID
2815         fi
2816         wait $MULTIPID
2817         local nsz=`stat -c %s $DIR/$tfile`
2818         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2819 }
2820 run_test 34h "ftruncate file under grouplock should not block"
2821
2822 test_35a() {
2823         cp /bin/sh $DIR/f35a
2824         chmod 444 $DIR/f35a
2825         chown $RUNAS_ID $DIR/f35a
2826         $RUNAS $DIR/f35a && error || true
2827         rm $DIR/f35a
2828 }
2829 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2830
2831 test_36a() {
2832         rm -f $DIR/f36
2833         utime $DIR/f36 || error
2834 }
2835 run_test 36a "MDS utime check (mknod, utime) ==================="
2836
2837 test_36b() {
2838         echo "" > $DIR/f36
2839         utime $DIR/f36 || error
2840 }
2841 run_test 36b "OST utime check (open, utime) ===================="
2842
2843 test_36c() {
2844         rm -f $DIR/d36/f36
2845         test_mkdir $DIR/d36
2846         chown $RUNAS_ID $DIR/d36
2847         $RUNAS utime $DIR/d36/f36 || error
2848 }
2849 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2850
2851 test_36d() {
2852         [ ! -d $DIR/d36 ] && test_36c
2853         echo "" > $DIR/d36/f36
2854         $RUNAS utime $DIR/d36/f36 || error
2855 }
2856 run_test 36d "non-root OST utime check (open, utime) ==========="
2857
2858 test_36e() {
2859         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2860         test_mkdir -p $DIR/$tdir
2861         touch $DIR/$tdir/$tfile
2862         $RUNAS utime $DIR/$tdir/$tfile && \
2863                 error "utime worked, expected failure" || true
2864 }
2865 run_test 36e "utime on non-owned file (should return error) ===="
2866
2867 subr_36fh() {
2868         local fl="$1"
2869         local LANG_SAVE=$LANG
2870         local LC_LANG_SAVE=$LC_LANG
2871         export LANG=C LC_LANG=C # for date language
2872
2873         DATESTR="Dec 20  2000"
2874         test_mkdir -p $DIR/$tdir
2875         lctl set_param fail_loc=$fl
2876         date; date +%s
2877         cp /etc/hosts $DIR/$tdir/$tfile
2878         sync & # write RPC generated with "current" inode timestamp, but delayed
2879         sleep 1
2880         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2881         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2882         cancel_lru_locks osc
2883         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2884         date; date +%s
2885         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2886                 echo "BEFORE: $LS_BEFORE" && \
2887                 echo "AFTER : $LS_AFTER" && \
2888                 echo "WANT  : $DATESTR" && \
2889                 error "$DIR/$tdir/$tfile timestamps changed" || true
2890
2891         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2892 }
2893
2894 test_36f() {
2895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2896         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2897         subr_36fh "0x80000214"
2898 }
2899 run_test 36f "utime on file racing with OST BRW write =========="
2900
2901 test_36g() {
2902         remote_ost_nodsh && skip "remote OST with nodsh" && return
2903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2904         local fmd_max_age
2905         local fmd_before
2906         local fmd_after
2907
2908         test_mkdir -p $DIR/$tdir
2909         fmd_max_age=$(do_facet ost1 \
2910                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2911                 head -n 1")
2912
2913         fmd_before=$(do_facet ost1 \
2914                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2915         touch $DIR/$tdir/$tfile
2916         sleep $((fmd_max_age + 12))
2917         fmd_after=$(do_facet ost1 \
2918                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2919
2920         echo "fmd_before: $fmd_before"
2921         echo "fmd_after: $fmd_after"
2922         [[ $fmd_after -gt $fmd_before ]] &&
2923                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2924                 error "fmd didn't expire after ping" || true
2925 }
2926 run_test 36g "filter mod data cache expiry ====================="
2927
2928 test_36h() {
2929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2930         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2931         subr_36fh "0x80000227"
2932 }
2933 run_test 36h "utime on file racing with OST BRW write =========="
2934
2935 test_36i() {
2936         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2937
2938         mkdir $DIR/$tdir
2939         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2940
2941         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2942         local new_mtime=$((mtime + 200))
2943
2944         #change Modify time of striped dir
2945         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2946                         error "change mtime failed"
2947
2948         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2949
2950         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2951 }
2952 run_test 36i "change mtime on striped directory"
2953
2954 # test_37 - duplicate with tests 32q 32r
2955
2956 test_38() {
2957         local file=$DIR/$tfile
2958         touch $file
2959         openfile -f O_DIRECTORY $file
2960         local RC=$?
2961         local ENOTDIR=20
2962         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2963         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2964 }
2965 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2966
2967 test_39() {
2968         touch $DIR/$tfile
2969         touch $DIR/${tfile}2
2970 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2971 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2972 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2973         sleep 2
2974         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2975         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2976                 echo "mtime"
2977                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2978                 echo "atime"
2979                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2980                 echo "ctime"
2981                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2982                 error "O_TRUNC didn't change timestamps"
2983         fi
2984 }
2985 run_test 39 "mtime changed on create ==========================="
2986
2987 test_39b() {
2988         test_mkdir -p -c1 $DIR/$tdir
2989         cp -p /etc/passwd $DIR/$tdir/fopen
2990         cp -p /etc/passwd $DIR/$tdir/flink
2991         cp -p /etc/passwd $DIR/$tdir/funlink
2992         cp -p /etc/passwd $DIR/$tdir/frename
2993         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2994
2995         sleep 1
2996         echo "aaaaaa" >> $DIR/$tdir/fopen
2997         echo "aaaaaa" >> $DIR/$tdir/flink
2998         echo "aaaaaa" >> $DIR/$tdir/funlink
2999         echo "aaaaaa" >> $DIR/$tdir/frename
3000
3001         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3002         local link_new=`stat -c %Y $DIR/$tdir/flink`
3003         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3004         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3005
3006         cat $DIR/$tdir/fopen > /dev/null
3007         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3008         rm -f $DIR/$tdir/funlink2
3009         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3010
3011         for (( i=0; i < 2; i++ )) ; do
3012                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3013                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3014                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3015                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3016
3017                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3018                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3019                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3020                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3021
3022                 cancel_lru_locks osc
3023                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3024         done
3025 }
3026 run_test 39b "mtime change on open, link, unlink, rename  ======"
3027
3028 # this should be set to past
3029 TEST_39_MTIME=`date -d "1 year ago" +%s`
3030
3031 # bug 11063
3032 test_39c() {
3033         touch $DIR1/$tfile
3034         sleep 2
3035         local mtime0=`stat -c %Y $DIR1/$tfile`
3036
3037         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3038         local mtime1=`stat -c %Y $DIR1/$tfile`
3039         [ "$mtime1" = $TEST_39_MTIME ] || \
3040                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3041
3042         local d1=`date +%s`
3043         echo hello >> $DIR1/$tfile
3044         local d2=`date +%s`
3045         local mtime2=`stat -c %Y $DIR1/$tfile`
3046         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3047                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3048
3049         mv $DIR1/$tfile $DIR1/$tfile-1
3050
3051         for (( i=0; i < 2; i++ )) ; do
3052                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3053                 [ "$mtime2" = "$mtime3" ] || \
3054                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3055
3056                 cancel_lru_locks osc
3057                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3058         done
3059 }
3060 run_test 39c "mtime change on rename ==========================="
3061
3062 # bug 21114
3063 test_39d() {
3064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3065         touch $DIR1/$tfile
3066
3067         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3068
3069         for (( i=0; i < 2; i++ )) ; do
3070                 local mtime=`stat -c %Y $DIR1/$tfile`
3071                 [ $mtime = $TEST_39_MTIME ] || \
3072                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3073
3074                 cancel_lru_locks osc
3075                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3076         done
3077 }
3078 run_test 39d "create, utime, stat =============================="
3079
3080 # bug 21114
3081 test_39e() {
3082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3083         touch $DIR1/$tfile
3084         local mtime1=`stat -c %Y $DIR1/$tfile`
3085
3086         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3087
3088         for (( i=0; i < 2; i++ )) ; do
3089                 local mtime2=`stat -c %Y $DIR1/$tfile`
3090                 [ $mtime2 = $TEST_39_MTIME ] || \
3091                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3092
3093                 cancel_lru_locks osc
3094                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3095         done
3096 }
3097 run_test 39e "create, stat, utime, stat ========================"
3098
3099 # bug 21114
3100 test_39f() {
3101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3102         touch $DIR1/$tfile
3103         mtime1=`stat -c %Y $DIR1/$tfile`
3104
3105         sleep 2
3106         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3107
3108         for (( i=0; i < 2; i++ )) ; do
3109                 local mtime2=`stat -c %Y $DIR1/$tfile`
3110                 [ $mtime2 = $TEST_39_MTIME ] || \
3111                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3112
3113                 cancel_lru_locks osc
3114                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3115         done
3116 }
3117 run_test 39f "create, stat, sleep, utime, stat ================="
3118
3119 # bug 11063
3120 test_39g() {
3121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3122         echo hello >> $DIR1/$tfile
3123         local mtime1=`stat -c %Y $DIR1/$tfile`
3124
3125         sleep 2
3126         chmod o+r $DIR1/$tfile
3127
3128         for (( i=0; i < 2; i++ )) ; do
3129                 local mtime2=`stat -c %Y $DIR1/$tfile`
3130                 [ "$mtime1" = "$mtime2" ] || \
3131                         error "lost mtime: $mtime2, should be $mtime1"
3132
3133                 cancel_lru_locks osc
3134                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3135         done
3136 }
3137 run_test 39g "write, chmod, stat ==============================="
3138
3139 # bug 11063
3140 test_39h() {
3141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3142         touch $DIR1/$tfile
3143         sleep 1
3144
3145         local d1=`date`
3146         echo hello >> $DIR1/$tfile
3147         local mtime1=`stat -c %Y $DIR1/$tfile`
3148
3149         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3150         local d2=`date`
3151         if [ "$d1" != "$d2" ]; then
3152                 echo "write and touch not within one second"
3153         else
3154                 for (( i=0; i < 2; i++ )) ; do
3155                         local mtime2=`stat -c %Y $DIR1/$tfile`
3156                         [ "$mtime2" = $TEST_39_MTIME ] || \
3157                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3158
3159                         cancel_lru_locks osc
3160                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3161                 done
3162         fi
3163 }
3164 run_test 39h "write, utime within one second, stat ============="
3165
3166 test_39i() {
3167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3168         touch $DIR1/$tfile
3169         sleep 1
3170
3171         echo hello >> $DIR1/$tfile
3172         local mtime1=`stat -c %Y $DIR1/$tfile`
3173
3174         mv $DIR1/$tfile $DIR1/$tfile-1
3175
3176         for (( i=0; i < 2; i++ )) ; do
3177                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3178
3179                 [ "$mtime1" = "$mtime2" ] || \
3180                         error "lost mtime: $mtime2, should be $mtime1"
3181
3182                 cancel_lru_locks osc
3183                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3184         done
3185 }
3186 run_test 39i "write, rename, stat =============================="
3187
3188 test_39j() {
3189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3190         start_full_debug_logging
3191         touch $DIR1/$tfile
3192         sleep 1
3193
3194         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3195         lctl set_param fail_loc=0x80000412
3196         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3197                 error "multiop failed"
3198         local multipid=$!
3199         local mtime1=`stat -c %Y $DIR1/$tfile`
3200
3201         mv $DIR1/$tfile $DIR1/$tfile-1
3202
3203         kill -USR1 $multipid
3204         wait $multipid || error "multiop close failed"
3205
3206         for (( i=0; i < 2; i++ )) ; do
3207                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3208                 [ "$mtime1" = "$mtime2" ] ||
3209                         error "mtime is lost on close: $mtime2, " \
3210                               "should be $mtime1"
3211
3212                 cancel_lru_locks osc
3213                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3214         done
3215         lctl set_param fail_loc=0
3216         stop_full_debug_logging
3217 }
3218 run_test 39j "write, rename, close, stat ======================="
3219
3220 test_39k() {
3221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3222         touch $DIR1/$tfile
3223         sleep 1
3224
3225         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3226         local multipid=$!
3227         local mtime1=`stat -c %Y $DIR1/$tfile`
3228
3229         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3230
3231         kill -USR1 $multipid
3232         wait $multipid || error "multiop close failed"
3233
3234         for (( i=0; i < 2; i++ )) ; do
3235                 local mtime2=`stat -c %Y $DIR1/$tfile`
3236
3237                 [ "$mtime2" = $TEST_39_MTIME ] || \
3238                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3239
3240                 cancel_lru_locks osc
3241                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3242         done
3243 }
3244 run_test 39k "write, utime, close, stat ========================"
3245
3246 # this should be set to future
3247 TEST_39_ATIME=`date -d "1 year" +%s`
3248
3249 test_39l() {
3250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3251         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3252         local atime_diff=$(do_facet $SINGLEMDS \
3253                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3254         rm -rf $DIR/$tdir
3255         mkdir -p $DIR/$tdir
3256
3257         # test setting directory atime to future
3258         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3259         local atime=$(stat -c %X $DIR/$tdir)
3260         [ "$atime" = $TEST_39_ATIME ] || \
3261                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3262
3263         # test setting directory atime from future to now
3264         local d1=$(date +%s)
3265         ls $DIR/$tdir
3266         local d2=$(date +%s)
3267
3268         cancel_lru_locks mdc
3269         atime=$(stat -c %X $DIR/$tdir)
3270         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3271                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3272
3273         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3274         sleep 3
3275
3276         # test setting directory atime when now > dir atime + atime_diff
3277         d1=$(date +%s)
3278         ls $DIR/$tdir
3279         d2=$(date +%s)
3280         cancel_lru_locks mdc
3281         atime=$(stat -c %X $DIR/$tdir)
3282         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3283                 error "atime is not updated  : $atime, should be $d2"
3284
3285         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3286         sleep 3
3287
3288         # test not setting directory atime when now < dir atime + atime_diff
3289         ls $DIR/$tdir
3290         cancel_lru_locks mdc
3291         atime=$(stat -c %X $DIR/$tdir)
3292         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3293                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3294
3295         do_facet $SINGLEMDS \
3296                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3297 }
3298 run_test 39l "directory atime update ==========================="
3299
3300 test_39m() {
3301         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3302         touch $DIR1/$tfile
3303         sleep 2
3304         local far_past_mtime=$(date -d "May 29 1953" +%s)
3305         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3306
3307         touch -m -d @$far_past_mtime $DIR1/$tfile
3308         touch -a -d @$far_past_atime $DIR1/$tfile
3309
3310         for (( i=0; i < 2; i++ )) ; do
3311                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3312                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3313                         error "atime or mtime set incorrectly"
3314
3315                 cancel_lru_locks osc
3316                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3317         done
3318 }
3319 run_test 39m "test atime and mtime before 1970"
3320
3321 test_39n() { # LU-3832
3322         local atime_diff=$(do_facet $SINGLEMDS \
3323                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3324         local atime0
3325         local atime1
3326         local atime2
3327
3328         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3329
3330         rm -rf $DIR/$tfile
3331         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3332         atime0=$(stat -c %X $DIR/$tfile)
3333
3334         sleep 5
3335         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3336         atime1=$(stat -c %X $DIR/$tfile)
3337
3338         sleep 5
3339         cancel_lru_locks mdc
3340         cancel_lru_locks osc
3341         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3342         atime2=$(stat -c %X $DIR/$tfile)
3343
3344         do_facet $SINGLEMDS \
3345                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3346
3347         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3348         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3349 }
3350 run_test 39n "check that O_NOATIME is honored"
3351
3352 test_39o() {
3353         TESTDIR=$DIR/$tdir/$tfile
3354         [ -e $TESTDIR ] && rm -rf $TESTDIR
3355         test_mkdir -p $TESTDIR
3356         cd $TESTDIR
3357         links1=2
3358         ls
3359         mkdir a b
3360         ls
3361         links2=$(stat -c %h .)
3362         [ $(($links1 + 2)) != $links2 ] &&
3363                 error "wrong links count $(($links1 + 2)) != $links2"
3364         rmdir b
3365         links3=$(stat -c %h .)
3366         [ $(($links1 + 1)) != $links3 ] &&
3367                 error "wrong links count $links1 != $links3"
3368         return 0
3369 }
3370 run_test 39o "directory cached attributes updated after create ========"
3371
3372 test_39p() {
3373         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3374         local MDTIDX=1
3375         TESTDIR=$DIR/$tdir/$tfile
3376         [ -e $TESTDIR ] && rm -rf $TESTDIR
3377         mkdir -p $TESTDIR
3378         cd $TESTDIR
3379         links1=2
3380         ls
3381         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3382         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3383         ls
3384         links2=$(stat -c %h .)
3385         [ $(($links1 + 2)) != $links2 ] &&
3386                 error "wrong links count $(($links1 + 2)) != $links2"
3387         rmdir remote_dir2
3388         links3=$(stat -c %h .)
3389         [ $(($links1 + 1)) != $links3 ] &&
3390                 error "wrong links count $links1 != $links3"
3391         return 0
3392 }
3393 run_test 39p "remote directory cached attributes updated after create ========"
3394
3395
3396 test_40() {
3397         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3398         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3399                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3400         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3401                 error "$tfile is not 4096 bytes in size"
3402 }
3403 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3404
3405 test_41() {
3406         # bug 1553
3407         small_write $DIR/f41 18
3408 }
3409 run_test 41 "test small file write + fstat ====================="
3410
3411 count_ost_writes() {
3412         lctl get_param -n osc.*.stats |
3413                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3414                         END { printf("%0.0f", writes) }'
3415 }
3416
3417 # decent default
3418 WRITEBACK_SAVE=500
3419 DIRTY_RATIO_SAVE=40
3420 MAX_DIRTY_RATIO=50
3421 BG_DIRTY_RATIO_SAVE=10
3422 MAX_BG_DIRTY_RATIO=25
3423
3424 start_writeback() {
3425         trap 0
3426         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3427         # dirty_ratio, dirty_background_ratio
3428         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3429                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3430                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3431                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3432         else
3433                 # if file not here, we are a 2.4 kernel
3434                 kill -CONT `pidof kupdated`
3435         fi
3436 }
3437
3438 stop_writeback() {
3439         # setup the trap first, so someone cannot exit the test at the
3440         # exact wrong time and mess up a machine
3441         trap start_writeback EXIT
3442         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3443         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3444                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3445                 sysctl -w vm.dirty_writeback_centisecs=0
3446                 sysctl -w vm.dirty_writeback_centisecs=0
3447                 # save and increase /proc/sys/vm/dirty_ratio
3448                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3449                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3450                 # save and increase /proc/sys/vm/dirty_background_ratio
3451                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3452                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3453         else
3454                 # if file not here, we are a 2.4 kernel
3455                 kill -STOP `pidof kupdated`
3456         fi
3457 }
3458
3459 # ensure that all stripes have some grant before we test client-side cache
3460 setup_test42() {
3461         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3462                 dd if=/dev/zero of=$i bs=4k count=1
3463                 rm $i
3464         done
3465 }
3466
3467 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3468 # file truncation, and file removal.
3469 test_42a() {
3470         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3471         setup_test42
3472         cancel_lru_locks osc
3473         stop_writeback
3474         sync; sleep 1; sync # just to be safe
3475         BEFOREWRITES=`count_ost_writes`
3476         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3477         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3478         AFTERWRITES=`count_ost_writes`
3479         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3480                 error "$BEFOREWRITES < $AFTERWRITES"
3481         start_writeback
3482 }
3483 run_test 42a "ensure that we don't flush on close =============="
3484
3485 test_42b() {
3486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3487         setup_test42
3488         cancel_lru_locks osc
3489         stop_writeback
3490         sync
3491         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3492         BEFOREWRITES=$(count_ost_writes)
3493         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3494         AFTERWRITES=$(count_ost_writes)
3495         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3496                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3497         fi
3498         BEFOREWRITES=$(count_ost_writes)
3499         sync || error "sync: $?"
3500         AFTERWRITES=$(count_ost_writes)
3501         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3502                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3503         fi
3504         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3505         start_writeback
3506         return 0
3507 }
3508 run_test 42b "test destroy of file with cached dirty data ======"
3509
3510 # if these tests just want to test the effect of truncation,
3511 # they have to be very careful.  consider:
3512 # - the first open gets a {0,EOF}PR lock
3513 # - the first write conflicts and gets a {0, count-1}PW
3514 # - the rest of the writes are under {count,EOF}PW
3515 # - the open for truncate tries to match a {0,EOF}PR
3516 #   for the filesize and cancels the PWs.
3517 # any number of fixes (don't get {0,EOF} on open, match
3518 # composite locks, do smarter file size management) fix
3519 # this, but for now we want these tests to verify that
3520 # the cancellation with truncate intent works, so we
3521 # start the file with a full-file pw lock to match against
3522 # until the truncate.
3523 trunc_test() {
3524         test=$1
3525         file=$DIR/$test
3526         offset=$2
3527         cancel_lru_locks osc
3528         stop_writeback
3529         # prime the file with 0,EOF PW to match
3530         touch $file
3531         $TRUNCATE $file 0
3532         sync; sync
3533         # now the real test..
3534         dd if=/dev/zero of=$file bs=1024 count=100
3535         BEFOREWRITES=`count_ost_writes`
3536         $TRUNCATE $file $offset
3537         cancel_lru_locks osc
3538         AFTERWRITES=`count_ost_writes`
3539         start_writeback
3540 }
3541
3542 test_42c() {
3543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3544         trunc_test 42c 1024
3545         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3546             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3547         rm $file
3548 }
3549 run_test 42c "test partial truncate of file with cached dirty data"
3550
3551 test_42d() {
3552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3553         trunc_test 42d 0
3554         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3555             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3556         rm $file
3557 }
3558 run_test 42d "test complete truncate of file with cached dirty data"
3559
3560 test_42e() { # bug22074
3561         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3562         local TDIR=$DIR/${tdir}e
3563         local pagesz=$(page_size)
3564         local pages=16 # hardcoded 16 pages, don't change it.
3565         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3566         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3567         local max_dirty_mb
3568         local warmup_files
3569
3570         test_mkdir -p $DIR/${tdir}e
3571         $SETSTRIPE -c 1 $TDIR
3572         createmany -o $TDIR/f $files
3573
3574         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3575
3576         # we assume that with $OSTCOUNT files, at least one of them will
3577         # be allocated on OST0.
3578         warmup_files=$((OSTCOUNT * max_dirty_mb))
3579         createmany -o $TDIR/w $warmup_files
3580
3581         # write a large amount of data into one file and sync, to get good
3582         # avail_grant number from OST.
3583         for ((i=0; i<$warmup_files; i++)); do
3584                 idx=$($GETSTRIPE -i $TDIR/w$i)
3585                 [ $idx -ne 0 ] && continue
3586                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3587                 break
3588         done
3589         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3590         sync
3591         $LCTL get_param $proc_osc0/cur_dirty_bytes
3592         $LCTL get_param $proc_osc0/cur_grant_bytes
3593
3594         # create as much dirty pages as we can while not to trigger the actual
3595         # RPCs directly. but depends on the env, VFS may trigger flush during this
3596         # period, hopefully we are good.
3597         for ((i=0; i<$warmup_files; i++)); do
3598                 idx=$($GETSTRIPE -i $TDIR/w$i)
3599                 [ $idx -ne 0 ] && continue
3600                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3601         done
3602         $LCTL get_param $proc_osc0/cur_dirty_bytes
3603         $LCTL get_param $proc_osc0/cur_grant_bytes
3604
3605         # perform the real test
3606         $LCTL set_param $proc_osc0/rpc_stats 0
3607         for ((;i<$files; i++)); do
3608                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3609                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3610         done
3611         sync
3612         $LCTL get_param $proc_osc0/rpc_stats
3613
3614         local percent=0
3615         local have_ppr=false
3616         $LCTL get_param $proc_osc0/rpc_stats |
3617                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3618                         # skip lines until we are at the RPC histogram data
3619                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3620                         $have_ppr || continue
3621
3622                         # we only want the percent stat for < 16 pages
3623                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3624
3625                         percent=$((percent + WPCT))
3626                         if [[ $percent -gt 15 ]]; then
3627                                 error "less than 16-pages write RPCs" \
3628                                       "$percent% > 15%"
3629                                 break
3630                         fi
3631                 done
3632         rm -rf $TDIR
3633 }
3634 run_test 42e "verify sub-RPC writes are not done synchronously"
3635
3636 test_43() {
3637         test_mkdir -p $DIR/$tdir
3638         cp -p /bin/ls $DIR/$tdir/$tfile
3639         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3640         pid=$!
3641         # give multiop a chance to open
3642         sleep 1
3643
3644         $DIR/$tdir/$tfile && error || true
3645         kill -USR1 $pid
3646 }
3647 run_test 43 "execution of file opened for write should return -ETXTBSY"
3648
3649 test_43a() {
3650         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3651         test_mkdir -p $DIR/$tdir
3652         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3653                         cp -p multiop $DIR/$tdir/multiop
3654         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3655                         return 1
3656         MULTIOP_PID=$!
3657         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3658         kill -USR1 $MULTIOP_PID || return 2
3659         wait $MULTIOP_PID || return 3
3660         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3661 }
3662 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3663
3664 test_43b() {
3665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3666         test_mkdir -p $DIR/$tdir
3667         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3668                         cp -p multiop $DIR/$tdir/multiop
3669         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3670                         return 1
3671         MULTIOP_PID=$!
3672         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3673         kill -USR1 $MULTIOP_PID || return 2
3674         wait $MULTIOP_PID || return 3
3675         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3676 }
3677 run_test 43b "truncate of file being executed should return -ETXTBSY"
3678
3679 test_43c() {
3680         local testdir="$DIR/$tdir"
3681         test_mkdir -p $DIR/$tdir
3682         cp $SHELL $testdir/
3683         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3684                 ( cd $testdir && md5sum -c)
3685 }
3686 run_test 43c "md5sum of copy into lustre========================"
3687
3688 test_44() {
3689         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3690         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3691         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3692 }
3693 run_test 44 "zero length read from a sparse stripe ============="
3694
3695 test_44a() {
3696         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3697                 awk '{ print $2 }')
3698         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3699         [[ $nstripe -gt $OSTCOUNT ]] &&
3700             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3701             return
3702         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3703                 awk '{ print $2 }')
3704         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3705                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3706                         awk '{ print $2 }')
3707         fi
3708
3709         OFFSETS="0 $((stride/2)) $((stride-1))"
3710         for offset in $OFFSETS; do
3711                 for i in $(seq 0 $((nstripe-1))); do
3712                         local GLOBALOFFSETS=""
3713                         # size in Bytes
3714                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3715                         local myfn=$DIR/d44a-$size
3716                         echo "--------writing $myfn at $size"
3717                         ll_sparseness_write $myfn $size ||
3718                                 error "ll_sparseness_write"
3719                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3720                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3721                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3722
3723                         for j in $(seq 0 $((nstripe-1))); do
3724                                 # size in Bytes
3725                                 size=$((((j + $nstripe )*$stride + $offset)))
3726                                 ll_sparseness_write $myfn $size ||
3727                                         error "ll_sparseness_write"
3728                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3729                         done
3730                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3731                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3732                         rm -f $myfn
3733                 done
3734         done
3735 }
3736 run_test 44a "test sparse pwrite ==============================="
3737
3738 dirty_osc_total() {
3739         tot=0
3740         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3741                 tot=$(($tot + $d))
3742         done
3743         echo $tot
3744 }
3745 do_dirty_record() {
3746         before=`dirty_osc_total`
3747         echo executing "\"$*\""
3748         eval $*
3749         after=`dirty_osc_total`
3750         echo before $before, after $after
3751 }
3752 test_45() {
3753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3754         f="$DIR/f45"
3755         # Obtain grants from OST if it supports it
3756         echo blah > ${f}_grant
3757         stop_writeback
3758         sync
3759         do_dirty_record "echo blah > $f"
3760         [[ $before -eq $after ]] && error "write wasn't cached"
3761         do_dirty_record "> $f"
3762         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3763         do_dirty_record "echo blah > $f"
3764         [[ $before -eq $after ]] && error "write wasn't cached"
3765         do_dirty_record "sync"
3766         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3767         do_dirty_record "echo blah > $f"
3768         [[ $before -eq $after ]] && error "write wasn't cached"
3769         do_dirty_record "cancel_lru_locks osc"
3770         [[ $before -gt $after ]] ||
3771                 error "lock cancellation didn't lower dirty count"
3772         start_writeback
3773 }
3774 run_test 45 "osc io page accounting ============================"
3775
3776 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3777 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3778 # objects offset and an assert hit when an rpc was built with 1023's mapped
3779 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3780 test_46() {
3781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3782         f="$DIR/f46"
3783         stop_writeback
3784         sync
3785         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3786         sync
3787         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3788         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3789         sync
3790         start_writeback
3791 }
3792 run_test 46 "dirtying a previously written page ================"
3793
3794 # test_47 is removed "Device nodes check" is moved to test_28
3795
3796 test_48a() { # bug 2399
3797         check_kernel_version 34 || return 0
3798         test_mkdir -p $DIR/$tdir
3799         cd $DIR/$tdir
3800         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3801         test_mkdir $DIR/$tdir || error "recreate directory failed"
3802         touch foo || error "'touch foo' failed after recreating cwd"
3803         test_mkdir $DIR/$tdir/bar ||
3804                      error "'mkdir foo' failed after recreating cwd"
3805         if check_kernel_version 44; then
3806                 touch .foo || error "'touch .foo' failed after recreating cwd"
3807                 test_mkdir $DIR/$tdir/.bar ||
3808                               error "'mkdir .foo' failed after recreating cwd"
3809         fi
3810         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3811         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3812         cd . || error "'cd .' failed after recreating cwd"
3813         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3814         rmdir . && error "'rmdir .' worked after recreating cwd"
3815         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3816         cd .. || error "'cd ..' failed after recreating cwd"
3817 }
3818 run_test 48a "Access renamed working dir (should return errors)="
3819
3820 test_48b() { # bug 2399
3821         check_kernel_version 34 || return 0
3822         rm -rf $DIR/$tdir
3823         test_mkdir -p $DIR/$tdir
3824         cd $DIR/$tdir
3825         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3826         touch foo && error "'touch foo' worked after removing cwd"
3827         test_mkdir $DIR/$tdir/foo &&
3828                      error "'mkdir foo' worked after removing cwd"
3829         if check_kernel_version 44; then
3830                 touch .foo && error "'touch .foo' worked after removing cwd"
3831                 test_mkdir $DIR/$tdir/.foo &&
3832                               error "'mkdir .foo' worked after removing cwd"
3833         fi
3834         ls . > /dev/null && error "'ls .' worked after removing cwd"
3835         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3836         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3837         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3838         rmdir . && error "'rmdir .' worked after removing cwd"
3839         ln -s . foo && error "'ln -s .' worked after removing cwd"
3840         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3841 }
3842 run_test 48b "Access removed working dir (should return errors)="
3843
3844 test_48c() { # bug 2350
3845         check_kernel_version 36 || return 0
3846         #lctl set_param debug=-1
3847         #set -vx
3848         rm -rf $DIR/$tdir
3849         test_mkdir -p $DIR/$tdir/dir
3850         cd $DIR/$tdir/dir
3851         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3852         $TRACE touch foo && error "touch foo worked after removing cwd"
3853         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3854         if check_kernel_version 44; then
3855                 touch .foo && error "touch .foo worked after removing cwd"
3856                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3857         fi
3858         $TRACE ls . && error "'ls .' worked after removing cwd"
3859         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3860         is_patchless || ( $TRACE cd . &&
3861                         error "'cd .' worked after removing cwd" )
3862         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3863         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3864         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3865         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3866 }
3867 run_test 48c "Access removed working subdir (should return errors)"
3868
3869 test_48d() { # bug 2350
3870         check_kernel_version 36 || return 0
3871         #lctl set_param debug=-1
3872         #set -vx
3873         rm -rf $DIR/$tdir
3874         test_mkdir -p $DIR/$tdir/dir
3875         cd $DIR/$tdir/dir
3876         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3877         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3878         $TRACE touch foo && error "'touch foo' worked after removing parent"
3879         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3880         if check_kernel_version 44; then
3881                 touch .foo && error "'touch .foo' worked after removing parent"
3882                 test_mkdir .foo &&
3883                               error "mkdir .foo worked after removing parent"
3884         fi
3885         $TRACE ls . && error "'ls .' worked after removing parent"
3886         $TRACE ls .. && error "'ls ..' worked after removing parent"
3887         is_patchless || ( $TRACE cd . &&
3888                         error "'cd .' worked after recreate parent" )
3889         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3890         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3891         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3892         is_patchless || ( $TRACE cd .. &&
3893                         error "'cd ..' worked after removing parent" || true )
3894 }
3895 run_test 48d "Access removed parent subdir (should return errors)"
3896
3897 test_48e() { # bug 4134
3898         check_kernel_version 41 || return 0
3899         #lctl set_param debug=-1
3900         #set -vx
3901         rm -rf $DIR/$tdir
3902         test_mkdir -p $DIR/$tdir/dir
3903         cd $DIR/$tdir/dir
3904         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3905         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3906         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3907         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3908         # On a buggy kernel addition of "touch foo" after cd .. will
3909         # produce kernel oops in lookup_hash_it
3910         touch ../foo && error "'cd ..' worked after recreate parent"
3911         cd $DIR
3912         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3913 }
3914 run_test 48e "Access to recreated parent subdir (should return errors)"
3915
3916 test_49() { # LU-1030
3917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3918         # get ost1 size - lustre-OST0000
3919         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3920                 awk '{ print $4 }')
3921         # write 800M at maximum
3922         [[ $ost1_size -lt 2 ]] && ost1_size=2
3923         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3924
3925         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3926         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3927         local dd_pid=$!
3928
3929         # change max_pages_per_rpc while writing the file
3930         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3931         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3932         # loop until dd process exits
3933         while ps ax -opid | grep -wq $dd_pid; do
3934                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3935                 sleep $((RANDOM % 5 + 1))
3936         done
3937         # restore original max_pages_per_rpc
3938         $LCTL set_param $osc1_mppc=$orig_mppc
3939         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3940 }
3941 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3942
3943 test_50() {
3944         # bug 1485
3945         test_mkdir $DIR/$tdir
3946         cd $DIR/$tdir
3947         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3948 }
3949 run_test 50 "special situations: /proc symlinks  ==============="
3950
3951 test_51a() {    # was test_51
3952         # bug 1516 - create an empty entry right after ".." then split dir
3953         test_mkdir -c1 $DIR/$tdir
3954         touch $DIR/$tdir/foo
3955         $MCREATE $DIR/$tdir/bar
3956         rm $DIR/$tdir/foo
3957         createmany -m $DIR/$tdir/longfile 201
3958         FNUM=202
3959         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3960                 $MCREATE $DIR/$tdir/longfile$FNUM
3961                 FNUM=$(($FNUM + 1))
3962                 echo -n "+"
3963         done
3964         echo
3965         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3966 }
3967 run_test 51a "special situations: split htree with empty entry =="
3968
3969 export NUMTEST=70000
3970 test_51b() {
3971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3972         local BASE=$DIR/d${base}.${TESTSUITE}
3973
3974         # cleanup the directory
3975         rm -fr $BASE
3976
3977         test_mkdir -p -c1 $BASE
3978
3979         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3980         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3981         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3982                 return
3983
3984         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3985                 echo "reduced count to $NUMTEST due to inodes"
3986
3987         # need to check free space for the directories as well
3988         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3989         numfree=$((blkfree / 4))
3990         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3991                 echo "reduced count to $NUMTEST due to blocks"
3992
3993         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3994                 echo "failed" > $BASE/fnum
3995 }
3996 run_test 51b "exceed 64k subdirectory nlink limit"
3997
3998 test_51ba() { # LU-993
3999         local BASE=$DIR/d${base}.${TESTSUITE}
4000         # unlink all but 100 subdirectories, then check it still works
4001         local LEFT=100
4002         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4003
4004         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4005         local DELETE=$((NUMTEST - LEFT))
4006
4007         # continue on to run this test even if 51b didn't finish,
4008         # just to delete the many subdirectories created.
4009         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4010
4011         # for ldiskfs the nlink count should be 1, but this is OSD specific
4012         # and so this is listed for informational purposes only
4013         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4014         unlinkmany -d $BASE/d $DELETE
4015         RC=$?
4016
4017         if [ $RC -ne 0 ]; then
4018                 if [ "$NUMPREV" == "failed" ]; then
4019                         skip "previous setup failed"
4020                         return 0
4021                 else
4022                         error "unlink of first $DELETE subdirs failed"
4023                         return $RC
4024                 fi
4025         fi
4026
4027         echo "nlink between: $(stat -c %h $BASE)"
4028         # trim the first line of ls output
4029         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4030         [ $FOUND -ne $LEFT ] &&
4031                 error "can't find subdirs: found only $FOUND/$LEFT"
4032
4033         unlinkmany -d $BASE/d $DELETE $LEFT ||
4034                 error "unlink of second $LEFT subdirs failed"
4035         # regardless of whether the backing filesystem tracks nlink accurately
4036         # or not, the nlink count shouldn't be more than "." and ".." here
4037         local AFTER=$(stat -c %h $BASE)
4038         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4039                 echo "nlink after: $AFTER"
4040 }
4041 run_test 51ba "verify nlink for many subdirectory cleanup"
4042
4043 test_51d() {
4044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4045         [[ $OSTCOUNT -lt 3 ]] &&
4046                 skip_env "skipping test with few OSTs" && return
4047         test_mkdir -p $DIR/$tdir
4048         createmany -o $DIR/$tdir/t- 1000
4049         $GETSTRIPE $DIR/$tdir > $TMP/files
4050         for N in $(seq 0 $((OSTCOUNT - 1))); do
4051                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4052                         END { printf("%0.0f", objs) }' $TMP/files)
4053                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4054                         '($1 == '$N') { objs += 1 } \
4055                         END { printf("%0.0f", objs) }')
4056                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4057         done
4058         unlinkmany $DIR/$tdir/t- 1000
4059
4060         NLAST=0
4061         for N in $(seq 1 $((OSTCOUNT - 1))); do
4062                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4063                         error "OST $N has less objects vs OST $NLAST" \
4064                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4065                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4066                         error "OST $N has less objects vs OST $NLAST" \
4067                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4068
4069                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4070                         error "OST $N has less #0 objects vs OST $NLAST" \
4071                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4072                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4073                         error "OST $N has less #0 objects vs OST $NLAST" \
4074                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4075                 NLAST=$N
4076         done
4077 }
4078 run_test 51d "check object distribution ===================="
4079
4080 test_51e() {
4081         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4082                 skip "Only applicable to ldiskfs-based MDTs"
4083                 return
4084         fi
4085
4086         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4087         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4088
4089         touch $DIR/$tdir/d0/foo
4090         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4091                 error "file exceed 65000 nlink limit!"
4092         unlinkmany $DIR/$tdir/d0/f- 65001
4093         return 0
4094 }
4095 run_test 51e "check file nlink limit"
4096
4097 test_52a() {
4098         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4099         test_mkdir -p $DIR/$tdir
4100         touch $DIR/$tdir/foo
4101         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4102         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4103         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4104         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4105         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4106                                         error "link worked"
4107         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4108         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4109         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4110                                                      error "lsattr"
4111         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4112         cp -r $DIR/$tdir /tmp/
4113         rm -fr $DIR/$tdir || error "cleanup rm failed"
4114 }
4115 run_test 52a "append-only flag test (should return errors) ====="
4116
4117 test_52b() {
4118         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4119         test_mkdir -p $DIR/$tdir
4120         touch $DIR/$tdir/foo
4121         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4122         cat test > $DIR/$tdir/foo && error "cat test worked"
4123         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4124         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4125         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4126                                         error "link worked"
4127         echo foo >> $DIR/$tdir/foo && error "echo worked"
4128         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4129         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4130         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4131         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4132                                                         error "lsattr"
4133         chattr -i $DIR/$tdir/foo || error "chattr failed"
4134
4135         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4136 }
4137 run_test 52b "immutable flag test (should return errors) ======="
4138
4139 test_53() {
4140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4141         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4142         remote_ost_nodsh && skip "remote OST with nodsh" && return
4143
4144         local param
4145         local param_seq
4146         local ostname
4147         local mds_last
4148         local mds_last_seq
4149         local ost_last
4150         local ost_last_seq
4151         local ost_last_id
4152         local ostnum
4153         local node
4154         local found=false
4155         local support_last_seq=true
4156
4157         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4158                 support_last_seq=false
4159
4160         # only test MDT0000
4161         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4162         local value
4163         for value in $(do_facet $SINGLEMDS \
4164                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4165                 param=$(echo ${value[0]} | cut -d "=" -f1)
4166                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4167
4168                 if $support_last_seq; then
4169                         param_seq=$(echo $param |
4170                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4171                         mds_last_seq=$(do_facet $SINGLEMDS \
4172                                        $LCTL get_param -n $param_seq)
4173                 fi
4174                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4175
4176                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4177                 node=$(facet_active_host ost$((ostnum+1)))
4178                 param="obdfilter.$ostname.last_id"
4179                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4180                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4181                         ost_last_id=$ost_last
4182
4183                         if $support_last_seq; then
4184                                 ost_last_id=$(echo $ost_last |
4185                                               awk -F':' '{print $2}' |
4186                                               sed -e "s/^0x//g")
4187                                 ost_last_seq=$(echo $ost_last |
4188                                                awk -F':' '{print $1}')
4189                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4190                         fi
4191
4192                         if [[ $ost_last_id != $mds_last ]]; then
4193                                 error "$ost_last_id != $mds_last"
4194                         else
4195                                 found=true
4196                                 break
4197                         fi
4198                 done
4199         done
4200         $found || error "can not match last_seq/last_id for $mdtosc"
4201         return 0
4202 }
4203 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4204
4205 test_54a() {
4206         $SOCKETSERVER $DIR/socket ||
4207                 error "$SOCKETSERVER $DIR/socket failed: $?"
4208         $SOCKETCLIENT $DIR/socket ||
4209                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4210         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4211 }
4212 run_test 54a "unix domain socket test =========================="
4213
4214 test_54b() {
4215         f="$DIR/f54b"
4216         mknod $f c 1 3
4217         chmod 0666 $f
4218         dd if=/dev/zero of=$f bs=$(page_size) count=1
4219 }
4220 run_test 54b "char device works in lustre ======================"
4221
4222 find_loop_dev() {
4223         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4224         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4225         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4226
4227         for i in $(seq 3 7); do
4228                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4229                 LOOPDEV=$LOOPBASE$i
4230                 LOOPNUM=$i
4231                 break
4232         done
4233 }
4234
4235 cleanup_54c() {
4236         loopdev="$DIR/loop54c"
4237
4238         trap 0
4239         $UMOUNT -d $DIR/$tdir || rc=$?
4240         losetup -d $loopdev || true
4241         losetup -d $LOOPDEV || true
4242         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4243         return $rc
4244 }
4245
4246 test_54c() {
4247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4248         loopdev="$DIR/loop54c"
4249
4250         find_loop_dev
4251         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4252         trap cleanup_54c EXIT
4253         mknod $loopdev b 7 $LOOPNUM
4254         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4255         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4256         losetup $loopdev $DIR/$tfile ||
4257                 error "can't set up $loopdev for $DIR/$tfile"
4258         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4259         test_mkdir -p $DIR/$tdir
4260         mount -t ext2 $loopdev $DIR/$tdir ||
4261                 error "error mounting $loopdev on $DIR/$tdir"
4262         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4263                 error "dd write"
4264         df $DIR/$tdir
4265         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4266                 error "dd read"
4267         cleanup_54c
4268 }
4269 run_test 54c "block device works in lustre ====================="
4270
4271 test_54d() {
4272         f="$DIR/f54d"
4273         string="aaaaaa"
4274         mknod $f p
4275         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4276 }
4277 run_test 54d "fifo device works in lustre ======================"
4278
4279 test_54e() {
4280         check_kernel_version 46 || return 0
4281         f="$DIR/f54e"
4282         string="aaaaaa"
4283         cp -aL /dev/console $f
4284         echo $string > $f || error "echo $string to $f failed"
4285 }
4286 run_test 54e "console/tty device works in lustre ======================"
4287
4288 #The test_55 used to be iopen test and it was removed by bz#24037.
4289 #run_test 55 "check iopen_connect_dentry() ======================"
4290
4291 test_56a() {    # was test_56
4292         rm -rf $DIR/$tdir
4293         $SETSTRIPE -d $DIR
4294         test_mkdir -p $DIR/$tdir/dir
4295         NUMFILES=3
4296         NUMFILESx2=$(($NUMFILES * 2))
4297         for i in $(seq 1 $NUMFILES); do
4298                 touch $DIR/$tdir/file$i
4299                 touch $DIR/$tdir/dir/file$i
4300         done
4301
4302         # test lfs getstripe with --recursive
4303         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4304         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4305                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4306         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4307         [[ $FILENUM -eq $NUMFILES ]] ||
4308                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4309         echo "$GETSTRIPE --recursive passed."
4310
4311         # test lfs getstripe with file instead of dir
4312         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4313         [[ $FILENUM -eq 1 ]] ||
4314                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4315         echo "$GETSTRIPE file1 passed."
4316
4317         #test lfs getstripe with --verbose
4318         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4319                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4320                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4321         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4322                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4323         echo "$GETSTRIPE --verbose passed."
4324
4325         #test lfs getstripe with --obd
4326         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4327                 grep -q "unknown obduuid" ||
4328                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4329
4330         [[ $OSTCOUNT -lt 2 ]] &&
4331                 skip_env "skipping other $GETSTRIPE --obd test" && return
4332
4333         OSTIDX=1
4334         OBDUUID=$(ostuuid_from_index $OSTIDX)
4335         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4336         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4337         [[ $FOUND -eq $FILENUM ]] ||
4338                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4339         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4340                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4341                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4342                 error "$GETSTRIPE --obd: should not show file on other obd"
4343         echo "$GETSTRIPE --obd passed"
4344 }
4345 run_test 56a "check $GETSTRIPE"
4346
4347 NUMFILES=3
4348 NUMDIRS=3
4349 setup_56() {
4350         local LOCAL_NUMFILES="$1"
4351         local LOCAL_NUMDIRS="$2"
4352         local MKDIR_PARAMS="$3"
4353         local DIR_STRIPE_PARAMS="$4"
4354
4355         if [ ! -d "$TDIR" ] ; then
4356                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4357                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4358                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4359                         touch $TDIR/file$i
4360                 done
4361                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4362                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4363                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4364                                 touch $TDIR/dir$i/file$j
4365                         done
4366                 done
4367         fi
4368 }
4369
4370 setup_56_special() {
4371         LOCAL_NUMFILES=$1
4372         LOCAL_NUMDIRS=$2
4373         setup_56 $1 $2
4374         if [ ! -e "$TDIR/loop1b" ] ; then
4375                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4376                         mknod $TDIR/loop${i}b b 7 $i
4377                         mknod $TDIR/null${i}c c 1 3
4378                         ln -s $TDIR/file1 $TDIR/link${i}l
4379                 done
4380                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4381                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4382                         mknod $TDIR/dir$i/null${i}c c 1 3
4383                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4384                 done
4385         fi
4386 }
4387
4388 test_56g() {
4389         $SETSTRIPE -d $DIR
4390
4391         TDIR=$DIR/${tdir}g
4392         setup_56 $NUMFILES $NUMDIRS
4393
4394         EXPECTED=$(($NUMDIRS + 2))
4395         # test lfs find with -name
4396         for i in $(seq 1 $NUMFILES) ; do
4397                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4398                 [ $NUMS -eq $EXPECTED ] ||
4399                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4400                               "found $NUMS, expected $EXPECTED"
4401         done
4402 }
4403 run_test 56g "check lfs find -name ============================="
4404
4405 test_56h() {
4406         $SETSTRIPE -d $DIR
4407
4408         TDIR=$DIR/${tdir}g
4409         setup_56 $NUMFILES $NUMDIRS
4410
4411         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4412         # test lfs find with ! -name
4413         for i in $(seq 1 $NUMFILES) ; do
4414                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4415                 [ $NUMS -eq $EXPECTED ] ||
4416                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4417                               "found $NUMS, expected $EXPECTED"
4418         done
4419 }
4420 run_test 56h "check lfs find ! -name ============================="
4421
4422 test_56i() {
4423        tdir=${tdir}i
4424        test_mkdir -p $DIR/$tdir
4425        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4426        CMD="$LFIND -ost $UUID $DIR/$tdir"
4427        OUT=$($CMD)
4428        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4429 }
4430 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4431
4432 test_56j() {
4433         TDIR=$DIR/${tdir}g
4434         setup_56_special $NUMFILES $NUMDIRS
4435
4436         EXPECTED=$((NUMDIRS + 1))
4437         CMD="$LFIND -type d $TDIR"
4438         NUMS=$($CMD | wc -l)
4439         [ $NUMS -eq $EXPECTED ] ||
4440                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4441 }
4442 run_test 56j "check lfs find -type d ============================="
4443
4444 test_56k() {
4445         TDIR=$DIR/${tdir}g
4446         setup_56_special $NUMFILES $NUMDIRS
4447
4448         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4449         CMD="$LFIND -type f $TDIR"
4450         NUMS=$($CMD | wc -l)
4451         [ $NUMS -eq $EXPECTED ] ||
4452                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4453 }
4454 run_test 56k "check lfs find -type f ============================="
4455
4456 test_56l() {
4457         TDIR=$DIR/${tdir}g
4458         setup_56_special $NUMFILES $NUMDIRS
4459
4460         EXPECTED=$((NUMDIRS + NUMFILES))
4461         CMD="$LFIND -type b $TDIR"
4462         NUMS=$($CMD | wc -l)
4463         [ $NUMS -eq $EXPECTED ] ||
4464                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4465 }
4466 run_test 56l "check lfs find -type b ============================="
4467
4468 test_56m() {
4469         TDIR=$DIR/${tdir}g
4470         setup_56_special $NUMFILES $NUMDIRS
4471
4472         EXPECTED=$((NUMDIRS + NUMFILES))
4473         CMD="$LFIND -type c $TDIR"
4474         NUMS=$($CMD | wc -l)
4475         [ $NUMS -eq $EXPECTED ] ||
4476                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4477 }
4478 run_test 56m "check lfs find -type c ============================="
4479
4480 test_56n() {
4481         TDIR=$DIR/${tdir}g
4482         setup_56_special $NUMFILES $NUMDIRS
4483
4484         EXPECTED=$((NUMDIRS + NUMFILES))
4485         CMD="$LFIND -type l $TDIR"
4486         NUMS=$($CMD | wc -l)
4487         [ $NUMS -eq $EXPECTED ] ||
4488                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4489 }
4490 run_test 56n "check lfs find -type l ============================="
4491
4492 test_56o() {
4493         TDIR=$DIR/${tdir}o
4494         setup_56 $NUMFILES $NUMDIRS
4495         utime $TDIR/file1 > /dev/null || error "utime (1)"
4496         utime $TDIR/file2 > /dev/null || error "utime (2)"
4497         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4498         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4499         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4500         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4501
4502         EXPECTED=4
4503         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4504         [ $NUMS -eq $EXPECTED ] || \
4505                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4506
4507         EXPECTED=12
4508         CMD="$LFIND -mtime 0 $TDIR"
4509         NUMS=$($CMD | wc -l)
4510         [ $NUMS -eq $EXPECTED ] ||
4511                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4512 }
4513 run_test 56o "check lfs find -mtime for old files =========================="
4514
4515 test_56p() {
4516         [ $RUNAS_ID -eq $UID ] &&
4517                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4518
4519         TDIR=$DIR/${tdir}p
4520         setup_56 $NUMFILES $NUMDIRS
4521
4522         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4523         EXPECTED=$NUMFILES
4524         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4525         NUMS=$($CMD | wc -l)
4526         [ $NUMS -eq $EXPECTED ] || \
4527                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4528
4529         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4530         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4531         NUMS=$($CMD | wc -l)
4532         [ $NUMS -eq $EXPECTED ] || \
4533                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4534 }
4535 run_test 56p "check lfs find -uid and ! -uid ==============================="
4536
4537 test_56q() {
4538         [ $RUNAS_ID -eq $UID ] &&
4539                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4540
4541         TDIR=$DIR/${tdir}q
4542         setup_56 $NUMFILES $NUMDIRS
4543
4544         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4545
4546         EXPECTED=$NUMFILES
4547         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4548         NUMS=$($CMD | wc -l)
4549         [ $NUMS -eq $EXPECTED ] ||
4550                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4551
4552         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4553         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4554         NUMS=$($CMD | wc -l)
4555         [ $NUMS -eq $EXPECTED ] ||
4556                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4557 }
4558 run_test 56q "check lfs find -gid and ! -gid ==============================="
4559
4560 test_56r() {
4561         TDIR=$DIR/${tdir}r
4562         setup_56 $NUMFILES $NUMDIRS
4563
4564         EXPECTED=12
4565         CMD="$LFIND -size 0 -type f $TDIR"
4566         NUMS=$($CMD | wc -l)
4567         [ $NUMS -eq $EXPECTED ] ||
4568                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4569         EXPECTED=0
4570         CMD="$LFIND ! -size 0 -type f $TDIR"
4571         NUMS=$($CMD | wc -l)
4572         [ $NUMS -eq $EXPECTED ] ||
4573                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4574         echo "test" > $TDIR/$tfile
4575         echo "test2" > $TDIR/$tfile.2 && sync
4576         EXPECTED=1
4577         CMD="$LFIND -size 5 -type f $TDIR"
4578         NUMS=$($CMD | wc -l)
4579         [ $NUMS -eq $EXPECTED ] ||
4580                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4581         EXPECTED=1
4582         CMD="$LFIND -size +5 -type f $TDIR"
4583         NUMS=$($CMD | wc -l)
4584         [ $NUMS -eq $EXPECTED ] ||
4585                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4586         EXPECTED=2
4587         CMD="$LFIND -size +0 -type f $TDIR"
4588         NUMS=$($CMD | wc -l)
4589         [ $NUMS -eq $EXPECTED ] ||
4590                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4591         EXPECTED=2
4592         CMD="$LFIND ! -size -5 -type f $TDIR"
4593         NUMS=$($CMD | wc -l)
4594         [ $NUMS -eq $EXPECTED ] ||
4595                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4596         EXPECTED=12
4597         CMD="$LFIND -size -5 -type f $TDIR"
4598         NUMS=$($CMD | wc -l)
4599         [ $NUMS -eq $EXPECTED ] ||
4600                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4601 }
4602 run_test 56r "check lfs find -size works =========================="
4603
4604 test_56s() { # LU-611
4605         TDIR=$DIR/${tdir}s
4606         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4607
4608         if [[ $OSTCOUNT -gt 1 ]]; then
4609                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4610                 ONESTRIPE=4
4611                 EXTRA=4
4612         else
4613                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4614                 EXTRA=0
4615         fi
4616
4617         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4618         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4619         NUMS=$($CMD | wc -l)
4620         [ $NUMS -eq $EXPECTED ] || {
4621                 $GETSTRIPE -R $TDIR
4622                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4623         }
4624
4625         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4626         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4627         NUMS=$($CMD | wc -l)
4628         [ $NUMS -eq $EXPECTED ] || {
4629                 $GETSTRIPE -R $TDIR
4630                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4631         }
4632
4633         EXPECTED=$ONESTRIPE
4634         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4635         NUMS=$($CMD | wc -l)
4636         [ $NUMS -eq $EXPECTED ] || {
4637                 $GETSTRIPE -R $TDIR
4638                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4639         }
4640
4641         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4642         NUMS=$($CMD | wc -l)
4643         [ $NUMS -eq $EXPECTED ] || {
4644                 $GETSTRIPE -R $TDIR
4645                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4646         }
4647
4648         EXPECTED=0
4649         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4650         NUMS=$($CMD | wc -l)
4651         [ $NUMS -eq $EXPECTED ] || {
4652                 $GETSTRIPE -R $TDIR
4653                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4654         }
4655 }
4656 run_test 56s "check lfs find -stripe-count works"
4657
4658 test_56t() { # LU-611
4659         TDIR=$DIR/${tdir}t
4660         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4661
4662         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4663
4664         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4665         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4666         NUMS=$($CMD | wc -l)
4667         [ $NUMS -eq $EXPECTED ] ||
4668                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4669
4670         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4671         NUMS=$($CMD | wc -l)
4672         [ $NUMS -eq $EXPECTED ] ||
4673                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4674
4675         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4676         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4677         NUMS=$($CMD | wc -l)
4678         [ $NUMS -eq $EXPECTED ] ||
4679                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4680
4681         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4682         NUMS=$($CMD | wc -l)
4683         [ $NUMS -eq $EXPECTED ] ||
4684                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4685
4686         EXPECTED=4
4687         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4688         NUMS=$($CMD | wc -l)
4689         [ $NUMS -eq $EXPECTED ] ||
4690                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4691
4692         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4693         NUMS=$($CMD | wc -l)
4694         [ $NUMS -eq $EXPECTED ] ||
4695                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4696
4697         EXPECTED=0
4698         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4699         NUMS=$($CMD | wc -l)
4700         [ $NUMS -eq $EXPECTED ] ||
4701                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4702 }
4703 run_test 56t "check lfs find -stripe-size works"
4704
4705 test_56u() { # LU-611
4706         TDIR=$DIR/${tdir}u
4707         setup_56 $NUMFILES $NUMDIRS "-i 0"
4708
4709         if [[ $OSTCOUNT -gt 1 ]]; then
4710                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4711                 ONESTRIPE=4
4712         else
4713                 ONESTRIPE=0
4714         fi
4715
4716         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4717         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4718         NUMS=$($CMD | wc -l)
4719         [ $NUMS -eq $EXPECTED ] ||
4720                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4721
4722         EXPECTED=$ONESTRIPE
4723         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4724         NUMS=$($CMD | wc -l)
4725         [ $NUMS -eq $EXPECTED ] ||
4726                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4727
4728         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4729         NUMS=$($CMD | wc -l)
4730         [ $NUMS -eq $EXPECTED ] ||
4731                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4732
4733         EXPECTED=0
4734         # This should produce an error and not return any files
4735         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4736         NUMS=$($CMD 2>/dev/null | wc -l)
4737         [ $NUMS -eq $EXPECTED ] ||
4738                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4739
4740         if [[ $OSTCOUNT -gt 1 ]]; then
4741                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4742                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4743                 NUMS=$($CMD | wc -l)
4744                 [ $NUMS -eq $EXPECTED ] ||
4745                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4746         fi
4747 }
4748 run_test 56u "check lfs find -stripe-index works"
4749
4750 test_56v() {
4751     local MDT_IDX=0
4752
4753     TDIR=$DIR/${tdir}v
4754     rm -rf $TDIR
4755     setup_56 $NUMFILES $NUMDIRS
4756
4757     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4758     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4759
4760     for file in $($LFIND -mdt $UUID $TDIR); do
4761         file_mdt_idx=$($GETSTRIPE -M $file)
4762         [ $file_mdt_idx -eq $MDT_IDX ] ||
4763             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4764     done
4765 }
4766 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4767
4768 test_56w() {
4769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4770         TDIR=$DIR/${tdir}w
4771
4772     rm -rf $TDIR || error "remove $TDIR failed"
4773     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4774
4775     local stripe_size
4776     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4777         error "$GETSTRIPE -S -d $TDIR failed"
4778     stripe_size=${stripe_size%% *}
4779
4780     local file_size=$((stripe_size * OSTCOUNT))
4781     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4782     local required_space=$((file_num * file_size))
4783     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4784     [[ $free_space -le $((required_space / 1024)) ]] &&
4785         skip_env "need at least $required_space bytes free space," \
4786                  "have $free_space kbytes" && return
4787
4788     local dd_bs=65536
4789     local dd_count=$((file_size / dd_bs))
4790
4791     # write data into the files
4792     local i
4793     local j
4794     local file
4795     for i in $(seq 1 $NUMFILES); do
4796         file=$TDIR/file$i
4797         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4798             error "write data into $file failed"
4799     done
4800     for i in $(seq 1 $NUMDIRS); do
4801         for j in $(seq 1 $NUMFILES); do
4802             file=$TDIR/dir$i/file$j
4803             yes | dd bs=$dd_bs count=$dd_count of=$file \
4804                 >/dev/null 2>&1 ||
4805                 error "write data into $file failed"
4806         done
4807     done
4808
4809     local expected=-1
4810     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4811
4812     # lfs_migrate file
4813     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4814     echo "$cmd"
4815     eval $cmd || error "$cmd failed"
4816
4817     check_stripe_count $TDIR/file1 $expected
4818
4819     # lfs_migrate dir
4820     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4821     echo "$cmd"
4822     eval $cmd || error "$cmd failed"
4823
4824     for j in $(seq 1 $NUMFILES); do
4825         check_stripe_count $TDIR/dir1/file$j $expected
4826     done
4827
4828     # lfs_migrate works with lfs find
4829     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4830          $LFS_MIGRATE -y -c $expected"
4831     echo "$cmd"
4832     eval $cmd || error "$cmd failed"
4833
4834     for i in $(seq 2 $NUMFILES); do
4835         check_stripe_count $TDIR/file$i $expected
4836     done
4837     for i in $(seq 2 $NUMDIRS); do
4838         for j in $(seq 1 $NUMFILES); do
4839             check_stripe_count $TDIR/dir$i/file$j $expected
4840         done
4841     done
4842 }
4843 run_test 56w "check lfs_migrate -c stripe_count works"
4844
4845 test_56x() {
4846         check_swap_layouts_support && return 0
4847         [[ $OSTCOUNT -lt 2 ]] &&
4848                 skip_env "need 2 OST, skipping test" && return
4849
4850         local dir0=$DIR/$tdir/$testnum
4851         mkdir -p $dir0 || error "creating dir $dir0"
4852
4853         local ref1=/etc/passwd
4854         local file1=$dir0/file1
4855
4856         $SETSTRIPE -c 2 $file1
4857         cp $ref1 $file1
4858         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4859         stripe=$($GETSTRIPE -c $file1)
4860         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4861         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4862
4863         # clean up
4864         rm -f $file1
4865 }
4866 run_test 56x "lfs migration support"
4867
4868 test_56y() {
4869         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4870                 skip "No HSM support on MDS of $(get_lustre_version)," \
4871                          "need 2.4.53 at least" && return
4872         local res=""
4873
4874         local dir0=$DIR/$tdir/$testnum
4875         mkdir -p $dir0 || error "creating dir $dir0"
4876         local f1=$dir0/file1
4877         local f2=$dir0/file2
4878
4879         touch $f1 || error "creating std file $f1"
4880         $MULTIOP $f2 H2c || error "creating released file $f2"
4881
4882         # a directory can be raid0, so ask only for files
4883         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4884         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4885
4886         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4887         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4888
4889         # only files can be released, so no need to force file search
4890         res=$($LFIND $dir0 -L released)
4891         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4892
4893         res=$($LFIND $dir0 \! -L released)
4894         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4895
4896 }
4897 run_test 56y "lfs find -L raid0|released"
4898
4899 test_56z() { # LU-4824
4900         # This checks to make sure 'lfs find' continues after errors
4901         # There are two classes of errors that should be caught:
4902         # - If multiple paths are provided, all should be searched even if one
4903         #   errors out
4904         # - If errors are encountered during the search, it should not terminate
4905         #   early
4906         local i
4907         test_mkdir $DIR/$tdir
4908         for i in d{0..9}; do
4909                 test_mkdir $DIR/$tdir/$i
4910         done
4911         touch $DIR/$tdir/d{0..9}/$tfile
4912         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4913                 error "$LFS find did not return an error"
4914         # Make a directory unsearchable. This should NOT be the last entry in
4915         # directory order.  Arbitrarily pick the 6th entry
4916         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
4917         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
4918         # The user should be able to see 10 directories and 9 files
4919         [ $count == 19 ] || error "$LFS find did not continue after error"
4920 }
4921 run_test 56z "lfs find should continue after an error"
4922
4923 test_57a() {
4924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4925         # note test will not do anything if MDS is not local
4926         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4927                 skip "Only applicable to ldiskfs-based MDTs"
4928                 return
4929         fi
4930
4931         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4932         local MNTDEV="osd*.*MDT*.mntdev"
4933         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4934         [ -z "$DEV" ] && error "can't access $MNTDEV"
4935         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4936                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4937                         error "can't access $DEV"
4938                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4939                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4940                 rm $TMP/t57a.dump
4941         done
4942 }
4943 run_test 57a "verify MDS filesystem created with large inodes =="
4944
4945 test_57b() {
4946         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4947         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4948                 skip "Only applicable to ldiskfs-based MDTs"
4949                 return
4950         fi
4951
4952         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4953         local dir=$DIR/d57b
4954
4955         local FILECOUNT=100
4956         local FILE1=$dir/f1
4957         local FILEN=$dir/f$FILECOUNT
4958
4959         rm -rf $dir || error "removing $dir"
4960         test_mkdir -p $dir || error "creating $dir"
4961         local num=$(get_mds_dir $dir)
4962         local mymds=mds$num
4963
4964         echo "mcreating $FILECOUNT files"
4965         createmany -m $dir/f 1 $FILECOUNT || \
4966                 error "creating files in $dir"
4967
4968         # verify that files do not have EAs yet
4969         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4970         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4971
4972         sync
4973         sleep 1
4974         df $dir  #make sure we get new statfs data
4975         local MDSFREE=$(do_facet $mymds \
4976                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4977         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4978         echo "opening files to create objects/EAs"
4979         local FILE
4980         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4981                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4982         done
4983
4984         # verify that files have EAs now
4985         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4986         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4987
4988         sleep 1  #make sure we get new statfs data
4989         df $dir
4990         local MDSFREE2=$(do_facet $mymds \
4991                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4992         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4993         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4994                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4995                         error "MDC before $MDCFREE != after $MDCFREE2"
4996                 else
4997                         echo "MDC before $MDCFREE != after $MDCFREE2"
4998                         echo "unable to confirm if MDS has large inodes"
4999                 fi
5000         fi
5001         rm -rf $dir
5002 }
5003 run_test 57b "default LOV EAs are stored inside large inodes ==="
5004
5005 test_58() {
5006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5007         [ -z "$(which wiretest 2>/dev/null)" ] &&
5008                         skip_env "could not find wiretest" && return
5009         wiretest
5010 }
5011 run_test 58 "verify cross-platform wire constants =============="
5012
5013 test_59() {
5014         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5015         echo "touch 130 files"
5016         createmany -o $DIR/f59- 130
5017         echo "rm 130 files"
5018         unlinkmany $DIR/f59- 130
5019         sync
5020         # wait for commitment of removal
5021         wait_delete_completed
5022 }
5023 run_test 59 "verify cancellation of llog records async ========="
5024
5025 TEST60_HEAD="test_60 run $RANDOM"
5026 test_60a() {
5027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5028         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5029         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5030                 skip_env "missing subtest run-llog.sh" && return
5031         log "$TEST60_HEAD - from kernel mode"
5032         do_facet mgs sh run-llog.sh
5033 }
5034 run_test 60a "llog sanity tests run from kernel module =========="
5035
5036 test_60b() { # bug 6411
5037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5038         dmesg > $DIR/$tfile
5039         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5040                                  /llog.test/ {
5041                                          if (marker)
5042                                                  from_marker++
5043                                          from_begin++
5044                                  }
5045                                  END {
5046                                          if (marker)
5047                                                  print from_marker
5048                                          else
5049                                                  print from_begin
5050                                  }"`
5051         [[ $LLOG_COUNT -gt 50 ]] &&
5052                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5053 }
5054 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5055
5056 test_60c() {
5057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5058         echo "create 5000 files"
5059         createmany -o $DIR/f60c- 5000
5060 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5061         lctl set_param fail_loc=0x80000137
5062         unlinkmany $DIR/f60c- 5000
5063         lctl set_param fail_loc=0
5064 }
5065 run_test 60c "unlink file when mds full"
5066
5067 test_60d() {
5068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5069         SAVEPRINTK=$(lctl get_param -n printk)
5070
5071         # verify "lctl mark" is even working"
5072         MESSAGE="test message ID $RANDOM $$"
5073         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5074         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5075
5076         lctl set_param printk=0 || error "set lnet.printk failed"
5077         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5078         MESSAGE="new test message ID $RANDOM $$"
5079         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5080         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5081         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5082
5083         lctl set_param -n printk="$SAVEPRINTK"
5084 }
5085 run_test 60d "test printk console message masking"
5086
5087 test_61() {
5088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5089         f="$DIR/f61"
5090         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5091         cancel_lru_locks osc
5092         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5093         sync
5094 }
5095 run_test 61 "mmap() writes don't make sync hang ================"
5096
5097 # bug 2330 - insufficient obd_match error checking causes LBUG
5098 test_62() {
5099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5100         f="$DIR/f62"
5101         echo foo > $f
5102         cancel_lru_locks osc
5103         lctl set_param fail_loc=0x405
5104         cat $f && error "cat succeeded, expect -EIO"
5105         lctl set_param fail_loc=0
5106 }
5107 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5108 # match every page all of the time.
5109 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5110
5111 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5112 # Though this test is irrelevant anymore, it helped to reveal some
5113 # other grant bugs (LU-4482), let's keep it.
5114 test_63a() {   # was test_63
5115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5116         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5117         for i in `seq 10` ; do
5118                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5119                 sleep 5
5120                 kill $!
5121                 sleep 1
5122         done
5123
5124         rm -f $DIR/f63 || true
5125 }
5126 run_test 63a "Verify oig_wait interruption does not crash ======="
5127
5128 # bug 2248 - async write errors didn't return to application on sync
5129 # bug 3677 - async write errors left page locked
5130 test_63b() {
5131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5132         debugsave
5133         lctl set_param debug=-1
5134
5135         # ensure we have a grant to do async writes
5136         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5137         rm $DIR/$tfile
5138
5139         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5140         lctl set_param fail_loc=0x80000406
5141         $MULTIOP $DIR/$tfile Owy && \
5142                 error "sync didn't return ENOMEM"
5143         sync; sleep 2; sync     # do a real sync this time to flush page
5144         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5145                 error "locked page left in cache after async error" || true
5146         debugrestore
5147 }
5148 run_test 63b "async write errors should be returned to fsync ==="
5149
5150 test_64a () {
5151         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5152         df $DIR
5153         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5154 }
5155 run_test 64a "verify filter grant calculations (in kernel) ====="
5156
5157 test_64b () {
5158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5159         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5160 }
5161 run_test 64b "check out-of-space detection on client ==========="
5162
5163 test_64c() {
5164         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5165 }
5166 run_test 64c "verify grant shrink ========================------"
5167
5168 # bug 1414 - set/get directories' stripe info
5169 test_65a() {
5170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5171         test_mkdir -p $DIR/$tdir
5172         touch $DIR/$tdir/f1
5173         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5174 }
5175 run_test 65a "directory with no stripe info ===================="
5176
5177 test_65b() {
5178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5179         test_mkdir -p $DIR/$tdir
5180         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5181                                                 error "setstripe"
5182         touch $DIR/$tdir/f2
5183         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5184 }
5185 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5186
5187 test_65c() {
5188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5189         if [[ $OSTCOUNT -gt 1 ]]; then
5190                 test_mkdir -p $DIR/$tdir
5191                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5192                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5193                 touch $DIR/$tdir/f3
5194                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5195         fi
5196 }
5197 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5198
5199 test_65d() {
5200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5201         test_mkdir -p $DIR/$tdir
5202         if [[ $STRIPECOUNT -le 0 ]]; then
5203                 sc=1
5204         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5205 #LOV_MAX_STRIPE_COUNT is 2000
5206                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5207         else
5208                 sc=$(($STRIPECOUNT - 1))
5209         fi
5210         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5211         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5212         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5213                 error "lverify failed"
5214 }
5215 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5216
5217 test_65e() {
5218         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5219         test_mkdir -p $DIR/$tdir
5220
5221         $SETSTRIPE $DIR/$tdir || error "setstripe"
5222         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5223                                         error "no stripe info failed"
5224         touch $DIR/$tdir/f6
5225         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5226 }
5227 run_test 65e "directory setstripe defaults ======================="
5228
5229 test_65f() {
5230         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5231         test_mkdir -p $DIR/${tdir}f
5232         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5233 }
5234 run_test 65f "dir setstripe permission (should return error) ==="
5235
5236 test_65g() {
5237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5238         test_mkdir -p $DIR/$tdir
5239         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5240                                                         error "setstripe"
5241         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5242         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5243                 error "delete default stripe failed"
5244 }
5245 run_test 65g "directory setstripe -d ==========================="
5246
5247 test_65h() {
5248         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5249         test_mkdir -p $DIR/$tdir
5250         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5251                                                         error "setstripe"
5252         test_mkdir -p $DIR/$tdir/dd1
5253         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5254                 error "stripe info inherit failed"
5255 }
5256 run_test 65h "directory stripe info inherit ===================="
5257
5258 test_65i() { # bug6367
5259         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5260         $SETSTRIPE -S 65536 -c -1 $MOUNT
5261 }
5262 run_test 65i "set non-default striping on root directory (bug 6367)="
5263
5264 test_65ia() { # bug12836
5265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5266         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5267 }
5268 run_test 65ia "getstripe on -1 default directory striping"
5269
5270 test_65ib() { # bug12836
5271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5272         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5273 }
5274 run_test 65ib "getstripe -v on -1 default directory striping"
5275
5276 test_65ic() { # bug12836
5277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5278         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5279 }
5280 run_test 65ic "new find on -1 default directory striping"
5281
5282 test_65j() { # bug6367
5283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5284         sync; sleep 1
5285         # if we aren't already remounting for each test, do so for this test
5286         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5287                 cleanup || error "failed to unmount"
5288                 setup
5289         fi
5290         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5291 }
5292 run_test 65j "set default striping on root directory (bug 6367)="
5293
5294 test_65k() { # bug11679
5295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5296         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5297         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5298
5299     echo "Check OST status: "
5300     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5301               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5302
5303     for OSC in $MDS_OSCS; do
5304         echo $OSC "is activate"
5305         do_facet $SINGLEMDS lctl --device %$OSC activate
5306     done
5307
5308     mkdir -p $DIR/$tdir
5309     for INACTIVE_OSC in $MDS_OSCS; do
5310         echo "Deactivate: " $INACTIVE_OSC
5311         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5312         for STRIPE_OSC in $MDS_OSCS; do
5313             OST=`osc_to_ost $STRIPE_OSC`
5314             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5315                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5316
5317             [ -f $DIR/$tdir/$IDX ] && continue
5318             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5319             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5320             RC=$?
5321             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5322         done
5323         rm -f $DIR/$tdir/*
5324         echo $INACTIVE_OSC "is Activate."
5325         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5326     done
5327 }
5328 run_test 65k "validate manual striping works properly with deactivated OSCs"
5329
5330 test_65l() { # bug 12836
5331         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5332         test_mkdir -p $DIR/$tdir/test_dir
5333         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5334         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5335 }
5336 run_test 65l "lfs find on -1 stripe dir ========================"
5337
5338 # bug 2543 - update blocks count on client
5339 test_66() {
5340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5341         COUNT=${COUNT:-8}
5342         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5343         sync; sync_all_data; sync; sync_all_data
5344         cancel_lru_locks osc
5345         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5346         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5347 }
5348 run_test 66 "update inode blocks count on client ==============="
5349
5350 LLOOP=
5351 LLITELOOPLOAD=
5352 cleanup_68() {
5353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5354         trap 0
5355         if [ ! -z "$LLOOP" ]; then
5356                 if swapon -s | grep -q $LLOOP; then
5357                         swapoff $LLOOP || error "swapoff failed"
5358                 fi
5359
5360                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5361                 rm -f $LLOOP
5362                 unset LLOOP
5363         fi
5364         if [ ! -z "$LLITELOOPLOAD" ]; then
5365                 rmmod llite_lloop
5366                 unset LLITELOOPLOAD
5367         fi
5368         rm -f $DIR/f68*
5369 }
5370
5371 meminfo() {
5372         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5373 }
5374
5375 swap_used() {
5376         swapon -s | awk '($1 == "'$1'") { print $4 }'
5377 }
5378
5379 # test case for lloop driver, basic function
5380 test_68a() {
5381         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5382         [ "$UID" != 0 ] && skip_env "must run as root" && return
5383         llite_lloop_enabled || \
5384                 { skip_env "llite_lloop module disabled" && return; }
5385
5386         trap cleanup_68 EXIT
5387
5388         if ! module_loaded llite_lloop; then
5389                 if load_module llite/llite_lloop; then
5390                         LLITELOOPLOAD=yes
5391                 else
5392                         skip_env "can't find module llite_lloop"
5393                         return
5394                 fi
5395         fi
5396
5397         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5398         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5399         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5400
5401         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5402         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5403
5404         cleanup_68
5405 }
5406 run_test 68a "lloop driver - basic test ========================"
5407
5408 # excercise swapping to lustre by adding a high priority swapfile entry
5409 # and then consuming memory until it is used.
5410 test_68b() {  # was test_68
5411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5412         [ "$UID" != 0 ] && skip_env "must run as root" && return
5413         lctl get_param -n devices | grep -q obdfilter && \
5414                 skip "local OST" && return
5415
5416         grep -q llite_lloop /proc/modules
5417         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5418
5419         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5420                 skip "can't reliably test swap with TCP" && return
5421
5422         MEMTOTAL=`meminfo MemTotal`
5423         NR_BLOCKS=$((MEMTOTAL>>8))
5424         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5425
5426         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5427         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5428         mkswap $DIR/f68b
5429
5430         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5431
5432         trap cleanup_68 EXIT
5433
5434         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5435
5436         echo "before: `swapon -s | grep $LLOOP`"
5437         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5438         echo "after: `swapon -s | grep $LLOOP`"
5439         SWAPUSED=`swap_used $LLOOP`
5440
5441         cleanup_68
5442
5443         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5444 }
5445 run_test 68b "support swapping to Lustre ========================"
5446
5447 # bug5265, obdfilter oa2dentry return -ENOENT
5448 # #define OBD_FAIL_OST_ENOENT 0x217
5449 test_69() {
5450         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5451         remote_ost_nodsh && skip "remote OST with nodsh" && return
5452
5453         f="$DIR/$tfile"
5454         $SETSTRIPE -c 1 -i 0 $f
5455
5456         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5457
5458         do_facet ost1 lctl set_param fail_loc=0x217
5459         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5460         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5461
5462         do_facet ost1 lctl set_param fail_loc=0
5463         $DIRECTIO write $f 0 2 || error "write error"
5464
5465         cancel_lru_locks osc
5466         $DIRECTIO read $f 0 1 || error "read error"
5467
5468         do_facet ost1 lctl set_param fail_loc=0x217
5469         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5470
5471         do_facet ost1 lctl set_param fail_loc=0
5472         rm -f $f
5473 }
5474 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5475
5476 test_71() {
5477     test_mkdir -p $DIR/$tdir
5478     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5479 }
5480 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5481
5482 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5484         [ "$RUNAS_ID" = "$UID" ] &&
5485                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5486
5487         # Check that testing environment is properly set up. Skip if not
5488         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5489                 skip_env "User $RUNAS_ID does not exist - skipping"
5490                 return 0
5491         }
5492         touch $DIR/$tfile
5493         chmod 777 $DIR/$tfile
5494         chmod ug+s $DIR/$tfile
5495         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5496                 error "$RUNAS dd $DIR/$tfile failed"
5497         # See if we are still setuid/sgid
5498         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5499                 error "S/gid is not dropped on write"
5500         # Now test that MDS is updated too
5501         cancel_lru_locks mdc
5502         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5503                 error "S/gid is not dropped on MDS"
5504         rm -f $DIR/$tfile
5505 }
5506 run_test 72a "Test that remove suid works properly (bug5695) ===="
5507
5508 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5509         local perm
5510
5511         [ "$RUNAS_ID" = "$UID" ] && \
5512                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5513         [ "$RUNAS_ID" -eq 0 ] && \
5514                 skip_env "RUNAS_ID = 0 -- skipping" && return
5515
5516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5517         # Check that testing environment is properly set up. Skip if not
5518         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5519                 skip_env "User $RUNAS_ID does not exist - skipping"
5520                 return 0
5521         }
5522         touch $DIR/${tfile}-f{g,u}
5523         test_mkdir $DIR/${tfile}-dg
5524         test_mkdir $DIR/${tfile}-du
5525         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5526         chmod g+s $DIR/${tfile}-{f,d}g
5527         chmod u+s $DIR/${tfile}-{f,d}u
5528         for perm in 777 2777 4777; do
5529                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5530                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5531                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5532                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5533         done
5534         true
5535 }
5536 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5537
5538 # bug 3462 - multiple simultaneous MDC requests
5539 test_73() {
5540         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5541         test_mkdir $DIR/d73-1
5542         test_mkdir $DIR/d73-2
5543         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5544         pid1=$!
5545
5546         lctl set_param fail_loc=0x80000129
5547         $MULTIOP $DIR/d73-1/f73-2 Oc &
5548         sleep 1
5549         lctl set_param fail_loc=0
5550
5551         $MULTIOP $DIR/d73-2/f73-3 Oc &
5552         pid3=$!
5553
5554         kill -USR1 $pid1
5555         wait $pid1 || return 1
5556
5557         sleep 25
5558
5559         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5560         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5561         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5562
5563         rm -rf $DIR/d73-*
5564 }
5565 run_test 73 "multiple MDC requests (should not deadlock)"
5566
5567 test_74a() { # bug 6149, 6184
5568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5569         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5570         #
5571         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5572         # will spin in a tight reconnection loop
5573         touch $DIR/f74a
5574         $LCTL set_param fail_loc=0x8000030e
5575         # get any lock that won't be difficult - lookup works.
5576         ls $DIR/f74a
5577         $LCTL set_param fail_loc=0
5578         rm -f $DIR/f74a
5579         true
5580 }
5581 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5582
5583 test_74b() { # bug 13310
5584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5585         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5586         #
5587         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5588         # will spin in a tight reconnection loop
5589         $LCTL set_param fail_loc=0x8000030e
5590         # get a "difficult" lock
5591         touch $DIR/f74b
5592         $LCTL set_param fail_loc=0
5593         rm -f $DIR/f74b
5594         true
5595 }
5596 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5597
5598 test_74c() {
5599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5600         #define OBD_FAIL_LDLM_NEW_LOCK
5601         $LCTL set_param fail_loc=0x319
5602         touch $DIR/$tfile && error "touch successful"
5603         $LCTL set_param fail_loc=0
5604         true
5605 }
5606 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5607
5608 num_inodes() {
5609         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5610 }
5611
5612 get_inode_slab_tunables() {
5613         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5614 }
5615
5616 set_inode_slab_tunables() {
5617         echo "lustre_inode_cache $1" > /proc/slabinfo
5618 }
5619
5620 test_76() { # Now for bug 20433, added originally in bug 1443
5621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5622         local SLAB_SETTINGS=`get_inode_slab_tunables`
5623         local CPUS=`getconf _NPROCESSORS_ONLN`
5624         # we cannot set limit below 1 which means 1 inode in each
5625         # per-cpu cache is still allowed
5626         set_inode_slab_tunables "1 1 0"
5627         cancel_lru_locks osc
5628         BEFORE_INODES=$(num_inodes)
5629         echo "before inodes: $BEFORE_INODES"
5630         local COUNT=1000
5631         [ "$SLOW" = "no" ] && COUNT=100
5632         for i in $(seq $COUNT); do
5633                 touch $DIR/$tfile
5634                 rm -f $DIR/$tfile
5635         done
5636         cancel_lru_locks osc
5637         AFTER_INODES=$(num_inodes)
5638         echo "after inodes: $AFTER_INODES"
5639         local wait=0
5640         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5641                 sleep 2
5642                 AFTER_INODES=$(num_inodes)
5643                 wait=$((wait+2))
5644                 echo "wait $wait seconds inodes: $AFTER_INODES"
5645                 if [ $wait -gt 30 ]; then
5646                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5647                 fi
5648         done
5649         set_inode_slab_tunables "$SLAB_SETTINGS"
5650 }
5651 run_test 76 "confirm clients recycle inodes properly ===="
5652
5653
5654 export ORIG_CSUM=""
5655 set_checksums()
5656 {
5657         # Note: in sptlrpc modes which enable its own bulk checksum, the
5658         # original crc32_le bulk checksum will be automatically disabled,
5659         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5660         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5661         # In this case set_checksums() will not be no-op, because sptlrpc
5662         # bulk checksum will be enabled all through the test.
5663
5664         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5665         lctl set_param -n osc.*.checksums $1
5666         return 0
5667 }
5668
5669 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5670                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5671 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5672 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5673 set_checksum_type()
5674 {
5675         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5676         log "set checksum type to $1"
5677         return 0
5678 }
5679 F77_TMP=$TMP/f77-temp
5680 F77SZ=8
5681 setup_f77() {
5682         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5683                 error "error writing to $F77_TMP"
5684 }
5685
5686 test_77a() { # bug 10889
5687         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5688         $GSS && skip "could not run with gss" && return
5689         [ ! -f $F77_TMP ] && setup_f77
5690         set_checksums 1
5691         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5692         set_checksums 0
5693         rm -f $DIR/$tfile
5694 }
5695 run_test 77a "normal checksum read/write operation"
5696
5697 test_77b() { # bug 10889
5698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5699         $GSS && skip "could not run with gss" && return
5700         [ ! -f $F77_TMP ] && setup_f77
5701         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5702         $LCTL set_param fail_loc=0x80000409
5703         set_checksums 1
5704
5705         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5706                 error "dd error: $?"
5707         $LCTL set_param fail_loc=0
5708
5709         for algo in $CKSUM_TYPES; do
5710                 cancel_lru_locks osc
5711                 set_checksum_type $algo
5712                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5713                 $LCTL set_param fail_loc=0x80000408
5714                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5715                 $LCTL set_param fail_loc=0
5716         done
5717         set_checksums 0
5718         set_checksum_type $ORIG_CSUM_TYPE
5719         rm -f $DIR/$tfile
5720 }
5721 run_test 77b "checksum error on client write, read"
5722
5723 test_77d() { # bug 10889
5724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5725         $GSS && skip "could not run with gss" && return
5726         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5727         $LCTL set_param fail_loc=0x80000409
5728         set_checksums 1
5729         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5730                 error "direct write: rc=$?"
5731         $LCTL set_param fail_loc=0
5732         set_checksums 0
5733
5734         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5735         $LCTL set_param fail_loc=0x80000408
5736         set_checksums 1
5737         cancel_lru_locks osc
5738         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5739                 error "direct read: rc=$?"
5740         $LCTL set_param fail_loc=0
5741         set_checksums 0
5742 }
5743 run_test 77d "checksum error on OST direct write, read"
5744
5745 test_77f() { # bug 10889
5746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5747         $GSS && skip "could not run with gss" && return
5748         set_checksums 1
5749         for algo in $CKSUM_TYPES; do
5750                 cancel_lru_locks osc
5751                 set_checksum_type $algo
5752                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5753                 $LCTL set_param fail_loc=0x409
5754                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5755                         error "direct write succeeded"
5756                 $LCTL set_param fail_loc=0
5757         done
5758         set_checksum_type $ORIG_CSUM_TYPE
5759         set_checksums 0
5760 }
5761 run_test 77f "repeat checksum error on write (expect error)"
5762
5763 test_77g() { # bug 10889
5764         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5765         $GSS && skip "could not run with gss" && return
5766         remote_ost_nodsh && skip "remote OST with nodsh" && return
5767
5768         [ ! -f $F77_TMP ] && setup_f77
5769
5770         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5771         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5772         do_facet ost1 lctl set_param fail_loc=0x8000021a
5773         set_checksums 1
5774         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5775                 error "write error: rc=$?"
5776         do_facet ost1 lctl set_param fail_loc=0
5777         set_checksums 0
5778
5779         cancel_lru_locks osc
5780         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5781         do_facet ost1 lctl set_param fail_loc=0x8000021b
5782         set_checksums 1
5783         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5784         do_facet ost1 lctl set_param fail_loc=0
5785         set_checksums 0
5786 }
5787 run_test 77g "checksum error on OST write, read"
5788
5789 test_77i() { # bug 13805
5790         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5791         $GSS && skip "could not run with gss" && return
5792         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5793         lctl set_param fail_loc=0x40b
5794         remount_client $MOUNT
5795         lctl set_param fail_loc=0
5796         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5797                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5798                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5799                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5800         done
5801         remount_client $MOUNT
5802 }
5803 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5804
5805 test_77j() { # bug 13805
5806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5807         $GSS && skip "could not run with gss" && return
5808         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5809         lctl set_param fail_loc=0x40c
5810         remount_client $MOUNT
5811         lctl set_param fail_loc=0
5812         sleep 2 # wait async osc connect to finish
5813         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5814                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5815                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5816                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5817         done
5818         remount_client $MOUNT
5819 }
5820 run_test 77j "client only supporting ADLER32"
5821
5822 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5823 rm -f $F77_TMP
5824 unset F77_TMP
5825
5826 test_78() { # bug 10901
5827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5828         remote_ost || { skip_env "local OST" && return; }
5829
5830         NSEQ=5
5831         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5832         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5833         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5834         echo "MemTotal: $MEMTOTAL"
5835
5836         # reserve 256MB of memory for the kernel and other running processes,
5837         # and then take 1/2 of the remaining memory for the read/write buffers.
5838         if [ $MEMTOTAL -gt 512 ] ;then
5839                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5840         else
5841                 # for those poor memory-starved high-end clusters...
5842                 MEMTOTAL=$((MEMTOTAL / 2))
5843         fi
5844         echo "Mem to use for directio: $MEMTOTAL"
5845
5846         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5847         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5848         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5849         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5850                 head -n1)
5851         echo "Smallest OST: $SMALLESTOST"
5852         [[ $SMALLESTOST -lt 10240 ]] &&
5853                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5854
5855         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5856                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5857
5858         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5859         echo "File size: $F78SIZE"
5860         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5861         for i in $(seq 1 $NSEQ); do
5862                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5863                 echo directIO rdwr round $i of $NSEQ
5864                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5865         done
5866
5867         rm -f $DIR/$tfile
5868 }
5869 run_test 78 "handle large O_DIRECT writes correctly ============"
5870
5871 test_79() { # bug 12743
5872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5873         wait_delete_completed
5874
5875         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5876         BKFREE=$(calc_osc_kbytes kbytesfree)
5877         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5878
5879         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5880         DFTOTAL=`echo $STRING | cut -d, -f1`
5881         DFUSED=`echo $STRING  | cut -d, -f2`
5882         DFAVAIL=`echo $STRING | cut -d, -f3`
5883         DFFREE=$(($DFTOTAL - $DFUSED))
5884
5885         ALLOWANCE=$((64 * $OSTCOUNT))
5886
5887         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5888            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5889                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5890         fi
5891         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5892            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5893                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5894         fi
5895         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5896            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5897                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5898         fi
5899 }
5900 run_test 79 "df report consistency check ======================="
5901
5902 test_80() { # bug 10718
5903         remote_ost_nodsh && skip "remote OST with nodsh" && return
5904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5905         # relax strong synchronous semantics for slow backends like ZFS
5906         local soc="obdfilter.*.sync_on_lock_cancel"
5907         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5908         local hosts=
5909         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5910                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5911                           facet_active_host $host; done | sort -u)
5912                 do_nodes $hosts lctl set_param $soc=never
5913         fi
5914
5915         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5916         sync; sleep 1; sync
5917         local BEFORE=`date +%s`
5918         cancel_lru_locks osc
5919         local AFTER=`date +%s`
5920         local DIFF=$((AFTER-BEFORE))
5921         if [ $DIFF -gt 1 ] ; then
5922                 error "elapsed for 1M@1T = $DIFF"
5923         fi
5924
5925         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5926
5927         rm -f $DIR/$tfile
5928 }
5929 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5930
5931 test_81a() { # LU-456
5932         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5933         remote_ost_nodsh && skip "remote OST with nodsh" && return
5934         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5935         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5936         do_facet ost1 lctl set_param fail_loc=0x80000228
5937
5938         # write should trigger a retry and success
5939         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5940         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5941         RC=$?
5942         if [ $RC -ne 0 ] ; then
5943                 error "write should success, but failed for $RC"
5944         fi
5945 }
5946 run_test 81a "OST should retry write when get -ENOSPC ==============="
5947
5948 test_81b() { # LU-456
5949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5950         remote_ost_nodsh && skip "remote OST with nodsh" && return
5951         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5952         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5953         do_facet ost1 lctl set_param fail_loc=0x228
5954
5955         # write should retry several times and return -ENOSPC finally
5956         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5957         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5958         RC=$?
5959         ENOSPC=28
5960         if [ $RC -ne $ENOSPC ] ; then
5961                 error "dd should fail for -ENOSPC, but succeed."
5962         fi
5963 }
5964 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5965
5966 test_82() { # LU-1031
5967         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5968         local gid1=14091995
5969         local gid2=16022000
5970
5971         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5972         local MULTIPID1=$!
5973         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5974         local MULTIPID2=$!
5975         kill -USR1 $MULTIPID2
5976         sleep 2
5977         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5978                 error "First grouplock does not block second one"
5979         else
5980                 echo "Second grouplock blocks first one"
5981         fi
5982         kill -USR1 $MULTIPID1
5983         wait $MULTIPID1
5984         wait $MULTIPID2
5985 }
5986 run_test 82 "Basic grouplock test ==============================="
5987
5988 test_99a() {
5989         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5990                 return
5991         test_mkdir -p $DIR/d99cvsroot
5992         chown $RUNAS_ID $DIR/d99cvsroot
5993         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5994         cd $TMP
5995
5996         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5997         cd $oldPWD
5998 }
5999 run_test 99a "cvs init ========================================="
6000
6001 test_99b() {
6002         [ -z "$(which cvs 2>/dev/null)" ] &&
6003                 skip_env "could not find cvs" && return
6004         [ ! -d $DIR/d99cvsroot ] && test_99a
6005         cd /etc/init.d
6006         # some versions of cvs import exit(1) when asked to import links or
6007         # files they can't read.  ignore those files.
6008         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6009                         ! -perm +4 -printf '-I %f\n')
6010         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6011                 d99reposname vtag rtag
6012 }
6013 run_test 99b "cvs import ======================================="
6014
6015 test_99c() {
6016         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6017         [ ! -d $DIR/d99cvsroot ] && test_99b
6018         cd $DIR
6019         test_mkdir -p $DIR/d99reposname
6020         chown $RUNAS_ID $DIR/d99reposname
6021         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6022 }
6023 run_test 99c "cvs checkout ====================================="
6024
6025 test_99d() {
6026         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6027         [ ! -d $DIR/d99cvsroot ] && test_99c
6028         cd $DIR/d99reposname
6029         $RUNAS touch foo99
6030         $RUNAS cvs add -m 'addmsg' foo99
6031 }
6032 run_test 99d "cvs add =========================================="
6033
6034 test_99e() {
6035         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6036         [ ! -d $DIR/d99cvsroot ] && test_99c
6037         cd $DIR/d99reposname
6038         $RUNAS cvs update
6039 }
6040 run_test 99e "cvs update ======================================="
6041
6042 test_99f() {
6043         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6044         [ ! -d $DIR/d99cvsroot ] && test_99d
6045         cd $DIR/d99reposname
6046         $RUNAS cvs commit -m 'nomsg' foo99
6047     rm -fr $DIR/d99cvsroot
6048 }
6049 run_test 99f "cvs commit ======================================="
6050
6051 test_100() {
6052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6053         [ "$NETTYPE" = tcp ] || \
6054                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6055                         return ; }
6056
6057         remote_ost_nodsh && skip "remote OST with nodsh" && return
6058         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6059         remote_servers || \
6060                 { skip "useless for local single node setup" && return; }
6061
6062         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6063                 [ "$PROT" != "tcp" ] && continue
6064                 RPORT=$(echo $REMOTE | cut -d: -f2)
6065                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6066
6067                 rc=0
6068                 LPORT=`echo $LOCAL | cut -d: -f2`
6069                 if [ $LPORT -ge 1024 ]; then
6070                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6071                         netstat -tna
6072                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6073                 fi
6074         done
6075         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6076 }
6077 run_test 100 "check local port using privileged port ==========="
6078
6079 function get_named_value()
6080 {
6081     local tag
6082
6083     tag=$1
6084     while read ;do
6085         line=$REPLY
6086         case $line in
6087         $tag*)
6088             echo $line | sed "s/^$tag[ ]*//"
6089             break
6090             ;;
6091         esac
6092     done
6093 }
6094
6095 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6096                    awk '/^max_cached_mb/ { print $2 }')
6097
6098 cleanup_101a() {
6099         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6100         trap 0
6101 }
6102
6103 test_101a() {
6104         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6105         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6106         local s
6107         local discard
6108         local nreads=10000
6109         local cache_limit=32
6110
6111         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6112         trap cleanup_101a EXIT
6113         $LCTL set_param -n llite.*.read_ahead_stats 0
6114         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6115
6116         #
6117         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6118         #
6119         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6120         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6121
6122         discard=0
6123         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6124                 get_named_value 'read but discarded' | cut -d" " -f1); do
6125                         discard=$(($discard + $s))
6126         done
6127         cleanup_101a
6128
6129         if [[ $(($discard * 10)) -gt $nreads ]]; then
6130                 $LCTL get_param osc.*-osc*.rpc_stats
6131                 $LCTL get_param llite.*.read_ahead_stats
6132                 error "too many ($discard) discarded pages"
6133         fi
6134         rm -f $DIR/$tfile || true
6135 }
6136 run_test 101a "check read-ahead for random reads ================"
6137
6138 setup_test101bc() {
6139         test_mkdir -p $DIR/$tdir
6140         local STRIPE_SIZE=$1
6141         local FILE_LENGTH=$2
6142         STRIPE_OFFSET=0
6143
6144         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6145
6146         local list=$(comma_list $(osts_nodes))
6147         set_osd_param $list '' read_cache_enable 0
6148         set_osd_param $list '' writethrough_cache_enable 0
6149
6150         trap cleanup_test101bc EXIT
6151         # prepare the read-ahead file
6152         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6153
6154         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6155                                 count=$FILE_SIZE_MB 2> /dev/null
6156
6157 }
6158
6159 cleanup_test101bc() {
6160         trap 0
6161         rm -rf $DIR/$tdir
6162         rm -f $DIR/$tfile
6163
6164         local list=$(comma_list $(osts_nodes))
6165         set_osd_param $list '' read_cache_enable 1
6166         set_osd_param $list '' writethrough_cache_enable 1
6167 }
6168
6169 calc_total() {
6170         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6171 }
6172
6173 ra_check_101() {
6174         local READ_SIZE=$1
6175         local STRIPE_SIZE=$2
6176         local FILE_LENGTH=$3
6177         local RA_INC=1048576
6178         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6179         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6180                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6181         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6182                         get_named_value 'read but discarded' |
6183                         cut -d" " -f1 | calc_total)
6184         if [[ $DISCARD -gt $discard_limit ]]; then
6185                 $LCTL get_param llite.*.read_ahead_stats
6186                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6187         else
6188                 echo "Read-ahead success for size ${READ_SIZE}"
6189         fi
6190 }
6191
6192 test_101b() {
6193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6194         [[ $OSTCOUNT -lt 2 ]] &&
6195                 skip_env "skipping stride IO stride-ahead test" && return
6196         local STRIPE_SIZE=1048576
6197         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6198         if [ $SLOW == "yes" ]; then
6199                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6200         else
6201                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6202         fi
6203
6204         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6205
6206         # prepare the read-ahead file
6207         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6208         cancel_lru_locks osc
6209         for BIDX in 2 4 8 16 32 64 128 256
6210         do
6211                 local BSIZE=$((BIDX*4096))
6212                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6213                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6214                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6215                 $LCTL set_param -n llite.*.read_ahead_stats 0
6216                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6217                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6218                 cancel_lru_locks osc
6219                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6220         done
6221         cleanup_test101bc
6222         true
6223 }
6224 run_test 101b "check stride-io mode read-ahead ================="
6225
6226 test_101c() {
6227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6228         local STRIPE_SIZE=1048576
6229         local FILE_LENGTH=$((STRIPE_SIZE*100))
6230         local nreads=10000
6231         local osc_rpc_stats
6232
6233         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6234
6235         cancel_lru_locks osc
6236         $LCTL set_param osc.*.rpc_stats 0
6237         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6238         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6239                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6240                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6241                 local size
6242
6243                 if [ $lines -le 20 ]; then
6244                         continue
6245                 fi
6246                 for size in 1 2 4 8; do
6247                         local rpc=$(echo "$stats" |
6248                                     awk '($1 == "'$size':") {print $2; exit; }')
6249                         [ $rpc != 0 ] &&
6250                                 error "Small $((size*4))k read IO $rpc !"
6251                 done
6252                 echo "$osc_rpc_stats check passed!"
6253         done
6254         cleanup_test101bc
6255         true
6256 }
6257 run_test 101c "check stripe_size aligned read-ahead ================="
6258
6259 set_read_ahead() {
6260         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6261         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6262 }
6263
6264 test_101d() {
6265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6266         local file=$DIR/$tfile
6267         local sz_MB=${FILESIZE_101d:-500}
6268         local ra_MB=${READAHEAD_MB:-40}
6269
6270         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6271         [ $free_MB -lt $sz_MB ] &&
6272                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6273
6274         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6275         $SETSTRIPE -c -1 $file || error "setstripe failed"
6276
6277         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6278         echo Cancel LRU locks on lustre client to flush the client cache
6279         cancel_lru_locks osc
6280
6281         echo Disable read-ahead
6282         local old_READAHEAD=$(set_read_ahead 0)
6283
6284         echo Reading the test file $file with read-ahead disabled
6285         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6286
6287         echo Cancel LRU locks on lustre client to flush the client cache
6288         cancel_lru_locks osc
6289         echo Enable read-ahead with ${ra_MB}MB
6290         set_read_ahead $ra_MB
6291
6292         echo Reading the test file $file with read-ahead enabled
6293         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6294
6295         echo "read-ahead disabled time read $raOFF"
6296         echo "read-ahead enabled  time read $raON"
6297
6298         set_read_ahead $old_READAHEAD
6299         rm -f $file
6300         wait_delete_completed
6301
6302         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6303                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6304 }
6305 run_test 101d "file read with and without read-ahead enabled"
6306
6307 test_101e() {
6308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6309         local file=$DIR/$tfile
6310         local size_KB=500  #KB
6311         local count=100
6312         local bsize=1024
6313
6314         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6315         local need_KB=$((count * size_KB))
6316         [[ $free_KB -le $need_KB ]] &&
6317                 skip_env "Need free space $need_KB, have $free_KB" && return
6318
6319         echo "Creating $count ${size_KB}K test files"
6320         for ((i = 0; i < $count; i++)); do
6321                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6322         done
6323
6324         echo "Cancel LRU locks on lustre client to flush the client cache"
6325         cancel_lru_locks osc
6326
6327         echo "Reset readahead stats"
6328         $LCTL set_param -n llite.*.read_ahead_stats 0
6329
6330         for ((i = 0; i < $count; i++)); do
6331                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6332         done
6333
6334         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6335                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6336
6337         for ((i = 0; i < $count; i++)); do
6338                 rm -rf $file.$i 2>/dev/null
6339         done
6340
6341         #10000 means 20% reads are missing in readahead
6342         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6343 }
6344 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6345
6346 cleanup_test101f() {
6347     trap 0
6348     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6349     rm -rf $DIR/$tfile 2>/dev/null
6350 }
6351
6352 test_101f() {
6353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6354     local file=$DIR/$tfile
6355     local nreads=1000
6356
6357     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6358     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6359     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6360     trap cleanup_test101f EXIT
6361
6362     echo Cancel LRU locks on lustre client to flush the client cache
6363     cancel_lru_locks osc
6364
6365     echo Reset readahead stats
6366     $LCTL set_param -n llite.*.read_ahead_stats 0
6367     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6368     # readahead should read in 2M file on second read, so only miss
6369     # 2 pages.
6370     echo Random 4K reads on 2M file for 1000 times
6371     $READS -f $file -s 2097152 -b 4096 -n $nreads
6372
6373     echo checking missing pages
6374     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6375           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6376
6377     [ $miss -lt 3 ] || error "misses too much pages!"
6378     cleanup_test101f
6379 }
6380 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6381
6382 setup_test102() {
6383         test_mkdir -p $DIR/$tdir
6384         chown $RUNAS_ID $DIR/$tdir
6385         STRIPE_SIZE=65536
6386         STRIPE_OFFSET=1
6387         STRIPE_COUNT=$OSTCOUNT
6388         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6389
6390         trap cleanup_test102 EXIT
6391         cd $DIR
6392         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6393         cd $DIR/$tdir
6394         for num in 1 2 3 4; do
6395                 for count in $(seq 1 $STRIPE_COUNT); do
6396                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6397                                 local size=`expr $STRIPE_SIZE \* $num`
6398                                 local file=file"$num-$idx-$count"
6399                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6400                         done
6401                 done
6402         done
6403
6404         cd $DIR
6405         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6406 }
6407
6408 cleanup_test102() {
6409         trap 0
6410         rm -f $TMP/f102.tar
6411         rm -rf $DIR/d0.sanity/d102
6412 }
6413
6414 test_102a() {
6415         local testfile=$DIR/$tfile
6416
6417         touch $testfile
6418
6419         [ "$UID" != 0 ] && skip_env "must run as root" && return
6420         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6421                 skip_env "must have user_xattr" && return
6422
6423         [ -z "$(which setfattr 2>/dev/null)" ] &&
6424                 skip_env "could not find setfattr" && return
6425
6426         echo "set/get xattr..."
6427         setfattr -n trusted.name1 -v value1 $testfile ||
6428                 error "setfattr -n trusted.name1=value1 $testfile failed"
6429         getfattr -n trusted.name1 $testfile 2> /dev/null |
6430           grep "trusted.name1=.value1" ||
6431                 error "$testfile missing trusted.name1=value1"
6432
6433         setfattr -n user.author1 -v author1 $testfile ||
6434                 error "setfattr -n user.author1=author1 $testfile failed"
6435         getfattr -n user.author1 $testfile 2> /dev/null |
6436           grep "user.author1=.author1" ||
6437                 error "$testfile missing trusted.author1=author1"
6438
6439         echo "listxattr..."
6440         setfattr -n trusted.name2 -v value2 $testfile ||
6441                 error "$testfile unable to set trusted.name2"
6442         setfattr -n trusted.name3 -v value3 $testfile ||
6443                 error "$testfile unable to set trusted.name3"
6444         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6445             grep "trusted.name" | wc -l) -eq 3 ] ||
6446                 error "$testfile missing 3 trusted.name xattrs"
6447
6448         setfattr -n user.author2 -v author2 $testfile ||
6449                 error "$testfile unable to set user.author2"
6450         setfattr -n user.author3 -v author3 $testfile ||
6451                 error "$testfile unable to set user.author3"
6452         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6453             grep "user.author" | wc -l) -eq 3 ] ||
6454                 error "$testfile missing 3 user.author xattrs"
6455
6456         echo "remove xattr..."
6457         setfattr -x trusted.name1 $testfile ||
6458                 error "$testfile error deleting trusted.name1"
6459         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6460                 error "$testfile did not delete trusted.name1 xattr"
6461
6462         setfattr -x user.author1 $testfile ||
6463                 error "$testfile error deleting user.author1"
6464         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6465                 error "$testfile did not delete trusted.name1 xattr"
6466
6467         # b10667: setting lustre special xattr be silently discarded
6468         echo "set lustre special xattr ..."
6469         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6470                 error "$testfile allowed setting trusted.lov"
6471 }
6472 run_test 102a "user xattr test =================================="
6473
6474 test_102b() {
6475         [ -z "$(which setfattr 2>/dev/null)" ] &&
6476                 skip_env "could not find setfattr" && return
6477
6478         # b10930: get/set/list trusted.lov xattr
6479         echo "get/set/list trusted.lov xattr ..."
6480         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6481         local testfile=$DIR/$tfile
6482         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6483                 error "setstripe failed"
6484         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6485                 error "getstripe failed"
6486         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6487                 error "can't get trusted.lov from $testfile"
6488
6489         local testfile2=${testfile}2
6490         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6491                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6492
6493         $MCREATE $testfile2
6494         setfattr -n trusted.lov -v $value $testfile2
6495         local stripe_size=$($GETSTRIPE -S $testfile2)
6496         local stripe_count=$($GETSTRIPE -c $testfile2)
6497         [[ $stripe_size -eq 65536 ]] ||
6498                 error "stripe size $stripe_size != 65536"
6499         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6500                 error "stripe count $stripe_count != $STRIPECOUNT"
6501         rm -f $DIR/$tfile
6502 }
6503 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6504
6505 test_102c() {
6506         [ -z "$(which setfattr 2>/dev/null)" ] &&
6507                 skip_env "could not find setfattr" && return
6508
6509         # b10930: get/set/list lustre.lov xattr
6510         echo "get/set/list lustre.lov xattr ..."
6511         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6512         test_mkdir -p $DIR/$tdir
6513         chown $RUNAS_ID $DIR/$tdir
6514         local testfile=$DIR/$tdir/$tfile
6515         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6516                 error "setstripe failed"
6517         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6518                 error "getstripe failed"
6519         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6520         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6521
6522         local testfile2=${testfile}2
6523         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6524                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6525
6526         $RUNAS $MCREATE $testfile2
6527         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6528         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6529         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6530         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6531         [ $stripe_count -eq $STRIPECOUNT ] ||
6532                 error "stripe count $stripe_count != $STRIPECOUNT"
6533 }
6534 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6535
6536 compare_stripe_info1() {
6537         local stripe_index_all_zero=true
6538
6539         for num in 1 2 3 4; do
6540                 for count in $(seq 1 $STRIPE_COUNT); do
6541                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6542                                 local size=$((STRIPE_SIZE * num))
6543                                 local file=file"$num-$offset-$count"
6544                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6545                                 [[ $stripe_size -ne $size ]] &&
6546                                     error "$file: size $stripe_size != $size"
6547                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6548                                 # allow fewer stripes to be created, ORI-601
6549                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6550                                     error "$file: count $stripe_count != $count"
6551                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6552                                 [[ $stripe_index -ne 0 ]] &&
6553                                         stripe_index_all_zero=false
6554                         done
6555                 done
6556         done
6557         $stripe_index_all_zero &&
6558                 error "all files are being extracted starting from OST index 0"
6559         return 0
6560 }
6561
6562 find_lustre_tar() {
6563         [ -n "$(which tar 2>/dev/null)" ] &&
6564                 strings $(which tar) | grep -q "lustre" && echo tar
6565 }
6566
6567 test_102d() {
6568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6569         # b10930: tar test for trusted.lov xattr
6570         TAR=$(find_lustre_tar)
6571         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6572         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6573         setup_test102
6574         test_mkdir -p $DIR/d102d
6575         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6576         cd $DIR/d102d/$tdir
6577         compare_stripe_info1
6578 }
6579 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6580
6581 test_102f() {
6582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6583         # b10930: tar test for trusted.lov xattr
6584         TAR=$(find_lustre_tar)
6585         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6586         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6587         setup_test102
6588         test_mkdir -p $DIR/d102f
6589         cd $DIR
6590         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6591         cd $DIR/d102f/$tdir
6592         compare_stripe_info1
6593 }
6594 run_test 102f "tar copy files, not keep osts ==========="
6595
6596 grow_xattr() {
6597         local xsize=${1:-1024}  # in bytes
6598         local file=$DIR/$tfile
6599
6600         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6601                 skip "must have user_xattr" && return 0
6602         [ -z "$(which setfattr 2>/dev/null)" ] &&
6603                 skip_env "could not find setfattr" && return 0
6604         [ -z "$(which getfattr 2>/dev/null)" ] &&
6605                 skip_env "could not find getfattr" && return 0
6606
6607         touch $file
6608
6609         local value="$(generate_string $xsize)"
6610
6611         local xbig=trusted.big
6612         log "save $xbig on $file"
6613         setfattr -n $xbig -v $value $file ||
6614                 error "saving $xbig on $file failed"
6615
6616         local orig=$(get_xattr_value $xbig $file)
6617         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6618
6619         local xsml=trusted.sml
6620         log "save $xsml on $file"
6621         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6622
6623         local new=$(get_xattr_value $xbig $file)
6624         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6625
6626         log "grow $xsml on $file"
6627         setfattr -n $xsml -v "$value" $file ||
6628                 error "growing $xsml on $file failed"
6629
6630         new=$(get_xattr_value $xbig $file)
6631         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6632         log "$xbig still valid after growing $xsml"
6633
6634         rm -f $file
6635 }
6636
6637 test_102h() { # bug 15777
6638         grow_xattr 1024
6639 }
6640 run_test 102h "grow xattr from inside inode to external block"
6641
6642 test_102ha() {
6643         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6644         grow_xattr $(max_xattr_size)
6645 }
6646 run_test 102ha "grow xattr from inside inode to external inode"
6647
6648 test_102i() { # bug 17038
6649         [ -z "$(which getfattr 2>/dev/null)" ] &&
6650                 skip "could not find getfattr" && return
6651         touch $DIR/$tfile
6652         ln -s $DIR/$tfile $DIR/${tfile}link
6653         getfattr -n trusted.lov $DIR/$tfile ||
6654                 error "lgetxattr on $DIR/$tfile failed"
6655         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6656                 grep -i "no such attr" ||
6657                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6658         rm -f $DIR/$tfile $DIR/${tfile}link
6659 }
6660 run_test 102i "lgetxattr test on symbolic link ============"
6661
6662 test_102j() {
6663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6664         TAR=$(find_lustre_tar)
6665         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6666         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6667         setup_test102 "$RUNAS"
6668         test_mkdir -p $DIR/d102j
6669         chown $RUNAS_ID $DIR/d102j
6670         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6671         cd $DIR/d102j/$tdir
6672         compare_stripe_info1 "$RUNAS"
6673 }
6674 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6675
6676 test_102k() {
6677         [ -z "$(which setfattr 2>/dev/null)" ] &&
6678                 skip "could not find setfattr" && return
6679         touch $DIR/$tfile
6680         # b22187 just check that does not crash for regular file.
6681         setfattr -n trusted.lov $DIR/$tfile
6682         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6683         local test_kdir=$DIR/d102k
6684         test_mkdir $test_kdir
6685         local default_size=`$GETSTRIPE -S $test_kdir`
6686         local default_count=`$GETSTRIPE -c $test_kdir`
6687         local default_offset=`$GETSTRIPE -i $test_kdir`
6688         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6689                 error 'dir setstripe failed'
6690         setfattr -n trusted.lov $test_kdir
6691         local stripe_size=`$GETSTRIPE -S $test_kdir`
6692         local stripe_count=`$GETSTRIPE -c $test_kdir`
6693         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6694         [ $stripe_size -eq $default_size ] ||
6695                 error "stripe size $stripe_size != $default_size"
6696         [ $stripe_count -eq $default_count ] ||
6697                 error "stripe count $stripe_count != $default_count"
6698         [ $stripe_offset -eq $default_offset ] ||
6699                 error "stripe offset $stripe_offset != $default_offset"
6700         rm -rf $DIR/$tfile $test_kdir
6701 }
6702 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6703
6704 test_102l() {
6705         [ -z "$(which getfattr 2>/dev/null)" ] &&
6706                 skip "could not find getfattr" && return
6707
6708         # LU-532 trusted. xattr is invisible to non-root
6709         local testfile=$DIR/$tfile
6710
6711         touch $testfile
6712
6713         echo "listxattr as user..."
6714         chown $RUNAS_ID $testfile
6715         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6716             grep -q "trusted" &&
6717                 error "$testfile trusted xattrs are user visible"
6718
6719         return 0;
6720 }
6721 run_test 102l "listxattr size test =================================="
6722
6723 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6724         local path=$DIR/$tfile
6725         touch $path
6726
6727         listxattr_size_check $path || error "listattr_size_check $path failed"
6728 }
6729 run_test 102m "Ensure listxattr fails on small bufffer ========"
6730
6731 cleanup_test102
6732
6733 getxattr() { # getxattr path name
6734         # Return the base64 encoding of the value of xattr name on path.
6735         local path=$1
6736         local name=$2
6737
6738         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6739         # file: $path
6740         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6741         #
6742         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6743
6744         getfattr --absolute-names --encoding=base64 --name=$name $path |
6745                 awk -F= -v name=$name '$1 == name {
6746                         print substr($0, index($0, "=") + 1);
6747         }'
6748 }
6749
6750 test_102n() { # LU-4101 mdt: protect internal xattrs
6751         local file0=$DIR/$tfile.0
6752         local file1=$DIR/$tfile.1
6753         local xattr0=$TMP/$tfile.0
6754         local xattr1=$TMP/$tfile.1
6755         local name
6756         local value
6757
6758         [ -z "$(which setfattr 2>/dev/null)" ] &&
6759                 skip "could not find setfattr" && return
6760
6761         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6762         then
6763                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6764                 return
6765         fi
6766
6767         rm -rf $file0 $file1 $xattr0 $xattr1
6768         touch $file0 $file1
6769
6770         # Get 'before' xattrs of $file1.
6771         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6772
6773         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6774                 # Try to copy xattr from $file0 to $file1.
6775                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6776
6777                 setfattr --name=trusted.$name --value="$value" $file1 ||
6778                         error "setxattr 'trusted.$name' failed"
6779
6780                 # Try to set a garbage xattr.
6781                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6782
6783                 setfattr --name=trusted.$name --value="$value" $file1 ||
6784                         error "setxattr 'trusted.$name' failed"
6785
6786                 # Try to remove the xattr from $file1. We don't care if this
6787                 # appears to succeed or fail, we just don't want there to be
6788                 # any changes or crashes.
6789                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6790         done
6791
6792         # Get 'after' xattrs of file1.
6793         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6794
6795         if ! diff $xattr0 $xattr1; then
6796                 error "before and after xattrs of '$file1' differ"
6797         fi
6798
6799         rm -rf $file0 $file1 $xattr0 $xattr1
6800
6801         return 0
6802 }
6803 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6804
6805 test_102p() { # LU-4703 setxattr did not check ownership
6806         local testfile=$DIR/$tfile
6807
6808         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6809                 skip "MDS needs to be at least 2.5.56" && return
6810
6811         touch $testfile
6812
6813         echo "setfacl as user..."
6814         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6815         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6816
6817         echo "setfattr as user..."
6818         setfacl -m "u:$RUNAS_ID:---" $testfile
6819         $RUNAS setfattr -x system.posix_acl_access $testfile
6820         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6821 }
6822 run_test 102p "check setxattr(2) correctly fails without permission"
6823
6824 run_acl_subtest()
6825 {
6826     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6827     return $?
6828 }
6829
6830 test_103a() {
6831         [ "$UID" != 0 ] && skip_env "must run as root" && return
6832         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6833                 skip "must have acl enabled" && return
6834         [ -z "$(which setfacl 2>/dev/null)" ] &&
6835                 skip_env "could not find setfacl" && return
6836         $GSS && skip "could not run under gss" && return
6837
6838         gpasswd -a daemon bin   # LU-5641
6839
6840         declare -a identity_old
6841
6842         for num in $(seq $MDSCOUNT); do
6843                 switch_identity $num true || identity_old[$num]=$?
6844         done
6845
6846         SAVE_UMASK=$(umask)
6847         umask 0022
6848         cd $DIR
6849
6850         echo "performing cp ..."
6851         run_acl_subtest cp || error "run_acl_subtest cp failed"
6852         echo "performing getfacl-noacl..."
6853         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6854         echo "performing misc..."
6855         run_acl_subtest misc || error  "misc test failed"
6856         echo "performing permissions..."
6857         run_acl_subtest permissions || error "permissions failed"
6858         echo "performing setfacl..."
6859         run_acl_subtest setfacl || error  "setfacl test failed"
6860
6861         # inheritance test got from HP
6862         echo "performing inheritance..."
6863         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6864         chmod +x make-tree || error "chmod +x failed"
6865         run_acl_subtest inheritance || error "inheritance test failed"
6866         rm -f make-tree
6867
6868         echo "LU-974 ignore umask when acl is enabled..."
6869         run_acl_subtest 974 || error "LU-974 umask test failed"
6870         if [ $MDSCOUNT -ge 2 ]; then
6871                 run_acl_subtest 974_remote ||
6872                         error "LU-974 umask test failed under remote dir"
6873         fi
6874
6875         echo "LU-2561 newly created file is same size as directory..."
6876         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6877                 run_acl_subtest 2561 || error "LU-2561 test failed"
6878         else
6879                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6880         fi
6881
6882         run_acl_subtest 4924 || error "LU-4924 test failed"
6883
6884         cd $SAVE_PWD
6885         umask $SAVE_UMASK
6886
6887         for num in $(seq $MDSCOUNT); do
6888                 if [ "${identity_old[$num]}" = 1 ]; then
6889                         switch_identity $num false || identity_old[$num]=$?
6890                 fi
6891         done
6892 }
6893 run_test 103a "acl test ========================================="
6894
6895 test_103b() {
6896         local noacl=false
6897         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
6898         local mountopts=$MDS_MOUNT_OPTS
6899
6900         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
6901                 noacl=true
6902         else
6903                 # stop the MDT
6904                 stop $SINGLEMDS || error "failed to stop MDT."
6905                 # remount the MDT
6906                 if [ -z "$MDS_MOUNT_OPTS" ]; then
6907                         MDS_MOUNT_OPTS="-o noacl"
6908                 else
6909                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
6910                 fi
6911                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6912                         error "failed to start MDT."
6913                 MDS_MOUNT_OPTS=$mountopts
6914         fi
6915
6916         touch $DIR/$tfile
6917         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
6918
6919         if ! $noacl; then
6920                 # stop the MDT
6921                 stop $SINGLEMDS || error "failed to stop MDT."
6922                 # remount the MDT
6923                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
6924                         error "failed to start MDT."
6925         fi
6926
6927         true
6928 }
6929 run_test 103b "MDS mount option 'noacl'"
6930
6931 test_103c() {
6932         mkdir -p $DIR/$tdir
6933         cp -rp $DIR/$tdir $DIR/$tdir.bak
6934
6935         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
6936                 error "$DIR/$tdir shouldn't contain default ACL"
6937         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
6938                 error "$DIR/$tdir.bak shouldn't contain default ACL"
6939         true
6940 }
6941 run_test 103c "'cp -rp' won't set empty acl"
6942
6943 test_104a() {
6944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6945         touch $DIR/$tfile
6946         lfs df || error "lfs df failed"
6947         lfs df -ih || error "lfs df -ih failed"
6948         lfs df -h $DIR || error "lfs df -h $DIR failed"
6949         lfs df -i $DIR || error "lfs df -i $DIR failed"
6950         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6951         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6952
6953         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6954         lctl --device %$OSC deactivate
6955         lfs df || error "lfs df with deactivated OSC failed"
6956         lctl --device %$OSC activate
6957         # wait the osc back to normal
6958         wait_osc_import_state client ost FULL
6959
6960         lfs df || error "lfs df with reactivated OSC failed"
6961         rm -f $DIR/$tfile
6962 }
6963 run_test 104a "lfs df [-ih] [path] test ========================="
6964
6965 test_104b() {
6966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6967         [ $RUNAS_ID -eq $UID ] &&
6968                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6969         chmod 666 /dev/obd
6970         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6971                         grep "Permission denied" | wc -l)))
6972         if [ $denied_cnt -ne 0 ]; then
6973                 error "lfs check servers test failed"
6974         fi
6975 }
6976 run_test 104b "$RUNAS lfs check servers test ===================="
6977
6978 test_105a() {
6979         # doesn't work on 2.4 kernels
6980         touch $DIR/$tfile
6981         if $(flock_is_enabled); then
6982                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6983         else
6984                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6985         fi
6986         rm -f $DIR/$tfile
6987 }
6988 run_test 105a "flock when mounted without -o flock test ========"
6989
6990 test_105b() {
6991         touch $DIR/$tfile
6992         if $(flock_is_enabled); then
6993                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6994         else
6995                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6996         fi
6997         rm -f $DIR/$tfile
6998 }
6999 run_test 105b "fcntl when mounted without -o flock test ========"
7000
7001 test_105c() {
7002         touch $DIR/$tfile
7003         if $(flock_is_enabled); then
7004                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7005         else
7006                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7007         fi
7008         rm -f $DIR/$tfile
7009 }
7010 run_test 105c "lockf when mounted without -o flock test ========"
7011
7012 test_105d() { # bug 15924
7013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7014         test_mkdir -p $DIR/$tdir
7015         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7016         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7017         $LCTL set_param fail_loc=0x80000315
7018         flocks_test 2 $DIR/$tdir
7019 }
7020 run_test 105d "flock race (should not freeze) ========"
7021
7022 test_105e() { # bug 22660 && 22040
7023         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7024         touch $DIR/$tfile
7025         flocks_test 3 $DIR/$tfile
7026 }
7027 run_test 105e "Two conflicting flocks from same process ======="
7028
7029 test_106() { #bug 10921
7030         test_mkdir -p $DIR/$tdir
7031         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7032         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7033 }
7034 run_test 106 "attempt exec of dir followed by chown of that dir"
7035
7036 test_107() {
7037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7038         CDIR=`pwd`
7039         cd $DIR
7040
7041         local file=core
7042         rm -f $file
7043
7044         local save_pattern=$(sysctl -n kernel.core_pattern)
7045         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7046         sysctl -w kernel.core_pattern=$file
7047         sysctl -w kernel.core_uses_pid=0
7048
7049         ulimit -c unlimited
7050         sleep 60 &
7051         SLEEPPID=$!
7052
7053         sleep 1
7054
7055         kill -s 11 $SLEEPPID
7056         wait $SLEEPPID
7057         if [ -e $file ]; then
7058                 size=`stat -c%s $file`
7059                 [ $size -eq 0 ] && error "Fail to create core file $file"
7060         else
7061                 error "Fail to create core file $file"
7062         fi
7063         rm -f $file
7064         sysctl -w kernel.core_pattern=$save_pattern
7065         sysctl -w kernel.core_uses_pid=$save_uses_pid
7066         cd $CDIR
7067 }
7068 run_test 107 "Coredump on SIG"
7069
7070 test_110() {
7071         test_mkdir -p $DIR/$tdir
7072         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7073                 error "mkdir with 255 char failed"
7074         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7075                 error "mkdir with 256 char should fail, but did not"
7076         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7077                 error "create with 255 char failed"
7078         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7079                 error "create with 256 char should fail, but did not"
7080
7081         ls -l $DIR/$tdir
7082         rm -rf $DIR/$tdir
7083 }
7084 run_test 110 "filename length checking"
7085
7086 test_115() {
7087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7088         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7089                 tail -1 | cut -c11-20)
7090         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7091         echo "Starting with $OSTIO_pre threads"
7092
7093         NUMTEST=20000
7094         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7095         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7096         echo "$NUMTEST creates/unlinks"
7097         test_mkdir -p $DIR/$tdir
7098         createmany -o $DIR/$tdir/$tfile $NUMTEST
7099         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7100
7101         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7102                 tail -1 | cut -c11-20)
7103
7104         # don't return an error
7105         [ $OSTIO_post == $OSTIO_pre ] && echo \
7106             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7107             echo "This may be fine, depending on what ran before this test" &&
7108             echo "and how fast this system is." && return
7109
7110         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7111 }
7112 run_test 115 "verify dynamic thread creation===================="
7113
7114 free_min_max () {
7115         wait_delete_completed
7116         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7117         echo OST kbytes available: ${AVAIL[@]}
7118         MAXI=0; MAXV=${AVAIL[0]}
7119         MINI=0; MINV=${AVAIL[0]}
7120         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7121                 #echo OST $i: ${AVAIL[i]}kb
7122                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7123                         MAXV=${AVAIL[i]}; MAXI=$i
7124                 fi
7125                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7126                         MINV=${AVAIL[i]}; MINI=$i
7127                 fi
7128         done
7129         echo Min free space: OST $MINI: $MINV
7130         echo Max free space: OST $MAXI: $MAXV
7131 }
7132
7133 test_116a() { # was previously test_116()
7134         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7135         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7136
7137         echo -n "Free space priority "
7138         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7139                 head -n1
7140         declare -a AVAIL
7141         free_min_max
7142
7143         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7144         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7145                 && return
7146         trap simple_cleanup_common EXIT
7147
7148
7149         # Check if we need to generate uneven OSTs
7150         test_mkdir -p $DIR/$tdir/OST${MINI}
7151         local FILL=$(($MINV / 4))
7152         local DIFF=$(($MAXV - $MINV))
7153         local DIFF2=$(($DIFF * 100 / $MINV))
7154
7155         local threshold=$(do_facet $SINGLEMDS \
7156                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7157         threshold=${threshold%%%}
7158         echo -n "Check for uneven OSTs: "
7159         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7160
7161         if [[ $DIFF2 -gt $threshold ]]; then
7162                 echo "ok"
7163                 echo "Don't need to fill OST$MINI"
7164         else
7165                 # generate uneven OSTs. Write 2% over the QOS threshold value
7166                 echo "no"
7167                 DIFF=$(($threshold - $DIFF2 + 2))
7168                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7169                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7170                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7171                         error "setstripe failed"
7172                 DIFF=$(($DIFF2 / 2048))
7173                 i=0
7174                 while [ $i -lt $DIFF ]; do
7175                         i=$(($i + 1))
7176                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7177                                 bs=2M count=1 2>/dev/null
7178                         echo -n .
7179                 done
7180                 echo .
7181                 sync
7182                 sleep_maxage
7183                 free_min_max
7184         fi
7185
7186         DIFF=$(($MAXV - $MINV))
7187         DIFF2=$(($DIFF * 100 / $MINV))
7188         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7189         if [[ $DIFF2 -gt $threshold ]]; then
7190                 echo "ok"
7191         else
7192                 echo "failed - QOS mode won't be used"
7193                 skip "QOS imbalance criteria not met"
7194                 simple_cleanup_common
7195                 return
7196         fi
7197
7198         MINI1=$MINI; MINV1=$MINV
7199         MAXI1=$MAXI; MAXV1=$MAXV
7200
7201         # now fill using QOS
7202         $SETSTRIPE -c 1 $DIR/$tdir
7203         FILL=$(($FILL / 200))
7204         if [ $FILL -gt 600 ]; then
7205                 FILL=600
7206         fi
7207         echo "writing $FILL files to QOS-assigned OSTs"
7208         i=0
7209         while [ $i -lt $FILL ]; do
7210                 i=$((i + 1))
7211                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7212                         count=1 2>/dev/null
7213                 echo -n .
7214         done
7215         echo "wrote $i 200k files"
7216         sync
7217         sleep_maxage
7218
7219         echo "Note: free space may not be updated, so measurements might be off"
7220         free_min_max
7221         DIFF2=$(($MAXV - $MINV))
7222         echo "free space delta: orig $DIFF final $DIFF2"
7223         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7224         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7225         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7226         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7227         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7228         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7229         [ $DIFF -gt 0 ] &&
7230                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7231
7232         # Figure out which files were written where
7233         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7234                   awk '/'$MINI1': / {print $2; exit}')
7235         echo $UUID
7236         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7237         echo "$MINC files created on smaller OST $MINI1"
7238         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7239                   awk '/'$MAXI1': / {print $2; exit}')
7240         echo $UUID
7241         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7242         echo "$MAXC files created on larger OST $MAXI1"
7243         FILL=$(($MAXC * 100 / $MINC - 100))
7244         [[ $MINC -gt 0 ]] &&
7245                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7246         [[ $MAXC -gt $MINC ]] ||
7247                 error_ignore LU-9 "stripe QOS didn't balance free space"
7248         simple_cleanup_common
7249 }
7250 run_test 116a "stripe QOS: free space balance ==================="
7251
7252 test_116b() { # LU-2093
7253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7254         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7255                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7256 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7257         local old_rr
7258         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7259                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7260         do_facet $SINGLEMDS lctl set_param \
7261                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7262         mkdir -p $DIR/$tdir
7263         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7264         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7265         do_facet $SINGLEMDS lctl set_param fail_loc=0
7266         rm -rf $DIR/$tdir
7267         do_facet $SINGLEMDS lctl set_param \
7268                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7269 }
7270 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7271
7272 test_117() # bug 10891
7273 {
7274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7275         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7276         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7277         lctl set_param fail_loc=0x21e
7278         > $DIR/$tfile || error "truncate failed"
7279         lctl set_param fail_loc=0
7280         echo "Truncate succeeded."
7281         rm -f $DIR/$tfile
7282 }
7283 run_test 117 "verify osd extend =========="
7284
7285 NO_SLOW_RESENDCOUNT=4
7286 export OLD_RESENDCOUNT=""
7287 set_resend_count () {
7288         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7289         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7290         lctl set_param -n $PROC_RESENDCOUNT $1
7291         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7292 }
7293
7294 # for reduce test_118* time (b=14842)
7295 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7296
7297 # Reset async IO behavior after error case
7298 reset_async() {
7299         FILE=$DIR/reset_async
7300
7301         # Ensure all OSCs are cleared
7302         $SETSTRIPE -c -1 $FILE
7303         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7304         sync
7305         rm $FILE
7306 }
7307
7308 test_118a() #bug 11710
7309 {
7310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7311         reset_async
7312
7313         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7314         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7315         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7316
7317         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7318                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7319                 return 1;
7320         fi
7321         rm -f $DIR/$tfile
7322 }
7323 run_test 118a "verify O_SYNC works =========="
7324
7325 test_118b()
7326 {
7327         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7328         remote_ost_nodsh && skip "remote OST with nodsh" && return
7329
7330         reset_async
7331
7332         #define OBD_FAIL_OST_ENOENT 0x217
7333         set_nodes_failloc "$(osts_nodes)" 0x217
7334         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7335         RC=$?
7336         set_nodes_failloc "$(osts_nodes)" 0
7337         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7338         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7339                     grep -c writeback)
7340
7341         if [[ $RC -eq 0 ]]; then
7342                 error "Must return error due to dropped pages, rc=$RC"
7343                 return 1;
7344         fi
7345
7346         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7347                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7348                 return 1;
7349         fi
7350
7351         echo "Dirty pages not leaked on ENOENT"
7352
7353         # Due to the above error the OSC will issue all RPCs syncronously
7354         # until a subsequent RPC completes successfully without error.
7355         $MULTIOP $DIR/$tfile Ow4096yc
7356         rm -f $DIR/$tfile
7357
7358         return 0
7359 }
7360 run_test 118b "Reclaim dirty pages on fatal error =========="
7361
7362 test_118c()
7363 {
7364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7365
7366         # for 118c, restore the original resend count, LU-1940
7367         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7368                                 set_resend_count $OLD_RESENDCOUNT
7369         remote_ost_nodsh && skip "remote OST with nodsh" && return
7370
7371         reset_async
7372
7373         #define OBD_FAIL_OST_EROFS               0x216
7374         set_nodes_failloc "$(osts_nodes)" 0x216
7375
7376         # multiop should block due to fsync until pages are written
7377         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7378         MULTIPID=$!
7379         sleep 1
7380
7381         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7382                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7383         fi
7384
7385         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7386                     grep -c writeback)
7387         if [[ $WRITEBACK -eq 0 ]]; then
7388                 error "No page in writeback, writeback=$WRITEBACK"
7389         fi
7390
7391         set_nodes_failloc "$(osts_nodes)" 0
7392         wait $MULTIPID
7393         RC=$?
7394         if [[ $RC -ne 0 ]]; then
7395                 error "Multiop fsync failed, rc=$RC"
7396         fi
7397
7398         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7399         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7400                     grep -c writeback)
7401         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7402                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7403         fi
7404
7405         rm -f $DIR/$tfile
7406         echo "Dirty pages flushed via fsync on EROFS"
7407         return 0
7408 }
7409 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7410
7411 # continue to use small resend count to reduce test_118* time (b=14842)
7412 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7413
7414 test_118d()
7415 {
7416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7417         remote_ost_nodsh && skip "remote OST with nodsh" && return
7418
7419         reset_async
7420
7421         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7422         set_nodes_failloc "$(osts_nodes)" 0x214
7423         # multiop should block due to fsync until pages are written
7424         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7425         MULTIPID=$!
7426         sleep 1
7427
7428         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7429                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7430         fi
7431
7432         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7433                     grep -c writeback)
7434         if [[ $WRITEBACK -eq 0 ]]; then
7435                 error "No page in writeback, writeback=$WRITEBACK"
7436         fi
7437
7438         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7439         set_nodes_failloc "$(osts_nodes)" 0
7440
7441         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7442         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7443                     grep -c writeback)
7444         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7445                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7446         fi
7447
7448         rm -f $DIR/$tfile
7449         echo "Dirty pages gaurenteed flushed via fsync"
7450         return 0
7451 }
7452 run_test 118d "Fsync validation inject a delay of the bulk =========="
7453
7454 test_118f() {
7455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7456         reset_async
7457
7458         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7459         lctl set_param fail_loc=0x8000040a
7460
7461         # Should simulate EINVAL error which is fatal
7462         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7463         RC=$?
7464         if [[ $RC -eq 0 ]]; then
7465                 error "Must return error due to dropped pages, rc=$RC"
7466         fi
7467
7468         lctl set_param fail_loc=0x0
7469
7470         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7471         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7472         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7473                     grep -c writeback)
7474         if [[ $LOCKED -ne 0 ]]; then
7475                 error "Locked pages remain in cache, locked=$LOCKED"
7476         fi
7477
7478         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7479                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7480         fi
7481
7482         rm -f $DIR/$tfile
7483         echo "No pages locked after fsync"
7484
7485         reset_async
7486         return 0
7487 }
7488 run_test 118f "Simulate unrecoverable OSC side error =========="
7489
7490 test_118g() {
7491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7492         reset_async
7493
7494         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7495         lctl set_param fail_loc=0x406
7496
7497         # simulate local -ENOMEM
7498         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7499         RC=$?
7500
7501         lctl set_param fail_loc=0
7502         if [[ $RC -eq 0 ]]; then
7503                 error "Must return error due to dropped pages, rc=$RC"
7504         fi
7505
7506         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7507         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7508         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7509                         grep -c writeback)
7510         if [[ $LOCKED -ne 0 ]]; then
7511                 error "Locked pages remain in cache, locked=$LOCKED"
7512         fi
7513
7514         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7515                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7516         fi
7517
7518         rm -f $DIR/$tfile
7519         echo "No pages locked after fsync"
7520
7521         reset_async
7522         return 0
7523 }
7524 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7525
7526 test_118h() {
7527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7528         remote_ost_nodsh && skip "remote OST with nodsh" && return
7529
7530         reset_async
7531
7532         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7533         set_nodes_failloc "$(osts_nodes)" 0x20e
7534         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7535         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7536         RC=$?
7537
7538         set_nodes_failloc "$(osts_nodes)" 0
7539         if [[ $RC -eq 0 ]]; then
7540                 error "Must return error due to dropped pages, rc=$RC"
7541         fi
7542
7543         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7544         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7545         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7546                     grep -c writeback)
7547         if [[ $LOCKED -ne 0 ]]; then
7548                 error "Locked pages remain in cache, locked=$LOCKED"
7549         fi
7550
7551         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7552                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7553         fi
7554
7555         rm -f $DIR/$tfile
7556         echo "No pages locked after fsync"
7557
7558         return 0
7559 }
7560 run_test 118h "Verify timeout in handling recoverables errors  =========="
7561
7562 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7563
7564 test_118i() {
7565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7566         remote_ost_nodsh && skip "remote OST with nodsh" && return
7567
7568         reset_async
7569
7570         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7571         set_nodes_failloc "$(osts_nodes)" 0x20e
7572
7573         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7574         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7575         PID=$!
7576         sleep 5
7577         set_nodes_failloc "$(osts_nodes)" 0
7578
7579         wait $PID
7580         RC=$?
7581         if [[ $RC -ne 0 ]]; then
7582                 error "got error, but should be not, rc=$RC"
7583         fi
7584
7585         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7586         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7587         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7588         if [[ $LOCKED -ne 0 ]]; then
7589                 error "Locked pages remain in cache, locked=$LOCKED"
7590         fi
7591
7592         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7593                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7594         fi
7595
7596         rm -f $DIR/$tfile
7597         echo "No pages locked after fsync"
7598
7599         return 0
7600 }
7601 run_test 118i "Fix error before timeout in recoverable error  =========="
7602
7603 [ "$SLOW" = "no" ] && set_resend_count 4
7604
7605 test_118j() {
7606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7607         remote_ost_nodsh && skip "remote OST with nodsh" && return
7608
7609         reset_async
7610
7611         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7612         set_nodes_failloc "$(osts_nodes)" 0x220
7613
7614         # return -EIO from OST
7615         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7616         RC=$?
7617         set_nodes_failloc "$(osts_nodes)" 0x0
7618         if [[ $RC -eq 0 ]]; then
7619                 error "Must return error due to dropped pages, rc=$RC"
7620         fi
7621
7622         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7623         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7624         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7625         if [[ $LOCKED -ne 0 ]]; then
7626                 error "Locked pages remain in cache, locked=$LOCKED"
7627         fi
7628
7629         # in recoverable error on OST we want resend and stay until it finished
7630         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7631                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7632         fi
7633
7634         rm -f $DIR/$tfile
7635         echo "No pages locked after fsync"
7636
7637         return 0
7638 }
7639 run_test 118j "Simulate unrecoverable OST side error =========="
7640
7641 test_118k()
7642 {
7643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7644         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7645
7646         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7647         set_nodes_failloc "$(osts_nodes)" 0x20e
7648         test_mkdir -p $DIR/$tdir
7649
7650         for ((i=0;i<10;i++)); do
7651                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7652                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7653                 SLEEPPID=$!
7654                 sleep 0.500s
7655                 kill $SLEEPPID
7656                 wait $SLEEPPID
7657         done
7658
7659         set_nodes_failloc "$(osts_nodes)" 0
7660         rm -rf $DIR/$tdir
7661 }
7662 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7663
7664 test_118l()
7665 {
7666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7667         # LU-646
7668         test_mkdir -p $DIR/$tdir
7669         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7670         rm -rf $DIR/$tdir
7671 }
7672 run_test 118l "fsync dir ========="
7673
7674 test_118m() # LU-3066
7675 {
7676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7677         test_mkdir -p $DIR/$tdir
7678         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7679         rm -rf $DIR/$tdir
7680 }
7681 run_test 118m "fdatasync dir ========="
7682
7683 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7684
7685 test_119a() # bug 11737
7686 {
7687         BSIZE=$((512 * 1024))
7688         directio write $DIR/$tfile 0 1 $BSIZE
7689         # We ask to read two blocks, which is more than a file size.
7690         # directio will indicate an error when requested and actual
7691         # sizes aren't equeal (a normal situation in this case) and
7692         # print actual read amount.
7693         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7694         if [ "$NOB" != "$BSIZE" ]; then
7695                 error "read $NOB bytes instead of $BSIZE"
7696         fi
7697         rm -f $DIR/$tfile
7698 }
7699 run_test 119a "Short directIO read must return actual read amount"
7700
7701 test_119b() # bug 11737
7702 {
7703         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7704
7705         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7706         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7707         sync
7708         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7709                 error "direct read failed"
7710         rm -f $DIR/$tfile
7711 }
7712 run_test 119b "Sparse directIO read must return actual read amount"
7713
7714 test_119c() # bug 13099
7715 {
7716         BSIZE=1048576
7717         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7718         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7719         rm -f $DIR/$tfile
7720 }
7721 run_test 119c "Testing for direct read hitting hole"
7722
7723 test_119d() # bug 15950
7724 {
7725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7726         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7727         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7728         BSIZE=1048576
7729         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7730         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7731         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7732         lctl set_param fail_loc=0x40d
7733         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7734         pid_dio=$!
7735         sleep 1
7736         cat $DIR/$tfile > /dev/null &
7737         lctl set_param fail_loc=0
7738         pid_reads=$!
7739         wait $pid_dio
7740         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7741         sleep 2
7742         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7743         error "the read rpcs have not completed in 2s"
7744         rm -f $DIR/$tfile
7745         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7746 }
7747 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7748
7749 test_120a() {
7750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7751         test_mkdir -p $DIR/$tdir
7752         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7753                skip "no early lock cancel on server" && return 0
7754
7755         lru_resize_disable mdc
7756         lru_resize_disable osc
7757         cancel_lru_locks mdc
7758         # asynchronous object destroy at MDT could cause bl ast to client
7759         cancel_lru_locks osc
7760
7761         stat $DIR/$tdir > /dev/null
7762         can1=$(do_facet $SINGLEMDS \
7763                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7764                awk '/ldlm_cancel/ {print $2}')
7765         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7766                awk '/ldlm_bl_callback/ {print $2}')
7767         test_mkdir -c1 $DIR/$tdir/d1
7768         can2=$(do_facet $SINGLEMDS \
7769                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7770                awk '/ldlm_cancel/ {print $2}')
7771         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7772                awk '/ldlm_bl_callback/ {print $2}')
7773         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7774         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7775         lru_resize_enable mdc
7776         lru_resize_enable osc
7777 }
7778 run_test 120a "Early Lock Cancel: mkdir test"
7779
7780 test_120b() {
7781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7782         test_mkdir $DIR/$tdir
7783         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7784                skip "no early lock cancel on server" && return 0
7785         lru_resize_disable mdc
7786         lru_resize_disable osc
7787         cancel_lru_locks mdc
7788         stat $DIR/$tdir > /dev/null
7789         can1=$(do_facet $SINGLEMDS \
7790                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7791                awk '/ldlm_cancel/ {print $2}')
7792         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7793                awk '/ldlm_bl_callback/ {print $2}')
7794         touch $DIR/$tdir/f1
7795         can2=$(do_facet $SINGLEMDS \
7796                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7797                awk '/ldlm_cancel/ {print $2}')
7798         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7799                awk '/ldlm_bl_callback/ {print $2}')
7800         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7801         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7802         lru_resize_enable mdc
7803         lru_resize_enable osc
7804 }
7805 run_test 120b "Early Lock Cancel: create test"
7806
7807 test_120c() {
7808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7809         test_mkdir -c1 $DIR/$tdir
7810         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7811                skip "no early lock cancel on server" && return 0
7812         lru_resize_disable mdc
7813         lru_resize_disable osc
7814         test_mkdir -p -c1 $DIR/$tdir/d1
7815         test_mkdir -p -c1 $DIR/$tdir/d2
7816         touch $DIR/$tdir/d1/f1
7817         cancel_lru_locks mdc
7818         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7819         can1=$(do_facet $SINGLEMDS \
7820                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7821                awk '/ldlm_cancel/ {print $2}')
7822         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7823                awk '/ldlm_bl_callback/ {print $2}')
7824         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7825         can2=$(do_facet $SINGLEMDS \
7826                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7827                awk '/ldlm_cancel/ {print $2}')
7828         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7829                awk '/ldlm_bl_callback/ {print $2}')
7830         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7831         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7832         lru_resize_enable mdc
7833         lru_resize_enable osc
7834 }
7835 run_test 120c "Early Lock Cancel: link test"
7836
7837 test_120d() {
7838         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7839         test_mkdir -p -c1 $DIR/$tdir
7840         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7841                skip "no early lock cancel on server" && return 0
7842         lru_resize_disable mdc
7843         lru_resize_disable osc
7844         touch $DIR/$tdir
7845         cancel_lru_locks mdc
7846         stat $DIR/$tdir > /dev/null
7847         can1=$(do_facet $SINGLEMDS \
7848                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7849                awk '/ldlm_cancel/ {print $2}')
7850         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7851                awk '/ldlm_bl_callback/ {print $2}')
7852         chmod a+x $DIR/$tdir
7853         can2=$(do_facet $SINGLEMDS \
7854                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7855                awk '/ldlm_cancel/ {print $2}')
7856         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7857                awk '/ldlm_bl_callback/ {print $2}')
7858         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7859         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7860         lru_resize_enable mdc
7861         lru_resize_enable osc
7862 }
7863 run_test 120d "Early Lock Cancel: setattr test"
7864
7865 test_120e() {
7866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7867         test_mkdir -p -c1 $DIR/$tdir
7868         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7869                skip "no early lock cancel on server" && return 0
7870         lru_resize_disable mdc
7871         lru_resize_disable osc
7872         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7873         cancel_lru_locks mdc
7874         cancel_lru_locks osc
7875         dd if=$DIR/$tdir/f1 of=/dev/null
7876         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7877         # XXX client can not do early lock cancel of OST lock
7878         # during unlink (LU-4206), so cancel osc lock now.
7879         cancel_lru_locks osc
7880         can1=$(do_facet $SINGLEMDS \
7881                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7882                awk '/ldlm_cancel/ {print $2}')
7883         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7884                awk '/ldlm_bl_callback/ {print $2}')
7885         unlink $DIR/$tdir/f1
7886         sleep 5
7887         can2=$(do_facet $SINGLEMDS \
7888                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7889                awk '/ldlm_cancel/ {print $2}')
7890         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7891                awk '/ldlm_bl_callback/ {print $2}')
7892         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7893         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7894         lru_resize_enable mdc
7895         lru_resize_enable osc
7896 }
7897 run_test 120e "Early Lock Cancel: unlink test"
7898
7899 test_120f() {
7900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7901         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7902                skip "no early lock cancel on server" && return 0
7903         test_mkdir -p -c1 $DIR/$tdir
7904         lru_resize_disable mdc
7905         lru_resize_disable osc
7906         test_mkdir -p -c1 $DIR/$tdir/d1
7907         test_mkdir -p -c1 $DIR/$tdir/d2
7908         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7909         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7910         cancel_lru_locks mdc
7911         cancel_lru_locks osc
7912         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7913         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7914         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7915         # XXX client can not do early lock cancel of OST lock
7916         # during rename (LU-4206), so cancel osc lock now.
7917         cancel_lru_locks osc
7918         can1=$(do_facet $SINGLEMDS \
7919                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7920                awk '/ldlm_cancel/ {print $2}')
7921         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7922                awk '/ldlm_bl_callback/ {print $2}')
7923         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7924         sleep 5
7925         can2=$(do_facet $SINGLEMDS \
7926                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7927                awk '/ldlm_cancel/ {print $2}')
7928         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7929                awk '/ldlm_bl_callback/ {print $2}')
7930         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7931         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7932         lru_resize_enable mdc
7933         lru_resize_enable osc
7934 }
7935 run_test 120f "Early Lock Cancel: rename test"
7936
7937 test_120g() {
7938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7939         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7940                skip "no early lock cancel on server" && return 0
7941         lru_resize_disable mdc
7942         lru_resize_disable osc
7943         count=10000
7944         echo create $count files
7945         test_mkdir -p $DIR/$tdir
7946         cancel_lru_locks mdc
7947         cancel_lru_locks osc
7948         t0=`date +%s`
7949
7950         can0=$(do_facet $SINGLEMDS \
7951                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7952                awk '/ldlm_cancel/ {print $2}')
7953         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7954                awk '/ldlm_bl_callback/ {print $2}')
7955         createmany -o $DIR/$tdir/f $count
7956         sync
7957         can1=$(do_facet $SINGLEMDS \
7958                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7959                awk '/ldlm_cancel/ {print $2}')
7960         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7961                awk '/ldlm_bl_callback/ {print $2}')
7962         t1=$(date +%s)
7963         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7964         echo rm $count files
7965         rm -r $DIR/$tdir
7966         sync
7967         can2=$(do_facet $SINGLEMDS \
7968                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7969                awk '/ldlm_cancel/ {print $2}')
7970         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7971                awk '/ldlm_bl_callback/ {print $2}')
7972         t2=$(date +%s)
7973         echo total: $count removes in $((t2-t1))
7974         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7975         sleep 2
7976         # wait for commitment of removal
7977         lru_resize_enable mdc
7978         lru_resize_enable osc
7979 }
7980 run_test 120g "Early Lock Cancel: performance test"
7981
7982 test_121() { #bug #10589
7983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7984         rm -rf $DIR/$tfile
7985         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7986 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7987         lctl set_param fail_loc=0x310
7988         cancel_lru_locks osc > /dev/null
7989         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7990         lctl set_param fail_loc=0
7991         [[ $reads -eq $writes ]] ||
7992                 error "read $reads blocks, must be $writes blocks"
7993 }
7994 run_test 121 "read cancel race ========="
7995
7996 test_123a() { # was test 123, statahead(bug 11401)
7997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7998         SLOWOK=0
7999         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8000             log "testing on UP system. Performance may be not as good as expected."
8001                         SLOWOK=1
8002         fi
8003
8004         rm -rf $DIR/$tdir
8005         test_mkdir -p $DIR/$tdir
8006         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8007         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8008         MULT=10
8009         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8010                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8011
8012                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8013                 lctl set_param -n llite.*.statahead_max 0
8014                 lctl get_param llite.*.statahead_max
8015                 cancel_lru_locks mdc
8016                 cancel_lru_locks osc
8017                 stime=`date +%s`
8018                 time ls -l $DIR/$tdir | wc -l
8019                 etime=`date +%s`
8020                 delta=$((etime - stime))
8021                 log "ls $i files without statahead: $delta sec"
8022                 lctl set_param llite.*.statahead_max=$max
8023
8024                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8025                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8026                 cancel_lru_locks mdc
8027                 cancel_lru_locks osc
8028                 stime=`date +%s`
8029                 time ls -l $DIR/$tdir | wc -l
8030                 etime=`date +%s`
8031                 delta_sa=$((etime - stime))
8032                 log "ls $i files with statahead: $delta_sa sec"
8033                 lctl get_param -n llite.*.statahead_stats
8034                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8035
8036                 [[ $swrong -lt $ewrong ]] &&
8037                         log "statahead was stopped, maybe too many locks held!"
8038                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8039
8040                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8041                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8042                     lctl set_param -n llite.*.statahead_max 0
8043                     lctl get_param llite.*.statahead_max
8044                     cancel_lru_locks mdc
8045                     cancel_lru_locks osc
8046                     stime=`date +%s`
8047                     time ls -l $DIR/$tdir | wc -l
8048                     etime=`date +%s`
8049                     delta=$((etime - stime))
8050                     log "ls $i files again without statahead: $delta sec"
8051                     lctl set_param llite.*.statahead_max=$max
8052                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8053                         if [  $SLOWOK -eq 0 ]; then
8054                                 error "ls $i files is slower with statahead!"
8055                         else
8056                                 log "ls $i files is slower with statahead!"
8057                         fi
8058                         break
8059                     fi
8060                 fi
8061
8062                 [ $delta -gt 20 ] && break
8063                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8064                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8065         done
8066         log "ls done"
8067
8068         stime=`date +%s`
8069         rm -r $DIR/$tdir
8070         sync
8071         etime=`date +%s`
8072         delta=$((etime - stime))
8073         log "rm -r $DIR/$tdir/: $delta seconds"
8074         log "rm done"
8075         lctl get_param -n llite.*.statahead_stats
8076 }
8077 run_test 123a "verify statahead work"
8078
8079 test_123b () { # statahead(bug 15027)
8080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8081         test_mkdir -p $DIR/$tdir
8082         createmany -o $DIR/$tdir/$tfile-%d 1000
8083
8084         cancel_lru_locks mdc
8085         cancel_lru_locks osc
8086
8087 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8088         lctl set_param fail_loc=0x80000803
8089         ls -lR $DIR/$tdir > /dev/null
8090         log "ls done"
8091         lctl set_param fail_loc=0x0
8092         lctl get_param -n llite.*.statahead_stats
8093         rm -r $DIR/$tdir
8094         sync
8095
8096 }
8097 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8098
8099 test_124a() {
8100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8101         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8102                 skip "no lru resize on server" && return 0
8103         local NR=2000
8104         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8105
8106         log "create $NR files at $DIR/$tdir"
8107         createmany -o $DIR/$tdir/f $NR ||
8108                 error "failed to create $NR files in $DIR/$tdir"
8109
8110         cancel_lru_locks mdc
8111         ls -l $DIR/$tdir > /dev/null
8112
8113         local NSDIR=""
8114         local LRU_SIZE=0
8115         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8116                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8117                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8118                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8119                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8120                         log "NSDIR=$NSDIR"
8121                         log "NS=$(basename $NSDIR)"
8122                         break
8123                 fi
8124         done
8125
8126         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8127                 skip "Not enough cached locks created!"
8128                 return 0
8129         fi
8130         log "LRU=$LRU_SIZE"
8131
8132         local SLEEP=30
8133
8134         # We know that lru resize allows one client to hold $LIMIT locks
8135         # for 10h. After that locks begin to be killed by client.
8136         local MAX_HRS=10
8137         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8138         log "LIMIT=$LIMIT"
8139
8140         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8141         # killing locks. Some time was spent for creating locks. This means
8142         # that up to the moment of sleep finish we must have killed some of
8143         # them (10-100 locks). This depends on how fast ther were created.
8144         # Many of them were touched in almost the same moment and thus will
8145         # be killed in groups.
8146         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8147
8148         # Use $LRU_SIZE_B here to take into account real number of locks
8149         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8150         local LRU_SIZE_B=$LRU_SIZE
8151         log "LVF=$LVF"
8152         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8153         log "OLD_LVF=$OLD_LVF"
8154         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8155
8156         # Let's make sure that we really have some margin. Client checks
8157         # cached locks every 10 sec.
8158         SLEEP=$((SLEEP+20))
8159         log "Sleep ${SLEEP} sec"
8160         local SEC=0
8161         while ((SEC<$SLEEP)); do
8162                 echo -n "..."
8163                 sleep 5
8164                 SEC=$((SEC+5))
8165                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8166                 echo -n "$LRU_SIZE"
8167         done
8168         echo ""
8169         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8170         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8171
8172         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8173                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8174                 unlinkmany $DIR/$tdir/f $NR
8175                 return
8176         }
8177
8178         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8179         log "unlink $NR files at $DIR/$tdir"
8180         unlinkmany $DIR/$tdir/f $NR
8181 }
8182 run_test 124a "lru resize ======================================="
8183
8184 get_max_pool_limit()
8185 {
8186         local limit=$($LCTL get_param \
8187                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8188         local max=0
8189         for l in $limit; do
8190                 if [[ $l -gt $max ]]; then
8191                         max=$l
8192                 fi
8193         done
8194         echo $max
8195 }
8196
8197 test_124b() {
8198         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8199         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8200                 skip "no lru resize on server" && return 0
8201
8202         LIMIT=$(get_max_pool_limit)
8203
8204         NR=$(($(default_lru_size)*20))
8205         if [[ $NR -gt $LIMIT ]]; then
8206                 log "Limit lock number by $LIMIT locks"
8207                 NR=$LIMIT
8208         fi
8209         lru_resize_disable mdc
8210         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8211                 error "failed to create $DIR/$tdir/disable_lru_resize"
8212
8213         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8214         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8215         cancel_lru_locks mdc
8216         stime=`date +%s`
8217         PID=""
8218         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8219         PID="$PID $!"
8220         sleep 2
8221         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8222         PID="$PID $!"
8223         sleep 2
8224         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8225         PID="$PID $!"
8226         wait $PID
8227         etime=`date +%s`
8228         nolruresize_delta=$((etime-stime))
8229         log "ls -la time: $nolruresize_delta seconds"
8230         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8231         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8232
8233         lru_resize_enable mdc
8234         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8235                 error "failed to create $DIR/$tdir/enable_lru_resize"
8236
8237         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8238         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8239         cancel_lru_locks mdc
8240         stime=`date +%s`
8241         PID=""
8242         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8243         PID="$PID $!"
8244         sleep 2
8245         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8246         PID="$PID $!"
8247         sleep 2
8248         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8249         PID="$PID $!"
8250         wait $PID
8251         etime=`date +%s`
8252         lruresize_delta=$((etime-stime))
8253         log "ls -la time: $lruresize_delta seconds"
8254         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8255
8256         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8257                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8258         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8259                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8260         else
8261                 log "lru resize performs the same with no lru resize"
8262         fi
8263         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8264 }
8265 run_test 124b "lru resize (performance test) ======================="
8266
8267 test_125() { # 13358
8268         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8269         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8270         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8271         test_mkdir -p $DIR/d125 || error "mkdir failed"
8272         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8273         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8274         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8275 }
8276 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8277
8278 test_126() { # bug 12829/13455
8279         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8280         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8281         $GSS && skip "must run as gss disabled" && return
8282
8283         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8284         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8285         rm -f $DIR/$tfile
8286         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8287 }
8288 run_test 126 "check that the fsgid provided by the client is taken into account"
8289
8290 test_127a() { # bug 15521
8291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8292         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8293         $LCTL set_param osc.*.stats=0
8294         FSIZE=$((2048 * 1024))
8295         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8296         cancel_lru_locks osc
8297         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8298
8299         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8300         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8301                 echo "got $COUNT $NAME"
8302                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8303                 eval $NAME=$COUNT || error "Wrong proc format"
8304
8305                 case $NAME in
8306                         read_bytes|write_bytes)
8307                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8308                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8309                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8310                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8311                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8312                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8313                                 error "sumsquare is too small: $SUMSQ"
8314                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8315                                 error "sumsquare is too big: $SUMSQ"
8316                         ;;
8317                         *) ;;
8318                 esac
8319         done < $DIR/${tfile}.tmp
8320
8321         #check that we actually got some stats
8322         [ "$read_bytes" ] || error "Missing read_bytes stats"
8323         [ "$write_bytes" ] || error "Missing write_bytes stats"
8324         [ "$read_bytes" != 0 ] || error "no read done"
8325         [ "$write_bytes" != 0 ] || error "no write done"
8326 }
8327 run_test 127a "verify the client stats are sane"
8328
8329 test_127b() { # bug LU-333
8330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8331         $LCTL set_param llite.*.stats=0
8332         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8333         # perform 2 reads and writes so MAX is different from SUM.
8334         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8335         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8336         cancel_lru_locks osc
8337         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8338         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8339
8340         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8341         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8342                 echo "got $COUNT $NAME"
8343                 eval $NAME=$COUNT || error "Wrong proc format"
8344
8345         case $NAME in
8346                 read_bytes)
8347                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8348                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8349                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8350                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8351                         ;;
8352                 write_bytes)
8353                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8354                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8355                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8356                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8357                         ;;
8358                         *) ;;
8359                 esac
8360         done < $TMP/${tfile}.tmp
8361
8362         #check that we actually got some stats
8363         [ "$read_bytes" ] || error "Missing read_bytes stats"
8364         [ "$write_bytes" ] || error "Missing write_bytes stats"
8365         [ "$read_bytes" != 0 ] || error "no read done"
8366         [ "$write_bytes" != 0 ] || error "no write done"
8367 }
8368 run_test 127b "verify the llite client stats are sane"
8369
8370 test_128() { # bug 15212
8371         touch $DIR/$tfile
8372         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8373                 find $DIR/$tfile
8374                 find $DIR/$tfile
8375         EOF
8376
8377         result=$(grep error $TMP/$tfile.log)
8378         rm -f $DIR/$tfile
8379         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8380 }
8381 run_test 128 "interactive lfs for 2 consecutive find's"
8382
8383 set_dir_limits () {
8384         local mntdev
8385         local canondev
8386         local node
8387
8388         local LDPROC=/proc/fs/ldiskfs
8389         local facets=$(get_facets MDS)
8390
8391         for facet in ${facets//,/ }; do
8392                 canondev=$(ldiskfs_canon \
8393                            *.$(convert_facet2label $facet).mntdev $facet)
8394                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8395                                                 LDPROC=/sys/fs/ldiskfs
8396                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8397         done
8398 }
8399
8400 test_129() {
8401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8402         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8403                 skip "Only applicable to ldiskfs-based MDTs"
8404                 return
8405         fi
8406         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8407         ENOSPC=28
8408         EFBIG=27
8409
8410         rm -rf $DIR/$tdir
8411         test_mkdir -p $DIR/$tdir
8412
8413         # block size of mds1
8414         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8415         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8416         local MAX=$((MDSBLOCKSIZE * 3))
8417         set_dir_limits $MAX
8418         local I=$(stat -c%s "$DIR/$tdir")
8419         local J=0
8420         local STRIPE_COUNT=1
8421         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8422         MAX=$((MAX*STRIPE_COUNT))
8423         while [[ $I -le $MAX ]]; do
8424                 $MULTIOP $DIR/$tdir/$J Oc
8425                 rc=$?
8426                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8427                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8428                 #and EFBIG for previous versions
8429                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8430                         set_dir_limits 0
8431                         echo "return code $rc received as expected"
8432                         multiop $DIR/$tdir/$J Oc ||
8433                                 error_exit "multiop failed w/o dir size limit"
8434
8435                         I=$(stat -c%s "$DIR/$tdir")
8436
8437                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8438                                         $(version_code 2.4.51) ]
8439                         then
8440                                 [[ $I -eq $MAX ]] && return 0
8441                         else
8442                                 [[ $I -gt $MAX ]] && return 0
8443                         fi
8444                         error_exit "current dir size $I, previous limit $MAX"
8445                 elif [ $rc -ne 0 ]; then
8446                         set_dir_limits 0
8447                         error_exit "return code $rc received instead of expected " \
8448                                    "$EFBIG or $ENOSPC, files in dir $I"
8449                 fi
8450                 J=$((J+1))
8451                 I=$(stat -c%s "$DIR/$tdir")
8452         done
8453
8454         set_dir_limits 0
8455         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8456 }
8457 run_test 129 "test directory size limit ========================"
8458
8459 OLDIFS="$IFS"
8460 cleanup_130() {
8461         trap 0
8462         IFS="$OLDIFS"
8463 }
8464
8465 test_130a() {
8466         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8467         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8468                 return
8469
8470         trap cleanup_130 EXIT RETURN
8471
8472         local fm_file=$DIR/$tfile
8473         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8474         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8475                 error "dd failed for $fm_file"
8476
8477         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8478         filefrag -ves $fm_file
8479         RC=$?
8480         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8481                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8482         [ $RC != 0 ] && error "filefrag $fm_file failed"
8483
8484         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8485                       grep -v "ext:" | grep -v "found")
8486         lun=$($GETSTRIPE -i $fm_file)
8487
8488         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8489         IFS=$'\n'
8490         tot_len=0
8491         for line in $filefrag_op
8492         do
8493                 frag_lun=`echo $line | cut -d: -f5`
8494                 ext_len=`echo $line | cut -d: -f4`
8495                 if (( $frag_lun != $lun )); then
8496                         cleanup_130
8497                         error "FIEMAP on 1-stripe file($fm_file) failed"
8498                         return
8499                 fi
8500                 (( tot_len += ext_len ))
8501         done
8502
8503         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8504                 cleanup_130
8505                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8506                 return
8507         fi
8508
8509         cleanup_130
8510
8511         echo "FIEMAP on single striped file succeeded"
8512 }
8513 run_test 130a "FIEMAP (1-stripe file)"
8514
8515 test_130b() {
8516         [ "$OSTCOUNT" -lt "2" ] &&
8517                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8518
8519         [ "$OSTCOUNT" -ge "10" ] &&
8520                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8521
8522         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8523         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8524                 return
8525
8526         trap cleanup_130 EXIT RETURN
8527
8528         local fm_file=$DIR/$tfile
8529         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8530         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8531                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8532
8533         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8534                 error "dd failed on $fm_file"
8535
8536         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8537         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8538                       grep -v "ext:" | grep -v "found")
8539
8540         last_lun=$(echo $filefrag_op | cut -d: -f5)
8541
8542         IFS=$'\n'
8543         tot_len=0
8544         num_luns=1
8545         for line in $filefrag_op
8546         do
8547                 frag_lun=`echo $line | cut -d: -f5`
8548                 ext_len=`echo $line | cut -d: -f4`
8549                 if (( $frag_lun != $last_lun )); then
8550                         if (( tot_len != 1024 )); then
8551                                 cleanup_130
8552                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8553                                 return
8554                         else
8555                                 (( num_luns += 1 ))
8556                                 tot_len=0
8557                         fi
8558                 fi
8559                 (( tot_len += ext_len ))
8560                 last_lun=$frag_lun
8561         done
8562         if (( num_luns != 2 || tot_len != 1024 )); then
8563                 cleanup_130
8564                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8565                 return
8566         fi
8567
8568         cleanup_130
8569
8570         echo "FIEMAP on 2-stripe file succeeded"
8571 }
8572 run_test 130b "FIEMAP (2-stripe file)"
8573
8574 test_130c() {
8575         [ "$OSTCOUNT" -lt "2" ] &&
8576                 skip_env "skipping FIEMAP on 2-stripe file" && return
8577
8578         [ "$OSTCOUNT" -ge "10" ] &&
8579                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8580
8581         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8582         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8583                 return
8584
8585         trap cleanup_130 EXIT RETURN
8586
8587         local fm_file=$DIR/$tfile
8588         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8589         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8590                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8591
8592         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8593
8594         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8595         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8596
8597         last_lun=`echo $filefrag_op | cut -d: -f5`
8598
8599         IFS=$'\n'
8600         tot_len=0
8601         num_luns=1
8602         for line in $filefrag_op
8603         do
8604                 frag_lun=`echo $line | cut -d: -f5`
8605                 ext_len=`echo $line | cut -d: -f4`
8606                 if (( $frag_lun != $last_lun )); then
8607                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8608                         if (( logical != 512 )); then
8609                                 cleanup_130
8610                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8611                                 return
8612                         fi
8613                         if (( tot_len != 512 )); then
8614                                 cleanup_130
8615                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8616                                 return
8617                         else
8618                                 (( num_luns += 1 ))
8619                                 tot_len=0
8620                         fi
8621                 fi
8622                 (( tot_len += ext_len ))
8623                 last_lun=$frag_lun
8624         done
8625         if (( num_luns != 2 || tot_len != 512 )); then
8626                 cleanup_130
8627                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8628                 return
8629         fi
8630
8631         cleanup_130
8632
8633         echo "FIEMAP on 2-stripe file with hole succeeded"
8634 }
8635 run_test 130c "FIEMAP (2-stripe file with hole)"
8636
8637 test_130d() {
8638         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8639
8640         [ "$OSTCOUNT" -ge "10" ] &&
8641                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8642
8643         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8644         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8645
8646         trap cleanup_130 EXIT RETURN
8647
8648         local fm_file=$DIR/$tfile
8649         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8650         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8651                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8652
8653         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8654         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8655                 error "dd failed on $fm_file"
8656
8657         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8658         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8659                 grep -v "ext:" | grep -v "found"`
8660
8661         last_lun=`echo $filefrag_op | cut -d: -f5`
8662
8663         IFS=$'\n'
8664         tot_len=0
8665         num_luns=1
8666         for line in $filefrag_op
8667         do
8668                 frag_lun=`echo $line | cut -d: -f5`
8669                 ext_len=`echo $line | cut -d: -f4`
8670                 if (( $frag_lun != $last_lun )); then
8671                         if (( tot_len != 1024 )); then
8672                                 cleanup_130
8673                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8674                                 return
8675                         else
8676                                 (( num_luns += 1 ))
8677                                 tot_len=0
8678                         fi
8679                 fi
8680                 (( tot_len += ext_len ))
8681                 last_lun=$frag_lun
8682         done
8683         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8684                 cleanup_130
8685                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8686                 return
8687         fi
8688
8689         cleanup_130
8690
8691         echo "FIEMAP on N-stripe file succeeded"
8692 }
8693 run_test 130d "FIEMAP (N-stripe file)"
8694
8695 test_130e() {
8696         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8697
8698         [ "$OSTCOUNT" -ge "10" ] &&
8699                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8700
8701         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8702         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8703
8704         trap cleanup_130 EXIT RETURN
8705
8706         local fm_file=$DIR/$tfile
8707         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8708         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8709                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8710
8711         NUM_BLKS=512
8712         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8713         for ((i = 0; i < $NUM_BLKS; i++))
8714         do
8715                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8716         done
8717
8718         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8719         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8720
8721         last_lun=`echo $filefrag_op | cut -d: -f5`
8722
8723         IFS=$'\n'
8724         tot_len=0
8725         num_luns=1
8726         for line in $filefrag_op
8727         do
8728                 frag_lun=`echo $line | cut -d: -f5`
8729                 ext_len=`echo $line | cut -d: -f4`
8730                 if (( $frag_lun != $last_lun )); then
8731                         if (( tot_len != $EXPECTED_LEN )); then
8732                                 cleanup_130
8733                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8734                                 return
8735                         else
8736                                 (( num_luns += 1 ))
8737                                 tot_len=0
8738                         fi
8739                 fi
8740                 (( tot_len += ext_len ))
8741                 last_lun=$frag_lun
8742         done
8743         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8744                 cleanup_130
8745                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8746                 return
8747         fi
8748
8749         cleanup_130
8750
8751         echo "FIEMAP with continuation calls succeeded"
8752 }
8753 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8754
8755 # Test for writev/readv
8756 test_131a() {
8757         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8758         error "writev test failed"
8759         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8760         error "readv failed"
8761         rm -f $DIR/$tfile
8762 }
8763 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8764
8765 test_131b() {
8766         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8767         error "append writev test failed"
8768         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8769         error "append writev test failed"
8770         rm -f $DIR/$tfile
8771 }
8772 run_test 131b "test append writev"
8773
8774 test_131c() {
8775         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8776         error "NOT PASS"
8777 }
8778 run_test 131c "test read/write on file w/o objects"
8779
8780 test_131d() {
8781         rwv -f $DIR/$tfile -w -n 1 1572864
8782         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8783         if [ "$NOB" != 1572864 ]; then
8784                 error "Short read filed: read $NOB bytes instead of 1572864"
8785         fi
8786         rm -f $DIR/$tfile
8787 }
8788 run_test 131d "test short read"
8789
8790 test_131e() {
8791         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8792         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8793         error "read hitting hole failed"
8794         rm -f $DIR/$tfile
8795 }
8796 run_test 131e "test read hitting hole"
8797
8798 get_ost_param() {
8799         local token=$1
8800         local gl_sum=0
8801         for node in $(osts_nodes); do
8802                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8803                 [ x$gl = x"" ] && gl=0
8804                 gl_sum=$((gl_sum + gl))
8805         done
8806         echo $gl_sum
8807 }
8808
8809 som_mode_switch() {
8810         local som=$1
8811         local gl1=$2
8812         local gl2=$3
8813
8814         if [ x$som = x"enabled" ]; then
8815                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8816                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8817                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8818                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8819         else
8820                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8821                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8822                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8823         fi
8824
8825         # do remount to make new mount-conf parameters actual
8826         echo remounting...
8827         sync
8828         stopall
8829         setupall
8830 }
8831
8832 test_132() { #1028, SOM
8833         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8834         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8835         local num=$(get_mds_dir $DIR)
8836         local mymds=mds${num}
8837         local MOUNT_OPTS_SAVE=$MOUNT_OPTS
8838
8839         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8840         cancel_lru_locks osc
8841
8842         som1=$(do_facet $mymds "$LCTL get_param -n mdt.*.som" | head -n 1)
8843
8844         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8845         stat $DIR/$tfile >/dev/null
8846         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8847         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8848         rm $DIR/$tfile
8849         som_mode_switch $som1 $gl1 $gl2
8850
8851         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8852         cancel_lru_locks osc
8853
8854         som2=$(do_facet $mymds "$LCTL get_param -n mdt.*.som" | head -n 1)
8855         if [ $som1 == $som2 ]; then
8856             error "som is still "$som2
8857             if [ x$som2 = x"enabled" ]; then
8858                 som2="disabled"
8859             else
8860                 som2="enabled"
8861             fi
8862         fi
8863
8864         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8865         stat $DIR/$tfile >/dev/null
8866         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8867         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8868         som_mode_switch $som2 $gl1 $gl2
8869         MOUNT_OPTS=$MOUNT_OPTS_SAVE
8870 }
8871 run_test 132 "som avoids glimpse rpc"
8872
8873 check_stats() {
8874         local res
8875         local count
8876         case $1 in
8877         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8878                  ;;
8879         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8880                  ;;
8881         *) error "Wrong argument $1" ;;
8882         esac
8883         echo $res
8884         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8885         # if the argument $3 is zero, it means any stat increment is ok.
8886         if [[ $3 -gt 0 ]]; then
8887                 count=$(echo $res | awk '{ print $2 }')
8888                 [[ $count -ne $3 ]] &&
8889                         error "The $2 counter on $1 is wrong - expected $3"
8890         fi
8891 }
8892
8893 test_133a() {
8894         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8895         remote_ost_nodsh && skip "remote OST with nodsh" && return
8896         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8897
8898         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8899                 { skip "MDS doesn't support rename stats"; return; }
8900         local testdir=$DIR/${tdir}/stats_testdir
8901         mkdir -p $DIR/${tdir}
8902
8903         # clear stats.
8904         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8905         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8906
8907         # verify mdt stats first.
8908         mkdir ${testdir} || error "mkdir failed"
8909         check_stats $SINGLEMDS "mkdir" 1
8910         touch ${testdir}/${tfile} || "touch failed"
8911         check_stats $SINGLEMDS "open" 1
8912         check_stats $SINGLEMDS "close" 1
8913         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8914         check_stats $SINGLEMDS "mknod" 1
8915         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8916         check_stats $SINGLEMDS "unlink" 1
8917         rm -f ${testdir}/${tfile} || error "file remove failed"
8918         check_stats $SINGLEMDS "unlink" 2
8919
8920         # remove working dir and check mdt stats again.
8921         rmdir ${testdir} || error "rmdir failed"
8922         check_stats $SINGLEMDS "rmdir" 1
8923
8924         local testdir1=$DIR/${tdir}/stats_testdir1
8925         mkdir -p ${testdir}
8926         mkdir -p ${testdir1}
8927         touch ${testdir1}/test1
8928         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8929         check_stats $SINGLEMDS "crossdir_rename" 1
8930
8931         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8932         check_stats $SINGLEMDS "samedir_rename" 1
8933
8934         rm -rf $DIR/${tdir}
8935 }
8936 run_test 133a "Verifying MDT stats ========================================"
8937
8938 test_133b() {
8939         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8940         remote_ost_nodsh && skip "remote OST with nodsh" && return
8941         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8942         local testdir=$DIR/${tdir}/stats_testdir
8943         mkdir -p ${testdir} || error "mkdir failed"
8944         touch ${testdir}/${tfile} || "touch failed"
8945         cancel_lru_locks mdc
8946
8947         # clear stats.
8948         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8949         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8950
8951         # extra mdt stats verification.
8952         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8953         check_stats $SINGLEMDS "setattr" 1
8954         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8955         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8956         then            # LU-1740
8957                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8958                 check_stats $SINGLEMDS "getattr" 1
8959         fi
8960         $LFS df || error "lfs failed"
8961         check_stats $SINGLEMDS "statfs" 1
8962
8963         rm -rf $DIR/${tdir}
8964 }
8965 run_test 133b "Verifying extra MDT stats =================================="
8966
8967 test_133c() {
8968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8969         remote_ost_nodsh && skip "remote OST with nodsh" && return
8970         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8971         local testdir=$DIR/${tdir}/stats_testdir
8972         test_mkdir -p ${testdir} || error "mkdir failed"
8973
8974         # verify obdfilter stats.
8975         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8976         sync
8977         cancel_lru_locks osc
8978         wait_delete_completed
8979
8980         # clear stats.
8981         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8982         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8983
8984         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8985         sync
8986         cancel_lru_locks osc
8987         check_stats ost "write" 1
8988
8989         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8990         check_stats ost "read" 1
8991
8992         > ${testdir}/${tfile} || error "truncate failed"
8993         check_stats ost "punch" 1
8994
8995         rm -f ${testdir}/${tfile} || error "file remove failed"
8996         wait_delete_completed
8997         check_stats ost "destroy" 1
8998
8999         rm -rf $DIR/${tdir}
9000 }
9001 run_test 133c "Verifying OST stats ========================================"
9002
9003 order_2() {
9004         local value=$1
9005         local orig=$value
9006         local order=1
9007
9008         while [ $value -ge 2 ]; do
9009                 order=$((order*2))
9010                 value=$((value/2))
9011         done
9012
9013         if [ $orig -gt $order ]; then
9014                 order=$((order*2))
9015         fi
9016         echo $order
9017 }
9018
9019 size_in_KMGT() {
9020     local value=$1
9021     local size=('K' 'M' 'G' 'T');
9022     local i=0
9023     local size_string=$value
9024
9025     while [ $value -ge 1024 ]; do
9026         if [ $i -gt 3 ]; then
9027             #T is the biggest unit we get here, if that is bigger,
9028             #just return XXXT
9029             size_string=${value}T
9030             break
9031         fi
9032         value=$((value >> 10))
9033         if [ $value -lt 1024 ]; then
9034             size_string=${value}${size[$i]}
9035             break
9036         fi
9037         i=$((i + 1))
9038     done
9039
9040     echo $size_string
9041 }
9042
9043 get_rename_size() {
9044     local size=$1
9045     local context=${2:-.}
9046     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9047                 grep -A1 $context |
9048                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9049     echo $sample
9050 }
9051
9052 test_133d() {
9053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9054         remote_ost_nodsh && skip "remote OST with nodsh" && return
9055         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9056         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9057         { skip "MDS doesn't support rename stats"; return; }
9058
9059         local testdir1=$DIR/${tdir}/stats_testdir1
9060         local testdir2=$DIR/${tdir}/stats_testdir2
9061
9062         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9063
9064         mkdir -p ${testdir1} || error "mkdir failed"
9065         mkdir -p ${testdir2} || error "mkdir failed"
9066
9067         createmany -o $testdir1/test 512 || error "createmany failed"
9068
9069         # check samedir rename size
9070         mv ${testdir1}/test0 ${testdir1}/test_0
9071
9072         local testdir1_size=$(ls -l $DIR/${tdir} |
9073                 awk '/stats_testdir1/ {print $5}')
9074         local testdir2_size=$(ls -l $DIR/${tdir} |
9075                 awk '/stats_testdir2/ {print $5}')
9076
9077         testdir1_size=$(order_2 $testdir1_size)
9078         testdir2_size=$(order_2 $testdir2_size)
9079
9080         testdir1_size=$(size_in_KMGT $testdir1_size)
9081         testdir2_size=$(size_in_KMGT $testdir2_size)
9082
9083         echo "source rename dir size: ${testdir1_size}"
9084         echo "target rename dir size: ${testdir2_size}"
9085
9086         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9087         eval $cmd || error "$cmd failed"
9088         local samedir=$($cmd | grep 'same_dir')
9089         local same_sample=$(get_rename_size $testdir1_size)
9090         [ -z "$samedir" ] && error "samedir_rename_size count error"
9091         [[ $same_sample -eq 1 ]] ||
9092                 error "samedir_rename_size error $same_sample"
9093         echo "Check same dir rename stats success"
9094
9095         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9096
9097         # check crossdir rename size
9098         mv ${testdir1}/test_0 ${testdir2}/test_0
9099
9100         testdir1_size=$(ls -l $DIR/${tdir} |
9101                 awk '/stats_testdir1/ {print $5}')
9102         testdir2_size=$(ls -l $DIR/${tdir} |
9103                 awk '/stats_testdir2/ {print $5}')
9104
9105         testdir1_size=$(order_2 $testdir1_size)
9106         testdir2_size=$(order_2 $testdir2_size)
9107
9108         testdir1_size=$(size_in_KMGT $testdir1_size)
9109         testdir2_size=$(size_in_KMGT $testdir2_size)
9110
9111         echo "source rename dir size: ${testdir1_size}"
9112         echo "target rename dir size: ${testdir2_size}"
9113
9114         eval $cmd || error "$cmd failed"
9115         local crossdir=$($cmd | grep 'crossdir')
9116         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9117         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9118         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9119         [[ $src_sample -eq 1 ]] ||
9120                 error "crossdir_rename_size error $src_sample"
9121         [[ $tgt_sample -eq 1 ]] ||
9122                 error "crossdir_rename_size error $tgt_sample"
9123         echo "Check cross dir rename stats success"
9124         rm -rf $DIR/${tdir}
9125 }
9126 run_test 133d "Verifying rename_stats ========================================"
9127
9128 test_133e() {
9129         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9130         remote_ost_nodsh && skip "remote OST with nodsh" && return
9131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9132         local testdir=$DIR/${tdir}/stats_testdir
9133         local ctr f0 f1 bs=32768 count=42 sum
9134
9135         mkdir -p ${testdir} || error "mkdir failed"
9136
9137         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9138
9139         for ctr in {write,read}_bytes; do
9140                 sync
9141                 cancel_lru_locks osc
9142
9143                 do_facet ost1 $LCTL set_param -n \
9144                         "obdfilter.*.exports.clear=clear"
9145
9146                 if [ $ctr = write_bytes ]; then
9147                         f0=/dev/zero
9148                         f1=${testdir}/${tfile}
9149                 else
9150                         f0=${testdir}/${tfile}
9151                         f1=/dev/null
9152                 fi
9153
9154                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9155                         error "dd failed"
9156                 sync
9157                 cancel_lru_locks osc
9158
9159                 sum=$(do_facet ost1 $LCTL get_param \
9160                         "obdfilter.*.exports.*.stats" |
9161                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9162                                 $1 == ctr { sum += $7 }
9163                                 END { printf("%0.0f", sum) }')
9164
9165                 if ((sum != bs * count)); then
9166                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9167                 fi
9168         done
9169
9170         rm -rf $DIR/${tdir}
9171 }
9172 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9173
9174 test_133f() {
9175         local proc_dirs
9176
9177         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9178 /sys/fs/lustre/ /sys/fs/lnet/"
9179         local dir
9180         for dir in $dirs; do
9181                 if [ -d $dir ]; then
9182                         proc_dirs="$proc_dirs $dir"
9183                 fi
9184         done
9185
9186         local facet
9187
9188         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9189         remote_ost_nodsh && skip "remote OST with nodsh" && return
9190         # First without trusting modes.
9191         find $proc_dirs -exec cat '{}' \; &> /dev/null
9192
9193         # Second verifying readability.
9194         find $proc_dirs \
9195                 -type f \
9196                 -exec cat '{}' \; &> /dev/null ||
9197                         error "proc file read failed"
9198
9199         for facet in $SINGLEMDS ost1; do
9200                 do_facet $facet find $proc_dirs \
9201                         ! -name req_history \
9202                         -exec cat '{}' \\\; &> /dev/null
9203
9204                 do_facet $facet find $proc_dirs \
9205                         ! -name req_history \
9206                         -type f \
9207                         -exec cat '{}' \\\; &> /dev/null ||
9208                                 error "proc file read failed"
9209         done
9210 }
9211 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9212
9213 test_133g() {
9214         local proc_dirs
9215
9216         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9217 /sys/fs/lustre/ /sys/fs/lnet/"
9218         local dir
9219         for dir in $dirs; do
9220                 if [ -d $dir ]; then
9221                         proc_dirs="$proc_dirs $dir"
9222                 fi
9223         done
9224
9225         local facet
9226
9227         # Second verifying readability.
9228         find $proc_dirs \
9229                 -type f \
9230                 -not -name force_lbug \
9231                 -not -name changelog_mask \
9232                 -exec badarea_io '{}' \; &> /dev/null ||
9233                 error "find $proc_dirs failed"
9234
9235         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9236                 skip "Too old lustre on MDS"
9237
9238         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9239                 skip "Too old lustre on ost1"
9240
9241         for facet in $SINGLEMDS ost1; do
9242                 do_facet $facet find $proc_dirs \
9243                         -type f \
9244                         -not -name force_lbug \
9245                         -not -name changelog_mask \
9246                         -exec badarea_io '{}' \\\; &> /dev/null ||
9247                 error "$facet find $proc_dirs failed"
9248
9249         done
9250
9251         # remount the FS in case writes/reads /proc break the FS
9252         cleanup || error "failed to unmount"
9253         setup || error "failed to setup"
9254         true
9255 }
9256 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9257
9258 test_140() { #bug-17379
9259         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9260         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9261         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9262         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9263
9264         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9265         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9266         local i=0
9267         while i=`expr $i + 1`; do
9268                 test_mkdir -p $i || error "Creating dir $i"
9269                 cd $i || error "Changing to $i"
9270                 ln -s ../stat stat || error "Creating stat symlink"
9271                 # Read the symlink until ELOOP present,
9272                 # not LBUGing the system is considered success,
9273                 # we didn't overrun the stack.
9274                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9275                 [ $ret -ne 0 ] && {
9276                         if [ $ret -eq 40 ]; then
9277                                 break  # -ELOOP
9278                         else
9279                                 error "Open stat symlink"
9280                                 return
9281                         fi
9282                 }
9283         done
9284         i=`expr $i - 1`
9285         echo "The symlink depth = $i"
9286         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9287                                         error "Invalid symlink depth"
9288
9289         # Test recursive symlink
9290         ln -s symlink_self symlink_self
9291         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9292         echo "open symlink_self returns $ret"
9293         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9294 }
9295 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9296
9297 test_150() {
9298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9299         local TF="$TMP/$tfile"
9300
9301         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9302         cp $TF $DIR/$tfile
9303         cancel_lru_locks osc
9304         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9305         remount_client $MOUNT
9306         df -P $MOUNT
9307         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9308
9309         $TRUNCATE $TF 6000
9310         $TRUNCATE $DIR/$tfile 6000
9311         cancel_lru_locks osc
9312         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9313
9314         echo "12345" >>$TF
9315         echo "12345" >>$DIR/$tfile
9316         cancel_lru_locks osc
9317         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9318
9319         echo "12345" >>$TF
9320         echo "12345" >>$DIR/$tfile
9321         cancel_lru_locks osc
9322         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9323
9324         rm -f $TF
9325         true
9326 }
9327 run_test 150 "truncate/append tests"
9328
9329 #LU-2902 roc_hit was not able to read all values from lproc
9330 function roc_hit_init() {
9331         local list=$(comma_list $(osts_nodes))
9332         local dir=$DIR/$tdir-check
9333         local file=$dir/file
9334         local BEFORE
9335         local AFTER
9336         local idx
9337
9338         test_mkdir -p $dir
9339         #use setstripe to do a write to every ost
9340         for i in $(seq 0 $((OSTCOUNT-1))); do
9341                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9342                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9343                 idx=$(printf %04x $i)
9344                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9345                         awk '$1 == "cache_access" {sum += $7}
9346                                 END { printf("%0.0f", sum) }')
9347
9348                 cancel_lru_locks osc
9349                 cat $file >/dev/null
9350
9351                 AFTER=$(get_osd_param $list *OST*$idx stats |
9352                         awk '$1 == "cache_access" {sum += $7}
9353                                 END { printf("%0.0f", sum) }')
9354
9355                 echo BEFORE:$BEFORE AFTER:$AFTER
9356                 if ! let "AFTER - BEFORE == 4"; then
9357                         rm -rf $dir
9358                         error "roc_hit is not safe to use"
9359                 fi
9360                 rm $file
9361         done
9362
9363         rm -rf $dir
9364 }
9365
9366 function roc_hit() {
9367         local list=$(comma_list $(osts_nodes))
9368         echo $(get_osd_param $list '' stats |
9369                 awk '$1 == "cache_hit" {sum += $7}
9370                         END { printf("%0.0f", sum) }')
9371 }
9372
9373 function set_cache() {
9374         local on=1
9375
9376         if [ "$2" == "off" ]; then
9377                 on=0;
9378         fi
9379         local list=$(comma_list $(osts_nodes))
9380         set_osd_param $list '' $1_cache_enable $on
9381
9382         cancel_lru_locks osc
9383 }
9384
9385 test_151() {
9386         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9387         remote_ost_nodsh && skip "remote OST with nodsh" && return
9388
9389         local CPAGES=3
9390         local list=$(comma_list $(osts_nodes))
9391
9392         # check whether obdfilter is cache capable at all
9393         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9394                 echo "not cache-capable obdfilter"
9395                 return 0
9396         fi
9397
9398         # check cache is enabled on all obdfilters
9399         if get_osd_param $list '' read_cache_enable | grep 0; then
9400                 echo "oss cache is disabled"
9401                 return 0
9402         fi
9403
9404         set_osd_param $list '' writethrough_cache_enable 1
9405
9406         # check write cache is enabled on all obdfilters
9407         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9408                 echo "oss write cache is NOT enabled"
9409                 return 0
9410         fi
9411
9412         roc_hit_init
9413
9414         #define OBD_FAIL_OBD_NO_LRU  0x609
9415         do_nodes $list $LCTL set_param fail_loc=0x609
9416
9417         # pages should be in the case right after write
9418         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9419                 error "dd failed"
9420
9421         local BEFORE=$(roc_hit)
9422         cancel_lru_locks osc
9423         cat $DIR/$tfile >/dev/null
9424         local AFTER=$(roc_hit)
9425
9426         do_nodes $list $LCTL set_param fail_loc=0
9427
9428         if ! let "AFTER - BEFORE == CPAGES"; then
9429                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9430         fi
9431
9432         # the following read invalidates the cache
9433         cancel_lru_locks osc
9434         set_osd_param $list '' read_cache_enable 0
9435         cat $DIR/$tfile >/dev/null
9436
9437         # now data shouldn't be found in the cache
9438         BEFORE=$(roc_hit)
9439         cancel_lru_locks osc
9440         cat $DIR/$tfile >/dev/null
9441         AFTER=$(roc_hit)
9442         if let "AFTER - BEFORE != 0"; then
9443                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9444         fi
9445
9446         set_osd_param $list '' read_cache_enable 1
9447         rm -f $DIR/$tfile
9448 }
9449 run_test 151 "test cache on oss and controls ==============================="
9450
9451 test_152() {
9452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9453         local TF="$TMP/$tfile"
9454
9455         # simulate ENOMEM during write
9456 #define OBD_FAIL_OST_NOMEM      0x226
9457         lctl set_param fail_loc=0x80000226
9458         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9459         cp $TF $DIR/$tfile
9460         sync || error "sync failed"
9461         lctl set_param fail_loc=0
9462
9463         # discard client's cache
9464         cancel_lru_locks osc
9465
9466         # simulate ENOMEM during read
9467         lctl set_param fail_loc=0x80000226
9468         cmp $TF $DIR/$tfile || error "cmp failed"
9469         lctl set_param fail_loc=0
9470
9471         rm -f $TF
9472 }
9473 run_test 152 "test read/write with enomem ============================"
9474
9475 test_153() {
9476         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9477 }
9478 run_test 153 "test if fdatasync does not crash ======================="
9479
9480 dot_lustre_fid_permission_check() {
9481         local fid=$1
9482         local ffid=$MOUNT/.lustre/fid/$fid
9483         local test_dir=$2
9484
9485         echo "stat fid $fid"
9486         stat $ffid > /dev/null || error "stat $ffid failed."
9487         echo "touch fid $fid"
9488         touch $ffid || error "touch $ffid failed."
9489         echo "write to fid $fid"
9490         cat /etc/hosts > $ffid || error "write $ffid failed."
9491         echo "read fid $fid"
9492         diff /etc/hosts $ffid || error "read $ffid failed."
9493         echo "append write to fid $fid"
9494         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9495         echo "rename fid $fid"
9496         mv $ffid $test_dir/$tfile.1 &&
9497                 error "rename $ffid to $tfile.1 should fail."
9498         touch $test_dir/$tfile.1
9499         mv $test_dir/$tfile.1 $ffid &&
9500                 error "rename $tfile.1 to $ffid should fail."
9501         rm -f $test_dir/$tfile.1
9502         echo "truncate fid $fid"
9503         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9504         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9505                 echo "link fid $fid"
9506                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9507         fi
9508         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9509                 echo "setfacl fid $fid"
9510                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9511                 echo "getfacl fid $fid"
9512                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9513         fi
9514         echo "unlink fid $fid"
9515         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9516         echo "mknod fid $fid"
9517         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9518
9519         fid=[0xf00000400:0x1:0x0]
9520         ffid=$MOUNT/.lustre/fid/$fid
9521
9522         echo "stat non-exist fid $fid"
9523         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9524         echo "write to non-exist fid $fid"
9525         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9526         echo "link new fid $fid"
9527         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9528
9529         mkdir -p $test_dir/$tdir
9530         touch $test_dir/$tdir/$tfile
9531         fid=$($LFS path2fid $test_dir/$tdir)
9532         rc=$?
9533         [ $rc -ne 0 ] &&
9534                 error "error: could not get fid for $test_dir/$dir/$tfile."
9535
9536         ffid=$MOUNT/.lustre/fid/$fid
9537
9538         echo "ls $fid"
9539         ls $ffid > /dev/null || error "ls $ffid failed."
9540         echo "touch $fid/$tfile.1"
9541         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9542
9543         echo "touch $MOUNT/.lustre/fid/$tfile"
9544         touch $MOUNT/.lustre/fid/$tfile && \
9545                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9546
9547         echo "setxattr to $MOUNT/.lustre/fid"
9548         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9549
9550         echo "listxattr for $MOUNT/.lustre/fid"
9551         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9552
9553         echo "delxattr from $MOUNT/.lustre/fid"
9554         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9555
9556         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9557         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9558                 error "touch invalid fid should fail."
9559
9560         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9561         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9562                 error "touch non-normal fid should fail."
9563
9564         echo "rename $tdir to $MOUNT/.lustre/fid"
9565         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9566                 error "rename to $MOUNT/.lustre/fid should fail."
9567
9568         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9569         then            # LU-3547
9570                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9571                 local new_obf_mode=777
9572
9573                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9574                 chmod $new_obf_mode $DIR/.lustre/fid ||
9575                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9576
9577                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9578                 [ $obf_mode -eq $new_obf_mode ] ||
9579                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9580
9581                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9582                 chmod $old_obf_mode $DIR/.lustre/fid ||
9583                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9584         fi
9585
9586         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9587         fid=$($LFS path2fid $test_dir/$tfile-2)
9588
9589         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9590         then # LU-5424
9591                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9592                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9593                         error "create lov data thru .lustre failed"
9594         fi
9595         echo "cp /etc/passwd $test_dir/$tfile-2"
9596         cp /etc/passwd $test_dir/$tfile-2 ||
9597                 error "copy to $test_dir/$tfile-2 failed."
9598         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9599         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9600                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9601
9602         rm -rf $test_dir/tfile.lnk
9603         rm -rf $test_dir/$tfile-2
9604 }
9605
9606 test_154A() {
9607         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9608                 skip "Need MDS version at least 2.4.1" && return
9609
9610         touch $DIR/$tfile
9611         local FID=$($LFS path2fid $DIR/$tfile)
9612         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9613
9614         # check that we get the same pathname back
9615         local FOUND=$($LFS fid2path $MOUNT $FID)
9616         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9617         [ "$FOUND" != "$DIR/$tfile" ] &&
9618                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9619
9620         rm -rf $DIR/$tfile
9621 }
9622 run_test 154A "lfs path2fid and fid2path basic checks"
9623
9624 test_154a() {
9625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9626         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9627                 { skip "Need MDS version at least 2.2.51"; return 0; }
9628         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9629
9630         cp /etc/hosts $DIR/$tfile
9631
9632         fid=$($LFS path2fid $DIR/$tfile)
9633         rc=$?
9634         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9635
9636         dot_lustre_fid_permission_check "$fid" $DIR ||
9637                 error "dot lustre permission check $fid failed"
9638
9639         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9640
9641         touch $MOUNT/.lustre/file &&
9642                 error "creation is not allowed under .lustre"
9643
9644         mkdir $MOUNT/.lustre/dir &&
9645                 error "mkdir is not allowed under .lustre"
9646
9647         rm -rf $DIR/$tfile
9648 }
9649 run_test 154a "Open-by-FID"
9650
9651 test_154b() {
9652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9653         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9654                 { skip "Need MDS version at least 2.2.51"; return 0; }
9655
9656         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9657
9658         local remote_dir=$DIR/$tdir/remote_dir
9659         local MDTIDX=1
9660         local rc=0
9661
9662         mkdir -p $DIR/$tdir
9663         $LFS mkdir -i $MDTIDX $remote_dir ||
9664                 error "create remote directory failed"
9665
9666         cp /etc/hosts $remote_dir/$tfile
9667
9668         fid=$($LFS path2fid $remote_dir/$tfile)
9669         rc=$?
9670         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9671
9672         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9673                 error "dot lustre permission check $fid failed"
9674         rm -rf $DIR/$tdir
9675 }
9676 run_test 154b "Open-by-FID for remote directory"
9677
9678 test_154c() {
9679         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9680                 skip "Need MDS version at least 2.4.1" && return
9681
9682         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9683         local FID1=$($LFS path2fid $DIR/$tfile.1)
9684         local FID2=$($LFS path2fid $DIR/$tfile.2)
9685         local FID3=$($LFS path2fid $DIR/$tfile.3)
9686
9687         local N=1
9688         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9689                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9690                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9691                 local want=FID$N
9692                 [ "$FID" = "${!want}" ] ||
9693                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9694                 N=$((N + 1))
9695         done
9696
9697         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9698                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9699                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9700                 N=$((N + 1))
9701         done
9702 }
9703 run_test 154c "lfs path2fid and fid2path multiple arguments"
9704
9705 test_154d() {
9706         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9707                 skip "Need MDS version at least 2.5.53" && return
9708
9709         if remote_mds; then
9710                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9711         else
9712                 nid="0@lo"
9713         fi
9714         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9715         local fd
9716         local cmd
9717
9718         rm -f $DIR/$tfile
9719         touch $DIR/$tfile
9720
9721         fid=$($LFS path2fid $DIR/$tfile)
9722         # Open the file
9723         fd=$(free_fd)
9724         cmd="exec $fd<$DIR/$tfile"
9725         eval $cmd
9726         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9727         echo $fid_list | grep $fid
9728         rc=$?
9729
9730         cmd="exec $fd>/dev/null"
9731         eval $cmd
9732         if [ $rc -ne 0 ]; then
9733                 error "FID $fid not found in open files list $fid_list"
9734         fi
9735 }
9736 run_test 154d "Verify open file fid"
9737
9738 test_154e()
9739 {
9740         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9741                 skip "Need MDS version at least 2.6.50" && return
9742
9743         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9744                 error ".lustre returned by readdir"
9745         fi
9746 }
9747 run_test 154e ".lustre is not returned by readdir"
9748
9749 test_154f() {
9750         # create parent directory on a single MDT to avoid cross-MDT hardlinks
9751         test_mkdir -p -c1 $DIR/$tdir/d
9752         # test dirs inherit from its stripe
9753         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
9754         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
9755         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
9756         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
9757         touch $DIR/f
9758
9759         # get fid of parents
9760         local FID0=$($LFS path2fid $DIR/$tdir/d)
9761         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
9762         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
9763         local FID3=$($LFS path2fid $DIR)
9764
9765         # check that path2fid --parents returns expected <parent_fid>/name
9766         # 1) test for a directory (single parent)
9767         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
9768         [ "$parent" == "$FID0/foo1" ] ||
9769                 error "expected parent: $FID0/foo1, got: $parent"
9770
9771         # 2) test for a file with nlink > 1 (multiple parents)
9772         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
9773         echo "$parent" | grep -F "$FID1/$tfile" ||
9774                 error "$FID1/$tfile not returned in parent list"
9775         echo "$parent" | grep -F "$FID2/link" ||
9776                 error "$FID2/link not returned in parent list"
9777
9778         # 3) get parent by fid
9779         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
9780         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9781         echo "$parent" | grep -F "$FID1/$tfile" ||
9782                 error "$FID1/$tfile not returned in parent list (by fid)"
9783         echo "$parent" | grep -F "$FID2/link" ||
9784                 error "$FID2/link not returned in parent list (by fid)"
9785
9786         # 4) test for entry in root directory
9787         parent=$($LFS path2fid --parents $DIR/f)
9788         echo "$parent" | grep -F "$FID3/f" ||
9789                 error "$FID3/f not returned in parent list"
9790
9791         # 5) test it on root directory
9792         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
9793                 error "$MOUNT should not have parents"
9794
9795         # enable xattr caching and check that linkea is correctly updated
9796         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9797         save_lustre_params client "llite.*.xattr_cache" > $save
9798         lctl set_param llite.*.xattr_cache 1
9799
9800         # 6.1) linkea update on rename
9801         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
9802
9803         # get parents by fid
9804         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9805         # foo1 should no longer be returned in parent list
9806         echo "$parent" | grep -F "$FID1" &&
9807                 error "$FID1 should no longer be in parent list"
9808         # the new path should appear
9809         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
9810                 error "$FID2/$tfile.moved is not in parent list"
9811
9812         # 6.2) linkea update on unlink
9813         rm -f $DIR/$tdir/d/foo2/link
9814         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9815         # foo2/link should no longer be returned in parent list
9816         echo "$parent" | grep -F "$FID2/link" &&
9817                 error "$FID2/link should no longer be in parent list"
9818         true
9819
9820         rm -f $DIR/f
9821         restore_lustre_params < $save
9822 }
9823 run_test 154f "get parent fids by reading link ea"
9824
9825 test_155_small_load() {
9826     local temp=$TMP/$tfile
9827     local file=$DIR/$tfile
9828
9829     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9830         error "dd of=$temp bs=6096 count=1 failed"
9831     cp $temp $file
9832     cancel_lru_locks osc
9833     cmp $temp $file || error "$temp $file differ"
9834
9835     $TRUNCATE $temp 6000
9836     $TRUNCATE $file 6000
9837     cmp $temp $file || error "$temp $file differ (truncate1)"
9838
9839     echo "12345" >>$temp
9840     echo "12345" >>$file
9841     cmp $temp $file || error "$temp $file differ (append1)"
9842
9843     echo "12345" >>$temp
9844     echo "12345" >>$file
9845     cmp $temp $file || error "$temp $file differ (append2)"
9846
9847     rm -f $temp $file
9848     true
9849 }
9850
9851 test_155_big_load() {
9852     remote_ost_nodsh && skip "remote OST with nodsh" && return
9853     local temp=$TMP/$tfile
9854     local file=$DIR/$tfile
9855
9856     free_min_max
9857     local cache_size=$(do_facet ost$((MAXI+1)) \
9858         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9859     local large_file_size=$((cache_size * 2))
9860
9861     echo "OSS cache size: $cache_size KB"
9862     echo "Large file size: $large_file_size KB"
9863
9864     [ $MAXV -le $large_file_size ] && \
9865         skip_env "max available OST size needs > $large_file_size KB" && \
9866         return 0
9867
9868     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9869
9870     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9871         error "dd of=$temp bs=$large_file_size count=1k failed"
9872     cp $temp $file
9873     ls -lh $temp $file
9874     cancel_lru_locks osc
9875     cmp $temp $file || error "$temp $file differ"
9876
9877     rm -f $temp $file
9878     true
9879 }
9880
9881 test_155a() {
9882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9883         set_cache read on
9884         set_cache writethrough on
9885         test_155_small_load
9886 }
9887 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9888
9889 test_155b() {
9890         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9891         set_cache read on
9892         set_cache writethrough off
9893         test_155_small_load
9894 }
9895 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9896
9897 test_155c() {
9898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9899         set_cache read off
9900         set_cache writethrough on
9901         test_155_small_load
9902 }
9903 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9904
9905 test_155d() {
9906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9907         set_cache read off
9908         set_cache writethrough off
9909         test_155_small_load
9910 }
9911 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9912
9913 test_155e() {
9914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9915         set_cache read on
9916         set_cache writethrough on
9917         test_155_big_load
9918 }
9919 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9920
9921 test_155f() {
9922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9923         set_cache read on
9924         set_cache writethrough off
9925         test_155_big_load
9926 }
9927 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9928
9929 test_155g() {
9930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9931         set_cache read off
9932         set_cache writethrough on
9933         test_155_big_load
9934 }
9935 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9936
9937 test_155h() {
9938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9939         set_cache read off
9940         set_cache writethrough off
9941         test_155_big_load
9942 }
9943 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9944
9945 test_156() {
9946         remote_ost_nodsh && skip "remote OST with nodsh" && return
9947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9948         local CPAGES=3
9949         local BEFORE
9950         local AFTER
9951         local file="$DIR/$tfile"
9952
9953         [ "$(facet_fstype ost1)" = "zfs" ] &&
9954                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9955                 return
9956
9957         roc_hit_init
9958
9959     log "Turn on read and write cache"
9960     set_cache read on
9961     set_cache writethrough on
9962
9963     log "Write data and read it back."
9964     log "Read should be satisfied from the cache."
9965     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9966     BEFORE=`roc_hit`
9967     cancel_lru_locks osc
9968     cat $file >/dev/null
9969     AFTER=`roc_hit`
9970     if ! let "AFTER - BEFORE == CPAGES"; then
9971         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9972     else
9973         log "cache hits:: before: $BEFORE, after: $AFTER"
9974     fi
9975
9976     log "Read again; it should be satisfied from the cache."
9977     BEFORE=$AFTER
9978     cancel_lru_locks osc
9979     cat $file >/dev/null
9980     AFTER=`roc_hit`
9981     if ! let "AFTER - BEFORE == CPAGES"; then
9982         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9983     else
9984         log "cache hits:: before: $BEFORE, after: $AFTER"
9985     fi
9986
9987
9988     log "Turn off the read cache and turn on the write cache"
9989     set_cache read off
9990     set_cache writethrough on
9991
9992     log "Read again; it should be satisfied from the cache."
9993     BEFORE=`roc_hit`
9994     cancel_lru_locks osc
9995     cat $file >/dev/null
9996     AFTER=`roc_hit`
9997     if ! let "AFTER - BEFORE == CPAGES"; then
9998         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9999     else
10000         log "cache hits:: before: $BEFORE, after: $AFTER"
10001     fi
10002
10003     log "Read again; it should not be satisfied from the cache."
10004     BEFORE=$AFTER
10005     cancel_lru_locks osc
10006     cat $file >/dev/null
10007     AFTER=`roc_hit`
10008     if ! let "AFTER - BEFORE == 0"; then
10009         error "IN CACHE: before: $BEFORE, after: $AFTER"
10010     else
10011         log "cache hits:: before: $BEFORE, after: $AFTER"
10012     fi
10013
10014     log "Write data and read it back."
10015     log "Read should be satisfied from the cache."
10016     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10017     BEFORE=`roc_hit`
10018     cancel_lru_locks osc
10019     cat $file >/dev/null
10020     AFTER=`roc_hit`
10021     if ! let "AFTER - BEFORE == CPAGES"; then
10022         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10023     else
10024         log "cache hits:: before: $BEFORE, after: $AFTER"
10025     fi
10026
10027     log "Read again; it should not be satisfied from the cache."
10028     BEFORE=$AFTER
10029     cancel_lru_locks osc
10030     cat $file >/dev/null
10031     AFTER=`roc_hit`
10032     if ! let "AFTER - BEFORE == 0"; then
10033         error "IN CACHE: before: $BEFORE, after: $AFTER"
10034     else
10035         log "cache hits:: before: $BEFORE, after: $AFTER"
10036     fi
10037
10038
10039     log "Turn off read and write cache"
10040     set_cache read off
10041     set_cache writethrough off
10042
10043     log "Write data and read it back"
10044     log "It should not be satisfied from the cache."
10045     rm -f $file
10046     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10047     cancel_lru_locks osc
10048     BEFORE=`roc_hit`
10049     cat $file >/dev/null
10050     AFTER=`roc_hit`
10051         if ! let "AFTER - BEFORE == 0"; then
10052                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10053         else
10054                 log "cache hits:: before: $BEFORE, after: $AFTER"
10055         fi
10056
10057     log "Turn on the read cache and turn off the write cache"
10058     set_cache read on
10059     set_cache writethrough off
10060
10061     log "Write data and read it back"
10062     log "It should not be satisfied from the cache."
10063     rm -f $file
10064     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10065     BEFORE=`roc_hit`
10066     cancel_lru_locks osc
10067     cat $file >/dev/null
10068     AFTER=`roc_hit`
10069         if ! let "AFTER - BEFORE == 0"; then
10070                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10071         else
10072                 log "cache hits:: before: $BEFORE, after: $AFTER"
10073         fi
10074
10075     log "Read again; it should be satisfied from the cache."
10076     BEFORE=`roc_hit`
10077     cancel_lru_locks osc
10078     cat $file >/dev/null
10079     AFTER=`roc_hit`
10080     if ! let "AFTER - BEFORE == CPAGES"; then
10081         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10082     else
10083         log "cache hits:: before: $BEFORE, after: $AFTER"
10084     fi
10085
10086     rm -f $file
10087 }
10088 run_test 156 "Verification of tunables ============================"
10089
10090 #Changelogs
10091 err17935 () {
10092         if [[ $MDSCOUNT -gt 1 ]]; then
10093                 error_ignore bz17935 $*
10094         else
10095                 error $*
10096         fi
10097 }
10098
10099 changelog_chmask()
10100 {
10101         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10102
10103         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10104
10105         if [ $MASK -eq 1 ]; then
10106                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10107         else
10108                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10109         fi
10110 }
10111
10112 changelog_extract_field() {
10113         local mdt=$1
10114         local cltype=$2
10115         local file=$3
10116         local identifier=$4
10117
10118         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10119                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10120                 tail -1
10121 }
10122
10123 test_160a() {
10124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10125         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10126         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10127                 { skip "Need MDS version at least 2.2.0"; return; }
10128
10129         local CL_USERS="mdd.$MDT0.changelog_users"
10130         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10131         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10132         echo "Registered as changelog user $USER"
10133         $GET_CL_USERS | grep -q $USER ||
10134                 error "User $USER not found in changelog_users"
10135
10136         # change something
10137         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10138         touch $DIR/$tdir/pics/2008/zachy/timestamp
10139         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10140         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10141         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10142         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10143         rm $DIR/$tdir/pics/desktop.jpg
10144
10145         $LFS changelog $MDT0 | tail -5
10146
10147         echo "verifying changelog mask"
10148         changelog_chmask "MKDIR"
10149         changelog_chmask "CLOSE"
10150
10151         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10152         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10153
10154         changelog_chmask "MKDIR"
10155         changelog_chmask "CLOSE"
10156
10157         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10158         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10159
10160         $LFS changelog $MDT0
10161         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10162         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10163         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10164         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10165
10166         # verify contents
10167         echo "verifying target fid"
10168         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10169         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10170         [ "$fidc" == "$fidf" ] ||
10171                 err17935 "fid in changelog $fidc != file fid $fidf"
10172         echo "verifying parent fid"
10173         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10174         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10175         [ "$fidc" == "$fidf" ] ||
10176                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10177
10178         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10179         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10180         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10181         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10182         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10183                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10184
10185         MIN_REC=$($GET_CL_USERS |
10186                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10187         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10188         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10189         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10190                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10191
10192         # LU-3446 changelog index reset on MDT restart
10193         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10194         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10195         $LFS changelog_clear $MDT0 $USER 0
10196         stop $SINGLEMDS || error "Fail to stop MDT."
10197         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10198         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10199         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10200         [ $CUR_REC1 == $CUR_REC2 ] ||
10201                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10202
10203         echo "verifying user deregister"
10204         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10205         $GET_CL_USERS | grep -q $USER &&
10206                 error "User $USER still in changelog_users"
10207
10208         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10209         if [ $USERS -eq 0 ]; then
10210                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10211                 touch $DIR/$tdir/chloe
10212                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10213                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10214                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10215         else
10216                 echo "$USERS other changelog users; can't verify off"
10217         fi
10218 }
10219 run_test 160a "changelog sanity"
10220
10221 test_160b() { # LU-3587
10222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10223         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10224         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10225                 { skip "Need MDS version at least 2.2.0"; return; }
10226
10227         local CL_USERS="mdd.$MDT0.changelog_users"
10228         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10229         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10230         echo "Registered as changelog user $USER"
10231         $GET_CL_USERS | grep -q $USER ||
10232                 error "User $USER not found in changelog_users"
10233
10234         local LONGNAME1=$(str_repeat a 255)
10235         local LONGNAME2=$(str_repeat b 255)
10236
10237         cd $DIR
10238         echo "creating very long named file"
10239         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10240         echo "moving very long named file"
10241         mv $LONGNAME1 $LONGNAME2
10242
10243         $LFS changelog $MDT0 | grep RENME
10244
10245         echo "deregistering $USER"
10246         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10247
10248         rm -f $LONGNAME2
10249 }
10250 run_test 160b "Verify that very long rename doesn't crash in changelog"
10251
10252 test_160c() {
10253         local rc=0
10254         local server_version=$(lustre_version_code $SINGLEMDS)
10255
10256         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10257                 [[ $server_version -gt $(version_code 2.5.1) &&
10258                    $server_version -lt $(version_code 2.5.50) ]] ||
10259                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10261
10262         # Registration step
10263         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10264                 changelog_register -n)
10265
10266         rm -rf $DIR/$tdir
10267         mkdir -p $DIR/$tdir
10268         $MCREATE $DIR/$tdir/foo_160c
10269         changelog_chmask "TRUNC"
10270         $TRUNCATE $DIR/$tdir/foo_160c 200
10271         changelog_chmask "TRUNC"
10272         $TRUNCATE $DIR/$tdir/foo_160c 199
10273         $LFS changelog $MDT0
10274         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10275         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10276         $LFS changelog_clear $MDT0 $USER 0
10277
10278         # Deregistration step
10279         echo "deregistering $USER"
10280         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10281 }
10282 run_test 160c "verify that changelog log catch the truncate event"
10283
10284 test_161a() {
10285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10286         test_mkdir -p -c1 $DIR/$tdir
10287         cp /etc/hosts $DIR/$tdir/$tfile
10288         test_mkdir -c1 $DIR/$tdir/foo1
10289         test_mkdir -c1 $DIR/$tdir/foo2
10290         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10291         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10292         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10293         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10294         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10295         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10296                 $LFS fid2path $DIR $FID
10297                 err17935 "bad link ea"
10298         fi
10299     # middle
10300     rm $DIR/$tdir/foo2/zachary
10301     # last
10302     rm $DIR/$tdir/foo2/thor
10303     # first
10304     rm $DIR/$tdir/$tfile
10305     # rename
10306     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10307     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10308         then
10309         $LFS fid2path $DIR $FID
10310         err17935 "bad link rename"
10311     fi
10312     rm $DIR/$tdir/foo2/maggie
10313
10314         # overflow the EA
10315         local longname=filename_avg_len_is_thirty_two_
10316         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10317                 error "failed to hardlink many files"
10318         links=$($LFS fid2path $DIR $FID | wc -l)
10319         echo -n "${links}/1000 links in link EA"
10320         [[ $links -gt 60 ]] ||
10321                 err17935 "expected at least 60 links in link EA"
10322         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10323                 error "failed to unlink many hardlinks"
10324 }
10325 run_test 161a "link ea sanity"
10326
10327 test_161b() {
10328         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10329         [ $MDSCOUNT -lt 2 ] &&
10330                 skip "skipping remote directory test" && return
10331         local MDTIDX=1
10332         local remote_dir=$DIR/$tdir/remote_dir
10333
10334         mkdir -p $DIR/$tdir
10335         $LFS mkdir -i $MDTIDX $remote_dir ||
10336                 error "create remote directory failed"
10337
10338         cp /etc/hosts $remote_dir/$tfile
10339         mkdir -p $remote_dir/foo1
10340         mkdir -p $remote_dir/foo2
10341         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10342         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10343         ln $remote_dir/$tfile $remote_dir/foo1/luna
10344         ln $remote_dir/$tfile $remote_dir/foo2/thor
10345
10346         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10347                      tr -d ']')
10348         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10349                 $LFS fid2path $DIR $FID
10350                 err17935 "bad link ea"
10351         fi
10352         # middle
10353         rm $remote_dir/foo2/zachary
10354         # last
10355         rm $remote_dir/foo2/thor
10356         # first
10357         rm $remote_dir/$tfile
10358         # rename
10359         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10360         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10361         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10362                 $LFS fid2path $DIR $FID
10363                 err17935 "bad link rename"
10364         fi
10365         rm $remote_dir/foo2/maggie
10366
10367         # overflow the EA
10368         local longname=filename_avg_len_is_thirty_two_
10369         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10370                 error "failed to hardlink many files"
10371         links=$($LFS fid2path $DIR $FID | wc -l)
10372         echo -n "${links}/1000 links in link EA"
10373         [[ ${links} -gt 60 ]] ||
10374                 err17935 "expected at least 60 links in link EA"
10375         unlinkmany $remote_dir/foo2/$longname 1000 ||
10376         error "failed to unlink many hardlinks"
10377 }
10378 run_test 161b "link ea sanity under remote directory"
10379
10380 test_161c() {
10381         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10383         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10384                 skip "Need MDS version at least 2.1.5" && return
10385
10386         # define CLF_RENAME_LAST 0x0001
10387         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10388         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10389                 changelog_register -n)
10390         rm -rf $DIR/$tdir
10391         mkdir -p $DIR/$tdir
10392         touch $DIR/$tdir/foo_161c
10393         touch $DIR/$tdir/bar_161c
10394         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10395         $LFS changelog $MDT0 | grep RENME
10396         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10397                 cut -f5 -d' ')
10398         $LFS changelog_clear $MDT0 $USER 0
10399         if [ x$flags != "x0x1" ]; then
10400                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10401                         $USER
10402                 error "flag $flags is not 0x1"
10403         fi
10404         echo "rename overwrite a target having nlink = 1," \
10405                 "changelog record has flags of $flags"
10406
10407         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10408         touch $DIR/$tdir/foo_161c
10409         touch $DIR/$tdir/bar_161c
10410         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10411         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10412         $LFS changelog $MDT0 | grep RENME
10413         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10414         $LFS changelog_clear $MDT0 $USER 0
10415         if [ x$flags != "x0x0" ]; then
10416                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10417                         $USER
10418                 error "flag $flags is not 0x0"
10419         fi
10420         echo "rename overwrite a target having nlink > 1," \
10421                 "changelog record has flags of $flags"
10422
10423         # rename doesn't overwrite a target (changelog flag 0x0)
10424         touch $DIR/$tdir/foo_161c
10425         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10426         $LFS changelog $MDT0 | grep RENME
10427         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10428         $LFS changelog_clear $MDT0 $USER 0
10429         if [ x$flags != "x0x0" ]; then
10430                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10431                         $USER
10432                 error "flag $flags is not 0x0"
10433         fi
10434         echo "rename doesn't overwrite a target," \
10435                 "changelog record has flags of $flags"
10436
10437         # define CLF_UNLINK_LAST 0x0001
10438         # unlink a file having nlink = 1 (changelog flag 0x1)
10439         rm -f $DIR/$tdir/foo2_161c
10440         $LFS changelog $MDT0 | grep UNLNK
10441         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10442         $LFS changelog_clear $MDT0 $USER 0
10443         if [ x$flags != "x0x1" ]; then
10444                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10445                         $USER
10446                 error "flag $flags is not 0x1"
10447         fi
10448         echo "unlink a file having nlink = 1," \
10449                 "changelog record has flags of $flags"
10450
10451         # unlink a file having nlink > 1 (changelog flag 0x0)
10452         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10453         rm -f $DIR/$tdir/foobar_161c
10454         $LFS changelog $MDT0 | grep UNLNK
10455         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10456         $LFS changelog_clear $MDT0 $USER 0
10457         if [ x$flags != "x0x0" ]; then
10458                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10459                         $USER
10460                 error "flag $flags is not 0x0"
10461         fi
10462         echo "unlink a file having nlink > 1," \
10463                 "changelog record has flags of $flags"
10464         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10465 }
10466 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10467
10468 check_path() {
10469     local expected=$1
10470     shift
10471     local fid=$2
10472
10473     local path=$(${LFS} fid2path $*)
10474     RC=$?
10475
10476     if [ $RC -ne 0 ]; then
10477         err17935 "path looked up of $expected failed. Error $RC"
10478         return $RC
10479     elif [ "${path}" != "${expected}" ]; then
10480         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10481         return 2
10482     fi
10483     echo "fid $fid resolves to path $path (expected $expected)"
10484 }
10485
10486 test_162() {
10487         # Make changes to filesystem
10488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10489         test_mkdir -p -c1 $DIR/$tdir/d2
10490         touch $DIR/$tdir/d2/$tfile
10491         touch $DIR/$tdir/d2/x1
10492         touch $DIR/$tdir/d2/x2
10493         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10494         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10495         # regular file
10496         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10497         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10498                 error "check path $tdir/d2/$tfile failed"
10499
10500         # softlink
10501         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10502         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10503         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10504                 error "check path $tdir/d2/p/q/r/slink failed"
10505
10506         # softlink to wrong file
10507         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10508         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10509         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10510                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10511
10512         # hardlink
10513         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10514         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10515         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10516         # fid2path dir/fsname should both work
10517         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10518                 error "check path $tdir/d2/a/b/c/new_file failed"
10519         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10520                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10521
10522         # hardlink count: check that there are 2 links
10523         # Doesnt work with CMD yet: 17935
10524         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10525                 err17935 "expected 2 links"
10526
10527         # hardlink indexing: remove the first link
10528         rm $DIR/$tdir/d2/p/q/r/hlink
10529         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10530                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10531
10532         return 0
10533 }
10534 run_test 162 "path lookup sanity"
10535
10536 test_162b() {
10537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10538         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10539
10540         mkdir $DIR/$tdir
10541         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10542                                 error "create striped dir failed"
10543
10544         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10545                                         tail -n 1 | awk '{print $2}')
10546         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10547
10548         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10549         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10550
10551         # regular file
10552         for ((i=0;i<5;i++)); do
10553                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10554                         error "get fid for f$i failed"
10555                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10556                         error "check path $tdir/striped_dir/f$i failed"
10557
10558                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10559                         error "get fid for d$i failed"
10560                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10561                         error "check path $tdir/striped_dir/d$i failed"
10562         done
10563
10564         return 0
10565 }
10566 run_test 162b "striped directory path lookup sanity"
10567
10568 test_169() {
10569         # do directio so as not to populate the page cache
10570         log "creating a 10 Mb file"
10571         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10572         log "starting reads"
10573         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10574         log "truncating the file"
10575         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10576         log "killing dd"
10577         kill %+ || true # reads might have finished
10578         echo "wait until dd is finished"
10579         wait
10580         log "removing the temporary file"
10581         rm -rf $DIR/$tfile || error "tmp file removal failed"
10582 }
10583 run_test 169 "parallel read and truncate should not deadlock"
10584
10585 test_170() {
10586         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10587         $LCTL clear     # bug 18514
10588         $LCTL debug_daemon start $TMP/${tfile}_log_good
10589         touch $DIR/$tfile
10590         $LCTL debug_daemon stop
10591         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10592                error "sed failed to read log_good"
10593
10594         $LCTL debug_daemon start $TMP/${tfile}_log_good
10595         rm -rf $DIR/$tfile
10596         $LCTL debug_daemon stop
10597
10598         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10599                error "lctl df log_bad failed"
10600
10601         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10602         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10603
10604         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10605         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10606
10607         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10608                 error "bad_line good_line1 good_line2 are empty"
10609
10610         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10611         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10612         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10613
10614         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10615         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10616         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10617
10618         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10619                 error "bad_line_new good_line_new are empty"
10620
10621         local expected_good=$((good_line1 + good_line2*2))
10622
10623         rm -f $TMP/${tfile}*
10624         # LU-231, short malformed line may not be counted into bad lines
10625         if [ $bad_line -ne $bad_line_new ] &&
10626                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10627                 error "expected $bad_line bad lines, but got $bad_line_new"
10628                 return 1
10629         fi
10630
10631         if [ $expected_good -ne $good_line_new ]; then
10632                 error "expected $expected_good good lines, but got $good_line_new"
10633                 return 2
10634         fi
10635         true
10636 }
10637 run_test 170 "test lctl df to handle corrupted log ====================="
10638
10639 test_171() { # bug20592
10640         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10641 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10642         $LCTL set_param fail_loc=0x50e
10643         $LCTL set_param fail_val=3000
10644         multiop_bg_pause $DIR/$tfile O_s || true
10645         local MULTIPID=$!
10646         kill -USR1 $MULTIPID
10647         # cause log dump
10648         sleep 3
10649         wait $MULTIPID
10650         if dmesg | grep "recursive fault"; then
10651                 error "caught a recursive fault"
10652         fi
10653         $LCTL set_param fail_loc=0
10654         true
10655 }
10656 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10657
10658 # it would be good to share it with obdfilter-survey/iokit-libecho code
10659 setup_obdecho_osc () {
10660         local rc=0
10661         local ost_nid=$1
10662         local obdfilter_name=$2
10663         echo "Creating new osc for $obdfilter_name on $ost_nid"
10664         # make sure we can find loopback nid
10665         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10666
10667         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10668                            ${obdfilter_name}_osc_UUID || rc=2; }
10669         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10670                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10671         return $rc
10672 }
10673
10674 cleanup_obdecho_osc () {
10675         local obdfilter_name=$1
10676         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10677         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10678         return 0
10679 }
10680
10681 obdecho_test() {
10682         local OBD=$1
10683         local node=$2
10684         local pages=${3:-64}
10685         local rc=0
10686         local id
10687
10688         local count=10
10689         local obd_size=$(get_obd_size $node $OBD)
10690         local page_size=$(get_page_size $node)
10691         if [[ -n "$obd_size" ]]; then
10692                 local new_count=$((obd_size / (pages * page_size / 1024)))
10693                 [[ $new_count -ge $count ]] || count=$new_count
10694         fi
10695
10696         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10697         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10698                            rc=2; }
10699         if [ $rc -eq 0 ]; then
10700             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10701             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10702         fi
10703         echo "New object id is $id"
10704         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10705                            rc=4; }
10706         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10707                            "test_brw $count w v $pages $id" || rc=4; }
10708         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10709                            rc=4; }
10710         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10711                                         "cleanup" || rc=5; }
10712         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10713                                         "detach" || rc=6; }
10714         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10715         return $rc
10716 }
10717
10718 test_180a() {
10719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10720         remote_ost_nodsh && skip "remote OST with nodsh" && return
10721         local rc=0
10722         local rmmod_local=0
10723
10724         if ! module_loaded obdecho; then
10725             load_module obdecho/obdecho
10726             rmmod_local=1
10727         fi
10728
10729         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10730         local host=$(lctl get_param -n osc.$osc.import |
10731                              awk '/current_connection:/ {print $2}' )
10732         local target=$(lctl get_param -n osc.$osc.import |
10733                              awk '/target:/ {print $2}' )
10734         target=${target%_UUID}
10735
10736         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10737         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10738         [[ -n $target ]] && cleanup_obdecho_osc $target
10739         [ $rmmod_local -eq 1 ] && rmmod obdecho
10740         return $rc
10741 }
10742 run_test 180a "test obdecho on osc"
10743
10744 test_180b() {
10745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10746         remote_ost_nodsh && skip "remote OST with nodsh" && return
10747         local rc=0
10748         local rmmod_remote=0
10749
10750         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10751                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10752                       "modprobe obdecho; }" && rmmod_remote=1
10753         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10754         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10755         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10756         return $rc
10757 }
10758 run_test 180b "test obdecho directly on obdfilter"
10759
10760 test_180c() { # LU-2598
10761         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10762         remote_ost_nodsh && skip "remote OST with nodsh" && return
10763         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10764                 skip "Need MDS version at least 2.4.0" && return
10765
10766         local rc=0
10767         local rmmod_remote=false
10768         local pages=16384 # 64MB bulk I/O RPC size
10769         local target
10770
10771         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10772                 rmmod_remote=true || error "failed to load module obdecho"
10773
10774         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10775                 head -n1)
10776         if [ -n "$target" ]; then
10777                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10778         else
10779                 echo "there is no obdfilter target on ost1"
10780                 rc=2
10781         fi
10782         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10783         return $rc
10784 }
10785 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10786
10787 test_181() { # bug 22177
10788         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10789         # create enough files to index the directory
10790         createmany -o $DIR/$tdir/foobar 4000
10791         # print attributes for debug purpose
10792         lsattr -d .
10793         # open dir
10794         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10795         MULTIPID=$!
10796         # remove the files & current working dir
10797         unlinkmany $DIR/$tdir/foobar 4000
10798         rmdir $DIR/$tdir
10799         kill -USR1 $MULTIPID
10800         wait $MULTIPID
10801         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10802         return 0
10803 }
10804 run_test 181 "Test open-unlinked dir ========================"
10805
10806 test_182() {
10807         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10808         # disable MDC RPC lock wouldn't crash client
10809         local fcount=1000
10810         local tcount=4
10811
10812         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10813 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10814         $LCTL set_param fail_loc=0x804
10815
10816         for (( i=0; i < $tcount; i++ )) ; do
10817                 mkdir $DIR/$tdir/$i
10818                 createmany -o $DIR/$tdir/$i/f- $fcount &
10819         done
10820         wait
10821
10822         for (( i=0; i < $tcount; i++ )) ; do
10823                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10824         done
10825         wait
10826
10827         rm -rf $DIR/$tdir
10828
10829         $LCTL set_param fail_loc=0
10830 }
10831 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10832
10833 test_183() { # LU-2275
10834         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10835         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10836                 skip "Need MDS version at least 2.3.56" && return
10837
10838         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10839         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10840         echo aaa > $DIR/$tdir/$tfile
10841
10842 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10843         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10844
10845         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10846         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10847
10848         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10849
10850         # Flush negative dentry cache
10851         touch $DIR/$tdir/$tfile
10852
10853         # We are not checking for any leaked references here, they'll
10854         # become evident next time we do cleanup with module unload.
10855         rm -rf $DIR/$tdir
10856 }
10857 run_test 183 "No crash or request leak in case of strange dispositions ========"
10858
10859 # test suite 184 is for LU-2016, LU-2017
10860 test_184a() {
10861         check_swap_layouts_support && return 0
10862
10863         dir0=$DIR/$tdir/$testnum
10864         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10865         ref1=/etc/passwd
10866         ref2=/etc/group
10867         file1=$dir0/f1
10868         file2=$dir0/f2
10869         $SETSTRIPE -c1 $file1
10870         cp $ref1 $file1
10871         $SETSTRIPE -c2 $file2
10872         cp $ref2 $file2
10873         gen1=$($GETSTRIPE -g $file1)
10874         gen2=$($GETSTRIPE -g $file2)
10875
10876         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10877         gen=$($GETSTRIPE -g $file1)
10878         [[ $gen1 != $gen ]] ||
10879                 "Layout generation on $file1 does not change"
10880         gen=$($GETSTRIPE -g $file2)
10881         [[ $gen2 != $gen ]] ||
10882                 "Layout generation on $file2 does not change"
10883
10884         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10885         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10886 }
10887 run_test 184a "Basic layout swap"
10888
10889 test_184b() {
10890         check_swap_layouts_support && return 0
10891
10892         dir0=$DIR/$tdir/$testnum
10893         mkdir -p $dir0 || error "creating dir $dir0"
10894         file1=$dir0/f1
10895         file2=$dir0/f2
10896         file3=$dir0/f3
10897         dir1=$dir0/d1
10898         dir2=$dir0/d2
10899         mkdir $dir1 $dir2
10900         $SETSTRIPE -c1 $file1
10901         $SETSTRIPE -c2 $file2
10902         $SETSTRIPE -c1 $file3
10903         chown $RUNAS_ID $file3
10904         gen1=$($GETSTRIPE -g $file1)
10905         gen2=$($GETSTRIPE -g $file2)
10906
10907         $LFS swap_layouts $dir1 $dir2 &&
10908                 error "swap of directories layouts should fail"
10909         $LFS swap_layouts $dir1 $file1 &&
10910                 error "swap of directory and file layouts should fail"
10911         $RUNAS $LFS swap_layouts $file1 $file2 &&
10912                 error "swap of file we cannot write should fail"
10913         $LFS swap_layouts $file1 $file3 &&
10914                 error "swap of file with different owner should fail"
10915         /bin/true # to clear error code
10916 }
10917 run_test 184b "Forbidden layout swap (will generate errors)"
10918
10919 test_184c() {
10920         check_swap_layouts_support && return 0
10921
10922         local dir0=$DIR/$tdir/$testnum
10923         mkdir -p $dir0 || error "creating dir $dir0"
10924
10925         local ref1=$dir0/ref1
10926         local ref2=$dir0/ref2
10927         local file1=$dir0/file1
10928         local file2=$dir0/file2
10929         # create a file large enough for the concurent test
10930         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10931         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10932         echo "ref file size: ref1($(stat -c %s $ref1))," \
10933              "ref2($(stat -c %s $ref2))"
10934
10935         cp $ref2 $file2
10936         dd if=$ref1 of=$file1 bs=16k &
10937         local DD_PID=$!
10938
10939         # Make sure dd starts to copy file
10940         while [ ! -f $file1 ]; do sleep 0.1; done
10941
10942         $LFS swap_layouts $file1 $file2
10943         local rc=$?
10944         wait $DD_PID
10945         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10946         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10947
10948         # how many bytes copied before swapping layout
10949         local copied=$(stat -c %s $file2)
10950         local remaining=$(stat -c %s $ref1)
10951         remaining=$((remaining - copied))
10952         echo "Copied $copied bytes before swapping layout..."
10953
10954         cmp -n $copied $file1 $ref2 | grep differ &&
10955                 error "Content mismatch [0, $copied) of ref2 and file1"
10956         cmp -n $copied $file2 $ref1 ||
10957                 error "Content mismatch [0, $copied) of ref1 and file2"
10958         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10959                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10960
10961         # clean up
10962         rm -f $ref1 $ref2 $file1 $file2
10963 }
10964 run_test 184c "Concurrent write and layout swap"
10965
10966 test_184d() {
10967         check_swap_layouts_support && return 0
10968         [ -z "$(which getfattr 2>/dev/null)" ] &&
10969                 skip "no getfattr command" && return 0
10970
10971         local file1=$DIR/$tdir/$tfile-1
10972         local file2=$DIR/$tdir/$tfile-2
10973         local file3=$DIR/$tdir/$tfile-3
10974         local lovea1
10975         local lovea2
10976
10977         mkdir -p $DIR/$tdir
10978         touch $file1 || error "create $file1 failed"
10979         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10980                 error "create $file2 failed"
10981         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10982                 error "create $file3 failed"
10983         lovea1=$($LFS getstripe $file1 | sed 1d)
10984
10985         $LFS swap_layouts $file2 $file3 ||
10986                 error "swap $file2 $file3 layouts failed"
10987         $LFS swap_layouts $file1 $file2 ||
10988                 error "swap $file1 $file2 layouts failed"
10989
10990         lovea2=$($LFS getstripe $file2 | sed 1d)
10991         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10992
10993         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10994         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10995 }
10996 run_test 184d "allow stripeless layouts swap"
10997
10998
10999 test_185() { # LU-2441
11000         # LU-3553 - no volatile file support in old servers
11001         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11002                 { skip "Need MDS version at least 2.3.60"; return 0; }
11003
11004         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11005         touch $DIR/$tdir/spoo
11006         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11007         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11008                 error "cannot create/write a volatile file"
11009         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11010                 error "FID is still valid after close"
11011
11012         multiop_bg_pause $DIR/$tdir vVw4096_c
11013         local multi_pid=$!
11014
11015         local OLD_IFS=$IFS
11016         IFS=":"
11017         local fidv=($fid)
11018         IFS=$OLD_IFS
11019         # assume that the next FID for this client is sequential, since stdout
11020         # is unfortunately eaten by multiop_bg_pause
11021         local n=$((${fidv[1]} + 1))
11022         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11023         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11024                 error "FID is missing before close"
11025         kill -USR1 $multi_pid
11026         # 1 second delay, so if mtime change we will see it
11027         sleep 1
11028         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11029         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11030 }
11031 run_test 185 "Volatile file support"
11032
11033 test_187a() {
11034         local dir0=$DIR/$tdir/$testnum
11035         mkdir -p $dir0 || error "creating dir $dir0"
11036
11037         local file=$dir0/file1
11038         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11039         local dv1=$($LFS data_version $file)
11040         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11041         local dv2=$($LFS data_version $file)
11042         [[ $dv1 != $dv2 ]] ||
11043                 error "data version did not change on write $dv1 == $dv2"
11044
11045         # clean up
11046         rm -f $file1
11047 }
11048 run_test 187a "Test data version change"
11049
11050 test_187b() {
11051         local dir0=$DIR/$tdir/$testnum
11052         mkdir -p $dir0 || error "creating dir $dir0"
11053
11054         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11055         [[ ${DV[0]} != ${DV[1]} ]] ||
11056                 error "data version did not change on write"\
11057                       " ${DV[0]} == ${DV[1]}"
11058
11059         # clean up
11060         rm -f $file1
11061 }
11062 run_test 187b "Test data version change on volatile file"
11063
11064 test_200() {
11065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11066         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11067
11068         local POOL=${POOL:-cea1}
11069         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11070         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11071         # Pool OST targets
11072         local first_ost=0
11073         local last_ost=$(($OSTCOUNT - 1))
11074         local ost_step=2
11075         local ost_list=$(seq $first_ost $ost_step $last_ost)
11076         local ost_range="$first_ost $last_ost $ost_step"
11077         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11078         local file_dir=$POOL_ROOT/file_tst
11079
11080         local rc=0
11081         while : ; do
11082                 # former test_200a test_200b
11083                 pool_add $POOL                          || { rc=$? ; break; }
11084                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11085                 # former test_200c test_200d
11086                 mkdir -p $test_path
11087                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11088                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11089                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11090                                                         || { rc=$? ; break; }
11091                 # former test_200e test_200f
11092                 local files=$((OSTCOUNT*3))
11093                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11094                                                         || { rc=$? ; break; }
11095                 pool_create_files $POOL $file_dir $files "$ost_list" \
11096                                                         || { rc=$? ; break; }
11097                 # former test_200g test_200h
11098                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11099                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11100
11101                 # former test_201a test_201b test_201c
11102                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11103
11104                 local f=$test_path/$tfile
11105                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11106                 pool_remove $POOL $f                    || { rc=$? ; break; }
11107                 break
11108         done
11109
11110         cleanup_pools
11111         return $rc
11112 }
11113 run_test 200 "OST pools"
11114
11115 # usage: default_attr <count | size | offset>
11116 default_attr() {
11117         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11118 }
11119
11120 # usage: check_default_stripe_attr
11121 check_default_stripe_attr() {
11122         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11123         case $1 in
11124         --stripe-count|--count)
11125                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11126         --stripe-size|--size)
11127                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11128         --stripe-index|--index)
11129                 EXPECTED=-1;;
11130         *)
11131                 error "unknown getstripe attr '$1'"
11132         esac
11133
11134         [ $ACTUAL != $EXPECTED ] &&
11135                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11136 }
11137
11138 test_204a() {
11139         test_mkdir -p $DIR/$tdir
11140         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11141
11142         check_default_stripe_attr --stripe-count
11143         check_default_stripe_attr --stripe-size
11144         check_default_stripe_attr --stripe-index
11145
11146         return 0
11147 }
11148 run_test 204a "Print default stripe attributes ================="
11149
11150 test_204b() {
11151         test_mkdir -p $DIR/$tdir
11152         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11153
11154         check_default_stripe_attr --stripe-size
11155         check_default_stripe_attr --stripe-index
11156
11157         return 0
11158 }
11159 run_test 204b "Print default stripe size and offset  ==========="
11160
11161 test_204c() {
11162         test_mkdir -p $DIR/$tdir
11163         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11164
11165         check_default_stripe_attr --stripe-count
11166         check_default_stripe_attr --stripe-index
11167
11168         return 0
11169 }
11170 run_test 204c "Print default stripe count and offset ==========="
11171
11172 test_204d() {
11173         test_mkdir -p $DIR/$tdir
11174         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11175
11176         check_default_stripe_attr --stripe-count
11177         check_default_stripe_attr --stripe-size
11178
11179         return 0
11180 }
11181 run_test 204d "Print default stripe count and size ============="
11182
11183 test_204e() {
11184         test_mkdir -p $DIR/$tdir
11185         $SETSTRIPE -d $DIR/$tdir
11186
11187         check_default_stripe_attr --stripe-count --raw
11188         check_default_stripe_attr --stripe-size --raw
11189         check_default_stripe_attr --stripe-index --raw
11190
11191         return 0
11192 }
11193 run_test 204e "Print raw stripe attributes ================="
11194
11195 test_204f() {
11196         test_mkdir -p $DIR/$tdir
11197         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11198
11199         check_default_stripe_attr --stripe-size --raw
11200         check_default_stripe_attr --stripe-index --raw
11201
11202         return 0
11203 }
11204 run_test 204f "Print raw stripe size and offset  ==========="
11205
11206 test_204g() {
11207         test_mkdir -p $DIR/$tdir
11208         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11209
11210         check_default_stripe_attr --stripe-count --raw
11211         check_default_stripe_attr --stripe-index --raw
11212
11213         return 0
11214 }
11215 run_test 204g "Print raw stripe count and offset ==========="
11216
11217 test_204h() {
11218         test_mkdir -p $DIR/$tdir
11219         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11220
11221         check_default_stripe_attr --stripe-count --raw
11222         check_default_stripe_attr --stripe-size --raw
11223
11224         return 0
11225 }
11226 run_test 204h "Print raw stripe count and size ============="
11227
11228 # Figure out which job scheduler is being used, if any,
11229 # or use a fake one
11230 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11231         JOBENV=SLURM_JOB_ID
11232 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11233         JOBENV=LSB_JOBID
11234 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11235         JOBENV=PBS_JOBID
11236 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11237         JOBENV=LOADL_STEP_ID
11238 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11239         JOBENV=JOB_ID
11240 else
11241         JOBENV=FAKE_JOBID
11242 fi
11243
11244 verify_jobstats() {
11245         local cmd=$1
11246         local target=$2
11247
11248         # clear old jobstats
11249         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11250         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11251
11252         # use a new JobID for this test, or we might see an old one
11253         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11254
11255         JOBVAL=${!JOBENV}
11256         log "Test: $cmd"
11257         log "Using JobID environment variable $JOBENV=$JOBVAL"
11258
11259         if [ $JOBENV = "FAKE_JOBID" ]; then
11260                 FAKE_JOBID=$JOBVAL $cmd
11261         else
11262                 $cmd
11263         fi
11264
11265         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11266                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11267                 do_facet $FACET lctl get_param mdt.*.job_stats |
11268                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11269         fi
11270         if [ "$target" = "ost" -o "$target" = "both" ]; then
11271                 FACET=ost1
11272                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11273                         grep $JOBVAL || error "No job stats found on OST $FACET"
11274         fi
11275 }
11276
11277 jobstats_set() {
11278         trap 0
11279         NEW_JOBENV=${1:-$OLD_JOBENV}
11280         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11281         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11282 }
11283
11284 test_205() { # Job stats
11285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11286         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11287         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11288                 skip "Server doesn't support jobstats" && return 0
11289         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11290
11291         local cmd
11292         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11293         if [ $OLD_JOBENV != $JOBENV ]; then
11294                 jobstats_set $JOBENV
11295                 trap jobstats_set EXIT
11296         fi
11297
11298         local user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11299                      changelog_register -n)
11300         echo "Registered as changelog user $user"
11301
11302         # mkdir
11303         cmd="mkdir $DIR/$tfile"
11304         verify_jobstats "$cmd" "mdt"
11305         # rmdir
11306         cmd="rm -fr $DIR/$tfile"
11307         verify_jobstats "$cmd" "mdt"
11308         # mknod
11309         cmd="mknod $DIR/$tfile c 1 3"
11310         verify_jobstats "$cmd" "mdt"
11311         # unlink
11312         cmd="rm -f $DIR/$tfile"
11313         verify_jobstats "$cmd" "mdt"
11314         # open & close
11315         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11316         verify_jobstats "$cmd" "mdt"
11317         # setattr
11318         cmd="touch $DIR/$tfile"
11319         verify_jobstats "$cmd" "both"
11320         # write
11321         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11322         verify_jobstats "$cmd" "ost"
11323         # read
11324         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11325         verify_jobstats "$cmd" "ost"
11326         # truncate
11327         cmd="$TRUNCATE $DIR/$tfile 0"
11328         verify_jobstats "$cmd" "both"
11329         # rename
11330         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11331         verify_jobstats "$cmd" "mdt"
11332
11333         # Ensure that jobid are present in changelog (if supported by MDS)
11334         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11335         then
11336                 $LFS changelog $MDT0 | tail -9
11337                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11338                 [ $jobids -eq 9 ] ||
11339                         error "Wrong changelog jobid count $jobids != 9"
11340         fi
11341
11342         # cleanup
11343         rm -f $DIR/jobstats_test_rename
11344
11345         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11346 }
11347 run_test 205 "Verify job stats"
11348
11349 # LU-1480, LU-1773 and LU-1657
11350 test_206() {
11351         mkdir -p $DIR/$tdir
11352         $SETSTRIPE -c -1 $DIR/$tdir
11353 #define OBD_FAIL_LOV_INIT 0x1403
11354         $LCTL set_param fail_loc=0xa0001403
11355         $LCTL set_param fail_val=1
11356         touch $DIR/$tdir/$tfile || true
11357 }
11358 run_test 206 "fail lov_init_raid0() doesn't lbug"
11359
11360 test_207a() {
11361         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11362         local fsz=`stat -c %s $DIR/$tfile`
11363         cancel_lru_locks mdc
11364
11365         # do not return layout in getattr intent
11366 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11367         $LCTL set_param fail_loc=0x170
11368         local sz=`stat -c %s $DIR/$tfile`
11369
11370         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11371
11372         rm -rf $DIR/$tfile
11373 }
11374 run_test 207a "can refresh layout at glimpse"
11375
11376 test_207b() {
11377         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11378         local cksum=`md5sum $DIR/$tfile`
11379         local fsz=`stat -c %s $DIR/$tfile`
11380         cancel_lru_locks mdc
11381         cancel_lru_locks osc
11382
11383         # do not return layout in getattr intent
11384 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11385         $LCTL set_param fail_loc=0x171
11386
11387         # it will refresh layout after the file is opened but before read issues
11388         echo checksum is "$cksum"
11389         echo "$cksum" |md5sum -c --quiet || error "file differs"
11390
11391         rm -rf $DIR/$tfile
11392 }
11393 run_test 207b "can refresh layout at open"
11394
11395 test_208() {
11396         # FIXME: in this test suite, only RD lease is used. This is okay
11397         # for now as only exclusive open is supported. After generic lease
11398         # is done, this test suite should be revised. - Jinshan
11399
11400         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11401                 { skip "Need MDS version at least 2.4.52"; return 0; }
11402         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11403
11404         echo "==== test 1: verify get lease work"
11405         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11406
11407         echo "==== test 2: verify lease can be broken by upcoming open"
11408         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11409         local PID=$!
11410         sleep 1
11411
11412         $MULTIOP $DIR/$tfile oO_RDONLY:c
11413         kill -USR1 $PID && wait $PID || error "break lease error"
11414
11415         echo "==== test 3: verify lease can't be granted if an open already exists"
11416         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11417         local PID=$!
11418         sleep 1
11419
11420         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11421         kill -USR1 $PID && wait $PID || error "open file error"
11422
11423         echo "==== test 4: lease can sustain over recovery"
11424         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11425         PID=$!
11426         sleep 1
11427
11428         fail mds1
11429
11430         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11431
11432         echo "==== test 5: lease broken can't be regained by replay"
11433         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11434         PID=$!
11435         sleep 1
11436
11437         # open file to break lease and then recovery
11438         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11439         fail mds1
11440
11441         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11442
11443         rm -f $DIR/$tfile
11444 }
11445 run_test 208 "Exclusive open"
11446
11447 test_209() {
11448         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11449                 skip_env "must have disp_stripe" && return
11450
11451         touch $DIR/$tfile
11452         sync; sleep 5; sync;
11453
11454         echo 3 > /proc/sys/vm/drop_caches
11455         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11456
11457         # open/close 500 times
11458         for i in $(seq 500); do
11459                 cat $DIR/$tfile
11460         done
11461
11462         echo 3 > /proc/sys/vm/drop_caches
11463         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11464
11465         echo "before: $req_before, after: $req_after"
11466         [ $((req_after - req_before)) -ge 300 ] &&
11467                 error "open/close requests are not freed"
11468         return 0
11469 }
11470 run_test 209 "read-only open/close requests should be freed promptly"
11471
11472 test_212() {
11473         size=`date +%s`
11474         size=$((size % 8192 + 1))
11475         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11476         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11477         rm -f $DIR/f212 $DIR/f212.xyz
11478 }
11479 run_test 212 "Sendfile test ============================================"
11480
11481 test_213() {
11482         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11483         cancel_lru_locks osc
11484         lctl set_param fail_loc=0x8000040f
11485         # generate a read lock
11486         cat $DIR/$tfile > /dev/null
11487         # write to the file, it will try to cancel the above read lock.
11488         cat /etc/hosts >> $DIR/$tfile
11489 }
11490 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11491
11492 test_214() { # for bug 20133
11493         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11494         for (( i=0; i < 340; i++ )) ; do
11495                 touch $DIR/$tdir/d214c/a$i
11496         done
11497
11498         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11499         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11500         ls $DIR/d214c || error "ls $DIR/d214c failed"
11501         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11502         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11503 }
11504 run_test 214 "hash-indexed directory test - bug 20133"
11505
11506 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11507 create_lnet_proc_files() {
11508         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11509         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11510
11511         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11512         rm -f "$TMP/lnet_$1.sys_tmp"
11513 }
11514
11515 # counterpart of create_lnet_proc_files
11516 remove_lnet_proc_files() {
11517         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11518 }
11519
11520 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11521 # 3rd arg as regexp for body
11522 check_lnet_proc_stats() {
11523         local l=$(cat "$TMP/lnet_$1" |wc -l)
11524         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11525
11526         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11527 }
11528
11529 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11530 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11531 # optional and can be regexp for 2nd line (lnet.routes case)
11532 check_lnet_proc_entry() {
11533         local blp=2          # blp stands for 'position of 1st line of body'
11534         [ -z "$5" ] || blp=3 # lnet.routes case
11535
11536         local l=$(cat "$TMP/lnet_$1" |wc -l)
11537         # subtracting one from $blp because the body can be empty
11538         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11539
11540         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11541                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11542
11543         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11544                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11545
11546         # bail out if any unexpected line happened
11547         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11548         [ "$?" != 0 ] || error "$2 misformatted"
11549 }
11550
11551 test_215() { # for bugs 18102, 21079, 21517
11552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11553         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11554         local P='[1-9][0-9]*'           # positive numeric
11555         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11556         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11557         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11558         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11559
11560         local L1 # regexp for 1st line
11561         local L2 # regexp for 2nd line (optional)
11562         local BR # regexp for the rest (body)
11563
11564         # lnet.stats should look as 11 space-separated non-negative numerics
11565         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11566         create_lnet_proc_files "stats"
11567         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11568         remove_lnet_proc_files "stats"
11569
11570         # lnet.routes should look like this:
11571         # Routing disabled/enabled
11572         # net hops priority state router
11573         # where net is a string like tcp0, hops > 0, priority >= 0,
11574         # state is up/down,
11575         # router is a string like 192.168.1.1@tcp2
11576         L1="^Routing (disabled|enabled)$"
11577         L2="^net +hops +priority +state +router$"
11578         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11579         create_lnet_proc_files "routes"
11580         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11581         remove_lnet_proc_files "routes"
11582
11583         # lnet.routers should look like this:
11584         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11585         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11586         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11587         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11588         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11589         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11590         create_lnet_proc_files "routers"
11591         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11592         remove_lnet_proc_files "routers"
11593
11594         # lnet.peers should look like this:
11595         # nid refs state last max rtr min tx min queue
11596         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11597         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11598         # numeric (0 or >0 or <0), queue >= 0.
11599         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11600         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11601         create_lnet_proc_files "peers"
11602         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11603         remove_lnet_proc_files "peers"
11604
11605         # lnet.buffers  should look like this:
11606         # pages count credits min
11607         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11608         L1="^pages +count +credits +min$"
11609         BR="^ +$N +$N +$I +$I$"
11610         create_lnet_proc_files "buffers"
11611         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11612         remove_lnet_proc_files "buffers"
11613
11614         # lnet.nis should look like this:
11615         # nid status alive refs peer rtr max tx min
11616         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11617         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11618         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11619         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11620         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11621         create_lnet_proc_files "nis"
11622         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11623         remove_lnet_proc_files "nis"
11624
11625         # can we successfully write to lnet.stats?
11626         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
11627         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11628 }
11629 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
11630
11631 test_216() { # bug 20317
11632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11633         remote_ost_nodsh && skip "remote OST with nodsh" && return
11634
11635         local node
11636         local facets=$(get_facets OST)
11637         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11638
11639         save_lustre_params client "osc.*.contention_seconds" > $p
11640         save_lustre_params $facets \
11641                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11642         save_lustre_params $facets \
11643                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11644         save_lustre_params $facets \
11645                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11646         clear_osc_stats
11647
11648         # agressive lockless i/o settings
11649         for node in $(osts_nodes); do
11650                 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'
11651         done
11652         lctl set_param -n osc.*.contention_seconds 60
11653
11654         $DIRECTIO write $DIR/$tfile 0 10 4096
11655         $CHECKSTAT -s 40960 $DIR/$tfile
11656
11657         # disable lockless i/o
11658         for node in $(osts_nodes); do
11659                 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'
11660         done
11661         lctl set_param -n osc.*.contention_seconds 0
11662         clear_osc_stats
11663
11664         dd if=/dev/zero of=$DIR/$tfile count=0
11665         $CHECKSTAT -s 0 $DIR/$tfile
11666
11667         restore_lustre_params <$p
11668         rm -f $p
11669         rm $DIR/$tfile
11670 }
11671 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11672
11673 test_217() { # bug 22430
11674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11675         local node
11676         local nid
11677
11678         for node in $(nodes_list); do
11679                 nid=$(host_nids_address $node $NETTYPE)
11680                 if [[ $nid = *-* ]] ; then
11681                         echo "lctl ping $nid@$NETTYPE"
11682                         lctl ping $nid@$NETTYPE
11683                 else
11684                         echo "skipping $node (no hyphen detected)"
11685                 fi
11686         done
11687 }
11688 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11689
11690 test_218() {
11691        # do directio so as not to populate the page cache
11692        log "creating a 10 Mb file"
11693        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11694        log "starting reads"
11695        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11696        log "truncating the file"
11697        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11698        log "killing dd"
11699        kill %+ || true # reads might have finished
11700        echo "wait until dd is finished"
11701        wait
11702        log "removing the temporary file"
11703        rm -rf $DIR/$tfile || error "tmp file removal failed"
11704 }
11705 run_test 218 "parallel read and truncate should not deadlock ======================="
11706
11707 test_219() {
11708         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11709         # write one partial page
11710         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11711         # set no grant so vvp_io_commit_write will do sync write
11712         $LCTL set_param fail_loc=0x411
11713         # write a full page at the end of file
11714         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11715
11716         $LCTL set_param fail_loc=0
11717         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11718         $LCTL set_param fail_loc=0x411
11719         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11720
11721         # LU-4201
11722         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11723         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11724 }
11725 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11726
11727 test_220() { #LU-325
11728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11729         remote_ost_nodsh && skip "remote OST with nodsh" && return
11730         local OSTIDX=0
11731
11732         test_mkdir -p $DIR/$tdir
11733         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11734                 awk '{print $2}' | sed -e 's/_UUID$//')
11735
11736         # on the mdt's osc
11737         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11738         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11739                         osc.$mdtosc_proc1.prealloc_last_id)
11740         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11741                         osc.$mdtosc_proc1.prealloc_next_id)
11742
11743         $LFS df -i
11744
11745         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11746         #define OBD_FAIL_OST_ENOINO              0x229
11747         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11748         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11749         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11750
11751         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11752
11753         MDSOBJS=$((last_id - next_id))
11754         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11755
11756         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11757         echo "OST still has $count kbytes free"
11758
11759         echo "create $MDSOBJS files @next_id..."
11760         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11761
11762         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11763                         osc.$mdtosc_proc1.prealloc_last_id)
11764         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11765                         osc.$mdtosc_proc1.prealloc_next_id)
11766
11767         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11768         $LFS df -i
11769
11770         echo "cleanup..."
11771
11772         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11773         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11774
11775         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11776         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11777         echo "unlink $MDSOBJS files @$next_id..."
11778         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11779 }
11780 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11781
11782 test_221() {
11783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11784         dd if=`which date` of=$MOUNT/date oflag=sync
11785         chmod +x $MOUNT/date
11786
11787         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11788         $LCTL set_param fail_loc=0x80001401
11789
11790         $MOUNT/date > /dev/null
11791         rm -f $MOUNT/date
11792 }
11793 run_test 221 "make sure fault and truncate race to not cause OOM"
11794
11795 test_222a () {
11796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11797        rm -rf $DIR/$tdir
11798        test_mkdir -p $DIR/$tdir
11799        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11800        createmany -o $DIR/$tdir/$tfile 10
11801        cancel_lru_locks mdc
11802        cancel_lru_locks osc
11803        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11804        $LCTL set_param fail_loc=0x31a
11805        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11806        $LCTL set_param fail_loc=0
11807        rm -r $DIR/$tdir
11808 }
11809 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11810
11811 test_222b () {
11812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11813        rm -rf $DIR/$tdir
11814        test_mkdir -p $DIR/$tdir
11815        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11816        createmany -o $DIR/$tdir/$tfile 10
11817        cancel_lru_locks mdc
11818        cancel_lru_locks osc
11819        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11820        $LCTL set_param fail_loc=0x31a
11821        rm -r $DIR/$tdir || "AGL for rmdir failed"
11822        $LCTL set_param fail_loc=0
11823 }
11824 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11825
11826 test_223 () {
11827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11828        rm -rf $DIR/$tdir
11829        test_mkdir -p $DIR/$tdir
11830        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11831        createmany -o $DIR/$tdir/$tfile 10
11832        cancel_lru_locks mdc
11833        cancel_lru_locks osc
11834        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11835        $LCTL set_param fail_loc=0x31b
11836        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11837        $LCTL set_param fail_loc=0
11838        rm -r $DIR/$tdir
11839 }
11840 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11841
11842 test_224a() { # LU-1039, MRP-303
11843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11844         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11845         $LCTL set_param fail_loc=0x508
11846         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11847         $LCTL set_param fail_loc=0
11848         df $DIR
11849 }
11850 run_test 224a "Don't panic on bulk IO failure"
11851
11852 test_224b() { # LU-1039, MRP-303
11853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11854         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11855         cancel_lru_locks osc
11856         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11857         $LCTL set_param fail_loc=0x515
11858         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11859         $LCTL set_param fail_loc=0
11860         df $DIR
11861 }
11862 run_test 224b "Don't panic on bulk IO failure"
11863
11864 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11865 test_225a () {
11866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11867         if [ -z ${MDSSURVEY} ]; then
11868               skip_env "mds-survey not found" && return
11869         fi
11870
11871         [ $MDSCOUNT -ge 2 ] &&
11872                 skip "skipping now for more than one MDT" && return
11873
11874        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11875             { skip "Need MDS version at least 2.2.51"; return; }
11876
11877        local mds=$(facet_host $SINGLEMDS)
11878        local target=$(do_nodes $mds 'lctl dl' | \
11879                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11880
11881        local cmd1="file_count=1000 thrhi=4"
11882        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11883        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11884        local cmd="$cmd1 $cmd2 $cmd3"
11885
11886        rm -f ${TMP}/mds_survey*
11887        echo + $cmd
11888        eval $cmd || error "mds-survey with zero-stripe failed"
11889        cat ${TMP}/mds_survey*
11890        rm -f ${TMP}/mds_survey*
11891 }
11892 run_test 225a "Metadata survey sanity with zero-stripe"
11893
11894 test_225b () {
11895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11896
11897         if [ -z ${MDSSURVEY} ]; then
11898               skip_env "mds-survey not found" && return
11899         fi
11900         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11901             { skip "Need MDS version at least 2.2.51"; return; }
11902
11903         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11904               skip_env "Need to mount OST to test" && return
11905         fi
11906
11907         [ $MDSCOUNT -ge 2 ] &&
11908                 skip "skipping now for more than one MDT" && return
11909        local mds=$(facet_host $SINGLEMDS)
11910        local target=$(do_nodes $mds 'lctl dl' | \
11911                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11912
11913        local cmd1="file_count=1000 thrhi=4"
11914        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11915        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11916        local cmd="$cmd1 $cmd2 $cmd3"
11917
11918        rm -f ${TMP}/mds_survey*
11919        echo + $cmd
11920        eval $cmd || error "mds-survey with stripe_count failed"
11921        cat ${TMP}/mds_survey*
11922        rm -f ${TMP}/mds_survey*
11923 }
11924 run_test 225b "Metadata survey sanity with stripe_count = 1"
11925
11926 mcreate_path2fid () {
11927         local mode=$1
11928         local major=$2
11929         local minor=$3
11930         local name=$4
11931         local desc=$5
11932         local path=$DIR/$tdir/$name
11933         local fid
11934         local rc
11935         local fid_path
11936
11937         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11938                 error "cannot create $desc"
11939
11940         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11941         rc=$?
11942         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11943
11944         fid_path=$($LFS fid2path $MOUNT $fid)
11945         rc=$?
11946         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11947
11948         [ "$path" == "$fid_path" ] ||
11949                 error "fid2path returned $fid_path, expected $path"
11950
11951         echo "pass with $path and $fid"
11952 }
11953
11954 test_226a () {
11955         rm -rf $DIR/$tdir
11956         mkdir -p $DIR/$tdir
11957
11958         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11959         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11960         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11961         mcreate_path2fid 0040666 0 0 dir "directory"
11962         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11963         mcreate_path2fid 0100666 0 0 file "regular file"
11964         mcreate_path2fid 0120666 0 0 link "symbolic link"
11965         mcreate_path2fid 0140666 0 0 sock "socket"
11966 }
11967 run_test 226a "call path2fid and fid2path on files of all type"
11968
11969 test_226b () {
11970         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11971         rm -rf $DIR/$tdir
11972         local MDTIDX=1
11973
11974         mkdir -p $DIR/$tdir
11975         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11976                 error "create remote directory failed"
11977         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11978         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11979                                 "character special file (null)"
11980         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11981                                 "character special file (no device)"
11982         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11983         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11984                                 "block special file (loop)"
11985         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11986         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11987         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11988 }
11989 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11990
11991 # LU-1299 Executing or running ldd on a truncated executable does not
11992 # cause an out-of-memory condition.
11993 test_227() {
11994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11995         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
11996         dd if=$(which date) of=$MOUNT/date bs=1k count=1
11997         chmod +x $MOUNT/date
11998
11999         $MOUNT/date > /dev/null
12000         ldd $MOUNT/date > /dev/null
12001         rm -f $MOUNT/date
12002 }
12003 run_test 227 "running truncated executable does not cause OOM"
12004
12005 # LU-1512 try to reuse idle OI blocks
12006 test_228a() {
12007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12008         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12009         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12010                 skip "non-ldiskfs backend" && return
12011
12012         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12013         local myDIR=$DIR/$tdir
12014
12015         mkdir -p $myDIR
12016         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12017         $LCTL set_param fail_loc=0x80001002
12018         createmany -o $myDIR/t- 10000
12019         $LCTL set_param fail_loc=0
12020         # The guard is current the largest FID holder
12021         touch $myDIR/guard
12022         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12023                     tr -d '[')
12024         local IDX=$(($SEQ % 64))
12025
12026         do_facet $SINGLEMDS sync
12027         # Make sure journal flushed.
12028         sleep 6
12029         local blk1=$(do_facet $SINGLEMDS \
12030                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12031                      grep Blockcount | awk '{print $4}')
12032
12033         # Remove old files, some OI blocks will become idle.
12034         unlinkmany $myDIR/t- 10000
12035         # Create new files, idle OI blocks should be reused.
12036         createmany -o $myDIR/t- 2000
12037         do_facet $SINGLEMDS sync
12038         # Make sure journal flushed.
12039         sleep 6
12040         local blk2=$(do_facet $SINGLEMDS \
12041                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12042                      grep Blockcount | awk '{print $4}')
12043
12044         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12045 }
12046 run_test 228a "try to reuse idle OI blocks"
12047
12048 test_228b() {
12049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12050         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12051         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12052                 skip "non-ldiskfs backend" && return
12053
12054         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12055         local myDIR=$DIR/$tdir
12056
12057         mkdir -p $myDIR
12058         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12059         $LCTL set_param fail_loc=0x80001002
12060         createmany -o $myDIR/t- 10000
12061         $LCTL set_param fail_loc=0
12062         # The guard is current the largest FID holder
12063         touch $myDIR/guard
12064         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12065                     tr -d '[')
12066         local IDX=$(($SEQ % 64))
12067
12068         do_facet $SINGLEMDS sync
12069         # Make sure journal flushed.
12070         sleep 6
12071         local blk1=$(do_facet $SINGLEMDS \
12072                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12073                      grep Blockcount | awk '{print $4}')
12074
12075         # Remove old files, some OI blocks will become idle.
12076         unlinkmany $myDIR/t- 10000
12077
12078         # stop the MDT
12079         stop $SINGLEMDS || error "Fail to stop MDT."
12080         # remount the MDT
12081         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12082
12083         df $MOUNT || error "Fail to df."
12084         # Create new files, idle OI blocks should be reused.
12085         createmany -o $myDIR/t- 2000
12086         do_facet $SINGLEMDS sync
12087         # Make sure journal flushed.
12088         sleep 6
12089         local blk2=$(do_facet $SINGLEMDS \
12090                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12091                      grep Blockcount | awk '{print $4}')
12092
12093         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12094 }
12095 run_test 228b "idle OI blocks can be reused after MDT restart"
12096
12097 #LU-1881
12098 test_228c() {
12099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12100         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12101         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12102                 skip "non-ldiskfs backend" && return
12103
12104         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12105         local myDIR=$DIR/$tdir
12106
12107         mkdir -p $myDIR
12108         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12109         $LCTL set_param fail_loc=0x80001002
12110         # 20000 files can guarantee there are index nodes in the OI file
12111         createmany -o $myDIR/t- 20000
12112         $LCTL set_param fail_loc=0
12113         # The guard is current the largest FID holder
12114         touch $myDIR/guard
12115         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12116                     tr -d '[')
12117         local IDX=$(($SEQ % 64))
12118
12119         do_facet $SINGLEMDS sync
12120         # Make sure journal flushed.
12121         sleep 6
12122         local blk1=$(do_facet $SINGLEMDS \
12123                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12124                      grep Blockcount | awk '{print $4}')
12125
12126         # Remove old files, some OI blocks will become idle.
12127         unlinkmany $myDIR/t- 20000
12128         rm -f $myDIR/guard
12129         # The OI file should become empty now
12130
12131         # Create new files, idle OI blocks should be reused.
12132         createmany -o $myDIR/t- 2000
12133         do_facet $SINGLEMDS sync
12134         # Make sure journal flushed.
12135         sleep 6
12136         local blk2=$(do_facet $SINGLEMDS \
12137                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12138                      grep Blockcount | awk '{print $4}')
12139
12140         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12141 }
12142 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12143
12144 test_229() { # LU-2482, LU-3448
12145         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12146                 skip "No HSM support on MDS of $(get_lustre_version)," \
12147                          "need 2.4.53 at least" && return
12148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12149         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12150
12151         rm -f $DIR/$tfile
12152
12153         # Create a file with a released layout and stripe count 2.
12154         $MULTIOP $DIR/$tfile H2c ||
12155                 error "failed to create file with released layout"
12156
12157         $GETSTRIPE -v $DIR/$tfile
12158
12159         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12160         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12161
12162         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12163         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12164         stat $DIR/$tfile || error "failed to stat released file"
12165
12166         chown $RUNAS_ID $DIR/$tfile ||
12167                 error "chown $RUNAS_ID $DIR/$tfile failed"
12168
12169         chgrp $RUNAS_ID $DIR/$tfile ||
12170                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12171
12172         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12173         rm $DIR/$tfile || error "failed to remove released file"
12174 }
12175 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12176
12177 test_230a() {
12178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12179         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12180         local MDTIDX=1
12181
12182         mkdir -p $DIR/$tdir/test_230_local
12183         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12184         [ $mdt_idx -ne 0 ] &&
12185                 error "create local directory on wrong MDT $mdt_idx"
12186
12187         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12188                         error "create remote directory failed"
12189         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12190         [ $mdt_idx -ne $MDTIDX ] &&
12191                 error "create remote directory on wrong MDT $mdt_idx"
12192
12193         createmany -o $DIR/$tdir/test_230/t- 10 ||
12194                 error "create files on remote directory failed"
12195         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12196         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12197         rm -r $DIR/$tdir || error "unlink remote directory failed"
12198 }
12199 run_test 230a "Create remote directory and files under the remote directory"
12200
12201 test_230b() {
12202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12203         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12204         local MDTIDX=1
12205         local mdt_index
12206         local i
12207         local file
12208         local pid
12209         local stripe_count
12210         local migrate_dir=$DIR/$tdir/migrate_dir
12211         local other_dir=$DIR/$tdir/other_dir
12212
12213         mkdir -p $migrate_dir
12214         mkdir -p $other_dir
12215         for ((i=0; i<10; i++)); do
12216                 mkdir -p $migrate_dir/dir_${i}
12217                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12218                         error "create files under remote dir failed $i"
12219         done
12220
12221         cp /etc/passwd $migrate_dir/$tfile
12222         cp /etc/passwd $other_dir/$tfile
12223         chattr +SAD $migrate_dir
12224         chattr +SAD $migrate_dir/$tfile
12225
12226         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12227         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12228         local old_dir_mode=$(stat -c%f $migrate_dir)
12229         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12230
12231         mkdir -p $migrate_dir/dir_default_stripe2
12232         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12233         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12234
12235         mkdir -p $other_dir
12236         ln $migrate_dir/$tfile $other_dir/luna
12237         ln $migrate_dir/$tfile $migrate_dir/sofia
12238         ln $other_dir/$tfile $migrate_dir/david
12239         ln -s $migrate_dir/$tfile $other_dir/zachary
12240         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12241         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12242
12243         $LFS mv -v -M $MDTIDX $migrate_dir ||
12244                         error "migrate remote dir error"
12245
12246         echo "migratate to MDT1, then checking.."
12247         for ((i=0; i<10; i++)); do
12248                 for file in $(find $migrate_dir/dir_${i}); do
12249                         mdt_index=$($LFS getstripe -M $file)
12250                         [ $mdt_index == $MDTIDX ] ||
12251                                 error "$file is not on MDT${MDTIDX}"
12252                 done
12253         done
12254
12255         # the multiple link file should still in MDT0
12256         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12257         [ $mdt_index == 0 ] ||
12258                 error "$file is not on MDT${MDTIDX}"
12259
12260         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12261         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12262                 error " expect $old_dir_flag get $new_dir_flag"
12263
12264         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12265         [ "$old_file_flag" = "$new_file_flag" ] ||
12266                 error " expect $old_file_flag get $new_file_flag"
12267
12268         local new_dir_mode=$(stat -c%f $migrate_dir)
12269         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12270                 error "expect mode $old_dir_mode get $new_dir_mode"
12271
12272         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12273         [ "$old_file_mode" = "$new_file_mode" ] ||
12274                 error "expect mode $old_file_mode get $new_file_mode"
12275
12276         diff /etc/passwd $migrate_dir/$tfile ||
12277                 error "$tfile different after migration"
12278
12279         diff /etc/passwd $other_dir/luna ||
12280                 error "luna different after migration"
12281
12282         diff /etc/passwd $migrate_dir/sofia ||
12283                 error "sofia different after migration"
12284
12285         diff /etc/passwd $migrate_dir/david ||
12286                 error "david different after migration"
12287
12288         diff /etc/passwd $other_dir/zachary ||
12289                 error "zachary different after migration"
12290
12291         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12292                 error "${tfile}_ln different after migration"
12293
12294         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12295                 error "${tfile}_ln_other different after migration"
12296
12297         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12298         [ $stripe_count = 2 ] ||
12299                         error "dir strpe_count $d != 2 after migration."
12300
12301         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12302         [ $stripe_count = 2 ] ||
12303                         error "file strpe_count $d != 2 after migration."
12304
12305         #migrate back to MDT0
12306         MDTIDX=0
12307         $LFS mv -v -M $MDTIDX $migrate_dir ||
12308                         error "migrate remote dir error"
12309
12310         echo "migrate back to MDT0, checking.."
12311         for file in $(find $migrate_dir); do
12312                 mdt_index=$($LFS getstripe -M $file)
12313                 [ $mdt_index == $MDTIDX ] ||
12314                         error "$file is not on MDT${MDTIDX}"
12315         done
12316
12317         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12318         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12319                 error " expect $old_dir_flag get $new_dir_flag"
12320
12321         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12322         [ "$old_file_flag" = "$new_file_flag" ] ||
12323                 error " expect $old_file_flag get $new_file_flag"
12324
12325         local new_dir_mode=$(stat -c%f $migrate_dir)
12326         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12327                 error "expect mode $old_dir_mode get $new_dir_mode"
12328
12329         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12330         [ "$old_file_mode" = "$new_file_mode" ] ||
12331                 error "expect mode $old_file_mode get $new_file_mode"
12332
12333         diff /etc/passwd ${migrate_dir}/$tfile ||
12334                 error "$tfile different after migration"
12335
12336         diff /etc/passwd ${other_dir}/luna ||
12337                 error "luna different after migration"
12338
12339         diff /etc/passwd ${migrate_dir}/sofia ||
12340                 error "sofia different after migration"
12341
12342         diff /etc/passwd ${other_dir}/zachary ||
12343                 error "zachary different after migration"
12344
12345         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12346                 error "${tfile}_ln different after migration"
12347
12348         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12349                 error "${tfile}_ln_other different after migration"
12350
12351         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12352         [ $stripe_count = 2 ] ||
12353                 error "dir strpe_count $d != 2 after migration."
12354
12355         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12356         [ $stripe_count = 2 ] ||
12357                 error "file strpe_count $d != 2 after migration."
12358
12359         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12360 }
12361 run_test 230b "migrate directory"
12362
12363 test_230c() {
12364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12365         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12366         local MDTIDX=1
12367         local mdt_index
12368         local file
12369
12370         #If migrating directory fails in the middle, all entries of
12371         #the directory is still accessiable.
12372         mkdir -p $DIR/$tdir
12373         stat $DIR/$tdir
12374         createmany -o $DIR/$tdir/f 10 ||
12375                 error "create files under ${tdir} failed"
12376
12377         #failed after migrating 5 entries
12378         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12379         do_facet mds1 lctl set_param fail_loc=0x20001801
12380         do_facet mds1 lctl  set_param fail_val=5
12381         local t=`ls $DIR/$tdir | wc -l`
12382         $LFS mv -M $MDTIDX $DIR/$tdir &&
12383                 error "migrate should failed after 5 entries"
12384         local u=`ls $DIR/$tdir | wc -l`
12385         [ "$u" == "$t" ] || error "$u != $t during migration"
12386
12387         for file in $(find $DIR/$tdir); do
12388                 stat $file || error "stat $file failed"
12389         done
12390
12391         do_facet mds1 lctl set_param fail_loc=0
12392         do_facet mds1 lctl set_param fail_val=0
12393
12394         $LFS mv -M $MDTIDX $DIR/$tdir ||
12395                 error "migrate open files should failed with open files"
12396
12397         echo "Finish migration, then checking.."
12398         for file in $(find $DIR/$tdir); do
12399                 mdt_index=$($LFS getstripe -M $file)
12400                 [ $mdt_index == $MDTIDX ] ||
12401                         error "$file is not on MDT${MDTIDX}"
12402         done
12403
12404         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12405 }
12406 run_test 230c "check directory accessiblity if migration is failed"
12407
12408 test_230d() {
12409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12410         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12411         local MDTIDX=1
12412         local mdt_index
12413         local i
12414         local j
12415
12416         mkdir -p $DIR/$tdir
12417
12418         for ((i=0; i<100; i++)); do
12419                 mkdir -p $DIR/$tdir/dir_${i}
12420                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12421                         error "create files under remote dir failed $i"
12422         done
12423
12424         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12425
12426         echo "Finish migration, then checking.."
12427         for file in $(find $DIR/$tdir); do
12428                 mdt_index=$($LFS getstripe -M $file)
12429                 [ $mdt_index == $MDTIDX ] ||
12430                         error "$file is not on MDT${MDTIDX}"
12431         done
12432
12433         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12434 }
12435 run_test 230d "check migrate big directory"
12436
12437 test_231a()
12438 {
12439         # For simplicity this test assumes that max_pages_per_rpc
12440         # is the same across all OSCs
12441         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12442         local bulk_size=$((max_pages * 4096))
12443
12444         mkdir -p $DIR/$tdir
12445
12446         # clear the OSC stats
12447         $LCTL set_param osc.*.stats=0 &>/dev/null
12448
12449         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12450         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12451                 oflag=direct &>/dev/null || error "dd failed"
12452
12453         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12454         if [ x$nrpcs != "x1" ]; then
12455                 error "found $nrpc ost_write RPCs, not 1 as expected"
12456         fi
12457
12458         # Drop the OSC cache, otherwise we will read from it
12459         cancel_lru_locks osc
12460
12461         # clear the OSC stats
12462         $LCTL set_param osc.*.stats=0 &>/dev/null
12463
12464         # Client reads $bulk_size.
12465         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12466                 iflag=direct &>/dev/null || error "dd failed"
12467
12468         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12469         if [ x$nrpcs != "x1" ]; then
12470                 error "found $nrpc ost_read RPCs, not 1 as expected"
12471         fi
12472 }
12473 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12474
12475 test_231b() {
12476         mkdir -p $DIR/$tdir
12477         local i
12478         for i in {0..1023}; do
12479                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12480                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12481                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12482         done
12483         sync
12484 }
12485 run_test 231b "must not assert on fully utilized OST request buffer"
12486
12487 test_232() {
12488         mkdir -p $DIR/$tdir
12489         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12490         $LCTL set_param fail_loc=0x31c
12491
12492         # ignore dd failure
12493         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12494
12495         $LCTL set_param fail_loc=0
12496         umount_client $MOUNT || error "umount failed"
12497         mount_client $MOUNT || error "mount failed"
12498 }
12499 run_test 232 "failed lock should not block umount"
12500
12501 test_233a() {
12502         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12503         { skip "Need MDS version at least 2.3.64"; return; }
12504
12505         local fid=$($LFS path2fid $MOUNT)
12506         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12507                 error "cannot access $MOUNT using its FID '$fid'"
12508 }
12509 run_test 233a "checking that OBF of the FS root succeeds"
12510
12511 test_233b() {
12512         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12513         { skip "Need MDS version at least 2.5.90"; return; }
12514
12515         local fid=$($LFS path2fid $MOUNT/.lustre)
12516         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12517                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12518
12519         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12520         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12521                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12522 }
12523 run_test 233b "checking that OBF of the FS .lustre succeeds"
12524
12525 test_234() {
12526         local p="$TMP/sanityN-$TESTNAME.parameters"
12527         save_lustre_params client "llite.*.xattr_cache" > $p
12528         lctl set_param llite.*.xattr_cache 1 ||
12529                 { skip "xattr cache is not supported"; return 0; }
12530
12531         mkdir -p $DIR/$tdir || error "mkdir failed"
12532         touch $DIR/$tdir/$tfile || error "touch failed"
12533         # OBD_FAIL_LLITE_XATTR_ENOMEM
12534         $LCTL set_param fail_loc=0x1405
12535         # output of the form: attr 2 4 44 3 fc13 x86_64
12536         V=($(IFS=".-" rpm -q attr))
12537         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
12538               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
12539                 # attr pre-2.4.44-7 had a bug with rc
12540                 # LU-3703 - SLES 11 and FC13 clients have older attr
12541                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12542                         error "getfattr should have failed with ENOMEM"
12543         else
12544                 skip "LU-3703: attr version $(getfattr --version) too old"
12545         fi
12546         $LCTL set_param fail_loc=0x0
12547         rm -rf $DIR/$tdir
12548
12549         restore_lustre_params < $p
12550         rm -f $p
12551 }
12552 run_test 234 "xattr cache should not crash on ENOMEM"
12553
12554 test_235() {
12555         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12556                 skip "Need MDS version at least 2.4.52" && return
12557         flock_deadlock $DIR/$tfile
12558         local RC=$?
12559         case $RC in
12560                 0)
12561                 ;;
12562                 124) error "process hangs on a deadlock"
12563                 ;;
12564                 *) error "error executing flock_deadlock $DIR/$tfile"
12565                 ;;
12566         esac
12567 }
12568 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12569
12570 #LU-2935
12571 test_236() {
12572         check_swap_layouts_support && return 0
12573         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12574
12575         local ref1=/etc/passwd
12576         local ref2=/etc/group
12577         local file1=$DIR/$tdir/f1
12578         local file2=$DIR/$tdir/f2
12579
12580         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12581         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12582         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12583         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12584         local fd=$(free_fd)
12585         local cmd="exec $fd<>$file2"
12586         eval $cmd
12587         rm $file2
12588         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12589                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12590         cmd="exec $fd>&-"
12591         eval $cmd
12592         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12593
12594         #cleanup
12595         rm -rf $DIR/$tdir
12596 }
12597 run_test 236 "Layout swap on open unlinked file"
12598
12599 # test to verify file handle related system calls
12600 # (name_to_handle_at/open_by_handle_at)
12601 # The new system calls are supported in glibc >= 2.14.
12602
12603 test_237() {
12604         echo "Test file_handle syscalls" > $DIR/$tfile ||
12605                 error "write failed"
12606         check_fhandle_syscalls $DIR/$tfile ||
12607                 error "check_fhandle_syscalls failed"
12608 }
12609 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12610
12611 # LU-4659 linkea consistency
12612 test_238() {
12613         local server_version=$(lustre_version_code $SINGLEMDS)
12614
12615         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12616                 [[ $server_version -gt $(version_code 2.5.1) &&
12617                    $server_version -lt $(version_code 2.5.50) ]] ||
12618                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12619
12620         touch $DIR/$tfile
12621         ln $DIR/$tfile $DIR/$tfile.lnk
12622         touch $DIR/$tfile.new
12623         mv $DIR/$tfile.new $DIR/$tfile
12624         local fid1=$(lfs path2fid $DIR/$tfile)
12625         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12626         local path1=$(lfs fid2path $FSNAME $fid1)
12627         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12628         local path2=$(lfs fid2path $FSNAME $fid2)
12629         [ $tfile.lnk == $path2 ] ||
12630                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12631         rm -f $DIR/$tfile*
12632 }
12633 run_test 238 "Verify linkea consistency"
12634
12635 test_239() {
12636         local list=$(comma_list $(mdts_nodes))
12637
12638         mkdir -p $DIR/$tdir
12639         createmany -o $DIR/$tdir/f- 5000
12640         unlinkmany $DIR/$tdir/f- 5000
12641         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12642         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12643                         osc.*MDT*.sync_in_flight" | calc_sum)
12644         [ "$changes" -eq 0 ] || error "$changes not synced"
12645 }
12646 run_test 239 "osp_sync test"
12647
12648 test_240() {
12649         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12650
12651         mkdir -p $DIR/$tdir
12652
12653         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12654                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12655         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12656                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12657
12658         umount_client $MOUNT || error "umount failed"
12659         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12660         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12661         mount_client $MOUNT || error "failed to mount client"
12662
12663         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12664         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12665 }
12666 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12667
12668 test_241_bio() {
12669         for LOOP in $(seq $1); do
12670                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
12671                 cancel_lru_locks osc
12672         done
12673 }
12674
12675 test_241_dio() {
12676         for LOOP in $(seq $1); do
12677                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
12678                                                 iflag=direct 2>/dev/null
12679         done
12680 }
12681
12682 test_241() {
12683         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
12684         ls -la $DIR/$tfile
12685         cancel_lru_locks osc
12686         test_241_bio 1000 &
12687         PID=$!
12688         test_241_dio 1000
12689         wait $PID
12690 }
12691 run_test 241 "bio vs dio"
12692
12693 cleanup_test_300() {
12694         trap 0
12695         umask $SAVE_UMASK
12696 }
12697 test_striped_dir() {
12698         local mdt_index=$1
12699         local stripe_count
12700         local stripe_index
12701
12702         mkdir -p $DIR/$tdir
12703
12704         SAVE_UMASK=$(umask)
12705         trap cleanup_test_300 RETURN EXIT
12706
12707         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12708                                                 $DIR/$tdir/striped_dir ||
12709                 error "set striped dir error"
12710
12711         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12712         [ "$mode" = "755" ] || error "expect 755 got $mode"
12713
12714         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12715         if [ "$stripe_count" != "2" ]; then
12716                 error "stripe_count is $stripe_count, expect 2"
12717         fi
12718
12719         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12720         if [ "$stripe_index" != "$mdt_index" ]; then
12721                 error "stripe_index is $stripe_index, expect $mdt_index"
12722         fi
12723
12724         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12725                 error "nlink error after create striped dir"
12726
12727         mkdir $DIR/$tdir/striped_dir/a
12728         mkdir $DIR/$tdir/striped_dir/b
12729
12730         stat $DIR/$tdir/striped_dir/a ||
12731                 error "create dir under striped dir failed"
12732         stat $DIR/$tdir/striped_dir/b ||
12733                 error "create dir under striped dir failed"
12734
12735         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12736                 error "nlink error after mkdir"
12737
12738         rmdir $DIR/$tdir/striped_dir/a
12739         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12740                 error "nlink error after rmdir"
12741
12742         rmdir $DIR/$tdir/striped_dir/b
12743         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12744                 error "nlink error after rmdir"
12745
12746         rmdir $DIR/$tdir/striped_dir ||
12747                 error "rmdir striped dir error"
12748
12749         cleanup_test_300
12750
12751         true
12752 }
12753
12754 test_300a() {
12755         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12756         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12757
12758         test_striped_dir 0 || error "failed on striped dir on MDT0"
12759         test_striped_dir 1 || error "failed on striped dir on MDT0"
12760 }
12761 run_test 300a "basic striped dir sanity test"
12762
12763 test_300b() {
12764         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12765         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12766         local i
12767         local mtime1
12768         local mtime2
12769         local mtime3
12770
12771         test_mkdir $DIR/$tdir || error "mkdir fail"
12772         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12773                 error "set striped dir error"
12774         for ((i=0; i<10; i++)); do
12775                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12776                 sleep 1
12777                 touch $DIR/$tdir/striped_dir/file_$i ||
12778                                         error "touch error $i"
12779                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12780                 [ $mtime1 -eq $mtime2 ] &&
12781                         error "mtime not change after create"
12782                 sleep 1
12783                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12784                                         error "unlink error $i"
12785                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12786                 [ $mtime2 -eq $mtime3 ] &&
12787                         error "mtime did not change after unlink"
12788         done
12789         true
12790 }
12791 run_test 300b "check ctime/mtime for striped dir"
12792
12793 test_300c() {
12794         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12795         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12796         local file_count
12797
12798         mkdir -p $DIR/$tdir
12799         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12800                 error "set striped dir error"
12801
12802         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12803                 error "chown striped dir failed"
12804
12805         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12806                 error "create 5k files failed"
12807
12808         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12809
12810         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12811
12812         rm -rf $DIR/$tdir
12813 }
12814 run_test 300c "chown && check ls under striped directory"
12815
12816 test_300d() {
12817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12818         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12819         local stripe_count
12820         local file
12821
12822         mkdir -p $DIR/$tdir
12823         $SETSTRIPE -c 2 $DIR/$tdir
12824
12825         #local striped directory
12826         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12827                 error "set striped dir error"
12828         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12829                 error "create 10 files failed"
12830
12831         #remote striped directory
12832         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12833                 error "set striped dir error"
12834         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12835                 error "create 10 files failed"
12836
12837         for file in $(find $DIR/$tdir); do
12838                 stripe_count=$($GETSTRIPE -c $file)
12839                 [ $stripe_count -eq 2 ] ||
12840                         error "wrong stripe $stripe_count for $file"
12841         done
12842
12843         rm -rf $DIR/$tdir
12844 }
12845 run_test 300d "check default stripe under striped directory"
12846
12847 test_300e() {
12848         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12849         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12850         local stripe_count
12851         local file
12852
12853         mkdir -p $DIR/$tdir
12854
12855         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12856                 error "set striped dir error"
12857
12858         touch $DIR/$tdir/striped_dir/a
12859         touch $DIR/$tdir/striped_dir/b
12860         touch $DIR/$tdir/striped_dir/c
12861
12862         mkdir $DIR/$tdir/striped_dir/dir_a
12863         mkdir $DIR/$tdir/striped_dir/dir_b
12864         mkdir $DIR/$tdir/striped_dir/dir_c
12865
12866         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12867                 error "set striped dir under striped dir error"
12868
12869         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12870                 error "set striped dir under striped dir error"
12871
12872         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12873                 error "set striped dir under striped dir error"
12874
12875         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12876                 error "rename file under striped dir should fail"
12877
12878         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12879                 error "rename dir under striped dir should fail"
12880
12881         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12882                 error "rename dir under different stripes should fail"
12883
12884         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12885                 error "rename file under striped dir should succeed"
12886
12887         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12888                 error "rename dir under striped dir should succeed"
12889
12890         rm -rf $DIR/$tdir
12891 }
12892 run_test 300e "check rename under striped directory"
12893
12894 test_300f() {
12895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12896         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12897         local stripe_count
12898         local file
12899
12900         rm -rf $DIR/$tdir
12901         mkdir -p $DIR/$tdir
12902
12903         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12904                 error "set striped dir error"
12905
12906         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12907                 error "set striped dir error"
12908
12909         touch $DIR/$tdir/striped_dir/a
12910         mkdir $DIR/$tdir/striped_dir/dir_a
12911         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12912                 error "create striped dir under striped dir fails"
12913
12914         touch $DIR/$tdir/striped_dir1/b
12915         mkdir $DIR/$tdir/striped_dir1/dir_b
12916         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12917                 error "create striped dir under striped dir fails"
12918
12919         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12920                 error "rename file under different striped dir should fail"
12921
12922         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12923                 error "rename dir under different striped dir should fail"
12924
12925         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12926                 error "rename striped dir under diff striped dir should fail"
12927
12928         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12929                 error "rename file under diff striped dirs fails"
12930
12931         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12932                 error "rename dir under diff striped dirs fails"
12933
12934         rm -rf $DIR/$tdir
12935 }
12936 run_test 300f "check rename cross striped directory"
12937
12938 test_300g() {
12939         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12940         local stripe_count
12941         local dir
12942
12943         mkdir $DIR/$tdir
12944         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
12945                                         $DIR/$tdir/striped_dir ||
12946                 error "set striped dir error"
12947
12948         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12949                 error "set default stripe on striped dir error"
12950
12951         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
12952         [ $stripe_count -eq $MDSCOUNT ] ||
12953                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
12954
12955         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12956
12957         for dir in $(find $DIR/$tdir/striped_dir/*); do
12958                 stripe_count=$($LFS getdirstripe -c $dir)
12959                 [ $stripe_count -eq $MDSCOUNT ] ||
12960                         error "expect $MDSCOUNT get $stripe_count for $dir"
12961         done
12962
12963         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
12964         #change default stripe count to 2
12965         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
12966                 error "set default stripe on striped dir error"
12967
12968         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12969
12970         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
12971
12972         #change default stripe count to 1
12973         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
12974                 error "set default stripe on striped dir error"
12975
12976         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12977         for dir in $(find $DIR/$tdir/striped_dir/*); do
12978                 stripe_count=$($LFS getdirstripe -c $dir)
12979                 [ $stripe_count -eq 1 ] ||
12980                         error "expect 1 get $stripe_count for $dir"
12981         done
12982         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
12983 }
12984 run_test 300g "check default striped directory for striped directory"
12985
12986 test_300h() {
12987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12988         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12989         local stripe_count
12990         local file
12991
12992         mkdir $DIR/$tdir
12993
12994         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12995                 error "set striped dir error"
12996
12997         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
12998                 error "create files under striped dir failed"
12999
13000         # unfortunately, we need to umount to clear dir layout cache for now
13001         # once we fully implement dir layout, we can drop this
13002         umount_client $MOUNT || error "umount failed"
13003         mount_client $MOUNT || error "mount failed"
13004
13005         #set the stripe to be unknown hash type
13006         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13007         $LCTL set_param fail_loc=0x1901
13008         for ((i = 0; i < 10; i++)); do
13009                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13010                         error "stat f-$i failed"
13011                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13012         done
13013
13014         touch $DIR/$tdir/striped_dir/f0 &&
13015                 error "create under striped dir with unknown hash should fail"
13016
13017         $LCTL set_param fail_loc=0
13018
13019         umount_client $MOUNT || error "umount failed"
13020         mount_client $MOUNT || error "mount failed"
13021
13022         return 0
13023 }
13024 run_test 300h "client handle unknown hash type striped directory"
13025
13026 test_400a() { # LU-1606, was conf-sanity test_74
13027         local extra_flags=''
13028         local out=$TMP/$tfile
13029         local prefix=/usr/include/lustre
13030         local prog
13031
13032         if ! which $CC > /dev/null 2>&1; then
13033                 skip_env "$CC is not installed"
13034                 return 0
13035         fi
13036
13037         if ! [[ -d $prefix ]]; then
13038                 # Assume we're running in tree and fixup the include path.
13039                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13040                 extra_flags+=" -I$LUSTRE/include"
13041                 extra_flags+=" -L$LUSTRE/utils"
13042         fi
13043
13044         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13045                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13046                         error "client api broken"
13047         done
13048 }
13049 run_test 400a "Lustre client api program can compile and link"
13050
13051 test_400b() { # LU-1606, LU-5011
13052         local header
13053         local out=$TMP/$tfile
13054         local prefix=/usr/include/lustre
13055
13056         # We use a hard coded prefix so that this test will not fail
13057         # when run in tree. There are headers in lustre/include/lustre/
13058         # that are not packaged (like lustre_idl.h) and have more
13059         # complicated include dependencies (like config.h and lnet/types.h).
13060         # Since this test about correct packaging we just skip them when
13061         # they don't exist (see below) rather than try to fixup cppflags.
13062
13063         if ! which $CC > /dev/null 2>&1; then
13064                 skip_env "$CC is not installed"
13065                 return 0
13066         fi
13067
13068         for header in $prefix/*.h; do
13069                 if ! [[ -f "$header" ]]; then
13070                         continue
13071                 fi
13072
13073                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13074                         continue # liblustreapi.h is deprecated.
13075                 fi
13076
13077                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13078                         error "cannot compile '$header'"
13079         done
13080 }
13081 run_test 400b "packaged headers can be compiled"
13082
13083 #
13084 # tests that do cleanup/setup should be run at the end
13085 #
13086
13087 test_900() {
13088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13089         local ls
13090         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13091         $LCTL set_param fail_loc=0x903
13092
13093         cancel_lru_locks MGC
13094
13095         FAIL_ON_ERROR=true cleanup
13096         FAIL_ON_ERROR=true setup
13097 }
13098 run_test 900 "umount should not race with any mgc requeue thread"
13099
13100 complete $SECONDS
13101 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13102 check_and_cleanup_lustre
13103 if [ "$I_MOUNTED" != "yes" ]; then
13104         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13105 fi
13106 exit_status